1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxString class
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
13 Efficient string class [more or less] compatible with MFC CString,
14 wxWidgets version 1 wxString and std::string and some handy functions
15 missing from string.h.
18 #ifndef _WX_WXSTRING_H__
19 #define _WX_WXSTRING_H__
21 // ----------------------------------------------------------------------------
23 // ----------------------------------------------------------------------------
25 #include "wx/defs.h" // everybody should include this
28 #if defined(__WXMAC__) || defined(__VISAGECPP__)
32 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
33 // problem in VACPP V4 with including stdlib.h multiple times
34 // strconv includes it anyway
47 #ifdef HAVE_STRCASECMP_IN_STRINGS_H
48 #include <strings.h> // for strcasecmp()
49 #endif // HAVE_STRCASECMP_IN_STRINGS_H
50 #endif // ! __WXPALMOS5__
52 #include "wx/wxcrtbase.h" // for wxChar, wxStrlen() etc.
53 #include "wx/strvararg.h"
54 #include "wx/buffer.h" // for wxCharBuffer
55 #include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes
56 #include "wx/stringimpl.h"
57 #include "wx/stringops.h"
58 #include "wx/unichar.h"
60 // by default we cache the mapping of the positions in UTF-8 string to the byte
61 // offset as this results in noticeable performance improvements for loops over
62 // strings using indices; comment out this line to disable this
64 // notice that this optimization is well worth using even in debug builds as it
65 // changes asymptotic complexity of algorithms using indices to iterate over
66 // wxString back to expected linear from quadratic
68 // also notice that wxTLS_TYPE() (__declspec(thread) in this case) is unsafe to
69 // use in DLL build under pre-Vista Windows so we disable this code for now, if
70 // anybody really needs to use UTF-8 build under Windows with this optimization
71 // it would have to be re-tested and probably corrected
72 // CS: under OSX release builds the string destructor/cache cleanup sometimes
73 // crashes, disable until we find the true reason or a better workaround
74 #if wxUSE_UNICODE_UTF8 && !defined(__WXMSW__) && !defined(__WXOSX__)
75 #define wxUSE_STRING_POS_CACHE 1
77 #define wxUSE_STRING_POS_CACHE 0
80 #if wxUSE_STRING_POS_CACHE
83 // change this 0 to 1 to enable additional (very expensive) asserts
84 // verifying that string caching logic works as expected
86 #define wxSTRING_CACHE_ASSERT(cond) wxASSERT(cond)
88 #define wxSTRING_CACHE_ASSERT(cond)
90 #endif // wxUSE_STRING_POS_CACHE
92 class WXDLLIMPEXP_FWD_BASE wxString
;
94 // unless this symbol is predefined to disable the compatibility functions, do
96 #ifndef WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
97 #define WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER 1
102 template <typename T
> struct wxStringAsBufHelper
;
105 // ---------------------------------------------------------------------------
107 // ---------------------------------------------------------------------------
109 // casts [unfortunately!] needed to call some broken functions which require
110 // "char *" instead of "const char *"
111 #define WXSTRINGCAST (wxChar *)(const wxChar *)
112 #define wxCSTRINGCAST (wxChar *)(const wxChar *)
113 #define wxMBSTRINGCAST (char *)(const char *)
114 #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *)
116 // ----------------------------------------------------------------------------
118 // ----------------------------------------------------------------------------
120 #if WXWIN_COMPATIBILITY_2_6
122 // deprecated in favour of wxString::npos, don't use in new code
124 // maximum possible length for a string means "take all string" everywhere
125 #define wxSTRING_MAXLEN wxString::npos
127 #endif // WXWIN_COMPATIBILITY_2_6
129 // ---------------------------------------------------------------------------
130 // global functions complementing standard C string library replacements for
131 // strlen() and portable strcasecmp()
132 //---------------------------------------------------------------------------
134 #if WXWIN_COMPATIBILITY_2_8
135 // Use wxXXX() functions from wxcrt.h instead! These functions are for
136 // backwards compatibility only.
138 // checks whether the passed in pointer is NULL and if the string is empty
139 wxDEPRECATED( inline bool IsEmpty(const char *p
) );
140 inline bool IsEmpty(const char *p
) { return (!p
|| !*p
); }
142 // safe version of strlen() (returns 0 if passed NULL pointer)
143 wxDEPRECATED( inline size_t Strlen(const char *psz
) );
144 inline size_t Strlen(const char *psz
)
145 { return psz
? strlen(psz
) : 0; }
147 // portable strcasecmp/_stricmp
148 wxDEPRECATED( inline int Stricmp(const char *psz1
, const char *psz2
) );
149 inline int Stricmp(const char *psz1
, const char *psz2
)
151 #if defined(__VISUALC__) && defined(__WXWINCE__)
152 register char c1
, c2
;
154 c1
= tolower(*psz1
++);
155 c2
= tolower(*psz2
++);
156 } while ( c1
&& (c1
== c2
) );
159 #elif defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) )
160 return _stricmp(psz1
, psz2
);
161 #elif defined(__SC__)
162 return _stricmp(psz1
, psz2
);
163 #elif defined(__BORLANDC__)
164 return stricmp(psz1
, psz2
);
165 #elif defined(__WATCOMC__)
166 return stricmp(psz1
, psz2
);
167 #elif defined(__DJGPP__)
168 return stricmp(psz1
, psz2
);
169 #elif defined(__EMX__)
170 return stricmp(psz1
, psz2
);
171 #elif defined(__WXPM__)
172 return stricmp(psz1
, psz2
);
173 #elif defined(__WXPALMOS__) || \
174 defined(HAVE_STRCASECMP_IN_STRING_H) || \
175 defined(HAVE_STRCASECMP_IN_STRINGS_H) || \
176 defined(__GNUWIN32__)
177 return strcasecmp(psz1
, psz2
);
178 #elif defined(__MWERKS__) && !defined(__INTEL__)
179 register char c1
, c2
;
181 c1
= tolower(*psz1
++);
182 c2
= tolower(*psz2
++);
183 } while ( c1
&& (c1
== c2
) );
187 // almost all compilers/libraries provide this function (unfortunately under
188 // different names), that's why we don't implement our own which will surely
189 // be more efficient than this code (uncomment to use):
191 register char c1, c2;
193 c1 = tolower(*psz1++);
194 c2 = tolower(*psz2++);
195 } while ( c1 && (c1 == c2) );
200 #error "Please define string case-insensitive compare for your OS/compiler"
201 #endif // OS/compiler
204 #endif // WXWIN_COMPATIBILITY_2_8
206 // ----------------------------------------------------------------------------
208 // ----------------------------------------------------------------------------
210 // Lightweight object returned by wxString::c_str() and implicitly convertible
211 // to either const char* or const wchar_t*.
215 // Ctors; for internal use by wxString and wxCStrData only
216 wxCStrData(const wxString
*str
, size_t offset
= 0, bool owned
= false)
217 : m_str(str
), m_offset(offset
), m_owned(owned
) {}
220 // Ctor constructs the object from char literal; they are needed to make
221 // operator?: compile and they intentionally take char*, not const char*
222 inline wxCStrData(char *buf
);
223 inline wxCStrData(wchar_t *buf
);
224 inline wxCStrData(const wxCStrData
& data
);
226 inline ~wxCStrData();
228 // AsWChar() and AsChar() can't be defined here as they use wxString and so
229 // must come after it and because of this won't be inlined when called from
230 // wxString methods (without a lot of work to extract these wxString methods
231 // from inside the class itself). But we still define them being inline
232 // below to let compiler inline them from elsewhere. And because of this we
233 // must declare them as inline here because otherwise some compilers give
234 // warnings about them, e.g. mingw32 3.4.5 warns about "<symbol> defined
235 // locally after being referenced with dllimport linkage" while IRIX
236 // mipsPro 7.4 warns about "function declared inline after being called".
237 inline const wchar_t* AsWChar() const;
238 operator const wchar_t*() const { return AsWChar(); }
240 inline const char* AsChar() const;
241 const unsigned char* AsUnsignedChar() const
242 { return (const unsigned char *) AsChar(); }
243 operator const char*() const { return AsChar(); }
244 operator const unsigned char*() const { return AsUnsignedChar(); }
246 operator const void*() const { return AsChar(); }
248 // returns buffers that are valid as long as the associated wxString exists
249 const wxScopedCharBuffer
AsCharBuf() const
251 return wxScopedCharBuffer::CreateNonOwned(AsChar());
254 const wxScopedWCharBuffer
AsWCharBuf() const
256 return wxScopedWCharBuffer::CreateNonOwned(AsWChar());
259 inline wxString
AsString() const;
261 // returns the value as C string in internal representation (equivalent
262 // to AsString().wx_str(), but more efficient)
263 const wxStringCharType
*AsInternal() const;
265 // allow expressions like "c_str()[0]":
266 inline wxUniChar
operator[](size_t n
) const;
267 wxUniChar
operator[](int n
) const { return operator[](size_t(n
)); }
268 wxUniChar
operator[](long n
) const { return operator[](size_t(n
)); }
269 #ifndef wxSIZE_T_IS_UINT
270 wxUniChar
operator[](unsigned int n
) const { return operator[](size_t(n
)); }
271 #endif // size_t != unsigned int
273 // These operators are needed to emulate the pointer semantics of c_str():
274 // expressions like "wxChar *p = str.c_str() + 1;" should continue to work
275 // (we need both versions to resolve ambiguities). Note that this means
276 // the 'n' value is interpreted as addition to char*/wchar_t* pointer, it
277 // is *not* number of Unicode characters in wxString.
278 wxCStrData
operator+(int n
) const
279 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
280 wxCStrData
operator+(long n
) const
281 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
282 wxCStrData
operator+(size_t n
) const
283 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
285 // and these for "str.c_str() + (p2 - p1)" (it also works for any integer
286 // expression but it must be ptrdiff_t and not e.g. int to work in this
288 wxCStrData
operator-(ptrdiff_t n
) const
290 wxASSERT_MSG( n
<= (ptrdiff_t)m_offset
,
291 wxT("attempt to construct address before the beginning of the string") );
292 return wxCStrData(m_str
, m_offset
- n
, m_owned
);
295 // this operator is needed to make expressions like "*c_str()" or
296 // "*(c_str() + 2)" work
297 inline wxUniChar
operator*() const;
300 // the wxString this object was returned for
301 const wxString
*m_str
;
302 // Offset into c_str() return value. Note that this is *not* offset in
303 // m_str in Unicode characters. Instead, it is index into the
304 // char*/wchar_t* buffer returned by c_str(). It's interpretation depends
305 // on how is the wxCStrData instance used: if it is eventually cast to
306 // const char*, m_offset will be in bytes form string's start; if it is
307 // cast to const wchar_t*, it will be in wchar_t values.
309 // should m_str be deleted, i.e. is it owned by us?
312 friend class WXDLLIMPEXP_FWD_BASE wxString
;
315 // ----------------------------------------------------------------------------
316 // wxStringPrintfMixin
317 // ---------------------------------------------------------------------------
319 // NB: VC6 has a bug that causes linker errors if you have template methods
320 // in a class using __declspec(dllimport). The solution is to split such
321 // class into two classes, one that contains the template methods and does
322 // *not* use WXDLLIMPEXP_BASE and another class that contains the rest
323 // (with DLL linkage).
325 // We only do this for VC6 here, because the code is less efficient
326 // (Printf() has to use dynamic_cast<>) and because OpenWatcom compiler
327 // cannot compile this code.
329 #if defined(__VISUALC__) && __VISUALC__ < 1300
330 #define wxNEEDS_WXSTRING_PRINTF_MIXIN
333 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
334 // this class contains implementation of wxString's vararg methods, it's
335 // exported from wxBase DLL
336 class WXDLLIMPEXP_BASE wxStringPrintfMixinBase
339 wxStringPrintfMixinBase() {}
341 #if !wxUSE_UTF8_LOCALE_ONLY
342 int DoPrintfWchar(const wxChar
*format
, ...);
343 static wxString
DoFormatWchar(const wxChar
*format
, ...);
345 #if wxUSE_UNICODE_UTF8
346 int DoPrintfUtf8(const char *format
, ...);
347 static wxString
DoFormatUtf8(const char *format
, ...);
351 // this class contains template wrappers for wxString's vararg methods, it's
352 // intentionally *not* exported from the DLL in order to fix the VC6 bug
354 class wxStringPrintfMixin
: public wxStringPrintfMixinBase
357 // to further complicate things, we can't return wxString from
358 // wxStringPrintfMixin::Format() because wxString is not yet declared at
359 // this point; the solution is to use this fake type trait template - this
360 // way the compiler won't know the return type until Format() is used
361 // (this doesn't compile with Watcom, but VC6 compiles it just fine):
362 template<typename T
> struct StringReturnType
364 typedef wxString type
;
368 // these are duplicated wxString methods, they're also declared below
369 // if !wxNEEDS_WXSTRING_PRINTF_MIXIN:
371 // static wxString Format(const wString& format, ...) WX_ATTRIBUTE_PRINTF_1;
372 WX_DEFINE_VARARG_FUNC_SANS_N0(static typename StringReturnType
<T1
>::type
,
373 Format
, 1, (const wxFormatString
&),
374 DoFormatWchar
, DoFormatUtf8
)
375 // We have to implement the version without template arguments manually
376 // because of the StringReturnType<> hack, although WX_DEFINE_VARARG_FUNC
377 // normally does it itself. It has to be a template so that we can use
378 // the hack, even though there's no real template parameter. We can't move
379 // it to wxStrig, because it would shadow these versions of Format() then.
381 inline static typename StringReturnType
<T
>::type
384 // NB: this doesn't compile if T is not (some form of) a string;
385 // this makes Format's prototype equivalent to
386 // Format(const wxFormatString& fmt)
387 return DoFormatWchar(wxFormatString(fmt
));
390 // int Printf(const wxString& format, ...);
391 WX_DEFINE_VARARG_FUNC(int, Printf
, 1, (const wxFormatString
&),
392 DoPrintfWchar
, DoPrintfUtf8
)
393 // int sprintf(const wxString& format, ...) WX_ATTRIBUTE_PRINTF_2;
394 WX_DEFINE_VARARG_FUNC(int, sprintf
, 1, (const wxFormatString
&),
395 DoPrintfWchar
, DoPrintfUtf8
)
398 wxStringPrintfMixin() : wxStringPrintfMixinBase() {}
400 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
403 // ----------------------------------------------------------------------------
404 // wxString: string class trying to be compatible with std::string, MFC
405 // CString and wxWindows 1.x wxString all at once
406 // ---------------------------------------------------------------------------
408 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
409 // "non dll-interface class 'wxStringPrintfMixin' used as base interface
410 // for dll-interface class 'wxString'" -- this is OK in our case
411 #pragma warning (push)
412 #pragma warning (disable:4275)
415 #if wxUSE_UNICODE_UTF8
416 // see the comment near wxString::iterator for why we need this
417 class WXDLLIMPEXP_BASE wxStringIteratorNode
420 wxStringIteratorNode()
421 : m_str(NULL
), m_citer(NULL
), m_iter(NULL
), m_prev(NULL
), m_next(NULL
) {}
422 wxStringIteratorNode(const wxString
*str
,
423 wxStringImpl::const_iterator
*citer
)
424 { DoSet(str
, citer
, NULL
); }
425 wxStringIteratorNode(const wxString
*str
, wxStringImpl::iterator
*iter
)
426 { DoSet(str
, NULL
, iter
); }
427 ~wxStringIteratorNode()
430 inline void set(const wxString
*str
, wxStringImpl::const_iterator
*citer
)
431 { clear(); DoSet(str
, citer
, NULL
); }
432 inline void set(const wxString
*str
, wxStringImpl::iterator
*iter
)
433 { clear(); DoSet(str
, NULL
, iter
); }
435 const wxString
*m_str
;
436 wxStringImpl::const_iterator
*m_citer
;
437 wxStringImpl::iterator
*m_iter
;
438 wxStringIteratorNode
*m_prev
, *m_next
;
442 inline void DoSet(const wxString
*str
,
443 wxStringImpl::const_iterator
*citer
,
444 wxStringImpl::iterator
*iter
);
446 // the node belongs to a particular iterator instance, it's not copied
447 // when a copy of the iterator is made
448 wxDECLARE_NO_COPY_CLASS(wxStringIteratorNode
);
450 #endif // wxUSE_UNICODE_UTF8
452 class WXDLLIMPEXP_BASE wxString
453 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
454 : public wxStringPrintfMixin
457 // NB: special care was taken in arranging the member functions in such order
458 // that all inline functions can be effectively inlined, verify that all
459 // performance critical functions are still inlined if you change order!
461 // an 'invalid' value for string index, moved to this place due to a CW bug
462 static const size_t npos
;
465 // if we hadn't made these operators private, it would be possible to
466 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
467 // converted to char in C and we do have operator=(char)
469 // NB: we don't need other versions (short/long and unsigned) as attempt
470 // to assign another numeric type to wxString will now result in
471 // ambiguity between operator=(char) and operator=(int)
472 wxString
& operator=(int);
474 // these methods are not implemented - there is _no_ conversion from int to
475 // string, you're doing something wrong if the compiler wants to call it!
477 // try `s << i' or `s.Printf("%d", i)' instead
481 // buffer for holding temporary substring when using any of the methods
482 // that take (char*,size_t) or (wchar_t*,size_t) arguments:
484 struct SubstrBufFromType
489 SubstrBufFromType(const T
& data_
, size_t len_
)
490 : data(data_
), len(len_
)
492 wxASSERT_MSG( len
!= npos
, "must have real length" );
496 #if wxUSE_UNICODE_UTF8
497 // even char* -> char* needs conversion, from locale charset to UTF-8
498 typedef SubstrBufFromType
<wxScopedCharBuffer
> SubstrBufFromWC
;
499 typedef SubstrBufFromType
<wxScopedCharBuffer
> SubstrBufFromMB
;
500 #elif wxUSE_UNICODE_WCHAR
501 typedef SubstrBufFromType
<const wchar_t*> SubstrBufFromWC
;
502 typedef SubstrBufFromType
<wxScopedWCharBuffer
> SubstrBufFromMB
;
504 typedef SubstrBufFromType
<const char*> SubstrBufFromMB
;
505 typedef SubstrBufFromType
<wxScopedCharBuffer
> SubstrBufFromWC
;
509 // Functions implementing primitive operations on string data; wxString
510 // methods and iterators are implemented in terms of it. The differences
511 // between UTF-8 and wchar_t* representations of the string are mostly
514 #if wxUSE_UNICODE_UTF8
515 static SubstrBufFromMB
ConvertStr(const char *psz
, size_t nLength
,
516 const wxMBConv
& conv
);
517 static SubstrBufFromWC
ConvertStr(const wchar_t *pwz
, size_t nLength
,
518 const wxMBConv
& conv
);
519 #elif wxUSE_UNICODE_WCHAR
520 static SubstrBufFromMB
ConvertStr(const char *psz
, size_t nLength
,
521 const wxMBConv
& conv
);
523 static SubstrBufFromWC
ConvertStr(const wchar_t *pwz
, size_t nLength
,
524 const wxMBConv
& conv
);
527 #if !wxUSE_UNICODE_UTF8 // wxUSE_UNICODE_WCHAR or !wxUSE_UNICODE
528 // returns C string encoded as the implementation expects:
530 static const wchar_t* ImplStr(const wchar_t* str
)
531 { return str
? str
: wxT(""); }
532 static const SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
533 { return SubstrBufFromWC(str
, (str
&& n
== npos
) ? wxWcslen(str
) : n
); }
534 static wxScopedWCharBuffer
ImplStr(const char* str
,
535 const wxMBConv
& conv
= wxConvLibc
)
536 { return ConvertStr(str
, npos
, conv
).data
; }
537 static SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
538 const wxMBConv
& conv
= wxConvLibc
)
539 { return ConvertStr(str
, n
, conv
); }
541 static const char* ImplStr(const char* str
,
542 const wxMBConv
& WXUNUSED(conv
) = wxConvLibc
)
543 { return str
? str
: ""; }
544 static const SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
545 const wxMBConv
& WXUNUSED(conv
) = wxConvLibc
)
546 { return SubstrBufFromMB(str
, (str
&& n
== npos
) ? wxStrlen(str
) : n
); }
547 static wxScopedCharBuffer
ImplStr(const wchar_t* str
)
548 { return ConvertStr(str
, npos
, wxConvLibc
).data
; }
549 static SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
550 { return ConvertStr(str
, n
, wxConvLibc
); }
553 // translates position index in wxString to/from index in underlying
555 static size_t PosToImpl(size_t pos
) { return pos
; }
556 static void PosLenToImpl(size_t pos
, size_t len
,
557 size_t *implPos
, size_t *implLen
)
558 { *implPos
= pos
; *implLen
= len
; }
559 static size_t LenToImpl(size_t len
) { return len
; }
560 static size_t PosFromImpl(size_t pos
) { return pos
; }
562 // we don't want to define these as empty inline functions as it could
563 // result in noticeable (and quite unnecessary in non-UTF-8 build) slowdown
564 // in debug build where the inline functions are not effectively inlined
565 #define wxSTRING_INVALIDATE_CACHE()
566 #define wxSTRING_INVALIDATE_CACHED_LENGTH()
567 #define wxSTRING_UPDATE_CACHED_LENGTH(n)
568 #define wxSTRING_SET_CACHED_LENGTH(n)
570 #else // wxUSE_UNICODE_UTF8
572 static wxScopedCharBuffer
ImplStr(const char* str
,
573 const wxMBConv
& conv
= wxConvLibc
)
574 { return ConvertStr(str
, npos
, conv
).data
; }
575 static SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
576 const wxMBConv
& conv
= wxConvLibc
)
577 { return ConvertStr(str
, n
, conv
); }
579 static wxScopedCharBuffer
ImplStr(const wchar_t* str
)
580 { return ConvertStr(str
, npos
, wxMBConvUTF8()).data
; }
581 static SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
582 { return ConvertStr(str
, n
, wxMBConvUTF8()); }
584 #if wxUSE_STRING_POS_CACHE
585 // this is an extremely simple cache used by PosToImpl(): each cache element
586 // contains the string it applies to and the index corresponding to the last
587 // used position in this wxString in its m_impl string
589 // NB: notice that this struct (and nested Element one) must be a POD or we
590 // wouldn't be able to use a thread-local variable of this type, in
591 // particular it should have no ctor -- we rely on statics being
592 // initialized to 0 instead
599 const wxString
*str
; // the string to which this element applies
600 size_t pos
, // the cached index in this string
601 impl
, // the corresponding position in its m_impl
602 len
; // cached length or npos if unknown
604 // reset cached index to 0
605 void ResetPos() { pos
= impl
= 0; }
607 // reset position and length
608 void Reset() { ResetPos(); len
= npos
; }
611 // cache the indices mapping for the last few string used
612 Element cached
[SIZE
];
614 // the last used index
618 #ifndef wxHAS_COMPILER_TLS
619 // we must use an accessor function and not a static variable when the TLS
620 // variables support is implemented in the library (and not by the compiler)
621 // because the global s_cache variable could be not yet initialized when a
622 // ctor of another global object is executed and if that ctor uses any
623 // wxString methods, bad things happen
625 // however notice that this approach does not work when compiler TLS is used,
626 // at least not with g++ 4.1.2 under amd64 as it apparently compiles code
627 // using this accessor incorrectly when optimizations are enabled (-O2 is
628 // enough) -- luckily we don't need it then neither as static __thread
629 // variables are initialized by 0 anyhow then and so we can use the variable
631 WXEXPORT
static Cache
& GetCache()
633 static wxTLS_TYPE(Cache
) s_cache
;
635 return wxTLS_VALUE(s_cache
);
638 // this helper struct is used to ensure that GetCache() is called during
639 // static initialization time, i.e. before any threads creation, as otherwise
640 // the static s_cache construction inside GetCache() wouldn't be MT-safe
641 friend struct wxStrCacheInitializer
;
642 #else // wxHAS_COMPILER_TLS
643 static wxTLS_TYPE(Cache
) ms_cache
;
644 static Cache
& GetCache() { return wxTLS_VALUE(ms_cache
); }
645 #endif // !wxHAS_COMPILER_TLS/wxHAS_COMPILER_TLS
647 static Cache::Element
*GetCacheBegin() { return GetCache().cached
; }
648 static Cache::Element
*GetCacheEnd() { return GetCacheBegin() + Cache::SIZE
; }
649 static unsigned& LastUsedCacheElement() { return GetCache().lastUsed
; }
651 // this is used in debug builds only to provide a convenient function,
652 // callable from a debugger, to show the cache contents
653 friend struct wxStrCacheDumper
;
655 // uncomment this to have access to some profiling statistics on program
657 //#define wxPROFILE_STRING_CACHE
659 #ifdef wxPROFILE_STRING_CACHE
660 static struct PosToImplCacheStats
662 unsigned postot
, // total non-trivial calls to PosToImpl
663 poshits
, // cache hits from PosToImpl()
664 mishits
, // cached position beyond the needed one
665 sumpos
, // sum of all positions, used to compute the
666 // average position after dividing by postot
667 sumofs
, // sum of all offsets after using the cache, used to
668 // compute the average after dividing by hits
669 lentot
, // number of total calls to length()
670 lenhits
; // number of cache hits in length()
673 friend struct wxStrCacheStatsDumper
;
675 #define wxCACHE_PROFILE_FIELD_INC(field) ms_cacheStats.field++
676 #define wxCACHE_PROFILE_FIELD_ADD(field, val) ms_cacheStats.field += (val)
677 #else // !wxPROFILE_STRING_CACHE
678 #define wxCACHE_PROFILE_FIELD_INC(field)
679 #define wxCACHE_PROFILE_FIELD_ADD(field, val)
680 #endif // wxPROFILE_STRING_CACHE/!wxPROFILE_STRING_CACHE
682 // note: it could seem that the functions below shouldn't be inline because
683 // they are big, contain loops and so the compiler shouldn't be able to
684 // inline them anyhow, however moving them into string.cpp does decrease the
685 // code performance by ~5%, at least when using g++ 4.1 so do keep them here
686 // unless tests show that it's not advantageous any more
688 // return the pointer to the cache element for this string or NULL if not
690 Cache::Element
*FindCacheElement() const
692 // profiling seems to show a small but consistent gain if we use this
693 // simple loop instead of starting from the last used element (there are
694 // a lot of misses in this function...)
695 Cache::Element
* const cacheBegin
= GetCacheBegin();
696 #ifndef wxHAS_COMPILER_TLS
697 // during destruction tls calls may return NULL, in this case return NULL
698 // immediately without accessing anything else
699 if ( cacheBegin
== NULL
)
702 Cache::Element
* const cacheEnd
= GetCacheEnd();
703 for ( Cache::Element
*c
= cacheBegin
; c
!= cacheEnd
; c
++ )
705 if ( c
->str
== this )
712 // unlike FindCacheElement(), this one always returns a valid pointer to the
713 // cache element for this string, it may have valid last cached position and
714 // its corresponding index in the byte string or not
715 Cache::Element
*GetCacheElement() const
717 Cache::Element
* const cacheBegin
= GetCacheBegin();
718 Cache::Element
* const cacheEnd
= GetCacheEnd();
719 Cache::Element
* const cacheStart
= cacheBegin
+ LastUsedCacheElement();
721 // check the last used first, this does no (measurable) harm for a miss
722 // but does help for simple loops addressing the same string all the time
723 if ( cacheStart
->str
== this )
726 // notice that we're going to check cacheStart again inside this call but
727 // profiling shows that it's still faster to use a simple loop like
728 // inside FindCacheElement() than manually looping with wrapping starting
729 // from the cache entry after the start one
730 Cache::Element
*c
= FindCacheElement();
733 // claim the next cache entry for this string
735 if ( ++c
== cacheEnd
)
741 // and remember the last used element
742 LastUsedCacheElement() = c
- cacheBegin
;
748 size_t DoPosToImpl(size_t pos
) const
750 wxCACHE_PROFILE_FIELD_INC(postot
);
752 // NB: although the case of pos == 1 (and offset from cached position
753 // equal to 1) are common, nothing is gained by writing special code
754 // for handling them, the compiler (at least g++ 4.1 used) seems to
755 // optimize the code well enough on its own
757 wxCACHE_PROFILE_FIELD_ADD(sumpos
, pos
);
759 Cache::Element
* const cache
= GetCacheElement();
761 // cached position can't be 0 so if it is, it means that this entry was
762 // used for length caching only so far, i.e. it doesn't count as a hit
763 // from our point of view
766 wxCACHE_PROFILE_FIELD_INC(poshits
);
769 if ( pos
== cache
->pos
)
772 // this seems to happen only rarely so just reset the cache in this case
773 // instead of complicating code even further by seeking backwards in this
775 if ( cache
->pos
> pos
)
777 wxCACHE_PROFILE_FIELD_INC(mishits
);
782 wxCACHE_PROFILE_FIELD_ADD(sumofs
, pos
- cache
->pos
);
785 wxStringImpl::const_iterator
i(m_impl
.begin() + cache
->impl
);
786 for ( size_t n
= cache
->pos
; n
< pos
; n
++ )
787 wxStringOperations::IncIter(i
);
790 cache
->impl
= i
- m_impl
.begin();
792 wxSTRING_CACHE_ASSERT(
793 (int)cache
->impl
== (begin() + pos
).impl() - m_impl
.begin() );
798 void InvalidateCache()
800 Cache::Element
* const cache
= FindCacheElement();
805 void InvalidateCachedLength()
807 Cache::Element
* const cache
= FindCacheElement();
812 void SetCachedLength(size_t len
)
814 // we optimistically cache the length here even if the string wasn't
815 // present in the cache before, this seems to do no harm and the
816 // potential for avoiding length recomputation for long strings looks
818 GetCacheElement()->len
= len
;
821 void UpdateCachedLength(ptrdiff_t delta
)
823 Cache::Element
* const cache
= FindCacheElement();
824 if ( cache
&& cache
->len
!= npos
)
826 wxSTRING_CACHE_ASSERT( (ptrdiff_t)cache
->len
+ delta
>= 0 );
832 #define wxSTRING_INVALIDATE_CACHE() InvalidateCache()
833 #define wxSTRING_INVALIDATE_CACHED_LENGTH() InvalidateCachedLength()
834 #define wxSTRING_UPDATE_CACHED_LENGTH(n) UpdateCachedLength(n)
835 #define wxSTRING_SET_CACHED_LENGTH(n) SetCachedLength(n)
836 #else // !wxUSE_STRING_POS_CACHE
837 size_t DoPosToImpl(size_t pos
) const
839 return (begin() + pos
).impl() - m_impl
.begin();
842 #define wxSTRING_INVALIDATE_CACHE()
843 #define wxSTRING_INVALIDATE_CACHED_LENGTH()
844 #define wxSTRING_UPDATE_CACHED_LENGTH(n)
845 #define wxSTRING_SET_CACHED_LENGTH(n)
846 #endif // wxUSE_STRING_POS_CACHE/!wxUSE_STRING_POS_CACHE
848 size_t PosToImpl(size_t pos
) const
850 return pos
== 0 || pos
== npos
? pos
: DoPosToImpl(pos
);
853 void PosLenToImpl(size_t pos
, size_t len
, size_t *implPos
, size_t *implLen
) const;
855 size_t LenToImpl(size_t len
) const
858 PosLenToImpl(0, len
, &pos
, &len2
);
862 size_t PosFromImpl(size_t pos
) const
864 if ( pos
== 0 || pos
== npos
)
867 return const_iterator(this, m_impl
.begin() + pos
) - begin();
869 #endif // !wxUSE_UNICODE_UTF8/wxUSE_UNICODE_UTF8
873 typedef wxUniChar value_type
;
874 typedef wxUniChar char_type
;
875 typedef wxUniCharRef reference
;
876 typedef wxChar
* pointer
;
877 typedef const wxChar
* const_pointer
;
879 typedef size_t size_type
;
880 typedef wxUniChar const_reference
;
883 #if wxUSE_UNICODE_UTF8
884 // random access is not O(1), as required by Random Access Iterator
885 #define WX_STR_ITERATOR_TAG std::bidirectional_iterator_tag
887 #define WX_STR_ITERATOR_TAG std::random_access_iterator_tag
889 #define WX_DEFINE_ITERATOR_CATEGORY(cat) typedef cat iterator_category;
891 // not defining iterator_category at all in this case is better than defining
892 // it as some dummy type -- at least it results in more intelligible error
894 #define WX_DEFINE_ITERATOR_CATEGORY(cat)
897 #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, reference_type) \
899 typedef wxStringImpl::iterator_name underlying_iterator; \
901 WX_DEFINE_ITERATOR_CATEGORY(WX_STR_ITERATOR_TAG) \
902 typedef wxUniChar value_type; \
903 typedef int difference_type; \
904 typedef reference_type reference; \
905 typedef pointer_type pointer; \
907 reference operator[](size_t n) const { return *(*this + n); } \
909 iterator_name& operator++() \
910 { wxStringOperations::IncIter(m_cur); return *this; } \
911 iterator_name& operator--() \
912 { wxStringOperations::DecIter(m_cur); return *this; } \
913 iterator_name operator++(int) \
915 iterator_name tmp = *this; \
916 wxStringOperations::IncIter(m_cur); \
919 iterator_name operator--(int) \
921 iterator_name tmp = *this; \
922 wxStringOperations::DecIter(m_cur); \
926 iterator_name& operator+=(ptrdiff_t n) \
928 m_cur = wxStringOperations::AddToIter(m_cur, n); \
931 iterator_name& operator-=(ptrdiff_t n) \
933 m_cur = wxStringOperations::AddToIter(m_cur, -n); \
937 difference_type operator-(const iterator_name& i) const \
938 { return wxStringOperations::DiffIters(m_cur, i.m_cur); } \
940 bool operator==(const iterator_name& i) const \
941 { return m_cur == i.m_cur; } \
942 bool operator!=(const iterator_name& i) const \
943 { return m_cur != i.m_cur; } \
945 bool operator<(const iterator_name& i) const \
946 { return m_cur < i.m_cur; } \
947 bool operator>(const iterator_name& i) const \
948 { return m_cur > i.m_cur; } \
949 bool operator<=(const iterator_name& i) const \
950 { return m_cur <= i.m_cur; } \
951 bool operator>=(const iterator_name& i) const \
952 { return m_cur >= i.m_cur; } \
955 /* for internal wxString use only: */ \
956 underlying_iterator impl() const { return m_cur; } \
958 friend class wxString; \
959 friend class wxCStrData; \
962 underlying_iterator m_cur
964 class WXDLLIMPEXP_FWD_BASE const_iterator
;
966 #if wxUSE_UNICODE_UTF8
967 // NB: In UTF-8 build, (non-const) iterator needs to keep reference
968 // to the underlying wxStringImpl, because UTF-8 is variable-length
969 // encoding and changing the value pointer to by an iterator (using
970 // its operator*) requires calling wxStringImpl::replace() if the old
971 // and new values differ in their encoding's length.
973 // Furthermore, the replace() call may invalid all iterators for the
974 // string, so we have to keep track of outstanding iterators and update
975 // them if replace() happens.
977 // This is implemented by maintaining linked list of iterators for every
978 // string and traversing it in wxUniCharRef::operator=(). Head of the
979 // list is stored in wxString. (FIXME-UTF8)
981 class WXDLLIMPEXP_BASE iterator
983 WX_STR_ITERATOR_IMPL(iterator
, wxChar
*, wxUniCharRef
);
987 iterator(const iterator
& i
)
988 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
989 iterator
& operator=(const iterator
& i
)
994 m_node
.set(i
.str(), &m_cur
);
999 reference
operator*()
1000 { return wxUniCharRef::CreateForString(*str(), m_cur
); }
1002 iterator
operator+(ptrdiff_t n
) const
1003 { return iterator(str(), wxStringOperations::AddToIter(m_cur
, n
)); }
1004 iterator
operator-(ptrdiff_t n
) const
1005 { return iterator(str(), wxStringOperations::AddToIter(m_cur
, -n
)); }
1007 // Normal iterators need to be comparable with the const_iterators so
1008 // declare the comparison operators and implement them below after the
1009 // full const_iterator declaration.
1010 bool operator==(const const_iterator
& i
) const;
1011 bool operator!=(const const_iterator
& i
) const;
1012 bool operator<(const const_iterator
& i
) const;
1013 bool operator>(const const_iterator
& i
) const;
1014 bool operator<=(const const_iterator
& i
) const;
1015 bool operator>=(const const_iterator
& i
) const;
1018 iterator(wxString
*wxstr
, underlying_iterator ptr
)
1019 : m_cur(ptr
), m_node(wxstr
, &m_cur
) {}
1021 wxString
* str() const { return const_cast<wxString
*>(m_node
.m_str
); }
1023 wxStringIteratorNode m_node
;
1025 friend class const_iterator
;
1028 class WXDLLIMPEXP_BASE const_iterator
1030 // NB: reference_type is intentionally value, not reference, the character
1031 // may be encoded differently in wxString data:
1032 WX_STR_ITERATOR_IMPL(const_iterator
, const wxChar
*, wxUniChar
);
1036 const_iterator(const const_iterator
& i
)
1037 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
1038 const_iterator(const iterator
& i
)
1039 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
1041 const_iterator
& operator=(const const_iterator
& i
)
1046 m_node
.set(i
.str(), &m_cur
);
1050 const_iterator
& operator=(const iterator
& i
)
1051 { m_cur
= i
.m_cur
; m_node
.set(i
.str(), &m_cur
); return *this; }
1053 reference
operator*() const
1054 { return wxStringOperations::DecodeChar(m_cur
); }
1056 const_iterator
operator+(ptrdiff_t n
) const
1057 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur
, n
)); }
1058 const_iterator
operator-(ptrdiff_t n
) const
1059 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur
, -n
)); }
1061 // Notice that comparison operators taking non-const iterator are not
1062 // needed here because of the implicit conversion from non-const iterator
1063 // to const ones ensure that the versions for const_iterator declared
1064 // inside WX_STR_ITERATOR_IMPL can be used.
1067 // for internal wxString use only:
1068 const_iterator(const wxString
*wxstr
, underlying_iterator ptr
)
1069 : m_cur(ptr
), m_node(wxstr
, &m_cur
) {}
1071 const wxString
* str() const { return m_node
.m_str
; }
1073 wxStringIteratorNode m_node
;
1076 size_t IterToImplPos(wxString::iterator i
) const
1077 { return wxStringImpl::const_iterator(i
.impl()) - m_impl
.begin(); }
1079 iterator
GetIterForNthChar(size_t n
)
1080 { return iterator(this, m_impl
.begin() + PosToImpl(n
)); }
1081 const_iterator
GetIterForNthChar(size_t n
) const
1082 { return const_iterator(this, m_impl
.begin() + PosToImpl(n
)); }
1083 #else // !wxUSE_UNICODE_UTF8
1085 class WXDLLIMPEXP_BASE iterator
1087 WX_STR_ITERATOR_IMPL(iterator
, wxChar
*, wxUniCharRef
);
1091 iterator(const iterator
& i
) : m_cur(i
.m_cur
) {}
1093 reference
operator*()
1094 { return wxUniCharRef::CreateForString(m_cur
); }
1096 iterator
operator+(ptrdiff_t n
) const
1097 { return iterator(wxStringOperations::AddToIter(m_cur
, n
)); }
1098 iterator
operator-(ptrdiff_t n
) const
1099 { return iterator(wxStringOperations::AddToIter(m_cur
, -n
)); }
1101 // As in UTF-8 case above, define comparison operators taking
1102 // const_iterator too.
1103 bool operator==(const const_iterator
& i
) const;
1104 bool operator!=(const const_iterator
& i
) const;
1105 bool operator<(const const_iterator
& i
) const;
1106 bool operator>(const const_iterator
& i
) const;
1107 bool operator<=(const const_iterator
& i
) const;
1108 bool operator>=(const const_iterator
& i
) const;
1111 // for internal wxString use only:
1112 iterator(underlying_iterator ptr
) : m_cur(ptr
) {}
1113 iterator(wxString
*WXUNUSED(str
), underlying_iterator ptr
) : m_cur(ptr
) {}
1115 friend class const_iterator
;
1118 class WXDLLIMPEXP_BASE const_iterator
1120 // NB: reference_type is intentionally value, not reference, the character
1121 // may be encoded differently in wxString data:
1122 WX_STR_ITERATOR_IMPL(const_iterator
, const wxChar
*, wxUniChar
);
1126 const_iterator(const const_iterator
& i
) : m_cur(i
.m_cur
) {}
1127 const_iterator(const iterator
& i
) : m_cur(i
.m_cur
) {}
1129 reference
operator*() const
1130 { return wxStringOperations::DecodeChar(m_cur
); }
1132 const_iterator
operator+(ptrdiff_t n
) const
1133 { return const_iterator(wxStringOperations::AddToIter(m_cur
, n
)); }
1134 const_iterator
operator-(ptrdiff_t n
) const
1135 { return const_iterator(wxStringOperations::AddToIter(m_cur
, -n
)); }
1137 // As in UTF-8 case above, we don't need comparison operators taking
1138 // iterator because we have an implicit conversion from iterator to
1139 // const_iterator so the operators declared by WX_STR_ITERATOR_IMPL will
1143 // for internal wxString use only:
1144 const_iterator(underlying_iterator ptr
) : m_cur(ptr
) {}
1145 const_iterator(const wxString
*WXUNUSED(str
), underlying_iterator ptr
)
1149 iterator
GetIterForNthChar(size_t n
) { return begin() + n
; }
1150 const_iterator
GetIterForNthChar(size_t n
) const { return begin() + n
; }
1151 #endif // wxUSE_UNICODE_UTF8/!wxUSE_UNICODE_UTF8
1153 #undef WX_STR_ITERATOR_TAG
1154 #undef WX_STR_ITERATOR_IMPL
1156 friend class iterator
;
1157 friend class const_iterator
;
1159 template <typename T
>
1160 class reverse_iterator_impl
1163 typedef T iterator_type
;
1165 WX_DEFINE_ITERATOR_CATEGORY(typename
T::iterator_category
)
1166 typedef typename
T::value_type value_type
;
1167 typedef typename
T::difference_type difference_type
;
1168 typedef typename
T::reference reference
;
1169 typedef typename
T::pointer
*pointer
;
1171 reverse_iterator_impl() {}
1172 reverse_iterator_impl(iterator_type i
) : m_cur(i
) {}
1173 reverse_iterator_impl(const reverse_iterator_impl
& ri
)
1174 : m_cur(ri
.m_cur
) {}
1176 iterator_type
base() const { return m_cur
; }
1178 reference
operator*() const { return *(m_cur
-1); }
1179 reference
operator[](size_t n
) const { return *(*this + n
); }
1181 reverse_iterator_impl
& operator++()
1182 { --m_cur
; return *this; }
1183 reverse_iterator_impl
operator++(int)
1184 { reverse_iterator_impl tmp
= *this; --m_cur
; return tmp
; }
1185 reverse_iterator_impl
& operator--()
1186 { ++m_cur
; return *this; }
1187 reverse_iterator_impl
operator--(int)
1188 { reverse_iterator_impl tmp
= *this; ++m_cur
; return tmp
; }
1190 // NB: explicit <T> in the functions below is to keep BCC 5.5 happy
1191 reverse_iterator_impl
operator+(ptrdiff_t n
) const
1192 { return reverse_iterator_impl
<T
>(m_cur
- n
); }
1193 reverse_iterator_impl
operator-(ptrdiff_t n
) const
1194 { return reverse_iterator_impl
<T
>(m_cur
+ n
); }
1195 reverse_iterator_impl
operator+=(ptrdiff_t n
)
1196 { m_cur
-= n
; return *this; }
1197 reverse_iterator_impl
operator-=(ptrdiff_t n
)
1198 { m_cur
+= n
; return *this; }
1200 unsigned operator-(const reverse_iterator_impl
& i
) const
1201 { return i
.m_cur
- m_cur
; }
1203 bool operator==(const reverse_iterator_impl
& ri
) const
1204 { return m_cur
== ri
.m_cur
; }
1205 bool operator!=(const reverse_iterator_impl
& ri
) const
1206 { return !(*this == ri
); }
1208 bool operator<(const reverse_iterator_impl
& i
) const
1209 { return m_cur
> i
.m_cur
; }
1210 bool operator>(const reverse_iterator_impl
& i
) const
1211 { return m_cur
< i
.m_cur
; }
1212 bool operator<=(const reverse_iterator_impl
& i
) const
1213 { return m_cur
>= i
.m_cur
; }
1214 bool operator>=(const reverse_iterator_impl
& i
) const
1215 { return m_cur
<= i
.m_cur
; }
1218 iterator_type m_cur
;
1221 typedef reverse_iterator_impl
<iterator
> reverse_iterator
;
1222 typedef reverse_iterator_impl
<const_iterator
> const_reverse_iterator
;
1225 // used to transform an expression built using c_str() (and hence of type
1226 // wxCStrData) to an iterator into the string
1227 static const_iterator
CreateConstIterator(const wxCStrData
& data
)
1229 return const_iterator(data
.m_str
,
1230 (data
.m_str
->begin() + data
.m_offset
).impl());
1233 // in UTF-8 STL build, creation from std::string requires conversion under
1234 // non-UTF8 locales, so we can't have and use wxString(wxStringImpl) ctor;
1235 // instead we define dummy type that lets us have wxString ctor for creation
1236 // from wxStringImpl that couldn't be used by user code (in all other builds,
1237 // "standard" ctors can be used):
1238 #if wxUSE_UNICODE_UTF8 && wxUSE_STL_BASED_WXSTRING
1239 struct CtorFromStringImplTag
{};
1241 wxString(CtorFromStringImplTag
* WXUNUSED(dummy
), const wxStringImpl
& src
)
1244 static wxString
FromImpl(const wxStringImpl
& src
)
1245 { return wxString((CtorFromStringImplTag
*)NULL
, src
); }
1247 #if !wxUSE_STL_BASED_WXSTRING
1248 wxString(const wxStringImpl
& src
) : m_impl(src
) { }
1249 // else: already defined as wxString(wxStdString) below
1251 static wxString
FromImpl(const wxStringImpl
& src
) { return wxString(src
); }
1255 // constructors and destructor
1256 // ctor for an empty string
1260 wxString(const wxString
& stringSrc
) : m_impl(stringSrc
.m_impl
) { }
1262 // string containing nRepeat copies of ch
1263 wxString(wxUniChar ch
, size_t nRepeat
= 1 )
1264 { assign(nRepeat
, ch
); }
1265 wxString(size_t nRepeat
, wxUniChar ch
)
1266 { assign(nRepeat
, ch
); }
1267 wxString(wxUniCharRef ch
, size_t nRepeat
= 1)
1268 { assign(nRepeat
, ch
); }
1269 wxString(size_t nRepeat
, wxUniCharRef ch
)
1270 { assign(nRepeat
, ch
); }
1271 wxString(char ch
, size_t nRepeat
= 1)
1272 { assign(nRepeat
, ch
); }
1273 wxString(size_t nRepeat
, char ch
)
1274 { assign(nRepeat
, ch
); }
1275 wxString(wchar_t ch
, size_t nRepeat
= 1)
1276 { assign(nRepeat
, ch
); }
1277 wxString(size_t nRepeat
, wchar_t ch
)
1278 { assign(nRepeat
, ch
); }
1280 // ctors from char* strings:
1281 wxString(const char *psz
)
1282 : m_impl(ImplStr(psz
)) {}
1283 wxString(const char *psz
, const wxMBConv
& conv
)
1284 : m_impl(ImplStr(psz
, conv
)) {}
1285 wxString(const char *psz
, size_t nLength
)
1286 { assign(psz
, nLength
); }
1287 wxString(const char *psz
, const wxMBConv
& conv
, size_t nLength
)
1289 SubstrBufFromMB
str(ImplStr(psz
, nLength
, conv
));
1290 m_impl
.assign(str
.data
, str
.len
);
1293 // and unsigned char*:
1294 wxString(const unsigned char *psz
)
1295 : m_impl(ImplStr((const char*)psz
)) {}
1296 wxString(const unsigned char *psz
, const wxMBConv
& conv
)
1297 : m_impl(ImplStr((const char*)psz
, conv
)) {}
1298 wxString(const unsigned char *psz
, size_t nLength
)
1299 { assign((const char*)psz
, nLength
); }
1300 wxString(const unsigned char *psz
, const wxMBConv
& conv
, size_t nLength
)
1302 SubstrBufFromMB
str(ImplStr((const char*)psz
, nLength
, conv
));
1303 m_impl
.assign(str
.data
, str
.len
);
1306 // ctors from wchar_t* strings:
1307 wxString(const wchar_t *pwz
)
1308 : m_impl(ImplStr(pwz
)) {}
1309 wxString(const wchar_t *pwz
, const wxMBConv
& WXUNUSED(conv
))
1310 : m_impl(ImplStr(pwz
)) {}
1311 wxString(const wchar_t *pwz
, size_t nLength
)
1312 { assign(pwz
, nLength
); }
1313 wxString(const wchar_t *pwz
, const wxMBConv
& WXUNUSED(conv
), size_t nLength
)
1314 { assign(pwz
, nLength
); }
1316 wxString(const wxScopedCharBuffer
& buf
)
1317 { assign(buf
.data(), buf
.length()); }
1318 wxString(const wxScopedWCharBuffer
& buf
)
1319 { assign(buf
.data(), buf
.length()); }
1321 // NB: this version uses m_impl.c_str() to force making a copy of the
1322 // string, so that "wxString(str.c_str())" idiom for passing strings
1323 // between threads works
1324 wxString(const wxCStrData
& cstr
)
1325 : m_impl(cstr
.AsString().m_impl
.c_str()) { }
1327 // as we provide both ctors with this signature for both char and unsigned
1328 // char string, we need to provide one for wxCStrData to resolve ambiguity
1329 wxString(const wxCStrData
& cstr
, size_t nLength
)
1330 : m_impl(cstr
.AsString().Mid(0, nLength
).m_impl
) {}
1332 // and because wxString is convertible to wxCStrData and const wxChar *
1333 // we also need to provide this one
1334 wxString(const wxString
& str
, size_t nLength
)
1335 { assign(str
, nLength
); }
1338 #if wxUSE_STRING_POS_CACHE
1341 // we need to invalidate our cache entry as another string could be
1342 // recreated at the same address (unlikely, but still possible, with the
1343 // heap-allocated strings but perfectly common with stack-allocated ones)
1346 #endif // wxUSE_STRING_POS_CACHE
1348 // even if we're not built with wxUSE_STL == 1 it is very convenient to allow
1349 // implicit conversions from std::string to wxString and vice verse as this
1350 // allows to use the same strings in non-GUI and GUI code, however we don't
1351 // want to unconditionally add this ctor as it would make wx lib dependent on
1352 // libstdc++ on some Linux versions which is bad, so instead we ask the
1353 // client code to define this wxUSE_STD_STRING symbol if they need it
1354 #if wxUSE_STD_STRING
1355 #if wxUSE_UNICODE_WCHAR
1356 wxString(const wxStdWideString
& str
) : m_impl(str
) {}
1357 #else // UTF-8 or ANSI
1358 wxString(const wxStdWideString
& str
)
1359 { assign(str
.c_str(), str
.length()); }
1362 #if !wxUSE_UNICODE // ANSI build
1363 // FIXME-UTF8: do this in UTF8 build #if wxUSE_UTF8_LOCALE_ONLY, too
1364 wxString(const std::string
& str
) : m_impl(str
) {}
1366 wxString(const std::string
& str
)
1367 { assign(str
.c_str(), str
.length()); }
1369 #endif // wxUSE_STD_STRING
1371 // Unlike ctor from std::string, we provide conversion to std::string only
1372 // if wxUSE_STL and not merely wxUSE_STD_STRING (which is on by default),
1373 // because it conflicts with operator const char/wchar_t* but we still
1374 // provide explicit conversions to std::[w]string for convenience in any case
1375 #if wxUSE_STD_STRING
1376 // We can avoid a copy if we already use this string type internally,
1377 // otherwise we create a copy on the fly:
1378 #if wxUSE_UNICODE_WCHAR && wxUSE_STL_BASED_WXSTRING
1379 #define wxStringToStdWstringRetType const wxStdWideString&
1380 const wxStdWideString
& ToStdWstring() const { return m_impl
; }
1382 // wxStringImpl is either not std::string or needs conversion
1383 #define wxStringToStdWstringRetType wxStdWideString
1384 wxStdWideString
ToStdWstring() const
1386 wxScopedWCharBuffer
buf(wc_str());
1387 return wxStdWideString(buf
.data(), buf
.length());
1391 #if (!wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY) && wxUSE_STL_BASED_WXSTRING
1392 // wxStringImpl is std::string in the encoding we want
1393 #define wxStringToStdStringRetType const std::string&
1394 const std::string
& ToStdString() const { return m_impl
; }
1396 // wxStringImpl is either not std::string or needs conversion
1397 #define wxStringToStdStringRetType std::string
1398 std::string
ToStdString() const
1400 wxScopedCharBuffer
buf(mb_str());
1401 return std::string(buf
.data(), buf
.length());
1406 // In wxUSE_STL case we also provide implicit conversions as there is no
1407 // ambiguity with the const char/wchar_t* ones as they are disabled in this
1408 // build (for consistency with std::basic_string<>)
1409 operator wxStringToStdStringRetType() const { return ToStdString(); }
1410 operator wxStringToStdWstringRetType() const { return ToStdWstring(); }
1413 #undef wxStringToStdStringRetType
1414 #undef wxStringToStdWstringRetType
1416 #endif // wxUSE_STD_STRING
1418 wxString
Clone() const
1420 // make a deep copy of the string, i.e. the returned string will have
1421 // ref count = 1 with refcounted implementation
1422 return wxString::FromImpl(wxStringImpl(m_impl
.c_str(), m_impl
.length()));
1425 // first valid index position
1426 const_iterator
begin() const { return const_iterator(this, m_impl
.begin()); }
1427 iterator
begin() { return iterator(this, m_impl
.begin()); }
1428 // position one after the last valid one
1429 const_iterator
end() const { return const_iterator(this, m_impl
.end()); }
1430 iterator
end() { return iterator(this, m_impl
.end()); }
1432 // first element of the reversed string
1433 const_reverse_iterator
rbegin() const
1434 { return const_reverse_iterator(end()); }
1435 reverse_iterator
rbegin()
1436 { return reverse_iterator(end()); }
1437 // one beyond the end of the reversed string
1438 const_reverse_iterator
rend() const
1439 { return const_reverse_iterator(begin()); }
1440 reverse_iterator
rend()
1441 { return reverse_iterator(begin()); }
1443 // std::string methods:
1444 #if wxUSE_UNICODE_UTF8
1445 size_t length() const
1447 #if wxUSE_STRING_POS_CACHE
1448 wxCACHE_PROFILE_FIELD_INC(lentot
);
1450 Cache::Element
* const cache
= GetCacheElement();
1452 if ( cache
->len
== npos
)
1454 // it's probably not worth trying to be clever and using cache->pos
1455 // here as it's probably 0 anyhow -- you usually call length() before
1456 // starting to index the string
1457 cache
->len
= end() - begin();
1461 wxCACHE_PROFILE_FIELD_INC(lenhits
);
1463 wxSTRING_CACHE_ASSERT( (int)cache
->len
== end() - begin() );
1467 #else // !wxUSE_STRING_POS_CACHE
1468 return end() - begin();
1469 #endif // wxUSE_STRING_POS_CACHE/!wxUSE_STRING_POS_CACHE
1472 size_t length() const { return m_impl
.length(); }
1475 size_type
size() const { return length(); }
1476 size_type
max_size() const { return npos
; }
1478 bool empty() const { return m_impl
.empty(); }
1480 // NB: these methods don't have a well-defined meaning in UTF-8 case
1481 size_type
capacity() const { return m_impl
.capacity(); }
1482 void reserve(size_t sz
) { m_impl
.reserve(sz
); }
1484 void resize(size_t nSize
, wxUniChar ch
= wxT('\0'))
1486 const size_t len
= length();
1490 #if wxUSE_UNICODE_UTF8
1493 wxSTRING_INVALIDATE_CACHE();
1495 // we can't use wxStringImpl::resize() for truncating the string as it
1496 // counts in bytes, not characters
1501 // we also can't use (presumably more efficient) resize() if we have to
1502 // append characters taking more than one byte
1503 if ( !ch
.IsAscii() )
1505 append(nSize
- len
, ch
);
1507 else // can use (presumably faster) resize() version
1508 #endif // wxUSE_UNICODE_UTF8
1510 wxSTRING_INVALIDATE_CACHED_LENGTH();
1512 m_impl
.resize(nSize
, (wxStringCharType
)ch
);
1516 wxString
substr(size_t nStart
= 0, size_t nLen
= npos
) const
1519 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
1520 return FromImpl(m_impl
.substr(pos
, len
));
1523 // generic attributes & operations
1524 // as standard strlen()
1525 size_t Len() const { return length(); }
1526 // string contains any characters?
1527 bool IsEmpty() const { return empty(); }
1528 // empty string is "false", so !str will return true
1529 bool operator!() const { return empty(); }
1530 // truncate the string to given length
1531 wxString
& Truncate(size_t uiLen
);
1532 // empty string contents
1533 void Empty() { clear(); }
1534 // empty the string and free memory
1535 void Clear() { clear(); }
1538 // Is an ascii value
1539 bool IsAscii() const;
1541 bool IsNumber() const;
1543 bool IsWord() const;
1545 // data access (all indexes are 0 based)
1547 wxUniChar
at(size_t n
) const
1548 { return wxStringOperations::DecodeChar(m_impl
.begin() + PosToImpl(n
)); }
1549 wxUniChar
GetChar(size_t n
) const
1551 // read/write access
1552 wxUniCharRef
at(size_t n
)
1553 { return *GetIterForNthChar(n
); }
1554 wxUniCharRef
GetWritableChar(size_t n
)
1557 void SetChar(size_t n
, wxUniChar ch
)
1560 // get last character
1561 wxUniChar
Last() const
1563 wxASSERT_MSG( !empty(), wxT("wxString: index out of bounds") );
1567 // get writable last character
1570 wxASSERT_MSG( !empty(), wxT("wxString: index out of bounds") );
1575 Note that we we must define all of the overloads below to avoid
1576 ambiguity when using str[0].
1578 wxUniChar
operator[](int n
) const
1580 wxUniChar
operator[](long n
) const
1582 wxUniChar
operator[](size_t n
) const
1584 #ifndef wxSIZE_T_IS_UINT
1585 wxUniChar
operator[](unsigned int n
) const
1587 #endif // size_t != unsigned int
1589 // operator versions of GetWriteableChar()
1590 wxUniCharRef
operator[](int n
)
1592 wxUniCharRef
operator[](long n
)
1594 wxUniCharRef
operator[](size_t n
)
1596 #ifndef wxSIZE_T_IS_UINT
1597 wxUniCharRef
operator[](unsigned int n
)
1599 #endif // size_t != unsigned int
1603 Overview of wxString conversions, implicit and explicit:
1605 - wxString has a std::[w]string-like c_str() method, however it does
1606 not return a C-style string directly but instead returns wxCStrData
1607 helper object which is convertible to either "char *" narrow string
1608 or "wchar_t *" wide string. Usually the correct conversion will be
1609 applied by the compiler automatically but if this doesn't happen you
1610 need to explicitly choose one using wxCStrData::AsChar() or AsWChar()
1611 methods or another wxString conversion function.
1613 - One of the places where the conversion does *NOT* happen correctly is
1614 when c_str() is passed to a vararg function such as printf() so you
1615 must *NOT* use c_str() with them. Either use wxPrintf() (all wx
1616 functions do handle c_str() correctly, even if they appear to be
1617 vararg (but they're not, really)) or add an explicit AsChar() or, if
1618 compatibility with previous wxWidgets versions is important, add a
1619 cast to "const char *".
1621 - In non-STL mode only, wxString is also implicitly convertible to
1622 wxCStrData. The same warning as above applies.
1624 - c_str() is polymorphic as it can be converted to either narrow or
1625 wide string. If you explicitly need one or the other, choose to use
1626 mb_str() (for narrow) or wc_str() (for wide) instead. Notice that
1627 these functions can return either the pointer to string directly (if
1628 this is what the string uses internally) or a temporary buffer
1629 containing the string and convertible to it. Again, conversion will
1630 usually be done automatically by the compiler but beware of the
1631 vararg functions: you need an explicit cast when using them.
1633 - There are also non-const versions of mb_str() and wc_str() called
1634 char_str() and wchar_str(). They are only meant to be used with
1635 non-const-correct functions and they always return buffers.
1637 - Finally wx_str() returns whatever string representation is used by
1638 wxString internally. It may be either a narrow or wide string
1639 depending on wxWidgets build mode but it will always be a raw pointer
1643 // explicit conversion to wxCStrData
1644 wxCStrData
c_str() const { return wxCStrData(this); }
1645 wxCStrData
data() const { return c_str(); }
1647 // implicit conversion to wxCStrData
1648 operator wxCStrData() const { return c_str(); }
1650 // the first two operators conflict with operators for conversion to
1651 // std::string and they must be disabled in STL build; the next one only
1652 // makes sense if conversions to char* are also defined and not defining it
1653 // in STL build also helps us to get more clear error messages for the code
1654 // which relies on implicit conversion to char* in STL build
1656 operator const char*() const { return c_str(); }
1657 operator const wchar_t*() const { return c_str(); }
1659 // implicit conversion to untyped pointer for compatibility with previous
1660 // wxWidgets versions: this is the same as conversion to const char * so it
1662 operator const void*() const { return c_str(); }
1665 // identical to c_str(), for MFC compatibility
1666 const wxCStrData
GetData() const { return c_str(); }
1668 // explicit conversion to C string in internal representation (char*,
1669 // wchar_t*, UTF-8-encoded char*, depending on the build):
1670 const wxStringCharType
*wx_str() const { return m_impl
.c_str(); }
1672 // conversion to *non-const* multibyte or widestring buffer; modifying
1673 // returned buffer won't affect the string, these methods are only useful
1674 // for passing values to const-incorrect functions
1675 wxWritableCharBuffer
char_str(const wxMBConv
& conv
= wxConvLibc
) const
1676 { return mb_str(conv
); }
1677 wxWritableWCharBuffer
wchar_str() const { return wc_str(); }
1679 // conversion to the buffer of the given type T (= char or wchar_t) and
1680 // also optionally return the buffer length
1682 // this is mostly/only useful for the template functions
1684 // FIXME-VC6: the second argument only exists for VC6 which doesn't support
1685 // explicit template function selection, do not use it unless
1686 // you must support VC6!
1687 template <typename T
>
1688 wxCharTypeBuffer
<T
> tchar_str(size_t *len
= NULL
,
1689 T
* WXUNUSED(dummy
) = NULL
) const
1692 // we need a helper dispatcher depending on type
1693 return wxPrivate::wxStringAsBufHelper
<T
>::Get(*this, len
);
1695 // T can only be char in ANSI build
1699 return wxCharTypeBuffer
<T
>::CreateNonOwned(wx_str(), length());
1700 #endif // Unicode build kind
1703 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
1704 // converting numbers or strings which are certain not to contain special
1705 // chars (typically system functions, X atoms, environment variables etc.)
1707 // the behaviour of these functions with the strings containing anything
1708 // else than 7 bit ASCII characters is undefined, use at your own risk.
1710 static wxString
FromAscii(const char *ascii
, size_t len
);
1711 static wxString
FromAscii(const char *ascii
);
1712 static wxString
FromAscii(char ascii
);
1713 const wxScopedCharBuffer
ToAscii() const;
1715 static wxString
FromAscii(const char *ascii
) { return wxString( ascii
); }
1716 static wxString
FromAscii(const char *ascii
, size_t len
)
1717 { return wxString( ascii
, len
); }
1718 static wxString
FromAscii(char ascii
) { return wxString( ascii
); }
1719 const char *ToAscii() const { return c_str(); }
1720 #endif // Unicode/!Unicode
1722 // also provide unsigned char overloads as signed/unsigned doesn't matter
1723 // for 7 bit ASCII characters
1724 static wxString
FromAscii(const unsigned char *ascii
)
1725 { return FromAscii((const char *)ascii
); }
1726 static wxString
FromAscii(const unsigned char *ascii
, size_t len
)
1727 { return FromAscii((const char *)ascii
, len
); }
1729 // conversion to/from UTF-8:
1730 #if wxUSE_UNICODE_UTF8
1731 static wxString
FromUTF8Unchecked(const char *utf8
)
1734 return wxEmptyString
;
1736 wxASSERT( wxStringOperations::IsValidUtf8String(utf8
) );
1737 return FromImpl(wxStringImpl(utf8
));
1739 static wxString
FromUTF8Unchecked(const char *utf8
, size_t len
)
1742 return wxEmptyString
;
1744 return FromUTF8Unchecked(utf8
);
1746 wxASSERT( wxStringOperations::IsValidUtf8String(utf8
, len
) );
1747 return FromImpl(wxStringImpl(utf8
, len
));
1750 static wxString
FromUTF8(const char *utf8
)
1752 if ( !utf8
|| !wxStringOperations::IsValidUtf8String(utf8
) )
1755 return FromImpl(wxStringImpl(utf8
));
1757 static wxString
FromUTF8(const char *utf8
, size_t len
)
1760 return FromUTF8(utf8
);
1762 if ( !utf8
|| !wxStringOperations::IsValidUtf8String(utf8
, len
) )
1765 return FromImpl(wxStringImpl(utf8
, len
));
1768 const wxScopedCharBuffer
utf8_str() const
1769 { return wxCharBuffer::CreateNonOwned(m_impl
.c_str(), m_impl
.length()); }
1771 // this function exists in UTF-8 build only and returns the length of the
1772 // internal UTF-8 representation
1773 size_t utf8_length() const { return m_impl
.length(); }
1774 #elif wxUSE_UNICODE_WCHAR
1775 static wxString
FromUTF8(const char *utf8
, size_t len
= npos
)
1776 { return wxString(utf8
, wxMBConvUTF8(), len
); }
1777 static wxString
FromUTF8Unchecked(const char *utf8
, size_t len
= npos
)
1779 const wxString
s(utf8
, wxMBConvUTF8(), len
);
1780 wxASSERT_MSG( !utf8
|| !*utf8
|| !s
.empty(),
1781 "string must be valid UTF-8" );
1784 const wxScopedCharBuffer
utf8_str() const { return mb_str(wxMBConvUTF8()); }
1786 static wxString
FromUTF8(const char *utf8
)
1787 { return wxString(wxMBConvUTF8().cMB2WC(utf8
)); }
1788 static wxString
FromUTF8(const char *utf8
, size_t len
)
1791 wxScopedWCharBuffer
buf(wxMBConvUTF8().cMB2WC(utf8
, len
== npos
? wxNO_LEN
: len
, &wlen
));
1792 return wxString(buf
.data(), wlen
);
1794 static wxString
FromUTF8Unchecked(const char *utf8
, size_t len
= npos
)
1797 wxScopedWCharBuffer buf
1799 wxMBConvUTF8().cMB2WC
1802 len
== npos
? wxNO_LEN
: len
,
1806 wxASSERT_MSG( !utf8
|| !*utf8
|| wlen
,
1807 "string must be valid UTF-8" );
1809 return wxString(buf
.data(), wlen
);
1811 const wxScopedCharBuffer
utf8_str() const
1812 { return wxMBConvUTF8().cWC2MB(wc_str()); }
1815 const wxScopedCharBuffer
ToUTF8() const { return utf8_str(); }
1817 // functions for storing binary data in wxString:
1819 static wxString
From8BitData(const char *data
, size_t len
)
1820 { return wxString(data
, wxConvISO8859_1
, len
); }
1821 // version for NUL-terminated data:
1822 static wxString
From8BitData(const char *data
)
1823 { return wxString(data
, wxConvISO8859_1
); }
1824 const wxScopedCharBuffer
To8BitData() const
1825 { return mb_str(wxConvISO8859_1
); }
1827 static wxString
From8BitData(const char *data
, size_t len
)
1828 { return wxString(data
, len
); }
1829 // version for NUL-terminated data:
1830 static wxString
From8BitData(const char *data
)
1831 { return wxString(data
); }
1832 const wxScopedCharBuffer
To8BitData() const
1833 { return wxScopedCharBuffer::CreateNonOwned(wx_str(), length()); }
1834 #endif // Unicode/ANSI
1836 // conversions with (possible) format conversions: have to return a
1837 // buffer with temporary data
1839 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
1840 // return an ANSI (multibyte) string, wc_str() to return a wide string and
1841 // fn_str() to return a string which should be used with the OS APIs
1842 // accepting the file names. The return value is always the same, but the
1843 // type differs because a function may either return pointer to the buffer
1844 // directly or have to use intermediate buffer for translation.
1848 // this is an optimization: even though using mb_str(wxConvLibc) does the
1849 // same thing (i.e. returns pointer to internal representation as locale is
1850 // always an UTF-8 one) in wxUSE_UTF8_LOCALE_ONLY case, we can avoid the
1851 // extra checks and the temporary buffer construction by providing a
1852 // separate mb_str() overload
1853 #if wxUSE_UTF8_LOCALE_ONLY
1854 const char* mb_str() const { return wx_str(); }
1855 const wxScopedCharBuffer
mb_str(const wxMBConv
& conv
) const
1857 return AsCharBuf(conv
);
1859 #else // !wxUSE_UTF8_LOCALE_ONLY
1860 const wxScopedCharBuffer
mb_str(const wxMBConv
& conv
= wxConvLibc
) const
1862 return AsCharBuf(conv
);
1864 #endif // wxUSE_UTF8_LOCALE_ONLY/!wxUSE_UTF8_LOCALE_ONLY
1866 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
1868 #if wxUSE_UNICODE_WCHAR
1869 const wchar_t* wc_str() const { return wx_str(); }
1870 #elif wxUSE_UNICODE_UTF8
1871 const wxScopedWCharBuffer
wc_str() const
1872 { return AsWCharBuf(wxMBConvStrictUTF8()); }
1874 // for compatibility with !wxUSE_UNICODE version
1875 const wxWX2WCbuf
wc_str(const wxMBConv
& WXUNUSED(conv
)) const
1876 { return wc_str(); }
1879 const wxScopedCharBuffer
fn_str() const { return mb_str(wxConvFile
); }
1881 const wxWX2WCbuf
fn_str() const { return wc_str(); }
1882 #endif // wxMBFILES/!wxMBFILES
1885 const char* mb_str() const { return wx_str(); }
1887 // for compatibility with wxUSE_UNICODE version
1888 const char* mb_str(const wxMBConv
& WXUNUSED(conv
)) const { return wx_str(); }
1890 const wxWX2MBbuf
mbc_str() const { return mb_str(); }
1892 const wxScopedWCharBuffer
wc_str(const wxMBConv
& conv
= wxConvLibc
) const
1893 { return AsWCharBuf(conv
); }
1895 const wxScopedCharBuffer
fn_str() const
1896 { return wxConvFile
.cWC2WX( wc_str( wxConvLibc
) ); }
1897 #endif // Unicode/ANSI
1899 #if wxUSE_UNICODE_UTF8
1900 const wxScopedWCharBuffer
t_str() const { return wc_str(); }
1901 #elif wxUSE_UNICODE_WCHAR
1902 const wchar_t* t_str() const { return wx_str(); }
1904 const char* t_str() const { return wx_str(); }
1908 // overloaded assignment
1909 // from another wxString
1910 wxString
& operator=(const wxString
& stringSrc
)
1912 if ( this != &stringSrc
)
1914 wxSTRING_INVALIDATE_CACHE();
1916 m_impl
= stringSrc
.m_impl
;
1922 wxString
& operator=(const wxCStrData
& cstr
)
1923 { return *this = cstr
.AsString(); }
1925 wxString
& operator=(wxUniChar ch
)
1927 wxSTRING_INVALIDATE_CACHE();
1929 #if wxUSE_UNICODE_UTF8
1930 if ( !ch
.IsAscii() )
1931 m_impl
= wxStringOperations::EncodeChar(ch
);
1933 #endif // wxUSE_UNICODE_UTF8
1934 m_impl
= (wxStringCharType
)ch
;
1938 wxString
& operator=(wxUniCharRef ch
)
1939 { return operator=((wxUniChar
)ch
); }
1940 wxString
& operator=(char ch
)
1941 { return operator=(wxUniChar(ch
)); }
1942 wxString
& operator=(unsigned char ch
)
1943 { return operator=(wxUniChar(ch
)); }
1944 wxString
& operator=(wchar_t ch
)
1945 { return operator=(wxUniChar(ch
)); }
1946 // from a C string - STL probably will crash on NULL,
1947 // so we need to compensate in that case
1948 #if wxUSE_STL_BASED_WXSTRING
1949 wxString
& operator=(const char *psz
)
1951 wxSTRING_INVALIDATE_CACHE();
1954 m_impl
= ImplStr(psz
);
1961 wxString
& operator=(const wchar_t *pwz
)
1963 wxSTRING_INVALIDATE_CACHE();
1966 m_impl
= ImplStr(pwz
);
1972 #else // !wxUSE_STL_BASED_WXSTRING
1973 wxString
& operator=(const char *psz
)
1975 wxSTRING_INVALIDATE_CACHE();
1977 m_impl
= ImplStr(psz
);
1982 wxString
& operator=(const wchar_t *pwz
)
1984 wxSTRING_INVALIDATE_CACHE();
1986 m_impl
= ImplStr(pwz
);
1990 #endif // wxUSE_STL_BASED_WXSTRING/!wxUSE_STL_BASED_WXSTRING
1992 wxString
& operator=(const unsigned char *psz
)
1993 { return operator=((const char*)psz
); }
1995 // from wxScopedWCharBuffer
1996 wxString
& operator=(const wxScopedWCharBuffer
& s
)
1997 { return assign(s
); }
1998 // from wxScopedCharBuffer
1999 wxString
& operator=(const wxScopedCharBuffer
& s
)
2000 { return assign(s
); }
2002 // string concatenation
2003 // in place concatenation
2005 Concatenate and return the result. Note that the left to right
2006 associativity of << allows to write things like "str << str1 << str2
2007 << ..." (unlike with +=)
2010 wxString
& operator<<(const wxString
& s
)
2012 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2013 wxASSERT_MSG( s
.IsValid(),
2014 wxT("did you forget to call UngetWriteBuf()?") );
2020 // string += C string
2021 wxString
& operator<<(const char *psz
)
2022 { append(psz
); return *this; }
2023 wxString
& operator<<(const wchar_t *pwz
)
2024 { append(pwz
); return *this; }
2025 wxString
& operator<<(const wxCStrData
& psz
)
2026 { append(psz
.AsString()); return *this; }
2028 wxString
& operator<<(wxUniChar ch
) { append(1, ch
); return *this; }
2029 wxString
& operator<<(wxUniCharRef ch
) { append(1, ch
); return *this; }
2030 wxString
& operator<<(char ch
) { append(1, ch
); return *this; }
2031 wxString
& operator<<(unsigned char ch
) { append(1, ch
); return *this; }
2032 wxString
& operator<<(wchar_t ch
) { append(1, ch
); return *this; }
2034 // string += buffer (i.e. from wxGetString)
2035 wxString
& operator<<(const wxScopedWCharBuffer
& s
)
2036 { return append(s
); }
2037 wxString
& operator<<(const wxScopedCharBuffer
& s
)
2038 { return append(s
); }
2040 // string += C string
2041 wxString
& Append(const wxString
& s
)
2043 // test for empty() to share the string if possible
2050 wxString
& Append(const char* psz
)
2051 { append(psz
); return *this; }
2052 wxString
& Append(const wchar_t* pwz
)
2053 { append(pwz
); return *this; }
2054 wxString
& Append(const wxCStrData
& psz
)
2055 { append(psz
); return *this; }
2056 wxString
& Append(const wxScopedCharBuffer
& psz
)
2057 { append(psz
); return *this; }
2058 wxString
& Append(const wxScopedWCharBuffer
& psz
)
2059 { append(psz
); return *this; }
2060 wxString
& Append(const char* psz
, size_t nLen
)
2061 { append(psz
, nLen
); return *this; }
2062 wxString
& Append(const wchar_t* pwz
, size_t nLen
)
2063 { append(pwz
, nLen
); return *this; }
2064 wxString
& Append(const wxCStrData
& psz
, size_t nLen
)
2065 { append(psz
, nLen
); return *this; }
2066 wxString
& Append(const wxScopedCharBuffer
& psz
, size_t nLen
)
2067 { append(psz
, nLen
); return *this; }
2068 wxString
& Append(const wxScopedWCharBuffer
& psz
, size_t nLen
)
2069 { append(psz
, nLen
); return *this; }
2070 // append count copies of given character
2071 wxString
& Append(wxUniChar ch
, size_t count
= 1u)
2072 { append(count
, ch
); return *this; }
2073 wxString
& Append(wxUniCharRef ch
, size_t count
= 1u)
2074 { append(count
, ch
); return *this; }
2075 wxString
& Append(char ch
, size_t count
= 1u)
2076 { append(count
, ch
); return *this; }
2077 wxString
& Append(unsigned char ch
, size_t count
= 1u)
2078 { append(count
, ch
); return *this; }
2079 wxString
& Append(wchar_t ch
, size_t count
= 1u)
2080 { append(count
, ch
); return *this; }
2082 // prepend a string, return the string itself
2083 wxString
& Prepend(const wxString
& str
)
2084 { *this = str
+ *this; return *this; }
2086 // non-destructive concatenation
2088 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
,
2089 const wxString
& string2
);
2090 // string with a single char
2091 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
2092 // char with a string
2093 friend wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
2094 // string with C string
2095 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
2097 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
2098 const wchar_t *pwz
);
2099 // C string with string
2100 friend wxString WXDLLIMPEXP_BASE
operator+(const char *psz
,
2101 const wxString
& string
);
2102 friend wxString WXDLLIMPEXP_BASE
operator+(const wchar_t *pwz
,
2103 const wxString
& string
);
2105 // stream-like functions
2106 // insert an int into string
2107 wxString
& operator<<(int i
)
2108 { return (*this) << Format(wxT("%d"), i
); }
2109 // insert an unsigned int into string
2110 wxString
& operator<<(unsigned int ui
)
2111 { return (*this) << Format(wxT("%u"), ui
); }
2112 // insert a long into string
2113 wxString
& operator<<(long l
)
2114 { return (*this) << Format(wxT("%ld"), l
); }
2115 // insert an unsigned long into string
2116 wxString
& operator<<(unsigned long ul
)
2117 { return (*this) << Format(wxT("%lu"), ul
); }
2118 #if defined wxLongLong_t && !defined wxLongLongIsLong
2119 // insert a long long if they exist and aren't longs
2120 wxString
& operator<<(wxLongLong_t ll
)
2122 return (*this) << Format("%" wxLongLongFmtSpec
"d", ll
);
2124 // insert an unsigned long long
2125 wxString
& operator<<(wxULongLong_t ull
)
2127 return (*this) << Format("%" wxLongLongFmtSpec
"u" , ull
);
2129 #endif // wxLongLong_t && !wxLongLongIsLong
2130 // insert a float into string
2131 wxString
& operator<<(float f
)
2132 { return (*this) << Format(wxT("%f"), f
); }
2133 // insert a double into string
2134 wxString
& operator<<(double d
)
2135 { return (*this) << Format(wxT("%g"), d
); }
2137 // string comparison
2138 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
2139 int Cmp(const char *psz
) const
2140 { return compare(psz
); }
2141 int Cmp(const wchar_t *pwz
) const
2142 { return compare(pwz
); }
2143 int Cmp(const wxString
& s
) const
2144 { return compare(s
); }
2145 int Cmp(const wxCStrData
& s
) const
2146 { return compare(s
); }
2147 int Cmp(const wxScopedCharBuffer
& s
) const
2148 { return compare(s
); }
2149 int Cmp(const wxScopedWCharBuffer
& s
) const
2150 { return compare(s
); }
2151 // same as Cmp() but not case-sensitive
2152 int CmpNoCase(const wxString
& s
) const;
2154 // test for the string equality, either considering case or not
2155 // (if compareWithCase then the case matters)
2156 bool IsSameAs(const wxString
& str
, bool compareWithCase
= true) const
2158 #if !wxUSE_UNICODE_UTF8
2159 // in UTF-8 build, length() is O(n) and doing this would be _slower_
2160 if ( length() != str
.length() )
2163 return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0;
2165 bool IsSameAs(const char *str
, bool compareWithCase
= true) const
2166 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
2167 bool IsSameAs(const wchar_t *str
, bool compareWithCase
= true) const
2168 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
2170 bool IsSameAs(const wxCStrData
& str
, bool compareWithCase
= true) const
2171 { return IsSameAs(str
.AsString(), compareWithCase
); }
2172 bool IsSameAs(const wxScopedCharBuffer
& str
, bool compareWithCase
= true) const
2173 { return IsSameAs(str
.data(), compareWithCase
); }
2174 bool IsSameAs(const wxScopedWCharBuffer
& str
, bool compareWithCase
= true) const
2175 { return IsSameAs(str
.data(), compareWithCase
); }
2176 // comparison with a single character: returns true if equal
2177 bool IsSameAs(wxUniChar c
, bool compareWithCase
= true) const;
2178 // FIXME-UTF8: remove these overloads
2179 bool IsSameAs(wxUniCharRef c
, bool compareWithCase
= true) const
2180 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
2181 bool IsSameAs(char c
, bool compareWithCase
= true) const
2182 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
2183 bool IsSameAs(unsigned char c
, bool compareWithCase
= true) const
2184 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
2185 bool IsSameAs(wchar_t c
, bool compareWithCase
= true) const
2186 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
2187 bool IsSameAs(int c
, bool compareWithCase
= true) const
2188 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
2190 // simple sub-string extraction
2191 // return substring starting at nFirst of length nCount (or till the end
2192 // if nCount = default value)
2193 wxString
Mid(size_t nFirst
, size_t nCount
= npos
) const;
2195 // operator version of Mid()
2196 wxString
operator()(size_t start
, size_t len
) const
2197 { return Mid(start
, len
); }
2199 // check if the string starts with the given prefix and return the rest
2200 // of the string in the provided pointer if it is not NULL; otherwise
2202 bool StartsWith(const wxString
& prefix
, wxString
*rest
= NULL
) const;
2203 // check if the string ends with the given suffix and return the
2204 // beginning of the string before the suffix in the provided pointer if
2205 // it is not NULL; otherwise return false
2206 bool EndsWith(const wxString
& suffix
, wxString
*rest
= NULL
) const;
2208 // get first nCount characters
2209 wxString
Left(size_t nCount
) const;
2210 // get last nCount characters
2211 wxString
Right(size_t nCount
) const;
2212 // get all characters before the first occurrence of ch
2213 // (returns the whole string if ch not found) and also put everything
2214 // following the first occurrence of ch into rest if it's non-NULL
2215 wxString
BeforeFirst(wxUniChar ch
, wxString
*rest
= NULL
) const;
2216 // get all characters before the last occurrence of ch
2217 // (returns empty string if ch not found) and also put everything
2218 // following the last occurrence of ch into rest if it's non-NULL
2219 wxString
BeforeLast(wxUniChar ch
, wxString
*rest
= NULL
) const;
2220 // get all characters after the first occurrence of ch
2221 // (returns empty string if ch not found)
2222 wxString
AfterFirst(wxUniChar ch
) const;
2223 // get all characters after the last occurrence of ch
2224 // (returns the whole string if ch not found)
2225 wxString
AfterLast(wxUniChar ch
) const;
2227 // for compatibility only, use more explicitly named functions above
2228 wxString
Before(wxUniChar ch
) const { return BeforeLast(ch
); }
2229 wxString
After(wxUniChar ch
) const { return AfterFirst(ch
); }
2232 // convert to upper case in place, return the string itself
2233 wxString
& MakeUpper();
2234 // convert to upper case, return the copy of the string
2235 wxString
Upper() const { return wxString(*this).MakeUpper(); }
2236 // convert to lower case in place, return the string itself
2237 wxString
& MakeLower();
2238 // convert to lower case, return the copy of the string
2239 wxString
Lower() const { return wxString(*this).MakeLower(); }
2240 // convert the first character to the upper case and the rest to the
2241 // lower one, return the modified string itself
2242 wxString
& MakeCapitalized();
2243 // convert the first character to the upper case and the rest to the
2244 // lower one, return the copy of the string
2245 wxString
Capitalize() const { return wxString(*this).MakeCapitalized(); }
2247 // trimming/padding whitespace (either side) and truncating
2248 // remove spaces from left or from right (default) side
2249 wxString
& Trim(bool bFromRight
= true);
2250 // add nCount copies chPad in the beginning or at the end (default)
2251 wxString
& Pad(size_t nCount
, wxUniChar chPad
= wxT(' '), bool bFromRight
= true);
2253 // searching and replacing
2254 // searching (return starting index, or -1 if not found)
2255 int Find(wxUniChar ch
, bool bFromEnd
= false) const; // like strchr/strrchr
2256 int Find(wxUniCharRef ch
, bool bFromEnd
= false) const
2257 { return Find(wxUniChar(ch
), bFromEnd
); }
2258 int Find(char ch
, bool bFromEnd
= false) const
2259 { return Find(wxUniChar(ch
), bFromEnd
); }
2260 int Find(unsigned char ch
, bool bFromEnd
= false) const
2261 { return Find(wxUniChar(ch
), bFromEnd
); }
2262 int Find(wchar_t ch
, bool bFromEnd
= false) const
2263 { return Find(wxUniChar(ch
), bFromEnd
); }
2264 // searching (return starting index, or -1 if not found)
2265 int Find(const wxString
& sub
) const // like strstr
2267 size_type idx
= find(sub
);
2268 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
2270 int Find(const char *sub
) const // like strstr
2272 size_type idx
= find(sub
);
2273 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
2275 int Find(const wchar_t *sub
) const // like strstr
2277 size_type idx
= find(sub
);
2278 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
2281 int Find(const wxCStrData
& sub
) const
2282 { return Find(sub
.AsString()); }
2283 int Find(const wxScopedCharBuffer
& sub
) const
2284 { return Find(sub
.data()); }
2285 int Find(const wxScopedWCharBuffer
& sub
) const
2286 { return Find(sub
.data()); }
2288 // replace first (or all of bReplaceAll) occurrences of substring with
2289 // another string, returns the number of replacements made
2290 size_t Replace(const wxString
& strOld
,
2291 const wxString
& strNew
,
2292 bool bReplaceAll
= true);
2294 // check if the string contents matches a mask containing '*' and '?'
2295 bool Matches(const wxString
& mask
) const;
2297 // conversion to numbers: all functions return true only if the whole
2298 // string is a number and put the value of this number into the pointer
2299 // provided, the base is the numeric base in which the conversion should be
2300 // done and must be comprised between 2 and 36 or be 0 in which case the
2301 // standard C rules apply (leading '0' => octal, "0x" => hex)
2302 // convert to a signed integer
2303 bool ToLong(long *val
, int base
= 10) const;
2304 // convert to an unsigned integer
2305 bool ToULong(unsigned long *val
, int base
= 10) const;
2306 // convert to wxLongLong
2307 #if defined(wxLongLong_t)
2308 bool ToLongLong(wxLongLong_t
*val
, int base
= 10) const;
2309 // convert to wxULongLong
2310 bool ToULongLong(wxULongLong_t
*val
, int base
= 10) const;
2311 #endif // wxLongLong_t
2312 // convert to a double
2313 bool ToDouble(double *val
) const;
2315 // conversions to numbers using C locale
2316 // convert to a signed integer
2317 bool ToCLong(long *val
, int base
= 10) const;
2318 // convert to an unsigned integer
2319 bool ToCULong(unsigned long *val
, int base
= 10) const;
2320 // convert to a double
2321 bool ToCDouble(double *val
) const;
2323 // create a string representing the given floating point number
2324 // in the current locale
2325 static wxString
FromDouble(double val
)
2326 { return wxString::Format(wxS("%g"), val
); }
2328 static wxString
FromCDouble(double val
);
2330 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2331 // formatted input/output
2332 // as sprintf(), returns the number of characters written or < 0 on error
2333 // (take 'this' into account in attribute parameter count)
2334 // int Printf(const wxString& format, ...);
2335 WX_DEFINE_VARARG_FUNC(int, Printf
, 1, (const wxFormatString
&),
2336 DoPrintfWchar
, DoPrintfUtf8
)
2338 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
2339 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wxString
&),
2340 (wxFormatString(f1
)));
2341 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wxCStrData
&),
2342 (wxFormatString(f1
)));
2343 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const char*),
2344 (wxFormatString(f1
)));
2345 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wchar_t*),
2346 (wxFormatString(f1
)));
2348 #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN
2349 // as vprintf(), returns the number of characters written or < 0 on error
2350 int PrintfV(const wxString
& format
, va_list argptr
);
2352 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2353 // returns the string containing the result of Printf() to it
2354 // static wxString Format(const wxString& format, ...) WX_ATTRIBUTE_PRINTF_1;
2355 WX_DEFINE_VARARG_FUNC(static wxString
, Format
, 1, (const wxFormatString
&),
2356 DoFormatWchar
, DoFormatUtf8
)
2358 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
2359 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wxString
&),
2360 (wxFormatString(f1
)));
2361 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wxCStrData
&),
2362 (wxFormatString(f1
)));
2363 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const char*),
2364 (wxFormatString(f1
)));
2365 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wchar_t*),
2366 (wxFormatString(f1
)));
2369 // the same as above, but takes a va_list
2370 static wxString
FormatV(const wxString
& format
, va_list argptr
);
2372 // raw access to string memory
2373 // ensure that string has space for at least nLen characters
2374 // only works if the data of this string is not shared
2375 bool Alloc(size_t nLen
) { reserve(nLen
); return capacity() >= nLen
; }
2376 // minimize the string's memory
2377 // only works if the data of this string is not shared
2379 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2380 // These are deprecated, use wxStringBuffer or wxStringBufferLength instead
2382 // get writable buffer of at least nLen bytes. Unget() *must* be called
2383 // a.s.a.p. to put string back in a reasonable state!
2384 wxDEPRECATED( wxStringCharType
*GetWriteBuf(size_t nLen
) );
2385 // call this immediately after GetWriteBuf() has been used
2386 wxDEPRECATED( void UngetWriteBuf() );
2387 wxDEPRECATED( void UngetWriteBuf(size_t nLen
) );
2388 #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && wxUSE_UNICODE_UTF8
2390 // wxWidgets version 1 compatibility functions
2393 wxString
SubString(size_t from
, size_t to
) const
2394 { return Mid(from
, (to
- from
+ 1)); }
2395 // values for second parameter of CompareTo function
2396 enum caseCompare
{exact
, ignoreCase
};
2397 // values for first parameter of Strip function
2398 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
2400 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2402 // (take 'this' into account in attribute parameter count)
2403 // int sprintf(const wxString& format, ...) WX_ATTRIBUTE_PRINTF_2;
2404 WX_DEFINE_VARARG_FUNC(int, sprintf
, 1, (const wxFormatString
&),
2405 DoPrintfWchar
, DoPrintfUtf8
)
2407 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
2408 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wxString
&),
2409 (wxFormatString(f1
)));
2410 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wxCStrData
&),
2411 (wxFormatString(f1
)));
2412 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const char*),
2413 (wxFormatString(f1
)));
2414 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wchar_t*),
2415 (wxFormatString(f1
)));
2417 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
2420 int CompareTo(const wxChar
* psz
, caseCompare cmp
= exact
) const
2421 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
2424 size_t Length() const { return length(); }
2425 // Count the number of characters
2426 int Freq(wxUniChar ch
) const;
2428 void LowerCase() { MakeLower(); }
2430 void UpperCase() { MakeUpper(); }
2431 // use Trim except that it doesn't change this string
2432 wxString
Strip(stripType w
= trailing
) const;
2434 // use Find (more general variants not yet supported)
2435 size_t Index(const wxChar
* psz
) const { return Find(psz
); }
2436 size_t Index(wxUniChar ch
) const { return Find(ch
); }
2438 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
2439 wxString
& RemoveLast(size_t n
= 1) { return Truncate(length() - n
); }
2441 wxString
& Remove(size_t nStart
, size_t nLen
)
2442 { return (wxString
&)erase( nStart
, nLen
); }
2445 int First( wxUniChar ch
) const { return Find(ch
); }
2446 int First( wxUniCharRef ch
) const { return Find(ch
); }
2447 int First( char ch
) const { return Find(ch
); }
2448 int First( unsigned char ch
) const { return Find(ch
); }
2449 int First( wchar_t ch
) const { return Find(ch
); }
2450 int First( const wxString
& str
) const { return Find(str
); }
2451 int Last( wxUniChar ch
) const { return Find(ch
, true); }
2452 bool Contains(const wxString
& str
) const { return Find(str
) != wxNOT_FOUND
; }
2455 bool IsNull() const { return empty(); }
2457 // std::string compatibility functions
2459 // take nLen chars starting at nPos
2460 wxString(const wxString
& str
, size_t nPos
, size_t nLen
)
2461 { assign(str
, nPos
, nLen
); }
2462 // take all characters from first to last
2463 wxString(const_iterator first
, const_iterator last
)
2464 : m_impl(first
.impl(), last
.impl()) { }
2465 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2466 // the 2 overloads below are for compatibility with the existing code using
2467 // pointers instead of iterators
2468 wxString(const char *first
, const char *last
)
2470 SubstrBufFromMB
str(ImplStr(first
, last
- first
));
2471 m_impl
.assign(str
.data
, str
.len
);
2473 wxString(const wchar_t *first
, const wchar_t *last
)
2475 SubstrBufFromWC
str(ImplStr(first
, last
- first
));
2476 m_impl
.assign(str
.data
, str
.len
);
2478 // and this one is needed to compile code adding offsets to c_str() result
2479 wxString(const wxCStrData
& first
, const wxCStrData
& last
)
2480 : m_impl(CreateConstIterator(first
).impl(),
2481 CreateConstIterator(last
).impl())
2483 wxASSERT_MSG( first
.m_str
== last
.m_str
,
2484 wxT("pointers must be into the same string") );
2486 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2488 // lib.string.modifiers
2489 // append elements str[pos], ..., str[pos+n]
2490 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
2492 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2495 str
.PosLenToImpl(pos
, n
, &from
, &len
);
2496 m_impl
.append(str
.m_impl
, from
, len
);
2500 wxString
& append(const wxString
& str
)
2502 wxSTRING_UPDATE_CACHED_LENGTH(str
.length());
2504 m_impl
.append(str
.m_impl
);
2508 // append first n (or all if n == npos) characters of sz
2509 wxString
& append(const char *sz
)
2511 wxSTRING_INVALIDATE_CACHED_LENGTH();
2513 m_impl
.append(ImplStr(sz
));
2517 wxString
& append(const wchar_t *sz
)
2519 wxSTRING_INVALIDATE_CACHED_LENGTH();
2521 m_impl
.append(ImplStr(sz
));
2525 wxString
& append(const char *sz
, size_t n
)
2527 wxSTRING_INVALIDATE_CACHED_LENGTH();
2529 SubstrBufFromMB
str(ImplStr(sz
, n
));
2530 m_impl
.append(str
.data
, str
.len
);
2533 wxString
& append(const wchar_t *sz
, size_t n
)
2535 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2537 SubstrBufFromWC
str(ImplStr(sz
, n
));
2538 m_impl
.append(str
.data
, str
.len
);
2542 wxString
& append(const wxCStrData
& str
)
2543 { return append(str
.AsString()); }
2544 wxString
& append(const wxScopedCharBuffer
& str
)
2545 { return append(str
.data(), str
.length()); }
2546 wxString
& append(const wxScopedWCharBuffer
& str
)
2547 { return append(str
.data(), str
.length()); }
2548 wxString
& append(const wxCStrData
& str
, size_t n
)
2549 { return append(str
.AsString(), 0, n
); }
2550 wxString
& append(const wxScopedCharBuffer
& str
, size_t n
)
2551 { return append(str
.data(), n
); }
2552 wxString
& append(const wxScopedWCharBuffer
& str
, size_t n
)
2553 { return append(str
.data(), n
); }
2555 // append n copies of ch
2556 wxString
& append(size_t n
, wxUniChar ch
)
2558 #if wxUSE_UNICODE_UTF8
2559 if ( !ch
.IsAscii() )
2561 wxSTRING_INVALIDATE_CACHED_LENGTH();
2563 m_impl
.append(wxStringOperations::EncodeNChars(n
, ch
));
2568 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2570 m_impl
.append(n
, (wxStringCharType
)ch
);
2576 wxString
& append(size_t n
, wxUniCharRef ch
)
2577 { return append(n
, wxUniChar(ch
)); }
2578 wxString
& append(size_t n
, char ch
)
2579 { return append(n
, wxUniChar(ch
)); }
2580 wxString
& append(size_t n
, unsigned char ch
)
2581 { return append(n
, wxUniChar(ch
)); }
2582 wxString
& append(size_t n
, wchar_t ch
)
2583 { return append(n
, wxUniChar(ch
)); }
2585 // append from first to last
2586 wxString
& append(const_iterator first
, const_iterator last
)
2588 wxSTRING_INVALIDATE_CACHED_LENGTH();
2590 m_impl
.append(first
.impl(), last
.impl());
2593 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2594 wxString
& append(const char *first
, const char *last
)
2595 { return append(first
, last
- first
); }
2596 wxString
& append(const wchar_t *first
, const wchar_t *last
)
2597 { return append(first
, last
- first
); }
2598 wxString
& append(const wxCStrData
& first
, const wxCStrData
& last
)
2599 { return append(CreateConstIterator(first
), CreateConstIterator(last
)); }
2600 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2602 // same as `this_string = str'
2603 wxString
& assign(const wxString
& str
)
2605 wxSTRING_SET_CACHED_LENGTH(str
.length());
2607 m_impl
= str
.m_impl
;
2612 wxString
& assign(const wxString
& str
, size_t len
)
2614 wxSTRING_SET_CACHED_LENGTH(len
);
2616 m_impl
.assign(str
.m_impl
, 0, str
.LenToImpl(len
));
2621 // same as ` = str[pos..pos + n]
2622 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
2625 str
.PosLenToImpl(pos
, n
, &from
, &len
);
2626 m_impl
.assign(str
.m_impl
, from
, len
);
2628 // it's important to call this after PosLenToImpl() above in case str is
2629 // the same string as this one
2630 wxSTRING_SET_CACHED_LENGTH(n
);
2635 // same as `= first n (or all if n == npos) characters of sz'
2636 wxString
& assign(const char *sz
)
2638 wxSTRING_INVALIDATE_CACHE();
2640 m_impl
.assign(ImplStr(sz
));
2645 wxString
& assign(const wchar_t *sz
)
2647 wxSTRING_INVALIDATE_CACHE();
2649 m_impl
.assign(ImplStr(sz
));
2654 wxString
& assign(const char *sz
, size_t n
)
2656 wxSTRING_SET_CACHED_LENGTH(n
);
2658 SubstrBufFromMB
str(ImplStr(sz
, n
));
2659 m_impl
.assign(str
.data
, str
.len
);
2664 wxString
& assign(const wchar_t *sz
, size_t n
)
2666 wxSTRING_SET_CACHED_LENGTH(n
);
2668 SubstrBufFromWC
str(ImplStr(sz
, n
));
2669 m_impl
.assign(str
.data
, str
.len
);
2674 wxString
& assign(const wxCStrData
& str
)
2675 { return assign(str
.AsString()); }
2676 wxString
& assign(const wxScopedCharBuffer
& str
)
2677 { return assign(str
.data(), str
.length()); }
2678 wxString
& assign(const wxScopedWCharBuffer
& str
)
2679 { return assign(str
.data(), str
.length()); }
2680 wxString
& assign(const wxCStrData
& str
, size_t len
)
2681 { return assign(str
.AsString(), len
); }
2682 wxString
& assign(const wxScopedCharBuffer
& str
, size_t len
)
2683 { return assign(str
.data(), len
); }
2684 wxString
& assign(const wxScopedWCharBuffer
& str
, size_t len
)
2685 { return assign(str
.data(), len
); }
2687 // same as `= n copies of ch'
2688 wxString
& assign(size_t n
, wxUniChar ch
)
2690 wxSTRING_SET_CACHED_LENGTH(n
);
2692 #if wxUSE_UNICODE_UTF8
2693 if ( !ch
.IsAscii() )
2694 m_impl
.assign(wxStringOperations::EncodeNChars(n
, ch
));
2697 m_impl
.assign(n
, (wxStringCharType
)ch
);
2702 wxString
& assign(size_t n
, wxUniCharRef ch
)
2703 { return assign(n
, wxUniChar(ch
)); }
2704 wxString
& assign(size_t n
, char ch
)
2705 { return assign(n
, wxUniChar(ch
)); }
2706 wxString
& assign(size_t n
, unsigned char ch
)
2707 { return assign(n
, wxUniChar(ch
)); }
2708 wxString
& assign(size_t n
, wchar_t ch
)
2709 { return assign(n
, wxUniChar(ch
)); }
2711 // assign from first to last
2712 wxString
& assign(const_iterator first
, const_iterator last
)
2714 wxSTRING_INVALIDATE_CACHE();
2716 m_impl
.assign(first
.impl(), last
.impl());
2720 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2721 wxString
& assign(const char *first
, const char *last
)
2722 { return assign(first
, last
- first
); }
2723 wxString
& assign(const wchar_t *first
, const wchar_t *last
)
2724 { return assign(first
, last
- first
); }
2725 wxString
& assign(const wxCStrData
& first
, const wxCStrData
& last
)
2726 { return assign(CreateConstIterator(first
), CreateConstIterator(last
)); }
2727 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2729 // string comparison
2730 int compare(const wxString
& str
) const;
2731 int compare(const char* sz
) const;
2732 int compare(const wchar_t* sz
) const;
2733 int compare(const wxCStrData
& str
) const
2734 { return compare(str
.AsString()); }
2735 int compare(const wxScopedCharBuffer
& str
) const
2736 { return compare(str
.data()); }
2737 int compare(const wxScopedWCharBuffer
& str
) const
2738 { return compare(str
.data()); }
2739 // comparison with a substring
2740 int compare(size_t nStart
, size_t nLen
, const wxString
& str
) const;
2741 // comparison of 2 substrings
2742 int compare(size_t nStart
, size_t nLen
,
2743 const wxString
& str
, size_t nStart2
, size_t nLen2
) const;
2744 // substring comparison with first nCount characters of sz
2745 int compare(size_t nStart
, size_t nLen
,
2746 const char* sz
, size_t nCount
= npos
) const;
2747 int compare(size_t nStart
, size_t nLen
,
2748 const wchar_t* sz
, size_t nCount
= npos
) const;
2750 // insert another string
2751 wxString
& insert(size_t nPos
, const wxString
& str
)
2752 { insert(GetIterForNthChar(nPos
), str
.begin(), str
.end()); return *this; }
2753 // insert n chars of str starting at nStart (in str)
2754 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
2756 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2759 str
.PosLenToImpl(nStart
, n
, &from
, &len
);
2760 m_impl
.insert(PosToImpl(nPos
), str
.m_impl
, from
, len
);
2765 // insert first n (or all if n == npos) characters of sz
2766 wxString
& insert(size_t nPos
, const char *sz
)
2768 wxSTRING_INVALIDATE_CACHE();
2770 m_impl
.insert(PosToImpl(nPos
), ImplStr(sz
));
2775 wxString
& insert(size_t nPos
, const wchar_t *sz
)
2777 wxSTRING_INVALIDATE_CACHE();
2779 m_impl
.insert(PosToImpl(nPos
), ImplStr(sz
)); return *this;
2782 wxString
& insert(size_t nPos
, const char *sz
, size_t n
)
2784 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2786 SubstrBufFromMB
str(ImplStr(sz
, n
));
2787 m_impl
.insert(PosToImpl(nPos
), str
.data
, str
.len
);
2792 wxString
& insert(size_t nPos
, const wchar_t *sz
, size_t n
)
2794 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2796 SubstrBufFromWC
str(ImplStr(sz
, n
));
2797 m_impl
.insert(PosToImpl(nPos
), str
.data
, str
.len
);
2802 // insert n copies of ch
2803 wxString
& insert(size_t nPos
, size_t n
, wxUniChar ch
)
2805 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2807 #if wxUSE_UNICODE_UTF8
2808 if ( !ch
.IsAscii() )
2809 m_impl
.insert(PosToImpl(nPos
), wxStringOperations::EncodeNChars(n
, ch
));
2812 m_impl
.insert(PosToImpl(nPos
), n
, (wxStringCharType
)ch
);
2816 iterator
insert(iterator it
, wxUniChar ch
)
2818 wxSTRING_UPDATE_CACHED_LENGTH(1);
2820 #if wxUSE_UNICODE_UTF8
2821 if ( !ch
.IsAscii() )
2823 size_t pos
= IterToImplPos(it
);
2824 m_impl
.insert(pos
, wxStringOperations::EncodeChar(ch
));
2825 return iterator(this, m_impl
.begin() + pos
);
2829 return iterator(this, m_impl
.insert(it
.impl(), (wxStringCharType
)ch
));
2832 void insert(iterator it
, const_iterator first
, const_iterator last
)
2834 wxSTRING_INVALIDATE_CACHE();
2836 m_impl
.insert(it
.impl(), first
.impl(), last
.impl());
2839 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2840 void insert(iterator it
, const char *first
, const char *last
)
2841 { insert(it
- begin(), first
, last
- first
); }
2842 void insert(iterator it
, const wchar_t *first
, const wchar_t *last
)
2843 { insert(it
- begin(), first
, last
- first
); }
2844 void insert(iterator it
, const wxCStrData
& first
, const wxCStrData
& last
)
2845 { insert(it
, CreateConstIterator(first
), CreateConstIterator(last
)); }
2846 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2848 void insert(iterator it
, size_type n
, wxUniChar ch
)
2850 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2852 #if wxUSE_UNICODE_UTF8
2853 if ( !ch
.IsAscii() )
2854 m_impl
.insert(IterToImplPos(it
), wxStringOperations::EncodeNChars(n
, ch
));
2857 m_impl
.insert(it
.impl(), n
, (wxStringCharType
)ch
);
2860 // delete characters from nStart to nStart + nLen
2861 wxString
& erase(size_type pos
= 0, size_type n
= npos
)
2863 wxSTRING_INVALIDATE_CACHE();
2866 PosLenToImpl(pos
, n
, &from
, &len
);
2867 m_impl
.erase(from
, len
);
2872 // delete characters from first up to last
2873 iterator
erase(iterator first
, iterator last
)
2875 wxSTRING_INVALIDATE_CACHE();
2877 return iterator(this, m_impl
.erase(first
.impl(), last
.impl()));
2880 iterator
erase(iterator first
)
2882 wxSTRING_UPDATE_CACHED_LENGTH(-1);
2884 return iterator(this, m_impl
.erase(first
.impl()));
2887 #ifdef wxSTRING_BASE_HASNT_CLEAR
2888 void clear() { erase(); }
2892 wxSTRING_SET_CACHED_LENGTH(0);
2898 // replaces the substring of length nLen starting at nStart
2899 wxString
& replace(size_t nStart
, size_t nLen
, const char* sz
)
2901 wxSTRING_INVALIDATE_CACHE();
2904 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2905 m_impl
.replace(from
, len
, ImplStr(sz
));
2910 wxString
& replace(size_t nStart
, size_t nLen
, const wchar_t* sz
)
2912 wxSTRING_INVALIDATE_CACHE();
2915 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2916 m_impl
.replace(from
, len
, ImplStr(sz
));
2921 // replaces the substring of length nLen starting at nStart
2922 wxString
& replace(size_t nStart
, size_t nLen
, const wxString
& str
)
2924 wxSTRING_INVALIDATE_CACHE();
2927 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2928 m_impl
.replace(from
, len
, str
.m_impl
);
2933 // replaces the substring with nCount copies of ch
2934 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxUniChar ch
)
2936 wxSTRING_INVALIDATE_CACHE();
2939 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2940 #if wxUSE_UNICODE_UTF8
2941 if ( !ch
.IsAscii() )
2942 m_impl
.replace(from
, len
, wxStringOperations::EncodeNChars(nCount
, ch
));
2945 m_impl
.replace(from
, len
, nCount
, (wxStringCharType
)ch
);
2950 // replaces a substring with another substring
2951 wxString
& replace(size_t nStart
, size_t nLen
,
2952 const wxString
& str
, size_t nStart2
, size_t nLen2
)
2954 wxSTRING_INVALIDATE_CACHE();
2957 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2960 str
.PosLenToImpl(nStart2
, nLen2
, &from2
, &len2
);
2962 m_impl
.replace(from
, len
, str
.m_impl
, from2
, len2
);
2967 // replaces the substring with first nCount chars of sz
2968 wxString
& replace(size_t nStart
, size_t nLen
,
2969 const char* sz
, size_t nCount
)
2971 wxSTRING_INVALIDATE_CACHE();
2974 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2976 SubstrBufFromMB
str(ImplStr(sz
, nCount
));
2978 m_impl
.replace(from
, len
, str
.data
, str
.len
);
2983 wxString
& replace(size_t nStart
, size_t nLen
,
2984 const wchar_t* sz
, size_t nCount
)
2986 wxSTRING_INVALIDATE_CACHE();
2989 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2991 SubstrBufFromWC
str(ImplStr(sz
, nCount
));
2993 m_impl
.replace(from
, len
, str
.data
, str
.len
);
2998 wxString
& replace(size_t nStart
, size_t nLen
,
2999 const wxString
& s
, size_t nCount
)
3001 wxSTRING_INVALIDATE_CACHE();
3004 PosLenToImpl(nStart
, nLen
, &from
, &len
);
3005 m_impl
.replace(from
, len
, s
.m_impl
.c_str(), s
.LenToImpl(nCount
));
3010 wxString
& replace(iterator first
, iterator last
, const char* s
)
3012 wxSTRING_INVALIDATE_CACHE();
3014 m_impl
.replace(first
.impl(), last
.impl(), ImplStr(s
));
3019 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
)
3021 wxSTRING_INVALIDATE_CACHE();
3023 m_impl
.replace(first
.impl(), last
.impl(), ImplStr(s
));
3028 wxString
& replace(iterator first
, iterator last
, const char* s
, size_type n
)
3030 wxSTRING_INVALIDATE_CACHE();
3032 SubstrBufFromMB
str(ImplStr(s
, n
));
3033 m_impl
.replace(first
.impl(), last
.impl(), str
.data
, str
.len
);
3038 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
, size_type n
)
3040 wxSTRING_INVALIDATE_CACHE();
3042 SubstrBufFromWC
str(ImplStr(s
, n
));
3043 m_impl
.replace(first
.impl(), last
.impl(), str
.data
, str
.len
);
3048 wxString
& replace(iterator first
, iterator last
, const wxString
& s
)
3050 wxSTRING_INVALIDATE_CACHE();
3052 m_impl
.replace(first
.impl(), last
.impl(), s
.m_impl
);
3057 wxString
& replace(iterator first
, iterator last
, size_type n
, wxUniChar ch
)
3059 wxSTRING_INVALIDATE_CACHE();
3061 #if wxUSE_UNICODE_UTF8
3062 if ( !ch
.IsAscii() )
3063 m_impl
.replace(first
.impl(), last
.impl(),
3064 wxStringOperations::EncodeNChars(n
, ch
));
3067 m_impl
.replace(first
.impl(), last
.impl(), n
, (wxStringCharType
)ch
);
3072 wxString
& replace(iterator first
, iterator last
,
3073 const_iterator first1
, const_iterator last1
)
3075 wxSTRING_INVALIDATE_CACHE();
3077 m_impl
.replace(first
.impl(), last
.impl(), first1
.impl(), last1
.impl());
3082 wxString
& replace(iterator first
, iterator last
,
3083 const char *first1
, const char *last1
)
3084 { replace(first
, last
, first1
, last1
- first1
); return *this; }
3085 wxString
& replace(iterator first
, iterator last
,
3086 const wchar_t *first1
, const wchar_t *last1
)
3087 { replace(first
, last
, first1
, last1
- first1
); return *this; }
3090 void swap(wxString
& str
)
3092 #if wxUSE_STRING_POS_CACHE
3093 // we modify not only this string but also the other one directly so we
3094 // need to invalidate cache for both of them (we could also try to
3095 // exchange their cache entries but it seems unlikely to be worth it)
3097 str
.InvalidateCache();
3098 #endif // wxUSE_STRING_POS_CACHE
3100 m_impl
.swap(str
.m_impl
);
3104 size_t find(const wxString
& str
, size_t nStart
= 0) const
3105 { return PosFromImpl(m_impl
.find(str
.m_impl
, PosToImpl(nStart
))); }
3107 // find first n characters of sz
3108 size_t find(const char* sz
, size_t nStart
= 0, size_t n
= npos
) const
3110 SubstrBufFromMB
str(ImplStr(sz
, n
));
3111 return PosFromImpl(m_impl
.find(str
.data
, PosToImpl(nStart
), str
.len
));
3113 size_t find(const wchar_t* sz
, size_t nStart
= 0, size_t n
= npos
) const
3115 SubstrBufFromWC
str(ImplStr(sz
, n
));
3116 return PosFromImpl(m_impl
.find(str
.data
, PosToImpl(nStart
), str
.len
));
3118 size_t find(const wxScopedCharBuffer
& s
, size_t nStart
= 0, size_t n
= npos
) const
3119 { return find(s
.data(), nStart
, n
); }
3120 size_t find(const wxScopedWCharBuffer
& s
, size_t nStart
= 0, size_t n
= npos
) const
3121 { return find(s
.data(), nStart
, n
); }
3122 size_t find(const wxCStrData
& s
, size_t nStart
= 0, size_t n
= npos
) const
3123 { return find(s
.AsWChar(), nStart
, n
); }
3125 // find the first occurrence of character ch after nStart
3126 size_t find(wxUniChar ch
, size_t nStart
= 0) const
3128 #if wxUSE_UNICODE_UTF8
3129 if ( !ch
.IsAscii() )
3130 return PosFromImpl(m_impl
.find(wxStringOperations::EncodeChar(ch
),
3131 PosToImpl(nStart
)));
3134 return PosFromImpl(m_impl
.find((wxStringCharType
)ch
,
3135 PosToImpl(nStart
)));
3138 size_t find(wxUniCharRef ch
, size_t nStart
= 0) const
3139 { return find(wxUniChar(ch
), nStart
); }
3140 size_t find(char ch
, size_t nStart
= 0) const
3141 { return find(wxUniChar(ch
), nStart
); }
3142 size_t find(unsigned char ch
, size_t nStart
= 0) const
3143 { return find(wxUniChar(ch
), nStart
); }
3144 size_t find(wchar_t ch
, size_t nStart
= 0) const
3145 { return find(wxUniChar(ch
), nStart
); }
3147 // rfind() family is exactly like find() but works right to left
3149 // as find, but from the end
3150 size_t rfind(const wxString
& str
, size_t nStart
= npos
) const
3151 { return PosFromImpl(m_impl
.rfind(str
.m_impl
, PosToImpl(nStart
))); }
3153 // as find, but from the end
3154 size_t rfind(const char* sz
, size_t nStart
= npos
, size_t n
= npos
) const
3156 SubstrBufFromMB
str(ImplStr(sz
, n
));
3157 return PosFromImpl(m_impl
.rfind(str
.data
, PosToImpl(nStart
), str
.len
));
3159 size_t rfind(const wchar_t* sz
, size_t nStart
= npos
, size_t n
= npos
) const
3161 SubstrBufFromWC
str(ImplStr(sz
, n
));
3162 return PosFromImpl(m_impl
.rfind(str
.data
, PosToImpl(nStart
), str
.len
));
3164 size_t rfind(const wxScopedCharBuffer
& s
, size_t nStart
= npos
, size_t n
= npos
) const
3165 { return rfind(s
.data(), nStart
, n
); }
3166 size_t rfind(const wxScopedWCharBuffer
& s
, size_t nStart
= npos
, size_t n
= npos
) const
3167 { return rfind(s
.data(), nStart
, n
); }
3168 size_t rfind(const wxCStrData
& s
, size_t nStart
= npos
, size_t n
= npos
) const
3169 { return rfind(s
.AsWChar(), nStart
, n
); }
3170 // as find, but from the end
3171 size_t rfind(wxUniChar ch
, size_t nStart
= npos
) const
3173 #if wxUSE_UNICODE_UTF8
3174 if ( !ch
.IsAscii() )
3175 return PosFromImpl(m_impl
.rfind(wxStringOperations::EncodeChar(ch
),
3176 PosToImpl(nStart
)));
3179 return PosFromImpl(m_impl
.rfind((wxStringCharType
)ch
,
3180 PosToImpl(nStart
)));
3182 size_t rfind(wxUniCharRef ch
, size_t nStart
= npos
) const
3183 { return rfind(wxUniChar(ch
), nStart
); }
3184 size_t rfind(char ch
, size_t nStart
= npos
) const
3185 { return rfind(wxUniChar(ch
), nStart
); }
3186 size_t rfind(unsigned char ch
, size_t nStart
= npos
) const
3187 { return rfind(wxUniChar(ch
), nStart
); }
3188 size_t rfind(wchar_t ch
, size_t nStart
= npos
) const
3189 { return rfind(wxUniChar(ch
), nStart
); }
3191 // find first/last occurrence of any character (not) in the set:
3192 #if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
3193 // FIXME-UTF8: this is not entirely correct, because it doesn't work if
3194 // sizeof(wchar_t)==2 and surrogates are present in the string;
3195 // should we care? Probably not.
3196 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
3197 { return m_impl
.find_first_of(str
.m_impl
, nStart
); }
3198 size_t find_first_of(const char* sz
, size_t nStart
= 0) const
3199 { return m_impl
.find_first_of(ImplStr(sz
), nStart
); }
3200 size_t find_first_of(const wchar_t* sz
, size_t nStart
= 0) const
3201 { return m_impl
.find_first_of(ImplStr(sz
), nStart
); }
3202 size_t find_first_of(const char* sz
, size_t nStart
, size_t n
) const
3203 { return m_impl
.find_first_of(ImplStr(sz
), nStart
, n
); }
3204 size_t find_first_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
3205 { return m_impl
.find_first_of(ImplStr(sz
), nStart
, n
); }
3206 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const
3207 { return m_impl
.find_first_of((wxChar
)c
, nStart
); }
3209 size_t find_last_of(const wxString
& str
, size_t nStart
= npos
) const
3210 { return m_impl
.find_last_of(str
.m_impl
, nStart
); }
3211 size_t find_last_of(const char* sz
, size_t nStart
= npos
) const
3212 { return m_impl
.find_last_of(ImplStr(sz
), nStart
); }
3213 size_t find_last_of(const wchar_t* sz
, size_t nStart
= npos
) const
3214 { return m_impl
.find_last_of(ImplStr(sz
), nStart
); }
3215 size_t find_last_of(const char* sz
, size_t nStart
, size_t n
) const
3216 { return m_impl
.find_last_of(ImplStr(sz
), nStart
, n
); }
3217 size_t find_last_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
3218 { return m_impl
.find_last_of(ImplStr(sz
), nStart
, n
); }
3219 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const
3220 { return m_impl
.find_last_of((wxChar
)c
, nStart
); }
3222 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
3223 { return m_impl
.find_first_not_of(str
.m_impl
, nStart
); }
3224 size_t find_first_not_of(const char* sz
, size_t nStart
= 0) const
3225 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
); }
3226 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
= 0) const
3227 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
); }
3228 size_t find_first_not_of(const char* sz
, size_t nStart
, size_t n
) const
3229 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
, n
); }
3230 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
3231 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
, n
); }
3232 size_t find_first_not_of(wxUniChar c
, size_t nStart
= 0) const
3233 { return m_impl
.find_first_not_of((wxChar
)c
, nStart
); }
3235 size_t find_last_not_of(const wxString
& str
, size_t nStart
= npos
) const
3236 { return m_impl
.find_last_not_of(str
.m_impl
, nStart
); }
3237 size_t find_last_not_of(const char* sz
, size_t nStart
= npos
) const
3238 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
); }
3239 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
= npos
) const
3240 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
); }
3241 size_t find_last_not_of(const char* sz
, size_t nStart
, size_t n
) const
3242 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
, n
); }
3243 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
3244 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
, n
); }
3245 size_t find_last_not_of(wxUniChar c
, size_t nStart
= npos
) const
3246 { return m_impl
.find_last_not_of((wxChar
)c
, nStart
); }
3248 // we can't use std::string implementation in UTF-8 build, because the
3249 // character sets would be interpreted wrongly:
3251 // as strpbrk() but starts at nStart, returns npos if not found
3252 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
3253 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3254 { return find_first_of(str
.wc_str(), nStart
); }
3256 { return find_first_of(str
.mb_str(), nStart
); }
3259 size_t find_first_of(const char* sz
, size_t nStart
= 0) const;
3260 size_t find_first_of(const wchar_t* sz
, size_t nStart
= 0) const;
3261 size_t find_first_of(const char* sz
, size_t nStart
, size_t n
) const;
3262 size_t find_first_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
3263 // same as find(char, size_t)
3264 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const
3265 { return find(c
, nStart
); }
3266 // find the last (starting from nStart) char from str in this string
3267 size_t find_last_of (const wxString
& str
, size_t nStart
= npos
) const
3268 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3269 { return find_last_of(str
.wc_str(), nStart
); }
3271 { return find_last_of(str
.mb_str(), nStart
); }
3274 size_t find_last_of (const char* sz
, size_t nStart
= npos
) const;
3275 size_t find_last_of (const wchar_t* sz
, size_t nStart
= npos
) const;
3276 size_t find_last_of(const char* sz
, size_t nStart
, size_t n
) const;
3277 size_t find_last_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
3279 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const
3280 { return rfind(c
, nStart
); }
3282 // find first/last occurrence of any character not in the set
3284 // as strspn() (starting from nStart), returns npos on failure
3285 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
3286 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3287 { return find_first_not_of(str
.wc_str(), nStart
); }
3289 { return find_first_not_of(str
.mb_str(), nStart
); }
3292 size_t find_first_not_of(const char* sz
, size_t nStart
= 0) const;
3293 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
= 0) const;
3294 size_t find_first_not_of(const char* sz
, size_t nStart
, size_t n
) const;
3295 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
3297 size_t find_first_not_of(wxUniChar ch
, size_t nStart
= 0) const;
3299 size_t find_last_not_of(const wxString
& str
, size_t nStart
= npos
) const
3300 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3301 { return find_last_not_of(str
.wc_str(), nStart
); }
3303 { return find_last_not_of(str
.mb_str(), nStart
); }
3306 size_t find_last_not_of(const char* sz
, size_t nStart
= npos
) const;
3307 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
= npos
) const;
3308 size_t find_last_not_of(const char* sz
, size_t nStart
, size_t n
) const;
3309 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
3311 size_t find_last_not_of(wxUniChar ch
, size_t nStart
= npos
) const;
3312 #endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not
3314 // provide char/wchar_t/wxUniCharRef overloads for char-finding functions
3315 // above to resolve ambiguities:
3316 size_t find_first_of(wxUniCharRef ch
, size_t nStart
= 0) const
3317 { return find_first_of(wxUniChar(ch
), nStart
); }
3318 size_t find_first_of(char ch
, size_t nStart
= 0) const
3319 { return find_first_of(wxUniChar(ch
), nStart
); }
3320 size_t find_first_of(unsigned char ch
, size_t nStart
= 0) const
3321 { return find_first_of(wxUniChar(ch
), nStart
); }
3322 size_t find_first_of(wchar_t ch
, size_t nStart
= 0) const
3323 { return find_first_of(wxUniChar(ch
), nStart
); }
3324 size_t find_last_of(wxUniCharRef ch
, size_t nStart
= npos
) const
3325 { return find_last_of(wxUniChar(ch
), nStart
); }
3326 size_t find_last_of(char ch
, size_t nStart
= npos
) const
3327 { return find_last_of(wxUniChar(ch
), nStart
); }
3328 size_t find_last_of(unsigned char ch
, size_t nStart
= npos
) const
3329 { return find_last_of(wxUniChar(ch
), nStart
); }
3330 size_t find_last_of(wchar_t ch
, size_t nStart
= npos
) const
3331 { return find_last_of(wxUniChar(ch
), nStart
); }
3332 size_t find_first_not_of(wxUniCharRef ch
, size_t nStart
= 0) const
3333 { return find_first_not_of(wxUniChar(ch
), nStart
); }
3334 size_t find_first_not_of(char ch
, size_t nStart
= 0) const
3335 { return find_first_not_of(wxUniChar(ch
), nStart
); }
3336 size_t find_first_not_of(unsigned char ch
, size_t nStart
= 0) const
3337 { return find_first_not_of(wxUniChar(ch
), nStart
); }
3338 size_t find_first_not_of(wchar_t ch
, size_t nStart
= 0) const
3339 { return find_first_not_of(wxUniChar(ch
), nStart
); }
3340 size_t find_last_not_of(wxUniCharRef ch
, size_t nStart
= npos
) const
3341 { return find_last_not_of(wxUniChar(ch
), nStart
); }
3342 size_t find_last_not_of(char ch
, size_t nStart
= npos
) const
3343 { return find_last_not_of(wxUniChar(ch
), nStart
); }
3344 size_t find_last_not_of(unsigned char ch
, size_t nStart
= npos
) const
3345 { return find_last_not_of(wxUniChar(ch
), nStart
); }
3346 size_t find_last_not_of(wchar_t ch
, size_t nStart
= npos
) const
3347 { return find_last_not_of(wxUniChar(ch
), nStart
); }
3349 // and additional overloads for the versions taking strings:
3350 size_t find_first_of(const wxCStrData
& sz
, size_t nStart
= 0) const
3351 { return find_first_of(sz
.AsString(), nStart
); }
3352 size_t find_first_of(const wxScopedCharBuffer
& sz
, size_t nStart
= 0) const
3353 { return find_first_of(sz
.data(), nStart
); }
3354 size_t find_first_of(const wxScopedWCharBuffer
& sz
, size_t nStart
= 0) const
3355 { return find_first_of(sz
.data(), nStart
); }
3356 size_t find_first_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
3357 { return find_first_of(sz
.AsWChar(), nStart
, n
); }
3358 size_t find_first_of(const wxScopedCharBuffer
& sz
, size_t nStart
, size_t n
) const
3359 { return find_first_of(sz
.data(), nStart
, n
); }
3360 size_t find_first_of(const wxScopedWCharBuffer
& sz
, size_t nStart
, size_t n
) const
3361 { return find_first_of(sz
.data(), nStart
, n
); }
3363 size_t find_last_of(const wxCStrData
& sz
, size_t nStart
= 0) const
3364 { return find_last_of(sz
.AsString(), nStart
); }
3365 size_t find_last_of(const wxScopedCharBuffer
& sz
, size_t nStart
= 0) const
3366 { return find_last_of(sz
.data(), nStart
); }
3367 size_t find_last_of(const wxScopedWCharBuffer
& sz
, size_t nStart
= 0) const
3368 { return find_last_of(sz
.data(), nStart
); }
3369 size_t find_last_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
3370 { return find_last_of(sz
.AsWChar(), nStart
, n
); }
3371 size_t find_last_of(const wxScopedCharBuffer
& sz
, size_t nStart
, size_t n
) const
3372 { return find_last_of(sz
.data(), nStart
, n
); }
3373 size_t find_last_of(const wxScopedWCharBuffer
& sz
, size_t nStart
, size_t n
) const
3374 { return find_last_of(sz
.data(), nStart
, n
); }
3376 size_t find_first_not_of(const wxCStrData
& sz
, size_t nStart
= 0) const
3377 { return find_first_not_of(sz
.AsString(), nStart
); }
3378 size_t find_first_not_of(const wxScopedCharBuffer
& sz
, size_t nStart
= 0) const
3379 { return find_first_not_of(sz
.data(), nStart
); }
3380 size_t find_first_not_of(const wxScopedWCharBuffer
& sz
, size_t nStart
= 0) const
3381 { return find_first_not_of(sz
.data(), nStart
); }
3382 size_t find_first_not_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
3383 { return find_first_not_of(sz
.AsWChar(), nStart
, n
); }
3384 size_t find_first_not_of(const wxScopedCharBuffer
& sz
, size_t nStart
, size_t n
) const
3385 { return find_first_not_of(sz
.data(), nStart
, n
); }
3386 size_t find_first_not_of(const wxScopedWCharBuffer
& sz
, size_t nStart
, size_t n
) const
3387 { return find_first_not_of(sz
.data(), nStart
, n
); }
3389 size_t find_last_not_of(const wxCStrData
& sz
, size_t nStart
= 0) const
3390 { return find_last_not_of(sz
.AsString(), nStart
); }
3391 size_t find_last_not_of(const wxScopedCharBuffer
& sz
, size_t nStart
= 0) const
3392 { return find_last_not_of(sz
.data(), nStart
); }
3393 size_t find_last_not_of(const wxScopedWCharBuffer
& sz
, size_t nStart
= 0) const
3394 { return find_last_not_of(sz
.data(), nStart
); }
3395 size_t find_last_not_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
3396 { return find_last_not_of(sz
.AsWChar(), nStart
, n
); }
3397 size_t find_last_not_of(const wxScopedCharBuffer
& sz
, size_t nStart
, size_t n
) const
3398 { return find_last_not_of(sz
.data(), nStart
, n
); }
3399 size_t find_last_not_of(const wxScopedWCharBuffer
& sz
, size_t nStart
, size_t n
) const
3400 { return find_last_not_of(sz
.data(), nStart
, n
); }
3403 wxString
& operator+=(const wxString
& s
)
3405 wxSTRING_INVALIDATE_CACHED_LENGTH();
3410 // string += C string
3411 wxString
& operator+=(const char *psz
)
3413 wxSTRING_INVALIDATE_CACHED_LENGTH();
3415 m_impl
+= ImplStr(psz
);
3418 wxString
& operator+=(const wchar_t *pwz
)
3420 wxSTRING_INVALIDATE_CACHED_LENGTH();
3422 m_impl
+= ImplStr(pwz
);
3425 wxString
& operator+=(const wxCStrData
& s
)
3427 wxSTRING_INVALIDATE_CACHED_LENGTH();
3429 m_impl
+= s
.AsString().m_impl
;
3432 wxString
& operator+=(const wxScopedCharBuffer
& s
)
3433 { return append(s
); }
3434 wxString
& operator+=(const wxScopedWCharBuffer
& s
)
3435 { return append(s
); }
3437 wxString
& operator+=(wxUniChar ch
)
3439 wxSTRING_UPDATE_CACHED_LENGTH(1);
3441 #if wxUSE_UNICODE_UTF8
3442 if ( !ch
.IsAscii() )
3443 m_impl
+= wxStringOperations::EncodeChar(ch
);
3446 m_impl
+= (wxStringCharType
)ch
;
3449 wxString
& operator+=(wxUniCharRef ch
) { return *this += wxUniChar(ch
); }
3450 wxString
& operator+=(int ch
) { return *this += wxUniChar(ch
); }
3451 wxString
& operator+=(char ch
) { return *this += wxUniChar(ch
); }
3452 wxString
& operator+=(unsigned char ch
) { return *this += wxUniChar(ch
); }
3453 wxString
& operator+=(wchar_t ch
) { return *this += wxUniChar(ch
); }
3456 #if !wxUSE_STL_BASED_WXSTRING
3457 // helpers for wxStringBuffer and wxStringBufferLength
3458 wxStringCharType
*DoGetWriteBuf(size_t nLen
)
3460 return m_impl
.DoGetWriteBuf(nLen
);
3463 void DoUngetWriteBuf()
3465 wxSTRING_INVALIDATE_CACHE();
3467 m_impl
.DoUngetWriteBuf();
3470 void DoUngetWriteBuf(size_t nLen
)
3472 wxSTRING_SET_CACHED_LENGTH(nLen
);
3474 m_impl
.DoUngetWriteBuf(nLen
);
3476 #endif // !wxUSE_STL_BASED_WXSTRING
3478 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
3479 #if !wxUSE_UTF8_LOCALE_ONLY
3480 int DoPrintfWchar(const wxChar
*format
, ...);
3481 static wxString
DoFormatWchar(const wxChar
*format
, ...);
3483 #if wxUSE_UNICODE_UTF8
3484 int DoPrintfUtf8(const char *format
, ...);
3485 static wxString
DoFormatUtf8(const char *format
, ...);
3489 #if !wxUSE_STL_BASED_WXSTRING
3490 // check string's data validity
3491 bool IsValid() const { return m_impl
.GetStringData()->IsValid(); }
3495 wxStringImpl m_impl
;
3497 // buffers for compatibility conversion from (char*)c_str() and
3498 // (wchar_t*)c_str(): the pointers returned by these functions should remain
3499 // valid until the string itself is modified for compatibility with the
3500 // existing code and consistency with std::string::c_str() so returning a
3501 // temporary buffer won't do and we need to cache the conversion results
3503 // TODO-UTF8: benchmark various approaches to keeping compatibility buffers
3504 template<typename T
>
3505 struct ConvertedBuffer
3507 // notice that there is no need to initialize m_len here as it's unused
3508 // as long as m_str is NULL
3509 ConvertedBuffer() : m_str(NULL
) {}
3513 bool Extend(size_t len
)
3515 // add extra 1 for the trailing NUL
3516 void * const str
= realloc(m_str
, sizeof(T
)*(len
+ 1));
3520 m_str
= static_cast<T
*>(str
);
3526 const wxScopedCharTypeBuffer
<T
> AsScopedBuffer() const
3528 return wxScopedCharTypeBuffer
<T
>::CreateNonOwned(m_str
, m_len
);
3531 T
*m_str
; // pointer to the string data
3532 size_t m_len
; // length, not size, i.e. in chars and without last NUL
3537 // common mb_str() and wxCStrData::AsChar() helper: performs the conversion
3538 // and returns either m_convertedToChar.m_str (in which case its m_len is
3539 // also updated) or NULL if it failed
3541 // there is an important exception: in wxUSE_UNICODE_UTF8 build if conv is a
3542 // UTF-8 one, we return m_impl.c_str() directly, without doing any conversion
3543 // as optimization and so the caller needs to check for this before using
3544 // m_convertedToChar
3546 // NB: AsChar() returns char* in any build, unlike mb_str()
3547 const char *AsChar(const wxMBConv
& conv
) const;
3549 // mb_str() implementation helper
3550 wxScopedCharBuffer
AsCharBuf(const wxMBConv
& conv
) const
3552 #if wxUSE_UNICODE_UTF8
3553 // avoid conversion if we can
3554 if ( conv
.IsUTF8() )
3556 return wxScopedCharBuffer::CreateNonOwned(m_impl
.c_str(),
3559 #endif // wxUSE_UNICODE_UTF8
3561 // call this solely in order to fill in m_convertedToChar as AsChar()
3562 // updates it as a side effect: this is a bit ugly but it's a completely
3563 // internal function so the users of this class shouldn't care or know
3564 // about it and doing it like this, i.e. having a separate AsChar(),
3565 // allows us to avoid the creation and destruction of a temporary buffer
3566 // when using wxCStrData without duplicating any code
3567 if ( !AsChar(conv
) )
3569 // although it would be probably more correct to return NULL buffer
3570 // from here if the conversion fails, a lot of existing code doesn't
3571 // expect mb_str() (or wc_str()) to ever return NULL so return an
3572 // empty string otherwise to avoid crashes in it
3574 // also, some existing code does check for the conversion success and
3575 // so asserting here would be bad too -- even if it does mean that
3576 // silently losing data is possible for badly written code
3577 return wxScopedCharBuffer::CreateNonOwned("", 0);
3580 return m_convertedToChar
.AsScopedBuffer();
3583 ConvertedBuffer
<char> m_convertedToChar
;
3584 #endif // !wxUSE_UNICODE
3586 #if !wxUSE_UNICODE_WCHAR
3587 // common wc_str() and wxCStrData::AsWChar() helper for both UTF-8 and ANSI
3588 // builds: converts the string contents into m_convertedToWChar and returns
3589 // NULL if the conversion failed (this can only happen in ANSI build)
3591 // NB: AsWChar() returns wchar_t* in any build, unlike wc_str()
3592 const wchar_t *AsWChar(const wxMBConv
& conv
) const;
3594 // wc_str() implementation helper
3595 wxScopedWCharBuffer
AsWCharBuf(const wxMBConv
& conv
) const
3597 if ( !AsWChar(conv
) )
3598 return wxScopedWCharBuffer::CreateNonOwned(L
"", 0);
3600 return m_convertedToWChar
.AsScopedBuffer();
3603 ConvertedBuffer
<wchar_t> m_convertedToWChar
;
3604 #endif // !wxUSE_UNICODE_WCHAR
3606 #if wxUSE_UNICODE_UTF8
3607 // FIXME-UTF8: (try to) move this elsewhere (TLS) or solve differently
3608 // assigning to character pointer to by wxString::iterator may
3609 // change the underlying wxStringImpl iterator, so we have to
3610 // keep track of all iterators and update them as necessary:
3611 struct wxStringIteratorNodeHead
3613 wxStringIteratorNodeHead() : ptr(NULL
) {}
3614 wxStringIteratorNode
*ptr
;
3616 // copying is disallowed as it would result in more than one pointer into
3617 // the same linked list
3618 wxDECLARE_NO_COPY_CLASS(wxStringIteratorNodeHead
);
3621 wxStringIteratorNodeHead m_iterators
;
3623 friend class WXDLLIMPEXP_FWD_BASE wxStringIteratorNode
;
3624 friend class WXDLLIMPEXP_FWD_BASE wxUniCharRef
;
3625 #endif // wxUSE_UNICODE_UTF8
3627 friend class WXDLLIMPEXP_FWD_BASE wxCStrData
;
3628 friend class wxStringInternalBuffer
;
3629 friend class wxStringInternalBufferLength
;
3632 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
3633 #pragma warning (pop)
3636 // string iterator operators that satisfy STL Random Access Iterator
3638 inline wxString::iterator
operator+(ptrdiff_t n
, wxString::iterator i
)
3640 inline wxString::const_iterator
operator+(ptrdiff_t n
, wxString::const_iterator i
)
3642 inline wxString::reverse_iterator
operator+(ptrdiff_t n
, wxString::reverse_iterator i
)
3644 inline wxString::const_reverse_iterator
operator+(ptrdiff_t n
, wxString::const_reverse_iterator i
)
3647 // notice that even though for many compilers the friend declarations above are
3648 // enough, from the point of view of C++ standard we must have the declarations
3649 // here as friend ones are not injected in the enclosing namespace and without
3650 // them the code fails to compile with conforming compilers such as xlC or g++4
3651 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
, const wxString
& string2
);
3652 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const char *psz
);
3653 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const wchar_t *pwz
);
3654 wxString WXDLLIMPEXP_BASE
operator+(const char *psz
, const wxString
& string
);
3655 wxString WXDLLIMPEXP_BASE
operator+(const wchar_t *pwz
, const wxString
& string
);
3657 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
3658 wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
3660 inline wxString
operator+(const wxString
& string
, wxUniCharRef ch
)
3661 { return string
+ (wxUniChar
)ch
; }
3662 inline wxString
operator+(const wxString
& string
, char ch
)
3663 { return string
+ wxUniChar(ch
); }
3664 inline wxString
operator+(const wxString
& string
, wchar_t ch
)
3665 { return string
+ wxUniChar(ch
); }
3666 inline wxString
operator+(wxUniCharRef ch
, const wxString
& string
)
3667 { return (wxUniChar
)ch
+ string
; }
3668 inline wxString
operator+(char ch
, const wxString
& string
)
3669 { return wxUniChar(ch
) + string
; }
3670 inline wxString
operator+(wchar_t ch
, const wxString
& string
)
3671 { return wxUniChar(ch
) + string
; }
3674 #define wxGetEmptyString() wxString()
3676 // ----------------------------------------------------------------------------
3677 // helper functions which couldn't be defined inline
3678 // ----------------------------------------------------------------------------
3683 #if wxUSE_UNICODE_WCHAR
3686 struct wxStringAsBufHelper
<char>
3688 static wxScopedCharBuffer
Get(const wxString
& s
, size_t *len
)
3690 wxScopedCharBuffer
buf(s
.mb_str());
3692 *len
= buf
? strlen(buf
) : 0;
3698 struct wxStringAsBufHelper
<wchar_t>
3700 static wxScopedWCharBuffer
Get(const wxString
& s
, size_t *len
)
3702 const size_t length
= s
.length();
3705 return wxScopedWCharBuffer::CreateNonOwned(s
.wx_str(), length
);
3709 #elif wxUSE_UNICODE_UTF8
3712 struct wxStringAsBufHelper
<char>
3714 static wxScopedCharBuffer
Get(const wxString
& s
, size_t *len
)
3716 const size_t length
= s
.utf8_length();
3719 return wxScopedCharBuffer::CreateNonOwned(s
.wx_str(), length
);
3724 struct wxStringAsBufHelper
<wchar_t>
3726 static wxScopedWCharBuffer
Get(const wxString
& s
, size_t *len
)
3728 wxScopedWCharBuffer
wbuf(s
.wc_str());
3730 *len
= wxWcslen(wbuf
);
3735 #endif // Unicode build kind
3737 } // namespace wxPrivate
3739 // ----------------------------------------------------------------------------
3740 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
3741 // ----------------------------------------------------------------------------
3743 #if !wxUSE_STL_BASED_WXSTRING
3744 // string buffer for direct access to string data in their native
3746 class wxStringInternalBuffer
3749 typedef wxStringCharType CharType
;
3751 wxStringInternalBuffer(wxString
& str
, size_t lenWanted
= 1024)
3752 : m_str(str
), m_buf(NULL
)
3753 { m_buf
= m_str
.DoGetWriteBuf(lenWanted
); }
3755 ~wxStringInternalBuffer() { m_str
.DoUngetWriteBuf(); }
3757 operator wxStringCharType
*() const { return m_buf
; }
3761 wxStringCharType
*m_buf
;
3763 wxDECLARE_NO_COPY_CLASS(wxStringInternalBuffer
);
3766 class wxStringInternalBufferLength
3769 typedef wxStringCharType CharType
;
3771 wxStringInternalBufferLength(wxString
& str
, size_t lenWanted
= 1024)
3772 : m_str(str
), m_buf(NULL
), m_len(0), m_lenSet(false)
3774 m_buf
= m_str
.DoGetWriteBuf(lenWanted
);
3775 wxASSERT(m_buf
!= NULL
);
3778 ~wxStringInternalBufferLength()
3781 m_str
.DoUngetWriteBuf(m_len
);
3784 operator wxStringCharType
*() const { return m_buf
; }
3785 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
3789 wxStringCharType
*m_buf
;
3793 wxDECLARE_NO_COPY_CLASS(wxStringInternalBufferLength
);
3796 #endif // !wxUSE_STL_BASED_WXSTRING
3798 template<typename T
>
3799 class wxStringTypeBufferBase
3804 wxStringTypeBufferBase(wxString
& str
, size_t lenWanted
= 1024)
3805 : m_str(str
), m_buf(lenWanted
)
3807 // for compatibility with old wxStringBuffer which provided direct
3808 // access to wxString internal buffer, initialize ourselves with the
3809 // string initial contents
3811 // FIXME-VC6: remove the ugly (CharType *)NULL and use normal
3812 // tchar_str<CharType>
3814 const wxCharTypeBuffer
<CharType
> buf(str
.tchar_str(&len
, (CharType
*)NULL
));
3817 if ( len
> lenWanted
)
3819 // in this case there is not enough space for terminating NUL,
3820 // ensure that we still put it there
3821 m_buf
.data()[lenWanted
] = 0;
3822 len
= lenWanted
- 1;
3825 memcpy(m_buf
.data(), buf
, (len
+ 1)*sizeof(CharType
));
3827 //else: conversion failed, this can happen when trying to get Unicode
3828 // string contents into a char string
3831 operator CharType
*() { return m_buf
.data(); }
3835 wxCharTypeBuffer
<CharType
> m_buf
;
3838 template<typename T
>
3839 class wxStringTypeBufferLengthBase
: public wxStringTypeBufferBase
<T
>
3842 wxStringTypeBufferLengthBase(wxString
& str
, size_t lenWanted
= 1024)
3843 : wxStringTypeBufferBase
<T
>(str
, lenWanted
),
3848 ~wxStringTypeBufferLengthBase()
3850 wxASSERT_MSG( this->m_lenSet
, "forgot to call SetLength()" );
3853 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
3860 template<typename T
>
3861 class wxStringTypeBuffer
: public wxStringTypeBufferBase
<T
>
3864 wxStringTypeBuffer(wxString
& str
, size_t lenWanted
= 1024)
3865 : wxStringTypeBufferBase
<T
>(str
, lenWanted
)
3868 ~wxStringTypeBuffer()
3870 this->m_str
.assign(this->m_buf
.data());
3873 wxDECLARE_NO_COPY_CLASS(wxStringTypeBuffer
);
3876 template<typename T
>
3877 class wxStringTypeBufferLength
: public wxStringTypeBufferLengthBase
<T
>
3880 wxStringTypeBufferLength(wxString
& str
, size_t lenWanted
= 1024)
3881 : wxStringTypeBufferLengthBase
<T
>(str
, lenWanted
)
3884 ~wxStringTypeBufferLength()
3886 this->m_str
.assign(this->m_buf
.data(), this->m_len
);
3889 wxDECLARE_NO_COPY_CLASS(wxStringTypeBufferLength
);
3892 #if wxUSE_STL_BASED_WXSTRING
3894 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase
<wxStringCharType
> )
3896 class wxStringInternalBuffer
: public wxStringTypeBufferBase
<wxStringCharType
>
3899 wxStringInternalBuffer(wxString
& str
, size_t lenWanted
= 1024)
3900 : wxStringTypeBufferBase
<wxStringCharType
>(str
, lenWanted
) {}
3901 ~wxStringInternalBuffer()
3902 { m_str
.m_impl
.assign(m_buf
.data()); }
3904 wxDECLARE_NO_COPY_CLASS(wxStringInternalBuffer
);
3907 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE(
3908 wxStringTypeBufferLengthBase
<wxStringCharType
> )
3910 class wxStringInternalBufferLength
3911 : public wxStringTypeBufferLengthBase
<wxStringCharType
>
3914 wxStringInternalBufferLength(wxString
& str
, size_t lenWanted
= 1024)
3915 : wxStringTypeBufferLengthBase
<wxStringCharType
>(str
, lenWanted
) {}
3917 ~wxStringInternalBufferLength()
3919 m_str
.m_impl
.assign(m_buf
.data(), m_len
);
3922 wxDECLARE_NO_COPY_CLASS(wxStringInternalBufferLength
);
3925 #endif // wxUSE_STL_BASED_WXSTRING
3928 #if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
3929 typedef wxStringTypeBuffer
<wxChar
> wxStringBuffer
;
3930 typedef wxStringTypeBufferLength
<wxChar
> wxStringBufferLength
;
3931 #else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
3932 typedef wxStringInternalBuffer wxStringBuffer
;
3933 typedef wxStringInternalBufferLength wxStringBufferLength
;
3934 #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
3936 #if wxUSE_UNICODE_UTF8
3937 typedef wxStringInternalBuffer wxUTF8StringBuffer
;
3938 typedef wxStringInternalBufferLength wxUTF8StringBufferLength
;
3939 #elif wxUSE_UNICODE_WCHAR
3941 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase
<char> )
3943 // Note about inlined dtors in the classes below: this is done not for
3944 // performance reasons but just to avoid linking errors in the MSVC DLL build
3945 // under Windows: if a class has non-inline methods it must be declared as
3946 // being DLL-exported but, due to an extremely interesting feature of MSVC 7
3947 // and later, any template class which is used as a base of a DLL-exported
3948 // class is implicitly made DLL-exported too, as explained at the bottom of
3949 // http://msdn.microsoft.com/en-us/library/twa2aw10.aspx (just to confirm: yes,
3950 // _inheriting_ from a class can change whether it is being exported from DLL)
3952 // But this results in link errors because the base template class is not DLL-
3953 // exported, whether it is declared with WXDLLIMPEXP_BASE or not, because it
3954 // does have only inline functions. So the simplest fix is to just make all the
3955 // functions of these classes inline too.
3957 class wxUTF8StringBuffer
: public wxStringTypeBufferBase
<char>
3960 wxUTF8StringBuffer(wxString
& str
, size_t lenWanted
= 1024)
3961 : wxStringTypeBufferBase
<char>(str
, lenWanted
) {}
3962 ~wxUTF8StringBuffer()
3964 wxMBConvStrictUTF8 conv
;
3965 size_t wlen
= conv
.ToWChar(NULL
, 0, m_buf
);
3966 wxCHECK_RET( wlen
!= wxCONV_FAILED
, "invalid UTF-8 data in string buffer?" );
3968 wxStringInternalBuffer
wbuf(m_str
, wlen
);
3969 conv
.ToWChar(wbuf
, wlen
, m_buf
);
3972 wxDECLARE_NO_COPY_CLASS(wxUTF8StringBuffer
);
3975 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferLengthBase
<char> )
3977 class wxUTF8StringBufferLength
: public wxStringTypeBufferLengthBase
<char>
3980 wxUTF8StringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
3981 : wxStringTypeBufferLengthBase
<char>(str
, lenWanted
) {}
3982 ~wxUTF8StringBufferLength()
3984 wxCHECK_RET(m_lenSet
, "length not set");
3986 wxMBConvStrictUTF8 conv
;
3987 size_t wlen
= conv
.ToWChar(NULL
, 0, m_buf
, m_len
);
3988 wxCHECK_RET( wlen
!= wxCONV_FAILED
, "invalid UTF-8 data in string buffer?" );
3990 wxStringInternalBufferLength
wbuf(m_str
, wlen
);
3991 conv
.ToWChar(wbuf
, wlen
, m_buf
, m_len
);
3992 wbuf
.SetLength(wlen
);
3995 wxDECLARE_NO_COPY_CLASS(wxUTF8StringBufferLength
);
3997 #endif // wxUSE_UNICODE_UTF8/wxUSE_UNICODE_WCHAR
4000 // ---------------------------------------------------------------------------
4001 // wxString comparison functions: operator versions are always case sensitive
4002 // ---------------------------------------------------------------------------
4004 #define wxCMP_WXCHAR_STRING(p, s, op) 0 op s.Cmp(p)
4006 wxDEFINE_ALL_COMPARISONS(const wxChar
*, const wxString
&, wxCMP_WXCHAR_STRING
)
4008 #undef wxCMP_WXCHAR_STRING
4010 inline bool operator==(const wxString
& s1
, const wxString
& s2
)
4011 { return s1
.IsSameAs(s2
); }
4012 inline bool operator!=(const wxString
& s1
, const wxString
& s2
)
4013 { return !s1
.IsSameAs(s2
); }
4014 inline bool operator< (const wxString
& s1
, const wxString
& s2
)
4015 { return s1
.Cmp(s2
) < 0; }
4016 inline bool operator> (const wxString
& s1
, const wxString
& s2
)
4017 { return s1
.Cmp(s2
) > 0; }
4018 inline bool operator<=(const wxString
& s1
, const wxString
& s2
)
4019 { return s1
.Cmp(s2
) <= 0; }
4020 inline bool operator>=(const wxString
& s1
, const wxString
& s2
)
4021 { return s1
.Cmp(s2
) >= 0; }
4023 inline bool operator==(const wxString
& s1
, const wxCStrData
& s2
)
4024 { return s1
== s2
.AsString(); }
4025 inline bool operator==(const wxCStrData
& s1
, const wxString
& s2
)
4026 { return s1
.AsString() == s2
; }
4027 inline bool operator!=(const wxString
& s1
, const wxCStrData
& s2
)
4028 { return s1
!= s2
.AsString(); }
4029 inline bool operator!=(const wxCStrData
& s1
, const wxString
& s2
)
4030 { return s1
.AsString() != s2
; }
4032 inline bool operator==(const wxString
& s1
, const wxScopedWCharBuffer
& s2
)
4033 { return (s1
.Cmp((const wchar_t *)s2
) == 0); }
4034 inline bool operator==(const wxScopedWCharBuffer
& s1
, const wxString
& s2
)
4035 { return (s2
.Cmp((const wchar_t *)s1
) == 0); }
4036 inline bool operator!=(const wxString
& s1
, const wxScopedWCharBuffer
& s2
)
4037 { return (s1
.Cmp((const wchar_t *)s2
) != 0); }
4038 inline bool operator!=(const wxScopedWCharBuffer
& s1
, const wxString
& s2
)
4039 { return (s2
.Cmp((const wchar_t *)s1
) != 0); }
4041 inline bool operator==(const wxString
& s1
, const wxScopedCharBuffer
& s2
)
4042 { return (s1
.Cmp((const char *)s2
) == 0); }
4043 inline bool operator==(const wxScopedCharBuffer
& s1
, const wxString
& s2
)
4044 { return (s2
.Cmp((const char *)s1
) == 0); }
4045 inline bool operator!=(const wxString
& s1
, const wxScopedCharBuffer
& s2
)
4046 { return (s1
.Cmp((const char *)s2
) != 0); }
4047 inline bool operator!=(const wxScopedCharBuffer
& s1
, const wxString
& s2
)
4048 { return (s2
.Cmp((const char *)s1
) != 0); }
4050 inline wxString
operator+(const wxString
& string
, const wxScopedWCharBuffer
& buf
)
4051 { return string
+ (const wchar_t *)buf
; }
4052 inline wxString
operator+(const wxScopedWCharBuffer
& buf
, const wxString
& string
)
4053 { return (const wchar_t *)buf
+ string
; }
4055 inline wxString
operator+(const wxString
& string
, const wxScopedCharBuffer
& buf
)
4056 { return string
+ (const char *)buf
; }
4057 inline wxString
operator+(const wxScopedCharBuffer
& buf
, const wxString
& string
)
4058 { return (const char *)buf
+ string
; }
4060 // comparison with char
4061 inline bool operator==(const wxUniChar
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
4062 inline bool operator==(const wxUniCharRef
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
4063 inline bool operator==(char c
, const wxString
& s
) { return s
.IsSameAs(c
); }
4064 inline bool operator==(wchar_t c
, const wxString
& s
) { return s
.IsSameAs(c
); }
4065 inline bool operator==(int c
, const wxString
& s
) { return s
.IsSameAs(c
); }
4066 inline bool operator==(const wxString
& s
, const wxUniChar
& c
) { return s
.IsSameAs(c
); }
4067 inline bool operator==(const wxString
& s
, const wxUniCharRef
& c
) { return s
.IsSameAs(c
); }
4068 inline bool operator==(const wxString
& s
, char c
) { return s
.IsSameAs(c
); }
4069 inline bool operator==(const wxString
& s
, wchar_t c
) { return s
.IsSameAs(c
); }
4070 inline bool operator!=(const wxUniChar
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
4071 inline bool operator!=(const wxUniCharRef
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
4072 inline bool operator!=(char c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
4073 inline bool operator!=(wchar_t c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
4074 inline bool operator!=(int c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
4075 inline bool operator!=(const wxString
& s
, const wxUniChar
& c
) { return !s
.IsSameAs(c
); }
4076 inline bool operator!=(const wxString
& s
, const wxUniCharRef
& c
) { return !s
.IsSameAs(c
); }
4077 inline bool operator!=(const wxString
& s
, char c
) { return !s
.IsSameAs(c
); }
4078 inline bool operator!=(const wxString
& s
, wchar_t c
) { return !s
.IsSameAs(c
); }
4081 // wxString iterators comparisons
4082 inline bool wxString::iterator::operator==(const const_iterator
& i
) const
4083 { return i
== *this; }
4084 inline bool wxString::iterator::operator!=(const const_iterator
& i
) const
4085 { return i
!= *this; }
4086 inline bool wxString::iterator::operator<(const const_iterator
& i
) const
4087 { return i
> *this; }
4088 inline bool wxString::iterator::operator>(const const_iterator
& i
) const
4089 { return i
< *this; }
4090 inline bool wxString::iterator::operator<=(const const_iterator
& i
) const
4091 { return i
>= *this; }
4092 inline bool wxString::iterator::operator>=(const const_iterator
& i
) const
4093 { return i
<= *this; }
4095 // comparison with C string in Unicode build
4098 #define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s
4100 wxDEFINE_ALL_COMPARISONS(const char *, const wxString
&, wxCMP_CHAR_STRING
)
4102 #undef wxCMP_CHAR_STRING
4104 #endif // wxUSE_UNICODE
4106 // we also need to provide the operators for comparison with wxCStrData to
4107 // resolve ambiguity between operator(const wxChar *,const wxString &) and
4108 // operator(const wxChar *, const wxChar *) for "p == s.c_str()"
4110 // notice that these are (shallow) pointer comparisons, not (deep) string ones
4111 #define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar()
4112 #define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar()
4114 wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData
&, wxCMP_WCHAR_CSTRDATA
)
4115 wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData
&, wxCMP_CHAR_CSTRDATA
)
4117 #undef wxCMP_CHAR_CSTRDATA
4118 #undef wxCMP_WCHAR_CSTRDATA
4120 // ---------------------------------------------------------------------------
4121 // Implementation only from here until the end of file
4122 // ---------------------------------------------------------------------------
4124 #if wxUSE_STD_IOSTREAM
4126 #include "wx/iosfwrap.h"
4128 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxString
&);
4129 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxCStrData
&);
4130 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxScopedCharBuffer
&);
4131 #ifndef __BORLANDC__
4132 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxScopedWCharBuffer
&);
4135 #if wxUSE_UNICODE && defined(HAVE_WOSTREAM)
4137 WXDLLIMPEXP_BASE wxSTD wostream
& operator<<(wxSTD wostream
&, const wxString
&);
4138 WXDLLIMPEXP_BASE wxSTD wostream
& operator<<(wxSTD wostream
&, const wxCStrData
&);
4139 WXDLLIMPEXP_BASE wxSTD wostream
& operator<<(wxSTD wostream
&, const wxScopedWCharBuffer
&);
4141 #endif // wxUSE_UNICODE && defined(HAVE_WOSTREAM)
4143 #endif // wxUSE_STD_IOSTREAM
4145 // ---------------------------------------------------------------------------
4146 // wxCStrData implementation
4147 // ---------------------------------------------------------------------------
4149 inline wxCStrData::wxCStrData(char *buf
)
4150 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
4151 inline wxCStrData::wxCStrData(wchar_t *buf
)
4152 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
4154 inline wxCStrData::wxCStrData(const wxCStrData
& data
)
4155 : m_str(data
.m_owned
? new wxString(*data
.m_str
) : data
.m_str
),
4156 m_offset(data
.m_offset
),
4157 m_owned(data
.m_owned
)
4161 inline wxCStrData::~wxCStrData()
4164 delete const_cast<wxString
*>(m_str
); // cast to silence warnings
4167 // AsChar() and AsWChar() implementations simply forward to wxString methods
4169 inline const wchar_t* wxCStrData::AsWChar() const
4171 const wchar_t * const p
=
4172 #if wxUSE_UNICODE_WCHAR
4174 #elif wxUSE_UNICODE_UTF8
4175 m_str
->AsWChar(wxMBConvStrictUTF8());
4177 m_str
->AsWChar(wxConvLibc
);
4180 // in Unicode build the string always has a valid Unicode representation
4181 // and even if a conversion is needed (as in UTF8 case) it can't fail
4183 // but in ANSI build the string contents might be not convertible to
4184 // Unicode using the current locale encoding so we do need to check for
4189 // if conversion fails, return empty string and not NULL to avoid
4190 // crashes in code written with either wxWidgets 2 wxString or
4191 // std::string behaviour in mind: neither of them ever returns NULL
4192 // from its c_str() and so we shouldn't neither
4194 // notice that the same is done in AsChar() below and
4195 // wxString::wc_str() and mb_str() for the same reasons
4198 #endif // !wxUSE_UNICODE
4200 return p
+ m_offset
;
4203 inline const char* wxCStrData::AsChar() const
4205 #if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
4206 const char * const p
= m_str
->AsChar(wxConvLibc
);
4209 #else // !wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY
4210 const char * const p
= m_str
->mb_str();
4211 #endif // wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
4213 return p
+ m_offset
;
4216 inline wxString
wxCStrData::AsString() const
4218 if ( m_offset
== 0 )
4221 return m_str
->Mid(m_offset
);
4224 inline const wxStringCharType
*wxCStrData::AsInternal() const
4226 #if wxUSE_UNICODE_UTF8
4227 return wxStringOperations::AddToIter(m_str
->wx_str(), m_offset
);
4229 return m_str
->wx_str() + m_offset
;
4233 inline wxUniChar
wxCStrData::operator*() const
4235 if ( m_str
->empty() )
4236 return wxUniChar(wxT('\0'));
4238 return (*m_str
)[m_offset
];
4241 inline wxUniChar
wxCStrData::operator[](size_t n
) const
4243 // NB: we intentionally use operator[] and not at() here because the former
4244 // works for the terminating NUL while the latter does not
4245 return (*m_str
)[m_offset
+ n
];
4248 // ----------------------------------------------------------------------------
4249 // more wxCStrData operators
4250 // ----------------------------------------------------------------------------
4252 // we need to define those to allow "size_t pos = p - s.c_str()" where p is
4253 // some pointer into the string
4254 inline size_t operator-(const char *p
, const wxCStrData
& cs
)
4256 return p
- cs
.AsChar();
4259 inline size_t operator-(const wchar_t *p
, const wxCStrData
& cs
)
4261 return p
- cs
.AsWChar();
4264 // ----------------------------------------------------------------------------
4265 // implementation of wx[W]CharBuffer inline methods using wxCStrData
4266 // ----------------------------------------------------------------------------
4268 // FIXME-UTF8: move this to buffer.h
4269 inline wxCharBuffer::wxCharBuffer(const wxCStrData
& cstr
)
4270 : wxCharTypeBufferBase(cstr
.AsCharBuf())
4274 inline wxWCharBuffer::wxWCharBuffer(const wxCStrData
& cstr
)
4275 : wxCharTypeBufferBase(cstr
.AsWCharBuf())
4279 #if wxUSE_UNICODE_UTF8
4280 // ----------------------------------------------------------------------------
4281 // implementation of wxStringIteratorNode inline methods
4282 // ----------------------------------------------------------------------------
4284 void wxStringIteratorNode::DoSet(const wxString
*str
,
4285 wxStringImpl::const_iterator
*citer
,
4286 wxStringImpl::iterator
*iter
)
4294 m_next
= str
->m_iterators
.ptr
;
4295 const_cast<wxString
*>(m_str
)->m_iterators
.ptr
= this;
4297 m_next
->m_prev
= this;
4305 void wxStringIteratorNode::clear()
4308 m_next
->m_prev
= m_prev
;
4310 m_prev
->m_next
= m_next
;
4311 else if ( m_str
) // first in the list
4312 const_cast<wxString
*>(m_str
)->m_iterators
.ptr
= m_next
;
4314 m_next
= m_prev
= NULL
;
4319 #endif // wxUSE_UNICODE_UTF8
4321 #if WXWIN_COMPATIBILITY_2_8
4322 // lot of code out there doesn't explicitly include wx/crt.h, but uses
4323 // CRT wrappers that are now declared in wx/wxcrt.h and wx/wxcrtvararg.h,
4324 // so let's include this header now that wxString is defined and it's safe
4329 // ----------------------------------------------------------------------------
4330 // Checks on wxString characters
4331 // ----------------------------------------------------------------------------
4333 template<bool (T
)(const wxUniChar
& c
)>
4334 inline bool wxStringCheck(const wxString
& val
)
4336 for ( wxString::const_iterator i
= val
.begin();
4344 #endif // _WX_WXSTRING_H_