+ return *this;
+ }
+
+ wxString& replace(size_t nStart, size_t nLen, const wchar_t* sz)
+ {
+ wxSTRING_INVALIDATE_CACHE();
+
+ size_t from, len;
+ PosLenToImpl(nStart, nLen, &from, &len);
+ m_impl.replace(from, len, ImplStr(sz));
+
+ return *this;
+ }
+
+ // replaces the substring of length nLen starting at nStart
+ wxString& replace(size_t nStart, size_t nLen, const wxString& str)
+ {
+ wxSTRING_INVALIDATE_CACHE();
+
+ size_t from, len;
+ PosLenToImpl(nStart, nLen, &from, &len);
+ m_impl.replace(from, len, str.m_impl);
+
+ return *this;
+ }
+
+ // replaces the substring with nCount copies of ch
+ wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxUniChar ch)
+ {
+ wxSTRING_INVALIDATE_CACHE();
+
+ size_t from, len;
+ PosLenToImpl(nStart, nLen, &from, &len);
+#if wxUSE_UNICODE_UTF8
+ if ( !ch.IsAscii() )
+ m_impl.replace(from, len, wxStringOperations::EncodeNChars(nCount, ch));
+ else
+#endif
+ m_impl.replace(from, len, nCount, (wxStringCharType)ch);
+
+ return *this;
+ }
+
+ // replaces a substring with another substring
+ wxString& replace(size_t nStart, size_t nLen,
+ const wxString& str, size_t nStart2, size_t nLen2)
+ {
+ wxSTRING_INVALIDATE_CACHE();
+
+ size_t from, len;
+ PosLenToImpl(nStart, nLen, &from, &len);
+
+ size_t from2, len2;
+ str.PosLenToImpl(nStart2, nLen2, &from2, &len2);
+
+ m_impl.replace(from, len, str.m_impl, from2, len2);
+
+ return *this;
+ }
+
+ // replaces the substring with first nCount chars of sz
+ wxString& replace(size_t nStart, size_t nLen,
+ const char* sz, size_t nCount)
+ {
+ wxSTRING_INVALIDATE_CACHE();
+
+ size_t from, len;
+ PosLenToImpl(nStart, nLen, &from, &len);
+
+ SubstrBufFromMB str(ImplStr(sz, nCount));
+
+ m_impl.replace(from, len, str.data, str.len);
+
+ return *this;
+ }
+
+ wxString& replace(size_t nStart, size_t nLen,
+ const wchar_t* sz, size_t nCount)
+ {
+ wxSTRING_INVALIDATE_CACHE();
+
+ size_t from, len;
+ PosLenToImpl(nStart, nLen, &from, &len);
+
+ SubstrBufFromWC str(ImplStr(sz, nCount));
+
+ m_impl.replace(from, len, str.data, str.len);
+
+ return *this;
+ }
+
+ wxString& replace(size_t nStart, size_t nLen,
+ const wxString& s, size_t nCount)
+ {
+ wxSTRING_INVALIDATE_CACHE();
+
+ size_t from, len;
+ PosLenToImpl(nStart, nLen, &from, &len);
+ m_impl.replace(from, len, s.m_impl.c_str(), s.LenToImpl(nCount));
+
+ return *this;
+ }
+
+ wxString& replace(iterator first, iterator last, const char* s)
+ {
+ wxSTRING_INVALIDATE_CACHE();
+
+ m_impl.replace(first.impl(), last.impl(), ImplStr(s));
+
+ return *this;
+ }
+
+ wxString& replace(iterator first, iterator last, const wchar_t* s)
+ {
+ wxSTRING_INVALIDATE_CACHE();
+
+ m_impl.replace(first.impl(), last.impl(), ImplStr(s));
+
+ return *this;
+ }
+
+ wxString& replace(iterator first, iterator last, const char* s, size_type n)
+ {
+ wxSTRING_INVALIDATE_CACHE();
+
+ SubstrBufFromMB str(ImplStr(s, n));
+ m_impl.replace(first.impl(), last.impl(), str.data, str.len);
+
+ return *this;
+ }
+
+ wxString& replace(iterator first, iterator last, const wchar_t* s, size_type n)
+ {
+ wxSTRING_INVALIDATE_CACHE();
+
+ SubstrBufFromWC str(ImplStr(s, n));
+ m_impl.replace(first.impl(), last.impl(), str.data, str.len);
+
+ return *this;
+ }
+
+ wxString& replace(iterator first, iterator last, const wxString& s)
+ {
+ wxSTRING_INVALIDATE_CACHE();
+
+ m_impl.replace(first.impl(), last.impl(), s.m_impl);
+
+ return *this;
+ }
+
+ wxString& replace(iterator first, iterator last, size_type n, wxUniChar ch)
+ {
+ wxSTRING_INVALIDATE_CACHE();
+
+#if wxUSE_UNICODE_UTF8
+ if ( !ch.IsAscii() )
+ m_impl.replace(first.impl(), last.impl(),
+ wxStringOperations::EncodeNChars(n, ch));
+ else
+#endif
+ m_impl.replace(first.impl(), last.impl(), n, (wxStringCharType)ch);
+
+ return *this;
+ }
+
+ wxString& replace(iterator first, iterator last,
+ const_iterator first1, const_iterator last1)
+ {
+ wxSTRING_INVALIDATE_CACHE();
+
+ m_impl.replace(first.impl(), last.impl(), first1.impl(), last1.impl());
+
+ return *this;
+ }
+
+ wxString& replace(iterator first, iterator last,
+ const char *first1, const char *last1)
+ { replace(first, last, first1, last1 - first1); return *this; }
+ wxString& replace(iterator first, iterator last,
+ const wchar_t *first1, const wchar_t *last1)
+ { replace(first, last, first1, last1 - first1); return *this; }
+
+ // swap two strings
+ void swap(wxString& str)
+ {
+#if wxUSE_STRING_POS_CACHE
+ // we modify not only this string but also the other one directly so we
+ // need to invalidate cache for both of them (we could also try to
+ // exchange their cache entries but it seems unlikely to be worth it)
+ InvalidateCache();
+ str.InvalidateCache();
+#endif // wxUSE_STRING_POS_CACHE
+
+ m_impl.swap(str.m_impl);
+ }
+
+ // find a substring
+ size_t find(const wxString& str, size_t nStart = 0) const
+ { return PosFromImpl(m_impl.find(str.m_impl, PosToImpl(nStart))); }
+
+ // find first n characters of sz
+ size_t find(const char* sz, size_t nStart = 0, size_t n = npos) const
+ {
+ SubstrBufFromMB str(ImplStr(sz, n));
+ return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len));
+ }
+ size_t find(const wchar_t* sz, size_t nStart = 0, size_t n = npos) const
+ {
+ SubstrBufFromWC str(ImplStr(sz, n));
+ return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len));
+ }
+ size_t find(const wxScopedCharBuffer& s, size_t nStart = 0, size_t n = npos) const
+ { return find(s.data(), nStart, n); }
+ size_t find(const wxScopedWCharBuffer& s, size_t nStart = 0, size_t n = npos) const
+ { return find(s.data(), nStart, n); }
+ size_t find(const wxCStrData& s, size_t nStart = 0, size_t n = npos) const
+ { return find(s.AsWChar(), nStart, n); }
+
+ // find the first occurrence of character ch after nStart
+ size_t find(wxUniChar ch, size_t nStart = 0) const
+ {
+#if wxUSE_UNICODE_UTF8
+ if ( !ch.IsAscii() )
+ return PosFromImpl(m_impl.find(wxStringOperations::EncodeChar(ch),
+ PosToImpl(nStart)));
+ else
+#endif
+ return PosFromImpl(m_impl.find((wxStringCharType)ch,
+ PosToImpl(nStart)));
+
+ }
+ size_t find(wxUniCharRef ch, size_t nStart = 0) const
+ { return find(wxUniChar(ch), nStart); }
+ size_t find(char ch, size_t nStart = 0) const
+ { return find(wxUniChar(ch), nStart); }
+ size_t find(unsigned char ch, size_t nStart = 0) const
+ { return find(wxUniChar(ch), nStart); }
+ size_t find(wchar_t ch, size_t nStart = 0) const
+ { return find(wxUniChar(ch), nStart); }
+
+ // rfind() family is exactly like find() but works right to left
+
+ // as find, but from the end
+ size_t rfind(const wxString& str, size_t nStart = npos) const
+ { return PosFromImpl(m_impl.rfind(str.m_impl, PosToImpl(nStart))); }
+
+ // as find, but from the end
+ size_t rfind(const char* sz, size_t nStart = npos, size_t n = npos) const
+ {
+ SubstrBufFromMB str(ImplStr(sz, n));
+ return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len));
+ }
+ size_t rfind(const wchar_t* sz, size_t nStart = npos, size_t n = npos) const
+ {
+ SubstrBufFromWC str(ImplStr(sz, n));
+ return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len));
+ }
+ size_t rfind(const wxScopedCharBuffer& s, size_t nStart = npos, size_t n = npos) const
+ { return rfind(s.data(), nStart, n); }
+ size_t rfind(const wxScopedWCharBuffer& s, size_t nStart = npos, size_t n = npos) const
+ { return rfind(s.data(), nStart, n); }
+ size_t rfind(const wxCStrData& s, size_t nStart = npos, size_t n = npos) const
+ { return rfind(s.AsWChar(), nStart, n); }
+ // as find, but from the end
+ size_t rfind(wxUniChar ch, size_t nStart = npos) const
+ {
+#if wxUSE_UNICODE_UTF8
+ if ( !ch.IsAscii() )
+ return PosFromImpl(m_impl.rfind(wxStringOperations::EncodeChar(ch),
+ PosToImpl(nStart)));
+ else
+#endif
+ return PosFromImpl(m_impl.rfind((wxStringCharType)ch,
+ PosToImpl(nStart)));
+ }
+ size_t rfind(wxUniCharRef ch, size_t nStart = npos) const
+ { return rfind(wxUniChar(ch), nStart); }
+ size_t rfind(char ch, size_t nStart = npos) const
+ { return rfind(wxUniChar(ch), nStart); }
+ size_t rfind(unsigned char ch, size_t nStart = npos) const
+ { return rfind(wxUniChar(ch), nStart); }
+ size_t rfind(wchar_t ch, size_t nStart = npos) const
+ { return rfind(wxUniChar(ch), nStart); }
+
+ // find first/last occurrence of any character (not) in the set:
+#if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
+ // FIXME-UTF8: this is not entirely correct, because it doesn't work if
+ // sizeof(wchar_t)==2 and surrogates are present in the string;
+ // should we care? Probably not.
+ size_t find_first_of(const wxString& str, size_t nStart = 0) const
+ { return m_impl.find_first_of(str.m_impl, nStart); }
+ size_t find_first_of(const char* sz, size_t nStart = 0) const
+ { return m_impl.find_first_of(ImplStr(sz), nStart); }
+ size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const
+ { return m_impl.find_first_of(ImplStr(sz), nStart); }
+ size_t find_first_of(const char* sz, size_t nStart, size_t n) const
+ { return m_impl.find_first_of(ImplStr(sz), nStart, n); }
+ size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const
+ { return m_impl.find_first_of(ImplStr(sz), nStart, n); }
+ size_t find_first_of(wxUniChar c, size_t nStart = 0) const
+ { return m_impl.find_first_of((wxChar)c, nStart); }
+
+ size_t find_last_of(const wxString& str, size_t nStart = npos) const
+ { return m_impl.find_last_of(str.m_impl, nStart); }
+ size_t find_last_of(const char* sz, size_t nStart = npos) const
+ { return m_impl.find_last_of(ImplStr(sz), nStart); }
+ size_t find_last_of(const wchar_t* sz, size_t nStart = npos) const
+ { return m_impl.find_last_of(ImplStr(sz), nStart); }
+ size_t find_last_of(const char* sz, size_t nStart, size_t n) const
+ { return m_impl.find_last_of(ImplStr(sz), nStart, n); }
+ size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const
+ { return m_impl.find_last_of(ImplStr(sz), nStart, n); }
+ size_t find_last_of(wxUniChar c, size_t nStart = npos) const
+ { return m_impl.find_last_of((wxChar)c, nStart); }
+
+ size_t find_first_not_of(const wxString& str, size_t nStart = 0) const
+ { return m_impl.find_first_not_of(str.m_impl, nStart); }
+ size_t find_first_not_of(const char* sz, size_t nStart = 0) const
+ { return m_impl.find_first_not_of(ImplStr(sz), nStart); }
+ size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const
+ { return m_impl.find_first_not_of(ImplStr(sz), nStart); }
+ size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const
+ { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); }
+ size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const
+ { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); }
+ size_t find_first_not_of(wxUniChar c, size_t nStart = 0) const
+ { return m_impl.find_first_not_of((wxChar)c, nStart); }
+
+ size_t find_last_not_of(const wxString& str, size_t nStart = npos) const
+ { return m_impl.find_last_not_of(str.m_impl, nStart); }
+ size_t find_last_not_of(const char* sz, size_t nStart = npos) const
+ { return m_impl.find_last_not_of(ImplStr(sz), nStart); }
+ size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const
+ { return m_impl.find_last_not_of(ImplStr(sz), nStart); }
+ size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const
+ { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); }
+ size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const
+ { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); }
+ size_t find_last_not_of(wxUniChar c, size_t nStart = npos) const
+ { return m_impl.find_last_not_of((wxChar)c, nStart); }
+#else
+ // we can't use std::string implementation in UTF-8 build, because the
+ // character sets would be interpreted wrongly:
+
+ // as strpbrk() but starts at nStart, returns npos if not found
+ size_t find_first_of(const wxString& str, size_t nStart = 0) const
+#if wxUSE_UNICODE // FIXME-UTF8: temporary
+ { return find_first_of(str.wc_str(), nStart); }
+#else
+ { return find_first_of(str.mb_str(), nStart); }
+#endif
+ // same as above
+ size_t find_first_of(const char* sz, size_t nStart = 0) const;
+ size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const;
+ size_t find_first_of(const char* sz, size_t nStart, size_t n) const;
+ size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const;
+ // same as find(char, size_t)
+ size_t find_first_of(wxUniChar c, size_t nStart = 0) const
+ { return find(c, nStart); }
+ // find the last (starting from nStart) char from str in this string
+ size_t find_last_of (const wxString& str, size_t nStart = npos) const
+#if wxUSE_UNICODE // FIXME-UTF8: temporary
+ { return find_last_of(str.wc_str(), nStart); }
+#else
+ { return find_last_of(str.mb_str(), nStart); }
+#endif
+ // same as above
+ size_t find_last_of (const char* sz, size_t nStart = npos) const;
+ size_t find_last_of (const wchar_t* sz, size_t nStart = npos) const;
+ size_t find_last_of(const char* sz, size_t nStart, size_t n) const;
+ size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const;
+ // same as above
+ size_t find_last_of(wxUniChar c, size_t nStart = npos) const
+ { return rfind(c, nStart); }
+
+ // find first/last occurrence of any character not in the set
+
+ // as strspn() (starting from nStart), returns npos on failure
+ size_t find_first_not_of(const wxString& str, size_t nStart = 0) const
+#if wxUSE_UNICODE // FIXME-UTF8: temporary
+ { return find_first_not_of(str.wc_str(), nStart); }
+#else
+ { return find_first_not_of(str.mb_str(), nStart); }
+#endif
+ // same as above
+ size_t find_first_not_of(const char* sz, size_t nStart = 0) const;
+ size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const;
+ size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const;
+ size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const;
+ // same as above
+ size_t find_first_not_of(wxUniChar ch, size_t nStart = 0) const;
+ // as strcspn()
+ size_t find_last_not_of(const wxString& str, size_t nStart = npos) const
+#if wxUSE_UNICODE // FIXME-UTF8: temporary
+ { return find_last_not_of(str.wc_str(), nStart); }
+#else
+ { return find_last_not_of(str.mb_str(), nStart); }
+#endif
+ // same as above
+ size_t find_last_not_of(const char* sz, size_t nStart = npos) const;
+ size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const;
+ size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const;
+ size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const;
+ // same as above
+ size_t find_last_not_of(wxUniChar ch, size_t nStart = npos) const;
+#endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not
+
+ // provide char/wchar_t/wxUniCharRef overloads for char-finding functions
+ // above to resolve ambiguities:
+ size_t find_first_of(wxUniCharRef ch, size_t nStart = 0) const
+ { return find_first_of(wxUniChar(ch), nStart); }
+ size_t find_first_of(char ch, size_t nStart = 0) const
+ { return find_first_of(wxUniChar(ch), nStart); }
+ size_t find_first_of(unsigned char ch, size_t nStart = 0) const
+ { return find_first_of(wxUniChar(ch), nStart); }
+ size_t find_first_of(wchar_t ch, size_t nStart = 0) const
+ { return find_first_of(wxUniChar(ch), nStart); }
+ size_t find_last_of(wxUniCharRef ch, size_t nStart = npos) const
+ { return find_last_of(wxUniChar(ch), nStart); }
+ size_t find_last_of(char ch, size_t nStart = npos) const
+ { return find_last_of(wxUniChar(ch), nStart); }
+ size_t find_last_of(unsigned char ch, size_t nStart = npos) const
+ { return find_last_of(wxUniChar(ch), nStart); }
+ size_t find_last_of(wchar_t ch, size_t nStart = npos) const
+ { return find_last_of(wxUniChar(ch), nStart); }
+ size_t find_first_not_of(wxUniCharRef ch, size_t nStart = 0) const
+ { return find_first_not_of(wxUniChar(ch), nStart); }
+ size_t find_first_not_of(char ch, size_t nStart = 0) const
+ { return find_first_not_of(wxUniChar(ch), nStart); }
+ size_t find_first_not_of(unsigned char ch, size_t nStart = 0) const
+ { return find_first_not_of(wxUniChar(ch), nStart); }
+ size_t find_first_not_of(wchar_t ch, size_t nStart = 0) const
+ { return find_first_not_of(wxUniChar(ch), nStart); }
+ size_t find_last_not_of(wxUniCharRef ch, size_t nStart = npos) const
+ { return find_last_not_of(wxUniChar(ch), nStart); }
+ size_t find_last_not_of(char ch, size_t nStart = npos) const
+ { return find_last_not_of(wxUniChar(ch), nStart); }
+ size_t find_last_not_of(unsigned char ch, size_t nStart = npos) const
+ { return find_last_not_of(wxUniChar(ch), nStart); }
+ size_t find_last_not_of(wchar_t ch, size_t nStart = npos) const
+ { return find_last_not_of(wxUniChar(ch), nStart); }
+
+ // and additional overloads for the versions taking strings:
+ size_t find_first_of(const wxCStrData& sz, size_t nStart = 0) const
+ { return find_first_of(sz.AsString(), nStart); }
+ size_t find_first_of(const wxScopedCharBuffer& sz, size_t nStart = 0) const
+ { return find_first_of(sz.data(), nStart); }
+ size_t find_first_of(const wxScopedWCharBuffer& sz, size_t nStart = 0) const
+ { return find_first_of(sz.data(), nStart); }
+ size_t find_first_of(const wxCStrData& sz, size_t nStart, size_t n) const
+ { return find_first_of(sz.AsWChar(), nStart, n); }
+ size_t find_first_of(const wxScopedCharBuffer& sz, size_t nStart, size_t n) const
+ { return find_first_of(sz.data(), nStart, n); }
+ size_t find_first_of(const wxScopedWCharBuffer& sz, size_t nStart, size_t n) const
+ { return find_first_of(sz.data(), nStart, n); }
+
+ size_t find_last_of(const wxCStrData& sz, size_t nStart = 0) const
+ { return find_last_of(sz.AsString(), nStart); }
+ size_t find_last_of(const wxScopedCharBuffer& sz, size_t nStart = 0) const
+ { return find_last_of(sz.data(), nStart); }
+ size_t find_last_of(const wxScopedWCharBuffer& sz, size_t nStart = 0) const
+ { return find_last_of(sz.data(), nStart); }
+ size_t find_last_of(const wxCStrData& sz, size_t nStart, size_t n) const
+ { return find_last_of(sz.AsWChar(), nStart, n); }
+ size_t find_last_of(const wxScopedCharBuffer& sz, size_t nStart, size_t n) const
+ { return find_last_of(sz.data(), nStart, n); }
+ size_t find_last_of(const wxScopedWCharBuffer& sz, size_t nStart, size_t n) const
+ { return find_last_of(sz.data(), nStart, n); }
+
+ size_t find_first_not_of(const wxCStrData& sz, size_t nStart = 0) const
+ { return find_first_not_of(sz.AsString(), nStart); }
+ size_t find_first_not_of(const wxScopedCharBuffer& sz, size_t nStart = 0) const
+ { return find_first_not_of(sz.data(), nStart); }
+ size_t find_first_not_of(const wxScopedWCharBuffer& sz, size_t nStart = 0) const
+ { return find_first_not_of(sz.data(), nStart); }
+ size_t find_first_not_of(const wxCStrData& sz, size_t nStart, size_t n) const
+ { return find_first_not_of(sz.AsWChar(), nStart, n); }
+ size_t find_first_not_of(const wxScopedCharBuffer& sz, size_t nStart, size_t n) const
+ { return find_first_not_of(sz.data(), nStart, n); }
+ size_t find_first_not_of(const wxScopedWCharBuffer& sz, size_t nStart, size_t n) const
+ { return find_first_not_of(sz.data(), nStart, n); }
+
+ size_t find_last_not_of(const wxCStrData& sz, size_t nStart = 0) const
+ { return find_last_not_of(sz.AsString(), nStart); }
+ size_t find_last_not_of(const wxScopedCharBuffer& sz, size_t nStart = 0) const
+ { return find_last_not_of(sz.data(), nStart); }
+ size_t find_last_not_of(const wxScopedWCharBuffer& sz, size_t nStart = 0) const
+ { return find_last_not_of(sz.data(), nStart); }
+ size_t find_last_not_of(const wxCStrData& sz, size_t nStart, size_t n) const
+ { return find_last_not_of(sz.AsWChar(), nStart, n); }
+ size_t find_last_not_of(const wxScopedCharBuffer& sz, size_t nStart, size_t n) const
+ { return find_last_not_of(sz.data(), nStart, n); }
+ size_t find_last_not_of(const wxScopedWCharBuffer& sz, size_t nStart, size_t n) const
+ { return find_last_not_of(sz.data(), nStart, n); }
+
+ // string += string
+ wxString& operator+=(const wxString& s)
+ {
+ wxSTRING_INVALIDATE_CACHED_LENGTH();
+
+ m_impl += s.m_impl;
+ return *this;
+ }
+ // string += C string
+ wxString& operator+=(const char *psz)
+ {
+ wxSTRING_INVALIDATE_CACHED_LENGTH();
+
+ m_impl += ImplStr(psz);
+ return *this;
+ }
+ wxString& operator+=(const wchar_t *pwz)
+ {
+ wxSTRING_INVALIDATE_CACHED_LENGTH();
+
+ m_impl += ImplStr(pwz);
+ return *this;
+ }
+ wxString& operator+=(const wxCStrData& s)
+ {
+ wxSTRING_INVALIDATE_CACHED_LENGTH();
+
+ m_impl += s.AsString().m_impl;
+ return *this;
+ }
+ wxString& operator+=(const wxScopedCharBuffer& s)
+ { return append(s); }
+ wxString& operator+=(const wxScopedWCharBuffer& s)
+ { return append(s); }
+ // string += char
+ wxString& operator+=(wxUniChar ch)
+ {
+ wxSTRING_UPDATE_CACHED_LENGTH(1);
+
+#if wxUSE_UNICODE_UTF8
+ if ( !ch.IsAscii() )
+ m_impl += wxStringOperations::EncodeChar(ch);
+ else
+#endif
+ m_impl += (wxStringCharType)ch;
+ return *this;
+ }
+ wxString& operator+=(wxUniCharRef ch) { return *this += wxUniChar(ch); }
+ wxString& operator+=(int ch) { return *this += wxUniChar(ch); }
+ wxString& operator+=(char ch) { return *this += wxUniChar(ch); }
+ wxString& operator+=(unsigned char ch) { return *this += wxUniChar(ch); }
+ wxString& operator+=(wchar_t ch) { return *this += wxUniChar(ch); }
+
+private:
+#if !wxUSE_STL_BASED_WXSTRING
+ // helpers for wxStringBuffer and wxStringBufferLength
+ wxStringCharType *DoGetWriteBuf(size_t nLen)
+ {
+ return m_impl.DoGetWriteBuf(nLen);
+ }
+
+ void DoUngetWriteBuf()
+ {
+ wxSTRING_INVALIDATE_CACHE();
+
+ m_impl.DoUngetWriteBuf();
+ }
+
+ void DoUngetWriteBuf(size_t nLen)
+ {
+ wxSTRING_INVALIDATE_CACHE();
+
+ m_impl.DoUngetWriteBuf(nLen);
+ }
+#endif // !wxUSE_STL_BASED_WXSTRING
+
+#ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
+ #if !wxUSE_UTF8_LOCALE_ONLY
+ int DoPrintfWchar(const wxChar *format, ...);
+ static wxString DoFormatWchar(const wxChar *format, ...);
+ #endif
+ #if wxUSE_UNICODE_UTF8
+ int DoPrintfUtf8(const char *format, ...);
+ static wxString DoFormatUtf8(const char *format, ...);
+ #endif
+#endif
+
+#if !wxUSE_STL_BASED_WXSTRING
+ // check string's data validity
+ bool IsValid() const { return m_impl.GetStringData()->IsValid(); }
+#endif
+
+private:
+ wxStringImpl m_impl;
+
+ // buffers for compatibility conversion from (char*)c_str() and
+ // (wchar_t*)c_str(): the pointers returned by these functions should remain
+ // valid until the string itself is modified for compatibility with the
+ // existing code and consistency with std::string::c_str() so returning a
+ // temporary buffer won't do and we need to cache the conversion results
+
+ // TODO-UTF8: benchmark various approaches to keeping compatibility buffers
+ template<typename T>
+ struct ConvertedBuffer
+ {
+ // notice that there is no need to initialize m_len here as it's unused
+ // as long as m_str is NULL
+ ConvertedBuffer() : m_str(NULL) {}
+ ~ConvertedBuffer()
+ { free(m_str); }
+
+ bool Extend(size_t len)
+ {
+ // add extra 1 for the trailing NUL
+ void * const str = realloc(m_str, sizeof(T)*(len + 1));
+ if ( !str )
+ return false;
+
+ m_str = static_cast<T *>(str);
+ m_len = len;
+
+ return true;
+ }
+
+ const wxScopedCharTypeBuffer<T> AsScopedBuffer() const
+ {
+ return wxScopedCharTypeBuffer<T>::CreateNonOwned(m_str, m_len);
+ }
+
+ T *m_str; // pointer to the string data
+ size_t m_len; // length, not size, i.e. in chars and without last NUL
+ };
+
+
+#if wxUSE_UNICODE
+ // common mb_str() and wxCStrData::AsChar() helper: performs the conversion
+ // and returns either m_convertedToChar.m_str (in which case its m_len is
+ // also updated) or NULL if it failed
+ //
+ // there is an important exception: in wxUSE_UNICODE_UTF8 build if conv is a
+ // UTF-8 one, we return m_impl.c_str() directly, without doing any conversion
+ // as optimization and so the caller needs to check for this before using
+ // m_convertedToChar
+ //
+ // NB: AsChar() returns char* in any build, unlike mb_str()
+ const char *AsChar(const wxMBConv& conv) const;
+
+ // mb_str() implementation helper
+ wxScopedCharBuffer AsCharBuf(const wxMBConv& conv) const
+ {
+#if wxUSE_UNICODE_UTF8
+ // avoid conversion if we can
+ if ( conv.IsUTF8() )
+ {
+ return wxScopedCharBuffer::CreateNonOwned(m_impl.c_str(),
+ m_impl.length());
+ }
+#endif // wxUSE_UNICODE_UTF8
+
+ // call this solely in order to fill in m_convertedToChar as AsChar()
+ // updates it as a side effect: this is a bit ugly but it's a completely
+ // internal function so the users of this class shouldn't care or know
+ // about it and doing it like this, i.e. having a separate AsChar(),
+ // allows us to avoid the creation and destruction of a temporary buffer
+ // when using wxCStrData without duplicating any code
+ if ( !AsChar(conv) )
+ {
+ // although it would be probably more correct to return NULL buffer
+ // from here if the conversion fails, a lot of existing code doesn't
+ // expect mb_str() (or wc_str()) to ever return NULL so return an
+ // empty string otherwise to avoid crashes in it
+ //
+ // also, some existing code does check for the conversion success and
+ // so asserting here would be bad too -- even if it does mean that
+ // silently losing data is possible for badly written code
+ return wxScopedCharBuffer::CreateNonOwned("", 0);
+ }
+
+ return m_convertedToChar.AsScopedBuffer();
+ }
+
+ ConvertedBuffer<char> m_convertedToChar;
+#endif // !wxUSE_UNICODE
+
+#if !wxUSE_UNICODE_WCHAR
+ // common wc_str() and wxCStrData::AsWChar() helper for both UTF-8 and ANSI
+ // builds: converts the string contents into m_convertedToWChar and returns
+ // NULL if the conversion failed (this can only happen in ANSI build)
+ //
+ // NB: AsWChar() returns wchar_t* in any build, unlike wc_str()
+ const wchar_t *AsWChar(const wxMBConv& conv) const;
+
+ // wc_str() implementation helper
+ wxScopedWCharBuffer AsWCharBuf(const wxMBConv& conv) const
+ {
+ if ( !AsWChar(conv) )
+ return wxScopedWCharBuffer::CreateNonOwned(L"", 0);
+
+ return m_convertedToWChar.AsScopedBuffer();
+ }
+
+ ConvertedBuffer<wchar_t> m_convertedToWChar;
+#endif // !wxUSE_UNICODE_WCHAR
+
+#if wxUSE_UNICODE_UTF8
+ // FIXME-UTF8: (try to) move this elsewhere (TLS) or solve differently
+ // assigning to character pointer to by wxString::iterator 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
+ wxDECLARE_NO_COPY_CLASS(wxStringIteratorNodeHead);
+ };
+
+ wxStringIteratorNodeHead m_iterators;
+
+ friend class WXDLLIMPEXP_FWD_BASE wxStringIteratorNode;
+ friend class WXDLLIMPEXP_FWD_BASE wxUniCharRef;
+#endif // wxUSE_UNICODE_UTF8
+
+ friend class WXDLLIMPEXP_FWD_BASE wxCStrData;
+ friend class wxStringInternalBuffer;
+ friend class wxStringInternalBufferLength;
+};
+
+#ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
+ #pragma warning (pop)
+#endif
+
+// string iterator operators that satisfy STL Random Access Iterator
+// requirements:
+inline wxString::iterator operator+(ptrdiff_t n, wxString::iterator i)
+ { return i + n; }
+inline wxString::const_iterator operator+(ptrdiff_t n, wxString::const_iterator i)
+ { return i + n; }
+inline wxString::reverse_iterator operator+(ptrdiff_t n, wxString::reverse_iterator i)
+ { return i + n; }
+inline wxString::const_reverse_iterator operator+(ptrdiff_t n, wxString::const_reverse_iterator i)
+ { return i + n; }
+
+// notice that even though for many compilers the friend declarations above are
+// enough, from the point of view of C++ standard we must have the declarations
+// here as friend ones are not injected in the enclosing namespace and without
+// them the code fails to compile with conforming compilers such as xlC or g++4
+wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2);
+wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const char *psz);
+wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wchar_t *pwz);
+wxString WXDLLIMPEXP_BASE operator+(const char *psz, const wxString& string);
+wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz, const wxString& string);
+
+wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch);
+wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string);
+
+inline wxString operator+(const wxString& string, wxUniCharRef ch)
+ { return string + (wxUniChar)ch; }
+inline wxString operator+(const wxString& string, char ch)
+ { return string + wxUniChar(ch); }
+inline wxString operator+(const wxString& string, wchar_t ch)
+ { return string + wxUniChar(ch); }
+inline wxString operator+(wxUniCharRef ch, const wxString& string)
+ { return (wxUniChar)ch + string; }
+inline wxString operator+(char ch, const wxString& string)
+ { return wxUniChar(ch) + string; }
+inline wxString operator+(wchar_t ch, const wxString& string)
+ { return wxUniChar(ch) + string; }
+
+
+#define wxGetEmptyString() wxString()
+
+// ----------------------------------------------------------------------------
+// helper functions which couldn't be defined inline
+// ----------------------------------------------------------------------------
+
+namespace wxPrivate
+{
+
+#if wxUSE_UNICODE_WCHAR
+
+template <>
+struct wxStringAsBufHelper<char>
+{
+ static wxScopedCharBuffer Get(const wxString& s, size_t *len)
+ {
+ wxScopedCharBuffer buf(s.mb_str());
+ if ( len )
+ *len = buf ? strlen(buf) : 0;
+ return buf;
+ }
+};
+
+template <>
+struct wxStringAsBufHelper<wchar_t>
+{
+ static wxScopedWCharBuffer Get(const wxString& s, size_t *len)
+ {
+ const size_t length = s.length();
+ if ( len )
+ *len = length;
+ return wxScopedWCharBuffer::CreateNonOwned(s.wx_str(), length);
+ }
+};
+
+#elif wxUSE_UNICODE_UTF8
+
+template <>
+struct wxStringAsBufHelper<char>
+{
+ static wxScopedCharBuffer Get(const wxString& s, size_t *len)
+ {
+ const size_t length = s.utf8_length();
+ if ( len )
+ *len = length;
+ return wxScopedCharBuffer::CreateNonOwned(s.wx_str(), length);
+ }
+};
+
+template <>
+struct wxStringAsBufHelper<wchar_t>
+{
+ static wxScopedWCharBuffer Get(const wxString& s, size_t *len)
+ {
+ wxScopedWCharBuffer wbuf(s.wc_str());
+ if ( len )
+ *len = wxWcslen(wbuf);
+ return wbuf;
+ }
+};
+
+#endif // Unicode build kind
+
+} // namespace wxPrivate
+
+// ----------------------------------------------------------------------------
+// wxStringBuffer: a tiny class allowing to get a writable pointer into string
+// ----------------------------------------------------------------------------
+
+#if !wxUSE_STL_BASED_WXSTRING
+// string buffer for direct access to string data in their native
+// representation:
+class wxStringInternalBuffer
+{
+public:
+ typedef wxStringCharType CharType;
+
+ wxStringInternalBuffer(wxString& str, size_t lenWanted = 1024)
+ : m_str(str), m_buf(NULL)
+ { m_buf = m_str.DoGetWriteBuf(lenWanted); }
+
+ ~wxStringInternalBuffer() { m_str.DoUngetWriteBuf(); }
+
+ operator wxStringCharType*() const { return m_buf; }
+
+private:
+ wxString& m_str;
+ wxStringCharType *m_buf;
+
+ wxDECLARE_NO_COPY_CLASS(wxStringInternalBuffer);
+};
+
+class wxStringInternalBufferLength
+{
+public:
+ typedef wxStringCharType CharType;
+
+ wxStringInternalBufferLength(wxString& str, size_t lenWanted = 1024)
+ : m_str(str), m_buf(NULL), m_len(0), m_lenSet(false)
+ {
+ m_buf = m_str.DoGetWriteBuf(lenWanted);
+ wxASSERT(m_buf != NULL);
+ }
+
+ ~wxStringInternalBufferLength()
+ {
+ wxASSERT(m_lenSet);
+ m_str.DoUngetWriteBuf(m_len);
+ }
+
+ operator wxStringCharType*() const { return m_buf; }
+ void SetLength(size_t length) { m_len = length; m_lenSet = true; }
+
+private:
+ wxString& m_str;
+ wxStringCharType *m_buf;
+ size_t m_len;
+ bool m_lenSet;
+
+ wxDECLARE_NO_COPY_CLASS(wxStringInternalBufferLength);
+};
+
+#endif // !wxUSE_STL_BASED_WXSTRING
+
+template<typename T>
+class wxStringTypeBufferBase
+{
+public:
+ typedef T CharType;
+
+ wxStringTypeBufferBase(wxString& str, size_t lenWanted = 1024)
+ : m_str(str), m_buf(lenWanted)
+ {
+ // for compatibility with old wxStringBuffer which provided direct
+ // access to wxString internal buffer, initialize ourselves with the
+ // string initial contents
+
+ // FIXME-VC6: remove the ugly (CharType *)NULL and use normal
+ // tchar_str<CharType>
+ size_t len;
+ const wxCharTypeBuffer<CharType> buf(str.tchar_str(&len, (CharType *)NULL));
+ if ( buf )
+ {
+ if ( len > lenWanted )
+ {
+ // in this case there is not enough space for terminating NUL,
+ // ensure that we still put it there
+ m_buf.data()[lenWanted] = 0;
+ len = lenWanted - 1;
+ }
+
+ memcpy(m_buf.data(), buf, (len + 1)*sizeof(CharType));
+ }
+ //else: conversion failed, this can happen when trying to get Unicode
+ // string contents into a char string
+ }
+
+ operator CharType*() { return m_buf.data(); }
+
+protected:
+ wxString& m_str;
+ wxCharTypeBuffer<CharType> m_buf;
+};
+
+template<typename T>
+class wxStringTypeBufferLengthBase : public wxStringTypeBufferBase<T>
+{
+public:
+ wxStringTypeBufferLengthBase(wxString& str, size_t lenWanted = 1024)
+ : wxStringTypeBufferBase<T>(str, lenWanted),
+ m_len(0),
+ m_lenSet(false)
+ { }
+
+ ~wxStringTypeBufferLengthBase()
+ {
+ wxASSERT_MSG( this->m_lenSet, "forgot to call SetLength()" );
+ }
+
+ void SetLength(size_t length) { m_len = length; m_lenSet = true; }
+
+protected:
+ size_t m_len;
+ bool m_lenSet;
+};
+
+template<typename T>
+class wxStringTypeBuffer : public wxStringTypeBufferBase<T>
+{
+public:
+ wxStringTypeBuffer(wxString& str, size_t lenWanted = 1024)
+ : wxStringTypeBufferBase<T>(str, lenWanted)
+ { }
+
+ ~wxStringTypeBuffer()
+ {
+ this->m_str.assign(this->m_buf.data());
+ }
+
+ wxDECLARE_NO_COPY_CLASS(wxStringTypeBuffer);
+};
+
+template<typename T>
+class wxStringTypeBufferLength : public wxStringTypeBufferLengthBase<T>
+{
+public:
+ wxStringTypeBufferLength(wxString& str, size_t lenWanted = 1024)
+ : wxStringTypeBufferLengthBase<T>(str, lenWanted)
+ { }
+
+ ~wxStringTypeBufferLength()
+ {
+ this->m_str.assign(this->m_buf.data(), this->m_len);
+ }
+
+ wxDECLARE_NO_COPY_CLASS(wxStringTypeBufferLength);
+};
+
+#if wxUSE_STL_BASED_WXSTRING
+
+WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase<wxStringCharType> )
+
+class wxStringInternalBuffer : public wxStringTypeBufferBase<wxStringCharType>
+{
+public:
+ wxStringInternalBuffer(wxString& str, size_t lenWanted = 1024)
+ : wxStringTypeBufferBase<wxStringCharType>(str, lenWanted) {}
+ ~wxStringInternalBuffer()
+ { m_str.m_impl.assign(m_buf.data()); }
+
+ wxDECLARE_NO_COPY_CLASS(wxStringInternalBuffer);
+};
+
+WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE(
+ wxStringTypeBufferLengthBase<wxStringCharType> )
+
+class wxStringInternalBufferLength
+ : public wxStringTypeBufferLengthBase<wxStringCharType>
+{
+public:
+ wxStringInternalBufferLength(wxString& str, size_t lenWanted = 1024)
+ : wxStringTypeBufferLengthBase<wxStringCharType>(str, lenWanted) {}
+
+ ~wxStringInternalBufferLength()
+ {
+ m_str.m_impl.assign(m_buf.data(), m_len);
+ }
+
+ wxDECLARE_NO_COPY_CLASS(wxStringInternalBufferLength);
+};
+
+#endif // wxUSE_STL_BASED_WXSTRING
+
+
+#if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
+typedef wxStringTypeBuffer<wxChar> wxStringBuffer;
+typedef wxStringTypeBufferLength<wxChar> wxStringBufferLength;
+#else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
+typedef wxStringInternalBuffer wxStringBuffer;
+typedef wxStringInternalBufferLength wxStringBufferLength;
+#endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
+
+#if wxUSE_UNICODE_UTF8
+typedef wxStringInternalBuffer wxUTF8StringBuffer;
+typedef wxStringInternalBufferLength wxUTF8StringBufferLength;
+#elif wxUSE_UNICODE_WCHAR
+
+WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase<char> )
+
+// Note about inlined dtors in the classes below: this is done not for
+// performance reasons but just to avoid linking errors in the MSVC DLL build
+// under Windows: if a class has non-inline methods it must be declared as
+// being DLL-exported but, due to an extremely interesting feature of MSVC 7
+// and later, any template class which is used as a base of a DLL-exported
+// class is implicitly made DLL-exported too, as explained at the bottom of
+// http://msdn.microsoft.com/en-us/library/twa2aw10.aspx (just to confirm: yes,
+// _inheriting_ from a class can change whether it is being exported from DLL)
+//
+// But this results in link errors because the base template class is not DLL-
+// exported, whether it is declared with WXDLLIMPEXP_BASE or not, because it
+// does have only inline functions. So the simplest fix is to just make all the
+// functions of these classes inline too.
+
+class wxUTF8StringBuffer : public wxStringTypeBufferBase<char>
+{
+public:
+ wxUTF8StringBuffer(wxString& str, size_t lenWanted = 1024)
+ : wxStringTypeBufferBase<char>(str, lenWanted) {}
+ ~wxUTF8StringBuffer()
+ {
+ wxMBConvStrictUTF8 conv;
+ size_t wlen = conv.ToWChar(NULL, 0, m_buf);
+ wxCHECK_RET( wlen != wxCONV_FAILED, "invalid UTF-8 data in string buffer?" );
+
+ wxStringInternalBuffer wbuf(m_str, wlen);
+ conv.ToWChar(wbuf, wlen, m_buf);
+ }
+
+ wxDECLARE_NO_COPY_CLASS(wxUTF8StringBuffer);
+};
+
+WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferLengthBase<char> )
+
+class wxUTF8StringBufferLength : public wxStringTypeBufferLengthBase<char>
+{
+public:
+ wxUTF8StringBufferLength(wxString& str, size_t lenWanted = 1024)
+ : wxStringTypeBufferLengthBase<char>(str, lenWanted) {}
+ ~wxUTF8StringBufferLength()
+ {
+ wxCHECK_RET(m_lenSet, "length not set");
+
+ wxMBConvStrictUTF8 conv;
+ size_t wlen = conv.ToWChar(NULL, 0, m_buf, m_len);
+ wxCHECK_RET( wlen != wxCONV_FAILED, "invalid UTF-8 data in string buffer?" );
+
+ wxStringInternalBufferLength wbuf(m_str, wlen);
+ conv.ToWChar(wbuf, wlen, m_buf, m_len);
+ wbuf.SetLength(wlen);
+ }
+
+ wxDECLARE_NO_COPY_CLASS(wxUTF8StringBufferLength);
+};
+#endif // wxUSE_UNICODE_UTF8/wxUSE_UNICODE_WCHAR