+ static wxScopedCharBuffer ImplStr(const char* str,
+ const wxMBConv& conv = wxConvLibc)
+ { return ConvertStr(str, npos, conv).data; }
+ static SubstrBufFromMB ImplStr(const char* str, size_t n,
+ const wxMBConv& conv = wxConvLibc)
+ { return ConvertStr(str, n, conv); }
+
+ static wxScopedCharBuffer ImplStr(const wchar_t* str)
+ { return ConvertStr(str, npos, wxMBConvUTF8()).data; }
+ static SubstrBufFromWC ImplStr(const wchar_t* str, size_t n)
+ { return ConvertStr(str, n, wxMBConvUTF8()); }
+
+#if wxUSE_STRING_POS_CACHE
+ // this is an extremely simple cache used by PosToImpl(): each cache element
+ // contains the string it applies to and the index corresponding to the last
+ // used position in this wxString in its m_impl string
+ //
+ // NB: notice that this struct (and nested Element one) must be a POD or we
+ // wouldn't be able to use a thread-local variable of this type, in
+ // particular it should have no ctor -- we rely on statics being
+ // initialized to 0 instead
+ struct Cache
+ {
+ enum { SIZE = 8 };
+
+ struct Element
+ {
+ const wxString *str; // the string to which this element applies
+ size_t pos, // the cached index in this string
+ impl, // the corresponding position in its m_impl
+ len; // cached length or npos if unknown
+
+ // reset cached index to 0
+ void ResetPos() { pos = impl = 0; }
+
+ // reset position and length
+ void Reset() { ResetPos(); len = npos; }
+ };
+
+ // cache the indices mapping for the last few string used
+ Element cached[SIZE];
+
+ // the last used index
+ unsigned lastUsed;
+ };
+
+#ifndef wxHAS_COMPILER_TLS
+ // we must use an accessor function and not a static variable when the TLS
+ // variables support is implemented in the library (and not by the compiler)
+ // because the global s_cache variable could be not yet initialized when a
+ // ctor of another global object is executed and if that ctor uses any
+ // wxString methods, bad things happen
+ //
+ // however notice that this approach does not work when compiler TLS is used,
+ // at least not with g++ 4.1.2 under amd64 as it apparently compiles code
+ // using this accessor incorrectly when optimizations are enabled (-O2 is
+ // enough) -- luckily we don't need it then neither as static __thread
+ // variables are initialized by 0 anyhow then and so we can use the variable
+ // directly
+ WXEXPORT static Cache& GetCache()
+ {
+ static wxTLS_TYPE(Cache) s_cache;
+
+ return wxTLS_VALUE(s_cache);
+ }
+
+ // this helper struct is used to ensure that GetCache() is called during
+ // static initialization time, i.e. before any threads creation, as otherwise
+ // the static s_cache construction inside GetCache() wouldn't be MT-safe
+ friend struct wxStrCacheInitializer;
+#else // wxHAS_COMPILER_TLS
+ static wxTLS_TYPE(Cache) ms_cache;
+ static Cache& GetCache() { return wxTLS_VALUE(ms_cache); }
+#endif // !wxHAS_COMPILER_TLS/wxHAS_COMPILER_TLS
+
+ static Cache::Element *GetCacheBegin() { return GetCache().cached; }
+ static Cache::Element *GetCacheEnd() { return GetCacheBegin() + Cache::SIZE; }
+ static unsigned& LastUsedCacheElement() { return GetCache().lastUsed; }
+
+ // this is used in debug builds only to provide a convenient function,
+ // callable from a debugger, to show the cache contents
+ friend struct wxStrCacheDumper;
+
+ // uncomment this to have access to some profiling statistics on program
+ // termination
+ //#define wxPROFILE_STRING_CACHE
+
+#ifdef wxPROFILE_STRING_CACHE
+ static struct PosToImplCacheStats
+ {
+ unsigned postot, // total non-trivial calls to PosToImpl
+ poshits, // cache hits from PosToImpl()
+ mishits, // cached position beyond the needed one
+ sumpos, // sum of all positions, used to compute the
+ // average position after dividing by postot
+ sumofs, // sum of all offsets after using the cache, used to
+ // compute the average after dividing by hits
+ lentot, // number of total calls to length()
+ lenhits; // number of cache hits in length()
+ } ms_cacheStats;
+
+ friend struct wxStrCacheStatsDumper;
+
+ #define wxCACHE_PROFILE_FIELD_INC(field) ms_cacheStats.field++
+ #define wxCACHE_PROFILE_FIELD_ADD(field, val) ms_cacheStats.field += (val)
+#else // !wxPROFILE_STRING_CACHE
+ #define wxCACHE_PROFILE_FIELD_INC(field)
+ #define wxCACHE_PROFILE_FIELD_ADD(field, val)
+#endif // wxPROFILE_STRING_CACHE/!wxPROFILE_STRING_CACHE
+
+ // note: it could seem that the functions below shouldn't be inline because
+ // they are big, contain loops and so the compiler shouldn't be able to
+ // inline them anyhow, however moving them into string.cpp does decrease the
+ // code performance by ~5%, at least when using g++ 4.1 so do keep them here
+ // unless tests show that it's not advantageous any more
+
+ // return the pointer to the cache element for this string or NULL if not
+ // cached
+ Cache::Element *FindCacheElement() const
+ {
+ // profiling seems to show a small but consistent gain if we use this
+ // simple loop instead of starting from the last used element (there are
+ // a lot of misses in this function...)
+ Cache::Element * const cacheBegin = GetCacheBegin();
+#ifndef wxHAS_COMPILER_TLS
+ // during destruction tls calls may return NULL, in this case return NULL
+ // immediately without accessing anything else
+ if ( cacheBegin == NULL )
+ return NULL;