BALL  1.4.79
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
string.h
Go to the documentation of this file.
1 // -*- Mode: C++; tab-width: 2; -*-
2 // vi: set ts=2:
3 //
4 
5 #ifndef BALL_DATATYPE_STRING_H
6 #define BALL_DATATYPE_STRING_H
7 
8 #ifndef BALL_CONFIG_CONFIG_H
9 # include <BALL/CONFIG/config.h>
10 #endif
11 #ifndef BALL_COMMON_GLOBAL_H
12 # include <BALL/COMMON/global.h>
13 #endif
14 #ifndef BALL_COMMON_CREATE_H
15 # include <BALL/COMMON/create.h>
16 #endif
17 #ifndef BALL_COMMON_MACROS_H
18 # include <BALL/COMMON/macros.h>
19 #endif
20 #ifndef BALL_COMMON_EXCEPTION_H
21 # include <BALL/COMMON/exception.h>
22 #endif
23 #ifndef BALL_COMMON_DEBUG_H
24 # include <BALL/COMMON/debug.h>
25 #endif
26 
27 #include <string>
28 #include <cctype>
29 #include <cerrno>
30 #include <cstdlib>
31 #include <cstring>
32 #include <iostream>
33 #include <vector>
34 
35 #ifdef BALL_HAS_SSTREAM
36 # include <sstream>
37 #else
38 # include <strstream>
39 #endif
40 
41 using std::string;
42 
43 class QString;
44 class QByteArray;
45 
46 namespace BALL
47 {
48  // forward declaration
49  class Substring;
50 
57 
62  : public string
63  {
65  friend class Substring;
66 
67  public:
68 
69  // String has no copy constructor taking String&, bool as arguments.
70  // the compiler would confuse it with another copy constructor,
71  // cast true to 1 and copy only the string from the second character
72  // on! We could use BALL_CREATE_NODEEP, but this leads to trouble with
73  // inline constructors, so we code it by hand (here and in string.C)
74  virtual void* create(bool /* deep */ = true, bool empty = false) const;
75 
78 
87  {
89  CASE_SENSITIVE = 0,
90 
92  CASE_INSENSITIVE = 1
93  };
94 
100  static const Size EndPos;
101 
103 
109 
111  static const char* CHARACTER_CLASS__ASCII_ALPHA;
112 
115 
117  static const char* CHARACTER_CLASS__ASCII_LOWER;
118 
120  static const char* CHARACTER_CLASS__ASCII_UPPER;
121 
123  static const char* CHARACTER_CLASS__ASCII_NUMERIC;
124 
126  static const char* CHARACTER_CLASS__ASCII_FLOAT;
127 
139  static const char* CHARACTER_CLASS__WHITESPACE;
140 
143  static const char* CHARACTER_CLASS__QUOTES;
144 
146 
149 
151  String();
152 
154  String(const string& string);
155 
157  String(const String& s);
158 
159 #ifdef BALL_STD_STRING_HAS_RVALUE_REFERENCES
160  String(String&& s);
162 
164  String(string&& s);
165 
167  String& operator=(String&& s);
168 
170  String& operator=(string&& s);
171 #endif
172 
174  explicit String(const QString& string);
175 
177  explicit String(const QByteArray& string);
178 
184  String(const String& s, Index from, Size len = EndPos);
185 
195  String(const char* char_ptr, Index from = 0, Size len = EndPos);
196 
207  String(Size buffer_size, const char* format, ... );
208 
214 #ifdef BALL_HAS_SSTREAM
215  String(std::stringstream& s);
216 #else
217  String(std::strstream& s);
218 #endif
219 
222  String(const char c, Size len = 1);
223 
225  String(const unsigned char uc);
226 
228  String(short s);
229 
231  String(unsigned short us);
232 
234  String(int i);
235 
237  String(unsigned int ui);
238 
240  String(long l);
241 
243  String(unsigned long);
244 
245 #ifdef BALL_ALLOW_LONG64_TYPE_OVERLOADS
246  String(LongIndex l);
248 
250  String(LongSize);
251 #endif
252 
254  String(float f);
255 
257  String(double d);
258 
260  virtual ~String();
261 
263  void destroy();
264 
266  virtual void clear();
268 
272 
274  void set(const String& s);
275 
280  void set(const String& string, Index from, Size len = EndPos);
281 
287  void set(const char* char_ptr, Index from = 0, Size len = EndPos);
288 
293  void set(Size buffer_size, const char *format, ...);
294 
298 #ifdef BALL_HAS_SSTREAM
299  void set(std::stringstream& s);
300 #else
301  void set(std::strstream& s);
302 #endif
303 
305  void set(char c, Size len = 1);
306 
308  void set(unsigned char uc);
309 
311  void set(short s);
312 
314  void set(unsigned short us);
315 
317  void set(int i);
318 
320  void set(unsigned int ui);
321 
323  void set(long l);
324 
326  void set(unsigned long ul);
327 
328 #ifdef BALL_ALLOW_LONG64_TYPE_OVERLOADS
329  void set(LongIndex l);
331 
333  void set(LongSize ul);
334 #endif
335 
337  void set(float f);
338 
340  void set(double d);
341 
351  void get(char* char_ptr, Index from = 0, Size len = EndPos) const;
352 
354  const String& operator = (const String& s);
355 
359  const String& operator = (const char* pc);
360 
364 #ifdef BALL_HAS_SSTREAM
365  const String& operator = (std::stringstream& s);
366 #else
367  const String& operator = (std::strstream& s);
368 #endif
369 
371  const String& operator = (char c);
372 
374  const String& operator = (unsigned char uc);
375 
377  const String& operator = (short s);
378 
380  const String& operator = (unsigned short us);
381 
383  const String& operator = (int i);
384 
386  const String& operator = (unsigned int ui);
387 
389  const String& operator = (long l);
390 
392  const String& operator = (unsigned long ul);
393 
394 #ifdef BALL_ALLOW_LONG64_TYPE_OVERLOADS
395  const String& operator = (LongIndex l);
397 
399  const String& operator = (LongSize ul);
400 #endif
401 
403  const String& operator = (float f);
404 
406  const String& operator = (double d);
408 
415  static void setCompareMode(CompareMode compare_mode);
417 
419  static CompareMode getCompareMode();
421 
425 
430  bool toBool() const;
431 
433  char toChar() const;
434 
436  unsigned char toUnsignedChar() const;
437 
441  short toShort() const;
442 
446  unsigned short toUnsignedShort() const;
447 
451  int toInt() const;
452 
456  unsigned int toUnsignedInt() const;
457 
461  long toLong() const;
462 
466  unsigned long toUnsignedLong() const;
467 
471  float toFloat() const;
472 
476  double toDouble() const;
478 
479 
483 
488  void toLower(Index from = 0, Size len = EndPos);
489 
494  void toUpper(Index from = 0, Size len = EndPos);
495 
497 
500 
505  Substring getSubstring(Index from = 0, Size len = EndPos) const;
506 
511  Substring operator () (Index from, Size len = EndPos) const;
512 
515  Substring before(const String& s, Index from = 0) const;
516 
519  Substring through(const String& s, Index from = 0) const;
520 
523  Substring from(const String& s, Index from = 0) const;
524 
527  Substring after(const String& s, Index from = 0) const;
528 
530 
533 
537  Size countFields(const char* delimiters = CHARACTER_CLASS__WHITESPACE) const;
538 
542  Size countFieldsQuoted(const char* delimiters = CHARACTER_CLASS__WHITESPACE,
543  const char* quotes = CHARACTER_CLASS__QUOTES) const;
544 
549  String getField(Index index, const char* delimiters = CHARACTER_CLASS__WHITESPACE, Index* from = 0) const;
550 
555  String getFieldQuoted(Index index, const char* delimiters = CHARACTER_CLASS__WHITESPACE,
556  const char* quotes = CHARACTER_CLASS__QUOTES, Index* from = 0) const;
557 
562  Size split(String string_array[], Size array_size, const char* delimiters = CHARACTER_CLASS__WHITESPACE, Index from = 0) const;
563 
569  Size split(std::vector<String>& strings, const char* delimiters = CHARACTER_CLASS__WHITESPACE, Index from = 0) const;
570 
578  Size splitQuoted(std::vector<String>& strings, const char* delimiters = CHARACTER_CLASS__WHITESPACE,
579  const char* quotes = CHARACTER_CLASS__QUOTES, Index from = 0) const;
580 
582 
585 
592  String& trimLeft(const char* trimmed = CHARACTER_CLASS__WHITESPACE);
593 
600  String& trimRight(const char* trimmed = CHARACTER_CLASS__WHITESPACE);
601 
605  String& trim(const char* trimmed = CHARACTER_CLASS__WHITESPACE);
606 
607  // ?????
611  String trim(const char* trimmed = CHARACTER_CLASS__WHITESPACE) const;
612 
614  String& truncate(Size size);
615 
617  Substring left(Size len) const;
618 
620  Substring right(Size len) const;
621 
630  Substring instr(const String& pattern, Index from = 0) const;
631 
633 
636 
637  // NOTE: please, please, pretty please, only try to optimize away operator+ definitions
638  // if you *really* know what you are doing. We didn't, and we definitely don't want
639  // to touch this stinking heap of C++ garbage ever again!
640  // (dstoeckel & anhi)
643  friend String operator + (const String& s1, const string& s2);
644 
647  friend String operator + (const string& s1, const String& s2);
648 
651  friend String operator + (const String& s1, const String& s2);
652 
655  friend String operator + (const String& s1, const char* char_ptr);
656 
659  friend String operator + (const char* char_ptr, const String& s);
660 
663  friend String operator + (const String& s, char c);
664 
667  friend String operator + (char c, const String& s);
668 
669 #ifdef BALL_STD_STRING_HAS_RVALUE_REFERENCES
672  friend String operator + (String&& s1, const string& s2);
673 
676  friend String operator + (String&& s1, const String& s2);
677 
680  friend String operator + (String&& s1, String&& s2);
681 
683  friend String operator + (const String& s1, string&& s2);
684 
686  friend String operator + (string&& s1, const String& s2);
687 
689  friend String operator + (const string& s1, String&& s2);
690 
693  friend String operator + (const String& s1, String&& s2);
694 
697  friend String operator + (String&& s1, const char* char_ptr);
698 
701  friend String operator + (const char* char_ptr, String&& s);
702 
705  friend String operator + (String&& s, char c);
706 
709  friend String operator + (char c, String&& s);
710 #endif
711 
713  void swap(String& s);
714 
724  String& reverse(Index from = 0, Size len = EndPos);
725 
729  Size substitute(const String& to_replace, const String& replacing);
730 
732  void substituteAll(const String& to_replace, const String& replacing);
733 
735 
739 
741  bool has(char c) const;
742 
744  bool hasSubstring(const String& s, Index from = 0) const;
745 
747  bool hasPrefix(const String& s) const;
748 
750  bool hasSuffix(const String& s) const;
751 
753  bool isEmpty() const;
754 
758  bool isAlpha() const;
759 
763  bool isAlnum() const;
764 
768  bool isDigit() const;
769 
774  bool isFloat() const;
775 
779  bool isSpace() const;
780 
785  bool isWhitespace() const;
786 
788  static bool isAlpha(char c);
789 
791  static bool isAlnum(char c);
792 
794  static bool isDigit(char c);
795 
797  static bool isSpace(char c);
798 
802  static bool isWhitespace(char c);
803 
805 
808 
810  String encodeBase64();
811 
815  String decodeBase64();
816 
818 
821 
826  int compare(const String& string, Index from = 0) const;
827 
832  int compare(const String& string, Index from, Size len) const;
833 
834 
840  int compare(const char* char_ptr, Index from = 0) const;
841 
847  int compare(const char* char_ptr, Index from, Size len) const;
848 
853  int compare(char c, Index from = 0) const;
854 
856  bool operator == (const String& string) const;
857 
859  bool operator != (const String& string) const;
860 
862  bool operator < (const String& string) const;
863 
865  bool operator <= (const String& string) const;
866 
868  bool operator >= (const String& string) const;
869 
871  bool operator > (const String& string) const;
872 
877  friend bool operator == (const char* char_ptr, const String& string);
878 
883  friend bool operator != (const char* char_ptr, const String& string);
884 
889  friend bool operator < (const char* char_ptr, const String& string);
890 
895  friend bool operator <= (const char* char_ptr, const String& string);
896 
901  friend bool operator > (const char* char_ptr, const String& string);
902 
907  friend bool operator >= (const char* char_ptr, const String& string);
908 
912  bool operator == (const char* char_ptr) const;
913 
917  bool operator != (const char* char_ptr) const;
918 
922  bool operator < (const char* char_ptr) const;
923 
927  bool operator <= (const char* char_ptr) const;
928 
932  bool operator > (const char* char_ptr) const;
933 
937  bool operator >= (const char* char_ptr) const;
938 
941  friend bool operator == (char c, const String& string);
942 
945  friend bool operator != (char c, const String& string);
946 
949  friend bool operator < (char c, const String& string);
950 
953  friend bool operator <= (char c, const String& string);
954 
957  friend bool operator > (char c, const String& string);
958 
960  friend bool operator >= (char c, const String& string);
961 
963  bool operator == (char c) const;
964 
966  bool operator != (char c) const;
967 
969  bool operator < (char c) const;
970 
972  bool operator <= (char c) const;
973 
975  bool operator > (char c) const;
976 
978  bool operator >= (char c) const;
979 
981 
984 
986  bool isValid() const;
987 
989  void dump(std::ostream& s = std::cout, Size depth = 0) const;
990 
992 
995 
997  std::istream& getline(std::istream& s = std::cin, char delimiter = '\n');
998 
1000  BALL_EXPORT
1001  friend std::istream& getline(std::istream& s, String& string, char delimiter = '\n');
1002 
1004 
1006  static const String EMPTY;
1007 
1008  protected:
1009 
1010  // the validate... methods check perform a thorough
1011  // index checking and an index translation
1012  // Indices below zero are interpreted as indices
1013  // relative to the end of the string
1014  // All methods throw IndexUnder|Overflow exceptions
1015  //
1016  void validateIndex_(Index& index) const;
1017 
1018  void validateRange_(Index& from, Size& len) const;
1019 
1020  static void validateCharPtrRange_(Index& from, Size& len, const char* char_ptr);
1021 
1022  static void valudateCharPtrIndex_(Index& index);
1023 
1024  private:
1025 
1026  static int compareAscendingly_(const char* a, const char* b);
1027 
1028  static int compareDescendingly_(const char* a, const char* b);
1029 
1030  static CompareMode compare_mode_;
1031 
1032  static char B64Chars_[64];
1033 
1034  static int Index_64_[128];
1035  };
1036 
1046  {
1047  friend class String;
1048 
1049  public:
1050 
1052 
1053 
1056 
1062  : public Exception::GeneralException
1063  {
1064  public:
1065  UnboundSubstring(const char* file, int line);
1066  };
1067 
1075  {
1076  public:
1077  InvalidSubstring(const char* file, int line);
1078  };
1079 
1081 
1084 
1088  Substring();
1089 
1095  Substring(const Substring& substring, bool deep = true);
1096 
1104  Substring(const String& string, Index from = 0, Size len = String::EndPos);
1105 
1109  virtual ~Substring();
1110 
1115  void destroy();
1116 
1121  virtual void clear();
1122 
1124 
1127 
1132  operator String() const;
1133 
1138  String toString() const;
1139 
1141 
1144 
1153  Substring& bind(const String& string, Index from = 0, Size len = String::EndPos);
1154 
1160  Substring& bind(const Substring& substring, Index from = 0, Size len = String::EndPos);
1161 
1163  void unbind();
1164 
1166  String* getBoundString();
1167 
1169  const String* getBoundString() const
1170 ;
1171 
1173 
1176 
1180  void set(const String& string);
1181 
1185  void set(const Substring& s);
1186 
1192  void set(const char* char_ptr, Size size = String::EndPos);
1193 
1197  const Substring& operator = (const String& string);
1198 
1202  const Substring& operator = (const Substring& substring);
1203 
1208  const Substring& operator = (const char* char_ptr);
1209 
1211 
1214 
1218  char* c_str();
1219 
1223  const char* c_str() const;
1224 
1229  Index getFirstIndex() const;
1230 
1235  Index getLastIndex() const;
1236 
1238  Size size() const;
1239 
1245  char& operator [] (Index index);
1246 
1252  char operator [] (Index index) const;
1253 
1257  Substring& toLower();
1258 
1262  Substring& toUpper();
1263 
1265 
1268 
1270  bool isBound() const;
1271 
1273  bool isEmpty() const;
1274 
1276 
1279 
1283  bool operator == (const Substring& substring) const;
1284 
1288  bool operator != (const Substring& substring) const;
1289 
1293  bool operator == (const String& string) const;
1294 
1298  bool operator != (const String& string) const;
1299 
1303  BALL_EXPORT
1304  friend bool operator == (const String& string, const Substring& substring);
1305 
1309  BALL_EXPORT
1310  friend bool operator != (const String& string, const Substring& substring);
1311 
1316  bool operator == (const char* char_ptr) const;
1317 
1322  bool operator != (const char* char_ptr) const;
1323 
1327  bool operator == (char c) const;
1328 
1332  bool operator != (char c) const;
1333 
1335 
1338 
1340  BALL_EXPORT
1341  friend std::ostream& operator << (std::ostream& s, const Substring& substring);
1342 
1344 
1347 
1352  bool isValid() const;
1353 
1357  void dump(std::ostream& s = std::cout, Size depth = 0) const;
1358 
1360 
1361  protected:
1362 
1363  // throws IndexUnderflow|IndexOverflow
1364  void validateRange_(Index& from, Size& len) const;
1365 
1366  private:
1367 
1368  /*_ @name Attributes
1369  */
1370  //_@{
1371 
1372  //_ pointer to the bound String
1373  String* bound_;
1374 
1375  //_ start index in the bound String
1376  Index from_;
1377 
1378  //_ end index in the bound String
1379  Index to_;
1380  //_@}
1381  };
1382 
1384 
1385 # ifndef BALL_NO_INLINE_FUNCTIONS
1386 # include <BALL/DATATYPE/string.iC>
1387 # endif
1388 } // namespace BALL
1389 
1390 #endif // BALL_DATATYPE_STRING_H
static const char * CHARACTER_CLASS__ASCII_UPPER
Character class containing all upper case letters.
Definition: string.h:120
static const char * CHARACTER_CLASS__ASCII_FLOAT
Character class containing the digits from 0 to 9 and a dot.
Definition: string.h:126
BALL_EXPORT std::ostream & operator<<(std::ostream &os, const Exception::GeneralException &e)
BALL_EXTERN_VARIABLE const double c
Definition: constants.h:149
static const char * CHARACTER_CLASS__ASCII_ALPHA
Character class containing all letters (lower and upper case)
Definition: string.h:111
static const String EMPTY
Constant empty string.
Definition: string.h:1006
#define BALL_CREATE_DEEP(name)
Definition: create.h:26
BALL_ULONG64_TYPE LongSize
BALL_LONG64_TYPE LongIndex
static const char * CHARACTER_CLASS__WHITESPACE
Definition: string.h:139
static const char * CHARACTER_CLASS__QUOTES
Definition: string.h:143
static const Size EndPos
Definition: string.h:100
static const char * CHARACTER_CLASS__ASCII_ALPHANUMERIC
Character class containing all letters and digits.
Definition: string.h:114
ConstRandomAccessIterator< Container, DataType, Position, Traits > operator+(Distance distance, const ConstRandomAccessIterator< Container, DataType, Position, Traits > &iterator)
static const char * CHARACTER_CLASS__ASCII_LOWER
Character class containing all lower case letters.
Definition: string.h:117
static const char * CHARACTER_CLASS__ASCII_NUMERIC
Character class containing the digits from 0 to 9.
Definition: string.h:123
#define BALL_EXPORT
Definition: COMMON/global.h:50
Index compare(const T1 &a, const T2 &b)
Definition: MATHS/common.h:314