From 658b5c946e49d72266377750d6b96107113f7677 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Mon, 10 Jul 2023 11:54:00 -0700 Subject: String is now not shared. --- src/stable/string.cpp | 58 +++++++++++++++++++++++++-------------------------- src/stable/string.h | 12 ++++++----- 2 files changed, 36 insertions(+), 34 deletions(-) (limited to 'src/stable') diff --git a/src/stable/string.cpp b/src/stable/string.cpp index 98b78bd..3cad22b 100644 --- a/src/stable/string.cpp +++ b/src/stable/string.cpp @@ -128,59 +128,69 @@ void Bu::StringCore::prependChunk( Bu::StringCore::Chunk *pNewChunk ) nLength += pNewChunk->nLength; } -Bu::String::String() +Bu::String::String() : + core( new StringCore() ) { } -Bu::String::String( const char *pData ) +Bu::String::String( const char *pData ) : + core( new StringCore() ) { append( pData ); } -Bu::String::String( const char *pData, long nLength ) +Bu::String::String( const char *pData, long nLength ) : + core( new StringCore() ) { append( pData, nLength ); } Bu::String::String( const Bu::String &rSrc ) : - Bu::SharedCore( rSrc ) + core( new StringCore( *rSrc.core ) ) { } -Bu::String::String( const Bu::String &rSrc, long nLength ) +Bu::String::String( const Bu::String &rSrc, long nLength ) : + core( new StringCore() ) { append( rSrc, nLength ); } -Bu::String::String( const Bu::String &rSrc, long nStart, long nLength ) +Bu::String::String( const Bu::String &rSrc, long nStart, long nLength ) : + core( new StringCore() ) { append( rSrc, nStart, nLength ); } -Bu::String::String( long nSize ) +Bu::String::String( long nSize ) : + core( new StringCore() ) { core->pFirst = core->pLast = core->newChunk( nSize ); core->nLength = nSize; } -Bu::String::String( const class Bu::Blob &rSrc ) +Bu::String::String( const class Bu::Blob &rSrc ) : + core( new StringCore() ) { append( rSrc.getData(), rSrc.getSize() ); } -Bu::String::String( const Bu::String::const_iterator &s ) +Bu::String::String( const Bu::String::const_iterator &s ) : + core( new StringCore() ) { append( s ); } Bu::String::String( const Bu::String::const_iterator &s, - const Bu::String::const_iterator &e ) + const Bu::String::const_iterator &e ) : + core( new StringCore() ) { append( s, e ); } Bu::String::~String() { + delete core; } void Bu::String::append( const char *pData ) @@ -205,8 +215,6 @@ void Bu::String::append( const char *pData, long nStart, long nLen ) pData += nStart; - _hardCopy(); - if( core->pLast && core->pLast->nLength+1 < nMinSize ) { int nAmnt = nMinSize - core->pLast->nLength; @@ -234,7 +242,6 @@ void Bu::String::append( const char *pData, long nStart, long nLen ) void Bu::String::append( const char &cData ) { - _hardCopy(); if( core->pLast && core->pLast->nLength+1 < nMinSize ) { core->pLast->pData[core->pLast->nLength] = cData; @@ -273,7 +280,6 @@ void Bu::String::append( const const_iterator &s ) Chunk *pNew = core->newChunk( pSrc->nLength-s.iPos ); memcpy( pNew->pData, pSrc->pData+s.iPos, pSrc->nLength-s.iPos ); - _hardCopy(); core->appendChunk( pNew ); while( (pSrc = pSrc->pNext) ) @@ -296,7 +302,6 @@ void Bu::String::append( const const_iterator &s, const const_iterator &e ) append( s ); return; } - _hardCopy(); if( s.pChunk == e.pChunk ) { // Simple case, they're the same chunk @@ -333,7 +338,6 @@ void Bu::String::prepend( const char *pData ) if( pData == NULL ) return; - _hardCopy(); long nLen; for( nLen = 0; pData[nLen] != (char)0; nLen++ ) { } @@ -349,7 +353,6 @@ void Bu::String::prepend( const char *pData, long nLen ) memcpy( pNew->pData, pData, nLen ); - _hardCopy(); core->prependChunk( pNew ); } @@ -374,7 +377,6 @@ void Bu::String::insert( long nPos, const char *pData, long nLen ) { // If we're going to flatten anyway, might as well for everyone flatten(); - _hardCopy(); Chunk *p1 = core->newChunk( nPos ); Chunk *p2 = core->newChunk( nLen ); Chunk *p3 = core->newChunk( core->nLength-nPos ); @@ -401,7 +403,6 @@ void Bu::String::insert( long nPos, const String &str ) else { flatten(); - _hardCopy(); Chunk *p1 = core->newChunk( nPos ); Chunk *p3 = core->newChunk( core->nLength-nPos ); memcpy( p1->pData, core->pFirst->pData, nPos ); @@ -430,7 +431,6 @@ void Bu::String::remove( long nPos, long nLen ) if( nLen > core->nLength-nPos ) nLen = core->nLength-nPos; flatten(); - _hardCopy(); memmove( core->pFirst->pData+nPos, core->pFirst->pData+nPos+nLen, core->nLength-nPos-nLen+1 ); core->nLength -= nLen; core->pFirst->nLength -= nLen; @@ -438,10 +438,18 @@ void Bu::String::remove( long nPos, long nLen ) void Bu::String::clear() { - _hardCopy(); core->clear(); } +const Bu::String &Bu::String::clone() const +{ + return *this; +} + +void Bu::String::unshare() +{ +} + Bu::String Bu::String::replace( const Bu::String &fnd, const Bu::String &rep ) const { @@ -473,7 +481,6 @@ void Bu::String::resize( long nNewSize ) nNewSize = 0; flatten(); - _hardCopy(); // TODO: This is bad @@ -501,7 +508,6 @@ char *Bu::String::getStr() return (char *)""; flatten(); - _hardCopy(); core->pFirst->pData[core->nLength] = (char)0; return core->pFirst->pData; } @@ -631,7 +637,6 @@ Bu::String &Bu::String::operator+=( const Bu::String::const_iterator &i ) Bu::String &Bu::String::operator+=( const char cData ) { - _hardCopy(); if( core->pLast && core->pLast->nLength+1 < nMinSize ) { core->pLast->pData[core->pLast->nLength] = cData; @@ -732,7 +737,6 @@ void Bu::String::set( const_iterator s, const_iterator e ) void Bu::String::setSize( long iSize ) { - _hardCopy(); core->clear(); core->appendChunk( core->newChunk( iSize ) ); } @@ -867,7 +871,6 @@ char &Bu::String::operator[]( long nIndex ) if( nIndex < 0 || nIndex >= core->nLength ) throw Bu::ExceptionBase("Index out of range."); flatten(); - _hardCopy(); return core->pFirst->pData[nIndex]; } @@ -958,7 +961,6 @@ Bu::String Bu::String::toLower() const Bu::String sRet = *this; sRet.flatten(); - sRet._hardCopy(); for( long j = 0; j < sRet.core->nLength; j++ ) { @@ -975,7 +977,6 @@ Bu::String Bu::String::toUpper() const Bu::String sRet = *this; sRet.flatten(); - sRet._hardCopy(); for( long j = 0; j < sRet.core->nLength; j++ ) { @@ -1149,7 +1150,6 @@ void Bu::String::trimFront( long nAmnt ) flatten(); Chunk *pNew = core->newChunk( nNewLen ); memcpy( pNew->pData, core->pFirst->pData+nAmnt, nNewLen ); - _hardCopy(); core->clear(); core->appendChunk( pNew ); } diff --git a/src/stable/string.h b/src/stable/string.h index 1fc5496..b174031 100644 --- a/src/stable/string.h +++ b/src/stable/string.h @@ -58,12 +58,8 @@ namespace Bu /** */ - class String : public SharedCore + class String { - protected: - using SharedCore::core; - using SharedCore::_hardCopy; - private: typedef StringCore::Chunk Chunk; @@ -696,6 +692,9 @@ namespace Bu */ void clear(); + const String &clone() const; + void unshare(); + String replace( const String &fnd, const String &rep ) const; /** @@ -1054,6 +1053,9 @@ namespace Bu { return FormatProxy( *this, pEndAction ); } + + protected: + StringCore *core; }; template String operator+( const T *pLeft, const String &rRight ) -- cgit v1.2.3