35 #include <Pt/Locale.h>
76 : _value( (unsigned char)ch )
81 : _value( (unsigned)(val) )
111 return _value > 0xff ? def :
static_cast<char>(_value);
119 PT_API std::ctype_base::mask ctypeMask(
const Char& ch);
127 return ctypeMask(ch) & std::ctype_base::alpha;
136 return ctypeMask(ch) & std::ctype_base::alnum;
145 return ctypeMask(ch) & std::ctype_base::punct;
154 return ctypeMask(ch) & std::ctype_base::cntrl;
161 inline int isdigit(
const Char& ch)
163 return ctypeMask(ch) & std::ctype_base::digit;
172 return ctypeMask(ch) & std::ctype_base::xdigit;
181 return ctypeMask(ch) & std::ctype_base::graph;
190 return ctypeMask(ch) & std::ctype_base::lower;
199 return ctypeMask(ch) & std::ctype_base::upper;
208 return ctypeMask(ch) & std::ctype_base::print;
217 return ctypeMask(ch) & std::ctype_base::space;
224 PT_API
Char tolower(
const Char& ch);
230 PT_API
Char toupper(
const Char& ch);
252 struct char_traits<Pt::
Char>
256 typedef streamoff off_type;
257 typedef streampos pos_type;
258 typedef Pt::MBState state_type;
260 inline static void assign(char_type& c1,
const char_type& c2)
265 inline static bool eq(
const char_type& c1,
const char_type& c2)
270 inline static bool lt(
const char_type& c1,
const char_type& c2)
275 inline static int compare(
const char_type* s1,
const char_type* s2, std::size_t n)
280 return lt(*s1, *s2) ? -1 : +1;
289 inline static std::size_t length(
const char_type* s)
294 while( ! eq(s[n], term) )
300 inline static const char_type* find(
const char_type* s, std::size_t n,
const char_type& a)
313 inline static char_type* move(char_type* s1,
const char_type* s2, std::size_t n)
318 inline static char_type* copy(char_type* s1,
const char_type* s2, std::size_t n)
323 inline static char_type* assign(char_type* s, std::size_t n, char_type a)
331 inline static char_type to_char_type(
const int_type& c)
336 inline static int_type to_int_type(
const char_type& c)
341 inline static bool eq_int_type(
const int_type& c1,
const int_type& c2)
346 inline static int_type eof()
351 inline static int_type not_eof(
const int_type& c)
353 return eq_int_type(c, eof()) ? 0 : c;
360 class PT_API basic_string<Pt::Char>
364 typedef std::size_t size_type;
365 typedef char_traits< Pt::Char > traits_type;
366 typedef std::allocator<Pt::Char> allocator_type;
367 typedef allocator_type::difference_type difference_type;
368 typedef allocator_type::reference reference;
369 typedef allocator_type::const_reference const_reference;
370 typedef allocator_type::pointer pointer;
371 typedef allocator_type::const_pointer const_pointer;
375 #ifdef _RWSTD_NO_CLASS_PARTIAL_SPEC
378 const_reverse_iterator;
380 typedef std::reverse_iterator<
iterator,
384 typedef std::reverse_iterator<iterator> reverse_iterator;
385 typedef const std::reverse_iterator<const_iterator> const_reverse_iterator;
388 static const size_type npos =
static_cast<size_type
>(-1);
393 explicit basic_string(
const allocator_type& a = allocator_type());
397 basic_string(
const Pt::Char* str,
const allocator_type& a = allocator_type());
401 basic_string(
const Pt::Char* str, size_type n,
const allocator_type& a = allocator_type());
405 basic_string(
const wchar_t* str,
const allocator_type& a = allocator_type());
409 basic_string(
const wchar_t* str, size_type n,
const allocator_type& a = allocator_type());
413 basic_string(
const char* str,
const allocator_type& a = allocator_type());
417 basic_string(
const char* str, size_type n,
const allocator_type& a = allocator_type());
421 basic_string(size_type n,
Pt::Char c,
const allocator_type& a = allocator_type());
425 basic_string(
const basic_string& str);
429 basic_string(
const basic_string& str,
const allocator_type& a);
433 basic_string(
const basic_string& str, size_type pos, size_type n = npos,
const allocator_type& a = allocator_type());
437 basic_string(
const Pt::Char* begin,
const Pt::Char* end,
const allocator_type& a = allocator_type());
439 template <
typename InputIterator>
440 basic_string(InputIterator begin, InputIterator end,
const allocator_type& a = allocator_type());
450 {
return privdata_rw(); }
455 {
return privdata_rw() + length(); }
459 const_iterator begin()
const
460 {
return privdata_ro(); }
464 const_iterator end()
const
465 {
return privdata_ro() + length(); }
469 reverse_iterator rbegin()
470 {
return reverse_iterator( this->end() ); }
474 reverse_iterator rend()
475 {
return reverse_iterator( this->begin() ); }
479 const_reverse_iterator rbegin()
const
480 {
return const_reverse_iterator( this->end() ); }
484 const_reverse_iterator rend()
const
485 {
return const_reverse_iterator( this->begin() ); }
489 reference operator[](size_type n)
490 {
return privdata_rw()[n]; }
494 const_reference operator[](size_type n)
const
495 {
return privdata_ro()[n]; }
499 reference at(size_type n)
502 throw out_of_range(
"at");
504 return privdata_rw()[n];
509 const_reference at(size_type n)
const
512 throw out_of_range(
"at");
514 return privdata_ro()[n];
525 void resize( std::size_t n,
Pt::Char ch = value_type() );
529 void reserve(std::size_t n = 0);
533 void swap(basic_string& str);
537 allocator_type get_allocator()
const
542 size_type copy(
Pt::Char* a, size_type n, size_type pos = 0)
const;
546 basic_string substr(size_type pos = 0, size_type n = npos)
const
547 {
return basic_string(*
this, pos, n); }
552 size_type length()
const
553 {
return isShortString() ? shortStringLength() : longStringLength(); }
557 size_type size()
const
563 {
return length() == 0; }
567 size_type max_size()
const
568 {
return ( size_type(-1) /
sizeof(
Pt::Char) ) - 1; }
572 size_type capacity()
const
573 {
return isShortString() ? shortStringCapacity() : longStringCapacity(); }
578 {
return privdata_ro(); }
583 {
return privdata_ro(); }
587 basic_string& assign(
const basic_string& str);
591 basic_string& assign(
const basic_string& str, size_type pos, size_type n);
595 basic_string& assign(
const wchar_t* str);
599 basic_string& assign(
const wchar_t* str, size_type n);
603 basic_string& assign(
const Pt::Char* str);
607 basic_string& assign(
const Pt::Char* str, size_type length);
611 basic_string& assign(
const char* str);
615 basic_string& assign(
const char* str, size_type length);
619 basic_string& assign(size_type n,
Pt::Char c);
621 template <
typename InputIterator>
622 basic_string& assign(InputIterator begin, InputIterator end);
630 basic_string& append(
const Pt::Char* str, size_type n);
634 basic_string& append(size_type n,
Pt::Char ch);
638 basic_string& append(
const basic_string& str);
642 basic_string& append(
const basic_string& str, size_type pos, size_type n);
644 template <
typename InputIterator>
645 basic_string& append(InputIterator begin, InputIterator end);
653 basic_string& insert(size_type pos,
const Pt::Char* str);
657 basic_string& insert(size_type pos,
const Pt::Char* str, size_type n);
661 basic_string& insert(size_type pos, size_type n,
Pt::Char ch);
665 basic_string& insert(size_type pos,
const basic_string& str);
669 basic_string& insert(size_type pos,
const basic_string& str, size_type pos2, size_type n);
690 basic_string& erase(size_type pos = 0, size_type n = npos);
694 iterator erase(iterator pos);
698 iterator erase(iterator first, iterator last);
702 basic_string& replace(size_type pos, size_type n,
const Pt::Char* str);
706 basic_string& replace(size_type pos, size_type n,
const Pt::Char* str, size_type n2);
710 basic_string& replace(size_type pos, size_type n, size_type n2,
Pt::Char ch);
714 basic_string& replace(size_type pos, size_type n,
const basic_string& str);
718 basic_string& replace(size_type pos, size_type n,
const basic_string& str, size_type pos2, size_type n2);
722 basic_string& replace(iterator i1, iterator i2,
const Pt::Char* str);
726 basic_string& replace(iterator i1, iterator i2,
const Pt::Char* str, size_type n);
730 basic_string& replace(iterator i1, iterator i2, size_type n,
Pt::Char ch);
734 basic_string& replace(iterator i1, iterator i2,
const basic_string& str);
738 int compare(
const basic_string& str)
const;
742 int compare(
const Pt::Char* str)
const;
746 int compare(
const Pt::Char* str, size_type n)
const;
750 int compare(
const wchar_t* str)
const;
754 int compare(
const wchar_t* str, size_type n)
const;
758 int compare(
const char* str)
const;
762 int compare(
const char* str, size_type n)
const;
766 int compare(size_type pos, size_type n,
const basic_string& str)
const;
770 int compare(size_type pos, size_type n,
const basic_string& str, size_type pos2, size_type n2)
const;
774 int compare(size_type pos, size_type n,
const Pt::Char* str)
const;
778 int compare(size_type pos, size_type n,
const Pt::Char* str, size_type n2)
const;
782 size_type find(
const basic_string& str, size_type pos = 0)
const;
786 size_type find(
const Pt::Char* str, size_type pos, size_type n)
const;
790 size_type find(
const Pt::Char* str, size_type pos = 0)
const;
794 size_type find(
Pt::Char ch, size_type pos = 0)
const;
798 size_type rfind(
const basic_string& str, size_type pos = npos)
const;
802 size_type rfind(
const Pt::Char* str, size_type pos, size_type n)
const;
806 size_type rfind(
const Pt::Char* str, size_type pos = npos)
const;
810 size_type rfind(
Pt::Char ch, size_type pos = npos)
const;
814 size_type find_first_of(
const basic_string& str, size_type pos = 0)
const
815 {
return this->find_first_of( str.data(), pos, str.size() ); }
819 size_type find_first_of(
const Pt::Char* s, size_type pos, size_type n)
const;
823 size_type find_first_of(
const Pt::Char* str, size_type pos = 0)
const
824 {
return this->find_first_of( str, pos, traits_type::length(str) ); }
828 size_type find_first_of(
const Pt::Char ch, size_type pos = 0)
const
829 {
return this->find(ch, pos); }
833 size_type find_last_of(
const basic_string& str, size_type pos = npos)
const
834 {
return this->find_last_of( str.data(), pos, str.size() ); }
838 size_type find_last_of(
const Pt::Char* s, size_type pos, size_type n)
const;
842 size_type find_last_of(
const Pt::Char* str, size_type pos = npos)
const
843 {
return this->find_last_of( str, pos, traits_type::length(str) ); }
847 size_type find_last_of(
const Pt::Char ch, size_type pos = npos)
const
848 {
return this->rfind(ch, pos); }
852 size_type find_first_not_of(
const basic_string& str, size_type pos = 0)
const
853 {
return this->find_first_not_of( str.data(), pos, str.size() ); }
857 size_type find_first_not_of(
const Pt::Char* s, size_type pos, size_type n)
const;
861 size_type find_first_not_of(
const Pt::Char* str, size_type pos = 0)
const
862 {
return this->find_first_not_of( str, pos, traits_type::length(str) ); }
866 size_type find_first_not_of(
const Pt::Char ch, size_type pos = 0)
const;
870 size_type find_last_not_of(
const basic_string& str, size_type pos = npos)
const
871 {
return this->find_last_not_of( str.data(), pos, str.size() ); }
875 size_type find_last_not_of(
const Pt::Char* tok, size_type pos, size_type n)
const;
879 size_type find_last_not_of(
const Pt::Char* str, size_type pos = npos)
const
880 {
return this->find_last_not_of( str, pos, traits_type::length(str) ); }
884 size_type find_last_not_of(
Pt::Char ch, size_type pos = npos)
const;
889 std::string
narrow(
char dfault =
'?')
const;
893 static basic_string widen(
const char* str);
897 static basic_string widen(
const std::string& str);
901 template <
typename OutIterT>
902 OutIterT toUtf16(OutIterT to)
const;
906 template <
typename InIterT>
907 static basic_string fromUtf16(InIterT from, InIterT fromEnd);
912 basic_string& operator=(
const basic_string& str)
913 {
return this->assign(str); }
917 basic_string& operator=(
const wchar_t* str)
918 {
return this->assign(str); }
922 basic_string& operator=(
const char* str)
923 {
return this->assign(str); }
927 basic_string& operator=(
const Pt::Char* str)
928 {
return this->assign(str); }
932 basic_string& operator=(
Pt::Char ch)
944 basic_string& operator+=(
const basic_string& str)
945 {
return this->append(str); }
949 basic_string& operator+=(
const Pt::Char* str)
950 {
return this->append(str); }
954 basic_string& operator+=(
Pt::Char c);
965 static const unsigned _minN = (
sizeof(Ptr) /
sizeof(
Pt::Char)) + 1;
968 static const unsigned _nN = _minN < 8 ? 8 : _minN;
971 static const unsigned _nS = _nN *
sizeof(
Pt::Char);
973 struct Data :
public allocator_type
975 Data(
const allocator_type& a)
981 _u._s[_nS - 1] = _nN - 1;
987 unsigned char _s[_nS];
993 {
return isShortString() ? shortStringData() : longStringData(); }
996 {
return isShortString() ? shortStringData() : longStringData(); }
998 void privreserve(std::size_t n);
1000 bool isShortString()
const
1001 {
return shortStringMagic() != 0xff; }
1003 void markLongString()
1004 { shortStringMagic() = 0xff; }
1006 const Pt::Char* shortStringData()
const
1007 {
return reinterpret_cast<const Pt::Char*
>(&_d._u._s[0]); }
1010 {
return reinterpret_cast<Pt::Char*
>(&_d._u._s[0]); }
1012 unsigned char shortStringMagic()
const
1013 {
return _d._u._s[_nS - 1]; }
1015 unsigned char& shortStringMagic()
1016 {
return _d._u._s[_nS - 1]; }
1018 size_type shortStringLength()
const
1019 {
return _nN - 1 - shortStringMagic(); }
1021 size_type shortStringCapacity()
const
1024 void setShortStringLength(size_type n)
1026 shortStringData()[n] =
Pt::Char(0);
1027 shortStringMagic() =
static_cast<unsigned char>(_nN - n - 1);
1030 void shortStringAssign(
const Pt::Char* str, size_type n)
1032 traits_type::copy(shortStringData(), str, n);
1033 shortStringData()[n] =
Pt::Char(0);
1034 shortStringMagic() =
static_cast<unsigned char>(_nN - n - 1);
1036 void shortStringAssign(
const wchar_t* str, size_type n)
1038 for (size_type nn = 0; nn < n; ++nn)
1039 shortStringData()[nn] = str[nn];
1040 shortStringData()[n] =
Pt::Char(0);
1041 shortStringMagic() =
static_cast<unsigned char>(_nN - n - 1);
1044 const Pt::Char* longStringData()
const
1045 {
return _d._u._p._begin; }
1048 {
return _d._u._p._begin; }
1050 size_type longStringLength()
const
1051 {
return _d._u._p._end - _d._u._p._begin; }
1053 size_type longStringCapacity()
const
1054 {
return _d._u._p._capacity - _d._u._p._begin; }
1056 void setLength(size_type n)
1058 if (isShortString())
1059 setShortStringLength(n);
1062 _d._u._p._end = _d._u._p._begin + n;
1063 _d._u._p._begin[n] = 0;
1072 inline void swap(basic_string<Pt::Char>& a, basic_string<Pt::Char>& b)
1081 inline basic_string<Pt::Char> operator+(
const basic_string<Pt::Char>& a,
const basic_string<Pt::Char>& b)
1082 { basic_string<Pt::Char> temp; temp += a; temp += b;
return temp; }
1088 inline basic_string<Pt::Char> operator+(
const basic_string<Pt::Char>& a,
const Pt::Char* b)
1089 { basic_string<Pt::Char> temp; temp += a; temp += b;
return temp; }
1095 inline basic_string<Pt::Char> operator+(
const Pt::Char* a,
const basic_string<Pt::Char>& b)
1096 { basic_string<Pt::Char> temp; temp += a; temp += b;
return temp; }
1102 inline basic_string<Pt::Char> operator+(
const basic_string<Pt::Char>& a,
Pt::Char b)
1103 { basic_string<Pt::Char> temp; temp += a; temp += b;
return temp; }
1109 inline basic_string<Pt::Char> operator+(
Pt::Char a,
const basic_string<Pt::Char>& b)
1110 { basic_string<Pt::Char> temp; temp += a; temp += b;
return temp; }
1118 inline bool operator==(
const basic_string<Pt::Char>& a,
const basic_string<Pt::Char>& b)
1119 {
return a.compare(b) == 0; }
1125 inline bool operator==(
const Pt::Char* a,
const basic_string<Pt::Char>& b)
1126 {
return b.compare(a) == 0; }
1132 inline bool operator==(
const basic_string<Pt::Char>& a,
const Pt::Char* b)
1133 {
return a.compare(b) == 0; }
1139 inline bool operator==(
const basic_string<Pt::Char>& a,
const wchar_t* b)
1140 {
return a.compare(b) == 0; }
1146 inline bool operator==(
const wchar_t* b,
const basic_string<Pt::Char>& a)
1147 {
return a.compare(b) == 0; }
1153 inline bool operator==(
const basic_string<Pt::Char>& a,
const char* b)
1154 {
return a.compare(b) == 0; }
1160 inline bool operator==(
const char* b,
const basic_string<Pt::Char>& a)
1161 {
return a.compare(b) == 0; }
1169 inline bool operator!=(
const basic_string<Pt::Char>& a,
const basic_string<Pt::Char>& b)
1170 {
return a.compare(b) != 0; }
1176 inline bool operator!=(
const Pt::Char* a,
const basic_string<Pt::Char>& b)
1177 {
return b.compare(a) != 0; }
1183 inline bool operator!=(
const basic_string<Pt::Char>& a,
const Pt::Char* b)
1184 {
return a.compare(b) != 0; }
1190 inline bool operator!=(
const basic_string<Pt::Char>& a,
const wchar_t* b)
1191 {
return a.compare(b) != 0; }
1197 inline bool operator!=(
const wchar_t* b,
const basic_string<Pt::Char>& a)
1198 {
return a.compare(b) != 0; }
1204 inline bool operator!=(
const basic_string<Pt::Char>& a,
const char* b)
1205 {
return a.compare(b) != 0; }
1211 inline bool operator!=(
const char* b,
const basic_string<Pt::Char>& a)
1212 {
return a.compare(b) != 0; }
1220 inline bool operator<(const basic_string<Pt::Char>& a,
const basic_string<Pt::Char>& b)
1221 {
return a.compare(b) < 0; }
1227 inline bool operator<(const Pt::Char* a, const basic_string<Pt::Char>& b)
1228 {
return b.compare(a) > 0; }
1234 inline bool operator<(const basic_string<Pt::Char>& a,
const Pt::Char* b)
1235 {
return a.compare(b) < 0; }
1241 inline bool operator<(const basic_string<Pt::Char>& a,
const wchar_t* b)
1242 {
return a.compare(b) < 0; }
1248 inline bool operator<(const wchar_t* b, const basic_string<Pt::Char>& a)
1249 {
return a.compare(b) > 0; }
1255 inline bool operator<(const basic_string<Pt::Char>& a,
const char* b)
1256 {
return a.compare(b) < 0; }
1262 inline bool operator<(const char* b, const basic_string<Pt::Char>& a)
1263 {
return a.compare(b) > 0; }
1271 inline bool operator<=(const basic_string<Pt::Char>& a,
const basic_string<Pt::Char>& b)
1272 {
return a.compare(b) <= 0; }
1278 inline bool operator<=(const Pt::Char* a, const basic_string<Pt::Char>& b)
1279 {
return b.compare(a) >= 0; }
1285 inline bool operator<=(const basic_string<Pt::Char>& a,
const Pt::Char* b)
1286 {
return a.compare(b) <= 0; }
1292 inline bool operator<=(const basic_string<Pt::Char>& a,
const wchar_t* b)
1293 {
return a.compare(b) <= 0; }
1299 inline bool operator<=(const wchar_t* b, const basic_string<Pt::Char>& a)
1300 {
return a.compare(b) >= 0; }
1306 inline bool operator<=(const basic_string<Pt::Char>& a,
const char* b)
1307 {
return a.compare(b) <= 0; }
1313 inline bool operator<=(const char* b, const basic_string<Pt::Char>& a)
1314 {
return a.compare(b) >= 0; }
1322 inline bool operator>(
const basic_string<Pt::Char>& a,
const basic_string<Pt::Char>& b)
1323 {
return a.compare(b) > 0; }
1329 inline bool operator>(
const Pt::Char* a,
const basic_string<Pt::Char>& b)
1330 {
return b.compare(a) < 0; }
1336 inline bool operator>(
const basic_string<Pt::Char>& a,
const Pt::Char* b)
1337 {
return a.compare(b) > 0; }
1343 inline bool operator>(
const basic_string<Pt::Char>& a,
const wchar_t* b)
1344 {
return a.compare(b) > 0; }
1350 inline bool operator>(
const wchar_t* b,
const basic_string<Pt::Char>& a)
1351 {
return a.compare(b) < 0; }
1357 inline bool operator>(
const basic_string<Pt::Char>& a,
const char* b)
1358 {
return a.compare(b) > 0; }
1364 inline bool operator>(
const char* b,
const basic_string<Pt::Char>& a)
1365 {
return a.compare(b) < 0; }
1373 inline bool operator>=(
const basic_string<Pt::Char>& a,
const basic_string<Pt::Char>& b)
1374 {
return a.compare(b) >= 0; }
1380 inline bool operator>=(
const Pt::Char* a,
const basic_string<Pt::Char>& b)
1381 {
return b.compare(a) <= 0; }
1387 inline bool operator>=(
const basic_string<Pt::Char>& a,
const Pt::Char* b)
1388 {
return a.compare(b) >= 0; }
1394 inline bool operator>=(
const basic_string<Pt::Char>& a,
const wchar_t* b)
1395 {
return a.compare(b) >= 0; }
1401 inline bool operator>=(
const wchar_t* b,
const basic_string<Pt::Char>& a)
1402 {
return a.compare(b) <= 0; }
1408 inline bool operator>=(
const basic_string<Pt::Char>& a,
const char* b)
1409 {
return a.compare(b) >= 0; }
1415 inline bool operator>=(
const char* b,
const basic_string<Pt::Char>& a)
1416 {
return a.compare(b) <= 0; }
1422 PT_API ostream& operator<< (ostream& out, const basic_string<Pt::Char>& str);
1428 typedef std::basic_string<Pt::Char> String;
1432 #ifdef PT_WITH_STD_LOCALE
1433 #include <Pt/Facets.h>
1437 #include <Pt/String.tpp>
int isgraph(const Char &ch)
Checks whether ch is a graphical character.
Definition: String.h:179
int islower(const Char &ch)
Checks whether ch is lower case.
Definition: String.h:188
int isspace(const Char &ch)
Checks whether ch is a whitespace character.
Definition: String.h:215
R narrow(T from)
Checked numeric conversion.
Definition: Convert.h:112
int isalnum(const Char &ch)
Checks whether ch is a alphanumeric character.
Definition: String.h:134
uint32_t value() const
Returns the unicode value.
Definition: String.h:90
Char(int val)
Construct from int.
Definition: String.h:80
Char & operator=(const Char &ch)
Assignment operator.
Definition: String.h:98
Unicode character type.
Definition: String.h:66
int iscntrl(const Char &ch)
Checks whether ch is a control character.
Definition: String.h:152
char narrow(char def= '?') const
Narrows the character to 8-bit.
Definition: String.h:109
int isalpha(const Char &ch)
Checks whether ch is a alphabetic character.
Definition: String.h:125
int ispunct(const Char &ch)
Checks whether ch is a punctuation character.
Definition: String.h:143
uint_type uint32_t
Unsigned 32-bit integer type.
Definition: Types.h:42
int isupper(const Char &ch)
Checks whether ch is upper case.
Definition: String.h:197
int isxdigit(const Char &ch)
Checks whether ch is a hexadecimal digit.
Definition: String.h:170
Char(uint32_t val)
Construct from unsigned 32-bit integer.
Definition: String.h:85
int isprint(const Char &ch)
Checks whether ch is a printable character.
Definition: String.h:206
Char(char ch)
Construct from char.
Definition: String.h:75
Char()
Default Constructor.
Definition: String.h:70