X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/bfb6847df68d5d048749a83d04f1fda0188ab9d2..57e2b887a0e798ab5ab2921cdeba550f322b000c:/include/wx/string.h diff --git a/include/wx/string.h b/include/wx/string.h index bf297f6a9a..ac8c35b8bf 100644 --- a/include/wx/string.h +++ b/include/wx/string.h @@ -51,7 +51,7 @@ #include #endif -#include "wx/wxcrt.h" // for wxChar, wxStrlen() etc. +#include "wx/wxcrtbase.h" // for wxChar, wxStrlen() etc. #include "wx/strvararg.h" #include "wx/buffer.h" // for wxCharBuffer #include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes @@ -78,6 +78,14 @@ class WXDLLIMPEXP_BASE wxString; #define wxMBSTRINGCAST (char *)(const char *) #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *) +// like _T(), but for literals in wxString's internal representation, i.e. +// char* in UTF-8 build and wxChar* otherwise: +#if wxUSE_UNICODE_UTF8 + #define wxSTRING_TEXT(str) str +#else + #define wxSTRING_TEXT(str) _T(str) +#endif + // ---------------------------------------------------------------------------- // constants // ---------------------------------------------------------------------------- @@ -368,6 +376,42 @@ protected: #pragma warning (disable:4275) #endif +#if wxUSE_UNICODE_UTF8 +// see the comment near wxString::iterator for why we need this +struct WXDLLIMPEXP_BASE wxStringIteratorNode +{ + wxStringIteratorNode() + : m_str(NULL), m_citer(NULL), m_iter(NULL), m_prev(NULL), m_next(NULL) {} + wxStringIteratorNode(const wxString *str, + wxStringImpl::const_iterator *citer) + { DoSet(str, citer, NULL); } + wxStringIteratorNode(const wxString *str, wxStringImpl::iterator *iter) + { DoSet(str, NULL, iter); } + ~wxStringIteratorNode() + { clear(); } + + inline void set(const wxString *str, wxStringImpl::const_iterator *citer) + { clear(); DoSet(str, citer, NULL); } + inline void set(const wxString *str, wxStringImpl::iterator *iter) + { clear(); DoSet(str, NULL, iter); } + + const wxString *m_str; + wxStringImpl::const_iterator *m_citer; + wxStringImpl::iterator *m_iter; + wxStringIteratorNode *m_prev, *m_next; + +private: + inline void clear(); + inline void DoSet(const wxString *str, + wxStringImpl::const_iterator *citer, + wxStringImpl::iterator *iter); + + // the node belongs to a particular iterator instance, it's not copied + // when a copy of the iterator is made + DECLARE_NO_COPY_CLASS(wxStringIteratorNode) +}; +#endif // wxUSE_UNICODE_UTF8 + class WXDLLIMPEXP_BASE wxString #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN : public wxStringPrintfMixin @@ -511,7 +555,7 @@ private: if ( pos == 0 || pos == npos ) return pos; else - return const_iterator(m_impl.begin() + pos) - begin(); + return const_iterator(this, m_impl.begin() + pos) - begin(); } #endif // !wxUSE_UNICODE_UTF8/wxUSE_UNICODE_UTF8 @@ -537,8 +581,7 @@ public: #define WX_STR_ITERATOR_TAG void /* dummy type */ #endif - #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, \ - reference_type, reference_ctor) \ + #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, reference_type) \ private: \ typedef wxStringImpl::iterator_name underlying_iterator; \ public: \ @@ -548,7 +591,6 @@ public: typedef reference_type reference; \ typedef pointer_type pointer; \ \ - reference operator*() const { return reference_ctor; } \ reference operator[](size_t n) const { return *(*this + n); } \ \ iterator_name& operator++() \ @@ -616,56 +658,113 @@ public: private: \ underlying_iterator m_cur - class const_iterator; + class WXDLLIMPEXP_BASE const_iterator; #if wxUSE_UNICODE_UTF8 - class iterator - { - // NB: In UTF-8 build, (non-const) iterator needs to keep reference - // to the underlying wxStringImpl, because UTF-8 is variable-length - // encoding and changing the value pointer to by an iterator using - // its operator* requires calling wxStringImpl::replace() if the old - // and new values differ in their encoding's length. + // NB: In UTF-8 build, (non-const) iterator needs to keep reference + // to the underlying wxStringImpl, because UTF-8 is variable-length + // encoding and changing the value pointer to by an iterator (using + // its operator*) requires calling wxStringImpl::replace() if the old + // and new values differ in their encoding's length. + // + // Furthermore, the replace() call may invalid all iterators for the + // string, so we have to keep track of outstanding iterators and update + // them if replace() happens. + // + // This is implemented by maintaining linked list of iterators for every + // string and traversing it in wxUniCharRef::operator=(). Head of the + // list is stored in wxString. (FIXME-UTF8) - WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef, - wxUniCharRef::CreateForString(m_str, m_cur)); + class WXDLLIMPEXP_BASE iterator + { + WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef); public: - iterator(const iterator& i) : m_cur(i.m_cur), m_str(i.m_str) {} + iterator() {} + iterator(const iterator& i) + : m_cur(i.m_cur), m_node(i.str(), &m_cur) {} + iterator& operator=(const iterator& i) + { m_cur = i.m_cur; m_node.set(i.str(), &m_cur); return *this; } + + reference operator*() + { return wxUniCharRef::CreateForString(m_node, m_cur); } iterator operator+(int n) const - { return iterator(m_str, wxStringOperations::AddToIter(m_cur, n)); } + { return iterator(str(), wxStringOperations::AddToIter(m_cur, n)); } iterator operator+(size_t n) const - { return iterator(m_str, wxStringOperations::AddToIter(m_cur, (int)n)); } + { return iterator(str(), wxStringOperations::AddToIter(m_cur, (int)n)); } iterator operator-(int n) const - { return iterator(m_str, wxStringOperations::AddToIter(m_cur, -n)); } + { return iterator(str(), wxStringOperations::AddToIter(m_cur, -n)); } iterator operator-(size_t n) const - { return iterator(m_str, wxStringOperations::AddToIter(m_cur, -(int)n)); } + { return iterator(str(), wxStringOperations::AddToIter(m_cur, -(int)n)); } private: iterator(wxString *str, underlying_iterator ptr) - : m_cur(ptr), m_str(str->m_impl) {} - iterator(wxStringImpl& str, underlying_iterator ptr) - : m_cur(ptr), m_str(str) {} + : m_cur(ptr), m_node(str, &m_cur) {} + + wxString* str() const { return wx_const_cast(wxString*, m_node.m_str); } - wxStringImpl& m_str; + wxStringIteratorNode m_node; friend class const_iterator; }; + class WXDLLIMPEXP_BASE const_iterator + { + // NB: reference_type is intentionally value, not reference, the character + // may be encoded differently in wxString data: + WX_STR_ITERATOR_IMPL(const_iterator, const wxChar*, wxUniChar); + + public: + const_iterator() {} + const_iterator(const const_iterator& i) + : m_cur(i.m_cur), m_node(i.str(), &m_cur) {} + const_iterator(const iterator& i) + : m_cur(i.m_cur), m_node(i.str(), &m_cur) {} + + const_iterator& operator=(const const_iterator& i) + { m_cur = i.m_cur; m_node.set(i.str(), &m_cur); return *this; } + const_iterator& operator=(const iterator& i) + { m_cur = i.m_cur; m_node.set(i.str(), &m_cur); return *this; } + + reference operator*() const + { return wxStringOperations::DecodeChar(m_cur); } + + const_iterator operator+(int n) const + { return const_iterator(str(), wxStringOperations::AddToIter(m_cur, n)); } + const_iterator operator+(size_t n) const + { return const_iterator(str(), wxStringOperations::AddToIter(m_cur, (int)n)); } + const_iterator operator-(int n) const + { return const_iterator(str(), wxStringOperations::AddToIter(m_cur, -n)); } + const_iterator operator-(size_t n) const + { return const_iterator(str(), wxStringOperations::AddToIter(m_cur, -(int)n)); } + + private: + // for internal wxString use only: + const_iterator(const wxString *str, underlying_iterator ptr) + : m_cur(ptr), m_node(str, &m_cur) {} + + const wxString* str() const { return m_node.m_str; } + + wxStringIteratorNode m_node; + }; + size_t IterToImplPos(wxString::iterator i) const { return wxStringImpl::const_iterator(i.impl()) - m_impl.begin(); } #else // !wxUSE_UNICODE_UTF8 - class iterator + class WXDLLIMPEXP_BASE iterator { - WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef, - wxUniCharRef::CreateForString(m_cur)); + WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef); public: + iterator() {} iterator(const iterator& i) : m_cur(i.m_cur) {} + reference operator*() + { return wxUniCharRef::CreateForString(m_cur); } + iterator operator+(int n) const { return iterator(wxStringOperations::AddToIter(m_cur, n)); } iterator operator+(size_t n) const @@ -682,19 +781,21 @@ public: friend class const_iterator; }; -#endif // wxUSE_UNICODE_UTF8/!wxUSE_UNICODE_UTF8 - class const_iterator + class WXDLLIMPEXP_BASE const_iterator { // NB: reference_type is intentionally value, not reference, the character // may be encoded differently in wxString data: - WX_STR_ITERATOR_IMPL(const_iterator, const wxChar*, wxUniChar, - wxStringOperations::DecodeChar(m_cur)); + WX_STR_ITERATOR_IMPL(const_iterator, const wxChar*, wxUniChar); public: + const_iterator() {} const_iterator(const const_iterator& i) : m_cur(i.m_cur) {} const_iterator(const iterator& i) : m_cur(i.m_cur) {} + reference operator*() const + { return wxStringOperations::DecodeChar(m_cur); } + const_iterator operator+(int n) const { return const_iterator(wxStringOperations::AddToIter(m_cur, n)); } const_iterator operator+(size_t n) const @@ -707,7 +808,10 @@ public: private: // for internal wxString use only: const_iterator(underlying_iterator ptr) : m_cur(ptr) {} + const_iterator(const wxString *WXUNUSED(str), underlying_iterator ptr) + : m_cur(ptr) {} }; +#endif // wxUSE_UNICODE_UTF8/!wxUSE_UNICODE_UTF8 #undef WX_STR_ITERATOR_TAG #undef WX_STR_ITERATOR_IMPL @@ -727,6 +831,7 @@ public: typedef typename T::reference reference; typedef typename T::pointer *pointer; + reverse_iterator_impl() {} reverse_iterator_impl(iterator_type i) : m_cur(i) {} reverse_iterator_impl(const reverse_iterator_impl& ri) : m_cur(ri.m_cur) {} @@ -792,7 +897,8 @@ private: // wxCStrData) to an iterator into the string static const_iterator CreateConstIterator(const wxCStrData& data) { - return const_iterator(data.m_str->begin() + data.m_offset); + return const_iterator(data.m_str, + (data.m_str->begin() + data.m_offset).impl()); } // in UTF-8 STL build, creation from std::string requires conversion under @@ -917,7 +1023,12 @@ public: wxString(const std::string& str) { assign(str.c_str(), str.length()); } #endif +#endif // wxUSE_STD_STRING + // Unlike ctor from std::string, we provide conversion to std::string only + // if wxUSE_STL and not merely wxUSE_STD_STRING (which is on by default), + // because it conflicts with operator const char/wchar_t*: +#if wxUSE_STL #if wxUSE_UNICODE_WCHAR && wxUSE_STL_BASED_WXSTRING // wxStringImpl is std::string in the encoding we want operator const wxStdWideString&() const { return m_impl; } @@ -940,10 +1051,10 @@ public: #endif // wxUSE_STL // first valid index position - const_iterator begin() const { return const_iterator(m_impl.begin()); } + const_iterator begin() const { return const_iterator(this, m_impl.begin()); } iterator begin() { return iterator(this, m_impl.begin()); } // position one after the last valid one - const_iterator end() const { return const_iterator(m_impl.end()); } + const_iterator end() const { return const_iterator(this, m_impl.end()); } iterator end() { return iterator(this, m_impl.end()); } // first element of the reversed string @@ -1090,8 +1201,13 @@ public: // implicit conversion to C string operator wxCStrData() const { return c_str(); } + + // these operators conflict with operators for conversion to std::string, + // so they must be disabled in STL build: +#if !wxUSE_STL operator const char*() const { return c_str(); } operator const wchar_t*() const { return c_str(); } +#endif // implicit conversion to untyped pointer for compatibility with previous // wxWidgets versions: this is the same as conversion to const char * so it @@ -1119,11 +1235,14 @@ public: // the behaviour of these functions with the strings containing anything // else than 7 bit ASCII characters is undefined, use at your own risk. #if wxUSE_UNICODE + static wxString FromAscii(const char *ascii, size_t len); // string static wxString FromAscii(const char *ascii); // string static wxString FromAscii(const char ascii); // char const wxCharBuffer ToAscii() const; #else // ANSI static wxString FromAscii(const char *ascii) { return wxString( ascii ); } + static wxString FromAscii(const char *ascii, size_t len) + { return wxString( ascii, len ); } static wxString FromAscii(const char ascii) { return wxString( ascii ); } const char *ToAscii() const { return c_str(); } #endif // Unicode/!Unicode @@ -1132,11 +1251,19 @@ public: #if wxUSE_UNICODE_UTF8 static wxString FromUTF8(const char *utf8) { + if ( !utf8 ) + return wxEmptyString; + wxASSERT( wxStringOperations::IsValidUtf8String(utf8) ); return FromImpl(wxStringImpl(utf8)); } static wxString FromUTF8(const char *utf8, size_t len) { + if ( !utf8 ) + return wxEmptyString; + if ( len == npos ) + return FromUTF8(utf8); + wxASSERT( wxStringOperations::IsValidUtf8String(utf8, len) ); return FromImpl(wxStringImpl(utf8, len)); } @@ -1435,11 +1562,7 @@ public: bool IsSameAs(const wxWCharBuffer& str, bool compareWithCase = true) const { return IsSameAs(str.data(), compareWithCase); } // comparison with a single character: returns true if equal - bool IsSameAs(wxUniChar c, bool compareWithCase = true) const - { - return (length() == 1) && (compareWithCase ? GetChar(0u) == c - : wxToupper(GetChar(0u)) == wxToupper(c)); - } + bool IsSameAs(wxUniChar c, bool compareWithCase = true) const; // FIXME-UTF8: remove these overloads bool IsSameAs(wxUniCharRef c, bool compareWithCase = true) const { return IsSameAs(wxUniChar(c), compareWithCase); } @@ -2449,6 +2572,12 @@ private: private: wxStringImpl m_impl; +#ifdef __VISUALC__ + // "struct 'ConvertedBuffer' needs to have dll-interface to be used by + // clients of class 'wxString'" - this is private, we don't care + #pragma warning (disable:4251) +#endif + // buffers for compatibility conversion from (char*)c_str() and // (wchar_t*)c_str(): // FIXME-UTF8: bechmark various approaches to keeping compatibility buffers @@ -2477,6 +2606,31 @@ private: ConvertedBuffer m_convertedToWChar; #endif +#ifdef __VISUALC__ + #pragma warning (default:4251) +#endif + +#if wxUSE_UNICODE_UTF8 + // FIXME-UTF8: (try to) move this elsewhere (TLS) or solve differently + // assigning to character pointer to by wxString::interator may + // change the underlying wxStringImpl iterator, so we have to + // keep track of all iterators and update them as necessary: + struct wxStringIteratorNodeHead + { + wxStringIteratorNodeHead() : ptr(NULL) {} + wxStringIteratorNode *ptr; + + // copying is disallowed as it would result in more than one pointer into + // the same linked list + DECLARE_NO_COPY_CLASS(wxStringIteratorNodeHead) + }; + + wxStringIteratorNodeHead m_iterators; + + friend class WXDLLIMPEXP_BASE wxStringIteratorNode; + friend class WXDLLIMPEXP_BASE wxUniCharRef; +#endif // wxUSE_UNICODE_UTF8 + friend class WXDLLIMPEXP_BASE wxCStrData; friend class wxImplStringBuffer; friend class wxImplStringBufferLength; @@ -2738,7 +2892,15 @@ inline bool operator<=(const wxString& s1, const wxString& s2) inline bool operator>=(const wxString& s1, const wxString& s2) { return s1.Cmp(s2) >= 0; } -#if wxUSE_UNICODE +inline bool operator==(const wxString& s1, const wxCStrData& s2) + { return s1 == s2.AsString(); } +inline bool operator==(const wxCStrData& s1, const wxString& s2) + { return s1.AsString() == s2; } +inline bool operator!=(const wxString& s1, const wxCStrData& s2) + { return s1 != s2.AsString(); } +inline bool operator!=(const wxCStrData& s1, const wxString& s2) + { return s1.AsString() != s2; } + inline bool operator==(const wxString& s1, const wxWCharBuffer& s2) { return (s1.Cmp((const wchar_t *)s2) == 0); } inline bool operator==(const wxWCharBuffer& s1, const wxString& s2) @@ -2747,7 +2909,7 @@ inline bool operator!=(const wxString& s1, const wxWCharBuffer& s2) { return (s1.Cmp((const wchar_t *)s2) != 0); } inline bool operator!=(const wxWCharBuffer& s1, const wxString& s2) { return (s2.Cmp((const wchar_t *)s1) != 0); } -#else // !wxUSE_UNICODE + inline bool operator==(const wxString& s1, const wxCharBuffer& s2) { return (s1.Cmp((const char *)s2) == 0); } inline bool operator==(const wxCharBuffer& s1, const wxString& s2) @@ -2756,19 +2918,16 @@ inline bool operator!=(const wxString& s1, const wxCharBuffer& s2) { return (s1.Cmp((const char *)s2) != 0); } inline bool operator!=(const wxCharBuffer& s1, const wxString& s2) { return (s2.Cmp((const char *)s1) != 0); } -#endif // wxUSE_UNICODE/!wxUSE_UNICODE -#if wxUSE_UNICODE inline wxString operator+(const wxString& string, const wxWCharBuffer& buf) { return string + (const wchar_t *)buf; } inline wxString operator+(const wxWCharBuffer& buf, const wxString& string) { return (const wchar_t *)buf + string; } -#else // !wxUSE_UNICODE + inline wxString operator+(const wxString& string, const wxCharBuffer& buf) { return string + (const char *)buf; } inline wxString operator+(const wxCharBuffer& buf, const wxString& string) { return (const char *)buf + string; } -#endif // wxUSE_UNICODE/!wxUSE_UNICODE // comparison with char inline bool operator==(const wxUniChar& c, const wxString& s) { return s.IsSameAs(c); } @@ -2851,7 +3010,7 @@ inline wxCStrData::wxCStrData(const wxCStrData& data) inline wxCStrData::~wxCStrData() { if ( m_owned ) - delete m_str; + delete wx_const_cast(wxString*, m_str); // cast to silence warnings } // simple cases for AsChar() and AsWChar(), the complicated ones are @@ -2958,12 +3117,54 @@ inline wxWCharBuffer::wxWCharBuffer(const wxCStrData& cstr) { } +#if wxUSE_UNICODE_UTF8 +// ---------------------------------------------------------------------------- +// implementation of wxStringIteratorNode inline methods +// ---------------------------------------------------------------------------- + +void wxStringIteratorNode::DoSet(const wxString *str, + wxStringImpl::const_iterator *citer, + wxStringImpl::iterator *iter) +{ + m_prev = NULL; + m_iter = iter; + m_citer = citer; + m_str = str; + if ( str ) + { + m_next = str->m_iterators.ptr; + wx_const_cast(wxString*, m_str)->m_iterators.ptr = this; + if ( m_next ) + m_next->m_prev = this; + } + else + { + m_next = NULL; + } +} + +void wxStringIteratorNode::clear() +{ + if ( m_next ) + m_next->m_prev = m_prev; + if ( m_prev ) + m_prev->m_next = m_next; + else if ( m_str ) // first in the list + wx_const_cast(wxString*, m_str)->m_iterators.ptr = m_next; + + m_next = m_prev = NULL; + m_citer = NULL; + m_iter = NULL; + m_str = NULL; +} +#endif // wxUSE_UNICODE_UTF8 + #if WXWIN_COMPATIBILITY_2_8 - // lot of code out there doesn't explicitly include wx/wxchar.h, but uses + // lot of code out there doesn't explicitly include wx/crt.h, but uses // CRT wrappers that are now declared in wx/wxcrt.h and wx/wxcrtvararg.h, // so let's include this header now that wxString is defined and it's safe // to do it: - #include "wx/wxchar.h" + #include "wx/crt.h" #endif #endif // _WX_WXSTRING_H_