From 3a27454dca4a16d021a4d418f0725adccc5baabb Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Sat, 25 May 2019 20:34:29 -0700 Subject: Just about everything that Blob needs is in. --- src/unit/blob.unit | 63 ++++++++++++++++++++ src/unstable/blob.cpp | 161 +++++++++++++++++++++++++++++++++++--------------- src/unstable/blob.h | 39 +++++++----- 3 files changed, 203 insertions(+), 60 deletions(-) (limited to 'src') diff --git a/src/unit/blob.unit b/src/unit/blob.unit index 645052b..6f98cfc 100644 --- a/src/unit/blob.unit +++ b/src/unit/blob.unit @@ -26,6 +26,23 @@ suite Blob unitTest( !strcmp( b.getData(), "New string") ); } + test empty + { + Bu::Blob n; + Bu::Blob e(""); + + unitTest( n.isEmpty() == false ); + unitTest( n.isNull() == true ); + unitTest( n.isNullOrEmpty() == true ); + + unitTest( e.isEmpty() == true ); + unitTest( e.isNull() == false ); + unitTest( e.isNullOrEmpty() == true ); + + unitTestCatch( n[0], Bu::ExceptionIndexOutOfBounds ); + unitTestCatch( e[0], Bu::ExceptionIndexOutOfBounds ); + } + test index { Bu::Blob a("hello there"); @@ -87,10 +104,56 @@ suite Blob test iterator { + const char *sDat = "abcdefghijklmnopqrstuvwxyz"; + Bu::Blob bDat( sDat ); + + Bu::Blob::iterator i2; + const char *sCmp = sDat; + for( Bu::Blob::iterator i = bDat.begin(); i; i++, sCmp++ ) + { + unitTest( *i == *sCmp ); + if( *i == 'k' ) + i2 = i; + } + + i2++; + unitTest( *i2 == 'l' ); + unitTest( *(i2 + 3) == 'o' ); + unitTest( Bu::Blob( i2 ) == "lmnopqrstuvwxyz" ); + unitTest( Bu::Blob( i2, i2+5 ) == "lmnop" ); + + sCmp--; + for( Bu::Blob::iterator i = bDat.rbegin(); i; i++, sCmp-- ) + { + unitTest( *i == *sCmp ); + } } test const_iterator { + const char *sDat = "abcdefghijklmnopqrstuvwxyz"; + Bu::Blob bDat( sDat ); + + Bu::Blob::const_iterator i2; + const char *sCmp = sDat; + for( Bu::Blob::const_iterator i = bDat.begin(); i; i++, sCmp++ ) + { + unitTest( *i == *sCmp ); + if( *i == 'k' ) + i2 = i; + } + + i2++; + unitTest( *i2 == 'l' ); + unitTest( *(i2 + 3) == 'o' ); + unitTest( Bu::Blob( i2 ) == "lmnopqrstuvwxyz" ); + unitTest( Bu::Blob( i2, i2+5 ) == "lmnop" ); + + sCmp--; + for( Bu::Blob::const_iterator i = bDat.rbegin(); i; i++, sCmp-- ) + { + unitTest( *i == *sCmp ); + } } test sort diff --git a/src/unstable/blob.cpp b/src/unstable/blob.cpp index f8cf1b0..3f7d9d9 100644 --- a/src/unstable/blob.cpp +++ b/src/unstable/blob.cpp @@ -34,14 +34,48 @@ Bu::Blob::Blob( const char *pSrc ) : memcpy( pData, pSrc, iSize+1 ); } -Bu::Blob::Blob( const void *pSrc, int32_t iSize ) +Bu::Blob::Blob( const void *pSrc, int32_t iSize ) : + pData( 0 ), + iSize( iSize ) { - this->iSize = iSize; pData = new char[iSize+1]; memcpy( pData, pSrc, iSize ); pData[iSize] = '\0'; } +Bu::Blob::Blob( const Bu::Blob::const_iterator &iStart ) : + pData( 0 ), + iSize( -1 ) +{ + if( !iStart ) + return; + + iSize = iStart.pBlob->iSize - iStart.iIndex; + pData = new char[iSize+1]; + memcpy( pData, iStart.pBlob->pData+iStart.iIndex, iSize ); +} + +Bu::Blob::Blob( const Bu::Blob::const_iterator &iStart, + const Bu::Blob::const_iterator &iEnd ) : + pData( 0 ), + iSize( -1 ) +{ + if( !iStart ) + return; + + if( iEnd ) + { + iSize = iEnd.iIndex - iStart.iIndex; + } + else + { + iSize = iStart.pBlob->iSize - iStart.iIndex; + } + + pData = new char[iSize+1]; + memcpy( pData, iStart.pBlob->pData+iStart.iIndex, iSize ); +} + Bu::Blob::~Blob() { delete[] pData; @@ -316,6 +350,45 @@ bool Bu::Blob::operator>=( const char *pRhs ) const return true; } +Bu::Blob::iterator Bu::Blob::begin() +{ + return iterator( this, true ); +} + +Bu::Blob::const_iterator Bu::Blob::begin() const +{ + return const_iterator( this, true ); +} + +Bu::Blob::iterator Bu::Blob::end() +{ + return iterator(); +} + +Bu::Blob::const_iterator Bu::Blob::end() const +{ + return const_iterator(); +} + +Bu::Blob::iterator Bu::Blob::rbegin() +{ + return iterator( this, false ); +} + +Bu::Blob::const_iterator Bu::Blob::rbegin() const +{ + return const_iterator( this, false ); +} + +Bu::Blob::iterator Bu::Blob::rend() +{ + return iterator(); +} + +Bu::Blob::const_iterator Bu::Blob::rend() const +{ + return const_iterator(); +} ///// // Iterators @@ -328,6 +401,13 @@ Bu::Blob::iterator::iterator( Bu::Blob *pBlob, bool bForward ) : { } +Bu::Blob::iterator::iterator( Bu::Blob *pBlob, int32_t iIndex, bool bForward ) : + pBlob( pBlob ), + iIndex( iIndex ), + bForward( bForward ) +{ +} + Bu::Blob::iterator::iterator() : pBlob( NULL ), iIndex( -1 ), @@ -366,7 +446,7 @@ char &Bu::Blob::iterator::operator *() return pBlob->pData[iIndex]; } -Bu::Blob::iterator &Bu::Blob::iterator::operator++( int ) +Bu::Blob::iterator &Bu::Blob::iterator::operator++( int32_t ) { if( bForward ) ++iIndex; @@ -386,7 +466,7 @@ Bu::Blob::iterator &Bu::Blob::iterator::operator++() return *this; } -Bu::Blob::iterator &Bu::Blob::iterator::operator--( int ) +Bu::Blob::iterator &Bu::Blob::iterator::operator--( int32_t ) { if( bForward ) --iIndex; @@ -406,6 +486,16 @@ Bu::Blob::iterator &Bu::Blob::iterator::operator--() return *this; } +Bu::Blob::iterator Bu::Blob::iterator::operator+( int32_t iDelta ) const +{ + return iterator( pBlob, iIndex + iDelta, bForward ); +} + +Bu::Blob::iterator Bu::Blob::iterator::operator-( int32_t iDelta ) const +{ + return iterator( pBlob, iIndex - iDelta, bForward ); +} + bool Bu::Blob::iterator::operator==( const Bu::Blob::iterator &rRhs ) { if( isValid() == false && rRhs.isValid() == false ) @@ -454,6 +544,13 @@ Bu::Blob::const_iterator::const_iterator( const Blob *pBlob, bool bForward ) : bForward( bForward ) { } +Bu::Blob::const_iterator::const_iterator( const Blob *pBlob, int32_t iIndex, + bool bForward ) : + pBlob( pBlob ), + iIndex( iIndex ), + bForward( bForward ) +{ +} Bu::Blob::const_iterator::const_iterator() : pBlob( NULL ), @@ -541,6 +638,18 @@ Bu::Blob::const_iterator &Bu::Blob::const_iterator::operator--() return *this; } +Bu::Blob::const_iterator Bu::Blob::const_iterator::operator+( int32_t iDelta ) + const +{ + return const_iterator( pBlob, iIndex + iDelta, bForward ); +} + +Bu::Blob::const_iterator Bu::Blob::const_iterator::operator-( int32_t iDelta ) + const +{ + return const_iterator( pBlob, iIndex - iDelta, bForward ); +} + bool Bu::Blob::const_iterator::operator==( const Bu::Blob::iterator &rRhs ) { if( isValid() == false && rRhs.isValid() == false ) @@ -595,50 +704,10 @@ Bu::Blob::const_iterator &Bu::Blob::const_iterator::operator=( return *this; } - -Bu::Blob::iterator Bu::Blob::begin() -{ - return iterator( this, true ); -} - -Bu::Blob::const_iterator Bu::Blob::begin() const -{ - return const_iterator( this, true ); -} - -Bu::Blob::iterator Bu::Blob::end() -{ - return iterator(); -} - -Bu::Blob::const_iterator Bu::Blob::end() const -{ - return const_iterator(); -} - -Bu::Blob::iterator Bu::Blob::rbegin() -{ - return iterator( this, false ); -} - -Bu::Blob::const_iterator Bu::Blob::rbegin() const -{ - return const_iterator( this, false ); -} - -Bu::Blob::iterator Bu::Blob::rend() -{ - return iterator(); -} - -Bu::Blob::const_iterator Bu::Blob::rend() const -{ - return const_iterator(); -} - -//// +///// // Helper functions // + template<> uint32_t Bu::__calcHashCode( const Bu::Blob &k ) { int32_t sz = k.getSize(); diff --git a/src/unstable/blob.h b/src/unstable/blob.h index 6ce0c67..daee5f9 100644 --- a/src/unstable/blob.h +++ b/src/unstable/blob.h @@ -24,11 +24,17 @@ namespace Bu */ class Blob { + public: + class iterator; + class const_iterator; + public: Blob(); Blob( const Blob &rSrc ); Blob( const char *pSrc ); Blob( const void *pSrc, int32_t iSize ); + Blob( const const_iterator &iStart ); + Blob( const const_iterator &iStart, const const_iterator &iEnd ); virtual ~Blob(); int32_t getSize() const; @@ -58,13 +64,22 @@ namespace Bu bool operator>=( const Blob &rRhs ) const; bool operator>=( const char *rRhs ) const; - class const_iterator; + iterator begin(); + const_iterator begin() const; + iterator end(); + const_iterator end() const; + iterator rbegin(); + const_iterator rbegin() const; + iterator rend(); + const_iterator rend() const; + class iterator { friend class Blob; friend class const_iterator; private: iterator( Blob *pBlob, bool bForward ); + iterator( Blob *pBlob, int32_t iIndex, bool bForward ); public: iterator(); @@ -74,10 +89,12 @@ namespace Bu operator bool() const; char &operator *(); - iterator &operator++( int ); + iterator &operator++( int32_t ); iterator &operator++(); - iterator &operator--( int ); + iterator &operator--( int32_t ); iterator &operator--(); + iterator operator+( int32_t iDelta ) const; + iterator operator-( int32_t iDelta ) const; bool operator==( const iterator &rRhs ); bool operator==( const const_iterator &rRhs ); bool operator!=( const iterator &rRhs ); @@ -97,6 +114,7 @@ namespace Bu friend class iterator; private: const_iterator( const Blob *pBlob, bool bForward ); + const_iterator( const Blob *pBlob, int32_t iIndex, bool bForward ); public: const_iterator(); @@ -107,10 +125,12 @@ namespace Bu operator bool() const; char operator *() const; - const_iterator &operator++( int ); + const_iterator &operator++( int32_t ); const_iterator &operator++(); - const_iterator &operator--( int ); + const_iterator &operator--( int32_t ); const_iterator &operator--(); + const_iterator operator+( int32_t iDelta ) const; + const_iterator operator-( int32_t iDelta ) const; bool operator==( const iterator &rRhs ); bool operator==( const const_iterator &rRhs ); bool operator!=( const iterator &rRhs ); @@ -125,15 +145,6 @@ namespace Bu bool bForward; }; - iterator begin(); - const_iterator begin() const; - iterator end(); - const_iterator end() const; - iterator rbegin(); - const_iterator rbegin() const; - iterator rend(); - const_iterator rend() const; - private: char *pData; int32_t iSize; -- cgit v1.2.3