Dynalib Utils
String.h
Go to the documentation of this file.
1 
2 #ifndef STRING_H_INCLUDED
3 #define STRING_H_INCLUDED
4 
5 /*******************************************************************/
6 // class String
7 // String is a lightweight wrapper class for std::string
8 // (a.k.a typedef std::basic_string<...>)
9 //
10 // The article that describes this class is:
11 // "String - A lightweight wrapper for std::string"
12 // and can be found at:
13 // http://www.codeproject.com/
14 //
15 // Features:
16 // - String contains no templates in the interface (almost).
17 // - wrappers are provided for iterator and const_iterator
18 // - no namespace is used
19 // - the implementation mostly forwards calls to the underlying std::string
20 //
21 // the implementation is platform independent;
22 // _WIN32: automatic adaption to char or wchar_t (according to macro _UNICODE)
23 //
24 // introductory material to std::string:
25 // http://www.msoe.edu/eecs/cese/resources/stl/string.htm
26 // http://www.cppreference.com/cppstring/
27 //
28 // Windows specific:
29 // The Complete Guide to C++ Strings
30 // http://www.codeproject.com/string/CPPStringGuide1.asp
31 // http://www.codeproject.com/string/CPPStringGuide2.asp
32 /*******************************************************************/
33 
34 
35 #if defined (_MSC_VER) && _MSC_VER > 1000
36 # pragma once
37 # pragma warning (disable : 4786)
38 #endif
39 
40 #include <iterator>
41 #include <algorithm>
42 #include <string>
43 #include <vector>
44 #include <istream>
45 #include <ostream>
46 #include <stdexcept>
47 #include <assert.h>
48 #include <sstream>
49 #include "IHashable.h"
50 #include "ICopyable.h"
51 #include "HashCoder.h"
52 #include "Utils.h"
53 
54 //using namespace std;
55 
56 #if defined (_WIN32)
57 # include<tchar.h> // _TCHAR
58 #endif
59 
60 class String : IHashable<std::basic_string<char>>, ICopyable<String> {
61 // change this typedef to adapt String to any char type if desired
62 #if defined (_WIN32)
63  typedef _TCHAR charT;
64 #else
65  typedef char charT;
66 #endif
67  typedef std::basic_string<charT> string_type;
68 
69 public:
70  // types:
71  typedef string_type::traits_type traits_type;
72  typedef string_type::value_type value_type;
73  typedef string_type::allocator_type allocator_type;
74  typedef string_type::size_type size_type;
75  typedef string_type::difference_type difference_type;
76  typedef string_type::reference reference;
77  typedef string_type::const_reference const_reference;
78  typedef string_type::pointer pointer;
79  typedef string_type::const_pointer const_pointer;
80 
81  int hashCode() const override {
82  auto code = HashCoder();
83  code.add(imp);
84  return code.getCode();
85  }
86  bool operator== (const std::basic_string<char> &other) const override {
87  return imp == other;
88  }
89  bool operator!= (const std::basic_string<char> &other) const {
90  return !(imp == other);
91  }
92 
93  String* copy() override {
94  return new String(*this);
95  }
96 
97 
98 // easily switch from the new iterators below to the
99 // basic_string<...>::iterators (you also need to change function base_iter())
100 #if 0
101  typedef string_type::iterator iterator; //See 23.1
102  typedef string_type::const_iterator const_iterator; // See 23.1
103 
104 //----- NEW ITERATORs BEGIN -----------------------------------------
105 #else
106  struct iterator {
107  typedef std::random_access_iterator_tag iterator_category;
108  typedef string_type::value_type value_type;
109  typedef string_type::reference reference;
110  typedef string_type::pointer pointer;
111  typedef string_type::difference_type difference_type;
112 
113  iterator() : iterImp (string_type::iterator()) {}
114  iterator (const iterator& other) : iterImp (other.iterImp) {}
115  private:
116  iterator (const string_type::iterator& other) : iterImp (other) {}
117  public:
118  reference operator*() const { return *iterImp; }
119  pointer operator->() const { return &(*iterImp); }
120 
121  iterator& operator++() { ++iterImp; return *this; }
122  iterator operator++(int){ return iterImp++; }
123  iterator& operator--() { --iterImp; return *this; }
124  iterator operator--(int){ return iterImp--; }
125 
126  iterator operator+ (difference_type n) const { return (iterImp + n); }
127  iterator& operator+= (difference_type n) { iterImp += n; return *this; }
128  iterator operator- (difference_type n) const { return (iterImp - n); }
129  iterator& operator-= (difference_type n) { iterImp -= n; return *this; }
130  difference_type operator- (const iterator& other) const { return (iterImp - other.iterImp); }
131  reference operator[] (difference_type n) const { return iterImp[n]; }
132 
133  friend bool operator== (const iterator& lhs, const iterator& rhs) {
134  return lhs.iterImp == rhs.iterImp;
135  }
136  friend bool operator!= (const iterator& lhs, const iterator& rhs) {
137  return lhs.iterImp != rhs.iterImp;
138  }
139  friend bool operator< (const iterator& lhs, const iterator& rhs) {
140  return lhs.iterImp < rhs.iterImp;
141  }
142  friend bool operator> (const iterator& lhs, const iterator& rhs) {
143  return lhs.iterImp > rhs.iterImp;
144  }
145  friend bool operator<= (const iterator& lhs, const iterator& rhs) {
146  return lhs.iterImp <= rhs.iterImp;
147  }
148  friend bool operator>= (const iterator& lhs, const iterator& rhs) {
149  return lhs.iterImp >= rhs.iterImp;
150  }
151  private:
152  friend class String;
153  friend struct const_iterator;
154  string_type::iterator iterImp;
155  };
156 
157  struct const_iterator {
158  typedef std::random_access_iterator_tag iterator_category;
159  typedef string_type::value_type value_type;
160  typedef string_type::const_reference reference;
161  typedef string_type::const_pointer pointer;
162  typedef string_type::difference_type difference_type;
163 
164  const_iterator() : iterImp (string_type::const_iterator()) {}
165  const_iterator (const const_iterator& other) : iterImp (other.iterImp) {};
166  const_iterator (const iterator& other) : iterImp(other.iterImp) {};
167  private:
168  const_iterator (const string_type::const_iterator& other) : iterImp (other) {};
169  public:
170  reference operator*() const { return *iterImp; }
171  pointer operator->() const { return &(*iterImp); }
172 
173  const_iterator& operator++() { ++iterImp; return *this; }
174  const_iterator operator++(int){ return (const_iterator)iterImp++; }
175  const_iterator& operator--() { --iterImp; return *this; }
176  const_iterator operator--(int){ return (const_iterator)iterImp--; }
177 
178  const_iterator operator+ (difference_type n) const { return (iterImp + n); }
179  const_iterator& operator+= (difference_type n) { iterImp += n; return *this; }
180  const_iterator operator- (difference_type n) const { return (iterImp - n); }
181  const_iterator& operator-= (difference_type n) { iterImp -= n; return *this; }
182  difference_type operator- (const const_iterator& other) const { return (iterImp - other.iterImp); }
183  reference operator[] (difference_type n) const { return iterImp[n]; }
184 
185  friend bool operator== (const const_iterator& lhs, const const_iterator& rhs) {
186  return lhs.iterImp == rhs.iterImp;
187  }
188  friend bool operator!= (const const_iterator& lhs, const const_iterator& rhs) {
189  return lhs.iterImp != rhs.iterImp;
190  }
191  friend bool operator< (const const_iterator& lhs, const const_iterator& rhs) {
192  return lhs.iterImp < rhs.iterImp;
193  }
194  friend bool operator> (const const_iterator& lhs, const const_iterator& rhs) {
195  return lhs.iterImp > rhs.iterImp;
196  }
197  friend bool operator<= (const const_iterator& lhs, const const_iterator& rhs) {
198  return lhs.iterImp <= rhs.iterImp;
199  }
200  friend bool operator>= (const const_iterator& lhs, const const_iterator& rhs) {
201  return lhs.iterImp >= rhs.iterImp;
202  }
203  private:
204  friend class String;
205  string_type::const_iterator iterImp;
206  };
207 #endif
208 
209 //----- NEW ITERATORs END -------------------------------------------
210 
211  typedef string_type::reverse_iterator reverse_iterator;
212  typedef string_type::const_reverse_iterator const_reverse_iterator;
213 
214  enum { npos = size_type (-1) };
215 
216  // construct/copy/destroy:
217  explicit String() {}
218  String (const String& str, size_type pos = 0, size_type n = npos) : imp (str.imp, pos, n) {};
219  String (const string_type& str, size_type pos = 0, size_type n = npos) : imp (str, pos, n) {};
220  String (const charT* s, size_type n) : imp (s, n) { assert (s); }
221  String (const charT* s) : imp (s) { assert (s); }
222  String (size_type n, charT c) : imp (n, c) {};
223  template<class InputIterator>
224  String (InputIterator first, InputIterator last) : imp (first, last) {}
226  }
227 
228  String& operator= (const String& str) { imp = str.imp; return *this; }
229  String& operator= (const string_type& str){ imp = str; return *this; }
230  String& operator= (const charT* s) { assert (s); imp = s; return *this; }
231  String& operator= (charT c) { imp = c; return *this; }
232 
233  // operator string_type() const { return imp; }
234 
235  // iterators:
236  iterator begin() { return imp.begin(); }
237  const_iterator begin() const { return imp.begin(); }
238  iterator end() { return imp.end(); }
239  const_iterator end() const { return imp.end(); }
240  reverse_iterator rbegin() { return imp.rbegin(); }
241  const_reverse_iterator rbegin() const { return imp.rbegin(); }
242  reverse_iterator rend() { return imp.rend(); }
243  const_reverse_iterator rend() const { return imp.rend(); }
244 
245  // capacity:
246  size_type size() const { return imp.size(); }
247  size_type length() const { return imp.length(); }
248  size_type max_size() const { return imp.max_size(); }
249  void resize (size_type n, charT c) { imp.resize (n, c); }
250  void resize (size_type n) { imp.resize (n); }
251  size_type capacity() const { return imp.capacity(); }
252  void reserve (size_type n = 0) { imp.reserve (n); }
253  void clear() { imp.erase (base_iter(begin()), base_iter(end())); } // no clear() in MSVC++ 6.0
254  bool empty() const { return imp.empty(); }
255 
256  // element access:
257  reference operator[] (size_type pos) { return imp[pos]; }
258  const_reference operator[] (size_type pos) const { return imp[pos]; }
259  reference at (size_type n) { return imp.at(n); }
260  const_reference at (size_type n) const { return imp.at(n); }
261 
262 
263  // modifiers:
264  String& operator+= (const String& str) { imp += (str.imp); return *this; }
265  String& operator+= (const charT* s) { assert(s); imp += (s); return *this; }
266  String& operator+= (charT c) { imp += (c); return *this; }
267  String& append (const String& str) { imp.append(str.imp); return *this; }
268  String& append (const String& str, size_type pos, size_type n) { imp.append(str.imp, pos, n); return *this; }
269  String& append (const charT* s, size_type n) { assert(s); imp.append (s, n); return *this; }
270  String& append (const charT* s) { assert(s); imp.append (s); return *this; }
271  String& append (size_type n, charT c) { imp.append(n, c); return *this; }
272  template<class InputIterator>
273  String& append (InputIterator first, InputIterator last) { imp.append (first, last); return *this; }
274  void push_back (const charT c) { imp += (c); } // // no push_back() in MSVC++ 6.0
275 
276  String& assign (const String& str) { imp.assign(str.imp); return *this; }
277  String& assign (const String& str, size_type pos, size_type n)
278  { imp.assign(str.imp, pos, n); return *this; }
279  String& assign (const charT* s, size_type n) { assert(s); imp.assign (s, n); return *this; }
280  String& assign (const charT* s) { assert(s); imp.assign (s); return *this; }
281  String& assign (size_type n, charT c) { imp.assign(n, c); return *this; }
282  template<class InputIterator>
283  String& assign (InputIterator first, InputIterator last)
284  { imp.assign(first, last); return *this; }
285 
286  String& insert (size_type pos1, const String& str)
287  { imp.insert(pos1, str.imp); return *this; }
288  String& insert (size_type pos1, const String& str, size_type pos2, size_type n)
289  { imp.insert(pos1, str.imp, pos2, n); return *this; }
290  String& insert (size_type pos, const charT* s, size_type n)
291  { assert(s); imp.insert(pos, s, n); return *this; }
292  String& insert (size_type pos, const charT* s)
293  { assert(s); imp.insert(pos, s); return *this; }
294  String& insert (size_type pos, size_type n, charT c)
295  { imp.insert(pos, n, c); return *this; }
296  iterator insert (iterator p, charT c)
297  { return imp.insert(base_iter (p), c); }
298  void insert (iterator p, size_type n, charT c)
299  { imp.insert(base_iter (p), n, c); }
300  template<class InputIterator>
301  void insert (iterator p, InputIterator first, InputIterator last)
302  { imp.insert(base_iter (p), first, last); }
303 
304  String& erase (size_type pos = 0, size_type n = npos)
305  { imp.erase(pos, n); return *this; }
306  iterator erase (iterator position)
307  { return imp.erase(base_iter(position)); }
308  iterator erase (iterator first, iterator last)
309  { return imp.erase(base_iter(first), base_iter(last)); }
310  String& replace (size_type pos1, size_type n1, const String& str)
311  { imp.replace(pos1, n1, str.imp); return *this; }
312  String& replace (size_type pos1, size_type n1, const String& str, size_type pos2, size_type n2)
313  { imp.replace(pos1, n1, str.imp, pos2, n2); return *this; }
314  String& replace (size_type pos, size_type n1, const charT* s, size_type n2)
315  { assert(s); imp.replace(pos, n1, s, n2); return *this; }
316  String& replace (size_type pos, size_type n1, const charT* s)
317  { assert (s); imp.replace(pos, n1, s); return *this; }
318  String& replace (size_type pos, size_type n1, size_type n2, charT c)
319  { imp.replace(pos, n1, n2, c); return *this; }
320  String& replace (iterator& i1, iterator& i2, const String& str)
321  { imp.replace(base_iter (i1), base_iter(i2), str.imp); return *this; }
322  String& replace (iterator& i1, iterator& i2, const charT* s, size_type n)
323  { assert(s); imp.replace(base_iter(i1), base_iter(i2), s, n); return *this; }
324  String& replace (iterator& i1, iterator& i2, const charT* s)
325  { assert(s); imp.replace(base_iter(i1), base_iter(i2), s); return *this; }
326  String& replace (iterator& i1, iterator& i2, size_type n, charT c)
327  { imp.replace(base_iter(i1), base_iter(i2), n, c); return *this; }
328  template<class InputIterator>
329  String& replace (iterator& i1, iterator& i2,InputIterator j1, InputIterator j2)
330  { imp.replace(base_iter(i1), base_iter(i2), j1, j2); return *this; }
331  size_type copy (charT* s, size_type n, size_type pos = 0) const
332  { assert(s); return imp.copy(s, n, pos); }
333 
334 
335  void swap (String& str) { imp.swap(str.imp); }
336  friend inline void swap (String& lhs, String& rhs) { lhs.swap (rhs); }
337 
338  // additional swap functions to exchange string contents with std::string (std::basic_string);
339  void swap (string_type& str) { imp.swap (str); }
340  friend inline void swap (String& lhs, string_type& rhs) { lhs.imp.swap(rhs); }
341  friend inline void swap (string_type& lhs, String& rhs) { lhs.swap(rhs.imp); }
342 
343  // string operations:
344  const charT* c_str() const { return imp.c_str(); }
345  const charT* data() const { return imp.data(); }
346 
347  allocator_type get_allocator() const { return imp.get_allocator(); }
348 
349  size_type find (const String& str, size_type pos = 0) const
350  { return imp.find(str.imp, pos); }
351  size_type find (const charT* s, size_type pos, size_type n) const
352  { assert(s); return imp.find(s, pos, n); }
353  size_type find (const charT* s, size_type pos = 0) const
354  { assert(s); return imp.find(s, pos); }
355  size_type find (charT c, size_type pos = 0) const
356  { return imp.find(c, pos); }
357  size_type rfind (const String& str, size_type pos = npos) const
358  { return imp.rfind(str.imp, pos); }
359  size_type rfind (const charT* s, size_type pos, size_type n) const
360  { assert(s); return imp.rfind(s, pos, n); }
361  size_type rfind (const charT* s, size_type pos = npos) const
362  { assert(s); return imp.rfind(s, pos); }
363  size_type rfind (charT c, size_type pos = npos) const
364  { return imp.rfind(c, pos); }
365 
366  size_type find_first_of (const String& str, size_type pos = 0) const
367  { return imp.find_first_of(str.imp, pos); }
368  size_type find_first_of (const charT* s, size_type pos, size_type n) const
369  { assert(s); return imp.find_first_of(s, pos, n); }
370  size_type find_first_of (const charT* s, size_type pos = 0) const
371  { assert (s); return imp.find_first_of(s, pos); }
372  size_type find_first_of (charT c, size_type pos = 0) const
373  { return imp.find_first_of(c, pos); }
374 
375  size_type find_last_of (const String& str, size_type pos = npos) const
376  { return imp.find_last_of(str.imp, pos); }
377  size_type find_last_of (const charT* s, size_type pos, size_type n) const
378  { assert(s); return imp.find_last_of(s, pos, n); }
379  size_type find_last_of (const charT* s, size_type pos = npos) const
380  { assert(s); return imp.find_last_of(s, pos); }
381  size_type find_last_of (charT c, size_type pos = npos) const
382  { return imp.find_last_of(c, pos); }
383 
384  size_type find_first_not_of (const String& str, size_type pos = 0) const
385  { return imp.find_first_not_of(str.imp, pos); }
386  size_type find_first_not_of (const charT* s, size_type pos, size_type n) const
387  { assert(s); return imp.find_first_not_of(s, pos, n); }
388  size_type find_first_not_of (const charT* s, size_type pos = 0) const
389  { assert(s); return imp.find_first_not_of(s, pos); }
390  size_type find_first_not_of (charT c, size_type pos = 0) const
391  { return imp.find_first_not_of(c, pos); }
392 
393  size_type find_last_not_of (const String& str, size_type pos = npos) const
394  { return imp.find_last_not_of(str.imp, pos); }
395  size_type find_last_not_of (const charT* s, size_type pos, size_type n) const
396  { assert(s); return imp.find_last_not_of(s, pos, n); }
397  size_type find_last_not_of (const charT* s, size_type pos = npos) const
398  { assert(s); return imp.find_last_not_of(s, pos); }
399  size_type find_last_not_of (charT c, size_type pos = npos) const
400  { return imp.find_last_not_of(c, pos); }
401 
402 
403  // String& ltrim_inplace() {
404  // imp.erase(imp.begin(), find_if(imp.begin(), imp.end(), [](int ch) { return !isspace(ch); }));
405  // return *this;
406  // }
407 
408  // // trim from end (in place)
409  // String& rtrim_inplace() {
410  // imp.erase(find_if(imp.rbegin(), imp.rend(), [](int ch) { return !isspace(ch); }).base(), imp.end());
411  // return *this;
412  // }
413 
414  // // trim from both ends (in place)
415  // String& trim_inplace() {
416  // ltrim_inplace();
417  // rtrim_inplace();
418  // return *this;
419  // }
420 
421  String trim() const {
422  auto wsfront=find_if_not(imp.begin(),imp.end(),[](int c) { return isspace(c);});
423  auto wsback=find_if_not(imp.rbegin(),imp.rend(),[](int c) { return isspace(c);}).base();
424  return (wsback <= wsfront ? String() : String(wsfront, wsback));
425  }
426 
427  String* trim_copy() const {
428  auto wsfront=find_if_not(imp.begin(),imp.end(),[](int c) { return isspace(c);});
429  auto wsback=find_if_not(imp.rbegin(),imp.rend(),[](int c) { return isspace(c);}).base();
430  return (wsback <= wsfront ? new String() : new String(wsfront, wsback));
431  }
432 
434  imp.erase(imp.begin(), find_if(imp.begin(),imp.end(),[](int c) {
435  return !isspace(c);
436  }));
437  imp.erase(find_if(imp.rbegin(),imp.rend(),[](int c) {
438  return !isspace(c);
439  }).base(), imp.end());
440  return *this;
441  }
442 
443  String tolower() const {
444  if (!mapsInitFlag)
445  initConvertMaps();
446  String str2;
447  for (char i : imp)
448  str2.append(1, uToL[i]);
449  return String(str2);
450  }
451 
452  String* tolower_copy() const {
453  if (!mapsInitFlag)
454  initConvertMaps();
455  auto str2 = new String();
456  for (char i : imp)
457  str2->append(1, uToL[i]);
458  return str2;
459  }
460 
462  if (!mapsInitFlag)
463  initConvertMaps();
464  for (unsigned long i = 0, len = imp.length(); i < len; ++i)
465  imp.replace(i, 1, 1, uToL[imp[i]]);
466  return *this;
467  }
468 
469  String toupper() const {
470  if (!mapsInitFlag)
471  initConvertMaps();
472  String str2;
473  for (char i : imp)
474  str2.append(1, lToU[i]);
475  return String(str2);
476  }
477 
478  String* toupper_copy() const {
479  if (!mapsInitFlag)
480  initConvertMaps();
481  auto str2 = new String();
482  for (char i : imp)
483  str2->append(1, lToU[i]);
484  return str2;
485  }
486 
488  if (!mapsInitFlag)
489  initConvertMaps();
490  for (unsigned long i = 0, len = imp.length(); i < len; ++i)
491  imp.replace(i, 1, 1, lToU[imp[i]]);
492  return *this;
493  }
494 
495  String substr (size_type pos = 0, size_type n = npos) const {
496  if (pos <= size()) { // see C++ Standard 21.3.6.7
497  size_type rlen = n < size()-pos ? n : size()-pos;
498  return String (c_str() + pos, rlen);
499  } else {
500  throw std::out_of_range("String::substr");
501  }
502  }
503 
504  std::vector<std::string> split (char delim) const {
505  std::stringstream ss(imp);
506  std::string item;
507  std::vector<std::string> splittedString;
508  while (getline(ss, item, delim)) {
509  splittedString.push_back(item);
510  }
511  return splittedString;
512  }
513 
514  std::vector<std::string> split (const string_type& delim) const {
515  std::vector<std::string> splittedString;
516  size_type startIndex = 0;
517  size_type endIndex = 0;
518  while ( (endIndex = imp.find(delim, startIndex)) < imp.size() ) {
519  std::string val = imp.substr(startIndex, endIndex - startIndex);
520  splittedString.push_back(val);
521  startIndex = endIndex + delim.size();
522  }
523  if (startIndex < imp.size()) {
524  std::string val = imp.substr(startIndex);
525  splittedString.push_back(val);
526  }
527  return splittedString;
528  }
529 
530  int compare (const String& str) const
531  { return imp.compare(str.imp); }
532  int compare (size_type pos1, size_type n1, const String& str) const
533  { return imp.compare(pos1, n1, str.imp); }
534  int compare (size_type pos1, size_type n1, const String& str, size_type pos2, size_type n2) const
535  { return imp.compare(pos1, n1, str.imp, pos2, n2); }
536  int compare (const charT* s) const
537  { assert(s); return imp.compare(s); }
538  int compare (size_type pos1, size_type n1, const charT* s, size_type n2 = npos) const
539  { assert(s); return imp.compare(pos1, n1, s, n2); }
540 
541  friend inline String operator+ (const String& lhs, const String& rhs) {
542  return String (lhs, rhs, false);
543  }
544  friend inline String operator+ (const charT* lhs, const String& rhs) {
545  assert(lhs);
546  return String (lhs, rhs);
547  }
548  friend inline String operator+ (charT lhs, const String& rhs) {
549  return String(lhs, rhs);
550  }
551  friend inline String operator+ (const String& lhs, const charT* rhs) {
552  assert(rhs);
553  return String(lhs, rhs);
554  }
555  friend inline String operator+ (const String& lhs, charT rhs) {
556  return String(lhs, rhs);
557  }
558 
559  friend inline bool operator== (String& lhs, String& rhs) { return lhs.imp == rhs.imp; }
560  friend inline bool operator== (const charT* lhs, String& rhs) {
561  assert(lhs);
562  return lhs == rhs.imp;
563  }
564  friend inline bool operator== (String& lhs, const charT* rhs) {
565  assert(rhs);
566  return lhs.imp == rhs;
567  }
568  friend inline bool operator< (String& lhs, String& rhs) { return lhs.imp < rhs.imp; }
569  friend inline bool operator< (String& lhs, const charT* rhs) {
570  assert(rhs);
571  return lhs.imp < rhs;
572  }
573  friend inline bool operator< (const charT* lhs, String& rhs) {
574  assert(lhs);
575  return lhs < rhs.imp;
576  }
577  friend inline bool operator== (const String& lhs, const String& rhs) { return lhs.imp == rhs.imp; }
578  friend inline bool operator== (const charT* lhs, const String& rhs) {
579  assert(lhs);
580  return lhs == rhs.imp;
581  }
582  friend inline bool operator== (const String& lhs, const charT* rhs) {
583  assert(rhs);
584  return lhs.imp == rhs;
585  }
586  friend inline bool operator< (const String& lhs, const String& rhs) { return lhs.imp < rhs.imp; }
587  friend inline bool operator< (const String& lhs, const charT* rhs) {
588  assert(rhs);
589  return lhs.imp < rhs;
590  }
591  friend inline bool operator< (const charT* lhs, const String& rhs) {
592  assert(lhs);
593  return lhs < rhs.imp;
594  }
595 
596  friend inline bool operator!= (String& lhs, String& rhs) { return !(lhs == rhs); }
597  friend inline bool operator!= (const charT* lhs, String& rhs) { return !(lhs == rhs); }
598  friend inline bool operator!= (String& lhs, const charT* rhs) { return !(lhs == rhs); }
599  friend inline bool operator> (String& lhs, String& rhs) { return rhs < lhs; }
600  friend inline bool operator> (String& lhs, const charT* rhs) { return rhs < lhs; }
601  friend inline bool operator> (const charT* lhs, String& rhs) { return rhs < lhs; }
602  friend inline bool operator<= (String& lhs, String& rhs) { return !(rhs < lhs); }
603  friend inline bool operator<= (String& lhs, const charT* rhs) { return !(rhs < lhs); }
604  friend inline bool operator<= (const charT* lhs, String& rhs) { return !(rhs < lhs); }
605  friend inline bool operator>= (String& lhs, String& rhs) { return !(lhs < rhs); }
606  friend inline bool operator>= (String& lhs, const charT* rhs) { return !(lhs < rhs); }
607  friend inline bool operator>= (const charT* lhs, String& rhs) { return !(lhs < rhs); }
608 
609  friend inline bool operator!= (const String& lhs, const String& rhs) { return !(lhs == rhs); }
610  friend inline bool operator!= (const charT* lhs, const String& rhs) { return !(lhs == rhs); }
611  friend inline bool operator!= (const String& lhs, const charT* rhs) { return !(lhs == rhs); }
612  friend inline bool operator> (const String& lhs, const String& rhs) { return rhs < lhs; }
613  friend inline bool operator> (const String& lhs, const charT* rhs) { return rhs < lhs; }
614  friend inline bool operator> (const charT* lhs, const String& rhs) { return rhs < lhs; }
615  friend inline bool operator<= (const String& lhs, const String& rhs) { return !(rhs < lhs); }
616  friend inline bool operator<= (const String& lhs, const charT* rhs) { return !(rhs < lhs); }
617  friend inline bool operator<= (const charT* lhs, const String& rhs) { return !(rhs < lhs); }
618  friend inline bool operator>= (const String& lhs, const String& rhs) { return !(lhs < rhs); }
619  friend inline bool operator>= (const String& lhs, const charT* rhs) { return !(lhs < rhs); }
620  friend inline bool operator>= (const charT* lhs, const String& rhs) { return !(lhs < rhs); }
621 
622  friend inline std::basic_ostream<charT, string_type::traits_type>&
623  operator<< (std::basic_ostream<charT, string_type::traits_type>& os, const String& str) {
624  return os << str.imp;
625  }
626 
627  friend inline std::basic_istream <charT, string_type::traits_type>&
628  operator>> (std::basic_istream<charT, string_type::traits_type>& is, String& str) {
629  is >> str.imp;
630  return is;
631  }
632 
633  friend inline std::basic_istream<charT, string_type::traits_type>&
634  getline(std::basic_istream<charT, string_type::traits_type>& is, String& str, charT delim) {
635  std::getline(is, str.imp, delim);
636  return is;
637  }
638 
639  friend inline std::basic_istream<charT, string_type::traits_type>&
640  getline(std::basic_istream<charT, string_type::traits_type>& is, String& str) {
641  return getline(is, str.imp, is.widen('\n'));
642  }
643 
644 private:
645  // private constructors, used only to facilitate RVO for operator+()
646 
647  // the third argument is not used, it disambiguates parsing for older compilers
648  explicit String (const String& lhs, const String& rhs, bool/*not used*/) {
649  imp.reserve(lhs.length() + rhs.length());
650  imp.append(lhs.imp).append (rhs.imp);
651  }
652  explicit String (const charT* lhs, const String& rhs) {
653  assert(lhs);
654  size_type lhsLen = std::char_traits<charT>::length (lhs);
655  imp.reserve(lhsLen + rhs.length());
656  imp.append(lhs, lhsLen).append (rhs.imp);
657  }
658  explicit String (charT lhs, const String& rhs) {
659  imp.reserve(1 + rhs.length());
660  imp.append(1, lhs).append (rhs.imp);
661  }
662  explicit String (const String& lhs, const charT* rhs) {
663  assert(rhs);
664  size_type rhsLen = std::char_traits<charT>::length (rhs);
665  imp.reserve(lhs.length() + rhsLen);
666  imp.append(lhs.imp).append (rhs, rhsLen);
667  }
668  explicit String (const String& lhs, charT rhs) {
669  imp.reserve(lhs.length() + 1);
670  imp.append(lhs.imp).append (1, rhs);
671  }
672 
673  string_type::iterator base_iter (iterator iter) {
674 #if 0 // for basic_string<...>::iterator
675  return iter;
676 #else // for new lstring::iterator
677  return iter.iterImp;
678 #endif
679  }
680 
681  string_type imp;
682 };
683 
684 // _UNICODE related Macros
685 #if !defined(_T)
686 # if defined (_WIN32) && defined (_UNICODE)
687 # define _T(x) L##x
688 # else
689 # define _T(x) x
690 # endif
691 #endif
692 
693 
694 #endif // STRING_H_INCLUDED
iterator erase(iterator first, iterator last)
Definition: String.h:308
size_type find_first_of(const String &str, size_type pos=0) const
Definition: String.h:366
const_iterator end() const
Definition: String.h:239
string_type::reverse_iterator reverse_iterator
Definition: String.h:211
String * toupper_copy() const
Definition: String.h:478
size_type find_last_not_of(const String &str, size_type pos=npos) const
Definition: String.h:393
Definition: IHashable.h:10
size_type find_last_of(charT c, size_type pos=npos) const
Definition: String.h:381
String * copy() override
Definition: String.h:93
void insert(iterator p, size_type n, charT c)
Definition: String.h:298
friend void swap(String &lhs, String &rhs)
Definition: String.h:336
String & append(const charT *s, size_type n)
Definition: String.h:269
void clear()
Definition: String.h:253
friend bool operator>=(const iterator &lhs, const iterator &rhs)
Definition: String.h:148
friend bool operator==(const iterator &lhs, const iterator &rhs)
Definition: String.h:133
size_type rfind(const charT *s, size_type pos, size_type n) const
Definition: String.h:359
reference operator*() const
Definition: String.h:118
string_type::value_type value_type
Definition: String.h:159
int compare(const charT *s) const
Definition: String.h:536
string_type::pointer pointer
Definition: String.h:110
char lToU[256]
Definition: Utils.cpp:12
size_type find(const String &str, size_type pos=0) const
Definition: String.h:349
friend std::basic_istream< charT, string_type::traits_type > & operator>>(std::basic_istream< charT, string_type::traits_type > &is, String &str)
Definition: String.h:628
Definition: HashCoder.h:41
size_type find_last_not_of(charT c, size_type pos=npos) const
Definition: String.h:399
const_iterator operator++(int)
Definition: String.h:174
String & insert(size_type pos1, const String &str, size_type pos2, size_type n)
Definition: String.h:288
string_type::value_type value_type
Definition: String.h:72
iterator erase(iterator position)
Definition: String.h:306
iterator operator-(difference_type n) const
Definition: String.h:128
String & replace(size_type pos1, size_type n1, const String &str)
Definition: String.h:310
reference at(size_type n)
Definition: String.h:259
size_type find_first_not_of(charT c, size_type pos=0) const
Definition: String.h:390
String & replace(iterator &i1, iterator &i2, const charT *s)
Definition: String.h:324
iterator begin()
Definition: String.h:236
size_type find_first_not_of(const charT *s, size_type pos=0) const
Definition: String.h:388
std::vector< std::string > split(const string_type &delim) const
Definition: String.h:514
friend std::basic_istream< charT, string_type::traits_type > & getline(std::basic_istream< charT, string_type::traits_type > &is, String &str)
Definition: String.h:640
String & replace(iterator &i1, iterator &i2, InputIterator j1, InputIterator j2)
Definition: String.h:329
size_type find_first_not_of(const charT *s, size_type pos, size_type n) const
Definition: String.h:386
friend bool operator<(const iterator &lhs, const iterator &rhs)
Definition: String.h:139
Definition: String.h:106
reference operator[](difference_type n) const
Definition: String.h:131
String & append(const String &str, size_type pos, size_type n)
Definition: String.h:268
size_type find(const charT *s, size_type pos, size_type n) const
Definition: String.h:351
int compare(size_type pos1, size_type n1, const charT *s, size_type n2=npos) const
Definition: String.h:538
void swap(String &str)
Definition: String.h:335
string_type::const_pointer pointer
Definition: String.h:161
size_type find_last_of(const charT *s, size_type pos=npos) const
Definition: String.h:379
size_type find_last_of(const charT *s, size_type pos, size_type n) const
Definition: String.h:377
std::random_access_iterator_tag iterator_category
Definition: String.h:107
Definition: String.h:214
String & replace(iterator &i1, iterator &i2, const String &str)
Definition: String.h:320
iterator & operator++()
Definition: String.h:121
int hashCode() const override
Definition: String.h:81
string_type::difference_type difference_type
Definition: String.h:75
Definition: String.h:60
reverse_iterator rbegin()
Definition: String.h:240
int compare(size_type pos1, size_type n1, const String &str) const
Definition: String.h:532
const_reverse_iterator rbegin() const
Definition: String.h:241
String & erase(size_type pos=0, size_type n=npos)
Definition: String.h:304
void reserve(size_type n=0)
Definition: String.h:252
String & assign(const String &str)
Definition: String.h:276
String(const charT *s)
Definition: String.h:221
friend std::basic_istream< charT, string_type::traits_type > & getline(std::basic_istream< charT, string_type::traits_type > &is, String &str, charT delim)
Definition: String.h:634
friend void swap(string_type &lhs, String &rhs)
Definition: String.h:341
size_type find_first_not_of(const String &str, size_type pos=0) const
Definition: String.h:384
void swap(string_type &str)
Definition: String.h:339
iterator operator--(int)
Definition: String.h:124
iterator & operator--()
Definition: String.h:123
String substr(size_type pos=0, size_type n=npos) const
Definition: String.h:495
void resize(size_type n)
Definition: String.h:250
const_iterator & operator++()
Definition: String.h:173
iterator operator++(int)
Definition: String.h:122
string_type::difference_type difference_type
Definition: String.h:162
String(size_type n, charT c)
Definition: String.h:222
int compare(const String &str) const
Definition: String.h:530
size_type rfind(const String &str, size_type pos=npos) const
Definition: String.h:357
Definition: String.h:157
String toupper() const
Definition: String.h:469
String & replace(iterator &i1, iterator &i2, size_type n, charT c)
Definition: String.h:326
const_iterator()
Definition: String.h:164
const_iterator begin() const
Definition: String.h:237
iterator insert(iterator p, charT c)
Definition: String.h:296
size_type find_last_not_of(const charT *s, size_type pos=npos) const
Definition: String.h:397
friend bool operator<=(const iterator &lhs, const iterator &rhs)
Definition: String.h:145
String & replace(size_type pos1, size_type n1, const String &str, size_type pos2, size_type n2)
Definition: String.h:312
const_iterator(const const_iterator &other)
Definition: String.h:165
void resize(size_type n, charT c)
Definition: String.h:249
void initConvertMaps()
Definition: Utils.cpp:22
return os str()
String & append(InputIterator first, InputIterator last)
Definition: String.h:273
String & assign(size_type n, charT c)
Definition: String.h:281
string_type::reference reference
Definition: String.h:109
String & toupper_inplace()
Definition: String.h:487
string_type::const_reference const_reference
Definition: String.h:77
String * trim_copy() const
Definition: String.h:427
String & append(size_type n, charT c)
Definition: String.h:271
String & replace(iterator &i1, iterator &i2, const charT *s, size_type n)
Definition: String.h:322
friend bool operator>(const iterator &lhs, const iterator &rhs)
Definition: String.h:142
String & insert(size_type pos1, const String &str)
Definition: String.h:286
String tolower() const
Definition: String.h:443
String & insert(size_type pos, size_type n, charT c)
Definition: String.h:294
size_type find_last_of(const String &str, size_type pos=npos) const
Definition: String.h:375
String & replace(size_type pos, size_type n1, size_type n2, charT c)
Definition: String.h:318
pointer operator->() const
Definition: String.h:171
string_type::const_reference reference
Definition: String.h:160
const_reverse_iterator rend() const
Definition: String.h:243
bool mapsInitFlag
Definition: Utils.cpp:13
String & append(const charT *s)
Definition: String.h:270
string_type::reference reference
Definition: String.h:76
size_type copy(charT *s, size_type n, size_type pos=0) const
Definition: String.h:331
bool operator==(const std::basic_string< char > &other) const override
Definition: String.h:86
string_type::pointer pointer
Definition: String.h:78
String()
Definition: String.h:217
bool operator!=(const std::basic_string< char > &other) const
Definition: String.h:89
allocator_type get_allocator() const
Definition: String.h:347
CONSTDATA date::last_spec last
Definition: date.h:1834
size_type capacity() const
Definition: String.h:251
String(const string_type &str, size_type pos=0, size_type n=npos)
Definition: String.h:219
String(const charT *s, size_type n)
Definition: String.h:220
String & assign(InputIterator first, InputIterator last)
Definition: String.h:283
void insert(iterator p, InputIterator first, InputIterator last)
Definition: String.h:301
iterator & operator-=(difference_type n)
Definition: String.h:129
string_type::difference_type difference_type
Definition: String.h:111
size_type find(charT c, size_type pos=0) const
Definition: String.h:355
bool empty() const
Definition: String.h:254
size_type find(const charT *s, size_type pos=0) const
Definition: String.h:353
int compare(size_type pos1, size_type n1, const String &str, size_type pos2, size_type n2) const
Definition: String.h:534
iterator operator+(difference_type n) const
Definition: String.h:126
size_type find_last_not_of(const charT *s, size_type pos, size_type n) const
Definition: String.h:395
size_type rfind(const charT *s, size_type pos=npos) const
Definition: String.h:361
friend bool operator!=(const iterator &lhs, const iterator &rhs)
Definition: String.h:136
reference operator*() const
Definition: String.h:170
String & append(const String &str)
Definition: String.h:267
string_type::traits_type traits_type
Definition: String.h:71
~String()
Definition: String.h:225
String & replace(size_type pos, size_type n1, const charT *s)
Definition: String.h:316
String & assign(const charT *s)
Definition: String.h:280
void push_back(const charT c)
Definition: String.h:274
reverse_iterator rend()
Definition: String.h:242
iterator & operator+=(difference_type n)
Definition: String.h:127
char uToL[256]
Definition: Utils.cpp:11
string_type::const_pointer const_pointer
Definition: String.h:79
string_type::const_reverse_iterator const_reverse_iterator
Definition: String.h:212
const_iterator & operator--()
Definition: String.h:175
String & replace(size_type pos, size_type n1, const charT *s, size_type n2)
Definition: String.h:314
String & trim_inplace()
Definition: String.h:433
friend class String
Definition: String.h:152
size_type find_first_of(const charT *s, size_type pos=0) const
Definition: String.h:370
string_type::value_type value_type
Definition: String.h:108
String(const String &str, size_type pos=0, size_type n=npos)
Definition: String.h:218
const_iterator(const iterator &other)
Definition: String.h:166
friend struct const_iterator
Definition: String.h:153
friend void swap(String &lhs, string_type &rhs)
Definition: String.h:340
String(InputIterator first, InputIterator last)
Definition: String.h:224
String & assign(const charT *s, size_type n)
Definition: String.h:279
size_type max_size() const
Definition: String.h:248
String & insert(size_type pos, const charT *s)
Definition: String.h:292
String & assign(const String &str, size_type pos, size_type n)
Definition: String.h:277
iterator end()
Definition: String.h:238
String & tolower_inplace()
Definition: String.h:461
std::random_access_iterator_tag iterator_category
Definition: String.h:158
String & insert(size_type pos, const charT *s, size_type n)
Definition: String.h:290
string_type::size_type size_type
Definition: String.h:74
string_type::allocator_type allocator_type
Definition: String.h:73
const charT * data() const
Definition: String.h:345
pointer operator->() const
Definition: String.h:119
const_iterator operator--(int)
Definition: String.h:176
String trim() const
Definition: String.h:421
size_type size() const
Definition: String.h:246
Definition: ICopyable.h:8
iterator(const iterator &other)
Definition: String.h:114
const_reference at(size_type n) const
Definition: String.h:260
const charT * c_str() const
Definition: String.h:344
size_type rfind(charT c, size_type pos=npos) const
Definition: String.h:363
iterator()
Definition: String.h:113
String * tolower_copy() const
Definition: String.h:452
size_type find_first_of(const charT *s, size_type pos, size_type n) const
Definition: String.h:368
size_type find_first_of(charT c, size_type pos=0) const
Definition: String.h:372
std::vector< std::string > split(char delim) const
Definition: String.h:504
size_type length() const
Definition: String.h:247
String & operator=(const String &str)
Definition: String.h:228