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
27 #if defined(__WXMAC__) || defined(__VISAGECPP__)
31 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
32 // problem in VACPP V4 with including stdlib.h multiple times
33 // strconv includes it anyway
46 #ifdef HAVE_STRCASECMP_IN_STRINGS_H
47 #include <strings.h> // for strcasecmp()
48 #endif // HAVE_STRCASECMP_IN_STRINGS_H
50 #include "wx/wxcrtbase.h" // for wxChar, wxStrlen() etc.
51 #include "wx/strvararg.h"
52 #include "wx/buffer.h" // for wxCharBuffer
53 #include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes
54 #include "wx/stringimpl.h"
55 #include "wx/stringops.h"
56 #include "wx/unichar.h"
58 // by default we cache the mapping of the positions in UTF-8 string to the byte
59 // offset as this results in noticeable performance improvements for loops over
60 // strings using indices; comment out this line to disable this
62 // notice that this optimization is well worth using even in debug builds as it
63 // changes asymptotic complexity of algorithms using indices to iterate over
64 // wxString back to expected linear from quadratic
66 // also notice that wxTLS_TYPE() (__declspec(thread) in this case) is unsafe to
67 // use in DLL build under pre-Vista Windows so we disable this code for now, if
68 // anybody really needs to use UTF-8 build under Windows with this optimization
69 // it would have to be re-tested and probably corrected
70 // CS: under OSX release builds the string destructor/cache cleanup sometimes
71 // crashes, disable until we find the true reason or a better workaround
72 #if wxUSE_UNICODE_UTF8 && !defined(__WINDOWS__) && !defined(__WXOSX__)
73 #define wxUSE_STRING_POS_CACHE 1
75 #define wxUSE_STRING_POS_CACHE 0
78 #if wxUSE_STRING_POS_CACHE
81 // change this 0 to 1 to enable additional (very expensive) asserts
82 // verifying that string caching logic works as expected
84 #define wxSTRING_CACHE_ASSERT(cond) wxASSERT(cond)
86 #define wxSTRING_CACHE_ASSERT(cond)
88 #endif // wxUSE_STRING_POS_CACHE
90 class WXDLLIMPEXP_FWD_BASE wxString
;
92 // unless this symbol is predefined to disable the compatibility functions, do
94 #ifndef WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
95 #define WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER 1
100 template <typename T
> struct wxStringAsBufHelper
;
103 // ---------------------------------------------------------------------------
105 // ---------------------------------------------------------------------------
107 // casts [unfortunately!] needed to call some broken functions which require
108 // "char *" instead of "const char *"
109 #define WXSTRINGCAST (wxChar *)(const wxChar *)
110 #define wxCSTRINGCAST (wxChar *)(const wxChar *)
111 #define wxMBSTRINGCAST (char *)(const char *)
112 #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *)
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
118 #if WXWIN_COMPATIBILITY_2_6
120 // deprecated in favour of wxString::npos, don't use in new code
122 // maximum possible length for a string means "take all string" everywhere
123 #define wxSTRING_MAXLEN wxString::npos
125 #endif // WXWIN_COMPATIBILITY_2_6
127 // ---------------------------------------------------------------------------
128 // global functions complementing standard C string library replacements for
129 // strlen() and portable strcasecmp()
130 //---------------------------------------------------------------------------
132 #if WXWIN_COMPATIBILITY_2_8
133 // Use wxXXX() functions from wxcrt.h instead! These functions are for
134 // backwards compatibility only.
136 // checks whether the passed in pointer is NULL and if the string is empty
137 wxDEPRECATED( inline bool IsEmpty(const char *p
) );
138 inline bool IsEmpty(const char *p
) { return (!p
|| !*p
); }
140 // safe version of strlen() (returns 0 if passed NULL pointer)
141 wxDEPRECATED( inline size_t Strlen(const char *psz
) );
142 inline size_t Strlen(const char *psz
)
143 { return psz
? strlen(psz
) : 0; }
145 // portable strcasecmp/_stricmp
146 wxDEPRECATED( inline int Stricmp(const char *psz1
, const char *psz2
) );
147 inline int Stricmp(const char *psz1
, const char *psz2
)
149 #if defined(__VISUALC__) && defined(__WXWINCE__)
150 register char c1
, c2
;
152 c1
= tolower(*psz1
++);
153 c2
= tolower(*psz2
++);
154 } while ( c1
&& (c1
== c2
) );
157 #elif defined(__VISUALC__)
158 return _stricmp(psz1
, psz2
);
159 #elif defined(__SC__)
160 return _stricmp(psz1
, psz2
);
161 #elif defined(__BORLANDC__)
162 return stricmp(psz1
, psz2
);
163 #elif defined(__WATCOMC__)
164 return stricmp(psz1
, psz2
);
165 #elif defined(__DJGPP__)
166 return stricmp(psz1
, psz2
);
167 #elif defined(__EMX__)
168 return stricmp(psz1
, psz2
);
169 #elif defined(__WXPM__)
170 return stricmp(psz1
, psz2
);
171 #elif defined(HAVE_STRCASECMP_IN_STRING_H) || \
172 defined(HAVE_STRCASECMP_IN_STRINGS_H) || \
173 defined(__GNUWIN32__)
174 return strcasecmp(psz1
, psz2
);
176 // almost all compilers/libraries provide this function (unfortunately under
177 // different names), that's why we don't implement our own which will surely
178 // be more efficient than this code (uncomment to use):
180 register char c1, c2;
182 c1 = tolower(*psz1++);
183 c2 = tolower(*psz2++);
184 } while ( c1 && (c1 == c2) );
189 #error "Please define string case-insensitive compare for your OS/compiler"
190 #endif // OS/compiler
193 #endif // WXWIN_COMPATIBILITY_2_8
195 // ----------------------------------------------------------------------------
197 // ----------------------------------------------------------------------------
199 // Lightweight object returned by wxString::c_str() and implicitly convertible
200 // to either const char* or const wchar_t*.
204 // Ctors; for internal use by wxString and wxCStrData only
205 wxCStrData(const wxString
*str
, size_t offset
= 0, bool owned
= false)
206 : m_str(str
), m_offset(offset
), m_owned(owned
) {}
209 // Ctor constructs the object from char literal; they are needed to make
210 // operator?: compile and they intentionally take char*, not const char*
211 inline wxCStrData(char *buf
);
212 inline wxCStrData(wchar_t *buf
);
213 inline wxCStrData(const wxCStrData
& data
);
215 inline ~wxCStrData();
217 // AsWChar() and AsChar() can't be defined here as they use wxString and so
218 // must come after it and because of this won't be inlined when called from
219 // wxString methods (without a lot of work to extract these wxString methods
220 // from inside the class itself). But we still define them being inline
221 // below to let compiler inline them from elsewhere. And because of this we
222 // must declare them as inline here because otherwise some compilers give
223 // warnings about them, e.g. mingw32 3.4.5 warns about "<symbol> defined
224 // locally after being referenced with dllimport linkage" while IRIX
225 // mipsPro 7.4 warns about "function declared inline after being called".
226 inline const wchar_t* AsWChar() const;
227 operator const wchar_t*() const { return AsWChar(); }
229 inline const char* AsChar() const;
230 const unsigned char* AsUnsignedChar() const
231 { return (const unsigned char *) AsChar(); }
232 operator const char*() const { return AsChar(); }
233 operator const unsigned char*() const { return AsUnsignedChar(); }
235 operator const void*() const { return AsChar(); }
237 // returns buffers that are valid as long as the associated wxString exists
238 const wxScopedCharBuffer
AsCharBuf() const
240 return wxScopedCharBuffer::CreateNonOwned(AsChar());
243 const wxScopedWCharBuffer
AsWCharBuf() const
245 return wxScopedWCharBuffer::CreateNonOwned(AsWChar());
248 inline wxString
AsString() const;
250 // returns the value as C string in internal representation (equivalent
251 // to AsString().wx_str(), but more efficient)
252 const wxStringCharType
*AsInternal() const;
254 // allow expressions like "c_str()[0]":
255 inline wxUniChar
operator[](size_t n
) const;
256 wxUniChar
operator[](int n
) const { return operator[](size_t(n
)); }
257 wxUniChar
operator[](long n
) const { return operator[](size_t(n
)); }
258 #ifndef wxSIZE_T_IS_UINT
259 wxUniChar
operator[](unsigned int n
) const { return operator[](size_t(n
)); }
260 #endif // size_t != unsigned int
262 // These operators are needed to emulate the pointer semantics of c_str():
263 // expressions like "wxChar *p = str.c_str() + 1;" should continue to work
264 // (we need both versions to resolve ambiguities). Note that this means
265 // the 'n' value is interpreted as addition to char*/wchar_t* pointer, it
266 // is *not* number of Unicode characters in wxString.
267 wxCStrData
operator+(int n
) const
268 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
269 wxCStrData
operator+(long n
) const
270 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
271 wxCStrData
operator+(size_t n
) const
272 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
274 // and these for "str.c_str() + (p2 - p1)" (it also works for any integer
275 // expression but it must be ptrdiff_t and not e.g. int to work in this
277 wxCStrData
operator-(ptrdiff_t n
) const
279 wxASSERT_MSG( n
<= (ptrdiff_t)m_offset
,
280 wxT("attempt to construct address before the beginning of the string") );
281 return wxCStrData(m_str
, m_offset
- n
, m_owned
);
284 // this operator is needed to make expressions like "*c_str()" or
285 // "*(c_str() + 2)" work
286 inline wxUniChar
operator*() const;
289 // the wxString this object was returned for
290 const wxString
*m_str
;
291 // Offset into c_str() return value. Note that this is *not* offset in
292 // m_str in Unicode characters. Instead, it is index into the
293 // char*/wchar_t* buffer returned by c_str(). It's interpretation depends
294 // on how is the wxCStrData instance used: if it is eventually cast to
295 // const char*, m_offset will be in bytes form string's start; if it is
296 // cast to const wchar_t*, it will be in wchar_t values.
298 // should m_str be deleted, i.e. is it owned by us?
301 friend class WXDLLIMPEXP_FWD_BASE wxString
;
304 // ----------------------------------------------------------------------------
305 // wxStringPrintfMixin
306 // ---------------------------------------------------------------------------
308 // NB: VC6 has a bug that causes linker errors if you have template methods
309 // in a class using __declspec(dllimport). The solution is to split such
310 // class into two classes, one that contains the template methods and does
311 // *not* use WXDLLIMPEXP_BASE and another class that contains the rest
312 // (with DLL linkage).
314 // We only do this for VC6 here, because the code is less efficient
315 // (Printf() has to use dynamic_cast<>) and because OpenWatcom compiler
316 // cannot compile this code.
318 #if defined(__VISUALC__) && __VISUALC__ < 1300
319 #define wxNEEDS_WXSTRING_PRINTF_MIXIN
322 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
323 // this class contains implementation of wxString's vararg methods, it's
324 // exported from wxBase DLL
325 class WXDLLIMPEXP_BASE wxStringPrintfMixinBase
328 wxStringPrintfMixinBase() {}
330 #if !wxUSE_UTF8_LOCALE_ONLY
331 int DoPrintfWchar(const wxChar
*format
, ...);
332 static wxString
DoFormatWchar(const wxChar
*format
, ...);
334 #if wxUSE_UNICODE_UTF8
335 int DoPrintfUtf8(const char *format
, ...);
336 static wxString
DoFormatUtf8(const char *format
, ...);
340 // this class contains template wrappers for wxString's vararg methods, it's
341 // intentionally *not* exported from the DLL in order to fix the VC6 bug
343 class wxStringPrintfMixin
: public wxStringPrintfMixinBase
346 // to further complicate things, we can't return wxString from
347 // wxStringPrintfMixin::Format() because wxString is not yet declared at
348 // this point; the solution is to use this fake type trait template - this
349 // way the compiler won't know the return type until Format() is used
350 // (this doesn't compile with Watcom, but VC6 compiles it just fine):
351 template<typename T
> struct StringReturnType
353 typedef wxString type
;
357 // these are duplicated wxString methods, they're also declared below
358 // if !wxNEEDS_WXSTRING_PRINTF_MIXIN:
360 // static wxString Format(const wString& format, ...) WX_ATTRIBUTE_PRINTF_1;
361 WX_DEFINE_VARARG_FUNC_SANS_N0(static typename StringReturnType
<T1
>::type
,
362 Format
, 1, (const wxFormatString
&),
363 DoFormatWchar
, DoFormatUtf8
)
364 // We have to implement the version without template arguments manually
365 // because of the StringReturnType<> hack, although WX_DEFINE_VARARG_FUNC
366 // normally does it itself. It has to be a template so that we can use
367 // the hack, even though there's no real template parameter. We can't move
368 // it to wxStrig, because it would shadow these versions of Format() then.
370 inline static typename StringReturnType
<T
>::type
373 // NB: this doesn't compile if T is not (some form of) a string;
374 // this makes Format's prototype equivalent to
375 // Format(const wxFormatString& fmt)
376 return DoFormatWchar(wxFormatString(fmt
));
379 // int Printf(const wxString& format, ...);
380 WX_DEFINE_VARARG_FUNC(int, Printf
, 1, (const wxFormatString
&),
381 DoPrintfWchar
, DoPrintfUtf8
)
382 // int sprintf(const wxString& format, ...) WX_ATTRIBUTE_PRINTF_2;
383 WX_DEFINE_VARARG_FUNC(int, sprintf
, 1, (const wxFormatString
&),
384 DoPrintfWchar
, DoPrintfUtf8
)
387 wxStringPrintfMixin() : wxStringPrintfMixinBase() {}
389 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
392 // ----------------------------------------------------------------------------
393 // wxString: string class trying to be compatible with std::string, MFC
394 // CString and wxWindows 1.x wxString all at once
395 // ---------------------------------------------------------------------------
397 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
398 // "non dll-interface class 'wxStringPrintfMixin' used as base interface
399 // for dll-interface class 'wxString'" -- this is OK in our case
400 #pragma warning (push)
401 #pragma warning (disable:4275)
404 #if wxUSE_UNICODE_UTF8
405 // see the comment near wxString::iterator for why we need this
406 class WXDLLIMPEXP_BASE wxStringIteratorNode
409 wxStringIteratorNode()
410 : m_str(NULL
), m_citer(NULL
), m_iter(NULL
), m_prev(NULL
), m_next(NULL
) {}
411 wxStringIteratorNode(const wxString
*str
,
412 wxStringImpl::const_iterator
*citer
)
413 { DoSet(str
, citer
, NULL
); }
414 wxStringIteratorNode(const wxString
*str
, wxStringImpl::iterator
*iter
)
415 { DoSet(str
, NULL
, iter
); }
416 ~wxStringIteratorNode()
419 inline void set(const wxString
*str
, wxStringImpl::const_iterator
*citer
)
420 { clear(); DoSet(str
, citer
, NULL
); }
421 inline void set(const wxString
*str
, wxStringImpl::iterator
*iter
)
422 { clear(); DoSet(str
, NULL
, iter
); }
424 const wxString
*m_str
;
425 wxStringImpl::const_iterator
*m_citer
;
426 wxStringImpl::iterator
*m_iter
;
427 wxStringIteratorNode
*m_prev
, *m_next
;
431 inline void DoSet(const wxString
*str
,
432 wxStringImpl::const_iterator
*citer
,
433 wxStringImpl::iterator
*iter
);
435 // the node belongs to a particular iterator instance, it's not copied
436 // when a copy of the iterator is made
437 wxDECLARE_NO_COPY_CLASS(wxStringIteratorNode
);
439 #endif // wxUSE_UNICODE_UTF8
441 class WXDLLIMPEXP_BASE wxString
442 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
443 : public wxStringPrintfMixin
446 // NB: special care was taken in arranging the member functions in such order
447 // that all inline functions can be effectively inlined, verify that all
448 // performance critical functions are still inlined if you change order!
450 // an 'invalid' value for string index, moved to this place due to a CW bug
451 static const size_t npos
;
454 // if we hadn't made these operators private, it would be possible to
455 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
456 // converted to char in C and we do have operator=(char)
458 // NB: we don't need other versions (short/long and unsigned) as attempt
459 // to assign another numeric type to wxString will now result in
460 // ambiguity between operator=(char) and operator=(int)
461 wxString
& operator=(int);
463 // these methods are not implemented - there is _no_ conversion from int to
464 // string, you're doing something wrong if the compiler wants to call it!
466 // try `s << i' or `s.Printf("%d", i)' instead
470 // buffer for holding temporary substring when using any of the methods
471 // that take (char*,size_t) or (wchar_t*,size_t) arguments:
473 struct SubstrBufFromType
478 SubstrBufFromType(const T
& data_
, size_t len_
)
479 : data(data_
), len(len_
)
481 wxASSERT_MSG( len
!= npos
, "must have real length" );
485 #if wxUSE_UNICODE_UTF8
486 // even char* -> char* needs conversion, from locale charset to UTF-8
487 typedef SubstrBufFromType
<wxScopedCharBuffer
> SubstrBufFromWC
;
488 typedef SubstrBufFromType
<wxScopedCharBuffer
> SubstrBufFromMB
;
489 #elif wxUSE_UNICODE_WCHAR
490 typedef SubstrBufFromType
<const wchar_t*> SubstrBufFromWC
;
491 typedef SubstrBufFromType
<wxScopedWCharBuffer
> SubstrBufFromMB
;
493 typedef SubstrBufFromType
<const char*> SubstrBufFromMB
;
494 typedef SubstrBufFromType
<wxScopedCharBuffer
> SubstrBufFromWC
;
498 // Functions implementing primitive operations on string data; wxString
499 // methods and iterators are implemented in terms of it. The differences
500 // between UTF-8 and wchar_t* representations of the string are mostly
503 #if wxUSE_UNICODE_UTF8
504 static SubstrBufFromMB
ConvertStr(const char *psz
, size_t nLength
,
505 const wxMBConv
& conv
);
506 static SubstrBufFromWC
ConvertStr(const wchar_t *pwz
, size_t nLength
,
507 const wxMBConv
& conv
);
508 #elif wxUSE_UNICODE_WCHAR
509 static SubstrBufFromMB
ConvertStr(const char *psz
, size_t nLength
,
510 const wxMBConv
& conv
);
512 static SubstrBufFromWC
ConvertStr(const wchar_t *pwz
, size_t nLength
,
513 const wxMBConv
& conv
);
516 #if !wxUSE_UNICODE_UTF8 // wxUSE_UNICODE_WCHAR or !wxUSE_UNICODE
517 // returns C string encoded as the implementation expects:
519 static const wchar_t* ImplStr(const wchar_t* str
)
520 { return str
? str
: wxT(""); }
521 static const SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
522 { return SubstrBufFromWC(str
, (str
&& n
== npos
) ? wxWcslen(str
) : n
); }
523 static wxScopedWCharBuffer
ImplStr(const char* str
,
524 const wxMBConv
& conv
= wxConvLibc
)
525 { return ConvertStr(str
, npos
, conv
).data
; }
526 static SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
527 const wxMBConv
& conv
= wxConvLibc
)
528 { return ConvertStr(str
, n
, conv
); }
530 static const char* ImplStr(const char* str
,
531 const wxMBConv
& WXUNUSED(conv
) = wxConvLibc
)
532 { return str
? str
: ""; }
533 static const SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
534 const wxMBConv
& WXUNUSED(conv
) = wxConvLibc
)
535 { return SubstrBufFromMB(str
, (str
&& n
== npos
) ? wxStrlen(str
) : n
); }
536 static wxScopedCharBuffer
ImplStr(const wchar_t* str
)
537 { return ConvertStr(str
, npos
, wxConvLibc
).data
; }
538 static SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
539 { return ConvertStr(str
, n
, wxConvLibc
); }
542 // translates position index in wxString to/from index in underlying
544 static size_t PosToImpl(size_t pos
) { return pos
; }
545 static void PosLenToImpl(size_t pos
, size_t len
,
546 size_t *implPos
, size_t *implLen
)
547 { *implPos
= pos
; *implLen
= len
; }
548 static size_t LenToImpl(size_t len
) { return len
; }
549 static size_t PosFromImpl(size_t pos
) { return pos
; }
551 // we don't want to define these as empty inline functions as it could
552 // result in noticeable (and quite unnecessary in non-UTF-8 build) slowdown
553 // in debug build where the inline functions are not effectively inlined
554 #define wxSTRING_INVALIDATE_CACHE()
555 #define wxSTRING_INVALIDATE_CACHED_LENGTH()
556 #define wxSTRING_UPDATE_CACHED_LENGTH(n)
557 #define wxSTRING_SET_CACHED_LENGTH(n)
559 #else // wxUSE_UNICODE_UTF8
561 static wxScopedCharBuffer
ImplStr(const char* str
,
562 const wxMBConv
& conv
= wxConvLibc
)
563 { return ConvertStr(str
, npos
, conv
).data
; }
564 static SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
565 const wxMBConv
& conv
= wxConvLibc
)
566 { return ConvertStr(str
, n
, conv
); }
568 static wxScopedCharBuffer
ImplStr(const wchar_t* str
)
569 { return ConvertStr(str
, npos
, wxMBConvUTF8()).data
; }
570 static SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
571 { return ConvertStr(str
, n
, wxMBConvUTF8()); }
573 #if wxUSE_STRING_POS_CACHE
574 // this is an extremely simple cache used by PosToImpl(): each cache element
575 // contains the string it applies to and the index corresponding to the last
576 // used position in this wxString in its m_impl string
578 // NB: notice that this struct (and nested Element one) must be a POD or we
579 // wouldn't be able to use a thread-local variable of this type, in
580 // particular it should have no ctor -- we rely on statics being
581 // initialized to 0 instead
588 const wxString
*str
; // the string to which this element applies
589 size_t pos
, // the cached index in this string
590 impl
, // the corresponding position in its m_impl
591 len
; // cached length or npos if unknown
593 // reset cached index to 0
594 void ResetPos() { pos
= impl
= 0; }
596 // reset position and length
597 void Reset() { ResetPos(); len
= npos
; }
600 // cache the indices mapping for the last few string used
601 Element cached
[SIZE
];
603 // the last used index
607 #ifndef wxHAS_COMPILER_TLS
608 // we must use an accessor function and not a static variable when the TLS
609 // variables support is implemented in the library (and not by the compiler)
610 // because the global s_cache variable could be not yet initialized when a
611 // ctor of another global object is executed and if that ctor uses any
612 // wxString methods, bad things happen
614 // however notice that this approach does not work when compiler TLS is used,
615 // at least not with g++ 4.1.2 under amd64 as it apparently compiles code
616 // using this accessor incorrectly when optimizations are enabled (-O2 is
617 // enough) -- luckily we don't need it then neither as static __thread
618 // variables are initialized by 0 anyhow then and so we can use the variable
620 WXEXPORT
static Cache
& GetCache()
622 static wxTLS_TYPE(Cache
) s_cache
;
624 return wxTLS_VALUE(s_cache
);
627 // this helper struct is used to ensure that GetCache() is called during
628 // static initialization time, i.e. before any threads creation, as otherwise
629 // the static s_cache construction inside GetCache() wouldn't be MT-safe
630 friend struct wxStrCacheInitializer
;
631 #else // wxHAS_COMPILER_TLS
632 static wxTLS_TYPE(Cache
) ms_cache
;
633 static Cache
& GetCache() { return wxTLS_VALUE(ms_cache
); }
634 #endif // !wxHAS_COMPILER_TLS/wxHAS_COMPILER_TLS
636 static Cache::Element
*GetCacheBegin() { return GetCache().cached
; }
637 static Cache::Element
*GetCacheEnd() { return GetCacheBegin() + Cache::SIZE
; }
638 static unsigned& LastUsedCacheElement() { return GetCache().lastUsed
; }
640 // this is used in debug builds only to provide a convenient function,
641 // callable from a debugger, to show the cache contents
642 friend struct wxStrCacheDumper
;
644 // uncomment this to have access to some profiling statistics on program
646 //#define wxPROFILE_STRING_CACHE
648 #ifdef wxPROFILE_STRING_CACHE
649 static struct PosToImplCacheStats
651 unsigned postot
, // total non-trivial calls to PosToImpl
652 poshits
, // cache hits from PosToImpl()
653 mishits
, // cached position beyond the needed one
654 sumpos
, // sum of all positions, used to compute the
655 // average position after dividing by postot
656 sumofs
, // sum of all offsets after using the cache, used to
657 // compute the average after dividing by hits
658 lentot
, // number of total calls to length()
659 lenhits
; // number of cache hits in length()
662 friend struct wxStrCacheStatsDumper
;
664 #define wxCACHE_PROFILE_FIELD_INC(field) ms_cacheStats.field++
665 #define wxCACHE_PROFILE_FIELD_ADD(field, val) ms_cacheStats.field += (val)
666 #else // !wxPROFILE_STRING_CACHE
667 #define wxCACHE_PROFILE_FIELD_INC(field)
668 #define wxCACHE_PROFILE_FIELD_ADD(field, val)
669 #endif // wxPROFILE_STRING_CACHE/!wxPROFILE_STRING_CACHE
671 // note: it could seem that the functions below shouldn't be inline because
672 // they are big, contain loops and so the compiler shouldn't be able to
673 // inline them anyhow, however moving them into string.cpp does decrease the
674 // code performance by ~5%, at least when using g++ 4.1 so do keep them here
675 // unless tests show that it's not advantageous any more
677 // return the pointer to the cache element for this string or NULL if not
679 Cache::Element
*FindCacheElement() const
681 // profiling seems to show a small but consistent gain if we use this
682 // simple loop instead of starting from the last used element (there are
683 // a lot of misses in this function...)
684 Cache::Element
* const cacheBegin
= GetCacheBegin();
685 #ifndef wxHAS_COMPILER_TLS
686 // during destruction tls calls may return NULL, in this case return NULL
687 // immediately without accessing anything else
688 if ( cacheBegin
== NULL
)
691 Cache::Element
* const cacheEnd
= GetCacheEnd();
692 for ( Cache::Element
*c
= cacheBegin
; c
!= cacheEnd
; c
++ )
694 if ( c
->str
== this )
701 // unlike FindCacheElement(), this one always returns a valid pointer to the
702 // cache element for this string, it may have valid last cached position and
703 // its corresponding index in the byte string or not
704 Cache::Element
*GetCacheElement() const
706 Cache::Element
* const cacheBegin
= GetCacheBegin();
707 Cache::Element
* const cacheEnd
= GetCacheEnd();
708 Cache::Element
* const cacheStart
= cacheBegin
+ LastUsedCacheElement();
710 // check the last used first, this does no (measurable) harm for a miss
711 // but does help for simple loops addressing the same string all the time
712 if ( cacheStart
->str
== this )
715 // notice that we're going to check cacheStart again inside this call but
716 // profiling shows that it's still faster to use a simple loop like
717 // inside FindCacheElement() than manually looping with wrapping starting
718 // from the cache entry after the start one
719 Cache::Element
*c
= FindCacheElement();
722 // claim the next cache entry for this string
724 if ( ++c
== cacheEnd
)
730 // and remember the last used element
731 LastUsedCacheElement() = c
- cacheBegin
;
737 size_t DoPosToImpl(size_t pos
) const
739 wxCACHE_PROFILE_FIELD_INC(postot
);
741 // NB: although the case of pos == 1 (and offset from cached position
742 // equal to 1) are common, nothing is gained by writing special code
743 // for handling them, the compiler (at least g++ 4.1 used) seems to
744 // optimize the code well enough on its own
746 wxCACHE_PROFILE_FIELD_ADD(sumpos
, pos
);
748 Cache::Element
* const cache
= GetCacheElement();
750 // cached position can't be 0 so if it is, it means that this entry was
751 // used for length caching only so far, i.e. it doesn't count as a hit
752 // from our point of view
755 wxCACHE_PROFILE_FIELD_INC(poshits
);
758 if ( pos
== cache
->pos
)
761 // this seems to happen only rarely so just reset the cache in this case
762 // instead of complicating code even further by seeking backwards in this
764 if ( cache
->pos
> pos
)
766 wxCACHE_PROFILE_FIELD_INC(mishits
);
771 wxCACHE_PROFILE_FIELD_ADD(sumofs
, pos
- cache
->pos
);
774 wxStringImpl::const_iterator
i(m_impl
.begin() + cache
->impl
);
775 for ( size_t n
= cache
->pos
; n
< pos
; n
++ )
776 wxStringOperations::IncIter(i
);
779 cache
->impl
= i
- m_impl
.begin();
781 wxSTRING_CACHE_ASSERT(
782 (int)cache
->impl
== (begin() + pos
).impl() - m_impl
.begin() );
787 void InvalidateCache()
789 Cache::Element
* const cache
= FindCacheElement();
794 void InvalidateCachedLength()
796 Cache::Element
* const cache
= FindCacheElement();
801 void SetCachedLength(size_t len
)
803 // we optimistically cache the length here even if the string wasn't
804 // present in the cache before, this seems to do no harm and the
805 // potential for avoiding length recomputation for long strings looks
807 GetCacheElement()->len
= len
;
810 void UpdateCachedLength(ptrdiff_t delta
)
812 Cache::Element
* const cache
= FindCacheElement();
813 if ( cache
&& cache
->len
!= npos
)
815 wxSTRING_CACHE_ASSERT( (ptrdiff_t)cache
->len
+ delta
>= 0 );
821 #define wxSTRING_INVALIDATE_CACHE() InvalidateCache()
822 #define wxSTRING_INVALIDATE_CACHED_LENGTH() InvalidateCachedLength()
823 #define wxSTRING_UPDATE_CACHED_LENGTH(n) UpdateCachedLength(n)
824 #define wxSTRING_SET_CACHED_LENGTH(n) SetCachedLength(n)
825 #else // !wxUSE_STRING_POS_CACHE
826 size_t DoPosToImpl(size_t pos
) const
828 return (begin() + pos
).impl() - m_impl
.begin();
831 #define wxSTRING_INVALIDATE_CACHE()
832 #define wxSTRING_INVALIDATE_CACHED_LENGTH()
833 #define wxSTRING_UPDATE_CACHED_LENGTH(n)
834 #define wxSTRING_SET_CACHED_LENGTH(n)
835 #endif // wxUSE_STRING_POS_CACHE/!wxUSE_STRING_POS_CACHE
837 size_t PosToImpl(size_t pos
) const
839 return pos
== 0 || pos
== npos
? pos
: DoPosToImpl(pos
);
842 void PosLenToImpl(size_t pos
, size_t len
, size_t *implPos
, size_t *implLen
) const;
844 size_t LenToImpl(size_t len
) const
847 PosLenToImpl(0, len
, &pos
, &len2
);
851 size_t PosFromImpl(size_t pos
) const
853 if ( pos
== 0 || pos
== npos
)
856 return const_iterator(this, m_impl
.begin() + pos
) - begin();
858 #endif // !wxUSE_UNICODE_UTF8/wxUSE_UNICODE_UTF8
862 typedef wxUniChar value_type
;
863 typedef wxUniChar char_type
;
864 typedef wxUniCharRef reference
;
865 typedef wxChar
* pointer
;
866 typedef const wxChar
* const_pointer
;
868 typedef size_t size_type
;
869 typedef wxUniChar const_reference
;
872 #if wxUSE_UNICODE_UTF8
873 // random access is not O(1), as required by Random Access Iterator
874 #define WX_STR_ITERATOR_TAG std::bidirectional_iterator_tag
876 #define WX_STR_ITERATOR_TAG std::random_access_iterator_tag
878 #define WX_DEFINE_ITERATOR_CATEGORY(cat) typedef cat iterator_category;
880 // not defining iterator_category at all in this case is better than defining
881 // it as some dummy type -- at least it results in more intelligible error
883 #define WX_DEFINE_ITERATOR_CATEGORY(cat)
886 #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, reference_type) \
888 typedef wxStringImpl::iterator_name underlying_iterator; \
890 WX_DEFINE_ITERATOR_CATEGORY(WX_STR_ITERATOR_TAG) \
891 typedef wxUniChar value_type; \
892 typedef ptrdiff_t difference_type; \
893 typedef reference_type reference; \
894 typedef pointer_type pointer; \
896 reference operator[](size_t n) const { return *(*this + n); } \
898 iterator_name& operator++() \
899 { wxStringOperations::IncIter(m_cur); return *this; } \
900 iterator_name& operator--() \
901 { wxStringOperations::DecIter(m_cur); return *this; } \
902 iterator_name operator++(int) \
904 iterator_name tmp = *this; \
905 wxStringOperations::IncIter(m_cur); \
908 iterator_name operator--(int) \
910 iterator_name tmp = *this; \
911 wxStringOperations::DecIter(m_cur); \
915 iterator_name& operator+=(ptrdiff_t n) \
917 m_cur = wxStringOperations::AddToIter(m_cur, n); \
920 iterator_name& operator-=(ptrdiff_t n) \
922 m_cur = wxStringOperations::AddToIter(m_cur, -n); \
926 difference_type operator-(const iterator_name& i) const \
927 { return wxStringOperations::DiffIters(m_cur, i.m_cur); } \
929 bool operator==(const iterator_name& i) const \
930 { return m_cur == i.m_cur; } \
931 bool operator!=(const iterator_name& i) const \
932 { return m_cur != i.m_cur; } \
934 bool operator<(const iterator_name& i) const \
935 { return m_cur < i.m_cur; } \
936 bool operator>(const iterator_name& i) const \
937 { return m_cur > i.m_cur; } \
938 bool operator<=(const iterator_name& i) const \
939 { return m_cur <= i.m_cur; } \
940 bool operator>=(const iterator_name& i) const \
941 { return m_cur >= i.m_cur; } \
944 /* for internal wxString use only: */ \
945 underlying_iterator impl() const { return m_cur; } \
947 friend class wxString; \
948 friend class wxCStrData; \
951 underlying_iterator m_cur
953 class WXDLLIMPEXP_FWD_BASE const_iterator
;
955 #if wxUSE_UNICODE_UTF8
956 // NB: In UTF-8 build, (non-const) iterator needs to keep reference
957 // to the underlying wxStringImpl, because UTF-8 is variable-length
958 // encoding and changing the value pointer to by an iterator (using
959 // its operator*) requires calling wxStringImpl::replace() if the old
960 // and new values differ in their encoding's length.
962 // Furthermore, the replace() call may invalid all iterators for the
963 // string, so we have to keep track of outstanding iterators and update
964 // them if replace() happens.
966 // This is implemented by maintaining linked list of iterators for every
967 // string and traversing it in wxUniCharRef::operator=(). Head of the
968 // list is stored in wxString. (FIXME-UTF8)
970 class WXDLLIMPEXP_BASE iterator
972 WX_STR_ITERATOR_IMPL(iterator
, wxChar
*, wxUniCharRef
);
976 iterator(const iterator
& i
)
977 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
978 iterator
& operator=(const iterator
& i
)
983 m_node
.set(i
.str(), &m_cur
);
988 reference
operator*()
989 { return wxUniCharRef::CreateForString(*str(), m_cur
); }
991 iterator
operator+(ptrdiff_t n
) const
992 { return iterator(str(), wxStringOperations::AddToIter(m_cur
, n
)); }
993 iterator
operator-(ptrdiff_t n
) const
994 { return iterator(str(), wxStringOperations::AddToIter(m_cur
, -n
)); }
996 // Normal iterators need to be comparable with the const_iterators so
997 // declare the comparison operators and implement them below after the
998 // full const_iterator declaration.
999 bool operator==(const const_iterator
& i
) const;
1000 bool operator!=(const const_iterator
& i
) const;
1001 bool operator<(const const_iterator
& i
) const;
1002 bool operator>(const const_iterator
& i
) const;
1003 bool operator<=(const const_iterator
& i
) const;
1004 bool operator>=(const const_iterator
& i
) const;
1007 iterator(wxString
*wxstr
, underlying_iterator ptr
)
1008 : m_cur(ptr
), m_node(wxstr
, &m_cur
) {}
1010 wxString
* str() const { return const_cast<wxString
*>(m_node
.m_str
); }
1012 wxStringIteratorNode m_node
;
1014 friend class const_iterator
;
1017 class WXDLLIMPEXP_BASE const_iterator
1019 // NB: reference_type is intentionally value, not reference, the character
1020 // may be encoded differently in wxString data:
1021 WX_STR_ITERATOR_IMPL(const_iterator
, const wxChar
*, wxUniChar
);
1025 const_iterator(const const_iterator
& i
)
1026 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
1027 const_iterator(const iterator
& i
)
1028 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
1030 const_iterator
& operator=(const const_iterator
& i
)
1035 m_node
.set(i
.str(), &m_cur
);
1039 const_iterator
& operator=(const iterator
& i
)
1040 { m_cur
= i
.m_cur
; m_node
.set(i
.str(), &m_cur
); return *this; }
1042 reference
operator*() const
1043 { return wxStringOperations::DecodeChar(m_cur
); }
1045 const_iterator
operator+(ptrdiff_t n
) const
1046 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur
, n
)); }
1047 const_iterator
operator-(ptrdiff_t n
) const
1048 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur
, -n
)); }
1050 // Notice that comparison operators taking non-const iterator are not
1051 // needed here because of the implicit conversion from non-const iterator
1052 // to const ones ensure that the versions for const_iterator declared
1053 // inside WX_STR_ITERATOR_IMPL can be used.
1056 // for internal wxString use only:
1057 const_iterator(const wxString
*wxstr
, underlying_iterator ptr
)
1058 : m_cur(ptr
), m_node(wxstr
, &m_cur
) {}
1060 const wxString
* str() const { return m_node
.m_str
; }
1062 wxStringIteratorNode m_node
;
1065 size_t IterToImplPos(wxString::iterator i
) const
1066 { return wxStringImpl::const_iterator(i
.impl()) - m_impl
.begin(); }
1068 iterator
GetIterForNthChar(size_t n
)
1069 { return iterator(this, m_impl
.begin() + PosToImpl(n
)); }
1070 const_iterator
GetIterForNthChar(size_t n
) const
1071 { return const_iterator(this, m_impl
.begin() + PosToImpl(n
)); }
1072 #else // !wxUSE_UNICODE_UTF8
1074 class WXDLLIMPEXP_BASE iterator
1076 WX_STR_ITERATOR_IMPL(iterator
, wxChar
*, wxUniCharRef
);
1080 iterator(const iterator
& i
) : m_cur(i
.m_cur
) {}
1082 reference
operator*()
1083 { return wxUniCharRef::CreateForString(m_cur
); }
1085 iterator
operator+(ptrdiff_t n
) const
1086 { return iterator(wxStringOperations::AddToIter(m_cur
, n
)); }
1087 iterator
operator-(ptrdiff_t n
) const
1088 { return iterator(wxStringOperations::AddToIter(m_cur
, -n
)); }
1090 // As in UTF-8 case above, define comparison operators taking
1091 // const_iterator too.
1092 bool operator==(const const_iterator
& i
) const;
1093 bool operator!=(const const_iterator
& i
) const;
1094 bool operator<(const const_iterator
& i
) const;
1095 bool operator>(const const_iterator
& i
) const;
1096 bool operator<=(const const_iterator
& i
) const;
1097 bool operator>=(const const_iterator
& i
) const;
1100 // for internal wxString use only:
1101 iterator(underlying_iterator ptr
) : m_cur(ptr
) {}
1102 iterator(wxString
*WXUNUSED(str
), underlying_iterator ptr
) : m_cur(ptr
) {}
1104 friend class const_iterator
;
1107 class WXDLLIMPEXP_BASE const_iterator
1109 // NB: reference_type is intentionally value, not reference, the character
1110 // may be encoded differently in wxString data:
1111 WX_STR_ITERATOR_IMPL(const_iterator
, const wxChar
*, wxUniChar
);
1115 const_iterator(const const_iterator
& i
) : m_cur(i
.m_cur
) {}
1116 const_iterator(const iterator
& i
) : m_cur(i
.m_cur
) {}
1118 reference
operator*() const
1119 { return wxStringOperations::DecodeChar(m_cur
); }
1121 const_iterator
operator+(ptrdiff_t n
) const
1122 { return const_iterator(wxStringOperations::AddToIter(m_cur
, n
)); }
1123 const_iterator
operator-(ptrdiff_t n
) const
1124 { return const_iterator(wxStringOperations::AddToIter(m_cur
, -n
)); }
1126 // As in UTF-8 case above, we don't need comparison operators taking
1127 // iterator because we have an implicit conversion from iterator to
1128 // const_iterator so the operators declared by WX_STR_ITERATOR_IMPL will
1132 // for internal wxString use only:
1133 const_iterator(underlying_iterator ptr
) : m_cur(ptr
) {}
1134 const_iterator(const wxString
*WXUNUSED(str
), underlying_iterator ptr
)
1138 iterator
GetIterForNthChar(size_t n
) { return begin() + n
; }
1139 const_iterator
GetIterForNthChar(size_t n
) const { return begin() + n
; }
1140 #endif // wxUSE_UNICODE_UTF8/!wxUSE_UNICODE_UTF8
1142 #undef WX_STR_ITERATOR_TAG
1143 #undef WX_STR_ITERATOR_IMPL
1145 // This method is mostly used by wxWidgets itself and return the offset of
1146 // the given iterator in bytes relative to the start of the buffer
1147 // representing the current string contents in the current locale encoding.
1149 // It is inefficient as it involves converting part of the string to this
1150 // encoding (and also unsafe as it simply returns 0 if the conversion fails)
1151 // and so should be avoided if possible, wx itself only uses it to implement
1152 // backwards-compatible API.
1153 ptrdiff_t IterOffsetInMBStr(const const_iterator
& i
) const
1155 const wxString
str(begin(), i
);
1157 // This is logically equivalent to strlen(str.mb_str()) but avoids
1158 // actually converting the string to multibyte and just computes the
1159 // length that it would have after conversion.
1160 size_t ofs
= wxConvLibc
.FromWChar(NULL
, 0, str
.wc_str(), str
.length());
1161 return ofs
== wxCONV_FAILED
? 0 : static_cast<ptrdiff_t>(ofs
);
1164 friend class iterator
;
1165 friend class const_iterator
;
1167 template <typename T
>
1168 class reverse_iterator_impl
1171 typedef T iterator_type
;
1173 WX_DEFINE_ITERATOR_CATEGORY(typename
T::iterator_category
)
1174 typedef typename
T::value_type value_type
;
1175 typedef typename
T::difference_type difference_type
;
1176 typedef typename
T::reference reference
;
1177 typedef typename
T::pointer
*pointer
;
1179 reverse_iterator_impl() {}
1180 reverse_iterator_impl(iterator_type i
) : m_cur(i
) {}
1181 reverse_iterator_impl(const reverse_iterator_impl
& ri
)
1182 : m_cur(ri
.m_cur
) {}
1184 iterator_type
base() const { return m_cur
; }
1186 reference
operator*() const { return *(m_cur
-1); }
1187 reference
operator[](size_t n
) const { return *(*this + n
); }
1189 reverse_iterator_impl
& operator++()
1190 { --m_cur
; return *this; }
1191 reverse_iterator_impl
operator++(int)
1192 { reverse_iterator_impl tmp
= *this; --m_cur
; return tmp
; }
1193 reverse_iterator_impl
& operator--()
1194 { ++m_cur
; return *this; }
1195 reverse_iterator_impl
operator--(int)
1196 { reverse_iterator_impl tmp
= *this; ++m_cur
; return tmp
; }
1198 // NB: explicit <T> in the functions below is to keep BCC 5.5 happy
1199 reverse_iterator_impl
operator+(ptrdiff_t n
) const
1200 { return reverse_iterator_impl
<T
>(m_cur
- n
); }
1201 reverse_iterator_impl
operator-(ptrdiff_t n
) const
1202 { return reverse_iterator_impl
<T
>(m_cur
+ n
); }
1203 reverse_iterator_impl
operator+=(ptrdiff_t n
)
1204 { m_cur
-= n
; return *this; }
1205 reverse_iterator_impl
operator-=(ptrdiff_t n
)
1206 { m_cur
+= n
; return *this; }
1208 unsigned operator-(const reverse_iterator_impl
& i
) const
1209 { return i
.m_cur
- m_cur
; }
1211 bool operator==(const reverse_iterator_impl
& ri
) const
1212 { return m_cur
== ri
.m_cur
; }
1213 bool operator!=(const reverse_iterator_impl
& ri
) const
1214 { return !(*this == ri
); }
1216 bool operator<(const reverse_iterator_impl
& i
) const
1217 { return m_cur
> i
.m_cur
; }
1218 bool operator>(const reverse_iterator_impl
& i
) const
1219 { return m_cur
< i
.m_cur
; }
1220 bool operator<=(const reverse_iterator_impl
& i
) const
1221 { return m_cur
>= i
.m_cur
; }
1222 bool operator>=(const reverse_iterator_impl
& i
) const
1223 { return m_cur
<= i
.m_cur
; }
1226 iterator_type m_cur
;
1229 typedef reverse_iterator_impl
<iterator
> reverse_iterator
;
1230 typedef reverse_iterator_impl
<const_iterator
> const_reverse_iterator
;
1233 // used to transform an expression built using c_str() (and hence of type
1234 // wxCStrData) to an iterator into the string
1235 static const_iterator
CreateConstIterator(const wxCStrData
& data
)
1237 return const_iterator(data
.m_str
,
1238 (data
.m_str
->begin() + data
.m_offset
).impl());
1241 // in UTF-8 STL build, creation from std::string requires conversion under
1242 // non-UTF8 locales, so we can't have and use wxString(wxStringImpl) ctor;
1243 // instead we define dummy type that lets us have wxString ctor for creation
1244 // from wxStringImpl that couldn't be used by user code (in all other builds,
1245 // "standard" ctors can be used):
1246 #if wxUSE_UNICODE_UTF8 && wxUSE_STL_BASED_WXSTRING
1247 struct CtorFromStringImplTag
{};
1249 wxString(CtorFromStringImplTag
* WXUNUSED(dummy
), const wxStringImpl
& src
)
1252 static wxString
FromImpl(const wxStringImpl
& src
)
1253 { return wxString((CtorFromStringImplTag
*)NULL
, src
); }
1255 #if !wxUSE_STL_BASED_WXSTRING
1256 wxString(const wxStringImpl
& src
) : m_impl(src
) { }
1257 // else: already defined as wxString(wxStdString) below
1259 static wxString
FromImpl(const wxStringImpl
& src
) { return wxString(src
); }
1263 // constructors and destructor
1264 // ctor for an empty string
1268 wxString(const wxString
& stringSrc
) : m_impl(stringSrc
.m_impl
) { }
1270 // string containing nRepeat copies of ch
1271 wxString(wxUniChar ch
, size_t nRepeat
= 1 )
1272 { assign(nRepeat
, ch
); }
1273 wxString(size_t nRepeat
, wxUniChar ch
)
1274 { assign(nRepeat
, ch
); }
1275 wxString(wxUniCharRef ch
, size_t nRepeat
= 1)
1276 { assign(nRepeat
, ch
); }
1277 wxString(size_t nRepeat
, wxUniCharRef ch
)
1278 { assign(nRepeat
, ch
); }
1279 wxString(char ch
, size_t nRepeat
= 1)
1280 { assign(nRepeat
, ch
); }
1281 wxString(size_t nRepeat
, char ch
)
1282 { assign(nRepeat
, ch
); }
1283 wxString(wchar_t ch
, size_t nRepeat
= 1)
1284 { assign(nRepeat
, ch
); }
1285 wxString(size_t nRepeat
, wchar_t ch
)
1286 { assign(nRepeat
, ch
); }
1288 // ctors from char* strings:
1289 wxString(const char *psz
)
1290 : m_impl(ImplStr(psz
)) {}
1291 wxString(const char *psz
, const wxMBConv
& conv
)
1292 : m_impl(ImplStr(psz
, conv
)) {}
1293 wxString(const char *psz
, size_t nLength
)
1294 { assign(psz
, nLength
); }
1295 wxString(const char *psz
, const wxMBConv
& conv
, size_t nLength
)
1297 SubstrBufFromMB
str(ImplStr(psz
, nLength
, conv
));
1298 m_impl
.assign(str
.data
, str
.len
);
1301 // and unsigned char*:
1302 wxString(const unsigned char *psz
)
1303 : m_impl(ImplStr((const char*)psz
)) {}
1304 wxString(const unsigned char *psz
, const wxMBConv
& conv
)
1305 : m_impl(ImplStr((const char*)psz
, conv
)) {}
1306 wxString(const unsigned char *psz
, size_t nLength
)
1307 { assign((const char*)psz
, nLength
); }
1308 wxString(const unsigned char *psz
, const wxMBConv
& conv
, size_t nLength
)
1310 SubstrBufFromMB
str(ImplStr((const char*)psz
, nLength
, conv
));
1311 m_impl
.assign(str
.data
, str
.len
);
1314 // ctors from wchar_t* strings:
1315 wxString(const wchar_t *pwz
)
1316 : m_impl(ImplStr(pwz
)) {}
1317 wxString(const wchar_t *pwz
, const wxMBConv
& WXUNUSED(conv
))
1318 : m_impl(ImplStr(pwz
)) {}
1319 wxString(const wchar_t *pwz
, size_t nLength
)
1320 { assign(pwz
, nLength
); }
1321 wxString(const wchar_t *pwz
, const wxMBConv
& WXUNUSED(conv
), size_t nLength
)
1322 { assign(pwz
, nLength
); }
1324 wxString(const wxScopedCharBuffer
& buf
)
1325 { assign(buf
.data(), buf
.length()); }
1326 wxString(const wxScopedWCharBuffer
& buf
)
1327 { assign(buf
.data(), buf
.length()); }
1329 // NB: this version uses m_impl.c_str() to force making a copy of the
1330 // string, so that "wxString(str.c_str())" idiom for passing strings
1331 // between threads works
1332 wxString(const wxCStrData
& cstr
)
1333 : m_impl(cstr
.AsString().m_impl
.c_str()) { }
1335 // as we provide both ctors with this signature for both char and unsigned
1336 // char string, we need to provide one for wxCStrData to resolve ambiguity
1337 wxString(const wxCStrData
& cstr
, size_t nLength
)
1338 : m_impl(cstr
.AsString().Mid(0, nLength
).m_impl
) {}
1340 // and because wxString is convertible to wxCStrData and const wxChar *
1341 // we also need to provide this one
1342 wxString(const wxString
& str
, size_t nLength
)
1343 { assign(str
, nLength
); }
1346 #if wxUSE_STRING_POS_CACHE
1349 // we need to invalidate our cache entry as another string could be
1350 // recreated at the same address (unlikely, but still possible, with the
1351 // heap-allocated strings but perfectly common with stack-allocated ones)
1354 #endif // wxUSE_STRING_POS_CACHE
1356 // even if we're not built with wxUSE_STD_STRING_CONV_IN_WXSTRING == 1 it is
1357 // very convenient to allow implicit conversions from std::string to wxString
1358 // and vice verse as this allows to use the same strings in non-GUI and GUI
1359 // code, however we don't want to unconditionally add this ctor as it would
1360 // make wx lib dependent on libstdc++ on some Linux versions which is bad, so
1361 // instead we ask the client code to define this wxUSE_STD_STRING symbol if
1363 #if wxUSE_STD_STRING
1364 #if wxUSE_UNICODE_WCHAR
1365 wxString(const wxStdWideString
& str
) : m_impl(str
) {}
1366 #else // UTF-8 or ANSI
1367 wxString(const wxStdWideString
& str
)
1368 { assign(str
.c_str(), str
.length()); }
1371 #if !wxUSE_UNICODE // ANSI build
1372 // FIXME-UTF8: do this in UTF8 build #if wxUSE_UTF8_LOCALE_ONLY, too
1373 wxString(const std::string
& str
) : m_impl(str
) {}
1375 wxString(const std::string
& str
)
1376 { assign(str
.c_str(), str
.length()); }
1378 #endif // wxUSE_STD_STRING
1380 // Also always provide explicit conversions to std::[w]string in any case,
1381 // see below for the implicit ones.
1382 #if wxUSE_STD_STRING
1383 // We can avoid a copy if we already use this string type internally,
1384 // otherwise we create a copy on the fly:
1385 #if wxUSE_UNICODE_WCHAR && wxUSE_STL_BASED_WXSTRING
1386 #define wxStringToStdWstringRetType const wxStdWideString&
1387 const wxStdWideString
& ToStdWstring() const { return m_impl
; }
1389 // wxStringImpl is either not std::string or needs conversion
1390 #define wxStringToStdWstringRetType wxStdWideString
1391 wxStdWideString
ToStdWstring() const
1393 #if wxUSE_UNICODE_WCHAR
1394 wxScopedWCharBuffer buf
=
1395 wxScopedWCharBuffer::CreateNonOwned(m_impl
.c_str(), m_impl
.length());
1396 #else // !wxUSE_UNICODE_WCHAR
1397 wxScopedWCharBuffer
buf(wc_str());
1400 return wxStdWideString(buf
.data(), buf
.length());
1404 #if (!wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY) && wxUSE_STL_BASED_WXSTRING
1405 // wxStringImpl is std::string in the encoding we want
1406 #define wxStringToStdStringRetType const std::string&
1407 const std::string
& ToStdString() const { return m_impl
; }
1409 // wxStringImpl is either not std::string or needs conversion
1410 #define wxStringToStdStringRetType std::string
1411 std::string
ToStdString() const
1413 wxScopedCharBuffer
buf(mb_str());
1414 return std::string(buf
.data(), buf
.length());
1418 #if wxUSE_STD_STRING_CONV_IN_WXSTRING
1419 // Implicit conversions to std::[w]string are not provided by default as
1420 // they conflict with the implicit conversions to "const char/wchar_t *"
1421 // which we use for backwards compatibility but do provide them if
1422 // explicitly requested.
1423 operator wxStringToStdStringRetType() const { return ToStdString(); }
1424 operator wxStringToStdWstringRetType() const { return ToStdWstring(); }
1425 #endif // wxUSE_STD_STRING_CONV_IN_WXSTRING
1427 #undef wxStringToStdStringRetType
1428 #undef wxStringToStdWstringRetType
1430 #endif // wxUSE_STD_STRING
1432 wxString
Clone() const
1434 // make a deep copy of the string, i.e. the returned string will have
1435 // ref count = 1 with refcounted implementation
1436 return wxString::FromImpl(wxStringImpl(m_impl
.c_str(), m_impl
.length()));
1439 // first valid index position
1440 const_iterator
begin() const { return const_iterator(this, m_impl
.begin()); }
1441 iterator
begin() { return iterator(this, m_impl
.begin()); }
1442 // position one after the last valid one
1443 const_iterator
end() const { return const_iterator(this, m_impl
.end()); }
1444 iterator
end() { return iterator(this, m_impl
.end()); }
1446 // first element of the reversed string
1447 const_reverse_iterator
rbegin() const
1448 { return const_reverse_iterator(end()); }
1449 reverse_iterator
rbegin()
1450 { return reverse_iterator(end()); }
1451 // one beyond the end of the reversed string
1452 const_reverse_iterator
rend() const
1453 { return const_reverse_iterator(begin()); }
1454 reverse_iterator
rend()
1455 { return reverse_iterator(begin()); }
1457 // std::string methods:
1458 #if wxUSE_UNICODE_UTF8
1459 size_t length() const
1461 #if wxUSE_STRING_POS_CACHE
1462 wxCACHE_PROFILE_FIELD_INC(lentot
);
1464 Cache::Element
* const cache
= GetCacheElement();
1466 if ( cache
->len
== npos
)
1468 // it's probably not worth trying to be clever and using cache->pos
1469 // here as it's probably 0 anyhow -- you usually call length() before
1470 // starting to index the string
1471 cache
->len
= end() - begin();
1475 wxCACHE_PROFILE_FIELD_INC(lenhits
);
1477 wxSTRING_CACHE_ASSERT( (int)cache
->len
== end() - begin() );
1481 #else // !wxUSE_STRING_POS_CACHE
1482 return end() - begin();
1483 #endif // wxUSE_STRING_POS_CACHE/!wxUSE_STRING_POS_CACHE
1486 size_t length() const { return m_impl
.length(); }
1489 size_type
size() const { return length(); }
1490 size_type
max_size() const { return npos
; }
1492 bool empty() const { return m_impl
.empty(); }
1494 // NB: these methods don't have a well-defined meaning in UTF-8 case
1495 size_type
capacity() const { return m_impl
.capacity(); }
1496 void reserve(size_t sz
) { m_impl
.reserve(sz
); }
1498 void resize(size_t nSize
, wxUniChar ch
= wxT('\0'))
1500 const size_t len
= length();
1504 #if wxUSE_UNICODE_UTF8
1507 wxSTRING_INVALIDATE_CACHE();
1509 // we can't use wxStringImpl::resize() for truncating the string as it
1510 // counts in bytes, not characters
1515 // we also can't use (presumably more efficient) resize() if we have to
1516 // append characters taking more than one byte
1517 if ( !ch
.IsAscii() )
1519 append(nSize
- len
, ch
);
1521 else // can use (presumably faster) resize() version
1522 #endif // wxUSE_UNICODE_UTF8
1524 wxSTRING_INVALIDATE_CACHED_LENGTH();
1526 m_impl
.resize(nSize
, (wxStringCharType
)ch
);
1530 wxString
substr(size_t nStart
= 0, size_t nLen
= npos
) const
1533 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
1534 return FromImpl(m_impl
.substr(pos
, len
));
1537 // generic attributes & operations
1538 // as standard strlen()
1539 size_t Len() const { return length(); }
1540 // string contains any characters?
1541 bool IsEmpty() const { return empty(); }
1542 // empty string is "false", so !str will return true
1543 bool operator!() const { return empty(); }
1544 // truncate the string to given length
1545 wxString
& Truncate(size_t uiLen
);
1546 // empty string contents
1547 void Empty() { clear(); }
1548 // empty the string and free memory
1549 void Clear() { clear(); }
1552 // Is an ascii value
1553 bool IsAscii() const;
1555 bool IsNumber() const;
1557 bool IsWord() const;
1559 // data access (all indexes are 0 based)
1561 wxUniChar
at(size_t n
) const
1562 { return wxStringOperations::DecodeChar(m_impl
.begin() + PosToImpl(n
)); }
1563 wxUniChar
GetChar(size_t n
) const
1565 // read/write access
1566 wxUniCharRef
at(size_t n
)
1567 { return *GetIterForNthChar(n
); }
1568 wxUniCharRef
GetWritableChar(size_t n
)
1571 void SetChar(size_t n
, wxUniChar ch
)
1574 // get last character
1575 wxUniChar
Last() const
1577 wxASSERT_MSG( !empty(), wxT("wxString: index out of bounds") );
1581 // get writable last character
1584 wxASSERT_MSG( !empty(), wxT("wxString: index out of bounds") );
1589 Note that we we must define all of the overloads below to avoid
1590 ambiguity when using str[0].
1592 wxUniChar
operator[](int n
) const
1594 wxUniChar
operator[](long n
) const
1596 wxUniChar
operator[](size_t n
) const
1598 #ifndef wxSIZE_T_IS_UINT
1599 wxUniChar
operator[](unsigned int n
) const
1601 #endif // size_t != unsigned int
1603 // operator versions of GetWriteableChar()
1604 wxUniCharRef
operator[](int n
)
1606 wxUniCharRef
operator[](long n
)
1608 wxUniCharRef
operator[](size_t n
)
1610 #ifndef wxSIZE_T_IS_UINT
1611 wxUniCharRef
operator[](unsigned int n
)
1613 #endif // size_t != unsigned int
1617 Overview of wxString conversions, implicit and explicit:
1619 - wxString has a std::[w]string-like c_str() method, however it does
1620 not return a C-style string directly but instead returns wxCStrData
1621 helper object which is convertible to either "char *" narrow string
1622 or "wchar_t *" wide string. Usually the correct conversion will be
1623 applied by the compiler automatically but if this doesn't happen you
1624 need to explicitly choose one using wxCStrData::AsChar() or AsWChar()
1625 methods or another wxString conversion function.
1627 - One of the places where the conversion does *NOT* happen correctly is
1628 when c_str() is passed to a vararg function such as printf() so you
1629 must *NOT* use c_str() with them. Either use wxPrintf() (all wx
1630 functions do handle c_str() correctly, even if they appear to be
1631 vararg (but they're not, really)) or add an explicit AsChar() or, if
1632 compatibility with previous wxWidgets versions is important, add a
1633 cast to "const char *".
1635 - In non-STL mode only, wxString is also implicitly convertible to
1636 wxCStrData. The same warning as above applies.
1638 - c_str() is polymorphic as it can be converted to either narrow or
1639 wide string. If you explicitly need one or the other, choose to use
1640 mb_str() (for narrow) or wc_str() (for wide) instead. Notice that
1641 these functions can return either the pointer to string directly (if
1642 this is what the string uses internally) or a temporary buffer
1643 containing the string and convertible to it. Again, conversion will
1644 usually be done automatically by the compiler but beware of the
1645 vararg functions: you need an explicit cast when using them.
1647 - There are also non-const versions of mb_str() and wc_str() called
1648 char_str() and wchar_str(). They are only meant to be used with
1649 non-const-correct functions and they always return buffers.
1651 - Finally wx_str() returns whatever string representation is used by
1652 wxString internally. It may be either a narrow or wide string
1653 depending on wxWidgets build mode but it will always be a raw pointer
1657 // explicit conversion to wxCStrData
1658 wxCStrData
c_str() const { return wxCStrData(this); }
1659 wxCStrData
data() const { return c_str(); }
1661 // implicit conversion to wxCStrData
1662 operator wxCStrData() const { return c_str(); }
1664 // the first two operators conflict with operators for conversion to
1665 // std::string and they must be disabled if those conversions are enabled;
1666 // the next one only makes sense if conversions to char* are also defined
1667 // and not defining it in STL build also helps us to get more clear error
1668 // messages for the code which relies on implicit conversion to char* in
1670 #if !wxUSE_STD_STRING_CONV_IN_WXSTRING
1671 operator const char*() const { return c_str(); }
1672 operator const wchar_t*() const { return c_str(); }
1674 // implicit conversion to untyped pointer for compatibility with previous
1675 // wxWidgets versions: this is the same as conversion to const char * so it
1677 operator const void*() const { return c_str(); }
1678 #endif // !wxUSE_STD_STRING_CONV_IN_WXSTRING
1680 // identical to c_str(), for MFC compatibility
1681 const wxCStrData
GetData() const { return c_str(); }
1683 // explicit conversion to C string in internal representation (char*,
1684 // wchar_t*, UTF-8-encoded char*, depending on the build):
1685 const wxStringCharType
*wx_str() const { return m_impl
.c_str(); }
1687 // conversion to *non-const* multibyte or widestring buffer; modifying
1688 // returned buffer won't affect the string, these methods are only useful
1689 // for passing values to const-incorrect functions
1690 wxWritableCharBuffer
char_str(const wxMBConv
& conv
= wxConvLibc
) const
1691 { return mb_str(conv
); }
1692 wxWritableWCharBuffer
wchar_str() const { return wc_str(); }
1694 // conversion to the buffer of the given type T (= char or wchar_t) and
1695 // also optionally return the buffer length
1697 // this is mostly/only useful for the template functions
1699 // FIXME-VC6: the second argument only exists for VC6 which doesn't support
1700 // explicit template function selection, do not use it unless
1701 // you must support VC6!
1702 template <typename T
>
1703 wxCharTypeBuffer
<T
> tchar_str(size_t *len
= NULL
,
1704 T
* WXUNUSED(dummy
) = NULL
) const
1707 // we need a helper dispatcher depending on type
1708 return wxPrivate::wxStringAsBufHelper
<T
>::Get(*this, len
);
1710 // T can only be char in ANSI build
1714 return wxCharTypeBuffer
<T
>::CreateNonOwned(wx_str(), length());
1715 #endif // Unicode build kind
1718 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
1719 // converting numbers or strings which are certain not to contain special
1720 // chars (typically system functions, X atoms, environment variables etc.)
1722 // the behaviour of these functions with the strings containing anything
1723 // else than 7 bit ASCII characters is undefined, use at your own risk.
1725 static wxString
FromAscii(const char *ascii
, size_t len
);
1726 static wxString
FromAscii(const char *ascii
);
1727 static wxString
FromAscii(char ascii
);
1728 const wxScopedCharBuffer
ToAscii() const;
1730 static wxString
FromAscii(const char *ascii
) { return wxString( ascii
); }
1731 static wxString
FromAscii(const char *ascii
, size_t len
)
1732 { return wxString( ascii
, len
); }
1733 static wxString
FromAscii(char ascii
) { return wxString( ascii
); }
1734 const char *ToAscii() const { return c_str(); }
1735 #endif // Unicode/!Unicode
1737 // also provide unsigned char overloads as signed/unsigned doesn't matter
1738 // for 7 bit ASCII characters
1739 static wxString
FromAscii(const unsigned char *ascii
)
1740 { return FromAscii((const char *)ascii
); }
1741 static wxString
FromAscii(const unsigned char *ascii
, size_t len
)
1742 { return FromAscii((const char *)ascii
, len
); }
1744 // conversion to/from UTF-8:
1745 #if wxUSE_UNICODE_UTF8
1746 static wxString
FromUTF8Unchecked(const char *utf8
)
1749 return wxEmptyString
;
1751 wxASSERT( wxStringOperations::IsValidUtf8String(utf8
) );
1752 return FromImpl(wxStringImpl(utf8
));
1754 static wxString
FromUTF8Unchecked(const char *utf8
, size_t len
)
1757 return wxEmptyString
;
1759 return FromUTF8Unchecked(utf8
);
1761 wxASSERT( wxStringOperations::IsValidUtf8String(utf8
, len
) );
1762 return FromImpl(wxStringImpl(utf8
, len
));
1765 static wxString
FromUTF8(const char *utf8
)
1767 if ( !utf8
|| !wxStringOperations::IsValidUtf8String(utf8
) )
1770 return FromImpl(wxStringImpl(utf8
));
1772 static wxString
FromUTF8(const char *utf8
, size_t len
)
1775 return FromUTF8(utf8
);
1777 if ( !utf8
|| !wxStringOperations::IsValidUtf8String(utf8
, len
) )
1780 return FromImpl(wxStringImpl(utf8
, len
));
1783 const wxScopedCharBuffer
utf8_str() const
1784 { return wxCharBuffer::CreateNonOwned(m_impl
.c_str(), m_impl
.length()); }
1786 // this function exists in UTF-8 build only and returns the length of the
1787 // internal UTF-8 representation
1788 size_t utf8_length() const { return m_impl
.length(); }
1789 #elif wxUSE_UNICODE_WCHAR
1790 static wxString
FromUTF8(const char *utf8
, size_t len
= npos
)
1791 { return wxString(utf8
, wxMBConvUTF8(), len
); }
1792 static wxString
FromUTF8Unchecked(const char *utf8
, size_t len
= npos
)
1794 const wxString
s(utf8
, wxMBConvUTF8(), len
);
1795 wxASSERT_MSG( !utf8
|| !*utf8
|| !s
.empty(),
1796 "string must be valid UTF-8" );
1799 const wxScopedCharBuffer
utf8_str() const { return mb_str(wxMBConvUTF8()); }
1801 static wxString
FromUTF8(const char *utf8
)
1802 { return wxString(wxMBConvUTF8().cMB2WC(utf8
)); }
1803 static wxString
FromUTF8(const char *utf8
, size_t len
)
1806 wxScopedWCharBuffer
buf(wxMBConvUTF8().cMB2WC(utf8
, len
== npos
? wxNO_LEN
: len
, &wlen
));
1807 return wxString(buf
.data(), wlen
);
1809 static wxString
FromUTF8Unchecked(const char *utf8
, size_t len
= npos
)
1812 wxScopedWCharBuffer buf
1814 wxMBConvUTF8().cMB2WC
1817 len
== npos
? wxNO_LEN
: len
,
1821 wxASSERT_MSG( !utf8
|| !*utf8
|| wlen
,
1822 "string must be valid UTF-8" );
1824 return wxString(buf
.data(), wlen
);
1826 const wxScopedCharBuffer
utf8_str() const
1827 { return wxMBConvUTF8().cWC2MB(wc_str()); }
1830 const wxScopedCharBuffer
ToUTF8() const { return utf8_str(); }
1832 // functions for storing binary data in wxString:
1834 static wxString
From8BitData(const char *data
, size_t len
)
1835 { return wxString(data
, wxConvISO8859_1
, len
); }
1836 // version for NUL-terminated data:
1837 static wxString
From8BitData(const char *data
)
1838 { return wxString(data
, wxConvISO8859_1
); }
1839 const wxScopedCharBuffer
To8BitData() const
1840 { return mb_str(wxConvISO8859_1
); }
1842 static wxString
From8BitData(const char *data
, size_t len
)
1843 { return wxString(data
, len
); }
1844 // version for NUL-terminated data:
1845 static wxString
From8BitData(const char *data
)
1846 { return wxString(data
); }
1847 const wxScopedCharBuffer
To8BitData() const
1848 { return wxScopedCharBuffer::CreateNonOwned(wx_str(), length()); }
1849 #endif // Unicode/ANSI
1851 // conversions with (possible) format conversions: have to return a
1852 // buffer with temporary data
1854 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
1855 // return an ANSI (multibyte) string, wc_str() to return a wide string and
1856 // fn_str() to return a string which should be used with the OS APIs
1857 // accepting the file names. The return value is always the same, but the
1858 // type differs because a function may either return pointer to the buffer
1859 // directly or have to use intermediate buffer for translation.
1863 // this is an optimization: even though using mb_str(wxConvLibc) does the
1864 // same thing (i.e. returns pointer to internal representation as locale is
1865 // always an UTF-8 one) in wxUSE_UTF8_LOCALE_ONLY case, we can avoid the
1866 // extra checks and the temporary buffer construction by providing a
1867 // separate mb_str() overload
1868 #if wxUSE_UTF8_LOCALE_ONLY
1869 const char* mb_str() const { return wx_str(); }
1870 const wxScopedCharBuffer
mb_str(const wxMBConv
& conv
) const
1872 return AsCharBuf(conv
);
1874 #else // !wxUSE_UTF8_LOCALE_ONLY
1875 const wxScopedCharBuffer
mb_str(const wxMBConv
& conv
= wxConvLibc
) const
1877 return AsCharBuf(conv
);
1879 #endif // wxUSE_UTF8_LOCALE_ONLY/!wxUSE_UTF8_LOCALE_ONLY
1881 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
1883 #if wxUSE_UNICODE_WCHAR
1884 const wchar_t* wc_str() const { return wx_str(); }
1885 #elif wxUSE_UNICODE_UTF8
1886 const wxScopedWCharBuffer
wc_str() const
1887 { return AsWCharBuf(wxMBConvStrictUTF8()); }
1889 // for compatibility with !wxUSE_UNICODE version
1890 const wxWX2WCbuf
wc_str(const wxMBConv
& WXUNUSED(conv
)) const
1891 { return wc_str(); }
1894 const wxScopedCharBuffer
fn_str() const { return mb_str(wxConvFile
); }
1896 const wxWX2WCbuf
fn_str() const { return wc_str(); }
1897 #endif // wxMBFILES/!wxMBFILES
1900 const char* mb_str() const { return wx_str(); }
1902 // for compatibility with wxUSE_UNICODE version
1903 const char* mb_str(const wxMBConv
& WXUNUSED(conv
)) const { return wx_str(); }
1905 const wxWX2MBbuf
mbc_str() const { return mb_str(); }
1907 const wxScopedWCharBuffer
wc_str(const wxMBConv
& conv
= wxConvLibc
) const
1908 { return AsWCharBuf(conv
); }
1910 const wxScopedCharBuffer
fn_str() const
1911 { return wxConvFile
.cWC2WX( wc_str( wxConvLibc
) ); }
1912 #endif // Unicode/ANSI
1914 #if wxUSE_UNICODE_UTF8
1915 const wxScopedWCharBuffer
t_str() const { return wc_str(); }
1916 #elif wxUSE_UNICODE_WCHAR
1917 const wchar_t* t_str() const { return wx_str(); }
1919 const char* t_str() const { return wx_str(); }
1923 // overloaded assignment
1924 // from another wxString
1925 wxString
& operator=(const wxString
& stringSrc
)
1927 if ( this != &stringSrc
)
1929 wxSTRING_INVALIDATE_CACHE();
1931 m_impl
= stringSrc
.m_impl
;
1937 wxString
& operator=(const wxCStrData
& cstr
)
1938 { return *this = cstr
.AsString(); }
1940 wxString
& operator=(wxUniChar ch
)
1942 wxSTRING_INVALIDATE_CACHE();
1944 #if wxUSE_UNICODE_UTF8
1945 if ( !ch
.IsAscii() )
1946 m_impl
= wxStringOperations::EncodeChar(ch
);
1948 #endif // wxUSE_UNICODE_UTF8
1949 m_impl
= (wxStringCharType
)ch
;
1953 wxString
& operator=(wxUniCharRef ch
)
1954 { return operator=((wxUniChar
)ch
); }
1955 wxString
& operator=(char ch
)
1956 { return operator=(wxUniChar(ch
)); }
1957 wxString
& operator=(unsigned char ch
)
1958 { return operator=(wxUniChar(ch
)); }
1959 wxString
& operator=(wchar_t ch
)
1960 { return operator=(wxUniChar(ch
)); }
1961 // from a C string - STL probably will crash on NULL,
1962 // so we need to compensate in that case
1963 #if wxUSE_STL_BASED_WXSTRING
1964 wxString
& operator=(const char *psz
)
1966 wxSTRING_INVALIDATE_CACHE();
1969 m_impl
= ImplStr(psz
);
1976 wxString
& operator=(const wchar_t *pwz
)
1978 wxSTRING_INVALIDATE_CACHE();
1981 m_impl
= ImplStr(pwz
);
1987 #else // !wxUSE_STL_BASED_WXSTRING
1988 wxString
& operator=(const char *psz
)
1990 wxSTRING_INVALIDATE_CACHE();
1992 m_impl
= ImplStr(psz
);
1997 wxString
& operator=(const wchar_t *pwz
)
1999 wxSTRING_INVALIDATE_CACHE();
2001 m_impl
= ImplStr(pwz
);
2005 #endif // wxUSE_STL_BASED_WXSTRING/!wxUSE_STL_BASED_WXSTRING
2007 wxString
& operator=(const unsigned char *psz
)
2008 { return operator=((const char*)psz
); }
2010 // from wxScopedWCharBuffer
2011 wxString
& operator=(const wxScopedWCharBuffer
& s
)
2012 { return assign(s
); }
2013 // from wxScopedCharBuffer
2014 wxString
& operator=(const wxScopedCharBuffer
& s
)
2015 { return assign(s
); }
2017 // string concatenation
2018 // in place concatenation
2020 Concatenate and return the result. Note that the left to right
2021 associativity of << allows to write things like "str << str1 << str2
2022 << ..." (unlike with +=)
2025 wxString
& operator<<(const wxString
& s
)
2027 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2028 wxASSERT_MSG( s
.IsValid(),
2029 wxT("did you forget to call UngetWriteBuf()?") );
2035 // string += C string
2036 wxString
& operator<<(const char *psz
)
2037 { append(psz
); return *this; }
2038 wxString
& operator<<(const wchar_t *pwz
)
2039 { append(pwz
); return *this; }
2040 wxString
& operator<<(const wxCStrData
& psz
)
2041 { append(psz
.AsString()); return *this; }
2043 wxString
& operator<<(wxUniChar ch
) { append(1, ch
); return *this; }
2044 wxString
& operator<<(wxUniCharRef ch
) { append(1, ch
); return *this; }
2045 wxString
& operator<<(char ch
) { append(1, ch
); return *this; }
2046 wxString
& operator<<(unsigned char ch
) { append(1, ch
); return *this; }
2047 wxString
& operator<<(wchar_t ch
) { append(1, ch
); return *this; }
2049 // string += buffer (i.e. from wxGetString)
2050 wxString
& operator<<(const wxScopedWCharBuffer
& s
)
2051 { return append(s
); }
2052 wxString
& operator<<(const wxScopedCharBuffer
& s
)
2053 { return append(s
); }
2055 // string += C string
2056 wxString
& Append(const wxString
& s
)
2058 // test for empty() to share the string if possible
2065 wxString
& Append(const char* psz
)
2066 { append(psz
); return *this; }
2067 wxString
& Append(const wchar_t* pwz
)
2068 { append(pwz
); return *this; }
2069 wxString
& Append(const wxCStrData
& psz
)
2070 { append(psz
); return *this; }
2071 wxString
& Append(const wxScopedCharBuffer
& psz
)
2072 { append(psz
); return *this; }
2073 wxString
& Append(const wxScopedWCharBuffer
& psz
)
2074 { append(psz
); return *this; }
2075 wxString
& Append(const char* psz
, size_t nLen
)
2076 { append(psz
, nLen
); return *this; }
2077 wxString
& Append(const wchar_t* pwz
, size_t nLen
)
2078 { append(pwz
, nLen
); return *this; }
2079 wxString
& Append(const wxCStrData
& psz
, size_t nLen
)
2080 { append(psz
, nLen
); return *this; }
2081 wxString
& Append(const wxScopedCharBuffer
& psz
, size_t nLen
)
2082 { append(psz
, nLen
); return *this; }
2083 wxString
& Append(const wxScopedWCharBuffer
& psz
, size_t nLen
)
2084 { append(psz
, nLen
); return *this; }
2085 // append count copies of given character
2086 wxString
& Append(wxUniChar ch
, size_t count
= 1u)
2087 { append(count
, ch
); return *this; }
2088 wxString
& Append(wxUniCharRef ch
, size_t count
= 1u)
2089 { append(count
, ch
); return *this; }
2090 wxString
& Append(char ch
, size_t count
= 1u)
2091 { append(count
, ch
); return *this; }
2092 wxString
& Append(unsigned char ch
, size_t count
= 1u)
2093 { append(count
, ch
); return *this; }
2094 wxString
& Append(wchar_t ch
, size_t count
= 1u)
2095 { append(count
, ch
); return *this; }
2097 // prepend a string, return the string itself
2098 wxString
& Prepend(const wxString
& str
)
2099 { *this = str
+ *this; return *this; }
2101 // non-destructive concatenation
2103 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
,
2104 const wxString
& string2
);
2105 // string with a single char
2106 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
2107 // char with a string
2108 friend wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
2109 // string with C string
2110 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
2112 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
2113 const wchar_t *pwz
);
2114 // C string with string
2115 friend wxString WXDLLIMPEXP_BASE
operator+(const char *psz
,
2116 const wxString
& string
);
2117 friend wxString WXDLLIMPEXP_BASE
operator+(const wchar_t *pwz
,
2118 const wxString
& string
);
2120 // stream-like functions
2121 // insert an int into string
2122 wxString
& operator<<(int i
)
2123 { return (*this) << Format(wxT("%d"), i
); }
2124 // insert an unsigned int into string
2125 wxString
& operator<<(unsigned int ui
)
2126 { return (*this) << Format(wxT("%u"), ui
); }
2127 // insert a long into string
2128 wxString
& operator<<(long l
)
2129 { return (*this) << Format(wxT("%ld"), l
); }
2130 // insert an unsigned long into string
2131 wxString
& operator<<(unsigned long ul
)
2132 { return (*this) << Format(wxT("%lu"), ul
); }
2133 #ifdef wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
2134 // insert a long long if they exist and aren't longs
2135 wxString
& operator<<(wxLongLong_t ll
)
2137 return (*this) << Format("%" wxLongLongFmtSpec
"d", ll
);
2139 // insert an unsigned long long
2140 wxString
& operator<<(wxULongLong_t ull
)
2142 return (*this) << Format("%" wxLongLongFmtSpec
"u" , ull
);
2144 #endif // wxHAS_LONG_LONG_T_DIFFERENT_FROM_LONG
2145 // insert a float into string
2146 wxString
& operator<<(float f
)
2147 { return (*this) << Format(wxT("%f"), f
); }
2148 // insert a double into string
2149 wxString
& operator<<(double d
)
2150 { return (*this) << Format(wxT("%g"), d
); }
2152 // string comparison
2153 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
2154 int Cmp(const char *psz
) const
2155 { return compare(psz
); }
2156 int Cmp(const wchar_t *pwz
) const
2157 { return compare(pwz
); }
2158 int Cmp(const wxString
& s
) const
2159 { return compare(s
); }
2160 int Cmp(const wxCStrData
& s
) const
2161 { return compare(s
); }
2162 int Cmp(const wxScopedCharBuffer
& s
) const
2163 { return compare(s
); }
2164 int Cmp(const wxScopedWCharBuffer
& s
) const
2165 { return compare(s
); }
2166 // same as Cmp() but not case-sensitive
2167 int CmpNoCase(const wxString
& s
) const;
2169 // test for the string equality, either considering case or not
2170 // (if compareWithCase then the case matters)
2171 bool IsSameAs(const wxString
& str
, bool compareWithCase
= true) const
2173 #if !wxUSE_UNICODE_UTF8
2174 // in UTF-8 build, length() is O(n) and doing this would be _slower_
2175 if ( length() != str
.length() )
2178 return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0;
2180 bool IsSameAs(const char *str
, bool compareWithCase
= true) const
2181 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
2182 bool IsSameAs(const wchar_t *str
, bool compareWithCase
= true) const
2183 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
2185 bool IsSameAs(const wxCStrData
& str
, bool compareWithCase
= true) const
2186 { return IsSameAs(str
.AsString(), compareWithCase
); }
2187 bool IsSameAs(const wxScopedCharBuffer
& str
, bool compareWithCase
= true) const
2188 { return IsSameAs(str
.data(), compareWithCase
); }
2189 bool IsSameAs(const wxScopedWCharBuffer
& str
, bool compareWithCase
= true) const
2190 { return IsSameAs(str
.data(), compareWithCase
); }
2191 // comparison with a single character: returns true if equal
2192 bool IsSameAs(wxUniChar c
, bool compareWithCase
= true) const;
2193 // FIXME-UTF8: remove these overloads
2194 bool IsSameAs(wxUniCharRef c
, bool compareWithCase
= true) const
2195 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
2196 bool IsSameAs(char c
, bool compareWithCase
= true) const
2197 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
2198 bool IsSameAs(unsigned char c
, bool compareWithCase
= true) const
2199 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
2200 bool IsSameAs(wchar_t c
, bool compareWithCase
= true) const
2201 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
2202 bool IsSameAs(int c
, bool compareWithCase
= true) const
2203 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
2205 // simple sub-string extraction
2206 // return substring starting at nFirst of length nCount (or till the end
2207 // if nCount = default value)
2208 wxString
Mid(size_t nFirst
, size_t nCount
= npos
) const;
2210 // operator version of Mid()
2211 wxString
operator()(size_t start
, size_t len
) const
2212 { return Mid(start
, len
); }
2214 // check if the string starts with the given prefix and return the rest
2215 // of the string in the provided pointer if it is not NULL; otherwise
2217 bool StartsWith(const wxString
& prefix
, wxString
*rest
= NULL
) const;
2218 // check if the string ends with the given suffix and return the
2219 // beginning of the string before the suffix in the provided pointer if
2220 // it is not NULL; otherwise return false
2221 bool EndsWith(const wxString
& suffix
, wxString
*rest
= NULL
) const;
2223 // get first nCount characters
2224 wxString
Left(size_t nCount
) const;
2225 // get last nCount characters
2226 wxString
Right(size_t nCount
) const;
2227 // get all characters before the first occurrence of ch
2228 // (returns the whole string if ch not found) and also put everything
2229 // following the first occurrence of ch into rest if it's non-NULL
2230 wxString
BeforeFirst(wxUniChar ch
, wxString
*rest
= NULL
) const;
2231 // get all characters before the last occurrence of ch
2232 // (returns empty string if ch not found) and also put everything
2233 // following the last occurrence of ch into rest if it's non-NULL
2234 wxString
BeforeLast(wxUniChar ch
, wxString
*rest
= NULL
) const;
2235 // get all characters after the first occurrence of ch
2236 // (returns empty string if ch not found)
2237 wxString
AfterFirst(wxUniChar ch
) const;
2238 // get all characters after the last occurrence of ch
2239 // (returns the whole string if ch not found)
2240 wxString
AfterLast(wxUniChar ch
) const;
2242 // for compatibility only, use more explicitly named functions above
2243 wxString
Before(wxUniChar ch
) const { return BeforeLast(ch
); }
2244 wxString
After(wxUniChar ch
) const { return AfterFirst(ch
); }
2247 // convert to upper case in place, return the string itself
2248 wxString
& MakeUpper();
2249 // convert to upper case, return the copy of the string
2250 wxString
Upper() const { return wxString(*this).MakeUpper(); }
2251 // convert to lower case in place, return the string itself
2252 wxString
& MakeLower();
2253 // convert to lower case, return the copy of the string
2254 wxString
Lower() const { return wxString(*this).MakeLower(); }
2255 // convert the first character to the upper case and the rest to the
2256 // lower one, return the modified string itself
2257 wxString
& MakeCapitalized();
2258 // convert the first character to the upper case and the rest to the
2259 // lower one, return the copy of the string
2260 wxString
Capitalize() const { return wxString(*this).MakeCapitalized(); }
2262 // trimming/padding whitespace (either side) and truncating
2263 // remove spaces from left or from right (default) side
2264 wxString
& Trim(bool bFromRight
= true);
2265 // add nCount copies chPad in the beginning or at the end (default)
2266 wxString
& Pad(size_t nCount
, wxUniChar chPad
= wxT(' '), bool bFromRight
= true);
2268 // searching and replacing
2269 // searching (return starting index, or -1 if not found)
2270 int Find(wxUniChar ch
, bool bFromEnd
= false) const; // like strchr/strrchr
2271 int Find(wxUniCharRef ch
, bool bFromEnd
= false) const
2272 { return Find(wxUniChar(ch
), bFromEnd
); }
2273 int Find(char ch
, bool bFromEnd
= false) const
2274 { return Find(wxUniChar(ch
), bFromEnd
); }
2275 int Find(unsigned char ch
, bool bFromEnd
= false) const
2276 { return Find(wxUniChar(ch
), bFromEnd
); }
2277 int Find(wchar_t ch
, bool bFromEnd
= false) const
2278 { return Find(wxUniChar(ch
), bFromEnd
); }
2279 // searching (return starting index, or -1 if not found)
2280 int Find(const wxString
& sub
) const // like strstr
2282 size_type idx
= find(sub
);
2283 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
2285 int Find(const char *sub
) const // like strstr
2287 size_type idx
= find(sub
);
2288 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
2290 int Find(const wchar_t *sub
) const // like strstr
2292 size_type idx
= find(sub
);
2293 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
2296 int Find(const wxCStrData
& sub
) const
2297 { return Find(sub
.AsString()); }
2298 int Find(const wxScopedCharBuffer
& sub
) const
2299 { return Find(sub
.data()); }
2300 int Find(const wxScopedWCharBuffer
& sub
) const
2301 { return Find(sub
.data()); }
2303 // replace first (or all of bReplaceAll) occurrences of substring with
2304 // another string, returns the number of replacements made
2305 size_t Replace(const wxString
& strOld
,
2306 const wxString
& strNew
,
2307 bool bReplaceAll
= true);
2309 // check if the string contents matches a mask containing '*' and '?'
2310 bool Matches(const wxString
& mask
) const;
2312 // conversion to numbers: all functions return true only if the whole
2313 // string is a number and put the value of this number into the pointer
2314 // provided, the base is the numeric base in which the conversion should be
2315 // done and must be comprised between 2 and 36 or be 0 in which case the
2316 // standard C rules apply (leading '0' => octal, "0x" => hex)
2317 // convert to a signed integer
2318 bool ToLong(long *val
, int base
= 10) const;
2319 // convert to an unsigned integer
2320 bool ToULong(unsigned long *val
, int base
= 10) const;
2321 // convert to wxLongLong
2322 #if defined(wxLongLong_t)
2323 bool ToLongLong(wxLongLong_t
*val
, int base
= 10) const;
2324 // convert to wxULongLong
2325 bool ToULongLong(wxULongLong_t
*val
, int base
= 10) const;
2326 #endif // wxLongLong_t
2327 // convert to a double
2328 bool ToDouble(double *val
) const;
2330 // conversions to numbers using C locale
2331 // convert to a signed integer
2332 bool ToCLong(long *val
, int base
= 10) const;
2333 // convert to an unsigned integer
2334 bool ToCULong(unsigned long *val
, int base
= 10) const;
2335 // convert to a double
2336 bool ToCDouble(double *val
) const;
2338 // create a string representing the given floating point number with the
2339 // default (like %g) or fixed (if precision >=0) precision
2340 // in the current locale
2341 static wxString
FromDouble(double val
, int precision
= -1);
2343 static wxString
FromCDouble(double val
, int precision
= -1);
2345 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2346 // formatted input/output
2347 // as sprintf(), returns the number of characters written or < 0 on error
2348 // (take 'this' into account in attribute parameter count)
2349 // int Printf(const wxString& format, ...);
2350 WX_DEFINE_VARARG_FUNC(int, Printf
, 1, (const wxFormatString
&),
2351 DoPrintfWchar
, DoPrintfUtf8
)
2353 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
2354 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wxString
&),
2355 (wxFormatString(f1
)));
2356 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wxCStrData
&),
2357 (wxFormatString(f1
)));
2358 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const char*),
2359 (wxFormatString(f1
)));
2360 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wchar_t*),
2361 (wxFormatString(f1
)));
2363 #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN
2364 // as vprintf(), returns the number of characters written or < 0 on error
2365 int PrintfV(const wxString
& format
, va_list argptr
);
2367 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2368 // returns the string containing the result of Printf() to it
2369 // static wxString Format(const wxString& format, ...) WX_ATTRIBUTE_PRINTF_1;
2370 WX_DEFINE_VARARG_FUNC(static wxString
, Format
, 1, (const wxFormatString
&),
2371 DoFormatWchar
, DoFormatUtf8
)
2373 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
2374 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wxString
&),
2375 (wxFormatString(f1
)));
2376 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wxCStrData
&),
2377 (wxFormatString(f1
)));
2378 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const char*),
2379 (wxFormatString(f1
)));
2380 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wchar_t*),
2381 (wxFormatString(f1
)));
2384 // the same as above, but takes a va_list
2385 static wxString
FormatV(const wxString
& format
, va_list argptr
);
2387 // raw access to string memory
2388 // ensure that string has space for at least nLen characters
2389 // only works if the data of this string is not shared
2390 bool Alloc(size_t nLen
) { reserve(nLen
); return capacity() >= nLen
; }
2391 // minimize the string's memory
2392 // only works if the data of this string is not shared
2394 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2395 // These are deprecated, use wxStringBuffer or wxStringBufferLength instead
2397 // get writable buffer of at least nLen bytes. Unget() *must* be called
2398 // a.s.a.p. to put string back in a reasonable state!
2399 wxDEPRECATED( wxStringCharType
*GetWriteBuf(size_t nLen
) );
2400 // call this immediately after GetWriteBuf() has been used
2401 wxDEPRECATED( void UngetWriteBuf() );
2402 wxDEPRECATED( void UngetWriteBuf(size_t nLen
) );
2403 #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && wxUSE_UNICODE_UTF8
2405 // wxWidgets version 1 compatibility functions
2408 wxString
SubString(size_t from
, size_t to
) const
2409 { return Mid(from
, (to
- from
+ 1)); }
2410 // values for second parameter of CompareTo function
2411 enum caseCompare
{exact
, ignoreCase
};
2412 // values for first parameter of Strip function
2413 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
2415 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2417 // (take 'this' into account in attribute parameter count)
2418 // int sprintf(const wxString& format, ...) WX_ATTRIBUTE_PRINTF_2;
2419 WX_DEFINE_VARARG_FUNC(int, sprintf
, 1, (const wxFormatString
&),
2420 DoPrintfWchar
, DoPrintfUtf8
)
2422 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
2423 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wxString
&),
2424 (wxFormatString(f1
)));
2425 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wxCStrData
&),
2426 (wxFormatString(f1
)));
2427 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const char*),
2428 (wxFormatString(f1
)));
2429 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wchar_t*),
2430 (wxFormatString(f1
)));
2432 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
2435 int CompareTo(const wxChar
* psz
, caseCompare cmp
= exact
) const
2436 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
2439 size_t Length() const { return length(); }
2440 // Count the number of characters
2441 int Freq(wxUniChar ch
) const;
2443 void LowerCase() { MakeLower(); }
2445 void UpperCase() { MakeUpper(); }
2446 // use Trim except that it doesn't change this string
2447 wxString
Strip(stripType w
= trailing
) const;
2449 // use Find (more general variants not yet supported)
2450 size_t Index(const wxChar
* psz
) const { return Find(psz
); }
2451 size_t Index(wxUniChar ch
) const { return Find(ch
); }
2453 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
2454 wxString
& RemoveLast(size_t n
= 1) { return Truncate(length() - n
); }
2456 wxString
& Remove(size_t nStart
, size_t nLen
)
2457 { return (wxString
&)erase( nStart
, nLen
); }
2460 int First( wxUniChar ch
) const { return Find(ch
); }
2461 int First( wxUniCharRef ch
) const { return Find(ch
); }
2462 int First( char ch
) const { return Find(ch
); }
2463 int First( unsigned char ch
) const { return Find(ch
); }
2464 int First( wchar_t ch
) const { return Find(ch
); }
2465 int First( const wxString
& str
) const { return Find(str
); }
2466 int Last( wxUniChar ch
) const { return Find(ch
, true); }
2467 bool Contains(const wxString
& str
) const { return Find(str
) != wxNOT_FOUND
; }
2470 bool IsNull() const { return empty(); }
2472 // std::string compatibility functions
2474 // take nLen chars starting at nPos
2475 wxString(const wxString
& str
, size_t nPos
, size_t nLen
)
2476 { assign(str
, nPos
, nLen
); }
2477 // take all characters from first to last
2478 wxString(const_iterator first
, const_iterator last
)
2479 : m_impl(first
.impl(), last
.impl()) { }
2480 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2481 // the 2 overloads below are for compatibility with the existing code using
2482 // pointers instead of iterators
2483 wxString(const char *first
, const char *last
)
2485 SubstrBufFromMB
str(ImplStr(first
, last
- first
));
2486 m_impl
.assign(str
.data
, str
.len
);
2488 wxString(const wchar_t *first
, const wchar_t *last
)
2490 SubstrBufFromWC
str(ImplStr(first
, last
- first
));
2491 m_impl
.assign(str
.data
, str
.len
);
2493 // and this one is needed to compile code adding offsets to c_str() result
2494 wxString(const wxCStrData
& first
, const wxCStrData
& last
)
2495 : m_impl(CreateConstIterator(first
).impl(),
2496 CreateConstIterator(last
).impl())
2498 wxASSERT_MSG( first
.m_str
== last
.m_str
,
2499 wxT("pointers must be into the same string") );
2501 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2503 // lib.string.modifiers
2504 // append elements str[pos], ..., str[pos+n]
2505 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
2507 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2510 str
.PosLenToImpl(pos
, n
, &from
, &len
);
2511 m_impl
.append(str
.m_impl
, from
, len
);
2515 wxString
& append(const wxString
& str
)
2517 wxSTRING_UPDATE_CACHED_LENGTH(str
.length());
2519 m_impl
.append(str
.m_impl
);
2523 // append first n (or all if n == npos) characters of sz
2524 wxString
& append(const char *sz
)
2526 wxSTRING_INVALIDATE_CACHED_LENGTH();
2528 m_impl
.append(ImplStr(sz
));
2532 wxString
& append(const wchar_t *sz
)
2534 wxSTRING_INVALIDATE_CACHED_LENGTH();
2536 m_impl
.append(ImplStr(sz
));
2540 wxString
& append(const char *sz
, size_t n
)
2542 wxSTRING_INVALIDATE_CACHED_LENGTH();
2544 SubstrBufFromMB
str(ImplStr(sz
, n
));
2545 m_impl
.append(str
.data
, str
.len
);
2548 wxString
& append(const wchar_t *sz
, size_t n
)
2550 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2552 SubstrBufFromWC
str(ImplStr(sz
, n
));
2553 m_impl
.append(str
.data
, str
.len
);
2557 wxString
& append(const wxCStrData
& str
)
2558 { return append(str
.AsString()); }
2559 wxString
& append(const wxScopedCharBuffer
& str
)
2560 { return append(str
.data(), str
.length()); }
2561 wxString
& append(const wxScopedWCharBuffer
& str
)
2562 { return append(str
.data(), str
.length()); }
2563 wxString
& append(const wxCStrData
& str
, size_t n
)
2564 { return append(str
.AsString(), 0, n
); }
2565 wxString
& append(const wxScopedCharBuffer
& str
, size_t n
)
2566 { return append(str
.data(), n
); }
2567 wxString
& append(const wxScopedWCharBuffer
& str
, size_t n
)
2568 { return append(str
.data(), n
); }
2570 // append n copies of ch
2571 wxString
& append(size_t n
, wxUniChar ch
)
2573 #if wxUSE_UNICODE_UTF8
2574 if ( !ch
.IsAscii() )
2576 wxSTRING_INVALIDATE_CACHED_LENGTH();
2578 m_impl
.append(wxStringOperations::EncodeNChars(n
, ch
));
2583 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2585 m_impl
.append(n
, (wxStringCharType
)ch
);
2591 wxString
& append(size_t n
, wxUniCharRef ch
)
2592 { return append(n
, wxUniChar(ch
)); }
2593 wxString
& append(size_t n
, char ch
)
2594 { return append(n
, wxUniChar(ch
)); }
2595 wxString
& append(size_t n
, unsigned char ch
)
2596 { return append(n
, wxUniChar(ch
)); }
2597 wxString
& append(size_t n
, wchar_t ch
)
2598 { return append(n
, wxUniChar(ch
)); }
2600 // append from first to last
2601 wxString
& append(const_iterator first
, const_iterator last
)
2603 wxSTRING_INVALIDATE_CACHED_LENGTH();
2605 m_impl
.append(first
.impl(), last
.impl());
2608 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2609 wxString
& append(const char *first
, const char *last
)
2610 { return append(first
, last
- first
); }
2611 wxString
& append(const wchar_t *first
, const wchar_t *last
)
2612 { return append(first
, last
- first
); }
2613 wxString
& append(const wxCStrData
& first
, const wxCStrData
& last
)
2614 { return append(CreateConstIterator(first
), CreateConstIterator(last
)); }
2615 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2617 // same as `this_string = str'
2618 wxString
& assign(const wxString
& str
)
2620 wxSTRING_SET_CACHED_LENGTH(str
.length());
2622 m_impl
= str
.m_impl
;
2627 // This is a non-standard-compliant overload taking the first "len"
2628 // characters of the source string.
2629 wxString
& assign(const wxString
& str
, size_t len
)
2631 #if wxUSE_STRING_POS_CACHE
2632 // It is legal to pass len > str.length() to wxStringImpl::assign() but
2633 // by restricting it here we save some work for that function so it's not
2634 // really less efficient and, at the same time, ensure that we don't
2635 // cache invalid length.
2636 const size_t lenSrc
= str
.length();
2640 wxSTRING_SET_CACHED_LENGTH(len
);
2641 #endif // wxUSE_STRING_POS_CACHE
2643 m_impl
.assign(str
.m_impl
, 0, str
.LenToImpl(len
));
2648 // same as ` = str[pos..pos + n]
2649 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
2652 str
.PosLenToImpl(pos
, n
, &from
, &len
);
2653 m_impl
.assign(str
.m_impl
, from
, len
);
2655 // it's important to call this after PosLenToImpl() above in case str is
2656 // the same string as this one
2657 wxSTRING_SET_CACHED_LENGTH(n
);
2662 // same as `= first n (or all if n == npos) characters of sz'
2663 wxString
& assign(const char *sz
)
2665 wxSTRING_INVALIDATE_CACHE();
2667 m_impl
.assign(ImplStr(sz
));
2672 wxString
& assign(const wchar_t *sz
)
2674 wxSTRING_INVALIDATE_CACHE();
2676 m_impl
.assign(ImplStr(sz
));
2681 wxString
& assign(const char *sz
, size_t n
)
2683 wxSTRING_INVALIDATE_CACHE();
2685 SubstrBufFromMB
str(ImplStr(sz
, n
));
2686 m_impl
.assign(str
.data
, str
.len
);
2691 wxString
& assign(const wchar_t *sz
, size_t n
)
2693 wxSTRING_SET_CACHED_LENGTH(n
);
2695 SubstrBufFromWC
str(ImplStr(sz
, n
));
2696 m_impl
.assign(str
.data
, str
.len
);
2701 wxString
& assign(const wxCStrData
& str
)
2702 { return assign(str
.AsString()); }
2703 wxString
& assign(const wxScopedCharBuffer
& str
)
2704 { return assign(str
.data(), str
.length()); }
2705 wxString
& assign(const wxScopedWCharBuffer
& str
)
2706 { return assign(str
.data(), str
.length()); }
2707 wxString
& assign(const wxCStrData
& str
, size_t len
)
2708 { return assign(str
.AsString(), len
); }
2709 wxString
& assign(const wxScopedCharBuffer
& str
, size_t len
)
2710 { return assign(str
.data(), len
); }
2711 wxString
& assign(const wxScopedWCharBuffer
& str
, size_t len
)
2712 { return assign(str
.data(), len
); }
2714 // same as `= n copies of ch'
2715 wxString
& assign(size_t n
, wxUniChar ch
)
2717 wxSTRING_SET_CACHED_LENGTH(n
);
2719 #if wxUSE_UNICODE_UTF8
2720 if ( !ch
.IsAscii() )
2721 m_impl
.assign(wxStringOperations::EncodeNChars(n
, ch
));
2724 m_impl
.assign(n
, (wxStringCharType
)ch
);
2729 wxString
& assign(size_t n
, wxUniCharRef ch
)
2730 { return assign(n
, wxUniChar(ch
)); }
2731 wxString
& assign(size_t n
, char ch
)
2732 { return assign(n
, wxUniChar(ch
)); }
2733 wxString
& assign(size_t n
, unsigned char ch
)
2734 { return assign(n
, wxUniChar(ch
)); }
2735 wxString
& assign(size_t n
, wchar_t ch
)
2736 { return assign(n
, wxUniChar(ch
)); }
2738 // assign from first to last
2739 wxString
& assign(const_iterator first
, const_iterator last
)
2741 wxSTRING_INVALIDATE_CACHE();
2743 m_impl
.assign(first
.impl(), last
.impl());
2747 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2748 wxString
& assign(const char *first
, const char *last
)
2749 { return assign(first
, last
- first
); }
2750 wxString
& assign(const wchar_t *first
, const wchar_t *last
)
2751 { return assign(first
, last
- first
); }
2752 wxString
& assign(const wxCStrData
& first
, const wxCStrData
& last
)
2753 { return assign(CreateConstIterator(first
), CreateConstIterator(last
)); }
2754 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2756 // string comparison
2757 int compare(const wxString
& str
) const;
2758 int compare(const char* sz
) const;
2759 int compare(const wchar_t* sz
) const;
2760 int compare(const wxCStrData
& str
) const
2761 { return compare(str
.AsString()); }
2762 int compare(const wxScopedCharBuffer
& str
) const
2763 { return compare(str
.data()); }
2764 int compare(const wxScopedWCharBuffer
& str
) const
2765 { return compare(str
.data()); }
2766 // comparison with a substring
2767 int compare(size_t nStart
, size_t nLen
, const wxString
& str
) const;
2768 // comparison of 2 substrings
2769 int compare(size_t nStart
, size_t nLen
,
2770 const wxString
& str
, size_t nStart2
, size_t nLen2
) const;
2771 // substring comparison with first nCount characters of sz
2772 int compare(size_t nStart
, size_t nLen
,
2773 const char* sz
, size_t nCount
= npos
) const;
2774 int compare(size_t nStart
, size_t nLen
,
2775 const wchar_t* sz
, size_t nCount
= npos
) const;
2777 // insert another string
2778 wxString
& insert(size_t nPos
, const wxString
& str
)
2779 { insert(GetIterForNthChar(nPos
), str
.begin(), str
.end()); return *this; }
2780 // insert n chars of str starting at nStart (in str)
2781 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
2783 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2786 str
.PosLenToImpl(nStart
, n
, &from
, &len
);
2787 m_impl
.insert(PosToImpl(nPos
), str
.m_impl
, from
, len
);
2792 // insert first n (or all if n == npos) characters of sz
2793 wxString
& insert(size_t nPos
, const char *sz
)
2795 wxSTRING_INVALIDATE_CACHE();
2797 m_impl
.insert(PosToImpl(nPos
), ImplStr(sz
));
2802 wxString
& insert(size_t nPos
, const wchar_t *sz
)
2804 wxSTRING_INVALIDATE_CACHE();
2806 m_impl
.insert(PosToImpl(nPos
), ImplStr(sz
)); return *this;
2809 wxString
& insert(size_t nPos
, const char *sz
, size_t n
)
2811 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2813 SubstrBufFromMB
str(ImplStr(sz
, n
));
2814 m_impl
.insert(PosToImpl(nPos
), str
.data
, str
.len
);
2819 wxString
& insert(size_t nPos
, const wchar_t *sz
, size_t n
)
2821 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2823 SubstrBufFromWC
str(ImplStr(sz
, n
));
2824 m_impl
.insert(PosToImpl(nPos
), str
.data
, str
.len
);
2829 // insert n copies of ch
2830 wxString
& insert(size_t nPos
, size_t n
, wxUniChar ch
)
2832 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2834 #if wxUSE_UNICODE_UTF8
2835 if ( !ch
.IsAscii() )
2836 m_impl
.insert(PosToImpl(nPos
), wxStringOperations::EncodeNChars(n
, ch
));
2839 m_impl
.insert(PosToImpl(nPos
), n
, (wxStringCharType
)ch
);
2843 iterator
insert(iterator it
, wxUniChar ch
)
2845 wxSTRING_UPDATE_CACHED_LENGTH(1);
2847 #if wxUSE_UNICODE_UTF8
2848 if ( !ch
.IsAscii() )
2850 size_t pos
= IterToImplPos(it
);
2851 m_impl
.insert(pos
, wxStringOperations::EncodeChar(ch
));
2852 return iterator(this, m_impl
.begin() + pos
);
2856 return iterator(this, m_impl
.insert(it
.impl(), (wxStringCharType
)ch
));
2859 void insert(iterator it
, const_iterator first
, const_iterator last
)
2861 wxSTRING_INVALIDATE_CACHE();
2863 m_impl
.insert(it
.impl(), first
.impl(), last
.impl());
2866 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2867 void insert(iterator it
, const char *first
, const char *last
)
2868 { insert(it
- begin(), first
, last
- first
); }
2869 void insert(iterator it
, const wchar_t *first
, const wchar_t *last
)
2870 { insert(it
- begin(), first
, last
- first
); }
2871 void insert(iterator it
, const wxCStrData
& first
, const wxCStrData
& last
)
2872 { insert(it
, CreateConstIterator(first
), CreateConstIterator(last
)); }
2873 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2875 void insert(iterator it
, size_type n
, wxUniChar ch
)
2877 wxSTRING_UPDATE_CACHED_LENGTH(n
);
2879 #if wxUSE_UNICODE_UTF8
2880 if ( !ch
.IsAscii() )
2881 m_impl
.insert(IterToImplPos(it
), wxStringOperations::EncodeNChars(n
, ch
));
2884 m_impl
.insert(it
.impl(), n
, (wxStringCharType
)ch
);
2887 // delete characters from nStart to nStart + nLen
2888 wxString
& erase(size_type pos
= 0, size_type n
= npos
)
2890 wxSTRING_INVALIDATE_CACHE();
2893 PosLenToImpl(pos
, n
, &from
, &len
);
2894 m_impl
.erase(from
, len
);
2899 // delete characters from first up to last
2900 iterator
erase(iterator first
, iterator last
)
2902 wxSTRING_INVALIDATE_CACHE();
2904 return iterator(this, m_impl
.erase(first
.impl(), last
.impl()));
2907 iterator
erase(iterator first
)
2909 wxSTRING_UPDATE_CACHED_LENGTH(-1);
2911 return iterator(this, m_impl
.erase(first
.impl()));
2914 #ifdef wxSTRING_BASE_HASNT_CLEAR
2915 void clear() { erase(); }
2919 wxSTRING_SET_CACHED_LENGTH(0);
2925 // replaces the substring of length nLen starting at nStart
2926 wxString
& replace(size_t nStart
, size_t nLen
, const char* sz
)
2928 wxSTRING_INVALIDATE_CACHE();
2931 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2932 m_impl
.replace(from
, len
, ImplStr(sz
));
2937 wxString
& replace(size_t nStart
, size_t nLen
, const wchar_t* sz
)
2939 wxSTRING_INVALIDATE_CACHE();
2942 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2943 m_impl
.replace(from
, len
, ImplStr(sz
));
2948 // replaces the substring of length nLen starting at nStart
2949 wxString
& replace(size_t nStart
, size_t nLen
, const wxString
& str
)
2951 wxSTRING_INVALIDATE_CACHE();
2954 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2955 m_impl
.replace(from
, len
, str
.m_impl
);
2960 // replaces the substring with nCount copies of ch
2961 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxUniChar ch
)
2963 wxSTRING_INVALIDATE_CACHE();
2966 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2967 #if wxUSE_UNICODE_UTF8
2968 if ( !ch
.IsAscii() )
2969 m_impl
.replace(from
, len
, wxStringOperations::EncodeNChars(nCount
, ch
));
2972 m_impl
.replace(from
, len
, nCount
, (wxStringCharType
)ch
);
2977 // replaces a substring with another substring
2978 wxString
& replace(size_t nStart
, size_t nLen
,
2979 const wxString
& str
, size_t nStart2
, size_t nLen2
)
2981 wxSTRING_INVALIDATE_CACHE();
2984 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2987 str
.PosLenToImpl(nStart2
, nLen2
, &from2
, &len2
);
2989 m_impl
.replace(from
, len
, str
.m_impl
, from2
, len2
);
2994 // replaces the substring with first nCount chars of sz
2995 wxString
& replace(size_t nStart
, size_t nLen
,
2996 const char* sz
, size_t nCount
)
2998 wxSTRING_INVALIDATE_CACHE();
3001 PosLenToImpl(nStart
, nLen
, &from
, &len
);
3003 SubstrBufFromMB
str(ImplStr(sz
, nCount
));
3005 m_impl
.replace(from
, len
, str
.data
, str
.len
);
3010 wxString
& replace(size_t nStart
, size_t nLen
,
3011 const wchar_t* sz
, size_t nCount
)
3013 wxSTRING_INVALIDATE_CACHE();
3016 PosLenToImpl(nStart
, nLen
, &from
, &len
);
3018 SubstrBufFromWC
str(ImplStr(sz
, nCount
));
3020 m_impl
.replace(from
, len
, str
.data
, str
.len
);
3025 wxString
& replace(size_t nStart
, size_t nLen
,
3026 const wxString
& s
, size_t nCount
)
3028 wxSTRING_INVALIDATE_CACHE();
3031 PosLenToImpl(nStart
, nLen
, &from
, &len
);
3032 m_impl
.replace(from
, len
, s
.m_impl
.c_str(), s
.LenToImpl(nCount
));
3037 wxString
& replace(iterator first
, iterator last
, const char* s
)
3039 wxSTRING_INVALIDATE_CACHE();
3041 m_impl
.replace(first
.impl(), last
.impl(), ImplStr(s
));
3046 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
)
3048 wxSTRING_INVALIDATE_CACHE();
3050 m_impl
.replace(first
.impl(), last
.impl(), ImplStr(s
));
3055 wxString
& replace(iterator first
, iterator last
, const char* s
, size_type n
)
3057 wxSTRING_INVALIDATE_CACHE();
3059 SubstrBufFromMB
str(ImplStr(s
, n
));
3060 m_impl
.replace(first
.impl(), last
.impl(), str
.data
, str
.len
);
3065 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
, size_type n
)
3067 wxSTRING_INVALIDATE_CACHE();
3069 SubstrBufFromWC
str(ImplStr(s
, n
));
3070 m_impl
.replace(first
.impl(), last
.impl(), str
.data
, str
.len
);
3075 wxString
& replace(iterator first
, iterator last
, const wxString
& s
)
3077 wxSTRING_INVALIDATE_CACHE();
3079 m_impl
.replace(first
.impl(), last
.impl(), s
.m_impl
);
3084 wxString
& replace(iterator first
, iterator last
, size_type n
, wxUniChar ch
)
3086 wxSTRING_INVALIDATE_CACHE();
3088 #if wxUSE_UNICODE_UTF8
3089 if ( !ch
.IsAscii() )
3090 m_impl
.replace(first
.impl(), last
.impl(),
3091 wxStringOperations::EncodeNChars(n
, ch
));
3094 m_impl
.replace(first
.impl(), last
.impl(), n
, (wxStringCharType
)ch
);
3099 wxString
& replace(iterator first
, iterator last
,
3100 const_iterator first1
, const_iterator last1
)
3102 wxSTRING_INVALIDATE_CACHE();
3104 m_impl
.replace(first
.impl(), last
.impl(), first1
.impl(), last1
.impl());
3109 wxString
& replace(iterator first
, iterator last
,
3110 const char *first1
, const char *last1
)
3111 { replace(first
, last
, first1
, last1
- first1
); return *this; }
3112 wxString
& replace(iterator first
, iterator last
,
3113 const wchar_t *first1
, const wchar_t *last1
)
3114 { replace(first
, last
, first1
, last1
- first1
); return *this; }
3117 void swap(wxString
& str
)
3119 #if wxUSE_STRING_POS_CACHE
3120 // we modify not only this string but also the other one directly so we
3121 // need to invalidate cache for both of them (we could also try to
3122 // exchange their cache entries but it seems unlikely to be worth it)
3124 str
.InvalidateCache();
3125 #endif // wxUSE_STRING_POS_CACHE
3127 m_impl
.swap(str
.m_impl
);
3131 size_t find(const wxString
& str
, size_t nStart
= 0) const
3132 { return PosFromImpl(m_impl
.find(str
.m_impl
, PosToImpl(nStart
))); }
3134 // find first n characters of sz
3135 size_t find(const char* sz
, size_t nStart
= 0, size_t n
= npos
) const
3137 SubstrBufFromMB
str(ImplStr(sz
, n
));
3138 return PosFromImpl(m_impl
.find(str
.data
, PosToImpl(nStart
), str
.len
));
3140 size_t find(const wchar_t* sz
, size_t nStart
= 0, size_t n
= npos
) const
3142 SubstrBufFromWC
str(ImplStr(sz
, n
));
3143 return PosFromImpl(m_impl
.find(str
.data
, PosToImpl(nStart
), str
.len
));
3145 size_t find(const wxScopedCharBuffer
& s
, size_t nStart
= 0, size_t n
= npos
) const
3146 { return find(s
.data(), nStart
, n
); }
3147 size_t find(const wxScopedWCharBuffer
& s
, size_t nStart
= 0, size_t n
= npos
) const
3148 { return find(s
.data(), nStart
, n
); }
3149 size_t find(const wxCStrData
& s
, size_t nStart
= 0, size_t n
= npos
) const
3150 { return find(s
.AsWChar(), nStart
, n
); }
3152 // find the first occurrence of character ch after nStart
3153 size_t find(wxUniChar ch
, size_t nStart
= 0) const
3155 #if wxUSE_UNICODE_UTF8
3156 if ( !ch
.IsAscii() )
3157 return PosFromImpl(m_impl
.find(wxStringOperations::EncodeChar(ch
),
3158 PosToImpl(nStart
)));
3161 return PosFromImpl(m_impl
.find((wxStringCharType
)ch
,
3162 PosToImpl(nStart
)));
3165 size_t find(wxUniCharRef ch
, size_t nStart
= 0) const
3166 { return find(wxUniChar(ch
), nStart
); }
3167 size_t find(char ch
, size_t nStart
= 0) const
3168 { return find(wxUniChar(ch
), nStart
); }
3169 size_t find(unsigned char ch
, size_t nStart
= 0) const
3170 { return find(wxUniChar(ch
), nStart
); }
3171 size_t find(wchar_t ch
, size_t nStart
= 0) const
3172 { return find(wxUniChar(ch
), nStart
); }
3174 // rfind() family is exactly like find() but works right to left
3176 // as find, but from the end
3177 size_t rfind(const wxString
& str
, size_t nStart
= npos
) const
3178 { return PosFromImpl(m_impl
.rfind(str
.m_impl
, PosToImpl(nStart
))); }
3180 // as find, but from the end
3181 size_t rfind(const char* sz
, size_t nStart
= npos
, size_t n
= npos
) const
3183 SubstrBufFromMB
str(ImplStr(sz
, n
));
3184 return PosFromImpl(m_impl
.rfind(str
.data
, PosToImpl(nStart
), str
.len
));
3186 size_t rfind(const wchar_t* sz
, size_t nStart
= npos
, size_t n
= npos
) const
3188 SubstrBufFromWC
str(ImplStr(sz
, n
));
3189 return PosFromImpl(m_impl
.rfind(str
.data
, PosToImpl(nStart
), str
.len
));
3191 size_t rfind(const wxScopedCharBuffer
& s
, size_t nStart
= npos
, size_t n
= npos
) const
3192 { return rfind(s
.data(), nStart
, n
); }
3193 size_t rfind(const wxScopedWCharBuffer
& s
, size_t nStart
= npos
, size_t n
= npos
) const
3194 { return rfind(s
.data(), nStart
, n
); }
3195 size_t rfind(const wxCStrData
& s
, size_t nStart
= npos
, size_t n
= npos
) const
3196 { return rfind(s
.AsWChar(), nStart
, n
); }
3197 // as find, but from the end
3198 size_t rfind(wxUniChar ch
, size_t nStart
= npos
) const
3200 #if wxUSE_UNICODE_UTF8
3201 if ( !ch
.IsAscii() )
3202 return PosFromImpl(m_impl
.rfind(wxStringOperations::EncodeChar(ch
),
3203 PosToImpl(nStart
)));
3206 return PosFromImpl(m_impl
.rfind((wxStringCharType
)ch
,
3207 PosToImpl(nStart
)));
3209 size_t rfind(wxUniCharRef ch
, size_t nStart
= npos
) const
3210 { return rfind(wxUniChar(ch
), nStart
); }
3211 size_t rfind(char ch
, size_t nStart
= npos
) const
3212 { return rfind(wxUniChar(ch
), nStart
); }
3213 size_t rfind(unsigned char ch
, size_t nStart
= npos
) const
3214 { return rfind(wxUniChar(ch
), nStart
); }
3215 size_t rfind(wchar_t ch
, size_t nStart
= npos
) const
3216 { return rfind(wxUniChar(ch
), nStart
); }
3218 // find first/last occurrence of any character (not) in the set:
3219 #if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
3220 // FIXME-UTF8: this is not entirely correct, because it doesn't work if
3221 // sizeof(wchar_t)==2 and surrogates are present in the string;
3222 // should we care? Probably not.
3223 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
3224 { return m_impl
.find_first_of(str
.m_impl
, nStart
); }
3225 size_t find_first_of(const char* sz
, size_t nStart
= 0) const
3226 { return m_impl
.find_first_of(ImplStr(sz
), nStart
); }
3227 size_t find_first_of(const wchar_t* sz
, size_t nStart
= 0) const
3228 { return m_impl
.find_first_of(ImplStr(sz
), nStart
); }
3229 size_t find_first_of(const char* sz
, size_t nStart
, size_t n
) const
3230 { return m_impl
.find_first_of(ImplStr(sz
), nStart
, n
); }
3231 size_t find_first_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
3232 { return m_impl
.find_first_of(ImplStr(sz
), nStart
, n
); }
3233 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const
3234 { return m_impl
.find_first_of((wxChar
)c
, nStart
); }
3236 size_t find_last_of(const wxString
& str
, size_t nStart
= npos
) const
3237 { return m_impl
.find_last_of(str
.m_impl
, nStart
); }
3238 size_t find_last_of(const char* sz
, size_t nStart
= npos
) const
3239 { return m_impl
.find_last_of(ImplStr(sz
), nStart
); }
3240 size_t find_last_of(const wchar_t* sz
, size_t nStart
= npos
) const
3241 { return m_impl
.find_last_of(ImplStr(sz
), nStart
); }
3242 size_t find_last_of(const char* sz
, size_t nStart
, size_t n
) const
3243 { return m_impl
.find_last_of(ImplStr(sz
), nStart
, n
); }
3244 size_t find_last_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
3245 { return m_impl
.find_last_of(ImplStr(sz
), nStart
, n
); }
3246 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const
3247 { return m_impl
.find_last_of((wxChar
)c
, nStart
); }
3249 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
3250 { return m_impl
.find_first_not_of(str
.m_impl
, nStart
); }
3251 size_t find_first_not_of(const char* sz
, size_t nStart
= 0) const
3252 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
); }
3253 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
= 0) const
3254 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
); }
3255 size_t find_first_not_of(const char* sz
, size_t nStart
, size_t n
) const
3256 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
, n
); }
3257 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
3258 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
, n
); }
3259 size_t find_first_not_of(wxUniChar c
, size_t nStart
= 0) const
3260 { return m_impl
.find_first_not_of((wxChar
)c
, nStart
); }
3262 size_t find_last_not_of(const wxString
& str
, size_t nStart
= npos
) const
3263 { return m_impl
.find_last_not_of(str
.m_impl
, nStart
); }
3264 size_t find_last_not_of(const char* sz
, size_t nStart
= npos
) const
3265 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
); }
3266 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
= npos
) const
3267 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
); }
3268 size_t find_last_not_of(const char* sz
, size_t nStart
, size_t n
) const
3269 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
, n
); }
3270 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
3271 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
, n
); }
3272 size_t find_last_not_of(wxUniChar c
, size_t nStart
= npos
) const
3273 { return m_impl
.find_last_not_of((wxChar
)c
, nStart
); }
3275 // we can't use std::string implementation in UTF-8 build, because the
3276 // character sets would be interpreted wrongly:
3278 // as strpbrk() but starts at nStart, returns npos if not found
3279 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
3280 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3281 { return find_first_of(str
.wc_str(), nStart
); }
3283 { return find_first_of(str
.mb_str(), nStart
); }
3286 size_t find_first_of(const char* sz
, size_t nStart
= 0) const;
3287 size_t find_first_of(const wchar_t* sz
, size_t nStart
= 0) const;
3288 size_t find_first_of(const char* sz
, size_t nStart
, size_t n
) const;
3289 size_t find_first_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
3290 // same as find(char, size_t)
3291 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const
3292 { return find(c
, nStart
); }
3293 // find the last (starting from nStart) char from str in this string
3294 size_t find_last_of (const wxString
& str
, size_t nStart
= npos
) const
3295 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3296 { return find_last_of(str
.wc_str(), nStart
); }
3298 { return find_last_of(str
.mb_str(), nStart
); }
3301 size_t find_last_of (const char* sz
, size_t nStart
= npos
) const;
3302 size_t find_last_of (const wchar_t* sz
, size_t nStart
= npos
) const;
3303 size_t find_last_of(const char* sz
, size_t nStart
, size_t n
) const;
3304 size_t find_last_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
3306 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const
3307 { return rfind(c
, nStart
); }
3309 // find first/last occurrence of any character not in the set
3311 // as strspn() (starting from nStart), returns npos on failure
3312 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
3313 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3314 { return find_first_not_of(str
.wc_str(), nStart
); }
3316 { return find_first_not_of(str
.mb_str(), nStart
); }
3319 size_t find_first_not_of(const char* sz
, size_t nStart
= 0) const;
3320 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
= 0) const;
3321 size_t find_first_not_of(const char* sz
, size_t nStart
, size_t n
) const;
3322 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
3324 size_t find_first_not_of(wxUniChar ch
, size_t nStart
= 0) const;
3326 size_t find_last_not_of(const wxString
& str
, size_t nStart
= npos
) const
3327 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3328 { return find_last_not_of(str
.wc_str(), nStart
); }
3330 { return find_last_not_of(str
.mb_str(), nStart
); }
3333 size_t find_last_not_of(const char* sz
, size_t nStart
= npos
) const;
3334 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
= npos
) const;
3335 size_t find_last_not_of(const char* sz
, size_t nStart
, size_t n
) const;
3336 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
3338 size_t find_last_not_of(wxUniChar ch
, size_t nStart
= npos
) const;
3339 #endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not
3341 // provide char/wchar_t/wxUniCharRef overloads for char-finding functions
3342 // above to resolve ambiguities:
3343 size_t find_first_of(wxUniCharRef ch
, size_t nStart
= 0) const
3344 { return find_first_of(wxUniChar(ch
), nStart
); }
3345 size_t find_first_of(char ch
, size_t nStart
= 0) const
3346 { return find_first_of(wxUniChar(ch
), nStart
); }
3347 size_t find_first_of(unsigned char ch
, size_t nStart
= 0) const
3348 { return find_first_of(wxUniChar(ch
), nStart
); }
3349 size_t find_first_of(wchar_t ch
, size_t nStart
= 0) const
3350 { return find_first_of(wxUniChar(ch
), nStart
); }
3351 size_t find_last_of(wxUniCharRef ch
, size_t nStart
= npos
) const
3352 { return find_last_of(wxUniChar(ch
), nStart
); }
3353 size_t find_last_of(char ch
, size_t nStart
= npos
) const
3354 { return find_last_of(wxUniChar(ch
), nStart
); }
3355 size_t find_last_of(unsigned char ch
, size_t nStart
= npos
) const
3356 { return find_last_of(wxUniChar(ch
), nStart
); }
3357 size_t find_last_of(wchar_t ch
, size_t nStart
= npos
) const
3358 { return find_last_of(wxUniChar(ch
), nStart
); }
3359 size_t find_first_not_of(wxUniCharRef ch
, size_t nStart
= 0) const
3360 { return find_first_not_of(wxUniChar(ch
), nStart
); }
3361 size_t find_first_not_of(char ch
, size_t nStart
= 0) const
3362 { return find_first_not_of(wxUniChar(ch
), nStart
); }
3363 size_t find_first_not_of(unsigned char ch
, size_t nStart
= 0) const
3364 { return find_first_not_of(wxUniChar(ch
), nStart
); }
3365 size_t find_first_not_of(wchar_t ch
, size_t nStart
= 0) const
3366 { return find_first_not_of(wxUniChar(ch
), nStart
); }
3367 size_t find_last_not_of(wxUniCharRef ch
, size_t nStart
= npos
) const
3368 { return find_last_not_of(wxUniChar(ch
), nStart
); }
3369 size_t find_last_not_of(char ch
, size_t nStart
= npos
) const
3370 { return find_last_not_of(wxUniChar(ch
), nStart
); }
3371 size_t find_last_not_of(unsigned char ch
, size_t nStart
= npos
) const
3372 { return find_last_not_of(wxUniChar(ch
), nStart
); }
3373 size_t find_last_not_of(wchar_t ch
, size_t nStart
= npos
) const
3374 { return find_last_not_of(wxUniChar(ch
), nStart
); }
3376 // and additional overloads for the versions taking strings:
3377 size_t find_first_of(const wxCStrData
& sz
, size_t nStart
= 0) const
3378 { return find_first_of(sz
.AsString(), nStart
); }
3379 size_t find_first_of(const wxScopedCharBuffer
& sz
, size_t nStart
= 0) const
3380 { return find_first_of(sz
.data(), nStart
); }
3381 size_t find_first_of(const wxScopedWCharBuffer
& sz
, size_t nStart
= 0) const
3382 { return find_first_of(sz
.data(), nStart
); }
3383 size_t find_first_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
3384 { return find_first_of(sz
.AsWChar(), nStart
, n
); }
3385 size_t find_first_of(const wxScopedCharBuffer
& sz
, size_t nStart
, size_t n
) const
3386 { return find_first_of(sz
.data(), nStart
, n
); }
3387 size_t find_first_of(const wxScopedWCharBuffer
& sz
, size_t nStart
, size_t n
) const
3388 { return find_first_of(sz
.data(), nStart
, n
); }
3390 size_t find_last_of(const wxCStrData
& sz
, size_t nStart
= 0) const
3391 { return find_last_of(sz
.AsString(), nStart
); }
3392 size_t find_last_of(const wxScopedCharBuffer
& sz
, size_t nStart
= 0) const
3393 { return find_last_of(sz
.data(), nStart
); }
3394 size_t find_last_of(const wxScopedWCharBuffer
& sz
, size_t nStart
= 0) const
3395 { return find_last_of(sz
.data(), nStart
); }
3396 size_t find_last_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
3397 { return find_last_of(sz
.AsWChar(), nStart
, n
); }
3398 size_t find_last_of(const wxScopedCharBuffer
& sz
, size_t nStart
, size_t n
) const
3399 { return find_last_of(sz
.data(), nStart
, n
); }
3400 size_t find_last_of(const wxScopedWCharBuffer
& sz
, size_t nStart
, size_t n
) const
3401 { return find_last_of(sz
.data(), nStart
, n
); }
3403 size_t find_first_not_of(const wxCStrData
& sz
, size_t nStart
= 0) const
3404 { return find_first_not_of(sz
.AsString(), nStart
); }
3405 size_t find_first_not_of(const wxScopedCharBuffer
& sz
, size_t nStart
= 0) const
3406 { return find_first_not_of(sz
.data(), nStart
); }
3407 size_t find_first_not_of(const wxScopedWCharBuffer
& sz
, size_t nStart
= 0) const
3408 { return find_first_not_of(sz
.data(), nStart
); }
3409 size_t find_first_not_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
3410 { return find_first_not_of(sz
.AsWChar(), nStart
, n
); }
3411 size_t find_first_not_of(const wxScopedCharBuffer
& sz
, size_t nStart
, size_t n
) const
3412 { return find_first_not_of(sz
.data(), nStart
, n
); }
3413 size_t find_first_not_of(const wxScopedWCharBuffer
& sz
, size_t nStart
, size_t n
) const
3414 { return find_first_not_of(sz
.data(), nStart
, n
); }
3416 size_t find_last_not_of(const wxCStrData
& sz
, size_t nStart
= 0) const
3417 { return find_last_not_of(sz
.AsString(), nStart
); }
3418 size_t find_last_not_of(const wxScopedCharBuffer
& sz
, size_t nStart
= 0) const
3419 { return find_last_not_of(sz
.data(), nStart
); }
3420 size_t find_last_not_of(const wxScopedWCharBuffer
& sz
, size_t nStart
= 0) const
3421 { return find_last_not_of(sz
.data(), nStart
); }
3422 size_t find_last_not_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
3423 { return find_last_not_of(sz
.AsWChar(), nStart
, n
); }
3424 size_t find_last_not_of(const wxScopedCharBuffer
& sz
, size_t nStart
, size_t n
) const
3425 { return find_last_not_of(sz
.data(), nStart
, n
); }
3426 size_t find_last_not_of(const wxScopedWCharBuffer
& sz
, size_t nStart
, size_t n
) const
3427 { return find_last_not_of(sz
.data(), nStart
, n
); }
3430 wxString
& operator+=(const wxString
& s
)
3432 wxSTRING_INVALIDATE_CACHED_LENGTH();
3437 // string += C string
3438 wxString
& operator+=(const char *psz
)
3440 wxSTRING_INVALIDATE_CACHED_LENGTH();
3442 m_impl
+= ImplStr(psz
);
3445 wxString
& operator+=(const wchar_t *pwz
)
3447 wxSTRING_INVALIDATE_CACHED_LENGTH();
3449 m_impl
+= ImplStr(pwz
);
3452 wxString
& operator+=(const wxCStrData
& s
)
3454 wxSTRING_INVALIDATE_CACHED_LENGTH();
3456 m_impl
+= s
.AsString().m_impl
;
3459 wxString
& operator+=(const wxScopedCharBuffer
& s
)
3460 { return append(s
); }
3461 wxString
& operator+=(const wxScopedWCharBuffer
& s
)
3462 { return append(s
); }
3464 wxString
& operator+=(wxUniChar ch
)
3466 wxSTRING_UPDATE_CACHED_LENGTH(1);
3468 #if wxUSE_UNICODE_UTF8
3469 if ( !ch
.IsAscii() )
3470 m_impl
+= wxStringOperations::EncodeChar(ch
);
3473 m_impl
+= (wxStringCharType
)ch
;
3476 wxString
& operator+=(wxUniCharRef ch
) { return *this += wxUniChar(ch
); }
3477 wxString
& operator+=(int ch
) { return *this += wxUniChar(ch
); }
3478 wxString
& operator+=(char ch
) { return *this += wxUniChar(ch
); }
3479 wxString
& operator+=(unsigned char ch
) { return *this += wxUniChar(ch
); }
3480 wxString
& operator+=(wchar_t ch
) { return *this += wxUniChar(ch
); }
3483 #if !wxUSE_STL_BASED_WXSTRING
3484 // helpers for wxStringBuffer and wxStringBufferLength
3485 wxStringCharType
*DoGetWriteBuf(size_t nLen
)
3487 return m_impl
.DoGetWriteBuf(nLen
);
3490 void DoUngetWriteBuf()
3492 wxSTRING_INVALIDATE_CACHE();
3494 m_impl
.DoUngetWriteBuf();
3497 void DoUngetWriteBuf(size_t nLen
)
3499 wxSTRING_INVALIDATE_CACHE();
3501 m_impl
.DoUngetWriteBuf(nLen
);
3503 #endif // !wxUSE_STL_BASED_WXSTRING
3505 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
3506 #if !wxUSE_UTF8_LOCALE_ONLY
3507 int DoPrintfWchar(const wxChar
*format
, ...);
3508 static wxString
DoFormatWchar(const wxChar
*format
, ...);
3510 #if wxUSE_UNICODE_UTF8
3511 int DoPrintfUtf8(const char *format
, ...);
3512 static wxString
DoFormatUtf8(const char *format
, ...);
3516 #if !wxUSE_STL_BASED_WXSTRING
3517 // check string's data validity
3518 bool IsValid() const { return m_impl
.GetStringData()->IsValid(); }
3522 wxStringImpl m_impl
;
3524 // buffers for compatibility conversion from (char*)c_str() and
3525 // (wchar_t*)c_str(): the pointers returned by these functions should remain
3526 // valid until the string itself is modified for compatibility with the
3527 // existing code and consistency with std::string::c_str() so returning a
3528 // temporary buffer won't do and we need to cache the conversion results
3530 // TODO-UTF8: benchmark various approaches to keeping compatibility buffers
3531 template<typename T
>
3532 struct ConvertedBuffer
3534 // notice that there is no need to initialize m_len here as it's unused
3535 // as long as m_str is NULL
3536 ConvertedBuffer() : m_str(NULL
) {}
3540 bool Extend(size_t len
)
3542 // add extra 1 for the trailing NUL
3543 void * const str
= realloc(m_str
, sizeof(T
)*(len
+ 1));
3547 m_str
= static_cast<T
*>(str
);
3553 const wxScopedCharTypeBuffer
<T
> AsScopedBuffer() const
3555 return wxScopedCharTypeBuffer
<T
>::CreateNonOwned(m_str
, m_len
);
3558 T
*m_str
; // pointer to the string data
3559 size_t m_len
; // length, not size, i.e. in chars and without last NUL
3564 // common mb_str() and wxCStrData::AsChar() helper: performs the conversion
3565 // and returns either m_convertedToChar.m_str (in which case its m_len is
3566 // also updated) or NULL if it failed
3568 // there is an important exception: in wxUSE_UNICODE_UTF8 build if conv is a
3569 // UTF-8 one, we return m_impl.c_str() directly, without doing any conversion
3570 // as optimization and so the caller needs to check for this before using
3571 // m_convertedToChar
3573 // NB: AsChar() returns char* in any build, unlike mb_str()
3574 const char *AsChar(const wxMBConv
& conv
) const;
3576 // mb_str() implementation helper
3577 wxScopedCharBuffer
AsCharBuf(const wxMBConv
& conv
) const
3579 #if wxUSE_UNICODE_UTF8
3580 // avoid conversion if we can
3581 if ( conv
.IsUTF8() )
3583 return wxScopedCharBuffer::CreateNonOwned(m_impl
.c_str(),
3586 #endif // wxUSE_UNICODE_UTF8
3588 // call this solely in order to fill in m_convertedToChar as AsChar()
3589 // updates it as a side effect: this is a bit ugly but it's a completely
3590 // internal function so the users of this class shouldn't care or know
3591 // about it and doing it like this, i.e. having a separate AsChar(),
3592 // allows us to avoid the creation and destruction of a temporary buffer
3593 // when using wxCStrData without duplicating any code
3594 if ( !AsChar(conv
) )
3596 // although it would be probably more correct to return NULL buffer
3597 // from here if the conversion fails, a lot of existing code doesn't
3598 // expect mb_str() (or wc_str()) to ever return NULL so return an
3599 // empty string otherwise to avoid crashes in it
3601 // also, some existing code does check for the conversion success and
3602 // so asserting here would be bad too -- even if it does mean that
3603 // silently losing data is possible for badly written code
3604 return wxScopedCharBuffer::CreateNonOwned("", 0);
3607 return m_convertedToChar
.AsScopedBuffer();
3610 ConvertedBuffer
<char> m_convertedToChar
;
3611 #endif // !wxUSE_UNICODE
3613 #if !wxUSE_UNICODE_WCHAR
3614 // common wc_str() and wxCStrData::AsWChar() helper for both UTF-8 and ANSI
3615 // builds: converts the string contents into m_convertedToWChar and returns
3616 // NULL if the conversion failed (this can only happen in ANSI build)
3618 // NB: AsWChar() returns wchar_t* in any build, unlike wc_str()
3619 const wchar_t *AsWChar(const wxMBConv
& conv
) const;
3621 // wc_str() implementation helper
3622 wxScopedWCharBuffer
AsWCharBuf(const wxMBConv
& conv
) const
3624 if ( !AsWChar(conv
) )
3625 return wxScopedWCharBuffer::CreateNonOwned(L
"", 0);
3627 return m_convertedToWChar
.AsScopedBuffer();
3630 ConvertedBuffer
<wchar_t> m_convertedToWChar
;
3631 #endif // !wxUSE_UNICODE_WCHAR
3633 #if wxUSE_UNICODE_UTF8
3634 // FIXME-UTF8: (try to) move this elsewhere (TLS) or solve differently
3635 // assigning to character pointer to by wxString::iterator may
3636 // change the underlying wxStringImpl iterator, so we have to
3637 // keep track of all iterators and update them as necessary:
3638 struct wxStringIteratorNodeHead
3640 wxStringIteratorNodeHead() : ptr(NULL
) {}
3641 wxStringIteratorNode
*ptr
;
3643 // copying is disallowed as it would result in more than one pointer into
3644 // the same linked list
3645 wxDECLARE_NO_COPY_CLASS(wxStringIteratorNodeHead
);
3648 wxStringIteratorNodeHead m_iterators
;
3650 friend class WXDLLIMPEXP_FWD_BASE wxStringIteratorNode
;
3651 friend class WXDLLIMPEXP_FWD_BASE wxUniCharRef
;
3652 #endif // wxUSE_UNICODE_UTF8
3654 friend class WXDLLIMPEXP_FWD_BASE wxCStrData
;
3655 friend class wxStringInternalBuffer
;
3656 friend class wxStringInternalBufferLength
;
3659 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
3660 #pragma warning (pop)
3663 // string iterator operators that satisfy STL Random Access Iterator
3665 inline wxString::iterator
operator+(ptrdiff_t n
, wxString::iterator i
)
3667 inline wxString::const_iterator
operator+(ptrdiff_t n
, wxString::const_iterator i
)
3669 inline wxString::reverse_iterator
operator+(ptrdiff_t n
, wxString::reverse_iterator i
)
3671 inline wxString::const_reverse_iterator
operator+(ptrdiff_t n
, wxString::const_reverse_iterator i
)
3674 // notice that even though for many compilers the friend declarations above are
3675 // enough, from the point of view of C++ standard we must have the declarations
3676 // here as friend ones are not injected in the enclosing namespace and without
3677 // them the code fails to compile with conforming compilers such as xlC or g++4
3678 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
, const wxString
& string2
);
3679 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const char *psz
);
3680 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const wchar_t *pwz
);
3681 wxString WXDLLIMPEXP_BASE
operator+(const char *psz
, const wxString
& string
);
3682 wxString WXDLLIMPEXP_BASE
operator+(const wchar_t *pwz
, const wxString
& string
);
3684 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
3685 wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
3687 inline wxString
operator+(const wxString
& string
, wxUniCharRef ch
)
3688 { return string
+ (wxUniChar
)ch
; }
3689 inline wxString
operator+(const wxString
& string
, char ch
)
3690 { return string
+ wxUniChar(ch
); }
3691 inline wxString
operator+(const wxString
& string
, wchar_t ch
)
3692 { return string
+ wxUniChar(ch
); }
3693 inline wxString
operator+(wxUniCharRef ch
, const wxString
& string
)
3694 { return (wxUniChar
)ch
+ string
; }
3695 inline wxString
operator+(char ch
, const wxString
& string
)
3696 { return wxUniChar(ch
) + string
; }
3697 inline wxString
operator+(wchar_t ch
, const wxString
& string
)
3698 { return wxUniChar(ch
) + string
; }
3701 #define wxGetEmptyString() wxString()
3703 // ----------------------------------------------------------------------------
3704 // helper functions which couldn't be defined inline
3705 // ----------------------------------------------------------------------------
3710 #if wxUSE_UNICODE_WCHAR
3713 struct wxStringAsBufHelper
<char>
3715 static wxScopedCharBuffer
Get(const wxString
& s
, size_t *len
)
3717 wxScopedCharBuffer
buf(s
.mb_str());
3719 *len
= buf
? strlen(buf
) : 0;
3725 struct wxStringAsBufHelper
<wchar_t>
3727 static wxScopedWCharBuffer
Get(const wxString
& s
, size_t *len
)
3729 const size_t length
= s
.length();
3732 return wxScopedWCharBuffer::CreateNonOwned(s
.wx_str(), length
);
3736 #elif wxUSE_UNICODE_UTF8
3739 struct wxStringAsBufHelper
<char>
3741 static wxScopedCharBuffer
Get(const wxString
& s
, size_t *len
)
3743 const size_t length
= s
.utf8_length();
3746 return wxScopedCharBuffer::CreateNonOwned(s
.wx_str(), length
);
3751 struct wxStringAsBufHelper
<wchar_t>
3753 static wxScopedWCharBuffer
Get(const wxString
& s
, size_t *len
)
3755 wxScopedWCharBuffer
wbuf(s
.wc_str());
3757 *len
= wxWcslen(wbuf
);
3762 #endif // Unicode build kind
3764 } // namespace wxPrivate
3766 // ----------------------------------------------------------------------------
3767 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
3768 // ----------------------------------------------------------------------------
3770 #if !wxUSE_STL_BASED_WXSTRING
3771 // string buffer for direct access to string data in their native
3773 class wxStringInternalBuffer
3776 typedef wxStringCharType CharType
;
3778 wxStringInternalBuffer(wxString
& str
, size_t lenWanted
= 1024)
3779 : m_str(str
), m_buf(NULL
)
3780 { m_buf
= m_str
.DoGetWriteBuf(lenWanted
); }
3782 ~wxStringInternalBuffer() { m_str
.DoUngetWriteBuf(); }
3784 operator wxStringCharType
*() const { return m_buf
; }
3788 wxStringCharType
*m_buf
;
3790 wxDECLARE_NO_COPY_CLASS(wxStringInternalBuffer
);
3793 class wxStringInternalBufferLength
3796 typedef wxStringCharType CharType
;
3798 wxStringInternalBufferLength(wxString
& str
, size_t lenWanted
= 1024)
3799 : m_str(str
), m_buf(NULL
), m_len(0), m_lenSet(false)
3801 m_buf
= m_str
.DoGetWriteBuf(lenWanted
);
3802 wxASSERT(m_buf
!= NULL
);
3805 ~wxStringInternalBufferLength()
3808 m_str
.DoUngetWriteBuf(m_len
);
3811 operator wxStringCharType
*() const { return m_buf
; }
3812 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
3816 wxStringCharType
*m_buf
;
3820 wxDECLARE_NO_COPY_CLASS(wxStringInternalBufferLength
);
3823 #endif // !wxUSE_STL_BASED_WXSTRING
3825 template<typename T
>
3826 class wxStringTypeBufferBase
3831 wxStringTypeBufferBase(wxString
& str
, size_t lenWanted
= 1024)
3832 : m_str(str
), m_buf(lenWanted
)
3834 // for compatibility with old wxStringBuffer which provided direct
3835 // access to wxString internal buffer, initialize ourselves with the
3836 // string initial contents
3838 // FIXME-VC6: remove the ugly (CharType *)NULL and use normal
3839 // tchar_str<CharType>
3841 const wxCharTypeBuffer
<CharType
> buf(str
.tchar_str(&len
, (CharType
*)NULL
));
3844 if ( len
> lenWanted
)
3846 // in this case there is not enough space for terminating NUL,
3847 // ensure that we still put it there
3848 m_buf
.data()[lenWanted
] = 0;
3849 len
= lenWanted
- 1;
3852 memcpy(m_buf
.data(), buf
, (len
+ 1)*sizeof(CharType
));
3854 //else: conversion failed, this can happen when trying to get Unicode
3855 // string contents into a char string
3858 operator CharType
*() { return m_buf
.data(); }
3862 wxCharTypeBuffer
<CharType
> m_buf
;
3865 template<typename T
>
3866 class wxStringTypeBufferLengthBase
: public wxStringTypeBufferBase
<T
>
3869 wxStringTypeBufferLengthBase(wxString
& str
, size_t lenWanted
= 1024)
3870 : wxStringTypeBufferBase
<T
>(str
, lenWanted
),
3875 ~wxStringTypeBufferLengthBase()
3877 wxASSERT_MSG( this->m_lenSet
, "forgot to call SetLength()" );
3880 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
3887 template<typename T
>
3888 class wxStringTypeBuffer
: public wxStringTypeBufferBase
<T
>
3891 wxStringTypeBuffer(wxString
& str
, size_t lenWanted
= 1024)
3892 : wxStringTypeBufferBase
<T
>(str
, lenWanted
)
3895 ~wxStringTypeBuffer()
3897 this->m_str
.assign(this->m_buf
.data());
3900 wxDECLARE_NO_COPY_CLASS(wxStringTypeBuffer
);
3903 template<typename T
>
3904 class wxStringTypeBufferLength
: public wxStringTypeBufferLengthBase
<T
>
3907 wxStringTypeBufferLength(wxString
& str
, size_t lenWanted
= 1024)
3908 : wxStringTypeBufferLengthBase
<T
>(str
, lenWanted
)
3911 ~wxStringTypeBufferLength()
3913 this->m_str
.assign(this->m_buf
.data(), this->m_len
);
3916 wxDECLARE_NO_COPY_CLASS(wxStringTypeBufferLength
);
3919 #if wxUSE_STL_BASED_WXSTRING
3921 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase
<wxStringCharType
> )
3923 class wxStringInternalBuffer
: public wxStringTypeBufferBase
<wxStringCharType
>
3926 wxStringInternalBuffer(wxString
& str
, size_t lenWanted
= 1024)
3927 : wxStringTypeBufferBase
<wxStringCharType
>(str
, lenWanted
) {}
3928 ~wxStringInternalBuffer()
3929 { m_str
.m_impl
.assign(m_buf
.data()); }
3931 wxDECLARE_NO_COPY_CLASS(wxStringInternalBuffer
);
3934 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE(
3935 wxStringTypeBufferLengthBase
<wxStringCharType
> )
3937 class wxStringInternalBufferLength
3938 : public wxStringTypeBufferLengthBase
<wxStringCharType
>
3941 wxStringInternalBufferLength(wxString
& str
, size_t lenWanted
= 1024)
3942 : wxStringTypeBufferLengthBase
<wxStringCharType
>(str
, lenWanted
) {}
3944 ~wxStringInternalBufferLength()
3946 m_str
.m_impl
.assign(m_buf
.data(), m_len
);
3949 wxDECLARE_NO_COPY_CLASS(wxStringInternalBufferLength
);
3952 #endif // wxUSE_STL_BASED_WXSTRING
3955 #if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
3956 typedef wxStringTypeBuffer
<wxChar
> wxStringBuffer
;
3957 typedef wxStringTypeBufferLength
<wxChar
> wxStringBufferLength
;
3958 #else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
3959 typedef wxStringInternalBuffer wxStringBuffer
;
3960 typedef wxStringInternalBufferLength wxStringBufferLength
;
3961 #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
3963 #if wxUSE_UNICODE_UTF8
3964 typedef wxStringInternalBuffer wxUTF8StringBuffer
;
3965 typedef wxStringInternalBufferLength wxUTF8StringBufferLength
;
3966 #elif wxUSE_UNICODE_WCHAR
3968 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase
<char> )
3970 // Note about inlined dtors in the classes below: this is done not for
3971 // performance reasons but just to avoid linking errors in the MSVC DLL build
3972 // under Windows: if a class has non-inline methods it must be declared as
3973 // being DLL-exported but, due to an extremely interesting feature of MSVC 7
3974 // and later, any template class which is used as a base of a DLL-exported
3975 // class is implicitly made DLL-exported too, as explained at the bottom of
3976 // http://msdn.microsoft.com/en-us/library/twa2aw10.aspx (just to confirm: yes,
3977 // _inheriting_ from a class can change whether it is being exported from DLL)
3979 // But this results in link errors because the base template class is not DLL-
3980 // exported, whether it is declared with WXDLLIMPEXP_BASE or not, because it
3981 // does have only inline functions. So the simplest fix is to just make all the
3982 // functions of these classes inline too.
3984 class wxUTF8StringBuffer
: public wxStringTypeBufferBase
<char>
3987 wxUTF8StringBuffer(wxString
& str
, size_t lenWanted
= 1024)
3988 : wxStringTypeBufferBase
<char>(str
, lenWanted
) {}
3989 ~wxUTF8StringBuffer()
3991 wxMBConvStrictUTF8 conv
;
3992 size_t wlen
= conv
.ToWChar(NULL
, 0, m_buf
);
3993 wxCHECK_RET( wlen
!= wxCONV_FAILED
, "invalid UTF-8 data in string buffer?" );
3995 wxStringInternalBuffer
wbuf(m_str
, wlen
);
3996 conv
.ToWChar(wbuf
, wlen
, m_buf
);
3999 wxDECLARE_NO_COPY_CLASS(wxUTF8StringBuffer
);
4002 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferLengthBase
<char> )
4004 class wxUTF8StringBufferLength
: public wxStringTypeBufferLengthBase
<char>
4007 wxUTF8StringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
4008 : wxStringTypeBufferLengthBase
<char>(str
, lenWanted
) {}
4009 ~wxUTF8StringBufferLength()
4011 wxCHECK_RET(m_lenSet
, "length not set");
4013 wxMBConvStrictUTF8 conv
;
4014 size_t wlen
= conv
.ToWChar(NULL
, 0, m_buf
, m_len
);
4015 wxCHECK_RET( wlen
!= wxCONV_FAILED
, "invalid UTF-8 data in string buffer?" );
4017 wxStringInternalBufferLength
wbuf(m_str
, wlen
);
4018 conv
.ToWChar(wbuf
, wlen
, m_buf
, m_len
);
4019 wbuf
.SetLength(wlen
);
4022 wxDECLARE_NO_COPY_CLASS(wxUTF8StringBufferLength
);
4024 #endif // wxUSE_UNICODE_UTF8/wxUSE_UNICODE_WCHAR
4027 // ---------------------------------------------------------------------------
4028 // wxString comparison functions: operator versions are always case sensitive
4029 // ---------------------------------------------------------------------------
4031 #define wxCMP_WXCHAR_STRING(p, s, op) 0 op s.Cmp(p)
4033 wxDEFINE_ALL_COMPARISONS(const wxChar
*, const wxString
&, wxCMP_WXCHAR_STRING
)
4035 #undef wxCMP_WXCHAR_STRING
4037 inline bool operator==(const wxString
& s1
, const wxString
& s2
)
4038 { return s1
.IsSameAs(s2
); }
4039 inline bool operator!=(const wxString
& s1
, const wxString
& s2
)
4040 { return !s1
.IsSameAs(s2
); }
4041 inline bool operator< (const wxString
& s1
, const wxString
& s2
)
4042 { return s1
.Cmp(s2
) < 0; }
4043 inline bool operator> (const wxString
& s1
, const wxString
& s2
)
4044 { return s1
.Cmp(s2
) > 0; }
4045 inline bool operator<=(const wxString
& s1
, const wxString
& s2
)
4046 { return s1
.Cmp(s2
) <= 0; }
4047 inline bool operator>=(const wxString
& s1
, const wxString
& s2
)
4048 { return s1
.Cmp(s2
) >= 0; }
4050 inline bool operator==(const wxString
& s1
, const wxCStrData
& s2
)
4051 { return s1
== s2
.AsString(); }
4052 inline bool operator==(const wxCStrData
& s1
, const wxString
& s2
)
4053 { return s1
.AsString() == s2
; }
4054 inline bool operator!=(const wxString
& s1
, const wxCStrData
& s2
)
4055 { return s1
!= s2
.AsString(); }
4056 inline bool operator!=(const wxCStrData
& s1
, const wxString
& s2
)
4057 { return s1
.AsString() != s2
; }
4059 inline bool operator==(const wxString
& s1
, const wxScopedWCharBuffer
& s2
)
4060 { return (s1
.Cmp((const wchar_t *)s2
) == 0); }
4061 inline bool operator==(const wxScopedWCharBuffer
& s1
, const wxString
& s2
)
4062 { return (s2
.Cmp((const wchar_t *)s1
) == 0); }
4063 inline bool operator!=(const wxString
& s1
, const wxScopedWCharBuffer
& s2
)
4064 { return (s1
.Cmp((const wchar_t *)s2
) != 0); }
4065 inline bool operator!=(const wxScopedWCharBuffer
& s1
, const wxString
& s2
)
4066 { return (s2
.Cmp((const wchar_t *)s1
) != 0); }
4068 inline bool operator==(const wxString
& s1
, const wxScopedCharBuffer
& s2
)
4069 { return (s1
.Cmp((const char *)s2
) == 0); }
4070 inline bool operator==(const wxScopedCharBuffer
& s1
, const wxString
& s2
)
4071 { return (s2
.Cmp((const char *)s1
) == 0); }
4072 inline bool operator!=(const wxString
& s1
, const wxScopedCharBuffer
& s2
)
4073 { return (s1
.Cmp((const char *)s2
) != 0); }
4074 inline bool operator!=(const wxScopedCharBuffer
& s1
, const wxString
& s2
)
4075 { return (s2
.Cmp((const char *)s1
) != 0); }
4077 inline wxString
operator+(const wxString
& string
, const wxScopedWCharBuffer
& buf
)
4078 { return string
+ (const wchar_t *)buf
; }
4079 inline wxString
operator+(const wxScopedWCharBuffer
& buf
, const wxString
& string
)
4080 { return (const wchar_t *)buf
+ string
; }
4082 inline wxString
operator+(const wxString
& string
, const wxScopedCharBuffer
& buf
)
4083 { return string
+ (const char *)buf
; }
4084 inline wxString
operator+(const wxScopedCharBuffer
& buf
, const wxString
& string
)
4085 { return (const char *)buf
+ string
; }
4087 // comparison with char
4088 inline bool operator==(const wxUniChar
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
4089 inline bool operator==(const wxUniCharRef
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
4090 inline bool operator==(char c
, const wxString
& s
) { return s
.IsSameAs(c
); }
4091 inline bool operator==(wchar_t c
, const wxString
& s
) { return s
.IsSameAs(c
); }
4092 inline bool operator==(int c
, const wxString
& s
) { return s
.IsSameAs(c
); }
4093 inline bool operator==(const wxString
& s
, const wxUniChar
& c
) { return s
.IsSameAs(c
); }
4094 inline bool operator==(const wxString
& s
, const wxUniCharRef
& c
) { return s
.IsSameAs(c
); }
4095 inline bool operator==(const wxString
& s
, char c
) { return s
.IsSameAs(c
); }
4096 inline bool operator==(const wxString
& s
, wchar_t c
) { return s
.IsSameAs(c
); }
4097 inline bool operator!=(const wxUniChar
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
4098 inline bool operator!=(const wxUniCharRef
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
4099 inline bool operator!=(char c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
4100 inline bool operator!=(wchar_t c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
4101 inline bool operator!=(int c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
4102 inline bool operator!=(const wxString
& s
, const wxUniChar
& c
) { return !s
.IsSameAs(c
); }
4103 inline bool operator!=(const wxString
& s
, const wxUniCharRef
& c
) { return !s
.IsSameAs(c
); }
4104 inline bool operator!=(const wxString
& s
, char c
) { return !s
.IsSameAs(c
); }
4105 inline bool operator!=(const wxString
& s
, wchar_t c
) { return !s
.IsSameAs(c
); }
4108 // wxString iterators comparisons
4109 inline bool wxString::iterator::operator==(const const_iterator
& i
) const
4110 { return i
== *this; }
4111 inline bool wxString::iterator::operator!=(const const_iterator
& i
) const
4112 { return i
!= *this; }
4113 inline bool wxString::iterator::operator<(const const_iterator
& i
) const
4114 { return i
> *this; }
4115 inline bool wxString::iterator::operator>(const const_iterator
& i
) const
4116 { return i
< *this; }
4117 inline bool wxString::iterator::operator<=(const const_iterator
& i
) const
4118 { return i
>= *this; }
4119 inline bool wxString::iterator::operator>=(const const_iterator
& i
) const
4120 { return i
<= *this; }
4122 // comparison with C string in Unicode build
4125 #define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s
4127 wxDEFINE_ALL_COMPARISONS(const char *, const wxString
&, wxCMP_CHAR_STRING
)
4129 #undef wxCMP_CHAR_STRING
4131 #endif // wxUSE_UNICODE
4133 // we also need to provide the operators for comparison with wxCStrData to
4134 // resolve ambiguity between operator(const wxChar *,const wxString &) and
4135 // operator(const wxChar *, const wxChar *) for "p == s.c_str()"
4137 // notice that these are (shallow) pointer comparisons, not (deep) string ones
4138 #define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar()
4139 #define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar()
4141 wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData
&, wxCMP_WCHAR_CSTRDATA
)
4142 wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData
&, wxCMP_CHAR_CSTRDATA
)
4144 #undef wxCMP_CHAR_CSTRDATA
4145 #undef wxCMP_WCHAR_CSTRDATA
4147 // ---------------------------------------------------------------------------
4148 // Implementation only from here until the end of file
4149 // ---------------------------------------------------------------------------
4151 #if wxUSE_STD_IOSTREAM
4153 #include "wx/iosfwrap.h"
4155 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxString
&);
4156 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxCStrData
&);
4157 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxScopedCharBuffer
&);
4158 #ifndef __BORLANDC__
4159 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxScopedWCharBuffer
&);
4162 #if wxUSE_UNICODE && defined(HAVE_WOSTREAM)
4164 WXDLLIMPEXP_BASE wxSTD wostream
& operator<<(wxSTD wostream
&, const wxString
&);
4165 WXDLLIMPEXP_BASE wxSTD wostream
& operator<<(wxSTD wostream
&, const wxCStrData
&);
4166 WXDLLIMPEXP_BASE wxSTD wostream
& operator<<(wxSTD wostream
&, const wxScopedWCharBuffer
&);
4168 #endif // wxUSE_UNICODE && defined(HAVE_WOSTREAM)
4170 #endif // wxUSE_STD_IOSTREAM
4172 // ---------------------------------------------------------------------------
4173 // wxCStrData implementation
4174 // ---------------------------------------------------------------------------
4176 inline wxCStrData::wxCStrData(char *buf
)
4177 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
4178 inline wxCStrData::wxCStrData(wchar_t *buf
)
4179 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
4181 inline wxCStrData::wxCStrData(const wxCStrData
& data
)
4182 : m_str(data
.m_owned
? new wxString(*data
.m_str
) : data
.m_str
),
4183 m_offset(data
.m_offset
),
4184 m_owned(data
.m_owned
)
4188 inline wxCStrData::~wxCStrData()
4191 delete const_cast<wxString
*>(m_str
); // cast to silence warnings
4194 // AsChar() and AsWChar() implementations simply forward to wxString methods
4196 inline const wchar_t* wxCStrData::AsWChar() const
4198 const wchar_t * const p
=
4199 #if wxUSE_UNICODE_WCHAR
4201 #elif wxUSE_UNICODE_UTF8
4202 m_str
->AsWChar(wxMBConvStrictUTF8());
4204 m_str
->AsWChar(wxConvLibc
);
4207 // in Unicode build the string always has a valid Unicode representation
4208 // and even if a conversion is needed (as in UTF8 case) it can't fail
4210 // but in ANSI build the string contents might be not convertible to
4211 // Unicode using the current locale encoding so we do need to check for
4216 // if conversion fails, return empty string and not NULL to avoid
4217 // crashes in code written with either wxWidgets 2 wxString or
4218 // std::string behaviour in mind: neither of them ever returns NULL
4219 // from its c_str() and so we shouldn't neither
4221 // notice that the same is done in AsChar() below and
4222 // wxString::wc_str() and mb_str() for the same reasons
4225 #endif // !wxUSE_UNICODE
4227 return p
+ m_offset
;
4230 inline const char* wxCStrData::AsChar() const
4232 #if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
4233 const char * const p
= m_str
->AsChar(wxConvLibc
);
4236 #else // !wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY
4237 const char * const p
= m_str
->mb_str();
4238 #endif // wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
4240 return p
+ m_offset
;
4243 inline wxString
wxCStrData::AsString() const
4245 if ( m_offset
== 0 )
4248 return m_str
->Mid(m_offset
);
4251 inline const wxStringCharType
*wxCStrData::AsInternal() const
4253 #if wxUSE_UNICODE_UTF8
4254 return wxStringOperations::AddToIter(m_str
->wx_str(), m_offset
);
4256 return m_str
->wx_str() + m_offset
;
4260 inline wxUniChar
wxCStrData::operator*() const
4262 if ( m_str
->empty() )
4263 return wxUniChar(wxT('\0'));
4265 return (*m_str
)[m_offset
];
4268 inline wxUniChar
wxCStrData::operator[](size_t n
) const
4270 // NB: we intentionally use operator[] and not at() here because the former
4271 // works for the terminating NUL while the latter does not
4272 return (*m_str
)[m_offset
+ n
];
4275 // ----------------------------------------------------------------------------
4276 // more wxCStrData operators
4277 // ----------------------------------------------------------------------------
4279 // we need to define those to allow "size_t pos = p - s.c_str()" where p is
4280 // some pointer into the string
4281 inline size_t operator-(const char *p
, const wxCStrData
& cs
)
4283 return p
- cs
.AsChar();
4286 inline size_t operator-(const wchar_t *p
, const wxCStrData
& cs
)
4288 return p
- cs
.AsWChar();
4291 // ----------------------------------------------------------------------------
4292 // implementation of wx[W]CharBuffer inline methods using wxCStrData
4293 // ----------------------------------------------------------------------------
4295 // FIXME-UTF8: move this to buffer.h
4296 inline wxCharBuffer::wxCharBuffer(const wxCStrData
& cstr
)
4297 : wxCharTypeBufferBase(cstr
.AsCharBuf())
4301 inline wxWCharBuffer::wxWCharBuffer(const wxCStrData
& cstr
)
4302 : wxCharTypeBufferBase(cstr
.AsWCharBuf())
4306 #if wxUSE_UNICODE_UTF8
4307 // ----------------------------------------------------------------------------
4308 // implementation of wxStringIteratorNode inline methods
4309 // ----------------------------------------------------------------------------
4311 void wxStringIteratorNode::DoSet(const wxString
*str
,
4312 wxStringImpl::const_iterator
*citer
,
4313 wxStringImpl::iterator
*iter
)
4321 m_next
= str
->m_iterators
.ptr
;
4322 const_cast<wxString
*>(m_str
)->m_iterators
.ptr
= this;
4324 m_next
->m_prev
= this;
4332 void wxStringIteratorNode::clear()
4335 m_next
->m_prev
= m_prev
;
4337 m_prev
->m_next
= m_next
;
4338 else if ( m_str
) // first in the list
4339 const_cast<wxString
*>(m_str
)->m_iterators
.ptr
= m_next
;
4341 m_next
= m_prev
= NULL
;
4346 #endif // wxUSE_UNICODE_UTF8
4348 #if WXWIN_COMPATIBILITY_2_8
4349 // lot of code out there doesn't explicitly include wx/crt.h, but uses
4350 // CRT wrappers that are now declared in wx/wxcrt.h and wx/wxcrtvararg.h,
4351 // so let's include this header now that wxString is defined and it's safe
4356 // ----------------------------------------------------------------------------
4357 // Checks on wxString characters
4358 // ----------------------------------------------------------------------------
4360 template<bool (T
)(const wxUniChar
& c
)>
4361 inline bool wxStringCheck(const wxString
& val
)
4363 for ( wxString::const_iterator i
= val
.begin();
4371 #endif // _WX_WXSTRING_H_