From fc96db275f65a9d5adc4a40758f50297fc5bdbf0 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Fri, 10 Apr 2009 06:23:07 +0000 Subject: Added some new goodness to the fbasicstring, fixing some inconsistancies and adding some more helpers. Hopefully this won't affect anything, but if it complains about any functions not working the way they used to, see if they're returning an int or an iterator. I made several functions handle iterators instead of ints, the int versions have an "Idx" suffix added now. I'm trying to switch entirely to iterators to reduce flattening and increase performance and stability. Also...something must have changed in the cache code... --- src/cache.h | 7 +++- src/fbasicstring.h | 98 ++++++++++++++++++++++++++++++++++++++++++++++++++- src/tests/cache.cpp | 5 +-- src/unit/fstring.unit | 9 +++-- 4 files changed, 110 insertions(+), 9 deletions(-) diff --git a/src/cache.h b/src/cache.h index f36c29b..2b93479 100644 --- a/src/cache.h +++ b/src/cache.h @@ -90,7 +90,12 @@ namespace Bu return pData; } - bool isLoaded() const + bool isValid() const + { + return pCache != NULL; + } + + bool isBound() const { return pData != NULL; } diff --git a/src/fbasicstring.h b/src/fbasicstring.h index a532d69..853625c 100644 --- a/src/fbasicstring.h +++ b/src/fbasicstring.h @@ -947,7 +947,7 @@ namespace Bu return pFirst->pData; } - MyType getSubStr( long iStart, long iSize=-1 ) const + MyType getSubStrIdx( long iStart, long iSize=-1 ) const { if( iStart < 0 ) iStart = 0; @@ -965,6 +965,50 @@ namespace Bu return ret; } + MyType getSubStr( const_iterator iBegin, + const_iterator iEnd=typename MyType::const_iterator() ) const + { + if( !iBegin.isValid() ) + return MyType(); + if( iBegin.pChunk == iEnd.pChunk ) + { + return MyType( iBegin.pChunk->pData+iBegin.iPos, + iEnd.iPos-iBegin.iPos ); + } + else if( !iEnd.isValid() ) + { + MyType ret; + ret.append( + iBegin.pChunk->pData+iBegin.iPos, + iBegin.pChunk->nLength-iBegin.iPos + ); + for( Chunk *pCur = iBegin.pChunk->pNext; + pCur; pCur = pCur->pNext ) + { + ret.append( pCur->pData, pCur->nLength ); + } + return ret; + } + else + { + MyType ret; + ret.append( + iBegin.pChunk->pData+iBegin.iPos, + iBegin.pChunk->nLength-iBegin.iPos + ); + for( Chunk *pCur = iBegin.pChunk->pNext; + pCur != iEnd.pChunk; pCur = pCur->pNext ) + { + ret.append( pCur->pData, pCur->nLength ); + } + ret.append( + iEnd.pChunk->pData, + iEnd.iPos + ); + return ret; + } + } + /** * (std::string compatability) Get a pointer to the string array. *@returns (chr *) The string data. @@ -1401,6 +1445,7 @@ namespace Bu const_iterator find( const chr cChar, const_iterator iStart=typename MyType::const_iterator() ) const { + if( !iStart ) iStart = begin(); for( ; iStart; iStart++ ) { if( cChar == *iStart ) @@ -1412,6 +1457,7 @@ namespace Bu const_iterator find( const chr *sText, int nLen, const_iterator iStart=typename MyType::const_iterator() ) const { + if( !iStart ) iStart = begin(); for( ; iStart; iStart++ ) { if( iStart.compare( sText, nLen ) ) @@ -1423,6 +1469,7 @@ namespace Bu const_iterator find( const MyType &rStr, const_iterator iStart=typename MyType::const_iterator() ) const { + if( !iStart ) iStart = begin(); for( ; iStart; iStart++ ) { if( iStart.compare( rStr ) ) @@ -1434,6 +1481,7 @@ namespace Bu const_iterator find( const MyType &rStr, int nLen, const_iterator iStart=typename MyType::const_iterator() ) const { + if( !iStart ) iStart = begin(); for( ; iStart; iStart++ ) { if( iStart.compare( rStr, nLen ) ) @@ -1442,6 +1490,54 @@ namespace Bu return end(); } + iterator find( const chr cChar, + const_iterator iStart=typename MyType::const_iterator() ) + { + if( !iStart ) iStart = begin(); + for( ; iStart; iStart++ ) + { + if( cChar == *iStart ) + return iterator( iStart.pChunk, iStart.iPos ); + } + return end(); + } + + iterator find( const chr *sText, int nLen, + const_iterator iStart=typename MyType::const_iterator() ) + { + if( !iStart ) iStart = begin(); + for( ; iStart; iStart++ ) + { + if( iStart.compare( sText, nLen ) ) + return iterator( iStart.pChunk, iStart.iPos ); + } + return end(); + } + + iterator find( const MyType &rStr, + const_iterator iStart=typename MyType::const_iterator() ) + { + if( !iStart ) iStart = begin(); + for( ; iStart; iStart++ ) + { + if( iStart.compare( rStr ) ) + return iterator( iStart.pChunk, iStart.iPos ); + } + return end(); + } + + iterator find( const MyType &rStr, int nLen, + const_iterator iStart=typename MyType::const_iterator() ) + { + if( !iStart ) iStart = begin(); + for( ; iStart; iStart++ ) + { + if( iStart.compare( rStr, nLen ) ) + return iterator( iStart.pChunk, iStart.iPos ); + } + return end(); + } + /** * Find the index of the first occurrance of cChar *@param sText (const chr *) The string to search for. diff --git a/src/tests/cache.cpp b/src/tests/cache.cpp index 8d95a9d..5d542ed 100644 --- a/src/tests/cache.cpp +++ b/src/tests/cache.cpp @@ -171,6 +171,7 @@ public: virtual bool shouldSync( Bob *, const long &, time_t ) { + return false; } private: @@ -224,9 +225,9 @@ int main( int argc, char *argv[] ) case 'l': { BobCache::Ptr pBob = cBob.getLazy( strtol( argv[2], NULL, 0 ) ); - printf("isLoaded: %s\n", pBob.isLoaded()?"yes":"no"); + printf("isBound: %s\n", pBob.isBound()?"yes":"no"); printf("Value = %d\n", pBob->getInt() ); - printf("isLoaded: %s\n", pBob.isLoaded()?"yes":"no"); + printf("isBound: %s\n", pBob.isBound()?"yes":"no"); } return 0; } diff --git a/src/unit/fstring.unit b/src/unit/fstring.unit index fbebd39..7314095 100644 --- a/src/unit/fstring.unit +++ b/src/unit/fstring.unit @@ -141,11 +141,10 @@ {%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" ); + Bu::FString::iterator i = a.find('f'); + unitTest( a.getSubStr( i, Bu::FString::iterator() ) == "fghijklmnop" ); + Bu::FString::iterator j = i.find('l'); + unitTest( a.getSubStr( i, j ) == "fghijk" ); } {%compareSub1} -- cgit v1.2.3