1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxString class
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
13 Efficient string class [more or less] compatible with MFC CString,
14 wxWidgets version 1 wxString and std::string and some handy functions
15 missing from string.h.
18 #ifndef _WX_WXSTRING_H__
19 #define _WX_WXSTRING_H__
21 // ----------------------------------------------------------------------------
23 // ----------------------------------------------------------------------------
25 #include "wx/defs.h" // everybody should include this
27 #if defined(__WXMAC__) || defined(__VISAGECPP__)
31 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
32 // problem in VACPP V4 with including stdlib.h multiple times
33 // strconv includes it anyway
46 #include "wx/wxcrtbase.h" // for wxChar, wxStrlen() etc.
47 #include "wx/strvararg.h"
48 #include "wx/buffer.h" // for wxCharBuffer
49 #include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes
50 #include "wx/stringimpl.h"
51 #include "wx/stringops.h"
52 #include "wx/unichar.h"
54 // by default we cache the mapping of the positions in UTF-8 string to the byte
55 // offset as this results in noticeable performance improvements for loops over
56 // strings using indices; comment out this line to disable this
58 // notice that this optimization is well worth using even in debug builds as it
59 // changes asymptotic complexity of algorithms using indices to iterate over
60 // wxString back to expected linear from quadratic
62 // also notice that wxTLS_TYPE() (__declspec(thread) in this case) is unsafe to
63 // use in DLL build under pre-Vista Windows so we disable this code for now, if
64 // anybody really needs to use UTF-8 build under Windows with this optimization
65 // it would have to be re-tested and probably corrected
66 // CS: under OSX release builds the string destructor/cache cleanup sometimes
67 // crashes, disable until we find the true reason or a better workaround
68 #if wxUSE_UNICODE_UTF8 && !defined(__WINDOWS__) && !defined(__WXOSX__)
69 #define wxUSE_STRING_POS_CACHE 1
71 #define wxUSE_STRING_POS_CACHE 0
74 #if wxUSE_STRING_POS_CACHE
77 // change this 0 to 1 to enable additional (very expensive) asserts
78 // verifying that string caching logic works as expected
80 #define wxSTRING_CACHE_ASSERT(cond) wxASSERT(cond)
82 #define wxSTRING_CACHE_ASSERT(cond)
84 #endif // wxUSE_STRING_POS_CACHE
86 class WXDLLIMPEXP_FWD_BASE wxString
;
88 // unless this symbol is predefined to disable the compatibility functions, do
90 #ifndef WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
91 #define WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER 1
96 template <typename T
> struct wxStringAsBufHelper
;
99 // ---------------------------------------------------------------------------
101 // ---------------------------------------------------------------------------
103 // casts [unfortunately!] needed to call some broken functions which require
104 // "char *" instead of "const char *"
105 #define WXSTRINGCAST (wxChar *)(const wxChar *)
106 #define wxCSTRINGCAST (wxChar *)(const wxChar *)
107 #define wxMBSTRINGCAST (char *)(const char *)
108 #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *)
110 // ----------------------------------------------------------------------------
112 // ----------------------------------------------------------------------------
114 #if WXWIN_COMPATIBILITY_2_6
116 // deprecated in favour of wxString::npos, don't use in new code
118 // maximum possible length for a string means "take all string" everywhere
119 #define wxSTRING_MAXLEN wxString::npos
121 #endif // WXWIN_COMPATIBILITY_2_6
123 // ---------------------------------------------------------------------------
124 // global functions complementing standard C string library replacements for
125 // strlen() and portable strcasecmp()
126 //---------------------------------------------------------------------------
128 #if WXWIN_COMPATIBILITY_2_8
129 // Use wxXXX() functions from wxcrt.h instead! These functions are for
130 // backwards compatibility only.
132 // checks whether the passed in pointer is NULL and if the string is empty
133 wxDEPRECATED( inline bool IsEmpty(const char *p
) );
134 inline bool IsEmpty(const char *p
) { return (!p
|| !*p
); }
136 // safe version of strlen() (returns 0 if passed NULL pointer)
137 wxDEPRECATED( inline size_t Strlen(const char *psz
) );
138 inline size_t Strlen(const char *psz
)
139 { return psz
? strlen(psz
) : 0; }
141 // portable strcasecmp/_stricmp
142 wxDEPRECATED( inline int Stricmp(const char *psz1
, const char *psz2
) );
143 inline int Stricmp(const char *psz1
, const char *psz2
)
144 { return wxCRT_StricmpA(psz1
, psz2
); }
146 #endif // WXWIN_COMPATIBILITY_2_8
148 // ----------------------------------------------------------------------------
150 // ----------------------------------------------------------------------------
152 // Lightweight object returned by wxString::c_str() and implicitly convertible
153 // to either const char* or const wchar_t*.
157 // Ctors; for internal use by wxString and wxCStrData only
158 wxCStrData(const wxString
*str
, size_t offset
= 0, bool owned
= false)
159 : m_str(str
), m_offset(offset
), m_owned(owned
) {}
162 // Ctor constructs the object from char literal; they are needed to make
163 // operator?: compile and they intentionally take char*, not const char*
164 inline wxCStrData(char *buf
);
165 inline wxCStrData(wchar_t *buf
);
166 inline wxCStrData(const wxCStrData
& data
);
168 inline ~wxCStrData();
170 // AsWChar() and AsChar() can't be defined here as they use wxString and so
171 // must come after it and because of this won't be inlined when called from
172 // wxString methods (without a lot of work to extract these wxString methods
173 // from inside the class itself). But we still define them being inline
174 // below to let compiler inline them from elsewhere. And because of this we
175 // must declare them as inline here because otherwise some compilers give
176 // warnings about them, e.g. mingw32 3.4.5 warns about "<symbol> defined
177 // locally after being referenced with dllimport linkage" while IRIX
178 // mipsPro 7.4 warns about "function declared inline after being called".
179 inline const wchar_t* AsWChar() const;
180 operator const wchar_t*() const { return AsWChar(); }
182 inline const char* AsChar() const;
183 const unsigned char* AsUnsignedChar() const
184 { return (const unsigned char *) AsChar(); }
185 operator const char*() const { return AsChar(); }
186 operator const unsigned char*() const { return AsUnsignedChar(); }
188 operator const void*() const { return AsChar(); }
190 // returns buffers that are valid as long as the associated wxString exists
191 const wxScopedCharBuffer
AsCharBuf() const
193 return wxScopedCharBuffer::CreateNonOwned(AsChar());
196 const wxScopedWCharBuffer
AsWCharBuf() const
198 return wxScopedWCharBuffer::CreateNonOwned(AsWChar());
201 inline wxString
AsString() const;
203 // returns the value as C string in internal representation (equivalent
204 // to AsString().wx_str(), but more efficient)
205 const wxStringCharType
*AsInternal() const;
207 // allow expressions like "c_str()[0]":
208 inline wxUniChar
operator[](size_t n
) const;
209 wxUniChar
operator[](int n
) const { return operator[](size_t(n
)); }
210 wxUniChar
operator[](long n
) const { return operator[](size_t(n
)); }
211 #ifndef wxSIZE_T_IS_UINT
212 wxUniChar
operator[](unsigned int n
) const { return operator[](size_t(n
)); }
213 #endif // size_t != unsigned int
215 // These operators are needed to emulate the pointer semantics of c_str():
216 // expressions like "wxChar *p = str.c_str() + 1;" should continue to work
217 // (we need both versions to resolve ambiguities). Note that this means
218 // the 'n' value is interpreted as addition to char*/wchar_t* pointer, it
219 // is *not* number of Unicode characters in wxString.
220 wxCStrData
operator+(int n
) const
221 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
222 wxCStrData
operator+(long n
) const
223 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
224 wxCStrData
operator+(size_t n
) const
225 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
227 // and these for "str.c_str() + (p2 - p1)" (it also works for any integer
228 // expression but it must be ptrdiff_t and not e.g. int to work in this
230 wxCStrData
operator-(ptrdiff_t n
) const
232 wxASSERT_MSG( n
<= (ptrdiff_t)m_offset
,
233 wxT("attempt to construct address before the beginning of the string") );
234 return wxCStrData(m_str
, m_offset
- n
, m_owned
);
237 // this operator is needed to make expressions like "*c_str()" or
238 // "*(c_str() + 2)" work
239 inline wxUniChar
operator*() const;
242 // the wxString this object was returned for
243 const wxString
*m_str
;
244 // Offset into c_str() return value. Note that this is *not* offset in
245 // m_str in Unicode characters. Instead, it is index into the
246 // char*/wchar_t* buffer returned by c_str(). It's interpretation depends
247 // on how is the wxCStrData instance used: if it is eventually cast to
248 // const char*, m_offset will be in bytes form string's start; if it is
249 // cast to const wchar_t*, it will be in wchar_t values.
251 // should m_str be deleted, i.e. is it owned by us?
254 friend class WXDLLIMPEXP_FWD_BASE wxString
;
257 // ----------------------------------------------------------------------------
258 // wxStringPrintfMixin
259 // ---------------------------------------------------------------------------
261 // NB: VC6 has a bug that causes linker errors if you have template methods
262 // in a class using __declspec(dllimport). The solution is to split such
263 // class into two classes, one that contains the template methods and does
264 // *not* use WXDLLIMPEXP_BASE and another class that contains the rest
265 // (with DLL linkage).
267 // We only do this for VC6 here, because the code is less efficient
268 // (Printf() has to use dynamic_cast<>) and because OpenWatcom compiler
269 // cannot compile this code.
271 #if defined(__VISUALC__) && __VISUALC__ < 1300
272 #define wxNEEDS_WXSTRING_PRINTF_MIXIN
275 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
276 // this class contains implementation of wxString's vararg methods, it's
277 // exported from wxBase DLL
278 class WXDLLIMPEXP_BASE wxStringPrintfMixinBase
281 wxStringPrintfMixinBase() {}
283 #if !wxUSE_UTF8_LOCALE_ONLY
284 int DoPrintfWchar(const wxChar
*format
, ...);
285 static wxString
DoFormatWchar(const wxChar
*format
, ...);
287 #if wxUSE_UNICODE_UTF8
288 int DoPrintfUtf8(const char *format
, ...);
289 static wxString
DoFormatUtf8(const char *format
, ...);
293 // this class contains template wrappers for wxString's vararg methods, it's
294 // intentionally *not* exported from the DLL in order to fix the VC6 bug
296 class wxStringPrintfMixin
: public wxStringPrintfMixinBase
299 // to further complicate things, we can't return wxString from
300 // wxStringPrintfMixin::Format() because wxString is not yet declared at
301 // this point; the solution is to use this fake type trait template - this
302 // way the compiler won't know the return type until Format() is used
303 // (this doesn't compile with Watcom, but VC6 compiles it just fine):
304 template<typename T
> struct StringReturnType
306 typedef wxString type
;
310 // these are duplicated wxString methods, they're also declared below
311 // if !wxNEEDS_WXSTRING_PRINTF_MIXIN:
313 // static wxString Format(const wString& format, ...) WX_ATTRIBUTE_PRINTF_1;
314 WX_DEFINE_VARARG_FUNC_SANS_N0(static typename StringReturnType
<T1
>::type
,
315 Format
, 1, (const wxFormatString
&),
316 DoFormatWchar
, DoFormatUtf8
)
317 // We have to implement the version without template arguments manually
318 // because of the StringReturnType<> hack, although WX_DEFINE_VARARG_FUNC
319 // normally does it itself. It has to be a template so that we can use
320 // the hack, even though there's no real template parameter. We can't move
321 // it to wxStrig, because it would shadow these versions of Format() then.
323 inline static typename StringReturnType
<T
>::type
326 // NB: this doesn't compile if T is not (some form of) a string;
327 // this makes Format's prototype equivalent to
328 // Format(const wxFormatString& fmt)
329 return DoFormatWchar(wxFormatString(fmt
));
332 // int Printf(const wxString& format, ...);
333 WX_DEFINE_VARARG_FUNC(int, Printf
, 1, (const wxFormatString
&),
334 DoPrintfWchar
, DoPrintfUtf8
)
335 // int sprintf(const wxString& format, ...) WX_ATTRIBUTE_PRINTF_2;
336 WX_DEFINE_VARARG_FUNC(int, sprintf
, 1, (const wxFormatString
&),
337 DoPrintfWchar
, DoPrintfUtf8
)
340 wxStringPrintfMixin() : wxStringPrintfMixinBase() {}
342 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
345 // ----------------------------------------------------------------------------
346 // wxString: string class trying to be compatible with std::string, MFC
347 // CString and wxWindows 1.x wxString all at once
348 // ---------------------------------------------------------------------------
350 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
351 // "non dll-interface class 'wxStringPrintfMixin' used as base interface
352 // for dll-interface class 'wxString'" -- this is OK in our case
353 #pragma warning (push)
354 #pragma warning (disable:4275)
357 #if wxUSE_UNICODE_UTF8
358 // see the comment near wxString::iterator for why we need this
359 class WXDLLIMPEXP_BASE wxStringIteratorNode
362 wxStringIteratorNode()
363 : m_str(NULL
), m_citer(NULL
), m_iter(NULL
), m_prev(NULL
), m_next(NULL
) {}
364 wxStringIteratorNode(const wxString
*str
,
365 wxStringImpl::const_iterator
*citer
)
366 { DoSet(str
, citer
, NULL
); }
367 wxStringIteratorNode(const wxString
*str
, wxStringImpl::iterator
*iter
)
368 { DoSet(str
, NULL
, iter
); }
369 ~wxStringIteratorNode()
372 inline void set(const wxString
*str
, wxStringImpl::const_iterator
*citer
)
373 { clear(); DoSet(str
, citer
, NULL
); }
374 inline void set(const wxString
*str
, wxStringImpl::iterator
*iter
)
375 { clear(); DoSet(str
, NULL
, iter
); }
377 const wxString
*m_str
;
378 wxStringImpl::const_iterator
*m_citer
;
379 wxStringImpl::iterator
*m_iter
;
380 wxStringIteratorNode
*m_prev
, *m_next
;
384 inline void DoSet(const wxString
*str
,
385 wxStringImpl::const_iterator
*citer
,
386 wxStringImpl::iterator
*iter
);
388 // the node belongs to a particular iterator instance, it's not copied
389 // when a copy of the iterator is made
390 wxDECLARE_NO_COPY_CLASS(wxStringIteratorNode
);
392 #endif // wxUSE_UNICODE_UTF8
394 class WXDLLIMPEXP_BASE wxString
395 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
396 : public wxStringPrintfMixin
399 // NB: special care was taken in arranging the member functions in such order
400 // that all inline functions can be effectively inlined, verify that all
401 // performance critical functions are still inlined if you change order!
403 // an 'invalid' value for string index, moved to this place due to a CW bug
404 static const size_t npos
;
407 // if we hadn't made these operators private, it would be possible to
408 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
409 // converted to char in C and we do have operator=(char)
411 // NB: we don't need other versions (short/long and unsigned) as attempt
412 // to assign another numeric type to wxString will now result in
413 // ambiguity between operator=(char) and operator=(int)
414 wxString
& operator=(int);
416 // these methods are not implemented - there is _no_ conversion from int to
417 // string, you're doing something wrong if the compiler wants to call it!
419 // try `s << i' or `s.Printf("%d", i)' instead
423 // buffer for holding temporary substring when using any of the methods
424 // that take (char*,size_t) or (wchar_t*,size_t) arguments:
426 struct SubstrBufFromType
431 SubstrBufFromType(const T
& data_
, size_t len_
)
432 : data(data_
), len(len_
)
434 wxASSERT_MSG( len
!= npos
, "must have real length" );
438 #if wxUSE_UNICODE_UTF8
439 // even char* -> char* needs conversion, from locale charset to UTF-8
440 typedef SubstrBufFromType
<wxScopedCharBuffer
> SubstrBufFromWC
;
441 typedef SubstrBufFromType
<wxScopedCharBuffer
> SubstrBufFromMB
;
442 #elif wxUSE_UNICODE_WCHAR
443 typedef SubstrBufFromType
<const wchar_t*> SubstrBufFromWC
;
444 typedef SubstrBufFromType
<wxScopedWCharBuffer
> SubstrBufFromMB
;
446 typedef SubstrBufFromType
<const char*> SubstrBufFromMB
;
447 typedef SubstrBufFromType
<wxScopedCharBuffer
> SubstrBufFromWC
;
451 // Functions implementing primitive operations on string data; wxString
452 // methods and iterators are implemented in terms of it. The differences
453 // between UTF-8 and wchar_t* representations of the string are mostly
456 #if wxUSE_UNICODE_UTF8
457 static SubstrBufFromMB
ConvertStr(const char *psz
, size_t nLength
,
458 const wxMBConv
& conv
);
459 static SubstrBufFromWC
ConvertStr(const wchar_t *pwz
, size_t nLength
,
460 const wxMBConv
& conv
);
461 #elif wxUSE_UNICODE_WCHAR
462 static SubstrBufFromMB
ConvertStr(const char *psz
, size_t nLength
,
463 const wxMBConv
& conv
);
465 static SubstrBufFromWC
ConvertStr(const wchar_t *pwz
, size_t nLength
,
466 const wxMBConv
& conv
);
469 #if !wxUSE_UNICODE_UTF8 // wxUSE_UNICODE_WCHAR or !wxUSE_UNICODE
470 // returns C string encoded as the implementation expects:
472 static const wchar_t* ImplStr(const wchar_t* str
)
473 { return str
? str
: wxT(""); }
474 static const SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
475 { return SubstrBufFromWC(str
, (str
&& n
== npos
) ? wxWcslen(str
) : n
); }
476 static wxScopedWCharBuffer
ImplStr(const char* str
,
477 const wxMBConv
& conv
= wxConvLibc
)
478 { return ConvertStr(str
, npos
, conv
).data
; }
479 static SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
480 const wxMBConv
& conv
= wxConvLibc
)
481 { return ConvertStr(str
, n
, conv
); }
483 static const char* ImplStr(const char* str
,
484 const wxMBConv
& WXUNUSED(conv
) = wxConvLibc
)
485 { return str
? str
: ""; }
486 static const SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
487 const wxMBConv
& WXUNUSED(conv
) = wxConvLibc
)
488 { return SubstrBufFromMB(str
, (str
&& n
== npos
) ? wxStrlen(str
) : n
); }
489 static wxScopedCharBuffer
ImplStr(const wchar_t* str
)
490 { return ConvertStr(str
, npos
, wxConvLibc
).data
; }
491 static SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
492 { return ConvertStr(str
, n
, wxConvLibc
); }
495 // translates position index in wxString to/from index in underlying
497 static size_t PosToImpl(size_t pos
) { return pos
; }
498 static void PosLenToImpl(size_t pos
, size_t len
,
499 size_t *implPos
, size_t *implLen
)
500 { *implPos
= pos
; *implLen
= len
; }
501 static size_t LenToImpl(size_t len
) { return len
; }
502 static size_t PosFromImpl(size_t pos
) { return pos
; }
504 // we don't want to define these as empty inline functions as it could
505 // result in noticeable (and quite unnecessary in non-UTF-8 build) slowdown
506 // in debug build where the inline functions are not effectively inlined
507 #define wxSTRING_INVALIDATE_CACHE()
508 #define wxSTRING_INVALIDATE_CACHED_LENGTH()
509 #define wxSTRING_UPDATE_CACHED_LENGTH(n)
510 #define wxSTRING_SET_CACHED_LENGTH(n)
512 #else // wxUSE_UNICODE_UTF8
514 static wxScopedCharBuffer
ImplStr(const char* str
,
515 const wxMBConv
& conv
= wxConvLibc
)
516 { return ConvertStr(str
, npos
, conv
).data
; }
517 static SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
518 const wxMBConv
& conv
= wxConvLibc
)
519 { return ConvertStr(str
, n
, conv
); }
521 static wxScopedCharBuffer
ImplStr(const wchar_t* str
)
522 { return ConvertStr(str
, npos
, wxMBConvUTF8()).data
; }
523 static SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
524 { return ConvertStr(str
, n
, wxMBConvUTF8()); }
526 #if wxUSE_STRING_POS_CACHE
527 // this is an extremely simple cache used by PosToImpl(): each cache element
528 // contains the string it applies to and the index corresponding to the last
529 // used position in this wxString in its m_impl string
531 // NB: notice that this struct (and nested Element one) must be a POD or we
532 // wouldn't be able to use a thread-local variable of this type, in
533 // particular it should have no ctor -- we rely on statics being
534 // initialized to 0 instead
541 const wxString
*str
; // the string to which this element applies
542 size_t pos
, // the cached index in this string
543 impl
, // the corresponding position in its m_impl
544 len
; // cached length or npos if unknown
546 // reset cached index to 0
547 void ResetPos() { pos
= impl
= 0; }
549 // reset position and length
550 void Reset() { ResetPos(); len
= npos
; }
553 // cache the indices mapping for the last few string used
554 Element cached
[SIZE
];
556 // the last used index
560 #ifndef wxHAS_COMPILER_TLS
561 // we must use an accessor function and not a static variable when the TLS
562 // variables support is implemented in the library (and not by the compiler)
563 // because the global s_cache variable could be not yet initialized when a
564 // ctor of another global object is executed and if that ctor uses any
565 // wxString methods, bad things happen
567 // however notice that this approach does not work when compiler TLS is used,
568 // at least not with g++ 4.1.2 under amd64 as it apparently compiles code
569 // using this accessor incorrectly when optimizations are enabled (-O2 is
570 // enough) -- luckily we don't need it then neither as static __thread
571 // variables are initialized by 0 anyhow then and so we can use the variable
573 WXEXPORT
static Cache
& GetCache()
575 static wxTLS_TYPE(Cache
) s_cache
;
577 return wxTLS_VALUE(s_cache
);
580 // this helper struct is used to ensure that GetCache() is called during
581 // static initialization time, i.e. before any threads creation, as otherwise
582 // the static s_cache construction inside GetCache() wouldn't be MT-safe
583 friend struct wxStrCacheInitializer
;
584 #else // wxHAS_COMPILER_TLS
585 static wxTLS_TYPE(Cache
) ms_cache
;
586 static Cache
& GetCache() { return wxTLS_VALUE(ms_cache
); }
587 #endif // !wxHAS_COMPILER_TLS/wxHAS_COMPILER_TLS
589 static Cache::Element
*GetCacheBegin() { return GetCache().cached
; }
590 static Cache::Element
*GetCacheEnd() { return GetCacheBegin() + Cache::SIZE
; }
591 static unsigned& LastUsedCacheElement() { return GetCache().lastUsed
; }
593 // this is used in debug builds only to provide a convenient function,
594 // callable from a debugger, to show the cache contents
595 friend struct wxStrCacheDumper
;
597 // uncomment this to have access to some profiling statistics on program
599 //#define wxPROFILE_STRING_CACHE
601 #ifdef wxPROFILE_STRING_CACHE
602 static struct PosToImplCacheStats
604 unsigned postot
, // total non-trivial calls to PosToImpl
605 poshits
, // cache hits from PosToImpl()
606 mishits
, // cached position beyond the needed one
607 sumpos
, // sum of all positions, used to compute the
608 // average position after dividing by postot
609 sumofs
, // sum of all offsets after using the cache, used to
610 // compute the average after dividing by hits
611 lentot
, // number of total calls to length()
612 lenhits
; // number of cache hits in length()
615 friend struct wxStrCacheStatsDumper
;
617 #define wxCACHE_PROFILE_FIELD_INC(field) ms_cacheStats.field++
618 #define wxCACHE_PROFILE_FIELD_ADD(field, val) ms_cacheStats.field += (val)
619 #else // !wxPROFILE_STRING_CACHE
620 #define wxCACHE_PROFILE_FIELD_INC(field)
621 #define wxCACHE_PROFILE_FIELD_ADD(field, val)
622 #endif // wxPROFILE_STRING_CACHE/!wxPROFILE_STRING_CACHE
624 // note: it could seem that the functions below shouldn't be inline because
625 // they are big, contain loops and so the compiler shouldn't be able to
626 // inline them anyhow, however moving them into string.cpp does decrease the
627 // code performance by ~5%, at least when using g++ 4.1 so do keep them here
628 // unless tests show that it's not advantageous any more
630 // return the pointer to the cache element for this string or NULL if not
632 Cache::Element
*FindCacheElement() const
634 // profiling seems to show a small but consistent gain if we use this
635 // simple loop instead of starting from the last used element (there are
636 // a lot of misses in this function...)
637 Cache::Element
* const cacheBegin
= GetCacheBegin();
638 #ifndef wxHAS_COMPILER_TLS
639 // during destruction tls calls may return NULL, in this case return NULL
640 // immediately without accessing anything else
641 if ( cacheBegin
== NULL
)
644 Cache::Element
* const cacheEnd
= GetCacheEnd();
645 for ( Cache::Element
*c
= cacheBegin
; c
!= cacheEnd
; c
++ )
647 if ( c
->str
== this )
654 // unlike FindCacheElement(), this one always returns a valid pointer to the
655 // cache element for this string, it may have valid last cached position and
656 // its corresponding index in the byte string or not
657 Cache::Element
*GetCacheElement() const
659 Cache::Element
* const cacheBegin
= GetCacheBegin();
660 Cache::Element
* const cacheEnd
= GetCacheEnd();
661 Cache::Element
* const cacheStart
= cacheBegin
+ LastUsedCacheElement();
663 // check the last used first, this does no (measurable) harm for a miss
664 // but does help for simple loops addressing the same string all the time
665 if ( cacheStart
->str
== this )
668 // notice that we're going to check cacheStart again inside this call but
669 // profiling shows that it's still faster to use a simple loop like
670 // inside FindCacheElement() than manually looping with wrapping starting
671 // from the cache entry after the start one
672 Cache::Element
*c
= FindCacheElement();
675 // claim the next cache entry for this string
677 if ( ++c
== cacheEnd
)
683 // and remember the last used element
684 LastUsedCacheElement() = c
- cacheBegin
;
690 size_t DoPosToImpl(size_t pos
) const
692 wxCACHE_PROFILE_FIELD_INC(postot
);
694 // NB: although the case of pos == 1 (and offset from cached position
695 // equal to 1) are common, nothing is gained by writing special code
696 // for handling them, the compiler (at least g++ 4.1 used) seems to
697 // optimize the code well enough on its own
699 wxCACHE_PROFILE_FIELD_ADD(sumpos
, pos
);
701 Cache::Element
* const cache
= GetCacheElement();
703 // cached position can't be 0 so if it is, it means that this entry was
704 // used for length caching only so far, i.e. it doesn't count as a hit
705 // from our point of view
708 wxCACHE_PROFILE_FIELD_INC(poshits
);
711 if ( pos
== cache
->pos
)
714 // this seems to happen only rarely so just reset the cache in this case
715 // instead of complicating code even further by seeking backwards in this
717 if ( cache
->pos
> pos
)
719 wxCACHE_PROFILE_FIELD_INC(mishits
);
724 wxCACHE_PROFILE_FIELD_ADD(sumofs
, pos
- cache
->pos
);
727 wxStringImpl::const_iterator
i(m_impl
.begin() + cache
->impl
);
728 for ( size_t n
= cache
->pos
; n
< pos
; n
++ )
729 wxStringOperations::IncIter(i
);
732 cache
->impl
= i
- m_impl
.begin();
734 wxSTRING_CACHE_ASSERT(
735 (int)cache
->impl
== (begin() + pos
).impl() - m_impl
.begin() );
740 void InvalidateCache()
742 Cache::Element
* const cache
= FindCacheElement();
747 void InvalidateCachedLength()
749 Cache::Element
* const cache
= FindCacheElement();
754 void SetCachedLength(size_t len
)
756 // we optimistically cache the length here even if the string wasn't
757 // present in the cache before, this seems to do no harm and the
758 // potential for avoiding length recomputation for long strings looks
760 GetCacheElement()->len
= len
;
763 void UpdateCachedLength(ptrdiff_t delta
)
765 Cache::Element
* const cache
= FindCacheElement();
766 if ( cache
&& cache
->len
!= npos
)
768 wxSTRING_CACHE_ASSERT( (ptrdiff_t)cache
->len
+ delta
>= 0 );
774 #define wxSTRING_INVALIDATE_CACHE() InvalidateCache()
775 #define wxSTRING_INVALIDATE_CACHED_LENGTH() InvalidateCachedLength()
776 #define wxSTRING_UPDATE_CACHED_LENGTH(n) UpdateCachedLength(n)
777 #define wxSTRING_SET_CACHED_LENGTH(n) SetCachedLength(n)
778 #else // !wxUSE_STRING_POS_CACHE
779 size_t DoPosToImpl(size_t pos
) const
781 return (begin() + pos
).impl() - m_impl
.begin();
784 #define wxSTRING_INVALIDATE_CACHE()
785 #define wxSTRING_INVALIDATE_CACHED_LENGTH()
786 #define wxSTRING_UPDATE_CACHED_LENGTH(n)
787 #define wxSTRING_SET_CACHED_LENGTH(n)
788 #endif // wxUSE_STRING_POS_CACHE/!wxUSE_STRING_POS_CACHE
790 size_t PosToImpl(size_t pos
) const
792 return pos
== 0 || pos
== npos
? pos
: DoPosToImpl(pos
);
795 void PosLenToImpl(size_t pos
, size_t len
, size_t *implPos
, size_t *implLen
) const;
797 size_t LenToImpl(size_t len
) const
800 PosLenToImpl(0, len
, &pos
, &len2
);
804 size_t PosFromImpl(size_t pos
) const
806 if ( pos
== 0 || pos
== npos
)
809 return const_iterator(this, m_impl
.begin() + pos
) - begin();
811 #endif // !wxUSE_UNICODE_UTF8/wxUSE_UNICODE_UTF8
815 typedef wxUniChar value_type
;
816 typedef wxUniChar char_type
;
817 typedef wxUniCharRef reference
;
818 typedef wxChar
* pointer
;
819 typedef const wxChar
* const_pointer
;
821 typedef size_t size_type
;
822 typedef wxUniChar const_reference
;
825 #if wxUSE_UNICODE_UTF8
826 // random access is not O(1), as required by Random Access Iterator
827 #define WX_STR_ITERATOR_TAG std::bidirectional_iterator_tag
829 #define WX_STR_ITERATOR_TAG std::random_access_iterator_tag
831 #define WX_DEFINE_ITERATOR_CATEGORY(cat) typedef cat iterator_category;
833 // not defining iterator_category at all in this case is better than defining
834 // it as some dummy type -- at least it results in more intelligible error
836 #define WX_DEFINE_ITERATOR_CATEGORY(cat)
839 #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, reference_type) \
841 typedef wxStringImpl::iterator_name underlying_iterator; \
843 WX_DEFINE_ITERATOR_CATEGORY(WX_STR_ITERATOR_TAG) \
844 typedef wxUniChar value_type; \
845 typedef ptrdiff_t difference_type; \
846 typedef reference_type reference; \
847 typedef pointer_type pointer; \
849 reference operator[](size_t n) const { return *(*this + n); } \
851 iterator_name& operator++() \
852 { wxStringOperations::IncIter(m_cur); return *this; } \
853 iterator_name& operator--() \
854 { wxStringOperations::DecIter(m_cur); return *this; } \
855 iterator_name operator++(int) \
857 iterator_name tmp = *this; \
858 wxStringOperations::IncIter(m_cur); \
861 iterator_name operator--(int) \
863 iterator_name tmp = *this; \
864 wxStringOperations::DecIter(m_cur); \
868 iterator_name& operator+=(ptrdiff_t n) \
870 m_cur = wxStringOperations::AddToIter(m_cur, n); \
873 iterator_name& operator-=(ptrdiff_t n) \
875 m_cur = wxStringOperations::AddToIter(m_cur, -n); \
879 difference_type operator-(const iterator_name& i) const \
880 { return wxStringOperations::DiffIters(m_cur, i.m_cur); } \
882 bool operator==(const iterator_name& i) const \
883 { return m_cur == i.m_cur; } \
884 bool operator!=(const iterator_name& i) const \
885 { return m_cur != i.m_cur; } \
887 bool operator<(const iterator_name& i) const \
888 { return m_cur < i.m_cur; } \
889 bool operator>(const iterator_name& i) const \
890 { return m_cur > i.m_cur; } \
891 bool operator<=(const iterator_name& i) const \
892 { return m_cur <= i.m_cur; } \
893 bool operator>=(const iterator_name& i) const \
894 { return m_cur >= i.m_cur; } \
897 /* for internal wxString use only: */ \
898 underlying_iterator impl() const { return m_cur; } \
900 friend class wxString; \
901 friend class wxCStrData; \
904 underlying_iterator m_cur
906 class WXDLLIMPEXP_FWD_BASE const_iterator
;
908 #if wxUSE_UNICODE_UTF8
909 // NB: In UTF-8 build, (non-const) iterator needs to keep reference
910 // to the underlying wxStringImpl, because UTF-8 is variable-length
911 // encoding and changing the value pointer to by an iterator (using
912 // its operator*) requires calling wxStringImpl::replace() if the old
913 // and new values differ in their encoding's length.
915 // Furthermore, the replace() call may invalid all iterators for the
916 // string, so we have to keep track of outstanding iterators and update
917 // them if replace() happens.
919 // This is implemented by maintaining linked list of iterators for every
920 // string and traversing it in wxUniCharRef::operator=(). Head of the
921 // list is stored in wxString. (FIXME-UTF8)
923 class WXDLLIMPEXP_BASE iterator
925 WX_STR_ITERATOR_IMPL(iterator
, wxChar
*, wxUniCharRef
);
929 iterator(const iterator
& i
)
930 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
931 iterator
& operator=(const iterator
& i
)
936 m_node
.set(i
.str(), &m_cur
);
941 reference
operator*()
942 { return wxUniCharRef::CreateForString(*str(), m_cur
); }
944 iterator
operator+(ptrdiff_t n
) const
945 { return iterator(str(), wxStringOperations::AddToIter(m_cur
, n
)); }
946 iterator
operator-(ptrdiff_t n
) const
947 { return iterator(str(), wxStringOperations::AddToIter(m_cur
, -n
)); }
949 // Normal iterators need to be comparable with the const_iterators so
950 // declare the comparison operators and implement them below after the
951 // full const_iterator declaration.
952 bool operator==(const const_iterator
& i
) const;
953 bool operator!=(const const_iterator
& i
) const;
954 bool operator<(const const_iterator
& i
) const;
955 bool operator>(const const_iterator
& i
) const;
956 bool operator<=(const const_iterator
& i
) const;
957 bool operator>=(const const_iterator
& i
) const;
960 iterator(wxString
*wxstr
, underlying_iterator ptr
)
961 : m_cur(ptr
), m_node(wxstr
, &m_cur
) {}
963 wxString
* str() const { return const_cast<wxString
*>(m_node
.m_str
); }
965 wxStringIteratorNode m_node
;
967 friend class const_iterator
;
970 class WXDLLIMPEXP_BASE const_iterator
972 // NB: reference_type is intentionally value, not reference, the character
973 // may be encoded differently in wxString data:
974 WX_STR_ITERATOR_IMPL(const_iterator
, const wxChar
*, wxUniChar
);
978 const_iterator(const const_iterator
& i
)
979 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
980 const_iterator(const iterator
& i
)
981 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
983 const_iterator
& operator=(const const_iterator
& i
)
988 m_node
.set(i
.str(), &m_cur
);
992 const_iterator
& operator=(const iterator
& i
)
993 { m_cur
= i
.m_cur
; m_node
.set(i
.str(), &m_cur
); return *this; }
995 reference
operator*() const
996 { return wxStringOperations::DecodeChar(m_cur
); }
998 const_iterator
operator+(ptrdiff_t n
) const
999 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur
, n
)); }
1000 const_iterator
operator-(ptrdiff_t n
) const
1001 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur
, -n
)); }
1003 // Notice that comparison operators taking non-const iterator are not
1004 // needed here because of the implicit conversion from non-const iterator
1005 // to const ones ensure that the versions for const_iterator declared
1006 // inside WX_STR_ITERATOR_IMPL can be used.
1009 // for internal wxString use only:
1010 const_iterator(const wxString
*wxstr
, underlying_iterator ptr
)
1011 : m_cur(ptr
), m_node(wxstr
, &m_cur
) {}
1013 const wxString
* str() const { return m_node
.m_str
; }
1015 wxStringIteratorNode m_node
;
1018 size_t IterToImplPos(wxString::iterator i
) const
1019 { return wxStringImpl::const_iterator(i
.impl()) - m_impl
.begin(); }
1021 iterator
GetIterForNthChar(size_t n
)
1022 { return iterator(this, m_impl
.begin() + PosToImpl(n
)); }
1023 const_iterator
GetIterForNthChar(size_t n
) const
1024 { return const_iterator(this, m_impl
.begin() + PosToImpl(n
)); }
1025 #else // !wxUSE_UNICODE_UTF8
1027 class WXDLLIMPEXP_BASE iterator
1029 WX_STR_ITERATOR_IMPL(iterator
, wxChar
*, wxUniCharRef
);
1033 iterator(const iterator
& i
) : m_cur(i
.m_cur
) {}
1035 reference
operator*()
1036 { return wxUniCharRef::CreateForString(m_cur
); }
1038 iterator
operator+(ptrdiff_t n
) const
1039 { return iterator(wxStringOperations::AddToIter(m_cur
, n
)); }
1040 iterator
operator-(ptrdiff_t n
) const
1041 { return iterator(wxStringOperations::AddToIter(m_cur
, -n
)); }
1043 // As in UTF-8 case above, define comparison operators taking
1044 // const_iterator too.
1045 bool operator==(const const_iterator
& i
) const;
1046 bool operator!=(const const_iterator
& i
) const;
1047 bool operator<(const const_iterator
& i
) const;
1048 bool operator>(const const_iterator
& i
) const;
1049 bool operator<=(const const_iterator
& i
) const;
1050 bool operator>=(const const_iterator
& i
) const;
1053 // for internal wxString use only:
1054 iterator(underlying_iterator ptr
) : m_cur(ptr
) {}
1055 iterator(wxString
*WXUNUSED(str
), underlying_iterator ptr
) : m_cur(ptr
) {}
1057 friend class const_iterator
;
1060 class WXDLLIMPEXP_BASE const_iterator
1062 // NB: reference_type is intentionally value, not reference, the character
1063 // may be encoded differently in wxString data:
1064 WX_STR_ITERATOR_IMPL(const_iterator
, const wxChar
*, wxUniChar
);
1068 const_iterator(const const_iterator
& i
) : m_cur(i
.m_cur
) {}
1069 const_iterator(const iterator
& i
) : m_cur(i
.m_cur
) {}
1071 reference
operator*() const
1072 { return wxStringOperations::DecodeChar(m_cur
); }
1074 const_iterator
operator+(ptrdiff_t n
) const
1075 { return const_iterator(wxStringOperations::AddToIter(m_cur
, n
)); }
1076 const_iterator
operator-(ptrdiff_t n
) const
1077 { return const_iterator(wxStringOperations::AddToIter(m_cur
, -n
)); }
1079 // As in UTF-8 case above, we don't need comparison operators taking
1080 // iterator because we have an implicit conversion from iterator to
1081 // const_iterator so the operators declared by WX_STR_ITERATOR_IMPL will
1085 // for internal wxString use only:
1086 const_iterator(underlying_iterator ptr
) : m_cur(ptr
) {}
1087 const_iterator(const wxString
*WXUNUSED(str
), underlying_iterator ptr
)
1091 iterator
GetIterForNthChar(size_t n
) { return begin() + n
; }
1092 const_iterator
GetIterForNthChar(size_t n
) const { return begin() + n
; }
1093 #endif // wxUSE_UNICODE_UTF8/!wxUSE_UNICODE_UTF8
1095 #undef WX_STR_ITERATOR_TAG
1096 #undef WX_STR_ITERATOR_IMPL
1098 // This method is mostly used by wxWidgets itself and return the offset of
1099 // the given iterator in bytes relative to the start of the buffer
1100 // representing the current string contents in the current locale encoding.
1102 // It is inefficient as it involves converting part of the string to this
1103 // encoding (and also unsafe as it simply returns 0 if the conversion fails)
1104 // and so should be avoided if possible, wx itself only uses it to implement
1105 // backwards-compatible API.
1106 ptrdiff_t IterOffsetInMBStr(const const_iterator
& i
) const
1108 const wxString
str(begin(), i
);
1110 // This is logically equivalent to strlen(str.mb_str()) but avoids
1111 // actually converting the string to multibyte and just computes the
1112 // length that it would have after conversion.
1113 size_t ofs
= wxConvLibc
.FromWChar(NULL
, 0, str
.wc_str(), str
.length());
1114 return ofs
== wxCONV_FAILED
? 0 : static_cast<ptrdiff_t>(ofs
);
1117 friend class iterator
;
1118 friend class const_iterator
;
1120 template <typename T
>
1121 class reverse_iterator_impl
1124 typedef T iterator_type
;
1126 WX_DEFINE_ITERATOR_CATEGORY(typename
T::iterator_category
)
1127 typedef typename
T::value_type value_type
;
1128 typedef typename
T::difference_type difference_type
;
1129 typedef typename
T::reference reference
;
1130 typedef typename
T::pointer
*pointer
;
1132 reverse_iterator_impl() {}
1133 reverse_iterator_impl(iterator_type i
) : m_cur(i
) {}
1134 reverse_iterator_impl(const reverse_iterator_impl
& ri
)
1135 : m_cur(ri
.m_cur
) {}
1137 iterator_type
base() const { return m_cur
; }
1139 reference
operator*() const { return *(m_cur
-1); }
1140 reference
operator[](size_t n
) const { return *(*this + n
); }
1142 reverse_iterator_impl
& operator++()
1143 { --m_cur
; return *this; }
1144 reverse_iterator_impl
operator++(int)
1145 { reverse_iterator_impl tmp
= *this; --m_cur
; return tmp
; }
1146 reverse_iterator_impl
& operator--()
1147 { ++m_cur
; return *this; }
1148 reverse_iterator_impl
operator--(int)
1149 { reverse_iterator_impl tmp
= *this; ++m_cur
; return tmp
; }
1151 // NB: explicit <T> in the functions below is to keep BCC 5.5 happy
1152 reverse_iterator_impl
operator+(ptrdiff_t n
) const
1153 { return reverse_iterator_impl
<T
>(m_cur
- n
); }
1154 reverse_iterator_impl
operator-(ptrdiff_t n
) const
1155 { return reverse_iterator_impl
<T
>(m_cur
+ n
); }
1156 reverse_iterator_impl
operator+=(ptrdiff_t n
)
1157 { m_cur
-= n
; return *this; }
1158 reverse_iterator_impl
operator-=(ptrdiff_t n
)
1159 { m_cur
+= n
; return *this; }
1161 unsigned operator-(const reverse_iterator_impl
& i
) const
1162 { return i
.m_cur
- m_cur
; }
1164 bool operator==(const reverse_iterator_impl
& ri
) const
1165 { return m_cur
== ri
.m_cur
; }
1166 bool operator!=(const reverse_iterator_impl
& ri
) const
1167 { return !(*this == ri
); }
1169 bool operator<(const reverse_iterator_impl
& i
) const
1170 { return m_cur
> i
.m_cur
; }
1171 bool operator>(const reverse_iterator_impl
& i
) const
1172 { return m_cur
< i
.m_cur
; }
1173 bool operator<=(const reverse_iterator_impl
& i
) const
1174 { return m_cur
>= i
.m_cur
; }
1175 bool operator>=(const reverse_iterator_impl
& i
) const
1176 { return m_cur
<= i
.m_cur
; }
1179 iterator_type m_cur
;
1182 typedef reverse_iterator_impl
<iterator
> reverse_iterator
;
1183 typedef reverse_iterator_impl
<const_iterator
> const_reverse_iterator
;
1186 // used to transform an expression built using c_str() (and hence of type
1187 // wxCStrData) to an iterator into the string
1188 static const_iterator
CreateConstIterator(const wxCStrData
& data
)
1190 return const_iterator(data
.m_str
,
1191 (data
.m_str
->begin() + data
.m_offset
).impl());
1194 // in UTF-8 STL build, creation from std::string requires conversion under
1195 // non-UTF8 locales, so we can't have and use wxString(wxStringImpl) ctor;
1196 // instead we define dummy type that lets us have wxString ctor for creation
1197 // from wxStringImpl that couldn't be used by user code (in all other builds,
1198 // "standard" ctors can be used):
1199 #if wxUSE_UNICODE_UTF8 && wxUSE_STL_BASED_WXSTRING
1200 struct CtorFromStringImplTag
{};
1202 wxString(CtorFromStringImplTag
* WXUNUSED(dummy
), const wxStringImpl
& src
)
1205 static wxString
FromImpl(const wxStringImpl
& src
)
1206 { return wxString((CtorFromStringImplTag
*)NULL
, src
); }
1208 #if !wxUSE_STL_BASED_WXSTRING
1209 wxString(const wxStringImpl
& src
) : m_impl(src
) { }
1210 // else: already defined as wxString(wxStdString) below
1212 static wxString
FromImpl(const wxStringImpl
& src
) { return wxString(src
); }
1216 // constructors and destructor
1217 // ctor for an empty string
1221 wxString(const wxString
& stringSrc
) : m_impl(stringSrc
.m_impl
) { }
1223 // string containing nRepeat copies of ch
1224 wxString(wxUniChar ch
, size_t nRepeat
= 1 )
1225 { assign(nRepeat
, ch
); }
1226 wxString(size_t nRepeat
, wxUniChar ch
)
1227 { assign(nRepeat
, ch
); }
1228 wxString(wxUniCharRef ch
, size_t nRepeat
= 1)
1229 { assign(nRepeat
, ch
); }
1230 wxString(size_t nRepeat
, wxUniCharRef ch
)
1231 { assign(nRepeat
, ch
); }
1232 wxString(char ch
, size_t nRepeat
= 1)
1233 { assign(nRepeat
, ch
); }
1234 wxString(size_t nRepeat
, char ch
)
1235 { assign(nRepeat
, ch
); }
1236 wxString(wchar_t ch
, size_t nRepeat
= 1)
1237 { assign(nRepeat
, ch
); }
1238 wxString(size_t nRepeat
, wchar_t ch
)
1239 { assign(nRepeat
, ch
); }
1241 // ctors from char* strings:
1242 wxString(const char *psz
)
1243 : m_impl(ImplStr(psz
)) {}
1244 wxString(const char *psz
, const wxMBConv
& conv
)
1245 : m_impl(ImplStr(psz
, conv
)) {}
1246 wxString(const char *psz
, size_t nLength
)
1247 { assign(psz
, nLength
); }
1248 wxString(const char *psz
, const wxMBConv
& conv
, size_t nLength
)
1250 SubstrBufFromMB
str(ImplStr(psz
, nLength
, conv
));
1251 m_impl
.assign(str
.data
, str
.len
);
1254 // and unsigned char*:
1255 wxString(const unsigned char *psz
)
1256 : m_impl(ImplStr((const char*)psz
)) {}
1257 wxString(const unsigned char *psz
, const wxMBConv
& conv
)
1258 : m_impl(ImplStr((const char*)psz
, conv
)) {}
1259 wxString(const unsigned char *psz
, size_t nLength
)
1260 { assign((const char*)psz
, nLength
); }
1261 wxString(const unsigned char *psz
, const wxMBConv
& conv
, size_t nLength
)
1263 SubstrBufFromMB
str(ImplStr((const char*)psz
, nLength
, conv
));
1264 m_impl
.assign(str
.data
, str
.len
);
1267 // ctors from wchar_t* strings:
1268 wxString(const wchar_t *pwz
)
1269 : m_impl(ImplStr(pwz
)) {}
1270 wxString(const wchar_t *pwz
, const wxMBConv
& WXUNUSED(conv
))
1271 : m_impl(ImplStr(pwz
)) {}
1272 wxString(const wchar_t *pwz
, size_t nLength
)
1273 { assign(pwz
, nLength
); }
1274 wxString(const wchar_t *pwz
, const wxMBConv
& WXUNUSED(conv
), size_t nLength
)
1275 { assign(pwz
, nLength
); }
1277 wxString(const wxScopedCharBuffer
& buf
)
1278 { assign(buf
.data(), buf
.length()); }
1279 wxString(const wxScopedWCharBuffer
& buf
)
1280 { assign(buf
.data(), buf
.length()); }
1282 // NB: this version uses m_impl.c_str() to force making a copy of the
1283 // string, so that "wxString(str.c_str())" idiom for passing strings
1284 // between threads works
1285 wxString(const wxCStrData
& cstr
)
1286 : m_impl(cstr
.AsString().m_impl
.c_str()) { }
1288 // as we provide both ctors with this signature for both char and unsigned
1289 // char string, we need to provide one for wxCStrData to resolve ambiguity
1290 wxString(const wxCStrData
& cstr
, size_t nLength
)
1291 : m_impl(cstr
.AsString().Mid(0, nLength
).m_impl
) {}
1293 // and because wxString is convertible to wxCStrData and const wxChar *
1294 // we also need to provide this one
1295 wxString(const wxString
& str
, size_t nLength
)
1296 { assign(str
, nLength
); }
1299 #if wxUSE_STRING_POS_CACHE
1302 // we need to invalidate our cache entry as another string could be
1303 // recreated at the same address (unlikely, but still possible, with the
1304 // heap-allocated strings but perfectly common with stack-allocated ones)
1307 #endif // wxUSE_STRING_POS_CACHE
1309 // even if we're not built with wxUSE_STD_STRING_CONV_IN_WXSTRING == 1 it is
1310 // very convenient to allow implicit conversions from std::string to wxString
1311 // and vice verse as this allows to use the same strings in non-GUI and GUI
1312 // code, however we don't want to unconditionally add this ctor as it would
1313 // make wx lib dependent on libstdc++ on some Linux versions which is bad, so
1314 // instead we ask the client code to define this wxUSE_STD_STRING symbol if
1316 #if wxUSE_STD_STRING
1317 #if wxUSE_UNICODE_WCHAR
1318 wxString(const wxStdWideString
& str
) : m_impl(str
) {}
1319 #else // UTF-8 or ANSI
1320 wxString(const wxStdWideString
& str
)
1321 { assign(str
.c_str(), str
.length()); }
1324 #if !wxUSE_UNICODE // ANSI build
1325 // FIXME-UTF8: do this in UTF8 build #if wxUSE_UTF8_LOCALE_ONLY, too
1326 wxString(const std::string
& str
) : m_impl(str
) {}
1328 wxString(const std::string
& str
)
1329 { assign(str
.c_str(), str
.length()); }
1331 #endif // wxUSE_STD_STRING
1333 // Also always provide explicit conversions to std::[w]string in any case,
1334 // see below for the implicit ones.
1335 #if wxUSE_STD_STRING
1336 // We can avoid a copy if we already use this string type internally,
1337 // otherwise we create a copy on the fly:
1338 #if wxUSE_UNICODE_WCHAR && wxUSE_STL_BASED_WXSTRING
1339 #define wxStringToStdWstringRetType const wxStdWideString&
1340 const wxStdWideString
& ToStdWstring() const { return m_impl
; }
1342 // wxStringImpl is either not std::string or needs conversion
1343 #define wxStringToStdWstringRetType wxStdWideString
1344 wxStdWideString
ToStdWstring() const
1346 #if wxUSE_UNICODE_WCHAR
1347 wxScopedWCharBuffer buf
=
1348 wxScopedWCharBuffer::CreateNonOwned(m_impl
.c_str(), m_impl
.length());
1349 #else // !wxUSE_UNICODE_WCHAR
1350 wxScopedWCharBuffer
buf(wc_str());
1353 return wxStdWideString(buf
.data(), buf
.length());
1357 #if (!wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY) && wxUSE_STL_BASED_WXSTRING
1358 // wxStringImpl is std::string in the encoding we want
1359 #define wxStringToStdStringRetType const std::string&
1360 const std::string
& ToStdString() const { return m_impl
; }
1362 // wxStringImpl is either not std::string or needs conversion
1363 #define wxStringToStdStringRetType std::string
1364 std::string
ToStdString() const
1366 wxScopedCharBuffer
buf(mb_str());
1367 return std::string(buf
.data(), buf
.length());
1371 #if wxUSE_STD_STRING_CONV_IN_WXSTRING
1372 // Implicit conversions to std::[w]string are not provided by default as
1373 // they conflict with the implicit conversions to "const char/wchar_t *"
1374 // which we use for backwards compatibility but do provide them if
1375 // explicitly requested.
1376 operator wxStringToStdStringRetType() const { return ToStdString(); }
1377 operator wxStringToStdWstringRetType() const { return ToStdWstring(); }
1378 #endif // wxUSE_STD_STRING_CONV_IN_WXSTRING
1380 #undef wxStringToStdStringRetType
1381 #undef wxStringToStdWstringRetType
1383 #endif // wxUSE_STD_STRING
1385 wxString
Clone() const
1387 // make a deep copy of the string, i.e. the returned string will have
1388 // ref count = 1 with refcounted implementation
1389 return wxString::FromImpl(wxStringImpl(m_impl
.c_str(), m_impl
.length()));
1392 // first valid index position
1393 const_iterator
begin() const { return const_iterator(this, m_impl
.begin()); }
1394 iterator
begin() { return iterator(this, m_impl
.begin()); }
1395 // position one after the last valid one
1396 const_iterator
end() const { return const_iterator(this, m_impl
.end()); }
1397 iterator
end() { return iterator(this, m_impl
.end()); }
1399 // first element of the reversed string
1400 const_reverse_iterator
rbegin() const
1401 { return const_reverse_iterator(end()); }
1402 reverse_iterator
rbegin()
1403 { return reverse_iterator(end()); }
1404 // one beyond the end of the reversed string
1405 const_reverse_iterator
rend() const
1406 { return const_reverse_iterator(begin()); }
1407 reverse_iterator
rend()
1408 { return reverse_iterator(begin()); }
1410 // std::string methods:
1411 #if wxUSE_UNICODE_UTF8
1412 size_t length() const
1414 #if wxUSE_STRING_POS_CACHE
1415 wxCACHE_PROFILE_FIELD_INC(lentot
);
1417 Cache::Element
* const cache
= GetCacheElement();
1419 if ( cache
->len
== npos
)
1421 // it's probably not worth trying to be clever and using cache->pos
1422 // here as it's probably 0 anyhow -- you usually call length() before
1423 // starting to index the string
1424 cache
->len
= end() - begin();
1428 wxCACHE_PROFILE_FIELD_INC(lenhits
);
1430 wxSTRING_CACHE_ASSERT( (int)cache
->len
== end() - begin() );
1434 #else // !wxUSE_STRING_POS_CACHE
1435 return end() - begin();
1436 #endif // wxUSE_STRING_POS_CACHE/!wxUSE_STRING_POS_CACHE
1439 size_t length() const { return m_impl
.length(); }
1442 size_type
size() const { return length(); }
1443 size_type
max_size() const { return npos
; }
1445 bool empty() const { return m_impl
.empty(); }
1447 // NB: these methods don't have a well-defined meaning in UTF-8 case
1448 size_type
capacity() const { return m_impl
.capacity(); }
1449 void reserve(size_t sz
) { m_impl
.reserve(sz
); }
1451 void resize(size_t nSize
, wxUniChar ch
= wxT('\0'))
1453 const size_t len
= length();
1457 #if wxUSE_UNICODE_UTF8
1460 wxSTRING_INVALIDATE_CACHE();
1462 // we can't use wxStringImpl::resize() for truncating the string as it
1463 // counts in bytes, not characters
1468 // we also can't use (presumably more efficient) resize() if we have to
1469 // append characters taking more than one byte
1470 if ( !ch
.IsAscii() )
1472 append(nSize
- len
, ch
);
1474 else // can use (presumably faster) resize() version
1475 #endif // wxUSE_UNICODE_UTF8
1477 wxSTRING_INVALIDATE_CACHED_LENGTH();
1479 m_impl
.resize(nSize
, (wxStringCharType
)ch
);
1483 wxString
substr(size_t nStart
= 0, size_t nLen
= npos
) const
1486 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
1487 return FromImpl(m_impl
.substr(pos
, len
));
1490 // generic attributes & operations
1491 // as standard strlen()
1492 size_t Len() const { return length(); }
1493 // string contains any characters?
1494 bool IsEmpty() const { return empty(); }
1495 // empty string is "false", so !str will return true
1496 bool operator!() const { return empty(); }
1497 // truncate the string to given length
1498 wxString
& Truncate(size_t uiLen
);
1499 // empty string contents
1500 void Empty() { clear(); }
1501 // empty the string and free memory
1502 void Clear() { clear(); }
1505 // Is an ascii value
1506 bool IsAscii() const;
1508 bool IsNumber() const;
1510 bool IsWord() const;
1512 // data access (all indexes are 0 based)
1514 wxUniChar
at(size_t n
) const
1515 { return wxStringOperations::DecodeChar(m_impl
.begin() + PosToImpl(n
)); }
1516 wxUniChar
GetChar(size_t n
) const
1518 // read/write access
1519 wxUniCharRef
at(size_t n
)
1520 { return *GetIterForNthChar(n
); }
1521 wxUniCharRef
GetWritableChar(size_t n
)
1524 void SetChar(size_t n
, wxUniChar ch
)
1527 // get last character
1528 wxUniChar
Last() const
1530 wxASSERT_MSG( !empty(), wxT("wxString: index out of bounds") );
1534 // get writable last character
1537 wxASSERT_MSG( !empty(), wxT("wxString: index out of bounds") );
1542 Note that we we must define all of the overloads below to avoid
1543 ambiguity when using str[0].
1545 wxUniChar
operator[](int n
) const
1547 wxUniChar
operator[](long n
) const
1549 wxUniChar
operator[](size_t n
) const
1551 #ifndef wxSIZE_T_IS_UINT
1552 wxUniChar
operator[](unsigned int n
) const
1554 #endif // size_t != unsigned int
1556 // operator versions of GetWriteableChar()
1557 wxUniCharRef
operator[](int n
)
1559 wxUniCharRef
operator[](long n
)
1561 wxUniCharRef
operator[](size_t n
)
1563 #ifndef wxSIZE_T_IS_UINT
1564 wxUniCharRef
operator[](unsigned int n
)
1566 #endif // size_t != unsigned int
1570 Overview of wxString conversions, implicit and explicit:
1572 - wxString has a std::[w]string-like c_str() method, however it does
1573 not return a C-style string directly but instead returns wxCStrData
1574 helper object which is convertible to either "char *" narrow string
1575 or "wchar_t *" wide string. Usually the correct conversion will be
1576 applied by the compiler automatically but if this doesn't happen you
1577 need to explicitly choose one using wxCStrData::AsChar() or AsWChar()
1578 methods or another wxString conversion function.
1580 - One of the places where the conversion does *NOT* happen correctly is
1581 when c_str() is passed to a vararg function such as printf() so you
1582 must *NOT* use c_str() with them. Either use wxPrintf() (all wx
1583 functions do handle c_str() correctly, even if they appear to be
1584 vararg (but they're not, really)) or add an explicit AsChar() or, if
1585 compatibility with previous wxWidgets versions is important, add a
1586 cast to "const char *".
1588 - In non-STL mode only, wxString is also implicitly convertible to
1589 wxCStrData. The same warning as above applies.
1591 - c_str() is polymorphic as it can be converted to either narrow or
1592 wide string. If you explicitly need one or the other, choose to use
1593 mb_str() (for narrow) or wc_str() (for wide) instead. Notice that
1594 these functions can return either the pointer to string directly (if
1595 this is what the string uses internally) or a temporary buffer
1596 containing the string and convertible to it. Again, conversion will
1597 usually be done automatically by the compiler but beware of the
1598 vararg functions: you need an explicit cast when using them.
1600 - There are also non-const versions of mb_str() and wc_str() called
1601 char_str() and wchar_str(). They are only meant to be used with
1602 non-const-correct functions and they always return buffers.
1604 - Finally wx_str() returns whatever string representation is used by
1605 wxString internally. It may be either a narrow or wide string
1606 depending on wxWidgets build mode but it will always be a raw pointer
1610 // explicit conversion to wxCStrData
1611 wxCStrData
c_str() const { return wxCStrData(this); }
1612 wxCStrData
data() const { return c_str(); }
1614 // implicit conversion to wxCStrData
1615 operator wxCStrData() const { return c_str(); }
1617 // the first two operators conflict with operators for conversion to
1618 // std::string and they must be disabled if those conversions are enabled;
1619 // the next one only makes sense if conversions to char* are also defined
1620 // and not defining it in STL build also helps us to get more clear error
1621 // messages for the code which relies on implicit conversion to char* in
1623 #if !wxUSE_STD_STRING_CONV_IN_WXSTRING
1624 operator const char*() const { return c_str(); }
1625 operator const wchar_t*() const { return c_str(); }
1627 // implicit conversion to untyped pointer for compatibility with previous
1628 // wxWidgets versions: this is the same as conversion to const char * so it
1630 operator const void*() const { return c_str(); }
1631 #endif // !wxUSE_STD_STRING_CONV_IN_WXSTRING
1633 // identical to c_str(), for MFC compatibility
1634 const wxCStrData
GetData() const { return c_str(); }
1636 // explicit conversion to C string in internal representation (char*,
1637 // wchar_t*, UTF-8-encoded char*, depending on the build):
1638 const wxStringCharType
*wx_str() const { return m_impl
.c_str(); }
1640 // conversion to *non-const* multibyte or widestring buffer; modifying
1641 // returned buffer won't affect the string, these methods are only useful
1642 // for passing values to const-incorrect functions
1643 wxWritableCharBuffer
char_str(const wxMBConv
& conv
= wxConvLibc
) const
1644 { return mb_str(conv
); }
1645 wxWritableWCharBuffer
wchar_str() const { return wc_str(); }
1647 // conversion to the buffer of the given type T (= char or wchar_t) and
1648 // also optionally return the buffer length
1650 // this is mostly/only useful for the template functions
1652 // FIXME-VC6: the second argument only exists for VC6 which doesn't support
1653 // explicit template function selection, do not use it unless
1654 // you must support VC6!
1655 template <typename T
>
1656 wxCharTypeBuffer
<T
> tchar_str(size_t *len
= NULL
,
1657 T
* WXUNUSED(dummy
) = NULL
) const
1660 // we need a helper dispatcher depending on type
1661 return wxPrivate::wxStringAsBufHelper
<T
>::Get(*this, len
);
1663 // T can only be char in ANSI build
1667 return wxCharTypeBuffer
<T
>::CreateNonOwned(wx_str(), length());
1668 #endif // Unicode build kind
1671 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
1672 // converting numbers or strings which are certain not to contain special
1673 // chars (typically system functions, X atoms, environment variables etc.)
1675 // the behaviour of these functions with the strings containing anything
1676 // else than 7 bit ASCII characters is undefined, use at your own risk.
1678 static wxString
FromAscii(const char *ascii
, size_t len
);
1679 static wxString
FromAscii(const char *ascii
);
1680 static wxString
FromAscii(char ascii
);
1681 const wxScopedCharBuffer
ToAscii() const;
1683 static wxString
FromAscii(const char *ascii
) { return wxString( ascii
); }
1684 static wxString
FromAscii(const char *ascii
, size_t len
)
1685 { return wxString( ascii
, len
); }
1686 static wxString
FromAscii(char ascii
) { return wxString( ascii
); }
1687 const char *ToAscii() const { return c_str(); }
1688 #endif // Unicode/!Unicode
1690 // also provide unsigned char overloads as signed/unsigned doesn't matter
1691 // for 7 bit ASCII characters
1692 static wxString
FromAscii(const unsigned char *ascii
)
1693 { return FromAscii((const char *)ascii
); }
1694 static wxString
FromAscii(const unsigned char *ascii
, size_t len
)
1695 { return FromAscii((const char *)ascii
, len
); }
1697 // conversion to/from UTF-8:
1698 #if wxUSE_UNICODE_UTF8
1699 static wxString
FromUTF8Unchecked(const char *utf8
)
1702 return wxEmptyString
;
1704 wxASSERT( wxStringOperations::IsValidUtf8String(utf8
) );
1705 return FromImpl(wxStringImpl(utf8
));
1707 static wxString
FromUTF8Unchecked(const char *utf8
, size_t len
)
1710 return wxEmptyString
;
1712 return FromUTF8Unchecked(utf8
);
1714 wxASSERT( wxStringOperations::IsValidUtf8String(utf8
, len
) );
1715 return FromImpl(wxStringImpl(utf8
, len
));
1718 static wxString
FromUTF8(const char *utf8
)
1720 if ( !utf8
|| !wxStringOperations::IsValidUtf8String(utf8
) )
1723 return FromImpl(wxStringImpl(utf8
));
1725 static wxString
FromUTF8(const char *utf8
, size_t len
)
1728 return FromUTF8(utf8
);
1730 if ( !utf8
|| !wxStringOperations::IsValidUtf8String(utf8
, len
) )
1733 return FromImpl(wxStringImpl(utf8
, len
));
1736 const wxScopedCharBuffer
utf8_str() const
1737 { return wxCharBuffer::CreateNonOwned(m_impl
.c_str(), m_impl
.length()); }
1739 // this function exists in UTF-8 build only and returns the length of the
1740 // internal UTF-8 representation
1741 size_t utf8_length() const { return m_impl
.length(); }
1742 #elif wxUSE_UNICODE_WCHAR
1743 static wxString
FromUTF8(const char *utf8
, size_t len
= npos
)
1744 { return wxString(utf8
, wxMBConvUTF8(), len
); }
1745 static wxString
FromUTF8Unchecked(const char *utf8
, size_t len
= npos
)
1747 const wxString
s(utf8
, wxMBConvUTF8(), len
);
1748 wxASSERT_MSG( !utf8
|| !*utf8
|| !s
.empty(),
1749 "string must be valid UTF-8" );
1752 const wxScopedCharBuffer
utf8_str() const { return mb_str(wxMBConvUTF8()); }
1754 static wxString
FromUTF8(const char *utf8
)
1755 { return wxString(wxMBConvUTF8().cMB2WC(utf8
)); }
1756 static wxString
FromUTF8(const char *utf8
, size_t len
)
1759 wxScopedWCharBuffer
buf(wxMBConvUTF8().cMB2WC(utf8
, len
== npos
? wxNO_LEN
: len
, &wlen
));
1760 return wxString(buf
.data(), wlen
);
1762 static wxString
FromUTF8Unchecked(const char *utf8
, size_t len
= npos
)
1765 wxScopedWCharBuffer buf
1767 wxMBConvUTF8().cMB2WC
1770 len
== npos
? wxNO_LEN
: len
,
1774 wxASSERT_MSG( !utf8
|| !*utf8
|| wlen
,
1775 "string must be valid UTF-8" );
1777 return wxString(buf
.data(), wlen
);
1779 const wxScopedCharBuffer
utf8_str() const
1780 { return wxMBConvUTF8().cWC2MB(wc_str()); }
1783 const wxScopedCharBuffer
ToUTF8() const { return utf8_str(); }
1785 // functions for storing binary data in wxString:
1787 static wxString
From8BitData(const char *data
, size_t len
)
1788 { return wxString(data
, wxConvISO8859_1
, len
); }
1789 // version for NUL-terminated data:
1790 static wxString
From8BitData(const char *data
)
1791 { return wxString(data
, wxConvISO8859_1
); }
1792 const wxScopedCharBuffer
To8BitData() const
1793 { return mb_str(wxConvISO8859_1
); }
1795 static wxString
From8BitData(const char *data
, size_t len
)
1796 { return wxString(data
, len
); }
1797 // version for NUL-terminated data:
1798 static wxString
From8BitData(const char *data
)
1799 { return wxString(data
); }
1800 const wxScopedCharBuffer
To8BitData() const
1801 { return wxScopedCharBuffer::CreateNonOwned(wx_str(), length()); }
1802 #endif // Unicode/ANSI
1804 // conversions with (possible) format conversions: have to return a
1805 // buffer with temporary data
1807 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
1808 // return an ANSI (multibyte) string, wc_str() to return a wide string and
1809 // fn_str() to return a string which should be used with the OS APIs
1810 // accepting the file names. The return value is always the same, but the
1811 // type differs because a function may either return pointer to the buffer
1812 // directly or have to use intermediate buffer for translation.
1816 // this is an optimization: even though using mb_str(wxConvLibc) does the
1817 // same thing (i.e. returns pointer to internal representation as locale is
1818 // always an UTF-8 one) in wxUSE_UTF8_LOCALE_ONLY case, we can avoid the
1819 // extra checks and the temporary buffer construction by providing a
1820 // separate mb_str() overload
1821 #if wxUSE_UTF8_LOCALE_ONLY
1822 const char* mb_str() const { return wx_str(); }
1823 const wxScopedCharBuffer
mb_str(const wxMBConv
& conv
) const
1825 return AsCharBuf(conv
);
1827 #else // !wxUSE_UTF8_LOCALE_ONLY
1828 const wxScopedCharBuffer
mb_str(const wxMBConv
& conv
= wxConvLibc
) const
1830 return AsCharBuf(conv
);
1832 #endif // wxUSE_UTF8_LOCALE_ONLY/!wxUSE_UTF8_LOCALE_ONLY
1834 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
1836 #if wxUSE_UNICODE_WCHAR
1837 const wchar_t* wc_str() const { return wx_str(); }
1838 #elif wxUSE_UNICODE_UTF8
1839 const wxScopedWCharBuffer
wc_str() const
1840 { return AsWCharBuf(wxMBConvStrictUTF8()); }
1842 // for compatibility with !wxUSE_UNICODE version
1843 const wxWX2WCbuf
wc_str(const wxMBConv
& WXUNUSED(conv
)) const
1844 { return wc_str(); }
1847 const wxScopedCharBuffer
fn_str() const { return mb_str(wxConvFile
); }
1849 const wxWX2WCbuf
fn_str() const { return wc_str(); }
1850 #endif // wxMBFILES/!wxMBFILES
1853 const char* mb_str() const { return wx_str(); }
1855 // for compatibility with wxUSE_UNICODE version
1856 const char* mb_str(const wxMBConv
& WXUNUSED(conv
)) const { return wx_str(); }
1858 const wxWX2MBbuf
mbc_str() const { return mb_str(); }
1860 const wxScopedWCharBuffer
wc_str(const wxMBConv
& conv
= wxConvLibc
) const
1861 { return AsWCharBuf(conv
); }
1863 const wxScopedCharBuffer
fn_str() const
1864 { return wxConvFile
.cWC2WX( wc_str( wxConvLibc
) ); }
1865 #endif // Unicode/ANSI
1867 #if wxUSE_UNICODE_UTF8
1868 const wxScopedWCharBuffer
t_str() const { return wc_str(); }
1869 #elif wxUSE_UNICODE_WCHAR
1870 const wchar_t* t_str() const { return wx_str(); }
1872 const char* t_str() const { return wx_str(); }
1876 // overloaded assignment
1877 // from another wxString
1878 wxString
& operator=(const wxString
& stringSrc
)
1880 if ( this != &stringSrc
)
1882 wxSTRING_INVALIDATE_CACHE();
1884 m_impl
= stringSrc
.m_impl
;
1890 wxString
& operator=(const wxCStrData
& cstr
)
1891 { return *this = cstr
.AsString(); }
1893 wxString
& operator=(wxUniChar ch
)
1895 wxSTRING_INVALIDATE_CACHE();
1897 #if wxUSE_UNICODE_UTF8
1898 if ( !ch
.IsAscii() )
1899 m_impl
= wxStringOperations::EncodeChar(ch
);
1901 #endif // wxUSE_UNICODE_UTF8
1902 m_impl
= (wxStringCharType
)ch
;
1906 wxString
& operator=(wxUniCharRef ch
)
1907 { return operator=((wxUniChar
)ch
); }
1908 wxString
& operator=(char ch
)
1909 { return operator=(wxUniChar(ch
)); }
1910 wxString
& operator=(unsigned char ch
)
1911 { return operator=(wxUniChar(ch
)); }
1912 wxString
& operator=(wchar_t ch
)
1913 { return operator=(wxUniChar(ch
)); }
1914 // from a C string - STL probably will crash on NULL,
1915 // so we need to compensate in that case
1916 #if wxUSE_STL_BASED_WXSTRING
1917 wxString
& operator=(const char *psz
)
1919 wxSTRING_INVALIDATE_CACHE();
1922 m_impl
= ImplStr(psz
);
1929 wxString
& operator=(const wchar_t *pwz
)
1931 wxSTRING_INVALIDATE_CACHE();
1934 m_impl
= ImplStr(pwz
);
1940 #else // !wxUSE_STL_BASED_WXSTRING
1941 wxString
& operator=(const char *psz
)
1943 wxSTRING_INVALIDATE_CACHE();
1945 m_impl
= ImplStr(psz
);
1950 wxString
& operator=(const wchar_t *pwz
)
1952 wxSTRING_INVALIDATE_CACHE();
1954 m_impl
= ImplStr(pwz
);
1958 #endif // wxUSE_STL_BASED_WXSTRING/!wxUSE_STL_BASED_WXSTRING
1960 wxString
& operator=(const unsigned char *psz
)
1961 { return operator=((const char*)psz
); }
1963 // from wxScopedWCharBuffer
1964 wxString
& operator=(const wxScopedWCharBuffer
& s
)
1965 { return assign(s
); }
1966 // from wxScopedCharBuffer
1967 wxString
& operator=(const wxScopedCharBuffer
& s
)
1968 { return assign(s
); }
1970 // string concatenation
1971 // in place concatenation
1973 Concatenate and return the result. Note that the left to right
1974 associativity of << allows to write things like "str << str1 << str2
1975 << ..." (unlike with +=)
1978 wxString
& operator<<(const wxString
& s
)
1980 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1981 wxASSERT_MSG( s
.IsValid(),
1982 wxT("did you forget to call UngetWriteBuf()?") );
1988 // string += C string
1989 wxString
& operator<<(const char *psz
)
1990 { append(psz
); return *this; }
1991 wxString
& operator<<(const wchar_t *pwz
)
1992 { append(pwz
); return *this; }
1993 wxString
& operator<<(const wxCStrData
& psz
)
1994 { append(psz
.AsString()); return *this; }
1996 wxString
& operator<<(wxUniChar ch
) { append(1, ch
); return *this; }
1997 wxString
& operator<<(wxUniCharRef ch
) { append(1, ch
); return *this; }
1998 wxString
& operator<<(char ch
) { append(1, ch
); return *this; }
1999 wxString
& operator<<(unsigned char ch
) { append(1, ch
); return *this; }
2000 wxString
& operator<<(wchar_t ch
) { append(1, ch
); return *this; }
2002 // string += buffer (i.e. from wxGetString)
2003 wxString
& operator<<(const wxScopedWCharBuffer
& s
)
2004 { return append(s
); }
2005 wxString
& operator<<(const wxScopedCharBuffer
& s
)
2006 { return append(s
); }
2008 // string += C string
2009 wxString
& Append(const wxString
& s
)
2011 // test for empty() to share the string if possible
2018 wxString
& Append(const char* psz
)
2019 { append(psz
); return *this; }
2020 wxString
& Append(const wchar_t* pwz
)
2021 { append(pwz
); return *this; }
2022 wxString
& Append(const wxCStrData
& psz
)
2023 { append(psz
); return *this; }
2024 wxString
& Append(const wxScopedCharBuffer
& psz
)
2025 { append(psz
); return *this; }
2026 wxString
& Append(const wxScopedWCharBuffer
& psz
)
2027 { append(psz
); return *this; }
2028 wxString
& Append(const char* psz
, size_t nLen
)
2029 { append(psz
, nLen
); return *this; }
2030 wxString
& Append(const wchar_t* pwz
, size_t nLen
)
2031 { append(pwz
, nLen
); return *this; }
2032 wxString
& Append(const wxCStrData
& psz
, size_t nLen
)
2033 { append(psz
, nLen
); return *this; }
2034 wxString
& Append(const wxScopedCharBuffer
& psz
, size_t nLen
)
2035 { append(psz
, nLen
); return *this; }
2036 wxString
& Append(const wxScopedWCharBuffer
& psz
, size_t nLen
)
2037 { append(psz
, nLen
); return *this; }
2038 // append count copies of given character
2039 wxString
& Append(wxUniChar ch
, size_t count
= 1u)
2040 { append(count
, ch
); return *this; }
2041 wxString
& Append(wxUniCharRef ch
, size_t count
= 1u)
2042 { append(count
, ch
); return *this; }
2043 wxString
& Append(char ch
, size_t count
= 1u)
2044 { append(count
, ch
); return *this; }
2045 wxString
& Append(unsigned char ch
, size_t count
= 1u)
2046 { append(count
, ch
); return *this; }
2047 wxString
& Append(wchar_t ch
, size_t count
= 1u)
2048 { append(count
, ch
); return *this; }
2050 // prepend a string, return the string itself
2051 wxString
& Prepend(const wxString
& str
)
2052 { *this = str
+ *this; return *this; }
2054 // non-destructive concatenation
2056 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
,
2057 const wxString
& string2
);
2058 // string with a single char
2059 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
2060 // char with a string
2061 friend wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
2062 // string with C string
2063 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
2065 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
2066 const wchar_t *pwz
);
2067 // C string with string
2068 friend wxString WXDLLIMPEXP_BASE
operator+(const char *psz
,
2069 const wxString
& string
);
2070 friend wxString WXDLLIMPEXP_BASE
operator+(const wchar_t *pwz
,
2071 const wxString
& string
);
2073 // stream-like functions
2074 // insert an int into string
2075 wxString
& operator<<(int i
)
2076 { return (*this) << Format(wxT("%d"), i
); }
2077 // insert an unsigned int into string
2078 wxString
& operator<<(unsigned int ui
)
2079 { return (*this) << Format(wxT("%u"), ui
); }
2080 // insert a long into string
2081 wxString
& operator<<(long l
)
2082 { return (*this) << Format(wxT("%ld"), l
); }
2083 // insert an unsigned long into string
2084 wxString
& operator<<(unsigned long ul
)
2085 { return (*this) << Format(wxT("%lu"), ul
); }
2086 #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
2087 // insert a long long if they exist and aren't longs
2088 wxString
& operator<<(wxLongLong_t ll
)
2090 return (*this) << Format("%" wxLongLongFmtSpec
"d", ll
);
2092 // insert an unsigned long long
2093 wxString
& operator<<(wxULongLong_t ull
)
2095 return (*this) << Format("%" wxLongLongFmtSpec
"u" , ull
);
2097 #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
2098 // insert a float into string
2099 wxString
& operator<<(float f
)
2100 { return (*this) << Format(wxT("%f"), f
); }
2101 // insert a double into string
2102 wxString
& operator<<(double d
)
2103 { return (*this) << Format(wxT("%g"), d
); }
2105 // string comparison
2106 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
2107 int Cmp(const char *psz
) const
2108 { return compare(psz
); }
2109 int Cmp(const wchar_t *pwz
) const
2110 { return compare(pwz
); }
2111 int Cmp(const wxString
& s
) const
2112 { return compare(s
); }
2113 int Cmp(const wxCStrData
& s
) const
2114 { return compare(s
); }
2115 int Cmp(const wxScopedCharBuffer
& s
) const
2116 { return compare(s
); }
2117 int Cmp(const wxScopedWCharBuffer
& s
) const
2118 { return compare(s
); }
2119 // same as Cmp() but not case-sensitive
2120 int CmpNoCase(const wxString
& s
) const;
2122 // test for the string equality, either considering case or not
2123 // (if compareWithCase then the case matters)
2124 bool IsSameAs(const wxString
& str
, bool compareWithCase
= true) const
2126 #if !wxUSE_UNICODE_UTF8
2127 // in UTF-8 build, length() is O(n) and doing this would be _slower_
2128 if ( length() != str
.length() )
2131 return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0;
2133 bool IsSameAs(const char *str
, bool compareWithCase
= true) const
2134 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
2135 bool IsSameAs(const wchar_t *str
, bool compareWithCase
= true) const
2136 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
2138 bool IsSameAs(const wxCStrData
& str
, bool compareWithCase
= true) const
2139 { return IsSameAs(str
.AsString(), compareWithCase
); }
2140 bool IsSameAs(const wxScopedCharBuffer
& str
, bool compareWithCase
= true) const
2141 { return IsSameAs(str
.data(), compareWithCase
); }
2142 bool IsSameAs(const wxScopedWCharBuffer
& str
, bool compareWithCase
= true) const
2143 { return IsSameAs(str
.data(), compareWithCase
); }
2144 // comparison with a single character: returns true if equal
2145 bool IsSameAs(wxUniChar c
, bool compareWithCase
= true) const;
2146 // FIXME-UTF8: remove these overloads
2147 bool IsSameAs(wxUniCharRef c
, bool compareWithCase
= true) const
2148 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
2149 bool IsSameAs(char c
, bool compareWithCase
= true) const
2150 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
2151 bool IsSameAs(unsigned char c
, bool compareWithCase
= true) const
2152 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
2153 bool IsSameAs(wchar_t c
, bool compareWithCase
= true) const
2154 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
2155 bool IsSameAs(int c
, bool compareWithCase
= true) const
2156 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
2158 // simple sub-string extraction
2159 // return substring starting at nFirst of length nCount (or till the end
2160 // if nCount = default value)
2161 wxString
Mid(size_t nFirst
, size_t nCount
= npos
) const;
2163 // operator version of Mid()
2164 wxString
operator()(size_t start
, size_t len
) const
2165 { return Mid(start
, len
); }
2167 // check if the string starts with the given prefix and return the rest
2168 // of the string in the provided pointer if it is not NULL; otherwise
2170 bool StartsWith(const wxString
& prefix
, wxString
*rest
= NULL
) const;
2171 // check if the string ends with the given suffix and return the
2172 // beginning of the string before the suffix in the provided pointer if
2173 // it is not NULL; otherwise return false
2174 bool EndsWith(const wxString
& suffix
, wxString
*rest
= NULL
) const;
2176 // get first nCount characters
2177 wxString
Left(size_t nCount
) const;
2178 // get last nCount characters
2179 wxString
Right(size_t nCount
) const;
2180 // get all characters before the first occurrence of ch
2181 // (returns the whole string if ch not found) and also put everything
2182 // following the first occurrence of ch into rest if it's non-NULL
2183 wxString
BeforeFirst(wxUniChar ch
, wxString
*rest
= NULL
) const;
2184 // get all characters before the last occurrence of ch
2185 // (returns empty string if ch not found) and also put everything
2186 // following the last occurrence of ch into rest if it's non-NULL
2187 wxString
BeforeLast(wxUniChar ch
, wxString
*rest
= NULL
) const;
2188 // get all characters after the first occurrence of ch
2189 // (returns empty string if ch not found)
2190 wxString
AfterFirst(wxUniChar ch
) const;
2191 // get all characters after the last occurrence of ch
2192 // (returns the whole string if ch not found)
2193 wxString
AfterLast(wxUniChar ch
) const;
2195 // for compatibility only, use more explicitly named functions above
2196 wxString
Before(wxUniChar ch
) const { return BeforeLast(ch
); }
2197 wxString
After(wxUniChar ch
) const { return AfterFirst(ch
); }
2200 // convert to upper case in place, return the string itself
2201 wxString
& MakeUpper();
2202 // convert to upper case, return the copy of the string
2203 wxString
Upper() const { return wxString(*this).MakeUpper(); }
2204 // convert to lower case in place, return the string itself
2205 wxString
& MakeLower();
2206 // convert to lower case, return the copy of the string
2207 wxString
Lower() const { return wxString(*this).MakeLower(); }
2208 // convert the first character to the upper case and the rest to the
2209 // lower one, return the modified string itself
2210 wxString
& MakeCapitalized();
2211 // convert the first character to the upper case and the rest to the
2212 // lower one, return the copy of the string
2213 wxString
Capitalize() const { return wxString(*this).MakeCapitalized(); }
2215 // trimming/padding whitespace (either side) and truncating
2216 // remove spaces from left or from right (default) side
2217 wxString
& Trim(bool bFromRight
= true);
2218 // add nCount copies chPad in the beginning or at the end (default)
2219 wxString
& Pad(size_t nCount
, wxUniChar chPad
= wxT(' '), bool bFromRight
= true);
2221 // searching and replacing
2222 // searching (return starting index, or -1 if not found)
2223 int Find(wxUniChar ch
, bool bFromEnd
= false) const; // like strchr/strrchr
2224 int Find(wxUniCharRef ch
, bool bFromEnd
= false) const
2225 { return Find(wxUniChar(ch
), bFromEnd
); }
2226 int Find(char ch
, bool bFromEnd
= false) const
2227 { return Find(wxUniChar(ch
), bFromEnd
); }
2228 int Find(unsigned char ch
, bool bFromEnd
= false) const
2229 { return Find(wxUniChar(ch
), bFromEnd
); }
2230 int Find(wchar_t ch
, bool bFromEnd
= false) const
2231 { return Find(wxUniChar(ch
), bFromEnd
); }
2232 // searching (return starting index, or -1 if not found)
2233 int Find(const wxString
& sub
) const // like strstr
2235 size_type idx
= find(sub
);
2236 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
2238 int Find(const char *sub
) const // like strstr
2240 size_type idx
= find(sub
);
2241 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
2243 int Find(const wchar_t *sub
) const // like strstr
2245 size_type idx
= find(sub
);
2246 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
2249 int Find(const wxCStrData
& sub
) const
2250 { return Find(sub
.AsString()); }
2251 int Find(const wxScopedCharBuffer
& sub
) const
2252 { return Find(sub
.data()); }
2253 int Find(const wxScopedWCharBuffer
& sub
) const
2254 { return Find(sub
.data()); }
2256 // replace first (or all of bReplaceAll) occurrences of substring with
2257 // another string, returns the number of replacements made
2258 size_t Replace(const wxString
& strOld
,
2259 const wxString
& strNew
,
2260 bool bReplaceAll
= true);
2262 // check if the string contents matches a mask containing '*' and '?'
2263 bool Matches(const wxString
& mask
) const;
2265 // conversion to numbers: all functions return true only if the whole
2266 // string is a number and put the value of this number into the pointer
2267 // provided, the base is the numeric base in which the conversion should be
2268 // done and must be comprised between 2 and 36 or be 0 in which case the
2269 // standard C rules apply (leading '0' => octal, "0x" => hex)
2270 // convert to a signed integer
2271 bool ToLong(long *val
, int base
= 10) const;
2272 // convert to an unsigned integer
2273 bool ToULong(unsigned long *val
, int base
= 10) const;
2274 // convert to wxLongLong
2275 #if defined(wxLongLong_t)
2276 bool ToLongLong(wxLongLong_t
*val
, int base
= 10) const;
2277 // convert to wxULongLong
2278 bool ToULongLong(wxULongLong_t
*val
, int base
= 10) const;
2279 #endif // wxLongLong_t
2280 // convert to a double
2281 bool ToDouble(double *val
) const;
2283 // conversions to numbers using C locale
2284 // convert to a signed integer
2285 bool ToCLong(long *val
, int base
= 10) const;
2286 // convert to an unsigned integer
2287 bool ToCULong(unsigned long *val
, int base
= 10) const;
2288 // convert to a double
2289 bool ToCDouble(double *val
) const;
2291 // create a string representing the given floating point number with the
2292 // default (like %g) or fixed (if precision >=0) precision
2293 // in the current locale
2294 static wxString
FromDouble(double val
, int precision
= -1);
2296 static wxString
FromCDouble(double val
, int precision
= -1);
2298 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2299 // formatted input/output
2300 // as sprintf(), returns the number of characters written or < 0 on error
2301 // (take 'this' into account in attribute parameter count)
2302 // int Printf(const wxString& format, ...);
2303 WX_DEFINE_VARARG_FUNC(int, Printf
, 1, (const wxFormatString
&),
2304 DoPrintfWchar
, DoPrintfUtf8
)
2306 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
2307 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wxString
&),
2308 (wxFormatString(f1
)));
2309 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wxCStrData
&),
2310 (wxFormatString(f1
)));
2311 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const char*),
2312 (wxFormatString(f1
)));
2313 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wchar_t*),
2314 (wxFormatString(f1
)));
2316 #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN
2317 // as vprintf(), returns the number of characters written or < 0 on error
2318 int PrintfV(const wxString
& format
, va_list argptr
);
2320 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2321 // returns the string containing the result of Printf() to it
2322 // static wxString Format(const wxString& format, ...) WX_ATTRIBUTE_PRINTF_1;
2323 WX_DEFINE_VARARG_FUNC(static wxString
, Format
, 1, (const wxFormatString
&),
2324 DoFormatWchar
, DoFormatUtf8
)
2326 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
2327 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wxString
&),
2328 (wxFormatString(f1
)));
2329 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wxCStrData
&),
2330 (wxFormatString(f1
)));
2331 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const char*),
2332 (wxFormatString(f1
)));
2333 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wchar_t*),
2334 (wxFormatString(f1
)));
2337 // the same as above, but takes a va_list
2338 static wxString
FormatV(const wxString
& format
, va_list argptr
);
2340 // raw access to string memory
2341 // ensure that string has space for at least nLen characters
2342 // only works if the data of this string is not shared
2343 bool Alloc(size_t nLen
) { reserve(nLen
); return capacity() >= nLen
; }
2344 // minimize the string's memory
2345 // only works if the data of this string is not shared
2347 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2348 // These are deprecated, use wxStringBuffer or wxStringBufferLength instead
2350 // get writable buffer of at least nLen bytes. Unget() *must* be called
2351 // a.s.a.p. to put string back in a reasonable state!
2352 wxDEPRECATED( wxStringCharType
*GetWriteBuf(size_t nLen
) );
2353 // call this immediately after GetWriteBuf() has been used
2354 wxDEPRECATED( void UngetWriteBuf() );
2355 wxDEPRECATED( void UngetWriteBuf(size_t nLen
) );
2356 #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && wxUSE_UNICODE_UTF8
2358 // wxWidgets version 1 compatibility functions
2361 wxString
SubString(size_t from
, size_t to
) const
2362 { return Mid(from
, (to
- from
+ 1)); }
2363 // values for second parameter of CompareTo function
2364 enum caseCompare
{exact
, ignoreCase
};
2365 // values for first parameter of Strip function
2366 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
2368 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2370 // (take 'this' into account in attribute parameter count)
2371 // int sprintf(const wxString& format, ...) WX_ATTRIBUTE_PRINTF_2;
2372 WX_DEFINE_VARARG_FUNC(int, sprintf
, 1, (const wxFormatString
&),
2373 DoPrintfWchar
, DoPrintfUtf8
)
2375 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
2376 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wxString
&),
2377 (wxFormatString(f1
)));
2378 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wxCStrData
&),
2379 (wxFormatString(f1
)));
2380 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const char*),
2381 (wxFormatString(f1
)));
2382 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wchar_t*),
2383 (wxFormatString(f1
)));
2385 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
2388 int CompareTo(const wxChar
* psz
, caseCompare cmp
= exact
) const
2389 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
2392 size_t Length() const { return length(); }
2393 // Count the number of characters
2394 int Freq(wxUniChar ch
) const;
2396 void LowerCase() { MakeLower(); }
2398 void UpperCase() { MakeUpper(); }
2399 // use Trim except that it doesn't change this string
2400 wxString
Strip(stripType w
= trailing
) const;
2402 // use Find (more general variants not yet supported)
2403 size_t Index(const wxChar
* psz
) const { return Find(psz
); }
2404 size_t Index(wxUniChar ch
) const { return Find(ch
); }
2406 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
2407 wxString
& RemoveLast(size_t n
= 1) { return Truncate(length() - n
); }
2409 wxString
& Remove(size_t nStart
, size_t nLen
)
2410 { return (wxString
&)erase( nStart
, nLen
); }
2413 int First( wxUniChar ch
) const { return Find(ch
); }
2414 int First( wxUniCharRef ch
) const { return Find(ch
); }
2415 int First( char ch
) const { return Find(ch
); }
2416 int First( unsigned char ch
) const { return Find(ch
); }
2417 int First( wchar_t ch
) const { return Find(ch
); }
2418 int First( const wxString
& str
) const { return Find(str
); }
2419 int Last( wxUniChar ch
) const { return Find(ch
, true); }
2420 bool Contains(const wxString
& str
) const { return Find(str
) != wxNOT_FOUND
; }
2423 bool IsNull() const { return empty(); }
2425 // std::string compatibility functions
2427 // take nLen chars starting at nPos
2428 wxString(const wxString
& str
, size_t nPos
, size_t nLen
)
2429 { assign(str
, nPos
, nLen
); }
2430 // take all characters from first to last
2431 wxString(const_iterator first
, const_iterator last
)
2432 : m_impl(first
.impl(), last
.impl()) { }
2433 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2434 // the 2 overloads below are for compatibility with the existing code using
2435 // pointers instead of iterators
2436 wxString(const char *first
, const char *last
)
2438 SubstrBufFromMB
str(ImplStr(first
, last
- first
));
2439 m_impl
.assign(str
.data
, str
.len
);
2441 wxString(const wchar_t *first
, const wchar_t *last
)
2443 SubstrBufFromWC
str(ImplStr(first
, last
- first
));
2444 m_impl
.assign(str
.data
, str
.len
);
2446 // and this one is needed to compile code adding offsets to c_str() result
2447 wxString(const wxCStrData
& first
, const wxCStrData
& last
)
2448 : m_impl(CreateConstIterator(first
).impl(),
2449 CreateConstIterator(last
).impl())
2451 wxASSERT_MSG( first
.m_str
== last
.m_str
,
2452 wxT("pointers must be into the same string") );
2454 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2456 // lib.string.modifiers
2457 // append elements str[pos], ..., str[pos+n]
2458 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
2460 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2463 str
.PosLenToImpl(pos
, n
, &from
, &len
);
2464 m_impl
.append(str
.m_impl
, from
, len
);
2468 wxString
& append(const wxString
& str
)
2470 wxSTRING_UPDATE_CACHED_LENGTH(str
.length());
2472 m_impl
.append(str
.m_impl
);
2476 // append first n (or all if n == npos) characters of sz
2477 wxString
& append(const char *sz
)
2479 wxSTRING_INVALIDATE_CACHED_LENGTH();
2481 m_impl
.append(ImplStr(sz
));
2485 wxString
& append(const wchar_t *sz
)
2487 wxSTRING_INVALIDATE_CACHED_LENGTH();
2489 m_impl
.append(ImplStr(sz
));
2493 wxString
& append(const char *sz
, size_t n
)
2495 wxSTRING_INVALIDATE_CACHED_LENGTH();
2497 SubstrBufFromMB
str(ImplStr(sz
, n
));
2498 m_impl
.append(str
.data
, str
.len
);
2501 wxString
& append(const wchar_t *sz
, size_t n
)
2503 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2505 SubstrBufFromWC
str(ImplStr(sz
, n
));
2506 m_impl
.append(str
.data
, str
.len
);
2510 wxString
& append(const wxCStrData
& str
)
2511 { return append(str
.AsString()); }
2512 wxString
& append(const wxScopedCharBuffer
& str
)
2513 { return append(str
.data(), str
.length()); }
2514 wxString
& append(const wxScopedWCharBuffer
& str
)
2515 { return append(str
.data(), str
.length()); }
2516 wxString
& append(const wxCStrData
& str
, size_t n
)
2517 { return append(str
.AsString(), 0, n
); }
2518 wxString
& append(const wxScopedCharBuffer
& str
, size_t n
)
2519 { return append(str
.data(), n
); }
2520 wxString
& append(const wxScopedWCharBuffer
& str
, size_t n
)
2521 { return append(str
.data(), n
); }
2523 // append n copies of ch
2524 wxString
& append(size_t n
, wxUniChar ch
)
2526 #if wxUSE_UNICODE_UTF8
2527 if ( !ch
.IsAscii() )
2529 wxSTRING_INVALIDATE_CACHED_LENGTH();
2531 m_impl
.append(wxStringOperations::EncodeNChars(n
, ch
));
2536 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2538 m_impl
.append(n
, (wxStringCharType
)ch
);
2544 wxString
& append(size_t n
, wxUniCharRef ch
)
2545 { return append(n
, wxUniChar(ch
)); }
2546 wxString
& append(size_t n
, char ch
)
2547 { return append(n
, wxUniChar(ch
)); }
2548 wxString
& append(size_t n
, unsigned char ch
)
2549 { return append(n
, wxUniChar(ch
)); }
2550 wxString
& append(size_t n
, wchar_t ch
)
2551 { return append(n
, wxUniChar(ch
)); }
2553 // append from first to last
2554 wxString
& append(const_iterator first
, const_iterator last
)
2556 wxSTRING_INVALIDATE_CACHED_LENGTH();
2558 m_impl
.append(first
.impl(), last
.impl());
2561 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2562 wxString
& append(const char *first
, const char *last
)
2563 { return append(first
, last
- first
); }
2564 wxString
& append(const wchar_t *first
, const wchar_t *last
)
2565 { return append(first
, last
- first
); }
2566 wxString
& append(const wxCStrData
& first
, const wxCStrData
& last
)
2567 { return append(CreateConstIterator(first
), CreateConstIterator(last
)); }
2568 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2570 // same as `this_string = str'
2571 wxString
& assign(const wxString
& str
)
2573 wxSTRING_SET_CACHED_LENGTH(str
.length());
2575 m_impl
= str
.m_impl
;
2580 // This is a non-standard-compliant overload taking the first "len"
2581 // characters of the source string.
2582 wxString
& assign(const wxString
& str
, size_t len
)
2584 #if wxUSE_STRING_POS_CACHE
2585 // It is legal to pass len > str.length() to wxStringImpl::assign() but
2586 // by restricting it here we save some work for that function so it's not
2587 // really less efficient and, at the same time, ensure that we don't
2588 // cache invalid length.
2589 const size_t lenSrc
= str
.length();
2593 wxSTRING_SET_CACHED_LENGTH(len
);
2594 #endif // wxUSE_STRING_POS_CACHE
2596 m_impl
.assign(str
.m_impl
, 0, str
.LenToImpl(len
));
2601 // same as ` = str[pos..pos + n]
2602 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
2605 str
.PosLenToImpl(pos
, n
, &from
, &len
);
2606 m_impl
.assign(str
.m_impl
, from
, len
);
2608 // it's important to call this after PosLenToImpl() above in case str is
2609 // the same string as this one
2610 wxSTRING_SET_CACHED_LENGTH(n
);
2615 // same as `= first n (or all if n == npos) characters of sz'
2616 wxString
& assign(const char *sz
)
2618 wxSTRING_INVALIDATE_CACHE();
2620 m_impl
.assign(ImplStr(sz
));
2625 wxString
& assign(const wchar_t *sz
)
2627 wxSTRING_INVALIDATE_CACHE();
2629 m_impl
.assign(ImplStr(sz
));
2634 wxString
& assign(const char *sz
, size_t n
)
2636 wxSTRING_INVALIDATE_CACHE();
2638 SubstrBufFromMB
str(ImplStr(sz
, n
));
2639 m_impl
.assign(str
.data
, str
.len
);
2644 wxString
& assign(const wchar_t *sz
, size_t n
)
2646 wxSTRING_SET_CACHED_LENGTH(n
);
2648 SubstrBufFromWC
str(ImplStr(sz
, n
));
2649 m_impl
.assign(str
.data
, str
.len
);
2654 wxString
& assign(const wxCStrData
& str
)
2655 { return assign(str
.AsString()); }
2656 wxString
& assign(const wxScopedCharBuffer
& str
)
2657 { return assign(str
.data(), str
.length()); }
2658 wxString
& assign(const wxScopedWCharBuffer
& str
)
2659 { return assign(str
.data(), str
.length()); }
2660 wxString
& assign(const wxCStrData
& str
, size_t len
)
2661 { return assign(str
.AsString(), len
); }
2662 wxString
& assign(const wxScopedCharBuffer
& str
, size_t len
)
2663 { return assign(str
.data(), len
); }
2664 wxString
& assign(const wxScopedWCharBuffer
& str
, size_t len
)
2665 { return assign(str
.data(), len
); }
2667 // same as `= n copies of ch'
2668 wxString
& assign(size_t n
, wxUniChar ch
)
2670 wxSTRING_SET_CACHED_LENGTH(n
);
2672 #if wxUSE_UNICODE_UTF8
2673 if ( !ch
.IsAscii() )
2674 m_impl
.assign(wxStringOperations::EncodeNChars(n
, ch
));
2677 m_impl
.assign(n
, (wxStringCharType
)ch
);
2682 wxString
& assign(size_t n
, wxUniCharRef ch
)
2683 { return assign(n
, wxUniChar(ch
)); }
2684 wxString
& assign(size_t n
, char ch
)
2685 { return assign(n
, wxUniChar(ch
)); }
2686 wxString
& assign(size_t n
, unsigned char ch
)
2687 { return assign(n
, wxUniChar(ch
)); }
2688 wxString
& assign(size_t n
, wchar_t ch
)
2689 { return assign(n
, wxUniChar(ch
)); }
2691 // assign from first to last
2692 wxString
& assign(const_iterator first
, const_iterator last
)
2694 wxSTRING_INVALIDATE_CACHE();
2696 m_impl
.assign(first
.impl(), last
.impl());
2700 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2701 wxString
& assign(const char *first
, const char *last
)
2702 { return assign(first
, last
- first
); }
2703 wxString
& assign(const wchar_t *first
, const wchar_t *last
)
2704 { return assign(first
, last
- first
); }
2705 wxString
& assign(const wxCStrData
& first
, const wxCStrData
& last
)
2706 { return assign(CreateConstIterator(first
), CreateConstIterator(last
)); }
2707 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2709 // string comparison
2710 int compare(const wxString
& str
) const;
2711 int compare(const char* sz
) const;
2712 int compare(const wchar_t* sz
) const;
2713 int compare(const wxCStrData
& str
) const
2714 { return compare(str
.AsString()); }
2715 int compare(const wxScopedCharBuffer
& str
) const
2716 { return compare(str
.data()); }
2717 int compare(const wxScopedWCharBuffer
& str
) const
2718 { return compare(str
.data()); }
2719 // comparison with a substring
2720 int compare(size_t nStart
, size_t nLen
, const wxString
& str
) const;
2721 // comparison of 2 substrings
2722 int compare(size_t nStart
, size_t nLen
,
2723 const wxString
& str
, size_t nStart2
, size_t nLen2
) const;
2724 // substring comparison with first nCount characters of sz
2725 int compare(size_t nStart
, size_t nLen
,
2726 const char* sz
, size_t nCount
= npos
) const;
2727 int compare(size_t nStart
, size_t nLen
,
2728 const wchar_t* sz
, size_t nCount
= npos
) const;
2730 // insert another string
2731 wxString
& insert(size_t nPos
, const wxString
& str
)
2732 { insert(GetIterForNthChar(nPos
), str
.begin(), str
.end()); return *this; }
2733 // insert n chars of str starting at nStart (in str)
2734 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
2736 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2739 str
.PosLenToImpl(nStart
, n
, &from
, &len
);
2740 m_impl
.insert(PosToImpl(nPos
), str
.m_impl
, from
, len
);
2745 // insert first n (or all if n == npos) characters of sz
2746 wxString
& insert(size_t nPos
, const char *sz
)
2748 wxSTRING_INVALIDATE_CACHE();
2750 m_impl
.insert(PosToImpl(nPos
), ImplStr(sz
));
2755 wxString
& insert(size_t nPos
, const wchar_t *sz
)
2757 wxSTRING_INVALIDATE_CACHE();
2759 m_impl
.insert(PosToImpl(nPos
), ImplStr(sz
)); return *this;
2762 wxString
& insert(size_t nPos
, const char *sz
, size_t n
)
2764 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2766 SubstrBufFromMB
str(ImplStr(sz
, n
));
2767 m_impl
.insert(PosToImpl(nPos
), str
.data
, str
.len
);
2772 wxString
& insert(size_t nPos
, const wchar_t *sz
, size_t n
)
2774 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2776 SubstrBufFromWC
str(ImplStr(sz
, n
));
2777 m_impl
.insert(PosToImpl(nPos
), str
.data
, str
.len
);
2782 // insert n copies of ch
2783 wxString
& insert(size_t nPos
, size_t n
, wxUniChar ch
)
2785 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2787 #if wxUSE_UNICODE_UTF8
2788 if ( !ch
.IsAscii() )
2789 m_impl
.insert(PosToImpl(nPos
), wxStringOperations::EncodeNChars(n
, ch
));
2792 m_impl
.insert(PosToImpl(nPos
), n
, (wxStringCharType
)ch
);
2796 iterator
insert(iterator it
, wxUniChar ch
)
2798 wxSTRING_UPDATE_CACHED_LENGTH(1);
2800 #if wxUSE_UNICODE_UTF8
2801 if ( !ch
.IsAscii() )
2803 size_t pos
= IterToImplPos(it
);
2804 m_impl
.insert(pos
, wxStringOperations::EncodeChar(ch
));
2805 return iterator(this, m_impl
.begin() + pos
);
2809 return iterator(this, m_impl
.insert(it
.impl(), (wxStringCharType
)ch
));
2812 void insert(iterator it
, const_iterator first
, const_iterator last
)
2814 wxSTRING_INVALIDATE_CACHE();
2816 m_impl
.insert(it
.impl(), first
.impl(), last
.impl());
2819 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2820 void insert(iterator it
, const char *first
, const char *last
)
2821 { insert(it
- begin(), first
, last
- first
); }
2822 void insert(iterator it
, const wchar_t *first
, const wchar_t *last
)
2823 { insert(it
- begin(), first
, last
- first
); }
2824 void insert(iterator it
, const wxCStrData
& first
, const wxCStrData
& last
)
2825 { insert(it
, CreateConstIterator(first
), CreateConstIterator(last
)); }
2826 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2828 void insert(iterator it
, size_type n
, wxUniChar ch
)
2830 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2832 #if wxUSE_UNICODE_UTF8
2833 if ( !ch
.IsAscii() )
2834 m_impl
.insert(IterToImplPos(it
), wxStringOperations::EncodeNChars(n
, ch
));
2837 m_impl
.insert(it
.impl(), n
, (wxStringCharType
)ch
);
2840 // delete characters from nStart to nStart + nLen
2841 wxString
& erase(size_type pos
= 0, size_type n
= npos
)
2843 wxSTRING_INVALIDATE_CACHE();
2846 PosLenToImpl(pos
, n
, &from
, &len
);
2847 m_impl
.erase(from
, len
);
2852 // delete characters from first up to last
2853 iterator
erase(iterator first
, iterator last
)
2855 wxSTRING_INVALIDATE_CACHE();
2857 return iterator(this, m_impl
.erase(first
.impl(), last
.impl()));
2860 iterator
erase(iterator first
)
2862 wxSTRING_UPDATE_CACHED_LENGTH(-1);
2864 return iterator(this, m_impl
.erase(first
.impl()));
2867 #ifdef wxSTRING_BASE_HASNT_CLEAR
2868 void clear() { erase(); }
2872 wxSTRING_SET_CACHED_LENGTH(0);
2878 // replaces the substring of length nLen starting at nStart
2879 wxString
& replace(size_t nStart
, size_t nLen
, const char* sz
)
2881 wxSTRING_INVALIDATE_CACHE();
2884 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2885 m_impl
.replace(from
, len
, ImplStr(sz
));
2890 wxString
& replace(size_t nStart
, size_t nLen
, const wchar_t* sz
)
2892 wxSTRING_INVALIDATE_CACHE();
2895 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2896 m_impl
.replace(from
, len
, ImplStr(sz
));
2901 // replaces the substring of length nLen starting at nStart
2902 wxString
& replace(size_t nStart
, size_t nLen
, const wxString
& str
)
2904 wxSTRING_INVALIDATE_CACHE();
2907 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2908 m_impl
.replace(from
, len
, str
.m_impl
);
2913 // replaces the substring with nCount copies of ch
2914 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxUniChar ch
)
2916 wxSTRING_INVALIDATE_CACHE();
2919 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2920 #if wxUSE_UNICODE_UTF8
2921 if ( !ch
.IsAscii() )
2922 m_impl
.replace(from
, len
, wxStringOperations::EncodeNChars(nCount
, ch
));
2925 m_impl
.replace(from
, len
, nCount
, (wxStringCharType
)ch
);
2930 // replaces a substring with another substring
2931 wxString
& replace(size_t nStart
, size_t nLen
,
2932 const wxString
& str
, size_t nStart2
, size_t nLen2
)
2934 wxSTRING_INVALIDATE_CACHE();
2937 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2940 str
.PosLenToImpl(nStart2
, nLen2
, &from2
, &len2
);
2942 m_impl
.replace(from
, len
, str
.m_impl
, from2
, len2
);
2947 // replaces the substring with first nCount chars of sz
2948 wxString
& replace(size_t nStart
, size_t nLen
,
2949 const char* sz
, size_t nCount
)
2951 wxSTRING_INVALIDATE_CACHE();
2954 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2956 SubstrBufFromMB
str(ImplStr(sz
, nCount
));
2958 m_impl
.replace(from
, len
, str
.data
, str
.len
);
2963 wxString
& replace(size_t nStart
, size_t nLen
,
2964 const wchar_t* sz
, size_t nCount
)
2966 wxSTRING_INVALIDATE_CACHE();
2969 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2971 SubstrBufFromWC
str(ImplStr(sz
, nCount
));
2973 m_impl
.replace(from
, len
, str
.data
, str
.len
);
2978 wxString
& replace(size_t nStart
, size_t nLen
,
2979 const wxString
& s
, size_t nCount
)
2981 wxSTRING_INVALIDATE_CACHE();
2984 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2985 m_impl
.replace(from
, len
, s
.m_impl
.c_str(), s
.LenToImpl(nCount
));
2990 wxString
& replace(iterator first
, iterator last
, const char* s
)
2992 wxSTRING_INVALIDATE_CACHE();
2994 m_impl
.replace(first
.impl(), last
.impl(), ImplStr(s
));
2999 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
)
3001 wxSTRING_INVALIDATE_CACHE();
3003 m_impl
.replace(first
.impl(), last
.impl(), ImplStr(s
));
3008 wxString
& replace(iterator first
, iterator last
, const char* s
, size_type n
)
3010 wxSTRING_INVALIDATE_CACHE();
3012 SubstrBufFromMB
str(ImplStr(s
, n
));
3013 m_impl
.replace(first
.impl(), last
.impl(), str
.data
, str
.len
);
3018 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
, size_type n
)
3020 wxSTRING_INVALIDATE_CACHE();
3022 SubstrBufFromWC
str(ImplStr(s
, n
));
3023 m_impl
.replace(first
.impl(), last
.impl(), str
.data
, str
.len
);
3028 wxString
& replace(iterator first
, iterator last
, const wxString
& s
)
3030 wxSTRING_INVALIDATE_CACHE();
3032 m_impl
.replace(first
.impl(), last
.impl(), s
.m_impl
);
3037 wxString
& replace(iterator first
, iterator last
, size_type n
, wxUniChar ch
)
3039 wxSTRING_INVALIDATE_CACHE();
3041 #if wxUSE_UNICODE_UTF8
3042 if ( !ch
.IsAscii() )
3043 m_impl
.replace(first
.impl(), last
.impl(),
3044 wxStringOperations::EncodeNChars(n
, ch
));
3047 m_impl
.replace(first
.impl(), last
.impl(), n
, (wxStringCharType
)ch
);
3052 wxString
& replace(iterator first
, iterator last
,
3053 const_iterator first1
, const_iterator last1
)
3055 wxSTRING_INVALIDATE_CACHE();
3057 m_impl
.replace(first
.impl(), last
.impl(), first1
.impl(), last1
.impl());
3062 wxString
& replace(iterator first
, iterator last
,
3063 const char *first1
, const char *last1
)
3064 { replace(first
, last
, first1
, last1
- first1
); return *this; }
3065 wxString
& replace(iterator first
, iterator last
,
3066 const wchar_t *first1
, const wchar_t *last1
)
3067 { replace(first
, last
, first1
, last1
- first1
); return *this; }
3070 void swap(wxString
& str
)
3072 #if wxUSE_STRING_POS_CACHE
3073 // we modify not only this string but also the other one directly so we
3074 // need to invalidate cache for both of them (we could also try to
3075 // exchange their cache entries but it seems unlikely to be worth it)
3077 str
.InvalidateCache();
3078 #endif // wxUSE_STRING_POS_CACHE
3080 m_impl
.swap(str
.m_impl
);
3084 size_t find(const wxString
& str
, size_t nStart
= 0) const
3085 { return PosFromImpl(m_impl
.find(str
.m_impl
, PosToImpl(nStart
))); }
3087 // find first n characters of sz
3088 size_t find(const char* sz
, size_t nStart
= 0, size_t n
= npos
) const
3090 SubstrBufFromMB
str(ImplStr(sz
, n
));
3091 return PosFromImpl(m_impl
.find(str
.data
, PosToImpl(nStart
), str
.len
));
3093 size_t find(const wchar_t* sz
, size_t nStart
= 0, size_t n
= npos
) const
3095 SubstrBufFromWC
str(ImplStr(sz
, n
));
3096 return PosFromImpl(m_impl
.find(str
.data
, PosToImpl(nStart
), str
.len
));
3098 size_t find(const wxScopedCharBuffer
& s
, size_t nStart
= 0, size_t n
= npos
) const
3099 { return find(s
.data(), nStart
, n
); }
3100 size_t find(const wxScopedWCharBuffer
& s
, size_t nStart
= 0, size_t n
= npos
) const
3101 { return find(s
.data(), nStart
, n
); }
3102 size_t find(const wxCStrData
& s
, size_t nStart
= 0, size_t n
= npos
) const
3103 { return find(s
.AsWChar(), nStart
, n
); }
3105 // find the first occurrence of character ch after nStart
3106 size_t find(wxUniChar ch
, size_t nStart
= 0) const
3108 #if wxUSE_UNICODE_UTF8
3109 if ( !ch
.IsAscii() )
3110 return PosFromImpl(m_impl
.find(wxStringOperations::EncodeChar(ch
),
3111 PosToImpl(nStart
)));
3114 return PosFromImpl(m_impl
.find((wxStringCharType
)ch
,
3115 PosToImpl(nStart
)));
3118 size_t find(wxUniCharRef ch
, size_t nStart
= 0) const
3119 { return find(wxUniChar(ch
), nStart
); }
3120 size_t find(char ch
, size_t nStart
= 0) const
3121 { return find(wxUniChar(ch
), nStart
); }
3122 size_t find(unsigned char ch
, size_t nStart
= 0) const
3123 { return find(wxUniChar(ch
), nStart
); }
3124 size_t find(wchar_t ch
, size_t nStart
= 0) const
3125 { return find(wxUniChar(ch
), nStart
); }
3127 // rfind() family is exactly like find() but works right to left
3129 // as find, but from the end
3130 size_t rfind(const wxString
& str
, size_t nStart
= npos
) const
3131 { return PosFromImpl(m_impl
.rfind(str
.m_impl
, PosToImpl(nStart
))); }
3133 // as find, but from the end
3134 size_t rfind(const char* sz
, size_t nStart
= npos
, size_t n
= npos
) const
3136 SubstrBufFromMB
str(ImplStr(sz
, n
));
3137 return PosFromImpl(m_impl
.rfind(str
.data
, PosToImpl(nStart
), str
.len
));
3139 size_t rfind(const wchar_t* sz
, size_t nStart
= npos
, size_t n
= npos
) const
3141 SubstrBufFromWC
str(ImplStr(sz
, n
));
3142 return PosFromImpl(m_impl
.rfind(str
.data
, PosToImpl(nStart
), str
.len
));
3144 size_t rfind(const wxScopedCharBuffer
& s
, size_t nStart
= npos
, size_t n
= npos
) const
3145 { return rfind(s
.data(), nStart
, n
); }
3146 size_t rfind(const wxScopedWCharBuffer
& s
, size_t nStart
= npos
, size_t n
= npos
) const
3147 { return rfind(s
.data(), nStart
, n
); }
3148 size_t rfind(const wxCStrData
& s
, size_t nStart
= npos
, size_t n
= npos
) const
3149 { return rfind(s
.AsWChar(), nStart
, n
); }
3150 // as find, but from the end
3151 size_t rfind(wxUniChar ch
, size_t nStart
= npos
) const
3153 #if wxUSE_UNICODE_UTF8
3154 if ( !ch
.IsAscii() )
3155 return PosFromImpl(m_impl
.rfind(wxStringOperations::EncodeChar(ch
),
3156 PosToImpl(nStart
)));
3159 return PosFromImpl(m_impl
.rfind((wxStringCharType
)ch
,
3160 PosToImpl(nStart
)));
3162 size_t rfind(wxUniCharRef ch
, size_t nStart
= npos
) const
3163 { return rfind(wxUniChar(ch
), nStart
); }
3164 size_t rfind(char ch
, size_t nStart
= npos
) const
3165 { return rfind(wxUniChar(ch
), nStart
); }
3166 size_t rfind(unsigned char ch
, size_t nStart
= npos
) const
3167 { return rfind(wxUniChar(ch
), nStart
); }
3168 size_t rfind(wchar_t ch
, size_t nStart
= npos
) const
3169 { return rfind(wxUniChar(ch
), nStart
); }
3171 // find first/last occurrence of any character (not) in the set:
3172 #if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
3173 // FIXME-UTF8: this is not entirely correct, because it doesn't work if
3174 // sizeof(wchar_t)==2 and surrogates are present in the string;
3175 // should we care? Probably not.
3176 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
3177 { return m_impl
.find_first_of(str
.m_impl
, nStart
); }
3178 size_t find_first_of(const char* sz
, size_t nStart
= 0) const
3179 { return m_impl
.find_first_of(ImplStr(sz
), nStart
); }
3180 size_t find_first_of(const wchar_t* sz
, size_t nStart
= 0) const
3181 { return m_impl
.find_first_of(ImplStr(sz
), nStart
); }
3182 size_t find_first_of(const char* sz
, size_t nStart
, size_t n
) const
3183 { return m_impl
.find_first_of(ImplStr(sz
), nStart
, n
); }
3184 size_t find_first_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
3185 { return m_impl
.find_first_of(ImplStr(sz
), nStart
, n
); }
3186 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const
3187 { return m_impl
.find_first_of((wxChar
)c
, nStart
); }
3189 size_t find_last_of(const wxString
& str
, size_t nStart
= npos
) const
3190 { return m_impl
.find_last_of(str
.m_impl
, nStart
); }
3191 size_t find_last_of(const char* sz
, size_t nStart
= npos
) const
3192 { return m_impl
.find_last_of(ImplStr(sz
), nStart
); }
3193 size_t find_last_of(const wchar_t* sz
, size_t nStart
= npos
) const
3194 { return m_impl
.find_last_of(ImplStr(sz
), nStart
); }
3195 size_t find_last_of(const char* sz
, size_t nStart
, size_t n
) const
3196 { return m_impl
.find_last_of(ImplStr(sz
), nStart
, n
); }
3197 size_t find_last_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
3198 { return m_impl
.find_last_of(ImplStr(sz
), nStart
, n
); }
3199 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const
3200 { return m_impl
.find_last_of((wxChar
)c
, nStart
); }
3202 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
3203 { return m_impl
.find_first_not_of(str
.m_impl
, nStart
); }
3204 size_t find_first_not_of(const char* sz
, size_t nStart
= 0) const
3205 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
); }
3206 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
= 0) const
3207 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
); }
3208 size_t find_first_not_of(const char* sz
, size_t nStart
, size_t n
) const
3209 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
, n
); }
3210 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
3211 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
, n
); }
3212 size_t find_first_not_of(wxUniChar c
, size_t nStart
= 0) const
3213 { return m_impl
.find_first_not_of((wxChar
)c
, nStart
); }
3215 size_t find_last_not_of(const wxString
& str
, size_t nStart
= npos
) const
3216 { return m_impl
.find_last_not_of(str
.m_impl
, nStart
); }
3217 size_t find_last_not_of(const char* sz
, size_t nStart
= npos
) const
3218 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
); }
3219 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
= npos
) const
3220 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
); }
3221 size_t find_last_not_of(const char* sz
, size_t nStart
, size_t n
) const
3222 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
, n
); }
3223 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
3224 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
, n
); }
3225 size_t find_last_not_of(wxUniChar c
, size_t nStart
= npos
) const
3226 { return m_impl
.find_last_not_of((wxChar
)c
, nStart
); }
3228 // we can't use std::string implementation in UTF-8 build, because the
3229 // character sets would be interpreted wrongly:
3231 // as strpbrk() but starts at nStart, returns npos if not found
3232 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
3233 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3234 { return find_first_of(str
.wc_str(), nStart
); }
3236 { return find_first_of(str
.mb_str(), nStart
); }
3239 size_t find_first_of(const char* sz
, size_t nStart
= 0) const;
3240 size_t find_first_of(const wchar_t* sz
, size_t nStart
= 0) const;
3241 size_t find_first_of(const char* sz
, size_t nStart
, size_t n
) const;
3242 size_t find_first_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
3243 // same as find(char, size_t)
3244 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const
3245 { return find(c
, nStart
); }
3246 // find the last (starting from nStart) char from str in this string
3247 size_t find_last_of (const wxString
& str
, size_t nStart
= npos
) const
3248 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3249 { return find_last_of(str
.wc_str(), nStart
); }
3251 { return find_last_of(str
.mb_str(), nStart
); }
3254 size_t find_last_of (const char* sz
, size_t nStart
= npos
) const;
3255 size_t find_last_of (const wchar_t* sz
, size_t nStart
= npos
) const;
3256 size_t find_last_of(const char* sz
, size_t nStart
, size_t n
) const;
3257 size_t find_last_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
3259 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const
3260 { return rfind(c
, nStart
); }
3262 // find first/last occurrence of any character not in the set
3264 // as strspn() (starting from nStart), returns npos on failure
3265 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
3266 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3267 { return find_first_not_of(str
.wc_str(), nStart
); }
3269 { return find_first_not_of(str
.mb_str(), nStart
); }
3272 size_t find_first_not_of(const char* sz
, size_t nStart
= 0) const;
3273 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
= 0) const;
3274 size_t find_first_not_of(const char* sz
, size_t nStart
, size_t n
) const;
3275 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
3277 size_t find_first_not_of(wxUniChar ch
, size_t nStart
= 0) const;
3279 size_t find_last_not_of(const wxString
& str
, size_t nStart
= npos
) const
3280 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3281 { return find_last_not_of(str
.wc_str(), nStart
); }
3283 { return find_last_not_of(str
.mb_str(), nStart
); }
3286 size_t find_last_not_of(const char* sz
, size_t nStart
= npos
) const;
3287 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
= npos
) const;
3288 size_t find_last_not_of(const char* sz
, size_t nStart
, size_t n
) const;
3289 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
3291 size_t find_last_not_of(wxUniChar ch
, size_t nStart
= npos
) const;
3292 #endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not
3294 // provide char/wchar_t/wxUniCharRef overloads for char-finding functions
3295 // above to resolve ambiguities:
3296 size_t find_first_of(wxUniCharRef ch
, size_t nStart
= 0) const
3297 { return find_first_of(wxUniChar(ch
), nStart
); }
3298 size_t find_first_of(char ch
, size_t nStart
= 0) const
3299 { return find_first_of(wxUniChar(ch
), nStart
); }
3300 size_t find_first_of(unsigned char ch
, size_t nStart
= 0) const
3301 { return find_first_of(wxUniChar(ch
), nStart
); }
3302 size_t find_first_of(wchar_t ch
, size_t nStart
= 0) const
3303 { return find_first_of(wxUniChar(ch
), nStart
); }
3304 size_t find_last_of(wxUniCharRef ch
, size_t nStart
= npos
) const
3305 { return find_last_of(wxUniChar(ch
), nStart
); }
3306 size_t find_last_of(char ch
, size_t nStart
= npos
) const
3307 { return find_last_of(wxUniChar(ch
), nStart
); }
3308 size_t find_last_of(unsigned char ch
, size_t nStart
= npos
) const
3309 { return find_last_of(wxUniChar(ch
), nStart
); }
3310 size_t find_last_of(wchar_t ch
, size_t nStart
= npos
) const
3311 { return find_last_of(wxUniChar(ch
), nStart
); }
3312 size_t find_first_not_of(wxUniCharRef ch
, size_t nStart
= 0) const
3313 { return find_first_not_of(wxUniChar(ch
), nStart
); }
3314 size_t find_first_not_of(char ch
, size_t nStart
= 0) const
3315 { return find_first_not_of(wxUniChar(ch
), nStart
); }
3316 size_t find_first_not_of(unsigned char ch
, size_t nStart
= 0) const
3317 { return find_first_not_of(wxUniChar(ch
), nStart
); }
3318 size_t find_first_not_of(wchar_t ch
, size_t nStart
= 0) const
3319 { return find_first_not_of(wxUniChar(ch
), nStart
); }
3320 size_t find_last_not_of(wxUniCharRef ch
, size_t nStart
= npos
) const
3321 { return find_last_not_of(wxUniChar(ch
), nStart
); }
3322 size_t find_last_not_of(char ch
, size_t nStart
= npos
) const
3323 { return find_last_not_of(wxUniChar(ch
), nStart
); }
3324 size_t find_last_not_of(unsigned char ch
, size_t nStart
= npos
) const
3325 { return find_last_not_of(wxUniChar(ch
), nStart
); }
3326 size_t find_last_not_of(wchar_t ch
, size_t nStart
= npos
) const
3327 { return find_last_not_of(wxUniChar(ch
), nStart
); }
3329 // and additional overloads for the versions taking strings:
3330 size_t find_first_of(const wxCStrData
& sz
, size_t nStart
= 0) const
3331 { return find_first_of(sz
.AsString(), nStart
); }
3332 size_t find_first_of(const wxScopedCharBuffer
& sz
, size_t nStart
= 0) const
3333 { return find_first_of(sz
.data(), nStart
); }
3334 size_t find_first_of(const wxScopedWCharBuffer
& sz
, size_t nStart
= 0) const
3335 { return find_first_of(sz
.data(), nStart
); }
3336 size_t find_first_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
3337 { return find_first_of(sz
.AsWChar(), nStart
, n
); }
3338 size_t find_first_of(const wxScopedCharBuffer
& sz
, size_t nStart
, size_t n
) const
3339 { return find_first_of(sz
.data(), nStart
, n
); }
3340 size_t find_first_of(const wxScopedWCharBuffer
& sz
, size_t nStart
, size_t n
) const
3341 { return find_first_of(sz
.data(), nStart
, n
); }
3343 size_t find_last_of(const wxCStrData
& sz
, size_t nStart
= 0) const
3344 { return find_last_of(sz
.AsString(), nStart
); }
3345 size_t find_last_of(const wxScopedCharBuffer
& sz
, size_t nStart
= 0) const
3346 { return find_last_of(sz
.data(), nStart
); }
3347 size_t find_last_of(const wxScopedWCharBuffer
& sz
, size_t nStart
= 0) const
3348 { return find_last_of(sz
.data(), nStart
); }
3349 size_t find_last_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
3350 { return find_last_of(sz
.AsWChar(), nStart
, n
); }
3351 size_t find_last_of(const wxScopedCharBuffer
& sz
, size_t nStart
, size_t n
) const
3352 { return find_last_of(sz
.data(), nStart
, n
); }
3353 size_t find_last_of(const wxScopedWCharBuffer
& sz
, size_t nStart
, size_t n
) const
3354 { return find_last_of(sz
.data(), nStart
, n
); }
3356 size_t find_first_not_of(const wxCStrData
& sz
, size_t nStart
= 0) const
3357 { return find_first_not_of(sz
.AsString(), nStart
); }
3358 size_t find_first_not_of(const wxScopedCharBuffer
& sz
, size_t nStart
= 0) const
3359 { return find_first_not_of(sz
.data(), nStart
); }
3360 size_t find_first_not_of(const wxScopedWCharBuffer
& sz
, size_t nStart
= 0) const
3361 { return find_first_not_of(sz
.data(), nStart
); }
3362 size_t find_first_not_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
3363 { return find_first_not_of(sz
.AsWChar(), nStart
, n
); }
3364 size_t find_first_not_of(const wxScopedCharBuffer
& sz
, size_t nStart
, size_t n
) const
3365 { return find_first_not_of(sz
.data(), nStart
, n
); }
3366 size_t find_first_not_of(const wxScopedWCharBuffer
& sz
, size_t nStart
, size_t n
) const
3367 { return find_first_not_of(sz
.data(), nStart
, n
); }
3369 size_t find_last_not_of(const wxCStrData
& sz
, size_t nStart
= 0) const
3370 { return find_last_not_of(sz
.AsString(), nStart
); }
3371 size_t find_last_not_of(const wxScopedCharBuffer
& sz
, size_t nStart
= 0) const
3372 { return find_last_not_of(sz
.data(), nStart
); }
3373 size_t find_last_not_of(const wxScopedWCharBuffer
& sz
, size_t nStart
= 0) const
3374 { return find_last_not_of(sz
.data(), nStart
); }
3375 size_t find_last_not_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
3376 { return find_last_not_of(sz
.AsWChar(), nStart
, n
); }
3377 size_t find_last_not_of(const wxScopedCharBuffer
& sz
, size_t nStart
, size_t n
) const
3378 { return find_last_not_of(sz
.data(), nStart
, n
); }
3379 size_t find_last_not_of(const wxScopedWCharBuffer
& sz
, size_t nStart
, size_t n
) const
3380 { return find_last_not_of(sz
.data(), nStart
, n
); }
3383 wxString
& operator+=(const wxString
& s
)
3385 wxSTRING_INVALIDATE_CACHED_LENGTH();
3390 // string += C string
3391 wxString
& operator+=(const char *psz
)
3393 wxSTRING_INVALIDATE_CACHED_LENGTH();
3395 m_impl
+= ImplStr(psz
);
3398 wxString
& operator+=(const wchar_t *pwz
)
3400 wxSTRING_INVALIDATE_CACHED_LENGTH();
3402 m_impl
+= ImplStr(pwz
);
3405 wxString
& operator+=(const wxCStrData
& s
)
3407 wxSTRING_INVALIDATE_CACHED_LENGTH();
3409 m_impl
+= s
.AsString().m_impl
;
3412 wxString
& operator+=(const wxScopedCharBuffer
& s
)
3413 { return append(s
); }
3414 wxString
& operator+=(const wxScopedWCharBuffer
& s
)
3415 { return append(s
); }
3417 wxString
& operator+=(wxUniChar ch
)
3419 wxSTRING_UPDATE_CACHED_LENGTH(1);
3421 #if wxUSE_UNICODE_UTF8
3422 if ( !ch
.IsAscii() )
3423 m_impl
+= wxStringOperations::EncodeChar(ch
);
3426 m_impl
+= (wxStringCharType
)ch
;
3429 wxString
& operator+=(wxUniCharRef ch
) { return *this += wxUniChar(ch
); }
3430 wxString
& operator+=(int ch
) { return *this += wxUniChar(ch
); }
3431 wxString
& operator+=(char ch
) { return *this += wxUniChar(ch
); }
3432 wxString
& operator+=(unsigned char ch
) { return *this += wxUniChar(ch
); }
3433 wxString
& operator+=(wchar_t ch
) { return *this += wxUniChar(ch
); }
3436 #if !wxUSE_STL_BASED_WXSTRING
3437 // helpers for wxStringBuffer and wxStringBufferLength
3438 wxStringCharType
*DoGetWriteBuf(size_t nLen
)
3440 return m_impl
.DoGetWriteBuf(nLen
);
3443 void DoUngetWriteBuf()
3445 wxSTRING_INVALIDATE_CACHE();
3447 m_impl
.DoUngetWriteBuf();
3450 void DoUngetWriteBuf(size_t nLen
)
3452 wxSTRING_INVALIDATE_CACHE();
3454 m_impl
.DoUngetWriteBuf(nLen
);
3456 #endif // !wxUSE_STL_BASED_WXSTRING
3458 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
3459 #if !wxUSE_UTF8_LOCALE_ONLY
3460 int DoPrintfWchar(const wxChar
*format
, ...);
3461 static wxString
DoFormatWchar(const wxChar
*format
, ...);
3463 #if wxUSE_UNICODE_UTF8
3464 int DoPrintfUtf8(const char *format
, ...);
3465 static wxString
DoFormatUtf8(const char *format
, ...);
3469 #if !wxUSE_STL_BASED_WXSTRING
3470 // check string's data validity
3471 bool IsValid() const { return m_impl
.GetStringData()->IsValid(); }
3475 wxStringImpl m_impl
;
3477 // buffers for compatibility conversion from (char*)c_str() and
3478 // (wchar_t*)c_str(): the pointers returned by these functions should remain
3479 // valid until the string itself is modified for compatibility with the
3480 // existing code and consistency with std::string::c_str() so returning a
3481 // temporary buffer won't do and we need to cache the conversion results
3483 // TODO-UTF8: benchmark various approaches to keeping compatibility buffers
3484 template<typename T
>
3485 struct ConvertedBuffer
3487 // notice that there is no need to initialize m_len here as it's unused
3488 // as long as m_str is NULL
3489 ConvertedBuffer() : m_str(NULL
) {}
3493 bool Extend(size_t len
)
3495 // add extra 1 for the trailing NUL
3496 void * const str
= realloc(m_str
, sizeof(T
)*(len
+ 1));
3500 m_str
= static_cast<T
*>(str
);
3506 const wxScopedCharTypeBuffer
<T
> AsScopedBuffer() const
3508 return wxScopedCharTypeBuffer
<T
>::CreateNonOwned(m_str
, m_len
);
3511 T
*m_str
; // pointer to the string data
3512 size_t m_len
; // length, not size, i.e. in chars and without last NUL
3517 // common mb_str() and wxCStrData::AsChar() helper: performs the conversion
3518 // and returns either m_convertedToChar.m_str (in which case its m_len is
3519 // also updated) or NULL if it failed
3521 // there is an important exception: in wxUSE_UNICODE_UTF8 build if conv is a
3522 // UTF-8 one, we return m_impl.c_str() directly, without doing any conversion
3523 // as optimization and so the caller needs to check for this before using
3524 // m_convertedToChar
3526 // NB: AsChar() returns char* in any build, unlike mb_str()
3527 const char *AsChar(const wxMBConv
& conv
) const;
3529 // mb_str() implementation helper
3530 wxScopedCharBuffer
AsCharBuf(const wxMBConv
& conv
) const
3532 #if wxUSE_UNICODE_UTF8
3533 // avoid conversion if we can
3534 if ( conv
.IsUTF8() )
3536 return wxScopedCharBuffer::CreateNonOwned(m_impl
.c_str(),
3539 #endif // wxUSE_UNICODE_UTF8
3541 // call this solely in order to fill in m_convertedToChar as AsChar()
3542 // updates it as a side effect: this is a bit ugly but it's a completely
3543 // internal function so the users of this class shouldn't care or know
3544 // about it and doing it like this, i.e. having a separate AsChar(),
3545 // allows us to avoid the creation and destruction of a temporary buffer
3546 // when using wxCStrData without duplicating any code
3547 if ( !AsChar(conv
) )
3549 // although it would be probably more correct to return NULL buffer
3550 // from here if the conversion fails, a lot of existing code doesn't
3551 // expect mb_str() (or wc_str()) to ever return NULL so return an
3552 // empty string otherwise to avoid crashes in it
3554 // also, some existing code does check for the conversion success and
3555 // so asserting here would be bad too -- even if it does mean that
3556 // silently losing data is possible for badly written code
3557 return wxScopedCharBuffer::CreateNonOwned("", 0);
3560 return m_convertedToChar
.AsScopedBuffer();
3563 ConvertedBuffer
<char> m_convertedToChar
;
3564 #endif // !wxUSE_UNICODE
3566 #if !wxUSE_UNICODE_WCHAR
3567 // common wc_str() and wxCStrData::AsWChar() helper for both UTF-8 and ANSI
3568 // builds: converts the string contents into m_convertedToWChar and returns
3569 // NULL if the conversion failed (this can only happen in ANSI build)
3571 // NB: AsWChar() returns wchar_t* in any build, unlike wc_str()
3572 const wchar_t *AsWChar(const wxMBConv
& conv
) const;
3574 // wc_str() implementation helper
3575 wxScopedWCharBuffer
AsWCharBuf(const wxMBConv
& conv
) const
3577 if ( !AsWChar(conv
) )
3578 return wxScopedWCharBuffer::CreateNonOwned(L
"", 0);
3580 return m_convertedToWChar
.AsScopedBuffer();
3583 ConvertedBuffer
<wchar_t> m_convertedToWChar
;
3584 #endif // !wxUSE_UNICODE_WCHAR
3586 #if wxUSE_UNICODE_UTF8
3587 // FIXME-UTF8: (try to) move this elsewhere (TLS) or solve differently
3588 // assigning to character pointer to by wxString::iterator may
3589 // change the underlying wxStringImpl iterator, so we have to
3590 // keep track of all iterators and update them as necessary:
3591 struct wxStringIteratorNodeHead
3593 wxStringIteratorNodeHead() : ptr(NULL
) {}
3594 wxStringIteratorNode
*ptr
;
3596 // copying is disallowed as it would result in more than one pointer into
3597 // the same linked list
3598 wxDECLARE_NO_COPY_CLASS(wxStringIteratorNodeHead
);
3601 wxStringIteratorNodeHead m_iterators
;
3603 friend class WXDLLIMPEXP_FWD_BASE wxStringIteratorNode
;
3604 friend class WXDLLIMPEXP_FWD_BASE wxUniCharRef
;
3605 #endif // wxUSE_UNICODE_UTF8
3607 friend class WXDLLIMPEXP_FWD_BASE wxCStrData
;
3608 friend class wxStringInternalBuffer
;
3609 friend class wxStringInternalBufferLength
;
3612 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
3613 #pragma warning (pop)
3616 // string iterator operators that satisfy STL Random Access Iterator
3618 inline wxString::iterator
operator+(ptrdiff_t n
, wxString::iterator i
)
3620 inline wxString::const_iterator
operator+(ptrdiff_t n
, wxString::const_iterator i
)
3622 inline wxString::reverse_iterator
operator+(ptrdiff_t n
, wxString::reverse_iterator i
)
3624 inline wxString::const_reverse_iterator
operator+(ptrdiff_t n
, wxString::const_reverse_iterator i
)
3627 // notice that even though for many compilers the friend declarations above are
3628 // enough, from the point of view of C++ standard we must have the declarations
3629 // here as friend ones are not injected in the enclosing namespace and without
3630 // them the code fails to compile with conforming compilers such as xlC or g++4
3631 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
, const wxString
& string2
);
3632 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const char *psz
);
3633 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const wchar_t *pwz
);
3634 wxString WXDLLIMPEXP_BASE
operator+(const char *psz
, const wxString
& string
);
3635 wxString WXDLLIMPEXP_BASE
operator+(const wchar_t *pwz
, const wxString
& string
);
3637 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
3638 wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
3640 inline wxString
operator+(const wxString
& string
, wxUniCharRef ch
)
3641 { return string
+ (wxUniChar
)ch
; }
3642 inline wxString
operator+(const wxString
& string
, char ch
)
3643 { return string
+ wxUniChar(ch
); }
3644 inline wxString
operator+(const wxString
& string
, wchar_t ch
)
3645 { return string
+ wxUniChar(ch
); }
3646 inline wxString
operator+(wxUniCharRef ch
, const wxString
& string
)
3647 { return (wxUniChar
)ch
+ string
; }
3648 inline wxString
operator+(char ch
, const wxString
& string
)
3649 { return wxUniChar(ch
) + string
; }
3650 inline wxString
operator+(wchar_t ch
, const wxString
& string
)
3651 { return wxUniChar(ch
) + string
; }
3654 #define wxGetEmptyString() wxString()
3656 // ----------------------------------------------------------------------------
3657 // helper functions which couldn't be defined inline
3658 // ----------------------------------------------------------------------------
3663 #if wxUSE_UNICODE_WCHAR
3666 struct wxStringAsBufHelper
<char>
3668 static wxScopedCharBuffer
Get(const wxString
& s
, size_t *len
)
3670 wxScopedCharBuffer
buf(s
.mb_str());
3672 *len
= buf
? strlen(buf
) : 0;
3678 struct wxStringAsBufHelper
<wchar_t>
3680 static wxScopedWCharBuffer
Get(const wxString
& s
, size_t *len
)
3682 const size_t length
= s
.length();
3685 return wxScopedWCharBuffer::CreateNonOwned(s
.wx_str(), length
);
3689 #elif wxUSE_UNICODE_UTF8
3692 struct wxStringAsBufHelper
<char>
3694 static wxScopedCharBuffer
Get(const wxString
& s
, size_t *len
)
3696 const size_t length
= s
.utf8_length();
3699 return wxScopedCharBuffer::CreateNonOwned(s
.wx_str(), length
);
3704 struct wxStringAsBufHelper
<wchar_t>
3706 static wxScopedWCharBuffer
Get(const wxString
& s
, size_t *len
)
3708 wxScopedWCharBuffer
wbuf(s
.wc_str());
3710 *len
= wxWcslen(wbuf
);
3715 #endif // Unicode build kind
3717 } // namespace wxPrivate
3719 // ----------------------------------------------------------------------------
3720 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
3721 // ----------------------------------------------------------------------------
3723 #if !wxUSE_STL_BASED_WXSTRING
3724 // string buffer for direct access to string data in their native
3726 class wxStringInternalBuffer
3729 typedef wxStringCharType CharType
;
3731 wxStringInternalBuffer(wxString
& str
, size_t lenWanted
= 1024)
3732 : m_str(str
), m_buf(NULL
)
3733 { m_buf
= m_str
.DoGetWriteBuf(lenWanted
); }
3735 ~wxStringInternalBuffer() { m_str
.DoUngetWriteBuf(); }
3737 operator wxStringCharType
*() const { return m_buf
; }
3741 wxStringCharType
*m_buf
;
3743 wxDECLARE_NO_COPY_CLASS(wxStringInternalBuffer
);
3746 class wxStringInternalBufferLength
3749 typedef wxStringCharType CharType
;
3751 wxStringInternalBufferLength(wxString
& str
, size_t lenWanted
= 1024)
3752 : m_str(str
), m_buf(NULL
), m_len(0), m_lenSet(false)
3754 m_buf
= m_str
.DoGetWriteBuf(lenWanted
);
3755 wxASSERT(m_buf
!= NULL
);
3758 ~wxStringInternalBufferLength()
3761 m_str
.DoUngetWriteBuf(m_len
);
3764 operator wxStringCharType
*() const { return m_buf
; }
3765 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
3769 wxStringCharType
*m_buf
;
3773 wxDECLARE_NO_COPY_CLASS(wxStringInternalBufferLength
);
3776 #endif // !wxUSE_STL_BASED_WXSTRING
3778 template<typename T
>
3779 class wxStringTypeBufferBase
3784 wxStringTypeBufferBase(wxString
& str
, size_t lenWanted
= 1024)
3785 : m_str(str
), m_buf(lenWanted
)
3787 // for compatibility with old wxStringBuffer which provided direct
3788 // access to wxString internal buffer, initialize ourselves with the
3789 // string initial contents
3791 // FIXME-VC6: remove the ugly (CharType *)NULL and use normal
3792 // tchar_str<CharType>
3794 const wxCharTypeBuffer
<CharType
> buf(str
.tchar_str(&len
, (CharType
*)NULL
));
3797 if ( len
> lenWanted
)
3799 // in this case there is not enough space for terminating NUL,
3800 // ensure that we still put it there
3801 m_buf
.data()[lenWanted
] = 0;
3802 len
= lenWanted
- 1;
3805 memcpy(m_buf
.data(), buf
, (len
+ 1)*sizeof(CharType
));
3807 //else: conversion failed, this can happen when trying to get Unicode
3808 // string contents into a char string
3811 operator CharType
*() { return m_buf
.data(); }
3815 wxCharTypeBuffer
<CharType
> m_buf
;
3818 template<typename T
>
3819 class wxStringTypeBufferLengthBase
: public wxStringTypeBufferBase
<T
>
3822 wxStringTypeBufferLengthBase(wxString
& str
, size_t lenWanted
= 1024)
3823 : wxStringTypeBufferBase
<T
>(str
, lenWanted
),
3828 ~wxStringTypeBufferLengthBase()
3830 wxASSERT_MSG( this->m_lenSet
, "forgot to call SetLength()" );
3833 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
3840 template<typename T
>
3841 class wxStringTypeBuffer
: public wxStringTypeBufferBase
<T
>
3844 wxStringTypeBuffer(wxString
& str
, size_t lenWanted
= 1024)
3845 : wxStringTypeBufferBase
<T
>(str
, lenWanted
)
3848 ~wxStringTypeBuffer()
3850 this->m_str
.assign(this->m_buf
.data());
3853 wxDECLARE_NO_COPY_CLASS(wxStringTypeBuffer
);
3856 template<typename T
>
3857 class wxStringTypeBufferLength
: public wxStringTypeBufferLengthBase
<T
>
3860 wxStringTypeBufferLength(wxString
& str
, size_t lenWanted
= 1024)
3861 : wxStringTypeBufferLengthBase
<T
>(str
, lenWanted
)
3864 ~wxStringTypeBufferLength()
3866 this->m_str
.assign(this->m_buf
.data(), this->m_len
);
3869 wxDECLARE_NO_COPY_CLASS(wxStringTypeBufferLength
);
3872 #if wxUSE_STL_BASED_WXSTRING
3874 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase
<wxStringCharType
> )
3876 class wxStringInternalBuffer
: public wxStringTypeBufferBase
<wxStringCharType
>
3879 wxStringInternalBuffer(wxString
& str
, size_t lenWanted
= 1024)
3880 : wxStringTypeBufferBase
<wxStringCharType
>(str
, lenWanted
) {}
3881 ~wxStringInternalBuffer()
3882 { m_str
.m_impl
.assign(m_buf
.data()); }
3884 wxDECLARE_NO_COPY_CLASS(wxStringInternalBuffer
);
3887 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE(
3888 wxStringTypeBufferLengthBase
<wxStringCharType
> )
3890 class wxStringInternalBufferLength
3891 : public wxStringTypeBufferLengthBase
<wxStringCharType
>
3894 wxStringInternalBufferLength(wxString
& str
, size_t lenWanted
= 1024)
3895 : wxStringTypeBufferLengthBase
<wxStringCharType
>(str
, lenWanted
) {}
3897 ~wxStringInternalBufferLength()
3899 m_str
.m_impl
.assign(m_buf
.data(), m_len
);
3902 wxDECLARE_NO_COPY_CLASS(wxStringInternalBufferLength
);
3905 #endif // wxUSE_STL_BASED_WXSTRING
3908 #if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
3909 typedef wxStringTypeBuffer
<wxChar
> wxStringBuffer
;
3910 typedef wxStringTypeBufferLength
<wxChar
> wxStringBufferLength
;
3911 #else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
3912 typedef wxStringInternalBuffer wxStringBuffer
;
3913 typedef wxStringInternalBufferLength wxStringBufferLength
;
3914 #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
3916 #if wxUSE_UNICODE_UTF8
3917 typedef wxStringInternalBuffer wxUTF8StringBuffer
;
3918 typedef wxStringInternalBufferLength wxUTF8StringBufferLength
;
3919 #elif wxUSE_UNICODE_WCHAR
3921 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase
<char> )
3923 // Note about inlined dtors in the classes below: this is done not for
3924 // performance reasons but just to avoid linking errors in the MSVC DLL build
3925 // under Windows: if a class has non-inline methods it must be declared as
3926 // being DLL-exported but, due to an extremely interesting feature of MSVC 7
3927 // and later, any template class which is used as a base of a DLL-exported
3928 // class is implicitly made DLL-exported too, as explained at the bottom of
3929 // http://msdn.microsoft.com/en-us/library/twa2aw10.aspx (just to confirm: yes,
3930 // _inheriting_ from a class can change whether it is being exported from DLL)
3932 // But this results in link errors because the base template class is not DLL-
3933 // exported, whether it is declared with WXDLLIMPEXP_BASE or not, because it
3934 // does have only inline functions. So the simplest fix is to just make all the
3935 // functions of these classes inline too.
3937 class wxUTF8StringBuffer
: public wxStringTypeBufferBase
<char>
3940 wxUTF8StringBuffer(wxString
& str
, size_t lenWanted
= 1024)
3941 : wxStringTypeBufferBase
<char>(str
, lenWanted
) {}
3942 ~wxUTF8StringBuffer()
3944 wxMBConvStrictUTF8 conv
;
3945 size_t wlen
= conv
.ToWChar(NULL
, 0, m_buf
);
3946 wxCHECK_RET( wlen
!= wxCONV_FAILED
, "invalid UTF-8 data in string buffer?" );
3948 wxStringInternalBuffer
wbuf(m_str
, wlen
);
3949 conv
.ToWChar(wbuf
, wlen
, m_buf
);
3952 wxDECLARE_NO_COPY_CLASS(wxUTF8StringBuffer
);
3955 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferLengthBase
<char> )
3957 class wxUTF8StringBufferLength
: public wxStringTypeBufferLengthBase
<char>
3960 wxUTF8StringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
3961 : wxStringTypeBufferLengthBase
<char>(str
, lenWanted
) {}
3962 ~wxUTF8StringBufferLength()
3964 wxCHECK_RET(m_lenSet
, "length not set");
3966 wxMBConvStrictUTF8 conv
;
3967 size_t wlen
= conv
.ToWChar(NULL
, 0, m_buf
, m_len
);
3968 wxCHECK_RET( wlen
!= wxCONV_FAILED
, "invalid UTF-8 data in string buffer?" );
3970 wxStringInternalBufferLength
wbuf(m_str
, wlen
);
3971 conv
.ToWChar(wbuf
, wlen
, m_buf
, m_len
);
3972 wbuf
.SetLength(wlen
);
3975 wxDECLARE_NO_COPY_CLASS(wxUTF8StringBufferLength
);
3977 #endif // wxUSE_UNICODE_UTF8/wxUSE_UNICODE_WCHAR
3980 // ---------------------------------------------------------------------------
3981 // wxString comparison functions: operator versions are always case sensitive
3982 // ---------------------------------------------------------------------------
3984 #define wxCMP_WXCHAR_STRING(p, s, op) 0 op s.Cmp(p)
3986 wxDEFINE_ALL_COMPARISONS(const wxChar
*, const wxString
&, wxCMP_WXCHAR_STRING
)
3988 #undef wxCMP_WXCHAR_STRING
3990 inline bool operator==(const wxString
& s1
, const wxString
& s2
)
3991 { return s1
.IsSameAs(s2
); }
3992 inline bool operator!=(const wxString
& s1
, const wxString
& s2
)
3993 { return !s1
.IsSameAs(s2
); }
3994 inline bool operator< (const wxString
& s1
, const wxString
& s2
)
3995 { return s1
.Cmp(s2
) < 0; }
3996 inline bool operator> (const wxString
& s1
, const wxString
& s2
)
3997 { return s1
.Cmp(s2
) > 0; }
3998 inline bool operator<=(const wxString
& s1
, const wxString
& s2
)
3999 { return s1
.Cmp(s2
) <= 0; }
4000 inline bool operator>=(const wxString
& s1
, const wxString
& s2
)
4001 { return s1
.Cmp(s2
) >= 0; }
4003 inline bool operator==(const wxString
& s1
, const wxCStrData
& s2
)
4004 { return s1
== s2
.AsString(); }
4005 inline bool operator==(const wxCStrData
& s1
, const wxString
& s2
)
4006 { return s1
.AsString() == s2
; }
4007 inline bool operator!=(const wxString
& s1
, const wxCStrData
& s2
)
4008 { return s1
!= s2
.AsString(); }
4009 inline bool operator!=(const wxCStrData
& s1
, const wxString
& s2
)
4010 { return s1
.AsString() != s2
; }
4012 inline bool operator==(const wxString
& s1
, const wxScopedWCharBuffer
& s2
)
4013 { return (s1
.Cmp((const wchar_t *)s2
) == 0); }
4014 inline bool operator==(const wxScopedWCharBuffer
& s1
, const wxString
& s2
)
4015 { return (s2
.Cmp((const wchar_t *)s1
) == 0); }
4016 inline bool operator!=(const wxString
& s1
, const wxScopedWCharBuffer
& s2
)
4017 { return (s1
.Cmp((const wchar_t *)s2
) != 0); }
4018 inline bool operator!=(const wxScopedWCharBuffer
& s1
, const wxString
& s2
)
4019 { return (s2
.Cmp((const wchar_t *)s1
) != 0); }
4021 inline bool operator==(const wxString
& s1
, const wxScopedCharBuffer
& s2
)
4022 { return (s1
.Cmp((const char *)s2
) == 0); }
4023 inline bool operator==(const wxScopedCharBuffer
& s1
, const wxString
& s2
)
4024 { return (s2
.Cmp((const char *)s1
) == 0); }
4025 inline bool operator!=(const wxString
& s1
, const wxScopedCharBuffer
& s2
)
4026 { return (s1
.Cmp((const char *)s2
) != 0); }
4027 inline bool operator!=(const wxScopedCharBuffer
& s1
, const wxString
& s2
)
4028 { return (s2
.Cmp((const char *)s1
) != 0); }
4030 inline wxString
operator+(const wxString
& string
, const wxScopedWCharBuffer
& buf
)
4031 { return string
+ (const wchar_t *)buf
; }
4032 inline wxString
operator+(const wxScopedWCharBuffer
& buf
, const wxString
& string
)
4033 { return (const wchar_t *)buf
+ string
; }
4035 inline wxString
operator+(const wxString
& string
, const wxScopedCharBuffer
& buf
)
4036 { return string
+ (const char *)buf
; }
4037 inline wxString
operator+(const wxScopedCharBuffer
& buf
, const wxString
& string
)
4038 { return (const char *)buf
+ string
; }
4040 // comparison with char
4041 inline bool operator==(const wxUniChar
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
4042 inline bool operator==(const wxUniCharRef
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
4043 inline bool operator==(char c
, const wxString
& s
) { return s
.IsSameAs(c
); }
4044 inline bool operator==(wchar_t c
, const wxString
& s
) { return s
.IsSameAs(c
); }
4045 inline bool operator==(int c
, const wxString
& s
) { return s
.IsSameAs(c
); }
4046 inline bool operator==(const wxString
& s
, const wxUniChar
& c
) { return s
.IsSameAs(c
); }
4047 inline bool operator==(const wxString
& s
, const wxUniCharRef
& c
) { return s
.IsSameAs(c
); }
4048 inline bool operator==(const wxString
& s
, char c
) { return s
.IsSameAs(c
); }
4049 inline bool operator==(const wxString
& s
, wchar_t c
) { return s
.IsSameAs(c
); }
4050 inline bool operator!=(const wxUniChar
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
4051 inline bool operator!=(const wxUniCharRef
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
4052 inline bool operator!=(char c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
4053 inline bool operator!=(wchar_t c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
4054 inline bool operator!=(int c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
4055 inline bool operator!=(const wxString
& s
, const wxUniChar
& c
) { return !s
.IsSameAs(c
); }
4056 inline bool operator!=(const wxString
& s
, const wxUniCharRef
& c
) { return !s
.IsSameAs(c
); }
4057 inline bool operator!=(const wxString
& s
, char c
) { return !s
.IsSameAs(c
); }
4058 inline bool operator!=(const wxString
& s
, wchar_t c
) { return !s
.IsSameAs(c
); }
4061 // wxString iterators comparisons
4062 inline bool wxString::iterator::operator==(const const_iterator
& i
) const
4063 { return i
== *this; }
4064 inline bool wxString::iterator::operator!=(const const_iterator
& i
) const
4065 { return i
!= *this; }
4066 inline bool wxString::iterator::operator<(const const_iterator
& i
) const
4067 { return i
> *this; }
4068 inline bool wxString::iterator::operator>(const const_iterator
& i
) const
4069 { return i
< *this; }
4070 inline bool wxString::iterator::operator<=(const const_iterator
& i
) const
4071 { return i
>= *this; }
4072 inline bool wxString::iterator::operator>=(const const_iterator
& i
) const
4073 { return i
<= *this; }
4075 // comparison with C string in Unicode build
4078 #define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s
4080 wxDEFINE_ALL_COMPARISONS(const char *, const wxString
&, wxCMP_CHAR_STRING
)
4082 #undef wxCMP_CHAR_STRING
4084 #endif // wxUSE_UNICODE
4086 // we also need to provide the operators for comparison with wxCStrData to
4087 // resolve ambiguity between operator(const wxChar *,const wxString &) and
4088 // operator(const wxChar *, const wxChar *) for "p == s.c_str()"
4090 // notice that these are (shallow) pointer comparisons, not (deep) string ones
4091 #define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar()
4092 #define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar()
4094 wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData
&, wxCMP_WCHAR_CSTRDATA
)
4095 wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData
&, wxCMP_CHAR_CSTRDATA
)
4097 #undef wxCMP_CHAR_CSTRDATA
4098 #undef wxCMP_WCHAR_CSTRDATA
4100 // ---------------------------------------------------------------------------
4101 // Implementation only from here until the end of file
4102 // ---------------------------------------------------------------------------
4104 #if wxUSE_STD_IOSTREAM
4106 #include "wx/iosfwrap.h"
4108 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxString
&);
4109 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxCStrData
&);
4110 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxScopedCharBuffer
&);
4111 #ifndef __BORLANDC__
4112 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxScopedWCharBuffer
&);
4115 #if wxUSE_UNICODE && defined(HAVE_WOSTREAM)
4117 WXDLLIMPEXP_BASE wxSTD wostream
& operator<<(wxSTD wostream
&, const wxString
&);
4118 WXDLLIMPEXP_BASE wxSTD wostream
& operator<<(wxSTD wostream
&, const wxCStrData
&);
4119 WXDLLIMPEXP_BASE wxSTD wostream
& operator<<(wxSTD wostream
&, const wxScopedWCharBuffer
&);
4121 #endif // wxUSE_UNICODE && defined(HAVE_WOSTREAM)
4123 #endif // wxUSE_STD_IOSTREAM
4125 // ---------------------------------------------------------------------------
4126 // wxCStrData implementation
4127 // ---------------------------------------------------------------------------
4129 inline wxCStrData::wxCStrData(char *buf
)
4130 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
4131 inline wxCStrData::wxCStrData(wchar_t *buf
)
4132 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
4134 inline wxCStrData::wxCStrData(const wxCStrData
& data
)
4135 : m_str(data
.m_owned
? new wxString(*data
.m_str
) : data
.m_str
),
4136 m_offset(data
.m_offset
),
4137 m_owned(data
.m_owned
)
4141 inline wxCStrData::~wxCStrData()
4144 delete const_cast<wxString
*>(m_str
); // cast to silence warnings
4147 // AsChar() and AsWChar() implementations simply forward to wxString methods
4149 inline const wchar_t* wxCStrData::AsWChar() const
4151 const wchar_t * const p
=
4152 #if wxUSE_UNICODE_WCHAR
4154 #elif wxUSE_UNICODE_UTF8
4155 m_str
->AsWChar(wxMBConvStrictUTF8());
4157 m_str
->AsWChar(wxConvLibc
);
4160 // in Unicode build the string always has a valid Unicode representation
4161 // and even if a conversion is needed (as in UTF8 case) it can't fail
4163 // but in ANSI build the string contents might be not convertible to
4164 // Unicode using the current locale encoding so we do need to check for
4169 // if conversion fails, return empty string and not NULL to avoid
4170 // crashes in code written with either wxWidgets 2 wxString or
4171 // std::string behaviour in mind: neither of them ever returns NULL
4172 // from its c_str() and so we shouldn't neither
4174 // notice that the same is done in AsChar() below and
4175 // wxString::wc_str() and mb_str() for the same reasons
4178 #endif // !wxUSE_UNICODE
4180 return p
+ m_offset
;
4183 inline const char* wxCStrData::AsChar() const
4185 #if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
4186 const char * const p
= m_str
->AsChar(wxConvLibc
);
4189 #else // !wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY
4190 const char * const p
= m_str
->mb_str();
4191 #endif // wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
4193 return p
+ m_offset
;
4196 inline wxString
wxCStrData::AsString() const
4198 if ( m_offset
== 0 )
4201 return m_str
->Mid(m_offset
);
4204 inline const wxStringCharType
*wxCStrData::AsInternal() const
4206 #if wxUSE_UNICODE_UTF8
4207 return wxStringOperations::AddToIter(m_str
->wx_str(), m_offset
);
4209 return m_str
->wx_str() + m_offset
;
4213 inline wxUniChar
wxCStrData::operator*() const
4215 if ( m_str
->empty() )
4216 return wxUniChar(wxT('\0'));
4218 return (*m_str
)[m_offset
];
4221 inline wxUniChar
wxCStrData::operator[](size_t n
) const
4223 // NB: we intentionally use operator[] and not at() here because the former
4224 // works for the terminating NUL while the latter does not
4225 return (*m_str
)[m_offset
+ n
];
4228 // ----------------------------------------------------------------------------
4229 // more wxCStrData operators
4230 // ----------------------------------------------------------------------------
4232 // we need to define those to allow "size_t pos = p - s.c_str()" where p is
4233 // some pointer into the string
4234 inline size_t operator-(const char *p
, const wxCStrData
& cs
)
4236 return p
- cs
.AsChar();
4239 inline size_t operator-(const wchar_t *p
, const wxCStrData
& cs
)
4241 return p
- cs
.AsWChar();
4244 // ----------------------------------------------------------------------------
4245 // implementation of wx[W]CharBuffer inline methods using wxCStrData
4246 // ----------------------------------------------------------------------------
4248 // FIXME-UTF8: move this to buffer.h
4249 inline wxCharBuffer::wxCharBuffer(const wxCStrData
& cstr
)
4250 : wxCharTypeBufferBase(cstr
.AsCharBuf())
4254 inline wxWCharBuffer::wxWCharBuffer(const wxCStrData
& cstr
)
4255 : wxCharTypeBufferBase(cstr
.AsWCharBuf())
4259 #if wxUSE_UNICODE_UTF8
4260 // ----------------------------------------------------------------------------
4261 // implementation of wxStringIteratorNode inline methods
4262 // ----------------------------------------------------------------------------
4264 void wxStringIteratorNode::DoSet(const wxString
*str
,
4265 wxStringImpl::const_iterator
*citer
,
4266 wxStringImpl::iterator
*iter
)
4274 m_next
= str
->m_iterators
.ptr
;
4275 const_cast<wxString
*>(m_str
)->m_iterators
.ptr
= this;
4277 m_next
->m_prev
= this;
4285 void wxStringIteratorNode::clear()
4288 m_next
->m_prev
= m_prev
;
4290 m_prev
->m_next
= m_next
;
4291 else if ( m_str
) // first in the list
4292 const_cast<wxString
*>(m_str
)->m_iterators
.ptr
= m_next
;
4294 m_next
= m_prev
= NULL
;
4299 #endif // wxUSE_UNICODE_UTF8
4301 #if WXWIN_COMPATIBILITY_2_8
4302 // lot of code out there doesn't explicitly include wx/crt.h, but uses
4303 // CRT wrappers that are now declared in wx/wxcrt.h and wx/wxcrtvararg.h,
4304 // so let's include this header now that wxString is defined and it's safe
4309 // ----------------------------------------------------------------------------
4310 // Checks on wxString characters
4311 // ----------------------------------------------------------------------------
4313 template<bool (T
)(const wxUniChar
& c
)>
4314 inline bool wxStringCheck(const wxString
& val
)
4316 for ( wxString::const_iterator i
= val
.begin();
4324 #endif // _WX_WXSTRING_H_