aboutsummaryrefslogtreecommitdiff
path: root/src/fstring.h
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/fstring.h61
1 files changed, 48 insertions, 13 deletions
diff --git a/src/fstring.h b/src/fstring.h
index 585073f..2f508b7 100644
--- a/src/fstring.h
+++ b/src/fstring.h
@@ -3,8 +3,10 @@
3 3
4#include <stdint.h> 4#include <stdint.h>
5#include <memory> 5#include <memory>
6#include "serializable.h"
7#include "serializer.h"
6 8
7template< typename chr=char > 9template< typename chr >
8struct FStringChunk 10struct FStringChunk
9{ 11{
10 long nLength; 12 long nLength;
@@ -23,9 +25,12 @@ struct FStringChunk
23 * data is actually copied. This also means that you never need to put any 25 * data is actually copied. This also means that you never need to put any
24 * FBasicString into a ref-counting container class. 26 * FBasicString into a ref-counting container class.
25 */ 27 */
26template< typename chr=char, typename chralloc=std::allocator<chr>, typename chunkalloc=std::allocator<struct FStringChunk<chr> > > 28template< typename chr, typename chralloc=std::allocator<chr>, typename chunkalloc=std::allocator<struct FStringChunk<chr> > >
27class FBasicString 29class FBasicString : public Serializable
28{ 30{
31#ifndef VALTEST
32#define cpy( dest, src, size ) memcpy( dest, src, size*sizeof(chr) )
33#endif
29private: 34private:
30 typedef struct FStringChunk<chr> Chunk; 35 typedef struct FStringChunk<chr> Chunk;
31 typedef struct FBasicString<chr, chralloc, chunkalloc> MyType; 36 typedef struct FBasicString<chr, chralloc, chunkalloc> MyType;
@@ -57,16 +62,6 @@ public:
57 append( pData, nLength ); 62 append( pData, nLength );
58 } 63 }
59 64
60 /*
61 FBasicString( MyType &rSrc ) :
62 nLength( 0 ),
63 pnRefs( NULL ),
64 pFirst( NULL ),
65 pLast( NULL )
66 {
67 joinShare( rSrc );
68 }*/
69
70 FBasicString( const MyType &rSrc ) : 65 FBasicString( const MyType &rSrc ) :
71 nLength( 0 ), 66 nLength( 0 ),
72 pnRefs( NULL ), 67 pnRefs( NULL ),
@@ -131,6 +126,23 @@ public:
131 realClear(); 126 realClear();
132 } 127 }
133 128
129 void resize( long nNewSize )
130 {
131 if( nLength == nNewSize )
132 return;
133
134 flatten();
135
136 Chunk *pNew = newChunk( nNewSize );
137 long nNewLen = (nNewSize<nLength)?(nNewSize):(nLength);
138 cpy( pNew->pData, pFirst->pData, nNewLen );
139 pNew->pData[nNewLen] = (chr)0;
140 aChr.deallocate( pFirst->pData, pFirst->nLength+1 );
141 aChunk.deallocate( pFirst, 1 );
142 pFirst = pLast = pNew;
143 nLength = nNewSize;
144 }
145
134 chr *c_str() 146 chr *c_str()
135 { 147 {
136 if( pFirst == NULL ) 148 if( pFirst == NULL )
@@ -243,6 +255,27 @@ public:
243 return pFirst->pData[nIndex]; 255 return pFirst->pData[nIndex];
244 } 256 }
245 257
258 void serialize( class Serializer &ar )
259 {
260 if( ar.isLoading() )
261 {
262 clear();
263 long nLen;
264 ar >> nLen;
265
266 Chunk *pNew = newChunk( nLen );
267 ar.read( pNew->pData, nLen*sizeof(chr) );
268 appendChunk( pNew );
269 }
270 else
271 {
272 flatten();
273
274 ar << nLength;
275 ar.write( pFirst->pData, nLength*sizeof(chr) );
276 }
277 }
278
246private: 279private:
247 void flatten() const 280 void flatten() const
248 { 281 {
@@ -471,6 +504,7 @@ private:
471 realClear(); 504 realClear();
472 } 505 }
473 506
507#ifdef VALTEST
474 void cpy( chr *dest, const chr *src, long count ) const 508 void cpy( chr *dest, const chr *src, long count ) const
475 { 509 {
476 for( int j = 0; j < count; j++ ) 510 for( int j = 0; j < count; j++ )
@@ -480,6 +514,7 @@ private:
480 src++; 514 src++;
481 } 515 }
482 } 516 }
517#endif
483 518
484 void initCount() const 519 void initCount() const
485 { 520 {