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 inline const wxCharBuffer
AsCharBuf() const;
249 inline const wxWCharBuffer
AsWCharBuf() const;
251 inline wxString
AsString() const;
253 // returns the value as C string in internal representation (equivalent
254 // to AsString().wx_str(), but more efficient)
255 const wxStringCharType
*AsInternal() const;
257 // allow expressions like "c_str()[0]":
258 inline wxUniChar
operator[](size_t n
) const;
259 wxUniChar
operator[](int n
) const { return operator[](size_t(n
)); }
260 wxUniChar
operator[](long n
) const { return operator[](size_t(n
)); }
261 #ifndef wxSIZE_T_IS_UINT
262 wxUniChar
operator[](unsigned int n
) const { return operator[](size_t(n
)); }
263 #endif // size_t != unsigned int
265 // These operators are needed to emulate the pointer semantics of c_str():
266 // expressions like "wxChar *p = str.c_str() + 1;" should continue to work
267 // (we need both versions to resolve ambiguities). Note that this means
268 // the 'n' value is interpreted as addition to char*/wchar_t* pointer, it
269 // is *not* number of Unicode characters in wxString.
270 wxCStrData
operator+(int n
) const
271 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
272 wxCStrData
operator+(long n
) const
273 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
274 wxCStrData
operator+(size_t n
) const
275 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
277 // and these for "str.c_str() + (p2 - p1)" (it also works for any integer
278 // expression but it must be ptrdiff_t and not e.g. int to work in this
280 wxCStrData
operator-(ptrdiff_t n
) const
282 wxASSERT_MSG( n
<= (ptrdiff_t)m_offset
,
283 _T("attempt to construct address before the beginning of the string") );
284 return wxCStrData(m_str
, m_offset
- n
, m_owned
);
287 // this operator is needed to make expressions like "*c_str()" or
288 // "*(c_str() + 2)" work
289 inline wxUniChar
operator*() const;
292 // the wxString this object was returned for
293 const wxString
*m_str
;
294 // Offset into c_str() return value. Note that this is *not* offset in
295 // m_str in Unicode characters. Instead, it is index into the
296 // char*/wchar_t* buffer returned by c_str(). It's interpretation depends
297 // on how is the wxCStrData instance used: if it is eventually cast to
298 // const char*, m_offset will be in bytes form string's start; if it is
299 // cast to const wchar_t*, it will be in wchar_t values.
301 // should m_str be deleted, i.e. is it owned by us?
304 friend class WXDLLIMPEXP_FWD_BASE wxString
;
307 // ----------------------------------------------------------------------------
308 // wxStringPrintfMixin
309 // ---------------------------------------------------------------------------
311 // NB: VC6 has a bug that causes linker errors if you have template methods
312 // in a class using __declspec(dllimport). The solution is to split such
313 // class into two classes, one that contains the template methods and does
314 // *not* use WXDLLIMPEXP_BASE and another class that contains the rest
315 // (with DLL linkage).
317 // We only do this for VC6 here, because the code is less efficient
318 // (Printf() has to use dynamic_cast<>) and because OpenWatcom compiler
319 // cannot compile this code.
321 #if defined(__VISUALC__) && __VISUALC__ < 1300
322 #define wxNEEDS_WXSTRING_PRINTF_MIXIN
325 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
326 // this class contains implementation of wxString's vararg methods, it's
327 // exported from wxBase DLL
328 class WXDLLIMPEXP_BASE wxStringPrintfMixinBase
331 wxStringPrintfMixinBase() {}
333 #if !wxUSE_UTF8_LOCALE_ONLY
334 int DoPrintfWchar(const wxChar
*format
, ...);
335 static wxString
DoFormatWchar(const wxChar
*format
, ...);
337 #if wxUSE_UNICODE_UTF8
338 int DoPrintfUtf8(const char *format
, ...);
339 static wxString
DoFormatUtf8(const char *format
, ...);
343 // this class contains template wrappers for wxString's vararg methods, it's
344 // intentionally *not* exported from the DLL in order to fix the VC6 bug
346 class wxStringPrintfMixin
: public wxStringPrintfMixinBase
349 // to further complicate things, we can't return wxString from
350 // wxStringPrintfMixin::Format() because wxString is not yet declared at
351 // this point; the solution is to use this fake type trait template - this
352 // way the compiler won't know the return type until Format() is used
353 // (this doesn't compile with Watcom, but VC6 compiles it just fine):
354 template<typename T
> struct StringReturnType
356 typedef wxString type
;
360 // these are duplicated wxString methods, they're also declared below
361 // if !wxNEEDS_WXSTRING_PRINTF_MIXIN:
363 // static wxString Format(const wString& format, ...) WX_ATTRIBUTE_PRINTF_1;
364 WX_DEFINE_VARARG_FUNC_SANS_N0(static typename StringReturnType
<T1
>::type
,
365 Format
, 1, (const wxFormatString
&),
366 DoFormatWchar
, DoFormatUtf8
)
367 // We have to implement the version without template arguments manually
368 // because of the StringReturnType<> hack, although WX_DEFINE_VARARG_FUNC
369 // normally does it itself. It has to be a template so that we can use
370 // the hack, even though there's no real template parameter. We can't move
371 // it to wxStrig, because it would shadow these versions of Format() then.
373 inline static typename StringReturnType
<T
>::type
376 // NB: this doesn't compile if T is not (some form of) a string;
377 // this makes Format's prototype equivalent to
378 // Format(const wxFormatString& fmt)
379 return DoFormatWchar(wxFormatString(fmt
));
382 // int Printf(const wxString& format, ...);
383 WX_DEFINE_VARARG_FUNC(int, Printf
, 1, (const wxFormatString
&),
384 DoPrintfWchar
, DoPrintfUtf8
)
385 // int sprintf(const wxString& format, ...) WX_ATTRIBUTE_PRINTF_2;
386 WX_DEFINE_VARARG_FUNC(int, sprintf
, 1, (const wxFormatString
&),
387 DoPrintfWchar
, DoPrintfUtf8
)
390 wxStringPrintfMixin() : wxStringPrintfMixinBase() {}
392 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
395 // ----------------------------------------------------------------------------
396 // wxString: string class trying to be compatible with std::string, MFC
397 // CString and wxWindows 1.x wxString all at once
398 // ---------------------------------------------------------------------------
400 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
401 // "non dll-interface class 'wxStringPrintfMixin' used as base interface
402 // for dll-interface class 'wxString'" -- this is OK in our case
403 #pragma warning (disable:4275)
406 #if wxUSE_UNICODE_UTF8
407 // see the comment near wxString::iterator for why we need this
408 class WXDLLIMPEXP_BASE wxStringIteratorNode
411 wxStringIteratorNode()
412 : m_str(NULL
), m_citer(NULL
), m_iter(NULL
), m_prev(NULL
), m_next(NULL
) {}
413 wxStringIteratorNode(const wxString
*str
,
414 wxStringImpl::const_iterator
*citer
)
415 { DoSet(str
, citer
, NULL
); }
416 wxStringIteratorNode(const wxString
*str
, wxStringImpl::iterator
*iter
)
417 { DoSet(str
, NULL
, iter
); }
418 ~wxStringIteratorNode()
421 inline void set(const wxString
*str
, wxStringImpl::const_iterator
*citer
)
422 { clear(); DoSet(str
, citer
, NULL
); }
423 inline void set(const wxString
*str
, wxStringImpl::iterator
*iter
)
424 { clear(); DoSet(str
, NULL
, iter
); }
426 const wxString
*m_str
;
427 wxStringImpl::const_iterator
*m_citer
;
428 wxStringImpl::iterator
*m_iter
;
429 wxStringIteratorNode
*m_prev
, *m_next
;
433 inline void DoSet(const wxString
*str
,
434 wxStringImpl::const_iterator
*citer
,
435 wxStringImpl::iterator
*iter
);
437 // the node belongs to a particular iterator instance, it's not copied
438 // when a copy of the iterator is made
439 wxDECLARE_NO_COPY_CLASS(wxStringIteratorNode
);
441 #endif // wxUSE_UNICODE_UTF8
443 class WXDLLIMPEXP_BASE wxString
444 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
445 : public wxStringPrintfMixin
448 // NB: special care was taken in arranging the member functions in such order
449 // that all inline functions can be effectively inlined, verify that all
450 // performance critical functions are still inlined if you change order!
452 // an 'invalid' value for string index, moved to this place due to a CW bug
453 static const size_t npos
;
456 // if we hadn't made these operators private, it would be possible to
457 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
458 // converted to char in C and we do have operator=(char)
460 // NB: we don't need other versions (short/long and unsigned) as attempt
461 // to assign another numeric type to wxString will now result in
462 // ambiguity between operator=(char) and operator=(int)
463 wxString
& operator=(int);
465 // these methods are not implemented - there is _no_ conversion from int to
466 // string, you're doing something wrong if the compiler wants to call it!
468 // try `s << i' or `s.Printf("%d", i)' instead
472 // buffer for holding temporary substring when using any of the methods
473 // that take (char*,size_t) or (wchar_t*,size_t) arguments:
475 struct SubstrBufFromType
480 SubstrBufFromType(const T
& data_
, size_t len_
)
481 : data(data_
), len(len_
)
483 wxASSERT_MSG( len
!= npos
, "must have real length" );
487 #if wxUSE_UNICODE_UTF8
488 // even char* -> char* needs conversion, from locale charset to UTF-8
489 typedef SubstrBufFromType
<wxCharBuffer
> SubstrBufFromWC
;
490 typedef SubstrBufFromType
<wxCharBuffer
> SubstrBufFromMB
;
491 #elif wxUSE_UNICODE_WCHAR
492 typedef SubstrBufFromType
<const wchar_t*> SubstrBufFromWC
;
493 typedef SubstrBufFromType
<wxWCharBuffer
> SubstrBufFromMB
;
495 typedef SubstrBufFromType
<const char*> SubstrBufFromMB
;
496 typedef SubstrBufFromType
<wxCharBuffer
> SubstrBufFromWC
;
500 // Functions implementing primitive operations on string data; wxString
501 // methods and iterators are implemented in terms of it. The differences
502 // between UTF-8 and wchar_t* representations of the string are mostly
505 #if wxUSE_UNICODE_UTF8
506 static SubstrBufFromMB
ConvertStr(const char *psz
, size_t nLength
,
507 const wxMBConv
& conv
);
508 static SubstrBufFromWC
ConvertStr(const wchar_t *pwz
, size_t nLength
,
509 const wxMBConv
& conv
);
510 #elif wxUSE_UNICODE_WCHAR
511 static SubstrBufFromMB
ConvertStr(const char *psz
, size_t nLength
,
512 const wxMBConv
& conv
);
514 static SubstrBufFromWC
ConvertStr(const wchar_t *pwz
, size_t nLength
,
515 const wxMBConv
& conv
);
518 #if !wxUSE_UNICODE_UTF8 // wxUSE_UNICODE_WCHAR or !wxUSE_UNICODE
519 // returns C string encoded as the implementation expects:
521 static const wchar_t* ImplStr(const wchar_t* str
)
522 { return str
? str
: wxT(""); }
523 static const SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
524 { return SubstrBufFromWC(str
, (str
&& n
== npos
) ? wxWcslen(str
) : n
); }
525 static wxWCharBuffer
ImplStr(const char* str
,
526 const wxMBConv
& conv
= wxConvLibc
)
527 { return ConvertStr(str
, npos
, conv
).data
; }
528 static SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
529 const wxMBConv
& conv
= wxConvLibc
)
530 { return ConvertStr(str
, n
, conv
); }
532 static const char* ImplStr(const char* str
,
533 const wxMBConv
& WXUNUSED(conv
) = wxConvLibc
)
534 { return str
? str
: ""; }
535 static const SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
536 const wxMBConv
& WXUNUSED(conv
) = wxConvLibc
)
537 { return SubstrBufFromMB(str
, (str
&& n
== npos
) ? wxStrlen(str
) : n
); }
538 static wxCharBuffer
ImplStr(const wchar_t* str
)
539 { return ConvertStr(str
, npos
, wxConvLibc
).data
; }
540 static SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
541 { return ConvertStr(str
, n
, wxConvLibc
); }
544 // translates position index in wxString to/from index in underlying
546 static size_t PosToImpl(size_t pos
) { return pos
; }
547 static void PosLenToImpl(size_t pos
, size_t len
,
548 size_t *implPos
, size_t *implLen
)
549 { *implPos
= pos
; *implLen
= len
; }
550 static size_t LenToImpl(size_t len
) { return len
; }
551 static size_t PosFromImpl(size_t pos
) { return pos
; }
553 // we don't want to define these as empty inline functions as it could
554 // result in noticeable (and quite unnecessary in non-UTF-8 build) slowdown
555 // in debug build where the inline functions are not effectively inlined
556 #define wxSTRING_INVALIDATE_CACHE()
557 #define wxSTRING_INVALIDATE_CACHED_LENGTH()
558 #define wxSTRING_UPDATE_CACHED_LENGTH(n)
559 #define wxSTRING_SET_CACHED_LENGTH(n)
561 #else // wxUSE_UNICODE_UTF8
563 static wxCharBuffer
ImplStr(const char* str
,
564 const wxMBConv
& conv
= wxConvLibc
)
565 { return ConvertStr(str
, npos
, conv
).data
; }
566 static SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
567 const wxMBConv
& conv
= wxConvLibc
)
568 { return ConvertStr(str
, n
, conv
); }
570 static wxCharBuffer
ImplStr(const wchar_t* str
)
571 { return ConvertStr(str
, npos
, wxMBConvUTF8()).data
; }
572 static SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
573 { return ConvertStr(str
, n
, wxMBConvUTF8()); }
575 #if wxUSE_STRING_POS_CACHE
576 // this is an extremely simple cache used by PosToImpl(): each cache element
577 // contains the string it applies to and the index corresponding to the last
578 // used position in this wxString in its m_impl string
580 // NB: notice that this struct (and nested Element one) must be a POD or we
581 // wouldn't be able to use a thread-local variable of this type, in
582 // particular it should have no ctor -- we rely on statics being
583 // initialized to 0 instead
590 const wxString
*str
; // the string to which this element applies
591 size_t pos
, // the cached index in this string
592 impl
, // the corresponding position in its m_impl
593 len
; // cached length or npos if unknown
595 // reset cached index to 0
596 void ResetPos() { pos
= impl
= 0; }
598 // reset position and length
599 void Reset() { ResetPos(); len
= npos
; }
602 // cache the indices mapping for the last few string used
603 Element cached
[SIZE
];
605 // the last used index
609 #ifndef wxHAS_COMPILER_TLS
610 // we must use an accessor function and not a static variable when the TLS
611 // variables support is implemented in the library (and not by the compiler)
612 // because the global s_cache variable could be not yet initialized when a
613 // ctor of another global object is executed and if that ctor uses any
614 // wxString methods, bad things happen
616 // however notice that this approach does not work when compiler TLS is used,
617 // at least not with g++ 4.1.2 under amd64 as it apparently compiles code
618 // using this accessor incorrectly when optimizations are enabled (-O2 is
619 // enough) -- luckily we don't need it then neither as static __thread
620 // variables are initialized by 0 anyhow then and so we can use the variable
622 WXEXPORT
static Cache
& GetCache()
624 static wxTLS_TYPE(Cache
) s_cache
;
626 return wxTLS_VALUE(s_cache
);
629 // this helper struct is used to ensure that GetCache() is called during
630 // static initialization time, i.e. before any threads creation, as otherwise
631 // the static s_cache construction inside GetCache() wouldn't be MT-safe
632 friend struct wxStrCacheInitializer
;
633 #else // wxHAS_COMPILER_TLS
634 static wxTLS_TYPE(Cache
) ms_cache
;
635 static Cache
& GetCache() { return wxTLS_VALUE(ms_cache
); }
636 #endif // !wxHAS_COMPILER_TLS/wxHAS_COMPILER_TLS
638 static Cache::Element
*GetCacheBegin() { return GetCache().cached
; }
639 static Cache::Element
*GetCacheEnd() { return GetCacheBegin() + Cache::SIZE
; }
640 static unsigned& LastUsedCacheElement() { return GetCache().lastUsed
; }
642 // this is used in debug builds only to provide a convenient function,
643 // callable from a debugger, to show the cache contents
644 friend struct wxStrCacheDumper
;
646 // uncomment this to have access to some profiling statistics on program
648 //#define wxPROFILE_STRING_CACHE
650 #ifdef wxPROFILE_STRING_CACHE
651 static struct PosToImplCacheStats
653 unsigned postot
, // total non-trivial calls to PosToImpl
654 poshits
, // cache hits from PosToImpl()
655 mishits
, // cached position beyond the needed one
656 sumpos
, // sum of all positions, used to compute the
657 // average position after dividing by postot
658 sumofs
, // sum of all offsets after using the cache, used to
659 // compute the average after dividing by hits
660 lentot
, // number of total calls to length()
661 lenhits
; // number of cache hits in length()
664 friend struct wxStrCacheStatsDumper
;
666 #define wxCACHE_PROFILE_FIELD_INC(field) ms_cacheStats.field++
667 #define wxCACHE_PROFILE_FIELD_ADD(field, val) ms_cacheStats.field += (val)
668 #else // !wxPROFILE_STRING_CACHE
669 #define wxCACHE_PROFILE_FIELD_INC(field)
670 #define wxCACHE_PROFILE_FIELD_ADD(field, val)
671 #endif // wxPROFILE_STRING_CACHE/!wxPROFILE_STRING_CACHE
673 // note: it could seem that the functions below shouldn't be inline because
674 // they are big, contain loops and so the compiler shouldn't be able to
675 // inline them anyhow, however moving them into string.cpp does decrease the
676 // code performance by ~5%, at least when using g++ 4.1 so do keep them here
677 // unless tests show that it's not advantageous any more
679 // return the pointer to the cache element for this string or NULL if not
681 Cache::Element
*FindCacheElement() const
683 // profiling seems to show a small but consistent gain if we use this
684 // simple loop instead of starting from the last used element (there are
685 // a lot of misses in this function...)
686 Cache::Element
* const cacheBegin
= GetCacheBegin();
687 #ifndef wxHAS_COMPILER_TLS
688 // during destruction tls calls may return NULL, in this case return NULL
689 // immediately without accessing anything else
690 if ( cacheBegin
== NULL
)
693 Cache::Element
* const cacheEnd
= GetCacheEnd();
694 for ( Cache::Element
*c
= cacheBegin
; c
!= cacheEnd
; c
++ )
696 if ( c
->str
== this )
703 // unlike FindCacheElement(), this one always returns a valid pointer to the
704 // cache element for this string, it may have valid last cached position and
705 // its corresponding index in the byte string or not
706 Cache::Element
*GetCacheElement() const
708 Cache::Element
* const cacheBegin
= GetCacheBegin();
709 Cache::Element
* const cacheEnd
= GetCacheEnd();
710 Cache::Element
* const cacheStart
= cacheBegin
+ LastUsedCacheElement();
712 // check the last used first, this does no (measurable) harm for a miss
713 // but does help for simple loops addressing the same string all the time
714 if ( cacheStart
->str
== this )
717 // notice that we're going to check cacheStart again inside this call but
718 // profiling shows that it's still faster to use a simple loop like
719 // inside FindCacheElement() than manually looping with wrapping starting
720 // from the cache entry after the start one
721 Cache::Element
*c
= FindCacheElement();
724 // claim the next cache entry for this string
726 if ( ++c
== cacheEnd
)
732 // and remember the last used element
733 LastUsedCacheElement() = c
- cacheBegin
;
739 size_t DoPosToImpl(size_t pos
) const
741 wxCACHE_PROFILE_FIELD_INC(postot
);
743 // NB: although the case of pos == 1 (and offset from cached position
744 // equal to 1) are common, nothing is gained by writing special code
745 // for handling them, the compiler (at least g++ 4.1 used) seems to
746 // optimize the code well enough on its own
748 wxCACHE_PROFILE_FIELD_ADD(sumpos
, pos
);
750 Cache::Element
* const cache
= GetCacheElement();
752 // cached position can't be 0 so if it is, it means that this entry was
753 // used for length caching only so far, i.e. it doesn't count as a hit
754 // from our point of view
757 wxCACHE_PROFILE_FIELD_INC(poshits
);
760 if ( pos
== cache
->pos
)
763 // this seems to happen only rarely so just reset the cache in this case
764 // instead of complicating code even further by seeking backwards in this
766 if ( cache
->pos
> pos
)
768 wxCACHE_PROFILE_FIELD_INC(mishits
);
773 wxCACHE_PROFILE_FIELD_ADD(sumofs
, pos
- cache
->pos
);
776 wxStringImpl::const_iterator
i(m_impl
.begin() + cache
->impl
);
777 for ( size_t n
= cache
->pos
; n
< pos
; n
++ )
778 wxStringOperations::IncIter(i
);
781 cache
->impl
= i
- m_impl
.begin();
783 wxSTRING_CACHE_ASSERT(
784 (int)cache
->impl
== (begin() + pos
).impl() - m_impl
.begin() );
789 void InvalidateCache()
791 Cache::Element
* const cache
= FindCacheElement();
796 void InvalidateCachedLength()
798 Cache::Element
* const cache
= FindCacheElement();
803 void SetCachedLength(size_t len
)
805 // we optimistically cache the length here even if the string wasn't
806 // present in the cache before, this seems to do no harm and the
807 // potential for avoiding length recomputation for long strings looks
809 GetCacheElement()->len
= len
;
812 void UpdateCachedLength(ptrdiff_t delta
)
814 Cache::Element
* const cache
= FindCacheElement();
815 if ( cache
&& cache
->len
!= npos
)
817 wxSTRING_CACHE_ASSERT( (ptrdiff_t)cache
->len
+ delta
>= 0 );
823 #define wxSTRING_INVALIDATE_CACHE() InvalidateCache()
824 #define wxSTRING_INVALIDATE_CACHED_LENGTH() InvalidateCachedLength()
825 #define wxSTRING_UPDATE_CACHED_LENGTH(n) UpdateCachedLength(n)
826 #define wxSTRING_SET_CACHED_LENGTH(n) SetCachedLength(n)
827 #else // !wxUSE_STRING_POS_CACHE
828 size_t DoPosToImpl(size_t pos
) const
830 return (begin() + pos
).impl() - m_impl
.begin();
833 #define wxSTRING_INVALIDATE_CACHE()
834 #define wxSTRING_INVALIDATE_CACHED_LENGTH()
835 #define wxSTRING_UPDATE_CACHED_LENGTH(n)
836 #define wxSTRING_SET_CACHED_LENGTH(n)
837 #endif // wxUSE_STRING_POS_CACHE/!wxUSE_STRING_POS_CACHE
839 size_t PosToImpl(size_t pos
) const
841 return pos
== 0 || pos
== npos
? pos
: DoPosToImpl(pos
);
844 void PosLenToImpl(size_t pos
, size_t len
, size_t *implPos
, size_t *implLen
) const;
846 size_t LenToImpl(size_t len
) const
849 PosLenToImpl(0, len
, &pos
, &len2
);
853 size_t PosFromImpl(size_t pos
) const
855 if ( pos
== 0 || pos
== npos
)
858 return const_iterator(this, m_impl
.begin() + pos
) - begin();
860 #endif // !wxUSE_UNICODE_UTF8/wxUSE_UNICODE_UTF8
864 typedef wxUniChar value_type
;
865 typedef wxUniChar char_type
;
866 typedef wxUniCharRef reference
;
867 typedef wxChar
* pointer
;
868 typedef const wxChar
* const_pointer
;
870 typedef size_t size_type
;
871 typedef wxUniChar const_reference
;
874 #if wxUSE_UNICODE_UTF8
875 // random access is not O(1), as required by Random Access Iterator
876 #define WX_STR_ITERATOR_TAG std::bidirectional_iterator_tag
878 #define WX_STR_ITERATOR_TAG std::random_access_iterator_tag
880 #define WX_DEFINE_ITERATOR_CATEGORY(cat) typedef cat iterator_category;
882 // not defining iterator_category at all in this case is better than defining
883 // it as some dummy type -- at least it results in more intelligible error
885 #define WX_DEFINE_ITERATOR_CATEGORY(cat)
888 #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, reference_type) \
890 typedef wxStringImpl::iterator_name underlying_iterator; \
892 WX_DEFINE_ITERATOR_CATEGORY(WX_STR_ITERATOR_TAG) \
893 typedef wxUniChar value_type; \
894 typedef int difference_type; \
895 typedef reference_type reference; \
896 typedef pointer_type pointer; \
898 reference operator[](size_t n) const { return *(*this + n); } \
900 iterator_name& operator++() \
901 { wxStringOperations::IncIter(m_cur); return *this; } \
902 iterator_name& operator--() \
903 { wxStringOperations::DecIter(m_cur); return *this; } \
904 iterator_name operator++(int) \
906 iterator_name tmp = *this; \
907 wxStringOperations::IncIter(m_cur); \
910 iterator_name operator--(int) \
912 iterator_name tmp = *this; \
913 wxStringOperations::DecIter(m_cur); \
917 iterator_name& operator+=(ptrdiff_t n) \
919 m_cur = wxStringOperations::AddToIter(m_cur, n); \
922 iterator_name& operator-=(ptrdiff_t n) \
924 m_cur = wxStringOperations::AddToIter(m_cur, -n); \
928 difference_type operator-(const iterator_name& i) const \
929 { return wxStringOperations::DiffIters(m_cur, i.m_cur); } \
931 bool operator==(const iterator_name& i) const \
932 { return m_cur == i.m_cur; } \
933 bool operator!=(const iterator_name& i) const \
934 { return m_cur != i.m_cur; } \
936 bool operator<(const iterator_name& i) const \
937 { return m_cur < i.m_cur; } \
938 bool operator>(const iterator_name& i) const \
939 { return m_cur > i.m_cur; } \
940 bool operator<=(const iterator_name& i) const \
941 { return m_cur <= i.m_cur; } \
942 bool operator>=(const iterator_name& i) const \
943 { return m_cur >= i.m_cur; } \
946 /* for internal wxString use only: */ \
947 underlying_iterator impl() const { return m_cur; } \
949 friend class wxString; \
950 friend class wxCStrData; \
953 underlying_iterator m_cur
955 class WXDLLIMPEXP_FWD_BASE const_iterator
;
957 #if wxUSE_UNICODE_UTF8
958 // NB: In UTF-8 build, (non-const) iterator needs to keep reference
959 // to the underlying wxStringImpl, because UTF-8 is variable-length
960 // encoding and changing the value pointer to by an iterator (using
961 // its operator*) requires calling wxStringImpl::replace() if the old
962 // and new values differ in their encoding's length.
964 // Furthermore, the replace() call may invalid all iterators for the
965 // string, so we have to keep track of outstanding iterators and update
966 // them if replace() happens.
968 // This is implemented by maintaining linked list of iterators for every
969 // string and traversing it in wxUniCharRef::operator=(). Head of the
970 // list is stored in wxString. (FIXME-UTF8)
972 class WXDLLIMPEXP_BASE iterator
974 WX_STR_ITERATOR_IMPL(iterator
, wxChar
*, wxUniCharRef
);
978 iterator(const iterator
& i
)
979 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
980 iterator
& operator=(const iterator
& i
)
985 m_node
.set(i
.str(), &m_cur
);
990 reference
operator*()
991 { return wxUniCharRef::CreateForString(*str(), m_cur
); }
993 iterator
operator+(ptrdiff_t n
) const
994 { return iterator(str(), wxStringOperations::AddToIter(m_cur
, n
)); }
995 iterator
operator-(ptrdiff_t n
) const
996 { return iterator(str(), wxStringOperations::AddToIter(m_cur
, -n
)); }
999 iterator(wxString
*str
, underlying_iterator ptr
)
1000 : m_cur(ptr
), m_node(str
, &m_cur
) {}
1002 wxString
* str() const { return const_cast<wxString
*>(m_node
.m_str
); }
1004 wxStringIteratorNode m_node
;
1006 friend class const_iterator
;
1009 class WXDLLIMPEXP_BASE const_iterator
1011 // NB: reference_type is intentionally value, not reference, the character
1012 // may be encoded differently in wxString data:
1013 WX_STR_ITERATOR_IMPL(const_iterator
, const wxChar
*, wxUniChar
);
1017 const_iterator(const const_iterator
& i
)
1018 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
1019 const_iterator(const iterator
& i
)
1020 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
1022 const_iterator
& operator=(const const_iterator
& i
)
1027 m_node
.set(i
.str(), &m_cur
);
1031 const_iterator
& operator=(const iterator
& i
)
1032 { m_cur
= i
.m_cur
; m_node
.set(i
.str(), &m_cur
); return *this; }
1034 reference
operator*() const
1035 { return wxStringOperations::DecodeChar(m_cur
); }
1037 const_iterator
operator+(ptrdiff_t n
) const
1038 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur
, n
)); }
1039 const_iterator
operator-(ptrdiff_t n
) const
1040 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur
, -n
)); }
1043 // for internal wxString use only:
1044 const_iterator(const wxString
*str
, underlying_iterator ptr
)
1045 : m_cur(ptr
), m_node(str
, &m_cur
) {}
1047 const wxString
* str() const { return m_node
.m_str
; }
1049 wxStringIteratorNode m_node
;
1052 size_t IterToImplPos(wxString::iterator i
) const
1053 { return wxStringImpl::const_iterator(i
.impl()) - m_impl
.begin(); }
1055 iterator
GetIterForNthChar(size_t n
)
1056 { return iterator(this, m_impl
.begin() + PosToImpl(n
)); }
1057 const_iterator
GetIterForNthChar(size_t n
) const
1058 { return const_iterator(this, m_impl
.begin() + PosToImpl(n
)); }
1059 #else // !wxUSE_UNICODE_UTF8
1061 class WXDLLIMPEXP_BASE iterator
1063 WX_STR_ITERATOR_IMPL(iterator
, wxChar
*, wxUniCharRef
);
1067 iterator(const iterator
& i
) : m_cur(i
.m_cur
) {}
1069 reference
operator*()
1070 { return wxUniCharRef::CreateForString(m_cur
); }
1072 iterator
operator+(ptrdiff_t n
) const
1073 { return iterator(wxStringOperations::AddToIter(m_cur
, n
)); }
1074 iterator
operator-(ptrdiff_t n
) const
1075 { return iterator(wxStringOperations::AddToIter(m_cur
, -n
)); }
1078 // for internal wxString use only:
1079 iterator(underlying_iterator ptr
) : m_cur(ptr
) {}
1080 iterator(wxString
*WXUNUSED(str
), underlying_iterator ptr
) : m_cur(ptr
) {}
1082 friend class const_iterator
;
1085 class WXDLLIMPEXP_BASE const_iterator
1087 // NB: reference_type is intentionally value, not reference, the character
1088 // may be encoded differently in wxString data:
1089 WX_STR_ITERATOR_IMPL(const_iterator
, const wxChar
*, wxUniChar
);
1093 const_iterator(const const_iterator
& i
) : m_cur(i
.m_cur
) {}
1094 const_iterator(const iterator
& i
) : m_cur(i
.m_cur
) {}
1096 reference
operator*() const
1097 { return wxStringOperations::DecodeChar(m_cur
); }
1099 const_iterator
operator+(ptrdiff_t n
) const
1100 { return const_iterator(wxStringOperations::AddToIter(m_cur
, n
)); }
1101 const_iterator
operator-(ptrdiff_t n
) const
1102 { return const_iterator(wxStringOperations::AddToIter(m_cur
, -n
)); }
1105 // for internal wxString use only:
1106 const_iterator(underlying_iterator ptr
) : m_cur(ptr
) {}
1107 const_iterator(const wxString
*WXUNUSED(str
), underlying_iterator ptr
)
1111 iterator
GetIterForNthChar(size_t n
) { return begin() + n
; }
1112 const_iterator
GetIterForNthChar(size_t n
) const { return begin() + n
; }
1113 #endif // wxUSE_UNICODE_UTF8/!wxUSE_UNICODE_UTF8
1115 #undef WX_STR_ITERATOR_TAG
1116 #undef WX_STR_ITERATOR_IMPL
1118 friend class iterator
;
1119 friend class const_iterator
;
1121 template <typename T
>
1122 class reverse_iterator_impl
1125 typedef T iterator_type
;
1127 WX_DEFINE_ITERATOR_CATEGORY(typename
T::iterator_category
)
1128 typedef typename
T::value_type value_type
;
1129 typedef typename
T::difference_type difference_type
;
1130 typedef typename
T::reference reference
;
1131 typedef typename
T::pointer
*pointer
;
1133 reverse_iterator_impl() {}
1134 reverse_iterator_impl(iterator_type i
) : m_cur(i
) {}
1135 reverse_iterator_impl(const reverse_iterator_impl
& ri
)
1136 : m_cur(ri
.m_cur
) {}
1138 iterator_type
base() const { return m_cur
; }
1140 reference
operator*() const { return *(m_cur
-1); }
1141 reference
operator[](size_t n
) const { return *(*this + n
); }
1143 reverse_iterator_impl
& operator++()
1144 { --m_cur
; return *this; }
1145 reverse_iterator_impl
operator++(int)
1146 { reverse_iterator_impl tmp
= *this; --m_cur
; return tmp
; }
1147 reverse_iterator_impl
& operator--()
1148 { ++m_cur
; return *this; }
1149 reverse_iterator_impl
operator--(int)
1150 { reverse_iterator_impl tmp
= *this; ++m_cur
; return tmp
; }
1152 // NB: explicit <T> in the functions below is to keep BCC 5.5 happy
1153 reverse_iterator_impl
operator+(ptrdiff_t n
) const
1154 { return reverse_iterator_impl
<T
>(m_cur
- n
); }
1155 reverse_iterator_impl
operator-(ptrdiff_t n
) const
1156 { return reverse_iterator_impl
<T
>(m_cur
+ n
); }
1157 reverse_iterator_impl
operator+=(ptrdiff_t n
)
1158 { m_cur
-= n
; return *this; }
1159 reverse_iterator_impl
operator-=(ptrdiff_t n
)
1160 { m_cur
+= n
; return *this; }
1162 unsigned operator-(const reverse_iterator_impl
& i
) const
1163 { return i
.m_cur
- m_cur
; }
1165 bool operator==(const reverse_iterator_impl
& ri
) const
1166 { return m_cur
== ri
.m_cur
; }
1167 bool operator!=(const reverse_iterator_impl
& ri
) const
1168 { return !(*this == ri
); }
1170 bool operator<(const reverse_iterator_impl
& i
) const
1171 { return m_cur
> i
.m_cur
; }
1172 bool operator>(const reverse_iterator_impl
& i
) const
1173 { return m_cur
< i
.m_cur
; }
1174 bool operator<=(const reverse_iterator_impl
& i
) const
1175 { return m_cur
>= i
.m_cur
; }
1176 bool operator>=(const reverse_iterator_impl
& i
) const
1177 { return m_cur
<= i
.m_cur
; }
1180 iterator_type m_cur
;
1183 typedef reverse_iterator_impl
<iterator
> reverse_iterator
;
1184 typedef reverse_iterator_impl
<const_iterator
> const_reverse_iterator
;
1187 // used to transform an expression built using c_str() (and hence of type
1188 // wxCStrData) to an iterator into the string
1189 static const_iterator
CreateConstIterator(const wxCStrData
& data
)
1191 return const_iterator(data
.m_str
,
1192 (data
.m_str
->begin() + data
.m_offset
).impl());
1195 // in UTF-8 STL build, creation from std::string requires conversion under
1196 // non-UTF8 locales, so we can't have and use wxString(wxStringImpl) ctor;
1197 // instead we define dummy type that lets us have wxString ctor for creation
1198 // from wxStringImpl that couldn't be used by user code (in all other builds,
1199 // "standard" ctors can be used):
1200 #if wxUSE_UNICODE_UTF8 && wxUSE_STL_BASED_WXSTRING
1201 struct CtorFromStringImplTag
{};
1203 wxString(CtorFromStringImplTag
* WXUNUSED(dummy
), const wxStringImpl
& src
)
1206 static wxString
FromImpl(const wxStringImpl
& src
)
1207 { return wxString((CtorFromStringImplTag
*)NULL
, src
); }
1209 #if !wxUSE_STL_BASED_WXSTRING
1210 wxString(const wxStringImpl
& src
) : m_impl(src
) { }
1211 // else: already defined as wxString(wxStdString) below
1213 static wxString
FromImpl(const wxStringImpl
& src
) { return wxString(src
); }
1217 // constructors and destructor
1218 // ctor for an empty string
1222 wxString(const wxString
& stringSrc
) : m_impl(stringSrc
.m_impl
) { }
1224 // string containing nRepeat copies of ch
1225 wxString(wxUniChar ch
, size_t nRepeat
= 1 )
1226 { assign(nRepeat
, ch
); }
1227 wxString(size_t nRepeat
, wxUniChar ch
)
1228 { assign(nRepeat
, ch
); }
1229 wxString(wxUniCharRef ch
, size_t nRepeat
= 1)
1230 { assign(nRepeat
, ch
); }
1231 wxString(size_t nRepeat
, wxUniCharRef ch
)
1232 { assign(nRepeat
, ch
); }
1233 wxString(char ch
, size_t nRepeat
= 1)
1234 { assign(nRepeat
, ch
); }
1235 wxString(size_t nRepeat
, char ch
)
1236 { assign(nRepeat
, ch
); }
1237 wxString(wchar_t ch
, size_t nRepeat
= 1)
1238 { assign(nRepeat
, ch
); }
1239 wxString(size_t nRepeat
, wchar_t ch
)
1240 { assign(nRepeat
, ch
); }
1242 // ctors from char* strings:
1243 wxString(const char *psz
)
1244 : m_impl(ImplStr(psz
)) {}
1245 wxString(const char *psz
, const wxMBConv
& conv
)
1246 : m_impl(ImplStr(psz
, conv
)) {}
1247 wxString(const char *psz
, size_t nLength
)
1248 { assign(psz
, nLength
); }
1249 wxString(const char *psz
, const wxMBConv
& conv
, size_t nLength
)
1251 SubstrBufFromMB
str(ImplStr(psz
, nLength
, conv
));
1252 m_impl
.assign(str
.data
, str
.len
);
1255 // and unsigned char*:
1256 wxString(const unsigned char *psz
)
1257 : m_impl(ImplStr((const char*)psz
)) {}
1258 wxString(const unsigned char *psz
, const wxMBConv
& conv
)
1259 : m_impl(ImplStr((const char*)psz
, conv
)) {}
1260 wxString(const unsigned char *psz
, size_t nLength
)
1261 { assign((const char*)psz
, nLength
); }
1262 wxString(const unsigned char *psz
, const wxMBConv
& conv
, size_t nLength
)
1264 SubstrBufFromMB
str(ImplStr((const char*)psz
, nLength
, conv
));
1265 m_impl
.assign(str
.data
, str
.len
);
1268 // ctors from wchar_t* strings:
1269 wxString(const wchar_t *pwz
)
1270 : m_impl(ImplStr(pwz
)) {}
1271 wxString(const wchar_t *pwz
, const wxMBConv
& WXUNUSED(conv
))
1272 : m_impl(ImplStr(pwz
)) {}
1273 wxString(const wchar_t *pwz
, size_t nLength
)
1274 { assign(pwz
, nLength
); }
1275 wxString(const wchar_t *pwz
, const wxMBConv
& WXUNUSED(conv
), size_t nLength
)
1276 { assign(pwz
, nLength
); }
1278 wxString(const wxCharBuffer
& buf
)
1279 { assign(buf
.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
1280 wxString(const wxWCharBuffer
& buf
)
1281 { assign(buf
.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
1283 // NB: this version uses m_impl.c_str() to force making a copy of the
1284 // string, so that "wxString(str.c_str())" idiom for passing strings
1285 // between threads works
1286 wxString(const wxCStrData
& cstr
)
1287 : m_impl(cstr
.AsString().m_impl
.c_str()) { }
1289 // as we provide both ctors with this signature for both char and unsigned
1290 // char string, we need to provide one for wxCStrData to resolve ambiguity
1291 wxString(const wxCStrData
& cstr
, size_t nLength
)
1292 : m_impl(cstr
.AsString().Mid(0, nLength
).m_impl
) {}
1294 // and because wxString is convertible to wxCStrData and const wxChar *
1295 // we also need to provide this one
1296 wxString(const wxString
& str
, size_t nLength
)
1297 { assign(str
, nLength
); }
1300 #if wxUSE_STRING_POS_CACHE
1303 // we need to invalidate our cache entry as another string could be
1304 // recreated at the same address (unlikely, but still possible, with the
1305 // heap-allocated strings but perfectly common with stack-allocated ones)
1308 #endif // wxUSE_STRING_POS_CACHE
1310 // even if we're not built with wxUSE_STL == 1 it is very convenient to allow
1311 // implicit conversions from std::string to wxString and vice verse as this
1312 // allows to use the same strings in non-GUI and GUI code, however we don't
1313 // want to unconditionally add this ctor as it would make wx lib dependent on
1314 // libstdc++ on some Linux versions which is bad, so instead we ask the
1315 // client code to define this wxUSE_STD_STRING symbol if they need it
1316 #if wxUSE_STD_STRING
1317 #if wxUSE_UNICODE_WCHAR
1318 wxString(const wxStdWideString
& str
) : m_impl(str
) {}
1319 #else // UTF-8 or ANSI
1320 wxString(const wxStdWideString
& str
)
1321 { assign(str
.c_str(), str
.length()); }
1324 #if !wxUSE_UNICODE // ANSI build
1325 // FIXME-UTF8: do this in UTF8 build #if wxUSE_UTF8_LOCALE_ONLY, too
1326 wxString(const std::string
& str
) : m_impl(str
) {}
1328 wxString(const std::string
& str
)
1329 { assign(str
.c_str(), str
.length()); }
1331 #endif // wxUSE_STD_STRING
1333 // Unlike ctor from std::string, we provide conversion to std::string only
1334 // if wxUSE_STL and not merely wxUSE_STD_STRING (which is on by default),
1335 // because it conflicts with operator const char/wchar_t*:
1337 #if wxUSE_UNICODE_WCHAR && wxUSE_STL_BASED_WXSTRING
1338 // wxStringImpl is std::string in the encoding we want
1339 operator const wxStdWideString
&() const { return m_impl
; }
1341 // wxStringImpl is either not std::string or needs conversion
1342 operator wxStdWideString() const
1343 // FIXME-UTF8: broken for embedded NULs
1344 { return wxStdWideString(wc_str()); }
1347 #if (!wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY) && wxUSE_STL_BASED_WXSTRING
1348 // wxStringImpl is std::string in the encoding we want
1349 operator const std::string
&() const { return m_impl
; }
1351 // wxStringImpl is either not std::string or needs conversion
1352 operator std::string() const
1353 // FIXME-UTF8: broken for embedded NULs
1354 { return std::string(mb_str()); }
1358 wxString
Clone() const
1360 // make a deep copy of the string, i.e. the returned string will have
1361 // ref count = 1 with refcounted implementation
1362 return wxString::FromImpl(wxStringImpl(m_impl
.c_str(), m_impl
.length()));
1365 // first valid index position
1366 const_iterator
begin() const { return const_iterator(this, m_impl
.begin()); }
1367 iterator
begin() { return iterator(this, m_impl
.begin()); }
1368 // position one after the last valid one
1369 const_iterator
end() const { return const_iterator(this, m_impl
.end()); }
1370 iterator
end() { return iterator(this, m_impl
.end()); }
1372 // first element of the reversed string
1373 const_reverse_iterator
rbegin() const
1374 { return const_reverse_iterator(end()); }
1375 reverse_iterator
rbegin()
1376 { return reverse_iterator(end()); }
1377 // one beyond the end of the reversed string
1378 const_reverse_iterator
rend() const
1379 { return const_reverse_iterator(begin()); }
1380 reverse_iterator
rend()
1381 { return reverse_iterator(begin()); }
1383 // std::string methods:
1384 #if wxUSE_UNICODE_UTF8
1385 size_t length() const
1387 #if wxUSE_STRING_POS_CACHE
1388 wxCACHE_PROFILE_FIELD_INC(lentot
);
1390 Cache::Element
* const cache
= GetCacheElement();
1392 if ( cache
->len
== npos
)
1394 // it's probably not worth trying to be clever and using cache->pos
1395 // here as it's probably 0 anyhow -- you usually call length() before
1396 // starting to index the string
1397 cache
->len
= end() - begin();
1401 wxCACHE_PROFILE_FIELD_INC(lenhits
);
1403 wxSTRING_CACHE_ASSERT( (int)cache
->len
== end() - begin() );
1407 #else // !wxUSE_STRING_POS_CACHE
1408 return end() - begin();
1409 #endif // wxUSE_STRING_POS_CACHE/!wxUSE_STRING_POS_CACHE
1412 size_t length() const { return m_impl
.length(); }
1415 size_type
size() const { return length(); }
1416 size_type
max_size() const { return npos
; }
1418 bool empty() const { return m_impl
.empty(); }
1420 // NB: these methods don't have a well-defined meaning in UTF-8 case
1421 size_type
capacity() const { return m_impl
.capacity(); }
1422 void reserve(size_t sz
) { m_impl
.reserve(sz
); }
1424 void resize(size_t nSize
, wxUniChar ch
= wxT('\0'))
1426 const size_t len
= length();
1430 #if wxUSE_UNICODE_UTF8
1433 wxSTRING_INVALIDATE_CACHE();
1435 // we can't use wxStringImpl::resize() for truncating the string as it
1436 // counts in bytes, not characters
1441 // we also can't use (presumably more efficient) resize() if we have to
1442 // append characters taking more than one byte
1443 if ( !ch
.IsAscii() )
1445 append(nSize
- len
, ch
);
1447 else // can use (presumably faster) resize() version
1448 #endif // wxUSE_UNICODE_UTF8
1450 wxSTRING_INVALIDATE_CACHED_LENGTH();
1452 m_impl
.resize(nSize
, (wxStringCharType
)ch
);
1456 wxString
substr(size_t nStart
= 0, size_t nLen
= npos
) const
1459 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
1460 return FromImpl(m_impl
.substr(pos
, len
));
1463 // generic attributes & operations
1464 // as standard strlen()
1465 size_t Len() const { return length(); }
1466 // string contains any characters?
1467 bool IsEmpty() const { return empty(); }
1468 // empty string is "false", so !str will return true
1469 bool operator!() const { return empty(); }
1470 // truncate the string to given length
1471 wxString
& Truncate(size_t uiLen
);
1472 // empty string contents
1473 void Empty() { clear(); }
1474 // empty the string and free memory
1475 void Clear() { clear(); }
1478 // Is an ascii value
1479 bool IsAscii() const;
1481 bool IsNumber() const;
1483 bool IsWord() const;
1485 // data access (all indexes are 0 based)
1487 wxUniChar
at(size_t n
) const
1488 { return wxStringOperations::DecodeChar(m_impl
.begin() + PosToImpl(n
)); }
1489 wxUniChar
GetChar(size_t n
) const
1491 // read/write access
1492 wxUniCharRef
at(size_t n
)
1493 { return *GetIterForNthChar(n
); }
1494 wxUniCharRef
GetWritableChar(size_t n
)
1497 void SetChar(size_t n
, wxUniChar ch
)
1500 // get last character
1501 wxUniChar
Last() const
1503 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1507 // get writable last character
1510 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1515 Note that we we must define all of the overloads below to avoid
1516 ambiguity when using str[0].
1518 wxUniChar
operator[](int n
) const
1520 wxUniChar
operator[](long n
) const
1522 wxUniChar
operator[](size_t n
) const
1524 #ifndef wxSIZE_T_IS_UINT
1525 wxUniChar
operator[](unsigned int n
) const
1527 #endif // size_t != unsigned int
1529 // operator versions of GetWriteableChar()
1530 wxUniCharRef
operator[](int n
)
1532 wxUniCharRef
operator[](long n
)
1534 wxUniCharRef
operator[](size_t n
)
1536 #ifndef wxSIZE_T_IS_UINT
1537 wxUniCharRef
operator[](unsigned int n
)
1539 #endif // size_t != unsigned int
1543 Overview of wxString conversions, implicit and explicit:
1545 - wxString has a std::[w]string-like c_str() method, however it does
1546 not return a C-style string directly but instead returns wxCStrData
1547 helper object which is convertible to either "char *" narrow string
1548 or "wchar_t *" wide string. Usually the correct conversion will be
1549 applied by the compiler automatically but if this doesn't happen you
1550 need to explicitly choose one using wxCStrData::AsChar() or AsWChar()
1551 methods or another wxString conversion function.
1553 - One of the places where the conversion does *NOT* happen correctly is
1554 when c_str() is passed to a vararg function such as printf() so you
1555 must *NOT* use c_str() with them. Either use wxPrintf() (all wx
1556 functions do handle c_str() correctly, even if they appear to be
1557 vararg (but they're not, really)) or add an explicit AsChar() or, if
1558 compatibility with previous wxWidgets versions is important, add a
1559 cast to "const char *".
1561 - In non-STL mode only, wxString is also implicitly convertible to
1562 wxCStrData. The same warning as above applies.
1564 - c_str() is polymorphic as it can be converted to either narrow or
1565 wide string. If you explicitly need one or the other, choose to use
1566 mb_str() (for narrow) or wc_str() (for wide) instead. Notice that
1567 these functions can return either the pointer to string directly (if
1568 this is what the string uses internally) or a temporary buffer
1569 containing the string and convertible to it. Again, conversion will
1570 usually be done automatically by the compiler but beware of the
1571 vararg functions: you need an explicit cast when using them.
1573 - There are also non-const versions of mb_str() and wc_str() called
1574 char_str() and wchar_str(). They are only meant to be used with
1575 non-const-correct functions and they always return buffers.
1577 - Finally wx_str() returns whatever string representation is used by
1578 wxString internally. It may be either a narrow or wide string
1579 depending on wxWidgets build mode but it will always be a raw pointer
1583 // explicit conversion to wxCStrData
1584 wxCStrData
c_str() const { return wxCStrData(this); }
1585 wxCStrData
data() const { return c_str(); }
1587 // implicit conversion to wxCStrData
1588 operator wxCStrData() const { return c_str(); }
1590 // the first two operators conflict with operators for conversion to
1591 // std::string and they must be disabled in STL build; the next one only
1592 // makes sense if conversions to char* are also defined and not defining it
1593 // in STL build also helps us to get more clear error messages for the code
1594 // which relies on implicit conversion to char* in STL build
1596 operator const char*() const { return c_str(); }
1597 operator const wchar_t*() const { return c_str(); }
1599 // implicit conversion to untyped pointer for compatibility with previous
1600 // wxWidgets versions: this is the same as conversion to const char * so it
1602 operator const void*() const { return c_str(); }
1605 // identical to c_str(), for MFC compatibility
1606 const wxCStrData
GetData() const { return c_str(); }
1608 // explicit conversion to C string in internal representation (char*,
1609 // wchar_t*, UTF-8-encoded char*, depending on the build):
1610 const wxStringCharType
*wx_str() const { return m_impl
.c_str(); }
1612 // conversion to *non-const* multibyte or widestring buffer; modifying
1613 // returned buffer won't affect the string, these methods are only useful
1614 // for passing values to const-incorrect functions
1615 wxWritableCharBuffer
char_str(const wxMBConv
& conv
= wxConvLibc
) const
1616 { return mb_str(conv
); }
1617 wxWritableWCharBuffer
wchar_str() const { return wc_str(); }
1619 // conversion to the buffer of the given type T (= char or wchar_t) and
1620 // also optionally return the buffer length
1622 // this is mostly/only useful for the template functions
1624 // FIXME-VC6: the second argument only exists for VC6 which doesn't support
1625 // explicit template function selection, do not use it unless
1626 // you must support VC6!
1627 template <typename T
>
1628 wxCharTypeBuffer
<T
> tchar_str(size_t *len
= NULL
,
1629 T
* WXUNUSED(dummy
) = NULL
) const
1632 // we need a helper dispatcher depending on type
1633 return wxPrivate::wxStringAsBufHelper
<T
>::Get(*this, len
);
1635 // T can only be char in ANSI build
1639 return wxCharTypeBuffer
<T
>::CreateNonOwned(wx_str());
1640 #endif // Unicode build kind
1643 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
1644 // converting numbers or strings which are certain not to contain special
1645 // chars (typically system functions, X atoms, environment variables etc.)
1647 // the behaviour of these functions with the strings containing anything
1648 // else than 7 bit ASCII characters is undefined, use at your own risk.
1650 static wxString
FromAscii(const char *ascii
, size_t len
);
1651 static wxString
FromAscii(const char *ascii
);
1652 static wxString
FromAscii(char ascii
);
1653 const wxCharBuffer
ToAscii() const;
1655 static wxString
FromAscii(const char *ascii
) { return wxString( ascii
); }
1656 static wxString
FromAscii(const char *ascii
, size_t len
)
1657 { return wxString( ascii
, len
); }
1658 static wxString
FromAscii(char ascii
) { return wxString( ascii
); }
1659 const char *ToAscii() const { return c_str(); }
1660 #endif // Unicode/!Unicode
1662 // also provide unsigned char overloads as signed/unsigned doesn't matter
1663 // for 7 bit ASCII characters
1664 static wxString
FromAscii(const unsigned char *ascii
)
1665 { return FromAscii((const char *)ascii
); }
1666 static wxString
FromAscii(const unsigned char *ascii
, size_t len
)
1667 { return FromAscii((const char *)ascii
, len
); }
1669 // conversion to/from UTF-8:
1670 #if wxUSE_UNICODE_UTF8
1671 static wxString
FromUTF8Unchecked(const char *utf8
)
1674 return wxEmptyString
;
1676 wxASSERT( wxStringOperations::IsValidUtf8String(utf8
) );
1677 return FromImpl(wxStringImpl(utf8
));
1679 static wxString
FromUTF8Unchecked(const char *utf8
, size_t len
)
1682 return wxEmptyString
;
1684 return FromUTF8Unchecked(utf8
);
1686 wxASSERT( wxStringOperations::IsValidUtf8String(utf8
, len
) );
1687 return FromImpl(wxStringImpl(utf8
, len
));
1690 static wxString
FromUTF8(const char *utf8
)
1692 if ( !utf8
|| !wxStringOperations::IsValidUtf8String(utf8
) )
1695 return FromImpl(wxStringImpl(utf8
));
1697 static wxString
FromUTF8(const char *utf8
, size_t len
)
1700 return FromUTF8(utf8
);
1702 if ( !utf8
|| !wxStringOperations::IsValidUtf8String(utf8
, len
) )
1705 return FromImpl(wxStringImpl(utf8
, len
));
1708 const char* utf8_str() const { return wx_str(); }
1709 const char* ToUTF8() const { return wx_str(); }
1711 // this function exists in UTF-8 build only and returns the length of the
1712 // internal UTF-8 representation
1713 size_t utf8_length() const { return m_impl
.length(); }
1714 #elif wxUSE_UNICODE_WCHAR
1715 static wxString
FromUTF8(const char *utf8
, size_t len
= npos
)
1716 { return wxString(utf8
, wxMBConvUTF8(), len
); }
1717 static wxString
FromUTF8Unchecked(const char *utf8
, size_t len
= npos
)
1719 const wxString
s(utf8
, wxMBConvUTF8(), len
);
1720 wxASSERT_MSG( !utf8
|| !*utf8
|| !s
.empty(),
1721 "string must be valid UTF-8" );
1724 const wxCharBuffer
utf8_str() const { return mb_str(wxMBConvUTF8()); }
1725 const wxCharBuffer
ToUTF8() const { return utf8_str(); }
1727 static wxString
FromUTF8(const char *utf8
)
1728 { return wxString(wxMBConvUTF8().cMB2WC(utf8
)); }
1729 static wxString
FromUTF8(const char *utf8
, size_t len
)
1732 wxWCharBuffer
buf(wxMBConvUTF8().cMB2WC(utf8
, len
== npos
? wxNO_LEN
: len
, &wlen
));
1733 return wxString(buf
.data(), wlen
);
1735 static wxString
FromUTF8Unchecked(const char *utf8
, size_t len
= npos
)
1738 wxWCharBuffer
buf(wxMBConvUTF8().cMB2WC(utf8
,
1739 len
== npos
? wxNO_LEN
: len
,
1741 wxASSERT_MSG( !utf8
|| !*utf8
|| wlen
,
1742 "string must be valid UTF-8" );
1744 return wxString(buf
.data(), wlen
);
1746 const wxCharBuffer
utf8_str() const
1747 { return wxMBConvUTF8().cWC2MB(wc_str()); }
1748 const wxCharBuffer
ToUTF8() const { return utf8_str(); }
1751 // functions for storing binary data in wxString:
1753 static wxString
From8BitData(const char *data
, size_t len
)
1754 { return wxString(data
, wxConvISO8859_1
, len
); }
1755 // version for NUL-terminated data:
1756 static wxString
From8BitData(const char *data
)
1757 { return wxString(data
, wxConvISO8859_1
); }
1758 const wxCharBuffer
To8BitData() const { return mb_str(wxConvISO8859_1
); }
1760 static wxString
From8BitData(const char *data
, size_t len
)
1761 { return wxString(data
, len
); }
1762 // version for NUL-terminated data:
1763 static wxString
From8BitData(const char *data
)
1764 { return wxString(data
); }
1765 const char *To8BitData() const { return c_str(); }
1766 #endif // Unicode/ANSI
1768 // conversions with (possible) format conversions: have to return a
1769 // buffer with temporary data
1771 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
1772 // return an ANSI (multibyte) string, wc_str() to return a wide string and
1773 // fn_str() to return a string which should be used with the OS APIs
1774 // accepting the file names. The return value is always the same, but the
1775 // type differs because a function may either return pointer to the buffer
1776 // directly or have to use intermediate buffer for translation.
1779 #if wxUSE_UTF8_LOCALE_ONLY
1780 const char* mb_str() const { return wx_str(); }
1781 const wxCharBuffer
mb_str(const wxMBConv
& conv
) const;
1783 const wxCharBuffer
mb_str(const wxMBConv
& conv
= wxConvLibc
) const;
1786 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
1788 #if wxUSE_UNICODE_WCHAR
1789 const wchar_t* wc_str() const { return wx_str(); }
1790 #elif wxUSE_UNICODE_UTF8
1791 const wxWCharBuffer
wc_str() const;
1793 // for compatibility with !wxUSE_UNICODE version
1794 const wxWX2WCbuf
wc_str(const wxMBConv
& WXUNUSED(conv
)) const
1795 { return wc_str(); }
1798 const wxCharBuffer
fn_str() const { return mb_str(wxConvFile
); }
1800 const wxWX2WCbuf
fn_str() const { return wc_str(); }
1801 #endif // wxMBFILES/!wxMBFILES
1804 const wxChar
* mb_str() const { return wx_str(); }
1806 // for compatibility with wxUSE_UNICODE version
1807 const char* mb_str(const wxMBConv
& WXUNUSED(conv
)) const { return wx_str(); }
1809 const wxWX2MBbuf
mbc_str() const { return mb_str(); }
1812 const wxWCharBuffer
wc_str(const wxMBConv
& conv
= wxConvLibc
) const;
1813 #endif // wxUSE_WCHAR_T
1814 const wxCharBuffer
fn_str() const { return wxConvFile
.cWC2WX( wc_str( wxConvLibc
) ); }
1815 #endif // Unicode/ANSI
1817 #if wxUSE_UNICODE_UTF8
1818 const wxWCharBuffer
t_str() const { return wc_str(); }
1819 #elif wxUSE_UNICODE_WCHAR
1820 const wchar_t* t_str() const { return wx_str(); }
1822 const char* t_str() const { return wx_str(); }
1826 // overloaded assignment
1827 // from another wxString
1828 wxString
& operator=(const wxString
& stringSrc
)
1830 if ( this != &stringSrc
)
1832 wxSTRING_INVALIDATE_CACHE();
1834 m_impl
= stringSrc
.m_impl
;
1840 wxString
& operator=(const wxCStrData
& cstr
)
1841 { return *this = cstr
.AsString(); }
1843 wxString
& operator=(wxUniChar ch
)
1845 wxSTRING_INVALIDATE_CACHE();
1847 #if wxUSE_UNICODE_UTF8
1848 if ( !ch
.IsAscii() )
1849 m_impl
= wxStringOperations::EncodeChar(ch
);
1851 #endif // wxUSE_UNICODE_UTF8
1852 m_impl
= (wxStringCharType
)ch
;
1856 wxString
& operator=(wxUniCharRef ch
)
1857 { return operator=((wxUniChar
)ch
); }
1858 wxString
& operator=(char ch
)
1859 { return operator=(wxUniChar(ch
)); }
1860 wxString
& operator=(unsigned char ch
)
1861 { return operator=(wxUniChar(ch
)); }
1862 wxString
& operator=(wchar_t ch
)
1863 { return operator=(wxUniChar(ch
)); }
1864 // from a C string - STL probably will crash on NULL,
1865 // so we need to compensate in that case
1866 #if wxUSE_STL_BASED_WXSTRING
1867 wxString
& operator=(const char *psz
)
1869 wxSTRING_INVALIDATE_CACHE();
1872 m_impl
= ImplStr(psz
);
1879 wxString
& operator=(const wchar_t *pwz
)
1881 wxSTRING_INVALIDATE_CACHE();
1884 m_impl
= ImplStr(pwz
);
1890 #else // !wxUSE_STL_BASED_WXSTRING
1891 wxString
& operator=(const char *psz
)
1893 wxSTRING_INVALIDATE_CACHE();
1895 m_impl
= ImplStr(psz
);
1900 wxString
& operator=(const wchar_t *pwz
)
1902 wxSTRING_INVALIDATE_CACHE();
1904 m_impl
= ImplStr(pwz
);
1908 #endif // wxUSE_STL_BASED_WXSTRING/!wxUSE_STL_BASED_WXSTRING
1910 wxString
& operator=(const unsigned char *psz
)
1911 { return operator=((const char*)psz
); }
1913 // from wxWCharBuffer
1914 wxString
& operator=(const wxWCharBuffer
& s
)
1915 { return operator=(s
.data()); } // FIXME-UTF8: fix for embedded NULs
1916 // from wxCharBuffer
1917 wxString
& operator=(const wxCharBuffer
& s
)
1918 { return operator=(s
.data()); } // FIXME-UTF8: fix for embedded NULs
1920 // string concatenation
1921 // in place concatenation
1923 Concatenate and return the result. Note that the left to right
1924 associativity of << allows to write things like "str << str1 << str2
1925 << ..." (unlike with +=)
1928 wxString
& operator<<(const wxString
& s
)
1930 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1931 wxASSERT_MSG( s
.IsValid(),
1932 _T("did you forget to call UngetWriteBuf()?") );
1938 // string += C string
1939 wxString
& operator<<(const char *psz
)
1940 { append(psz
); return *this; }
1941 wxString
& operator<<(const wchar_t *pwz
)
1942 { append(pwz
); return *this; }
1943 wxString
& operator<<(const wxCStrData
& psz
)
1944 { append(psz
.AsString()); return *this; }
1946 wxString
& operator<<(wxUniChar ch
) { append(1, ch
); return *this; }
1947 wxString
& operator<<(wxUniCharRef ch
) { append(1, ch
); return *this; }
1948 wxString
& operator<<(char ch
) { append(1, ch
); return *this; }
1949 wxString
& operator<<(unsigned char ch
) { append(1, ch
); return *this; }
1950 wxString
& operator<<(wchar_t ch
) { append(1, ch
); return *this; }
1952 // string += buffer (i.e. from wxGetString)
1953 wxString
& operator<<(const wxWCharBuffer
& s
)
1954 { return operator<<((const wchar_t *)s
); }
1955 wxString
& operator<<(const wxCharBuffer
& s
)
1956 { return operator<<((const char *)s
); }
1958 // string += C string
1959 wxString
& Append(const wxString
& s
)
1961 // test for empty() to share the string if possible
1968 wxString
& Append(const char* psz
)
1969 { append(psz
); return *this; }
1970 wxString
& Append(const wchar_t* pwz
)
1971 { append(pwz
); return *this; }
1972 wxString
& Append(const wxCStrData
& psz
)
1973 { append(psz
); return *this; }
1974 wxString
& Append(const wxCharBuffer
& psz
)
1975 { append(psz
); return *this; }
1976 wxString
& Append(const wxWCharBuffer
& psz
)
1977 { append(psz
); return *this; }
1978 wxString
& Append(const char* psz
, size_t nLen
)
1979 { append(psz
, nLen
); return *this; }
1980 wxString
& Append(const wchar_t* pwz
, size_t nLen
)
1981 { append(pwz
, nLen
); return *this; }
1982 wxString
& Append(const wxCStrData
& psz
, size_t nLen
)
1983 { append(psz
, nLen
); return *this; }
1984 wxString
& Append(const wxCharBuffer
& psz
, size_t nLen
)
1985 { append(psz
, nLen
); return *this; }
1986 wxString
& Append(const wxWCharBuffer
& psz
, size_t nLen
)
1987 { append(psz
, nLen
); return *this; }
1988 // append count copies of given character
1989 wxString
& Append(wxUniChar ch
, size_t count
= 1u)
1990 { append(count
, ch
); return *this; }
1991 wxString
& Append(wxUniCharRef ch
, size_t count
= 1u)
1992 { append(count
, ch
); return *this; }
1993 wxString
& Append(char ch
, size_t count
= 1u)
1994 { append(count
, ch
); return *this; }
1995 wxString
& Append(unsigned char ch
, size_t count
= 1u)
1996 { append(count
, ch
); return *this; }
1997 wxString
& Append(wchar_t ch
, size_t count
= 1u)
1998 { append(count
, ch
); return *this; }
2000 // prepend a string, return the string itself
2001 wxString
& Prepend(const wxString
& str
)
2002 { *this = str
+ *this; return *this; }
2004 // non-destructive concatenation
2006 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
,
2007 const wxString
& string2
);
2008 // string with a single char
2009 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
2010 // char with a string
2011 friend wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
2012 // string with C string
2013 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
2015 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
2016 const wchar_t *pwz
);
2017 // C string with string
2018 friend wxString WXDLLIMPEXP_BASE
operator+(const char *psz
,
2019 const wxString
& string
);
2020 friend wxString WXDLLIMPEXP_BASE
operator+(const wchar_t *pwz
,
2021 const wxString
& string
);
2023 // stream-like functions
2024 // insert an int into string
2025 wxString
& operator<<(int i
)
2026 { return (*this) << Format(_T("%d"), i
); }
2027 // insert an unsigned int into string
2028 wxString
& operator<<(unsigned int ui
)
2029 { return (*this) << Format(_T("%u"), ui
); }
2030 // insert a long into string
2031 wxString
& operator<<(long l
)
2032 { return (*this) << Format(_T("%ld"), l
); }
2033 // insert an unsigned long into string
2034 wxString
& operator<<(unsigned long ul
)
2035 { return (*this) << Format(_T("%lu"), ul
); }
2036 #if defined wxLongLong_t && !defined wxLongLongIsLong
2037 // insert a long long if they exist and aren't longs
2038 wxString
& operator<<(wxLongLong_t ll
)
2040 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("d");
2041 return (*this) << Format(fmt
, ll
);
2043 // insert an unsigned long long
2044 wxString
& operator<<(wxULongLong_t ull
)
2046 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("u");
2047 return (*this) << Format(fmt
, ull
);
2049 #endif // wxLongLong_t && !wxLongLongIsLong
2050 // insert a float into string
2051 wxString
& operator<<(float f
)
2052 { return (*this) << Format(_T("%f"), f
); }
2053 // insert a double into string
2054 wxString
& operator<<(double d
)
2055 { return (*this) << Format(_T("%g"), d
); }
2057 // string comparison
2058 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
2059 int Cmp(const char *psz
) const
2060 { return compare(psz
); }
2061 int Cmp(const wchar_t *pwz
) const
2062 { return compare(pwz
); }
2063 int Cmp(const wxString
& s
) const
2064 { return compare(s
); }
2065 int Cmp(const wxCStrData
& s
) const
2066 { return compare(s
); }
2067 int Cmp(const wxCharBuffer
& s
) const
2068 { return compare(s
); }
2069 int Cmp(const wxWCharBuffer
& s
) const
2070 { return compare(s
); }
2071 // same as Cmp() but not case-sensitive
2072 int CmpNoCase(const wxString
& s
) const;
2074 // test for the string equality, either considering case or not
2075 // (if compareWithCase then the case matters)
2076 bool IsSameAs(const wxString
& str
, bool compareWithCase
= true) const
2078 #if !wxUSE_UNICODE_UTF8
2079 // in UTF-8 build, length() is O(n) and doing this would be _slower_
2080 if ( length() != str
.length() )
2083 return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0;
2085 bool IsSameAs(const char *str
, bool compareWithCase
= true) const
2086 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
2087 bool IsSameAs(const wchar_t *str
, bool compareWithCase
= true) const
2088 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
2090 bool IsSameAs(const wxCStrData
& str
, bool compareWithCase
= true) const
2091 { return IsSameAs(str
.AsString(), compareWithCase
); }
2092 bool IsSameAs(const wxCharBuffer
& str
, bool compareWithCase
= true) const
2093 { return IsSameAs(str
.data(), compareWithCase
); }
2094 bool IsSameAs(const wxWCharBuffer
& str
, bool compareWithCase
= true) const
2095 { return IsSameAs(str
.data(), compareWithCase
); }
2096 // comparison with a single character: returns true if equal
2097 bool IsSameAs(wxUniChar c
, bool compareWithCase
= true) const;
2098 // FIXME-UTF8: remove these overloads
2099 bool IsSameAs(wxUniCharRef c
, bool compareWithCase
= true) const
2100 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
2101 bool IsSameAs(char c
, bool compareWithCase
= true) const
2102 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
2103 bool IsSameAs(unsigned char c
, bool compareWithCase
= true) const
2104 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
2105 bool IsSameAs(wchar_t c
, bool compareWithCase
= true) const
2106 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
2107 bool IsSameAs(int c
, bool compareWithCase
= true) const
2108 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
2110 // simple sub-string extraction
2111 // return substring starting at nFirst of length nCount (or till the end
2112 // if nCount = default value)
2113 wxString
Mid(size_t nFirst
, size_t nCount
= npos
) const;
2115 // operator version of Mid()
2116 wxString
operator()(size_t start
, size_t len
) const
2117 { return Mid(start
, len
); }
2119 // check if the string starts with the given prefix and return the rest
2120 // of the string in the provided pointer if it is not NULL; otherwise
2122 bool StartsWith(const wxString
& prefix
, wxString
*rest
= NULL
) const;
2123 // check if the string ends with the given suffix and return the
2124 // beginning of the string before the suffix in the provided pointer if
2125 // it is not NULL; otherwise return false
2126 bool EndsWith(const wxString
& suffix
, wxString
*rest
= NULL
) const;
2128 // get first nCount characters
2129 wxString
Left(size_t nCount
) const;
2130 // get last nCount characters
2131 wxString
Right(size_t nCount
) const;
2132 // get all characters before the first occurrence of ch
2133 // (returns the whole string if ch not found)
2134 wxString
BeforeFirst(wxUniChar ch
) const;
2135 // get all characters before the last occurrence of ch
2136 // (returns empty string if ch not found)
2137 wxString
BeforeLast(wxUniChar ch
) const;
2138 // get all characters after the first occurrence of ch
2139 // (returns empty string if ch not found)
2140 wxString
AfterFirst(wxUniChar ch
) const;
2141 // get all characters after the last occurrence of ch
2142 // (returns the whole string if ch not found)
2143 wxString
AfterLast(wxUniChar ch
) const;
2145 // for compatibility only, use more explicitly named functions above
2146 wxString
Before(wxUniChar ch
) const { return BeforeLast(ch
); }
2147 wxString
After(wxUniChar ch
) const { return AfterFirst(ch
); }
2150 // convert to upper case in place, return the string itself
2151 wxString
& MakeUpper();
2152 // convert to upper case, return the copy of the string
2153 wxString
Upper() const { return wxString(*this).MakeUpper(); }
2154 // convert to lower case in place, return the string itself
2155 wxString
& MakeLower();
2156 // convert to lower case, return the copy of the string
2157 wxString
Lower() const { return wxString(*this).MakeLower(); }
2158 // convert the first character to the upper case and the rest to the
2159 // lower one, return the modified string itself
2160 wxString
& MakeCapitalized();
2161 // convert the first character to the upper case and the rest to the
2162 // lower one, return the copy of the string
2163 wxString
Capitalize() const { return wxString(*this).MakeCapitalized(); }
2165 // trimming/padding whitespace (either side) and truncating
2166 // remove spaces from left or from right (default) side
2167 wxString
& Trim(bool bFromRight
= true);
2168 // add nCount copies chPad in the beginning or at the end (default)
2169 wxString
& Pad(size_t nCount
, wxUniChar chPad
= wxT(' '), bool bFromRight
= true);
2171 // searching and replacing
2172 // searching (return starting index, or -1 if not found)
2173 int Find(wxUniChar ch
, bool bFromEnd
= false) const; // like strchr/strrchr
2174 int Find(wxUniCharRef ch
, bool bFromEnd
= false) const
2175 { return Find(wxUniChar(ch
), bFromEnd
); }
2176 int Find(char ch
, bool bFromEnd
= false) const
2177 { return Find(wxUniChar(ch
), bFromEnd
); }
2178 int Find(unsigned char ch
, bool bFromEnd
= false) const
2179 { return Find(wxUniChar(ch
), bFromEnd
); }
2180 int Find(wchar_t ch
, bool bFromEnd
= false) const
2181 { return Find(wxUniChar(ch
), bFromEnd
); }
2182 // searching (return starting index, or -1 if not found)
2183 int Find(const wxString
& sub
) const // like strstr
2185 size_type idx
= find(sub
);
2186 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
2188 int Find(const char *sub
) const // like strstr
2190 size_type idx
= find(sub
);
2191 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
2193 int Find(const wchar_t *sub
) const // like strstr
2195 size_type idx
= find(sub
);
2196 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
2199 int Find(const wxCStrData
& sub
) const
2200 { return Find(sub
.AsString()); }
2201 int Find(const wxCharBuffer
& sub
) const
2202 { return Find(sub
.data()); }
2203 int Find(const wxWCharBuffer
& sub
) const
2204 { return Find(sub
.data()); }
2206 // replace first (or all of bReplaceAll) occurrences of substring with
2207 // another string, returns the number of replacements made
2208 size_t Replace(const wxString
& strOld
,
2209 const wxString
& strNew
,
2210 bool bReplaceAll
= true);
2212 // check if the string contents matches a mask containing '*' and '?'
2213 bool Matches(const wxString
& mask
) const;
2215 // conversion to numbers: all functions return true only if the whole
2216 // string is a number and put the value of this number into the pointer
2217 // provided, the base is the numeric base in which the conversion should be
2218 // done and must be comprised between 2 and 36 or be 0 in which case the
2219 // standard C rules apply (leading '0' => octal, "0x" => hex)
2220 // convert to a signed integer
2221 bool ToLong(long *val
, int base
= 10) const;
2222 // convert to an unsigned integer
2223 bool ToULong(unsigned long *val
, int base
= 10) const;
2224 // convert to wxLongLong
2225 #if defined(wxLongLong_t)
2226 bool ToLongLong(wxLongLong_t
*val
, int base
= 10) const;
2227 // convert to wxULongLong
2228 bool ToULongLong(wxULongLong_t
*val
, int base
= 10) const;
2229 #endif // wxLongLong_t
2230 // convert to a double
2231 bool ToDouble(double *val
) const;
2234 // conversions to numbers using C locale
2235 // convert to a signed integer
2236 bool ToCLong(long *val
, int base
= 10) const;
2237 // convert to an unsigned integer
2238 bool ToCULong(unsigned long *val
, int base
= 10) const;
2239 // convert to a double
2240 bool ToCDouble(double *val
) const;
2243 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2244 // formatted input/output
2245 // as sprintf(), returns the number of characters written or < 0 on error
2246 // (take 'this' into account in attribute parameter count)
2247 // int Printf(const wxString& format, ...);
2248 WX_DEFINE_VARARG_FUNC(int, Printf
, 1, (const wxFormatString
&),
2249 DoPrintfWchar
, DoPrintfUtf8
)
2251 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
2252 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wxString
&),
2253 (wxFormatString(f1
)));
2254 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wxCStrData
&),
2255 (wxFormatString(f1
)));
2256 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const char*),
2257 (wxFormatString(f1
)));
2258 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wchar_t*),
2259 (wxFormatString(f1
)));
2261 #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN
2262 // as vprintf(), returns the number of characters written or < 0 on error
2263 int PrintfV(const wxString
& format
, va_list argptr
);
2265 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2266 // returns the string containing the result of Printf() to it
2267 // static wxString Format(const wxString& format, ...) WX_ATTRIBUTE_PRINTF_1;
2268 WX_DEFINE_VARARG_FUNC(static wxString
, Format
, 1, (const wxFormatString
&),
2269 DoFormatWchar
, DoFormatUtf8
)
2271 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
2272 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wxString
&),
2273 (wxFormatString(f1
)));
2274 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wxCStrData
&),
2275 (wxFormatString(f1
)));
2276 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const char*),
2277 (wxFormatString(f1
)));
2278 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wchar_t*),
2279 (wxFormatString(f1
)));
2282 // the same as above, but takes a va_list
2283 static wxString
FormatV(const wxString
& format
, va_list argptr
);
2285 // raw access to string memory
2286 // ensure that string has space for at least nLen characters
2287 // only works if the data of this string is not shared
2288 bool Alloc(size_t nLen
) { reserve(nLen
); return capacity() >= nLen
; }
2289 // minimize the string's memory
2290 // only works if the data of this string is not shared
2292 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2293 // These are deprecated, use wxStringBuffer or wxStringBufferLength instead
2295 // get writable buffer of at least nLen bytes. Unget() *must* be called
2296 // a.s.a.p. to put string back in a reasonable state!
2297 wxDEPRECATED( wxStringCharType
*GetWriteBuf(size_t nLen
) );
2298 // call this immediately after GetWriteBuf() has been used
2299 wxDEPRECATED( void UngetWriteBuf() );
2300 wxDEPRECATED( void UngetWriteBuf(size_t nLen
) );
2301 #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && wxUSE_UNICODE_UTF8
2303 // wxWidgets version 1 compatibility functions
2306 wxString
SubString(size_t from
, size_t to
) const
2307 { return Mid(from
, (to
- from
+ 1)); }
2308 // values for second parameter of CompareTo function
2309 enum caseCompare
{exact
, ignoreCase
};
2310 // values for first parameter of Strip function
2311 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
2313 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2315 // (take 'this' into account in attribute parameter count)
2316 // int sprintf(const wxString& format, ...) WX_ATTRIBUTE_PRINTF_2;
2317 WX_DEFINE_VARARG_FUNC(int, sprintf
, 1, (const wxFormatString
&),
2318 DoPrintfWchar
, DoPrintfUtf8
)
2320 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
2321 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wxString
&),
2322 (wxFormatString(f1
)));
2323 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wxCStrData
&),
2324 (wxFormatString(f1
)));
2325 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const char*),
2326 (wxFormatString(f1
)));
2327 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wchar_t*),
2328 (wxFormatString(f1
)));
2330 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
2333 int CompareTo(const wxChar
* psz
, caseCompare cmp
= exact
) const
2334 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
2337 size_t Length() const { return length(); }
2338 // Count the number of characters
2339 int Freq(wxUniChar ch
) const;
2341 void LowerCase() { MakeLower(); }
2343 void UpperCase() { MakeUpper(); }
2344 // use Trim except that it doesn't change this string
2345 wxString
Strip(stripType w
= trailing
) const;
2347 // use Find (more general variants not yet supported)
2348 size_t Index(const wxChar
* psz
) const { return Find(psz
); }
2349 size_t Index(wxUniChar ch
) const { return Find(ch
); }
2351 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
2352 wxString
& RemoveLast(size_t n
= 1) { return Truncate(length() - n
); }
2354 wxString
& Remove(size_t nStart
, size_t nLen
)
2355 { return (wxString
&)erase( nStart
, nLen
); }
2358 int First( wxUniChar ch
) const { return Find(ch
); }
2359 int First( wxUniCharRef ch
) const { return Find(ch
); }
2360 int First( char ch
) const { return Find(ch
); }
2361 int First( unsigned char ch
) const { return Find(ch
); }
2362 int First( wchar_t ch
) const { return Find(ch
); }
2363 int First( const wxString
& str
) const { return Find(str
); }
2364 int Last( wxUniChar ch
) const { return Find(ch
, true); }
2365 bool Contains(const wxString
& str
) const { return Find(str
) != wxNOT_FOUND
; }
2368 bool IsNull() const { return empty(); }
2370 // std::string compatibility functions
2372 // take nLen chars starting at nPos
2373 wxString(const wxString
& str
, size_t nPos
, size_t nLen
)
2374 { assign(str
, nPos
, nLen
); }
2375 // take all characters from first to last
2376 wxString(const_iterator first
, const_iterator last
)
2377 : m_impl(first
.impl(), last
.impl()) { }
2378 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2379 // the 2 overloads below are for compatibility with the existing code using
2380 // pointers instead of iterators
2381 wxString(const char *first
, const char *last
)
2383 SubstrBufFromMB
str(ImplStr(first
, last
- first
));
2384 m_impl
.assign(str
.data
, str
.len
);
2386 wxString(const wchar_t *first
, const wchar_t *last
)
2388 SubstrBufFromWC
str(ImplStr(first
, last
- first
));
2389 m_impl
.assign(str
.data
, str
.len
);
2391 // and this one is needed to compile code adding offsets to c_str() result
2392 wxString(const wxCStrData
& first
, const wxCStrData
& last
)
2393 : m_impl(CreateConstIterator(first
).impl(),
2394 CreateConstIterator(last
).impl())
2396 wxASSERT_MSG( first
.m_str
== last
.m_str
,
2397 _T("pointers must be into the same string") );
2399 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2401 // lib.string.modifiers
2402 // append elements str[pos], ..., str[pos+n]
2403 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
2405 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2408 str
.PosLenToImpl(pos
, n
, &from
, &len
);
2409 m_impl
.append(str
.m_impl
, from
, len
);
2413 wxString
& append(const wxString
& str
)
2415 wxSTRING_UPDATE_CACHED_LENGTH(str
.length());
2417 m_impl
.append(str
.m_impl
);
2421 // append first n (or all if n == npos) characters of sz
2422 wxString
& append(const char *sz
)
2424 wxSTRING_INVALIDATE_CACHED_LENGTH();
2426 m_impl
.append(ImplStr(sz
));
2430 wxString
& append(const wchar_t *sz
)
2432 wxSTRING_INVALIDATE_CACHED_LENGTH();
2434 m_impl
.append(ImplStr(sz
));
2438 wxString
& append(const char *sz
, size_t n
)
2440 wxSTRING_INVALIDATE_CACHED_LENGTH();
2442 SubstrBufFromMB
str(ImplStr(sz
, n
));
2443 m_impl
.append(str
.data
, str
.len
);
2446 wxString
& append(const wchar_t *sz
, size_t n
)
2448 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2450 SubstrBufFromWC
str(ImplStr(sz
, n
));
2451 m_impl
.append(str
.data
, str
.len
);
2455 wxString
& append(const wxCStrData
& str
)
2456 { return append(str
.AsString()); }
2457 wxString
& append(const wxCharBuffer
& str
)
2458 { return append(str
.data()); }
2459 wxString
& append(const wxWCharBuffer
& str
)
2460 { return append(str
.data()); }
2461 wxString
& append(const wxCStrData
& str
, size_t n
)
2462 { return append(str
.AsString(), 0, n
); }
2463 wxString
& append(const wxCharBuffer
& str
, size_t n
)
2464 { return append(str
.data(), n
); }
2465 wxString
& append(const wxWCharBuffer
& str
, size_t n
)
2466 { return append(str
.data(), n
); }
2468 // append n copies of ch
2469 wxString
& append(size_t n
, wxUniChar ch
)
2471 #if wxUSE_UNICODE_UTF8
2472 if ( !ch
.IsAscii() )
2474 wxSTRING_INVALIDATE_CACHED_LENGTH();
2476 m_impl
.append(wxStringOperations::EncodeNChars(n
, ch
));
2481 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2483 m_impl
.append(n
, (wxStringCharType
)ch
);
2489 wxString
& append(size_t n
, wxUniCharRef ch
)
2490 { return append(n
, wxUniChar(ch
)); }
2491 wxString
& append(size_t n
, char ch
)
2492 { return append(n
, wxUniChar(ch
)); }
2493 wxString
& append(size_t n
, unsigned char ch
)
2494 { return append(n
, wxUniChar(ch
)); }
2495 wxString
& append(size_t n
, wchar_t ch
)
2496 { return append(n
, wxUniChar(ch
)); }
2498 // append from first to last
2499 wxString
& append(const_iterator first
, const_iterator last
)
2501 wxSTRING_INVALIDATE_CACHED_LENGTH();
2503 m_impl
.append(first
.impl(), last
.impl());
2506 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2507 wxString
& append(const char *first
, const char *last
)
2508 { return append(first
, last
- first
); }
2509 wxString
& append(const wchar_t *first
, const wchar_t *last
)
2510 { return append(first
, last
- first
); }
2511 wxString
& append(const wxCStrData
& first
, const wxCStrData
& last
)
2512 { return append(CreateConstIterator(first
), CreateConstIterator(last
)); }
2513 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2515 // same as `this_string = str'
2516 wxString
& assign(const wxString
& str
)
2518 wxSTRING_SET_CACHED_LENGTH(str
.length());
2520 m_impl
= str
.m_impl
;
2525 wxString
& assign(const wxString
& str
, size_t len
)
2527 wxSTRING_SET_CACHED_LENGTH(len
);
2529 m_impl
.assign(str
.m_impl
, 0, str
.LenToImpl(len
));
2534 // same as ` = str[pos..pos + n]
2535 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
2538 str
.PosLenToImpl(pos
, n
, &from
, &len
);
2539 m_impl
.assign(str
.m_impl
, from
, len
);
2541 // it's important to call this after PosLenToImpl() above in case str is
2542 // the same string as this one
2543 wxSTRING_SET_CACHED_LENGTH(n
);
2548 // same as `= first n (or all if n == npos) characters of sz'
2549 wxString
& assign(const char *sz
)
2551 wxSTRING_INVALIDATE_CACHE();
2553 m_impl
.assign(ImplStr(sz
));
2558 wxString
& assign(const wchar_t *sz
)
2560 wxSTRING_INVALIDATE_CACHE();
2562 m_impl
.assign(ImplStr(sz
));
2567 wxString
& assign(const char *sz
, size_t n
)
2569 wxSTRING_SET_CACHED_LENGTH(n
);
2571 SubstrBufFromMB
str(ImplStr(sz
, n
));
2572 m_impl
.assign(str
.data
, str
.len
);
2577 wxString
& assign(const wchar_t *sz
, size_t n
)
2579 wxSTRING_SET_CACHED_LENGTH(n
);
2581 SubstrBufFromWC
str(ImplStr(sz
, n
));
2582 m_impl
.assign(str
.data
, str
.len
);
2587 wxString
& assign(const wxCStrData
& str
)
2588 { return assign(str
.AsString()); }
2589 wxString
& assign(const wxCharBuffer
& str
)
2590 { return assign(str
.data()); }
2591 wxString
& assign(const wxWCharBuffer
& str
)
2592 { return assign(str
.data()); }
2593 wxString
& assign(const wxCStrData
& str
, size_t len
)
2594 { return assign(str
.AsString(), len
); }
2595 wxString
& assign(const wxCharBuffer
& str
, size_t len
)
2596 { return assign(str
.data(), len
); }
2597 wxString
& assign(const wxWCharBuffer
& str
, size_t len
)
2598 { return assign(str
.data(), len
); }
2600 // same as `= n copies of ch'
2601 wxString
& assign(size_t n
, wxUniChar ch
)
2603 wxSTRING_SET_CACHED_LENGTH(n
);
2605 #if wxUSE_UNICODE_UTF8
2606 if ( !ch
.IsAscii() )
2607 m_impl
.assign(wxStringOperations::EncodeNChars(n
, ch
));
2610 m_impl
.assign(n
, (wxStringCharType
)ch
);
2615 wxString
& assign(size_t n
, wxUniCharRef ch
)
2616 { return assign(n
, wxUniChar(ch
)); }
2617 wxString
& assign(size_t n
, char ch
)
2618 { return assign(n
, wxUniChar(ch
)); }
2619 wxString
& assign(size_t n
, unsigned char ch
)
2620 { return assign(n
, wxUniChar(ch
)); }
2621 wxString
& assign(size_t n
, wchar_t ch
)
2622 { return assign(n
, wxUniChar(ch
)); }
2624 // assign from first to last
2625 wxString
& assign(const_iterator first
, const_iterator last
)
2627 wxSTRING_INVALIDATE_CACHE();
2629 m_impl
.assign(first
.impl(), last
.impl());
2633 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2634 wxString
& assign(const char *first
, const char *last
)
2635 { return assign(first
, last
- first
); }
2636 wxString
& assign(const wchar_t *first
, const wchar_t *last
)
2637 { return assign(first
, last
- first
); }
2638 wxString
& assign(const wxCStrData
& first
, const wxCStrData
& last
)
2639 { return assign(CreateConstIterator(first
), CreateConstIterator(last
)); }
2640 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2642 // string comparison
2643 int compare(const wxString
& str
) const;
2644 int compare(const char* sz
) const;
2645 int compare(const wchar_t* sz
) const;
2646 int compare(const wxCStrData
& str
) const
2647 { return compare(str
.AsString()); }
2648 int compare(const wxCharBuffer
& str
) const
2649 { return compare(str
.data()); }
2650 int compare(const wxWCharBuffer
& str
) const
2651 { return compare(str
.data()); }
2652 // comparison with a substring
2653 int compare(size_t nStart
, size_t nLen
, const wxString
& str
) const;
2654 // comparison of 2 substrings
2655 int compare(size_t nStart
, size_t nLen
,
2656 const wxString
& str
, size_t nStart2
, size_t nLen2
) const;
2657 // substring comparison with first nCount characters of sz
2658 int compare(size_t nStart
, size_t nLen
,
2659 const char* sz
, size_t nCount
= npos
) const;
2660 int compare(size_t nStart
, size_t nLen
,
2661 const wchar_t* sz
, size_t nCount
= npos
) const;
2663 // insert another string
2664 wxString
& insert(size_t nPos
, const wxString
& str
)
2665 { insert(GetIterForNthChar(nPos
), str
.begin(), str
.end()); return *this; }
2666 // insert n chars of str starting at nStart (in str)
2667 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
2669 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2672 str
.PosLenToImpl(nStart
, n
, &from
, &len
);
2673 m_impl
.insert(PosToImpl(nPos
), str
.m_impl
, from
, len
);
2678 // insert first n (or all if n == npos) characters of sz
2679 wxString
& insert(size_t nPos
, const char *sz
)
2681 wxSTRING_INVALIDATE_CACHE();
2683 m_impl
.insert(PosToImpl(nPos
), ImplStr(sz
));
2688 wxString
& insert(size_t nPos
, const wchar_t *sz
)
2690 wxSTRING_INVALIDATE_CACHE();
2692 m_impl
.insert(PosToImpl(nPos
), ImplStr(sz
)); return *this;
2695 wxString
& insert(size_t nPos
, const char *sz
, size_t n
)
2697 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2699 SubstrBufFromMB
str(ImplStr(sz
, n
));
2700 m_impl
.insert(PosToImpl(nPos
), str
.data
, str
.len
);
2705 wxString
& insert(size_t nPos
, const wchar_t *sz
, size_t n
)
2707 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2709 SubstrBufFromWC
str(ImplStr(sz
, n
));
2710 m_impl
.insert(PosToImpl(nPos
), str
.data
, str
.len
);
2715 // insert n copies of ch
2716 wxString
& insert(size_t nPos
, size_t n
, wxUniChar ch
)
2718 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2720 #if wxUSE_UNICODE_UTF8
2721 if ( !ch
.IsAscii() )
2722 m_impl
.insert(PosToImpl(nPos
), wxStringOperations::EncodeNChars(n
, ch
));
2725 m_impl
.insert(PosToImpl(nPos
), n
, (wxStringCharType
)ch
);
2729 iterator
insert(iterator it
, wxUniChar ch
)
2731 wxSTRING_UPDATE_CACHED_LENGTH(1);
2733 #if wxUSE_UNICODE_UTF8
2734 if ( !ch
.IsAscii() )
2736 size_t pos
= IterToImplPos(it
);
2737 m_impl
.insert(pos
, wxStringOperations::EncodeChar(ch
));
2738 return iterator(this, m_impl
.begin() + pos
);
2742 return iterator(this, m_impl
.insert(it
.impl(), (wxStringCharType
)ch
));
2745 void insert(iterator it
, const_iterator first
, const_iterator last
)
2747 wxSTRING_INVALIDATE_CACHE();
2749 m_impl
.insert(it
.impl(), first
.impl(), last
.impl());
2752 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2753 void insert(iterator it
, const char *first
, const char *last
)
2754 { insert(it
- begin(), first
, last
- first
); }
2755 void insert(iterator it
, const wchar_t *first
, const wchar_t *last
)
2756 { insert(it
- begin(), first
, last
- first
); }
2757 void insert(iterator it
, const wxCStrData
& first
, const wxCStrData
& last
)
2758 { insert(it
, CreateConstIterator(first
), CreateConstIterator(last
)); }
2759 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2761 void insert(iterator it
, size_type n
, wxUniChar ch
)
2763 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2765 #if wxUSE_UNICODE_UTF8
2766 if ( !ch
.IsAscii() )
2767 m_impl
.insert(IterToImplPos(it
), wxStringOperations::EncodeNChars(n
, ch
));
2770 m_impl
.insert(it
.impl(), n
, (wxStringCharType
)ch
);
2773 // delete characters from nStart to nStart + nLen
2774 wxString
& erase(size_type pos
= 0, size_type n
= npos
)
2776 wxSTRING_INVALIDATE_CACHE();
2779 PosLenToImpl(pos
, n
, &from
, &len
);
2780 m_impl
.erase(from
, len
);
2785 // delete characters from first up to last
2786 iterator
erase(iterator first
, iterator last
)
2788 wxSTRING_INVALIDATE_CACHE();
2790 return iterator(this, m_impl
.erase(first
.impl(), last
.impl()));
2793 iterator
erase(iterator first
)
2795 wxSTRING_UPDATE_CACHED_LENGTH(-1);
2797 return iterator(this, m_impl
.erase(first
.impl()));
2800 #ifdef wxSTRING_BASE_HASNT_CLEAR
2801 void clear() { erase(); }
2805 wxSTRING_SET_CACHED_LENGTH(0);
2811 // replaces the substring of length nLen starting at nStart
2812 wxString
& replace(size_t nStart
, size_t nLen
, const char* sz
)
2814 wxSTRING_INVALIDATE_CACHE();
2817 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2818 m_impl
.replace(from
, len
, ImplStr(sz
));
2823 wxString
& replace(size_t nStart
, size_t nLen
, const wchar_t* sz
)
2825 wxSTRING_INVALIDATE_CACHE();
2828 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2829 m_impl
.replace(from
, len
, ImplStr(sz
));
2834 // replaces the substring of length nLen starting at nStart
2835 wxString
& replace(size_t nStart
, size_t nLen
, const wxString
& str
)
2837 wxSTRING_INVALIDATE_CACHE();
2840 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2841 m_impl
.replace(from
, len
, str
.m_impl
);
2846 // replaces the substring with nCount copies of ch
2847 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxUniChar ch
)
2849 wxSTRING_INVALIDATE_CACHE();
2852 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2853 #if wxUSE_UNICODE_UTF8
2854 if ( !ch
.IsAscii() )
2855 m_impl
.replace(from
, len
, wxStringOperations::EncodeNChars(nCount
, ch
));
2858 m_impl
.replace(from
, len
, nCount
, (wxStringCharType
)ch
);
2863 // replaces a substring with another substring
2864 wxString
& replace(size_t nStart
, size_t nLen
,
2865 const wxString
& str
, size_t nStart2
, size_t nLen2
)
2867 wxSTRING_INVALIDATE_CACHE();
2870 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2873 str
.PosLenToImpl(nStart2
, nLen2
, &from2
, &len2
);
2875 m_impl
.replace(from
, len
, str
.m_impl
, from2
, len2
);
2880 // replaces the substring with first nCount chars of sz
2881 wxString
& replace(size_t nStart
, size_t nLen
,
2882 const char* sz
, size_t nCount
)
2884 wxSTRING_INVALIDATE_CACHE();
2887 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2889 SubstrBufFromMB
str(ImplStr(sz
, nCount
));
2891 m_impl
.replace(from
, len
, str
.data
, str
.len
);
2896 wxString
& replace(size_t nStart
, size_t nLen
,
2897 const wchar_t* sz
, size_t nCount
)
2899 wxSTRING_INVALIDATE_CACHE();
2902 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2904 SubstrBufFromWC
str(ImplStr(sz
, nCount
));
2906 m_impl
.replace(from
, len
, str
.data
, str
.len
);
2911 wxString
& replace(size_t nStart
, size_t nLen
,
2912 const wxString
& s
, size_t nCount
)
2914 wxSTRING_INVALIDATE_CACHE();
2917 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2918 m_impl
.replace(from
, len
, s
.m_impl
.c_str(), s
.LenToImpl(nCount
));
2923 wxString
& replace(iterator first
, iterator last
, const char* s
)
2925 wxSTRING_INVALIDATE_CACHE();
2927 m_impl
.replace(first
.impl(), last
.impl(), ImplStr(s
));
2932 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
)
2934 wxSTRING_INVALIDATE_CACHE();
2936 m_impl
.replace(first
.impl(), last
.impl(), ImplStr(s
));
2941 wxString
& replace(iterator first
, iterator last
, const char* s
, size_type n
)
2943 wxSTRING_INVALIDATE_CACHE();
2945 SubstrBufFromMB
str(ImplStr(s
, n
));
2946 m_impl
.replace(first
.impl(), last
.impl(), str
.data
, str
.len
);
2951 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
, size_type n
)
2953 wxSTRING_INVALIDATE_CACHE();
2955 SubstrBufFromWC
str(ImplStr(s
, n
));
2956 m_impl
.replace(first
.impl(), last
.impl(), str
.data
, str
.len
);
2961 wxString
& replace(iterator first
, iterator last
, const wxString
& s
)
2963 wxSTRING_INVALIDATE_CACHE();
2965 m_impl
.replace(first
.impl(), last
.impl(), s
.m_impl
);
2970 wxString
& replace(iterator first
, iterator last
, size_type n
, wxUniChar ch
)
2972 wxSTRING_INVALIDATE_CACHE();
2974 #if wxUSE_UNICODE_UTF8
2975 if ( !ch
.IsAscii() )
2976 m_impl
.replace(first
.impl(), last
.impl(),
2977 wxStringOperations::EncodeNChars(n
, ch
));
2980 m_impl
.replace(first
.impl(), last
.impl(), n
, (wxStringCharType
)ch
);
2985 wxString
& replace(iterator first
, iterator last
,
2986 const_iterator first1
, const_iterator last1
)
2988 wxSTRING_INVALIDATE_CACHE();
2990 m_impl
.replace(first
.impl(), last
.impl(), first1
.impl(), last1
.impl());
2995 wxString
& replace(iterator first
, iterator last
,
2996 const char *first1
, const char *last1
)
2997 { replace(first
, last
, first1
, last1
- first1
); return *this; }
2998 wxString
& replace(iterator first
, iterator last
,
2999 const wchar_t *first1
, const wchar_t *last1
)
3000 { replace(first
, last
, first1
, last1
- first1
); return *this; }
3003 void swap(wxString
& str
)
3005 #if wxUSE_STRING_POS_CACHE
3006 // we modify not only this string but also the other one directly so we
3007 // need to invalidate cache for both of them (we could also try to
3008 // exchange their cache entries but it seems unlikely to be worth it)
3010 str
.InvalidateCache();
3011 #endif // wxUSE_STRING_POS_CACHE
3013 m_impl
.swap(str
.m_impl
);
3017 size_t find(const wxString
& str
, size_t nStart
= 0) const
3018 { return PosFromImpl(m_impl
.find(str
.m_impl
, PosToImpl(nStart
))); }
3020 // find first n characters of sz
3021 size_t find(const char* sz
, size_t nStart
= 0, size_t n
= npos
) const
3023 SubstrBufFromMB
str(ImplStr(sz
, n
));
3024 return PosFromImpl(m_impl
.find(str
.data
, PosToImpl(nStart
), str
.len
));
3026 size_t find(const wchar_t* sz
, size_t nStart
= 0, size_t n
= npos
) const
3028 SubstrBufFromWC
str(ImplStr(sz
, n
));
3029 return PosFromImpl(m_impl
.find(str
.data
, PosToImpl(nStart
), str
.len
));
3031 size_t find(const wxCharBuffer
& s
, size_t nStart
= 0, size_t n
= npos
) const
3032 { return find(s
.data(), nStart
, n
); }
3033 size_t find(const wxWCharBuffer
& s
, size_t nStart
= 0, size_t n
= npos
) const
3034 { return find(s
.data(), nStart
, n
); }
3035 size_t find(const wxCStrData
& s
, size_t nStart
= 0, size_t n
= npos
) const
3036 { return find(s
.AsWChar(), nStart
, n
); }
3038 // find the first occurrence of character ch after nStart
3039 size_t find(wxUniChar ch
, size_t nStart
= 0) const
3041 #if wxUSE_UNICODE_UTF8
3042 if ( !ch
.IsAscii() )
3043 return PosFromImpl(m_impl
.find(wxStringOperations::EncodeChar(ch
),
3044 PosToImpl(nStart
)));
3047 return PosFromImpl(m_impl
.find((wxStringCharType
)ch
,
3048 PosToImpl(nStart
)));
3051 size_t find(wxUniCharRef ch
, size_t nStart
= 0) const
3052 { return find(wxUniChar(ch
), nStart
); }
3053 size_t find(char ch
, size_t nStart
= 0) const
3054 { return find(wxUniChar(ch
), nStart
); }
3055 size_t find(unsigned char ch
, size_t nStart
= 0) const
3056 { return find(wxUniChar(ch
), nStart
); }
3057 size_t find(wchar_t ch
, size_t nStart
= 0) const
3058 { return find(wxUniChar(ch
), nStart
); }
3060 // rfind() family is exactly like find() but works right to left
3062 // as find, but from the end
3063 size_t rfind(const wxString
& str
, size_t nStart
= npos
) const
3064 { return PosFromImpl(m_impl
.rfind(str
.m_impl
, PosToImpl(nStart
))); }
3066 // as find, but from the end
3067 size_t rfind(const char* sz
, size_t nStart
= npos
, size_t n
= npos
) const
3069 SubstrBufFromMB
str(ImplStr(sz
, n
));
3070 return PosFromImpl(m_impl
.rfind(str
.data
, PosToImpl(nStart
), str
.len
));
3072 size_t rfind(const wchar_t* sz
, size_t nStart
= npos
, size_t n
= npos
) const
3074 SubstrBufFromWC
str(ImplStr(sz
, n
));
3075 return PosFromImpl(m_impl
.rfind(str
.data
, PosToImpl(nStart
), str
.len
));
3077 size_t rfind(const wxCharBuffer
& s
, size_t nStart
= npos
, size_t n
= npos
) const
3078 { return rfind(s
.data(), nStart
, n
); }
3079 size_t rfind(const wxWCharBuffer
& s
, size_t nStart
= npos
, size_t n
= npos
) const
3080 { return rfind(s
.data(), nStart
, n
); }
3081 size_t rfind(const wxCStrData
& s
, size_t nStart
= npos
, size_t n
= npos
) const
3082 { return rfind(s
.AsWChar(), nStart
, n
); }
3083 // as find, but from the end
3084 size_t rfind(wxUniChar ch
, size_t nStart
= npos
) const
3086 #if wxUSE_UNICODE_UTF8
3087 if ( !ch
.IsAscii() )
3088 return PosFromImpl(m_impl
.rfind(wxStringOperations::EncodeChar(ch
),
3089 PosToImpl(nStart
)));
3092 return PosFromImpl(m_impl
.rfind((wxStringCharType
)ch
,
3093 PosToImpl(nStart
)));
3095 size_t rfind(wxUniCharRef ch
, size_t nStart
= npos
) const
3096 { return rfind(wxUniChar(ch
), nStart
); }
3097 size_t rfind(char ch
, size_t nStart
= npos
) const
3098 { return rfind(wxUniChar(ch
), nStart
); }
3099 size_t rfind(unsigned char ch
, size_t nStart
= npos
) const
3100 { return rfind(wxUniChar(ch
), nStart
); }
3101 size_t rfind(wchar_t ch
, size_t nStart
= npos
) const
3102 { return rfind(wxUniChar(ch
), nStart
); }
3104 // find first/last occurrence of any character (not) in the set:
3105 #if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
3106 // FIXME-UTF8: this is not entirely correct, because it doesn't work if
3107 // sizeof(wchar_t)==2 and surrogates are present in the string;
3108 // should we care? Probably not.
3109 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
3110 { return m_impl
.find_first_of(str
.m_impl
, nStart
); }
3111 size_t find_first_of(const char* sz
, size_t nStart
= 0) const
3112 { return m_impl
.find_first_of(ImplStr(sz
), nStart
); }
3113 size_t find_first_of(const wchar_t* sz
, size_t nStart
= 0) const
3114 { return m_impl
.find_first_of(ImplStr(sz
), nStart
); }
3115 size_t find_first_of(const char* sz
, size_t nStart
, size_t n
) const
3116 { return m_impl
.find_first_of(ImplStr(sz
), nStart
, n
); }
3117 size_t find_first_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
3118 { return m_impl
.find_first_of(ImplStr(sz
), nStart
, n
); }
3119 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const
3120 { return m_impl
.find_first_of((wxChar
)c
, nStart
); }
3122 size_t find_last_of(const wxString
& str
, size_t nStart
= npos
) const
3123 { return m_impl
.find_last_of(str
.m_impl
, nStart
); }
3124 size_t find_last_of(const char* sz
, size_t nStart
= npos
) const
3125 { return m_impl
.find_last_of(ImplStr(sz
), nStart
); }
3126 size_t find_last_of(const wchar_t* sz
, size_t nStart
= npos
) const
3127 { return m_impl
.find_last_of(ImplStr(sz
), nStart
); }
3128 size_t find_last_of(const char* sz
, size_t nStart
, size_t n
) const
3129 { return m_impl
.find_last_of(ImplStr(sz
), nStart
, n
); }
3130 size_t find_last_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
3131 { return m_impl
.find_last_of(ImplStr(sz
), nStart
, n
); }
3132 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const
3133 { return m_impl
.find_last_of((wxChar
)c
, nStart
); }
3135 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
3136 { return m_impl
.find_first_not_of(str
.m_impl
, nStart
); }
3137 size_t find_first_not_of(const char* sz
, size_t nStart
= 0) const
3138 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
); }
3139 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
= 0) const
3140 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
); }
3141 size_t find_first_not_of(const char* sz
, size_t nStart
, size_t n
) const
3142 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
, n
); }
3143 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
3144 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
, n
); }
3145 size_t find_first_not_of(wxUniChar c
, size_t nStart
= 0) const
3146 { return m_impl
.find_first_not_of((wxChar
)c
, nStart
); }
3148 size_t find_last_not_of(const wxString
& str
, size_t nStart
= npos
) const
3149 { return m_impl
.find_last_not_of(str
.m_impl
, nStart
); }
3150 size_t find_last_not_of(const char* sz
, size_t nStart
= npos
) const
3151 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
); }
3152 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
= npos
) const
3153 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
); }
3154 size_t find_last_not_of(const char* sz
, size_t nStart
, size_t n
) const
3155 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
, n
); }
3156 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
3157 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
, n
); }
3158 size_t find_last_not_of(wxUniChar c
, size_t nStart
= npos
) const
3159 { return m_impl
.find_last_not_of((wxChar
)c
, nStart
); }
3161 // we can't use std::string implementation in UTF-8 build, because the
3162 // character sets would be interpreted wrongly:
3164 // as strpbrk() but starts at nStart, returns npos if not found
3165 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
3166 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3167 { return find_first_of(str
.wc_str(), nStart
); }
3169 { return find_first_of(str
.mb_str(), nStart
); }
3172 size_t find_first_of(const char* sz
, size_t nStart
= 0) const;
3173 size_t find_first_of(const wchar_t* sz
, size_t nStart
= 0) const;
3174 size_t find_first_of(const char* sz
, size_t nStart
, size_t n
) const;
3175 size_t find_first_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
3176 // same as find(char, size_t)
3177 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const
3178 { return find(c
, nStart
); }
3179 // find the last (starting from nStart) char from str in this string
3180 size_t find_last_of (const wxString
& str
, size_t nStart
= npos
) const
3181 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3182 { return find_last_of(str
.wc_str(), nStart
); }
3184 { return find_last_of(str
.mb_str(), nStart
); }
3187 size_t find_last_of (const char* sz
, size_t nStart
= npos
) const;
3188 size_t find_last_of (const wchar_t* sz
, size_t nStart
= npos
) const;
3189 size_t find_last_of(const char* sz
, size_t nStart
, size_t n
) const;
3190 size_t find_last_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
3192 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const
3193 { return rfind(c
, nStart
); }
3195 // find first/last occurrence of any character not in the set
3197 // as strspn() (starting from nStart), returns npos on failure
3198 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
3199 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3200 { return find_first_not_of(str
.wc_str(), nStart
); }
3202 { return find_first_not_of(str
.mb_str(), nStart
); }
3205 size_t find_first_not_of(const char* sz
, size_t nStart
= 0) const;
3206 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
= 0) const;
3207 size_t find_first_not_of(const char* sz
, size_t nStart
, size_t n
) const;
3208 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
3210 size_t find_first_not_of(wxUniChar ch
, size_t nStart
= 0) const;
3212 size_t find_last_not_of(const wxString
& str
, size_t nStart
= npos
) const
3213 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3214 { return find_last_not_of(str
.wc_str(), nStart
); }
3216 { return find_last_not_of(str
.mb_str(), nStart
); }
3219 size_t find_last_not_of(const char* sz
, size_t nStart
= npos
) const;
3220 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
= npos
) const;
3221 size_t find_last_not_of(const char* sz
, size_t nStart
, size_t n
) const;
3222 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
3224 size_t find_last_not_of(wxUniChar ch
, size_t nStart
= npos
) const;
3225 #endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not
3227 // provide char/wchar_t/wxUniCharRef overloads for char-finding functions
3228 // above to resolve ambiguities:
3229 size_t find_first_of(wxUniCharRef ch
, size_t nStart
= 0) const
3230 { return find_first_of(wxUniChar(ch
), nStart
); }
3231 size_t find_first_of(char ch
, size_t nStart
= 0) const
3232 { return find_first_of(wxUniChar(ch
), nStart
); }
3233 size_t find_first_of(unsigned char ch
, size_t nStart
= 0) const
3234 { return find_first_of(wxUniChar(ch
), nStart
); }
3235 size_t find_first_of(wchar_t ch
, size_t nStart
= 0) const
3236 { return find_first_of(wxUniChar(ch
), nStart
); }
3237 size_t find_last_of(wxUniCharRef ch
, size_t nStart
= npos
) const
3238 { return find_last_of(wxUniChar(ch
), nStart
); }
3239 size_t find_last_of(char ch
, size_t nStart
= npos
) const
3240 { return find_last_of(wxUniChar(ch
), nStart
); }
3241 size_t find_last_of(unsigned char ch
, size_t nStart
= npos
) const
3242 { return find_last_of(wxUniChar(ch
), nStart
); }
3243 size_t find_last_of(wchar_t ch
, size_t nStart
= npos
) const
3244 { return find_last_of(wxUniChar(ch
), nStart
); }
3245 size_t find_first_not_of(wxUniCharRef ch
, size_t nStart
= 0) const
3246 { return find_first_not_of(wxUniChar(ch
), nStart
); }
3247 size_t find_first_not_of(char ch
, size_t nStart
= 0) const
3248 { return find_first_not_of(wxUniChar(ch
), nStart
); }
3249 size_t find_first_not_of(unsigned char ch
, size_t nStart
= 0) const
3250 { return find_first_not_of(wxUniChar(ch
), nStart
); }
3251 size_t find_first_not_of(wchar_t ch
, size_t nStart
= 0) const
3252 { return find_first_not_of(wxUniChar(ch
), nStart
); }
3253 size_t find_last_not_of(wxUniCharRef ch
, size_t nStart
= npos
) const
3254 { return find_last_not_of(wxUniChar(ch
), nStart
); }
3255 size_t find_last_not_of(char ch
, size_t nStart
= npos
) const
3256 { return find_last_not_of(wxUniChar(ch
), nStart
); }
3257 size_t find_last_not_of(unsigned char ch
, size_t nStart
= npos
) const
3258 { return find_last_not_of(wxUniChar(ch
), nStart
); }
3259 size_t find_last_not_of(wchar_t ch
, size_t nStart
= npos
) const
3260 { return find_last_not_of(wxUniChar(ch
), nStart
); }
3262 // and additional overloads for the versions taking strings:
3263 size_t find_first_of(const wxCStrData
& sz
, size_t nStart
= 0) const
3264 { return find_first_of(sz
.AsString(), nStart
); }
3265 size_t find_first_of(const wxCharBuffer
& sz
, size_t nStart
= 0) const
3266 { return find_first_of(sz
.data(), nStart
); }
3267 size_t find_first_of(const wxWCharBuffer
& sz
, size_t nStart
= 0) const
3268 { return find_first_of(sz
.data(), nStart
); }
3269 size_t find_first_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
3270 { return find_first_of(sz
.AsWChar(), nStart
, n
); }
3271 size_t find_first_of(const wxCharBuffer
& sz
, size_t nStart
, size_t n
) const
3272 { return find_first_of(sz
.data(), nStart
, n
); }
3273 size_t find_first_of(const wxWCharBuffer
& sz
, size_t nStart
, size_t n
) const
3274 { return find_first_of(sz
.data(), nStart
, n
); }
3276 size_t find_last_of(const wxCStrData
& sz
, size_t nStart
= 0) const
3277 { return find_last_of(sz
.AsString(), nStart
); }
3278 size_t find_last_of(const wxCharBuffer
& sz
, size_t nStart
= 0) const
3279 { return find_last_of(sz
.data(), nStart
); }
3280 size_t find_last_of(const wxWCharBuffer
& sz
, size_t nStart
= 0) const
3281 { return find_last_of(sz
.data(), nStart
); }
3282 size_t find_last_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
3283 { return find_last_of(sz
.AsWChar(), nStart
, n
); }
3284 size_t find_last_of(const wxCharBuffer
& sz
, size_t nStart
, size_t n
) const
3285 { return find_last_of(sz
.data(), nStart
, n
); }
3286 size_t find_last_of(const wxWCharBuffer
& sz
, size_t nStart
, size_t n
) const
3287 { return find_last_of(sz
.data(), nStart
, n
); }
3289 size_t find_first_not_of(const wxCStrData
& sz
, size_t nStart
= 0) const
3290 { return find_first_not_of(sz
.AsString(), nStart
); }
3291 size_t find_first_not_of(const wxCharBuffer
& sz
, size_t nStart
= 0) const
3292 { return find_first_not_of(sz
.data(), nStart
); }
3293 size_t find_first_not_of(const wxWCharBuffer
& sz
, size_t nStart
= 0) const
3294 { return find_first_not_of(sz
.data(), nStart
); }
3295 size_t find_first_not_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
3296 { return find_first_not_of(sz
.AsWChar(), nStart
, n
); }
3297 size_t find_first_not_of(const wxCharBuffer
& sz
, size_t nStart
, size_t n
) const
3298 { return find_first_not_of(sz
.data(), nStart
, n
); }
3299 size_t find_first_not_of(const wxWCharBuffer
& sz
, size_t nStart
, size_t n
) const
3300 { return find_first_not_of(sz
.data(), nStart
, n
); }
3302 size_t find_last_not_of(const wxCStrData
& sz
, size_t nStart
= 0) const
3303 { return find_last_not_of(sz
.AsString(), nStart
); }
3304 size_t find_last_not_of(const wxCharBuffer
& sz
, size_t nStart
= 0) const
3305 { return find_last_not_of(sz
.data(), nStart
); }
3306 size_t find_last_not_of(const wxWCharBuffer
& sz
, size_t nStart
= 0) const
3307 { return find_last_not_of(sz
.data(), nStart
); }
3308 size_t find_last_not_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
3309 { return find_last_not_of(sz
.AsWChar(), nStart
, n
); }
3310 size_t find_last_not_of(const wxCharBuffer
& sz
, size_t nStart
, size_t n
) const
3311 { return find_last_not_of(sz
.data(), nStart
, n
); }
3312 size_t find_last_not_of(const wxWCharBuffer
& sz
, size_t nStart
, size_t n
) const
3313 { return find_last_not_of(sz
.data(), nStart
, n
); }
3316 wxString
& operator+=(const wxString
& s
)
3318 wxSTRING_INVALIDATE_CACHED_LENGTH();
3323 // string += C string
3324 wxString
& operator+=(const char *psz
)
3326 wxSTRING_INVALIDATE_CACHED_LENGTH();
3328 m_impl
+= ImplStr(psz
);
3331 wxString
& operator+=(const wchar_t *pwz
)
3333 wxSTRING_INVALIDATE_CACHED_LENGTH();
3335 m_impl
+= ImplStr(pwz
);
3338 wxString
& operator+=(const wxCStrData
& s
)
3340 wxSTRING_INVALIDATE_CACHED_LENGTH();
3342 m_impl
+= s
.AsString().m_impl
;
3345 wxString
& operator+=(const wxCharBuffer
& s
)
3346 { return operator+=(s
.data()); }
3347 wxString
& operator+=(const wxWCharBuffer
& s
)
3348 { return operator+=(s
.data()); }
3350 wxString
& operator+=(wxUniChar ch
)
3352 wxSTRING_UPDATE_CACHED_LENGTH(1);
3354 #if wxUSE_UNICODE_UTF8
3355 if ( !ch
.IsAscii() )
3356 m_impl
+= wxStringOperations::EncodeChar(ch
);
3359 m_impl
+= (wxStringCharType
)ch
;
3362 wxString
& operator+=(wxUniCharRef ch
) { return *this += wxUniChar(ch
); }
3363 wxString
& operator+=(int ch
) { return *this += wxUniChar(ch
); }
3364 wxString
& operator+=(char ch
) { return *this += wxUniChar(ch
); }
3365 wxString
& operator+=(unsigned char ch
) { return *this += wxUniChar(ch
); }
3366 wxString
& operator+=(wchar_t ch
) { return *this += wxUniChar(ch
); }
3369 #if !wxUSE_STL_BASED_WXSTRING
3370 // helpers for wxStringBuffer and wxStringBufferLength
3371 wxStringCharType
*DoGetWriteBuf(size_t nLen
)
3373 return m_impl
.DoGetWriteBuf(nLen
);
3376 void DoUngetWriteBuf()
3378 wxSTRING_INVALIDATE_CACHE();
3380 m_impl
.DoUngetWriteBuf();
3383 void DoUngetWriteBuf(size_t nLen
)
3385 wxSTRING_SET_CACHED_LENGTH(nLen
);
3387 m_impl
.DoUngetWriteBuf(nLen
);
3389 #endif // !wxUSE_STL_BASED_WXSTRING
3391 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
3392 #if !wxUSE_UTF8_LOCALE_ONLY
3393 int DoPrintfWchar(const wxChar
*format
, ...);
3394 static wxString
DoFormatWchar(const wxChar
*format
, ...);
3396 #if wxUSE_UNICODE_UTF8
3397 int DoPrintfUtf8(const char *format
, ...);
3398 static wxString
DoFormatUtf8(const char *format
, ...);
3402 #if !wxUSE_STL_BASED_WXSTRING
3403 // check string's data validity
3404 bool IsValid() const { return m_impl
.GetStringData()->IsValid(); }
3408 wxStringImpl m_impl
;
3410 // buffers for compatibility conversion from (char*)c_str() and
3411 // (wchar_t*)c_str():
3412 // FIXME-UTF8: bechmark various approaches to keeping compatibility buffers
3413 template<typename T
>
3414 struct ConvertedBuffer
3416 ConvertedBuffer() : m_buf(NULL
) {}
3420 operator T
*() const { return m_buf
; }
3422 ConvertedBuffer
& operator=(T
*str
)
3431 #if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
3432 ConvertedBuffer
<char> m_convertedToChar
;
3434 #if !wxUSE_UNICODE_WCHAR
3435 ConvertedBuffer
<wchar_t> m_convertedToWChar
;
3438 #if wxUSE_UNICODE_UTF8
3439 // FIXME-UTF8: (try to) move this elsewhere (TLS) or solve differently
3440 // assigning to character pointer to by wxString::interator may
3441 // change the underlying wxStringImpl iterator, so we have to
3442 // keep track of all iterators and update them as necessary:
3443 struct wxStringIteratorNodeHead
3445 wxStringIteratorNodeHead() : ptr(NULL
) {}
3446 wxStringIteratorNode
*ptr
;
3448 // copying is disallowed as it would result in more than one pointer into
3449 // the same linked list
3450 wxDECLARE_NO_COPY_CLASS(wxStringIteratorNodeHead
);
3453 wxStringIteratorNodeHead m_iterators
;
3455 friend class WXDLLIMPEXP_FWD_BASE wxStringIteratorNode
;
3456 friend class WXDLLIMPEXP_FWD_BASE wxUniCharRef
;
3457 #endif // wxUSE_UNICODE_UTF8
3459 friend class WXDLLIMPEXP_FWD_BASE wxCStrData
;
3460 friend class wxStringInternalBuffer
;
3461 friend class wxStringInternalBufferLength
;
3464 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
3465 #pragma warning (default:4275)
3468 // string iterator operators that satisfy STL Random Access Iterator
3470 inline wxString::iterator
operator+(ptrdiff_t n
, wxString::iterator i
)
3472 inline wxString::const_iterator
operator+(ptrdiff_t n
, wxString::const_iterator i
)
3474 inline wxString::reverse_iterator
operator+(ptrdiff_t n
, wxString::reverse_iterator i
)
3476 inline wxString::const_reverse_iterator
operator+(ptrdiff_t n
, wxString::const_reverse_iterator i
)
3479 // notice that even though for many compilers the friend declarations above are
3480 // enough, from the point of view of C++ standard we must have the declarations
3481 // here as friend ones are not injected in the enclosing namespace and without
3482 // them the code fails to compile with conforming compilers such as xlC or g++4
3483 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
, const wxString
& string2
);
3484 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const char *psz
);
3485 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const wchar_t *pwz
);
3486 wxString WXDLLIMPEXP_BASE
operator+(const char *psz
, const wxString
& string
);
3487 wxString WXDLLIMPEXP_BASE
operator+(const wchar_t *pwz
, const wxString
& string
);
3489 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
3490 wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
3492 inline wxString
operator+(const wxString
& string
, wxUniCharRef ch
)
3493 { return string
+ (wxUniChar
)ch
; }
3494 inline wxString
operator+(const wxString
& string
, char ch
)
3495 { return string
+ wxUniChar(ch
); }
3496 inline wxString
operator+(const wxString
& string
, wchar_t ch
)
3497 { return string
+ wxUniChar(ch
); }
3498 inline wxString
operator+(wxUniCharRef ch
, const wxString
& string
)
3499 { return (wxUniChar
)ch
+ string
; }
3500 inline wxString
operator+(char ch
, const wxString
& string
)
3501 { return wxUniChar(ch
) + string
; }
3502 inline wxString
operator+(wchar_t ch
, const wxString
& string
)
3503 { return wxUniChar(ch
) + string
; }
3506 #define wxGetEmptyString() wxString()
3508 // ----------------------------------------------------------------------------
3509 // helper functions which couldn't be defined inline
3510 // ----------------------------------------------------------------------------
3515 #if wxUSE_UNICODE_WCHAR
3518 struct wxStringAsBufHelper
<char>
3520 static wxCharBuffer
Get(const wxString
& s
, size_t *len
)
3522 wxCharBuffer
buf(s
.mb_str());
3524 *len
= buf
? strlen(buf
) : 0;
3530 struct wxStringAsBufHelper
<wchar_t>
3532 static wxWCharBuffer
Get(const wxString
& s
, size_t *len
)
3536 return wxWCharBuffer::CreateNonOwned(s
.wx_str());
3540 #elif wxUSE_UNICODE_UTF8
3543 struct wxStringAsBufHelper
<char>
3545 static wxCharBuffer
Get(const wxString
& s
, size_t *len
)
3548 *len
= s
.utf8_length();
3549 return wxCharBuffer::CreateNonOwned(s
.wx_str());
3554 struct wxStringAsBufHelper
<wchar_t>
3556 static wxWCharBuffer
Get(const wxString
& s
, size_t *len
)
3558 wxWCharBuffer
wbuf(s
.wc_str());
3560 *len
= wxWcslen(wbuf
);
3565 #endif // Unicode build kind
3567 } // namespace wxPrivate
3569 // ----------------------------------------------------------------------------
3570 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
3571 // ----------------------------------------------------------------------------
3573 #if !wxUSE_STL_BASED_WXSTRING
3574 // string buffer for direct access to string data in their native
3576 class wxStringInternalBuffer
3579 typedef wxStringCharType CharType
;
3581 wxStringInternalBuffer(wxString
& str
, size_t lenWanted
= 1024)
3582 : m_str(str
), m_buf(NULL
)
3583 { m_buf
= m_str
.DoGetWriteBuf(lenWanted
); }
3585 ~wxStringInternalBuffer() { m_str
.DoUngetWriteBuf(); }
3587 operator wxStringCharType
*() const { return m_buf
; }
3591 wxStringCharType
*m_buf
;
3593 wxDECLARE_NO_COPY_CLASS(wxStringInternalBuffer
);
3596 class wxStringInternalBufferLength
3599 typedef wxStringCharType CharType
;
3601 wxStringInternalBufferLength(wxString
& str
, size_t lenWanted
= 1024)
3602 : m_str(str
), m_buf(NULL
), m_len(0), m_lenSet(false)
3604 m_buf
= m_str
.DoGetWriteBuf(lenWanted
);
3605 wxASSERT(m_buf
!= NULL
);
3608 ~wxStringInternalBufferLength()
3611 m_str
.DoUngetWriteBuf(m_len
);
3614 operator wxStringCharType
*() const { return m_buf
; }
3615 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
3619 wxStringCharType
*m_buf
;
3623 wxDECLARE_NO_COPY_CLASS(wxStringInternalBufferLength
);
3626 #endif // !wxUSE_STL_BASED_WXSTRING
3628 template<typename T
>
3629 class wxStringTypeBufferBase
3634 wxStringTypeBufferBase(wxString
& str
, size_t lenWanted
= 1024)
3635 : m_str(str
), m_buf(lenWanted
)
3637 // for compatibility with old wxStringBuffer which provided direct
3638 // access to wxString internal buffer, initialize ourselves with the
3639 // string initial contents
3641 // FIXME-VC6: remove the ugly (CharType *)NULL and use normal
3642 // tchar_str<CharType>
3644 const wxCharTypeBuffer
<CharType
> buf(str
.tchar_str(&len
, (CharType
*)NULL
));
3647 if ( len
> lenWanted
)
3649 // in this case there is not enough space for terminating NUL,
3650 // ensure that we still put it there
3651 m_buf
.data()[lenWanted
] = 0;
3652 len
= lenWanted
- 1;
3655 memcpy(m_buf
.data(), buf
, (len
+ 1)*sizeof(CharType
));
3657 //else: conversion failed, this can happen when trying to get Unicode
3658 // string contents into a char string
3661 operator CharType
*() { return m_buf
.data(); }
3665 wxCharTypeBuffer
<CharType
> m_buf
;
3668 template<typename T
>
3669 class wxStringTypeBufferLengthBase
: public wxStringTypeBufferBase
<T
>
3672 wxStringTypeBufferLengthBase(wxString
& str
, size_t lenWanted
= 1024)
3673 : wxStringTypeBufferBase
<T
>(str
, lenWanted
),
3678 ~wxStringTypeBufferLengthBase()
3680 wxASSERT_MSG( this->m_lenSet
, "forgot to call SetLength()" );
3683 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
3690 template<typename T
>
3691 class wxStringTypeBuffer
: public wxStringTypeBufferBase
<T
>
3694 wxStringTypeBuffer(wxString
& str
, size_t lenWanted
= 1024)
3695 : wxStringTypeBufferBase
<T
>(str
, lenWanted
)
3698 ~wxStringTypeBuffer()
3700 this->m_str
.assign(this->m_buf
.data());
3703 wxDECLARE_NO_COPY_CLASS(wxStringTypeBuffer
);
3706 template<typename T
>
3707 class wxStringTypeBufferLength
: public wxStringTypeBufferLengthBase
<T
>
3710 wxStringTypeBufferLength(wxString
& str
, size_t lenWanted
= 1024)
3711 : wxStringTypeBufferLengthBase
<T
>(str
, lenWanted
)
3714 ~wxStringTypeBufferLength()
3716 this->m_str
.assign(this->m_buf
.data(), this->m_len
);
3719 wxDECLARE_NO_COPY_CLASS(wxStringTypeBufferLength
);
3722 #if wxUSE_STL_BASED_WXSTRING
3724 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase
<wxStringCharType
> )
3726 class wxStringInternalBuffer
: public wxStringTypeBufferBase
<wxStringCharType
>
3729 wxStringInternalBuffer(wxString
& str
, size_t lenWanted
= 1024)
3730 : wxStringTypeBufferBase
<wxStringCharType
>(str
, lenWanted
) {}
3731 ~wxStringInternalBuffer()
3732 { m_str
.m_impl
.assign(m_buf
.data()); }
3734 wxDECLARE_NO_COPY_CLASS(wxStringInternalBuffer
);
3737 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE(
3738 wxStringTypeBufferLengthBase
<wxStringCharType
> )
3740 class wxStringInternalBufferLength
3741 : public wxStringTypeBufferLengthBase
<wxStringCharType
>
3744 wxStringInternalBufferLength(wxString
& str
, size_t lenWanted
= 1024)
3745 : wxStringTypeBufferLengthBase
<wxStringCharType
>(str
, lenWanted
) {}
3747 ~wxStringInternalBufferLength()
3749 m_str
.m_impl
.assign(m_buf
.data(), m_len
);
3752 wxDECLARE_NO_COPY_CLASS(wxStringInternalBufferLength
);
3755 #endif // wxUSE_STL_BASED_WXSTRING
3758 #if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
3759 typedef wxStringTypeBuffer
<wxChar
> wxStringBuffer
;
3760 typedef wxStringTypeBufferLength
<wxChar
> wxStringBufferLength
;
3761 #else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
3762 typedef wxStringInternalBuffer wxStringBuffer
;
3763 typedef wxStringInternalBufferLength wxStringBufferLength
;
3764 #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
3766 #if wxUSE_UNICODE_UTF8
3767 typedef wxStringInternalBuffer wxUTF8StringBuffer
;
3768 typedef wxStringInternalBufferLength wxUTF8StringBufferLength
;
3769 #elif wxUSE_UNICODE_WCHAR
3771 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase
<char> )
3773 // Note about inlined dtors in the classes below: this is done not for
3774 // performance reasons but just to avoid linking errors in the MSVC DLL build
3775 // under Windows: if a class has non-inline methods it must be declared as
3776 // being DLL-exported but, due to an extremely interesting feature of MSVC 7
3777 // and later, any template class which is used as a base of a DLL-exported
3778 // class is implicitly made DLL-exported too, as explained at the bottom of
3779 // http://msdn.microsoft.com/en-us/library/twa2aw10.aspx (just to confirm: yes,
3780 // _inheriting_ from a class can change whether it is being exported from DLL)
3782 // But this results in link errors because the base template class is not DLL-
3783 // exported, whether it is declared with WXDLLIMPEXP_BASE or not, because it
3784 // does have only inline functions. So the simplest fix is to just make all the
3785 // functions of these classes inline too.
3787 class wxUTF8StringBuffer
: public wxStringTypeBufferBase
<char>
3790 wxUTF8StringBuffer(wxString
& str
, size_t lenWanted
= 1024)
3791 : wxStringTypeBufferBase
<char>(str
, lenWanted
) {}
3792 ~wxUTF8StringBuffer()
3794 wxMBConvStrictUTF8 conv
;
3795 size_t wlen
= conv
.ToWChar(NULL
, 0, m_buf
);
3796 wxCHECK_RET( wlen
!= wxCONV_FAILED
, "invalid UTF-8 data in string buffer?" );
3798 wxStringInternalBuffer
wbuf(m_str
, wlen
);
3799 conv
.ToWChar(wbuf
, wlen
, m_buf
);
3802 wxDECLARE_NO_COPY_CLASS(wxUTF8StringBuffer
);
3805 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferLengthBase
<char> )
3807 class wxUTF8StringBufferLength
: public wxStringTypeBufferLengthBase
<char>
3810 wxUTF8StringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
3811 : wxStringTypeBufferLengthBase
<char>(str
, lenWanted
) {}
3812 ~wxUTF8StringBufferLength()
3814 wxCHECK_RET(m_lenSet
, "length not set");
3816 wxMBConvStrictUTF8 conv
;
3817 size_t wlen
= conv
.ToWChar(NULL
, 0, m_buf
, m_len
);
3818 wxCHECK_RET( wlen
!= wxCONV_FAILED
, "invalid UTF-8 data in string buffer?" );
3820 wxStringInternalBufferLength
wbuf(m_str
, wlen
);
3821 conv
.ToWChar(wbuf
, wlen
, m_buf
, m_len
);
3822 wbuf
.SetLength(wlen
);
3825 wxDECLARE_NO_COPY_CLASS(wxUTF8StringBufferLength
);
3827 #endif // wxUSE_UNICODE_UTF8/wxUSE_UNICODE_WCHAR
3830 // ---------------------------------------------------------------------------
3831 // wxString comparison functions: operator versions are always case sensitive
3832 // ---------------------------------------------------------------------------
3834 #define wxCMP_WXCHAR_STRING(p, s, op) 0 op s.Cmp(p)
3836 wxDEFINE_ALL_COMPARISONS(const wxChar
*, const wxString
&, wxCMP_WXCHAR_STRING
)
3838 #undef wxCMP_WXCHAR_STRING
3840 inline bool operator==(const wxString
& s1
, const wxString
& s2
)
3841 { return s1
.IsSameAs(s2
); }
3842 inline bool operator!=(const wxString
& s1
, const wxString
& s2
)
3843 { return !s1
.IsSameAs(s2
); }
3844 inline bool operator< (const wxString
& s1
, const wxString
& s2
)
3845 { return s1
.Cmp(s2
) < 0; }
3846 inline bool operator> (const wxString
& s1
, const wxString
& s2
)
3847 { return s1
.Cmp(s2
) > 0; }
3848 inline bool operator<=(const wxString
& s1
, const wxString
& s2
)
3849 { return s1
.Cmp(s2
) <= 0; }
3850 inline bool operator>=(const wxString
& s1
, const wxString
& s2
)
3851 { return s1
.Cmp(s2
) >= 0; }
3853 inline bool operator==(const wxString
& s1
, const wxCStrData
& s2
)
3854 { return s1
== s2
.AsString(); }
3855 inline bool operator==(const wxCStrData
& s1
, const wxString
& s2
)
3856 { return s1
.AsString() == s2
; }
3857 inline bool operator!=(const wxString
& s1
, const wxCStrData
& s2
)
3858 { return s1
!= s2
.AsString(); }
3859 inline bool operator!=(const wxCStrData
& s1
, const wxString
& s2
)
3860 { return s1
.AsString() != s2
; }
3862 inline bool operator==(const wxString
& s1
, const wxWCharBuffer
& s2
)
3863 { return (s1
.Cmp((const wchar_t *)s2
) == 0); }
3864 inline bool operator==(const wxWCharBuffer
& s1
, const wxString
& s2
)
3865 { return (s2
.Cmp((const wchar_t *)s1
) == 0); }
3866 inline bool operator!=(const wxString
& s1
, const wxWCharBuffer
& s2
)
3867 { return (s1
.Cmp((const wchar_t *)s2
) != 0); }
3868 inline bool operator!=(const wxWCharBuffer
& s1
, const wxString
& s2
)
3869 { return (s2
.Cmp((const wchar_t *)s1
) != 0); }
3871 inline bool operator==(const wxString
& s1
, const wxCharBuffer
& s2
)
3872 { return (s1
.Cmp((const char *)s2
) == 0); }
3873 inline bool operator==(const wxCharBuffer
& s1
, const wxString
& s2
)
3874 { return (s2
.Cmp((const char *)s1
) == 0); }
3875 inline bool operator!=(const wxString
& s1
, const wxCharBuffer
& s2
)
3876 { return (s1
.Cmp((const char *)s2
) != 0); }
3877 inline bool operator!=(const wxCharBuffer
& s1
, const wxString
& s2
)
3878 { return (s2
.Cmp((const char *)s1
) != 0); }
3880 inline wxString
operator+(const wxString
& string
, const wxWCharBuffer
& buf
)
3881 { return string
+ (const wchar_t *)buf
; }
3882 inline wxString
operator+(const wxWCharBuffer
& buf
, const wxString
& string
)
3883 { return (const wchar_t *)buf
+ string
; }
3885 inline wxString
operator+(const wxString
& string
, const wxCharBuffer
& buf
)
3886 { return string
+ (const char *)buf
; }
3887 inline wxString
operator+(const wxCharBuffer
& buf
, const wxString
& string
)
3888 { return (const char *)buf
+ string
; }
3890 // comparison with char
3891 inline bool operator==(const wxUniChar
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
3892 inline bool operator==(const wxUniCharRef
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
3893 inline bool operator==(char c
, const wxString
& s
) { return s
.IsSameAs(c
); }
3894 inline bool operator==(wchar_t c
, const wxString
& s
) { return s
.IsSameAs(c
); }
3895 inline bool operator==(int c
, const wxString
& s
) { return s
.IsSameAs(c
); }
3896 inline bool operator==(const wxString
& s
, const wxUniChar
& c
) { return s
.IsSameAs(c
); }
3897 inline bool operator==(const wxString
& s
, const wxUniCharRef
& c
) { return s
.IsSameAs(c
); }
3898 inline bool operator==(const wxString
& s
, char c
) { return s
.IsSameAs(c
); }
3899 inline bool operator==(const wxString
& s
, wchar_t c
) { return s
.IsSameAs(c
); }
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
); }
3910 // comparison with C string in Unicode build
3913 #define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s
3915 wxDEFINE_ALL_COMPARISONS(const char *, const wxString
&, wxCMP_CHAR_STRING
)
3917 #undef wxCMP_CHAR_STRING
3919 #endif // wxUSE_UNICODE
3921 // we also need to provide the operators for comparison with wxCStrData to
3922 // resolve ambiguity between operator(const wxChar *,const wxString &) and
3923 // operator(const wxChar *, const wxChar *) for "p == s.c_str()"
3925 // notice that these are (shallow) pointer comparisons, not (deep) string ones
3926 #define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar()
3927 #define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar()
3929 wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData
&, wxCMP_WCHAR_CSTRDATA
)
3930 wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData
&, wxCMP_CHAR_CSTRDATA
)
3932 #undef wxCMP_CHAR_CSTRDATA
3933 #undef wxCMP_WCHAR_CSTRDATA
3935 // ---------------------------------------------------------------------------
3936 // Implementation only from here until the end of file
3937 // ---------------------------------------------------------------------------
3939 #if wxUSE_STD_IOSTREAM
3941 #include "wx/iosfwrap.h"
3943 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxString
&);
3944 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxCStrData
&);
3945 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxCharBuffer
&);
3946 #ifndef __BORLANDC__
3947 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxWCharBuffer
&);
3950 #if wxUSE_UNICODE && defined(HAVE_WOSTREAM)
3952 WXDLLIMPEXP_BASE wxSTD wostream
& operator<<(wxSTD wostream
&, const wxString
&);
3953 WXDLLIMPEXP_BASE wxSTD wostream
& operator<<(wxSTD wostream
&, const wxCStrData
&);
3954 WXDLLIMPEXP_BASE wxSTD wostream
& operator<<(wxSTD wostream
&, const wxWCharBuffer
&);
3956 #endif // wxUSE_UNICODE && defined(HAVE_WOSTREAM)
3958 #endif // wxUSE_STD_IOSTREAM
3960 // ---------------------------------------------------------------------------
3961 // wxCStrData implementation
3962 // ---------------------------------------------------------------------------
3964 inline wxCStrData::wxCStrData(char *buf
)
3965 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
3966 inline wxCStrData::wxCStrData(wchar_t *buf
)
3967 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
3969 inline wxCStrData::wxCStrData(const wxCStrData
& data
)
3970 : m_str(data
.m_owned
? new wxString(*data
.m_str
) : data
.m_str
),
3971 m_offset(data
.m_offset
),
3972 m_owned(data
.m_owned
)
3976 inline wxCStrData::~wxCStrData()
3979 delete const_cast<wxString
*>(m_str
); // cast to silence warnings
3982 // simple cases for AsChar() and AsWChar(), the complicated ones are
3984 #if wxUSE_UNICODE_WCHAR
3985 inline const wchar_t* wxCStrData::AsWChar() const
3987 return m_str
->wx_str() + m_offset
;
3989 #endif // wxUSE_UNICODE_WCHAR
3992 inline const char* wxCStrData::AsChar() const
3994 return m_str
->wx_str() + m_offset
;
3996 #endif // !wxUSE_UNICODE
3998 #if wxUSE_UTF8_LOCALE_ONLY
3999 inline const char* wxCStrData::AsChar() const
4001 return wxStringOperations::AddToIter(m_str
->wx_str(), m_offset
);
4003 #endif // wxUSE_UTF8_LOCALE_ONLY
4005 inline const wxCharBuffer
wxCStrData::AsCharBuf() const
4008 return wxCharBuffer::CreateNonOwned(AsChar());
4010 return AsString().mb_str();
4014 inline const wxWCharBuffer
wxCStrData::AsWCharBuf() const
4016 #if wxUSE_UNICODE_WCHAR
4017 return wxWCharBuffer::CreateNonOwned(AsWChar());
4019 return AsString().wc_str();
4023 inline wxString
wxCStrData::AsString() const
4025 if ( m_offset
== 0 )
4028 return m_str
->Mid(m_offset
);
4031 inline const wxStringCharType
*wxCStrData::AsInternal() const
4033 #if wxUSE_UNICODE_UTF8
4034 return wxStringOperations::AddToIter(m_str
->wx_str(), m_offset
);
4036 return m_str
->wx_str() + m_offset
;
4040 inline wxUniChar
wxCStrData::operator*() const
4042 if ( m_str
->empty() )
4043 return wxUniChar(_T('\0'));
4045 return (*m_str
)[m_offset
];
4048 inline wxUniChar
wxCStrData::operator[](size_t n
) const
4050 // NB: we intentionally use operator[] and not at() here because the former
4051 // works for the terminating NUL while the latter does not
4052 return (*m_str
)[m_offset
+ n
];
4055 // ----------------------------------------------------------------------------
4056 // more wxCStrData operators
4057 // ----------------------------------------------------------------------------
4059 // we need to define those to allow "size_t pos = p - s.c_str()" where p is
4060 // some pointer into the string
4061 inline size_t operator-(const char *p
, const wxCStrData
& cs
)
4063 return p
- cs
.AsChar();
4066 inline size_t operator-(const wchar_t *p
, const wxCStrData
& cs
)
4068 return p
- cs
.AsWChar();
4071 // ----------------------------------------------------------------------------
4072 // implementation of wx[W]CharBuffer inline methods using wxCStrData
4073 // ----------------------------------------------------------------------------
4075 // FIXME-UTF8: move this to buffer.h
4076 inline wxCharBuffer::wxCharBuffer(const wxCStrData
& cstr
)
4077 : wxCharTypeBufferBase(cstr
.AsCharBuf())
4081 inline wxWCharBuffer::wxWCharBuffer(const wxCStrData
& cstr
)
4082 : wxCharTypeBufferBase(cstr
.AsWCharBuf())
4086 #if wxUSE_UNICODE_UTF8
4087 // ----------------------------------------------------------------------------
4088 // implementation of wxStringIteratorNode inline methods
4089 // ----------------------------------------------------------------------------
4091 void wxStringIteratorNode::DoSet(const wxString
*str
,
4092 wxStringImpl::const_iterator
*citer
,
4093 wxStringImpl::iterator
*iter
)
4101 m_next
= str
->m_iterators
.ptr
;
4102 const_cast<wxString
*>(m_str
)->m_iterators
.ptr
= this;
4104 m_next
->m_prev
= this;
4112 void wxStringIteratorNode::clear()
4115 m_next
->m_prev
= m_prev
;
4117 m_prev
->m_next
= m_next
;
4118 else if ( m_str
) // first in the list
4119 const_cast<wxString
*>(m_str
)->m_iterators
.ptr
= m_next
;
4121 m_next
= m_prev
= NULL
;
4126 #endif // wxUSE_UNICODE_UTF8
4128 #if WXWIN_COMPATIBILITY_2_8
4129 // lot of code out there doesn't explicitly include wx/crt.h, but uses
4130 // CRT wrappers that are now declared in wx/wxcrt.h and wx/wxcrtvararg.h,
4131 // so let's include this header now that wxString is defined and it's safe
4136 // ----------------------------------------------------------------------------
4137 // Checks on wxString characters
4138 // ----------------------------------------------------------------------------
4140 template<bool (T
)(const wxUniChar
& c
)>
4141 inline bool wxStringCheck(const wxString
& val
)
4143 for ( wxString::const_iterator i
= val
.begin();
4151 #endif // _WX_WXSTRING_H_