X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/111d99489d509bc96819877b88596474f3253859..438959cca892a7651634cc3a7aad4819ac49b23c:/include/wx/stringops.h?ds=inline diff --git a/include/wx/stringops.h b/include/wx/stringops.h index ed53532d9e..c7eb83bc4b 100644 --- a/include/wx/stringops.h +++ b/include/wx/stringops.h @@ -28,27 +28,21 @@ struct WXDLLIMPEXP_BASE wxStringOperationsWchar { // moves the iterator to the next Unicode character - static void IncIter(wxStringImpl::iterator& i) { ++i; } - static void IncIter(wxStringImpl::const_iterator& i) { ++i; } + template + static void IncIter(Iterator& i) { ++i; } // moves the iterator to the previous Unicode character - static void DecIter(wxStringImpl::iterator& i) { --i; } - static void DecIter(wxStringImpl::const_iterator& i) { --i; } + template + static void DecIter(Iterator& i) { --i; } // moves the iterator by n Unicode characters - static wxStringImpl::iterator AddToIter(const wxStringImpl::iterator& i, int n) - { return i + n; } - static wxStringImpl::const_iterator AddToIter(const wxStringImpl::const_iterator& i, int n) - { return i + n; } - static const wxChar* AddToIter(const wxChar *i, int n) + template + static Iterator AddToIter(const Iterator& i, ptrdiff_t n) { return i + n; } // returns distance of the two iterators in Unicode characters - static int DiffIters(const wxStringImpl::iterator& i1, - const wxStringImpl::iterator& i2) - { return i1 - i2; } - static int DiffIters(const wxStringImpl::const_iterator& i1, - const wxStringImpl::const_iterator& i2) + template + static ptrdiff_t DiffIters(const Iterator& i1, const Iterator& i2) { return i1 - i2; } // encodes the character to a form used to represent it in internal @@ -67,12 +61,13 @@ struct WXDLLIMPEXP_BASE wxStringOperationsUtf8 // checks correctness of UTF-8 sequence static bool IsValidUtf8String(const char *c, size_t len = wxStringImpl::npos); -#ifdef __WXDEBUG__ - static bool IsValidUtf8LeadByte(unsigned char c); -#endif + static bool IsValidUtf8LeadByte(unsigned char c) + { + return (c <= 0x7F) || (c >= 0xC2 && c <= 0xF4); + } // table of offsets to skip forward when iterating over UTF-8 sequence - static unsigned char ms_utf8IterTable[256]; + static const unsigned char ms_utf8IterTable[256]; template @@ -99,18 +94,18 @@ struct WXDLLIMPEXP_BASE wxStringOperationsUtf8 } template - static Iterator AddToIter(const Iterator& i, int n) + static Iterator AddToIter(const Iterator& i, ptrdiff_t n) { Iterator out(i); if ( n > 0 ) { - for ( int j = 0; j < n; ++j ) + for ( ptrdiff_t j = 0; j < n; ++j ) IncIter(out); } else if ( n < 0 ) { - for ( int j = 0; j > n; --j ) + for ( ptrdiff_t j = 0; j > n; --j ) DecIter(out); } @@ -118,9 +113,9 @@ struct WXDLLIMPEXP_BASE wxStringOperationsUtf8 } template - static int DiffIters(Iterator i1, Iterator i2) + static ptrdiff_t DiffIters(Iterator i1, Iterator i2) { - int dist = 0; + ptrdiff_t dist = 0; if ( i1 < i2 ) { @@ -142,15 +137,10 @@ struct WXDLLIMPEXP_BASE wxStringOperationsUtf8 return dist; } - // buffer for single UTF-8 character - struct Utf8CharBuffer - { - char data[5]; - operator const char*() const { return data; } - }; - // encodes the character as UTF-8: - static Utf8CharBuffer EncodeChar(const wxUniChar& ch); + typedef wxUniChar::Utf8CharBuffer Utf8CharBuffer; + static Utf8CharBuffer EncodeChar(const wxUniChar& ch) + { return ch.AsUTF8(); } // returns n copies of ch encoded in UTF-8 string static wxCharBuffer EncodeNChars(size_t n, const wxUniChar& ch); @@ -163,7 +153,15 @@ struct WXDLLIMPEXP_BASE wxStringOperationsUtf8 } // decodes single UTF-8 character from UTF-8 string - static wxUniChar DecodeChar(wxStringImpl::const_iterator i); + static wxUniChar DecodeChar(wxStringImpl::const_iterator i) + { + if ( (unsigned char)*i < 0x80 ) + return (int)*i; + return DecodeNonAsciiChar(i); + } + +private: + static wxUniChar DecodeNonAsciiChar(wxStringImpl::const_iterator i); }; #endif // wxUSE_UNICODE_UTF8