BALL  1.4.2
 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 // $Id: string.h,v 1.56 2005/12/23 17:01:43 amoll Exp $
5 //
6 
7 #ifndef BALL_DATATYPE_STRING_H
8 #define BALL_DATATYPE_STRING_H
9 
10 #ifndef BALL_CONFIG_CONFIG_H
11 # include <BALL/CONFIG/config.h>
12 #endif
13 #ifndef BALL_COMMON_GLOBAL_H
14 # include <BALL/COMMON/global.h>
15 #endif
16 #ifndef BALL_COMMON_CREATE_H
17 # include <BALL/COMMON/create.h>
18 #endif
19 #ifndef BALL_COMMON_MACROS_H
20 # include <BALL/COMMON/macros.h>
21 #endif
22 #ifndef BALL_COMMON_EXCEPTION_H
23 # include <BALL/COMMON/exception.h>
24 #endif
25 #ifndef BALL_COMMON_DEBUG_H
26 # include <BALL/COMMON/debug.h>
27 #endif
28 
29 #include <string>
30 #include <cctype>
31 #include <cerrno>
32 #include <cstdlib>
33 #include <cstring>
34 #include <iostream>
35 #include <vector>
36 
37 #ifdef BALL_HAS_SSTREAM
38 # include <sstream>
39 #else
40 # include <strstream>
41 #endif
42 
43 using std::string;
44 
45 class QString;
46 class QByteArray;
47 
48 namespace BALL
49 {
50  // forward declaration
51  class Substring;
52 
59 
64  : public string
65  {
67  friend class Substring;
68 
69  public:
70 
71  // String has no copy constructor taking String&, bool as arguments.
72  // the compiler would confuse it with another copy constructor,
73  // cast true to 1 and copy only the string from the second character
74  // on! We could use BALL_CREATE_NODEEP, but this leads to trouble with
75  // inline constructors, so we code it by hand (here and in string.C)
76  virtual void* create(bool /* deep */ = true, bool empty = false) const;
77 
80 
89  {
91  CASE_SENSITIVE = 0,
92 
94  CASE_INSENSITIVE = 1
95  };
96 
102  static const Size EndPos;
103 
105 
111 
113  static const char* CHARACTER_CLASS__ASCII_ALPHA;
114 
117 
119  static const char* CHARACTER_CLASS__ASCII_LOWER;
120 
122  static const char* CHARACTER_CLASS__ASCII_UPPER;
123 
125  static const char* CHARACTER_CLASS__ASCII_NUMERIC;
126 
128  static const char* CHARACTER_CLASS__ASCII_FLOAT;
129 
141  static const char* CHARACTER_CLASS__WHITESPACE;
142 
145  static const char* CHARACTER_CLASS__QUOTES;
146 
148 
151 
153  String();
154 
156  String(const string& string);
157 
159  String(const String& s);
160 
161 #ifdef BALL_STD_STRING_HAS_RVALUE_REFERENCES
162 
163  String(String&& s);
164 
166  String(string&& s);
167 
169  String& operator=(String&& s);
170 
172  String& operator=(string&& s);
173 #endif
174 
176  explicit String(const QString& string);
177 
179  explicit String(const QByteArray& string);
180 
186  String(const String& s, Index from, Size len = EndPos);
187 
197  String(const char* char_ptr, Index from = 0, Size len = EndPos);
198 
209  String(Size buffer_size, const char* format, ... );
210 
216 #ifdef BALL_HAS_SSTREAM
217  String(std::stringstream& s);
218 #else
219  String(std::strstream& s);
220 #endif
221 
224  String(const char c, Size len = 1);
225 
227  String(const unsigned char uc);
228 
230  String(short s);
231 
233  String(unsigned short us);
234 
236  String(int i);
237 
239  String(unsigned int ui);
240 
242  String(long l);
243 
245  String(unsigned long);
246 
247 #ifdef BALL_ALLOW_LONG64_TYPE_OVERLOADS
248 
249  String(LongIndex l);
250 
252  String(LongSize);
253 #endif
254 
256  String(float f);
257 
259  String(double d);
260 
262  virtual ~String();
263 
265  void destroy();
266 
268  virtual void clear();
270 
274 
276  void set(const String& s);
277 
282  void set(const String& string, Index from, Size len = EndPos);
283 
289  void set(const char* char_ptr, Index from = 0, Size len = EndPos);
290 
295  void set(Size buffer_size, const char *format, ...);
296 
300 #ifdef BALL_HAS_SSTREAM
301  void set(std::stringstream& s);
302 #else
303  void set(std::strstream& s);
304 #endif
305 
307  void set(char c, Size len = 1);
308 
310  void set(unsigned char uc);
311 
313  void set(short s);
314 
316  void set(unsigned short us);
317 
319  void set(int i);
320 
322  void set(unsigned int ui);
323 
325  void set(long l);
326 
328  void set(unsigned long ul);
329 
330 #ifdef BALL_ALLOW_LONG64_TYPE_OVERLOADS
331 
332  void set(LongIndex l);
333 
335  void set(LongSize ul);
336 #endif
337 
339  void set(float f);
340 
342  void set(double d);
343 
353  void get(char* char_ptr, Index from = 0, Size len = EndPos) const;
354 
356  const String& operator = (const String& s);
357 
361  const String& operator = (const char* pc);
362 
366 #ifdef BALL_HAS_SSTREAM
367  const String& operator = (std::stringstream& s);
368 #else
369  const String& operator = (std::strstream& s);
370 #endif
371 
373  const String& operator = (char c);
374 
376  const String& operator = (unsigned char uc);
377 
379  const String& operator = (short s);
380 
382  const String& operator = (unsigned short us);
383 
385  const String& operator = (int i);
386 
388  const String& operator = (unsigned int ui);
389 
391  const String& operator = (long l);
392 
394  const String& operator = (unsigned long ul);
395 
396 #ifdef BALL_ALLOW_LONG64_TYPE_OVERLOADS
397 
398  const String& operator = (LongIndex l);
399 
401  const String& operator = (LongSize ul);
402 #endif
403 
405  const String& operator = (float f);
406 
408  const String& operator = (double d);
410 
417 
418  static void setCompareMode(CompareMode compare_mode);
419 
421  static CompareMode getCompareMode();
423 
427 
432  bool toBool() const;
433 
435  char toChar() const;
436 
438  unsigned char toUnsignedChar() const;
439 
443  short toShort() const;
444 
448  unsigned short toUnsignedShort() const;
449 
453  int toInt() const;
454 
458  unsigned int toUnsignedInt() const;
459 
463  long toLong() const;
464 
468  unsigned long toUnsignedLong() const;
469 
473  float toFloat() const;
474 
478  double toDouble() const;
480 
481 
485 
490  void toLower(Index from = 0, Size len = EndPos);
491 
496  void toUpper(Index from = 0, Size len = EndPos);
497 
499 
502 
507  Substring getSubstring(Index from = 0, Size len = EndPos) const;
508 
513  Substring operator () (Index from, Size len = EndPos) const;
514 
517  Substring before(const String& s, Index from = 0) const;
518 
521  Substring through(const String& s, Index from = 0) const;
522 
525  Substring from(const String& s, Index from = 0) const;
526 
529  Substring after(const String& s, Index from = 0) const;
530 
532 
535 
539  Size countFields(const char* delimiters = CHARACTER_CLASS__WHITESPACE) const;
540 
544  Size countFieldsQuoted(const char* delimiters = CHARACTER_CLASS__WHITESPACE,
545  const char* quotes = CHARACTER_CLASS__QUOTES) const;
546 
551  String getField(Index index, const char* delimiters = CHARACTER_CLASS__WHITESPACE, Index* from = 0) const;
552 
557  String getFieldQuoted(Index index, const char* delimiters = CHARACTER_CLASS__WHITESPACE,
558  const char* quotes = CHARACTER_CLASS__QUOTES, Index* from = 0) const;
559 
564  Size split(String string_array[], Size array_size, const char* delimiters = CHARACTER_CLASS__WHITESPACE, Index from = 0) const;
565 
571  Size split(std::vector<String>& strings, const char* delimiters = CHARACTER_CLASS__WHITESPACE, Index from = 0) const;
572 
580  Size splitQuoted(std::vector<String>& strings, const char* delimiters = CHARACTER_CLASS__WHITESPACE,
581  const char* quotes = CHARACTER_CLASS__QUOTES, Index from = 0) const;
582 
584 
587 
594  String& trimLeft(const char* trimmed = CHARACTER_CLASS__WHITESPACE);
595 
602  String& trimRight(const char* trimmed = CHARACTER_CLASS__WHITESPACE);
603 
607  String& trim(const char* trimmed = CHARACTER_CLASS__WHITESPACE);
608 
609  // ?????
613  String trim(const char* trimmed = CHARACTER_CLASS__WHITESPACE) const;
614 
616  String& truncate(Size size);
617 
619  Substring left(Size len) const;
620 
622  Substring right(Size len) const;
623 
632  Substring instr(const String& pattern, Index from = 0) const;
633 
635 
638 
639  // NOTE: please, please, pretty please, only try to optimize away operator+ definitions
640  // if you *really* know what you are doing. We didn't, and we definitely don't want
641  // to touch this stinking heap of C++ garbage ever again!
642  // (dstoeckel & anhi)
645  friend String operator + (const String& s1, const string& s2);
646 
649  friend String operator + (const string& s1, const String& s2);
650 
653  friend String operator + (const String& s1, const String& s2);
654 
657  friend String operator + (const String& s1, const char* char_ptr);
658 
661  friend String operator + (const char* char_ptr, const String& s);
662 
665  friend String operator + (const String& s, char c);
666 
669  friend String operator + (char c, const String& s);
670 
671 #ifdef BALL_STD_STRING_HAS_RVALUE_REFERENCES
672 
674  friend String operator + (String&& s1, const string& s2);
675 
678  friend String operator + (String&& s1, const String& s2);
679 
682  friend String operator + (String&& s1, String&& s2);
683 
685  friend String operator + (const String& s1, string&& s2);
686 
688  friend String operator + (string&& s1, const String& s2);
689 
691  friend String operator + (const string& s1, String&& s2);
692 
695  friend String operator + (const String& s1, String&& s2);
696 
699  friend String operator + (String&& s1, const char* char_ptr);
700 
703  friend String operator + (const char* char_ptr, String&& s);
704 
707  friend String operator + (String&& s, char c);
708 
711  friend String operator + (char c, String&& s);
712 #endif
713 
715  void swap(String& s);
716 
726  String& reverse(Index from = 0, Size len = EndPos);
727 
731  Size substitute(const String& to_replace, const String& replacing);
732 
734 
738 
740  bool has(char c) const;
741 
743  bool hasSubstring(const String& s, Index from = 0) const;
744 
746  bool hasPrefix(const String& s) const;
747 
749  bool hasSuffix(const String& s) const;
750 
752  bool isEmpty() const;
753 
757  bool isAlpha() const;
758 
762  bool isAlnum() const;
763 
767  bool isDigit() const;
768 
773  bool isFloat() const;
774 
778  bool isSpace() const;
779 
784  bool isWhitespace() const;
785 
787  static bool isAlpha(char c);
788 
790  static bool isAlnum(char c);
791 
793  static bool isDigit(char c);
794 
796  static bool isSpace(char c);
797 
801  static bool isWhitespace(char c);
802 
804 
807 
809  String encodeBase64();
810 
814  String decodeBase64();
815 
817 
820 
825  int compare(const String& string, Index from = 0) const;
826 
831  int compare(const String& string, Index from, Size len) const;
832 
833 
839  int compare(const char* char_ptr, Index from = 0) const;
840 
846  int compare(const char* char_ptr, Index from, Size len) const;
847 
852  int compare(char c, Index from = 0) const;
853 
855  bool operator == (const String& string) const;
856 
858  bool operator != (const String& string) const;
859 
861  bool operator < (const String& string) const;
862 
864  bool operator <= (const String& string) const;
865 
867  bool operator >= (const String& string) const;
868 
870  bool operator > (const String& string) const;
871 
876  friend bool operator == (const char* char_ptr, const String& string);
877 
882  friend bool operator != (const char* char_ptr, const String& string);
883 
888  friend bool operator < (const char* char_ptr, const String& string);
889 
894  friend bool operator <= (const char* char_ptr, const String& string);
895 
900  friend bool operator > (const char* char_ptr, const String& string);
901 
906  friend bool operator >= (const char* char_ptr, const String& string);
907 
911  bool operator == (const char* char_ptr) const;
912 
916  bool operator != (const char* char_ptr) const;
917 
921  bool operator < (const char* char_ptr) const;
922 
926  bool operator <= (const char* char_ptr) const;
927 
931  bool operator > (const char* char_ptr) const;
932 
936  bool operator >= (const char* char_ptr) const;
937 
940  friend bool operator == (char c, const String& string);
941 
944  friend bool operator != (char c, const String& string);
945 
948  friend bool operator < (char c, const String& string);
949 
952  friend bool operator <= (char c, const String& string);
953 
956  friend bool operator > (char c, const String& string);
957 
959  friend bool operator >= (char c, const String& string);
960 
962  bool operator == (char c) const;
963 
965  bool operator != (char c) const;
966 
968  bool operator < (char c) const;
969 
971  bool operator <= (char c) const;
972 
974  bool operator > (char c) const;
975 
977  bool operator >= (char c) const;
978 
980 
983 
985  bool isValid() const;
986 
988  void dump(std::ostream& s = std::cout, Size depth = 0) const;
989 
991 
994 
996  std::istream& getline(std::istream& s = std::cin, char delimiter = '\n');
997 
1000  friend std::istream& getline(std::istream& s, String& string, char delimiter = '\n');
1001 
1003 
1005  static const String EMPTY;
1006 
1007  protected:
1008 
1009  // the validate... methods check perform a thorough
1010  // index checking and an index translation
1011  // Indices below zero are interpreted as indices
1012  // relative to the end of the string
1013  // All methods throw IndexUnder|Overflow exceptions
1014  //
1015  void validateIndex_(Index& index) const;
1016 
1017  void validateRange_(Index& from, Size& len) const;
1018 
1019  static void validateCharPtrRange_(Index& from, Size& len, const char* char_ptr);
1020 
1021  static void valudateCharPtrIndex_(Index& index);
1022 
1023  private:
1024 
1025  static int compareAscendingly_(const char* a, const char* b);
1026 
1027  static int compareDescendingly_(const char* a, const char* b);
1028 
1030 
1031  static char B64Chars_[64];
1032 
1033  static int Index_64_[128];
1034  };
1035 
1045  {
1046  friend class String;
1047 
1048  public:
1049 
1051 
1052 
1055 
1061  : public Exception::GeneralException
1062  {
1063  public:
1064  UnboundSubstring(const char* file, int line);
1065  };
1066 
1074  {
1075  public:
1076  InvalidSubstring(const char* file, int line);
1077  };
1078 
1080 
1083 
1087  Substring();
1088 
1094  Substring(const Substring& substring, bool deep = true);
1095 
1103  Substring(const String& string, Index from = 0, Size len = String::EndPos);
1104 
1108  virtual ~Substring();
1109 
1114  void destroy();
1115 
1120  virtual void clear();
1121 
1123 
1126 
1131  operator String() const;
1132 
1137  String toString() const;
1138 
1140 
1143 
1152  Substring& bind(const String& string, Index from = 0, Size len = String::EndPos);
1153 
1159  Substring& bind(const Substring& substring, Index from = 0, Size len = String::EndPos);
1160 
1162  void unbind();
1163 
1165  String* getBoundString();
1166 
1168  const String* getBoundString() const
1169 ;
1170 
1172 
1175 
1179  void set(const String& string);
1180 
1184  void set(const Substring& s);
1185 
1191  void set(const char* char_ptr, Size size = String::EndPos);
1192 
1196  const Substring& operator = (const String& string);
1197 
1201  const Substring& operator = (const Substring& substring);
1202 
1207  const Substring& operator = (const char* char_ptr);
1208 
1210 
1213 
1217  char* c_str();
1218 
1222  const char* c_str() const;
1223 
1228  Index getFirstIndex() const;
1229 
1234  Index getLastIndex() const;
1235 
1237  Size size() const;
1238 
1244  char& operator [] (Index index);
1245 
1251  char operator [] (Index index) const;
1252 
1256  Substring& toLower();
1257 
1261  Substring& toUpper();
1262 
1264 
1267 
1269  bool isBound() const;
1270 
1272  bool isEmpty() const;
1273 
1275 
1278 
1282  bool operator == (const Substring& substring) const;
1283 
1287  bool operator != (const Substring& substring) const;
1288 
1292  bool operator == (const String& string) const;
1293 
1297  bool operator != (const String& string) const;
1298 
1302  BALL_EXPORT
1303  friend bool operator == (const String& string, const Substring& substring);
1304 
1308  BALL_EXPORT
1309  friend bool operator != (const String& string, const Substring& substring);
1310 
1315  bool operator == (const char* char_ptr) const;
1316 
1321  bool operator != (const char* char_ptr) const;
1322 
1326  bool operator == (char c) const;
1327 
1331  bool operator != (char c) const;
1332 
1334 
1337 
1339  BALL_EXPORT
1340  friend std::ostream& operator << (std::ostream& s, const Substring& substring);
1341 
1343 
1346 
1351  bool isValid() const;
1352 
1356  void dump(std::ostream& s = std::cout, Size depth = 0) const;
1357 
1359 
1360  protected:
1361 
1362  // throws IndexUnderflow|IndexOverflow
1363  void validateRange_(Index& from, Size& len) const;
1364 
1365  private:
1366 
1367  /*_ @name Attributes
1368  */
1369  //_@{
1370 
1371  //_ pointer to the bound String
1373 
1374  //_ start index in the bound String
1376 
1377  //_ end index in the bound String
1379  //_@}
1380  };
1381 
1383 
1384 # ifndef BALL_NO_INLINE_FUNCTIONS
1385 # include <BALL/DATATYPE/string.iC>
1386 # endif
1387 } // namespace BALL
1388 
1389 #endif // BALL_DATATYPE_STRING_H