gatelib  2.1
g_cont_gstr.h
1 #pragma once
2 
3 #include "g_cont_ref.h"
4 #include "g_cont_ref_const.h"
5 #include "g_cont_vect.h"
6 #include "g_str_lib.h"
7 #include <iostream>
8 #include <string>
9 
10 namespace g
11 {
12 namespace cont
13 {
14 
15 template < class T > class gstr
16 {
17 public:
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 ); }
23 
24  virtual ~gstr ( ){}
25 
26  gstr& operator = ( const gstr& other ) { mReceiveRef ( other ); return *this; }
27 
28  //returns len characters from start(count from 0) inside the string, if start > getLen() returns ""
29  gstr subset ( int start , size_t = 0xffffffff ) const;
30  //returns len characters from left of the string, if the string is shorter than len, returns the whole string
31  gstr left ( size_t len ) const { return subset ( 0 , len ); }
32  //returns len characters from right of the string, if the string is shorter than len, returns the whole string
33  gstr right ( size_t len ) const { return subset ( ( int ) ( getLength ( ) - len ) ); }
34 
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 ); }
37 
38  gstr lTrim ( ) const;
39  gstr rTrim ( ) const;
40  gstr trim ( ) const;
41 
42  gstr lCase ( ) const;
43  gstr uCase ( ) const;
44 
45  size_t getLength ( ) const { return mLength; }
46 
47  // plot the content onto console
48  void plot ( ) const;
49  // plot the content onto console and append newline
50  void plotLine ( ) const;
51 
52  // Searches str inside the string instance
53  // If the search succeeds the returns the offset, otherwise returns the size of the string
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)); }
56 
57  // special Char
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 ( );
62 
63  // returns the pointer to char buffer if exists otherwise
64  // returns a pointer to empty gstr ( either "" or L"" )
65  operator const T* ( ) const { return mBufferP; }
66 
67  gstr<wchar_t> toWideStr ( ) const;
68  gstr<char> toByteStr ( ) const;
69 
70  gstr append ( const T* s ) const;
71  gstr append ( const T c ) const;
72 
73  T operator [] ( int i ) const { return *(mBufferP+i); }
74 
75  gstr& operator += ( const T* s ) { *this = append ( s ); return *this; }
76  gstr& operator += ( const char c ) { *this = append ( c ); return *this; }
77 
78  static gstr sum ( const T* s1 , const T* s2 , AllocationPolicyAbstract* aAllocPolicyP = AllocationPolicyAbstract::get_FromStandardPolicy());
79 
80  HeapAbstract* getHeapP() const { return mInternalBufferRef.getHeapP ( ); }
81  AllocationPolicyAbstract* getAllocPolicyP() const { return mInternalBufferRef.getAllocPolicyP ( ); }
82 
83 private:
84  const T* mBufferP;
85  size_t mLength;
86 
87  typedef typename vect<T>::RefConst_t InternalBufferRefConst_t;
88  typedef typename vect<T>::Ref InternalBufferRef_t;
89 
90  InternalBufferRefConst_t mInternalBufferRef;
91 
92  InternalBufferRef_t mCreateBuffer ( size_t aStrLen , AllocationPolicyAbstract* aAllocPolicyP )
93  {
94  InternalBufferRef_t int_buffer_ref = InternalBufferRef_t::g_cont_new(aAllocPolicyP);
95 
96  mLength = aStrLen;
97  int_buffer_ref->reSize ( mLength + 1 );
98  mBufferP = int_buffer_ref->operator const T*();
99  mInternalBufferRef = int_buffer_ref;
100 
101  return int_buffer_ref;
102  }
103 
104  void mInstanciate ( const T* aData , size_t aStrLen , AllocationPolicyAbstract* aAllocPolicyP )
105  {
106  size_t i;
107 
108  for ( i = 0 ; aData[i] != 0x0 && i < aStrLen ; i++ );
109 
110  aStrLen = i;
111 
112  InternalBufferRef_t int_buffer_ref = mCreateBuffer ( aStrLen , aAllocPolicyP );
113 
114  for ( i = 0 ; i < aStrLen ; i++ )
115  {
116  int_buffer_ref->operator []((int)i) = aData[i];
117  }
118 
119  int_buffer_ref->operator []((int)i) = 0x0;
120  }
121 
122  void mReceiveRef ( const gstr& aOther )
123  {
124  mBufferP = aOther.mBufferP;
125  mLength = aOther.mLength;
126  mInternalBufferRef = aOther.mInternalBufferRef;
127  }
128 };
129 
130 typedef gstr< char > Gstr;
131 typedef gstr< wchar_t > Wstr;
132 
133 // Sum Policies
134 
135 // operator + ( gstr , gstr )
136 template < class T > gstr<T>
137  operator + ( const gstr<T>& s1 , const gstr<T>& s2 ){ return gstr<T> ( gstr<T>::sum(s1,s2) ); }
138 
139 // operator + ( gstr , const T* )
140 template < class T > gstr<T>
141  operator + ( const T* s1 , const gstr<T>& s2 ){ return gstr<T> ( gstr<T>::sum(s1,s2) ); }
142 // operator + ( const T* , gstr )
143 template < class T > gstr<T>
144  operator + ( const gstr<T>& s1 , const T* s2 ){ return gstr<T> ( gstr<T>::sum(s1,s2) ); }
145 
146 //The presence of const char*() operators forces to define the all of comparition operators
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 ); }
150 
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 ); }
154 
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; }
158 
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; }
162 
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; }
166 
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; }
170 
171 
172 template < class T > gstr<T> gstr<T>::append ( const T aChar ) const
173 {
174  T temp[2];
175 
176  temp[0] = aChar;
177  temp[1] = 0;
178 
179  return append (temp);
180 }
181 
182 template < class T > gstr<T> gstr<T>::append ( const T* aStr ) const
183 {
184  gstr result;
185 
186  InternalBufferRef_t int_buffer_ref = result.mCreateBuffer ( getLength() + str::get_len(aStr), AllocationPolicyAbstract::get_FromStandardPolicy() );
187 
188  T* result_buffer = &( int_buffer_ref->operator [](0) );
189 
190  str::str_copy<T> ( result_buffer , mBufferP );
191  str::str_copy<T> ( result_buffer + getLength() , aStr );
192 
193  return result;
194 }
195 
196 template < class T > gstr<T> gstr<T>::subset ( int aStart , size_t aLen ) const
197 {
198  if ( aLen == 0 || aStart >= (int)getLength() )
199  {
200  return empty();
201  }
202  else
203  {
204  size_t len = ( aLen - aStart < aLen)?(aLen - aStart ):aLen;
205 
206  return gstr<T>(operator const T* () + aStart , len );
207  }
208 }
209 
210 template < class T > gstr<T> gstr<T>::lTrim ( ) const
211 {
212  size_t start;
213 
214  for (
215  start = 0 ;
216  start < getLength ( ) && ( *this )[start] == *space ( ) ;
217  start++ );
218 
219  return gstr<T>( mBufferP + start , getLength ( ) - start );
220 }
221 
222 template < class T > gstr<T> gstr<T>::rTrim ( ) const
223 {
224  gstr<T> result;
225 
226  size_t end;
227 
228  for (
229  end = getLength ( ) - 1 ;
230  end >= 0 && ( *this )[end] == *space ( ) ;
231  end-- );
232 
233  return gstr<T>( mBufferP , end + 1 );
234 }
235 
236 template < class T > gstr<T> gstr<T>::trim ( ) const
237 {
238  size_t start;
239  size_t end;
240 
241  for (
242  start = 0 ;
243  start < getLength ( ) && ( *this )[(int)start] == *space ( ) ;
244  start++ );
245 
246  for (
247  end = getLength ( ) - 1 ;
248  end >= 0 && ( *this )[(int)end] == *space ( ) ;
249  end-- );
250 
251  return gstr<T>( mBufferP + (int)start , (size_t)( end - start + 1 ) , AllocationPolicyAbstract::get_FromStandardPolicy() );
252 }
253 
254 // plot the content onto console and append newline
255 template < class T > void gstr<T>::plotLine ( ) const
256 {
257  plot ( );
258  new_line ( ).plot( );
259 }
260 
261 template < class T > gstr <T> gstr<T>::lCase ( ) const
262 {
263  gstr result;
264 
265  InternalBufferRef_t int_buffer_ref = result.mCreateBuffer ( getLength() , AllocationPolicyAbstract::get_FromStandardPolicy() );
266 
267  T* result_buffer = &( int_buffer_ref->operator [](0) );
268 
269  size_t i;
270 
271  for ( i = 0 ; i < getLength ( ) ; i++ )
272  {
273  result_buffer[i] = car::l_case < T >( (*this)[(int)i] );
274  }
275 
276  result_buffer[i] = 0x0;
277 
278  return result;
279 }
280 
281 template < class T > gstr<T> gstr<T>::uCase ( ) const
282 {
283  gstr result;
284 
285  InternalBufferRef_t int_buffer_ref = result.mCreateBuffer ( getLength() , AllocationPolicyAbstract::get_FromStandardPolicy() );
286 
287  T* result_buffer = &( int_buffer_ref->operator [](0) );
288 
289  size_t i;
290 
291  for ( i = 0 ; i < getLength ( ) ; i++ )
292  {
293  result_buffer[i] = car::u_case < T >( (*this)[(int)i] );
294  }
295 
296  result_buffer[i] = 0x0;
297 
298  return result;
299 }
300 
301 template < class T > gstr<T> gstr<T>::sum ( const T* aS1 , const T* aS2 , AllocationPolicyAbstract* aAllocPolicyP )
302 {
303  size_t len_1 = str::get_len(aS1);
304  size_t len_2 = str::get_len(aS2);
305 
306  gstr result;
307 
308  InternalBufferRef_t int_buffer_ref = result.mCreateBuffer ( len_1 + len_2 , aAllocPolicyP );
309 
310  T* result_buffer = &( int_buffer_ref->operator [](0) );
311 
312  str::str_copy<T> ( result_buffer , aS1 );
313  str::str_copy<T> ( result_buffer + len_1 , aS2 );
314 
315  return result;
316 }
317 
318 template <> inline gstr<wchar_t> gstr<wchar_t>::toWideStr ( ) const { return Wstr ( *this ); }
319 
320 template <> inline gstr<wchar_t> gstr<char>::toWideStr ( ) const
321 {
322  vect<wchar_t> temp(AllocationPolicyAbstract::get_FromStandardPolicy());
323 
324  temp.reSize(getLength());
325 
326  int i;
327 
328  for ( i = 0 ; i <= (int)getLength ( ) ; i++ )
329  {
330  temp[i] = (unsigned char)( ( *this )[i] );
331  }
332 
333  return gstr<wchar_t>((const wchar_t*)temp,getLength(),AllocationPolicyAbstract::get_FromStandardPolicy());
334 }
335 
336 template <> inline gstr<char> gstr<char>::toByteStr ( ) const { return Gstr ( *this ); }
337 
338 template <> inline gstr<char> gstr<wchar_t>::toByteStr ( ) const
339 {
340  vect<char> temp(AllocationPolicyAbstract::get_FromStandardPolicy());
341 
342  temp.reSize(getLength()+1);
343 
344  int i;
345 
346  for ( i = 0 ; i <= (int)getLength ( ) ; i++ )
347  {
348  temp[i] = static_cast<char>(0xff & this->operator []((int)i));
349  }
350 
351  return gstr<char>((const char*)temp,getLength(),AllocationPolicyAbstract::get_FromStandardPolicy());
352 }
353 
354 template < class T > std::basic_ostream<T>& operator<< (std::basic_ostream<T> &out, const gstr<T>& aGstr )
355 {
356  out << (const T*) aGstr;
357 
358  return out;
359 }
360 
361 template < class T > std::basic_istream<T>& operator>> (std::basic_istream<T> &in, gstr<T>& aGstr )
362 {
363  const int buffer_size = 1024;
364  T temp[buffer_size];
365 
366  in >> temp;
367 
368  aGstr = temp;
369 
370  return in;
371 }
372 
373 }// namespace cont
374 }// namespace g
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