00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef _Tanl_String_H
00025 #define _Tanl_String_H
00026
00027 #include "Char.h"
00028 #include "CharBuffer.h"
00029 #include <string>
00030 #include <stdexcept>
00031
00032 namespace Tanl {
00033 namespace Text {
00034
00040 class String {
00041
00042
00043
00044 public:
00045
00046 typedef std::basic_string<UCS2> StringType;
00047 typedef Char CharType;
00048
00049 typedef StringType::traits_type traits_type;
00050 typedef StringType::value_type value_type;
00051 typedef StringType::size_type size_type;
00052 typedef StringType::difference_type difference_type;
00053 typedef StringType::reference reference;
00054 typedef StringType::const_reference const_reference;
00055 typedef StringType::pointer pointer;
00056 typedef StringType::const_pointer const_pointer;
00057 typedef StringType::iterator iterator;
00058 typedef StringType::const_iterator const_iterator;
00059 typedef StringType::reverse_iterator reverse_iterator;
00060 typedef StringType::const_reverse_iterator const_reverse_iterator;
00061
00062 static const size_type npos = -1;
00063
00064
00065 String() { }
00066 String(const String&, const size_type = 0, const size_type = npos);
00067 String(const CharType*);
00068 String(const CharType*, const size_type);
00069 String(const size_type, const CharType);
00070 template<class InputIterator>
00071 String(InputIterator, InputIterator);
00072 # ifdef __GNUC__
00073 private:
00074 struct Rep {
00075 size_t len, res, ref;
00076 bool selfish;
00077 };
00078 public:
00083 String(CharBuffer<Char>::iterator b, CharBuffer<Char>::iterator e) :
00084 str()
00085 {
00086 Rep& rep = *reinterpret_cast<Rep*>(&str);
00087 rep.len = e - b;
00088 rep.res = rep.len;
00089 rep.ref = 2;
00090 *((UCS2**)(&rep + 1)) = (UCS2*)b;
00091 }
00092 # endif
00093
00094
00095 String(const StringType&);
00096
00097
00098 ~String() { }
00099
00100
00101
00102 size_type size() const { return str.size(); }
00103 size_type length() const { return str.length(); }
00104 bool empty() const { return str.empty(); }
00105 size_type max_size() const { return str.max_size(); }
00106
00107
00108
00109 size_type capacity() const { return str.capacity(); }
00110 void reserve(const size_type l) { str.reserve(l); }
00111
00112
00113 int compare(const String& s) const { return compare(0, npos, s); }
00114 int compare(const size_type, const size_type, const String&) const;
00115 int compare(const size_type, const size_type, const String&,
00116 const size_type, const size_type) const;
00117 int compare(const CharType*) const;
00118 int compare(const size_type, const size_type, const CharType*) const;
00119 int compare(const size_type, const size_type,
00120 const CharType*, const size_type) const;
00121
00122
00123 CharType& operator[](const size_type p);
00124 CharType operator[](const size_type p) const { return str[p]; }
00125 CharType& at(const size_type p);
00126 CharType at(const size_type p) const { return str.at(p); }
00127
00128
00129
00130
00131 String& assign(const String& s) { return assign(s, 0, npos); }
00132 String& assign(const String&, const size_type, const size_type);
00133 String& assign(const CharType*);
00134 String& assign(const CharType*, const size_type);
00135 String& assign(const size_type, const CharType);
00136
00137 String& operator=(const String s) { return assign(s); }
00138 String& operator=(const CharType* c) { return assign(c); }
00139 String& operator=(CharType c) { return assign(1, c); }
00140
00141 void swap(String&) throw ();
00142
00143 String& append(const String& s) { return append(s, 0, npos); }
00144 String& append(const String&, const size_type, const size_type);
00145 String& append(const CharType*, const size_type);
00146 String& append(const CharType*);
00147 String& append(const size_type, const CharType);
00148 template<class InputIterator>
00149 String& append(InputIterator, InputIterator);
00150
00151 void push_back(CharType c) { append(1, c); }
00152
00153 String& operator+=(const String& s) { return append(s); }
00154 String& operator+=(const CharType* c) { return append(c); }
00155 String& operator+=(const CharType c) { return append(1, c); }
00156
00157 String& insert(const size_type p, const String& s)
00158 { return insert(p, s, 0, npos); }
00159 String& insert(const size_type, const String&,
00160 const size_type, const size_type);
00161 String& insert(const size_type, const CharType*, const size_type);
00162 String& insert(const size_type, const CharType*);
00163 String& insert(const size_type, const size_type, const CharType);
00164 void insert(iterator, const size_type, const CharType);
00165 iterator insert(iterator, const CharType);
00166 void insert(iterator, iterator, iterator);
00167
00168 void clear();
00169 String& erase();
00170 String& erase(const size_type);
00171 String& erase(const size_type, const size_type);
00172 String& erase(iterator, iterator);
00173
00174 void resize(const size_type);
00175 void resize(const size_type, CharType);
00176
00177 String& replace(const size_type p1, const size_type p2, const String& s)
00178 { return replace(p1, p2, s, 0, npos); }
00179 String& replace(iterator, iterator, const String&);
00180 String& replace(const size_type, const size_type, const String&,
00181 const size_type, const size_type);
00182 String& replace(const size_type, const size_type,
00183 const CharType*, const size_type);
00184 String& replace(iterator, iterator, const CharType*, const size_type);
00185 String& replace(const size_type, const size_type, const CharType*);
00186 String& replace(iterator, iterator, const CharType*);
00187 String& replace(const size_type, const size_type, const size_type, CharType);
00188 String& replace(iterator, iterator, const size_type, CharType);
00189 template<class InputIterator>
00190 String& replace(iterator, iterator, InputIterator, InputIterator);
00191
00192 size_type find(const CharType c) const { return str.find(c); }
00193 size_type find(const CharType c, const size_type i)
00194 { return str.find(c, i); }
00195 size_type rfind(const CharType c) const { return str.rfind(c); }
00196 size_type rfind(const CharType c, const size_type i)
00197 { return str.rfind(c, i); }
00198
00199 size_type find(const String& s) const { return str.find(s.str); }
00200 size_type find(const String& s, const size_type i)
00201 { return str.find(s.str, i); }
00202 size_type rfind(const String& s) const { return str.rfind(s.str); }
00203 size_type rfind(const String& s, const size_type i)
00204 { return str.rfind(s.str, i); }
00205
00206 size_type find(const CharType* c) const { return str.find((UCS2*)c); }
00207 size_type find(const CharType* c, const size_type i)
00208 { return str.find((UCS2*)c, i); }
00209 size_type rfind(const CharType* c) const { return str.rfind((UCS2*)c); }
00210 size_type rfind(const CharType* c, const size_type i)
00211 { return str.rfind((UCS2*)c, i); }
00212
00213 size_type find(const char* c) const;
00214
00215 size_type find(const CharType* c, const size_type i, const size_type l) const
00216 { return str.find((UCS2*)c, i, l); }
00217 size_type rfind(const CharType* c, const size_type i, const size_type l)
00218 { return str.rfind((UCS2*)c, i, l); }
00219
00220 size_type find_first_of(const String& s) const
00221 { return str.find_first_of(s.str); }
00222 size_type find_first_of(const String& s, const size_type i)
00223 { return str.find_first_of(s.str, i); }
00224 size_type find_first_not_of(const String& s) const
00225 { return str.find_first_not_of(s.str); }
00226 size_type find_first_not_of(const String& s, const size_type i)
00227 { return str.find_first_not_of(s.str, i); }
00228
00229 size_type find_first_of(const CharType* c) const
00230 { return str.find_first_of((UCS2*)c); }
00231 size_type find_first_of(const CharType* c, const size_type i)
00232 { return str.find_first_of((UCS2*)c, i); }
00233 size_type find_first_not_of(const CharType* c) const
00234 { return str.find_first_not_of((UCS2*)c); }
00235 size_type find_first_not_of(const CharType* c, const size_type i)
00236 { return str.find_first_not_of((UCS2*)c, i); }
00237
00238 size_type find_first_of(const CharType* c, const size_type i,
00239 const size_type l) const
00240 { return str.find_first_of((UCS2*)c, i, l); }
00241 size_type find_first_not_of(const CharType* c, const size_type i,
00242 const size_type l) const
00243 { return str.find_first_not_of((UCS2*)c, i, l); }
00244
00245 size_type find_first_of(const CharType c) const
00246 { return str.find_first_of(c); }
00247 size_type find_first_of(const CharType c, const size_type i)
00248 { return str.find_first_of(c, i); }
00249 size_type find_first_not_of(const CharType c) const
00250 { return str.find_first_not_of(c); }
00251 size_type find_first_not_of(const CharType c, const size_type i)
00252 { return str.find_first_not_of(c, i); }
00253
00254 size_type find_last_of(const String& s) const
00255 { return str.find_last_of(s.str); }
00256 size_type find_last_of(const String& s, const size_type i)
00257 { return str.find_last_of(s.str, i); }
00258 size_type find_flast_not_of(const String& s) const
00259 { return str.find_last_not_of(s.str); }
00260 size_type find_last_not_of(const String& s, const size_type i)
00261 { return str.find_last_not_of(s.str, i); }
00262
00263 size_type find_last_of(const CharType* c) const
00264 { return str.find_last_of((UCS2*)c); }
00265 size_type find_last_of(const CharType* c, const size_type i)
00266 { return str.find_last_of((UCS2*)c, i); }
00267 size_type find_last_not_of(const CharType* c) const
00268 { return str.find_last_not_of((UCS2*)c); }
00269 size_type find_last_not_of(const CharType* c, const size_type i)
00270 { return str.find_last_not_of((UCS2*)c, i); }
00271
00272 size_type find_last_of(const CharType* c, const size_type i,
00273 const size_type l) const
00274 { return str.find_last_of((UCS2*)c, i, l); }
00275 size_type find_last_not_of(const CharType* c, const size_type i,
00276 const size_type l) const
00277 { return str.find_last_not_of((UCS2*)c, i, l); }
00278
00279 size_type find_last_of(const CharType c) const
00280 { return str.find_last_of(c); }
00281 size_type find_last_of(const CharType c, const size_type i)
00282 { return str.find_last_of(c, i); }
00283 size_type find_last_not_of(const CharType c) const
00284 { return str.find_last_not_of(c); }
00285 size_type find_last_not_of(const CharType c, const size_type i)
00286 { return str.find_last_not_of(c, i); }
00287
00288
00289
00290 String substr() const { return String(str.substr()); }
00291 String substr(const size_type i) const
00292 { return String(str.substr(i)); }
00293 String substr(const size_type i, const size_type l) const
00294 { return String(str.substr(i, l)); }
00295
00296
00297
00298 iterator begin() { return str.begin(); }
00299 const_iterator begin() const { return str.begin(); }
00300 iterator end() { return str.end(); }
00301 const_iterator end() const { return str.end(); }
00302 reverse_iterator rbegin() { return str.rbegin(); }
00303 const_reverse_iterator rbegin() const { return str.rbegin(); }
00304 reverse_iterator rend() { return str.rend(); }
00305 const_reverse_iterator rend() const { return str.rend(); }
00306
00307
00308
00309
00310
00311
00312 String(const std::string&, const size_t pos = 0,
00313 const std::string encoding = "UTF8");
00314 String(const char*, const std::string encoding = "UTF8");
00315 String(const char*, const size_t len,
00316 const std::string encoding = "UTF8");
00317
00318
00319
00320 std::string convert(const std::string& encoding = "UTF8") const;
00321
00322 size_t convert(const char*, const size_t start, const size_t end,
00323 const std::string& = "UTF8")
00324 throw (std::length_error);
00325
00329 bool StartsWith(const char* c) const;
00333 bool StartsWithNoCase(const char* c) const;
00334
00335 bool contains(const char* c) const;
00336
00337 String ToLower() const;
00338
00339 String ToLower();
00340
00341 private:
00342 StringType str;
00343 };
00344
00345 inline bool operator==(const String& s1, const String& s2)
00346 { return 0 == s1.compare(s2); }
00347 inline bool operator==(const String& s1, const std::string& s2)
00348 { return 0 == s1.compare(s2); }
00349 inline bool operator==(const String& s1, const char* s2)
00350 { return 0 == s1.compare(s2); }
00351 inline bool operator==(const std::string& s1, const String& s2)
00352 { return 0 == s2.compare(s1); }
00353 inline bool operator==(const char* s1, const String& s2)
00354 { return 0 == s2.compare(s1); }
00355 inline bool operator!=(const String& s1, const String& s2)
00356 { return 0 != s1.compare(s2); }
00357 inline bool operator<(const String& s1, const String& s2)
00358 { return 0 > s1.compare(s2); }
00359 inline bool operator>(const String& s1, const String& s2)
00360 { return 0 < s1.compare(s2); }
00361 inline bool operator<=(const String& s1, const String& s2)
00362 { return 0 >= s1.compare(s2); }
00363 inline bool operator>=(const String& s1, const String& s2)
00364 { return 0 <=s1.compare(s2); }
00365
00366 inline String operator+(const String& s1, const String& s2) {
00367 String r(s1);
00368 return r.append(s2);
00369 }
00370 inline String operator+(const String& s1, String::CharType* c) {
00371 String r(s1);
00372 return r.append(c);
00373 }
00374 inline String operator+(String::CharType* c, const String& s1) {
00375 String r(c);
00376 return r.append(s1);
00377 }
00378 inline String operator+(const String& s1, String::CharType c) {
00379 String r(s1);
00380 r.push_back(c);
00381 return r;
00382 }
00383 inline String operator+(String::CharType c, const String& s1) {
00384 String r(1, c);
00385 return r.append(s1);
00386 }
00387
00388 }
00389 }
00390
00391 namespace std
00392 {
00393 inline void swap(Tanl::Text::String& a, Tanl::Text::String& b) { a.swap(b); }
00394
00395 }
00396
00397 #endif // _Tanl_String_H