+ 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)
+ {
+ if (&i != this)
+ {
+ 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+(ptrdiff_t n) const
+ { return const_iterator(str(), wxStringOperations::AddToIter(m_cur, n)); }
+ const_iterator operator-(ptrdiff_t n) const
+ { return const_iterator(str(), wxStringOperations::AddToIter(m_cur, -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 WXDLLIMPEXP_BASE iterator
+ {
+ 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+(ptrdiff_t n) const
+ { return iterator(wxStringOperations::AddToIter(m_cur, n)); }
+ iterator operator-(ptrdiff_t n) const
+ { return iterator(wxStringOperations::AddToIter(m_cur, -n)); }
+
+ private:
+ // for internal wxString use only:
+ iterator(underlying_iterator ptr) : m_cur(ptr) {}
+ iterator(wxString *WXUNUSED(str), underlying_iterator ptr) : m_cur(ptr) {}
+
+ 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) {}
+ const_iterator(const iterator& i) : m_cur(i.m_cur) {}
+
+ reference operator*() const
+ { return wxStringOperations::DecodeChar(m_cur); }
+
+ const_iterator operator+(ptrdiff_t n) const
+ { return const_iterator(wxStringOperations::AddToIter(m_cur, n)); }
+ const_iterator operator-(ptrdiff_t n) const
+ { return const_iterator(wxStringOperations::AddToIter(m_cur, -n)); }
+
+ 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
+
+ friend class iterator;
+ friend class const_iterator;
+
+ template <typename T>
+ class reverse_iterator_impl
+ {
+ public:
+ typedef T iterator_type;
+
+ typedef typename T::iterator_category iterator_category;
+ typedef typename T::value_type value_type;
+ typedef typename T::difference_type difference_type;
+ 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) {}
+
+ iterator_type base() const { return m_cur; }
+
+ reference operator*() const { return *(m_cur-1); }
+ reference operator[](size_t n) const { return *(*this + n); }
+
+ reverse_iterator_impl& operator++()
+ { --m_cur; return *this; }
+ reverse_iterator_impl operator++(int)
+ { reverse_iterator_impl tmp = *this; --m_cur; return tmp; }
+ reverse_iterator_impl& operator--()
+ { ++m_cur; return *this; }
+ reverse_iterator_impl operator--(int)
+ { reverse_iterator_impl tmp = *this; ++m_cur; return tmp; }
+
+ // NB: explicit <T> in the functions below is to keep BCC 5.5 happy
+ reverse_iterator_impl operator+(ptrdiff_t n) const
+ { return reverse_iterator_impl<T>(m_cur - n); }
+ reverse_iterator_impl operator-(ptrdiff_t n) const
+ { return reverse_iterator_impl<T>(m_cur + n); }
+ reverse_iterator_impl operator+=(ptrdiff_t n)
+ { m_cur -= n; return *this; }
+ reverse_iterator_impl operator-=(ptrdiff_t n)
+ { m_cur += n; return *this; }
+
+ unsigned operator-(const reverse_iterator_impl& i) const
+ { return i.m_cur - m_cur; }
+
+ bool operator==(const reverse_iterator_impl& ri) const
+ { return m_cur == ri.m_cur; }
+ bool operator!=(const reverse_iterator_impl& ri) const
+ { return !(*this == ri); }
+
+ bool operator<(const reverse_iterator_impl& i) const
+ { return m_cur > i.m_cur; }
+ bool operator>(const reverse_iterator_impl& i) const
+ { return m_cur < i.m_cur; }
+ bool operator<=(const reverse_iterator_impl& i) const
+ { return m_cur >= i.m_cur; }
+ bool operator>=(const reverse_iterator_impl& i) const
+ { return m_cur <= i.m_cur; }
+
+ private:
+ iterator_type m_cur;
+ };
+
+ typedef reverse_iterator_impl<iterator> reverse_iterator;
+ typedef reverse_iterator_impl<const_iterator> const_reverse_iterator;
+
+private:
+ // used to transform an expression built using c_str() (and hence of type
+ // wxCStrData) to an iterator into the string
+ static const_iterator CreateConstIterator(const wxCStrData& data)
+ {
+ 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
+ // non-UTF8 locales, so we can't have and use wxString(wxStringImpl) ctor;
+ // instead we define dummy type that lets us have wxString ctor for creation
+ // from wxStringImpl that couldn't be used by user code (in all other builds,
+ // "standard" ctors can be used):
+#if wxUSE_UNICODE_UTF8 && wxUSE_STL_BASED_WXSTRING
+ struct CtorFromStringImplTag {};
+
+ wxString(CtorFromStringImplTag* WXUNUSED(dummy), const wxStringImpl& src)
+ : m_impl(src) {}
+
+ static wxString FromImpl(const wxStringImpl& src)
+ { return wxString((CtorFromStringImplTag*)NULL, src); }
+#else
+ #if !wxUSE_STL_BASED_WXSTRING
+ wxString(const wxStringImpl& src) : m_impl(src) { }
+ // else: already defined as wxString(wxStdString) below
+ #endif
+ static wxString FromImpl(const wxStringImpl& src) { return wxString(src); }
+#endif
+
+public:
+ // constructors and destructor
+ // ctor for an empty string
+ wxString() {}
+
+ // copy ctor
+ wxString(const wxString& stringSrc) : m_impl(stringSrc.m_impl) { }
+
+ // string containing nRepeat copies of ch
+ wxString(wxUniChar ch, size_t nRepeat = 1 )
+ { assign(nRepeat, ch); }
+ wxString(size_t nRepeat, wxUniChar ch)
+ { assign(nRepeat, ch); }
+ wxString(wxUniCharRef ch, size_t nRepeat = 1)
+ { assign(nRepeat, ch); }
+ wxString(size_t nRepeat, wxUniCharRef ch)
+ { assign(nRepeat, ch); }
+ wxString(char ch, size_t nRepeat = 1)
+ { assign(nRepeat, ch); }
+ wxString(size_t nRepeat, char ch)
+ { assign(nRepeat, ch); }
+ wxString(wchar_t ch, size_t nRepeat = 1)
+ { assign(nRepeat, ch); }
+ wxString(size_t nRepeat, wchar_t ch)
+ { assign(nRepeat, ch); }
+
+ // ctors from char* strings:
+ wxString(const char *psz)
+ : m_impl(ImplStr(psz)) {}
+ wxString(const char *psz, const wxMBConv& conv)
+ : m_impl(ImplStr(psz, conv)) {}
+ wxString(const char *psz, size_t nLength)
+ { assign(psz, nLength); }
+ wxString(const char *psz, const wxMBConv& conv, size_t nLength)
+ {
+ SubstrBufFromMB str(ImplStr(psz, nLength, conv));
+ m_impl.assign(str.data, str.len);
+ }
+
+ // and unsigned char*:
+ wxString(const unsigned char *psz)
+ : m_impl(ImplStr((const char*)psz)) {}
+ wxString(const unsigned char *psz, const wxMBConv& conv)
+ : m_impl(ImplStr((const char*)psz, conv)) {}
+ wxString(const unsigned char *psz, size_t nLength)
+ { assign((const char*)psz, nLength); }
+ wxString(const unsigned char *psz, const wxMBConv& conv, size_t nLength)
+ {
+ SubstrBufFromMB str(ImplStr((const char*)psz, nLength, conv));
+ m_impl.assign(str.data, str.len);
+ }
+
+ // ctors from wchar_t* strings:
+ wxString(const wchar_t *pwz)
+ : m_impl(ImplStr(pwz)) {}
+ wxString(const wchar_t *pwz, const wxMBConv& WXUNUSED(conv))
+ : m_impl(ImplStr(pwz)) {}
+ wxString(const wchar_t *pwz, size_t nLength)
+ { assign(pwz, nLength); }
+ wxString(const wchar_t *pwz, const wxMBConv& WXUNUSED(conv), size_t nLength)
+ { assign(pwz, nLength); }
+
+ wxString(const wxCharBuffer& buf)
+ { assign(buf.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
+ wxString(const wxWCharBuffer& buf)
+ { assign(buf.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
+
+ // NB: this version uses m_impl.c_str() to force making a copy of the
+ // string, so that "wxString(str.c_str())" idiom for passing strings
+ // between threads works
+ wxString(const wxCStrData& cstr)
+ : m_impl(cstr.AsString().m_impl.c_str()) { }
+
+ // as we provide both ctors with this signature for both char and unsigned
+ // char string, we need to provide one for wxCStrData to resolve ambiguity
+ wxString(const wxCStrData& cstr, size_t nLength)
+ : m_impl(cstr.AsString().Mid(0, nLength).m_impl) {}
+
+ // and because wxString is convertible to wxCStrData and const wxChar *
+ // we also need to provide this one
+ wxString(const wxString& str, size_t nLength)
+ { assign(str, nLength); }
+
+ // even if we're not built with wxUSE_STL == 1 it is very convenient to allow
+ // implicit conversions from std::string to wxString and vice verse as this
+ // allows to use the same strings in non-GUI and GUI code, however we don't
+ // want to unconditionally add this ctor as it would make wx lib dependent on
+ // libstdc++ on some Linux versions which is bad, so instead we ask the
+ // client code to define this wxUSE_STD_STRING symbol if they need it
+#if wxUSE_STD_STRING
+ #if wxUSE_UNICODE_WCHAR
+ wxString(const wxStdWideString& str) : m_impl(str) {}
+ #else // UTF-8 or ANSI
+ wxString(const wxStdWideString& str)
+ { assign(str.c_str(), str.length()); }
+ #endif
+
+ #if !wxUSE_UNICODE // ANSI build
+ // FIXME-UTF8: do this in UTF8 build #if wxUSE_UTF8_LOCALE_ONLY, too
+ wxString(const std::string& str) : m_impl(str) {}
+ #else // Unicode
+ 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; }
+ #else
+ // wxStringImpl is either not std::string or needs conversion
+ operator wxStdWideString() const
+ // FIXME-UTF8: broken for embedded NULs
+ { return wxStdWideString(wc_str()); }
+ #endif
+
+ #if (!wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY) && wxUSE_STL_BASED_WXSTRING
+ // wxStringImpl is std::string in the encoding we want
+ operator const std::string&() const { return m_impl; }
+ #else
+ // wxStringImpl is either not std::string or needs conversion
+ operator std::string() const
+ // FIXME-UTF8: broken for embedded NULs
+ { return std::string(mb_str()); }
+ #endif
+#endif // wxUSE_STL
+
+ wxString Clone() const
+ {
+ // make a deep copy of the string, i.e. the returned string will have
+ // ref count = 1 with refcounted implementation
+ return wxString::FromImpl(wxStringImpl(m_impl.c_str(), m_impl.length()));
+ }
+
+ // first valid index position
+ 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(this, m_impl.end()); }
+ iterator end() { return iterator(this, m_impl.end()); }
+
+ // first element of the reversed string
+ const_reverse_iterator rbegin() const
+ { return const_reverse_iterator(end()); }
+ reverse_iterator rbegin()
+ { return reverse_iterator(end()); }
+ // one beyond the end of the reversed string
+ const_reverse_iterator rend() const
+ { return const_reverse_iterator(begin()); }
+ reverse_iterator rend()
+ { return reverse_iterator(begin()); }
+
+ // std::string methods:
+#if wxUSE_UNICODE_UTF8
+ size_t length() const { return end() - begin(); } // FIXME-UTF8: optimize!
+#else
+ size_t length() const { return m_impl.length(); }
+#endif
+
+ size_type size() const { return length(); }
+ size_type max_size() const { return npos; }
+
+ bool empty() const { return m_impl.empty(); }
+
+ size_type capacity() const { return m_impl.capacity(); } // FIXME-UTF8
+ void reserve(size_t sz) { m_impl.reserve(sz); } // FIXME-UTF8
+
+ void resize(size_t nSize, wxUniChar ch = wxT('\0'))
+ {
+ const size_t len = length();
+ if ( nSize == len)
+ return;
+
+#if wxUSE_UNICODE_UTF8
+ if ( nSize < len )
+ {
+ // we can't use wxStringImpl::resize() for truncating the string as it
+ // counts in bytes, not characters
+ erase(nSize);
+ return;
+ }
+
+ // we also can't use (presumably more efficient) resize() if we have to
+ // append characters taking more than one byte
+ if ( !ch.IsAscii() )
+ append(nSize - len, ch);
+ else
+#endif // wxUSE_UNICODE_UTF8
+ m_impl.resize(nSize, (wxStringCharType)ch);
+ }
+
+ wxString substr(size_t nStart = 0, size_t nLen = npos) const
+ {
+ size_t pos, len;
+ PosLenToImpl(nStart, nLen, &pos, &len);
+ return FromImpl(m_impl.substr(pos, len));
+ }
+
+ // generic attributes & operations
+ // as standard strlen()
+ size_t Len() const { return length(); }
+ // string contains any characters?
+ bool IsEmpty() const { return empty(); }
+ // empty string is "false", so !str will return true
+ bool operator!() const { return empty(); }
+ // truncate the string to given length
+ wxString& Truncate(size_t uiLen);
+ // empty string contents
+ void Empty()
+ {
+ Truncate(0);
+
+ wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") );
+ }
+ // empty the string and free memory
+ void Clear()
+ {
+ wxString tmp(wxEmptyString);
+ swap(tmp);
+ }
+
+ // contents test
+ // Is an ascii value
+ bool IsAscii() const;
+ // Is a number
+ bool IsNumber() const;
+ // Is a word
+ bool IsWord() const;
+
+ // data access (all indexes are 0 based)
+ // read access
+ wxUniChar at(size_t n) const
+ { return *(begin() + n); } // FIXME-UTF8: optimize?
+ wxUniChar GetChar(size_t n) const
+ { return at(n); }
+ // read/write access
+ wxUniCharRef at(size_t n)
+ { return *(begin() + n); } // FIXME-UTF8: optimize?
+ wxUniCharRef GetWritableChar(size_t n)
+ { return at(n); }
+ // write access
+ void SetChar(size_t n, wxUniChar ch)
+ { at(n) = ch; }
+
+ // get last character
+ wxUniChar Last() const
+ {
+ wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
+ return *rbegin();
+ }
+
+ // get writable last character
+ wxUniCharRef Last()
+ {
+ wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
+ return *rbegin();
+ }
+
+ /*
+ Note that we we must define all of the overloads below to avoid
+ ambiguity when using str[0].
+ */
+ wxUniChar operator[](int n) const
+ { return at(n); }
+ wxUniChar operator[](long n) const
+ { return at(n); }
+ wxUniChar operator[](size_t n) const
+ { return at(n); }
+#ifndef wxSIZE_T_IS_UINT
+ wxUniChar operator[](unsigned int n) const
+ { return at(n); }
+#endif // size_t != unsigned int
+
+ // operator versions of GetWriteableChar()
+ wxUniCharRef operator[](int n)
+ { return at(n); }
+ wxUniCharRef operator[](long n)
+ { return at(n); }
+ wxUniCharRef operator[](size_t n)
+ { return at(n); }
+#ifndef wxSIZE_T_IS_UINT
+ wxUniCharRef operator[](unsigned int n)
+ { return at(n); }
+#endif // size_t != unsigned int
+
+ // explicit conversion to C string (use this with printf()!)
+ wxCStrData c_str() const { return wxCStrData(this); }
+ wxCStrData data() const { return c_str(); }
+
+ // implicit conversion to C string
+ operator wxCStrData() const { return c_str(); }
+
+ // the first two operators conflict with operators for conversion to
+ // std::string and they must be disabled in STL build; the next one only
+ // makes sense if conversions to char* are also defined and not defining it
+ // in STL build also helps us to get more clear error messages for the code
+ // which relies on implicit conversion to char* in STL build
+#if !wxUSE_STL
+ operator const char*() const { return c_str(); }
+ operator const wchar_t*() const { return c_str(); }
+
+ // implicit conversion to untyped pointer for compatibility with previous
+ // wxWidgets versions: this is the same as conversion to const char * so it
+ // may fail!
+ operator const void*() const { return c_str(); }
+#endif // wxUSE_STL
+
+ // identical to c_str(), for MFC compatibility
+ const wxCStrData GetData() const { return c_str(); }
+
+ // explicit conversion to C string in internal representation (char*,
+ // wchar_t*, UTF-8-encoded char*, depending on the build):
+ const wxStringCharType *wx_str() const { return m_impl.c_str(); }
+
+ // conversion to *non-const* multibyte or widestring buffer; modifying
+ // returned buffer won't affect the string, these methods are only useful
+ // for passing values to const-incorrect functions
+ wxWritableCharBuffer char_str(const wxMBConv& conv = wxConvLibc) const
+ { return mb_str(conv); }
+ wxWritableWCharBuffer wchar_str() const { return wc_str(); }
+
+ // conversion to the buffer of the given type T (= char or wchar_t) and
+ // also optionally return the buffer length
+ //
+ // this is mostly/only useful for the template functions
+ //
+ // FIXME-VC6: the second argument only exists for VC6 which doesn't support
+ // explicit template function selection, do not use it unless
+ // you must support VC6!
+ template <typename T>
+ wxCharTypeBuffer<T> tchar_str(size_t *len = NULL,
+ T * WXUNUSED(dummy) = NULL) const
+ {
+#if wxUSE_UNICODE
+ // we need a helper dispatcher depending on type
+ return wxPrivate::wxStringAsBufHelper<T>::Get(*this, len);
+#else // ANSI
+ // T can only be char in ANSI build
+ if ( len )
+ *len = length();
+
+ return wxCharTypeBuffer<T>::CreateNonOwned(wx_str());
+#endif // Unicode build kind
+ }
+
+ // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
+ // converting numbers or strings which are certain not to contain special
+ // chars (typically system functions, X atoms, environment variables etc.)
+ //