From 238244337283a7f888cf49a1e56b809c22466a63 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 8 Aug 2019 09:59:52 -0700 Subject: Fixed a bug in the BlobBuilder. Appending wasn't working correctly. --- src/unit/blobbuilder.unit | 13 +++++++++++++ src/unstable/blobbuilder.cpp | 37 +++++++++++++++++++++++++++++++++++++ src/unstable/blobbuilder.h | 6 +++++- 3 files changed, 55 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/unit/blobbuilder.unit b/src/unit/blobbuilder.unit index 84bf549..7f3fb93 100644 --- a/src/unit/blobbuilder.unit +++ b/src/unit/blobbuilder.unit @@ -17,6 +17,7 @@ suite BlobBuilder { test append { + /* Bu::BlobBuilder a; a.append("a"); a.append("bc"); @@ -24,5 +25,17 @@ suite BlobBuilder Bu::println(">%1<\n\n").arg( a.getBlob() ); a.append("abcdef"); Bu::println(">%1<\n\n").arg( a.getBlob() ); + */ + } + + test appendChar + { + Bu::BlobBuilder a; + for( int j = 0; j < 20; j++ ) + { + a += 'A'; + } + Bu::println("%1").arg( a.getBlob() ); + unitTest( a.getBlob() == "AAAAAAAAAAAAAAAAAAAA" ); } } diff --git a/src/unstable/blobbuilder.cpp b/src/unstable/blobbuilder.cpp index 497a1a1..43c0779 100644 --- a/src/unstable/blobbuilder.cpp +++ b/src/unstable/blobbuilder.cpp @@ -7,6 +7,7 @@ #include "bu/blobbuilder.h" #include "bu/blob.h" +#include "bu/exceptionbase.h" #define PAGE_SIZE 8 @@ -132,6 +133,7 @@ void Bu::BlobBuilderCore::append( const char *pSrc, int32_t iLength ) if( iLength > 0 ) { pLast->pNext = new Chunk( pSrc, iLength ); + pLast = pLast->pNext; } } @@ -214,6 +216,21 @@ void Bu::BlobBuilderCore::copyTo( void *pDestRaw, int32_t iLength ) const } } +char Bu::BlobBuilderCore::getAt( int32_t iIndex ) const +{ + if( iIndex < 0 || iIndex >= iLength ) + throw Bu::ExceptionBase("Requseted index is out of range."); + + Chunk *pCur = pFirst; + while( iIndex >= pCur->iLength ) + { + iIndex -= pCur->iLength; + pCur = pCur->pNext; + } + + return pCur->pData[iIndex]; +} + ////// // BlobBuilder // @@ -245,6 +262,7 @@ void Bu::BlobBuilder::set( const char *pSrc, int32_t iLength ) void Bu::BlobBuilder::set( const char *pSrc ) { + _hardCopy(); set( pSrc, strlen( pSrc ) ); } @@ -262,6 +280,7 @@ void Bu::BlobBuilder::append( const char *pSrc, int32_t iLength ) void Bu::BlobBuilder::append( const char *pSrc ) { + _hardCopy(); append( pSrc, strlen( pSrc ) ); } @@ -279,6 +298,7 @@ void Bu::BlobBuilder::prepend( const char *pSrc, int32_t iLength ) void Bu::BlobBuilder::prepend( const char *pSrc ) { + _hardCopy(); prepend( pSrc, strlen( pSrc ) ); } @@ -297,6 +317,7 @@ void Bu::BlobBuilder::insert( int32_t iBefore, const char *pSrc, void Bu::BlobBuilder::insert( int32_t iBefore, const char *pSrc ) { + _hardCopy(); insert( iBefore, pSrc, strlen( pSrc ) ); } @@ -321,6 +342,11 @@ void Bu::BlobBuilder::copyTo( void *pDestRaw, int32_t iDestSize ) const core->copyTo( pDestRaw, iDestSize ); } +char Bu::BlobBuilder::operator[]( int32_t iIndex ) const +{ + return core->getAt( iIndex ); +} + Bu::BlobBuilder &Bu::BlobBuilder::operator=( const Blob &rSrc ) { set( rSrc ); @@ -333,6 +359,12 @@ Bu::BlobBuilder &Bu::BlobBuilder::operator=( const char *pSrc ) return *this; } +Bu::BlobBuilder &Bu::BlobBuilder::operator=( const char chr ) +{ + set( &chr, 1 ); + return *this; +} + Bu::BlobBuilder &Bu::BlobBuilder::operator+=( const Blob &rSrc ) { append( rSrc ); @@ -345,4 +377,9 @@ Bu::BlobBuilder &Bu::BlobBuilder::operator+=( const char *pSrc ) return *this; } +Bu::BlobBuilder &Bu::BlobBuilder::operator+=( const char chr ) +{ + append( &chr, 1 ); + return *this; +} diff --git a/src/unstable/blobbuilder.h b/src/unstable/blobbuilder.h index 744212a..c343a4f 100644 --- a/src/unstable/blobbuilder.h +++ b/src/unstable/blobbuilder.h @@ -53,7 +53,8 @@ namespace Bu void prepend( const char *pSrc, int32_t iLength ); void insert( int32_t iBefore, const char *pSrc, int32_t iLength ); void set( const char *pSrc, int32_t iLength ); - void copyTo( void *pDestRaw, int32_t iLength ) const; + void copyTo( void *pDestRaw, int32_t iLength ) const; + char getAt( int32_t iIndex ) const; Chunk *pFirst; Chunk *pLast; @@ -110,11 +111,14 @@ namespace Bu int32_t getSize() const; Blob getBlob() const; void copyTo( void *pDestRaw, int32_t iDestSize ) const; + char operator[]( int32_t iIndex ) const; BlobBuilder &operator=( const Blob &rSrc ); BlobBuilder &operator=( const char *pSrc ); + BlobBuilder &operator=( const char chr ); BlobBuilder &operator+=( const Blob &rSrc ); BlobBuilder &operator+=( const char *pSrc ); + BlobBuilder &operator+=( const char chr ); private: }; }; -- cgit v1.2.3