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 #if wxUSE_STRING_POS_CACHE
81 // change this 0 to 1 to enable additional (very expensive) asserts
82 // verifying that string caching logic works as expected
84 #define wxSTRING_CACHE_ASSERT(cond) wxASSERT(cond)
86 #define wxSTRING_CACHE_ASSERT(cond)
88 #endif // wxUSE_STRING_POS_CACHE
90 class WXDLLIMPEXP_FWD_BASE wxString
;
92 // unless this symbol is predefined to disable the compatibility functions, do
94 #ifndef WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
95 #define WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER 1
100 template <typename T
> struct wxStringAsBufHelper
;
103 // ---------------------------------------------------------------------------
105 // ---------------------------------------------------------------------------
107 // casts [unfortunately!] needed to call some broken functions which require
108 // "char *" instead of "const char *"
109 #define WXSTRINGCAST (wxChar *)(const wxChar *)
110 #define wxCSTRINGCAST (wxChar *)(const wxChar *)
111 #define wxMBSTRINGCAST (char *)(const char *)
112 #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *)
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
118 #if WXWIN_COMPATIBILITY_2_6
120 // deprecated in favour of wxString::npos, don't use in new code
122 // maximum possible length for a string means "take all string" everywhere
123 #define wxSTRING_MAXLEN wxString::npos
125 #endif // WXWIN_COMPATIBILITY_2_6
127 // ---------------------------------------------------------------------------
128 // global functions complementing standard C string library replacements for
129 // strlen() and portable strcasecmp()
130 //---------------------------------------------------------------------------
132 #if WXWIN_COMPATIBILITY_2_8
133 // Use wxXXX() functions from wxcrt.h instead! These functions are for
134 // backwards compatibility only.
136 // checks whether the passed in pointer is NULL and if the string is empty
137 wxDEPRECATED( inline bool IsEmpty(const char *p
) );
138 inline bool IsEmpty(const char *p
) { return (!p
|| !*p
); }
140 // safe version of strlen() (returns 0 if passed NULL pointer)
141 wxDEPRECATED( inline size_t Strlen(const char *psz
) );
142 inline size_t Strlen(const char *psz
)
143 { return psz
? strlen(psz
) : 0; }
145 // portable strcasecmp/_stricmp
146 wxDEPRECATED( inline int Stricmp(const char *psz1
, const char *psz2
) );
147 inline int Stricmp(const char *psz1
, const char *psz2
)
149 #if defined(__VISUALC__) && defined(__WXWINCE__)
150 register char c1
, c2
;
152 c1
= tolower(*psz1
++);
153 c2
= tolower(*psz2
++);
154 } while ( c1
&& (c1
== c2
) );
157 #elif defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) )
158 return _stricmp(psz1
, psz2
);
159 #elif defined(__SC__)
160 return _stricmp(psz1
, psz2
);
161 #elif defined(__BORLANDC__)
162 return stricmp(psz1
, psz2
);
163 #elif defined(__WATCOMC__)
164 return stricmp(psz1
, psz2
);
165 #elif defined(__DJGPP__)
166 return stricmp(psz1
, psz2
);
167 #elif defined(__EMX__)
168 return stricmp(psz1
, psz2
);
169 #elif defined(__WXPM__)
170 return stricmp(psz1
, psz2
);
171 #elif defined(__WXPALMOS__) || \
172 defined(HAVE_STRCASECMP_IN_STRING_H) || \
173 defined(HAVE_STRCASECMP_IN_STRINGS_H) || \
174 defined(__GNUWIN32__)
175 return strcasecmp(psz1
, psz2
);
176 #elif defined(__MWERKS__) && !defined(__INTEL__)
177 register char c1
, c2
;
179 c1
= tolower(*psz1
++);
180 c2
= tolower(*psz2
++);
181 } while ( c1
&& (c1
== c2
) );
185 // almost all compilers/libraries provide this function (unfortunately under
186 // different names), that's why we don't implement our own which will surely
187 // be more efficient than this code (uncomment to use):
189 register char c1, c2;
191 c1 = tolower(*psz1++);
192 c2 = tolower(*psz2++);
193 } while ( c1 && (c1 == c2) );
198 #error "Please define string case-insensitive compare for your OS/compiler"
199 #endif // OS/compiler
202 #endif // WXWIN_COMPATIBILITY_2_8
204 // ----------------------------------------------------------------------------
206 // ----------------------------------------------------------------------------
208 // Lightweight object returned by wxString::c_str() and implicitly convertible
209 // to either const char* or const wchar_t*.
210 class WXDLLIMPEXP_BASE wxCStrData
213 // Ctors; for internal use by wxString and wxCStrData only
214 wxCStrData(const wxString
*str
, size_t offset
= 0, bool owned
= false)
215 : m_str(str
), m_offset(offset
), m_owned(owned
) {}
218 // Ctor constructs the object from char literal; they are needed to make
219 // operator?: compile and they intentionally take char*, not const char*
220 inline wxCStrData(char *buf
);
221 inline wxCStrData(wchar_t *buf
);
222 inline wxCStrData(const wxCStrData
& data
);
224 inline ~wxCStrData();
226 // methods defined inline below must be declared inline or mingw32 3.4.5
227 // warns about "<symbol> defined locally after being referenced with
228 // dllimport linkage"
229 #if wxUSE_UNICODE_WCHAR
232 const wchar_t* AsWChar() const;
233 operator const wchar_t*() const { return AsWChar(); }
235 #if !wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY
238 const char* AsChar() const;
239 const unsigned char* AsUnsignedChar() const
240 { return (const unsigned char *) AsChar(); }
241 operator const char*() const { return AsChar(); }
242 operator const unsigned char*() const { return AsUnsignedChar(); }
244 operator const void*() const { return AsChar(); }
246 inline const wxCharBuffer
AsCharBuf() const;
247 inline const wxWCharBuffer
AsWCharBuf() const;
249 inline wxString
AsString() const;
251 // returns the value as C string in internal representation (equivalent
252 // to AsString().wx_str(), but more efficient)
253 const wxStringCharType
*AsInternal() const;
255 // allow expressions like "c_str()[0]":
256 inline wxUniChar
operator[](size_t n
) const;
257 wxUniChar
operator[](int n
) const { return operator[](size_t(n
)); }
258 wxUniChar
operator[](long n
) const { return operator[](size_t(n
)); }
259 #ifndef wxSIZE_T_IS_UINT
260 wxUniChar
operator[](unsigned int n
) const { return operator[](size_t(n
)); }
261 #endif // size_t != unsigned int
263 // these operators are needed to emulate the pointer semantics of c_str():
264 // expressions like "wxChar *p = str.c_str() + 1;" should continue to work
265 // (we need both versions to resolve ambiguities):
266 wxCStrData
operator+(int n
) const
267 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
268 wxCStrData
operator+(long n
) const
269 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
270 wxCStrData
operator+(size_t n
) const
271 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
273 // and these for "str.c_str() + (p2 - p1)" (it also works for any integer
274 // expression but it must be ptrdiff_t and not e.g. int to work in this
276 wxCStrData
operator-(ptrdiff_t n
) const
278 wxASSERT_MSG( n
<= (ptrdiff_t)m_offset
,
279 _T("attempt to construct address before the beginning of the string") );
280 return wxCStrData(m_str
, m_offset
- n
, m_owned
);
283 // this operator is needed to make expressions like "*c_str()" or
284 // "*(c_str() + 2)" work
285 inline wxUniChar
operator*() const;
288 const wxString
*m_str
;
292 friend class WXDLLIMPEXP_FWD_BASE wxString
;
295 // ----------------------------------------------------------------------------
296 // wxStringPrintfMixin
297 // ---------------------------------------------------------------------------
299 // NB: VC6 has a bug that causes linker errors if you have template methods
300 // in a class using __declspec(dllimport). The solution is to split such
301 // class into two classes, one that contains the template methods and does
302 // *not* use WXDLLIMPEXP_BASE and another class that contains the rest
303 // (with DLL linkage).
305 // We only do this for VC6 here, because the code is less efficient
306 // (Printf() has to use dynamic_cast<>) and because OpenWatcom compiler
307 // cannot compile this code.
309 #if defined(__VISUALC__) && __VISUALC__ < 1300
310 #define wxNEEDS_WXSTRING_PRINTF_MIXIN
313 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
314 // this class contains implementation of wxString's vararg methods, it's
315 // exported from wxBase DLL
316 class WXDLLIMPEXP_BASE wxStringPrintfMixinBase
319 wxStringPrintfMixinBase() {}
321 #if !wxUSE_UTF8_LOCALE_ONLY
322 int DoPrintfWchar(const wxChar
*format
, ...);
323 static wxString
DoFormatWchar(const wxChar
*format
, ...);
325 #if wxUSE_UNICODE_UTF8
326 int DoPrintfUtf8(const char *format
, ...);
327 static wxString
DoFormatUtf8(const char *format
, ...);
331 // this class contains template wrappers for wxString's vararg methods, it's
332 // intentionally *not* exported from the DLL in order to fix the VC6 bug
334 class wxStringPrintfMixin
: public wxStringPrintfMixinBase
337 // to further complicate things, we can't return wxString from
338 // wxStringPrintfMixin::Format() because wxString is not yet declared at
339 // this point; the solution is to use this fake type trait template - this
340 // way the compiler won't know the return type until Format() is used
341 // (this doesn't compile with Watcom, but VC6 compiles it just fine):
342 template<typename T
> struct StringReturnType
344 typedef wxString type
;
348 // these are duplicated wxString methods, they're also declared below
349 // if !wxNEEDS_WXSTRING_PRINTF_MIXIN:
351 // static wxString Format(const wString& format, ...) ATTRIBUTE_PRINTF_1;
352 WX_DEFINE_VARARG_FUNC_SANS_N0(static typename StringReturnType
<T1
>::type
,
353 Format
, 1, (const wxFormatString
&),
354 DoFormatWchar
, DoFormatUtf8
)
355 // We have to implement the version without template arguments manually
356 // because of the StringReturnType<> hack, although WX_DEFINE_VARARG_FUNC
357 // normally does it itself. It has to be a template so that we can use
358 // the hack, even though there's no real template parameter. We can't move
359 // it to wxStrig, because it would shadow these versions of Format() then.
361 inline static typename StringReturnType
<T
>::type
364 // NB: this doesn't compile if T is not (some form of) a string;
365 // this makes Format's prototype equivalent to
366 // Format(const wxFormatString& fmt)
367 return DoFormatWchar(wxFormatString(fmt
));
370 // int Printf(const wxString& format, ...);
371 WX_DEFINE_VARARG_FUNC(int, Printf
, 1, (const wxFormatString
&),
372 DoPrintfWchar
, DoPrintfUtf8
)
373 // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2;
374 WX_DEFINE_VARARG_FUNC(int, sprintf
, 1, (const wxFormatString
&),
375 DoPrintfWchar
, DoPrintfUtf8
)
378 wxStringPrintfMixin() : wxStringPrintfMixinBase() {}
380 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
383 // ----------------------------------------------------------------------------
384 // wxString: string class trying to be compatible with std::string, MFC
385 // CString and wxWindows 1.x wxString all at once
386 // ---------------------------------------------------------------------------
388 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
389 // "non dll-interface class 'wxStringPrintfMixin' used as base interface
390 // for dll-interface class 'wxString'" -- this is OK in our case
391 #pragma warning (disable:4275)
394 #if wxUSE_UNICODE_UTF8
395 // see the comment near wxString::iterator for why we need this
396 class WXDLLIMPEXP_BASE wxStringIteratorNode
399 wxStringIteratorNode()
400 : m_str(NULL
), m_citer(NULL
), m_iter(NULL
), m_prev(NULL
), m_next(NULL
) {}
401 wxStringIteratorNode(const wxString
*str
,
402 wxStringImpl::const_iterator
*citer
)
403 { DoSet(str
, citer
, NULL
); }
404 wxStringIteratorNode(const wxString
*str
, wxStringImpl::iterator
*iter
)
405 { DoSet(str
, NULL
, iter
); }
406 ~wxStringIteratorNode()
409 inline void set(const wxString
*str
, wxStringImpl::const_iterator
*citer
)
410 { clear(); DoSet(str
, citer
, NULL
); }
411 inline void set(const wxString
*str
, wxStringImpl::iterator
*iter
)
412 { clear(); DoSet(str
, NULL
, iter
); }
414 const wxString
*m_str
;
415 wxStringImpl::const_iterator
*m_citer
;
416 wxStringImpl::iterator
*m_iter
;
417 wxStringIteratorNode
*m_prev
, *m_next
;
421 inline void DoSet(const wxString
*str
,
422 wxStringImpl::const_iterator
*citer
,
423 wxStringImpl::iterator
*iter
);
425 // the node belongs to a particular iterator instance, it's not copied
426 // when a copy of the iterator is made
427 DECLARE_NO_COPY_CLASS(wxStringIteratorNode
)
429 #endif // wxUSE_UNICODE_UTF8
431 class WXDLLIMPEXP_BASE wxString
432 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
433 : public wxStringPrintfMixin
436 // NB: special care was taken in arranging the member functions in such order
437 // that all inline functions can be effectively inlined, verify that all
438 // performance critical functions are still inlined if you change order!
440 // an 'invalid' value for string index, moved to this place due to a CW bug
441 static const size_t npos
;
444 // if we hadn't made these operators private, it would be possible to
445 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
446 // converted to char in C and we do have operator=(char)
448 // NB: we don't need other versions (short/long and unsigned) as attempt
449 // to assign another numeric type to wxString will now result in
450 // ambiguity between operator=(char) and operator=(int)
451 wxString
& operator=(int);
453 // these methods are not implemented - there is _no_ conversion from int to
454 // string, you're doing something wrong if the compiler wants to call it!
456 // try `s << i' or `s.Printf("%d", i)' instead
460 // buffer for holding temporary substring when using any of the methods
461 // that take (char*,size_t) or (wchar_t*,size_t) arguments:
463 struct SubstrBufFromType
468 SubstrBufFromType(const T
& data_
, size_t len_
)
469 : data(data_
), len(len_
)
471 wxASSERT_MSG( len
!= npos
, "must have real length" );
475 #if wxUSE_UNICODE_UTF8
476 // even char* -> char* needs conversion, from locale charset to UTF-8
477 typedef SubstrBufFromType
<wxCharBuffer
> SubstrBufFromWC
;
478 typedef SubstrBufFromType
<wxCharBuffer
> SubstrBufFromMB
;
479 #elif wxUSE_UNICODE_WCHAR
480 typedef SubstrBufFromType
<const wchar_t*> SubstrBufFromWC
;
481 typedef SubstrBufFromType
<wxWCharBuffer
> SubstrBufFromMB
;
483 typedef SubstrBufFromType
<const char*> SubstrBufFromMB
;
484 typedef SubstrBufFromType
<wxCharBuffer
> SubstrBufFromWC
;
488 // Functions implementing primitive operations on string data; wxString
489 // methods and iterators are implemented in terms of it. The differences
490 // between UTF-8 and wchar_t* representations of the string are mostly
493 #if wxUSE_UNICODE_UTF8
494 static SubstrBufFromMB
ConvertStr(const char *psz
, size_t nLength
,
495 const wxMBConv
& conv
);
496 static SubstrBufFromWC
ConvertStr(const wchar_t *pwz
, size_t nLength
,
497 const wxMBConv
& conv
);
498 #elif wxUSE_UNICODE_WCHAR
499 static SubstrBufFromMB
ConvertStr(const char *psz
, size_t nLength
,
500 const wxMBConv
& conv
);
502 static SubstrBufFromWC
ConvertStr(const wchar_t *pwz
, size_t nLength
,
503 const wxMBConv
& conv
);
506 #if !wxUSE_UNICODE_UTF8 // wxUSE_UNICODE_WCHAR or !wxUSE_UNICODE
507 // returns C string encoded as the implementation expects:
509 static const wchar_t* ImplStr(const wchar_t* str
)
510 { return str
? str
: wxT(""); }
511 static const SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
512 { return SubstrBufFromWC(str
, (str
&& n
== npos
) ? wxWcslen(str
) : n
); }
513 static wxWCharBuffer
ImplStr(const char* str
,
514 const wxMBConv
& conv
= wxConvLibc
)
515 { return ConvertStr(str
, npos
, conv
).data
; }
516 static SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
517 const wxMBConv
& conv
= wxConvLibc
)
518 { return ConvertStr(str
, n
, conv
); }
520 static const char* ImplStr(const char* str
,
521 const wxMBConv
& WXUNUSED(conv
) = wxConvLibc
)
522 { return str
? str
: ""; }
523 static const SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
524 const wxMBConv
& WXUNUSED(conv
) = wxConvLibc
)
525 { return SubstrBufFromMB(str
, (str
&& n
== npos
) ? wxStrlen(str
) : n
); }
526 static wxCharBuffer
ImplStr(const wchar_t* str
)
527 { return ConvertStr(str
, npos
, wxConvLibc
).data
; }
528 static SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
529 { return ConvertStr(str
, n
, wxConvLibc
); }
532 // translates position index in wxString to/from index in underlying
534 static size_t PosToImpl(size_t pos
) { return pos
; }
535 static void PosLenToImpl(size_t pos
, size_t len
,
536 size_t *implPos
, size_t *implLen
)
537 { *implPos
= pos
; *implLen
= len
; }
538 static size_t LenToImpl(size_t len
) { return len
; }
539 static size_t PosFromImpl(size_t pos
) { return pos
; }
541 // we don't want to define these as empty inline functions as it could
542 // result in noticeable (and quite unnecessary in non-UTF-8 build) slowdown
543 // in debug build where the inline functions are not effectively inlined
544 #define wxSTRING_INVALIDATE_CACHE()
545 #define wxSTRING_INVALIDATE_CACHED_LENGTH()
546 #define wxSTRING_UPDATE_CACHED_LENGTH(n)
547 #define wxSTRING_SET_CACHED_LENGTH(n)
549 #else // wxUSE_UNICODE_UTF8
551 static wxCharBuffer
ImplStr(const char* str
,
552 const wxMBConv
& conv
= wxConvLibc
)
553 { return ConvertStr(str
, npos
, conv
).data
; }
554 static SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
555 const wxMBConv
& conv
= wxConvLibc
)
556 { return ConvertStr(str
, n
, conv
); }
558 static wxCharBuffer
ImplStr(const wchar_t* str
)
559 { return ConvertStr(str
, npos
, wxMBConvUTF8()).data
; }
560 static SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
561 { return ConvertStr(str
, n
, wxMBConvUTF8()); }
563 #if wxUSE_STRING_POS_CACHE
564 // this is an extremely simple cache used by PosToImpl(): each cache element
565 // contains the string it applies to and the index corresponding to the last
566 // used position in this wxString in its m_impl string
568 // NB: notice that this struct (and nested Element one) must be a POD or we
569 // wouldn't be able to use a thread-local variable of this type, in
570 // particular it should have no ctor -- we rely on statics being
571 // initialized to 0 instead
578 const wxString
*str
; // the string to which this element applies
579 size_t pos
, // the cached index in this string
580 impl
, // the corresponding position in its m_impl
581 len
; // cached length or npos if unknown
583 // reset cached index to 0
584 void ResetPos() { pos
= impl
= 0; }
586 // reset position and length
587 void Reset() { ResetPos(); len
= npos
; }
590 // cache the indices mapping for the last few string used
591 Element cached
[SIZE
];
593 // the last used index
597 // notice that we must use an accessor function and not a static variable
598 // because when the TLS variables support is implemented in the library (and
599 // not by the compiler), the global s_cache variable could be not yet
600 // initialized when a ctor of another global object is executed and if that
601 // ctor uses any wxString methods, bad things happen
603 // also note that for the same reason this function _is_ MT-safe: we know
604 // it's going to be called during the program startup (currently during
605 // globals initialization but even if they ever stop using wxString, it would
606 // still be called by wxInitialize()), i.e. before any threads are created
607 static Cache
& GetCache()
609 static wxTLS_TYPE(Cache
) s_cache
;
611 return wxTLS_VALUE(s_cache
);
614 static Cache::Element
*GetCacheBegin() { return GetCache().cached
; }
615 static Cache::Element
*GetCacheEnd() { return GetCacheBegin() + Cache::SIZE
; }
616 static unsigned& LastUsedCacheElement() { return GetCache().lastUsed
; }
618 friend struct wxStrCacheDumper
;
620 // uncomment this to have access to some profiling statistics on program
622 //#define wxPROFILE_STRING_CACHE
624 #ifdef wxPROFILE_STRING_CACHE
625 static struct PosToImplCacheStats
627 unsigned postot
, // total non-trivial calls to PosToImpl
628 poshits
, // cache hits from PosToImpl()
629 mishits
, // cached position beyond the needed one
630 sumpos
, // sum of all positions, used to compute the
631 // average position after dividing by postot
632 sumofs
, // sum of all offsets after using the cache, used to
633 // compute the average after dividing by hits
634 lentot
, // number of total calls to length()
635 lenhits
; // number of cache hits in length()
638 friend struct ShowCacheStats
;
640 #define wxCACHE_PROFILE_FIELD_INC(field) ms_cacheStats.field++
641 #define wxCACHE_PROFILE_FIELD_ADD(field, val) ms_cacheStats.field += (val)
642 #else // !wxPROFILE_STRING_CACHE
643 #define wxCACHE_PROFILE_FIELD_INC(field)
644 #define wxCACHE_PROFILE_FIELD_ADD(field, val)
645 #endif // wxPROFILE_STRING_CACHE/!wxPROFILE_STRING_CACHE
647 // note: it could seem that the functions below shouldn't be inline because
648 // they are big, contain loops and so the compiler shouldn't be able to
649 // inline them anyhow, however moving them into string.cpp does decrease the
650 // code performance by ~5%, at least when using g++ 4.1 so do keep them here
651 // unless tests show that it's not advantageous any more
653 // return the pointer to the cache element for this string or NULL if not
655 Cache::Element
*FindCacheElement() const
657 // profiling seems to show a small but consistent gain if we use this
658 // simple loop instead of starting from the last used element (there are
659 // a lot of misses in this function...)
660 for ( Cache::Element
*c
= GetCacheBegin(); c
!= GetCacheEnd(); c
++ )
662 if ( c
->str
== this )
669 // unlike FindCacheElement(), this one always returns a valid pointer to the
670 // cache element for this string, it may have valid last cached position and
671 // its corresponding index in the byte string or not
672 Cache::Element
*GetCacheElement() const
674 Cache::Element
* const cacheBegin
= GetCacheBegin();
675 Cache::Element
* const cacheEnd
= GetCacheEnd();
676 Cache::Element
* const cacheStart
= cacheBegin
+ LastUsedCacheElement();
678 // check the last used first, this does no (measurable) harm for a miss
679 // but does help for simple loops addressing the same string all the time
680 if ( cacheStart
->str
== this )
683 // notice that we're going to check cacheStart again inside this call but
684 // profiling shows that it's still faster to use a simple loop like
685 // inside FindCacheElement() than manually looping with wrapping starting
686 // from the cache entry after the start one
687 Cache::Element
*c
= FindCacheElement();
690 // claim the next cache entry for this string
692 if ( ++c
== cacheEnd
)
698 // and remember the last used element
699 LastUsedCacheElement() = c
- cacheBegin
;
705 size_t DoPosToImpl(size_t pos
) const
707 wxCACHE_PROFILE_FIELD_INC(postot
);
709 // NB: although the case of pos == 1 (and offset from cached position
710 // equal to 1) are common, nothing is gained by writing special code
711 // for handling them, the compiler (at least g++ 4.1 used) seems to
712 // optimize the code well enough on its own
714 wxCACHE_PROFILE_FIELD_ADD(sumpos
, pos
);
716 Cache::Element
* const cache
= GetCacheElement();
718 // cached position can't be 0 so if it is, it means that this entry was
719 // used for length caching only so far, i.e. it doesn't count as a hit
720 // from our point of view
722 wxCACHE_PROFILE_FIELD_INC(poshits
);
724 if ( pos
== cache
->pos
)
727 // this seems to happen only rarely so just reset the cache in this case
728 // instead of complicating code even further by seeking backwards in this
730 if ( cache
->pos
> pos
)
732 wxCACHE_PROFILE_FIELD_INC(mishits
);
737 wxCACHE_PROFILE_FIELD_ADD(sumofs
, pos
- cache
->pos
);
740 wxStringImpl::const_iterator
i(m_impl
.begin() + cache
->impl
);
741 for ( size_t n
= cache
->pos
; n
< pos
; n
++ )
742 wxStringOperations::IncIter(i
);
745 cache
->impl
= i
- m_impl
.begin();
747 wxSTRING_CACHE_ASSERT(
748 (int)cache
->impl
== (begin() + pos
).impl() - m_impl
.begin() );
753 void InvalidateCache()
755 Cache::Element
* const cache
= FindCacheElement();
760 void InvalidateCachedLength()
762 Cache::Element
* const cache
= FindCacheElement();
767 void SetCachedLength(size_t len
)
769 // we optimistically cache the length here even if the string wasn't
770 // present in the cache before, this seems to do no harm and the
771 // potential for avoiding length recomputation for long strings looks
773 GetCacheElement()->len
= len
;
776 void UpdateCachedLength(ptrdiff_t delta
)
778 Cache::Element
* const cache
= FindCacheElement();
779 if ( cache
&& cache
->len
!= npos
)
781 wxSTRING_CACHE_ASSERT( (ptrdiff_t)cache
->len
+ delta
>= 0 );
787 #define wxSTRING_INVALIDATE_CACHE() InvalidateCache()
788 #define wxSTRING_INVALIDATE_CACHED_LENGTH() InvalidateCachedLength()
789 #define wxSTRING_UPDATE_CACHED_LENGTH(n) UpdateCachedLength(n)
790 #define wxSTRING_SET_CACHED_LENGTH(n) SetCachedLength(n)
791 #else // !wxUSE_STRING_POS_CACHE
792 size_t DoPosToImpl(size_t pos
) const
794 return (begin() + pos
).impl() - m_impl
.begin();
797 #define wxSTRING_INVALIDATE_CACHE()
798 #define wxSTRING_INVALIDATE_CACHED_LENGTH()
799 #define wxSTRING_UPDATE_CACHED_LENGTH(n)
800 #define wxSTRING_SET_CACHED_LENGTH(n)
801 #endif // wxUSE_STRING_POS_CACHE/!wxUSE_STRING_POS_CACHE
803 size_t PosToImpl(size_t pos
) const
805 return pos
== 0 || pos
== npos
? pos
: DoPosToImpl(pos
);
808 void PosLenToImpl(size_t pos
, size_t len
, size_t *implPos
, size_t *implLen
) const;
810 size_t LenToImpl(size_t len
) const
813 PosLenToImpl(0, len
, &pos
, &len2
);
817 size_t PosFromImpl(size_t pos
) const
819 if ( pos
== 0 || pos
== npos
)
822 return const_iterator(this, m_impl
.begin() + pos
) - begin();
824 #endif // !wxUSE_UNICODE_UTF8/wxUSE_UNICODE_UTF8
828 typedef wxUniChar value_type
;
829 typedef wxUniChar char_type
;
830 typedef wxUniCharRef reference
;
831 typedef wxChar
* pointer
;
832 typedef const wxChar
* const_pointer
;
834 typedef size_t size_type
;
835 typedef wxUniChar const_reference
;
838 #if wxUSE_UNICODE_UTF8
839 // random access is not O(1), as required by Random Access Iterator
840 #define WX_STR_ITERATOR_TAG std::bidirectional_iterator_tag
842 #define WX_STR_ITERATOR_TAG std::random_access_iterator_tag
844 #define WX_DEFINE_ITERATOR_CATEGORY(cat) typedef cat iterator_category;
846 // not defining iterator_category at all in this case is better than defining
847 // it as some dummy type -- at least it results in more intelligible error
849 #define WX_DEFINE_ITERATOR_CATEGORY(cat)
852 #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, reference_type) \
854 typedef wxStringImpl::iterator_name underlying_iterator; \
856 WX_DEFINE_ITERATOR_CATEGORY(WX_STR_ITERATOR_TAG) \
857 typedef wxUniChar value_type; \
858 typedef int difference_type; \
859 typedef reference_type reference; \
860 typedef pointer_type pointer; \
862 reference operator[](size_t n) const { return *(*this + n); } \
864 iterator_name& operator++() \
865 { wxStringOperations::IncIter(m_cur); return *this; } \
866 iterator_name& operator--() \
867 { wxStringOperations::DecIter(m_cur); return *this; } \
868 iterator_name operator++(int) \
870 iterator_name tmp = *this; \
871 wxStringOperations::IncIter(m_cur); \
874 iterator_name operator--(int) \
876 iterator_name tmp = *this; \
877 wxStringOperations::DecIter(m_cur); \
881 iterator_name& operator+=(ptrdiff_t n) \
883 m_cur = wxStringOperations::AddToIter(m_cur, n); \
886 iterator_name& operator-=(ptrdiff_t n) \
888 m_cur = wxStringOperations::AddToIter(m_cur, -n); \
892 difference_type operator-(const iterator_name& i) const \
893 { return wxStringOperations::DiffIters(m_cur, i.m_cur); } \
895 bool operator==(const iterator_name& i) const \
896 { return m_cur == i.m_cur; } \
897 bool operator!=(const iterator_name& i) const \
898 { return m_cur != i.m_cur; } \
900 bool operator<(const iterator_name& i) const \
901 { return m_cur < i.m_cur; } \
902 bool operator>(const iterator_name& i) const \
903 { return m_cur > i.m_cur; } \
904 bool operator<=(const iterator_name& i) const \
905 { return m_cur <= i.m_cur; } \
906 bool operator>=(const iterator_name& i) const \
907 { return m_cur >= i.m_cur; } \
910 /* for internal wxString use only: */ \
911 underlying_iterator impl() const { return m_cur; } \
913 friend class wxString; \
914 friend class wxCStrData; \
917 underlying_iterator m_cur
919 class WXDLLIMPEXP_FWD_BASE const_iterator
;
921 #if wxUSE_UNICODE_UTF8
922 // NB: In UTF-8 build, (non-const) iterator needs to keep reference
923 // to the underlying wxStringImpl, because UTF-8 is variable-length
924 // encoding and changing the value pointer to by an iterator (using
925 // its operator*) requires calling wxStringImpl::replace() if the old
926 // and new values differ in their encoding's length.
928 // Furthermore, the replace() call may invalid all iterators for the
929 // string, so we have to keep track of outstanding iterators and update
930 // them if replace() happens.
932 // This is implemented by maintaining linked list of iterators for every
933 // string and traversing it in wxUniCharRef::operator=(). Head of the
934 // list is stored in wxString. (FIXME-UTF8)
936 class WXDLLIMPEXP_BASE iterator
938 WX_STR_ITERATOR_IMPL(iterator
, wxChar
*, wxUniCharRef
);
942 iterator(const iterator
& i
)
943 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
944 iterator
& operator=(const iterator
& i
)
949 m_node
.set(i
.str(), &m_cur
);
954 reference
operator*()
955 { return wxUniCharRef::CreateForString(*str(), m_cur
); }
957 iterator
operator+(ptrdiff_t n
) const
958 { return iterator(str(), wxStringOperations::AddToIter(m_cur
, n
)); }
959 iterator
operator-(ptrdiff_t n
) const
960 { return iterator(str(), wxStringOperations::AddToIter(m_cur
, -n
)); }
963 iterator(wxString
*str
, underlying_iterator ptr
)
964 : m_cur(ptr
), m_node(str
, &m_cur
) {}
966 wxString
* str() const { return wx_const_cast(wxString
*, m_node
.m_str
); }
968 wxStringIteratorNode m_node
;
970 friend class const_iterator
;
973 class WXDLLIMPEXP_BASE const_iterator
975 // NB: reference_type is intentionally value, not reference, the character
976 // may be encoded differently in wxString data:
977 WX_STR_ITERATOR_IMPL(const_iterator
, const wxChar
*, wxUniChar
);
981 const_iterator(const const_iterator
& i
)
982 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
983 const_iterator(const iterator
& i
)
984 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
986 const_iterator
& operator=(const const_iterator
& i
)
991 m_node
.set(i
.str(), &m_cur
);
995 const_iterator
& operator=(const iterator
& i
)
996 { m_cur
= i
.m_cur
; m_node
.set(i
.str(), &m_cur
); return *this; }
998 reference
operator*() const
999 { return wxStringOperations::DecodeChar(m_cur
); }
1001 const_iterator
operator+(ptrdiff_t n
) const
1002 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur
, n
)); }
1003 const_iterator
operator-(ptrdiff_t n
) const
1004 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur
, -n
)); }
1007 // for internal wxString use only:
1008 const_iterator(const wxString
*str
, underlying_iterator ptr
)
1009 : m_cur(ptr
), m_node(str
, &m_cur
) {}
1011 const wxString
* str() const { return m_node
.m_str
; }
1013 wxStringIteratorNode m_node
;
1016 size_t IterToImplPos(wxString::iterator i
) const
1017 { return wxStringImpl::const_iterator(i
.impl()) - m_impl
.begin(); }
1019 iterator
GetIterForNthChar(size_t n
)
1020 { return iterator(this, m_impl
.begin() + PosToImpl(n
)); }
1021 const_iterator
GetIterForNthChar(size_t n
) const
1022 { return const_iterator(this, m_impl
.begin() + PosToImpl(n
)); }
1023 #else // !wxUSE_UNICODE_UTF8
1025 class WXDLLIMPEXP_BASE iterator
1027 WX_STR_ITERATOR_IMPL(iterator
, wxChar
*, wxUniCharRef
);
1031 iterator(const iterator
& i
) : m_cur(i
.m_cur
) {}
1033 reference
operator*()
1034 { return wxUniCharRef::CreateForString(m_cur
); }
1036 iterator
operator+(ptrdiff_t n
) const
1037 { return iterator(wxStringOperations::AddToIter(m_cur
, n
)); }
1038 iterator
operator-(ptrdiff_t n
) const
1039 { return iterator(wxStringOperations::AddToIter(m_cur
, -n
)); }
1042 // for internal wxString use only:
1043 iterator(underlying_iterator ptr
) : m_cur(ptr
) {}
1044 iterator(wxString
*WXUNUSED(str
), underlying_iterator ptr
) : m_cur(ptr
) {}
1046 friend class const_iterator
;
1049 class WXDLLIMPEXP_BASE const_iterator
1051 // NB: reference_type is intentionally value, not reference, the character
1052 // may be encoded differently in wxString data:
1053 WX_STR_ITERATOR_IMPL(const_iterator
, const wxChar
*, wxUniChar
);
1057 const_iterator(const const_iterator
& i
) : m_cur(i
.m_cur
) {}
1058 const_iterator(const iterator
& i
) : m_cur(i
.m_cur
) {}
1060 reference
operator*() const
1061 { return wxStringOperations::DecodeChar(m_cur
); }
1063 const_iterator
operator+(ptrdiff_t n
) const
1064 { return const_iterator(wxStringOperations::AddToIter(m_cur
, n
)); }
1065 const_iterator
operator-(ptrdiff_t n
) const
1066 { return const_iterator(wxStringOperations::AddToIter(m_cur
, -n
)); }
1069 // for internal wxString use only:
1070 const_iterator(underlying_iterator ptr
) : m_cur(ptr
) {}
1071 const_iterator(const wxString
*WXUNUSED(str
), underlying_iterator ptr
)
1075 iterator
GetIterForNthChar(size_t n
) { return begin() + n
; }
1076 const_iterator
GetIterForNthChar(size_t n
) const { return begin() + n
; }
1077 #endif // wxUSE_UNICODE_UTF8/!wxUSE_UNICODE_UTF8
1079 #undef WX_STR_ITERATOR_TAG
1080 #undef WX_STR_ITERATOR_IMPL
1082 friend class iterator
;
1083 friend class const_iterator
;
1085 template <typename T
>
1086 class reverse_iterator_impl
1089 typedef T iterator_type
;
1091 WX_DEFINE_ITERATOR_CATEGORY(typename
T::iterator_category
)
1092 typedef typename
T::value_type value_type
;
1093 typedef typename
T::difference_type difference_type
;
1094 typedef typename
T::reference reference
;
1095 typedef typename
T::pointer
*pointer
;
1097 reverse_iterator_impl() {}
1098 reverse_iterator_impl(iterator_type i
) : m_cur(i
) {}
1099 reverse_iterator_impl(const reverse_iterator_impl
& ri
)
1100 : m_cur(ri
.m_cur
) {}
1102 iterator_type
base() const { return m_cur
; }
1104 reference
operator*() const { return *(m_cur
-1); }
1105 reference
operator[](size_t n
) const { return *(*this + n
); }
1107 reverse_iterator_impl
& operator++()
1108 { --m_cur
; return *this; }
1109 reverse_iterator_impl
operator++(int)
1110 { reverse_iterator_impl tmp
= *this; --m_cur
; return tmp
; }
1111 reverse_iterator_impl
& operator--()
1112 { ++m_cur
; return *this; }
1113 reverse_iterator_impl
operator--(int)
1114 { reverse_iterator_impl tmp
= *this; ++m_cur
; return tmp
; }
1116 // NB: explicit <T> in the functions below is to keep BCC 5.5 happy
1117 reverse_iterator_impl
operator+(ptrdiff_t n
) const
1118 { return reverse_iterator_impl
<T
>(m_cur
- n
); }
1119 reverse_iterator_impl
operator-(ptrdiff_t n
) const
1120 { return reverse_iterator_impl
<T
>(m_cur
+ n
); }
1121 reverse_iterator_impl
operator+=(ptrdiff_t n
)
1122 { m_cur
-= n
; return *this; }
1123 reverse_iterator_impl
operator-=(ptrdiff_t n
)
1124 { m_cur
+= n
; return *this; }
1126 unsigned operator-(const reverse_iterator_impl
& i
) const
1127 { return i
.m_cur
- m_cur
; }
1129 bool operator==(const reverse_iterator_impl
& ri
) const
1130 { return m_cur
== ri
.m_cur
; }
1131 bool operator!=(const reverse_iterator_impl
& ri
) const
1132 { return !(*this == ri
); }
1134 bool operator<(const reverse_iterator_impl
& i
) const
1135 { return m_cur
> i
.m_cur
; }
1136 bool operator>(const reverse_iterator_impl
& i
) const
1137 { return m_cur
< i
.m_cur
; }
1138 bool operator<=(const reverse_iterator_impl
& i
) const
1139 { return m_cur
>= i
.m_cur
; }
1140 bool operator>=(const reverse_iterator_impl
& i
) const
1141 { return m_cur
<= i
.m_cur
; }
1144 iterator_type m_cur
;
1147 typedef reverse_iterator_impl
<iterator
> reverse_iterator
;
1148 typedef reverse_iterator_impl
<const_iterator
> const_reverse_iterator
;
1151 // used to transform an expression built using c_str() (and hence of type
1152 // wxCStrData) to an iterator into the string
1153 static const_iterator
CreateConstIterator(const wxCStrData
& data
)
1155 return const_iterator(data
.m_str
,
1156 (data
.m_str
->begin() + data
.m_offset
).impl());
1159 // in UTF-8 STL build, creation from std::string requires conversion under
1160 // non-UTF8 locales, so we can't have and use wxString(wxStringImpl) ctor;
1161 // instead we define dummy type that lets us have wxString ctor for creation
1162 // from wxStringImpl that couldn't be used by user code (in all other builds,
1163 // "standard" ctors can be used):
1164 #if wxUSE_UNICODE_UTF8 && wxUSE_STL_BASED_WXSTRING
1165 struct CtorFromStringImplTag
{};
1167 wxString(CtorFromStringImplTag
* WXUNUSED(dummy
), const wxStringImpl
& src
)
1170 static wxString
FromImpl(const wxStringImpl
& src
)
1171 { return wxString((CtorFromStringImplTag
*)NULL
, src
); }
1173 #if !wxUSE_STL_BASED_WXSTRING
1174 wxString(const wxStringImpl
& src
) : m_impl(src
) { }
1175 // else: already defined as wxString(wxStdString) below
1177 static wxString
FromImpl(const wxStringImpl
& src
) { return wxString(src
); }
1181 // constructors and destructor
1182 // ctor for an empty string
1186 wxString(const wxString
& stringSrc
) : m_impl(stringSrc
.m_impl
) { }
1188 // string containing nRepeat copies of ch
1189 wxString(wxUniChar ch
, size_t nRepeat
= 1 )
1190 { assign(nRepeat
, ch
); }
1191 wxString(size_t nRepeat
, wxUniChar ch
)
1192 { assign(nRepeat
, ch
); }
1193 wxString(wxUniCharRef ch
, size_t nRepeat
= 1)
1194 { assign(nRepeat
, ch
); }
1195 wxString(size_t nRepeat
, wxUniCharRef ch
)
1196 { assign(nRepeat
, ch
); }
1197 wxString(char ch
, size_t nRepeat
= 1)
1198 { assign(nRepeat
, ch
); }
1199 wxString(size_t nRepeat
, char ch
)
1200 { assign(nRepeat
, ch
); }
1201 wxString(wchar_t ch
, size_t nRepeat
= 1)
1202 { assign(nRepeat
, ch
); }
1203 wxString(size_t nRepeat
, wchar_t ch
)
1204 { assign(nRepeat
, ch
); }
1206 // ctors from char* strings:
1207 wxString(const char *psz
)
1208 : m_impl(ImplStr(psz
)) {}
1209 wxString(const char *psz
, const wxMBConv
& conv
)
1210 : m_impl(ImplStr(psz
, conv
)) {}
1211 wxString(const char *psz
, size_t nLength
)
1212 { assign(psz
, nLength
); }
1213 wxString(const char *psz
, const wxMBConv
& conv
, size_t nLength
)
1215 SubstrBufFromMB
str(ImplStr(psz
, nLength
, conv
));
1216 m_impl
.assign(str
.data
, str
.len
);
1219 // and unsigned char*:
1220 wxString(const unsigned char *psz
)
1221 : m_impl(ImplStr((const char*)psz
)) {}
1222 wxString(const unsigned char *psz
, const wxMBConv
& conv
)
1223 : m_impl(ImplStr((const char*)psz
, conv
)) {}
1224 wxString(const unsigned char *psz
, size_t nLength
)
1225 { assign((const char*)psz
, nLength
); }
1226 wxString(const unsigned char *psz
, const wxMBConv
& conv
, size_t nLength
)
1228 SubstrBufFromMB
str(ImplStr((const char*)psz
, nLength
, conv
));
1229 m_impl
.assign(str
.data
, str
.len
);
1232 // ctors from wchar_t* strings:
1233 wxString(const wchar_t *pwz
)
1234 : m_impl(ImplStr(pwz
)) {}
1235 wxString(const wchar_t *pwz
, const wxMBConv
& WXUNUSED(conv
))
1236 : m_impl(ImplStr(pwz
)) {}
1237 wxString(const wchar_t *pwz
, size_t nLength
)
1238 { assign(pwz
, nLength
); }
1239 wxString(const wchar_t *pwz
, const wxMBConv
& WXUNUSED(conv
), size_t nLength
)
1240 { assign(pwz
, nLength
); }
1242 wxString(const wxCharBuffer
& buf
)
1243 { assign(buf
.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
1244 wxString(const wxWCharBuffer
& buf
)
1245 { assign(buf
.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
1247 // NB: this version uses m_impl.c_str() to force making a copy of the
1248 // string, so that "wxString(str.c_str())" idiom for passing strings
1249 // between threads works
1250 wxString(const wxCStrData
& cstr
)
1251 : m_impl(cstr
.AsString().m_impl
.c_str()) { }
1253 // as we provide both ctors with this signature for both char and unsigned
1254 // char string, we need to provide one for wxCStrData to resolve ambiguity
1255 wxString(const wxCStrData
& cstr
, size_t nLength
)
1256 : m_impl(cstr
.AsString().Mid(0, nLength
).m_impl
) {}
1258 // and because wxString is convertible to wxCStrData and const wxChar *
1259 // we also need to provide this one
1260 wxString(const wxString
& str
, size_t nLength
)
1261 { assign(str
, nLength
); }
1264 #if wxUSE_STRING_POS_CACHE
1267 // we need to invalidate our cache entry as another string could be
1268 // recreated at the same address (unlikely, but still possible, with the
1269 // heap-allocated strings but perfectly common with stack-allocated ones)
1272 #endif // wxUSE_STRING_POS_CACHE
1274 // even if we're not built with wxUSE_STL == 1 it is very convenient to allow
1275 // implicit conversions from std::string to wxString and vice verse as this
1276 // allows to use the same strings in non-GUI and GUI code, however we don't
1277 // want to unconditionally add this ctor as it would make wx lib dependent on
1278 // libstdc++ on some Linux versions which is bad, so instead we ask the
1279 // client code to define this wxUSE_STD_STRING symbol if they need it
1280 #if wxUSE_STD_STRING
1281 #if wxUSE_UNICODE_WCHAR
1282 wxString(const wxStdWideString
& str
) : m_impl(str
) {}
1283 #else // UTF-8 or ANSI
1284 wxString(const wxStdWideString
& str
)
1285 { assign(str
.c_str(), str
.length()); }
1288 #if !wxUSE_UNICODE // ANSI build
1289 // FIXME-UTF8: do this in UTF8 build #if wxUSE_UTF8_LOCALE_ONLY, too
1290 wxString(const std::string
& str
) : m_impl(str
) {}
1292 wxString(const std::string
& str
)
1293 { assign(str
.c_str(), str
.length()); }
1295 #endif // wxUSE_STD_STRING
1297 // Unlike ctor from std::string, we provide conversion to std::string only
1298 // if wxUSE_STL and not merely wxUSE_STD_STRING (which is on by default),
1299 // because it conflicts with operator const char/wchar_t*:
1301 #if wxUSE_UNICODE_WCHAR && wxUSE_STL_BASED_WXSTRING
1302 // wxStringImpl is std::string in the encoding we want
1303 operator const wxStdWideString
&() const { return m_impl
; }
1305 // wxStringImpl is either not std::string or needs conversion
1306 operator wxStdWideString() const
1307 // FIXME-UTF8: broken for embedded NULs
1308 { return wxStdWideString(wc_str()); }
1311 #if (!wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY) && wxUSE_STL_BASED_WXSTRING
1312 // wxStringImpl is std::string in the encoding we want
1313 operator const std::string
&() const { return m_impl
; }
1315 // wxStringImpl is either not std::string or needs conversion
1316 operator std::string() const
1317 // FIXME-UTF8: broken for embedded NULs
1318 { return std::string(mb_str()); }
1322 wxString
Clone() const
1324 // make a deep copy of the string, i.e. the returned string will have
1325 // ref count = 1 with refcounted implementation
1326 return wxString::FromImpl(wxStringImpl(m_impl
.c_str(), m_impl
.length()));
1329 // first valid index position
1330 const_iterator
begin() const { return const_iterator(this, m_impl
.begin()); }
1331 iterator
begin() { return iterator(this, m_impl
.begin()); }
1332 // position one after the last valid one
1333 const_iterator
end() const { return const_iterator(this, m_impl
.end()); }
1334 iterator
end() { return iterator(this, m_impl
.end()); }
1336 // first element of the reversed string
1337 const_reverse_iterator
rbegin() const
1338 { return const_reverse_iterator(end()); }
1339 reverse_iterator
rbegin()
1340 { return reverse_iterator(end()); }
1341 // one beyond the end of the reversed string
1342 const_reverse_iterator
rend() const
1343 { return const_reverse_iterator(begin()); }
1344 reverse_iterator
rend()
1345 { return reverse_iterator(begin()); }
1347 // std::string methods:
1348 #if wxUSE_UNICODE_UTF8
1349 size_t length() const
1351 #if wxUSE_STRING_POS_CACHE
1352 wxCACHE_PROFILE_FIELD_INC(lentot
);
1354 Cache::Element
* const cache
= GetCacheElement();
1356 if ( cache
->len
== npos
)
1358 // it's probably not worth trying to be clever and using cache->pos
1359 // here as it's probably 0 anyhow -- you usually call length() before
1360 // starting to index the string
1361 cache
->len
= end() - begin();
1365 wxCACHE_PROFILE_FIELD_INC(lenhits
);
1367 wxSTRING_CACHE_ASSERT( (int)cache
->len
== end() - begin() );
1371 #else // !wxUSE_STRING_POS_CACHE
1372 return end() - begin();
1373 #endif // wxUSE_STRING_POS_CACHE/!wxUSE_STRING_POS_CACHE
1376 size_t length() const { return m_impl
.length(); }
1379 size_type
size() const { return length(); }
1380 size_type
max_size() const { return npos
; }
1382 bool empty() const { return m_impl
.empty(); }
1384 // NB: these methods don't have a well-defined meaning in UTF-8 case
1385 size_type
capacity() const { return m_impl
.capacity(); }
1386 void reserve(size_t sz
) { m_impl
.reserve(sz
); }
1388 void resize(size_t nSize
, wxUniChar ch
= wxT('\0'))
1390 const size_t len
= length();
1394 #if wxUSE_UNICODE_UTF8
1397 wxSTRING_INVALIDATE_CACHE();
1399 // we can't use wxStringImpl::resize() for truncating the string as it
1400 // counts in bytes, not characters
1405 // we also can't use (presumably more efficient) resize() if we have to
1406 // append characters taking more than one byte
1407 if ( !ch
.IsAscii() )
1409 append(nSize
- len
, ch
);
1411 else // can use (presumably faster) resize() version
1412 #endif // wxUSE_UNICODE_UTF8
1414 wxSTRING_INVALIDATE_CACHED_LENGTH();
1416 m_impl
.resize(nSize
, (wxStringCharType
)ch
);
1420 wxString
substr(size_t nStart
= 0, size_t nLen
= npos
) const
1423 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
1424 return FromImpl(m_impl
.substr(pos
, len
));
1427 // generic attributes & operations
1428 // as standard strlen()
1429 size_t Len() const { return length(); }
1430 // string contains any characters?
1431 bool IsEmpty() const { return empty(); }
1432 // empty string is "false", so !str will return true
1433 bool operator!() const { return empty(); }
1434 // truncate the string to given length
1435 wxString
& Truncate(size_t uiLen
);
1436 // empty string contents
1441 wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") );
1443 // empty the string and free memory
1444 void Clear() { clear(); }
1447 // Is an ascii value
1448 bool IsAscii() const;
1450 bool IsNumber() const;
1452 bool IsWord() const;
1454 // data access (all indexes are 0 based)
1456 wxUniChar
at(size_t n
) const
1457 { return wxStringOperations::DecodeChar(m_impl
.begin() + PosToImpl(n
)); }
1458 wxUniChar
GetChar(size_t n
) const
1460 // read/write access
1461 wxUniCharRef
at(size_t n
)
1462 { return *GetIterForNthChar(n
); }
1463 wxUniCharRef
GetWritableChar(size_t n
)
1466 void SetChar(size_t n
, wxUniChar ch
)
1469 // get last character
1470 wxUniChar
Last() const
1472 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1476 // get writable last character
1479 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1484 Note that we we must define all of the overloads below to avoid
1485 ambiguity when using str[0].
1487 wxUniChar
operator[](int n
) const
1489 wxUniChar
operator[](long n
) const
1491 wxUniChar
operator[](size_t n
) const
1493 #ifndef wxSIZE_T_IS_UINT
1494 wxUniChar
operator[](unsigned int n
) const
1496 #endif // size_t != unsigned int
1498 // operator versions of GetWriteableChar()
1499 wxUniCharRef
operator[](int n
)
1501 wxUniCharRef
operator[](long n
)
1503 wxUniCharRef
operator[](size_t n
)
1505 #ifndef wxSIZE_T_IS_UINT
1506 wxUniCharRef
operator[](unsigned int n
)
1508 #endif // size_t != unsigned int
1510 // explicit conversion to C string (use this with printf()!)
1511 wxCStrData
c_str() const { return wxCStrData(this); }
1512 wxCStrData
data() const { return c_str(); }
1514 // implicit conversion to C string
1515 operator wxCStrData() const { return c_str(); }
1517 // the first two operators conflict with operators for conversion to
1518 // std::string and they must be disabled in STL build; the next one only
1519 // makes sense if conversions to char* are also defined and not defining it
1520 // in STL build also helps us to get more clear error messages for the code
1521 // which relies on implicit conversion to char* in STL build
1523 operator const char*() const { return c_str(); }
1524 operator const wchar_t*() const { return c_str(); }
1526 // implicit conversion to untyped pointer for compatibility with previous
1527 // wxWidgets versions: this is the same as conversion to const char * so it
1529 operator const void*() const { return c_str(); }
1532 // identical to c_str(), for MFC compatibility
1533 const wxCStrData
GetData() const { return c_str(); }
1535 // explicit conversion to C string in internal representation (char*,
1536 // wchar_t*, UTF-8-encoded char*, depending on the build):
1537 const wxStringCharType
*wx_str() const { return m_impl
.c_str(); }
1539 // conversion to *non-const* multibyte or widestring buffer; modifying
1540 // returned buffer won't affect the string, these methods are only useful
1541 // for passing values to const-incorrect functions
1542 wxWritableCharBuffer
char_str(const wxMBConv
& conv
= wxConvLibc
) const
1543 { return mb_str(conv
); }
1544 wxWritableWCharBuffer
wchar_str() const { return wc_str(); }
1546 // conversion to the buffer of the given type T (= char or wchar_t) and
1547 // also optionally return the buffer length
1549 // this is mostly/only useful for the template functions
1551 // FIXME-VC6: the second argument only exists for VC6 which doesn't support
1552 // explicit template function selection, do not use it unless
1553 // you must support VC6!
1554 template <typename T
>
1555 wxCharTypeBuffer
<T
> tchar_str(size_t *len
= NULL
,
1556 T
* WXUNUSED(dummy
) = NULL
) const
1559 // we need a helper dispatcher depending on type
1560 return wxPrivate::wxStringAsBufHelper
<T
>::Get(*this, len
);
1562 // T can only be char in ANSI build
1566 return wxCharTypeBuffer
<T
>::CreateNonOwned(wx_str());
1567 #endif // Unicode build kind
1570 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
1571 // converting numbers or strings which are certain not to contain special
1572 // chars (typically system functions, X atoms, environment variables etc.)
1574 // the behaviour of these functions with the strings containing anything
1575 // else than 7 bit ASCII characters is undefined, use at your own risk.
1577 static wxString
FromAscii(const char *ascii
, size_t len
);
1578 static wxString
FromAscii(const char *ascii
);
1579 static wxString
FromAscii(char ascii
);
1580 const wxCharBuffer
ToAscii() const;
1582 static wxString
FromAscii(const char *ascii
) { return wxString( ascii
); }
1583 static wxString
FromAscii(const char *ascii
, size_t len
)
1584 { return wxString( ascii
, len
); }
1585 static wxString
FromAscii(char ascii
) { return wxString( ascii
); }
1586 const char *ToAscii() const { return c_str(); }
1587 #endif // Unicode/!Unicode
1589 // also provide unsigned char overloads as signed/unsigned doesn't matter
1590 // for 7 bit ASCII characters
1591 static wxString
FromAscii(const unsigned char *ascii
)
1592 { return FromAscii((const char *)ascii
); }
1593 static wxString
FromAscii(const unsigned char *ascii
, size_t len
)
1594 { return FromAscii((const char *)ascii
, len
); }
1596 // conversion to/from UTF-8:
1597 #if wxUSE_UNICODE_UTF8
1598 static wxString
FromUTF8Unchecked(const char *utf8
)
1601 return wxEmptyString
;
1603 wxASSERT( wxStringOperations::IsValidUtf8String(utf8
) );
1604 return FromImpl(wxStringImpl(utf8
));
1606 static wxString
FromUTF8Unchecked(const char *utf8
, size_t len
)
1609 return wxEmptyString
;
1611 return FromUTF8Unchecked(utf8
);
1613 wxASSERT( wxStringOperations::IsValidUtf8String(utf8
, len
) );
1614 return FromImpl(wxStringImpl(utf8
, len
));
1617 static wxString
FromUTF8(const char *utf8
)
1619 if ( !utf8
|| !wxStringOperations::IsValidUtf8String(utf8
) )
1622 return FromImpl(wxStringImpl(utf8
));
1624 static wxString
FromUTF8(const char *utf8
, size_t len
)
1627 return FromUTF8(utf8
);
1629 if ( !utf8
|| !wxStringOperations::IsValidUtf8String(utf8
, len
) )
1632 return FromImpl(wxStringImpl(utf8
, len
));
1635 const char* utf8_str() const { return wx_str(); }
1636 const char* ToUTF8() const { return wx_str(); }
1638 // this function exists in UTF-8 build only and returns the length of the
1639 // internal UTF-8 representation
1640 size_t utf8_length() const { return m_impl
.length(); }
1641 #elif wxUSE_UNICODE_WCHAR
1642 static wxString
FromUTF8(const char *utf8
, size_t len
= npos
)
1643 { return wxString(utf8
, wxMBConvUTF8(), len
); }
1644 static wxString
FromUTF8Unchecked(const char *utf8
, size_t len
= npos
)
1646 const wxString
s(utf8
, wxMBConvUTF8(), len
);
1647 wxASSERT_MSG( !utf8
|| !*utf8
|| !s
.empty(),
1648 "string must be valid UTF-8" );
1651 const wxCharBuffer
utf8_str() const { return mb_str(wxMBConvUTF8()); }
1652 const wxCharBuffer
ToUTF8() const { return utf8_str(); }
1654 static wxString
FromUTF8(const char *utf8
)
1655 { return wxString(wxMBConvUTF8().cMB2WC(utf8
)); }
1656 static wxString
FromUTF8(const char *utf8
, size_t len
)
1659 wxWCharBuffer
buf(wxMBConvUTF8().cMB2WC(utf8
, len
== npos
? wxNO_LEN
: len
, &wlen
));
1660 return wxString(buf
.data(), wlen
);
1662 static wxString
FromUTF8Unchecked(const char *utf8
, size_t len
= npos
)
1665 wxWCharBuffer
buf(wxMBConvUTF8().cMB2WC(utf8
,
1666 len
== npos
? wxNO_LEN
: len
,
1668 wxASSERT_MSG( !utf8
|| !*utf8
|| wlen
,
1669 "string must be valid UTF-8" );
1671 return wxString(buf
.data(), wlen
);
1673 const wxCharBuffer
utf8_str() const
1674 { return wxMBConvUTF8().cWC2MB(wc_str()); }
1675 const wxCharBuffer
ToUTF8() const { return utf8_str(); }
1678 // functions for storing binary data in wxString:
1680 static wxString
From8BitData(const char *data
, size_t len
)
1681 { return wxString(data
, wxConvISO8859_1
, len
); }
1682 // version for NUL-terminated data:
1683 static wxString
From8BitData(const char *data
)
1684 { return wxString(data
, wxConvISO8859_1
); }
1685 const wxCharBuffer
To8BitData() const { return mb_str(wxConvISO8859_1
); }
1687 static wxString
From8BitData(const char *data
, size_t len
)
1688 { return wxString(data
, len
); }
1689 // version for NUL-terminated data:
1690 static wxString
From8BitData(const char *data
)
1691 { return wxString(data
); }
1692 const char *To8BitData() const { return c_str(); }
1693 #endif // Unicode/ANSI
1695 // conversions with (possible) format conversions: have to return a
1696 // buffer with temporary data
1698 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
1699 // return an ANSI (multibyte) string, wc_str() to return a wide string and
1700 // fn_str() to return a string which should be used with the OS APIs
1701 // accepting the file names. The return value is always the same, but the
1702 // type differs because a function may either return pointer to the buffer
1703 // directly or have to use intermediate buffer for translation.
1706 #if wxUSE_UTF8_LOCALE_ONLY
1707 const char* mb_str() const { return wx_str(); }
1708 const wxCharBuffer
mb_str(const wxMBConv
& conv
) const;
1710 const wxCharBuffer
mb_str(const wxMBConv
& conv
= wxConvLibc
) const;
1713 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
1715 #if wxUSE_UNICODE_WCHAR
1716 const wchar_t* wc_str() const { return wx_str(); }
1717 #elif wxUSE_UNICODE_UTF8
1718 const wxWCharBuffer
wc_str() const;
1720 // for compatibility with !wxUSE_UNICODE version
1721 const wxWX2WCbuf
wc_str(const wxMBConv
& WXUNUSED(conv
)) const
1722 { return wc_str(); }
1725 const wxCharBuffer
fn_str() const { return mb_str(wxConvFile
); }
1727 const wxWX2WCbuf
fn_str() const { return wc_str(); }
1728 #endif // wxMBFILES/!wxMBFILES
1731 const wxChar
* mb_str() const { return wx_str(); }
1733 // for compatibility with wxUSE_UNICODE version
1734 const char* mb_str(const wxMBConv
& WXUNUSED(conv
)) const { return wx_str(); }
1736 const wxWX2MBbuf
mbc_str() const { return mb_str(); }
1739 const wxWCharBuffer
wc_str(const wxMBConv
& conv
= wxConvLibc
) const;
1740 #endif // wxUSE_WCHAR_T
1741 const wxCharBuffer
fn_str() const { return wxConvFile
.cWC2WX( wc_str( wxConvLibc
) ); }
1742 #endif // Unicode/ANSI
1744 #if wxUSE_UNICODE_UTF8
1745 const wxWCharBuffer
t_str() const { return wc_str(); }
1746 #elif wxUSE_UNICODE_WCHAR
1747 const wchar_t* t_str() const { return wx_str(); }
1749 const char* t_str() const { return wx_str(); }
1753 // overloaded assignment
1754 // from another wxString
1755 wxString
& operator=(const wxString
& stringSrc
)
1757 if ( this != &stringSrc
)
1759 wxSTRING_INVALIDATE_CACHE();
1761 m_impl
= stringSrc
.m_impl
;
1767 wxString
& operator=(const wxCStrData
& cstr
)
1768 { return *this = cstr
.AsString(); }
1770 wxString
& operator=(wxUniChar ch
)
1772 wxSTRING_INVALIDATE_CACHE();
1774 #if wxUSE_UNICODE_UTF8
1775 if ( !ch
.IsAscii() )
1776 m_impl
= wxStringOperations::EncodeChar(ch
);
1778 #endif // wxUSE_UNICODE_UTF8
1779 m_impl
= (wxStringCharType
)ch
;
1783 wxString
& operator=(wxUniCharRef ch
)
1784 { return operator=((wxUniChar
)ch
); }
1785 wxString
& operator=(char ch
)
1786 { return operator=(wxUniChar(ch
)); }
1787 wxString
& operator=(unsigned char ch
)
1788 { return operator=(wxUniChar(ch
)); }
1789 wxString
& operator=(wchar_t ch
)
1790 { return operator=(wxUniChar(ch
)); }
1791 // from a C string - STL probably will crash on NULL,
1792 // so we need to compensate in that case
1793 #if wxUSE_STL_BASED_WXSTRING
1794 wxString
& operator=(const char *psz
)
1796 wxSTRING_INVALIDATE_CACHE();
1799 m_impl
= ImplStr(psz
);
1806 wxString
& operator=(const wchar_t *pwz
)
1808 wxSTRING_INVALIDATE_CACHE();
1811 m_impl
= ImplStr(pwz
);
1817 #else // !wxUSE_STL_BASED_WXSTRING
1818 wxString
& operator=(const char *psz
)
1820 wxSTRING_INVALIDATE_CACHE();
1822 m_impl
= ImplStr(psz
);
1827 wxString
& operator=(const wchar_t *pwz
)
1829 wxSTRING_INVALIDATE_CACHE();
1831 m_impl
= ImplStr(pwz
);
1835 #endif // wxUSE_STL_BASED_WXSTRING/!wxUSE_STL_BASED_WXSTRING
1837 wxString
& operator=(const unsigned char *psz
)
1838 { return operator=((const char*)psz
); }
1840 // from wxWCharBuffer
1841 wxString
& operator=(const wxWCharBuffer
& s
)
1842 { return operator=(s
.data()); } // FIXME-UTF8: fix for embedded NULs
1843 // from wxCharBuffer
1844 wxString
& operator=(const wxCharBuffer
& s
)
1845 { return operator=(s
.data()); } // FIXME-UTF8: fix for embedded NULs
1847 // string concatenation
1848 // in place concatenation
1850 Concatenate and return the result. Note that the left to right
1851 associativity of << allows to write things like "str << str1 << str2
1852 << ..." (unlike with +=)
1855 wxString
& operator<<(const wxString
& s
)
1857 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1858 wxASSERT_MSG( s
.IsValid(),
1859 _T("did you forget to call UngetWriteBuf()?") );
1865 // string += C string
1866 wxString
& operator<<(const char *psz
)
1867 { append(psz
); return *this; }
1868 wxString
& operator<<(const wchar_t *pwz
)
1869 { append(pwz
); return *this; }
1870 wxString
& operator<<(const wxCStrData
& psz
)
1871 { append(psz
.AsString()); return *this; }
1873 wxString
& operator<<(wxUniChar ch
) { append(1, ch
); return *this; }
1874 wxString
& operator<<(wxUniCharRef ch
) { append(1, ch
); return *this; }
1875 wxString
& operator<<(char ch
) { append(1, ch
); return *this; }
1876 wxString
& operator<<(unsigned char ch
) { append(1, ch
); return *this; }
1877 wxString
& operator<<(wchar_t ch
) { append(1, ch
); return *this; }
1879 // string += buffer (i.e. from wxGetString)
1880 wxString
& operator<<(const wxWCharBuffer
& s
)
1881 { return operator<<((const wchar_t *)s
); }
1882 wxString
& operator<<(const wxCharBuffer
& s
)
1883 { return operator<<((const char *)s
); }
1885 // string += C string
1886 wxString
& Append(const wxString
& s
)
1888 // test for empty() to share the string if possible
1895 wxString
& Append(const char* psz
)
1896 { append(psz
); return *this; }
1897 wxString
& Append(const wchar_t* pwz
)
1898 { append(pwz
); return *this; }
1899 wxString
& Append(const wxCStrData
& psz
)
1900 { append(psz
); return *this; }
1901 wxString
& Append(const wxCharBuffer
& psz
)
1902 { append(psz
); return *this; }
1903 wxString
& Append(const wxWCharBuffer
& psz
)
1904 { append(psz
); return *this; }
1905 wxString
& Append(const char* psz
, size_t nLen
)
1906 { append(psz
, nLen
); return *this; }
1907 wxString
& Append(const wchar_t* pwz
, size_t nLen
)
1908 { append(pwz
, nLen
); return *this; }
1909 wxString
& Append(const wxCStrData
& psz
, size_t nLen
)
1910 { append(psz
, nLen
); return *this; }
1911 wxString
& Append(const wxCharBuffer
& psz
, size_t nLen
)
1912 { append(psz
, nLen
); return *this; }
1913 wxString
& Append(const wxWCharBuffer
& psz
, size_t nLen
)
1914 { append(psz
, nLen
); return *this; }
1915 // append count copies of given character
1916 wxString
& Append(wxUniChar ch
, size_t count
= 1u)
1917 { append(count
, ch
); return *this; }
1918 wxString
& Append(wxUniCharRef ch
, size_t count
= 1u)
1919 { append(count
, ch
); return *this; }
1920 wxString
& Append(char ch
, size_t count
= 1u)
1921 { append(count
, ch
); return *this; }
1922 wxString
& Append(unsigned char ch
, size_t count
= 1u)
1923 { append(count
, ch
); return *this; }
1924 wxString
& Append(wchar_t ch
, size_t count
= 1u)
1925 { append(count
, ch
); return *this; }
1927 // prepend a string, return the string itself
1928 wxString
& Prepend(const wxString
& str
)
1929 { *this = str
+ *this; return *this; }
1931 // non-destructive concatenation
1933 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
,
1934 const wxString
& string2
);
1935 // string with a single char
1936 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
1937 // char with a string
1938 friend wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
1939 // string with C string
1940 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
1942 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
1943 const wchar_t *pwz
);
1944 // C string with string
1945 friend wxString WXDLLIMPEXP_BASE
operator+(const char *psz
,
1946 const wxString
& string
);
1947 friend wxString WXDLLIMPEXP_BASE
operator+(const wchar_t *pwz
,
1948 const wxString
& string
);
1950 // stream-like functions
1951 // insert an int into string
1952 wxString
& operator<<(int i
)
1953 { return (*this) << Format(_T("%d"), i
); }
1954 // insert an unsigned int into string
1955 wxString
& operator<<(unsigned int ui
)
1956 { return (*this) << Format(_T("%u"), ui
); }
1957 // insert a long into string
1958 wxString
& operator<<(long l
)
1959 { return (*this) << Format(_T("%ld"), l
); }
1960 // insert an unsigned long into string
1961 wxString
& operator<<(unsigned long ul
)
1962 { return (*this) << Format(_T("%lu"), ul
); }
1963 #if defined wxLongLong_t && !defined wxLongLongIsLong
1964 // insert a long long if they exist and aren't longs
1965 wxString
& operator<<(wxLongLong_t ll
)
1967 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("d");
1968 return (*this) << Format(fmt
, ll
);
1970 // insert an unsigned long long
1971 wxString
& operator<<(wxULongLong_t ull
)
1973 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("u");
1974 return (*this) << Format(fmt
, ull
);
1976 #endif // wxLongLong_t && !wxLongLongIsLong
1977 // insert a float into string
1978 wxString
& operator<<(float f
)
1979 { return (*this) << Format(_T("%f"), f
); }
1980 // insert a double into string
1981 wxString
& operator<<(double d
)
1982 { return (*this) << Format(_T("%g"), d
); }
1984 // string comparison
1985 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
1986 int Cmp(const char *psz
) const
1987 { return compare(psz
); }
1988 int Cmp(const wchar_t *pwz
) const
1989 { return compare(pwz
); }
1990 int Cmp(const wxString
& s
) const
1991 { return compare(s
); }
1992 int Cmp(const wxCStrData
& s
) const
1993 { return compare(s
); }
1994 int Cmp(const wxCharBuffer
& s
) const
1995 { return compare(s
); }
1996 int Cmp(const wxWCharBuffer
& s
) const
1997 { return compare(s
); }
1998 // same as Cmp() but not case-sensitive
1999 int CmpNoCase(const wxString
& s
) const;
2001 // test for the string equality, either considering case or not
2002 // (if compareWithCase then the case matters)
2003 bool IsSameAs(const wxString
& str
, bool compareWithCase
= true) const
2005 #if !wxUSE_UNICODE_UTF8
2006 // in UTF-8 build, length() is O(n) and doing this would be _slower_
2007 if ( length() != str
.length() )
2010 return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0;
2012 bool IsSameAs(const char *str
, bool compareWithCase
= true) const
2013 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
2014 bool IsSameAs(const wchar_t *str
, bool compareWithCase
= true) const
2015 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
2017 bool IsSameAs(const wxCStrData
& str
, bool compareWithCase
= true) const
2018 { return IsSameAs(str
.AsString(), compareWithCase
); }
2019 bool IsSameAs(const wxCharBuffer
& str
, bool compareWithCase
= true) const
2020 { return IsSameAs(str
.data(), compareWithCase
); }
2021 bool IsSameAs(const wxWCharBuffer
& str
, bool compareWithCase
= true) const
2022 { return IsSameAs(str
.data(), compareWithCase
); }
2023 // comparison with a single character: returns true if equal
2024 bool IsSameAs(wxUniChar c
, bool compareWithCase
= true) const;
2025 // FIXME-UTF8: remove these overloads
2026 bool IsSameAs(wxUniCharRef c
, bool compareWithCase
= true) const
2027 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
2028 bool IsSameAs(char c
, bool compareWithCase
= true) const
2029 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
2030 bool IsSameAs(unsigned char c
, bool compareWithCase
= true) const
2031 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
2032 bool IsSameAs(wchar_t c
, bool compareWithCase
= true) const
2033 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
2034 bool IsSameAs(int c
, bool compareWithCase
= true) const
2035 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
2037 // simple sub-string extraction
2038 // return substring starting at nFirst of length nCount (or till the end
2039 // if nCount = default value)
2040 wxString
Mid(size_t nFirst
, size_t nCount
= npos
) const;
2042 // operator version of Mid()
2043 wxString
operator()(size_t start
, size_t len
) const
2044 { return Mid(start
, len
); }
2046 // check if the string starts with the given prefix and return the rest
2047 // of the string in the provided pointer if it is not NULL; otherwise
2049 bool StartsWith(const wxString
& prefix
, wxString
*rest
= NULL
) const;
2050 // check if the string ends with the given suffix and return the
2051 // beginning of the string before the suffix in the provided pointer if
2052 // it is not NULL; otherwise return false
2053 bool EndsWith(const wxString
& suffix
, wxString
*rest
= NULL
) const;
2055 // get first nCount characters
2056 wxString
Left(size_t nCount
) const;
2057 // get last nCount characters
2058 wxString
Right(size_t nCount
) const;
2059 // get all characters before the first occurance of ch
2060 // (returns the whole string if ch not found)
2061 wxString
BeforeFirst(wxUniChar ch
) const;
2062 // get all characters before the last occurence of ch
2063 // (returns empty string if ch not found)
2064 wxString
BeforeLast(wxUniChar ch
) const;
2065 // get all characters after the first occurence of ch
2066 // (returns empty string if ch not found)
2067 wxString
AfterFirst(wxUniChar ch
) const;
2068 // get all characters after the last occurence of ch
2069 // (returns the whole string if ch not found)
2070 wxString
AfterLast(wxUniChar ch
) const;
2072 // for compatibility only, use more explicitly named functions above
2073 wxString
Before(wxUniChar ch
) const { return BeforeLast(ch
); }
2074 wxString
After(wxUniChar ch
) const { return AfterFirst(ch
); }
2077 // convert to upper case in place, return the string itself
2078 wxString
& MakeUpper();
2079 // convert to upper case, return the copy of the string
2080 wxString
Upper() const { return wxString(*this).MakeUpper(); }
2081 // convert to lower case in place, return the string itself
2082 wxString
& MakeLower();
2083 // convert to lower case, return the copy of the string
2084 wxString
Lower() const { return wxString(*this).MakeLower(); }
2085 // convert the first character to the upper case and the rest to the
2086 // lower one, return the modified string itself
2087 wxString
& MakeCapitalized();
2088 // convert the first character to the upper case and the rest to the
2089 // lower one, return the copy of the string
2090 wxString
Capitalize() const { return wxString(*this).MakeCapitalized(); }
2092 // trimming/padding whitespace (either side) and truncating
2093 // remove spaces from left or from right (default) side
2094 wxString
& Trim(bool bFromRight
= true);
2095 // add nCount copies chPad in the beginning or at the end (default)
2096 wxString
& Pad(size_t nCount
, wxUniChar chPad
= wxT(' '), bool bFromRight
= true);
2098 // searching and replacing
2099 // searching (return starting index, or -1 if not found)
2100 int Find(wxUniChar ch
, bool bFromEnd
= false) const; // like strchr/strrchr
2101 int Find(wxUniCharRef ch
, bool bFromEnd
= false) const
2102 { return Find(wxUniChar(ch
), bFromEnd
); }
2103 int Find(char ch
, bool bFromEnd
= false) const
2104 { return Find(wxUniChar(ch
), bFromEnd
); }
2105 int Find(unsigned char ch
, bool bFromEnd
= false) const
2106 { return Find(wxUniChar(ch
), bFromEnd
); }
2107 int Find(wchar_t ch
, bool bFromEnd
= false) const
2108 { return Find(wxUniChar(ch
), bFromEnd
); }
2109 // searching (return starting index, or -1 if not found)
2110 int Find(const wxString
& sub
) const // like strstr
2112 size_type idx
= find(sub
);
2113 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
2115 int Find(const char *sub
) const // like strstr
2117 size_type idx
= find(sub
);
2118 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
2120 int Find(const wchar_t *sub
) const // like strstr
2122 size_type idx
= find(sub
);
2123 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
2126 int Find(const wxCStrData
& sub
) const
2127 { return Find(sub
.AsString()); }
2128 int Find(const wxCharBuffer
& sub
) const
2129 { return Find(sub
.data()); }
2130 int Find(const wxWCharBuffer
& sub
) const
2131 { return Find(sub
.data()); }
2133 // replace first (or all of bReplaceAll) occurences of substring with
2134 // another string, returns the number of replacements made
2135 size_t Replace(const wxString
& strOld
,
2136 const wxString
& strNew
,
2137 bool bReplaceAll
= true);
2139 // check if the string contents matches a mask containing '*' and '?'
2140 bool Matches(const wxString
& mask
) const;
2142 // conversion to numbers: all functions return true only if the whole
2143 // string is a number and put the value of this number into the pointer
2144 // provided, the base is the numeric base in which the conversion should be
2145 // done and must be comprised between 2 and 36 or be 0 in which case the
2146 // standard C rules apply (leading '0' => octal, "0x" => hex)
2147 // convert to a signed integer
2148 bool ToLong(long *val
, int base
= 10) const;
2149 // convert to an unsigned integer
2150 bool ToULong(unsigned long *val
, int base
= 10) const;
2151 // convert to wxLongLong
2152 #if defined(wxLongLong_t)
2153 bool ToLongLong(wxLongLong_t
*val
, int base
= 10) const;
2154 // convert to wxULongLong
2155 bool ToULongLong(wxULongLong_t
*val
, int base
= 10) const;
2156 #endif // wxLongLong_t
2157 // convert to a double
2158 bool ToDouble(double *val
) const;
2161 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2162 // formatted input/output
2163 // as sprintf(), returns the number of characters written or < 0 on error
2164 // (take 'this' into account in attribute parameter count)
2165 // int Printf(const wxString& format, ...);
2166 WX_DEFINE_VARARG_FUNC(int, Printf
, 1, (const wxFormatString
&),
2167 DoPrintfWchar
, DoPrintfUtf8
)
2169 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
2170 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wxString
&),
2171 (wxFormatString(f1
)));
2172 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wxCStrData
&),
2173 (wxFormatString(f1
)));
2174 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const char*),
2175 (wxFormatString(f1
)));
2176 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wchar_t*),
2177 (wxFormatString(f1
)));
2179 #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN
2180 // as vprintf(), returns the number of characters written or < 0 on error
2181 int PrintfV(const wxString
& format
, va_list argptr
);
2183 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2184 // returns the string containing the result of Printf() to it
2185 // static wxString Format(const wxString& format, ...) ATTRIBUTE_PRINTF_1;
2186 WX_DEFINE_VARARG_FUNC(static wxString
, Format
, 1, (const wxFormatString
&),
2187 DoFormatWchar
, DoFormatUtf8
)
2189 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
2190 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wxString
&),
2191 (wxFormatString(f1
)));
2192 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wxCStrData
&),
2193 (wxFormatString(f1
)));
2194 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const char*),
2195 (wxFormatString(f1
)));
2196 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wchar_t*),
2197 (wxFormatString(f1
)));
2200 // the same as above, but takes a va_list
2201 static wxString
FormatV(const wxString
& format
, va_list argptr
);
2203 // raw access to string memory
2204 // ensure that string has space for at least nLen characters
2205 // only works if the data of this string is not shared
2206 bool Alloc(size_t nLen
) { reserve(nLen
); return capacity() >= nLen
; }
2207 // minimize the string's memory
2208 // only works if the data of this string is not shared
2210 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2211 // These are deprecated, use wxStringBuffer or wxStringBufferLength instead
2213 // get writable buffer of at least nLen bytes. Unget() *must* be called
2214 // a.s.a.p. to put string back in a reasonable state!
2215 wxDEPRECATED( wxStringCharType
*GetWriteBuf(size_t nLen
) );
2216 // call this immediately after GetWriteBuf() has been used
2217 wxDEPRECATED( void UngetWriteBuf() );
2218 wxDEPRECATED( void UngetWriteBuf(size_t nLen
) );
2219 #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && wxUSE_UNICODE_UTF8
2221 // wxWidgets version 1 compatibility functions
2224 wxString
SubString(size_t from
, size_t to
) const
2225 { return Mid(from
, (to
- from
+ 1)); }
2226 // values for second parameter of CompareTo function
2227 enum caseCompare
{exact
, ignoreCase
};
2228 // values for first parameter of Strip function
2229 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
2231 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2233 // (take 'this' into account in attribute parameter count)
2234 // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2;
2235 WX_DEFINE_VARARG_FUNC(int, sprintf
, 1, (const wxFormatString
&),
2236 DoPrintfWchar
, DoPrintfUtf8
)
2238 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
2239 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wxString
&),
2240 (wxFormatString(f1
)));
2241 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wxCStrData
&),
2242 (wxFormatString(f1
)));
2243 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const char*),
2244 (wxFormatString(f1
)));
2245 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wchar_t*),
2246 (wxFormatString(f1
)));
2248 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
2251 int CompareTo(const wxChar
* psz
, caseCompare cmp
= exact
) const
2252 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
2255 size_t Length() const { return length(); }
2256 // Count the number of characters
2257 int Freq(wxUniChar ch
) const;
2259 void LowerCase() { MakeLower(); }
2261 void UpperCase() { MakeUpper(); }
2262 // use Trim except that it doesn't change this string
2263 wxString
Strip(stripType w
= trailing
) const;
2265 // use Find (more general variants not yet supported)
2266 size_t Index(const wxChar
* psz
) const { return Find(psz
); }
2267 size_t Index(wxUniChar ch
) const { return Find(ch
); }
2269 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
2270 wxString
& RemoveLast(size_t n
= 1) { return Truncate(length() - n
); }
2272 wxString
& Remove(size_t nStart
, size_t nLen
)
2273 { return (wxString
&)erase( nStart
, nLen
); }
2276 int First( wxUniChar ch
) const { return Find(ch
); }
2277 int First( wxUniCharRef ch
) const { return Find(ch
); }
2278 int First( char ch
) const { return Find(ch
); }
2279 int First( unsigned char ch
) const { return Find(ch
); }
2280 int First( wchar_t ch
) const { return Find(ch
); }
2281 int First( const wxString
& str
) const { return Find(str
); }
2282 int Last( wxUniChar ch
) const { return Find(ch
, true); }
2283 bool Contains(const wxString
& str
) const { return Find(str
) != wxNOT_FOUND
; }
2286 bool IsNull() const { return empty(); }
2288 // std::string compatibility functions
2290 // take nLen chars starting at nPos
2291 wxString(const wxString
& str
, size_t nPos
, size_t nLen
)
2292 { assign(str
, nPos
, nLen
); }
2293 // take all characters from first to last
2294 wxString(const_iterator first
, const_iterator last
)
2295 : m_impl(first
.impl(), last
.impl()) { }
2296 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2297 // the 2 overloads below are for compatibility with the existing code using
2298 // pointers instead of iterators
2299 wxString(const char *first
, const char *last
)
2301 SubstrBufFromMB
str(ImplStr(first
, last
- first
));
2302 m_impl
.assign(str
.data
, str
.len
);
2304 wxString(const wchar_t *first
, const wchar_t *last
)
2306 SubstrBufFromWC
str(ImplStr(first
, last
- first
));
2307 m_impl
.assign(str
.data
, str
.len
);
2309 // and this one is needed to compile code adding offsets to c_str() result
2310 wxString(const wxCStrData
& first
, const wxCStrData
& last
)
2311 : m_impl(CreateConstIterator(first
).impl(),
2312 CreateConstIterator(last
).impl())
2314 wxASSERT_MSG( first
.m_str
== last
.m_str
,
2315 _T("pointers must be into the same string") );
2317 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2319 // lib.string.modifiers
2320 // append elements str[pos], ..., str[pos+n]
2321 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
2323 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2326 str
.PosLenToImpl(pos
, n
, &from
, &len
);
2327 m_impl
.append(str
.m_impl
, from
, len
);
2331 wxString
& append(const wxString
& str
)
2333 wxSTRING_UPDATE_CACHED_LENGTH(str
.length());
2335 m_impl
.append(str
.m_impl
);
2339 // append first n (or all if n == npos) characters of sz
2340 wxString
& append(const char *sz
)
2342 wxSTRING_INVALIDATE_CACHED_LENGTH();
2344 m_impl
.append(ImplStr(sz
));
2348 wxString
& append(const wchar_t *sz
)
2350 wxSTRING_INVALIDATE_CACHED_LENGTH();
2352 m_impl
.append(ImplStr(sz
));
2356 wxString
& append(const char *sz
, size_t n
)
2358 wxSTRING_INVALIDATE_CACHED_LENGTH();
2360 SubstrBufFromMB
str(ImplStr(sz
, n
));
2361 m_impl
.append(str
.data
, str
.len
);
2364 wxString
& append(const wchar_t *sz
, size_t n
)
2366 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2368 SubstrBufFromWC
str(ImplStr(sz
, n
));
2369 m_impl
.append(str
.data
, str
.len
);
2373 wxString
& append(const wxCStrData
& str
)
2374 { return append(str
.AsString()); }
2375 wxString
& append(const wxCharBuffer
& str
)
2376 { return append(str
.data()); }
2377 wxString
& append(const wxWCharBuffer
& str
)
2378 { return append(str
.data()); }
2379 wxString
& append(const wxCStrData
& str
, size_t n
)
2380 { return append(str
.AsString(), 0, n
); }
2381 wxString
& append(const wxCharBuffer
& str
, size_t n
)
2382 { return append(str
.data(), n
); }
2383 wxString
& append(const wxWCharBuffer
& str
, size_t n
)
2384 { return append(str
.data(), n
); }
2386 // append n copies of ch
2387 wxString
& append(size_t n
, wxUniChar ch
)
2389 #if wxUSE_UNICODE_UTF8
2390 if ( !ch
.IsAscii() )
2392 wxSTRING_INVALIDATE_CACHED_LENGTH();
2394 m_impl
.append(wxStringOperations::EncodeNChars(n
, ch
));
2399 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2401 m_impl
.append(n
, (wxStringCharType
)ch
);
2407 wxString
& append(size_t n
, wxUniCharRef ch
)
2408 { return append(n
, wxUniChar(ch
)); }
2409 wxString
& append(size_t n
, char ch
)
2410 { return append(n
, wxUniChar(ch
)); }
2411 wxString
& append(size_t n
, unsigned char ch
)
2412 { return append(n
, wxUniChar(ch
)); }
2413 wxString
& append(size_t n
, wchar_t ch
)
2414 { return append(n
, wxUniChar(ch
)); }
2416 // append from first to last
2417 wxString
& append(const_iterator first
, const_iterator last
)
2419 wxSTRING_INVALIDATE_CACHED_LENGTH();
2421 m_impl
.append(first
.impl(), last
.impl());
2424 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2425 wxString
& append(const char *first
, const char *last
)
2426 { return append(first
, last
- first
); }
2427 wxString
& append(const wchar_t *first
, const wchar_t *last
)
2428 { return append(first
, last
- first
); }
2429 wxString
& append(const wxCStrData
& first
, const wxCStrData
& last
)
2430 { return append(CreateConstIterator(first
), CreateConstIterator(last
)); }
2431 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2433 // same as `this_string = str'
2434 wxString
& assign(const wxString
& str
)
2436 wxSTRING_SET_CACHED_LENGTH(str
.length());
2438 m_impl
= str
.m_impl
;
2443 wxString
& assign(const wxString
& str
, size_t len
)
2445 wxSTRING_SET_CACHED_LENGTH(len
);
2447 m_impl
.assign(str
.m_impl
, 0, str
.LenToImpl(len
));
2452 // same as ` = str[pos..pos + n]
2453 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
2456 str
.PosLenToImpl(pos
, n
, &from
, &len
);
2457 m_impl
.assign(str
.m_impl
, from
, len
);
2459 // it's important to call this after PosLenToImpl() above in case str is
2460 // the same string as this one
2461 wxSTRING_SET_CACHED_LENGTH(n
);
2466 // same as `= first n (or all if n == npos) characters of sz'
2467 wxString
& assign(const char *sz
)
2469 wxSTRING_INVALIDATE_CACHE();
2471 m_impl
.assign(ImplStr(sz
));
2476 wxString
& assign(const wchar_t *sz
)
2478 wxSTRING_INVALIDATE_CACHE();
2480 m_impl
.assign(ImplStr(sz
));
2485 wxString
& assign(const char *sz
, size_t n
)
2487 wxSTRING_SET_CACHED_LENGTH(n
);
2489 SubstrBufFromMB
str(ImplStr(sz
, n
));
2490 m_impl
.assign(str
.data
, str
.len
);
2495 wxString
& assign(const wchar_t *sz
, size_t n
)
2497 wxSTRING_SET_CACHED_LENGTH(n
);
2499 SubstrBufFromWC
str(ImplStr(sz
, n
));
2500 m_impl
.assign(str
.data
, str
.len
);
2505 wxString
& assign(const wxCStrData
& str
)
2506 { return assign(str
.AsString()); }
2507 wxString
& assign(const wxCharBuffer
& str
)
2508 { return assign(str
.data()); }
2509 wxString
& assign(const wxWCharBuffer
& str
)
2510 { return assign(str
.data()); }
2511 wxString
& assign(const wxCStrData
& str
, size_t len
)
2512 { return assign(str
.AsString(), len
); }
2513 wxString
& assign(const wxCharBuffer
& str
, size_t len
)
2514 { return assign(str
.data(), len
); }
2515 wxString
& assign(const wxWCharBuffer
& str
, size_t len
)
2516 { return assign(str
.data(), len
); }
2518 // same as `= n copies of ch'
2519 wxString
& assign(size_t n
, wxUniChar ch
)
2521 wxSTRING_SET_CACHED_LENGTH(n
);
2523 #if wxUSE_UNICODE_UTF8
2524 if ( !ch
.IsAscii() )
2525 m_impl
.assign(wxStringOperations::EncodeNChars(n
, ch
));
2528 m_impl
.assign(n
, (wxStringCharType
)ch
);
2533 wxString
& assign(size_t n
, wxUniCharRef ch
)
2534 { return assign(n
, wxUniChar(ch
)); }
2535 wxString
& assign(size_t n
, char ch
)
2536 { return assign(n
, wxUniChar(ch
)); }
2537 wxString
& assign(size_t n
, unsigned char ch
)
2538 { return assign(n
, wxUniChar(ch
)); }
2539 wxString
& assign(size_t n
, wchar_t ch
)
2540 { return assign(n
, wxUniChar(ch
)); }
2542 // assign from first to last
2543 wxString
& assign(const_iterator first
, const_iterator last
)
2545 wxSTRING_INVALIDATE_CACHE();
2547 m_impl
.assign(first
.impl(), last
.impl());
2551 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2552 wxString
& assign(const char *first
, const char *last
)
2553 { return assign(first
, last
- first
); }
2554 wxString
& assign(const wchar_t *first
, const wchar_t *last
)
2555 { return assign(first
, last
- first
); }
2556 wxString
& assign(const wxCStrData
& first
, const wxCStrData
& last
)
2557 { return assign(CreateConstIterator(first
), CreateConstIterator(last
)); }
2558 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2560 // string comparison
2561 int compare(const wxString
& str
) const;
2562 int compare(const char* sz
) const;
2563 int compare(const wchar_t* sz
) const;
2564 int compare(const wxCStrData
& str
) const
2565 { return compare(str
.AsString()); }
2566 int compare(const wxCharBuffer
& str
) const
2567 { return compare(str
.data()); }
2568 int compare(const wxWCharBuffer
& str
) const
2569 { return compare(str
.data()); }
2570 // comparison with a substring
2571 int compare(size_t nStart
, size_t nLen
, const wxString
& str
) const;
2572 // comparison of 2 substrings
2573 int compare(size_t nStart
, size_t nLen
,
2574 const wxString
& str
, size_t nStart2
, size_t nLen2
) const;
2575 // substring comparison with first nCount characters of sz
2576 int compare(size_t nStart
, size_t nLen
,
2577 const char* sz
, size_t nCount
= npos
) const;
2578 int compare(size_t nStart
, size_t nLen
,
2579 const wchar_t* sz
, size_t nCount
= npos
) const;
2581 // insert another string
2582 wxString
& insert(size_t nPos
, const wxString
& str
)
2583 { insert(GetIterForNthChar(nPos
), str
.begin(), str
.end()); return *this; }
2584 // insert n chars of str starting at nStart (in str)
2585 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
2587 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2590 str
.PosLenToImpl(nStart
, n
, &from
, &len
);
2591 m_impl
.insert(PosToImpl(nPos
), str
.m_impl
, from
, len
);
2596 // insert first n (or all if n == npos) characters of sz
2597 wxString
& insert(size_t nPos
, const char *sz
)
2599 wxSTRING_INVALIDATE_CACHE();
2601 m_impl
.insert(PosToImpl(nPos
), ImplStr(sz
));
2606 wxString
& insert(size_t nPos
, const wchar_t *sz
)
2608 wxSTRING_INVALIDATE_CACHE();
2610 m_impl
.insert(PosToImpl(nPos
), ImplStr(sz
)); return *this;
2613 wxString
& insert(size_t nPos
, const char *sz
, size_t n
)
2615 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2617 SubstrBufFromMB
str(ImplStr(sz
, n
));
2618 m_impl
.insert(PosToImpl(nPos
), str
.data
, str
.len
);
2623 wxString
& insert(size_t nPos
, const wchar_t *sz
, size_t n
)
2625 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2627 SubstrBufFromWC
str(ImplStr(sz
, n
));
2628 m_impl
.insert(PosToImpl(nPos
), str
.data
, str
.len
);
2633 // insert n copies of ch
2634 wxString
& insert(size_t nPos
, size_t n
, wxUniChar ch
)
2636 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2638 #if wxUSE_UNICODE_UTF8
2639 if ( !ch
.IsAscii() )
2640 m_impl
.insert(PosToImpl(nPos
), wxStringOperations::EncodeNChars(n
, ch
));
2643 m_impl
.insert(PosToImpl(nPos
), n
, (wxStringCharType
)ch
);
2647 iterator
insert(iterator it
, wxUniChar ch
)
2649 wxSTRING_UPDATE_CACHED_LENGTH(1);
2651 #if wxUSE_UNICODE_UTF8
2652 if ( !ch
.IsAscii() )
2654 size_t pos
= IterToImplPos(it
);
2655 m_impl
.insert(pos
, wxStringOperations::EncodeChar(ch
));
2656 return iterator(this, m_impl
.begin() + pos
);
2660 return iterator(this, m_impl
.insert(it
.impl(), (wxStringCharType
)ch
));
2663 void insert(iterator it
, const_iterator first
, const_iterator last
)
2665 wxSTRING_INVALIDATE_CACHE();
2667 m_impl
.insert(it
.impl(), first
.impl(), last
.impl());
2670 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2671 void insert(iterator it
, const char *first
, const char *last
)
2672 { insert(it
- begin(), first
, last
- first
); }
2673 void insert(iterator it
, const wchar_t *first
, const wchar_t *last
)
2674 { insert(it
- begin(), first
, last
- first
); }
2675 void insert(iterator it
, const wxCStrData
& first
, const wxCStrData
& last
)
2676 { insert(it
, CreateConstIterator(first
), CreateConstIterator(last
)); }
2677 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2679 void insert(iterator it
, size_type n
, wxUniChar ch
)
2681 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2683 #if wxUSE_UNICODE_UTF8
2684 if ( !ch
.IsAscii() )
2685 m_impl
.insert(IterToImplPos(it
), wxStringOperations::EncodeNChars(n
, ch
));
2688 m_impl
.insert(it
.impl(), n
, (wxStringCharType
)ch
);
2691 // delete characters from nStart to nStart + nLen
2692 wxString
& erase(size_type pos
= 0, size_type n
= npos
)
2694 wxSTRING_INVALIDATE_CACHE();
2697 PosLenToImpl(pos
, n
, &from
, &len
);
2698 m_impl
.erase(from
, len
);
2703 // delete characters from first up to last
2704 iterator
erase(iterator first
, iterator last
)
2706 wxSTRING_INVALIDATE_CACHE();
2708 return iterator(this, m_impl
.erase(first
.impl(), last
.impl()));
2711 iterator
erase(iterator first
)
2713 wxSTRING_UPDATE_CACHED_LENGTH(-1);
2715 return iterator(this, m_impl
.erase(first
.impl()));
2718 #ifdef wxSTRING_BASE_HASNT_CLEAR
2719 void clear() { erase(); }
2723 wxSTRING_SET_CACHED_LENGTH(0);
2729 // replaces the substring of length nLen starting at nStart
2730 wxString
& replace(size_t nStart
, size_t nLen
, const char* sz
)
2732 wxSTRING_INVALIDATE_CACHE();
2735 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2736 m_impl
.replace(from
, len
, ImplStr(sz
));
2741 wxString
& replace(size_t nStart
, size_t nLen
, const wchar_t* sz
)
2743 wxSTRING_INVALIDATE_CACHE();
2746 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2747 m_impl
.replace(from
, len
, ImplStr(sz
));
2752 // replaces the substring of length nLen starting at nStart
2753 wxString
& replace(size_t nStart
, size_t nLen
, const wxString
& str
)
2755 wxSTRING_INVALIDATE_CACHE();
2758 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2759 m_impl
.replace(from
, len
, str
.m_impl
);
2764 // replaces the substring with nCount copies of ch
2765 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxUniChar ch
)
2767 wxSTRING_INVALIDATE_CACHE();
2770 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2771 #if wxUSE_UNICODE_UTF8
2772 if ( !ch
.IsAscii() )
2773 m_impl
.replace(from
, len
, wxStringOperations::EncodeNChars(nCount
, ch
));
2776 m_impl
.replace(from
, len
, nCount
, (wxStringCharType
)ch
);
2781 // replaces a substring with another substring
2782 wxString
& replace(size_t nStart
, size_t nLen
,
2783 const wxString
& str
, size_t nStart2
, size_t nLen2
)
2785 wxSTRING_INVALIDATE_CACHE();
2788 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2791 str
.PosLenToImpl(nStart2
, nLen2
, &from2
, &len2
);
2793 m_impl
.replace(from
, len
, str
.m_impl
, from2
, len2
);
2798 // replaces the substring with first nCount chars of sz
2799 wxString
& replace(size_t nStart
, size_t nLen
,
2800 const char* sz
, size_t nCount
)
2802 wxSTRING_INVALIDATE_CACHE();
2805 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2807 SubstrBufFromMB
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 wchar_t* sz
, size_t nCount
)
2817 wxSTRING_INVALIDATE_CACHE();
2820 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2822 SubstrBufFromWC
str(ImplStr(sz
, nCount
));
2824 m_impl
.replace(from
, len
, str
.data
, str
.len
);
2829 wxString
& replace(size_t nStart
, size_t nLen
,
2830 const wxString
& s
, size_t nCount
)
2832 wxSTRING_INVALIDATE_CACHE();
2835 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2836 m_impl
.replace(from
, len
, s
.m_impl
.c_str(), s
.LenToImpl(nCount
));
2841 wxString
& replace(iterator first
, iterator last
, const char* s
)
2843 wxSTRING_INVALIDATE_CACHE();
2845 m_impl
.replace(first
.impl(), last
.impl(), ImplStr(s
));
2850 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
)
2852 wxSTRING_INVALIDATE_CACHE();
2854 m_impl
.replace(first
.impl(), last
.impl(), ImplStr(s
));
2859 wxString
& replace(iterator first
, iterator last
, const char* s
, size_type n
)
2861 wxSTRING_INVALIDATE_CACHE();
2863 SubstrBufFromMB
str(ImplStr(s
, n
));
2864 m_impl
.replace(first
.impl(), last
.impl(), str
.data
, str
.len
);
2869 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
, size_type n
)
2871 wxSTRING_INVALIDATE_CACHE();
2873 SubstrBufFromWC
str(ImplStr(s
, n
));
2874 m_impl
.replace(first
.impl(), last
.impl(), str
.data
, str
.len
);
2879 wxString
& replace(iterator first
, iterator last
, const wxString
& s
)
2881 wxSTRING_INVALIDATE_CACHE();
2883 m_impl
.replace(first
.impl(), last
.impl(), s
.m_impl
);
2888 wxString
& replace(iterator first
, iterator last
, size_type n
, wxUniChar ch
)
2890 wxSTRING_INVALIDATE_CACHE();
2892 #if wxUSE_UNICODE_UTF8
2893 if ( !ch
.IsAscii() )
2894 m_impl
.replace(first
.impl(), last
.impl(),
2895 wxStringOperations::EncodeNChars(n
, ch
));
2898 m_impl
.replace(first
.impl(), last
.impl(), n
, (wxStringCharType
)ch
);
2903 wxString
& replace(iterator first
, iterator last
,
2904 const_iterator first1
, const_iterator last1
)
2906 wxSTRING_INVALIDATE_CACHE();
2908 m_impl
.replace(first
.impl(), last
.impl(), first1
.impl(), last1
.impl());
2913 wxString
& replace(iterator first
, iterator last
,
2914 const char *first1
, const char *last1
)
2915 { replace(first
, last
, first1
, last1
- first1
); return *this; }
2916 wxString
& replace(iterator first
, iterator last
,
2917 const wchar_t *first1
, const wchar_t *last1
)
2918 { replace(first
, last
, first1
, last1
- first1
); return *this; }
2921 void swap(wxString
& str
)
2923 #if wxUSE_STRING_POS_CACHE
2924 // we modify not only this string but also the other one directly so we
2925 // need to invalidate cache for both of them (we could also try to
2926 // exchange their cache entries but it seems unlikely to be worth it)
2928 str
.InvalidateCache();
2929 #endif // wxUSE_STRING_POS_CACHE
2931 m_impl
.swap(str
.m_impl
);
2935 size_t find(const wxString
& str
, size_t nStart
= 0) const
2936 { return PosFromImpl(m_impl
.find(str
.m_impl
, PosToImpl(nStart
))); }
2938 // find first n characters of sz
2939 size_t find(const char* sz
, size_t nStart
= 0, size_t n
= npos
) const
2941 SubstrBufFromMB
str(ImplStr(sz
, n
));
2942 return PosFromImpl(m_impl
.find(str
.data
, PosToImpl(nStart
), str
.len
));
2944 size_t find(const wchar_t* sz
, size_t nStart
= 0, size_t n
= npos
) const
2946 SubstrBufFromWC
str(ImplStr(sz
, n
));
2947 return PosFromImpl(m_impl
.find(str
.data
, PosToImpl(nStart
), str
.len
));
2949 size_t find(const wxCharBuffer
& s
, size_t nStart
= 0, size_t n
= npos
) const
2950 { return find(s
.data(), nStart
, n
); }
2951 size_t find(const wxWCharBuffer
& s
, size_t nStart
= 0, size_t n
= npos
) const
2952 { return find(s
.data(), nStart
, n
); }
2953 size_t find(const wxCStrData
& s
, size_t nStart
= 0, size_t n
= npos
) const
2954 { return find(s
.AsWChar(), nStart
, n
); }
2956 // find the first occurence of character ch after nStart
2957 size_t find(wxUniChar ch
, size_t nStart
= 0) const
2959 #if wxUSE_UNICODE_UTF8
2960 if ( !ch
.IsAscii() )
2961 return PosFromImpl(m_impl
.find(wxStringOperations::EncodeChar(ch
),
2962 PosToImpl(nStart
)));
2965 return PosFromImpl(m_impl
.find((wxStringCharType
)ch
,
2966 PosToImpl(nStart
)));
2969 size_t find(wxUniCharRef ch
, size_t nStart
= 0) const
2970 { return find(wxUniChar(ch
), nStart
); }
2971 size_t find(char ch
, size_t nStart
= 0) const
2972 { return find(wxUniChar(ch
), nStart
); }
2973 size_t find(unsigned char ch
, size_t nStart
= 0) const
2974 { return find(wxUniChar(ch
), nStart
); }
2975 size_t find(wchar_t ch
, size_t nStart
= 0) const
2976 { return find(wxUniChar(ch
), nStart
); }
2978 // rfind() family is exactly like find() but works right to left
2980 // as find, but from the end
2981 size_t rfind(const wxString
& str
, size_t nStart
= npos
) const
2982 { return PosFromImpl(m_impl
.rfind(str
.m_impl
, PosToImpl(nStart
))); }
2984 // as find, but from the end
2985 size_t rfind(const char* sz
, size_t nStart
= npos
, size_t n
= npos
) const
2987 SubstrBufFromMB
str(ImplStr(sz
, n
));
2988 return PosFromImpl(m_impl
.rfind(str
.data
, PosToImpl(nStart
), str
.len
));
2990 size_t rfind(const wchar_t* sz
, size_t nStart
= npos
, size_t n
= npos
) const
2992 SubstrBufFromWC
str(ImplStr(sz
, n
));
2993 return PosFromImpl(m_impl
.rfind(str
.data
, PosToImpl(nStart
), str
.len
));
2995 size_t rfind(const wxCharBuffer
& s
, size_t nStart
= npos
, size_t n
= npos
) const
2996 { return rfind(s
.data(), nStart
, n
); }
2997 size_t rfind(const wxWCharBuffer
& s
, size_t nStart
= npos
, size_t n
= npos
) const
2998 { return rfind(s
.data(), nStart
, n
); }
2999 size_t rfind(const wxCStrData
& s
, size_t nStart
= npos
, size_t n
= npos
) const
3000 { return rfind(s
.AsWChar(), nStart
, n
); }
3001 // as find, but from the end
3002 size_t rfind(wxUniChar ch
, size_t nStart
= npos
) const
3004 #if wxUSE_UNICODE_UTF8
3005 if ( !ch
.IsAscii() )
3006 return PosFromImpl(m_impl
.rfind(wxStringOperations::EncodeChar(ch
),
3007 PosToImpl(nStart
)));
3010 return PosFromImpl(m_impl
.rfind((wxStringCharType
)ch
,
3011 PosToImpl(nStart
)));
3013 size_t rfind(wxUniCharRef ch
, size_t nStart
= npos
) const
3014 { return rfind(wxUniChar(ch
), nStart
); }
3015 size_t rfind(char ch
, size_t nStart
= npos
) const
3016 { return rfind(wxUniChar(ch
), nStart
); }
3017 size_t rfind(unsigned char ch
, size_t nStart
= npos
) const
3018 { return rfind(wxUniChar(ch
), nStart
); }
3019 size_t rfind(wchar_t ch
, size_t nStart
= npos
) const
3020 { return rfind(wxUniChar(ch
), nStart
); }
3022 // find first/last occurence of any character (not) in the set:
3023 #if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
3024 // FIXME-UTF8: this is not entirely correct, because it doesn't work if
3025 // sizeof(wchar_t)==2 and surrogates are present in the string;
3026 // should we care? Probably not.
3027 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
3028 { return m_impl
.find_first_of(str
.m_impl
, nStart
); }
3029 size_t find_first_of(const char* sz
, size_t nStart
= 0) const
3030 { return m_impl
.find_first_of(ImplStr(sz
), nStart
); }
3031 size_t find_first_of(const wchar_t* sz
, size_t nStart
= 0) const
3032 { return m_impl
.find_first_of(ImplStr(sz
), nStart
); }
3033 size_t find_first_of(const char* sz
, size_t nStart
, size_t n
) const
3034 { return m_impl
.find_first_of(ImplStr(sz
), nStart
, n
); }
3035 size_t find_first_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
3036 { return m_impl
.find_first_of(ImplStr(sz
), nStart
, n
); }
3037 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const
3038 { return m_impl
.find_first_of((wxChar
)c
, nStart
); }
3040 size_t find_last_of(const wxString
& str
, size_t nStart
= npos
) const
3041 { return m_impl
.find_last_of(str
.m_impl
, nStart
); }
3042 size_t find_last_of(const char* sz
, size_t nStart
= npos
) const
3043 { return m_impl
.find_last_of(ImplStr(sz
), nStart
); }
3044 size_t find_last_of(const wchar_t* sz
, size_t nStart
= npos
) const
3045 { return m_impl
.find_last_of(ImplStr(sz
), nStart
); }
3046 size_t find_last_of(const char* sz
, size_t nStart
, size_t n
) const
3047 { return m_impl
.find_last_of(ImplStr(sz
), nStart
, n
); }
3048 size_t find_last_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
3049 { return m_impl
.find_last_of(ImplStr(sz
), nStart
, n
); }
3050 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const
3051 { return m_impl
.find_last_of((wxChar
)c
, nStart
); }
3053 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
3054 { return m_impl
.find_first_not_of(str
.m_impl
, nStart
); }
3055 size_t find_first_not_of(const char* sz
, size_t nStart
= 0) const
3056 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
); }
3057 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
= 0) const
3058 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
); }
3059 size_t find_first_not_of(const char* sz
, size_t nStart
, size_t n
) const
3060 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
, n
); }
3061 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
3062 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
, n
); }
3063 size_t find_first_not_of(wxUniChar c
, size_t nStart
= 0) const
3064 { return m_impl
.find_first_not_of((wxChar
)c
, nStart
); }
3066 size_t find_last_not_of(const wxString
& str
, size_t nStart
= npos
) const
3067 { return m_impl
.find_last_not_of(str
.m_impl
, nStart
); }
3068 size_t find_last_not_of(const char* sz
, size_t nStart
= npos
) const
3069 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
); }
3070 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
= npos
) const
3071 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
); }
3072 size_t find_last_not_of(const char* sz
, size_t nStart
, size_t n
) const
3073 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
, n
); }
3074 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
3075 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
, n
); }
3076 size_t find_last_not_of(wxUniChar c
, size_t nStart
= npos
) const
3077 { return m_impl
.find_last_not_of((wxChar
)c
, nStart
); }
3079 // we can't use std::string implementation in UTF-8 build, because the
3080 // character sets would be interpreted wrongly:
3082 // as strpbrk() but starts at nStart, returns npos if not found
3083 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
3084 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3085 { return find_first_of(str
.wc_str(), nStart
); }
3087 { return find_first_of(str
.mb_str(), nStart
); }
3090 size_t find_first_of(const char* sz
, size_t nStart
= 0) const;
3091 size_t find_first_of(const wchar_t* sz
, size_t nStart
= 0) const;
3092 size_t find_first_of(const char* sz
, size_t nStart
, size_t n
) const;
3093 size_t find_first_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
3094 // same as find(char, size_t)
3095 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const
3096 { return find(c
, nStart
); }
3097 // find the last (starting from nStart) char from str in this string
3098 size_t find_last_of (const wxString
& str
, size_t nStart
= npos
) const
3099 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3100 { return find_last_of(str
.wc_str(), nStart
); }
3102 { return find_last_of(str
.mb_str(), nStart
); }
3105 size_t find_last_of (const char* sz
, size_t nStart
= npos
) const;
3106 size_t find_last_of (const wchar_t* sz
, size_t nStart
= npos
) const;
3107 size_t find_last_of(const char* sz
, size_t nStart
, size_t n
) const;
3108 size_t find_last_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
3110 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const
3111 { return rfind(c
, nStart
); }
3113 // find first/last occurence of any character not in the set
3115 // as strspn() (starting from nStart), returns npos on failure
3116 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
3117 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3118 { return find_first_not_of(str
.wc_str(), nStart
); }
3120 { return find_first_not_of(str
.mb_str(), nStart
); }
3123 size_t find_first_not_of(const char* sz
, size_t nStart
= 0) const;
3124 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
= 0) const;
3125 size_t find_first_not_of(const char* sz
, size_t nStart
, size_t n
) const;
3126 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
3128 size_t find_first_not_of(wxUniChar ch
, size_t nStart
= 0) const;
3130 size_t find_last_not_of(const wxString
& str
, size_t nStart
= npos
) const
3131 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3132 { return find_last_not_of(str
.wc_str(), nStart
); }
3134 { return find_last_not_of(str
.mb_str(), nStart
); }
3137 size_t find_last_not_of(const char* sz
, size_t nStart
= npos
) const;
3138 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
= npos
) const;
3139 size_t find_last_not_of(const char* sz
, size_t nStart
, size_t n
) const;
3140 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
3142 size_t find_last_not_of(wxUniChar ch
, size_t nStart
= npos
) const;
3143 #endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not
3145 // provide char/wchar_t/wxUniCharRef overloads for char-finding functions
3146 // above to resolve ambiguities:
3147 size_t find_first_of(wxUniCharRef ch
, size_t nStart
= 0) const
3148 { return find_first_of(wxUniChar(ch
), nStart
); }
3149 size_t find_first_of(char ch
, size_t nStart
= 0) const
3150 { return find_first_of(wxUniChar(ch
), nStart
); }
3151 size_t find_first_of(unsigned char ch
, size_t nStart
= 0) const
3152 { return find_first_of(wxUniChar(ch
), nStart
); }
3153 size_t find_first_of(wchar_t ch
, size_t nStart
= 0) const
3154 { return find_first_of(wxUniChar(ch
), nStart
); }
3155 size_t find_last_of(wxUniCharRef ch
, size_t nStart
= npos
) const
3156 { return find_last_of(wxUniChar(ch
), nStart
); }
3157 size_t find_last_of(char ch
, size_t nStart
= npos
) const
3158 { return find_last_of(wxUniChar(ch
), nStart
); }
3159 size_t find_last_of(unsigned char ch
, size_t nStart
= npos
) const
3160 { return find_last_of(wxUniChar(ch
), nStart
); }
3161 size_t find_last_of(wchar_t ch
, size_t nStart
= npos
) const
3162 { return find_last_of(wxUniChar(ch
), nStart
); }
3163 size_t find_first_not_of(wxUniCharRef ch
, size_t nStart
= 0) const
3164 { return find_first_not_of(wxUniChar(ch
), nStart
); }
3165 size_t find_first_not_of(char ch
, size_t nStart
= 0) const
3166 { return find_first_not_of(wxUniChar(ch
), nStart
); }
3167 size_t find_first_not_of(unsigned char ch
, size_t nStart
= 0) const
3168 { return find_first_not_of(wxUniChar(ch
), nStart
); }
3169 size_t find_first_not_of(wchar_t ch
, size_t nStart
= 0) const
3170 { return find_first_not_of(wxUniChar(ch
), nStart
); }
3171 size_t find_last_not_of(wxUniCharRef ch
, size_t nStart
= npos
) const
3172 { return find_last_not_of(wxUniChar(ch
), nStart
); }
3173 size_t find_last_not_of(char ch
, size_t nStart
= npos
) const
3174 { return find_last_not_of(wxUniChar(ch
), nStart
); }
3175 size_t find_last_not_of(unsigned char ch
, size_t nStart
= npos
) const
3176 { return find_last_not_of(wxUniChar(ch
), nStart
); }
3177 size_t find_last_not_of(wchar_t ch
, size_t nStart
= npos
) const
3178 { return find_last_not_of(wxUniChar(ch
), nStart
); }
3180 // and additional overloads for the versions taking strings:
3181 size_t find_first_of(const wxCStrData
& sz
, size_t nStart
= 0) const
3182 { return find_first_of(sz
.AsString(), nStart
); }
3183 size_t find_first_of(const wxCharBuffer
& sz
, size_t nStart
= 0) const
3184 { return find_first_of(sz
.data(), nStart
); }
3185 size_t find_first_of(const wxWCharBuffer
& sz
, size_t nStart
= 0) const
3186 { return find_first_of(sz
.data(), nStart
); }
3187 size_t find_first_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
3188 { return find_first_of(sz
.AsWChar(), nStart
, n
); }
3189 size_t find_first_of(const wxCharBuffer
& sz
, size_t nStart
, size_t n
) const
3190 { return find_first_of(sz
.data(), nStart
, n
); }
3191 size_t find_first_of(const wxWCharBuffer
& sz
, size_t nStart
, size_t n
) const
3192 { return find_first_of(sz
.data(), nStart
, n
); }
3194 size_t find_last_of(const wxCStrData
& sz
, size_t nStart
= 0) const
3195 { return find_last_of(sz
.AsString(), nStart
); }
3196 size_t find_last_of(const wxCharBuffer
& sz
, size_t nStart
= 0) const
3197 { return find_last_of(sz
.data(), nStart
); }
3198 size_t find_last_of(const wxWCharBuffer
& sz
, size_t nStart
= 0) const
3199 { return find_last_of(sz
.data(), nStart
); }
3200 size_t find_last_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
3201 { return find_last_of(sz
.AsWChar(), nStart
, n
); }
3202 size_t find_last_of(const wxCharBuffer
& sz
, size_t nStart
, size_t n
) const
3203 { return find_last_of(sz
.data(), nStart
, n
); }
3204 size_t find_last_of(const wxWCharBuffer
& sz
, size_t nStart
, size_t n
) const
3205 { return find_last_of(sz
.data(), nStart
, n
); }
3207 size_t find_first_not_of(const wxCStrData
& sz
, size_t nStart
= 0) const
3208 { return find_first_not_of(sz
.AsString(), nStart
); }
3209 size_t find_first_not_of(const wxCharBuffer
& sz
, size_t nStart
= 0) const
3210 { return find_first_not_of(sz
.data(), nStart
); }
3211 size_t find_first_not_of(const wxWCharBuffer
& sz
, size_t nStart
= 0) const
3212 { return find_first_not_of(sz
.data(), nStart
); }
3213 size_t find_first_not_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
3214 { return find_first_not_of(sz
.AsWChar(), nStart
, n
); }
3215 size_t find_first_not_of(const wxCharBuffer
& sz
, size_t nStart
, size_t n
) const
3216 { return find_first_not_of(sz
.data(), nStart
, n
); }
3217 size_t find_first_not_of(const wxWCharBuffer
& sz
, size_t nStart
, size_t n
) const
3218 { return find_first_not_of(sz
.data(), nStart
, n
); }
3220 size_t find_last_not_of(const wxCStrData
& sz
, size_t nStart
= 0) const
3221 { return find_last_not_of(sz
.AsString(), nStart
); }
3222 size_t find_last_not_of(const wxCharBuffer
& sz
, size_t nStart
= 0) const
3223 { return find_last_not_of(sz
.data(), nStart
); }
3224 size_t find_last_not_of(const wxWCharBuffer
& sz
, size_t nStart
= 0) const
3225 { return find_last_not_of(sz
.data(), nStart
); }
3226 size_t find_last_not_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
3227 { return find_last_not_of(sz
.AsWChar(), nStart
, n
); }
3228 size_t find_last_not_of(const wxCharBuffer
& sz
, size_t nStart
, size_t n
) const
3229 { return find_last_not_of(sz
.data(), nStart
, n
); }
3230 size_t find_last_not_of(const wxWCharBuffer
& sz
, size_t nStart
, size_t n
) const
3231 { return find_last_not_of(sz
.data(), nStart
, n
); }
3234 wxString
& operator+=(const wxString
& s
)
3236 wxSTRING_INVALIDATE_CACHED_LENGTH();
3241 // string += C string
3242 wxString
& operator+=(const char *psz
)
3244 wxSTRING_INVALIDATE_CACHED_LENGTH();
3246 m_impl
+= ImplStr(psz
);
3249 wxString
& operator+=(const wchar_t *pwz
)
3251 wxSTRING_INVALIDATE_CACHED_LENGTH();
3253 m_impl
+= ImplStr(pwz
);
3256 wxString
& operator+=(const wxCStrData
& s
)
3258 wxSTRING_INVALIDATE_CACHED_LENGTH();
3260 m_impl
+= s
.AsString().m_impl
;
3263 wxString
& operator+=(const wxCharBuffer
& s
)
3264 { return operator+=(s
.data()); }
3265 wxString
& operator+=(const wxWCharBuffer
& s
)
3266 { return operator+=(s
.data()); }
3268 wxString
& operator+=(wxUniChar ch
)
3270 wxSTRING_UPDATE_CACHED_LENGTH(1);
3272 #if wxUSE_UNICODE_UTF8
3273 if ( !ch
.IsAscii() )
3274 m_impl
+= wxStringOperations::EncodeChar(ch
);
3277 m_impl
+= (wxStringCharType
)ch
;
3280 wxString
& operator+=(wxUniCharRef ch
) { return *this += wxUniChar(ch
); }
3281 wxString
& operator+=(int ch
) { return *this += wxUniChar(ch
); }
3282 wxString
& operator+=(char ch
) { return *this += wxUniChar(ch
); }
3283 wxString
& operator+=(unsigned char ch
) { return *this += wxUniChar(ch
); }
3284 wxString
& operator+=(wchar_t ch
) { return *this += wxUniChar(ch
); }
3287 #if !wxUSE_STL_BASED_WXSTRING
3288 // helpers for wxStringBuffer and wxStringBufferLength
3289 wxStringCharType
*DoGetWriteBuf(size_t nLen
)
3291 return m_impl
.DoGetWriteBuf(nLen
);
3294 void DoUngetWriteBuf()
3296 wxSTRING_INVALIDATE_CACHE();
3298 m_impl
.DoUngetWriteBuf();
3301 void DoUngetWriteBuf(size_t nLen
)
3303 wxSTRING_SET_CACHED_LENGTH(nLen
);
3305 m_impl
.DoUngetWriteBuf(nLen
);
3307 #endif // !wxUSE_STL_BASED_WXSTRING
3309 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
3310 #if !wxUSE_UTF8_LOCALE_ONLY
3311 int DoPrintfWchar(const wxChar
*format
, ...);
3312 static wxString
DoFormatWchar(const wxChar
*format
, ...);
3314 #if wxUSE_UNICODE_UTF8
3315 int DoPrintfUtf8(const char *format
, ...);
3316 static wxString
DoFormatUtf8(const char *format
, ...);
3320 #if !wxUSE_STL_BASED_WXSTRING
3321 // check string's data validity
3322 bool IsValid() const { return m_impl
.GetStringData()->IsValid(); }
3326 wxStringImpl m_impl
;
3328 // buffers for compatibility conversion from (char*)c_str() and
3329 // (wchar_t*)c_str():
3330 // FIXME-UTF8: bechmark various approaches to keeping compatibility buffers
3331 template<typename T
>
3332 struct ConvertedBuffer
3334 ConvertedBuffer() : m_buf(NULL
) {}
3338 operator T
*() const { return m_buf
; }
3340 ConvertedBuffer
& operator=(T
*str
)
3349 #if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
3350 ConvertedBuffer
<char> m_convertedToChar
;
3352 #if !wxUSE_UNICODE_WCHAR
3353 ConvertedBuffer
<wchar_t> m_convertedToWChar
;
3356 #if wxUSE_UNICODE_UTF8
3357 // FIXME-UTF8: (try to) move this elsewhere (TLS) or solve differently
3358 // assigning to character pointer to by wxString::interator may
3359 // change the underlying wxStringImpl iterator, so we have to
3360 // keep track of all iterators and update them as necessary:
3361 struct wxStringIteratorNodeHead
3363 wxStringIteratorNodeHead() : ptr(NULL
) {}
3364 wxStringIteratorNode
*ptr
;
3366 // copying is disallowed as it would result in more than one pointer into
3367 // the same linked list
3368 DECLARE_NO_COPY_CLASS(wxStringIteratorNodeHead
)
3371 wxStringIteratorNodeHead m_iterators
;
3373 friend class WXDLLIMPEXP_FWD_BASE wxStringIteratorNode
;
3374 friend class WXDLLIMPEXP_FWD_BASE wxUniCharRef
;
3375 #endif // wxUSE_UNICODE_UTF8
3377 friend class WXDLLIMPEXP_FWD_BASE wxCStrData
;
3378 friend class wxStringInternalBuffer
;
3379 friend class wxStringInternalBufferLength
;
3382 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
3383 #pragma warning (default:4275)
3386 // string iterator operators that satisfy STL Random Access Iterator
3388 inline wxString::iterator
operator+(ptrdiff_t n
, wxString::iterator i
)
3390 inline wxString::const_iterator
operator+(ptrdiff_t n
, wxString::const_iterator i
)
3392 inline wxString::reverse_iterator
operator+(ptrdiff_t n
, wxString::reverse_iterator i
)
3394 inline wxString::const_reverse_iterator
operator+(ptrdiff_t n
, wxString::const_reverse_iterator i
)
3397 // notice that even though for many compilers the friend declarations above are
3398 // enough, from the point of view of C++ standard we must have the declarations
3399 // here as friend ones are not injected in the enclosing namespace and without
3400 // them the code fails to compile with conforming compilers such as xlC or g++4
3401 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
, const wxString
& string2
);
3402 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const char *psz
);
3403 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const wchar_t *pwz
);
3404 wxString WXDLLIMPEXP_BASE
operator+(const char *psz
, const wxString
& string
);
3405 wxString WXDLLIMPEXP_BASE
operator+(const wchar_t *pwz
, const wxString
& string
);
3407 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
3408 wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
3410 inline wxString
operator+(const wxString
& string
, wxUniCharRef ch
)
3411 { return string
+ (wxUniChar
)ch
; }
3412 inline wxString
operator+(const wxString
& string
, char ch
)
3413 { return string
+ wxUniChar(ch
); }
3414 inline wxString
operator+(const wxString
& string
, wchar_t ch
)
3415 { return string
+ wxUniChar(ch
); }
3416 inline wxString
operator+(wxUniCharRef ch
, const wxString
& string
)
3417 { return (wxUniChar
)ch
+ string
; }
3418 inline wxString
operator+(char ch
, const wxString
& string
)
3419 { return wxUniChar(ch
) + string
; }
3420 inline wxString
operator+(wchar_t ch
, const wxString
& string
)
3421 { return wxUniChar(ch
) + string
; }
3424 #define wxGetEmptyString() wxString()
3426 // ----------------------------------------------------------------------------
3427 // helper functions which couldn't be defined inline
3428 // ----------------------------------------------------------------------------
3433 #if wxUSE_UNICODE_WCHAR
3436 struct wxStringAsBufHelper
<char>
3438 static wxCharBuffer
Get(const wxString
& s
, size_t *len
)
3440 wxCharBuffer
buf(s
.mb_str());
3442 *len
= buf
? strlen(buf
) : 0;
3448 struct wxStringAsBufHelper
<wchar_t>
3450 static wxWCharBuffer
Get(const wxString
& s
, size_t *len
)
3454 return wxWCharBuffer::CreateNonOwned(s
.wx_str());
3458 #elif wxUSE_UNICODE_UTF8
3461 struct wxStringAsBufHelper
<char>
3463 static wxCharBuffer
Get(const wxString
& s
, size_t *len
)
3466 *len
= s
.utf8_length();
3467 return wxCharBuffer::CreateNonOwned(s
.wx_str());
3472 struct wxStringAsBufHelper
<wchar_t>
3474 static wxWCharBuffer
Get(const wxString
& s
, size_t *len
)
3476 wxWCharBuffer
wbuf(s
.wc_str());
3478 *len
= wxWcslen(wbuf
);
3483 #endif // Unicode build kind
3485 } // namespace wxPrivate
3487 // ----------------------------------------------------------------------------
3488 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
3489 // ----------------------------------------------------------------------------
3491 #if !wxUSE_STL_BASED_WXSTRING
3492 // string buffer for direct access to string data in their native
3494 class wxStringInternalBuffer
3497 typedef wxStringCharType CharType
;
3499 wxStringInternalBuffer(wxString
& str
, size_t lenWanted
= 1024)
3500 : m_str(str
), m_buf(NULL
)
3501 { m_buf
= m_str
.DoGetWriteBuf(lenWanted
); }
3503 ~wxStringInternalBuffer() { m_str
.DoUngetWriteBuf(); }
3505 operator wxStringCharType
*() const { return m_buf
; }
3509 wxStringCharType
*m_buf
;
3511 DECLARE_NO_COPY_CLASS(wxStringInternalBuffer
)
3514 class wxStringInternalBufferLength
3517 typedef wxStringCharType CharType
;
3519 wxStringInternalBufferLength(wxString
& str
, size_t lenWanted
= 1024)
3520 : m_str(str
), m_buf(NULL
), m_len(0), m_lenSet(false)
3522 m_buf
= m_str
.DoGetWriteBuf(lenWanted
);
3523 wxASSERT(m_buf
!= NULL
);
3526 ~wxStringInternalBufferLength()
3529 m_str
.DoUngetWriteBuf(m_len
);
3532 operator wxStringCharType
*() const { return m_buf
; }
3533 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
3537 wxStringCharType
*m_buf
;
3541 DECLARE_NO_COPY_CLASS(wxStringInternalBufferLength
)
3544 #endif // !wxUSE_STL_BASED_WXSTRING
3546 template<typename T
>
3547 class WXDLLIMPEXP_BASE wxStringTypeBufferBase
3552 wxStringTypeBufferBase(wxString
& str
, size_t lenWanted
= 1024)
3553 : m_str(str
), m_buf(lenWanted
)
3555 // for compatibility with old wxStringBuffer which provided direct
3556 // access to wxString internal buffer, initialize ourselves with the
3557 // string initial contents
3559 // FIXME-VC6: remove the ugly (CharType *)NULL and use normal
3560 // tchar_str<CharType>
3562 const wxCharTypeBuffer
<CharType
> buf(str
.tchar_str(&len
, (CharType
*)NULL
));
3565 if ( len
> lenWanted
)
3567 // in this case there is not enough space for terminating NUL,
3568 // ensure that we still put it there
3569 m_buf
.data()[lenWanted
] = 0;
3570 len
= lenWanted
- 1;
3573 memcpy(m_buf
.data(), buf
, (len
+ 1)*sizeof(CharType
));
3575 //else: conversion failed, this can happen when trying to get Unicode
3576 // string contents into a char string
3579 operator CharType
*() { return m_buf
.data(); }
3583 wxCharTypeBuffer
<CharType
> m_buf
;
3586 template<typename T
>
3587 class WXDLLIMPEXP_BASE wxStringTypeBufferLengthBase
3588 : public wxStringTypeBufferBase
<T
>
3591 wxStringTypeBufferLengthBase(wxString
& str
, size_t lenWanted
= 1024)
3592 : wxStringTypeBufferBase
<T
>(str
, lenWanted
),
3597 ~wxStringTypeBufferLengthBase()
3599 wxASSERT_MSG( this->m_lenSet
, "forgot to call SetLength()" );
3602 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
3609 template<typename T
>
3610 class wxStringTypeBuffer
: public wxStringTypeBufferBase
<T
>
3613 wxStringTypeBuffer(wxString
& str
, size_t lenWanted
= 1024)
3614 : wxStringTypeBufferBase
<T
>(str
, lenWanted
)
3617 ~wxStringTypeBuffer()
3619 this->m_str
.assign(this->m_buf
.data());
3622 DECLARE_NO_COPY_CLASS(wxStringTypeBuffer
)
3625 template<typename T
>
3626 class wxStringTypeBufferLength
: public wxStringTypeBufferLengthBase
<T
>
3629 wxStringTypeBufferLength(wxString
& str
, size_t lenWanted
= 1024)
3630 : wxStringTypeBufferLengthBase
<T
>(str
, lenWanted
)
3633 ~wxStringTypeBufferLength()
3635 this->m_str
.assign(this->m_buf
.data(), this->m_len
);
3638 DECLARE_NO_COPY_CLASS(wxStringTypeBufferLength
)
3641 #if wxUSE_STL_BASED_WXSTRING
3643 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase
<wxStringCharType
> )
3645 class wxStringInternalBuffer
: public wxStringTypeBufferBase
<wxStringCharType
>
3648 wxStringInternalBuffer(wxString
& str
, size_t lenWanted
= 1024)
3649 : wxStringTypeBufferBase
<wxStringCharType
>(str
, lenWanted
) {}
3650 ~wxStringInternalBuffer()
3651 { m_str
.m_impl
.assign(m_buf
.data()); }
3653 DECLARE_NO_COPY_CLASS(wxStringInternalBuffer
)
3656 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE(
3657 wxStringTypeBufferLengthBase
<wxStringCharType
> )
3659 class wxStringInternalBufferLength
3660 : public wxStringTypeBufferLengthBase
<wxStringCharType
>
3663 wxStringInternalBufferLength(wxString
& str
, size_t lenWanted
= 1024)
3664 : wxStringTypeBufferLengthBase
<wxStringCharType
>(str
, lenWanted
) {}
3666 ~wxStringInternalBufferLength()
3668 m_str
.m_impl
.assign(m_buf
.data(), m_len
);
3671 DECLARE_NO_COPY_CLASS(wxStringInternalBufferLength
)
3674 #endif // wxUSE_STL_BASED_WXSTRING
3677 #if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
3678 typedef wxStringTypeBuffer
<wxChar
> wxStringBuffer
;
3679 typedef wxStringTypeBufferLength
<wxChar
> wxStringBufferLength
;
3680 #else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
3681 typedef wxStringInternalBuffer wxStringBuffer
;
3682 typedef wxStringInternalBufferLength wxStringBufferLength
;
3683 #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
3685 #if wxUSE_UNICODE_UTF8
3686 typedef wxStringInternalBuffer wxUTF8StringBuffer
;
3687 typedef wxStringInternalBufferLength wxUTF8StringBufferLength
;
3688 #elif wxUSE_UNICODE_WCHAR
3690 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase
<char> )
3692 class WXDLLIMPEXP_BASE wxUTF8StringBuffer
: public wxStringTypeBufferBase
<char>
3695 wxUTF8StringBuffer(wxString
& str
, size_t lenWanted
= 1024)
3696 : wxStringTypeBufferBase
<char>(str
, lenWanted
) {}
3697 ~wxUTF8StringBuffer();
3699 DECLARE_NO_COPY_CLASS(wxUTF8StringBuffer
)
3702 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferLengthBase
<char> )
3704 class WXDLLIMPEXP_BASE wxUTF8StringBufferLength
3705 : public wxStringTypeBufferLengthBase
<char>
3708 wxUTF8StringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
3709 : wxStringTypeBufferLengthBase
<char>(str
, lenWanted
) {}
3710 ~wxUTF8StringBufferLength();
3712 DECLARE_NO_COPY_CLASS(wxUTF8StringBufferLength
)
3714 #endif // wxUSE_UNICODE_UTF8/wxUSE_UNICODE_WCHAR
3717 // ---------------------------------------------------------------------------
3718 // wxString comparison functions: operator versions are always case sensitive
3719 // ---------------------------------------------------------------------------
3721 #define wxCMP_WXCHAR_STRING(p, s, op) 0 op s.Cmp(p)
3723 wxDEFINE_ALL_COMPARISONS(const wxChar
*, const wxString
&, wxCMP_WXCHAR_STRING
)
3725 #undef wxCMP_WXCHAR_STRING
3727 inline bool operator==(const wxString
& s1
, const wxString
& s2
)
3728 { return s1
.IsSameAs(s2
); }
3729 inline bool operator!=(const wxString
& s1
, const wxString
& s2
)
3730 { return !s1
.IsSameAs(s2
); }
3731 inline bool operator< (const wxString
& s1
, const wxString
& s2
)
3732 { return s1
.Cmp(s2
) < 0; }
3733 inline bool operator> (const wxString
& s1
, const wxString
& s2
)
3734 { return s1
.Cmp(s2
) > 0; }
3735 inline bool operator<=(const wxString
& s1
, const wxString
& s2
)
3736 { return s1
.Cmp(s2
) <= 0; }
3737 inline bool operator>=(const wxString
& s1
, const wxString
& s2
)
3738 { return s1
.Cmp(s2
) >= 0; }
3740 inline bool operator==(const wxString
& s1
, const wxCStrData
& s2
)
3741 { return s1
== s2
.AsString(); }
3742 inline bool operator==(const wxCStrData
& s1
, const wxString
& s2
)
3743 { return s1
.AsString() == s2
; }
3744 inline bool operator!=(const wxString
& s1
, const wxCStrData
& s2
)
3745 { return s1
!= s2
.AsString(); }
3746 inline bool operator!=(const wxCStrData
& s1
, const wxString
& s2
)
3747 { return s1
.AsString() != s2
; }
3749 inline bool operator==(const wxString
& s1
, const wxWCharBuffer
& s2
)
3750 { return (s1
.Cmp((const wchar_t *)s2
) == 0); }
3751 inline bool operator==(const wxWCharBuffer
& s1
, const wxString
& s2
)
3752 { return (s2
.Cmp((const wchar_t *)s1
) == 0); }
3753 inline bool operator!=(const wxString
& s1
, const wxWCharBuffer
& s2
)
3754 { return (s1
.Cmp((const wchar_t *)s2
) != 0); }
3755 inline bool operator!=(const wxWCharBuffer
& s1
, const wxString
& s2
)
3756 { return (s2
.Cmp((const wchar_t *)s1
) != 0); }
3758 inline bool operator==(const wxString
& s1
, const wxCharBuffer
& s2
)
3759 { return (s1
.Cmp((const char *)s2
) == 0); }
3760 inline bool operator==(const wxCharBuffer
& s1
, const wxString
& s2
)
3761 { return (s2
.Cmp((const char *)s1
) == 0); }
3762 inline bool operator!=(const wxString
& s1
, const wxCharBuffer
& s2
)
3763 { return (s1
.Cmp((const char *)s2
) != 0); }
3764 inline bool operator!=(const wxCharBuffer
& s1
, const wxString
& s2
)
3765 { return (s2
.Cmp((const char *)s1
) != 0); }
3767 inline wxString
operator+(const wxString
& string
, const wxWCharBuffer
& buf
)
3768 { return string
+ (const wchar_t *)buf
; }
3769 inline wxString
operator+(const wxWCharBuffer
& buf
, const wxString
& string
)
3770 { return (const wchar_t *)buf
+ string
; }
3772 inline wxString
operator+(const wxString
& string
, const wxCharBuffer
& buf
)
3773 { return string
+ (const char *)buf
; }
3774 inline wxString
operator+(const wxCharBuffer
& buf
, const wxString
& string
)
3775 { return (const char *)buf
+ string
; }
3777 // comparison with char
3778 inline bool operator==(const wxUniChar
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
3779 inline bool operator==(const wxUniCharRef
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
3780 inline bool operator==(char c
, const wxString
& s
) { return s
.IsSameAs(c
); }
3781 inline bool operator==(wchar_t c
, const wxString
& s
) { return s
.IsSameAs(c
); }
3782 inline bool operator==(int c
, const wxString
& s
) { return s
.IsSameAs(c
); }
3783 inline bool operator==(const wxString
& s
, const wxUniChar
& c
) { return s
.IsSameAs(c
); }
3784 inline bool operator==(const wxString
& s
, const wxUniCharRef
& c
) { return s
.IsSameAs(c
); }
3785 inline bool operator==(const wxString
& s
, char c
) { return s
.IsSameAs(c
); }
3786 inline bool operator==(const wxString
& s
, wchar_t c
) { return s
.IsSameAs(c
); }
3787 inline bool operator!=(const wxUniChar
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
3788 inline bool operator!=(const wxUniCharRef
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
3789 inline bool operator!=(char c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
3790 inline bool operator!=(wchar_t c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
3791 inline bool operator!=(int c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
3792 inline bool operator!=(const wxString
& s
, const wxUniChar
& c
) { return !s
.IsSameAs(c
); }
3793 inline bool operator!=(const wxString
& s
, const wxUniCharRef
& c
) { return !s
.IsSameAs(c
); }
3794 inline bool operator!=(const wxString
& s
, char c
) { return !s
.IsSameAs(c
); }
3795 inline bool operator!=(const wxString
& s
, wchar_t c
) { return !s
.IsSameAs(c
); }
3797 // comparison with C string in Unicode build
3800 #define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s
3802 wxDEFINE_ALL_COMPARISONS(const char *, const wxString
&, wxCMP_CHAR_STRING
)
3804 #undef wxCMP_CHAR_STRING
3806 #endif // wxUSE_UNICODE
3808 // we also need to provide the operators for comparison with wxCStrData to
3809 // resolve ambiguity between operator(const wxChar *,const wxString &) and
3810 // operator(const wxChar *, const wxChar *) for "p == s.c_str()"
3812 // notice that these are (shallow) pointer comparisons, not (deep) string ones
3813 #define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar()
3814 #define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar()
3816 wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData
&, wxCMP_WCHAR_CSTRDATA
)
3817 wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData
&, wxCMP_CHAR_CSTRDATA
)
3819 #undef wxCMP_CHAR_CSTRDATA
3820 #undef wxCMP_WCHAR_CSTRDATA
3822 // ---------------------------------------------------------------------------
3823 // Implementation only from here until the end of file
3824 // ---------------------------------------------------------------------------
3826 #if wxUSE_STD_IOSTREAM
3828 #include "wx/iosfwrap.h"
3830 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxString
&);
3831 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxCStrData
&);
3832 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxCharBuffer
&);
3833 #ifndef __BORLANDC__
3834 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxWCharBuffer
&);
3837 #if wxUSE_UNICODE && defined(HAVE_WOSTREAM)
3839 WXDLLIMPEXP_BASE wxSTD wostream
& operator<<(wxSTD wostream
&, const wxString
&);
3840 WXDLLIMPEXP_BASE wxSTD wostream
& operator<<(wxSTD wostream
&, const wxCStrData
&);
3841 WXDLLIMPEXP_BASE wxSTD wostream
& operator<<(wxSTD wostream
&, const wxWCharBuffer
&);
3843 #endif // wxUSE_UNICODE && defined(HAVE_WOSTREAM)
3845 #endif // wxUSE_STD_IOSTREAM
3847 // ---------------------------------------------------------------------------
3848 // wxCStrData implementation
3849 // ---------------------------------------------------------------------------
3851 inline wxCStrData::wxCStrData(char *buf
)
3852 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
3853 inline wxCStrData::wxCStrData(wchar_t *buf
)
3854 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
3856 inline wxCStrData::wxCStrData(const wxCStrData
& data
)
3857 : m_str(data
.m_owned
? new wxString(*data
.m_str
) : data
.m_str
),
3858 m_offset(data
.m_offset
),
3859 m_owned(data
.m_owned
)
3863 inline wxCStrData::~wxCStrData()
3866 delete wx_const_cast(wxString
*, m_str
); // cast to silence warnings
3869 // simple cases for AsChar() and AsWChar(), the complicated ones are
3871 #if wxUSE_UNICODE_WCHAR
3872 inline const wchar_t* wxCStrData::AsWChar() const
3874 return m_str
->wx_str() + m_offset
;
3876 #endif // wxUSE_UNICODE_WCHAR
3879 inline const char* wxCStrData::AsChar() const
3881 return m_str
->wx_str() + m_offset
;
3883 #endif // !wxUSE_UNICODE
3885 #if wxUSE_UTF8_LOCALE_ONLY
3886 inline const char* wxCStrData::AsChar() const
3888 return wxStringOperations::AddToIter(m_str
->wx_str(), m_offset
);
3890 #endif // wxUSE_UTF8_LOCALE_ONLY
3892 inline const wxCharBuffer
wxCStrData::AsCharBuf() const
3895 return wxCharBuffer::CreateNonOwned(AsChar());
3897 return AsString().mb_str();
3901 inline const wxWCharBuffer
wxCStrData::AsWCharBuf() const
3903 #if wxUSE_UNICODE_WCHAR
3904 return wxWCharBuffer::CreateNonOwned(AsWChar());
3906 return AsString().wc_str();
3910 inline wxString
wxCStrData::AsString() const
3912 if ( m_offset
== 0 )
3915 return m_str
->Mid(m_offset
);
3918 inline const wxStringCharType
*wxCStrData::AsInternal() const
3920 #if wxUSE_UNICODE_UTF8
3921 return wxStringOperations::AddToIter(m_str
->wx_str(), m_offset
);
3923 return m_str
->wx_str() + m_offset
;
3927 inline wxUniChar
wxCStrData::operator*() const
3929 if ( m_str
->empty() )
3930 return wxUniChar(_T('\0'));
3932 return (*m_str
)[m_offset
];
3935 inline wxUniChar
wxCStrData::operator[](size_t n
) const
3937 // NB: we intentionally use operator[] and not at() here because the former
3938 // works for the terminating NUL while the latter does not
3939 return (*m_str
)[m_offset
+ n
];
3942 // ----------------------------------------------------------------------------
3943 // more wxCStrData operators
3944 // ----------------------------------------------------------------------------
3946 // we need to define those to allow "size_t pos = p - s.c_str()" where p is
3947 // some pointer into the string
3948 inline size_t operator-(const char *p
, const wxCStrData
& cs
)
3950 return p
- cs
.AsChar();
3953 inline size_t operator-(const wchar_t *p
, const wxCStrData
& cs
)
3955 return p
- cs
.AsWChar();
3958 // ----------------------------------------------------------------------------
3959 // implementation of wx[W]CharBuffer inline methods using wxCStrData
3960 // ----------------------------------------------------------------------------
3962 // FIXME-UTF8: move this to buffer.h
3963 inline wxCharBuffer::wxCharBuffer(const wxCStrData
& cstr
)
3964 : wxCharTypeBufferBase(cstr
.AsCharBuf())
3968 inline wxWCharBuffer::wxWCharBuffer(const wxCStrData
& cstr
)
3969 : wxCharTypeBufferBase(cstr
.AsWCharBuf())
3973 #if wxUSE_UNICODE_UTF8
3974 // ----------------------------------------------------------------------------
3975 // implementation of wxStringIteratorNode inline methods
3976 // ----------------------------------------------------------------------------
3978 void wxStringIteratorNode::DoSet(const wxString
*str
,
3979 wxStringImpl::const_iterator
*citer
,
3980 wxStringImpl::iterator
*iter
)
3988 m_next
= str
->m_iterators
.ptr
;
3989 wx_const_cast(wxString
*, m_str
)->m_iterators
.ptr
= this;
3991 m_next
->m_prev
= this;
3999 void wxStringIteratorNode::clear()
4002 m_next
->m_prev
= m_prev
;
4004 m_prev
->m_next
= m_next
;
4005 else if ( m_str
) // first in the list
4006 wx_const_cast(wxString
*, m_str
)->m_iterators
.ptr
= m_next
;
4008 m_next
= m_prev
= NULL
;
4013 #endif // wxUSE_UNICODE_UTF8
4015 #if WXWIN_COMPATIBILITY_2_8
4016 // lot of code out there doesn't explicitly include wx/crt.h, but uses
4017 // CRT wrappers that are now declared in wx/wxcrt.h and wx/wxcrtvararg.h,
4018 // so let's include this header now that wxString is defined and it's safe
4023 #endif // _WX_WXSTRING_H_