35 #include <Pt/Locale.h>
69 #if __cplusplus >= 201103L
80 : _value( (
unsigned char)ch )
85 : _value( (unsigned)(val) )
115 return _value > 0xff ? def :
static_cast<char>(_value);
125 #if _LIBCPP_VERSION >= 5000 && __cplusplus < 201103L
130 struct is_trivial<
Pt::Char> {
131 static const bool value =
true;
141 PT_API std::ctype_base::mask ctypeMask(
const Char& ch);
149 return ctypeMask(ch) & std::ctype_base::alpha;
158 return ctypeMask(ch) & std::ctype_base::alnum;
167 return ctypeMask(ch) & std::ctype_base::punct;
176 return ctypeMask(ch) & std::ctype_base::cntrl;
185 return ctypeMask(ch) & std::ctype_base::digit;
194 return ctypeMask(ch) & std::ctype_base::xdigit;
203 return ctypeMask(ch) & std::ctype_base::graph;
212 return ctypeMask(ch) & std::ctype_base::lower;
221 return ctypeMask(ch) & std::ctype_base::upper;
230 return ctypeMask(ch) & std::ctype_base::print;
239 return ctypeMask(ch) & std::ctype_base::space;
274 struct char_traits<
Pt::Char>
278 typedef streamoff off_type;
279 typedef streampos pos_type;
280 typedef Pt::MBState state_type;
282 inline static void assign(char_type& c1,
const char_type& c2)
287 inline static bool eq(
const char_type& c1,
const char_type& c2)
292 inline static bool lt(
const char_type& c1,
const char_type& c2)
297 inline static int compare(
const char_type* s1,
const char_type* s2, std::size_t n)
302 return lt(*s1, *s2) ? -1 : +1;
311 inline static std::size_t length(
const char_type* s)
316 while( ! eq(s[n], term) )
322 inline static const char_type* find(
const char_type* s, std::size_t n,
const char_type& a)
335 inline static char_type* move(char_type* s1,
const char_type* s2, std::size_t n)
337 return (
Pt::Char*) std::memmove(
static_cast<void*
>(s1),
338 static_cast<const void*
>(s2),
342 inline static char_type* copy(char_type* s1,
const char_type* s2, std::size_t n)
344 return (
Pt::Char*) std::memcpy(
static_cast<void*
>(s1),
345 static_cast<const void*
>(s2),
349 inline static char_type* assign(char_type* s, std::size_t n, char_type a)
357 inline static char_type to_char_type(
const int_type& c)
362 inline static int_type to_int_type(
const char_type& c)
367 inline static bool eq_int_type(
const int_type& c1,
const int_type& c2)
372 inline static int_type eof()
377 inline static int_type not_eof(
const int_type& c)
379 return eq_int_type(c, eof()) ? 0 : c;
386 class PT_API basic_string<
Pt::Char>
391 typedef const Pt::Char& const_reference;
393 typedef const Pt::Char* const_pointer;
395 typedef std::size_t size_type;
396 typedef char_traits<Pt::Char> traits_type;
397 typedef std::allocator<Pt::Char> allocator_type;
398 typedef allocator_type::difference_type difference_type;
400 typedef value_type* iterator;
401 typedef const value_type* const_iterator;
403 #ifdef _RWSTD_NO_CLASS_PARTIAL_SPEC
404 typedef std::reverse_iterator<const_iterator,
405 random_access_iterator_tag, value_type>
406 const_reverse_iterator;
408 typedef std::reverse_iterator<iterator,
409 random_access_iterator_tag, value_type>
412 typedef std::reverse_iterator<iterator> reverse_iterator;
413 typedef const std::reverse_iterator<const_iterator> const_reverse_iterator;
416 static const size_type npos =
static_cast<size_type
>(-1);
421 explicit basic_string(
const allocator_type& a = allocator_type());
425 basic_string(
const Pt::Char* str,
const allocator_type& a = allocator_type());
429 basic_string(
const Pt::Char* str, size_type n,
const allocator_type& a = allocator_type());
433 basic_string(
const wchar_t* str,
const allocator_type& a = allocator_type());
437 basic_string(
const wchar_t* str, size_type n,
const allocator_type& a = allocator_type());
441 basic_string(
const char* str,
const allocator_type& a = allocator_type());
445 basic_string(
const char* str, size_type n,
const allocator_type& a = allocator_type());
449 basic_string(size_type n,
Pt::Char c,
const allocator_type& a = allocator_type());
453 basic_string(
const basic_string& str);
457 basic_string(
const basic_string& str,
const allocator_type& a);
461 basic_string(
const basic_string& str, size_type pos, size_type n = npos,
const allocator_type& a = allocator_type());
465 basic_string(
const Pt::Char* begin,
const Pt::Char* end,
const allocator_type& a = allocator_type());
467 template <
typename InputIterator>
468 basic_string(InputIterator begin, InputIterator end,
const allocator_type& a = allocator_type());
478 {
return privdata_rw(); }
483 {
return privdata_rw() + length(); }
487 const_iterator begin()
const
488 {
return privdata_ro(); }
492 const_iterator end()
const
493 {
return privdata_ro() + length(); }
497 reverse_iterator rbegin()
498 {
return reverse_iterator( this->end() ); }
502 reverse_iterator rend()
503 {
return reverse_iterator( this->begin() ); }
507 const_reverse_iterator rbegin()
const
508 {
return const_reverse_iterator( this->end() ); }
512 const_reverse_iterator rend()
const
513 {
return const_reverse_iterator( this->begin() ); }
517 reference operator[](size_type n)
518 {
return privdata_rw()[n]; }
522 const_reference operator[](size_type n)
const
523 {
return privdata_ro()[n]; }
527 reference at(size_type n)
530 throw out_of_range(
"at");
532 return privdata_rw()[n];
537 const_reference at(size_type n)
const
540 throw out_of_range(
"at");
542 return privdata_ro()[n];
553 void resize( std::size_t n,
Pt::Char ch = value_type() );
557 void reserve(std::size_t n = 0);
561 void swap(basic_string& str);
565 allocator_type get_allocator()
const
570 size_type copy(
Pt::Char* a, size_type n, size_type pos = 0)
const;
574 basic_string substr(size_type pos = 0, size_type n = npos)
const
575 {
return basic_string(*
this, pos, n); }
580 size_type length()
const
581 {
return isShortString() ? shortStringLength() : longStringLength(); }
585 size_type size()
const
591 {
return length() == 0; }
595 size_type max_size()
const
596 {
return ( size_type(-1) /
sizeof(
Pt::Char) ) - 1; }
600 size_type capacity()
const
601 {
return isShortString() ? shortStringCapacity() : longStringCapacity(); }
606 {
return privdata_ro(); }
611 {
return privdata_ro(); }
615 basic_string& assign(
const basic_string& str);
619 basic_string& assign(
const basic_string& str, size_type pos, size_type n);
623 basic_string& assign(
const wchar_t* str);
627 basic_string& assign(
const wchar_t* str, size_type n);
631 basic_string& assign(
const Pt::Char* str);
635 basic_string& assign(
const Pt::Char* str, size_type length);
639 basic_string& assign(
const char* str);
643 basic_string& assign(
const char* str, size_type length);
647 basic_string& assign(size_type n,
Pt::Char c);
649 template <
typename InputIterator>
650 basic_string& assign(InputIterator begin, InputIterator end);
654 basic_string& append(
const Pt::Char* str);
658 basic_string& append(
const Pt::Char* str, size_type n);
662 basic_string& append(size_type n,
Pt::Char ch);
666 basic_string& append(
const basic_string& str);
670 basic_string& append(
const basic_string& str, size_type pos, size_type n);
672 template <
typename InputIterator>
673 basic_string& append(InputIterator begin, InputIterator end);
681 basic_string& insert(size_type pos,
const Pt::Char* str);
685 basic_string& insert(size_type pos,
const Pt::Char* str, size_type n);
689 basic_string& insert(size_type pos, size_type n,
Pt::Char ch);
693 basic_string& insert(size_type pos,
const basic_string& str);
697 basic_string& insert(size_type pos,
const basic_string& str, size_type pos2, size_type n);
701 basic_string& insert(iterator p,
Pt::Char ch);
705 basic_string& insert(iterator p, size_type n,
Pt::Char ch);
718 basic_string& erase(size_type pos = 0, size_type n = npos);
722 iterator erase(iterator pos);
726 iterator erase(iterator first, iterator last);
730 basic_string& replace(size_type pos, size_type n,
const Pt::Char* str);
734 basic_string& replace(size_type pos, size_type n,
const Pt::Char* str, size_type n2);
738 basic_string& replace(size_type pos, size_type n, size_type n2,
Pt::Char ch);
742 basic_string& replace(size_type pos, size_type n,
const basic_string& str);
746 basic_string& replace(size_type pos, size_type n,
const basic_string& str, size_type pos2, size_type n2);
750 basic_string& replace(iterator i1, iterator i2,
const Pt::Char* str);
754 basic_string& replace(iterator i1, iterator i2,
const Pt::Char* str, size_type n);
758 basic_string& replace(iterator i1, iterator i2, size_type n,
Pt::Char ch);
762 basic_string& replace(iterator i1, iterator i2,
const basic_string& str);
766 int compare(
const basic_string& str)
const;
770 int compare(
const Pt::Char* str)
const;
774 int compare(
const Pt::Char* str, size_type n)
const;
778 int compare(
const wchar_t* str)
const;
782 int compare(
const wchar_t* str, size_type n)
const;
786 int compare(
const char* str)
const;
790 int compare(
const char* str, size_type n)
const;
794 int compare(size_type pos, size_type n,
const basic_string& str)
const;
798 int compare(size_type pos, size_type n,
const basic_string& str, size_type pos2, size_type n2)
const;
802 int compare(size_type pos, size_type n,
const Pt::Char* str)
const;
806 int compare(size_type pos, size_type n,
const Pt::Char* str, size_type n2)
const;
810 size_type find(
const basic_string& str, size_type pos = 0)
const;
814 size_type find(
const Pt::Char* str, size_type pos, size_type n)
const;
818 size_type find(
const Pt::Char* str, size_type pos = 0)
const;
822 size_type find(
Pt::Char ch, size_type pos = 0)
const;
826 size_type rfind(
const basic_string& str, size_type pos = npos)
const;
830 size_type rfind(
const Pt::Char* str, size_type pos, size_type n)
const;
834 size_type rfind(
const Pt::Char* str, size_type pos = npos)
const;
838 size_type rfind(
Pt::Char ch, size_type pos = npos)
const;
842 size_type find_first_of(
const basic_string& str, size_type pos = 0)
const
843 {
return this->find_first_of( str.data(), pos, str.size() ); }
847 size_type find_first_of(
const Pt::Char* s, size_type pos, size_type n)
const;
851 size_type find_first_of(
const Pt::Char* str, size_type pos = 0)
const
852 {
return this->find_first_of( str, pos, traits_type::length(str) ); }
856 size_type find_first_of(
const Pt::Char ch, size_type pos = 0)
const
857 {
return this->find(ch, pos); }
861 size_type find_last_of(
const basic_string& str, size_type pos = npos)
const
862 {
return this->find_last_of( str.data(), pos, str.size() ); }
866 size_type find_last_of(
const Pt::Char* s, size_type pos, size_type n)
const;
870 size_type find_last_of(
const Pt::Char* str, size_type pos = npos)
const
871 {
return this->find_last_of( str, pos, traits_type::length(str) ); }
875 size_type find_last_of(
const Pt::Char ch, size_type pos = npos)
const
876 {
return this->rfind(ch, pos); }
880 size_type find_first_not_of(
const basic_string& str, size_type pos = 0)
const
881 {
return this->find_first_not_of( str.data(), pos, str.size() ); }
885 size_type find_first_not_of(
const Pt::Char* s, size_type pos, size_type n)
const;
889 size_type find_first_not_of(
const Pt::Char* str, size_type pos = 0)
const
890 {
return this->find_first_not_of( str, pos, traits_type::length(str) ); }
894 size_type find_first_not_of(
const Pt::Char ch, size_type pos = 0)
const;
898 size_type find_last_not_of(
const basic_string& str, size_type pos = npos)
const
899 {
return this->find_last_not_of( str.data(), pos, str.size() ); }
903 size_type find_last_not_of(
const Pt::Char* tok, size_type pos, size_type n)
const;
907 size_type find_last_not_of(
const Pt::Char* str, size_type pos = npos)
const
908 {
return this->find_last_not_of( str, pos, traits_type::length(str) ); }
912 size_type find_last_not_of(
Pt::Char ch, size_type pos = npos)
const;
917 std::string
narrow(
char dfault =
'?')
const;
921 static basic_string widen(
const char* str);
925 static basic_string widen(
const std::string& str);
929 template <
typename OutIterT>
930 OutIterT toUtf16(OutIterT to)
const;
934 template <
typename InIterT>
935 static basic_string fromUtf16(InIterT from, InIterT fromEnd);
940 basic_string& operator=(
const basic_string& str)
941 {
return this->assign(str); }
945 basic_string& operator=(
const wchar_t* str)
946 {
return this->assign(str); }
950 basic_string& operator=(
const char* str)
951 {
return this->assign(str); }
955 basic_string& operator=(
const Pt::Char* str)
956 {
return this->assign(str); }
960 basic_string& operator=(
Pt::Char ch)
972 basic_string& operator+=(
const basic_string& str)
973 {
return this->append(str); }
977 basic_string& operator+=(
const Pt::Char* str)
978 {
return this->append(str); }
982 basic_string& operator+=(
Pt::Char c);
993 static const unsigned _minN = (
sizeof(Ptr) /
sizeof(
Pt::Char)) + 1;
996 static const unsigned _nN = _minN < 8 ? 8 : _minN;
999 static const unsigned _nS = _nN *
sizeof(
Pt::Char);
1001 struct Data :
public allocator_type
1003 Data(
const allocator_type& a)
1009 _u._s[_nS - 1] = _nN - 1;
1015 unsigned char _s[_nS];
1020 const Pt::Char* privdata_ro()
const
1021 {
return isShortString() ? shortStringData() : longStringData(); }
1024 {
return isShortString() ? shortStringData() : longStringData(); }
1026 void privreserve(std::size_t n);
1028 bool isShortString()
const
1029 {
return shortStringMagic() != 0xff; }
1031 void markLongString()
1032 { shortStringMagic() = 0xff; }
1034 const Pt::Char* shortStringData()
const
1035 {
return reinterpret_cast<const Pt::Char*
>(&_d._u._s[0]); }
1038 {
return reinterpret_cast<Pt::Char*
>(&_d._u._s[0]); }
1040 unsigned char shortStringMagic()
const
1041 {
return _d._u._s[_nS - 1]; }
1043 unsigned char& shortStringMagic()
1044 {
return _d._u._s[_nS - 1]; }
1046 size_type shortStringLength()
const
1047 {
return _nN - 1 - shortStringMagic(); }
1049 size_type shortStringCapacity()
const
1052 void setShortStringLength(size_type n)
1054 shortStringData()[n] =
Pt::Char(0);
1055 shortStringMagic() =
static_cast<unsigned char>(_nN - n - 1);
1058 void shortStringAssign(
const Pt::Char* str, size_type n)
1060 traits_type::copy(shortStringData(), str, n);
1061 shortStringData()[n] =
Pt::Char(0);
1062 shortStringMagic() =
static_cast<unsigned char>(_nN - n - 1);
1064 void shortStringAssign(
const wchar_t* str, size_type n)
1066 for (size_type nn = 0; nn < n; ++nn)
1067 shortStringData()[nn] = str[nn];
1068 shortStringData()[n] =
Pt::Char(0);
1069 shortStringMagic() =
static_cast<unsigned char>(_nN - n - 1);
1072 const Pt::Char* longStringData()
const
1073 {
return _d._u._p._begin; }
1076 {
return _d._u._p._begin; }
1078 size_type longStringLength()
const
1079 {
return _d._u._p._end - _d._u._p._begin; }
1081 size_type longStringCapacity()
const
1082 {
return _d._u._p._capacity - _d._u._p._begin; }
1084 void setLength(size_type n)
1086 if (isShortString())
1087 setShortStringLength(n);
1090 _d._u._p._end = _d._u._p._begin + n;
1091 _d._u._p._begin[n] = 0;
1100 inline void swap(basic_string<Pt::Char>& a, basic_string<Pt::Char>& b)
1109 inline basic_string<Pt::Char> operator+(
const basic_string<Pt::Char>& a,
const basic_string<Pt::Char>& b)
1110 { basic_string<Pt::Char> temp; temp += a; temp += b;
return temp; }
1116 inline basic_string<Pt::Char> operator+(
const basic_string<Pt::Char>& a,
const Pt::Char* b)
1117 { basic_string<Pt::Char> temp; temp += a; temp += b;
return temp; }
1123 inline basic_string<Pt::Char> operator+(
const Pt::Char* a,
const basic_string<Pt::Char>& b)
1124 { basic_string<Pt::Char> temp; temp += a; temp += b;
return temp; }
1130 inline basic_string<Pt::Char> operator+(
const basic_string<Pt::Char>& a,
Pt::Char b)
1131 { basic_string<Pt::Char> temp; temp += a; temp += b;
return temp; }
1137 inline basic_string<Pt::Char> operator+(
Pt::Char a,
const basic_string<Pt::Char>& b)
1138 { basic_string<Pt::Char> temp; temp += a; temp += b;
return temp; }
1146 inline bool operator==(
const basic_string<Pt::Char>& a,
const basic_string<Pt::Char>& b)
1147 {
return a.compare(b) == 0; }
1153 inline bool operator==(
const Pt::Char* a,
const basic_string<Pt::Char>& b)
1154 {
return b.compare(a) == 0; }
1160 inline bool operator==(
const basic_string<Pt::Char>& a,
const Pt::Char* b)
1161 {
return a.compare(b) == 0; }
1167 inline bool operator==(
const basic_string<Pt::Char>& a,
const wchar_t* b)
1168 {
return a.compare(b) == 0; }
1174 inline bool operator==(
const wchar_t* b,
const basic_string<Pt::Char>& a)
1175 {
return a.compare(b) == 0; }
1181 inline bool operator==(
const basic_string<Pt::Char>& a,
const char* b)
1182 {
return a.compare(b) == 0; }
1188 inline bool operator==(
const char* b,
const basic_string<Pt::Char>& a)
1189 {
return a.compare(b) == 0; }
1197 inline bool operator!=(
const basic_string<Pt::Char>& a,
const basic_string<Pt::Char>& b)
1198 {
return a.compare(b) != 0; }
1204 inline bool operator!=(
const Pt::Char* a,
const basic_string<Pt::Char>& b)
1205 {
return b.compare(a) != 0; }
1211 inline bool operator!=(
const basic_string<Pt::Char>& a,
const Pt::Char* b)
1212 {
return a.compare(b) != 0; }
1218 inline bool operator!=(
const basic_string<Pt::Char>& a,
const wchar_t* b)
1219 {
return a.compare(b) != 0; }
1225 inline bool operator!=(
const wchar_t* b,
const basic_string<Pt::Char>& a)
1226 {
return a.compare(b) != 0; }
1232 inline bool operator!=(
const basic_string<Pt::Char>& a,
const char* b)
1233 {
return a.compare(b) != 0; }
1239 inline bool operator!=(
const char* b,
const basic_string<Pt::Char>& a)
1240 {
return a.compare(b) != 0; }
1248 inline bool operator<(
const basic_string<Pt::Char>& a,
const basic_string<Pt::Char>& b)
1249 {
return a.compare(b) < 0; }
1255 inline bool operator<(
const Pt::Char* a,
const basic_string<Pt::Char>& b)
1256 {
return b.compare(a) > 0; }
1262 inline bool operator<(
const basic_string<Pt::Char>& a,
const Pt::Char* b)
1263 {
return a.compare(b) < 0; }
1269 inline bool operator<(
const basic_string<Pt::Char>& a,
const wchar_t* b)
1270 {
return a.compare(b) < 0; }
1276 inline bool operator<(
const wchar_t* b,
const basic_string<Pt::Char>& a)
1277 {
return a.compare(b) > 0; }
1283 inline bool operator<(
const basic_string<Pt::Char>& a,
const char* b)
1284 {
return a.compare(b) < 0; }
1290 inline bool operator<(
const char* b,
const basic_string<Pt::Char>& a)
1291 {
return a.compare(b) > 0; }
1299 inline bool operator<=(
const basic_string<Pt::Char>& a,
const basic_string<Pt::Char>& b)
1300 {
return a.compare(b) <= 0; }
1306 inline bool operator<=(
const Pt::Char* a,
const basic_string<Pt::Char>& b)
1307 {
return b.compare(a) >= 0; }
1313 inline bool operator<=(
const basic_string<Pt::Char>& a,
const Pt::Char* b)
1314 {
return a.compare(b) <= 0; }
1320 inline bool operator<=(
const basic_string<Pt::Char>& a,
const wchar_t* b)
1321 {
return a.compare(b) <= 0; }
1327 inline bool operator<=(
const wchar_t* b,
const basic_string<Pt::Char>& a)
1328 {
return a.compare(b) >= 0; }
1334 inline bool operator<=(
const basic_string<Pt::Char>& a,
const char* b)
1335 {
return a.compare(b) <= 0; }
1341 inline bool operator<=(
const char* b,
const basic_string<Pt::Char>& a)
1342 {
return a.compare(b) >= 0; }
1350 inline bool operator>(
const basic_string<Pt::Char>& a,
const basic_string<Pt::Char>& b)
1351 {
return a.compare(b) > 0; }
1357 inline bool operator>(
const Pt::Char* a,
const basic_string<Pt::Char>& b)
1358 {
return b.compare(a) < 0; }
1364 inline bool operator>(
const basic_string<Pt::Char>& a,
const Pt::Char* b)
1365 {
return a.compare(b) > 0; }
1371 inline bool operator>(
const basic_string<Pt::Char>& a,
const wchar_t* b)
1372 {
return a.compare(b) > 0; }
1378 inline bool operator>(
const wchar_t* b,
const basic_string<Pt::Char>& a)
1379 {
return a.compare(b) < 0; }
1385 inline bool operator>(
const basic_string<Pt::Char>& a,
const char* b)
1386 {
return a.compare(b) > 0; }
1392 inline bool operator>(
const char* b,
const basic_string<Pt::Char>& a)
1393 {
return a.compare(b) < 0; }
1401 inline bool operator>=(
const basic_string<Pt::Char>& a,
const basic_string<Pt::Char>& b)
1402 {
return a.compare(b) >= 0; }
1408 inline bool operator>=(
const Pt::Char* a,
const basic_string<Pt::Char>& b)
1409 {
return b.compare(a) <= 0; }
1415 inline bool operator>=(
const basic_string<Pt::Char>& a,
const Pt::Char* b)
1416 {
return a.compare(b) >= 0; }
1422 inline bool operator>=(
const basic_string<Pt::Char>& a,
const wchar_t* b)
1423 {
return a.compare(b) >= 0; }
1429 inline bool operator>=(
const wchar_t* b,
const basic_string<Pt::Char>& a)
1430 {
return a.compare(b) <= 0; }
1436 inline bool operator>=(
const basic_string<Pt::Char>& a,
const char* b)
1437 {
return a.compare(b) >= 0; }
1443 inline bool operator>=(
const char* b,
const basic_string<Pt::Char>& a)
1444 {
return a.compare(b) <= 0; }
1457 PT_API ostream& operator<< (ostream& out,
const basic_string<Pt::Char>& str);
1463 typedef std::basic_string<Pt::Char> String;
1467 #ifdef PT_WITH_STD_LOCALE
1468 #include <Pt/Facets.h>
1472 #include <Pt/String.tpp>