From c9574d3f77081fb4a654d42c298d6ebf34abca51 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 23 Nov 2007 23:47:37 +0000 Subject: Bu::FString now has insert and remove functions, yay! --- src/fstring.h | 39 +++++++++++++++++++++++++++++++++++++++ src/unit/fstring.cpp | 30 ++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+) (limited to 'src') diff --git a/src/fstring.h b/src/fstring.h index 8cccd5c..8b512e2 100644 --- a/src/fstring.h +++ b/src/fstring.h @@ -226,6 +226,45 @@ namespace Bu prependChunk( pNew ); } + void insert( long nPos, const chr *pData, long nLen ) + { + if( nLen <= 0 ) + return; + flatten(); + if( nPos <= 0 ) + { + prepend( pData, nLen ); + } + else if( nPos >= nLength ) + { + append( pData, nLen ); + } + else + { + Chunk *p1 = newChunk( nPos ); + Chunk *p2 = newChunk( nLen ); + Chunk *p3 = newChunk( nLength-nPos ); + cpy( p1->pData, pFirst->pData, nPos ); + cpy( p2->pData, pData, nLen ); + cpy( p3->pData, pFirst->pData+nPos, nLength-nPos ); + clear(); + appendChunk( p1 ); + appendChunk( p2 ); + appendChunk( p3 ); + } + } + + void remove( long nPos, long nLen ) + { + if( nLen <= 0 || nPos < 0 || nPos >= nLength ) + return; + if( nLen >= nLength-nPos ) + nLen = nLength-nPos-1; + flatten(); + cpy( pFirst->pData+nPos, pFirst->pData+nPos+nLen, nLen+1 ); + nLength -= nPos; + } + /** *@todo void prepend( const chr &cData ) */ diff --git a/src/unit/fstring.cpp b/src/unit/fstring.cpp index 69c7e0a..03df9c3 100644 --- a/src/unit/fstring.cpp +++ b/src/unit/fstring.cpp @@ -18,6 +18,8 @@ public: addTest( Unit::compare2 ); addTest( Unit::appendSingle ); addTest( Unit::shared1 ); + addTest( Unit::insert ); + addTest( Unit::remove ); } virtual ~Unit() @@ -57,6 +59,34 @@ public: a = b; unitTest( a.getStr() == b.getStr() ); } + + void insert() + { + Bu::FString a("abcd"); + a.insert( 2, "-!-", 3 ); + unitTest( a == "ab-!-cd" ); + + a.insert( 0, "!!", 2 ); + unitTest( a == "!!ab-!-cd" ); + + a.insert( -10, "789", 3 ); + unitTest( a == "789!!ab-!-cd" ); + + a.insert( 12, "89", 2 ); + unitTest( a == "789!!ab-!-cd89" ); + + a.insert( 1203, "12", 2 ); + unitTest( a == "789!!ab-!-cd8912" ); + } + + void remove() + { + Bu::FString a("abHEYcd"); + a.remove( 2, 3 ); + unitTest( a == "abcd" ); + a.remove( 2, 5 ); + unitTest( a == "ab" ); + } }; int main( int argc, char *argv[] ) -- cgit v1.2.3