libcommonc++  0.7
String.h++
Go to the documentation of this file.
1 /* ---------------------------------------------------------------------------
2  commonc++ - A C++ Common Class Library
3  Copyright (C) 2005-2014 Mark A Lindner
4 
5  This file is part of commonc++.
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Library General Public
9  License as published by the Free Software Foundation; either
10  version 2 of the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Library General Public License for more details.
16 
17  You should have received a copy of the GNU Library General Public
18  License along with this library; if not, write to the Free
19  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  ---------------------------------------------------------------------------
21 */
22 
23 #ifndef __ccxx_String_hxx
24 #define __ccxx_String_hxx
25 
26 #include <commonc++/Common.h++>
28 #include <commonc++/Char.h++>
29 #include <commonc++/CharRef.h++>
30 #include <commonc++/CString.h++>
32 #include <commonc++/Hash.h++>
35 
36 #include <string>
37 #include <vector>
38 #include <cerrno>
39 #include <iosfwd>
40 
41 namespace ccxx {
42 
43 class StringVec;
44 
51 {
52  public:
53 
55  typedef const char16_t* const_iterator;
56 
58  String();
59 
65  String(const String& other);
66 
78  String(char16_t* str, uint_t offset = 0, uint_t count = 0, bool copy = true);
79 
90  String(const char16_t* str, uint_t offset = 0, uint_t count = 0);
91 
98  String(const wchar_t* str);
99 
110  String(const char* str, uint_t offset = 0, uint_t count = 0);
111 
113  ~String();
114 
124  bool endsWith(const String& str, bool ignoreCase = false) const;
125 
135  bool endsWith(const Char& c, bool ignoreCase = false) const;
136 
146  bool endsWithAnyOf(const String& chars, bool ignoreCase = false) const;
147 
157  bool startsWith(const String& str, bool ignoreCase = false) const;
158 
168  bool startsWith(const Char& c, bool ignoreCase = false) const;
169 
180  bool startsWithAnyOf(const String& chars, bool ignoreCase = false) const;
181 
187  uint_t getLength() const;
188 
194  inline uint_t length() const
195  { return(getLength()); }
196 
204  void setLength(uint_t length);
205 
213  void reserve(uint_t capacity);
214 
219  uint_t getCapacity() const;
220 
225  void compact();
226 
232  inline String& clear()
233  {
234  setLength(0);
235  return(*this);
236  }
237 
244  bool isEmpty() const;
245 
252  bool isNull() const;
253 
261  bool isInteger() const;
262 
269  bool isAlphaNumeric() const;
270 
281  int compareTo(const String& other, bool ignoreCase = false) const;
282 
288  String& trim(const String &chars = whitespaceChars, bool begin = true,
289  bool end = true);
290 
297  inline String& trimBegin(const String& chars = whitespaceChars)
298  { return(trim(chars, true, false)); }
299 
306  inline String& trimEnd(const String& chars = whitespaceChars)
307  { return(trim(chars, false, true)); }
308 
315  String& chop(const Char& c);
316 
323  String& reverseChop(const Char& c);
324 
332  String& replace(const Char& a, const Char& b);
333 
341  String& replace(const String& a, const String& b);
342 
350  String& insert(const Char& c, uint_t index = 0);
351 
361  String& insert(const String& str, uint_t index = 0, uint_t length = 0);
362 
371  String& insert(const char16_t* str, uint_t index = 0, uint_t count = 0);
372 
382  inline String& insert(const wchar_t* str, uint_t index = 0, uint_t count = 0)
383  { return(insert(String(str), index, count)); }
384 
394  inline String& insert(const char* str, uint_t index = 0, uint_t count = 0)
395  { return(insert(String(str), index, count)); }
396 
403  String& append(bool v);
404 
410  inline String& append(char c)
411  { return(append(Char(c))); }
412 
418  inline String& append(char16_t c)
419  { return(append(Char(c))); }
420 
427  String& append(int val);
428 
437  String& append(int val, uint_t base);
438 
445  String& append(uint_t val);
446 
455  String& append(uint_t val, uint_t base);
456 
463  String& append(const int64_t& val);
464 
473  String& append(const double& val, uint_t precision = 6);
474 
481  String& append(const uint64_t& val);
482 
488  inline String& append(const Char& c)
489  { return(insert(c, END)); }
490 
498  inline String& append(const String& str, uint_t count = 0)
499  { return(insert(str, END, count)); }
500 
508  inline String& append(const char16_t* str, uint_t count = 0)
509  { return(insert(str, END, count)); }
510 
518  inline String& append(const char* str, uint_t count = 0)
519  { return(insert(String(str), END, count)); }
520 
528  inline String& append(const wchar_t* str, uint_t count = 0)
529  { return(insert(str, END, count)); }
530 
536  inline String& prepend(char c)
537  { return(insert(Char(c), 0)); }
538 
545  { return(insert(Char(c), 0)); }
546 
552  inline String& prepend(const Char& c)
553  { return(insert(c, 0)); }
554 
562  inline String& prepend(const String& str, uint_t count = 0)
563  { return(insert(str, 0, count)); }
564 
572  inline String& prepend(const char16_t* str, uint_t count = 0)
573  { return(insert(str, 0, count)); }
574 
582  inline String& prepend(const char* str, uint_t count = 0)
583  { return(insert(String(str), 0, count)); }
584 
592  inline String& prepend(const wchar_t* str, uint_t count = 0)
593  { return(insert(str, 0, count)); }
594 
602  String& remove(uint_t fromIndex, uint_t toIndex = END);
603 
611  Char charAt(uint_t index) const;
612 
620  String& setCharAt(uint_t index, const Char& c);
621 
628  uint_t hash(uint_t modulo = 256) const;
629 
639  String substring(uint_t fromIndex, uint_t toIndex = END) const;
640 
649  int indexOf(const Char& c, uint_t fromIndex = 0) const;
650 
659  inline int indexOf(const char* str, uint_t fromIndex = 0) const
660  { return(indexOf(String(str), fromIndex)); }
661 
671  int indexOfAnyOf(const String& chars, uint_t fromIndex = 0) const;
672 
681  int indexOf(const String& str, uint_t fromIndex = 0) const;
682 
692  int lastIndexOf(const Char& c, uint_t fromIndex = END) const;
693 
704  int lastIndexOfAnyOf(const String& chars, uint_t fromIndex = END) const;
705 
715  int lastIndexOf(const String& str, uint_t fromIndex = END) const;
716 
724  inline bool contains(const Char& c) const
725  { return(indexOf(c) >= 0); }
726 
734  inline bool contains(const String& str) const
735  { return(indexOf(str) >= 0); }
736 
745  inline bool containsAnyOf(const char* chars) const
746  { return(indexOfAnyOf(chars) >= 0); }
747 
759  String nextToken(uint_t& fromIndex, const String& delim = whitespaceChars)
760  const;
761 
775  uint_t split(StringVec& tokens, const String& delim = whitespaceChars,
776  uint_t limit = 0) const;
777 
779  String& toLowerCase();
780 
782  String& toUpperCase();
783 
790  String& fill(const Char& c, uint_t count);
791 
798  String& pad(const Char& c, uint_t count);
799 
806  bool toBool(uint_t index = 0) const;
807 
815  int toInt(uint_t index = 0, uint_t base = 10) const;
816 
824  uint_t toUInt(uint_t index = 0, uint_t base = 10) const;
825 
833  int64_t toInt64(uint_t index = 0, uint_t base = 10) const;
834 
842  uint64_t toUInt64(uint_t index = 0, uint_t base = 10) const;
843 
850  float toFloat(uint_t index = 0) const;
851 
858  double toDouble(uint_t index = 0) const;
859 
861  operator const void*() const;
862 
866  const char16_t* data() const;
867 
869  CString toUTF8() const;
870 
876  String& operator=(const char* other);
877 
883  String& operator=(const wchar_t* other);
884 
886  String& operator=(const String& other);
887 
893  inline String& operator=(const std::string& other)
894  { return(operator=(other.c_str())); }
895 
904  CharRef operator[](int index);
905 
914  const Char operator[](int index) const;
915 
921  const_iterator begin() const;
922 
928  const_iterator end() const;
929 
939  inline String operator()(uint_t fromIndex, uint_t toIndex = END) const
940  { return(substring(fromIndex, toIndex)); }
941 
948  inline bool operator!() const
949  { return(isNull()); }
950 
956  inline String& operator+=(char c)
957  { return(append(c)); }
958 
965  { return(append(c)); }
966 
972  inline String& operator+=(const Char& c)
973  { return(append(c)); }
974 
980  inline String& operator+=(const char* str)
981  { return(append(str)); }
982 
988  inline String& operator+=(const String& str)
989  { return(append(str)); }
990 
997  inline String& operator+=(bool val)
998  { return(append(val)); }
999 
1006  inline String& operator+=(int val)
1007  { return(append(val)); }
1008 
1015  inline String& operator+=(uint_t val)
1016  { return(append(val)); }
1017 
1024  inline String& operator+=(double val)
1025  { return(append(val)); }
1026 
1033  inline String& operator+=(int64_t val)
1034  { return(append(val)); }
1035 
1042  inline String& operator+=(uint64_t val)
1043  { return(append(val)); }
1044 
1046  inline String& operator<<(const char* str)
1047  { return(append(str)); }
1048 
1050  inline String& operator<<(const wchar_t* str)
1051  { return(append(str)); }
1052 
1054  inline String& operator<<(const String& str)
1055  { return(append(str)); }
1056 
1058  inline String& operator<<(bool val)
1059  { return(append(val)); }
1060 
1062  inline String& operator<<(char c)
1063  { return(append(c)); }
1064 
1067  { return(append(c)); }
1068 
1070  inline String& operator<<(const Char& c)
1071  { return(append(c)); }
1072 
1074  inline String& operator<<(int val)
1075  { return(append(val)); }
1076 
1078  inline String& operator<<(uint_t val)
1079  { return(append(val)); }
1080 
1082  inline String& operator<<(const int64_t& val)
1083  { return(append(val)); }
1084 
1086  inline String& operator<<(const uint64_t& val)
1087  { return(append(val)); }
1088 
1090  inline String& operator<<(const double& val)
1091  { return(append(val)); }
1092 
1094  inline String& operator^=(char c)
1095  { return(prepend(c)); }
1096 
1099  { return(prepend(c)); }
1100 
1102  inline String& operator^=(const Char& c)
1103  { return(prepend(c)); }
1104 
1106  inline String& operator^=(const char* str)
1107  { return(prepend(str)); }
1108 
1110  inline String& operator^=(const wchar_t* str)
1111  { return(prepend(str)); }
1112 
1114  inline String& operator^=(const String& str)
1115  { return(prepend(str)); }
1116 
1117  String operator+(const String& str);
1118  String operator+(const char* str);
1119  String operator+(const Char& c);
1120 
1122  void write(std::ostream& stream) const;
1123 
1125  static const uint_t END;
1126 
1128  static const String null;
1129 
1131  static const String nullRep;
1132 
1134  static const String empty;
1135 
1137  static const String whitespaceChars;
1138 
1139  private:
1140 
1141  class StringBuf;
1142 
1143  StringBuf* _buf;
1144 
1145  void _insert(const char16_t* str, uint_t length, uint_t index, uint_t count);
1146  void _release();
1147  void _makeCopy(uint_t size, bool unshareable = false);
1148  char16_t* _makeGap(uint_t index, uint_t gaplen);
1149  void _fromUTF8(const char* str, uint_t len, uint_t offset);
1150  void _fromUTF32(const char32_t* str, uint_t len, uint_t offset);
1151  void _fromWChar(const wchar_t* str);
1152 };
1153 
1159 class StringVec : public std::vector<String>
1160 {
1161  public:
1162 
1164  { }
1165 
1168  { push_back(s); return(*this); }
1169 
1171  static const StringVec emptyVec;
1172 };
1173 
1174 inline bool operator==(const String& s1, const String& s2)
1175 { return(s1.compareTo(s2) == 0); }
1176 
1177 inline bool operator==(const String& s1, const char* s2)
1178 { return(s1.compareTo(s2) == 0); }
1179 
1180 inline bool operator==(const char* s1, const String& s2)
1181 { return(s2.compareTo(s1) == 0); }
1182 
1183 inline bool operator!=(const String& s1, const String& s2)
1184 { return(s1.compareTo(s2) != 0); }
1185 
1186 inline bool operator!=(const String& s1, const char* s2)
1187 { return(s1.compareTo(s2) != 0); }
1188 
1189 inline bool operator!=(const char* s1, const String& s2)
1190 { return(s2.compareTo(s1) != 0); }
1191 
1192 inline bool operator<(const String& s1, const String& s2)
1193 { return(s1.compareTo(s2) < 0); }
1194 
1195 inline bool operator<(const String& s1, const char* s2)
1196 { return(s1.compareTo(s2) < 0); }
1197 
1198 inline bool operator<(const char* s1, const String& s2)
1199 { return(s2.compareTo(s1) > 0); }
1200 
1201 inline bool operator>(const String& s1, const String& s2)
1202 { return(s1.compareTo(s2) > 0); }
1203 
1204 inline bool operator>(const String& s1, const char* s2)
1205 { return(s1.compareTo(s2) > 0); }
1206 
1207 inline bool operator>(const char* s1, const String& s2)
1208 { return(s2.compareTo(s1) < 0); }
1209 
1210 inline bool operator<=(const String& s1, const String& s2)
1211 { return(s1.compareTo(s2) <= 0); }
1212 
1213 inline bool operator<=(const String& s1, const char* s2)
1214 { return(s1.compareTo(s2) <= 0); }
1215 
1216 inline bool operator<=(const char* s1, const String& s2)
1217 { return(s2.compareTo(s1) > 0); }
1218 
1219 inline bool operator>=(const String& s1, const String& s2)
1220 { return(s1.compareTo(s2) >= 0); }
1221 
1222 inline bool operator>=(const String& s1, const char* s2)
1223 { return(s1.compareTo(s2) >= 0); }
1224 
1225 inline bool operator>=(const char* s1, const String& s2)
1226 { return(s2.compareTo(s1) < 0); }
1227 
1228 inline String operator+(const char* s1, const String& s2)
1229 { return(String(s1) + s2); }
1230 
1231 inline String operator+(const Char& c, const String& s)
1232 { String t; t += c; t += s; return(t); }
1233 
1234 inline std::ostream& operator<<(std::ostream& stream, const String& s)
1235 { s.write(stream); return(stream); }
1236 
1237 } // namespace ccxx
1238 
1239 #endif // __ccxx_String_hxx
String & operator+=(uint_t val)
Append a string representation of an unsigned integer value onto the end of the string.
Definition: String.h++:1015
String & operator+=(const String &str)
Append a string onto the end of this string.
Definition: String.h++:988
StringVec & operator<<(String s)
Append operator.
Definition: String.h++:1167
String & operator<<(const double &val)
Append operator.
Definition: String.h++:1090
bool containsAnyOf(const char *chars) const
Determine if the string contains any character in a given set of characters.
Definition: String.h++:745
A String vector.
Definition: String.h++:1159
String & prepend(char16_t c)
Prepend a character to the beginning of the string.
Definition: String.h++:544
static const String empty
The empty string.
Definition: String.h++:1134
String & operator<<(uint_t val)
Append operator.
Definition: String.h++:1078
String & operator^=(char16_t c)
Prepend operator.
Definition: String.h++:1098
String & operator+=(const Char &c)
Append a character onto the end of the string.
Definition: String.h++:972
String & operator^=(const char *str)
Prepend operator.
Definition: String.h++:1106
String & append(const String &str, uint_t count=0)
Append a string to the end of the string.
Definition: String.h++:498
const TimeSpan operator+(const TimeSpan &s1, const TimeSpan &s2)
Definition: TimeSpan.c++:362
bool operator<=(const Blob &b1, const Blob &b2)
Definition: Blob.h++:356
String & operator<<(const Char &c)
Append operator.
Definition: String.h++:1070
String & operator+=(double val)
Append a string representation of double-precision floating point value onto the end of the string...
Definition: String.h++:1024
bool operator==(const Blob &b1, const Blob &b2)
Definition: Blob.h++:344
String & operator+=(uint64_t val)
Append a string representation of an unsigned long long (64-bit) integer onto the end of the string...
Definition: String.h++:1042
uint32_t char32_t
A UTF-32 character.
Definition: Common.h++:207
unsigned int uint_t
An alias for unsigned int.
Definition: Integers.h++:74
bool operator!() const
Unary NOT operator.
Definition: String.h++:948
String & operator+=(const char *str)
Append a C string onto the end of this string.
Definition: String.h++:980
String & append(char16_t c)
Append a UTF-16 character to the end of the string.
Definition: String.h++:418
String & operator+=(char16_t c)
Append a UTF-16 character onto the end of the string.
Definition: String.h++:964
String & insert(const wchar_t *str, uint_t index=0, uint_t count=0)
Insert a C wide string to this string.
Definition: String.h++:382
void write(std::ostream &stream) const
Write the string (in UTF-8 form) to a stream.
Definition: String.c++:1482
int compareTo(const String &other, bool ignoreCase=false) const
Compare the string to another.
Definition: String.c++:420
static const uint_t END
A pseudo-index indicating the end of the string.
Definition: String.h++:1125
bool contains(const String &str) const
Determine if the string contains a given substring.
Definition: String.h++:734
String & prepend(const char *str, uint_t count=0)
Prepend a string to the beginning of the string.
Definition: String.h++:582
static const String whitespaceChars
The set of whitespace delimiter characters.
Definition: String.h++:1137
String & prepend(char c)
Prepend a character to the beginning of the string.
Definition: String.h++:536
String & operator+=(int val)
Append a string representation of an integer value onto the end of the string.
Definition: String.h++:1006
String & operator=(const std::string &other)
Assignment operator.
Definition: String.h++:893
String & operator<<(const uint64_t &val)
Append operator.
Definition: String.h++:1086
String & append(const Char &c)
Append a character to the end of the string.
Definition: String.h++:488
String & prepend(const wchar_t *str, uint_t count=0)
Prepend a wide string to the beginning of the string.
Definition: String.h++:592
std::ostream & operator<<(std::ostream &stream, const BitSet &bs)
Definition: BitSet.h++:383
String operator()(uint_t fromIndex, uint_t toIndex=END) const
Substring operator.
Definition: String.h++:939
String & operator<<(const int64_t &val)
Append operator.
Definition: String.h++:1082
int indexOf(const char *str, uint_t fromIndex=0) const
Find the first occurrence of a given string in this string.
Definition: String.h++:659
char16_t value_type
Definition: String.h++:54
String & operator^=(const wchar_t *str)
Prepend operator.
Definition: String.h++:1110
String & operator<<(char c)
Append operator.
Definition: String.h++:1062
String & clear()
Clear the string.
Definition: String.h++:232
#define COMMONCPP_API
Definition: Common.h++:126
String & operator<<(int val)
Append operator.
Definition: String.h++:1074
String & trimEnd(const String &chars=whitespaceChars)
Trim characters from the end of the string.
Definition: String.h++:306
String & append(const wchar_t *str, uint_t count=0)
Append a wide string to the end of the string.
Definition: String.h++:528
String & operator+=(int64_t val)
Append a string representation of a long long (64-bit) integer onto the end of the string...
Definition: String.h++:1033
String & prepend(const char16_t *str, uint_t count=0)
Prepend a string to the beginning of the string.
Definition: String.h++:572
String & operator+=(char c)
Append a character onto the end of the string.
Definition: String.h++:956
bool operator!=(const Blob &b1, const Blob &b2)
Definition: Blob.h++:347
A class representing a UTF-16 character.
Definition: Char.h++:33
A flexible, reference counted, copy-on-write, thread-safe, nullable string.
Definition: String.h++:50
An implicitly shared, reference-counted container for an immutable, NUL-terminated C string...
Definition: CString.h++:39
String & operator<<(const String &str)
Append operator.
Definition: String.h++:1054
StringVec()
Definition: String.h++:1163
String & prepend(const String &str, uint_t count=0)
Prepend a string to the beginning of the string.
Definition: String.h++:562
uint_t length() const
Get the length of the string.
Definition: String.h++:194
bool operator<(const Blob &b1, const Blob &b2)
Definition: Blob.h++:350
String & prepend(const Char &c)
Prepend a character to the beginning of the string.
Definition: String.h++:552
uint16_t char16_t
A UTF-16 character.
Definition: Common.h++:201
String & append(char c)
Append a character to the end of the string.
Definition: String.h++:410
static const StringVec emptyVec
An empty StringVec.
Definition: String.h++:1171
String & insert(const char *str, uint_t index=0, uint_t count=0)
Insert a C string to this string.
Definition: String.h++:394
String & append(const char16_t *str, uint_t count=0)
Append a UTF-16 character array to the end of the string.
Definition: String.h++:508
String & operator+=(bool val)
Append a string representation of a boolean value onto the end of the string.
Definition: String.h++:997
bool operator>=(const Blob &b1, const Blob &b2)
Definition: Blob.h++:359
static const String null
The null string.
Definition: String.h++:1128
String & operator^=(const String &str)
Prepend operator.
Definition: String.h++:1114
String & operator^=(char c)
Prepend operator.
Definition: String.h++:1094
String & trimBegin(const String &chars=whitespaceChars)
Trim characters from the beginning of the string.
Definition: String.h++:297
String & append(const char *str, uint_t count=0)
Append a string to the end of the string.
Definition: String.h++:518
Definition: AllocationMap.c++:25
static const String nullRep
The string representation of the null value.
Definition: String.h++:1131
const char16_t * const_iterator
Definition: String.h++:55
bool operator>(const Blob &b1, const Blob &b2)
Definition: Blob.h++:353
String & operator<<(char16_t c)
Append operator.
Definition: String.h++:1066
String & operator<<(const wchar_t *str)
Append operator.
Definition: String.h++:1050
bool contains(const Char &c) const
Determine if the string contains a given character.
Definition: String.h++:724
String & operator<<(bool val)
Append operator.
Definition: String.h++:1058
String & operator^=(const Char &c)
Prepend operator.
Definition: String.h++:1102
String & operator<<(const char *str)
Append operator.
Definition: String.h++:1046