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
28 #if defined(__WXMAC__) || defined(__VISAGECPP__)
32 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
33 // problem in VACPP V4 with including stdlib.h multiple times
34 // strconv includes it anyway
47 #ifdef HAVE_STRCASECMP_IN_STRINGS_H
48 #include <strings.h> // for strcasecmp()
49 #endif // HAVE_STRCASECMP_IN_STRINGS_H
50 #endif // ! __WXPALMOS5__
52 #include "wx/wxcrtbase.h" // for wxChar, wxStrlen() etc.
53 #include "wx/strvararg.h"
54 #include "wx/buffer.h" // for wxCharBuffer
55 #include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes
56 #include "wx/stringimpl.h"
57 #include "wx/stringops.h"
58 #include "wx/unichar.h"
60 // by default we cache the mapping of the positions in UTF-8 string to the byte
61 // offset as this results in noticeable performance improvements for loops over
62 // strings using indices; comment out this line to disable this
64 // notice that this optimization is well worth using even in debug builds as it
65 // changes asymptotic complexity of algorithms using indices to iterate over
66 // wxString back to expected linear from quadratic
68 // also notice that wxTLS_TYPE() (__declspec(thread) in this case) is unsafe to
69 // use in DLL build under pre-Vista Windows so we disable this code for now, if
70 // anybody really needs to use UTF-8 build under Windows with this optimization
71 // it would have to be re-tested and probably corrected
72 // CS: under OSX release builds the string destructor/cache cleanup sometimes
73 // crashes, disable until we find the true reason or a better workaround
74 #if wxUSE_UNICODE_UTF8 && !defined(__WXMSW__) && !defined(__WXOSX__)
75 #define wxUSE_STRING_POS_CACHE 1
77 #define wxUSE_STRING_POS_CACHE 0
80 #if wxUSE_STRING_POS_CACHE
83 // change this 0 to 1 to enable additional (very expensive) asserts
84 // verifying that string caching logic works as expected
86 #define wxSTRING_CACHE_ASSERT(cond) wxASSERT(cond)
88 #define wxSTRING_CACHE_ASSERT(cond)
90 #endif // wxUSE_STRING_POS_CACHE
92 class WXDLLIMPEXP_FWD_BASE wxString
;
94 // unless this symbol is predefined to disable the compatibility functions, do
96 #ifndef WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
97 #define WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER 1
102 template <typename T
> struct wxStringAsBufHelper
;
105 // ---------------------------------------------------------------------------
107 // ---------------------------------------------------------------------------
109 // casts [unfortunately!] needed to call some broken functions which require
110 // "char *" instead of "const char *"
111 #define WXSTRINGCAST (wxChar *)(const wxChar *)
112 #define wxCSTRINGCAST (wxChar *)(const wxChar *)
113 #define wxMBSTRINGCAST (char *)(const char *)
114 #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *)
116 // ----------------------------------------------------------------------------
118 // ----------------------------------------------------------------------------
120 #if WXWIN_COMPATIBILITY_2_6
122 // deprecated in favour of wxString::npos, don't use in new code
124 // maximum possible length for a string means "take all string" everywhere
125 #define wxSTRING_MAXLEN wxString::npos
127 #endif // WXWIN_COMPATIBILITY_2_6
129 // ---------------------------------------------------------------------------
130 // global functions complementing standard C string library replacements for
131 // strlen() and portable strcasecmp()
132 //---------------------------------------------------------------------------
134 #if WXWIN_COMPATIBILITY_2_8
135 // Use wxXXX() functions from wxcrt.h instead! These functions are for
136 // backwards compatibility only.
138 // checks whether the passed in pointer is NULL and if the string is empty
139 wxDEPRECATED( inline bool IsEmpty(const char *p
) );
140 inline bool IsEmpty(const char *p
) { return (!p
|| !*p
); }
142 // safe version of strlen() (returns 0 if passed NULL pointer)
143 wxDEPRECATED( inline size_t Strlen(const char *psz
) );
144 inline size_t Strlen(const char *psz
)
145 { return psz
? strlen(psz
) : 0; }
147 // portable strcasecmp/_stricmp
148 wxDEPRECATED( inline int Stricmp(const char *psz1
, const char *psz2
) );
149 inline int Stricmp(const char *psz1
, const char *psz2
)
151 #if defined(__VISUALC__) && defined(__WXWINCE__)
152 register char c1
, c2
;
154 c1
= tolower(*psz1
++);
155 c2
= tolower(*psz2
++);
156 } while ( c1
&& (c1
== c2
) );
159 #elif defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) )
160 return _stricmp(psz1
, psz2
);
161 #elif defined(__SC__)
162 return _stricmp(psz1
, psz2
);
163 #elif defined(__BORLANDC__)
164 return stricmp(psz1
, psz2
);
165 #elif defined(__WATCOMC__)
166 return stricmp(psz1
, psz2
);
167 #elif defined(__DJGPP__)
168 return stricmp(psz1
, psz2
);
169 #elif defined(__EMX__)
170 return stricmp(psz1
, psz2
);
171 #elif defined(__WXPM__)
172 return stricmp(psz1
, psz2
);
173 #elif defined(__WXPALMOS__) || \
174 defined(HAVE_STRCASECMP_IN_STRING_H) || \
175 defined(HAVE_STRCASECMP_IN_STRINGS_H) || \
176 defined(__GNUWIN32__)
177 return strcasecmp(psz1
, psz2
);
178 #elif defined(__MWERKS__) && !defined(__INTEL__)
179 register char c1
, c2
;
181 c1
= tolower(*psz1
++);
182 c2
= tolower(*psz2
++);
183 } while ( c1
&& (c1
== c2
) );
187 // almost all compilers/libraries provide this function (unfortunately under
188 // different names), that's why we don't implement our own which will surely
189 // be more efficient than this code (uncomment to use):
191 register char c1, c2;
193 c1 = tolower(*psz1++);
194 c2 = tolower(*psz2++);
195 } while ( c1 && (c1 == c2) );
200 #error "Please define string case-insensitive compare for your OS/compiler"
201 #endif // OS/compiler
204 #endif // WXWIN_COMPATIBILITY_2_8
206 // ----------------------------------------------------------------------------
208 // ----------------------------------------------------------------------------
210 // Lightweight object returned by wxString::c_str() and implicitly convertible
211 // to either const char* or const wchar_t*.
212 class WXDLLIMPEXP_BASE wxCStrData
215 // Ctors; for internal use by wxString and wxCStrData only
216 wxCStrData(const wxString
*str
, size_t offset
= 0, bool owned
= false)
217 : m_str(str
), m_offset(offset
), m_owned(owned
) {}
220 // Ctor constructs the object from char literal; they are needed to make
221 // operator?: compile and they intentionally take char*, not const char*
222 inline wxCStrData(char *buf
);
223 inline wxCStrData(wchar_t *buf
);
224 inline wxCStrData(const wxCStrData
& data
);
226 inline ~wxCStrData();
228 // methods defined inline below must be declared inline or mingw32 3.4.5
229 // warns about "<symbol> defined locally after being referenced with
230 // dllimport linkage"
231 #if wxUSE_UNICODE_WCHAR
234 const wchar_t* AsWChar() const;
235 operator const wchar_t*() const { return AsWChar(); }
237 #if !wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY
240 const char* AsChar() const;
241 const unsigned char* AsUnsignedChar() const
242 { return (const unsigned char *) AsChar(); }
243 operator const char*() const { return AsChar(); }
244 operator const unsigned char*() const { return AsUnsignedChar(); }
246 operator const void*() const { return AsChar(); }
248 // returns buffers that are valid as long as the associated wxString exists
249 inline const wxScopedCharBuffer
AsCharBuf() const;
250 inline const wxScopedWCharBuffer
AsWCharBuf() const;
252 inline wxString
AsString() const;
254 // returns the value as C string in internal representation (equivalent
255 // to AsString().wx_str(), but more efficient)
256 const wxStringCharType
*AsInternal() const;
258 // allow expressions like "c_str()[0]":
259 inline wxUniChar
operator[](size_t n
) const;
260 wxUniChar
operator[](int n
) const { return operator[](size_t(n
)); }
261 wxUniChar
operator[](long n
) const { return operator[](size_t(n
)); }
262 #ifndef wxSIZE_T_IS_UINT
263 wxUniChar
operator[](unsigned int n
) const { return operator[](size_t(n
)); }
264 #endif // size_t != unsigned int
266 // These operators are needed to emulate the pointer semantics of c_str():
267 // expressions like "wxChar *p = str.c_str() + 1;" should continue to work
268 // (we need both versions to resolve ambiguities). Note that this means
269 // the 'n' value is interpreted as addition to char*/wchar_t* pointer, it
270 // is *not* number of Unicode characters in wxString.
271 wxCStrData
operator+(int n
) const
272 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
273 wxCStrData
operator+(long n
) const
274 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
275 wxCStrData
operator+(size_t n
) const
276 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
278 // and these for "str.c_str() + (p2 - p1)" (it also works for any integer
279 // expression but it must be ptrdiff_t and not e.g. int to work in this
281 wxCStrData
operator-(ptrdiff_t n
) const
283 wxASSERT_MSG( n
<= (ptrdiff_t)m_offset
,
284 _T("attempt to construct address before the beginning of the string") );
285 return wxCStrData(m_str
, m_offset
- n
, m_owned
);
288 // this operator is needed to make expressions like "*c_str()" or
289 // "*(c_str() + 2)" work
290 inline wxUniChar
operator*() const;
293 // the wxString this object was returned for
294 const wxString
*m_str
;
295 // Offset into c_str() return value. Note that this is *not* offset in
296 // m_str in Unicode characters. Instead, it is index into the
297 // char*/wchar_t* buffer returned by c_str(). It's interpretation depends
298 // on how is the wxCStrData instance used: if it is eventually cast to
299 // const char*, m_offset will be in bytes form string's start; if it is
300 // cast to const wchar_t*, it will be in wchar_t values.
302 // should m_str be deleted, i.e. is it owned by us?
305 friend class WXDLLIMPEXP_FWD_BASE wxString
;
308 // ----------------------------------------------------------------------------
309 // wxStringPrintfMixin
310 // ---------------------------------------------------------------------------
312 // NB: VC6 has a bug that causes linker errors if you have template methods
313 // in a class using __declspec(dllimport). The solution is to split such
314 // class into two classes, one that contains the template methods and does
315 // *not* use WXDLLIMPEXP_BASE and another class that contains the rest
316 // (with DLL linkage).
318 // We only do this for VC6 here, because the code is less efficient
319 // (Printf() has to use dynamic_cast<>) and because OpenWatcom compiler
320 // cannot compile this code.
322 #if defined(__VISUALC__) && __VISUALC__ < 1300
323 #define wxNEEDS_WXSTRING_PRINTF_MIXIN
326 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
327 // this class contains implementation of wxString's vararg methods, it's
328 // exported from wxBase DLL
329 class WXDLLIMPEXP_BASE wxStringPrintfMixinBase
332 wxStringPrintfMixinBase() {}
334 #if !wxUSE_UTF8_LOCALE_ONLY
335 int DoPrintfWchar(const wxChar
*format
, ...);
336 static wxString
DoFormatWchar(const wxChar
*format
, ...);
338 #if wxUSE_UNICODE_UTF8
339 int DoPrintfUtf8(const char *format
, ...);
340 static wxString
DoFormatUtf8(const char *format
, ...);
344 // this class contains template wrappers for wxString's vararg methods, it's
345 // intentionally *not* exported from the DLL in order to fix the VC6 bug
347 class wxStringPrintfMixin
: public wxStringPrintfMixinBase
350 // to further complicate things, we can't return wxString from
351 // wxStringPrintfMixin::Format() because wxString is not yet declared at
352 // this point; the solution is to use this fake type trait template - this
353 // way the compiler won't know the return type until Format() is used
354 // (this doesn't compile with Watcom, but VC6 compiles it just fine):
355 template<typename T
> struct StringReturnType
357 typedef wxString type
;
361 // these are duplicated wxString methods, they're also declared below
362 // if !wxNEEDS_WXSTRING_PRINTF_MIXIN:
364 // static wxString Format(const wString& format, ...) WX_ATTRIBUTE_PRINTF_1;
365 WX_DEFINE_VARARG_FUNC_SANS_N0(static typename StringReturnType
<T1
>::type
,
366 Format
, 1, (const wxFormatString
&),
367 DoFormatWchar
, DoFormatUtf8
)
368 // We have to implement the version without template arguments manually
369 // because of the StringReturnType<> hack, although WX_DEFINE_VARARG_FUNC
370 // normally does it itself. It has to be a template so that we can use
371 // the hack, even though there's no real template parameter. We can't move
372 // it to wxStrig, because it would shadow these versions of Format() then.
374 inline static typename StringReturnType
<T
>::type
377 // NB: this doesn't compile if T is not (some form of) a string;
378 // this makes Format's prototype equivalent to
379 // Format(const wxFormatString& fmt)
380 return DoFormatWchar(wxFormatString(fmt
));
383 // int Printf(const wxString& format, ...);
384 WX_DEFINE_VARARG_FUNC(int, Printf
, 1, (const wxFormatString
&),
385 DoPrintfWchar
, DoPrintfUtf8
)
386 // int sprintf(const wxString& format, ...) WX_ATTRIBUTE_PRINTF_2;
387 WX_DEFINE_VARARG_FUNC(int, sprintf
, 1, (const wxFormatString
&),
388 DoPrintfWchar
, DoPrintfUtf8
)
391 wxStringPrintfMixin() : wxStringPrintfMixinBase() {}
393 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
396 // ----------------------------------------------------------------------------
397 // wxString: string class trying to be compatible with std::string, MFC
398 // CString and wxWindows 1.x wxString all at once
399 // ---------------------------------------------------------------------------
401 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
402 // "non dll-interface class 'wxStringPrintfMixin' used as base interface
403 // for dll-interface class 'wxString'" -- this is OK in our case
404 #pragma warning (disable:4275)
407 #if wxUSE_UNICODE_UTF8
408 // see the comment near wxString::iterator for why we need this
409 class WXDLLIMPEXP_BASE wxStringIteratorNode
412 wxStringIteratorNode()
413 : m_str(NULL
), m_citer(NULL
), m_iter(NULL
), m_prev(NULL
), m_next(NULL
) {}
414 wxStringIteratorNode(const wxString
*str
,
415 wxStringImpl::const_iterator
*citer
)
416 { DoSet(str
, citer
, NULL
); }
417 wxStringIteratorNode(const wxString
*str
, wxStringImpl::iterator
*iter
)
418 { DoSet(str
, NULL
, iter
); }
419 ~wxStringIteratorNode()
422 inline void set(const wxString
*str
, wxStringImpl::const_iterator
*citer
)
423 { clear(); DoSet(str
, citer
, NULL
); }
424 inline void set(const wxString
*str
, wxStringImpl::iterator
*iter
)
425 { clear(); DoSet(str
, NULL
, iter
); }
427 const wxString
*m_str
;
428 wxStringImpl::const_iterator
*m_citer
;
429 wxStringImpl::iterator
*m_iter
;
430 wxStringIteratorNode
*m_prev
, *m_next
;
434 inline void DoSet(const wxString
*str
,
435 wxStringImpl::const_iterator
*citer
,
436 wxStringImpl::iterator
*iter
);
438 // the node belongs to a particular iterator instance, it's not copied
439 // when a copy of the iterator is made
440 wxDECLARE_NO_COPY_CLASS(wxStringIteratorNode
);
442 #endif // wxUSE_UNICODE_UTF8
444 class WXDLLIMPEXP_BASE wxString
445 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
446 : public wxStringPrintfMixin
449 // NB: special care was taken in arranging the member functions in such order
450 // that all inline functions can be effectively inlined, verify that all
451 // performance critical functions are still inlined if you change order!
453 // an 'invalid' value for string index, moved to this place due to a CW bug
454 static const size_t npos
;
457 // if we hadn't made these operators private, it would be possible to
458 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
459 // converted to char in C and we do have operator=(char)
461 // NB: we don't need other versions (short/long and unsigned) as attempt
462 // to assign another numeric type to wxString will now result in
463 // ambiguity between operator=(char) and operator=(int)
464 wxString
& operator=(int);
466 // these methods are not implemented - there is _no_ conversion from int to
467 // string, you're doing something wrong if the compiler wants to call it!
469 // try `s << i' or `s.Printf("%d", i)' instead
473 // buffer for holding temporary substring when using any of the methods
474 // that take (char*,size_t) or (wchar_t*,size_t) arguments:
476 struct SubstrBufFromType
481 SubstrBufFromType(const T
& data_
, size_t len_
)
482 : data(data_
), len(len_
)
484 wxASSERT_MSG( len
!= npos
, "must have real length" );
488 #if wxUSE_UNICODE_UTF8
489 // even char* -> char* needs conversion, from locale charset to UTF-8
490 typedef SubstrBufFromType
<wxScopedCharBuffer
> SubstrBufFromWC
;
491 typedef SubstrBufFromType
<wxScopedCharBuffer
> SubstrBufFromMB
;
492 #elif wxUSE_UNICODE_WCHAR
493 typedef SubstrBufFromType
<const wchar_t*> SubstrBufFromWC
;
494 typedef SubstrBufFromType
<wxScopedWCharBuffer
> SubstrBufFromMB
;
496 typedef SubstrBufFromType
<const char*> SubstrBufFromMB
;
497 typedef SubstrBufFromType
<wxScopedCharBuffer
> SubstrBufFromWC
;
501 // Functions implementing primitive operations on string data; wxString
502 // methods and iterators are implemented in terms of it. The differences
503 // between UTF-8 and wchar_t* representations of the string are mostly
506 #if wxUSE_UNICODE_UTF8
507 static SubstrBufFromMB
ConvertStr(const char *psz
, size_t nLength
,
508 const wxMBConv
& conv
);
509 static SubstrBufFromWC
ConvertStr(const wchar_t *pwz
, size_t nLength
,
510 const wxMBConv
& conv
);
511 #elif wxUSE_UNICODE_WCHAR
512 static SubstrBufFromMB
ConvertStr(const char *psz
, size_t nLength
,
513 const wxMBConv
& conv
);
515 static SubstrBufFromWC
ConvertStr(const wchar_t *pwz
, size_t nLength
,
516 const wxMBConv
& conv
);
519 #if !wxUSE_UNICODE_UTF8 // wxUSE_UNICODE_WCHAR or !wxUSE_UNICODE
520 // returns C string encoded as the implementation expects:
522 static const wchar_t* ImplStr(const wchar_t* str
)
523 { return str
? str
: wxT(""); }
524 static const SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
525 { return SubstrBufFromWC(str
, (str
&& n
== npos
) ? wxWcslen(str
) : n
); }
526 static wxScopedWCharBuffer
ImplStr(const char* str
,
527 const wxMBConv
& conv
= wxConvLibc
)
528 { return ConvertStr(str
, npos
, conv
).data
; }
529 static SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
530 const wxMBConv
& conv
= wxConvLibc
)
531 { return ConvertStr(str
, n
, conv
); }
533 static const char* ImplStr(const char* str
,
534 const wxMBConv
& WXUNUSED(conv
) = wxConvLibc
)
535 { return str
? str
: ""; }
536 static const SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
537 const wxMBConv
& WXUNUSED(conv
) = wxConvLibc
)
538 { return SubstrBufFromMB(str
, (str
&& n
== npos
) ? wxStrlen(str
) : n
); }
539 static wxScopedCharBuffer
ImplStr(const wchar_t* str
)
540 { return ConvertStr(str
, npos
, wxConvLibc
).data
; }
541 static SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
542 { return ConvertStr(str
, n
, wxConvLibc
); }
545 // translates position index in wxString to/from index in underlying
547 static size_t PosToImpl(size_t pos
) { return pos
; }
548 static void PosLenToImpl(size_t pos
, size_t len
,
549 size_t *implPos
, size_t *implLen
)
550 { *implPos
= pos
; *implLen
= len
; }
551 static size_t LenToImpl(size_t len
) { return len
; }
552 static size_t PosFromImpl(size_t pos
) { return pos
; }
554 // we don't want to define these as empty inline functions as it could
555 // result in noticeable (and quite unnecessary in non-UTF-8 build) slowdown
556 // in debug build where the inline functions are not effectively inlined
557 #define wxSTRING_INVALIDATE_CACHE()
558 #define wxSTRING_INVALIDATE_CACHED_LENGTH()
559 #define wxSTRING_UPDATE_CACHED_LENGTH(n)
560 #define wxSTRING_SET_CACHED_LENGTH(n)
562 #else // wxUSE_UNICODE_UTF8
564 static wxScopedCharBuffer
ImplStr(const char* str
,
565 const wxMBConv
& conv
= wxConvLibc
)
566 { return ConvertStr(str
, npos
, conv
).data
; }
567 static SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
568 const wxMBConv
& conv
= wxConvLibc
)
569 { return ConvertStr(str
, n
, conv
); }
571 static wxScopedCharBuffer
ImplStr(const wchar_t* str
)
572 { return ConvertStr(str
, npos
, wxMBConvUTF8()).data
; }
573 static SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
574 { return ConvertStr(str
, n
, wxMBConvUTF8()); }
576 #if wxUSE_STRING_POS_CACHE
577 // this is an extremely simple cache used by PosToImpl(): each cache element
578 // contains the string it applies to and the index corresponding to the last
579 // used position in this wxString in its m_impl string
581 // NB: notice that this struct (and nested Element one) must be a POD or we
582 // wouldn't be able to use a thread-local variable of this type, in
583 // particular it should have no ctor -- we rely on statics being
584 // initialized to 0 instead
591 const wxString
*str
; // the string to which this element applies
592 size_t pos
, // the cached index in this string
593 impl
, // the corresponding position in its m_impl
594 len
; // cached length or npos if unknown
596 // reset cached index to 0
597 void ResetPos() { pos
= impl
= 0; }
599 // reset position and length
600 void Reset() { ResetPos(); len
= npos
; }
603 // cache the indices mapping for the last few string used
604 Element cached
[SIZE
];
606 // the last used index
610 #ifndef wxHAS_COMPILER_TLS
611 // we must use an accessor function and not a static variable when the TLS
612 // variables support is implemented in the library (and not by the compiler)
613 // because the global s_cache variable could be not yet initialized when a
614 // ctor of another global object is executed and if that ctor uses any
615 // wxString methods, bad things happen
617 // however notice that this approach does not work when compiler TLS is used,
618 // at least not with g++ 4.1.2 under amd64 as it apparently compiles code
619 // using this accessor incorrectly when optimizations are enabled (-O2 is
620 // enough) -- luckily we don't need it then neither as static __thread
621 // variables are initialized by 0 anyhow then and so we can use the variable
623 WXEXPORT
static Cache
& GetCache()
625 static wxTLS_TYPE(Cache
) s_cache
;
627 return wxTLS_VALUE(s_cache
);
630 // this helper struct is used to ensure that GetCache() is called during
631 // static initialization time, i.e. before any threads creation, as otherwise
632 // the static s_cache construction inside GetCache() wouldn't be MT-safe
633 friend struct wxStrCacheInitializer
;
634 #else // wxHAS_COMPILER_TLS
635 static wxTLS_TYPE(Cache
) ms_cache
;
636 static Cache
& GetCache() { return wxTLS_VALUE(ms_cache
); }
637 #endif // !wxHAS_COMPILER_TLS/wxHAS_COMPILER_TLS
639 static Cache::Element
*GetCacheBegin() { return GetCache().cached
; }
640 static Cache::Element
*GetCacheEnd() { return GetCacheBegin() + Cache::SIZE
; }
641 static unsigned& LastUsedCacheElement() { return GetCache().lastUsed
; }
643 // this is used in debug builds only to provide a convenient function,
644 // callable from a debugger, to show the cache contents
645 friend struct wxStrCacheDumper
;
647 // uncomment this to have access to some profiling statistics on program
649 //#define wxPROFILE_STRING_CACHE
651 #ifdef wxPROFILE_STRING_CACHE
652 static struct PosToImplCacheStats
654 unsigned postot
, // total non-trivial calls to PosToImpl
655 poshits
, // cache hits from PosToImpl()
656 mishits
, // cached position beyond the needed one
657 sumpos
, // sum of all positions, used to compute the
658 // average position after dividing by postot
659 sumofs
, // sum of all offsets after using the cache, used to
660 // compute the average after dividing by hits
661 lentot
, // number of total calls to length()
662 lenhits
; // number of cache hits in length()
665 friend struct wxStrCacheStatsDumper
;
667 #define wxCACHE_PROFILE_FIELD_INC(field) ms_cacheStats.field++
668 #define wxCACHE_PROFILE_FIELD_ADD(field, val) ms_cacheStats.field += (val)
669 #else // !wxPROFILE_STRING_CACHE
670 #define wxCACHE_PROFILE_FIELD_INC(field)
671 #define wxCACHE_PROFILE_FIELD_ADD(field, val)
672 #endif // wxPROFILE_STRING_CACHE/!wxPROFILE_STRING_CACHE
674 // note: it could seem that the functions below shouldn't be inline because
675 // they are big, contain loops and so the compiler shouldn't be able to
676 // inline them anyhow, however moving them into string.cpp does decrease the
677 // code performance by ~5%, at least when using g++ 4.1 so do keep them here
678 // unless tests show that it's not advantageous any more
680 // return the pointer to the cache element for this string or NULL if not
682 Cache::Element
*FindCacheElement() const
684 // profiling seems to show a small but consistent gain if we use this
685 // simple loop instead of starting from the last used element (there are
686 // a lot of misses in this function...)
687 Cache::Element
* const cacheBegin
= GetCacheBegin();
688 #ifndef wxHAS_COMPILER_TLS
689 // during destruction tls calls may return NULL, in this case return NULL
690 // immediately without accessing anything else
691 if ( cacheBegin
== NULL
)
694 Cache::Element
* const cacheEnd
= GetCacheEnd();
695 for ( Cache::Element
*c
= cacheBegin
; c
!= cacheEnd
; c
++ )
697 if ( c
->str
== this )
704 // unlike FindCacheElement(), this one always returns a valid pointer to the
705 // cache element for this string, it may have valid last cached position and
706 // its corresponding index in the byte string or not
707 Cache::Element
*GetCacheElement() const
709 Cache::Element
* const cacheBegin
= GetCacheBegin();
710 Cache::Element
* const cacheEnd
= GetCacheEnd();
711 Cache::Element
* const cacheStart
= cacheBegin
+ LastUsedCacheElement();
713 // check the last used first, this does no (measurable) harm for a miss
714 // but does help for simple loops addressing the same string all the time
715 if ( cacheStart
->str
== this )
718 // notice that we're going to check cacheStart again inside this call but
719 // profiling shows that it's still faster to use a simple loop like
720 // inside FindCacheElement() than manually looping with wrapping starting
721 // from the cache entry after the start one
722 Cache::Element
*c
= FindCacheElement();
725 // claim the next cache entry for this string
727 if ( ++c
== cacheEnd
)
733 // and remember the last used element
734 LastUsedCacheElement() = c
- cacheBegin
;
740 size_t DoPosToImpl(size_t pos
) const
742 wxCACHE_PROFILE_FIELD_INC(postot
);
744 // NB: although the case of pos == 1 (and offset from cached position
745 // equal to 1) are common, nothing is gained by writing special code
746 // for handling them, the compiler (at least g++ 4.1 used) seems to
747 // optimize the code well enough on its own
749 wxCACHE_PROFILE_FIELD_ADD(sumpos
, pos
);
751 Cache::Element
* const cache
= GetCacheElement();
753 // cached position can't be 0 so if it is, it means that this entry was
754 // used for length caching only so far, i.e. it doesn't count as a hit
755 // from our point of view
758 wxCACHE_PROFILE_FIELD_INC(poshits
);
761 if ( pos
== cache
->pos
)
764 // this seems to happen only rarely so just reset the cache in this case
765 // instead of complicating code even further by seeking backwards in this
767 if ( cache
->pos
> pos
)
769 wxCACHE_PROFILE_FIELD_INC(mishits
);
774 wxCACHE_PROFILE_FIELD_ADD(sumofs
, pos
- cache
->pos
);
777 wxStringImpl::const_iterator
i(m_impl
.begin() + cache
->impl
);
778 for ( size_t n
= cache
->pos
; n
< pos
; n
++ )
779 wxStringOperations::IncIter(i
);
782 cache
->impl
= i
- m_impl
.begin();
784 wxSTRING_CACHE_ASSERT(
785 (int)cache
->impl
== (begin() + pos
).impl() - m_impl
.begin() );
790 void InvalidateCache()
792 Cache::Element
* const cache
= FindCacheElement();
797 void InvalidateCachedLength()
799 Cache::Element
* const cache
= FindCacheElement();
804 void SetCachedLength(size_t len
)
806 // we optimistically cache the length here even if the string wasn't
807 // present in the cache before, this seems to do no harm and the
808 // potential for avoiding length recomputation for long strings looks
810 GetCacheElement()->len
= len
;
813 void UpdateCachedLength(ptrdiff_t delta
)
815 Cache::Element
* const cache
= FindCacheElement();
816 if ( cache
&& cache
->len
!= npos
)
818 wxSTRING_CACHE_ASSERT( (ptrdiff_t)cache
->len
+ delta
>= 0 );
824 #define wxSTRING_INVALIDATE_CACHE() InvalidateCache()
825 #define wxSTRING_INVALIDATE_CACHED_LENGTH() InvalidateCachedLength()
826 #define wxSTRING_UPDATE_CACHED_LENGTH(n) UpdateCachedLength(n)
827 #define wxSTRING_SET_CACHED_LENGTH(n) SetCachedLength(n)
828 #else // !wxUSE_STRING_POS_CACHE
829 size_t DoPosToImpl(size_t pos
) const
831 return (begin() + pos
).impl() - m_impl
.begin();
834 #define wxSTRING_INVALIDATE_CACHE()
835 #define wxSTRING_INVALIDATE_CACHED_LENGTH()
836 #define wxSTRING_UPDATE_CACHED_LENGTH(n)
837 #define wxSTRING_SET_CACHED_LENGTH(n)
838 #endif // wxUSE_STRING_POS_CACHE/!wxUSE_STRING_POS_CACHE
840 size_t PosToImpl(size_t pos
) const
842 return pos
== 0 || pos
== npos
? pos
: DoPosToImpl(pos
);
845 void PosLenToImpl(size_t pos
, size_t len
, size_t *implPos
, size_t *implLen
) const;
847 size_t LenToImpl(size_t len
) const
850 PosLenToImpl(0, len
, &pos
, &len2
);
854 size_t PosFromImpl(size_t pos
) const
856 if ( pos
== 0 || pos
== npos
)
859 return const_iterator(this, m_impl
.begin() + pos
) - begin();
861 #endif // !wxUSE_UNICODE_UTF8/wxUSE_UNICODE_UTF8
865 typedef wxUniChar value_type
;
866 typedef wxUniChar char_type
;
867 typedef wxUniCharRef reference
;
868 typedef wxChar
* pointer
;
869 typedef const wxChar
* const_pointer
;
871 typedef size_t size_type
;
872 typedef wxUniChar const_reference
;
875 #if wxUSE_UNICODE_UTF8
876 // random access is not O(1), as required by Random Access Iterator
877 #define WX_STR_ITERATOR_TAG std::bidirectional_iterator_tag
879 #define WX_STR_ITERATOR_TAG std::random_access_iterator_tag
881 #define WX_DEFINE_ITERATOR_CATEGORY(cat) typedef cat iterator_category;
883 // not defining iterator_category at all in this case is better than defining
884 // it as some dummy type -- at least it results in more intelligible error
886 #define WX_DEFINE_ITERATOR_CATEGORY(cat)
889 #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, reference_type) \
891 typedef wxStringImpl::iterator_name underlying_iterator; \
893 WX_DEFINE_ITERATOR_CATEGORY(WX_STR_ITERATOR_TAG) \
894 typedef wxUniChar value_type; \
895 typedef int difference_type; \
896 typedef reference_type reference; \
897 typedef pointer_type pointer; \
899 reference operator[](size_t n) const { return *(*this + n); } \
901 iterator_name& operator++() \
902 { wxStringOperations::IncIter(m_cur); return *this; } \
903 iterator_name& operator--() \
904 { wxStringOperations::DecIter(m_cur); return *this; } \
905 iterator_name operator++(int) \
907 iterator_name tmp = *this; \
908 wxStringOperations::IncIter(m_cur); \
911 iterator_name operator--(int) \
913 iterator_name tmp = *this; \
914 wxStringOperations::DecIter(m_cur); \
918 iterator_name& operator+=(ptrdiff_t n) \
920 m_cur = wxStringOperations::AddToIter(m_cur, n); \
923 iterator_name& operator-=(ptrdiff_t n) \
925 m_cur = wxStringOperations::AddToIter(m_cur, -n); \
929 difference_type operator-(const iterator_name& i) const \
930 { return wxStringOperations::DiffIters(m_cur, i.m_cur); } \
932 bool operator==(const iterator_name& i) const \
933 { return m_cur == i.m_cur; } \
934 bool operator!=(const iterator_name& i) const \
935 { return m_cur != i.m_cur; } \
937 bool operator<(const iterator_name& i) const \
938 { return m_cur < i.m_cur; } \
939 bool operator>(const iterator_name& i) const \
940 { return m_cur > i.m_cur; } \
941 bool operator<=(const iterator_name& i) const \
942 { return m_cur <= i.m_cur; } \
943 bool operator>=(const iterator_name& i) const \
944 { return m_cur >= i.m_cur; } \
947 /* for internal wxString use only: */ \
948 underlying_iterator impl() const { return m_cur; } \
950 friend class wxString; \
951 friend class wxCStrData; \
954 underlying_iterator m_cur
956 class WXDLLIMPEXP_FWD_BASE const_iterator
;
958 #if wxUSE_UNICODE_UTF8
959 // NB: In UTF-8 build, (non-const) iterator needs to keep reference
960 // to the underlying wxStringImpl, because UTF-8 is variable-length
961 // encoding and changing the value pointer to by an iterator (using
962 // its operator*) requires calling wxStringImpl::replace() if the old
963 // and new values differ in their encoding's length.
965 // Furthermore, the replace() call may invalid all iterators for the
966 // string, so we have to keep track of outstanding iterators and update
967 // them if replace() happens.
969 // This is implemented by maintaining linked list of iterators for every
970 // string and traversing it in wxUniCharRef::operator=(). Head of the
971 // list is stored in wxString. (FIXME-UTF8)
973 class WXDLLIMPEXP_BASE iterator
975 WX_STR_ITERATOR_IMPL(iterator
, wxChar
*, wxUniCharRef
);
979 iterator(const iterator
& i
)
980 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
981 iterator
& operator=(const iterator
& i
)
986 m_node
.set(i
.str(), &m_cur
);
991 reference
operator*()
992 { return wxUniCharRef::CreateForString(*str(), m_cur
); }
994 iterator
operator+(ptrdiff_t n
) const
995 { return iterator(str(), wxStringOperations::AddToIter(m_cur
, n
)); }
996 iterator
operator-(ptrdiff_t n
) const
997 { return iterator(str(), wxStringOperations::AddToIter(m_cur
, -n
)); }
1000 iterator(wxString
*str
, underlying_iterator ptr
)
1001 : m_cur(ptr
), m_node(str
, &m_cur
) {}
1003 wxString
* str() const { return const_cast<wxString
*>(m_node
.m_str
); }
1005 wxStringIteratorNode m_node
;
1007 friend class const_iterator
;
1010 class WXDLLIMPEXP_BASE const_iterator
1012 // NB: reference_type is intentionally value, not reference, the character
1013 // may be encoded differently in wxString data:
1014 WX_STR_ITERATOR_IMPL(const_iterator
, const wxChar
*, wxUniChar
);
1018 const_iterator(const const_iterator
& i
)
1019 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
1020 const_iterator(const iterator
& i
)
1021 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
1023 const_iterator
& operator=(const const_iterator
& i
)
1028 m_node
.set(i
.str(), &m_cur
);
1032 const_iterator
& operator=(const iterator
& i
)
1033 { m_cur
= i
.m_cur
; m_node
.set(i
.str(), &m_cur
); return *this; }
1035 reference
operator*() const
1036 { return wxStringOperations::DecodeChar(m_cur
); }
1038 const_iterator
operator+(ptrdiff_t n
) const
1039 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur
, n
)); }
1040 const_iterator
operator-(ptrdiff_t n
) const
1041 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur
, -n
)); }
1044 // for internal wxString use only:
1045 const_iterator(const wxString
*str
, underlying_iterator ptr
)
1046 : m_cur(ptr
), m_node(str
, &m_cur
) {}
1048 const wxString
* str() const { return m_node
.m_str
; }
1050 wxStringIteratorNode m_node
;
1053 size_t IterToImplPos(wxString::iterator i
) const
1054 { return wxStringImpl::const_iterator(i
.impl()) - m_impl
.begin(); }
1056 iterator
GetIterForNthChar(size_t n
)
1057 { return iterator(this, m_impl
.begin() + PosToImpl(n
)); }
1058 const_iterator
GetIterForNthChar(size_t n
) const
1059 { return const_iterator(this, m_impl
.begin() + PosToImpl(n
)); }
1060 #else // !wxUSE_UNICODE_UTF8
1062 class WXDLLIMPEXP_BASE iterator
1064 WX_STR_ITERATOR_IMPL(iterator
, wxChar
*, wxUniCharRef
);
1068 iterator(const iterator
& i
) : m_cur(i
.m_cur
) {}
1070 reference
operator*()
1071 { return wxUniCharRef::CreateForString(m_cur
); }
1073 iterator
operator+(ptrdiff_t n
) const
1074 { return iterator(wxStringOperations::AddToIter(m_cur
, n
)); }
1075 iterator
operator-(ptrdiff_t n
) const
1076 { return iterator(wxStringOperations::AddToIter(m_cur
, -n
)); }
1079 // for internal wxString use only:
1080 iterator(underlying_iterator ptr
) : m_cur(ptr
) {}
1081 iterator(wxString
*WXUNUSED(str
), underlying_iterator ptr
) : m_cur(ptr
) {}
1083 friend class const_iterator
;
1086 class WXDLLIMPEXP_BASE const_iterator
1088 // NB: reference_type is intentionally value, not reference, the character
1089 // may be encoded differently in wxString data:
1090 WX_STR_ITERATOR_IMPL(const_iterator
, const wxChar
*, wxUniChar
);
1094 const_iterator(const const_iterator
& i
) : m_cur(i
.m_cur
) {}
1095 const_iterator(const iterator
& i
) : m_cur(i
.m_cur
) {}
1097 reference
operator*() const
1098 { return wxStringOperations::DecodeChar(m_cur
); }
1100 const_iterator
operator+(ptrdiff_t n
) const
1101 { return const_iterator(wxStringOperations::AddToIter(m_cur
, n
)); }
1102 const_iterator
operator-(ptrdiff_t n
) const
1103 { return const_iterator(wxStringOperations::AddToIter(m_cur
, -n
)); }
1106 // for internal wxString use only:
1107 const_iterator(underlying_iterator ptr
) : m_cur(ptr
) {}
1108 const_iterator(const wxString
*WXUNUSED(str
), underlying_iterator ptr
)
1112 iterator
GetIterForNthChar(size_t n
) { return begin() + n
; }
1113 const_iterator
GetIterForNthChar(size_t n
) const { return begin() + n
; }
1114 #endif // wxUSE_UNICODE_UTF8/!wxUSE_UNICODE_UTF8
1116 #undef WX_STR_ITERATOR_TAG
1117 #undef WX_STR_ITERATOR_IMPL
1119 friend class iterator
;
1120 friend class const_iterator
;
1122 template <typename T
>
1123 class reverse_iterator_impl
1126 typedef T iterator_type
;
1128 WX_DEFINE_ITERATOR_CATEGORY(typename
T::iterator_category
)
1129 typedef typename
T::value_type value_type
;
1130 typedef typename
T::difference_type difference_type
;
1131 typedef typename
T::reference reference
;
1132 typedef typename
T::pointer
*pointer
;
1134 reverse_iterator_impl() {}
1135 reverse_iterator_impl(iterator_type i
) : m_cur(i
) {}
1136 reverse_iterator_impl(const reverse_iterator_impl
& ri
)
1137 : m_cur(ri
.m_cur
) {}
1139 iterator_type
base() const { return m_cur
; }
1141 reference
operator*() const { return *(m_cur
-1); }
1142 reference
operator[](size_t n
) const { return *(*this + n
); }
1144 reverse_iterator_impl
& operator++()
1145 { --m_cur
; return *this; }
1146 reverse_iterator_impl
operator++(int)
1147 { reverse_iterator_impl tmp
= *this; --m_cur
; return tmp
; }
1148 reverse_iterator_impl
& operator--()
1149 { ++m_cur
; return *this; }
1150 reverse_iterator_impl
operator--(int)
1151 { reverse_iterator_impl tmp
= *this; ++m_cur
; return tmp
; }
1153 // NB: explicit <T> in the functions below is to keep BCC 5.5 happy
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
) const
1157 { return reverse_iterator_impl
<T
>(m_cur
+ n
); }
1158 reverse_iterator_impl
operator+=(ptrdiff_t n
)
1159 { m_cur
-= n
; return *this; }
1160 reverse_iterator_impl
operator-=(ptrdiff_t n
)
1161 { m_cur
+= n
; return *this; }
1163 unsigned operator-(const reverse_iterator_impl
& i
) const
1164 { return i
.m_cur
- m_cur
; }
1166 bool operator==(const reverse_iterator_impl
& ri
) const
1167 { return m_cur
== ri
.m_cur
; }
1168 bool operator!=(const reverse_iterator_impl
& ri
) const
1169 { return !(*this == ri
); }
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
; }
1177 bool operator>=(const reverse_iterator_impl
& i
) const
1178 { return m_cur
<= i
.m_cur
; }
1181 iterator_type m_cur
;
1184 typedef reverse_iterator_impl
<iterator
> reverse_iterator
;
1185 typedef reverse_iterator_impl
<const_iterator
> const_reverse_iterator
;
1188 // used to transform an expression built using c_str() (and hence of type
1189 // wxCStrData) to an iterator into the string
1190 static const_iterator
CreateConstIterator(const wxCStrData
& data
)
1192 return const_iterator(data
.m_str
,
1193 (data
.m_str
->begin() + data
.m_offset
).impl());
1196 // in UTF-8 STL build, creation from std::string requires conversion under
1197 // non-UTF8 locales, so we can't have and use wxString(wxStringImpl) ctor;
1198 // instead we define dummy type that lets us have wxString ctor for creation
1199 // from wxStringImpl that couldn't be used by user code (in all other builds,
1200 // "standard" ctors can be used):
1201 #if wxUSE_UNICODE_UTF8 && wxUSE_STL_BASED_WXSTRING
1202 struct CtorFromStringImplTag
{};
1204 wxString(CtorFromStringImplTag
* WXUNUSED(dummy
), const wxStringImpl
& src
)
1207 static wxString
FromImpl(const wxStringImpl
& src
)
1208 { return wxString((CtorFromStringImplTag
*)NULL
, src
); }
1210 #if !wxUSE_STL_BASED_WXSTRING
1211 wxString(const wxStringImpl
& src
) : m_impl(src
) { }
1212 // else: already defined as wxString(wxStdString) below
1214 static wxString
FromImpl(const wxStringImpl
& src
) { return wxString(src
); }
1218 // constructors and destructor
1219 // ctor for an empty string
1223 wxString(const wxString
& stringSrc
) : m_impl(stringSrc
.m_impl
) { }
1225 // string containing nRepeat copies of ch
1226 wxString(wxUniChar ch
, size_t nRepeat
= 1 )
1227 { assign(nRepeat
, ch
); }
1228 wxString(size_t nRepeat
, wxUniChar ch
)
1229 { assign(nRepeat
, ch
); }
1230 wxString(wxUniCharRef ch
, size_t nRepeat
= 1)
1231 { assign(nRepeat
, ch
); }
1232 wxString(size_t nRepeat
, wxUniCharRef ch
)
1233 { assign(nRepeat
, ch
); }
1234 wxString(char ch
, size_t nRepeat
= 1)
1235 { assign(nRepeat
, ch
); }
1236 wxString(size_t nRepeat
, char ch
)
1237 { assign(nRepeat
, ch
); }
1238 wxString(wchar_t ch
, size_t nRepeat
= 1)
1239 { assign(nRepeat
, ch
); }
1240 wxString(size_t nRepeat
, wchar_t ch
)
1241 { assign(nRepeat
, ch
); }
1243 // ctors from char* strings:
1244 wxString(const char *psz
)
1245 : m_impl(ImplStr(psz
)) {}
1246 wxString(const char *psz
, const wxMBConv
& conv
)
1247 : m_impl(ImplStr(psz
, conv
)) {}
1248 wxString(const char *psz
, size_t nLength
)
1249 { assign(psz
, nLength
); }
1250 wxString(const char *psz
, const wxMBConv
& conv
, size_t nLength
)
1252 SubstrBufFromMB
str(ImplStr(psz
, nLength
, conv
));
1253 m_impl
.assign(str
.data
, str
.len
);
1256 // and unsigned char*:
1257 wxString(const unsigned char *psz
)
1258 : m_impl(ImplStr((const char*)psz
)) {}
1259 wxString(const unsigned char *psz
, const wxMBConv
& conv
)
1260 : m_impl(ImplStr((const char*)psz
, conv
)) {}
1261 wxString(const unsigned char *psz
, size_t nLength
)
1262 { assign((const char*)psz
, nLength
); }
1263 wxString(const unsigned char *psz
, const wxMBConv
& conv
, size_t nLength
)
1265 SubstrBufFromMB
str(ImplStr((const char*)psz
, nLength
, conv
));
1266 m_impl
.assign(str
.data
, str
.len
);
1269 // ctors from wchar_t* strings:
1270 wxString(const wchar_t *pwz
)
1271 : m_impl(ImplStr(pwz
)) {}
1272 wxString(const wchar_t *pwz
, const wxMBConv
& WXUNUSED(conv
))
1273 : m_impl(ImplStr(pwz
)) {}
1274 wxString(const wchar_t *pwz
, size_t nLength
)
1275 { assign(pwz
, nLength
); }
1276 wxString(const wchar_t *pwz
, const wxMBConv
& WXUNUSED(conv
), size_t nLength
)
1277 { assign(pwz
, nLength
); }
1279 wxString(const wxScopedCharBuffer
& buf
)
1280 { assign(buf
.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
1281 wxString(const wxScopedWCharBuffer
& buf
)
1282 { assign(buf
.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
1284 // NB: this version uses m_impl.c_str() to force making a copy of the
1285 // string, so that "wxString(str.c_str())" idiom for passing strings
1286 // between threads works
1287 wxString(const wxCStrData
& cstr
)
1288 : m_impl(cstr
.AsString().m_impl
.c_str()) { }
1290 // as we provide both ctors with this signature for both char and unsigned
1291 // char string, we need to provide one for wxCStrData to resolve ambiguity
1292 wxString(const wxCStrData
& cstr
, size_t nLength
)
1293 : m_impl(cstr
.AsString().Mid(0, nLength
).m_impl
) {}
1295 // and because wxString is convertible to wxCStrData and const wxChar *
1296 // we also need to provide this one
1297 wxString(const wxString
& str
, size_t nLength
)
1298 { assign(str
, nLength
); }
1301 #if wxUSE_STRING_POS_CACHE
1304 // we need to invalidate our cache entry as another string could be
1305 // recreated at the same address (unlikely, but still possible, with the
1306 // heap-allocated strings but perfectly common with stack-allocated ones)
1309 #endif // wxUSE_STRING_POS_CACHE
1311 // even if we're not built with wxUSE_STL == 1 it is very convenient to allow
1312 // implicit conversions from std::string to wxString and vice verse as this
1313 // allows to use the same strings in non-GUI and GUI code, however we don't
1314 // want to unconditionally add this ctor as it would make wx lib dependent on
1315 // libstdc++ on some Linux versions which is bad, so instead we ask the
1316 // client code to define this wxUSE_STD_STRING symbol if they need it
1317 #if wxUSE_STD_STRING
1318 #if wxUSE_UNICODE_WCHAR
1319 wxString(const wxStdWideString
& str
) : m_impl(str
) {}
1320 #else // UTF-8 or ANSI
1321 wxString(const wxStdWideString
& str
)
1322 { assign(str
.c_str(), str
.length()); }
1325 #if !wxUSE_UNICODE // ANSI build
1326 // FIXME-UTF8: do this in UTF8 build #if wxUSE_UTF8_LOCALE_ONLY, too
1327 wxString(const std::string
& str
) : m_impl(str
) {}
1329 wxString(const std::string
& str
)
1330 { assign(str
.c_str(), str
.length()); }
1332 #endif // wxUSE_STD_STRING
1334 // Unlike ctor from std::string, we provide conversion to std::string only
1335 // if wxUSE_STL and not merely wxUSE_STD_STRING (which is on by default),
1336 // because it conflicts with operator const char/wchar_t*:
1338 #if wxUSE_UNICODE_WCHAR && wxUSE_STL_BASED_WXSTRING
1339 // wxStringImpl is std::string in the encoding we want
1340 operator const wxStdWideString
&() const { return m_impl
; }
1342 // wxStringImpl is either not std::string or needs conversion
1343 operator wxStdWideString() const
1344 // FIXME-UTF8: broken for embedded NULs
1345 { return wxStdWideString(wc_str()); }
1348 #if (!wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY) && wxUSE_STL_BASED_WXSTRING
1349 // wxStringImpl is std::string in the encoding we want
1350 operator const std::string
&() const { return m_impl
; }
1352 // wxStringImpl is either not std::string or needs conversion
1353 operator std::string() const
1354 // FIXME-UTF8: broken for embedded NULs
1355 { return std::string(mb_str()); }
1359 wxString
Clone() const
1361 // make a deep copy of the string, i.e. the returned string will have
1362 // ref count = 1 with refcounted implementation
1363 return wxString::FromImpl(wxStringImpl(m_impl
.c_str(), m_impl
.length()));
1366 // first valid index position
1367 const_iterator
begin() const { return const_iterator(this, m_impl
.begin()); }
1368 iterator
begin() { return iterator(this, m_impl
.begin()); }
1369 // position one after the last valid one
1370 const_iterator
end() const { return const_iterator(this, m_impl
.end()); }
1371 iterator
end() { return iterator(this, m_impl
.end()); }
1373 // first element of the reversed string
1374 const_reverse_iterator
rbegin() const
1375 { return const_reverse_iterator(end()); }
1376 reverse_iterator
rbegin()
1377 { return reverse_iterator(end()); }
1378 // one beyond the end of the reversed string
1379 const_reverse_iterator
rend() const
1380 { return const_reverse_iterator(begin()); }
1381 reverse_iterator
rend()
1382 { return reverse_iterator(begin()); }
1384 // std::string methods:
1385 #if wxUSE_UNICODE_UTF8
1386 size_t length() const
1388 #if wxUSE_STRING_POS_CACHE
1389 wxCACHE_PROFILE_FIELD_INC(lentot
);
1391 Cache::Element
* const cache
= GetCacheElement();
1393 if ( cache
->len
== npos
)
1395 // it's probably not worth trying to be clever and using cache->pos
1396 // here as it's probably 0 anyhow -- you usually call length() before
1397 // starting to index the string
1398 cache
->len
= end() - begin();
1402 wxCACHE_PROFILE_FIELD_INC(lenhits
);
1404 wxSTRING_CACHE_ASSERT( (int)cache
->len
== end() - begin() );
1408 #else // !wxUSE_STRING_POS_CACHE
1409 return end() - begin();
1410 #endif // wxUSE_STRING_POS_CACHE/!wxUSE_STRING_POS_CACHE
1413 size_t length() const { return m_impl
.length(); }
1416 size_type
size() const { return length(); }
1417 size_type
max_size() const { return npos
; }
1419 bool empty() const { return m_impl
.empty(); }
1421 // NB: these methods don't have a well-defined meaning in UTF-8 case
1422 size_type
capacity() const { return m_impl
.capacity(); }
1423 void reserve(size_t sz
) { m_impl
.reserve(sz
); }
1425 void resize(size_t nSize
, wxUniChar ch
= wxT('\0'))
1427 const size_t len
= length();
1431 #if wxUSE_UNICODE_UTF8
1434 wxSTRING_INVALIDATE_CACHE();
1436 // we can't use wxStringImpl::resize() for truncating the string as it
1437 // counts in bytes, not characters
1442 // we also can't use (presumably more efficient) resize() if we have to
1443 // append characters taking more than one byte
1444 if ( !ch
.IsAscii() )
1446 append(nSize
- len
, ch
);
1448 else // can use (presumably faster) resize() version
1449 #endif // wxUSE_UNICODE_UTF8
1451 wxSTRING_INVALIDATE_CACHED_LENGTH();
1453 m_impl
.resize(nSize
, (wxStringCharType
)ch
);
1457 wxString
substr(size_t nStart
= 0, size_t nLen
= npos
) const
1460 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
1461 return FromImpl(m_impl
.substr(pos
, len
));
1464 // generic attributes & operations
1465 // as standard strlen()
1466 size_t Len() const { return length(); }
1467 // string contains any characters?
1468 bool IsEmpty() const { return empty(); }
1469 // empty string is "false", so !str will return true
1470 bool operator!() const { return empty(); }
1471 // truncate the string to given length
1472 wxString
& Truncate(size_t uiLen
);
1473 // empty string contents
1474 void Empty() { clear(); }
1475 // empty the string and free memory
1476 void Clear() { clear(); }
1479 // Is an ascii value
1480 bool IsAscii() const;
1482 bool IsNumber() const;
1484 bool IsWord() const;
1486 // data access (all indexes are 0 based)
1488 wxUniChar
at(size_t n
) const
1489 { return wxStringOperations::DecodeChar(m_impl
.begin() + PosToImpl(n
)); }
1490 wxUniChar
GetChar(size_t n
) const
1492 // read/write access
1493 wxUniCharRef
at(size_t n
)
1494 { return *GetIterForNthChar(n
); }
1495 wxUniCharRef
GetWritableChar(size_t n
)
1498 void SetChar(size_t n
, wxUniChar ch
)
1501 // get last character
1502 wxUniChar
Last() const
1504 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1508 // get writable last character
1511 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1516 Note that we we must define all of the overloads below to avoid
1517 ambiguity when using str[0].
1519 wxUniChar
operator[](int n
) const
1521 wxUniChar
operator[](long n
) const
1523 wxUniChar
operator[](size_t n
) const
1525 #ifndef wxSIZE_T_IS_UINT
1526 wxUniChar
operator[](unsigned int n
) const
1528 #endif // size_t != unsigned int
1530 // operator versions of GetWriteableChar()
1531 wxUniCharRef
operator[](int n
)
1533 wxUniCharRef
operator[](long n
)
1535 wxUniCharRef
operator[](size_t n
)
1537 #ifndef wxSIZE_T_IS_UINT
1538 wxUniCharRef
operator[](unsigned int n
)
1540 #endif // size_t != unsigned int
1544 Overview of wxString conversions, implicit and explicit:
1546 - wxString has a std::[w]string-like c_str() method, however it does
1547 not return a C-style string directly but instead returns wxCStrData
1548 helper object which is convertible to either "char *" narrow string
1549 or "wchar_t *" wide string. Usually the correct conversion will be
1550 applied by the compiler automatically but if this doesn't happen you
1551 need to explicitly choose one using wxCStrData::AsChar() or AsWChar()
1552 methods or another wxString conversion function.
1554 - One of the places where the conversion does *NOT* happen correctly is
1555 when c_str() is passed to a vararg function such as printf() so you
1556 must *NOT* use c_str() with them. Either use wxPrintf() (all wx
1557 functions do handle c_str() correctly, even if they appear to be
1558 vararg (but they're not, really)) or add an explicit AsChar() or, if
1559 compatibility with previous wxWidgets versions is important, add a
1560 cast to "const char *".
1562 - In non-STL mode only, wxString is also implicitly convertible to
1563 wxCStrData. The same warning as above applies.
1565 - c_str() is polymorphic as it can be converted to either narrow or
1566 wide string. If you explicitly need one or the other, choose to use
1567 mb_str() (for narrow) or wc_str() (for wide) instead. Notice that
1568 these functions can return either the pointer to string directly (if
1569 this is what the string uses internally) or a temporary buffer
1570 containing the string and convertible to it. Again, conversion will
1571 usually be done automatically by the compiler but beware of the
1572 vararg functions: you need an explicit cast when using them.
1574 - There are also non-const versions of mb_str() and wc_str() called
1575 char_str() and wchar_str(). They are only meant to be used with
1576 non-const-correct functions and they always return buffers.
1578 - Finally wx_str() returns whatever string representation is used by
1579 wxString internally. It may be either a narrow or wide string
1580 depending on wxWidgets build mode but it will always be a raw pointer
1584 // explicit conversion to wxCStrData
1585 wxCStrData
c_str() const { return wxCStrData(this); }
1586 wxCStrData
data() const { return c_str(); }
1588 // implicit conversion to wxCStrData
1589 operator wxCStrData() const { return c_str(); }
1591 // the first two operators conflict with operators for conversion to
1592 // std::string and they must be disabled in STL build; the next one only
1593 // makes sense if conversions to char* are also defined and not defining it
1594 // in STL build also helps us to get more clear error messages for the code
1595 // which relies on implicit conversion to char* in STL build
1597 operator const char*() const { return c_str(); }
1598 operator const wchar_t*() const { return c_str(); }
1600 // implicit conversion to untyped pointer for compatibility with previous
1601 // wxWidgets versions: this is the same as conversion to const char * so it
1603 operator const void*() const { return c_str(); }
1606 // identical to c_str(), for MFC compatibility
1607 const wxCStrData
GetData() const { return c_str(); }
1609 // explicit conversion to C string in internal representation (char*,
1610 // wchar_t*, UTF-8-encoded char*, depending on the build):
1611 const wxStringCharType
*wx_str() const { return m_impl
.c_str(); }
1613 // conversion to *non-const* multibyte or widestring buffer; modifying
1614 // returned buffer won't affect the string, these methods are only useful
1615 // for passing values to const-incorrect functions
1616 wxWritableCharBuffer
char_str(const wxMBConv
& conv
= wxConvLibc
) const
1617 { return mb_str(conv
); }
1618 wxWritableWCharBuffer
wchar_str() const { return wc_str(); }
1620 // conversion to the buffer of the given type T (= char or wchar_t) and
1621 // also optionally return the buffer length
1623 // this is mostly/only useful for the template functions
1625 // FIXME-VC6: the second argument only exists for VC6 which doesn't support
1626 // explicit template function selection, do not use it unless
1627 // you must support VC6!
1628 template <typename T
>
1629 wxCharTypeBuffer
<T
> tchar_str(size_t *len
= NULL
,
1630 T
* WXUNUSED(dummy
) = NULL
) const
1633 // we need a helper dispatcher depending on type
1634 return wxPrivate::wxStringAsBufHelper
<T
>::Get(*this, len
);
1636 // T can only be char in ANSI build
1640 return wxCharTypeBuffer
<T
>::CreateNonOwned(wx_str());
1641 #endif // Unicode build kind
1644 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
1645 // converting numbers or strings which are certain not to contain special
1646 // chars (typically system functions, X atoms, environment variables etc.)
1648 // the behaviour of these functions with the strings containing anything
1649 // else than 7 bit ASCII characters is undefined, use at your own risk.
1651 static wxString
FromAscii(const char *ascii
, size_t len
);
1652 static wxString
FromAscii(const char *ascii
);
1653 static wxString
FromAscii(char ascii
);
1654 const wxScopedCharBuffer
ToAscii() const;
1656 static wxString
FromAscii(const char *ascii
) { return wxString( ascii
); }
1657 static wxString
FromAscii(const char *ascii
, size_t len
)
1658 { return wxString( ascii
, len
); }
1659 static wxString
FromAscii(char ascii
) { return wxString( ascii
); }
1660 const char *ToAscii() const { return c_str(); }
1661 #endif // Unicode/!Unicode
1663 // also provide unsigned char overloads as signed/unsigned doesn't matter
1664 // for 7 bit ASCII characters
1665 static wxString
FromAscii(const unsigned char *ascii
)
1666 { return FromAscii((const char *)ascii
); }
1667 static wxString
FromAscii(const unsigned char *ascii
, size_t len
)
1668 { return FromAscii((const char *)ascii
, len
); }
1670 // conversion to/from UTF-8:
1671 #if wxUSE_UNICODE_UTF8
1672 static wxString
FromUTF8Unchecked(const char *utf8
)
1675 return wxEmptyString
;
1677 wxASSERT( wxStringOperations::IsValidUtf8String(utf8
) );
1678 return FromImpl(wxStringImpl(utf8
));
1680 static wxString
FromUTF8Unchecked(const char *utf8
, size_t len
)
1683 return wxEmptyString
;
1685 return FromUTF8Unchecked(utf8
);
1687 wxASSERT( wxStringOperations::IsValidUtf8String(utf8
, len
) );
1688 return FromImpl(wxStringImpl(utf8
, len
));
1691 static wxString
FromUTF8(const char *utf8
)
1693 if ( !utf8
|| !wxStringOperations::IsValidUtf8String(utf8
) )
1696 return FromImpl(wxStringImpl(utf8
));
1698 static wxString
FromUTF8(const char *utf8
, size_t len
)
1701 return FromUTF8(utf8
);
1703 if ( !utf8
|| !wxStringOperations::IsValidUtf8String(utf8
, len
) )
1706 return FromImpl(wxStringImpl(utf8
, len
));
1709 const char* utf8_str() const { return wx_str(); }
1710 const char* ToUTF8() const { return wx_str(); }
1712 // this function exists in UTF-8 build only and returns the length of the
1713 // internal UTF-8 representation
1714 size_t utf8_length() const { return m_impl
.length(); }
1715 #elif wxUSE_UNICODE_WCHAR
1716 static wxString
FromUTF8(const char *utf8
, size_t len
= npos
)
1717 { return wxString(utf8
, wxMBConvUTF8(), len
); }
1718 static wxString
FromUTF8Unchecked(const char *utf8
, size_t len
= npos
)
1720 const wxString
s(utf8
, wxMBConvUTF8(), len
);
1721 wxASSERT_MSG( !utf8
|| !*utf8
|| !s
.empty(),
1722 "string must be valid UTF-8" );
1725 const wxScopedCharBuffer
utf8_str() const { return mb_str(wxMBConvUTF8()); }
1726 const wxScopedCharBuffer
ToUTF8() const { return utf8_str(); }
1728 static wxString
FromUTF8(const char *utf8
)
1729 { return wxString(wxMBConvUTF8().cMB2WC(utf8
)); }
1730 static wxString
FromUTF8(const char *utf8
, size_t len
)
1733 wxScopedWCharBuffer
buf(wxMBConvUTF8().cMB2WC(utf8
, len
== npos
? wxNO_LEN
: len
, &wlen
));
1734 return wxString(buf
.data(), wlen
);
1736 static wxString
FromUTF8Unchecked(const char *utf8
, size_t len
= npos
)
1739 wxScopedWCharBuffer buf
1741 wxMBConvUTF8().cMB2WC
1744 len
== npos
? wxNO_LEN
: len
,
1748 wxASSERT_MSG( !utf8
|| !*utf8
|| wlen
,
1749 "string must be valid UTF-8" );
1751 return wxString(buf
.data(), wlen
);
1753 const wxScopedCharBuffer
utf8_str() const
1754 { return wxMBConvUTF8().cWC2MB(wc_str()); }
1755 const wxScopedCharBuffer
ToUTF8() const { return utf8_str(); }
1758 // functions for storing binary data in wxString:
1760 static wxString
From8BitData(const char *data
, size_t len
)
1761 { return wxString(data
, wxConvISO8859_1
, len
); }
1762 // version for NUL-terminated data:
1763 static wxString
From8BitData(const char *data
)
1764 { return wxString(data
, wxConvISO8859_1
); }
1765 const wxScopedCharBuffer
To8BitData() const
1766 { return mb_str(wxConvISO8859_1
); }
1768 static wxString
From8BitData(const char *data
, size_t len
)
1769 { return wxString(data
, len
); }
1770 // version for NUL-terminated data:
1771 static wxString
From8BitData(const char *data
)
1772 { return wxString(data
); }
1773 const char *To8BitData() const { return c_str(); }
1774 #endif // Unicode/ANSI
1776 // conversions with (possible) format conversions: have to return a
1777 // buffer with temporary data
1779 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
1780 // return an ANSI (multibyte) string, wc_str() to return a wide string and
1781 // fn_str() to return a string which should be used with the OS APIs
1782 // accepting the file names. The return value is always the same, but the
1783 // type differs because a function may either return pointer to the buffer
1784 // directly or have to use intermediate buffer for translation.
1787 #if wxUSE_UTF8_LOCALE_ONLY
1788 const char* mb_str() const { return wx_str(); }
1789 const wxScopedCharBuffer
mb_str(const wxMBConv
& conv
) const;
1791 const wxScopedCharBuffer
mb_str(const wxMBConv
& conv
= wxConvLibc
) const;
1794 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
1796 #if wxUSE_UNICODE_WCHAR
1797 const wchar_t* wc_str() const { return wx_str(); }
1798 #elif wxUSE_UNICODE_UTF8
1799 const wxScopedWCharBuffer
wc_str() const;
1801 // for compatibility with !wxUSE_UNICODE version
1802 const wxWX2WCbuf
wc_str(const wxMBConv
& WXUNUSED(conv
)) const
1803 { return wc_str(); }
1806 const wxScopedCharBuffer
fn_str() const { return mb_str(wxConvFile
); }
1808 const wxWX2WCbuf
fn_str() const { return wc_str(); }
1809 #endif // wxMBFILES/!wxMBFILES
1812 const wxChar
* mb_str() const { return wx_str(); }
1814 // for compatibility with wxUSE_UNICODE version
1815 const char* mb_str(const wxMBConv
& WXUNUSED(conv
)) const { return wx_str(); }
1817 const wxWX2MBbuf
mbc_str() const { return mb_str(); }
1820 const wxScopedWCharBuffer
wc_str(const wxMBConv
& conv
= wxConvLibc
) const;
1821 #endif // wxUSE_WCHAR_T
1822 const wxScopedCharBuffer
fn_str() const
1823 { return wxConvFile
.cWC2WX( wc_str( wxConvLibc
) ); }
1824 #endif // Unicode/ANSI
1826 #if wxUSE_UNICODE_UTF8
1827 const wxScopedWCharBuffer
t_str() const { return wc_str(); }
1828 #elif wxUSE_UNICODE_WCHAR
1829 const wchar_t* t_str() const { return wx_str(); }
1831 const char* t_str() const { return wx_str(); }
1835 // overloaded assignment
1836 // from another wxString
1837 wxString
& operator=(const wxString
& stringSrc
)
1839 if ( this != &stringSrc
)
1841 wxSTRING_INVALIDATE_CACHE();
1843 m_impl
= stringSrc
.m_impl
;
1849 wxString
& operator=(const wxCStrData
& cstr
)
1850 { return *this = cstr
.AsString(); }
1852 wxString
& operator=(wxUniChar ch
)
1854 wxSTRING_INVALIDATE_CACHE();
1856 #if wxUSE_UNICODE_UTF8
1857 if ( !ch
.IsAscii() )
1858 m_impl
= wxStringOperations::EncodeChar(ch
);
1860 #endif // wxUSE_UNICODE_UTF8
1861 m_impl
= (wxStringCharType
)ch
;
1865 wxString
& operator=(wxUniCharRef ch
)
1866 { return operator=((wxUniChar
)ch
); }
1867 wxString
& operator=(char ch
)
1868 { return operator=(wxUniChar(ch
)); }
1869 wxString
& operator=(unsigned char ch
)
1870 { return operator=(wxUniChar(ch
)); }
1871 wxString
& operator=(wchar_t ch
)
1872 { return operator=(wxUniChar(ch
)); }
1873 // from a C string - STL probably will crash on NULL,
1874 // so we need to compensate in that case
1875 #if wxUSE_STL_BASED_WXSTRING
1876 wxString
& operator=(const char *psz
)
1878 wxSTRING_INVALIDATE_CACHE();
1881 m_impl
= ImplStr(psz
);
1888 wxString
& operator=(const wchar_t *pwz
)
1890 wxSTRING_INVALIDATE_CACHE();
1893 m_impl
= ImplStr(pwz
);
1899 #else // !wxUSE_STL_BASED_WXSTRING
1900 wxString
& operator=(const char *psz
)
1902 wxSTRING_INVALIDATE_CACHE();
1904 m_impl
= ImplStr(psz
);
1909 wxString
& operator=(const wchar_t *pwz
)
1911 wxSTRING_INVALIDATE_CACHE();
1913 m_impl
= ImplStr(pwz
);
1917 #endif // wxUSE_STL_BASED_WXSTRING/!wxUSE_STL_BASED_WXSTRING
1919 wxString
& operator=(const unsigned char *psz
)
1920 { return operator=((const char*)psz
); }
1922 // from wxScopedWCharBuffer
1923 wxString
& operator=(const wxScopedWCharBuffer
& s
)
1924 { return operator=(s
.data()); } // FIXME-UTF8: fix for embedded NULs
1925 // from wxScopedCharBuffer
1926 wxString
& operator=(const wxScopedCharBuffer
& s
)
1927 { return operator=(s
.data()); } // FIXME-UTF8: fix for embedded NULs
1929 // string concatenation
1930 // in place concatenation
1932 Concatenate and return the result. Note that the left to right
1933 associativity of << allows to write things like "str << str1 << str2
1934 << ..." (unlike with +=)
1937 wxString
& operator<<(const wxString
& s
)
1939 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1940 wxASSERT_MSG( s
.IsValid(),
1941 _T("did you forget to call UngetWriteBuf()?") );
1947 // string += C string
1948 wxString
& operator<<(const char *psz
)
1949 { append(psz
); return *this; }
1950 wxString
& operator<<(const wchar_t *pwz
)
1951 { append(pwz
); return *this; }
1952 wxString
& operator<<(const wxCStrData
& psz
)
1953 { append(psz
.AsString()); return *this; }
1955 wxString
& operator<<(wxUniChar ch
) { append(1, ch
); return *this; }
1956 wxString
& operator<<(wxUniCharRef ch
) { append(1, ch
); return *this; }
1957 wxString
& operator<<(char ch
) { append(1, ch
); return *this; }
1958 wxString
& operator<<(unsigned char ch
) { append(1, ch
); return *this; }
1959 wxString
& operator<<(wchar_t ch
) { append(1, ch
); return *this; }
1961 // string += buffer (i.e. from wxGetString)
1962 wxString
& operator<<(const wxScopedWCharBuffer
& s
)
1963 { return operator<<((const wchar_t *)s
); }
1964 wxString
& operator<<(const wxScopedCharBuffer
& s
)
1965 { return operator<<((const char *)s
); }
1967 // string += C string
1968 wxString
& Append(const wxString
& s
)
1970 // test for empty() to share the string if possible
1977 wxString
& Append(const char* psz
)
1978 { append(psz
); return *this; }
1979 wxString
& Append(const wchar_t* pwz
)
1980 { append(pwz
); return *this; }
1981 wxString
& Append(const wxCStrData
& psz
)
1982 { append(psz
); return *this; }
1983 wxString
& Append(const wxScopedCharBuffer
& psz
)
1984 { append(psz
); return *this; }
1985 wxString
& Append(const wxScopedWCharBuffer
& psz
)
1986 { append(psz
); return *this; }
1987 wxString
& Append(const char* psz
, size_t nLen
)
1988 { append(psz
, nLen
); return *this; }
1989 wxString
& Append(const wchar_t* pwz
, size_t nLen
)
1990 { append(pwz
, nLen
); return *this; }
1991 wxString
& Append(const wxCStrData
& psz
, size_t nLen
)
1992 { append(psz
, nLen
); return *this; }
1993 wxString
& Append(const wxScopedCharBuffer
& psz
, size_t nLen
)
1994 { append(psz
, nLen
); return *this; }
1995 wxString
& Append(const wxScopedWCharBuffer
& psz
, size_t nLen
)
1996 { append(psz
, nLen
); return *this; }
1997 // append count copies of given character
1998 wxString
& Append(wxUniChar ch
, size_t count
= 1u)
1999 { append(count
, ch
); return *this; }
2000 wxString
& Append(wxUniCharRef ch
, size_t count
= 1u)
2001 { append(count
, ch
); return *this; }
2002 wxString
& Append(char ch
, size_t count
= 1u)
2003 { append(count
, ch
); return *this; }
2004 wxString
& Append(unsigned char ch
, size_t count
= 1u)
2005 { append(count
, ch
); return *this; }
2006 wxString
& Append(wchar_t ch
, size_t count
= 1u)
2007 { append(count
, ch
); return *this; }
2009 // prepend a string, return the string itself
2010 wxString
& Prepend(const wxString
& str
)
2011 { *this = str
+ *this; return *this; }
2013 // non-destructive concatenation
2015 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
,
2016 const wxString
& string2
);
2017 // string with a single char
2018 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
2019 // char with a string
2020 friend wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
2021 // string with C string
2022 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
2024 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
2025 const wchar_t *pwz
);
2026 // C string with string
2027 friend wxString WXDLLIMPEXP_BASE
operator+(const char *psz
,
2028 const wxString
& string
);
2029 friend wxString WXDLLIMPEXP_BASE
operator+(const wchar_t *pwz
,
2030 const wxString
& string
);
2032 // stream-like functions
2033 // insert an int into string
2034 wxString
& operator<<(int i
)
2035 { return (*this) << Format(_T("%d"), i
); }
2036 // insert an unsigned int into string
2037 wxString
& operator<<(unsigned int ui
)
2038 { return (*this) << Format(_T("%u"), ui
); }
2039 // insert a long into string
2040 wxString
& operator<<(long l
)
2041 { return (*this) << Format(_T("%ld"), l
); }
2042 // insert an unsigned long into string
2043 wxString
& operator<<(unsigned long ul
)
2044 { return (*this) << Format(_T("%lu"), ul
); }
2045 #if defined wxLongLong_t && !defined wxLongLongIsLong
2046 // insert a long long if they exist and aren't longs
2047 wxString
& operator<<(wxLongLong_t ll
)
2049 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("d");
2050 return (*this) << Format(fmt
, ll
);
2052 // insert an unsigned long long
2053 wxString
& operator<<(wxULongLong_t ull
)
2055 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("u");
2056 return (*this) << Format(fmt
, ull
);
2058 #endif // wxLongLong_t && !wxLongLongIsLong
2059 // insert a float into string
2060 wxString
& operator<<(float f
)
2061 { return (*this) << Format(_T("%f"), f
); }
2062 // insert a double into string
2063 wxString
& operator<<(double d
)
2064 { return (*this) << Format(_T("%g"), d
); }
2066 // string comparison
2067 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
2068 int Cmp(const char *psz
) const
2069 { return compare(psz
); }
2070 int Cmp(const wchar_t *pwz
) const
2071 { return compare(pwz
); }
2072 int Cmp(const wxString
& s
) const
2073 { return compare(s
); }
2074 int Cmp(const wxCStrData
& s
) const
2075 { return compare(s
); }
2076 int Cmp(const wxScopedCharBuffer
& s
) const
2077 { return compare(s
); }
2078 int Cmp(const wxScopedWCharBuffer
& s
) const
2079 { return compare(s
); }
2080 // same as Cmp() but not case-sensitive
2081 int CmpNoCase(const wxString
& s
) const;
2083 // test for the string equality, either considering case or not
2084 // (if compareWithCase then the case matters)
2085 bool IsSameAs(const wxString
& str
, bool compareWithCase
= true) const
2087 #if !wxUSE_UNICODE_UTF8
2088 // in UTF-8 build, length() is O(n) and doing this would be _slower_
2089 if ( length() != str
.length() )
2092 return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0;
2094 bool IsSameAs(const char *str
, bool compareWithCase
= true) const
2095 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
2096 bool IsSameAs(const wchar_t *str
, bool compareWithCase
= true) const
2097 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
2099 bool IsSameAs(const wxCStrData
& str
, bool compareWithCase
= true) const
2100 { return IsSameAs(str
.AsString(), compareWithCase
); }
2101 bool IsSameAs(const wxScopedCharBuffer
& str
, bool compareWithCase
= true) const
2102 { return IsSameAs(str
.data(), compareWithCase
); }
2103 bool IsSameAs(const wxScopedWCharBuffer
& str
, bool compareWithCase
= true) const
2104 { return IsSameAs(str
.data(), compareWithCase
); }
2105 // comparison with a single character: returns true if equal
2106 bool IsSameAs(wxUniChar c
, bool compareWithCase
= true) const;
2107 // FIXME-UTF8: remove these overloads
2108 bool IsSameAs(wxUniCharRef c
, bool compareWithCase
= true) const
2109 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
2110 bool IsSameAs(char c
, bool compareWithCase
= true) const
2111 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
2112 bool IsSameAs(unsigned char c
, bool compareWithCase
= true) const
2113 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
2114 bool IsSameAs(wchar_t c
, bool compareWithCase
= true) const
2115 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
2116 bool IsSameAs(int c
, bool compareWithCase
= true) const
2117 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
2119 // simple sub-string extraction
2120 // return substring starting at nFirst of length nCount (or till the end
2121 // if nCount = default value)
2122 wxString
Mid(size_t nFirst
, size_t nCount
= npos
) const;
2124 // operator version of Mid()
2125 wxString
operator()(size_t start
, size_t len
) const
2126 { return Mid(start
, len
); }
2128 // check if the string starts with the given prefix and return the rest
2129 // of the string in the provided pointer if it is not NULL; otherwise
2131 bool StartsWith(const wxString
& prefix
, wxString
*rest
= NULL
) const;
2132 // check if the string ends with the given suffix and return the
2133 // beginning of the string before the suffix in the provided pointer if
2134 // it is not NULL; otherwise return false
2135 bool EndsWith(const wxString
& suffix
, wxString
*rest
= NULL
) const;
2137 // get first nCount characters
2138 wxString
Left(size_t nCount
) const;
2139 // get last nCount characters
2140 wxString
Right(size_t nCount
) const;
2141 // get all characters before the first occurrence of ch
2142 // (returns the whole string if ch not found)
2143 wxString
BeforeFirst(wxUniChar ch
) const;
2144 // get all characters before the last occurrence of ch
2145 // (returns empty string if ch not found)
2146 wxString
BeforeLast(wxUniChar ch
) const;
2147 // get all characters after the first occurrence of ch
2148 // (returns empty string if ch not found)
2149 wxString
AfterFirst(wxUniChar ch
) const;
2150 // get all characters after the last occurrence of ch
2151 // (returns the whole string if ch not found)
2152 wxString
AfterLast(wxUniChar ch
) const;
2154 // for compatibility only, use more explicitly named functions above
2155 wxString
Before(wxUniChar ch
) const { return BeforeLast(ch
); }
2156 wxString
After(wxUniChar ch
) const { return AfterFirst(ch
); }
2159 // convert to upper case in place, return the string itself
2160 wxString
& MakeUpper();
2161 // convert to upper case, return the copy of the string
2162 wxString
Upper() const { return wxString(*this).MakeUpper(); }
2163 // convert to lower case in place, return the string itself
2164 wxString
& MakeLower();
2165 // convert to lower case, return the copy of the string
2166 wxString
Lower() const { return wxString(*this).MakeLower(); }
2167 // convert the first character to the upper case and the rest to the
2168 // lower one, return the modified string itself
2169 wxString
& MakeCapitalized();
2170 // convert the first character to the upper case and the rest to the
2171 // lower one, return the copy of the string
2172 wxString
Capitalize() const { return wxString(*this).MakeCapitalized(); }
2174 // trimming/padding whitespace (either side) and truncating
2175 // remove spaces from left or from right (default) side
2176 wxString
& Trim(bool bFromRight
= true);
2177 // add nCount copies chPad in the beginning or at the end (default)
2178 wxString
& Pad(size_t nCount
, wxUniChar chPad
= wxT(' '), bool bFromRight
= true);
2180 // searching and replacing
2181 // searching (return starting index, or -1 if not found)
2182 int Find(wxUniChar ch
, bool bFromEnd
= false) const; // like strchr/strrchr
2183 int Find(wxUniCharRef ch
, bool bFromEnd
= false) const
2184 { return Find(wxUniChar(ch
), bFromEnd
); }
2185 int Find(char ch
, bool bFromEnd
= false) const
2186 { return Find(wxUniChar(ch
), bFromEnd
); }
2187 int Find(unsigned char ch
, bool bFromEnd
= false) const
2188 { return Find(wxUniChar(ch
), bFromEnd
); }
2189 int Find(wchar_t ch
, bool bFromEnd
= false) const
2190 { return Find(wxUniChar(ch
), bFromEnd
); }
2191 // searching (return starting index, or -1 if not found)
2192 int Find(const wxString
& sub
) const // like strstr
2194 size_type idx
= find(sub
);
2195 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
2197 int Find(const char *sub
) const // like strstr
2199 size_type idx
= find(sub
);
2200 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
2202 int Find(const wchar_t *sub
) const // like strstr
2204 size_type idx
= find(sub
);
2205 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
2208 int Find(const wxCStrData
& sub
) const
2209 { return Find(sub
.AsString()); }
2210 int Find(const wxScopedCharBuffer
& sub
) const
2211 { return Find(sub
.data()); }
2212 int Find(const wxScopedWCharBuffer
& sub
) const
2213 { return Find(sub
.data()); }
2215 // replace first (or all of bReplaceAll) occurrences of substring with
2216 // another string, returns the number of replacements made
2217 size_t Replace(const wxString
& strOld
,
2218 const wxString
& strNew
,
2219 bool bReplaceAll
= true);
2221 // check if the string contents matches a mask containing '*' and '?'
2222 bool Matches(const wxString
& mask
) const;
2224 // conversion to numbers: all functions return true only if the whole
2225 // string is a number and put the value of this number into the pointer
2226 // provided, the base is the numeric base in which the conversion should be
2227 // done and must be comprised between 2 and 36 or be 0 in which case the
2228 // standard C rules apply (leading '0' => octal, "0x" => hex)
2229 // convert to a signed integer
2230 bool ToLong(long *val
, int base
= 10) const;
2231 // convert to an unsigned integer
2232 bool ToULong(unsigned long *val
, int base
= 10) const;
2233 // convert to wxLongLong
2234 #if defined(wxLongLong_t)
2235 bool ToLongLong(wxLongLong_t
*val
, int base
= 10) const;
2236 // convert to wxULongLong
2237 bool ToULongLong(wxULongLong_t
*val
, int base
= 10) const;
2238 #endif // wxLongLong_t
2239 // convert to a double
2240 bool ToDouble(double *val
) const;
2243 // conversions to numbers using C locale
2244 // convert to a signed integer
2245 bool ToCLong(long *val
, int base
= 10) const;
2246 // convert to an unsigned integer
2247 bool ToCULong(unsigned long *val
, int base
= 10) const;
2248 // convert to a double
2249 bool ToCDouble(double *val
) const;
2252 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2253 // formatted input/output
2254 // as sprintf(), returns the number of characters written or < 0 on error
2255 // (take 'this' into account in attribute parameter count)
2256 // int Printf(const wxString& format, ...);
2257 WX_DEFINE_VARARG_FUNC(int, Printf
, 1, (const wxFormatString
&),
2258 DoPrintfWchar
, DoPrintfUtf8
)
2260 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
2261 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wxString
&),
2262 (wxFormatString(f1
)));
2263 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wxCStrData
&),
2264 (wxFormatString(f1
)));
2265 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const char*),
2266 (wxFormatString(f1
)));
2267 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wchar_t*),
2268 (wxFormatString(f1
)));
2270 #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN
2271 // as vprintf(), returns the number of characters written or < 0 on error
2272 int PrintfV(const wxString
& format
, va_list argptr
);
2274 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2275 // returns the string containing the result of Printf() to it
2276 // static wxString Format(const wxString& format, ...) WX_ATTRIBUTE_PRINTF_1;
2277 WX_DEFINE_VARARG_FUNC(static wxString
, Format
, 1, (const wxFormatString
&),
2278 DoFormatWchar
, DoFormatUtf8
)
2280 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
2281 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wxString
&),
2282 (wxFormatString(f1
)));
2283 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wxCStrData
&),
2284 (wxFormatString(f1
)));
2285 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const char*),
2286 (wxFormatString(f1
)));
2287 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wchar_t*),
2288 (wxFormatString(f1
)));
2291 // the same as above, but takes a va_list
2292 static wxString
FormatV(const wxString
& format
, va_list argptr
);
2294 // raw access to string memory
2295 // ensure that string has space for at least nLen characters
2296 // only works if the data of this string is not shared
2297 bool Alloc(size_t nLen
) { reserve(nLen
); return capacity() >= nLen
; }
2298 // minimize the string's memory
2299 // only works if the data of this string is not shared
2301 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2302 // These are deprecated, use wxStringBuffer or wxStringBufferLength instead
2304 // get writable buffer of at least nLen bytes. Unget() *must* be called
2305 // a.s.a.p. to put string back in a reasonable state!
2306 wxDEPRECATED( wxStringCharType
*GetWriteBuf(size_t nLen
) );
2307 // call this immediately after GetWriteBuf() has been used
2308 wxDEPRECATED( void UngetWriteBuf() );
2309 wxDEPRECATED( void UngetWriteBuf(size_t nLen
) );
2310 #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && wxUSE_UNICODE_UTF8
2312 // wxWidgets version 1 compatibility functions
2315 wxString
SubString(size_t from
, size_t to
) const
2316 { return Mid(from
, (to
- from
+ 1)); }
2317 // values for second parameter of CompareTo function
2318 enum caseCompare
{exact
, ignoreCase
};
2319 // values for first parameter of Strip function
2320 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
2322 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2324 // (take 'this' into account in attribute parameter count)
2325 // int sprintf(const wxString& format, ...) WX_ATTRIBUTE_PRINTF_2;
2326 WX_DEFINE_VARARG_FUNC(int, sprintf
, 1, (const wxFormatString
&),
2327 DoPrintfWchar
, DoPrintfUtf8
)
2329 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
2330 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wxString
&),
2331 (wxFormatString(f1
)));
2332 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wxCStrData
&),
2333 (wxFormatString(f1
)));
2334 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const char*),
2335 (wxFormatString(f1
)));
2336 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wchar_t*),
2337 (wxFormatString(f1
)));
2339 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
2342 int CompareTo(const wxChar
* psz
, caseCompare cmp
= exact
) const
2343 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
2346 size_t Length() const { return length(); }
2347 // Count the number of characters
2348 int Freq(wxUniChar ch
) const;
2350 void LowerCase() { MakeLower(); }
2352 void UpperCase() { MakeUpper(); }
2353 // use Trim except that it doesn't change this string
2354 wxString
Strip(stripType w
= trailing
) const;
2356 // use Find (more general variants not yet supported)
2357 size_t Index(const wxChar
* psz
) const { return Find(psz
); }
2358 size_t Index(wxUniChar ch
) const { return Find(ch
); }
2360 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
2361 wxString
& RemoveLast(size_t n
= 1) { return Truncate(length() - n
); }
2363 wxString
& Remove(size_t nStart
, size_t nLen
)
2364 { return (wxString
&)erase( nStart
, nLen
); }
2367 int First( wxUniChar ch
) const { return Find(ch
); }
2368 int First( wxUniCharRef ch
) const { return Find(ch
); }
2369 int First( char ch
) const { return Find(ch
); }
2370 int First( unsigned char ch
) const { return Find(ch
); }
2371 int First( wchar_t ch
) const { return Find(ch
); }
2372 int First( const wxString
& str
) const { return Find(str
); }
2373 int Last( wxUniChar ch
) const { return Find(ch
, true); }
2374 bool Contains(const wxString
& str
) const { return Find(str
) != wxNOT_FOUND
; }
2377 bool IsNull() const { return empty(); }
2379 // std::string compatibility functions
2381 // take nLen chars starting at nPos
2382 wxString(const wxString
& str
, size_t nPos
, size_t nLen
)
2383 { assign(str
, nPos
, nLen
); }
2384 // take all characters from first to last
2385 wxString(const_iterator first
, const_iterator last
)
2386 : m_impl(first
.impl(), last
.impl()) { }
2387 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2388 // the 2 overloads below are for compatibility with the existing code using
2389 // pointers instead of iterators
2390 wxString(const char *first
, const char *last
)
2392 SubstrBufFromMB
str(ImplStr(first
, last
- first
));
2393 m_impl
.assign(str
.data
, str
.len
);
2395 wxString(const wchar_t *first
, const wchar_t *last
)
2397 SubstrBufFromWC
str(ImplStr(first
, last
- first
));
2398 m_impl
.assign(str
.data
, str
.len
);
2400 // and this one is needed to compile code adding offsets to c_str() result
2401 wxString(const wxCStrData
& first
, const wxCStrData
& last
)
2402 : m_impl(CreateConstIterator(first
).impl(),
2403 CreateConstIterator(last
).impl())
2405 wxASSERT_MSG( first
.m_str
== last
.m_str
,
2406 _T("pointers must be into the same string") );
2408 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2410 // lib.string.modifiers
2411 // append elements str[pos], ..., str[pos+n]
2412 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
2414 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2417 str
.PosLenToImpl(pos
, n
, &from
, &len
);
2418 m_impl
.append(str
.m_impl
, from
, len
);
2422 wxString
& append(const wxString
& str
)
2424 wxSTRING_UPDATE_CACHED_LENGTH(str
.length());
2426 m_impl
.append(str
.m_impl
);
2430 // append first n (or all if n == npos) characters of sz
2431 wxString
& append(const char *sz
)
2433 wxSTRING_INVALIDATE_CACHED_LENGTH();
2435 m_impl
.append(ImplStr(sz
));
2439 wxString
& append(const wchar_t *sz
)
2441 wxSTRING_INVALIDATE_CACHED_LENGTH();
2443 m_impl
.append(ImplStr(sz
));
2447 wxString
& append(const char *sz
, size_t n
)
2449 wxSTRING_INVALIDATE_CACHED_LENGTH();
2451 SubstrBufFromMB
str(ImplStr(sz
, n
));
2452 m_impl
.append(str
.data
, str
.len
);
2455 wxString
& append(const wchar_t *sz
, size_t n
)
2457 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2459 SubstrBufFromWC
str(ImplStr(sz
, n
));
2460 m_impl
.append(str
.data
, str
.len
);
2464 wxString
& append(const wxCStrData
& str
)
2465 { return append(str
.AsString()); }
2466 wxString
& append(const wxScopedCharBuffer
& str
)
2467 { return append(str
.data()); }
2468 wxString
& append(const wxScopedWCharBuffer
& str
)
2469 { return append(str
.data()); }
2470 wxString
& append(const wxCStrData
& str
, size_t n
)
2471 { return append(str
.AsString(), 0, n
); }
2472 wxString
& append(const wxScopedCharBuffer
& str
, size_t n
)
2473 { return append(str
.data(), n
); }
2474 wxString
& append(const wxScopedWCharBuffer
& str
, size_t n
)
2475 { return append(str
.data(), n
); }
2477 // append n copies of ch
2478 wxString
& append(size_t n
, wxUniChar ch
)
2480 #if wxUSE_UNICODE_UTF8
2481 if ( !ch
.IsAscii() )
2483 wxSTRING_INVALIDATE_CACHED_LENGTH();
2485 m_impl
.append(wxStringOperations::EncodeNChars(n
, ch
));
2490 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2492 m_impl
.append(n
, (wxStringCharType
)ch
);
2498 wxString
& append(size_t n
, wxUniCharRef ch
)
2499 { return append(n
, wxUniChar(ch
)); }
2500 wxString
& append(size_t n
, char ch
)
2501 { return append(n
, wxUniChar(ch
)); }
2502 wxString
& append(size_t n
, unsigned char ch
)
2503 { return append(n
, wxUniChar(ch
)); }
2504 wxString
& append(size_t n
, wchar_t ch
)
2505 { return append(n
, wxUniChar(ch
)); }
2507 // append from first to last
2508 wxString
& append(const_iterator first
, const_iterator last
)
2510 wxSTRING_INVALIDATE_CACHED_LENGTH();
2512 m_impl
.append(first
.impl(), last
.impl());
2515 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2516 wxString
& append(const char *first
, const char *last
)
2517 { return append(first
, last
- first
); }
2518 wxString
& append(const wchar_t *first
, const wchar_t *last
)
2519 { return append(first
, last
- first
); }
2520 wxString
& append(const wxCStrData
& first
, const wxCStrData
& last
)
2521 { return append(CreateConstIterator(first
), CreateConstIterator(last
)); }
2522 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2524 // same as `this_string = str'
2525 wxString
& assign(const wxString
& str
)
2527 wxSTRING_SET_CACHED_LENGTH(str
.length());
2529 m_impl
= str
.m_impl
;
2534 wxString
& assign(const wxString
& str
, size_t len
)
2536 wxSTRING_SET_CACHED_LENGTH(len
);
2538 m_impl
.assign(str
.m_impl
, 0, str
.LenToImpl(len
));
2543 // same as ` = str[pos..pos + n]
2544 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
2547 str
.PosLenToImpl(pos
, n
, &from
, &len
);
2548 m_impl
.assign(str
.m_impl
, from
, len
);
2550 // it's important to call this after PosLenToImpl() above in case str is
2551 // the same string as this one
2552 wxSTRING_SET_CACHED_LENGTH(n
);
2557 // same as `= first n (or all if n == npos) characters of sz'
2558 wxString
& assign(const char *sz
)
2560 wxSTRING_INVALIDATE_CACHE();
2562 m_impl
.assign(ImplStr(sz
));
2567 wxString
& assign(const wchar_t *sz
)
2569 wxSTRING_INVALIDATE_CACHE();
2571 m_impl
.assign(ImplStr(sz
));
2576 wxString
& assign(const char *sz
, size_t n
)
2578 wxSTRING_SET_CACHED_LENGTH(n
);
2580 SubstrBufFromMB
str(ImplStr(sz
, n
));
2581 m_impl
.assign(str
.data
, str
.len
);
2586 wxString
& assign(const wchar_t *sz
, size_t n
)
2588 wxSTRING_SET_CACHED_LENGTH(n
);
2590 SubstrBufFromWC
str(ImplStr(sz
, n
));
2591 m_impl
.assign(str
.data
, str
.len
);
2596 wxString
& assign(const wxCStrData
& str
)
2597 { return assign(str
.AsString()); }
2598 wxString
& assign(const wxScopedCharBuffer
& str
)
2599 { return assign(str
.data()); }
2600 wxString
& assign(const wxScopedWCharBuffer
& str
)
2601 { return assign(str
.data()); }
2602 wxString
& assign(const wxCStrData
& str
, size_t len
)
2603 { return assign(str
.AsString(), len
); }
2604 wxString
& assign(const wxScopedCharBuffer
& str
, size_t len
)
2605 { return assign(str
.data(), len
); }
2606 wxString
& assign(const wxScopedWCharBuffer
& str
, size_t len
)
2607 { return assign(str
.data(), len
); }
2609 // same as `= n copies of ch'
2610 wxString
& assign(size_t n
, wxUniChar ch
)
2612 wxSTRING_SET_CACHED_LENGTH(n
);
2614 #if wxUSE_UNICODE_UTF8
2615 if ( !ch
.IsAscii() )
2616 m_impl
.assign(wxStringOperations::EncodeNChars(n
, ch
));
2619 m_impl
.assign(n
, (wxStringCharType
)ch
);
2624 wxString
& assign(size_t n
, wxUniCharRef ch
)
2625 { return assign(n
, wxUniChar(ch
)); }
2626 wxString
& assign(size_t n
, char ch
)
2627 { return assign(n
, wxUniChar(ch
)); }
2628 wxString
& assign(size_t n
, unsigned char ch
)
2629 { return assign(n
, wxUniChar(ch
)); }
2630 wxString
& assign(size_t n
, wchar_t ch
)
2631 { return assign(n
, wxUniChar(ch
)); }
2633 // assign from first to last
2634 wxString
& assign(const_iterator first
, const_iterator last
)
2636 wxSTRING_INVALIDATE_CACHE();
2638 m_impl
.assign(first
.impl(), last
.impl());
2642 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2643 wxString
& assign(const char *first
, const char *last
)
2644 { return assign(first
, last
- first
); }
2645 wxString
& assign(const wchar_t *first
, const wchar_t *last
)
2646 { return assign(first
, last
- first
); }
2647 wxString
& assign(const wxCStrData
& first
, const wxCStrData
& last
)
2648 { return assign(CreateConstIterator(first
), CreateConstIterator(last
)); }
2649 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2651 // string comparison
2652 int compare(const wxString
& str
) const;
2653 int compare(const char* sz
) const;
2654 int compare(const wchar_t* sz
) const;
2655 int compare(const wxCStrData
& str
) const
2656 { return compare(str
.AsString()); }
2657 int compare(const wxScopedCharBuffer
& str
) const
2658 { return compare(str
.data()); }
2659 int compare(const wxScopedWCharBuffer
& str
) const
2660 { return compare(str
.data()); }
2661 // comparison with a substring
2662 int compare(size_t nStart
, size_t nLen
, const wxString
& str
) const;
2663 // comparison of 2 substrings
2664 int compare(size_t nStart
, size_t nLen
,
2665 const wxString
& str
, size_t nStart2
, size_t nLen2
) const;
2666 // substring comparison with first nCount characters of sz
2667 int compare(size_t nStart
, size_t nLen
,
2668 const char* sz
, size_t nCount
= npos
) const;
2669 int compare(size_t nStart
, size_t nLen
,
2670 const wchar_t* sz
, size_t nCount
= npos
) const;
2672 // insert another string
2673 wxString
& insert(size_t nPos
, const wxString
& str
)
2674 { insert(GetIterForNthChar(nPos
), str
.begin(), str
.end()); return *this; }
2675 // insert n chars of str starting at nStart (in str)
2676 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
2678 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2681 str
.PosLenToImpl(nStart
, n
, &from
, &len
);
2682 m_impl
.insert(PosToImpl(nPos
), str
.m_impl
, from
, len
);
2687 // insert first n (or all if n == npos) characters of sz
2688 wxString
& insert(size_t nPos
, const char *sz
)
2690 wxSTRING_INVALIDATE_CACHE();
2692 m_impl
.insert(PosToImpl(nPos
), ImplStr(sz
));
2697 wxString
& insert(size_t nPos
, const wchar_t *sz
)
2699 wxSTRING_INVALIDATE_CACHE();
2701 m_impl
.insert(PosToImpl(nPos
), ImplStr(sz
)); return *this;
2704 wxString
& insert(size_t nPos
, const char *sz
, size_t n
)
2706 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2708 SubstrBufFromMB
str(ImplStr(sz
, n
));
2709 m_impl
.insert(PosToImpl(nPos
), str
.data
, str
.len
);
2714 wxString
& insert(size_t nPos
, const wchar_t *sz
, size_t n
)
2716 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2718 SubstrBufFromWC
str(ImplStr(sz
, n
));
2719 m_impl
.insert(PosToImpl(nPos
), str
.data
, str
.len
);
2724 // insert n copies of ch
2725 wxString
& insert(size_t nPos
, size_t n
, wxUniChar ch
)
2727 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2729 #if wxUSE_UNICODE_UTF8
2730 if ( !ch
.IsAscii() )
2731 m_impl
.insert(PosToImpl(nPos
), wxStringOperations::EncodeNChars(n
, ch
));
2734 m_impl
.insert(PosToImpl(nPos
), n
, (wxStringCharType
)ch
);
2738 iterator
insert(iterator it
, wxUniChar ch
)
2740 wxSTRING_UPDATE_CACHED_LENGTH(1);
2742 #if wxUSE_UNICODE_UTF8
2743 if ( !ch
.IsAscii() )
2745 size_t pos
= IterToImplPos(it
);
2746 m_impl
.insert(pos
, wxStringOperations::EncodeChar(ch
));
2747 return iterator(this, m_impl
.begin() + pos
);
2751 return iterator(this, m_impl
.insert(it
.impl(), (wxStringCharType
)ch
));
2754 void insert(iterator it
, const_iterator first
, const_iterator last
)
2756 wxSTRING_INVALIDATE_CACHE();
2758 m_impl
.insert(it
.impl(), first
.impl(), last
.impl());
2761 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2762 void insert(iterator it
, const char *first
, const char *last
)
2763 { insert(it
- begin(), first
, last
- first
); }
2764 void insert(iterator it
, const wchar_t *first
, const wchar_t *last
)
2765 { insert(it
- begin(), first
, last
- first
); }
2766 void insert(iterator it
, const wxCStrData
& first
, const wxCStrData
& last
)
2767 { insert(it
, CreateConstIterator(first
), CreateConstIterator(last
)); }
2768 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2770 void insert(iterator it
, size_type n
, wxUniChar ch
)
2772 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2774 #if wxUSE_UNICODE_UTF8
2775 if ( !ch
.IsAscii() )
2776 m_impl
.insert(IterToImplPos(it
), wxStringOperations::EncodeNChars(n
, ch
));
2779 m_impl
.insert(it
.impl(), n
, (wxStringCharType
)ch
);
2782 // delete characters from nStart to nStart + nLen
2783 wxString
& erase(size_type pos
= 0, size_type n
= npos
)
2785 wxSTRING_INVALIDATE_CACHE();
2788 PosLenToImpl(pos
, n
, &from
, &len
);
2789 m_impl
.erase(from
, len
);
2794 // delete characters from first up to last
2795 iterator
erase(iterator first
, iterator last
)
2797 wxSTRING_INVALIDATE_CACHE();
2799 return iterator(this, m_impl
.erase(first
.impl(), last
.impl()));
2802 iterator
erase(iterator first
)
2804 wxSTRING_UPDATE_CACHED_LENGTH(-1);
2806 return iterator(this, m_impl
.erase(first
.impl()));
2809 #ifdef wxSTRING_BASE_HASNT_CLEAR
2810 void clear() { erase(); }
2814 wxSTRING_SET_CACHED_LENGTH(0);
2820 // replaces the substring of length nLen starting at nStart
2821 wxString
& replace(size_t nStart
, size_t nLen
, const char* sz
)
2823 wxSTRING_INVALIDATE_CACHE();
2826 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2827 m_impl
.replace(from
, len
, ImplStr(sz
));
2832 wxString
& replace(size_t nStart
, size_t nLen
, const wchar_t* sz
)
2834 wxSTRING_INVALIDATE_CACHE();
2837 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2838 m_impl
.replace(from
, len
, ImplStr(sz
));
2843 // replaces the substring of length nLen starting at nStart
2844 wxString
& replace(size_t nStart
, size_t nLen
, const wxString
& str
)
2846 wxSTRING_INVALIDATE_CACHE();
2849 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2850 m_impl
.replace(from
, len
, str
.m_impl
);
2855 // replaces the substring with nCount copies of ch
2856 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxUniChar ch
)
2858 wxSTRING_INVALIDATE_CACHE();
2861 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2862 #if wxUSE_UNICODE_UTF8
2863 if ( !ch
.IsAscii() )
2864 m_impl
.replace(from
, len
, wxStringOperations::EncodeNChars(nCount
, ch
));
2867 m_impl
.replace(from
, len
, nCount
, (wxStringCharType
)ch
);
2872 // replaces a substring with another substring
2873 wxString
& replace(size_t nStart
, size_t nLen
,
2874 const wxString
& str
, size_t nStart2
, size_t nLen2
)
2876 wxSTRING_INVALIDATE_CACHE();
2879 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2882 str
.PosLenToImpl(nStart2
, nLen2
, &from2
, &len2
);
2884 m_impl
.replace(from
, len
, str
.m_impl
, from2
, len2
);
2889 // replaces the substring with first nCount chars of sz
2890 wxString
& replace(size_t nStart
, size_t nLen
,
2891 const char* sz
, size_t nCount
)
2893 wxSTRING_INVALIDATE_CACHE();
2896 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2898 SubstrBufFromMB
str(ImplStr(sz
, nCount
));
2900 m_impl
.replace(from
, len
, str
.data
, str
.len
);
2905 wxString
& replace(size_t nStart
, size_t nLen
,
2906 const wchar_t* sz
, size_t nCount
)
2908 wxSTRING_INVALIDATE_CACHE();
2911 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2913 SubstrBufFromWC
str(ImplStr(sz
, nCount
));
2915 m_impl
.replace(from
, len
, str
.data
, str
.len
);
2920 wxString
& replace(size_t nStart
, size_t nLen
,
2921 const wxString
& s
, size_t nCount
)
2923 wxSTRING_INVALIDATE_CACHE();
2926 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2927 m_impl
.replace(from
, len
, s
.m_impl
.c_str(), s
.LenToImpl(nCount
));
2932 wxString
& replace(iterator first
, iterator last
, const char* s
)
2934 wxSTRING_INVALIDATE_CACHE();
2936 m_impl
.replace(first
.impl(), last
.impl(), ImplStr(s
));
2941 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
)
2943 wxSTRING_INVALIDATE_CACHE();
2945 m_impl
.replace(first
.impl(), last
.impl(), ImplStr(s
));
2950 wxString
& replace(iterator first
, iterator last
, const char* s
, size_type n
)
2952 wxSTRING_INVALIDATE_CACHE();
2954 SubstrBufFromMB
str(ImplStr(s
, n
));
2955 m_impl
.replace(first
.impl(), last
.impl(), str
.data
, str
.len
);
2960 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
, size_type n
)
2962 wxSTRING_INVALIDATE_CACHE();
2964 SubstrBufFromWC
str(ImplStr(s
, n
));
2965 m_impl
.replace(first
.impl(), last
.impl(), str
.data
, str
.len
);
2970 wxString
& replace(iterator first
, iterator last
, const wxString
& s
)
2972 wxSTRING_INVALIDATE_CACHE();
2974 m_impl
.replace(first
.impl(), last
.impl(), s
.m_impl
);
2979 wxString
& replace(iterator first
, iterator last
, size_type n
, wxUniChar ch
)
2981 wxSTRING_INVALIDATE_CACHE();
2983 #if wxUSE_UNICODE_UTF8
2984 if ( !ch
.IsAscii() )
2985 m_impl
.replace(first
.impl(), last
.impl(),
2986 wxStringOperations::EncodeNChars(n
, ch
));
2989 m_impl
.replace(first
.impl(), last
.impl(), n
, (wxStringCharType
)ch
);
2994 wxString
& replace(iterator first
, iterator last
,
2995 const_iterator first1
, const_iterator last1
)
2997 wxSTRING_INVALIDATE_CACHE();
2999 m_impl
.replace(first
.impl(), last
.impl(), first1
.impl(), last1
.impl());
3004 wxString
& replace(iterator first
, iterator last
,
3005 const char *first1
, const char *last1
)
3006 { replace(first
, last
, first1
, last1
- first1
); return *this; }
3007 wxString
& replace(iterator first
, iterator last
,
3008 const wchar_t *first1
, const wchar_t *last1
)
3009 { replace(first
, last
, first1
, last1
- first1
); return *this; }
3012 void swap(wxString
& str
)
3014 #if wxUSE_STRING_POS_CACHE
3015 // we modify not only this string but also the other one directly so we
3016 // need to invalidate cache for both of them (we could also try to
3017 // exchange their cache entries but it seems unlikely to be worth it)
3019 str
.InvalidateCache();
3020 #endif // wxUSE_STRING_POS_CACHE
3022 m_impl
.swap(str
.m_impl
);
3026 size_t find(const wxString
& str
, size_t nStart
= 0) const
3027 { return PosFromImpl(m_impl
.find(str
.m_impl
, PosToImpl(nStart
))); }
3029 // find first n characters of sz
3030 size_t find(const char* sz
, size_t nStart
= 0, size_t n
= npos
) const
3032 SubstrBufFromMB
str(ImplStr(sz
, n
));
3033 return PosFromImpl(m_impl
.find(str
.data
, PosToImpl(nStart
), str
.len
));
3035 size_t find(const wchar_t* sz
, size_t nStart
= 0, size_t n
= npos
) const
3037 SubstrBufFromWC
str(ImplStr(sz
, n
));
3038 return PosFromImpl(m_impl
.find(str
.data
, PosToImpl(nStart
), str
.len
));
3040 size_t find(const wxScopedCharBuffer
& s
, size_t nStart
= 0, size_t n
= npos
) const
3041 { return find(s
.data(), nStart
, n
); }
3042 size_t find(const wxScopedWCharBuffer
& s
, size_t nStart
= 0, size_t n
= npos
) const
3043 { return find(s
.data(), nStart
, n
); }
3044 size_t find(const wxCStrData
& s
, size_t nStart
= 0, size_t n
= npos
) const
3045 { return find(s
.AsWChar(), nStart
, n
); }
3047 // find the first occurrence of character ch after nStart
3048 size_t find(wxUniChar ch
, size_t nStart
= 0) const
3050 #if wxUSE_UNICODE_UTF8
3051 if ( !ch
.IsAscii() )
3052 return PosFromImpl(m_impl
.find(wxStringOperations::EncodeChar(ch
),
3053 PosToImpl(nStart
)));
3056 return PosFromImpl(m_impl
.find((wxStringCharType
)ch
,
3057 PosToImpl(nStart
)));
3060 size_t find(wxUniCharRef ch
, size_t nStart
= 0) const
3061 { return find(wxUniChar(ch
), nStart
); }
3062 size_t find(char ch
, size_t nStart
= 0) const
3063 { return find(wxUniChar(ch
), nStart
); }
3064 size_t find(unsigned char ch
, size_t nStart
= 0) const
3065 { return find(wxUniChar(ch
), nStart
); }
3066 size_t find(wchar_t ch
, size_t nStart
= 0) const
3067 { return find(wxUniChar(ch
), nStart
); }
3069 // rfind() family is exactly like find() but works right to left
3071 // as find, but from the end
3072 size_t rfind(const wxString
& str
, size_t nStart
= npos
) const
3073 { return PosFromImpl(m_impl
.rfind(str
.m_impl
, PosToImpl(nStart
))); }
3075 // as find, but from the end
3076 size_t rfind(const char* sz
, size_t nStart
= npos
, size_t n
= npos
) const
3078 SubstrBufFromMB
str(ImplStr(sz
, n
));
3079 return PosFromImpl(m_impl
.rfind(str
.data
, PosToImpl(nStart
), str
.len
));
3081 size_t rfind(const wchar_t* sz
, size_t nStart
= npos
, size_t n
= npos
) const
3083 SubstrBufFromWC
str(ImplStr(sz
, n
));
3084 return PosFromImpl(m_impl
.rfind(str
.data
, PosToImpl(nStart
), str
.len
));
3086 size_t rfind(const wxScopedCharBuffer
& s
, size_t nStart
= npos
, size_t n
= npos
) const
3087 { return rfind(s
.data(), nStart
, n
); }
3088 size_t rfind(const wxScopedWCharBuffer
& s
, size_t nStart
= npos
, size_t n
= npos
) const
3089 { return rfind(s
.data(), nStart
, n
); }
3090 size_t rfind(const wxCStrData
& s
, size_t nStart
= npos
, size_t n
= npos
) const
3091 { return rfind(s
.AsWChar(), nStart
, n
); }
3092 // as find, but from the end
3093 size_t rfind(wxUniChar ch
, size_t nStart
= npos
) const
3095 #if wxUSE_UNICODE_UTF8
3096 if ( !ch
.IsAscii() )
3097 return PosFromImpl(m_impl
.rfind(wxStringOperations::EncodeChar(ch
),
3098 PosToImpl(nStart
)));
3101 return PosFromImpl(m_impl
.rfind((wxStringCharType
)ch
,
3102 PosToImpl(nStart
)));
3104 size_t rfind(wxUniCharRef ch
, size_t nStart
= npos
) const
3105 { return rfind(wxUniChar(ch
), nStart
); }
3106 size_t rfind(char ch
, size_t nStart
= npos
) const
3107 { return rfind(wxUniChar(ch
), nStart
); }
3108 size_t rfind(unsigned char ch
, size_t nStart
= npos
) const
3109 { return rfind(wxUniChar(ch
), nStart
); }
3110 size_t rfind(wchar_t ch
, size_t nStart
= npos
) const
3111 { return rfind(wxUniChar(ch
), nStart
); }
3113 // find first/last occurrence of any character (not) in the set:
3114 #if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
3115 // FIXME-UTF8: this is not entirely correct, because it doesn't work if
3116 // sizeof(wchar_t)==2 and surrogates are present in the string;
3117 // should we care? Probably not.
3118 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
3119 { return m_impl
.find_first_of(str
.m_impl
, nStart
); }
3120 size_t find_first_of(const char* sz
, size_t nStart
= 0) const
3121 { return m_impl
.find_first_of(ImplStr(sz
), nStart
); }
3122 size_t find_first_of(const wchar_t* sz
, size_t nStart
= 0) const
3123 { return m_impl
.find_first_of(ImplStr(sz
), nStart
); }
3124 size_t find_first_of(const char* sz
, size_t nStart
, size_t n
) const
3125 { return m_impl
.find_first_of(ImplStr(sz
), nStart
, n
); }
3126 size_t find_first_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
3127 { return m_impl
.find_first_of(ImplStr(sz
), nStart
, n
); }
3128 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const
3129 { return m_impl
.find_first_of((wxChar
)c
, nStart
); }
3131 size_t find_last_of(const wxString
& str
, size_t nStart
= npos
) const
3132 { return m_impl
.find_last_of(str
.m_impl
, nStart
); }
3133 size_t find_last_of(const char* sz
, size_t nStart
= npos
) const
3134 { return m_impl
.find_last_of(ImplStr(sz
), nStart
); }
3135 size_t find_last_of(const wchar_t* sz
, size_t nStart
= npos
) const
3136 { return m_impl
.find_last_of(ImplStr(sz
), nStart
); }
3137 size_t find_last_of(const char* sz
, size_t nStart
, size_t n
) const
3138 { return m_impl
.find_last_of(ImplStr(sz
), nStart
, n
); }
3139 size_t find_last_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
3140 { return m_impl
.find_last_of(ImplStr(sz
), nStart
, n
); }
3141 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const
3142 { return m_impl
.find_last_of((wxChar
)c
, nStart
); }
3144 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
3145 { return m_impl
.find_first_not_of(str
.m_impl
, nStart
); }
3146 size_t find_first_not_of(const char* sz
, size_t nStart
= 0) const
3147 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
); }
3148 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
= 0) const
3149 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
); }
3150 size_t find_first_not_of(const char* sz
, size_t nStart
, size_t n
) const
3151 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
, n
); }
3152 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
3153 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
, n
); }
3154 size_t find_first_not_of(wxUniChar c
, size_t nStart
= 0) const
3155 { return m_impl
.find_first_not_of((wxChar
)c
, nStart
); }
3157 size_t find_last_not_of(const wxString
& str
, size_t nStart
= npos
) const
3158 { return m_impl
.find_last_not_of(str
.m_impl
, nStart
); }
3159 size_t find_last_not_of(const char* sz
, size_t nStart
= npos
) const
3160 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
); }
3161 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
= npos
) const
3162 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
); }
3163 size_t find_last_not_of(const char* sz
, size_t nStart
, size_t n
) const
3164 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
, n
); }
3165 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
3166 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
, n
); }
3167 size_t find_last_not_of(wxUniChar c
, size_t nStart
= npos
) const
3168 { return m_impl
.find_last_not_of((wxChar
)c
, nStart
); }
3170 // we can't use std::string implementation in UTF-8 build, because the
3171 // character sets would be interpreted wrongly:
3173 // as strpbrk() but starts at nStart, returns npos if not found
3174 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
3175 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3176 { return find_first_of(str
.wc_str(), nStart
); }
3178 { return find_first_of(str
.mb_str(), nStart
); }
3181 size_t find_first_of(const char* sz
, size_t nStart
= 0) const;
3182 size_t find_first_of(const wchar_t* sz
, size_t nStart
= 0) const;
3183 size_t find_first_of(const char* sz
, size_t nStart
, size_t n
) const;
3184 size_t find_first_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
3185 // same as find(char, size_t)
3186 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const
3187 { return find(c
, nStart
); }
3188 // find the last (starting from nStart) char from str in this string
3189 size_t find_last_of (const wxString
& str
, size_t nStart
= npos
) const
3190 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3191 { return find_last_of(str
.wc_str(), nStart
); }
3193 { return find_last_of(str
.mb_str(), nStart
); }
3196 size_t find_last_of (const char* sz
, size_t nStart
= npos
) const;
3197 size_t find_last_of (const wchar_t* sz
, size_t nStart
= npos
) const;
3198 size_t find_last_of(const char* sz
, size_t nStart
, size_t n
) const;
3199 size_t find_last_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
3201 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const
3202 { return rfind(c
, nStart
); }
3204 // find first/last occurrence of any character not in the set
3206 // as strspn() (starting from nStart), returns npos on failure
3207 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
3208 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3209 { return find_first_not_of(str
.wc_str(), nStart
); }
3211 { return find_first_not_of(str
.mb_str(), nStart
); }
3214 size_t find_first_not_of(const char* sz
, size_t nStart
= 0) const;
3215 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
= 0) const;
3216 size_t find_first_not_of(const char* sz
, size_t nStart
, size_t n
) const;
3217 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
3219 size_t find_first_not_of(wxUniChar ch
, size_t nStart
= 0) const;
3221 size_t find_last_not_of(const wxString
& str
, size_t nStart
= npos
) const
3222 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3223 { return find_last_not_of(str
.wc_str(), nStart
); }
3225 { return find_last_not_of(str
.mb_str(), nStart
); }
3228 size_t find_last_not_of(const char* sz
, size_t nStart
= npos
) const;
3229 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
= npos
) const;
3230 size_t find_last_not_of(const char* sz
, size_t nStart
, size_t n
) const;
3231 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
3233 size_t find_last_not_of(wxUniChar ch
, size_t nStart
= npos
) const;
3234 #endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not
3236 // provide char/wchar_t/wxUniCharRef overloads for char-finding functions
3237 // above to resolve ambiguities:
3238 size_t find_first_of(wxUniCharRef ch
, size_t nStart
= 0) const
3239 { return find_first_of(wxUniChar(ch
), nStart
); }
3240 size_t find_first_of(char ch
, size_t nStart
= 0) const
3241 { return find_first_of(wxUniChar(ch
), nStart
); }
3242 size_t find_first_of(unsigned char ch
, size_t nStart
= 0) const
3243 { return find_first_of(wxUniChar(ch
), nStart
); }
3244 size_t find_first_of(wchar_t ch
, size_t nStart
= 0) const
3245 { return find_first_of(wxUniChar(ch
), nStart
); }
3246 size_t find_last_of(wxUniCharRef ch
, size_t nStart
= npos
) const
3247 { return find_last_of(wxUniChar(ch
), nStart
); }
3248 size_t find_last_of(char ch
, size_t nStart
= npos
) const
3249 { return find_last_of(wxUniChar(ch
), nStart
); }
3250 size_t find_last_of(unsigned char ch
, size_t nStart
= npos
) const
3251 { return find_last_of(wxUniChar(ch
), nStart
); }
3252 size_t find_last_of(wchar_t ch
, size_t nStart
= npos
) const
3253 { return find_last_of(wxUniChar(ch
), nStart
); }
3254 size_t find_first_not_of(wxUniCharRef ch
, size_t nStart
= 0) const
3255 { return find_first_not_of(wxUniChar(ch
), nStart
); }
3256 size_t find_first_not_of(char ch
, size_t nStart
= 0) const
3257 { return find_first_not_of(wxUniChar(ch
), nStart
); }
3258 size_t find_first_not_of(unsigned char ch
, size_t nStart
= 0) const
3259 { return find_first_not_of(wxUniChar(ch
), nStart
); }
3260 size_t find_first_not_of(wchar_t ch
, size_t nStart
= 0) const
3261 { return find_first_not_of(wxUniChar(ch
), nStart
); }
3262 size_t find_last_not_of(wxUniCharRef ch
, size_t nStart
= npos
) const
3263 { return find_last_not_of(wxUniChar(ch
), nStart
); }
3264 size_t find_last_not_of(char ch
, size_t nStart
= npos
) const
3265 { return find_last_not_of(wxUniChar(ch
), nStart
); }
3266 size_t find_last_not_of(unsigned char ch
, size_t nStart
= npos
) const
3267 { return find_last_not_of(wxUniChar(ch
), nStart
); }
3268 size_t find_last_not_of(wchar_t ch
, size_t nStart
= npos
) const
3269 { return find_last_not_of(wxUniChar(ch
), nStart
); }
3271 // and additional overloads for the versions taking strings:
3272 size_t find_first_of(const wxCStrData
& sz
, size_t nStart
= 0) const
3273 { return find_first_of(sz
.AsString(), nStart
); }
3274 size_t find_first_of(const wxScopedCharBuffer
& sz
, size_t nStart
= 0) const
3275 { return find_first_of(sz
.data(), nStart
); }
3276 size_t find_first_of(const wxScopedWCharBuffer
& sz
, size_t nStart
= 0) const
3277 { return find_first_of(sz
.data(), nStart
); }
3278 size_t find_first_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
3279 { return find_first_of(sz
.AsWChar(), nStart
, n
); }
3280 size_t find_first_of(const wxScopedCharBuffer
& sz
, size_t nStart
, size_t n
) const
3281 { return find_first_of(sz
.data(), nStart
, n
); }
3282 size_t find_first_of(const wxScopedWCharBuffer
& sz
, size_t nStart
, size_t n
) const
3283 { return find_first_of(sz
.data(), nStart
, n
); }
3285 size_t find_last_of(const wxCStrData
& sz
, size_t nStart
= 0) const
3286 { return find_last_of(sz
.AsString(), nStart
); }
3287 size_t find_last_of(const wxScopedCharBuffer
& sz
, size_t nStart
= 0) const
3288 { return find_last_of(sz
.data(), nStart
); }
3289 size_t find_last_of(const wxScopedWCharBuffer
& sz
, size_t nStart
= 0) const
3290 { return find_last_of(sz
.data(), nStart
); }
3291 size_t find_last_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
3292 { return find_last_of(sz
.AsWChar(), nStart
, n
); }
3293 size_t find_last_of(const wxScopedCharBuffer
& sz
, size_t nStart
, size_t n
) const
3294 { return find_last_of(sz
.data(), nStart
, n
); }
3295 size_t find_last_of(const wxScopedWCharBuffer
& sz
, size_t nStart
, size_t n
) const
3296 { return find_last_of(sz
.data(), nStart
, n
); }
3298 size_t find_first_not_of(const wxCStrData
& sz
, size_t nStart
= 0) const
3299 { return find_first_not_of(sz
.AsString(), nStart
); }
3300 size_t find_first_not_of(const wxScopedCharBuffer
& sz
, size_t nStart
= 0) const
3301 { return find_first_not_of(sz
.data(), nStart
); }
3302 size_t find_first_not_of(const wxScopedWCharBuffer
& sz
, size_t nStart
= 0) const
3303 { return find_first_not_of(sz
.data(), nStart
); }
3304 size_t find_first_not_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
3305 { return find_first_not_of(sz
.AsWChar(), nStart
, n
); }
3306 size_t find_first_not_of(const wxScopedCharBuffer
& sz
, size_t nStart
, size_t n
) const
3307 { return find_first_not_of(sz
.data(), nStart
, n
); }
3308 size_t find_first_not_of(const wxScopedWCharBuffer
& sz
, size_t nStart
, size_t n
) const
3309 { return find_first_not_of(sz
.data(), nStart
, n
); }
3311 size_t find_last_not_of(const wxCStrData
& sz
, size_t nStart
= 0) const
3312 { return find_last_not_of(sz
.AsString(), nStart
); }
3313 size_t find_last_not_of(const wxScopedCharBuffer
& sz
, size_t nStart
= 0) const
3314 { return find_last_not_of(sz
.data(), nStart
); }
3315 size_t find_last_not_of(const wxScopedWCharBuffer
& sz
, size_t nStart
= 0) const
3316 { return find_last_not_of(sz
.data(), nStart
); }
3317 size_t find_last_not_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
3318 { return find_last_not_of(sz
.AsWChar(), nStart
, n
); }
3319 size_t find_last_not_of(const wxScopedCharBuffer
& sz
, size_t nStart
, size_t n
) const
3320 { return find_last_not_of(sz
.data(), nStart
, n
); }
3321 size_t find_last_not_of(const wxScopedWCharBuffer
& sz
, size_t nStart
, size_t n
) const
3322 { return find_last_not_of(sz
.data(), nStart
, n
); }
3325 wxString
& operator+=(const wxString
& s
)
3327 wxSTRING_INVALIDATE_CACHED_LENGTH();
3332 // string += C string
3333 wxString
& operator+=(const char *psz
)
3335 wxSTRING_INVALIDATE_CACHED_LENGTH();
3337 m_impl
+= ImplStr(psz
);
3340 wxString
& operator+=(const wchar_t *pwz
)
3342 wxSTRING_INVALIDATE_CACHED_LENGTH();
3344 m_impl
+= ImplStr(pwz
);
3347 wxString
& operator+=(const wxCStrData
& s
)
3349 wxSTRING_INVALIDATE_CACHED_LENGTH();
3351 m_impl
+= s
.AsString().m_impl
;
3354 wxString
& operator+=(const wxScopedCharBuffer
& s
)
3355 { return operator+=(s
.data()); }
3356 wxString
& operator+=(const wxScopedWCharBuffer
& s
)
3357 { return operator+=(s
.data()); }
3359 wxString
& operator+=(wxUniChar ch
)
3361 wxSTRING_UPDATE_CACHED_LENGTH(1);
3363 #if wxUSE_UNICODE_UTF8
3364 if ( !ch
.IsAscii() )
3365 m_impl
+= wxStringOperations::EncodeChar(ch
);
3368 m_impl
+= (wxStringCharType
)ch
;
3371 wxString
& operator+=(wxUniCharRef ch
) { return *this += wxUniChar(ch
); }
3372 wxString
& operator+=(int ch
) { return *this += wxUniChar(ch
); }
3373 wxString
& operator+=(char ch
) { return *this += wxUniChar(ch
); }
3374 wxString
& operator+=(unsigned char ch
) { return *this += wxUniChar(ch
); }
3375 wxString
& operator+=(wchar_t ch
) { return *this += wxUniChar(ch
); }
3378 #if !wxUSE_STL_BASED_WXSTRING
3379 // helpers for wxStringBuffer and wxStringBufferLength
3380 wxStringCharType
*DoGetWriteBuf(size_t nLen
)
3382 return m_impl
.DoGetWriteBuf(nLen
);
3385 void DoUngetWriteBuf()
3387 wxSTRING_INVALIDATE_CACHE();
3389 m_impl
.DoUngetWriteBuf();
3392 void DoUngetWriteBuf(size_t nLen
)
3394 wxSTRING_SET_CACHED_LENGTH(nLen
);
3396 m_impl
.DoUngetWriteBuf(nLen
);
3398 #endif // !wxUSE_STL_BASED_WXSTRING
3400 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
3401 #if !wxUSE_UTF8_LOCALE_ONLY
3402 int DoPrintfWchar(const wxChar
*format
, ...);
3403 static wxString
DoFormatWchar(const wxChar
*format
, ...);
3405 #if wxUSE_UNICODE_UTF8
3406 int DoPrintfUtf8(const char *format
, ...);
3407 static wxString
DoFormatUtf8(const char *format
, ...);
3411 #if !wxUSE_STL_BASED_WXSTRING
3412 // check string's data validity
3413 bool IsValid() const { return m_impl
.GetStringData()->IsValid(); }
3417 wxStringImpl m_impl
;
3419 // buffers for compatibility conversion from (char*)c_str() and
3420 // (wchar_t*)c_str():
3421 // FIXME-UTF8: bechmark various approaches to keeping compatibility buffers
3422 template<typename T
>
3423 struct ConvertedBuffer
3425 ConvertedBuffer() : m_buf(NULL
) {}
3429 operator T
*() const { return m_buf
; }
3431 ConvertedBuffer
& operator=(T
*str
)
3440 #if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
3441 ConvertedBuffer
<char> m_convertedToChar
;
3443 #if !wxUSE_UNICODE_WCHAR
3444 ConvertedBuffer
<wchar_t> m_convertedToWChar
;
3447 #if wxUSE_UNICODE_UTF8
3448 // FIXME-UTF8: (try to) move this elsewhere (TLS) or solve differently
3449 // assigning to character pointer to by wxString::interator may
3450 // change the underlying wxStringImpl iterator, so we have to
3451 // keep track of all iterators and update them as necessary:
3452 struct wxStringIteratorNodeHead
3454 wxStringIteratorNodeHead() : ptr(NULL
) {}
3455 wxStringIteratorNode
*ptr
;
3457 // copying is disallowed as it would result in more than one pointer into
3458 // the same linked list
3459 wxDECLARE_NO_COPY_CLASS(wxStringIteratorNodeHead
);
3462 wxStringIteratorNodeHead m_iterators
;
3464 friend class WXDLLIMPEXP_FWD_BASE wxStringIteratorNode
;
3465 friend class WXDLLIMPEXP_FWD_BASE wxUniCharRef
;
3466 #endif // wxUSE_UNICODE_UTF8
3468 friend class WXDLLIMPEXP_FWD_BASE wxCStrData
;
3469 friend class wxStringInternalBuffer
;
3470 friend class wxStringInternalBufferLength
;
3473 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
3474 #pragma warning (default:4275)
3477 // string iterator operators that satisfy STL Random Access Iterator
3479 inline wxString::iterator
operator+(ptrdiff_t n
, wxString::iterator i
)
3481 inline wxString::const_iterator
operator+(ptrdiff_t n
, wxString::const_iterator i
)
3483 inline wxString::reverse_iterator
operator+(ptrdiff_t n
, wxString::reverse_iterator i
)
3485 inline wxString::const_reverse_iterator
operator+(ptrdiff_t n
, wxString::const_reverse_iterator i
)
3488 // notice that even though for many compilers the friend declarations above are
3489 // enough, from the point of view of C++ standard we must have the declarations
3490 // here as friend ones are not injected in the enclosing namespace and without
3491 // them the code fails to compile with conforming compilers such as xlC or g++4
3492 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
, const wxString
& string2
);
3493 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const char *psz
);
3494 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const wchar_t *pwz
);
3495 wxString WXDLLIMPEXP_BASE
operator+(const char *psz
, const wxString
& string
);
3496 wxString WXDLLIMPEXP_BASE
operator+(const wchar_t *pwz
, const wxString
& string
);
3498 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
3499 wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
3501 inline wxString
operator+(const wxString
& string
, wxUniCharRef ch
)
3502 { return string
+ (wxUniChar
)ch
; }
3503 inline wxString
operator+(const wxString
& string
, char ch
)
3504 { return string
+ wxUniChar(ch
); }
3505 inline wxString
operator+(const wxString
& string
, wchar_t ch
)
3506 { return string
+ wxUniChar(ch
); }
3507 inline wxString
operator+(wxUniCharRef ch
, const wxString
& string
)
3508 { return (wxUniChar
)ch
+ string
; }
3509 inline wxString
operator+(char ch
, const wxString
& string
)
3510 { return wxUniChar(ch
) + string
; }
3511 inline wxString
operator+(wchar_t ch
, const wxString
& string
)
3512 { return wxUniChar(ch
) + string
; }
3515 #define wxGetEmptyString() wxString()
3517 // ----------------------------------------------------------------------------
3518 // helper functions which couldn't be defined inline
3519 // ----------------------------------------------------------------------------
3524 #if wxUSE_UNICODE_WCHAR
3527 struct wxStringAsBufHelper
<char>
3529 static wxScopedCharBuffer
Get(const wxString
& s
, size_t *len
)
3531 wxScopedCharBuffer
buf(s
.mb_str());
3533 *len
= buf
? strlen(buf
) : 0;
3539 struct wxStringAsBufHelper
<wchar_t>
3541 static wxScopedWCharBuffer
Get(const wxString
& s
, size_t *len
)
3545 return wxScopedWCharBuffer::CreateNonOwned(s
.wx_str());
3549 #elif wxUSE_UNICODE_UTF8
3552 struct wxStringAsBufHelper
<char>
3554 static wxScopedCharBuffer
Get(const wxString
& s
, size_t *len
)
3557 *len
= s
.utf8_length();
3558 return wxScopedCharBuffer::CreateNonOwned(s
.wx_str());
3563 struct wxStringAsBufHelper
<wchar_t>
3565 static wxScopedWCharBuffer
Get(const wxString
& s
, size_t *len
)
3567 wxScopedWCharBuffer
wbuf(s
.wc_str());
3569 *len
= wxWcslen(wbuf
);
3574 #endif // Unicode build kind
3576 } // namespace wxPrivate
3578 // ----------------------------------------------------------------------------
3579 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
3580 // ----------------------------------------------------------------------------
3582 #if !wxUSE_STL_BASED_WXSTRING
3583 // string buffer for direct access to string data in their native
3585 class wxStringInternalBuffer
3588 typedef wxStringCharType CharType
;
3590 wxStringInternalBuffer(wxString
& str
, size_t lenWanted
= 1024)
3591 : m_str(str
), m_buf(NULL
)
3592 { m_buf
= m_str
.DoGetWriteBuf(lenWanted
); }
3594 ~wxStringInternalBuffer() { m_str
.DoUngetWriteBuf(); }
3596 operator wxStringCharType
*() const { return m_buf
; }
3600 wxStringCharType
*m_buf
;
3602 wxDECLARE_NO_COPY_CLASS(wxStringInternalBuffer
);
3605 class wxStringInternalBufferLength
3608 typedef wxStringCharType CharType
;
3610 wxStringInternalBufferLength(wxString
& str
, size_t lenWanted
= 1024)
3611 : m_str(str
), m_buf(NULL
), m_len(0), m_lenSet(false)
3613 m_buf
= m_str
.DoGetWriteBuf(lenWanted
);
3614 wxASSERT(m_buf
!= NULL
);
3617 ~wxStringInternalBufferLength()
3620 m_str
.DoUngetWriteBuf(m_len
);
3623 operator wxStringCharType
*() const { return m_buf
; }
3624 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
3628 wxStringCharType
*m_buf
;
3632 wxDECLARE_NO_COPY_CLASS(wxStringInternalBufferLength
);
3635 #endif // !wxUSE_STL_BASED_WXSTRING
3637 template<typename T
>
3638 class wxStringTypeBufferBase
3643 wxStringTypeBufferBase(wxString
& str
, size_t lenWanted
= 1024)
3644 : m_str(str
), m_buf(lenWanted
)
3646 // for compatibility with old wxStringBuffer which provided direct
3647 // access to wxString internal buffer, initialize ourselves with the
3648 // string initial contents
3650 // FIXME-VC6: remove the ugly (CharType *)NULL and use normal
3651 // tchar_str<CharType>
3653 const wxCharTypeBuffer
<CharType
> buf(str
.tchar_str(&len
, (CharType
*)NULL
));
3656 if ( len
> lenWanted
)
3658 // in this case there is not enough space for terminating NUL,
3659 // ensure that we still put it there
3660 m_buf
.data()[lenWanted
] = 0;
3661 len
= lenWanted
- 1;
3664 memcpy(m_buf
.data(), buf
, (len
+ 1)*sizeof(CharType
));
3666 //else: conversion failed, this can happen when trying to get Unicode
3667 // string contents into a char string
3670 operator CharType
*() { return m_buf
.data(); }
3674 wxCharTypeBuffer
<CharType
> m_buf
;
3677 template<typename T
>
3678 class wxStringTypeBufferLengthBase
: public wxStringTypeBufferBase
<T
>
3681 wxStringTypeBufferLengthBase(wxString
& str
, size_t lenWanted
= 1024)
3682 : wxStringTypeBufferBase
<T
>(str
, lenWanted
),
3687 ~wxStringTypeBufferLengthBase()
3689 wxASSERT_MSG( this->m_lenSet
, "forgot to call SetLength()" );
3692 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
3699 template<typename T
>
3700 class wxStringTypeBuffer
: public wxStringTypeBufferBase
<T
>
3703 wxStringTypeBuffer(wxString
& str
, size_t lenWanted
= 1024)
3704 : wxStringTypeBufferBase
<T
>(str
, lenWanted
)
3707 ~wxStringTypeBuffer()
3709 this->m_str
.assign(this->m_buf
.data());
3712 wxDECLARE_NO_COPY_CLASS(wxStringTypeBuffer
);
3715 template<typename T
>
3716 class wxStringTypeBufferLength
: public wxStringTypeBufferLengthBase
<T
>
3719 wxStringTypeBufferLength(wxString
& str
, size_t lenWanted
= 1024)
3720 : wxStringTypeBufferLengthBase
<T
>(str
, lenWanted
)
3723 ~wxStringTypeBufferLength()
3725 this->m_str
.assign(this->m_buf
.data(), this->m_len
);
3728 wxDECLARE_NO_COPY_CLASS(wxStringTypeBufferLength
);
3731 #if wxUSE_STL_BASED_WXSTRING
3733 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase
<wxStringCharType
> )
3735 class wxStringInternalBuffer
: public wxStringTypeBufferBase
<wxStringCharType
>
3738 wxStringInternalBuffer(wxString
& str
, size_t lenWanted
= 1024)
3739 : wxStringTypeBufferBase
<wxStringCharType
>(str
, lenWanted
) {}
3740 ~wxStringInternalBuffer()
3741 { m_str
.m_impl
.assign(m_buf
.data()); }
3743 wxDECLARE_NO_COPY_CLASS(wxStringInternalBuffer
);
3746 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE(
3747 wxStringTypeBufferLengthBase
<wxStringCharType
> )
3749 class wxStringInternalBufferLength
3750 : public wxStringTypeBufferLengthBase
<wxStringCharType
>
3753 wxStringInternalBufferLength(wxString
& str
, size_t lenWanted
= 1024)
3754 : wxStringTypeBufferLengthBase
<wxStringCharType
>(str
, lenWanted
) {}
3756 ~wxStringInternalBufferLength()
3758 m_str
.m_impl
.assign(m_buf
.data(), m_len
);
3761 wxDECLARE_NO_COPY_CLASS(wxStringInternalBufferLength
);
3764 #endif // wxUSE_STL_BASED_WXSTRING
3767 #if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
3768 typedef wxStringTypeBuffer
<wxChar
> wxStringBuffer
;
3769 typedef wxStringTypeBufferLength
<wxChar
> wxStringBufferLength
;
3770 #else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
3771 typedef wxStringInternalBuffer wxStringBuffer
;
3772 typedef wxStringInternalBufferLength wxStringBufferLength
;
3773 #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
3775 #if wxUSE_UNICODE_UTF8
3776 typedef wxStringInternalBuffer wxUTF8StringBuffer
;
3777 typedef wxStringInternalBufferLength wxUTF8StringBufferLength
;
3778 #elif wxUSE_UNICODE_WCHAR
3780 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase
<char> )
3782 // Note about inlined dtors in the classes below: this is done not for
3783 // performance reasons but just to avoid linking errors in the MSVC DLL build
3784 // under Windows: if a class has non-inline methods it must be declared as
3785 // being DLL-exported but, due to an extremely interesting feature of MSVC 7
3786 // and later, any template class which is used as a base of a DLL-exported
3787 // class is implicitly made DLL-exported too, as explained at the bottom of
3788 // http://msdn.microsoft.com/en-us/library/twa2aw10.aspx (just to confirm: yes,
3789 // _inheriting_ from a class can change whether it is being exported from DLL)
3791 // But this results in link errors because the base template class is not DLL-
3792 // exported, whether it is declared with WXDLLIMPEXP_BASE or not, because it
3793 // does have only inline functions. So the simplest fix is to just make all the
3794 // functions of these classes inline too.
3796 class wxUTF8StringBuffer
: public wxStringTypeBufferBase
<char>
3799 wxUTF8StringBuffer(wxString
& str
, size_t lenWanted
= 1024)
3800 : wxStringTypeBufferBase
<char>(str
, lenWanted
) {}
3801 ~wxUTF8StringBuffer()
3803 wxMBConvStrictUTF8 conv
;
3804 size_t wlen
= conv
.ToWChar(NULL
, 0, m_buf
);
3805 wxCHECK_RET( wlen
!= wxCONV_FAILED
, "invalid UTF-8 data in string buffer?" );
3807 wxStringInternalBuffer
wbuf(m_str
, wlen
);
3808 conv
.ToWChar(wbuf
, wlen
, m_buf
);
3811 wxDECLARE_NO_COPY_CLASS(wxUTF8StringBuffer
);
3814 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferLengthBase
<char> )
3816 class wxUTF8StringBufferLength
: public wxStringTypeBufferLengthBase
<char>
3819 wxUTF8StringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
3820 : wxStringTypeBufferLengthBase
<char>(str
, lenWanted
) {}
3821 ~wxUTF8StringBufferLength()
3823 wxCHECK_RET(m_lenSet
, "length not set");
3825 wxMBConvStrictUTF8 conv
;
3826 size_t wlen
= conv
.ToWChar(NULL
, 0, m_buf
, m_len
);
3827 wxCHECK_RET( wlen
!= wxCONV_FAILED
, "invalid UTF-8 data in string buffer?" );
3829 wxStringInternalBufferLength
wbuf(m_str
, wlen
);
3830 conv
.ToWChar(wbuf
, wlen
, m_buf
, m_len
);
3831 wbuf
.SetLength(wlen
);
3834 wxDECLARE_NO_COPY_CLASS(wxUTF8StringBufferLength
);
3836 #endif // wxUSE_UNICODE_UTF8/wxUSE_UNICODE_WCHAR
3839 // ---------------------------------------------------------------------------
3840 // wxString comparison functions: operator versions are always case sensitive
3841 // ---------------------------------------------------------------------------
3843 #define wxCMP_WXCHAR_STRING(p, s, op) 0 op s.Cmp(p)
3845 wxDEFINE_ALL_COMPARISONS(const wxChar
*, const wxString
&, wxCMP_WXCHAR_STRING
)
3847 #undef wxCMP_WXCHAR_STRING
3849 inline bool operator==(const wxString
& s1
, const wxString
& s2
)
3850 { return s1
.IsSameAs(s2
); }
3851 inline bool operator!=(const wxString
& s1
, const wxString
& s2
)
3852 { return !s1
.IsSameAs(s2
); }
3853 inline bool operator< (const wxString
& s1
, const wxString
& s2
)
3854 { return s1
.Cmp(s2
) < 0; }
3855 inline bool operator> (const wxString
& s1
, const wxString
& s2
)
3856 { return s1
.Cmp(s2
) > 0; }
3857 inline bool operator<=(const wxString
& s1
, const wxString
& s2
)
3858 { return s1
.Cmp(s2
) <= 0; }
3859 inline bool operator>=(const wxString
& s1
, const wxString
& s2
)
3860 { return s1
.Cmp(s2
) >= 0; }
3862 inline bool operator==(const wxString
& s1
, const wxCStrData
& s2
)
3863 { return s1
== s2
.AsString(); }
3864 inline bool operator==(const wxCStrData
& s1
, const wxString
& s2
)
3865 { return s1
.AsString() == s2
; }
3866 inline bool operator!=(const wxString
& s1
, const wxCStrData
& s2
)
3867 { return s1
!= s2
.AsString(); }
3868 inline bool operator!=(const wxCStrData
& s1
, const wxString
& s2
)
3869 { return s1
.AsString() != s2
; }
3871 inline bool operator==(const wxString
& s1
, const wxScopedWCharBuffer
& s2
)
3872 { return (s1
.Cmp((const wchar_t *)s2
) == 0); }
3873 inline bool operator==(const wxScopedWCharBuffer
& s1
, const wxString
& s2
)
3874 { return (s2
.Cmp((const wchar_t *)s1
) == 0); }
3875 inline bool operator!=(const wxString
& s1
, const wxScopedWCharBuffer
& s2
)
3876 { return (s1
.Cmp((const wchar_t *)s2
) != 0); }
3877 inline bool operator!=(const wxScopedWCharBuffer
& s1
, const wxString
& s2
)
3878 { return (s2
.Cmp((const wchar_t *)s1
) != 0); }
3880 inline bool operator==(const wxString
& s1
, const wxScopedCharBuffer
& s2
)
3881 { return (s1
.Cmp((const char *)s2
) == 0); }
3882 inline bool operator==(const wxScopedCharBuffer
& s1
, const wxString
& s2
)
3883 { return (s2
.Cmp((const char *)s1
) == 0); }
3884 inline bool operator!=(const wxString
& s1
, const wxScopedCharBuffer
& s2
)
3885 { return (s1
.Cmp((const char *)s2
) != 0); }
3886 inline bool operator!=(const wxScopedCharBuffer
& s1
, const wxString
& s2
)
3887 { return (s2
.Cmp((const char *)s1
) != 0); }
3889 inline wxString
operator+(const wxString
& string
, const wxScopedWCharBuffer
& buf
)
3890 { return string
+ (const wchar_t *)buf
; }
3891 inline wxString
operator+(const wxScopedWCharBuffer
& buf
, const wxString
& string
)
3892 { return (const wchar_t *)buf
+ string
; }
3894 inline wxString
operator+(const wxString
& string
, const wxScopedCharBuffer
& buf
)
3895 { return string
+ (const char *)buf
; }
3896 inline wxString
operator+(const wxScopedCharBuffer
& buf
, const wxString
& string
)
3897 { return (const char *)buf
+ string
; }
3899 // comparison with char
3900 inline bool operator==(const wxUniChar
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
3901 inline bool operator==(const wxUniCharRef
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
3902 inline bool operator==(char c
, const wxString
& s
) { return s
.IsSameAs(c
); }
3903 inline bool operator==(wchar_t c
, const wxString
& s
) { return s
.IsSameAs(c
); }
3904 inline bool operator==(int c
, const wxString
& s
) { return s
.IsSameAs(c
); }
3905 inline bool operator==(const wxString
& s
, const wxUniChar
& c
) { return s
.IsSameAs(c
); }
3906 inline bool operator==(const wxString
& s
, const wxUniCharRef
& c
) { return s
.IsSameAs(c
); }
3907 inline bool operator==(const wxString
& s
, char c
) { return s
.IsSameAs(c
); }
3908 inline bool operator==(const wxString
& s
, wchar_t c
) { return s
.IsSameAs(c
); }
3909 inline bool operator!=(const wxUniChar
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
3910 inline bool operator!=(const wxUniCharRef
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
3911 inline bool operator!=(char c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
3912 inline bool operator!=(wchar_t c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
3913 inline bool operator!=(int c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
3914 inline bool operator!=(const wxString
& s
, const wxUniChar
& c
) { return !s
.IsSameAs(c
); }
3915 inline bool operator!=(const wxString
& s
, const wxUniCharRef
& c
) { return !s
.IsSameAs(c
); }
3916 inline bool operator!=(const wxString
& s
, char c
) { return !s
.IsSameAs(c
); }
3917 inline bool operator!=(const wxString
& s
, wchar_t c
) { return !s
.IsSameAs(c
); }
3919 // comparison with C string in Unicode build
3922 #define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s
3924 wxDEFINE_ALL_COMPARISONS(const char *, const wxString
&, wxCMP_CHAR_STRING
)
3926 #undef wxCMP_CHAR_STRING
3928 #endif // wxUSE_UNICODE
3930 // we also need to provide the operators for comparison with wxCStrData to
3931 // resolve ambiguity between operator(const wxChar *,const wxString &) and
3932 // operator(const wxChar *, const wxChar *) for "p == s.c_str()"
3934 // notice that these are (shallow) pointer comparisons, not (deep) string ones
3935 #define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar()
3936 #define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar()
3938 wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData
&, wxCMP_WCHAR_CSTRDATA
)
3939 wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData
&, wxCMP_CHAR_CSTRDATA
)
3941 #undef wxCMP_CHAR_CSTRDATA
3942 #undef wxCMP_WCHAR_CSTRDATA
3944 // ---------------------------------------------------------------------------
3945 // Implementation only from here until the end of file
3946 // ---------------------------------------------------------------------------
3948 #if wxUSE_STD_IOSTREAM
3950 #include "wx/iosfwrap.h"
3952 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxString
&);
3953 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxCStrData
&);
3954 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxScopedCharBuffer
&);
3955 #ifndef __BORLANDC__
3956 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxScopedWCharBuffer
&);
3959 #if wxUSE_UNICODE && defined(HAVE_WOSTREAM)
3961 WXDLLIMPEXP_BASE wxSTD wostream
& operator<<(wxSTD wostream
&, const wxString
&);
3962 WXDLLIMPEXP_BASE wxSTD wostream
& operator<<(wxSTD wostream
&, const wxCStrData
&);
3963 WXDLLIMPEXP_BASE wxSTD wostream
& operator<<(wxSTD wostream
&, const wxScopedWCharBuffer
&);
3965 #endif // wxUSE_UNICODE && defined(HAVE_WOSTREAM)
3967 #endif // wxUSE_STD_IOSTREAM
3969 // ---------------------------------------------------------------------------
3970 // wxCStrData implementation
3971 // ---------------------------------------------------------------------------
3973 inline wxCStrData::wxCStrData(char *buf
)
3974 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
3975 inline wxCStrData::wxCStrData(wchar_t *buf
)
3976 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
3978 inline wxCStrData::wxCStrData(const wxCStrData
& data
)
3979 : m_str(data
.m_owned
? new wxString(*data
.m_str
) : data
.m_str
),
3980 m_offset(data
.m_offset
),
3981 m_owned(data
.m_owned
)
3985 inline wxCStrData::~wxCStrData()
3988 delete const_cast<wxString
*>(m_str
); // cast to silence warnings
3991 // simple cases for AsChar() and AsWChar(), the complicated ones are
3993 #if wxUSE_UNICODE_WCHAR
3994 inline const wchar_t* wxCStrData::AsWChar() const
3996 return m_str
->wx_str() + m_offset
;
3998 #endif // wxUSE_UNICODE_WCHAR
4001 inline const char* wxCStrData::AsChar() const
4003 return m_str
->wx_str() + m_offset
;
4005 #endif // !wxUSE_UNICODE
4007 #if wxUSE_UTF8_LOCALE_ONLY
4008 inline const char* wxCStrData::AsChar() const
4010 return wxStringOperations::AddToIter(m_str
->wx_str(), m_offset
);
4012 #endif // wxUSE_UTF8_LOCALE_ONLY
4014 inline const wxScopedCharBuffer
wxCStrData::AsCharBuf() const
4017 return wxScopedCharBuffer::CreateNonOwned(AsChar());
4019 return AsString().mb_str();
4023 inline const wxScopedWCharBuffer
wxCStrData::AsWCharBuf() const
4025 #if wxUSE_UNICODE_WCHAR
4026 return wxScopedWCharBuffer::CreateNonOwned(AsWChar());
4028 return AsString().wc_str();
4032 inline wxString
wxCStrData::AsString() const
4034 if ( m_offset
== 0 )
4037 return m_str
->Mid(m_offset
);
4040 inline const wxStringCharType
*wxCStrData::AsInternal() const
4042 #if wxUSE_UNICODE_UTF8
4043 return wxStringOperations::AddToIter(m_str
->wx_str(), m_offset
);
4045 return m_str
->wx_str() + m_offset
;
4049 inline wxUniChar
wxCStrData::operator*() const
4051 if ( m_str
->empty() )
4052 return wxUniChar(_T('\0'));
4054 return (*m_str
)[m_offset
];
4057 inline wxUniChar
wxCStrData::operator[](size_t n
) const
4059 // NB: we intentionally use operator[] and not at() here because the former
4060 // works for the terminating NUL while the latter does not
4061 return (*m_str
)[m_offset
+ n
];
4064 // ----------------------------------------------------------------------------
4065 // more wxCStrData operators
4066 // ----------------------------------------------------------------------------
4068 // we need to define those to allow "size_t pos = p - s.c_str()" where p is
4069 // some pointer into the string
4070 inline size_t operator-(const char *p
, const wxCStrData
& cs
)
4072 return p
- cs
.AsChar();
4075 inline size_t operator-(const wchar_t *p
, const wxCStrData
& cs
)
4077 return p
- cs
.AsWChar();
4080 // ----------------------------------------------------------------------------
4081 // implementation of wx[W]CharBuffer inline methods using wxCStrData
4082 // ----------------------------------------------------------------------------
4084 // FIXME-UTF8: move this to buffer.h
4085 inline wxCharBuffer::wxCharBuffer(const wxCStrData
& cstr
)
4086 : wxCharTypeBufferBase(cstr
.AsCharBuf())
4090 inline wxWCharBuffer::wxWCharBuffer(const wxCStrData
& cstr
)
4091 : wxCharTypeBufferBase(cstr
.AsWCharBuf())
4095 #if wxUSE_UNICODE_UTF8
4096 // ----------------------------------------------------------------------------
4097 // implementation of wxStringIteratorNode inline methods
4098 // ----------------------------------------------------------------------------
4100 void wxStringIteratorNode::DoSet(const wxString
*str
,
4101 wxStringImpl::const_iterator
*citer
,
4102 wxStringImpl::iterator
*iter
)
4110 m_next
= str
->m_iterators
.ptr
;
4111 const_cast<wxString
*>(m_str
)->m_iterators
.ptr
= this;
4113 m_next
->m_prev
= this;
4121 void wxStringIteratorNode::clear()
4124 m_next
->m_prev
= m_prev
;
4126 m_prev
->m_next
= m_next
;
4127 else if ( m_str
) // first in the list
4128 const_cast<wxString
*>(m_str
)->m_iterators
.ptr
= m_next
;
4130 m_next
= m_prev
= NULL
;
4135 #endif // wxUSE_UNICODE_UTF8
4137 #if WXWIN_COMPATIBILITY_2_8
4138 // lot of code out there doesn't explicitly include wx/crt.h, but uses
4139 // CRT wrappers that are now declared in wx/wxcrt.h and wx/wxcrtvararg.h,
4140 // so let's include this header now that wxString is defined and it's safe
4145 // ----------------------------------------------------------------------------
4146 // Checks on wxString characters
4147 // ----------------------------------------------------------------------------
4149 template<bool (T
)(const wxUniChar
& c
)>
4150 inline bool wxStringCheck(const wxString
& val
)
4152 for ( wxString::const_iterator i
= val
.begin();
4160 #endif // _WX_WXSTRING_H_