aboutsummaryrefslogtreecommitdiff
path: root/src/stable
diff options
context:
space:
mode:
Diffstat (limited to 'src/stable')
-rw-r--r--src/stable/string.cpp58
-rw-r--r--src/stable/string.h12
2 files changed, 36 insertions, 34 deletions
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 )
128 nLength += pNewChunk->nLength; 128 nLength += pNewChunk->nLength;
129} 129}
130 130
131Bu::String::String() 131Bu::String::String() :
132 core( new StringCore() )
132{ 133{
133} 134}
134 135
135Bu::String::String( const char *pData ) 136Bu::String::String( const char *pData ) :
137 core( new StringCore() )
136{ 138{
137 append( pData ); 139 append( pData );
138} 140}
139 141
140Bu::String::String( const char *pData, long nLength ) 142Bu::String::String( const char *pData, long nLength ) :
143 core( new StringCore() )
141{ 144{
142 append( pData, nLength ); 145 append( pData, nLength );
143} 146}
144 147
145Bu::String::String( const Bu::String &rSrc ) : 148Bu::String::String( const Bu::String &rSrc ) :
146 Bu::SharedCore<Bu::String, Bu::StringCore>( rSrc ) 149 core( new StringCore( *rSrc.core ) )
147{ 150{
148} 151}
149 152
150Bu::String::String( const Bu::String &rSrc, long nLength ) 153Bu::String::String( const Bu::String &rSrc, long nLength ) :
154 core( new StringCore() )
151{ 155{
152 append( rSrc, nLength ); 156 append( rSrc, nLength );
153} 157}
154 158
155Bu::String::String( const Bu::String &rSrc, long nStart, long nLength ) 159Bu::String::String( const Bu::String &rSrc, long nStart, long nLength ) :
160 core( new StringCore() )
156{ 161{
157 append( rSrc, nStart, nLength ); 162 append( rSrc, nStart, nLength );
158} 163}
159 164
160Bu::String::String( long nSize ) 165Bu::String::String( long nSize ) :
166 core( new StringCore() )
161{ 167{
162 core->pFirst = core->pLast = core->newChunk( nSize ); 168 core->pFirst = core->pLast = core->newChunk( nSize );
163 core->nLength = nSize; 169 core->nLength = nSize;
164} 170}
165 171
166Bu::String::String( const class Bu::Blob &rSrc ) 172Bu::String::String( const class Bu::Blob &rSrc ) :
173 core( new StringCore() )
167{ 174{
168 append( rSrc.getData(), rSrc.getSize() ); 175 append( rSrc.getData(), rSrc.getSize() );
169} 176}
170 177
171Bu::String::String( const Bu::String::const_iterator &s ) 178Bu::String::String( const Bu::String::const_iterator &s ) :
179 core( new StringCore() )
172{ 180{
173 append( s ); 181 append( s );
174} 182}
175 183
176Bu::String::String( const Bu::String::const_iterator &s, 184Bu::String::String( const Bu::String::const_iterator &s,
177 const Bu::String::const_iterator &e ) 185 const Bu::String::const_iterator &e ) :
186 core( new StringCore() )
178{ 187{
179 append( s, e ); 188 append( s, e );
180} 189}
181 190
182Bu::String::~String() 191Bu::String::~String()
183{ 192{
193 delete core;
184} 194}
185 195
186void Bu::String::append( const char *pData ) 196void Bu::String::append( const char *pData )
@@ -205,8 +215,6 @@ void Bu::String::append( const char *pData, long nStart, long nLen )
205 215
206 pData += nStart; 216 pData += nStart;
207 217
208 _hardCopy();
209
210 if( core->pLast && core->pLast->nLength+1 < nMinSize ) 218 if( core->pLast && core->pLast->nLength+1 < nMinSize )
211 { 219 {
212 int nAmnt = nMinSize - core->pLast->nLength; 220 int nAmnt = nMinSize - core->pLast->nLength;
@@ -234,7 +242,6 @@ void Bu::String::append( const char *pData, long nStart, long nLen )
234 242
235void Bu::String::append( const char &cData ) 243void Bu::String::append( const char &cData )
236{ 244{
237 _hardCopy();
238 if( core->pLast && core->pLast->nLength+1 < nMinSize ) 245 if( core->pLast && core->pLast->nLength+1 < nMinSize )
239 { 246 {
240 core->pLast->pData[core->pLast->nLength] = cData; 247 core->pLast->pData[core->pLast->nLength] = cData;
@@ -273,7 +280,6 @@ void Bu::String::append( const const_iterator &s )
273 Chunk *pNew = core->newChunk( pSrc->nLength-s.iPos ); 280 Chunk *pNew = core->newChunk( pSrc->nLength-s.iPos );
274 memcpy( pNew->pData, pSrc->pData+s.iPos, pSrc->nLength-s.iPos ); 281 memcpy( pNew->pData, pSrc->pData+s.iPos, pSrc->nLength-s.iPos );
275 282
276 _hardCopy();
277 core->appendChunk( pNew ); 283 core->appendChunk( pNew );
278 284
279 while( (pSrc = pSrc->pNext) ) 285 while( (pSrc = pSrc->pNext) )
@@ -296,7 +302,6 @@ void Bu::String::append( const const_iterator &s, const const_iterator &e )
296 append( s ); 302 append( s );
297 return; 303 return;
298 } 304 }
299 _hardCopy();
300 if( s.pChunk == e.pChunk ) 305 if( s.pChunk == e.pChunk )
301 { 306 {
302 // Simple case, they're the same chunk 307 // Simple case, they're the same chunk
@@ -333,7 +338,6 @@ void Bu::String::prepend( const char *pData )
333 if( pData == NULL ) 338 if( pData == NULL )
334 return; 339 return;
335 340
336 _hardCopy();
337 long nLen; 341 long nLen;
338 for( nLen = 0; pData[nLen] != (char)0; nLen++ ) { } 342 for( nLen = 0; pData[nLen] != (char)0; nLen++ ) { }
339 343
@@ -349,7 +353,6 @@ void Bu::String::prepend( const char *pData, long nLen )
349 353
350 memcpy( pNew->pData, pData, nLen ); 354 memcpy( pNew->pData, pData, nLen );
351 355
352 _hardCopy();
353 core->prependChunk( pNew ); 356 core->prependChunk( pNew );
354} 357}
355 358
@@ -374,7 +377,6 @@ void Bu::String::insert( long nPos, const char *pData, long nLen )
374 { 377 {
375 // If we're going to flatten anyway, might as well for everyone 378 // If we're going to flatten anyway, might as well for everyone
376 flatten(); 379 flatten();
377 _hardCopy();
378 Chunk *p1 = core->newChunk( nPos ); 380 Chunk *p1 = core->newChunk( nPos );
379 Chunk *p2 = core->newChunk( nLen ); 381 Chunk *p2 = core->newChunk( nLen );
380 Chunk *p3 = core->newChunk( core->nLength-nPos ); 382 Chunk *p3 = core->newChunk( core->nLength-nPos );
@@ -401,7 +403,6 @@ void Bu::String::insert( long nPos, const String &str )
401 else 403 else
402 { 404 {
403 flatten(); 405 flatten();
404 _hardCopy();
405 Chunk *p1 = core->newChunk( nPos ); 406 Chunk *p1 = core->newChunk( nPos );
406 Chunk *p3 = core->newChunk( core->nLength-nPos ); 407 Chunk *p3 = core->newChunk( core->nLength-nPos );
407 memcpy( p1->pData, core->pFirst->pData, nPos ); 408 memcpy( p1->pData, core->pFirst->pData, nPos );
@@ -430,7 +431,6 @@ void Bu::String::remove( long nPos, long nLen )
430 if( nLen > core->nLength-nPos ) 431 if( nLen > core->nLength-nPos )
431 nLen = core->nLength-nPos; 432 nLen = core->nLength-nPos;
432 flatten(); 433 flatten();
433 _hardCopy();
434 memmove( core->pFirst->pData+nPos, core->pFirst->pData+nPos+nLen, core->nLength-nPos-nLen+1 ); 434 memmove( core->pFirst->pData+nPos, core->pFirst->pData+nPos+nLen, core->nLength-nPos-nLen+1 );
435 core->nLength -= nLen; 435 core->nLength -= nLen;
436 core->pFirst->nLength -= nLen; 436 core->pFirst->nLength -= nLen;
@@ -438,10 +438,18 @@ void Bu::String::remove( long nPos, long nLen )
438 438
439void Bu::String::clear() 439void Bu::String::clear()
440{ 440{
441 _hardCopy();
442 core->clear(); 441 core->clear();
443} 442}
444 443
444const Bu::String &Bu::String::clone() const
445{
446 return *this;
447}
448
449void Bu::String::unshare()
450{
451}
452
445Bu::String Bu::String::replace( const Bu::String &fnd, 453Bu::String Bu::String::replace( const Bu::String &fnd,
446 const Bu::String &rep ) const 454 const Bu::String &rep ) const
447{ 455{
@@ -473,7 +481,6 @@ void Bu::String::resize( long nNewSize )
473 nNewSize = 0; 481 nNewSize = 0;
474 482
475 flatten(); 483 flatten();
476 _hardCopy();
477 484
478 // TODO: This is bad 485 // TODO: This is bad
479 486
@@ -501,7 +508,6 @@ char *Bu::String::getStr()
501 return (char *)""; 508 return (char *)"";
502 509
503 flatten(); 510 flatten();
504 _hardCopy();
505 core->pFirst->pData[core->nLength] = (char)0; 511 core->pFirst->pData[core->nLength] = (char)0;
506 return core->pFirst->pData; 512 return core->pFirst->pData;
507} 513}
@@ -631,7 +637,6 @@ Bu::String &Bu::String::operator+=( const Bu::String::const_iterator &i )
631 637
632Bu::String &Bu::String::operator+=( const char cData ) 638Bu::String &Bu::String::operator+=( const char cData )
633{ 639{
634 _hardCopy();
635 if( core->pLast && core->pLast->nLength+1 < nMinSize ) 640 if( core->pLast && core->pLast->nLength+1 < nMinSize )
636 { 641 {
637 core->pLast->pData[core->pLast->nLength] = cData; 642 core->pLast->pData[core->pLast->nLength] = cData;
@@ -732,7 +737,6 @@ void Bu::String::set( const_iterator s, const_iterator e )
732 737
733void Bu::String::setSize( long iSize ) 738void Bu::String::setSize( long iSize )
734{ 739{
735 _hardCopy();
736 core->clear(); 740 core->clear();
737 core->appendChunk( core->newChunk( iSize ) ); 741 core->appendChunk( core->newChunk( iSize ) );
738} 742}
@@ -867,7 +871,6 @@ char &Bu::String::operator[]( long nIndex )
867 if( nIndex < 0 || nIndex >= core->nLength ) 871 if( nIndex < 0 || nIndex >= core->nLength )
868 throw Bu::ExceptionBase("Index out of range."); 872 throw Bu::ExceptionBase("Index out of range.");
869 flatten(); 873 flatten();
870 _hardCopy();
871 874
872 return core->pFirst->pData[nIndex]; 875 return core->pFirst->pData[nIndex];
873} 876}
@@ -958,7 +961,6 @@ Bu::String Bu::String::toLower() const
958 Bu::String sRet = *this; 961 Bu::String sRet = *this;
959 962
960 sRet.flatten(); 963 sRet.flatten();
961 sRet._hardCopy();
962 964
963 for( long j = 0; j < sRet.core->nLength; j++ ) 965 for( long j = 0; j < sRet.core->nLength; j++ )
964 { 966 {
@@ -975,7 +977,6 @@ Bu::String Bu::String::toUpper() const
975 Bu::String sRet = *this; 977 Bu::String sRet = *this;
976 978
977 sRet.flatten(); 979 sRet.flatten();
978 sRet._hardCopy();
979 980
980 for( long j = 0; j < sRet.core->nLength; j++ ) 981 for( long j = 0; j < sRet.core->nLength; j++ )
981 { 982 {
@@ -1149,7 +1150,6 @@ void Bu::String::trimFront( long nAmnt )
1149 flatten(); 1150 flatten();
1150 Chunk *pNew = core->newChunk( nNewLen ); 1151 Chunk *pNew = core->newChunk( nNewLen );
1151 memcpy( pNew->pData, core->pFirst->pData+nAmnt, nNewLen ); 1152 memcpy( pNew->pData, core->pFirst->pData+nAmnt, nNewLen );
1152 _hardCopy();
1153 core->clear(); 1153 core->clear();
1154 core->appendChunk( pNew ); 1154 core->appendChunk( pNew );
1155} 1155}
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
58 58
59 /** 59 /**
60 */ 60 */
61 class String : public SharedCore<String, StringCore> 61 class String
62 { 62 {
63 protected:
64 using SharedCore<String, StringCore >::core;
65 using SharedCore<String, StringCore >::_hardCopy;
66
67 private: 63 private:
68 typedef StringCore::Chunk Chunk; 64 typedef StringCore::Chunk Chunk;
69 65
@@ -696,6 +692,9 @@ namespace Bu
696 */ 692 */
697 void clear(); 693 void clear();
698 694
695 const String &clone() const;
696 void unshare();
697
699 String replace( const String &fnd, const String &rep ) const; 698 String replace( const String &fnd, const String &rep ) const;
700 699
701 /** 700 /**
@@ -1054,6 +1053,9 @@ namespace Bu
1054 { 1053 {
1055 return FormatProxy( *this, pEndAction ); 1054 return FormatProxy( *this, pEndAction );
1056 } 1055 }
1056
1057 protected:
1058 StringCore *core;
1057 }; 1059 };
1058 1060
1059 template<class T> String operator+( const T *pLeft, const String &rRight ) 1061 template<class T> String operator+( const T *pLeft, const String &rRight )