00001
00002
00003
00004
00005 #ifndef xmog_quickstring_inc
00006 #define xmog_quickstring_inc
00007
00008
00009 template < int PreAlloc, class ElemType = char > class xmog_qstr
00010 {
00011 private:
00012
00013 ElemType buffer[ PreAlloc ];
00014
00015 ElemType * pBuffer;
00016
00017 size_t cUsed;
00018
00019 public:
00020
00021 xmog_qstr() :
00022 pBuffer( 0 ),
00023 cUsed( 0 )
00024 {
00025 }
00026
00027
00028 xmog_qstr( const ElemType * pStr, size_t max = -1 ) :
00029 pBuffer( 0 ),
00030 cUsed( 0 )
00031 {
00032 if( max != 0 )
00033 append( pStr, max == -1 ? 0 : max );
00034 else
00035 buffer[ 0 ] = 0;
00036 }
00037
00038 xmog_qstr( xmog_qstr & rhs ) :
00039 pBuffer( 0 ),
00040 cUsed( 0 )
00041 {
00042 if( rhs.cUsed > 0 )
00043 append( (const ElemType*)rhs, rhs.cUsed );
00044 else
00045 buffer[ 0 ] = 0;
00046 }
00047
00048
00049 xmog_qstr & operator = ( const xmog_qstr & rhs )
00050 {
00051 if( pBuffer != NULL )
00052 {
00053 delete pBuffer;
00054 pBuffer = NULL;
00055 }
00056
00057 cUsed = 0;
00058
00059 if( rhs.cUsed > 0 )
00060 append( (const ElemType*)rhs, rhs.cUsed );
00061 else
00062 buffer[ 0 ] = 0;
00063
00064 return *this;
00065 }
00066
00067
00068 xmog_qstr & operator = ( const ElemType * pStr )
00069 {
00070 if( pBuffer != NULL )
00071 {
00072 delete pBuffer;
00073 pBuffer = NULL;
00074 }
00075
00076 cUsed = 0;
00077 buffer[ 0 ] = 0;
00078
00079 append( pStr );
00080
00081 return *this;
00082 }
00083
00084 ~xmog_qstr()
00085 {
00086 if( pBuffer )
00087 delete pBuffer;
00088 buffer[ 0 ] = 0;
00089 pBuffer = 0;
00090 }
00091
00092 operator ElemType * ()
00093 {
00094 return pBuffer ? pBuffer : buffer;
00095 }
00096
00097 operator const ElemType * () const
00098 {
00099 return pBuffer ? pBuffer : buffer;
00100 }
00101
00102 void alloc( size_t size )
00103 {
00104 if( size > PreAlloc )
00105 pBuffer = new ElemType[ size ];
00106 }
00107
00108 void append( const ElemType * str, size_t max = 0 )
00109 {
00110 if( str == 0 )
00111 return;
00112
00113 size_t len = max ? max : strlen( str );
00114
00115 if( pBuffer == 0 && cUsed + len < PreAlloc )
00116 {
00117 strncpy( buffer + cUsed, str, len + 1 );
00118 cUsed += len;
00119 }
00120 else
00121 {
00122 ElemType * temp = new ElemType[ cUsed + len + 1 ];
00123
00124 if( pBuffer )
00125 {
00126 strncpy( temp, pBuffer, cUsed );
00127 delete pBuffer;
00128 }
00129 else
00130 strncpy( temp, buffer, cUsed );
00131
00132 strncpy( temp + cUsed, str, len + 1 );
00133 pBuffer = temp;
00134 cUsed += len;
00135 }
00136
00137 (pBuffer?pBuffer:buffer)[ cUsed ] = 0;
00138 }
00139
00140 void replace( ElemType src, ElemType dest )
00141 {
00142 ElemType * pElem = pBuffer ? pBuffer : buffer;
00143
00144 for( jint i=(jint)cUsed-1; i>=0; --i, pElem++ )
00145 if( *pElem == src )
00146 *pElem = dest;
00147 }
00148
00149 size_t length() const
00150 {
00151 return cUsed;
00152 }
00153 };
00154
00155
00156 #endif //xmog_quickstring_inc