+#if wxUSE_UNICODE_UTF8
+
+void wxString::PosLenToImpl(size_t pos, size_t len,
+ size_t *implPos, size_t *implLen) const
+{
+ if ( pos == npos )
+ *implPos = npos;
+ else
+ {
+ const_iterator i = begin() + pos;
+ *implPos = wxStringImpl::const_iterator(i.impl()) - m_impl.begin();
+ if ( len == npos )
+ *implLen = npos;
+ else
+ {
+ // too large length is interpreted as "to the end of the string"
+ // FIXME-UTF8: verify this is the case in std::string, assert
+ // otherwise
+ if ( pos + len > length() )
+ len = length() - pos;
+
+ *implLen = (i + len).impl() - i.impl();
+ }
+ }
+}
+
+#endif // wxUSE_UNICODE_UTF8
+
+// ----------------------------------------------------------------------------
+// wxCStrData converted strings caching
+// ----------------------------------------------------------------------------
+
+// FIXME-UTF8: temporarily disabled because it doesn't work with global
+// string objects; re-enable after fixing this bug and benchmarking
+// performance to see if using a hash is a good idea at all
+#if 0
+
+// For backward compatibility reasons, it must be possible to assign the value
+// returned by wxString::c_str() to a char* or wchar_t* variable and work with
+// it. Returning wxCharBuffer from (const char*)c_str() wouldn't do the trick,
+// because the memory would be freed immediately, but it has to be valid as long
+// as the string is not modified, so that code like this still works:
+//
+// const wxChar *s = str.c_str();
+// while ( s ) { ... }
+
+// FIXME-UTF8: not thread safe!
+// FIXME-UTF8: we currently clear the cached conversion only when the string is
+// destroyed, but we should do it when the string is modified, to
+// keep memory usage down
+// FIXME-UTF8: we do the conversion every time As[W]Char() is called, but if we
+// invalidated the cache on every change, we could keep the previous
+// conversion
+// FIXME-UTF8: add tracing of usage of these two methods - new code is supposed
+// to use mb_str() or wc_str() instead of (const [w]char*)c_str()
+
+template<typename T>
+static inline void DeleteStringFromConversionCache(T& hash, const wxString *s)
+{
+ typename T::iterator i = hash.find(wxConstCast(s, wxString));
+ if ( i != hash.end() )
+ {
+ free(i->second);
+ hash.erase(i);
+ }
+}
+
+#if wxUSE_UNICODE
+// NB: non-STL implementation doesn't compile with "const wxString*" key type,
+// so we have to use wxString* here and const-cast when used
+WX_DECLARE_HASH_MAP(wxString*, char*, wxPointerHash, wxPointerEqual,
+ wxStringCharConversionCache);
+static wxStringCharConversionCache gs_stringsCharCache;
+
+const char* wxCStrData::AsChar() const
+{
+ // remove previously cache value, if any (see FIXMEs above):
+ DeleteStringFromConversionCache(gs_stringsCharCache, m_str);
+
+ // convert the string and keep it:
+ const char *s = gs_stringsCharCache[wxConstCast(m_str, wxString)] =
+ m_str->mb_str().release();
+
+ return s + m_offset;
+}
+#endif // wxUSE_UNICODE
+
+#if !wxUSE_UNICODE_WCHAR
+WX_DECLARE_HASH_MAP(wxString*, wchar_t*, wxPointerHash, wxPointerEqual,
+ wxStringWCharConversionCache);
+static wxStringWCharConversionCache gs_stringsWCharCache;
+
+const wchar_t* wxCStrData::AsWChar() const
+{
+ // remove previously cache value, if any (see FIXMEs above):
+ DeleteStringFromConversionCache(gs_stringsWCharCache, m_str);
+
+ // convert the string and keep it:
+ const wchar_t *s = gs_stringsWCharCache[wxConstCast(m_str, wxString)] =
+ m_str->wc_str().release();
+
+ return s + m_offset;
+}
+#endif // !wxUSE_UNICODE_WCHAR
+
+wxString::~wxString()
+{
+#if wxUSE_UNICODE
+ // FIXME-UTF8: do this only if locale is not UTF8 if wxUSE_UNICODE_UTF8
+ DeleteStringFromConversionCache(gs_stringsCharCache, this);
+#endif
+#if !wxUSE_UNICODE_WCHAR
+ DeleteStringFromConversionCache(gs_stringsWCharCache, this);
+#endif
+}
+#endif
+
+#if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
+const char* wxCStrData::AsChar() const
+{
+#if wxUSE_UNICODE_UTF8
+ if ( wxLocaleIsUtf8 )
+ return AsInternal();
+#endif
+ // under non-UTF8 locales, we have to convert the internal UTF-8
+ // representation using wxConvLibc and cache the result
+
+ wxString *str = wxConstCast(m_str, wxString);
+
+ // convert the string:
+ wxCharBuffer buf(str->mb_str());
+
+ // FIXME-UTF8: do the conversion in-place in the existing buffer
+ if ( str->m_convertedToChar &&
+ strlen(buf) == strlen(str->m_convertedToChar) )
+ {
+ // keep the same buffer for as long as possible, so that several calls
+ // to c_str() in a row still work:
+ strcpy(str->m_convertedToChar, buf);
+ }
+ else
+ {
+ str->m_convertedToChar = buf.release();
+ }
+
+ // and keep it:
+ return str->m_convertedToChar + m_offset;
+}
+#endif // wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
+
+#if !wxUSE_UNICODE_WCHAR
+const wchar_t* wxCStrData::AsWChar() const
+{
+ wxString *str = wxConstCast(m_str, wxString);
+
+ // convert the string:
+ wxWCharBuffer buf(str->wc_str());
+
+ // FIXME-UTF8: do the conversion in-place in the existing buffer
+ if ( str->m_convertedToWChar &&
+ wxWcslen(buf) == wxWcslen(str->m_convertedToWChar) )
+ {
+ // keep the same buffer for as long as possible, so that several calls
+ // to c_str() in a row still work:
+ memcpy(str->m_convertedToWChar, buf, sizeof(wchar_t) * wxWcslen(buf));
+ }
+ else
+ {
+ str->m_convertedToWChar = buf.release();
+ }
+
+ // and keep it:
+ return str->m_convertedToWChar + m_offset;
+}
+#endif // !wxUSE_UNICODE_WCHAR
+
+// ===========================================================================
+// wxString class core
+// ===========================================================================
+