String builder - truncate() method

This commit is contained in:
Emmanuel BENOîT 2017-11-29 09:24:51 +01:00
parent 5db261909a
commit 86a3fe34b4
3 changed files with 36 additions and 0 deletions

View file

@ -391,6 +391,7 @@ class T_StringBuilder
T_StringBuilder& ensureCapacity( uint32_t minCap );
T_StringBuilder& clear( );
T_StringBuilder& free( );
T_StringBuilder& truncate( uint32_t maxLength ) noexcept;
// ---------------------------------------------------------------------

View file

@ -458,6 +458,16 @@ inline T_StringBuilder& T_StringBuilder::clear( )
return *this;
}
inline T_StringBuilder& T_StringBuilder::truncate(
const uint32_t maxLength ) noexcept
{
if ( maxLength < length_ ) {
size_ = UTF8GetMemoryOffset( data_ , maxLength );
length_ = maxLength;
}
return *this;
}
/*----------------------------------------------------------------------------*/
inline bool T_StringBuilder::operator== ( T_StringBuilder const& other ) const

View file

@ -29,6 +29,9 @@ class StringsBuilderTest : public CppUnit::TestFixture
CPPUNIT_TEST( testClear );
CPPUNIT_TEST( testFree );
CPPUNIT_TEST( testTruncate );
CPPUNIT_TEST( testTruncateLonger );
CPPUNIT_TEST( testAppendOther );
CPPUNIT_TEST( testAppendSwap );
CPPUNIT_TEST( testAppendStr );
@ -121,6 +124,9 @@ public:
void testClear( );
void testFree( );
void testTruncate( );
void testTruncateLonger( );
void testAppendOther( );
void testAppendSwap( );
void testAppendStr( );
@ -425,6 +431,25 @@ void StringsBuilderTest::testFree( )
/*----------------------------------------------------------------------------*/
void StringsBuilderTest::testTruncate( )
{
T_StringBuilder sb( "this is\uf1bb a test" );
sb.truncate( 8 );
CPPUNIT_ASSERT_EQUAL( uint32_t( 8 ) , sb.length( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 10 ) , sb.size( ) );
}
void StringsBuilderTest::testTruncateLonger( )
{
T_StringBuilder sb( "this is\uf1bb a test" );
sb.truncate( 20 );
printf( "%d\n" , sb.size( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 15 ) , sb.length( ) );
CPPUNIT_ASSERT_EQUAL( uint32_t( 17 ) , sb.size( ) );
}
/*----------------------------------------------------------------------------*/
void StringsBuilderTest::testAppendOther( )
{
T_StringBuilder sb1( "this is " ) , sb2( "a test!" );