commore  1.0.6-SNAPSHOT
All Classes Namespaces Functions Variables Typedefs Enumerations Pages
AString.h
1 
2 //
3 // Copyright (c) 2006-2014 Raphael David / CANTOR
4 //
5 
6 #ifndef CMR_ASTRING_INCLUDED
7 #define CMR_ASTRING_INCLUDED
8 
9 
10 #include "Commore.h"
11 
12 namespace commore
13 {
17  struct RawAString
18  {
22  size_t size;
26  size_t capacity;
30  AChar data[4];
31  };
32  typedef RawAString* PRawAString;
33 
39  class CMREXD AString
40  {
41  friend class Iterator;
42 
43  public:
44  AString();
45  AString(const AString& str);
52  AString(const AString& str, size_t pos, size_t n = (size_t)npos);
58  AString(const AChar* str, size_t n);
63  AString(const AChar* str);
68  AString(const Char* str);
75  AString(size_t n, AChar c);
76  AString(AChar c, size_t n);
83  AString(const void* v, size_t n, size_t base = 16);
87  AString& operator = (const AString& str)
88  {
89  return assign(str);
90  }
95  AString& operator = (const AChar* str)
96  {
97  return assign(str);
98  }
103  AString& operator = (AChar c)
104  {
105  assign_helper(c, 1);
106  return *this;
107  }
111  operator const AChar* () const
112  {
113  return c_str();
114  }
115  ~AString();
116 
117  public:
118 
119  typedef AChar value_type;
120  typedef size_t size_type;
121  typedef AChar& reference;
122  typedef const AChar& const_reference;
123  typedef AChar* pointer;
124  typedef const AChar* const_pointer;
125 
126  enum { STRING_INCREMENT_SIZE = 16 };
127 
128  public:
132  bool operator == (const AString& str) const
133  {
134  return compare(str) == 0;
135  }
139  bool operator == (const AChar* str) const
140  {
141  return compare(str) == 0;
142  }
146  bool operator != (const AString& str) const
147  {
148  return !compare(str) == 0;
149  }
153  bool operator != (const AChar* str) const
154  {
155  return !compare(str) == 0;
156  }
160  bool operator < (const commore::AString& str) const
161  {
162  return compare(str) < 0;
163  }
167  bool operator > (const commore::AString& str) const
168  {
169  return compare(str) > 0;
170  }
174  AString& operator += (const AString& str)
175  {
176  return append(str);
177  }
181  AString operator + (const AString& str) const;
182 
186  AString& operator += (const AChar* str)
187  {
188  return append(str);
189  }
193  AString& operator += (AChar c)
194  {
195  append_helper(c, 1);
196  return *this;
197  }
198 
202  AString& append(const AString& str)
203  {
204  append_helper(str, 0, (size_t)npos);
205  return *this;
206  }
207 
214  AString& append(const AString& str, size_t pos, size_t n)
215  {
216  append_helper(str, pos, n);
217  return *this;
218  }
224  AString& append(const AChar* str, size_t n)
225  {
226  append_helper(str, n);
227  return *this;
228  }
233  AString& append(const AChar* str)
234  {
235  append_helper(str, length(str));
236  return *this;
237  }
238 
244  AString& append(size_t n, AChar c)
245  {
246  append_helper(c, n);
247  return *this;
248  }
249 
257  bool validate_range(size_t pos, size_t* len) const
258  {
259  size_t s = size();
260  if (pos > s)
261  {
262  *len = 0;
263  // ERROR OUT OF RANGE
264  return false;
265  }
266  if (*len > (s - pos))
267  {
268  *len = s - pos; // Clamp to actual length.
269  return false;
270  }
271  return true;
272  }
276  AString& assign(const AString& other);
280  AString& assign(const AString& str, size_t pos, size_t n)
281  {
282  if (str.validate_range(pos, &n))
283  {
284  assign_helper(str.data_->data + pos, n);
285  }
286  return *this;
287  }
291  AString& assign(const AChar* str, size_t n)
292  {
293  assign_helper(str, n);
294  return *this;
295  }
299  AString& assign(const AChar* str)
300  {
301  assign_helper(str, length(str));
302  return *this;
303  }
307  AString& assign(size_t n, AChar c)
308  {
309  assign_helper(c, n);
310  return *this;
311  }
312 
316  void swap(AString& str);
320  void splice(AString& str);
321 
327  AString& insert(size_t pos1, const AString& str)
328  {
329  replace_helper(pos1, 0, str, 0, (size_t)npos);
330  return *this;
331  }
339  AString& insert(size_t pos1, const AString& str, size_t pos2, size_t n)
340  {
341  replace_helper(pos1, 0, str, pos2, n);
342  return *this;
343  }
350  AString& insert(size_t pos, const AChar* str, size_t n)
351  {
352  replace_helper(pos, 0, str, n);
353  return *this;
354  }
360  AString& insert(size_t pos, const AChar* str)
361  {
362  replace_helper(pos, 0, str, length(str));
363  return *this;
364  }
371  AString& insert(size_t pos, size_t n, AChar c)
372  {
373  replace_helper(pos, 0, c, n);
374  return *this;
375  }
376 
381  {
382  init();
383  return *this;
384  }
390  AString& erase(size_t pos = 0, size_t n = (size_t)npos)
391  {
392  replace_helper(pos, n, (const AChar*)0, 0);
393  return *this;
394  }
398  void Delete(size_t pos = 0, size_t n = 1)
399  {
400  replace_helper(pos, n, (const AChar*)0, 0);
401  }
408  AString& replace(size_t pos1, size_t n, const AString& str)
409  {
410  replace_helper(pos1, n, str, 0, str.size());
411  return *this;
412  }
413 
422  AString& replace(size_t pos1, size_t n1, const AString& str, size_t pos2, size_t n2)
423  {
424  replace_helper(pos1, n1, str, pos2, n2);
425  return *this;
426  }
434  AString& replace(size_t pos1, size_t n1, const AChar* str, size_t n2)
435  {
436  replace_helper(pos1, n1, str, n2);
437  return *this;
438  }
445  AString& replace(size_t pos, size_t n1, const AChar* str)
446  {
447  replace_helper(pos, n1, str, length(str));
448  return *this;
449  }
457  AString& replace(size_t pos, size_t n1, size_t n2, AChar c)
458  {
459  replace_helper(pos, n1, c, n2);
460  return *this;
461  }
462 
467  AString& replace(const AChar* s1, const AChar* s2, bool case_sensitive = true);
471  AString& Replace(const AChar* s1, const AChar* s2, bool case_sensitive = true)
472  {
473  return replace(s1, s2, case_sensitive);
474  }
475 
480  AChar operator[](size_t pos) const
481  {
482  return pos == size() ? eos() : get_at(pos);
483  }
488  AChar& operator[](size_t pos)
489  {
490  if (pos < size())
491  {
492  return *(data_->data + pos);
493  }
494  else
495  {
496  return *((AChar*)0);
497  }
498  }
503  AChar operator[](int pos) const
504  {
505  return pos == (int)size() ? eos() : get_at((size_t)pos);
506  }
511  AChar& operator[](int pos)
512  {
513  if (pos < (int)size())
514  {
515  return *(data_->data + pos);
516  }
517  else
518  {
519  return *((AChar*)0);
520  }
521  }
525  AChar at(size_t pos) const
526  {
527  return get_at(pos);
528  }
529 
533  AChar& at(size_t pos)
534  {
535  return operator[](pos);
536  }
542  const AChar* c_str() const
543  {
544  return data_ ? data_->data : nil_data();
545  }
549  const AChar* data() const
550  {
551  return data_ ? data_->data : nil_data();
552  }
553 
559  size_t size() const
560  {
561  return data_->size;
562  }
566  size_t length() const
567  {
568  return data_->size;
569  }
576  size_t max_size() const
577  {
578  return (size_t)npos - 1;
579  }
583  bool is_empty() const
584  {
585  return size() == 0;
586  }
598  void resize(size_t n, AChar c)
599  {
600  resize_helper(n, c);
601  }
602  void resize(size_t n)
603  {
604  resize(n, eos());
605  }
606  void resize();
617  size_t capacity() const
618  {
619  return data_->capacity;
620  }
632  void reserve(size_t size)
633  {
634  reserve_helper(size);
635  }
643  char* set_buffer(size_t size)
644  {
645  return allocate(size);
646  }
656  size_t copy(AChar* str, size_t n, size_t pos = 0) const
657  {
658  if (validate_range(pos, &n))
659  {
660  copy(str, data_->data + pos, n);
661  }
662  return n;
663  }
664 
683  size_t find(const AString& str, size_t pos = 0) const { return find(str.data(), pos, str.size()); }
684  size_t find(const AChar* s, size_t pos, size_t n) const;
685  size_t find(const AChar* str, size_t pos = 0) const { return find(str, pos, length(str)); }
686  size_t find(AChar c, size_t pos = 0) const { return find(&c, pos, 1); }
687  size_t find_first_of(const AString& str, size_t pos = 0) const { return find_first_of(str.data(), pos, str.size()); }
688  size_t find_first_of(const AChar* s, size_t pos, size_t n) const;
689  size_t find_first_of(const AChar* str, size_t pos = 0) const { return find_first_of(str, pos, length(str)); }
690  size_t find_first_of(AChar c, size_t pos = 0) const { return find_first_of(&c, pos, 1); }
691  size_t find_first_not_of(const AString& str, size_t pos = 0) const { return find_first_not_of(str.data(), pos, str.size()); }
692  size_t find_first_not_of(const AChar* s, size_t pos, size_t n) const;
693  size_t find_first_not_of(AChar c, size_t pos = 0) const { return find_first_not_of(&c, pos, 1); }
694  size_t find_first_not_of(const AChar* str, size_t pos = 0) const { return find_first_not_of(str, pos, length(str)); }
695  size_t rfind(const AChar* str, size_t pos, size_t n) const;
696  size_t find_last_of(const AChar* s, size_t pos, size_t n) const;
697  size_t find_last_not_of(const AChar* s, size_t pos, size_t n) const;
698  size_t rfind(const AString& str, size_t pos = (size_t)npos) const { return rfind(str.data(), pos, str.size()); }
699  size_t rfind(const AChar* str, size_t pos = (size_t)npos) const { return rfind(str, pos, length(str)); }
700  size_t rfind(AChar c, size_t pos = (size_t)npos) const { return rfind(&c, pos, 1); }
701  size_t find_last_of(const AString& str, size_t pos = (size_t)npos) const { return find_last_of(str.data(), pos, str.size()); }
702  size_t find_last_of(AChar c, size_t pos = (size_t)npos) const { return find_last_of(&c, pos, 1); }
703  size_t find_last_of(const AChar* str, size_t pos = (size_t)npos) const { return find_last_of(str, pos, length(str)); }
704  size_t find_last_not_of(const AString& str, size_t pos = (size_t)npos) const { return find_last_not_of(str.data(), pos, str.size()); }
705  size_t find_last_not_of(const AChar& c, size_t pos = (size_t)npos) const { return find_last_not_of(&c, pos, 1); }
706  size_t find_last_not_of(const AChar* str, size_t pos = (size_t)npos) const { return find_last_not_of(str, pos, length(str)); }
707 
716  AString substr(size_t pos = 0, size_t n = (size_t)npos) const;
717 
739  int compare(const AString& str) const { return compare_helper(str, 0, (size_t)npos); }
740  int compare(size_t pos, size_t n, const AString& str) const { return compare_helper(str, pos, n); }
741  int compare(size_t pos1, size_t n1, const AString& str, size_t pos2, size_t n2) const
742  {
743  AString temp(str, pos2, n2);
744  return compare_helper(temp, pos1, n1);
745  }
746  int compare(const AChar* s) const
747  {
748  return compare_helper(s, 0, length(s));
749  }
750  int compare(size_t pos, size_t n1, const AChar* str, size_t n2 = (size_t)npos) const;
752 
757  int icompare(const AString& str) const
758  {
759  return icompare_helper(str, 0, (size_t)npos);
760  }
761  int icompare(size_t pos, size_t n, const AString& str) const
762  {
763  return icompare_helper(str, pos, n);
764  }
765  int icompare(size_t pos1, size_t n1, const AString& str, size_t pos2, size_t n2) const
766  {
767  AString temp(str, pos2, n2);
768  return icompare_helper(temp, pos1, n1);
769  }
770  int icompare(const AChar* s) const
771  {
772  return icompare_helper(s, 0, length(s));
773  }
774  int icompare(size_t pos, size_t n1, const AChar* str, size_t n2 = (size_t)npos) const;
776  int CompareNoCase(const AChar* s) const
777  {
778  return icompare_helper(s, 0, length(s));
779  }
781 
785  Int to_int() const;
786 
790  AString& make_upper();
792  AString& MakeUpper() { return make_upper(); }
793  AString& make_lower();
795  AString& MakeLower() { return make_lower();}
797 
805  bool match(const AString& pattern) const;
806  bool match(const AString& pattern, AString& a0) const;
807  bool match(const AString& pattern, AString& a0, AString& a1) const;
808  bool match(const AString& pattern, AString& a0, AString& a1, AString& a2) const;
809  bool match(const AString& pattern, AString& a0, AString& a1, AString& a2, AString& a3) const;
810  bool match(const AString& pattern, AString& a0, AString& a1, AString& a2, AString& a3, AString& a4) const;
811  bool match(const AString& pattern, AString& a0, AString& a1, AString& a2, AString& a3, AString& a4, AString& a5) const;
812  bool match(const AString& pattern, AString& a0, AString& a1, AString& a2, AString& a3, AString& a4, AString& a5, AString& a6) const;
813  bool match(const AString& pattern, AString& a0, AString& a1, AString& a2, AString& a3, AString& a4, AString& a5, AString& a6, AString& a7) const;
814 
819  long write_xml(OBStream& o) const;
824  long read_xml(IBStream& i);
825 
826  public:
830  static AChar eos()
831  {
832  return AChar();
833  }
834 
835  public:
839  static int compare(const AChar* s1, const AChar* s2, size_t n);
843  static int icompare(const AChar* s1, const AChar* s2, size_t n);
847  static const AChar* find(const AChar* s, int n, const AChar& a);
851  static size_t length(const AChar* s);
856  static AChar* copy(AChar* dest, const AChar* src, size_t n);
861  static AChar* move(AChar* dest, const AChar* src, size_t n);
865  static AChar* assign(AChar* dest, size_t n, const AChar& c);
866  static void assign(AChar& c1, const AChar& c2)
867  {
868  c1 = c2;
869  }
870  static bool eq(const AChar& c1, const AChar& c2)
871  {
872  return c1 == c2;
873  }
874  static bool ne(const AChar& c1, const AChar& c2)
875  {
876  return !eq(c1, c2);
877  }
878  static bool lt(const AChar& c1, const AChar& c2)
879  {
880  return c1 < c2;
881  }
882  static bool ieq(const AChar& c1, const AChar& c2);
883  static bool ine(const AChar& c1, const AChar& c2)
884  {
885  return !ieq(c1, c2);
886  }
887  static bool ilt(const AChar& c1, const AChar& c2);
888 
889  private:
890  AString& assign(AChar c, size_t n = 1)
891  {
892  assign_helper(c, n);
893  return *this;
894  }
895 
896  AString& append(AChar c, size_t n = 1)
897  {
898  append_helper(c, n);
899  return *this;
900  }
901 
902  const AChar& get_at(size_t pos) const
903  {
904  if (pos < size())
905  {
906  return *(data_->data + pos);
907  }
908  else
909  {
910  return *(const AChar*)0;
911  }
912  }
913 
914  void put_at(size_t pos, AChar c);
915 
916  void assign_helper(const AChar* data, size_t n);
917  void assign_helper(AChar c, size_t n);
918  void replace_helper(size_t pos, size_t n1, const AChar* s, size_t n2);
919  void replace_helper(size_t pos, size_t n1, AChar c, size_t n2);
920  void replace_helper(size_t pos, size_t n1, const AString& str, size_t pos2, size_t n2);
921  void append_helper(const AString& str, size_t pos, size_t n);
922  void append_helper(const AChar* s, size_t n);
923  void append_helper(AChar c, size_t n);
924 
925  int compare_helper(const AString& str, size_t pos, size_t n) const;
926  int compare_helper(const AChar* s, size_t pos, size_t n) const;
927 
928  int icompare_helper(const AString& str, size_t pos, size_t n) const;
929  int icompare_helper(const AChar* s, size_t pos, size_t n) const;
930 
931  void reserve_helper(size_t capacity);
932  void resize_helper(size_t new_count, AChar fill_char);
933 
934  AChar* allocate(size_t count);
935  void reallocate(size_t count);
936  void size(size_t n);
937  void init();
938  AChar* data()
939  {
940  return data_->data;
941  }
942  void assign_(PRawAString& dest, PRawAString src);
943  PRawAString reallocate_nocopy_(PRawAString p, size_t new_size);
944  PRawAString allocate_(size_t size);
945  void release_(PRawAString p);
946  PRawAString reallocate_(PRawAString p, size_t new_size);
947 
948  private:
949  RawAString* data_;
950 
951  private:
952  static AChar* nil_data();
953 
954  AString& Format(const AChar* format, ...);
955  }; // class AString
956 
957  long CMREXD read_xml(AString& sOut, IBStream& i);
958  long CMREXD write_xml(const AString& sIn, OBStream& o);
959  AString CMREXD create_guid();
960  AString CMREXD create_cmr_guid();
961 
962 }
963 
964 
965 CMREXD commore::AString operator +(const commore::AString& string1, const commore::AString& string2);
966 CMREXD commore::AString operator +(const commore::AString& string, commore::AChar ch);
967 CMREXD commore::AString operator +(commore::AChar ch, const commore::AString& string);
968 CMREXD commore::AString operator +(const commore::AString& string, const commore::AChar* lpsz);
969 CMREXD commore::AString operator +(const commore::AChar* lpsz, const commore::AString& string);
970 
971 
972 
973 
974 //CMREXD commore::Log& operator << (commore::Log& log, const commore::AString& t);
975 //CMREXD commore::Log& operator << (commore::Log& log, const commore::ListAString& t);
976 
977 CMREXD commore::OBStream& operator << (commore::OBStream& o, const commore::AString& s);
978 CMREXD commore::IBStream& operator >> (commore::IBStream& i, commore::AString& s);
979 const CMREXD commore::OBStreamFormat& operator << (const commore::OBStreamFormat& o, const commore::AString&);
980 
981 CMREXD void DecodeFromUTF8(const commore::AString& source, commore::AString& dest);
982 
983 
984 #endif
AChar operator[](int pos) const
Definition: AString.h:503
AString & insert(size_t pos1, const AString &str, size_t pos2, size_t n)
Definition: AString.h:339
size_t capacity
Definition: AString.h:26
void Delete(size_t pos=0, size_t n=1)
Definition: AString.h:398
AString & MakeLower()
Definition: AString.h:795
AString & replace(size_t pos, size_t n1, size_t n2, AChar c)
Definition: AString.h:457
AString & erase(size_t pos=0, size_t n=(size_t) npos)
Definition: AString.h:390
Definition: AString.h:39
AChar operator[](size_t pos) const
Definition: AString.h:480
const AChar * data() const
Definition: AString.h:549
AString & Replace(const AChar *s1, const AChar *s2, bool case_sensitive=true)
Definition: AString.h:471
Definition: AString.h:17
AString & replace(size_t pos1, size_t n, const AString &str)
Definition: AString.h:408
AString & insert(size_t pos1, const AString &str)
Definition: AString.h:327
Definition: IOBStream.h:126
AString & append(const AString &str, size_t pos, size_t n)
Definition: AString.h:214
AString & append(size_t n, AChar c)
Definition: AString.h:244
size_t length() const
Definition: AString.h:566
bool is_empty() const
Definition: AString.h:583
bool validate_range(size_t pos, size_t *len) const
Definition: AString.h:257
size_t max_size() const
Definition: AString.h:576
size_t capacity() const
Return size of allocated storage.
Definition: AString.h:617
AChar & operator[](int pos)
Definition: AString.h:511
AString & insert(size_t pos, const AChar *str)
Definition: AString.h:360
void resize(size_t n, AChar c)
Definition: AString.h:598
AString & append(const AChar *str)
Definition: AString.h:233
int compare(const AString &str) const
Compare string.
Definition: AString.h:739
char * set_buffer(size_t size)
Definition: AString.h:643
size_t find(const AString &str, size_t pos=0) const
Find content in string Searches the string for the first occurrence of the sequence specified by its ...
Definition: AString.h:683
const AChar * c_str() const
Definition: AString.h:542
AString & clear()
Definition: AString.h:380
size_t copy(AChar *str, size_t n, size_t pos=0) const
Copy sequence of characters from string Copies a substring of the current value of the string object ...
Definition: AString.h:656
AString & assign(const AChar *str)
Definition: AString.h:299
int icompare(const AString &str) const
Compare string but do not care caracter case (acii charecter only)
Definition: AString.h:757
int CompareNoCase(const AChar *s) const
Definition: AString.h:776
AString & replace(size_t pos, size_t n1, const AChar *str)
Definition: AString.h:445
AString & insert(size_t pos, const AChar *str, size_t n)
Definition: AString.h:350
static AChar eos()
Definition: AString.h:830
AString & append(const AChar *str, size_t n)
Definition: AString.h:224
AString & MakeUpper()
Definition: AString.h:792
AString & replace(size_t pos1, size_t n1, const AString &str, size_t pos2, size_t n2)
Definition: AString.h:422
void reserve(size_t size)
Request a change in capacity Requests that the string capacity be adapted to a planned change in size...
Definition: AString.h:632
AString & assign(size_t n, AChar c)
Definition: AString.h:307
size_t size() const
Definition: AString.h:559
AString & assign(const AChar *str, size_t n)
Definition: AString.h:291
AChar & operator[](size_t pos)
Definition: AString.h:488
size_t size
Definition: AString.h:22
AString & assign(const AString &str, size_t pos, size_t n)
Definition: AString.h:280
AChar & at(size_t pos)
Definition: AString.h:533
AChar data[4]
Definition: AString.h:30
Definition: IOBStream.h:166
AString & insert(size_t pos, size_t n, AChar c)
Definition: AString.h:371
AString & append(const AString &str)
Definition: AString.h:202
Definition: IOBStream.h:29
AString & replace(size_t pos1, size_t n1, const AChar *str, size_t n2)
Definition: AString.h:434
AChar at(size_t pos) const
Definition: AString.h:525