27 #ifndef MYGUI_U_STRING_H_
28 #define MYGUI_U_STRING_H_
39 #if MYGUI_COMPILER == MYGUI_COMPILER_MSVC
41 # pragma warning (push)
42 # pragma warning (disable : 4275)
52 #if MYGUI_COMPILER == MYGUI_COMPILER_MSVC && (1300 <= MYGUI_COMP_VER && MYGUI_COMP_VER <= 1310)
54 # if defined(_DLL_CPPLIB)
58 template class _CRTIMP2 basic_string<unsigned short, char_traits<unsigned short>,
59 allocator<unsigned short> >;
61 template class _CRTIMP2 basic_string<__wchar_t, char_traits<__wchar_t>,
62 allocator<__wchar_t> >;
65 # endif // defined(_DLL_CPPLIB)
67 #endif // MYGUI_COMPILER == MYGUI_COMPILER_MSVC && MYGUI_COMP_VER == 1300
106 #ifdef __STDC_ISO_10646__
109 #else // #ifdef __STDC_ISO_10646__
110 #if defined( __WIN32__ ) || defined( _WIN32 )
111 #define WCHAR_UTF16 // All currently known Windows platforms utilize UTF-16 encoding in wchar_t
112 #else // #if defined( __WIN32__ ) || defined( _WIN32 )
113 #if MYGUI_COMPILER != MYGUI_COMPILER_GCCE
114 #if WCHAR_MAX <= 0xFFFF // this is a last resort fall back test; WCHAR_MAX is defined in <wchar.h>
115 #define WCHAR_UTF16 // best we can tell, wchar_t is not larger than 16-bit
116 #endif // #if WCHAR_MAX <= 0xFFFF
118 #endif // #if defined( __WIN32__ ) || defined( _WIN32 )
119 #endif // #ifdef __STDC_ISO_10646__
124 #if MYGUI_COMPILER == MYGUI_COMPILER_MSVC
130 # if defined(_NATIVE_WCHAR_T_DEFINED)
131 # define MYGUI_IS_NATIVE_WCHAR_T 1
133 # define MYGUI_IS_NATIVE_WCHAR_T 0
135 #else // MYGUI_COMPILER != MYGUI_COMPILER_MSVC
138 # define MYGUI_IS_NATIVE_WCHAR_T 1
140 #endif // MYGUI_COMPILER == MYGUI_COMPILER_MSVC
170 static const unsigned char _lead1 = 0xC0;
171 static const unsigned char _lead1_mask = 0x1F;
172 static const unsigned char _lead2 = 0xE0;
173 static const unsigned char _lead2_mask = 0x0F;
174 static const unsigned char _lead3 = 0xF0;
175 static const unsigned char _lead3_mask = 0x07;
176 static const unsigned char _lead4 = 0xF8;
177 static const unsigned char _lead4_mask = 0x03;
178 static const unsigned char _lead5 = 0xFC;
179 static const unsigned char _lead5_mask = 0x01;
180 static const unsigned char _cont = 0x80;
181 static const unsigned char _cont_mask = 0x3F;
187 static const size_type npos =
static_cast<size_type
>(~0);
198 typedef std::basic_string<code_point>
dstring;
207 explicit invalid_data(
const std::string& _Message ): std::runtime_error( _Message ) {
219 void _seekFwd( size_type c );
220 void _seekRev( size_type c );
222 bool _test_begin()
const;
223 bool _test_end()
const;
224 size_type _get_index()
const;
225 void _jump_to( size_type index );
227 unicode_char _getCharacter()
const;
228 int _setCharacter( unicode_char uc );
270 value_type& operator*()
const;
273 value_type& operator[]( difference_type n )
const;
280 unicode_char getCharacter()
const;
282 int setCharacter( unicode_char uc );
316 const value_type& operator*()
const;
319 const value_type& operator[]( difference_type n )
const;
326 unicode_char getCharacter()
const;
377 value_type& operator*()
const;
380 value_type& operator[]( difference_type n )
const;
410 const value_type& operator*()
const;
413 const value_type& operator[]( difference_type n )
const;
445 UString( size_type length,
const code_point& ch );
447 UString(
const code_point* str );
449 UString(
const code_point* str, size_type length );
452 #if MYGUI_IS_NATIVE_WCHAR_T
453 UString(
const wchar_t* w_str );
456 UString(
const wchar_t* w_str, size_type length );
458 UString(
const std::wstring& wstr );
463 UString(
const char* c_str, size_type length );
465 UString(
const std::string& str );
475 size_type size()
const;
478 size_type length()
const;
481 size_type length_Characters()
const;
483 size_type max_size()
const;
485 void reserve( size_type size );
487 void resize( size_type num,
const code_point& val = 0 );
493 const code_point* c_str()
const;
495 const code_point* data()
const;
497 size_type capacity()
const;
502 UString substr( size_type index, size_type num = npos )
const;
504 void push_back( unicode_char val );
505 #if MYGUI_IS_NATIVE_WCHAR_T
506 void push_back(
wchar_t val );
512 void push_back( code_point val );
515 void push_back(
char val );
517 bool inString( unicode_char ch )
const;
524 const std::string& asUTF8()
const;
527 const char* asUTF8_c_str()
const;
529 const utf32string& asUTF32()
const;
531 const unicode_char* asUTF32_c_str()
const;
533 const std::wstring& asWStr()
const;
535 const wchar_t* asWStr_c_str()
const;
542 code_point& at( size_type loc );
545 const code_point& at( size_type loc )
const;
551 unicode_char getChar( size_type loc )
const;
561 int setChar( size_type loc, unicode_char ch );
571 const_iterator begin()
const;
575 const_iterator end()
const;
577 reverse_iterator rbegin();
579 const_reverse_iterator rbegin()
const;
581 reverse_iterator rend();
583 const_reverse_iterator rend()
const;
590 UString& assign( iterator start, iterator end );
595 UString& assign(
const code_point* str );
597 UString& assign(
const code_point* str, size_type num );
601 UString& assign( size_type num,
const code_point& ch );
603 UString& assign(
const std::wstring& wstr );
604 #if MYGUI_IS_NATIVE_WCHAR_T
605 UString& assign(
const wchar_t* w_str );
608 UString& assign(
const wchar_t* w_str, size_type num );
610 UString& assign(
const std::string& str );
613 UString& assign(
const char* c_str );
615 UString& assign(
const char* c_str, size_type num );
625 UString& append(
const code_point* str );
629 UString& append(
const code_point* str, size_type num );
631 UString& append( size_type num, code_point ch );
633 UString& append( iterator start, iterator end );
634 #if MYGUI_IS_NATIVE_WCHAR_T
635 UString& append(
const wchar_t* w_str, size_type num );
638 UString& append( size_type num,
wchar_t ch );
640 UString& append(
const char* c_str, size_type num );
643 UString& append( size_type num,
char ch );
645 UString& append( size_type num, unicode_char ch );
652 iterator insert( iterator i,
const code_point& ch );
658 mData.
insert( index, str );
662 UString& insert( size_type index1,
const UString& str, size_type index2, size_type num );
664 void insert( iterator i, iterator start, iterator end );
666 UString& insert( size_type index,
const code_point* str, size_type num );
667 #if MYGUI_IS_NATIVE_WCHAR_T
668 UString& insert( size_type index,
const wchar_t* w_str, size_type num );
671 UString& insert( size_type index,
const char* c_str, size_type num );
674 UString& insert( size_type index, size_type num, code_point ch );
675 #if MYGUI_IS_NATIVE_WCHAR_T
676 UString& insert( size_type index, size_type num,
wchar_t ch );
679 UString& insert( size_type index, size_type num,
char ch );
682 UString& insert( size_type index, size_type num, unicode_char ch );
684 void insert( iterator i, size_type num,
const code_point& ch );
685 #if MYGUI_IS_NATIVE_WCHAR_T
686 void insert( iterator i, size_type num,
const wchar_t& ch );
689 void insert( iterator i, size_type num,
const char& ch );
692 void insert( iterator i, size_type num,
const unicode_char& ch );
699 iterator erase( iterator loc );
702 iterator erase( iterator start, iterator end );
704 UString& erase( size_type index = 0, size_type num = npos );
711 UString& replace( size_type index1, size_type num1,
const UString& str );
714 UString& replace( size_type index1, size_type num1,
const UString& str, size_type num2 );
716 UString& replace( size_type index1, size_type num1,
const UString& str, size_type index2, size_type num2 );
718 UString& replace( iterator start, iterator end,
const UString& str, size_type num = npos );
720 UString& replace( size_type index, size_type num1, size_type num2, code_point ch );
722 UString& replace( iterator start, iterator end, size_type num, code_point ch );
729 int compare(
const UString& str )
const;
732 int compare(
const code_point* str )
const;
734 int compare( size_type index, size_type length,
const UString& str )
const;
736 int compare( size_type index, size_type length,
const UString& str, size_type index2, size_type length2 )
const;
738 int compare( size_type index, size_type length,
const code_point* str, size_type length2 )
const;
739 #if MYGUI_IS_NATIVE_WCHAR_T
740 int compare( size_type index, size_type length,
const wchar_t* w_str, size_type length2 )
const;
743 int compare( size_type index, size_type length,
const char* c_str, size_type length2 )
const;
753 size_type find(
const UString& str, size_type index = 0 )
const;
756 size_type find(
const code_point* cp_str, size_type index, size_type length )
const;
759 size_type find(
const char* c_str, size_type index, size_type length )
const;
760 #if MYGUI_IS_NATIVE_WCHAR_T
763 size_type find(
const wchar_t* w_str, size_type index, size_type length )
const;
767 size_type find(
char ch, size_type index = 0 )
const;
770 size_type find( code_point ch, size_type index = 0 )
const;
771 #if MYGUI_IS_NATIVE_WCHAR_T
774 size_type find(
wchar_t ch, size_type index = 0 )
const;
778 size_type find( unicode_char ch, size_type index = 0 )
const;
781 size_type rfind(
const UString& str, size_type index = 0 )
const;
783 size_type rfind(
const code_point* cp_str, size_type index, size_type num )
const;
785 size_type rfind(
const char* c_str, size_type index, size_type num )
const;
786 #if MYGUI_IS_NATIVE_WCHAR_T
787 size_type rfind(
const wchar_t* w_str, size_type index, size_type num )
const;
790 size_type rfind(
char ch, size_type index = 0 )
const;
793 size_type rfind( code_point ch, size_type index )
const;
794 #if MYGUI_IS_NATIVE_WCHAR_T
795 size_type rfind(
wchar_t ch, size_type index = 0 )
const;
798 size_type rfind( unicode_char ch, size_type index = 0 )
const;
806 size_type find_first_of(
const UString &str, size_type index = 0, size_type num = npos )
const;
809 size_type find_first_of( code_point ch, size_type index = 0 )
const;
811 size_type find_first_of(
char ch, size_type index = 0 )
const;
812 #if MYGUI_IS_NATIVE_WCHAR_T
813 size_type find_first_of(
wchar_t ch, size_type index = 0 )
const;
816 size_type find_first_of( unicode_char ch, size_type index = 0 )
const;
820 size_type find_first_not_of(
const UString& str, size_type index = 0, size_type num = npos )
const;
822 size_type find_first_not_of( code_point ch, size_type index = 0 )
const;
824 size_type find_first_not_of(
char ch, size_type index = 0 )
const;
825 #if MYGUI_IS_NATIVE_WCHAR_T
826 size_type find_first_not_of(
wchar_t ch, size_type index = 0 )
const;
829 size_type find_first_not_of( unicode_char ch, size_type index = 0 )
const;
833 size_type find_last_of(
const UString& str, size_type index = npos, size_type num = npos )
const;
835 size_type find_last_of( code_point ch, size_type index = npos )
const;
838 return find_last_of( static_cast<code_point>( ch ), index );
840 #if MYGUI_IS_NATIVE_WCHAR_T
841 size_type find_last_of(
wchar_t ch, size_type index = npos )
const;
844 size_type find_last_of( unicode_char ch, size_type index = npos )
const;
848 size_type find_last_not_of(
const UString& str, size_type index = npos, size_type num = npos )
const;
850 size_type find_last_not_of( code_point ch, size_type index = npos )
const;
852 size_type find_last_not_of(
char ch, size_type index = npos )
const;
853 #if MYGUI_IS_NATIVE_WCHAR_T
854 size_type find_last_not_of(
wchar_t ch, size_type index = npos )
const;
857 size_type find_last_not_of( unicode_char ch, size_type index = npos )
const;
880 UString& operator=( code_point ch );
883 #if MYGUI_IS_NATIVE_WCHAR_T
884 UString& operator=(
wchar_t ch );
887 UString& operator=( unicode_char ch );
890 code_point& operator[]( size_type index );
892 const code_point& operator[]( size_type index )
const;
899 operator std::string()
const;
902 operator std::wstring()
const;
909 static bool _utf16_independent_char( code_point cp );
912 static bool _utf16_surrogate_lead( code_point cp );
914 static bool _utf16_surrogate_follow( code_point cp );
916 static size_t _utf16_char_length( code_point cp );
918 static size_t _utf16_char_length( unicode_char uc );
924 static size_t _utf16_to_utf32(
const code_point in_cp[2], unicode_char& out_uc );
931 static size_t _utf32_to_utf16(
const unicode_char& in_uc, code_point out_cp[2] );
938 static bool _utf8_start_char(
unsigned char cp );
941 static size_t _utf8_char_length(
unsigned char cp );
943 static size_t _utf8_char_length( unicode_char uc );
946 static size_t _utf8_to_utf32(
const unsigned char in_cp[6], unicode_char& out_uc );
948 static size_t _utf32_to_utf8(
const unicode_char& in_uc,
unsigned char out_cp[6] );
951 static size_type _verifyUTF8(
const unsigned char* c_str );
953 static size_type _verifyUTF8(
const std::string& str );
974 void _cleanBuffer()
const;
977 void _getBufferStr()
const;
979 void _getBufferWStr()
const;
981 void _getBufferUTF32Str()
const;
983 void _load_buffer_UTF8()
const;
984 void _load_buffer_WStr()
const;
985 void _load_buffer_UTF32()
const;
987 mutable BufferType m_bufferType;
988 mutable size_t m_bufferSize;
1016 #if MYGUI_IS_NATIVE_WCHAR_T
1034 #if MYGUI_IS_NATIVE_WCHAR_T
1089 inline std::ostream& operator << ( std::ostream& os,
const UString& s ) {
1094 inline std::wostream& operator << ( std::wostream& os,
const UString& s ) {
1100 #if MYGUI_COMPILER == MYGUI_COMPILER_MSVC
1101 # pragma warning (pop)
1104 #endif // __MYGUI_U_STRING_H__
std::basic_string< code_point > dstring
base iterator class for UString
UString & append(const UString &str)
appends str on to the end of the current string
UString operator+(const UString &s1, const UString &s2)
string addition operator
bool operator!=(const UString::_const_fwd_iterator &left, const UString::_const_fwd_iterator &right)
invalid_data(const std::string &_Message)
constructor takes a string message that can be later retrieved by the what() function ...
const std::wstring & asWStr() const
returns the current string in the native form of std::wstring
size_t size_type
size type used to indicate string size and character positions within the string
size_type find_last_of(char ch, size_type index=npos) const
returns the index of the first occurrence of ch in the current string, doing a reverse search from in...
This exception is used when invalid data streams are encountered.
bool operator==(const UString::_const_fwd_iterator &left, const UString::_const_fwd_iterator &right)
UString operator+(const UString &s1, UString::unicode_char c)
string addition operator
_const_fwd_iterator const_iterator
const iterator
UString operator+(UString::unicode_char c, const UString &s2)
string addition operator
bool operator<=(const UString::_const_fwd_iterator &left, const UString::_const_fwd_iterator &right)
const forward iterator for UString
UString operator+(const UString &s1, UString::code_point c)
string addition operator
_const_rev_iterator const_reverse_iterator
const reverse iterator
std::basic_string< unicode_char > utf32string
string type used for returning UTF-32 formatted data
const reverse iterator for UString
_fwd_iterator iterator
iterator
utf32string * mUTF32StrBuffer
uint16 code_point
a single UTF-16 code point
UString operator+(char c, const UString &s2)
string addition operator
bool operator>(const UString::_const_fwd_iterator &left, const UString::_const_fwd_iterator &right)
UString operator+(const UString &s1, char c)
string addition operator
_rev_iterator reverse_iterator
reverse iterator
std::wstring * mWStrBuffer
UString & insert(size_type index, const code_point *str)
inserts str into the current string, at location index
code_point value_type
value type typedef for use in iterators
forward iterator for UString
uint32 unicode_char
a single 32-bit Unicode character
bool operator>=(const UString::_const_fwd_iterator &left, const UString::_const_fwd_iterator &right)
forward iterator for UString
const std::string & asUTF8() const
returns the current string in UTF-8 form within a std::string
UString::size_type operator-(const UString::_const_fwd_iterator &left, const UString::_const_fwd_iterator &right)
A UTF-16 string with implicit conversion to/from std::string and std::wstring.
iterator insert(iterator i, const code_point &ch)
inserts ch before the code point denoted by i
bool operator<(const UString::_const_fwd_iterator &left, const UString::_const_fwd_iterator &right)
float len(float x, float y)