Kigs Framework  Doc version 0.8
Open source multi purpose Rapid Application Development framework
usString.h
1 #ifndef _USSTRING_H
2 #define _USSTRING_H
3 
4 #include "CoreTypes.h"
5 #include <string>
6 #include <locale>
7 
8 // ****************************************
9 // * usString class
10 // * --------------------------------------
14 // ****************************************
15 // Unsigned short String Management
16 
17 #include "kstlvector.h"
18 
19 // define sprintf on wchar
20 extern "C" int sprintfw(unsigned short* _Dest, const unsigned short* _format, ...);
21 extern "C" int vsprintfw(unsigned short* _Dest, const unsigned short* _format, va_list args);
22 // copy a nul terminated string to a wchar buffer
23 extern "C" void strcpyw(unsigned short* _Dest, const char* src);
24 extern "C" void strcpywUtoC(char * _Dest, const unsigned short * src);
25 extern "C" int setUnsignedShortToInt(unsigned short * buffer);
26 extern "C" float setUnsignedShortToFloat(unsigned short * buffer);
27 extern "C" void convertIntToString(char * buffer, int number);
28 
29 typedef unsigned char UTF8Char;
30 
31 class usString
32 {
33 public:
34  usString();
35 
36  // set this one as explicit to avoid ambiguity for method with kstl::string or usString parameter called with const char* parameter
37  explicit usString(const char* str) :
38  mString(0)
39  {
40  copy(str);
41  }
42 
43  usString(const UTF8Char* str) :
44  mString(0)
45  {
46  copy(str);
47  }
48 
49  usString(const unsigned short* str) :
50  mString(0)
51  {
52  copy(str);
53  }
54 
55  usString(const kstl::string& other) : mString(0) {
56  copy(other.c_str());
57  }
58 
59  usString(const usString& other);
60 
61  // reserve clear the current string if any
62  void reserve(int size);
63 
64  ~usString();
65 
66  // cast operator
67  operator bool() const;
68 
69  operator float() const;
70 
71  operator int() const;
72 
73  operator unsigned int() const;
74 
75  explicit operator kstl::string() const;
76 
77  const unsigned short* us_str() const
78  {
79  return mString;
80  }
81  /*const char* c_str() const
82  {
83  char * _Dest = new char[length()];
84  this->strcpywUtoC(_Dest, mString);
85  return _Dest;
86  }*/
87 
88 
89  bool operator == (const char* str)
90  {
91  usString usstr(str);
92  return usstr == *this;
93  }
94 
95  bool operator == (const usString& other)
96  {
97  const unsigned short* read_char1 = mString;
98  const unsigned short* read_char2 = other.mString;
99 
100  while (*read_char2 == *read_char1)
101  {
102  if (*read_char1 == 0)
103  {
104  return true;
105  }
106  ++read_char2;
107  ++read_char1;
108  }
109 
110  return false;
111  }
112 
113  inline const unsigned short& operator[](unsigned int i)const
114  {
115  // warning, no check for out of bound
116  return mString[i];
117  }
118 
119  inline unsigned short& operator[](unsigned int i)
120  {
121  // warning, no check for out of bound
122  return mString[i];
123  }
124 
125  void strcpywUtoC(char * _Dest, const unsigned short * src)const
126  {
127  const unsigned short* read_char = src;
128  char* write_char = _Dest;
129 
130  while (*read_char)
131  {
132  *write_char = (char)*read_char;
133  ++read_char;
134  ++write_char;
135  }
136 
137  *write_char = 0;
138  }
139 
140  bool operator<(const usString& other) const
141  {
142  const unsigned short* read_char1 = mString;
143  const unsigned short* read_char2 = other.mString;
144 
145  while (*read_char1)
146  {
147  if (*read_char1 < *read_char2)
148  {
149  return true;
150  }
151  else if (*read_char1 > *read_char2)
152  {
153  return false;
154  }
155  ++read_char1;
156  ++read_char2;
157  }
158  return false;
159  }
160 
161  kstl::string ToString() const
162  {
163  kstl::string ret = "";
164  ret.reserve(strlen());
165 
166  const unsigned short* read_char = mString;
167 
168  while (*read_char)
169  {
170  ret += ((char)*read_char);
171  ++read_char;
172  }
173 
174  return ret;
175  }
176 
177  // encode current unsigned short string to a char string :
178  // each unsigned short is print as a integer and each integer is separated by '.'
179  kstl::string encode();
180 
181  // decode a serie of integer separated by '.' to a unsigned short string
182  void decode(const kstl::string& todecode);
183 
184 
185  // len = -1 remove everything from start to end of the string
186  void removeRange(size_t start, int len);
187 
188  kstl::vector<usString> SplitByCharacter(unsigned short _value) const
189  {
190  kstl::vector<usString> returnedString;
191 
192  unsigned short* read = mString;
193 
194  unsigned int startCount = 0;
195  unsigned int count = 0;
196  while (*read)
197  {
198  if ((*read) == _value)
199  {
200  unsigned int size = count + 1;
201  unsigned short* temp = new unsigned short[size];
202 
203  memset(temp, 0, (size) * sizeof(unsigned short));
204  memcpy(temp, mString + startCount, count * sizeof(unsigned short));
205  returnedString.push_back(temp);
206  delete[] temp;
207 
208  startCount += size;
209  count = 0;
210  ++read;
211  }
212  else
213  {
214  ++read;
215  count++;
216  }
217 
218  }
219  unsigned int size = count + 1;
220  unsigned short* temp = new unsigned short[size];
221 
222  memset(temp, 0, (size) * sizeof(unsigned short));
223  memcpy(temp, mString + startCount, count * sizeof(unsigned short));
224  returnedString.push_back(temp);
225  delete[] temp;
226 
227  return returnedString;
228  }
229 
230  static inline unsigned int strlen(const char* str)
231  {
232  char* read = (char*)str;
233 
234  unsigned int len = 0;
235 
236  while (*read)
237  {
238  ++read;
239  ++len;
240  }
241 
242  return len;
243 
244  }
245 
246  bool empty() const
247  {
248  return (strlen(mString) == 0);
249  }
250 
251  inline unsigned int length() const
252  {
253  return strlen(mString);
254  }
255 
256  usString substr(int pos, int len) const
257  {
258  usString result;
259  int origlen = strlen(mString);
260  if ((origlen - pos) < len)
261  {
262  len = origlen - pos;
263  if (len < 0)
264  {
265  len = 0;
266  }
267  }
268 
269  if (result.mString)
270  delete[] result.mString;
271  result.mString = new unsigned short[len + 1];
272  memcpy(result.mString, mString + pos, len * sizeof(unsigned short));
273  result.mString[len] = 0;
274 
275  return result;
276  }
277 
278  std::string::size_type find(unsigned short toFind, int startpos = 0) const
279  {
280  int Len = strlen(mString);
281  if (startpos >= Len)
282  {
283  return std::string::npos;
284  }
285 
286  unsigned short* read = ((unsigned short*)mString) + startpos;
287  unsigned int pos = startpos;
288  while (*read)
289  {
290  if (*read == toFind)
291  {
292  return pos;
293  }
294  ++pos;
295  ++read;
296  }
297  return std::string::npos;
298  }
299  std::string::size_type rfind(unsigned short toFind, int startpos = 0) const
300  {
301  int Len = strlen(mString);
302  if (startpos < 0)
303  {
304  return std::string::npos;
305  }
306 
307  unsigned short* read = ((unsigned short*)mString) + startpos;
308  unsigned int pos = Len - 1 - startpos;
309  while (*read)
310  {
311  if (*read == toFind)
312  {
313  return pos;
314  }
315  --pos;
316  --read;
317  }
318  return std::string::npos;
319  }
320 
321  std::string::size_type find(const usString& toFind, int startpos = 0) const;
322  std::string::size_type rfind(const usString& toFind, int startpos = 0) const;
323 
324 
325  void replaceAllOccurence(const usString& toReplace, const usString& Replacement, int startpos = 0);
326 
327  void replaceAt(const usString& Replacement, int start,int lenToReplace);
328 
329  void replaceEscapeUnicode();
330 
331  static inline unsigned int strlen(const unsigned short* str)
332  {
333  unsigned short* read = (unsigned short*)str;
334 
335  unsigned int len = 0;
336 
337  while (*read)
338  {
339  ++read;
340  ++len;
341  }
342 
343  return len;
344 
345  }
346 
347  inline unsigned int strlen() const
348  {
349  return strlen(mString);
350  }
351 
352  usString& operator=(const usString& toCopy)
353  {
354  copy(toCopy.us_str());
355 
356  return *this;
357  }
358  /*
359  usString& operator=(const unsigned short* toCopy)
360  {
361  copy(toCopy);
362  return *this;
363  }
364 
365  usString& operator=(const char * toCopy)
366  {
367  copy(toCopy);
368  return *this;
369  }
370 
371  usString& operator=(const UTF8Char * toCopy)
372  {
373  copy(toCopy);
374  return *this;
375  }
376 
377  usString& operator=(const kstl::string& toCopy)
378  {
379  copy(toCopy.c_str());
380  return *this;
381  }*/
382 
383  bool operator==(const usString& _value) const
384  {
385  unsigned int len1 = _value.strlen();
386  unsigned int len2 = strlen();
387 
388  if (len1 == len2)
389  {
390  unsigned short* read = _value.mString;
391  unsigned short* read2 = mString;
392 
393  while (*read)
394  {
395  if ((*read2) != (*read))
396  {
397  return false;
398  }
399  ++read;
400  ++read2;
401  }
402 
403  return true;
404  }
405 
406  return false;
407  }
408 
409  bool operator==(unsigned short* _value) const
410  {
411  unsigned int len1 = strlen(_value);
412  unsigned int len2 = strlen();
413 
414  if (len1 == len2)
415  {
416  unsigned short* read = _value;
417  unsigned short* read2 = mString;
418 
419  while (*read)
420  {
421  if ((*read2) != (*read))
422  {
423  return false;
424  }
425  ++read;
426  ++read2;
427  }
428 
429  return true;
430  }
431 
432  return false;
433  }
434 
435  bool operator!=(const usString& _value) const
436  {
437  unsigned int len1 = _value.strlen();
438  unsigned int len2 = strlen();
439 
440  if (len1 == len2)
441  {
442  unsigned short* read = _value.mString;
443  unsigned short* read2 = mString;
444 
445  while (*read)
446  {
447  if ((*read2) != (*read))
448  {
449  return true;
450  }
451  ++read;
452  ++read2;
453  }
454 
455  return false;
456  }
457 
458  return true;
459  }
460 
461  bool operator!=(unsigned short* _value) const
462  {
463  unsigned int len1 = strlen(_value);
464  unsigned int len2 = strlen();
465 
466  if (len1 == len2)
467  {
468  unsigned short* read = _value;
469  unsigned short* read2 = mString;
470 
471  while (*read)
472  {
473  if ((*read2) != (*read))
474  {
475  return true;
476  }
477  ++read;
478  ++read2;
479  }
480  return false;
481  }
482  return true;
483  }
484 
485  usString& operator+=(const usString& _value)
486  {
487  unsigned int len1 = _value.strlen();
488  unsigned int len2 = strlen();
489 
490  unsigned int totalsize = len1 + len2 + 1;
491 
492  unsigned short* result = new unsigned short[totalsize];
493  memset(result, 0, totalsize * sizeof(unsigned short));
494  if (mString)
495  memcpy(result, mString, len2 * sizeof(unsigned short));
496 
497  memcpy(result + len2, _value.mString, len1 * sizeof(unsigned short));
498 
499  if (mString)
500  delete[] mString;
501  mString = result;
502 
503  return *this;
504 
505  }
506 
507  usString& operator+=(unsigned short _value)
508  {
509  unsigned int len2 = strlen();
510 
511 
512  unsigned int totalsize = 1 + len2 + 1;
513 
514  unsigned short* result = new unsigned short[totalsize];
515  result[totalsize - 2] = _value;
516  result[totalsize - 1] = 0;
517  if (mString)
518  memcpy(result, mString, len2 * sizeof(unsigned short));
519 
520  if (mString)
521  delete[] mString;
522  mString = result;
523 
524  return *this;
525 
526  }
527 
528  inline unsigned int hexaCharacterToInt(unsigned short character)
529  {
530  unsigned int result = 0;
531  if ((character >= 0x30) && (character <= 0x39))
532  {
533  result = character - 0x30;
534  }
535  else if ((character >= 0x61) && (character <= 0x66))
536  {
537  result = character - 0x61 + 10;
538  }
539  else if ((character >= 0x41) && (character <= 0x46)) // manage upper character also ?
540  {
541  result = character - 0x61 + 10;
542  }
543  return result;
544  }
545 
546  usString& operator+=(unsigned short* _value)
547  {
548  unsigned int len1 = strlen(_value);
549  unsigned int len2 = strlen();
550 
551  unsigned int totalsize = len1 + len2 + 1;
552 
553  unsigned short* result = new unsigned short[totalsize];
554  memset(result, 0, totalsize * sizeof(unsigned short));
555  if (mString)
556  memcpy(result, mString, len2 * sizeof(unsigned short));
557  memcpy(result + len2, _value, len1 * sizeof(unsigned short));
558 
559  if (mString)
560  delete[] mString;
561  mString = result;
562  return *this;
563  }
564 
565  void removeEscapeBackslash();
566 
567 protected:
568 
569  inline void copy(const char* str)
570  {
571  if (mString)
572  {
573  delete[] mString;
574  }
575  unsigned int len = strlen(str);
576  mString = new unsigned short[len + 1];
577  unsigned char* read = (unsigned char*)str;
578  unsigned short* write = mString;
579  *write = *read;
580 
581  while (*read)
582  {
583  *++write = *++read;
584  }
585 
586  }
587 
588  inline void copy(const unsigned short* str)
589  {
590  // mString is always valid and allocated
591  if (mString)
592  delete[] mString;
593 
594  unsigned int len = strlen(str);
595  mString = new unsigned short[len + 1];
596  memcpy(mString, str, (len + 1) * sizeof(unsigned short));
597  }
598 
599  void copy(const UTF8Char* str);
600 
601 
602  unsigned short* mString;
603 };
604 
605 
606 inline usString operator+(const usString& _l, const usString& _r)
607 {
608  usString ret = _l;
609  ret += _r;
610 
611  return ret;
612 }
613 
614 static inline usString ToLowerCase(const usString& a_entry)
615 {
616  std::locale loc;
617  usString str;
618 
619  unsigned int al = a_entry.length();
620 
621  for (unsigned int i = 0; i < al; ++i)
622  {
623  if (a_entry[i] < 128)
624  {
625  str += std::tolower((char)a_entry[i], loc);
626  }
627  else
628  {
629  str += a_entry[i];
630  }
631  }
632  return str;
633 }
634 
635 #endif // _USSTRING_H
operator<
bool operator<(const DrawableSorterItem &Item1, const DrawableSorterItem &Item2)
overload < operator for comparison
Definition: DrawableSorter.h:30