3 #include "g_cont_ref.h"
4 #include "g_cont_ref_const.h"
5 #include "g_cont_vect.h"
15 template <
class T >
class gstr
18 gstr ( ) : mBufferP( NULL ) , mLength ( 0 ) { mReceiveRef(empty()); }
19 gstr (
const gstr& other ) : mBufferP( NULL ) , mLength ( 0 ) { mReceiveRef ( other ); }
20 gstr (
const T* str , AllocationPolicyAbstract* aAllocPolicyP = AllocationPolicyAbstract::get_FromStandardPolicy() ) { mInstanciate ( str , infinite , aAllocPolicyP ); }
21 gstr (
const T* str ,
size_t size , AllocationPolicyAbstract* aAllocPolicyP = AllocationPolicyAbstract::get_FromStandardPolicy() ) { mInstanciate ( str , size , aAllocPolicyP ); }
22 gstr (
const std::basic_string<T> str ,
size_t size = infinite , AllocationPolicyAbstract* aAllocPolicyP = AllocationPolicyAbstract::get_FromStandardPolicy() ) { mInstanciate ( str.c_str() , size , aAllocPolicyP ); }
26 gstr& operator = (
const gstr& other ) { mReceiveRef ( other );
return *
this; }
29 gstr subset (
int start ,
size_t = 0xffffffff )
const;
31 gstr left (
size_t len )
const {
return subset ( 0 , len ); }
33 gstr right (
size_t len )
const {
return subset ( (
int ) ( getLength ( ) - len ) ); }
35 bool isLeftLike (
const gstr& cfr ,
bool is_case_sens =
true )
const {
return 0==
str::str_compare ( (
const T*)cfr , (
const T*)left(cfr.getLength()) , is_case_sens ); }
36 bool isRightLike (
const gstr& cfr ,
bool is_case_sens =
true )
const {
return 0==
str::str_compare ( (
const T*)cfr , (
const T*)right(cfr.getLength()) , is_case_sens ); }
45 size_t getLength ( )
const {
return mLength; }
50 void plotLine ( )
const;
54 size_t getIndexOf (
const gstr& str ,
size_t index = 0 ,
bool is_case_sens =
true )
const {
return (
size_t)index+
g::str::str_search ( (
const T*)(*
this) + index , (
const T*)str , is_case_sens ); }
55 gstr replace (
const T* what ,
const T* with ,
bool is_case_sens =
true )
const {
return gstr(
g::str::str_replace<T,gstr<T>,1024>((
const T*)(*
this),what,with)); }
58 static G_LIB_ITEM
const gstr& empty ( );
59 static G_LIB_ITEM
const gstr& new_line ( );
60 static G_LIB_ITEM
const gstr& space ( );
61 static G_LIB_ITEM
const gstr& tab ( );
65 operator const T* ( )
const {
return mBufferP; }
67 gstr<wchar_t> toWideStr ( )
const;
68 gstr<char> toByteStr ( )
const;
70 gstr append (
const T* s )
const;
71 gstr append (
const T c )
const;
73 T operator [] (
int i )
const {
return *(mBufferP+i); }
75 gstr& operator += (
const T* s ) { *
this = append ( s );
return *
this; }
76 gstr& operator += (
const char c ) { *
this = append ( c );
return *
this; }
78 static gstr sum (
const T* s1 ,
const T* s2 , AllocationPolicyAbstract* aAllocPolicyP = AllocationPolicyAbstract::get_FromStandardPolicy());
80 HeapAbstract* getHeapP()
const {
return mInternalBufferRef.getHeapP ( ); }
81 AllocationPolicyAbstract* getAllocPolicyP()
const {
return mInternalBufferRef.getAllocPolicyP ( ); }
87 typedef typename vect<T>::RefConst_t InternalBufferRefConst_t;
88 typedef typename vect<T>::Ref InternalBufferRef_t;
90 InternalBufferRefConst_t mInternalBufferRef;
92 InternalBufferRef_t mCreateBuffer (
size_t aStrLen , AllocationPolicyAbstract* aAllocPolicyP )
94 InternalBufferRef_t int_buffer_ref = InternalBufferRef_t::g_cont_new(aAllocPolicyP);
97 int_buffer_ref->reSize ( mLength + 1 );
98 mBufferP = int_buffer_ref->operator
const T*();
99 mInternalBufferRef = int_buffer_ref;
101 return int_buffer_ref;
104 void mInstanciate (
const T* aData ,
size_t aStrLen , AllocationPolicyAbstract* aAllocPolicyP )
108 for ( i = 0 ; aData[i] != 0x0 && i < aStrLen ; i++ );
112 InternalBufferRef_t int_buffer_ref = mCreateBuffer ( aStrLen , aAllocPolicyP );
114 for ( i = 0 ; i < aStrLen ; i++ )
116 int_buffer_ref->operator []((int)i) = aData[i];
119 int_buffer_ref->operator []((int)i) = 0x0;
122 void mReceiveRef (
const gstr& aOther )
124 mBufferP = aOther.mBufferP;
125 mLength = aOther.mLength;
126 mInternalBufferRef = aOther.mInternalBufferRef;
130 typedef gstr< char > Gstr;
131 typedef gstr< wchar_t > Wstr;
136 template <
class T > gstr<T>
137 operator + (
const gstr<T>& s1 ,
const gstr<T>& s2 ){
return gstr<T> ( gstr<T>::sum(s1,s2) ); }
140 template <
class T > gstr<T>
141 operator + (
const T* s1 ,
const gstr<T>& s2 ){
return gstr<T> ( gstr<T>::sum(s1,s2) ); }
143 template <
class T > gstr<T>
144 operator + (
const gstr<T>& s1 ,
const T* s2 ){
return gstr<T> ( gstr<T>::sum(s1,s2) ); }
147 template <
class T >
bool operator == (
const gstr<T>& s1 ,
const gstr<T>& s2 ) {
return str::str_equal<T> ( s1 , s2 ); }
148 template <
class T >
bool operator == (
const gstr<T>& s1 ,
const T* s2 ) {
return str::str_equal<T> ( (
const T* ) s1 , s2 ); }
149 template <
class T >
bool operator == (
const T* s1 ,
const gstr<T>& s2 ) {
return str::str_equal<T> ( s1 , (
const T* )s2 ); }
151 template <
class T >
bool operator != (
const gstr<T>& s1 ,
const gstr<T>& s2 ) {
return !str::str_equal<T> ( s1 , s2 ); }
152 template <
class T >
bool operator != (
const gstr<T>& s1 ,
const T* s2 ) {
return !str::str_equal<T> ( (
const T* ) s1 , s2 ); }
153 template <
class T >
bool operator != (
const T* s1 ,
const gstr<T>& s2 ) {
return !str::str_equal<T> ( s1 , (
const T* )s2 ); }
155 template <
class T >
bool operator > (
const gstr<T>& s1 ,
const gstr<T>& s2 ) {
return str::str_compare<T> ( s1 , s2 ) > 0 ; }
156 template <
class T >
bool operator > (
const gstr<T>& s1 ,
const T* s2 ) {
return str::str_compare<T> ( (
const T* ) s1 , s2 ) > 0; }
157 template <
class T >
bool operator > (
const T* s1 ,
const gstr<T>& s2 ) {
return str::str_compare<T> ( s1 , (
const T* )s2 ) > 0; }
159 template <
class T >
bool operator < ( const gstr<T>& s1 ,
const gstr<T>& s2 ) {
return str::str_compare<T> ( s1 , s2 ) < 0 ; }
160 template <
class T >
bool operator < ( const gstr<T>& s1 ,
const T* s2 ) {
return str::str_compare<T> ( (
const T* ) s1 , s2 ) < 0; }
161 template <
class T >
bool operator < ( const T* s1 , const gstr<T>& s2 ) {
return str::str_compare<T> ( s1 , (
const T* )s2 ) < 0; }
163 template <
class T >
bool operator >= (
const gstr<T>& s1 ,
const gstr<T>& s2 ) {
return str::str_compare<T> ( s1 , s2 ) >= 0 ; }
164 template <
class T >
bool operator >= (
const gstr<T>& s1 ,
const T* s2 ) {
return str::str_compare<T> ( (
const T* ) s1 , s2 ) >= 0; }
165 template <
class T >
bool operator >= (
const T* s1 ,
const gstr<T>& s2 ) {
return str::str_compare<T> ( s1 , (
const T* )s2 ) >= 0; }
167 template <
class T >
bool operator <= ( const gstr<T>& s1 ,
const gstr<T>& s2 ) {
return str::str_compare<T> ( s1 , s2 ) <= 0 ; }
168 template <
class T >
bool operator <= ( const gstr<T>& s1 ,
const T* s2 ) {
return str::str_compare<T> ( (
const T* ) s1 , s2 ) <= 0; }
169 template <
class T >
bool operator <= ( const T* s1 , const gstr<T>& s2 ) {
return str::str_compare<T> ( s1 , (
const T* )s2 ) <= 0; }
172 template <
class T > gstr<T> gstr<T>::append (
const T aChar )
const
179 return append (temp);
182 template <
class T > gstr<T> gstr<T>::append (
const T* aStr )
const
186 InternalBufferRef_t int_buffer_ref = result.mCreateBuffer ( getLength() +
str::get_len(aStr), AllocationPolicyAbstract::get_FromStandardPolicy() );
188 T* result_buffer = &( int_buffer_ref->operator [](0) );
190 str::str_copy<T> ( result_buffer , mBufferP );
191 str::str_copy<T> ( result_buffer + getLength() , aStr );
196 template <
class T > gstr<T> gstr<T>::subset (
int aStart ,
size_t aLen )
const
198 if ( aLen == 0 || aStart >= (
int)getLength() )
204 size_t len = ( aLen - aStart < aLen)?(aLen - aStart ):aLen;
206 return gstr<T>(
operator const T* () + aStart , len );
210 template <
class T > gstr<T> gstr<T>::lTrim ( )
const
216 start < getLength ( ) && ( *this )[start] == *space ( ) ;
219 return gstr<T>( mBufferP + start , getLength ( ) - start );
222 template <
class T > gstr<T> gstr<T>::rTrim ( )
const
229 end = getLength ( ) - 1 ;
230 end >= 0 && ( *this )[end] == *space ( ) ;
233 return gstr<T>( mBufferP , end + 1 );
236 template <
class T > gstr<T> gstr<T>::trim ( )
const
243 start < getLength ( ) && ( *this )[(int)start] == *space ( ) ;
247 end = getLength ( ) - 1 ;
248 end >= 0 && ( *this )[(int)end] == *space ( ) ;
251 return gstr<T>( mBufferP + (int)start , (
size_t)( end - start + 1 ) , AllocationPolicyAbstract::get_FromStandardPolicy() );
255 template <
class T >
void gstr<T>::plotLine ( )
const
258 new_line ( ).plot( );
261 template <
class T > gstr <T> gstr<T>::lCase ( )
const
265 InternalBufferRef_t int_buffer_ref = result.mCreateBuffer ( getLength() , AllocationPolicyAbstract::get_FromStandardPolicy() );
267 T* result_buffer = &( int_buffer_ref->operator [](0) );
271 for ( i = 0 ; i < getLength ( ) ; i++ )
273 result_buffer[i] = car::l_case < T >( (*this)[(int)i] );
276 result_buffer[i] = 0x0;
281 template <
class T > gstr<T> gstr<T>::uCase ( )
const
285 InternalBufferRef_t int_buffer_ref = result.mCreateBuffer ( getLength() , AllocationPolicyAbstract::get_FromStandardPolicy() );
287 T* result_buffer = &( int_buffer_ref->operator [](0) );
291 for ( i = 0 ; i < getLength ( ) ; i++ )
293 result_buffer[i] = car::u_case < T >( (*this)[(int)i] );
296 result_buffer[i] = 0x0;
301 template <
class T > gstr<T> gstr<T>::sum (
const T* aS1 ,
const T* aS2 , AllocationPolicyAbstract* aAllocPolicyP )
308 InternalBufferRef_t int_buffer_ref = result.mCreateBuffer ( len_1 + len_2 , aAllocPolicyP );
310 T* result_buffer = &( int_buffer_ref->operator [](0) );
312 str::str_copy<T> ( result_buffer , aS1 );
313 str::str_copy<T> ( result_buffer + len_1 , aS2 );
318 template <>
inline gstr<wchar_t> gstr<wchar_t>::toWideStr ( )
const {
return Wstr ( *
this ); }
320 template <>
inline gstr<wchar_t> gstr<char>::toWideStr ( )
const
322 vect<wchar_t> temp(AllocationPolicyAbstract::get_FromStandardPolicy());
324 temp.reSize(getLength());
328 for ( i = 0 ; i <= (int)getLength ( ) ; i++ )
330 temp[i] = (
unsigned char)( ( *
this )[i] );
333 return gstr<wchar_t>((
const wchar_t*)temp,getLength(),AllocationPolicyAbstract::get_FromStandardPolicy());
336 template <>
inline gstr<char> gstr<char>::toByteStr ( )
const {
return Gstr ( *
this ); }
338 template <>
inline gstr<char> gstr<wchar_t>::toByteStr ( )
const
340 vect<char> temp(AllocationPolicyAbstract::get_FromStandardPolicy());
342 temp.reSize(getLength()+1);
346 for ( i = 0 ; i <= (int)getLength ( ) ; i++ )
348 temp[i] =
static_cast<char>(0xff & this->operator []((
int)i));
351 return gstr<char>((
const char*)temp,getLength(),AllocationPolicyAbstract::get_FromStandardPolicy());
354 template <
class T > std::basic_ostream<T>& operator<< (std::basic_ostream<T> &out,
const gstr<T>& aGstr )
356 out << (
const T*) aGstr;
361 template <
class T > std::basic_istream<T>& operator>> (std::basic_istream<T> &in, gstr<T>& aGstr )
363 const int buffer_size = 1024;
int str_compare(const T *aS1, const T *aS2, bool aIsCaseSens=true, size_t aNumChars=infinite)
Compares two strings.
Definition: g_str_lib.h:142
size_t get_len(const T *aString)
Returns the string length.
Definition: g_str_lib.h:41
Definition: g.mthread.ThreadSimpleEvent.h:5
size_t str_replace(const T *aString, const T *aWhat, const T *aWith, T *aOutputBuffer, size_t aNumChars=infinite)
Replace aWhat with aWith inside aInput, the result is stored into aOutputBuffer, whose capacity us aN...
Definition: g_str_lib.h:275
size_t str_search(const T *aS1, const T *aS2, bool aIsCaseSens=true, size_t aNumChars=infinite)
Searches aS2 inside aS1.
Definition: g_str_lib.h:238