From 5aec71241c874a2249c14025a7df1eddc1c14654 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Wed, 24 Sep 2008 03:45:46 +0000 Subject: Added a getSubStr function to Bu::FString, and more tests to the fstring unit test. --- src/fstring.h | 18 ++++++++++++++++++ src/unit/fstring.cpp | 21 +++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/src/fstring.h b/src/fstring.h index b4cae3a..620b312 100644 --- a/src/fstring.h +++ b/src/fstring.h @@ -390,6 +390,24 @@ namespace Bu return pFirst->pData; } + MyType getSubStr( long iStart, long iSize=-1 ) const + { + if( iStart < 0 ) + iStart = 0; + if( iStart >= nLength ) + return ""; + if( iSize < 0 ) + iSize = nLength; + if( iStart+iSize > nLength ) + iSize = nLength-iStart; + if( iSize == 0 ) + return ""; + + flatten(); + MyType ret( pFirst->pData+iStart, iSize ); + return ret; + } + /** * (std::string compatability) Get a pointer to the string array. *@returns (chr *) The string data. diff --git a/src/unit/fstring.cpp b/src/unit/fstring.cpp index ea3342a..7be03a7 100644 --- a/src/unit/fstring.cpp +++ b/src/unit/fstring.cpp @@ -24,6 +24,8 @@ public: addTest( Unit::add2 ); addTest( Unit::add3 ); addTest( Unit::add4 ); + addTest( Unit::add5 ); + addTest( Unit::subStr1 ); } virtual ~Unit() @@ -128,6 +130,25 @@ public: unitTest( c == "hi there, yeah!" ); } + + void add5() + { + Bu::FString b; + Bu::FString c = "sup?"; + b += "hey, " + c; + + unitTest( b == "hey, sup?" ); + } + + void subStr1() + { + Bu::FString a("abcdefghijklmnop"); + unitTest( a.getSubStr( 5, 3 ) == "fgh" ); + unitTest( a.getSubStr( 10 ) == "klmnop" ); + unitTest( a.getSubStr( 40 ) == "" ); + unitTest( a.getSubStr( -10 ) == "abcdefghijklmnop" ); + unitTest( a.getSubStr( -15, 4 ) == "abcd" ); + } }; int main( int argc, char *argv[] ) -- cgit v1.2.3