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 #if wxUSE_UNICODE_UTF8 && !defined(__WXMSW__)
73 #define wxUSE_STRING_POS_CACHE 1
75 #define wxUSE_STRING_POS_CACHE 0
78 #ifndef wxHAS_COMPILER_TLS
79 // FIXME: currently the code only works with compiler TLS support
80 #undef wxUSE_STRING_POS_CACHE
81 #define wxUSE_STRING_POS_CACHE 0
84 #if wxUSE_STRING_POS_CACHE
87 // change this 0 to 1 to enable additional (very expensive) asserts
88 // verifying that string caching logic works as expected
90 #define wxSTRING_CACHE_ASSERT(cond) wxASSERT(cond)
92 #define wxSTRING_CACHE_ASSERT(cond)
94 #endif // wxUSE_STRING_POS_CACHE
96 class WXDLLIMPEXP_FWD_BASE wxString
;
98 // unless this symbol is predefined to disable the compatibility functions, do
100 #ifndef WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
101 #define WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER 1
106 template <typename T
> struct wxStringAsBufHelper
;
109 // ---------------------------------------------------------------------------
111 // ---------------------------------------------------------------------------
113 // casts [unfortunately!] needed to call some broken functions which require
114 // "char *" instead of "const char *"
115 #define WXSTRINGCAST (wxChar *)(const wxChar *)
116 #define wxCSTRINGCAST (wxChar *)(const wxChar *)
117 #define wxMBSTRINGCAST (char *)(const char *)
118 #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *)
120 // ----------------------------------------------------------------------------
122 // ----------------------------------------------------------------------------
124 #if WXWIN_COMPATIBILITY_2_6
126 // deprecated in favour of wxString::npos, don't use in new code
128 // maximum possible length for a string means "take all string" everywhere
129 #define wxSTRING_MAXLEN wxString::npos
131 #endif // WXWIN_COMPATIBILITY_2_6
133 // ---------------------------------------------------------------------------
134 // global functions complementing standard C string library replacements for
135 // strlen() and portable strcasecmp()
136 //---------------------------------------------------------------------------
138 #if WXWIN_COMPATIBILITY_2_8
139 // Use wxXXX() functions from wxcrt.h instead! These functions are for
140 // backwards compatibility only.
142 // checks whether the passed in pointer is NULL and if the string is empty
143 wxDEPRECATED( inline bool IsEmpty(const char *p
) );
144 inline bool IsEmpty(const char *p
) { return (!p
|| !*p
); }
146 // safe version of strlen() (returns 0 if passed NULL pointer)
147 wxDEPRECATED( inline size_t Strlen(const char *psz
) );
148 inline size_t Strlen(const char *psz
)
149 { return psz
? strlen(psz
) : 0; }
151 // portable strcasecmp/_stricmp
152 wxDEPRECATED( inline int Stricmp(const char *psz1
, const char *psz2
) );
153 inline int Stricmp(const char *psz1
, const char *psz2
)
155 #if defined(__VISUALC__) && defined(__WXWINCE__)
156 register char c1
, c2
;
158 c1
= tolower(*psz1
++);
159 c2
= tolower(*psz2
++);
160 } while ( c1
&& (c1
== c2
) );
163 #elif defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) )
164 return _stricmp(psz1
, psz2
);
165 #elif defined(__SC__)
166 return _stricmp(psz1
, psz2
);
167 #elif defined(__BORLANDC__)
168 return stricmp(psz1
, psz2
);
169 #elif defined(__WATCOMC__)
170 return stricmp(psz1
, psz2
);
171 #elif defined(__DJGPP__)
172 return stricmp(psz1
, psz2
);
173 #elif defined(__EMX__)
174 return stricmp(psz1
, psz2
);
175 #elif defined(__WXPM__)
176 return stricmp(psz1
, psz2
);
177 #elif defined(__WXPALMOS__) || \
178 defined(HAVE_STRCASECMP_IN_STRING_H) || \
179 defined(HAVE_STRCASECMP_IN_STRINGS_H) || \
180 defined(__GNUWIN32__)
181 return strcasecmp(psz1
, psz2
);
182 #elif defined(__MWERKS__) && !defined(__INTEL__)
183 register char c1
, c2
;
185 c1
= tolower(*psz1
++);
186 c2
= tolower(*psz2
++);
187 } while ( c1
&& (c1
== c2
) );
191 // almost all compilers/libraries provide this function (unfortunately under
192 // different names), that's why we don't implement our own which will surely
193 // be more efficient than this code (uncomment to use):
195 register char c1, c2;
197 c1 = tolower(*psz1++);
198 c2 = tolower(*psz2++);
199 } while ( c1 && (c1 == c2) );
204 #error "Please define string case-insensitive compare for your OS/compiler"
205 #endif // OS/compiler
208 #endif // WXWIN_COMPATIBILITY_2_8
210 // ----------------------------------------------------------------------------
212 // ----------------------------------------------------------------------------
214 // Lightweight object returned by wxString::c_str() and implicitly convertible
215 // to either const char* or const wchar_t*.
216 class WXDLLIMPEXP_BASE wxCStrData
219 // Ctors; for internal use by wxString and wxCStrData only
220 wxCStrData(const wxString
*str
, size_t offset
= 0, bool owned
= false)
221 : m_str(str
), m_offset(offset
), m_owned(owned
) {}
224 // Ctor constructs the object from char literal; they are needed to make
225 // operator?: compile and they intentionally take char*, not const char*
226 inline wxCStrData(char *buf
);
227 inline wxCStrData(wchar_t *buf
);
228 inline wxCStrData(const wxCStrData
& data
);
230 inline ~wxCStrData();
232 // methods defined inline below must be declared inline or mingw32 3.4.5
233 // warns about "<symbol> defined locally after being referenced with
234 // dllimport linkage"
235 #if wxUSE_UNICODE_WCHAR
238 const wchar_t* AsWChar() const;
239 operator const wchar_t*() const { return AsWChar(); }
241 #if !wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY
244 const char* AsChar() const;
245 const unsigned char* AsUnsignedChar() const
246 { return (const unsigned char *) AsChar(); }
247 operator const char*() const { return AsChar(); }
248 operator const unsigned char*() const { return AsUnsignedChar(); }
250 operator const void*() const { return AsChar(); }
252 inline const wxCharBuffer
AsCharBuf() const;
253 inline const wxWCharBuffer
AsWCharBuf() const;
255 inline wxString
AsString() const;
257 // returns the value as C string in internal representation (equivalent
258 // to AsString().wx_str(), but more efficient)
259 const wxStringCharType
*AsInternal() const;
261 // allow expressions like "c_str()[0]":
262 inline wxUniChar
operator[](size_t n
) const;
263 wxUniChar
operator[](int n
) const { return operator[](size_t(n
)); }
264 wxUniChar
operator[](long n
) const { return operator[](size_t(n
)); }
265 #ifndef wxSIZE_T_IS_UINT
266 wxUniChar
operator[](unsigned int n
) const { return operator[](size_t(n
)); }
267 #endif // size_t != unsigned int
269 // these operators are needed to emulate the pointer semantics of c_str():
270 // expressions like "wxChar *p = str.c_str() + 1;" should continue to work
271 // (we need both versions to resolve ambiguities):
272 wxCStrData
operator+(int n
) const
273 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
274 wxCStrData
operator+(long n
) const
275 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
276 wxCStrData
operator+(size_t n
) const
277 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
279 // and these for "str.c_str() + (p2 - p1)" (it also works for any integer
280 // expression but it must be ptrdiff_t and not e.g. int to work in this
282 wxCStrData
operator-(ptrdiff_t n
) const
284 wxASSERT_MSG( n
<= (ptrdiff_t)m_offset
,
285 _T("attempt to construct address before the beginning of the string") );
286 return wxCStrData(m_str
, m_offset
- n
, m_owned
);
289 // this operator is needed to make expressions like "*c_str()" or
290 // "*(c_str() + 2)" work
291 inline wxUniChar
operator*() const;
294 const wxString
*m_str
;
298 friend class WXDLLIMPEXP_FWD_BASE wxString
;
301 // ----------------------------------------------------------------------------
302 // wxStringPrintfMixin
303 // ---------------------------------------------------------------------------
305 // NB: VC6 has a bug that causes linker errors if you have template methods
306 // in a class using __declspec(dllimport). The solution is to split such
307 // class into two classes, one that contains the template methods and does
308 // *not* use WXDLLIMPEXP_BASE and another class that contains the rest
309 // (with DLL linkage).
311 // We only do this for VC6 here, because the code is less efficient
312 // (Printf() has to use dynamic_cast<>) and because OpenWatcom compiler
313 // cannot compile this code.
315 #if defined(__VISUALC__) && __VISUALC__ < 1300
316 #define wxNEEDS_WXSTRING_PRINTF_MIXIN
319 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
320 // this class contains implementation of wxString's vararg methods, it's
321 // exported from wxBase DLL
322 class WXDLLIMPEXP_BASE wxStringPrintfMixinBase
325 wxStringPrintfMixinBase() {}
327 #if !wxUSE_UTF8_LOCALE_ONLY
328 int DoPrintfWchar(const wxChar
*format
, ...);
329 static wxString
DoFormatWchar(const wxChar
*format
, ...);
331 #if wxUSE_UNICODE_UTF8
332 int DoPrintfUtf8(const char *format
, ...);
333 static wxString
DoFormatUtf8(const char *format
, ...);
337 // this class contains template wrappers for wxString's vararg methods, it's
338 // intentionally *not* exported from the DLL in order to fix the VC6 bug
340 class wxStringPrintfMixin
: public wxStringPrintfMixinBase
343 // to further complicate things, we can't return wxString from
344 // wxStringPrintfMixin::Format() because wxString is not yet declared at
345 // this point; the solution is to use this fake type trait template - this
346 // way the compiler won't know the return type until Format() is used
347 // (this doesn't compile with Watcom, but VC6 compiles it just fine):
348 template<typename T
> struct StringReturnType
350 typedef wxString type
;
354 // these are duplicated wxString methods, they're also declared below
355 // if !wxNEEDS_WXSTRING_PRINTF_MIXIN:
357 // static wxString Format(const wString& format, ...) ATTRIBUTE_PRINTF_1;
358 WX_DEFINE_VARARG_FUNC_SANS_N0(static typename StringReturnType
<T1
>::type
,
359 Format
, 1, (const wxFormatString
&),
360 DoFormatWchar
, DoFormatUtf8
)
361 // We have to implement the version without template arguments manually
362 // because of the StringReturnType<> hack, although WX_DEFINE_VARARG_FUNC
363 // normally does it itself. It has to be a template so that we can use
364 // the hack, even though there's no real template parameter. We can't move
365 // it to wxStrig, because it would shadow these versions of Format() then.
367 inline static typename StringReturnType
<T
>::type
370 // NB: this doesn't compile if T is not (some form of) a string;
371 // this makes Format's prototype equivalent to
372 // Format(const wxFormatString& fmt)
373 return DoFormatWchar(wxFormatString(fmt
));
376 // int Printf(const wxString& format, ...);
377 WX_DEFINE_VARARG_FUNC(int, Printf
, 1, (const wxFormatString
&),
378 DoPrintfWchar
, DoPrintfUtf8
)
379 // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2;
380 WX_DEFINE_VARARG_FUNC(int, sprintf
, 1, (const wxFormatString
&),
381 DoPrintfWchar
, DoPrintfUtf8
)
384 wxStringPrintfMixin() : wxStringPrintfMixinBase() {}
386 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
389 // ----------------------------------------------------------------------------
390 // wxString: string class trying to be compatible with std::string, MFC
391 // CString and wxWindows 1.x wxString all at once
392 // ---------------------------------------------------------------------------
394 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
395 // "non dll-interface class 'wxStringPrintfMixin' used as base interface
396 // for dll-interface class 'wxString'" -- this is OK in our case
397 #pragma warning (disable:4275)
400 #if wxUSE_UNICODE_UTF8
401 // see the comment near wxString::iterator for why we need this
402 class WXDLLIMPEXP_BASE wxStringIteratorNode
405 wxStringIteratorNode()
406 : m_str(NULL
), m_citer(NULL
), m_iter(NULL
), m_prev(NULL
), m_next(NULL
) {}
407 wxStringIteratorNode(const wxString
*str
,
408 wxStringImpl::const_iterator
*citer
)
409 { DoSet(str
, citer
, NULL
); }
410 wxStringIteratorNode(const wxString
*str
, wxStringImpl::iterator
*iter
)
411 { DoSet(str
, NULL
, iter
); }
412 ~wxStringIteratorNode()
415 inline void set(const wxString
*str
, wxStringImpl::const_iterator
*citer
)
416 { clear(); DoSet(str
, citer
, NULL
); }
417 inline void set(const wxString
*str
, wxStringImpl::iterator
*iter
)
418 { clear(); DoSet(str
, NULL
, iter
); }
420 const wxString
*m_str
;
421 wxStringImpl::const_iterator
*m_citer
;
422 wxStringImpl::iterator
*m_iter
;
423 wxStringIteratorNode
*m_prev
, *m_next
;
427 inline void DoSet(const wxString
*str
,
428 wxStringImpl::const_iterator
*citer
,
429 wxStringImpl::iterator
*iter
);
431 // the node belongs to a particular iterator instance, it's not copied
432 // when a copy of the iterator is made
433 DECLARE_NO_COPY_CLASS(wxStringIteratorNode
)
435 #endif // wxUSE_UNICODE_UTF8
437 class WXDLLIMPEXP_BASE wxString
438 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
439 : public wxStringPrintfMixin
442 // NB: special care was taken in arranging the member functions in such order
443 // that all inline functions can be effectively inlined, verify that all
444 // performance critical functions are still inlined if you change order!
446 // an 'invalid' value for string index, moved to this place due to a CW bug
447 static const size_t npos
;
450 // if we hadn't made these operators private, it would be possible to
451 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
452 // converted to char in C and we do have operator=(char)
454 // NB: we don't need other versions (short/long and unsigned) as attempt
455 // to assign another numeric type to wxString will now result in
456 // ambiguity between operator=(char) and operator=(int)
457 wxString
& operator=(int);
459 // these methods are not implemented - there is _no_ conversion from int to
460 // string, you're doing something wrong if the compiler wants to call it!
462 // try `s << i' or `s.Printf("%d", i)' instead
466 // buffer for holding temporary substring when using any of the methods
467 // that take (char*,size_t) or (wchar_t*,size_t) arguments:
469 struct SubstrBufFromType
474 SubstrBufFromType(const T
& data_
, size_t len_
)
475 : data(data_
), len(len_
)
477 wxASSERT_MSG( len
!= npos
, "must have real length" );
481 #if wxUSE_UNICODE_UTF8
482 // even char* -> char* needs conversion, from locale charset to UTF-8
483 typedef SubstrBufFromType
<wxCharBuffer
> SubstrBufFromWC
;
484 typedef SubstrBufFromType
<wxCharBuffer
> SubstrBufFromMB
;
485 #elif wxUSE_UNICODE_WCHAR
486 typedef SubstrBufFromType
<const wchar_t*> SubstrBufFromWC
;
487 typedef SubstrBufFromType
<wxWCharBuffer
> SubstrBufFromMB
;
489 typedef SubstrBufFromType
<const char*> SubstrBufFromMB
;
490 typedef SubstrBufFromType
<wxCharBuffer
> SubstrBufFromWC
;
494 // Functions implementing primitive operations on string data; wxString
495 // methods and iterators are implemented in terms of it. The differences
496 // between UTF-8 and wchar_t* representations of the string are mostly
499 #if wxUSE_UNICODE_UTF8
500 static SubstrBufFromMB
ConvertStr(const char *psz
, size_t nLength
,
501 const wxMBConv
& conv
);
502 static SubstrBufFromWC
ConvertStr(const wchar_t *pwz
, size_t nLength
,
503 const wxMBConv
& conv
);
504 #elif wxUSE_UNICODE_WCHAR
505 static SubstrBufFromMB
ConvertStr(const char *psz
, size_t nLength
,
506 const wxMBConv
& conv
);
508 static SubstrBufFromWC
ConvertStr(const wchar_t *pwz
, size_t nLength
,
509 const wxMBConv
& conv
);
512 #if !wxUSE_UNICODE_UTF8 // wxUSE_UNICODE_WCHAR or !wxUSE_UNICODE
513 // returns C string encoded as the implementation expects:
515 static const wchar_t* ImplStr(const wchar_t* str
)
516 { return str
? str
: wxT(""); }
517 static const SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
518 { return SubstrBufFromWC(str
, (str
&& n
== npos
) ? wxWcslen(str
) : n
); }
519 static wxWCharBuffer
ImplStr(const char* str
,
520 const wxMBConv
& conv
= wxConvLibc
)
521 { return ConvertStr(str
, npos
, conv
).data
; }
522 static SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
523 const wxMBConv
& conv
= wxConvLibc
)
524 { return ConvertStr(str
, n
, conv
); }
526 static const char* ImplStr(const char* str
,
527 const wxMBConv
& WXUNUSED(conv
) = wxConvLibc
)
528 { return str
? str
: ""; }
529 static const SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
530 const wxMBConv
& WXUNUSED(conv
) = wxConvLibc
)
531 { return SubstrBufFromMB(str
, (str
&& n
== npos
) ? wxStrlen(str
) : n
); }
532 static wxCharBuffer
ImplStr(const wchar_t* str
)
533 { return ConvertStr(str
, npos
, wxConvLibc
).data
; }
534 static SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
535 { return ConvertStr(str
, n
, wxConvLibc
); }
538 // translates position index in wxString to/from index in underlying
540 static size_t PosToImpl(size_t pos
) { return pos
; }
541 static void PosLenToImpl(size_t pos
, size_t len
,
542 size_t *implPos
, size_t *implLen
)
543 { *implPos
= pos
; *implLen
= len
; }
544 static size_t LenToImpl(size_t len
) { return len
; }
545 static size_t PosFromImpl(size_t pos
) { return pos
; }
547 // we don't want to define these as empty inline functions as it could
548 // result in noticeable (and quite unnecessary in non-UTF-8 build) slowdown
549 // in debug build where the inline functions are not effectively inlined
550 #define wxSTRING_INVALIDATE_CACHE()
551 #define wxSTRING_INVALIDATE_CACHED_LENGTH()
552 #define wxSTRING_UPDATE_CACHED_LENGTH(n)
553 #define wxSTRING_SET_CACHED_LENGTH(n)
555 #else // wxUSE_UNICODE_UTF8
557 static wxCharBuffer
ImplStr(const char* str
,
558 const wxMBConv
& conv
= wxConvLibc
)
559 { return ConvertStr(str
, npos
, conv
).data
; }
560 static SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
561 const wxMBConv
& conv
= wxConvLibc
)
562 { return ConvertStr(str
, n
, conv
); }
564 static wxCharBuffer
ImplStr(const wchar_t* str
)
565 { return ConvertStr(str
, npos
, wxMBConvUTF8()).data
; }
566 static SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
567 { return ConvertStr(str
, n
, wxMBConvUTF8()); }
569 #if wxUSE_STRING_POS_CACHE
570 // this is an extremely simple cache used by PosToImpl(): each cache element
571 // contains the string it applies to and the index corresponding to the last
572 // used position in this wxString in its m_impl string
574 // NB: notice that this struct (and nested Element one) must be a POD or we
575 // wouldn't be able to use a thread-local variable of this type, in
576 // particular it should have no ctor -- we rely on statics being
577 // initialized to 0 instead
584 const wxString
*str
; // the string to which this element applies
585 size_t pos
, // the cached index in this string
586 impl
, // the corresponding position in its m_impl
587 len
; // cached length or npos if unknown
589 // reset cached index to 0
590 void ResetPos() { pos
= impl
= 0; }
592 // reset position and length
593 void Reset() { ResetPos(); len
= npos
; }
596 // cache the indices mapping for the last few string used
597 Element cached
[SIZE
];
599 // the last used index
603 static wxTLS_TYPE(Cache
) ms_cache
;
605 friend struct wxStrCacheDumper
;
607 // uncomment this to have access to some profiling statistics on program
609 //#define wxPROFILE_STRING_CACHE
611 #ifdef wxPROFILE_STRING_CACHE
612 static struct PosToImplCacheStats
614 unsigned postot
, // total non-trivial calls to PosToImpl
615 poshits
, // cache hits from PosToImpl()
616 mishits
, // cached position beyond the needed one
617 sumpos
, // sum of all positions, used to compute the
618 // average position after dividing by postot
619 sumofs
, // sum of all offsets after using the cache, used to
620 // compute the average after dividing by hits
621 lentot
, // number of total calls to length()
622 lenhits
; // number of cache hits in length()
625 friend struct ShowCacheStats
;
627 #define wxCACHE_PROFILE_FIELD_INC(field) ms_cacheStats.field++
628 #define wxCACHE_PROFILE_FIELD_ADD(field, val) ms_cacheStats.field += (val)
629 #else // !wxPROFILE_STRING_CACHE
630 #define wxCACHE_PROFILE_FIELD_INC(field)
631 #define wxCACHE_PROFILE_FIELD_ADD(field, val)
632 #endif // wxPROFILE_STRING_CACHE/!wxPROFILE_STRING_CACHE
634 // note: it could seem that the functions below shouldn't be inline because
635 // they are big, contain loops and so the compiler shouldn't be able to
636 // inline them anyhow, however moving them into string.cpp does decrease the
637 // code performance by ~5%, at least when using g++ 4.1 so do keep them here
638 // unless tests show that it's not advantageous any more
640 // return the pointer to the cache element for this string or NULL if not
642 Cache::Element
*FindCacheElement() const
644 // profiling seems to show a small but consistent gain if we use this
645 // simple loop instead of starting from the last used element (there are
646 // a lot of misses in this function...)
647 for ( Cache::Element
*c
= ms_cache
.cached
;
648 c
!= ms_cache
.cached
+ Cache::SIZE
;
651 if ( c
->str
== this )
658 // unlike FindCacheElement(), this one always returns a valid pointer to the
659 // cache element for this string, it may have valid last cached position and
660 // its corresponding index in the byte string or not
661 Cache::Element
*GetCacheElement() const
663 Cache::Element
* const cacheBegin
= ms_cache
.cached
;
664 Cache::Element
* const cacheEnd
= ms_cache
.cached
+ Cache::SIZE
;
665 Cache::Element
* const cacheStart
= cacheBegin
+ ms_cache
.lastUsed
;
667 // check the last used first, this does no (measurable) harm for a miss
668 // but does help for simple loops addressing the same string all the time
669 if ( cacheStart
->str
== this )
672 // notice that we're going to check cacheStart again inside this call but
673 // profiling shows that it's still faster to use a simple loop like
674 // inside FindCacheElement() than manually looping with wrapping starting
675 // from the cache entry after the start one
676 Cache::Element
*c
= FindCacheElement();
679 // claim the next cache entry for this string
681 if ( ++c
== cacheEnd
)
687 // and remember the last used element
688 ms_cache
.lastUsed
= c
- cacheBegin
;
694 size_t DoPosToImpl(size_t pos
) const
696 wxCACHE_PROFILE_FIELD_INC(postot
);
698 // NB: although the case of pos == 1 (and offset from cached position
699 // equal to 1) are common, nothing is gained by writing special code
700 // for handling them, the compiler (at least g++ 4.1 used) seems to
701 // optimize the code well enough on its own
703 wxCACHE_PROFILE_FIELD_ADD(sumpos
, pos
);
705 Cache::Element
* const cache
= GetCacheElement();
707 // cached position can't be 0 so if it is, it means that this entry was
708 // used for length caching only so far, i.e. it doesn't count as a hit
709 // from our point of view
711 wxCACHE_PROFILE_FIELD_INC(poshits
);
713 if ( pos
== cache
->pos
)
716 // this seems to happen only rarely so just reset the cache in this case
717 // instead of complicating code even further by seeking backwards in this
719 if ( cache
->pos
> pos
)
721 wxCACHE_PROFILE_FIELD_INC(mishits
);
726 wxCACHE_PROFILE_FIELD_ADD(sumofs
, pos
- cache
->pos
);
729 wxStringImpl::const_iterator
i(m_impl
.begin() + cache
->impl
);
730 for ( size_t n
= cache
->pos
; n
< pos
; n
++ )
731 wxStringOperations::IncIter(i
);
734 cache
->impl
= i
- m_impl
.begin();
736 wxSTRING_CACHE_ASSERT(
737 (int)cache
->impl
== (begin() + pos
).impl() - m_impl
.begin() );
742 void InvalidateCache()
744 Cache::Element
* const cache
= FindCacheElement();
749 void InvalidateCachedLength()
751 Cache::Element
* const cache
= FindCacheElement();
756 void SetCachedLength(size_t len
)
758 // we optimistically cache the length here even if the string wasn't
759 // present in the cache before, this seems to do no harm and the
760 // potential for avoiding length recomputation for long strings looks
762 GetCacheElement()->len
= len
;
765 void UpdateCachedLength(ptrdiff_t delta
)
767 Cache::Element
* const cache
= FindCacheElement();
768 if ( cache
&& cache
->len
!= npos
)
770 wxSTRING_CACHE_ASSERT( (ptrdiff_t)cache
->len
+ delta
>= 0 );
776 #define wxSTRING_INVALIDATE_CACHE() InvalidateCache()
777 #define wxSTRING_INVALIDATE_CACHED_LENGTH() InvalidateCachedLength()
778 #define wxSTRING_UPDATE_CACHED_LENGTH(n) UpdateCachedLength(n)
779 #define wxSTRING_SET_CACHED_LENGTH(n) SetCachedLength(n)
780 #else // !wxUSE_STRING_POS_CACHE
781 size_t DoPosToImpl(size_t pos
) const
783 return (begin() + pos
).impl() - m_impl
.begin();
786 #define wxSTRING_INVALIDATE_CACHE()
787 #define wxSTRING_INVALIDATE_CACHED_LENGTH()
788 #define wxSTRING_UPDATE_CACHED_LENGTH(n)
789 #define wxSTRING_SET_CACHED_LENGTH(n)
790 #endif // wxUSE_STRING_POS_CACHE/!wxUSE_STRING_POS_CACHE
792 size_t PosToImpl(size_t pos
) const
794 return pos
== 0 || pos
== npos
? pos
: DoPosToImpl(pos
);
797 void PosLenToImpl(size_t pos
, size_t len
, size_t *implPos
, size_t *implLen
) const;
799 size_t LenToImpl(size_t len
) const
802 PosLenToImpl(0, len
, &pos
, &len2
);
806 size_t PosFromImpl(size_t pos
) const
808 if ( pos
== 0 || pos
== npos
)
811 return const_iterator(this, m_impl
.begin() + pos
) - begin();
813 #endif // !wxUSE_UNICODE_UTF8/wxUSE_UNICODE_UTF8
817 typedef wxUniChar value_type
;
818 typedef wxUniChar char_type
;
819 typedef wxUniCharRef reference
;
820 typedef wxChar
* pointer
;
821 typedef const wxChar
* const_pointer
;
823 typedef size_t size_type
;
824 typedef wxUniChar const_reference
;
827 #if wxUSE_UNICODE_UTF8
828 // random access is not O(1), as required by Random Access Iterator
829 #define WX_STR_ITERATOR_TAG std::bidirectional_iterator_tag
831 #define WX_STR_ITERATOR_TAG std::random_access_iterator_tag
834 #define WX_STR_ITERATOR_TAG void /* dummy type */
837 #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, reference_type) \
839 typedef wxStringImpl::iterator_name underlying_iterator; \
841 typedef WX_STR_ITERATOR_TAG iterator_category; \
842 typedef wxUniChar value_type; \
843 typedef int difference_type; \
844 typedef reference_type reference; \
845 typedef pointer_type pointer; \
847 reference operator[](size_t n) const { return *(*this + n); } \
849 iterator_name& operator++() \
850 { wxStringOperations::IncIter(m_cur); return *this; } \
851 iterator_name& operator--() \
852 { wxStringOperations::DecIter(m_cur); return *this; } \
853 iterator_name operator++(int) \
855 iterator_name tmp = *this; \
856 wxStringOperations::IncIter(m_cur); \
859 iterator_name operator--(int) \
861 iterator_name tmp = *this; \
862 wxStringOperations::DecIter(m_cur); \
866 iterator_name& operator+=(ptrdiff_t n) \
868 m_cur = wxStringOperations::AddToIter(m_cur, n); \
871 iterator_name& operator-=(ptrdiff_t n) \
873 m_cur = wxStringOperations::AddToIter(m_cur, -n); \
877 difference_type operator-(const iterator_name& i) const \
878 { return wxStringOperations::DiffIters(m_cur, i.m_cur); } \
880 bool operator==(const iterator_name& i) const \
881 { return m_cur == i.m_cur; } \
882 bool operator!=(const iterator_name& i) const \
883 { return m_cur != i.m_cur; } \
885 bool operator<(const iterator_name& i) const \
886 { return m_cur < i.m_cur; } \
887 bool operator>(const iterator_name& i) const \
888 { return m_cur > i.m_cur; } \
889 bool operator<=(const iterator_name& i) const \
890 { return m_cur <= i.m_cur; } \
891 bool operator>=(const iterator_name& i) const \
892 { return m_cur >= i.m_cur; } \
895 /* for internal wxString use only: */ \
896 underlying_iterator impl() const { return m_cur; } \
898 friend class wxString; \
899 friend class wxCStrData; \
902 underlying_iterator m_cur
904 class WXDLLIMPEXP_FWD_BASE const_iterator
;
906 #if wxUSE_UNICODE_UTF8
907 // NB: In UTF-8 build, (non-const) iterator needs to keep reference
908 // to the underlying wxStringImpl, because UTF-8 is variable-length
909 // encoding and changing the value pointer to by an iterator (using
910 // its operator*) requires calling wxStringImpl::replace() if the old
911 // and new values differ in their encoding's length.
913 // Furthermore, the replace() call may invalid all iterators for the
914 // string, so we have to keep track of outstanding iterators and update
915 // them if replace() happens.
917 // This is implemented by maintaining linked list of iterators for every
918 // string and traversing it in wxUniCharRef::operator=(). Head of the
919 // list is stored in wxString. (FIXME-UTF8)
921 class WXDLLIMPEXP_BASE iterator
923 WX_STR_ITERATOR_IMPL(iterator
, wxChar
*, wxUniCharRef
);
927 iterator(const iterator
& i
)
928 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
929 iterator
& operator=(const iterator
& i
)
934 m_node
.set(i
.str(), &m_cur
);
939 reference
operator*()
940 { return wxUniCharRef::CreateForString(*str(), m_cur
); }
942 iterator
operator+(ptrdiff_t n
) const
943 { return iterator(str(), wxStringOperations::AddToIter(m_cur
, n
)); }
944 iterator
operator-(ptrdiff_t n
) const
945 { return iterator(str(), wxStringOperations::AddToIter(m_cur
, -n
)); }
948 iterator(wxString
*str
, underlying_iterator ptr
)
949 : m_cur(ptr
), m_node(str
, &m_cur
) {}
951 wxString
* str() const { return wx_const_cast(wxString
*, m_node
.m_str
); }
953 wxStringIteratorNode m_node
;
955 friend class const_iterator
;
958 class WXDLLIMPEXP_BASE const_iterator
960 // NB: reference_type is intentionally value, not reference, the character
961 // may be encoded differently in wxString data:
962 WX_STR_ITERATOR_IMPL(const_iterator
, const wxChar
*, wxUniChar
);
966 const_iterator(const const_iterator
& i
)
967 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
968 const_iterator(const iterator
& i
)
969 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
971 const_iterator
& operator=(const const_iterator
& i
)
976 m_node
.set(i
.str(), &m_cur
);
980 const_iterator
& operator=(const iterator
& i
)
981 { m_cur
= i
.m_cur
; m_node
.set(i
.str(), &m_cur
); return *this; }
983 reference
operator*() const
984 { return wxStringOperations::DecodeChar(m_cur
); }
986 const_iterator
operator+(ptrdiff_t n
) const
987 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur
, n
)); }
988 const_iterator
operator-(ptrdiff_t n
) const
989 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur
, -n
)); }
992 // for internal wxString use only:
993 const_iterator(const wxString
*str
, underlying_iterator ptr
)
994 : m_cur(ptr
), m_node(str
, &m_cur
) {}
996 const wxString
* str() const { return m_node
.m_str
; }
998 wxStringIteratorNode m_node
;
1001 size_t IterToImplPos(wxString::iterator i
) const
1002 { return wxStringImpl::const_iterator(i
.impl()) - m_impl
.begin(); }
1004 iterator
GetIterForNthChar(size_t n
)
1005 { return iterator(this, m_impl
.begin() + PosToImpl(n
)); }
1006 const_iterator
GetIterForNthChar(size_t n
) const
1007 { return const_iterator(this, m_impl
.begin() + PosToImpl(n
)); }
1008 #else // !wxUSE_UNICODE_UTF8
1010 class WXDLLIMPEXP_BASE iterator
1012 WX_STR_ITERATOR_IMPL(iterator
, wxChar
*, wxUniCharRef
);
1016 iterator(const iterator
& i
) : m_cur(i
.m_cur
) {}
1018 reference
operator*()
1019 { return wxUniCharRef::CreateForString(m_cur
); }
1021 iterator
operator+(ptrdiff_t n
) const
1022 { return iterator(wxStringOperations::AddToIter(m_cur
, n
)); }
1023 iterator
operator-(ptrdiff_t n
) const
1024 { return iterator(wxStringOperations::AddToIter(m_cur
, -n
)); }
1027 // for internal wxString use only:
1028 iterator(underlying_iterator ptr
) : m_cur(ptr
) {}
1029 iterator(wxString
*WXUNUSED(str
), underlying_iterator ptr
) : m_cur(ptr
) {}
1031 friend class const_iterator
;
1034 class WXDLLIMPEXP_BASE const_iterator
1036 // NB: reference_type is intentionally value, not reference, the character
1037 // may be encoded differently in wxString data:
1038 WX_STR_ITERATOR_IMPL(const_iterator
, const wxChar
*, wxUniChar
);
1042 const_iterator(const const_iterator
& i
) : m_cur(i
.m_cur
) {}
1043 const_iterator(const iterator
& i
) : m_cur(i
.m_cur
) {}
1045 reference
operator*() const
1046 { return wxStringOperations::DecodeChar(m_cur
); }
1048 const_iterator
operator+(ptrdiff_t n
) const
1049 { return const_iterator(wxStringOperations::AddToIter(m_cur
, n
)); }
1050 const_iterator
operator-(ptrdiff_t n
) const
1051 { return const_iterator(wxStringOperations::AddToIter(m_cur
, -n
)); }
1054 // for internal wxString use only:
1055 const_iterator(underlying_iterator ptr
) : m_cur(ptr
) {}
1056 const_iterator(const wxString
*WXUNUSED(str
), underlying_iterator ptr
)
1060 iterator
GetIterForNthChar(size_t n
) { return begin() + n
; }
1061 const_iterator
GetIterForNthChar(size_t n
) const { return begin() + n
; }
1062 #endif // wxUSE_UNICODE_UTF8/!wxUSE_UNICODE_UTF8
1064 #undef WX_STR_ITERATOR_TAG
1065 #undef WX_STR_ITERATOR_IMPL
1067 friend class iterator
;
1068 friend class const_iterator
;
1070 template <typename T
>
1071 class reverse_iterator_impl
1074 typedef T iterator_type
;
1076 typedef typename
T::iterator_category iterator_category
;
1077 typedef typename
T::value_type value_type
;
1078 typedef typename
T::difference_type difference_type
;
1079 typedef typename
T::reference reference
;
1080 typedef typename
T::pointer
*pointer
;
1082 reverse_iterator_impl() {}
1083 reverse_iterator_impl(iterator_type i
) : m_cur(i
) {}
1084 reverse_iterator_impl(const reverse_iterator_impl
& ri
)
1085 : m_cur(ri
.m_cur
) {}
1087 iterator_type
base() const { return m_cur
; }
1089 reference
operator*() const { return *(m_cur
-1); }
1090 reference
operator[](size_t n
) const { return *(*this + n
); }
1092 reverse_iterator_impl
& operator++()
1093 { --m_cur
; return *this; }
1094 reverse_iterator_impl
operator++(int)
1095 { reverse_iterator_impl tmp
= *this; --m_cur
; return tmp
; }
1096 reverse_iterator_impl
& operator--()
1097 { ++m_cur
; return *this; }
1098 reverse_iterator_impl
operator--(int)
1099 { reverse_iterator_impl tmp
= *this; ++m_cur
; return tmp
; }
1101 // NB: explicit <T> in the functions below is to keep BCC 5.5 happy
1102 reverse_iterator_impl
operator+(ptrdiff_t n
) const
1103 { return reverse_iterator_impl
<T
>(m_cur
- n
); }
1104 reverse_iterator_impl
operator-(ptrdiff_t n
) const
1105 { return reverse_iterator_impl
<T
>(m_cur
+ n
); }
1106 reverse_iterator_impl
operator+=(ptrdiff_t n
)
1107 { m_cur
-= n
; return *this; }
1108 reverse_iterator_impl
operator-=(ptrdiff_t n
)
1109 { m_cur
+= n
; return *this; }
1111 unsigned operator-(const reverse_iterator_impl
& i
) const
1112 { return i
.m_cur
- m_cur
; }
1114 bool operator==(const reverse_iterator_impl
& ri
) const
1115 { return m_cur
== ri
.m_cur
; }
1116 bool operator!=(const reverse_iterator_impl
& ri
) const
1117 { return !(*this == ri
); }
1119 bool operator<(const reverse_iterator_impl
& i
) const
1120 { return m_cur
> i
.m_cur
; }
1121 bool operator>(const reverse_iterator_impl
& i
) const
1122 { return m_cur
< i
.m_cur
; }
1123 bool operator<=(const reverse_iterator_impl
& i
) const
1124 { return m_cur
>= i
.m_cur
; }
1125 bool operator>=(const reverse_iterator_impl
& i
) const
1126 { return m_cur
<= i
.m_cur
; }
1129 iterator_type m_cur
;
1132 typedef reverse_iterator_impl
<iterator
> reverse_iterator
;
1133 typedef reverse_iterator_impl
<const_iterator
> const_reverse_iterator
;
1136 // used to transform an expression built using c_str() (and hence of type
1137 // wxCStrData) to an iterator into the string
1138 static const_iterator
CreateConstIterator(const wxCStrData
& data
)
1140 return const_iterator(data
.m_str
,
1141 (data
.m_str
->begin() + data
.m_offset
).impl());
1144 // in UTF-8 STL build, creation from std::string requires conversion under
1145 // non-UTF8 locales, so we can't have and use wxString(wxStringImpl) ctor;
1146 // instead we define dummy type that lets us have wxString ctor for creation
1147 // from wxStringImpl that couldn't be used by user code (in all other builds,
1148 // "standard" ctors can be used):
1149 #if wxUSE_UNICODE_UTF8 && wxUSE_STL_BASED_WXSTRING
1150 struct CtorFromStringImplTag
{};
1152 wxString(CtorFromStringImplTag
* WXUNUSED(dummy
), const wxStringImpl
& src
)
1155 static wxString
FromImpl(const wxStringImpl
& src
)
1156 { return wxString((CtorFromStringImplTag
*)NULL
, src
); }
1158 #if !wxUSE_STL_BASED_WXSTRING
1159 wxString(const wxStringImpl
& src
) : m_impl(src
) { }
1160 // else: already defined as wxString(wxStdString) below
1162 static wxString
FromImpl(const wxStringImpl
& src
) { return wxString(src
); }
1166 // constructors and destructor
1167 // ctor for an empty string
1171 wxString(const wxString
& stringSrc
) : m_impl(stringSrc
.m_impl
) { }
1173 // string containing nRepeat copies of ch
1174 wxString(wxUniChar ch
, size_t nRepeat
= 1 )
1175 { assign(nRepeat
, ch
); }
1176 wxString(size_t nRepeat
, wxUniChar ch
)
1177 { assign(nRepeat
, ch
); }
1178 wxString(wxUniCharRef ch
, size_t nRepeat
= 1)
1179 { assign(nRepeat
, ch
); }
1180 wxString(size_t nRepeat
, wxUniCharRef ch
)
1181 { assign(nRepeat
, ch
); }
1182 wxString(char ch
, size_t nRepeat
= 1)
1183 { assign(nRepeat
, ch
); }
1184 wxString(size_t nRepeat
, char ch
)
1185 { assign(nRepeat
, ch
); }
1186 wxString(wchar_t ch
, size_t nRepeat
= 1)
1187 { assign(nRepeat
, ch
); }
1188 wxString(size_t nRepeat
, wchar_t ch
)
1189 { assign(nRepeat
, ch
); }
1191 // ctors from char* strings:
1192 wxString(const char *psz
)
1193 : m_impl(ImplStr(psz
)) {}
1194 wxString(const char *psz
, const wxMBConv
& conv
)
1195 : m_impl(ImplStr(psz
, conv
)) {}
1196 wxString(const char *psz
, size_t nLength
)
1197 { assign(psz
, nLength
); }
1198 wxString(const char *psz
, const wxMBConv
& conv
, size_t nLength
)
1200 SubstrBufFromMB
str(ImplStr(psz
, nLength
, conv
));
1201 m_impl
.assign(str
.data
, str
.len
);
1204 // and unsigned char*:
1205 wxString(const unsigned char *psz
)
1206 : m_impl(ImplStr((const char*)psz
)) {}
1207 wxString(const unsigned char *psz
, const wxMBConv
& conv
)
1208 : m_impl(ImplStr((const char*)psz
, conv
)) {}
1209 wxString(const unsigned char *psz
, size_t nLength
)
1210 { assign((const char*)psz
, nLength
); }
1211 wxString(const unsigned char *psz
, const wxMBConv
& conv
, size_t nLength
)
1213 SubstrBufFromMB
str(ImplStr((const char*)psz
, nLength
, conv
));
1214 m_impl
.assign(str
.data
, str
.len
);
1217 // ctors from wchar_t* strings:
1218 wxString(const wchar_t *pwz
)
1219 : m_impl(ImplStr(pwz
)) {}
1220 wxString(const wchar_t *pwz
, const wxMBConv
& WXUNUSED(conv
))
1221 : m_impl(ImplStr(pwz
)) {}
1222 wxString(const wchar_t *pwz
, size_t nLength
)
1223 { assign(pwz
, nLength
); }
1224 wxString(const wchar_t *pwz
, const wxMBConv
& WXUNUSED(conv
), size_t nLength
)
1225 { assign(pwz
, nLength
); }
1227 wxString(const wxCharBuffer
& buf
)
1228 { assign(buf
.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
1229 wxString(const wxWCharBuffer
& buf
)
1230 { assign(buf
.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
1232 // NB: this version uses m_impl.c_str() to force making a copy of the
1233 // string, so that "wxString(str.c_str())" idiom for passing strings
1234 // between threads works
1235 wxString(const wxCStrData
& cstr
)
1236 : m_impl(cstr
.AsString().m_impl
.c_str()) { }
1238 // as we provide both ctors with this signature for both char and unsigned
1239 // char string, we need to provide one for wxCStrData to resolve ambiguity
1240 wxString(const wxCStrData
& cstr
, size_t nLength
)
1241 : m_impl(cstr
.AsString().Mid(0, nLength
).m_impl
) {}
1243 // and because wxString is convertible to wxCStrData and const wxChar *
1244 // we also need to provide this one
1245 wxString(const wxString
& str
, size_t nLength
)
1246 { assign(str
, nLength
); }
1249 #if wxUSE_STRING_POS_CACHE
1252 // we need to invalidate our cache entry as another string could be
1253 // recreated at the same address (unlikely, but still possible, with the
1254 // heap-allocated strings but perfectly common with stack-allocated ones)
1257 #endif // wxUSE_STRING_POS_CACHE
1259 // even if we're not built with wxUSE_STL == 1 it is very convenient to allow
1260 // implicit conversions from std::string to wxString and vice verse as this
1261 // allows to use the same strings in non-GUI and GUI code, however we don't
1262 // want to unconditionally add this ctor as it would make wx lib dependent on
1263 // libstdc++ on some Linux versions which is bad, so instead we ask the
1264 // client code to define this wxUSE_STD_STRING symbol if they need it
1265 #if wxUSE_STD_STRING
1266 #if wxUSE_UNICODE_WCHAR
1267 wxString(const wxStdWideString
& str
) : m_impl(str
) {}
1268 #else // UTF-8 or ANSI
1269 wxString(const wxStdWideString
& str
)
1270 { assign(str
.c_str(), str
.length()); }
1273 #if !wxUSE_UNICODE // ANSI build
1274 // FIXME-UTF8: do this in UTF8 build #if wxUSE_UTF8_LOCALE_ONLY, too
1275 wxString(const std::string
& str
) : m_impl(str
) {}
1277 wxString(const std::string
& str
)
1278 { assign(str
.c_str(), str
.length()); }
1280 #endif // wxUSE_STD_STRING
1282 // Unlike ctor from std::string, we provide conversion to std::string only
1283 // if wxUSE_STL and not merely wxUSE_STD_STRING (which is on by default),
1284 // because it conflicts with operator const char/wchar_t*:
1286 #if wxUSE_UNICODE_WCHAR && wxUSE_STL_BASED_WXSTRING
1287 // wxStringImpl is std::string in the encoding we want
1288 operator const wxStdWideString
&() const { return m_impl
; }
1290 // wxStringImpl is either not std::string or needs conversion
1291 operator wxStdWideString() const
1292 // FIXME-UTF8: broken for embedded NULs
1293 { return wxStdWideString(wc_str()); }
1296 #if (!wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY) && wxUSE_STL_BASED_WXSTRING
1297 // wxStringImpl is std::string in the encoding we want
1298 operator const std::string
&() const { return m_impl
; }
1300 // wxStringImpl is either not std::string or needs conversion
1301 operator std::string() const
1302 // FIXME-UTF8: broken for embedded NULs
1303 { return std::string(mb_str()); }
1307 wxString
Clone() const
1309 // make a deep copy of the string, i.e. the returned string will have
1310 // ref count = 1 with refcounted implementation
1311 return wxString::FromImpl(wxStringImpl(m_impl
.c_str(), m_impl
.length()));
1314 // first valid index position
1315 const_iterator
begin() const { return const_iterator(this, m_impl
.begin()); }
1316 iterator
begin() { return iterator(this, m_impl
.begin()); }
1317 // position one after the last valid one
1318 const_iterator
end() const { return const_iterator(this, m_impl
.end()); }
1319 iterator
end() { return iterator(this, m_impl
.end()); }
1321 // first element of the reversed string
1322 const_reverse_iterator
rbegin() const
1323 { return const_reverse_iterator(end()); }
1324 reverse_iterator
rbegin()
1325 { return reverse_iterator(end()); }
1326 // one beyond the end of the reversed string
1327 const_reverse_iterator
rend() const
1328 { return const_reverse_iterator(begin()); }
1329 reverse_iterator
rend()
1330 { return reverse_iterator(begin()); }
1332 // std::string methods:
1333 #if wxUSE_UNICODE_UTF8
1334 size_t length() const
1336 #if wxUSE_STRING_POS_CACHE
1337 wxCACHE_PROFILE_FIELD_INC(lentot
);
1339 Cache::Element
* const cache
= GetCacheElement();
1341 if ( cache
->len
== npos
)
1343 // it's probably not worth trying to be clever and using cache->pos
1344 // here as it's probably 0 anyhow -- you usually call length() before
1345 // starting to index the string
1346 cache
->len
= end() - begin();
1350 wxCACHE_PROFILE_FIELD_INC(lenhits
);
1352 wxSTRING_CACHE_ASSERT( (int)cache
->len
== end() - begin() );
1356 #else // !wxUSE_STRING_POS_CACHE
1357 return end() - begin();
1358 #endif // wxUSE_STRING_POS_CACHE/!wxUSE_STRING_POS_CACHE
1361 size_t length() const { return m_impl
.length(); }
1364 size_type
size() const { return length(); }
1365 size_type
max_size() const { return npos
; }
1367 bool empty() const { return m_impl
.empty(); }
1369 // NB: these methods don't have a well-defined meaning in UTF-8 case
1370 size_type
capacity() const { return m_impl
.capacity(); }
1371 void reserve(size_t sz
) { m_impl
.reserve(sz
); }
1373 void resize(size_t nSize
, wxUniChar ch
= wxT('\0'))
1375 const size_t len
= length();
1379 #if wxUSE_UNICODE_UTF8
1382 wxSTRING_INVALIDATE_CACHE();
1384 // we can't use wxStringImpl::resize() for truncating the string as it
1385 // counts in bytes, not characters
1390 // we also can't use (presumably more efficient) resize() if we have to
1391 // append characters taking more than one byte
1392 if ( !ch
.IsAscii() )
1394 append(nSize
- len
, ch
);
1396 else // can use (presumably faster) resize() version
1397 #endif // wxUSE_UNICODE_UTF8
1399 wxSTRING_INVALIDATE_CACHED_LENGTH();
1401 m_impl
.resize(nSize
, (wxStringCharType
)ch
);
1405 wxString
substr(size_t nStart
= 0, size_t nLen
= npos
) const
1408 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
1409 return FromImpl(m_impl
.substr(pos
, len
));
1412 // generic attributes & operations
1413 // as standard strlen()
1414 size_t Len() const { return length(); }
1415 // string contains any characters?
1416 bool IsEmpty() const { return empty(); }
1417 // empty string is "false", so !str will return true
1418 bool operator!() const { return empty(); }
1419 // truncate the string to given length
1420 wxString
& Truncate(size_t uiLen
);
1421 // empty string contents
1426 wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") );
1428 // empty the string and free memory
1429 void Clear() { clear(); }
1432 // Is an ascii value
1433 bool IsAscii() const;
1435 bool IsNumber() const;
1437 bool IsWord() const;
1439 // data access (all indexes are 0 based)
1441 wxUniChar
at(size_t n
) const
1442 { return wxStringOperations::DecodeChar(m_impl
.begin() + PosToImpl(n
)); }
1443 wxUniChar
GetChar(size_t n
) const
1445 // read/write access
1446 wxUniCharRef
at(size_t n
)
1447 { return *GetIterForNthChar(n
); }
1448 wxUniCharRef
GetWritableChar(size_t n
)
1451 void SetChar(size_t n
, wxUniChar ch
)
1454 // get last character
1455 wxUniChar
Last() const
1457 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1461 // get writable last character
1464 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1469 Note that we we must define all of the overloads below to avoid
1470 ambiguity when using str[0].
1472 wxUniChar
operator[](int n
) const
1474 wxUniChar
operator[](long n
) const
1476 wxUniChar
operator[](size_t n
) const
1478 #ifndef wxSIZE_T_IS_UINT
1479 wxUniChar
operator[](unsigned int n
) const
1481 #endif // size_t != unsigned int
1483 // operator versions of GetWriteableChar()
1484 wxUniCharRef
operator[](int n
)
1486 wxUniCharRef
operator[](long n
)
1488 wxUniCharRef
operator[](size_t n
)
1490 #ifndef wxSIZE_T_IS_UINT
1491 wxUniCharRef
operator[](unsigned int n
)
1493 #endif // size_t != unsigned int
1495 // explicit conversion to C string (use this with printf()!)
1496 wxCStrData
c_str() const { return wxCStrData(this); }
1497 wxCStrData
data() const { return c_str(); }
1499 // implicit conversion to C string
1500 operator wxCStrData() const { return c_str(); }
1502 // the first two operators conflict with operators for conversion to
1503 // std::string and they must be disabled in STL build; the next one only
1504 // makes sense if conversions to char* are also defined and not defining it
1505 // in STL build also helps us to get more clear error messages for the code
1506 // which relies on implicit conversion to char* in STL build
1508 operator const char*() const { return c_str(); }
1509 operator const wchar_t*() const { return c_str(); }
1511 // implicit conversion to untyped pointer for compatibility with previous
1512 // wxWidgets versions: this is the same as conversion to const char * so it
1514 operator const void*() const { return c_str(); }
1517 // identical to c_str(), for MFC compatibility
1518 const wxCStrData
GetData() const { return c_str(); }
1520 // explicit conversion to C string in internal representation (char*,
1521 // wchar_t*, UTF-8-encoded char*, depending on the build):
1522 const wxStringCharType
*wx_str() const { return m_impl
.c_str(); }
1524 // conversion to *non-const* multibyte or widestring buffer; modifying
1525 // returned buffer won't affect the string, these methods are only useful
1526 // for passing values to const-incorrect functions
1527 wxWritableCharBuffer
char_str(const wxMBConv
& conv
= wxConvLibc
) const
1528 { return mb_str(conv
); }
1529 wxWritableWCharBuffer
wchar_str() const { return wc_str(); }
1531 // conversion to the buffer of the given type T (= char or wchar_t) and
1532 // also optionally return the buffer length
1534 // this is mostly/only useful for the template functions
1536 // FIXME-VC6: the second argument only exists for VC6 which doesn't support
1537 // explicit template function selection, do not use it unless
1538 // you must support VC6!
1539 template <typename T
>
1540 wxCharTypeBuffer
<T
> tchar_str(size_t *len
= NULL
,
1541 T
* WXUNUSED(dummy
) = NULL
) const
1544 // we need a helper dispatcher depending on type
1545 return wxPrivate::wxStringAsBufHelper
<T
>::Get(*this, len
);
1547 // T can only be char in ANSI build
1551 return wxCharTypeBuffer
<T
>::CreateNonOwned(wx_str());
1552 #endif // Unicode build kind
1555 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
1556 // converting numbers or strings which are certain not to contain special
1557 // chars (typically system functions, X atoms, environment variables etc.)
1559 // the behaviour of these functions with the strings containing anything
1560 // else than 7 bit ASCII characters is undefined, use at your own risk.
1562 static wxString
FromAscii(const char *ascii
, size_t len
);
1563 static wxString
FromAscii(const char *ascii
);
1564 static wxString
FromAscii(char ascii
);
1565 const wxCharBuffer
ToAscii() const;
1567 static wxString
FromAscii(const char *ascii
) { return wxString( ascii
); }
1568 static wxString
FromAscii(const char *ascii
, size_t len
)
1569 { return wxString( ascii
, len
); }
1570 static wxString
FromAscii(char ascii
) { return wxString( ascii
); }
1571 const char *ToAscii() const { return c_str(); }
1572 #endif // Unicode/!Unicode
1574 // also provide unsigned char overloads as signed/unsigned doesn't matter
1575 // for 7 bit ASCII characters
1576 static wxString
FromAscii(const unsigned char *ascii
)
1577 { return FromAscii((const char *)ascii
); }
1578 static wxString
FromAscii(const unsigned char *ascii
, size_t len
)
1579 { return FromAscii((const char *)ascii
, len
); }
1581 // conversion to/from UTF-8:
1582 #if wxUSE_UNICODE_UTF8
1583 static wxString
FromUTF8Unchecked(const char *utf8
)
1586 return wxEmptyString
;
1588 wxASSERT( wxStringOperations::IsValidUtf8String(utf8
) );
1589 return FromImpl(wxStringImpl(utf8
));
1591 static wxString
FromUTF8Unchecked(const char *utf8
, size_t len
)
1594 return wxEmptyString
;
1596 return FromUTF8Unchecked(utf8
);
1598 wxASSERT( wxStringOperations::IsValidUtf8String(utf8
, len
) );
1599 return FromImpl(wxStringImpl(utf8
, len
));
1602 static wxString
FromUTF8(const char *utf8
)
1604 if ( !utf8
|| !wxStringOperations::IsValidUtf8String(utf8
) )
1607 return FromImpl(wxStringImpl(utf8
));
1609 static wxString
FromUTF8(const char *utf8
, size_t len
)
1612 return FromUTF8(utf8
);
1614 if ( !utf8
|| !wxStringOperations::IsValidUtf8String(utf8
, len
) )
1617 return FromImpl(wxStringImpl(utf8
, len
));
1620 const char* utf8_str() const { return wx_str(); }
1621 const char* ToUTF8() const { return wx_str(); }
1623 // this function exists in UTF-8 build only and returns the length of the
1624 // internal UTF-8 representation
1625 size_t utf8_length() const { return m_impl
.length(); }
1626 #elif wxUSE_UNICODE_WCHAR
1627 static wxString
FromUTF8(const char *utf8
, size_t len
= npos
)
1628 { return wxString(utf8
, wxMBConvUTF8(), len
); }
1629 static wxString
FromUTF8Unchecked(const char *utf8
, size_t len
= npos
)
1631 const wxString
s(utf8
, wxMBConvUTF8(), len
);
1632 wxASSERT_MSG( !utf8
|| !*utf8
|| !s
.empty(),
1633 "string must be valid UTF-8" );
1636 const wxCharBuffer
utf8_str() const { return mb_str(wxMBConvUTF8()); }
1637 const wxCharBuffer
ToUTF8() const { return utf8_str(); }
1639 static wxString
FromUTF8(const char *utf8
)
1640 { return wxString(wxMBConvUTF8().cMB2WC(utf8
)); }
1641 static wxString
FromUTF8(const char *utf8
, size_t len
)
1644 wxWCharBuffer
buf(wxMBConvUTF8().cMB2WC(utf8
, len
== npos
? wxNO_LEN
: len
, &wlen
));
1645 return wxString(buf
.data(), wlen
);
1647 static wxString
FromUTF8Unchecked(const char *utf8
, size_t len
= npos
)
1650 wxWCharBuffer
buf(wxMBConvUTF8().cMB2WC(utf8
,
1651 len
== npos
? wxNO_LEN
: len
,
1653 wxASSERT_MSG( !utf8
|| !*utf8
|| wlen
,
1654 "string must be valid UTF-8" );
1656 return wxString(buf
.data(), wlen
);
1658 const wxCharBuffer
utf8_str() const
1659 { return wxMBConvUTF8().cWC2MB(wc_str()); }
1660 const wxCharBuffer
ToUTF8() const { return utf8_str(); }
1663 // functions for storing binary data in wxString:
1665 static wxString
From8BitData(const char *data
, size_t len
)
1666 { return wxString(data
, wxConvISO8859_1
, len
); }
1667 // version for NUL-terminated data:
1668 static wxString
From8BitData(const char *data
)
1669 { return wxString(data
, wxConvISO8859_1
); }
1670 const wxCharBuffer
To8BitData() const { return mb_str(wxConvISO8859_1
); }
1672 static wxString
From8BitData(const char *data
, size_t len
)
1673 { return wxString(data
, len
); }
1674 // version for NUL-terminated data:
1675 static wxString
From8BitData(const char *data
)
1676 { return wxString(data
); }
1677 const char *To8BitData() const { return c_str(); }
1678 #endif // Unicode/ANSI
1680 // conversions with (possible) format conversions: have to return a
1681 // buffer with temporary data
1683 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
1684 // return an ANSI (multibyte) string, wc_str() to return a wide string and
1685 // fn_str() to return a string which should be used with the OS APIs
1686 // accepting the file names. The return value is always the same, but the
1687 // type differs because a function may either return pointer to the buffer
1688 // directly or have to use intermediate buffer for translation.
1691 #if wxUSE_UTF8_LOCALE_ONLY
1692 const char* mb_str() const { return wx_str(); }
1693 const wxCharBuffer
mb_str(const wxMBConv
& conv
) const;
1695 const wxCharBuffer
mb_str(const wxMBConv
& conv
= wxConvLibc
) const;
1698 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
1700 #if wxUSE_UNICODE_WCHAR
1701 const wchar_t* wc_str() const { return wx_str(); }
1702 #elif wxUSE_UNICODE_UTF8
1703 const wxWCharBuffer
wc_str() const;
1705 // for compatibility with !wxUSE_UNICODE version
1706 const wxWX2WCbuf
wc_str(const wxMBConv
& WXUNUSED(conv
)) const
1707 { return wc_str(); }
1710 const wxCharBuffer
fn_str() const { return mb_str(wxConvFile
); }
1712 const wxWX2WCbuf
fn_str() const { return wc_str(); }
1713 #endif // wxMBFILES/!wxMBFILES
1716 const wxChar
* mb_str() const { return wx_str(); }
1718 // for compatibility with wxUSE_UNICODE version
1719 const char* mb_str(const wxMBConv
& WXUNUSED(conv
)) const { return wx_str(); }
1721 const wxWX2MBbuf
mbc_str() const { return mb_str(); }
1724 const wxWCharBuffer
wc_str(const wxMBConv
& conv
= wxConvLibc
) const;
1725 #endif // wxUSE_WCHAR_T
1726 const wxCharBuffer
fn_str() const { return wxConvFile
.cWC2WX( wc_str( wxConvLibc
) ); }
1727 #endif // Unicode/ANSI
1729 #if wxUSE_UNICODE_UTF8
1730 const wxWCharBuffer
t_str() const { return wc_str(); }
1731 #elif wxUSE_UNICODE_WCHAR
1732 const wchar_t* t_str() const { return wx_str(); }
1734 const char* t_str() const { return wx_str(); }
1738 // overloaded assignment
1739 // from another wxString
1740 wxString
& operator=(const wxString
& stringSrc
)
1742 if ( this != &stringSrc
)
1744 wxSTRING_INVALIDATE_CACHE();
1746 m_impl
= stringSrc
.m_impl
;
1752 wxString
& operator=(const wxCStrData
& cstr
)
1753 { return *this = cstr
.AsString(); }
1755 wxString
& operator=(wxUniChar ch
)
1757 wxSTRING_INVALIDATE_CACHE();
1759 #if wxUSE_UNICODE_UTF8
1760 if ( !ch
.IsAscii() )
1761 m_impl
= wxStringOperations::EncodeChar(ch
);
1763 #endif // wxUSE_UNICODE_UTF8
1764 m_impl
= (wxStringCharType
)ch
;
1768 wxString
& operator=(wxUniCharRef ch
)
1769 { return operator=((wxUniChar
)ch
); }
1770 wxString
& operator=(char ch
)
1771 { return operator=(wxUniChar(ch
)); }
1772 wxString
& operator=(unsigned char ch
)
1773 { return operator=(wxUniChar(ch
)); }
1774 wxString
& operator=(wchar_t ch
)
1775 { return operator=(wxUniChar(ch
)); }
1776 // from a C string - STL probably will crash on NULL,
1777 // so we need to compensate in that case
1778 #if wxUSE_STL_BASED_WXSTRING
1779 wxString
& operator=(const char *psz
)
1781 wxSTRING_INVALIDATE_CACHE();
1784 m_impl
= ImplStr(psz
);
1791 wxString
& operator=(const wchar_t *pwz
)
1793 wxSTRING_INVALIDATE_CACHE();
1796 m_impl
= ImplStr(pwz
);
1802 #else // !wxUSE_STL_BASED_WXSTRING
1803 wxString
& operator=(const char *psz
)
1805 wxSTRING_INVALIDATE_CACHE();
1807 m_impl
= ImplStr(psz
);
1812 wxString
& operator=(const wchar_t *pwz
)
1814 wxSTRING_INVALIDATE_CACHE();
1816 m_impl
= ImplStr(pwz
);
1820 #endif // wxUSE_STL_BASED_WXSTRING/!wxUSE_STL_BASED_WXSTRING
1822 wxString
& operator=(const unsigned char *psz
)
1823 { return operator=((const char*)psz
); }
1825 // from wxWCharBuffer
1826 wxString
& operator=(const wxWCharBuffer
& s
)
1827 { return operator=(s
.data()); } // FIXME-UTF8: fix for embedded NULs
1828 // from wxCharBuffer
1829 wxString
& operator=(const wxCharBuffer
& s
)
1830 { return operator=(s
.data()); } // FIXME-UTF8: fix for embedded NULs
1832 // string concatenation
1833 // in place concatenation
1835 Concatenate and return the result. Note that the left to right
1836 associativity of << allows to write things like "str << str1 << str2
1837 << ..." (unlike with +=)
1840 wxString
& operator<<(const wxString
& s
)
1842 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1843 wxASSERT_MSG( s
.IsValid(),
1844 _T("did you forget to call UngetWriteBuf()?") );
1850 // string += C string
1851 wxString
& operator<<(const char *psz
)
1852 { append(psz
); return *this; }
1853 wxString
& operator<<(const wchar_t *pwz
)
1854 { append(pwz
); return *this; }
1855 wxString
& operator<<(const wxCStrData
& psz
)
1856 { append(psz
.AsString()); return *this; }
1858 wxString
& operator<<(wxUniChar ch
) { append(1, ch
); return *this; }
1859 wxString
& operator<<(wxUniCharRef ch
) { append(1, ch
); return *this; }
1860 wxString
& operator<<(char ch
) { append(1, ch
); return *this; }
1861 wxString
& operator<<(unsigned char ch
) { append(1, ch
); return *this; }
1862 wxString
& operator<<(wchar_t ch
) { append(1, ch
); return *this; }
1864 // string += buffer (i.e. from wxGetString)
1865 wxString
& operator<<(const wxWCharBuffer
& s
)
1866 { return operator<<((const wchar_t *)s
); }
1867 wxString
& operator<<(const wxCharBuffer
& s
)
1868 { return operator<<((const char *)s
); }
1870 // string += C string
1871 wxString
& Append(const wxString
& s
)
1873 // test for empty() to share the string if possible
1880 wxString
& Append(const char* psz
)
1881 { append(psz
); return *this; }
1882 wxString
& Append(const wchar_t* pwz
)
1883 { append(pwz
); return *this; }
1884 wxString
& Append(const wxCStrData
& psz
)
1885 { append(psz
); return *this; }
1886 wxString
& Append(const wxCharBuffer
& psz
)
1887 { append(psz
); return *this; }
1888 wxString
& Append(const wxWCharBuffer
& psz
)
1889 { append(psz
); return *this; }
1890 wxString
& Append(const char* psz
, size_t nLen
)
1891 { append(psz
, nLen
); return *this; }
1892 wxString
& Append(const wchar_t* pwz
, size_t nLen
)
1893 { append(pwz
, nLen
); return *this; }
1894 wxString
& Append(const wxCStrData
& psz
, size_t nLen
)
1895 { append(psz
, nLen
); return *this; }
1896 wxString
& Append(const wxCharBuffer
& psz
, size_t nLen
)
1897 { append(psz
, nLen
); return *this; }
1898 wxString
& Append(const wxWCharBuffer
& psz
, size_t nLen
)
1899 { append(psz
, nLen
); return *this; }
1900 // append count copies of given character
1901 wxString
& Append(wxUniChar ch
, size_t count
= 1u)
1902 { append(count
, ch
); return *this; }
1903 wxString
& Append(wxUniCharRef ch
, size_t count
= 1u)
1904 { append(count
, ch
); return *this; }
1905 wxString
& Append(char ch
, size_t count
= 1u)
1906 { append(count
, ch
); return *this; }
1907 wxString
& Append(unsigned char ch
, size_t count
= 1u)
1908 { append(count
, ch
); return *this; }
1909 wxString
& Append(wchar_t ch
, size_t count
= 1u)
1910 { append(count
, ch
); return *this; }
1912 // prepend a string, return the string itself
1913 wxString
& Prepend(const wxString
& str
)
1914 { *this = str
+ *this; return *this; }
1916 // non-destructive concatenation
1918 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
,
1919 const wxString
& string2
);
1920 // string with a single char
1921 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
1922 // char with a string
1923 friend wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
1924 // string with C string
1925 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
1927 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
1928 const wchar_t *pwz
);
1929 // C string with string
1930 friend wxString WXDLLIMPEXP_BASE
operator+(const char *psz
,
1931 const wxString
& string
);
1932 friend wxString WXDLLIMPEXP_BASE
operator+(const wchar_t *pwz
,
1933 const wxString
& string
);
1935 // stream-like functions
1936 // insert an int into string
1937 wxString
& operator<<(int i
)
1938 { return (*this) << Format(_T("%d"), i
); }
1939 // insert an unsigned int into string
1940 wxString
& operator<<(unsigned int ui
)
1941 { return (*this) << Format(_T("%u"), ui
); }
1942 // insert a long into string
1943 wxString
& operator<<(long l
)
1944 { return (*this) << Format(_T("%ld"), l
); }
1945 // insert an unsigned long into string
1946 wxString
& operator<<(unsigned long ul
)
1947 { return (*this) << Format(_T("%lu"), ul
); }
1948 #if defined wxLongLong_t && !defined wxLongLongIsLong
1949 // insert a long long if they exist and aren't longs
1950 wxString
& operator<<(wxLongLong_t ll
)
1952 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("d");
1953 return (*this) << Format(fmt
, ll
);
1955 // insert an unsigned long long
1956 wxString
& operator<<(wxULongLong_t ull
)
1958 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("u");
1959 return (*this) << Format(fmt
, ull
);
1961 #endif // wxLongLong_t && !wxLongLongIsLong
1962 // insert a float into string
1963 wxString
& operator<<(float f
)
1964 { return (*this) << Format(_T("%f"), f
); }
1965 // insert a double into string
1966 wxString
& operator<<(double d
)
1967 { return (*this) << Format(_T("%g"), d
); }
1969 // string comparison
1970 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
1971 int Cmp(const char *psz
) const
1972 { return compare(psz
); }
1973 int Cmp(const wchar_t *pwz
) const
1974 { return compare(pwz
); }
1975 int Cmp(const wxString
& s
) const
1976 { return compare(s
); }
1977 int Cmp(const wxCStrData
& s
) const
1978 { return compare(s
); }
1979 int Cmp(const wxCharBuffer
& s
) const
1980 { return compare(s
); }
1981 int Cmp(const wxWCharBuffer
& s
) const
1982 { return compare(s
); }
1983 // same as Cmp() but not case-sensitive
1984 int CmpNoCase(const wxString
& s
) const;
1986 // test for the string equality, either considering case or not
1987 // (if compareWithCase then the case matters)
1988 bool IsSameAs(const wxString
& str
, bool compareWithCase
= true) const
1990 #if !wxUSE_UNICODE_UTF8
1991 // in UTF-8 build, length() is O(n) and doing this would be _slower_
1992 if ( length() != str
.length() )
1995 return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0;
1997 bool IsSameAs(const char *str
, bool compareWithCase
= true) const
1998 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
1999 bool IsSameAs(const wchar_t *str
, bool compareWithCase
= true) const
2000 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
2002 bool IsSameAs(const wxCStrData
& str
, bool compareWithCase
= true) const
2003 { return IsSameAs(str
.AsString(), compareWithCase
); }
2004 bool IsSameAs(const wxCharBuffer
& str
, bool compareWithCase
= true) const
2005 { return IsSameAs(str
.data(), compareWithCase
); }
2006 bool IsSameAs(const wxWCharBuffer
& str
, bool compareWithCase
= true) const
2007 { return IsSameAs(str
.data(), compareWithCase
); }
2008 // comparison with a single character: returns true if equal
2009 bool IsSameAs(wxUniChar c
, bool compareWithCase
= true) const;
2010 // FIXME-UTF8: remove these overloads
2011 bool IsSameAs(wxUniCharRef c
, bool compareWithCase
= true) const
2012 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
2013 bool IsSameAs(char c
, bool compareWithCase
= true) const
2014 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
2015 bool IsSameAs(unsigned char c
, bool compareWithCase
= true) const
2016 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
2017 bool IsSameAs(wchar_t c
, bool compareWithCase
= true) const
2018 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
2019 bool IsSameAs(int c
, bool compareWithCase
= true) const
2020 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
2022 // simple sub-string extraction
2023 // return substring starting at nFirst of length nCount (or till the end
2024 // if nCount = default value)
2025 wxString
Mid(size_t nFirst
, size_t nCount
= npos
) const;
2027 // operator version of Mid()
2028 wxString
operator()(size_t start
, size_t len
) const
2029 { return Mid(start
, len
); }
2031 // check if the string starts with the given prefix and return the rest
2032 // of the string in the provided pointer if it is not NULL; otherwise
2034 bool StartsWith(const wxString
& prefix
, wxString
*rest
= NULL
) const;
2035 // check if the string ends with the given suffix and return the
2036 // beginning of the string before the suffix in the provided pointer if
2037 // it is not NULL; otherwise return false
2038 bool EndsWith(const wxString
& suffix
, wxString
*rest
= NULL
) const;
2040 // get first nCount characters
2041 wxString
Left(size_t nCount
) const;
2042 // get last nCount characters
2043 wxString
Right(size_t nCount
) const;
2044 // get all characters before the first occurance of ch
2045 // (returns the whole string if ch not found)
2046 wxString
BeforeFirst(wxUniChar ch
) const;
2047 // get all characters before the last occurence of ch
2048 // (returns empty string if ch not found)
2049 wxString
BeforeLast(wxUniChar ch
) const;
2050 // get all characters after the first occurence of ch
2051 // (returns empty string if ch not found)
2052 wxString
AfterFirst(wxUniChar ch
) const;
2053 // get all characters after the last occurence of ch
2054 // (returns the whole string if ch not found)
2055 wxString
AfterLast(wxUniChar ch
) const;
2057 // for compatibility only, use more explicitly named functions above
2058 wxString
Before(wxUniChar ch
) const { return BeforeLast(ch
); }
2059 wxString
After(wxUniChar ch
) const { return AfterFirst(ch
); }
2062 // convert to upper case in place, return the string itself
2063 wxString
& MakeUpper();
2064 // convert to upper case, return the copy of the string
2065 wxString
Upper() const { return wxString(*this).MakeUpper(); }
2066 // convert to lower case in place, return the string itself
2067 wxString
& MakeLower();
2068 // convert to lower case, return the copy of the string
2069 wxString
Lower() const { return wxString(*this).MakeLower(); }
2070 // convert the first character to the upper case and the rest to the
2071 // lower one, return the modified string itself
2072 wxString
& MakeCapitalized();
2073 // convert the first character to the upper case and the rest to the
2074 // lower one, return the copy of the string
2075 wxString
Capitalize() const { return wxString(*this).MakeCapitalized(); }
2077 // trimming/padding whitespace (either side) and truncating
2078 // remove spaces from left or from right (default) side
2079 wxString
& Trim(bool bFromRight
= true);
2080 // add nCount copies chPad in the beginning or at the end (default)
2081 wxString
& Pad(size_t nCount
, wxUniChar chPad
= wxT(' '), bool bFromRight
= true);
2083 // searching and replacing
2084 // searching (return starting index, or -1 if not found)
2085 int Find(wxUniChar ch
, bool bFromEnd
= false) const; // like strchr/strrchr
2086 int Find(wxUniCharRef ch
, bool bFromEnd
= false) const
2087 { return Find(wxUniChar(ch
), bFromEnd
); }
2088 int Find(char ch
, bool bFromEnd
= false) const
2089 { return Find(wxUniChar(ch
), bFromEnd
); }
2090 int Find(unsigned char ch
, bool bFromEnd
= false) const
2091 { return Find(wxUniChar(ch
), bFromEnd
); }
2092 int Find(wchar_t ch
, bool bFromEnd
= false) const
2093 { return Find(wxUniChar(ch
), bFromEnd
); }
2094 // searching (return starting index, or -1 if not found)
2095 int Find(const wxString
& sub
) const // like strstr
2097 size_type idx
= find(sub
);
2098 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
2100 int Find(const char *sub
) const // like strstr
2102 size_type idx
= find(sub
);
2103 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
2105 int Find(const wchar_t *sub
) const // like strstr
2107 size_type idx
= find(sub
);
2108 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
2111 int Find(const wxCStrData
& sub
) const
2112 { return Find(sub
.AsString()); }
2113 int Find(const wxCharBuffer
& sub
) const
2114 { return Find(sub
.data()); }
2115 int Find(const wxWCharBuffer
& sub
) const
2116 { return Find(sub
.data()); }
2118 // replace first (or all of bReplaceAll) occurences of substring with
2119 // another string, returns the number of replacements made
2120 size_t Replace(const wxString
& strOld
,
2121 const wxString
& strNew
,
2122 bool bReplaceAll
= true);
2124 // check if the string contents matches a mask containing '*' and '?'
2125 bool Matches(const wxString
& mask
) const;
2127 // conversion to numbers: all functions return true only if the whole
2128 // string is a number and put the value of this number into the pointer
2129 // provided, the base is the numeric base in which the conversion should be
2130 // done and must be comprised between 2 and 36 or be 0 in which case the
2131 // standard C rules apply (leading '0' => octal, "0x" => hex)
2132 // convert to a signed integer
2133 bool ToLong(long *val
, int base
= 10) const;
2134 // convert to an unsigned integer
2135 bool ToULong(unsigned long *val
, int base
= 10) const;
2136 // convert to wxLongLong
2137 #if defined(wxLongLong_t)
2138 bool ToLongLong(wxLongLong_t
*val
, int base
= 10) const;
2139 // convert to wxULongLong
2140 bool ToULongLong(wxULongLong_t
*val
, int base
= 10) const;
2141 #endif // wxLongLong_t
2142 // convert to a double
2143 bool ToDouble(double *val
) const;
2146 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2147 // formatted input/output
2148 // as sprintf(), returns the number of characters written or < 0 on error
2149 // (take 'this' into account in attribute parameter count)
2150 // int Printf(const wxString& format, ...);
2151 WX_DEFINE_VARARG_FUNC(int, Printf
, 1, (const wxFormatString
&),
2152 DoPrintfWchar
, DoPrintfUtf8
)
2154 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
2155 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wxString
&),
2156 (wxFormatString(f1
)));
2157 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wxCStrData
&),
2158 (wxFormatString(f1
)));
2159 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const char*),
2160 (wxFormatString(f1
)));
2161 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wchar_t*),
2162 (wxFormatString(f1
)));
2164 #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN
2165 // as vprintf(), returns the number of characters written or < 0 on error
2166 int PrintfV(const wxString
& format
, va_list argptr
);
2168 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2169 // returns the string containing the result of Printf() to it
2170 // static wxString Format(const wxString& format, ...) ATTRIBUTE_PRINTF_1;
2171 WX_DEFINE_VARARG_FUNC(static wxString
, Format
, 1, (const wxFormatString
&),
2172 DoFormatWchar
, DoFormatUtf8
)
2174 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
2175 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wxString
&),
2176 (wxFormatString(f1
)));
2177 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wxCStrData
&),
2178 (wxFormatString(f1
)));
2179 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const char*),
2180 (wxFormatString(f1
)));
2181 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wchar_t*),
2182 (wxFormatString(f1
)));
2185 // the same as above, but takes a va_list
2186 static wxString
FormatV(const wxString
& format
, va_list argptr
);
2188 // raw access to string memory
2189 // ensure that string has space for at least nLen characters
2190 // only works if the data of this string is not shared
2191 bool Alloc(size_t nLen
) { reserve(nLen
); return capacity() >= nLen
; }
2192 // minimize the string's memory
2193 // only works if the data of this string is not shared
2195 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2196 // These are deprecated, use wxStringBuffer or wxStringBufferLength instead
2198 // get writable buffer of at least nLen bytes. Unget() *must* be called
2199 // a.s.a.p. to put string back in a reasonable state!
2200 wxDEPRECATED( wxStringCharType
*GetWriteBuf(size_t nLen
) );
2201 // call this immediately after GetWriteBuf() has been used
2202 wxDEPRECATED( void UngetWriteBuf() );
2203 wxDEPRECATED( void UngetWriteBuf(size_t nLen
) );
2204 #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && wxUSE_UNICODE_UTF8
2206 // wxWidgets version 1 compatibility functions
2209 wxString
SubString(size_t from
, size_t to
) const
2210 { return Mid(from
, (to
- from
+ 1)); }
2211 // values for second parameter of CompareTo function
2212 enum caseCompare
{exact
, ignoreCase
};
2213 // values for first parameter of Strip function
2214 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
2216 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2218 // (take 'this' into account in attribute parameter count)
2219 // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2;
2220 WX_DEFINE_VARARG_FUNC(int, sprintf
, 1, (const wxFormatString
&),
2221 DoPrintfWchar
, DoPrintfUtf8
)
2223 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
2224 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wxString
&),
2225 (wxFormatString(f1
)));
2226 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wxCStrData
&),
2227 (wxFormatString(f1
)));
2228 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const char*),
2229 (wxFormatString(f1
)));
2230 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wchar_t*),
2231 (wxFormatString(f1
)));
2233 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
2236 int CompareTo(const wxChar
* psz
, caseCompare cmp
= exact
) const
2237 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
2240 size_t Length() const { return length(); }
2241 // Count the number of characters
2242 int Freq(wxUniChar ch
) const;
2244 void LowerCase() { MakeLower(); }
2246 void UpperCase() { MakeUpper(); }
2247 // use Trim except that it doesn't change this string
2248 wxString
Strip(stripType w
= trailing
) const;
2250 // use Find (more general variants not yet supported)
2251 size_t Index(const wxChar
* psz
) const { return Find(psz
); }
2252 size_t Index(wxUniChar ch
) const { return Find(ch
); }
2254 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
2255 wxString
& RemoveLast(size_t n
= 1) { return Truncate(length() - n
); }
2257 wxString
& Remove(size_t nStart
, size_t nLen
)
2258 { return (wxString
&)erase( nStart
, nLen
); }
2261 int First( wxUniChar ch
) const { return Find(ch
); }
2262 int First( wxUniCharRef ch
) const { return Find(ch
); }
2263 int First( char ch
) const { return Find(ch
); }
2264 int First( unsigned char ch
) const { return Find(ch
); }
2265 int First( wchar_t ch
) const { return Find(ch
); }
2266 int First( const wxString
& str
) const { return Find(str
); }
2267 int Last( wxUniChar ch
) const { return Find(ch
, true); }
2268 bool Contains(const wxString
& str
) const { return Find(str
) != wxNOT_FOUND
; }
2271 bool IsNull() const { return empty(); }
2273 // std::string compatibility functions
2275 // take nLen chars starting at nPos
2276 wxString(const wxString
& str
, size_t nPos
, size_t nLen
)
2277 { assign(str
, nPos
, nLen
); }
2278 // take all characters from first to last
2279 wxString(const_iterator first
, const_iterator last
)
2280 : m_impl(first
.impl(), last
.impl()) { }
2281 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2282 // the 2 overloads below are for compatibility with the existing code using
2283 // pointers instead of iterators
2284 wxString(const char *first
, const char *last
)
2286 SubstrBufFromMB
str(ImplStr(first
, last
- first
));
2287 m_impl
.assign(str
.data
, str
.len
);
2289 wxString(const wchar_t *first
, const wchar_t *last
)
2291 SubstrBufFromWC
str(ImplStr(first
, last
- first
));
2292 m_impl
.assign(str
.data
, str
.len
);
2294 // and this one is needed to compile code adding offsets to c_str() result
2295 wxString(const wxCStrData
& first
, const wxCStrData
& last
)
2296 : m_impl(CreateConstIterator(first
).impl(),
2297 CreateConstIterator(last
).impl())
2299 wxASSERT_MSG( first
.m_str
== last
.m_str
,
2300 _T("pointers must be into the same string") );
2302 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2304 // lib.string.modifiers
2305 // append elements str[pos], ..., str[pos+n]
2306 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
2308 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2311 str
.PosLenToImpl(pos
, n
, &from
, &len
);
2312 m_impl
.append(str
.m_impl
, from
, len
);
2316 wxString
& append(const wxString
& str
)
2318 wxSTRING_UPDATE_CACHED_LENGTH(str
.length());
2320 m_impl
.append(str
.m_impl
);
2324 // append first n (or all if n == npos) characters of sz
2325 wxString
& append(const char *sz
)
2327 wxSTRING_INVALIDATE_CACHED_LENGTH();
2329 m_impl
.append(ImplStr(sz
));
2333 wxString
& append(const wchar_t *sz
)
2335 wxSTRING_INVALIDATE_CACHED_LENGTH();
2337 m_impl
.append(ImplStr(sz
));
2341 wxString
& append(const char *sz
, size_t n
)
2343 wxSTRING_INVALIDATE_CACHED_LENGTH();
2345 SubstrBufFromMB
str(ImplStr(sz
, n
));
2346 m_impl
.append(str
.data
, str
.len
);
2349 wxString
& append(const wchar_t *sz
, size_t n
)
2351 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2353 SubstrBufFromWC
str(ImplStr(sz
, n
));
2354 m_impl
.append(str
.data
, str
.len
);
2358 wxString
& append(const wxCStrData
& str
)
2359 { return append(str
.AsString()); }
2360 wxString
& append(const wxCharBuffer
& str
)
2361 { return append(str
.data()); }
2362 wxString
& append(const wxWCharBuffer
& str
)
2363 { return append(str
.data()); }
2364 wxString
& append(const wxCStrData
& str
, size_t n
)
2365 { return append(str
.AsString(), 0, n
); }
2366 wxString
& append(const wxCharBuffer
& str
, size_t n
)
2367 { return append(str
.data(), n
); }
2368 wxString
& append(const wxWCharBuffer
& str
, size_t n
)
2369 { return append(str
.data(), n
); }
2371 // append n copies of ch
2372 wxString
& append(size_t n
, wxUniChar ch
)
2374 #if wxUSE_UNICODE_UTF8
2375 if ( !ch
.IsAscii() )
2377 wxSTRING_INVALIDATE_CACHED_LENGTH();
2379 m_impl
.append(wxStringOperations::EncodeNChars(n
, ch
));
2384 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2386 m_impl
.append(n
, (wxStringCharType
)ch
);
2392 wxString
& append(size_t n
, wxUniCharRef ch
)
2393 { return append(n
, wxUniChar(ch
)); }
2394 wxString
& append(size_t n
, char ch
)
2395 { return append(n
, wxUniChar(ch
)); }
2396 wxString
& append(size_t n
, unsigned char ch
)
2397 { return append(n
, wxUniChar(ch
)); }
2398 wxString
& append(size_t n
, wchar_t ch
)
2399 { return append(n
, wxUniChar(ch
)); }
2401 // append from first to last
2402 wxString
& append(const_iterator first
, const_iterator last
)
2404 wxSTRING_INVALIDATE_CACHED_LENGTH();
2406 m_impl
.append(first
.impl(), last
.impl());
2409 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2410 wxString
& append(const char *first
, const char *last
)
2411 { return append(first
, last
- first
); }
2412 wxString
& append(const wchar_t *first
, const wchar_t *last
)
2413 { return append(first
, last
- first
); }
2414 wxString
& append(const wxCStrData
& first
, const wxCStrData
& last
)
2415 { return append(CreateConstIterator(first
), CreateConstIterator(last
)); }
2416 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2418 // same as `this_string = str'
2419 wxString
& assign(const wxString
& str
)
2421 wxSTRING_SET_CACHED_LENGTH(str
.length());
2423 m_impl
= str
.m_impl
;
2428 wxString
& assign(const wxString
& str
, size_t len
)
2430 wxSTRING_SET_CACHED_LENGTH(len
);
2432 m_impl
.assign(str
.m_impl
, 0, str
.LenToImpl(len
));
2437 // same as ` = str[pos..pos + n]
2438 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
2441 str
.PosLenToImpl(pos
, n
, &from
, &len
);
2442 m_impl
.assign(str
.m_impl
, from
, len
);
2444 // it's important to call this after PosLenToImpl() above in case str is
2445 // the same string as this one
2446 wxSTRING_SET_CACHED_LENGTH(n
);
2451 // same as `= first n (or all if n == npos) characters of sz'
2452 wxString
& assign(const char *sz
)
2454 wxSTRING_INVALIDATE_CACHE();
2456 m_impl
.assign(ImplStr(sz
));
2461 wxString
& assign(const wchar_t *sz
)
2463 wxSTRING_INVALIDATE_CACHE();
2465 m_impl
.assign(ImplStr(sz
));
2470 wxString
& assign(const char *sz
, size_t n
)
2472 wxSTRING_SET_CACHED_LENGTH(n
);
2474 SubstrBufFromMB
str(ImplStr(sz
, n
));
2475 m_impl
.assign(str
.data
, str
.len
);
2480 wxString
& assign(const wchar_t *sz
, size_t n
)
2482 wxSTRING_SET_CACHED_LENGTH(n
);
2484 SubstrBufFromWC
str(ImplStr(sz
, n
));
2485 m_impl
.assign(str
.data
, str
.len
);
2490 wxString
& assign(const wxCStrData
& str
)
2491 { return assign(str
.AsString()); }
2492 wxString
& assign(const wxCharBuffer
& str
)
2493 { return assign(str
.data()); }
2494 wxString
& assign(const wxWCharBuffer
& str
)
2495 { return assign(str
.data()); }
2496 wxString
& assign(const wxCStrData
& str
, size_t len
)
2497 { return assign(str
.AsString(), len
); }
2498 wxString
& assign(const wxCharBuffer
& str
, size_t len
)
2499 { return assign(str
.data(), len
); }
2500 wxString
& assign(const wxWCharBuffer
& str
, size_t len
)
2501 { return assign(str
.data(), len
); }
2503 // same as `= n copies of ch'
2504 wxString
& assign(size_t n
, wxUniChar ch
)
2506 wxSTRING_SET_CACHED_LENGTH(n
);
2508 #if wxUSE_UNICODE_UTF8
2509 if ( !ch
.IsAscii() )
2510 m_impl
.assign(wxStringOperations::EncodeNChars(n
, ch
));
2513 m_impl
.assign(n
, (wxStringCharType
)ch
);
2518 wxString
& assign(size_t n
, wxUniCharRef ch
)
2519 { return assign(n
, wxUniChar(ch
)); }
2520 wxString
& assign(size_t n
, char ch
)
2521 { return assign(n
, wxUniChar(ch
)); }
2522 wxString
& assign(size_t n
, unsigned char ch
)
2523 { return assign(n
, wxUniChar(ch
)); }
2524 wxString
& assign(size_t n
, wchar_t ch
)
2525 { return assign(n
, wxUniChar(ch
)); }
2527 // assign from first to last
2528 wxString
& assign(const_iterator first
, const_iterator last
)
2530 wxSTRING_INVALIDATE_CACHE();
2532 m_impl
.assign(first
.impl(), last
.impl());
2536 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2537 wxString
& assign(const char *first
, const char *last
)
2538 { return assign(first
, last
- first
); }
2539 wxString
& assign(const wchar_t *first
, const wchar_t *last
)
2540 { return assign(first
, last
- first
); }
2541 wxString
& assign(const wxCStrData
& first
, const wxCStrData
& last
)
2542 { return assign(CreateConstIterator(first
), CreateConstIterator(last
)); }
2543 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2545 // string comparison
2546 int compare(const wxString
& str
) const;
2547 int compare(const char* sz
) const;
2548 int compare(const wchar_t* sz
) const;
2549 int compare(const wxCStrData
& str
) const
2550 { return compare(str
.AsString()); }
2551 int compare(const wxCharBuffer
& str
) const
2552 { return compare(str
.data()); }
2553 int compare(const wxWCharBuffer
& str
) const
2554 { return compare(str
.data()); }
2555 // comparison with a substring
2556 int compare(size_t nStart
, size_t nLen
, const wxString
& str
) const;
2557 // comparison of 2 substrings
2558 int compare(size_t nStart
, size_t nLen
,
2559 const wxString
& str
, size_t nStart2
, size_t nLen2
) const;
2560 // substring comparison with first nCount characters of sz
2561 int compare(size_t nStart
, size_t nLen
,
2562 const char* sz
, size_t nCount
= npos
) const;
2563 int compare(size_t nStart
, size_t nLen
,
2564 const wchar_t* sz
, size_t nCount
= npos
) const;
2566 // insert another string
2567 wxString
& insert(size_t nPos
, const wxString
& str
)
2568 { insert(GetIterForNthChar(nPos
), str
.begin(), str
.end()); return *this; }
2569 // insert n chars of str starting at nStart (in str)
2570 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
2572 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2575 str
.PosLenToImpl(nStart
, n
, &from
, &len
);
2576 m_impl
.insert(PosToImpl(nPos
), str
.m_impl
, from
, len
);
2581 // insert first n (or all if n == npos) characters of sz
2582 wxString
& insert(size_t nPos
, const char *sz
)
2584 wxSTRING_INVALIDATE_CACHE();
2586 m_impl
.insert(PosToImpl(nPos
), ImplStr(sz
));
2591 wxString
& insert(size_t nPos
, const wchar_t *sz
)
2593 wxSTRING_INVALIDATE_CACHE();
2595 m_impl
.insert(PosToImpl(nPos
), ImplStr(sz
)); return *this;
2598 wxString
& insert(size_t nPos
, const char *sz
, size_t n
)
2600 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2602 SubstrBufFromMB
str(ImplStr(sz
, n
));
2603 m_impl
.insert(PosToImpl(nPos
), str
.data
, str
.len
);
2608 wxString
& insert(size_t nPos
, const wchar_t *sz
, size_t n
)
2610 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2612 SubstrBufFromWC
str(ImplStr(sz
, n
));
2613 m_impl
.insert(PosToImpl(nPos
), str
.data
, str
.len
);
2618 // insert n copies of ch
2619 wxString
& insert(size_t nPos
, size_t n
, wxUniChar ch
)
2621 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2623 #if wxUSE_UNICODE_UTF8
2624 if ( !ch
.IsAscii() )
2625 m_impl
.insert(PosToImpl(nPos
), wxStringOperations::EncodeNChars(n
, ch
));
2628 m_impl
.insert(PosToImpl(nPos
), n
, (wxStringCharType
)ch
);
2632 iterator
insert(iterator it
, wxUniChar ch
)
2634 wxSTRING_UPDATE_CACHED_LENGTH(1);
2636 #if wxUSE_UNICODE_UTF8
2637 if ( !ch
.IsAscii() )
2639 size_t pos
= IterToImplPos(it
);
2640 m_impl
.insert(pos
, wxStringOperations::EncodeChar(ch
));
2641 return iterator(this, m_impl
.begin() + pos
);
2645 return iterator(this, m_impl
.insert(it
.impl(), (wxStringCharType
)ch
));
2648 void insert(iterator it
, const_iterator first
, const_iterator last
)
2650 wxSTRING_INVALIDATE_CACHE();
2652 m_impl
.insert(it
.impl(), first
.impl(), last
.impl());
2655 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2656 void insert(iterator it
, const char *first
, const char *last
)
2657 { insert(it
- begin(), first
, last
- first
); }
2658 void insert(iterator it
, const wchar_t *first
, const wchar_t *last
)
2659 { insert(it
- begin(), first
, last
- first
); }
2660 void insert(iterator it
, const wxCStrData
& first
, const wxCStrData
& last
)
2661 { insert(it
, CreateConstIterator(first
), CreateConstIterator(last
)); }
2662 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2664 void insert(iterator it
, size_type n
, wxUniChar ch
)
2666 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2668 #if wxUSE_UNICODE_UTF8
2669 if ( !ch
.IsAscii() )
2670 m_impl
.insert(IterToImplPos(it
), wxStringOperations::EncodeNChars(n
, ch
));
2673 m_impl
.insert(it
.impl(), n
, (wxStringCharType
)ch
);
2676 // delete characters from nStart to nStart + nLen
2677 wxString
& erase(size_type pos
= 0, size_type n
= npos
)
2679 wxSTRING_INVALIDATE_CACHE();
2682 PosLenToImpl(pos
, n
, &from
, &len
);
2683 m_impl
.erase(from
, len
);
2688 // delete characters from first up to last
2689 iterator
erase(iterator first
, iterator last
)
2691 wxSTRING_INVALIDATE_CACHE();
2693 return iterator(this, m_impl
.erase(first
.impl(), last
.impl()));
2696 iterator
erase(iterator first
)
2698 wxSTRING_UPDATE_CACHED_LENGTH(-1);
2700 return iterator(this, m_impl
.erase(first
.impl()));
2703 #ifdef wxSTRING_BASE_HASNT_CLEAR
2704 void clear() { erase(); }
2708 wxSTRING_SET_CACHED_LENGTH(0);
2714 // replaces the substring of length nLen starting at nStart
2715 wxString
& replace(size_t nStart
, size_t nLen
, const char* sz
)
2717 wxSTRING_INVALIDATE_CACHE();
2720 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2721 m_impl
.replace(from
, len
, ImplStr(sz
));
2726 wxString
& replace(size_t nStart
, size_t nLen
, const wchar_t* sz
)
2728 wxSTRING_INVALIDATE_CACHE();
2731 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2732 m_impl
.replace(from
, len
, ImplStr(sz
));
2737 // replaces the substring of length nLen starting at nStart
2738 wxString
& replace(size_t nStart
, size_t nLen
, const wxString
& str
)
2740 wxSTRING_INVALIDATE_CACHE();
2743 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2744 m_impl
.replace(from
, len
, str
.m_impl
);
2749 // replaces the substring with nCount copies of ch
2750 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxUniChar ch
)
2752 wxSTRING_INVALIDATE_CACHE();
2755 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2756 #if wxUSE_UNICODE_UTF8
2757 if ( !ch
.IsAscii() )
2758 m_impl
.replace(from
, len
, wxStringOperations::EncodeNChars(nCount
, ch
));
2761 m_impl
.replace(from
, len
, nCount
, (wxStringCharType
)ch
);
2766 // replaces a substring with another substring
2767 wxString
& replace(size_t nStart
, size_t nLen
,
2768 const wxString
& str
, size_t nStart2
, size_t nLen2
)
2770 wxSTRING_INVALIDATE_CACHE();
2773 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2776 str
.PosLenToImpl(nStart2
, nLen2
, &from2
, &len2
);
2778 m_impl
.replace(from
, len
, str
.m_impl
, from2
, len2
);
2783 // replaces the substring with first nCount chars of sz
2784 wxString
& replace(size_t nStart
, size_t nLen
,
2785 const char* sz
, size_t nCount
)
2787 wxSTRING_INVALIDATE_CACHE();
2790 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2792 SubstrBufFromMB
str(ImplStr(sz
, nCount
));
2794 m_impl
.replace(from
, len
, str
.data
, str
.len
);
2799 wxString
& replace(size_t nStart
, size_t nLen
,
2800 const wchar_t* sz
, size_t nCount
)
2802 wxSTRING_INVALIDATE_CACHE();
2805 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2807 SubstrBufFromWC
str(ImplStr(sz
, nCount
));
2809 m_impl
.replace(from
, len
, str
.data
, str
.len
);
2814 wxString
& replace(size_t nStart
, size_t nLen
,
2815 const wxString
& s
, size_t nCount
)
2817 wxSTRING_INVALIDATE_CACHE();
2820 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2821 m_impl
.replace(from
, len
, s
.m_impl
.c_str(), s
.LenToImpl(nCount
));
2826 wxString
& replace(iterator first
, iterator last
, const char* s
)
2828 wxSTRING_INVALIDATE_CACHE();
2830 m_impl
.replace(first
.impl(), last
.impl(), ImplStr(s
));
2835 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
)
2837 wxSTRING_INVALIDATE_CACHE();
2839 m_impl
.replace(first
.impl(), last
.impl(), ImplStr(s
));
2844 wxString
& replace(iterator first
, iterator last
, const char* s
, size_type n
)
2846 wxSTRING_INVALIDATE_CACHE();
2848 SubstrBufFromMB
str(ImplStr(s
, n
));
2849 m_impl
.replace(first
.impl(), last
.impl(), str
.data
, str
.len
);
2854 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
, size_type n
)
2856 wxSTRING_INVALIDATE_CACHE();
2858 SubstrBufFromWC
str(ImplStr(s
, n
));
2859 m_impl
.replace(first
.impl(), last
.impl(), str
.data
, str
.len
);
2864 wxString
& replace(iterator first
, iterator last
, const wxString
& s
)
2866 wxSTRING_INVALIDATE_CACHE();
2868 m_impl
.replace(first
.impl(), last
.impl(), s
.m_impl
);
2873 wxString
& replace(iterator first
, iterator last
, size_type n
, wxUniChar ch
)
2875 wxSTRING_INVALIDATE_CACHE();
2877 #if wxUSE_UNICODE_UTF8
2878 if ( !ch
.IsAscii() )
2879 m_impl
.replace(first
.impl(), last
.impl(),
2880 wxStringOperations::EncodeNChars(n
, ch
));
2883 m_impl
.replace(first
.impl(), last
.impl(), n
, (wxStringCharType
)ch
);
2888 wxString
& replace(iterator first
, iterator last
,
2889 const_iterator first1
, const_iterator last1
)
2891 wxSTRING_INVALIDATE_CACHE();
2893 m_impl
.replace(first
.impl(), last
.impl(), first1
.impl(), last1
.impl());
2898 wxString
& replace(iterator first
, iterator last
,
2899 const char *first1
, const char *last1
)
2900 { replace(first
, last
, first1
, last1
- first1
); return *this; }
2901 wxString
& replace(iterator first
, iterator last
,
2902 const wchar_t *first1
, const wchar_t *last1
)
2903 { replace(first
, last
, first1
, last1
- first1
); return *this; }
2906 void swap(wxString
& str
)
2908 #if wxUSE_STRING_POS_CACHE
2909 // we modify not only this string but also the other one directly so we
2910 // need to invalidate cache for both of them (we could also try to
2911 // exchange their cache entries but it seems unlikely to be worth it)
2913 str
.InvalidateCache();
2914 #endif // wxUSE_STRING_POS_CACHE
2916 m_impl
.swap(str
.m_impl
);
2920 size_t find(const wxString
& str
, size_t nStart
= 0) const
2921 { return PosFromImpl(m_impl
.find(str
.m_impl
, PosToImpl(nStart
))); }
2923 // find first n characters of sz
2924 size_t find(const char* sz
, size_t nStart
= 0, size_t n
= npos
) const
2926 SubstrBufFromMB
str(ImplStr(sz
, n
));
2927 return PosFromImpl(m_impl
.find(str
.data
, PosToImpl(nStart
), str
.len
));
2929 size_t find(const wchar_t* sz
, size_t nStart
= 0, size_t n
= npos
) const
2931 SubstrBufFromWC
str(ImplStr(sz
, n
));
2932 return PosFromImpl(m_impl
.find(str
.data
, PosToImpl(nStart
), str
.len
));
2934 size_t find(const wxCharBuffer
& s
, size_t nStart
= 0, size_t n
= npos
) const
2935 { return find(s
.data(), nStart
, n
); }
2936 size_t find(const wxWCharBuffer
& s
, size_t nStart
= 0, size_t n
= npos
) const
2937 { return find(s
.data(), nStart
, n
); }
2938 size_t find(const wxCStrData
& s
, size_t nStart
= 0, size_t n
= npos
) const
2939 { return find(s
.AsWChar(), nStart
, n
); }
2941 // find the first occurence of character ch after nStart
2942 size_t find(wxUniChar ch
, size_t nStart
= 0) const
2944 #if wxUSE_UNICODE_UTF8
2945 if ( !ch
.IsAscii() )
2946 return PosFromImpl(m_impl
.find(wxStringOperations::EncodeChar(ch
),
2947 PosToImpl(nStart
)));
2950 return PosFromImpl(m_impl
.find((wxStringCharType
)ch
,
2951 PosToImpl(nStart
)));
2954 size_t find(wxUniCharRef ch
, size_t nStart
= 0) const
2955 { return find(wxUniChar(ch
), nStart
); }
2956 size_t find(char ch
, size_t nStart
= 0) const
2957 { return find(wxUniChar(ch
), nStart
); }
2958 size_t find(unsigned char ch
, size_t nStart
= 0) const
2959 { return find(wxUniChar(ch
), nStart
); }
2960 size_t find(wchar_t ch
, size_t nStart
= 0) const
2961 { return find(wxUniChar(ch
), nStart
); }
2963 // rfind() family is exactly like find() but works right to left
2965 // as find, but from the end
2966 size_t rfind(const wxString
& str
, size_t nStart
= npos
) const
2967 { return PosFromImpl(m_impl
.rfind(str
.m_impl
, PosToImpl(nStart
))); }
2969 // as find, but from the end
2970 size_t rfind(const char* sz
, size_t nStart
= npos
, size_t n
= npos
) const
2972 SubstrBufFromMB
str(ImplStr(sz
, n
));
2973 return PosFromImpl(m_impl
.rfind(str
.data
, PosToImpl(nStart
), str
.len
));
2975 size_t rfind(const wchar_t* sz
, size_t nStart
= npos
, size_t n
= npos
) const
2977 SubstrBufFromWC
str(ImplStr(sz
, n
));
2978 return PosFromImpl(m_impl
.rfind(str
.data
, PosToImpl(nStart
), str
.len
));
2980 size_t rfind(const wxCharBuffer
& s
, size_t nStart
= npos
, size_t n
= npos
) const
2981 { return rfind(s
.data(), nStart
, n
); }
2982 size_t rfind(const wxWCharBuffer
& s
, size_t nStart
= npos
, size_t n
= npos
) const
2983 { return rfind(s
.data(), nStart
, n
); }
2984 size_t rfind(const wxCStrData
& s
, size_t nStart
= npos
, size_t n
= npos
) const
2985 { return rfind(s
.AsWChar(), nStart
, n
); }
2986 // as find, but from the end
2987 size_t rfind(wxUniChar ch
, size_t nStart
= npos
) const
2989 #if wxUSE_UNICODE_UTF8
2990 if ( !ch
.IsAscii() )
2991 return PosFromImpl(m_impl
.rfind(wxStringOperations::EncodeChar(ch
),
2992 PosToImpl(nStart
)));
2995 return PosFromImpl(m_impl
.rfind((wxStringCharType
)ch
,
2996 PosToImpl(nStart
)));
2998 size_t rfind(wxUniCharRef ch
, size_t nStart
= npos
) const
2999 { return rfind(wxUniChar(ch
), nStart
); }
3000 size_t rfind(char ch
, size_t nStart
= npos
) const
3001 { return rfind(wxUniChar(ch
), nStart
); }
3002 size_t rfind(unsigned char ch
, size_t nStart
= npos
) const
3003 { return rfind(wxUniChar(ch
), nStart
); }
3004 size_t rfind(wchar_t ch
, size_t nStart
= npos
) const
3005 { return rfind(wxUniChar(ch
), nStart
); }
3007 // find first/last occurence of any character (not) in the set:
3008 #if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
3009 // FIXME-UTF8: this is not entirely correct, because it doesn't work if
3010 // sizeof(wchar_t)==2 and surrogates are present in the string;
3011 // should we care? Probably not.
3012 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
3013 { return m_impl
.find_first_of(str
.m_impl
, nStart
); }
3014 size_t find_first_of(const char* sz
, size_t nStart
= 0) const
3015 { return m_impl
.find_first_of(ImplStr(sz
), nStart
); }
3016 size_t find_first_of(const wchar_t* sz
, size_t nStart
= 0) const
3017 { return m_impl
.find_first_of(ImplStr(sz
), nStart
); }
3018 size_t find_first_of(const char* sz
, size_t nStart
, size_t n
) const
3019 { return m_impl
.find_first_of(ImplStr(sz
), nStart
, n
); }
3020 size_t find_first_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
3021 { return m_impl
.find_first_of(ImplStr(sz
), nStart
, n
); }
3022 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const
3023 { return m_impl
.find_first_of((wxChar
)c
, nStart
); }
3025 size_t find_last_of(const wxString
& str
, size_t nStart
= npos
) const
3026 { return m_impl
.find_last_of(str
.m_impl
, nStart
); }
3027 size_t find_last_of(const char* sz
, size_t nStart
= npos
) const
3028 { return m_impl
.find_last_of(ImplStr(sz
), nStart
); }
3029 size_t find_last_of(const wchar_t* sz
, size_t nStart
= npos
) const
3030 { return m_impl
.find_last_of(ImplStr(sz
), nStart
); }
3031 size_t find_last_of(const char* sz
, size_t nStart
, size_t n
) const
3032 { return m_impl
.find_last_of(ImplStr(sz
), nStart
, n
); }
3033 size_t find_last_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
3034 { return m_impl
.find_last_of(ImplStr(sz
), nStart
, n
); }
3035 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const
3036 { return m_impl
.find_last_of((wxChar
)c
, nStart
); }
3038 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
3039 { return m_impl
.find_first_not_of(str
.m_impl
, nStart
); }
3040 size_t find_first_not_of(const char* sz
, size_t nStart
= 0) const
3041 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
); }
3042 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
= 0) const
3043 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
); }
3044 size_t find_first_not_of(const char* sz
, size_t nStart
, size_t n
) const
3045 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
, n
); }
3046 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
3047 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
, n
); }
3048 size_t find_first_not_of(wxUniChar c
, size_t nStart
= 0) const
3049 { return m_impl
.find_first_not_of((wxChar
)c
, nStart
); }
3051 size_t find_last_not_of(const wxString
& str
, size_t nStart
= npos
) const
3052 { return m_impl
.find_last_not_of(str
.m_impl
, nStart
); }
3053 size_t find_last_not_of(const char* sz
, size_t nStart
= npos
) const
3054 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
); }
3055 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
= npos
) const
3056 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
); }
3057 size_t find_last_not_of(const char* sz
, size_t nStart
, size_t n
) const
3058 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
, n
); }
3059 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
3060 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
, n
); }
3061 size_t find_last_not_of(wxUniChar c
, size_t nStart
= npos
) const
3062 { return m_impl
.find_last_not_of((wxChar
)c
, nStart
); }
3064 // we can't use std::string implementation in UTF-8 build, because the
3065 // character sets would be interpreted wrongly:
3067 // as strpbrk() but starts at nStart, returns npos if not found
3068 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
3069 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3070 { return find_first_of(str
.wc_str(), nStart
); }
3072 { return find_first_of(str
.mb_str(), nStart
); }
3075 size_t find_first_of(const char* sz
, size_t nStart
= 0) const;
3076 size_t find_first_of(const wchar_t* sz
, size_t nStart
= 0) const;
3077 size_t find_first_of(const char* sz
, size_t nStart
, size_t n
) const;
3078 size_t find_first_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
3079 // same as find(char, size_t)
3080 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const
3081 { return find(c
, nStart
); }
3082 // find the last (starting from nStart) char from str in this string
3083 size_t find_last_of (const wxString
& str
, size_t nStart
= npos
) const
3084 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3085 { return find_last_of(str
.wc_str(), nStart
); }
3087 { return find_last_of(str
.mb_str(), nStart
); }
3090 size_t find_last_of (const char* sz
, size_t nStart
= npos
) const;
3091 size_t find_last_of (const wchar_t* sz
, size_t nStart
= npos
) const;
3092 size_t find_last_of(const char* sz
, size_t nStart
, size_t n
) const;
3093 size_t find_last_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
3095 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const
3096 { return rfind(c
, nStart
); }
3098 // find first/last occurence of any character not in the set
3100 // as strspn() (starting from nStart), returns npos on failure
3101 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
3102 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3103 { return find_first_not_of(str
.wc_str(), nStart
); }
3105 { return find_first_not_of(str
.mb_str(), nStart
); }
3108 size_t find_first_not_of(const char* sz
, size_t nStart
= 0) const;
3109 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
= 0) const;
3110 size_t find_first_not_of(const char* sz
, size_t nStart
, size_t n
) const;
3111 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
3113 size_t find_first_not_of(wxUniChar ch
, size_t nStart
= 0) const;
3115 size_t find_last_not_of(const wxString
& str
, size_t nStart
= npos
) const
3116 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3117 { return find_last_not_of(str
.wc_str(), nStart
); }
3119 { return find_last_not_of(str
.mb_str(), nStart
); }
3122 size_t find_last_not_of(const char* sz
, size_t nStart
= npos
) const;
3123 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
= npos
) const;
3124 size_t find_last_not_of(const char* sz
, size_t nStart
, size_t n
) const;
3125 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
3127 size_t find_last_not_of(wxUniChar ch
, size_t nStart
= npos
) const;
3128 #endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not
3130 // provide char/wchar_t/wxUniCharRef overloads for char-finding functions
3131 // above to resolve ambiguities:
3132 size_t find_first_of(wxUniCharRef ch
, size_t nStart
= 0) const
3133 { return find_first_of(wxUniChar(ch
), nStart
); }
3134 size_t find_first_of(char ch
, size_t nStart
= 0) const
3135 { return find_first_of(wxUniChar(ch
), nStart
); }
3136 size_t find_first_of(unsigned char ch
, size_t nStart
= 0) const
3137 { return find_first_of(wxUniChar(ch
), nStart
); }
3138 size_t find_first_of(wchar_t ch
, size_t nStart
= 0) const
3139 { return find_first_of(wxUniChar(ch
), nStart
); }
3140 size_t find_last_of(wxUniCharRef ch
, size_t nStart
= npos
) const
3141 { return find_last_of(wxUniChar(ch
), nStart
); }
3142 size_t find_last_of(char ch
, size_t nStart
= npos
) const
3143 { return find_last_of(wxUniChar(ch
), nStart
); }
3144 size_t find_last_of(unsigned char ch
, size_t nStart
= npos
) const
3145 { return find_last_of(wxUniChar(ch
), nStart
); }
3146 size_t find_last_of(wchar_t ch
, size_t nStart
= npos
) const
3147 { return find_last_of(wxUniChar(ch
), nStart
); }
3148 size_t find_first_not_of(wxUniCharRef ch
, size_t nStart
= 0) const
3149 { return find_first_not_of(wxUniChar(ch
), nStart
); }
3150 size_t find_first_not_of(char ch
, size_t nStart
= 0) const
3151 { return find_first_not_of(wxUniChar(ch
), nStart
); }
3152 size_t find_first_not_of(unsigned char ch
, size_t nStart
= 0) const
3153 { return find_first_not_of(wxUniChar(ch
), nStart
); }
3154 size_t find_first_not_of(wchar_t ch
, size_t nStart
= 0) const
3155 { return find_first_not_of(wxUniChar(ch
), nStart
); }
3156 size_t find_last_not_of(wxUniCharRef ch
, size_t nStart
= npos
) const
3157 { return find_last_not_of(wxUniChar(ch
), nStart
); }
3158 size_t find_last_not_of(char ch
, size_t nStart
= npos
) const
3159 { return find_last_not_of(wxUniChar(ch
), nStart
); }
3160 size_t find_last_not_of(unsigned char ch
, size_t nStart
= npos
) const
3161 { return find_last_not_of(wxUniChar(ch
), nStart
); }
3162 size_t find_last_not_of(wchar_t ch
, size_t nStart
= npos
) const
3163 { return find_last_not_of(wxUniChar(ch
), nStart
); }
3165 // and additional overloads for the versions taking strings:
3166 size_t find_first_of(const wxCStrData
& sz
, size_t nStart
= 0) const
3167 { return find_first_of(sz
.AsString(), nStart
); }
3168 size_t find_first_of(const wxCharBuffer
& sz
, size_t nStart
= 0) const
3169 { return find_first_of(sz
.data(), nStart
); }
3170 size_t find_first_of(const wxWCharBuffer
& sz
, size_t nStart
= 0) const
3171 { return find_first_of(sz
.data(), nStart
); }
3172 size_t find_first_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
3173 { return find_first_of(sz
.AsWChar(), nStart
, n
); }
3174 size_t find_first_of(const wxCharBuffer
& sz
, size_t nStart
, size_t n
) const
3175 { return find_first_of(sz
.data(), nStart
, n
); }
3176 size_t find_first_of(const wxWCharBuffer
& sz
, size_t nStart
, size_t n
) const
3177 { return find_first_of(sz
.data(), nStart
, n
); }
3179 size_t find_last_of(const wxCStrData
& sz
, size_t nStart
= 0) const
3180 { return find_last_of(sz
.AsString(), nStart
); }
3181 size_t find_last_of(const wxCharBuffer
& sz
, size_t nStart
= 0) const
3182 { return find_last_of(sz
.data(), nStart
); }
3183 size_t find_last_of(const wxWCharBuffer
& sz
, size_t nStart
= 0) const
3184 { return find_last_of(sz
.data(), nStart
); }
3185 size_t find_last_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
3186 { return find_last_of(sz
.AsWChar(), nStart
, n
); }
3187 size_t find_last_of(const wxCharBuffer
& sz
, size_t nStart
, size_t n
) const
3188 { return find_last_of(sz
.data(), nStart
, n
); }
3189 size_t find_last_of(const wxWCharBuffer
& sz
, size_t nStart
, size_t n
) const
3190 { return find_last_of(sz
.data(), nStart
, n
); }
3192 size_t find_first_not_of(const wxCStrData
& sz
, size_t nStart
= 0) const
3193 { return find_first_not_of(sz
.AsString(), nStart
); }
3194 size_t find_first_not_of(const wxCharBuffer
& sz
, size_t nStart
= 0) const
3195 { return find_first_not_of(sz
.data(), nStart
); }
3196 size_t find_first_not_of(const wxWCharBuffer
& sz
, size_t nStart
= 0) const
3197 { return find_first_not_of(sz
.data(), nStart
); }
3198 size_t find_first_not_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
3199 { return find_first_not_of(sz
.AsWChar(), nStart
, n
); }
3200 size_t find_first_not_of(const wxCharBuffer
& sz
, size_t nStart
, size_t n
) const
3201 { return find_first_not_of(sz
.data(), nStart
, n
); }
3202 size_t find_first_not_of(const wxWCharBuffer
& sz
, size_t nStart
, size_t n
) const
3203 { return find_first_not_of(sz
.data(), nStart
, n
); }
3205 size_t find_last_not_of(const wxCStrData
& sz
, size_t nStart
= 0) const
3206 { return find_last_not_of(sz
.AsString(), nStart
); }
3207 size_t find_last_not_of(const wxCharBuffer
& sz
, size_t nStart
= 0) const
3208 { return find_last_not_of(sz
.data(), nStart
); }
3209 size_t find_last_not_of(const wxWCharBuffer
& sz
, size_t nStart
= 0) const
3210 { return find_last_not_of(sz
.data(), nStart
); }
3211 size_t find_last_not_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
3212 { return find_last_not_of(sz
.AsWChar(), nStart
, n
); }
3213 size_t find_last_not_of(const wxCharBuffer
& sz
, size_t nStart
, size_t n
) const
3214 { return find_last_not_of(sz
.data(), nStart
, n
); }
3215 size_t find_last_not_of(const wxWCharBuffer
& sz
, size_t nStart
, size_t n
) const
3216 { return find_last_not_of(sz
.data(), nStart
, n
); }
3219 wxString
& operator+=(const wxString
& s
)
3221 wxSTRING_INVALIDATE_CACHED_LENGTH();
3226 // string += C string
3227 wxString
& operator+=(const char *psz
)
3229 wxSTRING_INVALIDATE_CACHED_LENGTH();
3231 m_impl
+= ImplStr(psz
);
3234 wxString
& operator+=(const wchar_t *pwz
)
3236 wxSTRING_INVALIDATE_CACHED_LENGTH();
3238 m_impl
+= ImplStr(pwz
);
3241 wxString
& operator+=(const wxCStrData
& s
)
3243 wxSTRING_INVALIDATE_CACHED_LENGTH();
3245 m_impl
+= s
.AsString().m_impl
;
3248 wxString
& operator+=(const wxCharBuffer
& s
)
3249 { return operator+=(s
.data()); }
3250 wxString
& operator+=(const wxWCharBuffer
& s
)
3251 { return operator+=(s
.data()); }
3253 wxString
& operator+=(wxUniChar ch
)
3255 wxSTRING_UPDATE_CACHED_LENGTH(1);
3257 #if wxUSE_UNICODE_UTF8
3258 if ( !ch
.IsAscii() )
3259 m_impl
+= wxStringOperations::EncodeChar(ch
);
3262 m_impl
+= (wxStringCharType
)ch
;
3265 wxString
& operator+=(wxUniCharRef ch
) { return *this += wxUniChar(ch
); }
3266 wxString
& operator+=(int ch
) { return *this += wxUniChar(ch
); }
3267 wxString
& operator+=(char ch
) { return *this += wxUniChar(ch
); }
3268 wxString
& operator+=(unsigned char ch
) { return *this += wxUniChar(ch
); }
3269 wxString
& operator+=(wchar_t ch
) { return *this += wxUniChar(ch
); }
3272 #if !wxUSE_STL_BASED_WXSTRING
3273 // helpers for wxStringBuffer and wxStringBufferLength
3274 wxStringCharType
*DoGetWriteBuf(size_t nLen
)
3276 return m_impl
.DoGetWriteBuf(nLen
);
3279 void DoUngetWriteBuf()
3281 wxSTRING_INVALIDATE_CACHE();
3283 m_impl
.DoUngetWriteBuf();
3286 void DoUngetWriteBuf(size_t nLen
)
3288 wxSTRING_SET_CACHED_LENGTH(nLen
);
3290 m_impl
.DoUngetWriteBuf(nLen
);
3292 #endif // !wxUSE_STL_BASED_WXSTRING
3294 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
3295 #if !wxUSE_UTF8_LOCALE_ONLY
3296 int DoPrintfWchar(const wxChar
*format
, ...);
3297 static wxString
DoFormatWchar(const wxChar
*format
, ...);
3299 #if wxUSE_UNICODE_UTF8
3300 int DoPrintfUtf8(const char *format
, ...);
3301 static wxString
DoFormatUtf8(const char *format
, ...);
3305 #if !wxUSE_STL_BASED_WXSTRING
3306 // check string's data validity
3307 bool IsValid() const { return m_impl
.GetStringData()->IsValid(); }
3311 wxStringImpl m_impl
;
3313 // buffers for compatibility conversion from (char*)c_str() and
3314 // (wchar_t*)c_str():
3315 // FIXME-UTF8: bechmark various approaches to keeping compatibility buffers
3316 template<typename T
>
3317 struct ConvertedBuffer
3319 ConvertedBuffer() : m_buf(NULL
) {}
3323 operator T
*() const { return m_buf
; }
3325 ConvertedBuffer
& operator=(T
*str
)
3334 #if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
3335 ConvertedBuffer
<char> m_convertedToChar
;
3337 #if !wxUSE_UNICODE_WCHAR
3338 ConvertedBuffer
<wchar_t> m_convertedToWChar
;
3341 #if wxUSE_UNICODE_UTF8
3342 // FIXME-UTF8: (try to) move this elsewhere (TLS) or solve differently
3343 // assigning to character pointer to by wxString::interator may
3344 // change the underlying wxStringImpl iterator, so we have to
3345 // keep track of all iterators and update them as necessary:
3346 struct wxStringIteratorNodeHead
3348 wxStringIteratorNodeHead() : ptr(NULL
) {}
3349 wxStringIteratorNode
*ptr
;
3351 // copying is disallowed as it would result in more than one pointer into
3352 // the same linked list
3353 DECLARE_NO_COPY_CLASS(wxStringIteratorNodeHead
)
3356 wxStringIteratorNodeHead m_iterators
;
3358 friend class WXDLLIMPEXP_FWD_BASE wxStringIteratorNode
;
3359 friend class WXDLLIMPEXP_FWD_BASE wxUniCharRef
;
3360 #endif // wxUSE_UNICODE_UTF8
3362 friend class WXDLLIMPEXP_FWD_BASE wxCStrData
;
3363 friend class wxStringInternalBuffer
;
3364 friend class wxStringInternalBufferLength
;
3367 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
3368 #pragma warning (default:4275)
3371 // string iterator operators that satisfy STL Random Access Iterator
3373 inline wxString::iterator
operator+(ptrdiff_t n
, wxString::iterator i
)
3375 inline wxString::const_iterator
operator+(ptrdiff_t n
, wxString::const_iterator i
)
3377 inline wxString::reverse_iterator
operator+(ptrdiff_t n
, wxString::reverse_iterator i
)
3379 inline wxString::const_reverse_iterator
operator+(ptrdiff_t n
, wxString::const_reverse_iterator i
)
3382 // notice that even though for many compilers the friend declarations above are
3383 // enough, from the point of view of C++ standard we must have the declarations
3384 // here as friend ones are not injected in the enclosing namespace and without
3385 // them the code fails to compile with conforming compilers such as xlC or g++4
3386 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
, const wxString
& string2
);
3387 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const char *psz
);
3388 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const wchar_t *pwz
);
3389 wxString WXDLLIMPEXP_BASE
operator+(const char *psz
, const wxString
& string
);
3390 wxString WXDLLIMPEXP_BASE
operator+(const wchar_t *pwz
, const wxString
& string
);
3392 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
3393 wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
3395 inline wxString
operator+(const wxString
& string
, wxUniCharRef ch
)
3396 { return string
+ (wxUniChar
)ch
; }
3397 inline wxString
operator+(const wxString
& string
, char ch
)
3398 { return string
+ wxUniChar(ch
); }
3399 inline wxString
operator+(const wxString
& string
, wchar_t ch
)
3400 { return string
+ wxUniChar(ch
); }
3401 inline wxString
operator+(wxUniCharRef ch
, const wxString
& string
)
3402 { return (wxUniChar
)ch
+ string
; }
3403 inline wxString
operator+(char ch
, const wxString
& string
)
3404 { return wxUniChar(ch
) + string
; }
3405 inline wxString
operator+(wchar_t ch
, const wxString
& string
)
3406 { return wxUniChar(ch
) + string
; }
3409 #define wxGetEmptyString() wxString()
3411 // ----------------------------------------------------------------------------
3412 // helper functions which couldn't be defined inline
3413 // ----------------------------------------------------------------------------
3418 #if wxUSE_UNICODE_WCHAR
3421 struct wxStringAsBufHelper
<char>
3423 static wxCharBuffer
Get(const wxString
& s
, size_t *len
)
3425 wxCharBuffer
buf(s
.mb_str());
3427 *len
= buf
? strlen(buf
) : 0;
3433 struct wxStringAsBufHelper
<wchar_t>
3435 static wxWCharBuffer
Get(const wxString
& s
, size_t *len
)
3439 return wxWCharBuffer::CreateNonOwned(s
.wx_str());
3443 #elif wxUSE_UNICODE_UTF8
3446 struct wxStringAsBufHelper
<char>
3448 static wxCharBuffer
Get(const wxString
& s
, size_t *len
)
3451 *len
= s
.utf8_length();
3452 return wxCharBuffer::CreateNonOwned(s
.wx_str());
3457 struct wxStringAsBufHelper
<wchar_t>
3459 static wxWCharBuffer
Get(const wxString
& s
, size_t *len
)
3461 wxWCharBuffer
wbuf(s
.wc_str());
3463 *len
= wxWcslen(wbuf
);
3468 #endif // Unicode build kind
3470 } // namespace wxPrivate
3472 // ----------------------------------------------------------------------------
3473 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
3474 // ----------------------------------------------------------------------------
3476 #if !wxUSE_STL_BASED_WXSTRING
3477 // string buffer for direct access to string data in their native
3479 class wxStringInternalBuffer
3482 typedef wxStringCharType CharType
;
3484 wxStringInternalBuffer(wxString
& str
, size_t lenWanted
= 1024)
3485 : m_str(str
), m_buf(NULL
)
3486 { m_buf
= m_str
.DoGetWriteBuf(lenWanted
); }
3488 ~wxStringInternalBuffer() { m_str
.DoUngetWriteBuf(); }
3490 operator wxStringCharType
*() const { return m_buf
; }
3494 wxStringCharType
*m_buf
;
3496 DECLARE_NO_COPY_CLASS(wxStringInternalBuffer
)
3499 class wxStringInternalBufferLength
3502 typedef wxStringCharType CharType
;
3504 wxStringInternalBufferLength(wxString
& str
, size_t lenWanted
= 1024)
3505 : m_str(str
), m_buf(NULL
), m_len(0), m_lenSet(false)
3507 m_buf
= m_str
.DoGetWriteBuf(lenWanted
);
3508 wxASSERT(m_buf
!= NULL
);
3511 ~wxStringInternalBufferLength()
3514 m_str
.DoUngetWriteBuf(m_len
);
3517 operator wxStringCharType
*() const { return m_buf
; }
3518 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
3522 wxStringCharType
*m_buf
;
3526 DECLARE_NO_COPY_CLASS(wxStringInternalBufferLength
)
3529 #endif // !wxUSE_STL_BASED_WXSTRING
3531 template<typename T
>
3532 class WXDLLIMPEXP_BASE wxStringTypeBufferBase
3537 wxStringTypeBufferBase(wxString
& str
, size_t lenWanted
= 1024)
3538 : m_str(str
), m_buf(lenWanted
)
3540 // for compatibility with old wxStringBuffer which provided direct
3541 // access to wxString internal buffer, initialize ourselves with the
3542 // string initial contents
3544 // FIXME-VC6: remove the ugly (CharType *)NULL and use normal
3545 // tchar_str<CharType>
3547 const wxCharTypeBuffer
<CharType
> buf(str
.tchar_str(&len
, (CharType
*)NULL
));
3550 if ( len
> lenWanted
)
3552 // in this case there is not enough space for terminating NUL,
3553 // ensure that we still put it there
3554 m_buf
.data()[lenWanted
] = 0;
3555 len
= lenWanted
- 1;
3558 memcpy(m_buf
.data(), buf
, (len
+ 1)*sizeof(CharType
));
3560 //else: conversion failed, this can happen when trying to get Unicode
3561 // string contents into a char string
3564 operator CharType
*() { return m_buf
.data(); }
3568 wxCharTypeBuffer
<CharType
> m_buf
;
3571 template<typename T
>
3572 class WXDLLIMPEXP_BASE wxStringTypeBufferLengthBase
3573 : public wxStringTypeBufferBase
<T
>
3576 wxStringTypeBufferLengthBase(wxString
& str
, size_t lenWanted
= 1024)
3577 : wxStringTypeBufferBase
<T
>(str
, lenWanted
),
3582 ~wxStringTypeBufferLengthBase()
3584 wxASSERT_MSG( this->m_lenSet
, "forgot to call SetLength()" );
3587 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
3594 template<typename T
>
3595 class wxStringTypeBuffer
: public wxStringTypeBufferBase
<T
>
3598 wxStringTypeBuffer(wxString
& str
, size_t lenWanted
= 1024)
3599 : wxStringTypeBufferBase
<T
>(str
, lenWanted
)
3602 ~wxStringTypeBuffer()
3604 this->m_str
.assign(this->m_buf
.data());
3607 DECLARE_NO_COPY_CLASS(wxStringTypeBuffer
)
3610 template<typename T
>
3611 class wxStringTypeBufferLength
: public wxStringTypeBufferLengthBase
<T
>
3614 wxStringTypeBufferLength(wxString
& str
, size_t lenWanted
= 1024)
3615 : wxStringTypeBufferLengthBase
<T
>(str
, lenWanted
)
3618 ~wxStringTypeBufferLength()
3620 this->m_str
.assign(this->m_buf
.data(), this->m_len
);
3623 DECLARE_NO_COPY_CLASS(wxStringTypeBufferLength
)
3626 #if wxUSE_STL_BASED_WXSTRING
3628 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase
<wxStringCharType
> )
3630 class wxStringInternalBuffer
: public wxStringTypeBufferBase
<wxStringCharType
>
3633 wxStringInternalBuffer(wxString
& str
, size_t lenWanted
= 1024)
3634 : wxStringTypeBufferBase
<wxStringCharType
>(str
, lenWanted
) {}
3635 ~wxStringInternalBuffer()
3636 { m_str
.m_impl
.assign(m_buf
.data()); }
3638 DECLARE_NO_COPY_CLASS(wxStringInternalBuffer
)
3641 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE(
3642 wxStringTypeBufferLengthBase
<wxStringCharType
> )
3644 class wxStringInternalBufferLength
3645 : public wxStringTypeBufferLengthBase
<wxStringCharType
>
3648 wxStringInternalBufferLength(wxString
& str
, size_t lenWanted
= 1024)
3649 : wxStringTypeBufferLengthBase
<wxStringCharType
>(str
, lenWanted
) {}
3651 ~wxStringInternalBufferLength()
3653 m_str
.m_impl
.assign(m_buf
.data(), m_len
);
3656 DECLARE_NO_COPY_CLASS(wxStringInternalBufferLength
)
3659 #endif // wxUSE_STL_BASED_WXSTRING
3662 #if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
3663 typedef wxStringTypeBuffer
<wxChar
> wxStringBuffer
;
3664 typedef wxStringTypeBufferLength
<wxChar
> wxStringBufferLength
;
3665 #else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
3666 typedef wxStringInternalBuffer wxStringBuffer
;
3667 typedef wxStringInternalBufferLength wxStringBufferLength
;
3668 #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
3670 #if wxUSE_UNICODE_UTF8
3671 typedef wxStringInternalBuffer wxUTF8StringBuffer
;
3672 typedef wxStringInternalBufferLength wxUTF8StringBufferLength
;
3673 #elif wxUSE_UNICODE_WCHAR
3675 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase
<char> )
3677 class WXDLLIMPEXP_BASE wxUTF8StringBuffer
: public wxStringTypeBufferBase
<char>
3680 wxUTF8StringBuffer(wxString
& str
, size_t lenWanted
= 1024)
3681 : wxStringTypeBufferBase
<char>(str
, lenWanted
) {}
3682 ~wxUTF8StringBuffer();
3684 DECLARE_NO_COPY_CLASS(wxUTF8StringBuffer
)
3687 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferLengthBase
<char> )
3689 class WXDLLIMPEXP_BASE wxUTF8StringBufferLength
3690 : public wxStringTypeBufferLengthBase
<char>
3693 wxUTF8StringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
3694 : wxStringTypeBufferLengthBase
<char>(str
, lenWanted
) {}
3695 ~wxUTF8StringBufferLength();
3697 DECLARE_NO_COPY_CLASS(wxUTF8StringBufferLength
)
3699 #endif // wxUSE_UNICODE_UTF8/wxUSE_UNICODE_WCHAR
3702 // ---------------------------------------------------------------------------
3703 // wxString comparison functions: operator versions are always case sensitive
3704 // ---------------------------------------------------------------------------
3706 #define wxCMP_WXCHAR_STRING(p, s, op) 0 op s.Cmp(p)
3708 wxDEFINE_ALL_COMPARISONS(const wxChar
*, const wxString
&, wxCMP_WXCHAR_STRING
)
3710 #undef wxCMP_WXCHAR_STRING
3712 inline bool operator==(const wxString
& s1
, const wxString
& s2
)
3713 { return s1
.IsSameAs(s2
); }
3714 inline bool operator!=(const wxString
& s1
, const wxString
& s2
)
3715 { return !s1
.IsSameAs(s2
); }
3716 inline bool operator< (const wxString
& s1
, const wxString
& s2
)
3717 { return s1
.Cmp(s2
) < 0; }
3718 inline bool operator> (const wxString
& s1
, const wxString
& s2
)
3719 { return s1
.Cmp(s2
) > 0; }
3720 inline bool operator<=(const wxString
& s1
, const wxString
& s2
)
3721 { return s1
.Cmp(s2
) <= 0; }
3722 inline bool operator>=(const wxString
& s1
, const wxString
& s2
)
3723 { return s1
.Cmp(s2
) >= 0; }
3725 inline bool operator==(const wxString
& s1
, const wxCStrData
& s2
)
3726 { return s1
== s2
.AsString(); }
3727 inline bool operator==(const wxCStrData
& s1
, const wxString
& s2
)
3728 { return s1
.AsString() == s2
; }
3729 inline bool operator!=(const wxString
& s1
, const wxCStrData
& s2
)
3730 { return s1
!= s2
.AsString(); }
3731 inline bool operator!=(const wxCStrData
& s1
, const wxString
& s2
)
3732 { return s1
.AsString() != s2
; }
3734 inline bool operator==(const wxString
& s1
, const wxWCharBuffer
& s2
)
3735 { return (s1
.Cmp((const wchar_t *)s2
) == 0); }
3736 inline bool operator==(const wxWCharBuffer
& s1
, const wxString
& s2
)
3737 { return (s2
.Cmp((const wchar_t *)s1
) == 0); }
3738 inline bool operator!=(const wxString
& s1
, const wxWCharBuffer
& s2
)
3739 { return (s1
.Cmp((const wchar_t *)s2
) != 0); }
3740 inline bool operator!=(const wxWCharBuffer
& s1
, const wxString
& s2
)
3741 { return (s2
.Cmp((const wchar_t *)s1
) != 0); }
3743 inline bool operator==(const wxString
& s1
, const wxCharBuffer
& s2
)
3744 { return (s1
.Cmp((const char *)s2
) == 0); }
3745 inline bool operator==(const wxCharBuffer
& s1
, const wxString
& s2
)
3746 { return (s2
.Cmp((const char *)s1
) == 0); }
3747 inline bool operator!=(const wxString
& s1
, const wxCharBuffer
& s2
)
3748 { return (s1
.Cmp((const char *)s2
) != 0); }
3749 inline bool operator!=(const wxCharBuffer
& s1
, const wxString
& s2
)
3750 { return (s2
.Cmp((const char *)s1
) != 0); }
3752 inline wxString
operator+(const wxString
& string
, const wxWCharBuffer
& buf
)
3753 { return string
+ (const wchar_t *)buf
; }
3754 inline wxString
operator+(const wxWCharBuffer
& buf
, const wxString
& string
)
3755 { return (const wchar_t *)buf
+ string
; }
3757 inline wxString
operator+(const wxString
& string
, const wxCharBuffer
& buf
)
3758 { return string
+ (const char *)buf
; }
3759 inline wxString
operator+(const wxCharBuffer
& buf
, const wxString
& string
)
3760 { return (const char *)buf
+ string
; }
3762 // comparison with char
3763 inline bool operator==(const wxUniChar
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
3764 inline bool operator==(const wxUniCharRef
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
3765 inline bool operator==(char c
, const wxString
& s
) { return s
.IsSameAs(c
); }
3766 inline bool operator==(wchar_t c
, const wxString
& s
) { return s
.IsSameAs(c
); }
3767 inline bool operator==(int c
, const wxString
& s
) { return s
.IsSameAs(c
); }
3768 inline bool operator==(const wxString
& s
, const wxUniChar
& c
) { return s
.IsSameAs(c
); }
3769 inline bool operator==(const wxString
& s
, const wxUniCharRef
& c
) { return s
.IsSameAs(c
); }
3770 inline bool operator==(const wxString
& s
, char c
) { return s
.IsSameAs(c
); }
3771 inline bool operator==(const wxString
& s
, wchar_t c
) { return s
.IsSameAs(c
); }
3772 inline bool operator!=(const wxUniChar
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
3773 inline bool operator!=(const wxUniCharRef
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
3774 inline bool operator!=(char c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
3775 inline bool operator!=(wchar_t c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
3776 inline bool operator!=(int c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
3777 inline bool operator!=(const wxString
& s
, const wxUniChar
& c
) { return !s
.IsSameAs(c
); }
3778 inline bool operator!=(const wxString
& s
, const wxUniCharRef
& c
) { return !s
.IsSameAs(c
); }
3779 inline bool operator!=(const wxString
& s
, char c
) { return !s
.IsSameAs(c
); }
3780 inline bool operator!=(const wxString
& s
, wchar_t c
) { return !s
.IsSameAs(c
); }
3782 // comparison with C string in Unicode build
3785 #define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s
3787 wxDEFINE_ALL_COMPARISONS(const char *, const wxString
&, wxCMP_CHAR_STRING
)
3789 #undef wxCMP_CHAR_STRING
3791 #endif // wxUSE_UNICODE
3793 // we also need to provide the operators for comparison with wxCStrData to
3794 // resolve ambiguity between operator(const wxChar *,const wxString &) and
3795 // operator(const wxChar *, const wxChar *) for "p == s.c_str()"
3797 // notice that these are (shallow) pointer comparisons, not (deep) string ones
3798 #define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar()
3799 #define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar()
3801 wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData
&, wxCMP_WCHAR_CSTRDATA
)
3802 wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData
&, wxCMP_CHAR_CSTRDATA
)
3804 #undef wxCMP_CHAR_CSTRDATA
3805 #undef wxCMP_WCHAR_CSTRDATA
3807 // ---------------------------------------------------------------------------
3808 // Implementation only from here until the end of file
3809 // ---------------------------------------------------------------------------
3811 #if wxUSE_STD_IOSTREAM
3813 #include "wx/iosfwrap.h"
3815 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxString
&);
3816 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxCStrData
&);
3817 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxCharBuffer
&);
3818 #ifndef __BORLANDC__
3819 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxWCharBuffer
&);
3822 #if wxUSE_UNICODE && defined(HAVE_WOSTREAM)
3824 WXDLLIMPEXP_BASE wxSTD wostream
& operator<<(wxSTD wostream
&, const wxString
&);
3825 WXDLLIMPEXP_BASE wxSTD wostream
& operator<<(wxSTD wostream
&, const wxCStrData
&);
3826 WXDLLIMPEXP_BASE wxSTD wostream
& operator<<(wxSTD wostream
&, const wxWCharBuffer
&);
3828 #endif // wxUSE_UNICODE && defined(HAVE_WOSTREAM)
3830 #endif // wxUSE_STD_IOSTREAM
3832 // ---------------------------------------------------------------------------
3833 // wxCStrData implementation
3834 // ---------------------------------------------------------------------------
3836 inline wxCStrData::wxCStrData(char *buf
)
3837 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
3838 inline wxCStrData::wxCStrData(wchar_t *buf
)
3839 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
3841 inline wxCStrData::wxCStrData(const wxCStrData
& data
)
3842 : m_str(data
.m_owned
? new wxString(*data
.m_str
) : data
.m_str
),
3843 m_offset(data
.m_offset
),
3844 m_owned(data
.m_owned
)
3848 inline wxCStrData::~wxCStrData()
3851 delete wx_const_cast(wxString
*, m_str
); // cast to silence warnings
3854 // simple cases for AsChar() and AsWChar(), the complicated ones are
3856 #if wxUSE_UNICODE_WCHAR
3857 inline const wchar_t* wxCStrData::AsWChar() const
3859 return m_str
->wx_str() + m_offset
;
3861 #endif // wxUSE_UNICODE_WCHAR
3864 inline const char* wxCStrData::AsChar() const
3866 return m_str
->wx_str() + m_offset
;
3868 #endif // !wxUSE_UNICODE
3870 #if wxUSE_UTF8_LOCALE_ONLY
3871 inline const char* wxCStrData::AsChar() const
3873 return wxStringOperations::AddToIter(m_str
->wx_str(), m_offset
);
3875 #endif // wxUSE_UTF8_LOCALE_ONLY
3877 inline const wxCharBuffer
wxCStrData::AsCharBuf() const
3880 return wxCharBuffer::CreateNonOwned(AsChar());
3882 return AsString().mb_str();
3886 inline const wxWCharBuffer
wxCStrData::AsWCharBuf() const
3888 #if wxUSE_UNICODE_WCHAR
3889 return wxWCharBuffer::CreateNonOwned(AsWChar());
3891 return AsString().wc_str();
3895 inline wxString
wxCStrData::AsString() const
3897 if ( m_offset
== 0 )
3900 return m_str
->Mid(m_offset
);
3903 inline const wxStringCharType
*wxCStrData::AsInternal() const
3905 #if wxUSE_UNICODE_UTF8
3906 return wxStringOperations::AddToIter(m_str
->wx_str(), m_offset
);
3908 return m_str
->wx_str() + m_offset
;
3912 inline wxUniChar
wxCStrData::operator*() const
3914 if ( m_str
->empty() )
3915 return wxUniChar(_T('\0'));
3917 return (*m_str
)[m_offset
];
3920 inline wxUniChar
wxCStrData::operator[](size_t n
) const
3922 // NB: we intentionally use operator[] and not at() here because the former
3923 // works for the terminating NUL while the latter does not
3924 return (*m_str
)[m_offset
+ n
];
3927 // ----------------------------------------------------------------------------
3928 // more wxCStrData operators
3929 // ----------------------------------------------------------------------------
3931 // we need to define those to allow "size_t pos = p - s.c_str()" where p is
3932 // some pointer into the string
3933 inline size_t operator-(const char *p
, const wxCStrData
& cs
)
3935 return p
- cs
.AsChar();
3938 inline size_t operator-(const wchar_t *p
, const wxCStrData
& cs
)
3940 return p
- cs
.AsWChar();
3943 // ----------------------------------------------------------------------------
3944 // implementation of wx[W]CharBuffer inline methods using wxCStrData
3945 // ----------------------------------------------------------------------------
3947 // FIXME-UTF8: move this to buffer.h
3948 inline wxCharBuffer::wxCharBuffer(const wxCStrData
& cstr
)
3949 : wxCharTypeBufferBase(cstr
.AsCharBuf())
3953 inline wxWCharBuffer::wxWCharBuffer(const wxCStrData
& cstr
)
3954 : wxCharTypeBufferBase(cstr
.AsWCharBuf())
3958 #if wxUSE_UNICODE_UTF8
3959 // ----------------------------------------------------------------------------
3960 // implementation of wxStringIteratorNode inline methods
3961 // ----------------------------------------------------------------------------
3963 void wxStringIteratorNode::DoSet(const wxString
*str
,
3964 wxStringImpl::const_iterator
*citer
,
3965 wxStringImpl::iterator
*iter
)
3973 m_next
= str
->m_iterators
.ptr
;
3974 wx_const_cast(wxString
*, m_str
)->m_iterators
.ptr
= this;
3976 m_next
->m_prev
= this;
3984 void wxStringIteratorNode::clear()
3987 m_next
->m_prev
= m_prev
;
3989 m_prev
->m_next
= m_next
;
3990 else if ( m_str
) // first in the list
3991 wx_const_cast(wxString
*, m_str
)->m_iterators
.ptr
= m_next
;
3993 m_next
= m_prev
= NULL
;
3998 #endif // wxUSE_UNICODE_UTF8
4000 #if WXWIN_COMPATIBILITY_2_8
4001 // lot of code out there doesn't explicitly include wx/crt.h, but uses
4002 // CRT wrappers that are now declared in wx/wxcrt.h and wx/wxcrtvararg.h,
4003 // so let's include this header now that wxString is defined and it's safe
4008 #endif // _WX_WXSTRING_H_