avoiding crashes on osx during app shutdown
[wxWidgets.git] / include / wx / string.h
1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/string.h
3 // Purpose: wxString class
4 // Author: Vadim Zeitlin
5 // Modified by:
6 // Created: 29/01/98
7 // RCS-ID: $Id$
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
11
12 /*
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.
16 */
17
18 #ifndef _WX_WXSTRING_H__
19 #define _WX_WXSTRING_H__
20
21 // ----------------------------------------------------------------------------
22 // headers
23 // ----------------------------------------------------------------------------
24
25 #include "wx/defs.h" // everybody should include this
26
27 #ifndef __WXPALMOS5__
28 #if defined(__WXMAC__) || defined(__VISAGECPP__)
29 #include <ctype.h>
30 #endif
31
32 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
33 // problem in VACPP V4 with including stdlib.h multiple times
34 // strconv includes it anyway
35 # include <stdio.h>
36 # include <string.h>
37 # include <stdarg.h>
38 # include <limits.h>
39 #else
40 # include <string.h>
41 # include <stdio.h>
42 # include <stdarg.h>
43 # include <limits.h>
44 # include <stdlib.h>
45 #endif
46
47 #ifdef HAVE_STRCASECMP_IN_STRINGS_H
48 #include <strings.h> // for strcasecmp()
49 #endif // HAVE_STRCASECMP_IN_STRINGS_H
50 #endif // ! __WXPALMOS5__
51
52 #include "wx/wxcrtbase.h" // for wxChar, wxStrlen() etc.
53 #include "wx/strvararg.h"
54 #include "wx/buffer.h" // for wxCharBuffer
55 #include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes
56 #include "wx/stringimpl.h"
57 #include "wx/stringops.h"
58 #include "wx/unichar.h"
59
60 // by default we cache the mapping of the positions in UTF-8 string to the byte
61 // offset as this results in noticeable performance improvements for loops over
62 // strings using indices; comment out this line to disable this
63 //
64 // notice that this optimization is well worth using even in debug builds as it
65 // changes asymptotic complexity of algorithms using indices to iterate over
66 // wxString back to expected linear from quadratic
67 //
68 // also notice that wxTLS_TYPE() (__declspec(thread) in this case) is unsafe to
69 // use in DLL build under pre-Vista Windows so we disable this code for now, if
70 // anybody really needs to use UTF-8 build under Windows with this optimization
71 // it would have to be re-tested and probably corrected
72 #if wxUSE_UNICODE_UTF8 && !defined(__WXMSW__)
73 #define wxUSE_STRING_POS_CACHE 1
74 #else
75 #define wxUSE_STRING_POS_CACHE 0
76 #endif
77
78 #if wxUSE_STRING_POS_CACHE
79 #include "wx/tls.h"
80
81 // change this 0 to 1 to enable additional (very expensive) asserts
82 // verifying that string caching logic works as expected
83 #if 0
84 #define wxSTRING_CACHE_ASSERT(cond) wxASSERT(cond)
85 #else
86 #define wxSTRING_CACHE_ASSERT(cond)
87 #endif
88 #endif // wxUSE_STRING_POS_CACHE
89
90 class WXDLLIMPEXP_FWD_BASE wxString;
91
92 // unless this symbol is predefined to disable the compatibility functions, do
93 // use them
94 #ifndef WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
95 #define WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER 1
96 #endif
97
98 namespace wxPrivate
99 {
100 template <typename T> struct wxStringAsBufHelper;
101 }
102
103 // ---------------------------------------------------------------------------
104 // macros
105 // ---------------------------------------------------------------------------
106
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 *)
113
114 // ----------------------------------------------------------------------------
115 // constants
116 // ----------------------------------------------------------------------------
117
118 #if WXWIN_COMPATIBILITY_2_6
119
120 // deprecated in favour of wxString::npos, don't use in new code
121 //
122 // maximum possible length for a string means "take all string" everywhere
123 #define wxSTRING_MAXLEN wxString::npos
124
125 #endif // WXWIN_COMPATIBILITY_2_6
126
127 // ---------------------------------------------------------------------------
128 // global functions complementing standard C string library replacements for
129 // strlen() and portable strcasecmp()
130 //---------------------------------------------------------------------------
131
132 #if WXWIN_COMPATIBILITY_2_8
133 // Use wxXXX() functions from wxcrt.h instead! These functions are for
134 // backwards compatibility only.
135
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); }
139
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; }
144
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)
148 {
149 #if defined(__VISUALC__) && defined(__WXWINCE__)
150 register char c1, c2;
151 do {
152 c1 = tolower(*psz1++);
153 c2 = tolower(*psz2++);
154 } while ( c1 && (c1 == c2) );
155
156 return c1 - c2;
157 #elif defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) )
158 return _stricmp(psz1, psz2);
159 #elif defined(__SC__)
160 return _stricmp(psz1, psz2);
161 #elif defined(__BORLANDC__)
162 return stricmp(psz1, psz2);
163 #elif defined(__WATCOMC__)
164 return stricmp(psz1, psz2);
165 #elif defined(__DJGPP__)
166 return stricmp(psz1, psz2);
167 #elif defined(__EMX__)
168 return stricmp(psz1, psz2);
169 #elif defined(__WXPM__)
170 return stricmp(psz1, psz2);
171 #elif defined(__WXPALMOS__) || \
172 defined(HAVE_STRCASECMP_IN_STRING_H) || \
173 defined(HAVE_STRCASECMP_IN_STRINGS_H) || \
174 defined(__GNUWIN32__)
175 return strcasecmp(psz1, psz2);
176 #elif defined(__MWERKS__) && !defined(__INTEL__)
177 register char c1, c2;
178 do {
179 c1 = tolower(*psz1++);
180 c2 = tolower(*psz2++);
181 } while ( c1 && (c1 == c2) );
182
183 return c1 - c2;
184 #else
185 // almost all compilers/libraries provide this function (unfortunately under
186 // different names), that's why we don't implement our own which will surely
187 // be more efficient than this code (uncomment to use):
188 /*
189 register char c1, c2;
190 do {
191 c1 = tolower(*psz1++);
192 c2 = tolower(*psz2++);
193 } while ( c1 && (c1 == c2) );
194
195 return c1 - c2;
196 */
197
198 #error "Please define string case-insensitive compare for your OS/compiler"
199 #endif // OS/compiler
200 }
201
202 #endif // WXWIN_COMPATIBILITY_2_8
203
204 // ----------------------------------------------------------------------------
205 // wxCStrData
206 // ----------------------------------------------------------------------------
207
208 // Lightweight object returned by wxString::c_str() and implicitly convertible
209 // to either const char* or const wchar_t*.
210 class WXDLLIMPEXP_BASE wxCStrData
211 {
212 private:
213 // Ctors; for internal use by wxString and wxCStrData only
214 wxCStrData(const wxString *str, size_t offset = 0, bool owned = false)
215 : m_str(str), m_offset(offset), m_owned(owned) {}
216
217 public:
218 // Ctor constructs the object from char literal; they are needed to make
219 // operator?: compile and they intentionally take char*, not const char*
220 inline wxCStrData(char *buf);
221 inline wxCStrData(wchar_t *buf);
222 inline wxCStrData(const wxCStrData& data);
223
224 inline ~wxCStrData();
225
226 // methods defined inline below must be declared inline or mingw32 3.4.5
227 // warns about "<symbol> defined locally after being referenced with
228 // dllimport linkage"
229 #if wxUSE_UNICODE_WCHAR
230 inline
231 #endif
232 const wchar_t* AsWChar() const;
233 operator const wchar_t*() const { return AsWChar(); }
234
235 #if !wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY
236 inline
237 #endif
238 const char* AsChar() const;
239 const unsigned char* AsUnsignedChar() const
240 { return (const unsigned char *) AsChar(); }
241 operator const char*() const { return AsChar(); }
242 operator const unsigned char*() const { return AsUnsignedChar(); }
243
244 operator const void*() const { return AsChar(); }
245
246 inline const wxCharBuffer AsCharBuf() const;
247 inline const wxWCharBuffer AsWCharBuf() const;
248
249 inline wxString AsString() const;
250
251 // returns the value as C string in internal representation (equivalent
252 // to AsString().wx_str(), but more efficient)
253 const wxStringCharType *AsInternal() const;
254
255 // allow expressions like "c_str()[0]":
256 inline wxUniChar operator[](size_t n) const;
257 wxUniChar operator[](int n) const { return operator[](size_t(n)); }
258 wxUniChar operator[](long n) const { return operator[](size_t(n)); }
259 #ifndef wxSIZE_T_IS_UINT
260 wxUniChar operator[](unsigned int n) const { return operator[](size_t(n)); }
261 #endif // size_t != unsigned int
262
263 // these operators are needed to emulate the pointer semantics of c_str():
264 // expressions like "wxChar *p = str.c_str() + 1;" should continue to work
265 // (we need both versions to resolve ambiguities):
266 wxCStrData operator+(int n) const
267 { return wxCStrData(m_str, m_offset + n, m_owned); }
268 wxCStrData operator+(long n) const
269 { return wxCStrData(m_str, m_offset + n, m_owned); }
270 wxCStrData operator+(size_t n) const
271 { return wxCStrData(m_str, m_offset + n, m_owned); }
272
273 // and these for "str.c_str() + (p2 - p1)" (it also works for any integer
274 // expression but it must be ptrdiff_t and not e.g. int to work in this
275 // example):
276 wxCStrData operator-(ptrdiff_t n) const
277 {
278 wxASSERT_MSG( n <= (ptrdiff_t)m_offset,
279 _T("attempt to construct address before the beginning of the string") );
280 return wxCStrData(m_str, m_offset - n, m_owned);
281 }
282
283 // this operator is needed to make expressions like "*c_str()" or
284 // "*(c_str() + 2)" work
285 inline wxUniChar operator*() const;
286
287 private:
288 const wxString *m_str;
289 size_t m_offset;
290 bool m_owned;
291
292 friend class WXDLLIMPEXP_FWD_BASE wxString;
293 };
294
295 // ----------------------------------------------------------------------------
296 // wxStringPrintfMixin
297 // ---------------------------------------------------------------------------
298
299 // NB: VC6 has a bug that causes linker errors if you have template methods
300 // in a class using __declspec(dllimport). The solution is to split such
301 // class into two classes, one that contains the template methods and does
302 // *not* use WXDLLIMPEXP_BASE and another class that contains the rest
303 // (with DLL linkage).
304 //
305 // We only do this for VC6 here, because the code is less efficient
306 // (Printf() has to use dynamic_cast<>) and because OpenWatcom compiler
307 // cannot compile this code.
308
309 #if defined(__VISUALC__) && __VISUALC__ < 1300
310 #define wxNEEDS_WXSTRING_PRINTF_MIXIN
311 #endif
312
313 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
314 // this class contains implementation of wxString's vararg methods, it's
315 // exported from wxBase DLL
316 class WXDLLIMPEXP_BASE wxStringPrintfMixinBase
317 {
318 protected:
319 wxStringPrintfMixinBase() {}
320
321 #if !wxUSE_UTF8_LOCALE_ONLY
322 int DoPrintfWchar(const wxChar *format, ...);
323 static wxString DoFormatWchar(const wxChar *format, ...);
324 #endif
325 #if wxUSE_UNICODE_UTF8
326 int DoPrintfUtf8(const char *format, ...);
327 static wxString DoFormatUtf8(const char *format, ...);
328 #endif
329 };
330
331 // this class contains template wrappers for wxString's vararg methods, it's
332 // intentionally *not* exported from the DLL in order to fix the VC6 bug
333 // described above
334 class wxStringPrintfMixin : public wxStringPrintfMixinBase
335 {
336 private:
337 // to further complicate things, we can't return wxString from
338 // wxStringPrintfMixin::Format() because wxString is not yet declared at
339 // this point; the solution is to use this fake type trait template - this
340 // way the compiler won't know the return type until Format() is used
341 // (this doesn't compile with Watcom, but VC6 compiles it just fine):
342 template<typename T> struct StringReturnType
343 {
344 typedef wxString type;
345 };
346
347 public:
348 // these are duplicated wxString methods, they're also declared below
349 // if !wxNEEDS_WXSTRING_PRINTF_MIXIN:
350
351 // static wxString Format(const wString& format, ...) ATTRIBUTE_PRINTF_1;
352 WX_DEFINE_VARARG_FUNC_SANS_N0(static typename StringReturnType<T1>::type,
353 Format, 1, (const wxFormatString&),
354 DoFormatWchar, DoFormatUtf8)
355 // We have to implement the version without template arguments manually
356 // because of the StringReturnType<> hack, although WX_DEFINE_VARARG_FUNC
357 // normally does it itself. It has to be a template so that we can use
358 // the hack, even though there's no real template parameter. We can't move
359 // it to wxStrig, because it would shadow these versions of Format() then.
360 template<typename T>
361 inline static typename StringReturnType<T>::type
362 Format(const T& fmt)
363 {
364 // NB: this doesn't compile if T is not (some form of) a string;
365 // this makes Format's prototype equivalent to
366 // Format(const wxFormatString& fmt)
367 return DoFormatWchar(wxFormatString(fmt));
368 }
369
370 // int Printf(const wxString& format, ...);
371 WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const wxFormatString&),
372 DoPrintfWchar, DoPrintfUtf8)
373 // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2;
374 WX_DEFINE_VARARG_FUNC(int, sprintf, 1, (const wxFormatString&),
375 DoPrintfWchar, DoPrintfUtf8)
376
377 protected:
378 wxStringPrintfMixin() : wxStringPrintfMixinBase() {}
379 };
380 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
381
382
383 // ----------------------------------------------------------------------------
384 // wxString: string class trying to be compatible with std::string, MFC
385 // CString and wxWindows 1.x wxString all at once
386 // ---------------------------------------------------------------------------
387
388 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
389 // "non dll-interface class 'wxStringPrintfMixin' used as base interface
390 // for dll-interface class 'wxString'" -- this is OK in our case
391 #pragma warning (disable:4275)
392 #endif
393
394 #if wxUSE_UNICODE_UTF8
395 // see the comment near wxString::iterator for why we need this
396 class WXDLLIMPEXP_BASE wxStringIteratorNode
397 {
398 public:
399 wxStringIteratorNode()
400 : m_str(NULL), m_citer(NULL), m_iter(NULL), m_prev(NULL), m_next(NULL) {}
401 wxStringIteratorNode(const wxString *str,
402 wxStringImpl::const_iterator *citer)
403 { DoSet(str, citer, NULL); }
404 wxStringIteratorNode(const wxString *str, wxStringImpl::iterator *iter)
405 { DoSet(str, NULL, iter); }
406 ~wxStringIteratorNode()
407 { clear(); }
408
409 inline void set(const wxString *str, wxStringImpl::const_iterator *citer)
410 { clear(); DoSet(str, citer, NULL); }
411 inline void set(const wxString *str, wxStringImpl::iterator *iter)
412 { clear(); DoSet(str, NULL, iter); }
413
414 const wxString *m_str;
415 wxStringImpl::const_iterator *m_citer;
416 wxStringImpl::iterator *m_iter;
417 wxStringIteratorNode *m_prev, *m_next;
418
419 private:
420 inline void clear();
421 inline void DoSet(const wxString *str,
422 wxStringImpl::const_iterator *citer,
423 wxStringImpl::iterator *iter);
424
425 // the node belongs to a particular iterator instance, it's not copied
426 // when a copy of the iterator is made
427 DECLARE_NO_COPY_CLASS(wxStringIteratorNode)
428 };
429 #endif // wxUSE_UNICODE_UTF8
430
431 class WXDLLIMPEXP_BASE wxString
432 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
433 : public wxStringPrintfMixin
434 #endif
435 {
436 // NB: special care was taken in arranging the member functions in such order
437 // that all inline functions can be effectively inlined, verify that all
438 // performance critical functions are still inlined if you change order!
439 public:
440 // an 'invalid' value for string index, moved to this place due to a CW bug
441 static const size_t npos;
442
443 private:
444 // if we hadn't made these operators private, it would be possible to
445 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
446 // converted to char in C and we do have operator=(char)
447 //
448 // NB: we don't need other versions (short/long and unsigned) as attempt
449 // to assign another numeric type to wxString will now result in
450 // ambiguity between operator=(char) and operator=(int)
451 wxString& operator=(int);
452
453 // these methods are not implemented - there is _no_ conversion from int to
454 // string, you're doing something wrong if the compiler wants to call it!
455 //
456 // try `s << i' or `s.Printf("%d", i)' instead
457 wxString(int);
458
459
460 // buffer for holding temporary substring when using any of the methods
461 // that take (char*,size_t) or (wchar_t*,size_t) arguments:
462 template<typename T>
463 struct SubstrBufFromType
464 {
465 T data;
466 size_t len;
467
468 SubstrBufFromType(const T& data_, size_t len_)
469 : data(data_), len(len_)
470 {
471 wxASSERT_MSG( len != npos, "must have real length" );
472 }
473 };
474
475 #if wxUSE_UNICODE_UTF8
476 // even char* -> char* needs conversion, from locale charset to UTF-8
477 typedef SubstrBufFromType<wxCharBuffer> SubstrBufFromWC;
478 typedef SubstrBufFromType<wxCharBuffer> SubstrBufFromMB;
479 #elif wxUSE_UNICODE_WCHAR
480 typedef SubstrBufFromType<const wchar_t*> SubstrBufFromWC;
481 typedef SubstrBufFromType<wxWCharBuffer> SubstrBufFromMB;
482 #else
483 typedef SubstrBufFromType<const char*> SubstrBufFromMB;
484 typedef SubstrBufFromType<wxCharBuffer> SubstrBufFromWC;
485 #endif
486
487
488 // Functions implementing primitive operations on string data; wxString
489 // methods and iterators are implemented in terms of it. The differences
490 // between UTF-8 and wchar_t* representations of the string are mostly
491 // contained here.
492
493 #if wxUSE_UNICODE_UTF8
494 static SubstrBufFromMB ConvertStr(const char *psz, size_t nLength,
495 const wxMBConv& conv);
496 static SubstrBufFromWC ConvertStr(const wchar_t *pwz, size_t nLength,
497 const wxMBConv& conv);
498 #elif wxUSE_UNICODE_WCHAR
499 static SubstrBufFromMB ConvertStr(const char *psz, size_t nLength,
500 const wxMBConv& conv);
501 #else
502 static SubstrBufFromWC ConvertStr(const wchar_t *pwz, size_t nLength,
503 const wxMBConv& conv);
504 #endif
505
506 #if !wxUSE_UNICODE_UTF8 // wxUSE_UNICODE_WCHAR or !wxUSE_UNICODE
507 // returns C string encoded as the implementation expects:
508 #if wxUSE_UNICODE
509 static const wchar_t* ImplStr(const wchar_t* str)
510 { return str ? str : wxT(""); }
511 static const SubstrBufFromWC ImplStr(const wchar_t* str, size_t n)
512 { return SubstrBufFromWC(str, (str && n == npos) ? wxWcslen(str) : n); }
513 static wxWCharBuffer ImplStr(const char* str,
514 const wxMBConv& conv = wxConvLibc)
515 { return ConvertStr(str, npos, conv).data; }
516 static SubstrBufFromMB ImplStr(const char* str, size_t n,
517 const wxMBConv& conv = wxConvLibc)
518 { return ConvertStr(str, n, conv); }
519 #else
520 static const char* ImplStr(const char* str,
521 const wxMBConv& WXUNUSED(conv) = wxConvLibc)
522 { return str ? str : ""; }
523 static const SubstrBufFromMB ImplStr(const char* str, size_t n,
524 const wxMBConv& WXUNUSED(conv) = wxConvLibc)
525 { return SubstrBufFromMB(str, (str && n == npos) ? wxStrlen(str) : n); }
526 static wxCharBuffer ImplStr(const wchar_t* str)
527 { return ConvertStr(str, npos, wxConvLibc).data; }
528 static SubstrBufFromWC ImplStr(const wchar_t* str, size_t n)
529 { return ConvertStr(str, n, wxConvLibc); }
530 #endif
531
532 // translates position index in wxString to/from index in underlying
533 // wxStringImpl:
534 static size_t PosToImpl(size_t pos) { return pos; }
535 static void PosLenToImpl(size_t pos, size_t len,
536 size_t *implPos, size_t *implLen)
537 { *implPos = pos; *implLen = len; }
538 static size_t LenToImpl(size_t len) { return len; }
539 static size_t PosFromImpl(size_t pos) { return pos; }
540
541 // we don't want to define these as empty inline functions as it could
542 // result in noticeable (and quite unnecessary in non-UTF-8 build) slowdown
543 // in debug build where the inline functions are not effectively inlined
544 #define wxSTRING_INVALIDATE_CACHE()
545 #define wxSTRING_INVALIDATE_CACHED_LENGTH()
546 #define wxSTRING_UPDATE_CACHED_LENGTH(n)
547 #define wxSTRING_SET_CACHED_LENGTH(n)
548
549 #else // wxUSE_UNICODE_UTF8
550
551 static wxCharBuffer ImplStr(const char* str,
552 const wxMBConv& conv = wxConvLibc)
553 { return ConvertStr(str, npos, conv).data; }
554 static SubstrBufFromMB ImplStr(const char* str, size_t n,
555 const wxMBConv& conv = wxConvLibc)
556 { return ConvertStr(str, n, conv); }
557
558 static wxCharBuffer ImplStr(const wchar_t* str)
559 { return ConvertStr(str, npos, wxMBConvUTF8()).data; }
560 static SubstrBufFromWC ImplStr(const wchar_t* str, size_t n)
561 { return ConvertStr(str, n, wxMBConvUTF8()); }
562
563 #if wxUSE_STRING_POS_CACHE
564 // this is an extremely simple cache used by PosToImpl(): each cache element
565 // contains the string it applies to and the index corresponding to the last
566 // used position in this wxString in its m_impl string
567 //
568 // NB: notice that this struct (and nested Element one) must be a POD or we
569 // wouldn't be able to use a thread-local variable of this type, in
570 // particular it should have no ctor -- we rely on statics being
571 // initialized to 0 instead
572 struct Cache
573 {
574 enum { SIZE = 8 };
575
576 struct Element
577 {
578 const wxString *str; // the string to which this element applies
579 size_t pos, // the cached index in this string
580 impl, // the corresponding position in its m_impl
581 len; // cached length or npos if unknown
582
583 // reset cached index to 0
584 void ResetPos() { pos = impl = 0; }
585
586 // reset position and length
587 void Reset() { ResetPos(); len = npos; }
588 };
589
590 // cache the indices mapping for the last few string used
591 Element cached[SIZE];
592
593 // the last used index
594 unsigned lastUsed;
595 };
596
597 #ifndef wxHAS_COMPILER_TLS
598 // we must use an accessor function and not a static variable when the TLS
599 // variables support is implemented in the library (and not by the compiler)
600 // because the global s_cache variable could be not yet initialized when a
601 // ctor of another global object is executed and if that ctor uses any
602 // wxString methods, bad things happen
603 //
604 // however notice that this approach does not work when compiler TLS is used,
605 // at least not with g++ 4.1.2 under amd64 as it apparently compiles code
606 // using this accessor incorrectly when optimizations are enabled (-O2 is
607 // enough) -- luckily we don't need it then neither as static __thread
608 // variables are initialized by 0 anyhow then and so we can use the variable
609 // directly
610 static Cache& GetCache()
611 {
612 static wxTLS_TYPE(Cache) s_cache;
613
614 return wxTLS_VALUE(s_cache);
615 }
616
617 // this helper struct is used to ensure that GetCache() is called during
618 // static initialization time, i.e. before any threads creation, as otherwise
619 // the static s_cache construction inside GetCache() wouldn't be MT-safe
620 friend struct wxStrCacheInitializer;
621 #else // wxHAS_COMPILER_TLS
622 static wxTLS_TYPE(Cache) ms_cache;
623 static Cache& GetCache() { return wxTLS_VALUE(ms_cache); }
624 #endif // !wxHAS_COMPILER_TLS/wxHAS_COMPILER_TLS
625
626 static Cache::Element *GetCacheBegin() { return GetCache().cached; }
627 static Cache::Element *GetCacheEnd() { return GetCacheBegin() + Cache::SIZE; }
628 static unsigned& LastUsedCacheElement() { return GetCache().lastUsed; }
629
630 // this is used in debug builds only to provide a convenient function,
631 // callable from a debugger, to show the cache contents
632 friend struct wxStrCacheDumper;
633
634 // uncomment this to have access to some profiling statistics on program
635 // termination
636 //#define wxPROFILE_STRING_CACHE
637
638 #ifdef wxPROFILE_STRING_CACHE
639 static struct PosToImplCacheStats
640 {
641 unsigned postot, // total non-trivial calls to PosToImpl
642 poshits, // cache hits from PosToImpl()
643 mishits, // cached position beyond the needed one
644 sumpos, // sum of all positions, used to compute the
645 // average position after dividing by postot
646 sumofs, // sum of all offsets after using the cache, used to
647 // compute the average after dividing by hits
648 lentot, // number of total calls to length()
649 lenhits; // number of cache hits in length()
650 } ms_cacheStats;
651
652 friend struct wxStrCacheStatsDumper;
653
654 #define wxCACHE_PROFILE_FIELD_INC(field) ms_cacheStats.field++
655 #define wxCACHE_PROFILE_FIELD_ADD(field, val) ms_cacheStats.field += (val)
656 #else // !wxPROFILE_STRING_CACHE
657 #define wxCACHE_PROFILE_FIELD_INC(field)
658 #define wxCACHE_PROFILE_FIELD_ADD(field, val)
659 #endif // wxPROFILE_STRING_CACHE/!wxPROFILE_STRING_CACHE
660
661 // note: it could seem that the functions below shouldn't be inline because
662 // they are big, contain loops and so the compiler shouldn't be able to
663 // inline them anyhow, however moving them into string.cpp does decrease the
664 // code performance by ~5%, at least when using g++ 4.1 so do keep them here
665 // unless tests show that it's not advantageous any more
666
667 // return the pointer to the cache element for this string or NULL if not
668 // cached
669 Cache::Element *FindCacheElement() const
670 {
671 // profiling seems to show a small but consistent gain if we use this
672 // simple loop instead of starting from the last used element (there are
673 // a lot of misses in this function...)
674 // during destruction tls calls may return NULL, thus return NULL
675 // immediately
676 if ( GetCacheBegin() == NULL )
677 return NULL;
678 for ( Cache::Element *c = GetCacheBegin(); c != GetCacheEnd(); c++ )
679 {
680 if ( c->str == this )
681 return c;
682 }
683
684 return NULL;
685 }
686
687 // unlike FindCacheElement(), this one always returns a valid pointer to the
688 // cache element for this string, it may have valid last cached position and
689 // its corresponding index in the byte string or not
690 Cache::Element *GetCacheElement() const
691 {
692 Cache::Element * const cacheBegin = GetCacheBegin();
693 Cache::Element * const cacheEnd = GetCacheEnd();
694 Cache::Element * const cacheStart = cacheBegin + LastUsedCacheElement();
695
696 // check the last used first, this does no (measurable) harm for a miss
697 // but does help for simple loops addressing the same string all the time
698 if ( cacheStart->str == this )
699 return cacheStart;
700
701 // notice that we're going to check cacheStart again inside this call but
702 // profiling shows that it's still faster to use a simple loop like
703 // inside FindCacheElement() than manually looping with wrapping starting
704 // from the cache entry after the start one
705 Cache::Element *c = FindCacheElement();
706 if ( !c )
707 {
708 // claim the next cache entry for this string
709 c = cacheStart;
710 if ( ++c == cacheEnd )
711 c = cacheBegin;
712
713 c->str = this;
714 c->Reset();
715
716 // and remember the last used element
717 LastUsedCacheElement() = c - cacheBegin;
718 }
719
720 return c;
721 }
722
723 size_t DoPosToImpl(size_t pos) const
724 {
725 wxCACHE_PROFILE_FIELD_INC(postot);
726
727 // NB: although the case of pos == 1 (and offset from cached position
728 // equal to 1) are common, nothing is gained by writing special code
729 // for handling them, the compiler (at least g++ 4.1 used) seems to
730 // optimize the code well enough on its own
731
732 wxCACHE_PROFILE_FIELD_ADD(sumpos, pos);
733
734 Cache::Element * const cache = GetCacheElement();
735
736 // cached position can't be 0 so if it is, it means that this entry was
737 // used for length caching only so far, i.e. it doesn't count as a hit
738 // from our point of view
739 if ( cache->pos )
740 {
741 wxCACHE_PROFILE_FIELD_INC(poshits);
742 }
743
744 if ( pos == cache->pos )
745 return cache->impl;
746
747 // this seems to happen only rarely so just reset the cache in this case
748 // instead of complicating code even further by seeking backwards in this
749 // case
750 if ( cache->pos > pos )
751 {
752 wxCACHE_PROFILE_FIELD_INC(mishits);
753
754 cache->ResetPos();
755 }
756
757 wxCACHE_PROFILE_FIELD_ADD(sumofs, pos - cache->pos);
758
759
760 wxStringImpl::const_iterator i(m_impl.begin() + cache->impl);
761 for ( size_t n = cache->pos; n < pos; n++ )
762 wxStringOperations::IncIter(i);
763
764 cache->pos = pos;
765 cache->impl = i - m_impl.begin();
766
767 wxSTRING_CACHE_ASSERT(
768 (int)cache->impl == (begin() + pos).impl() - m_impl.begin() );
769
770 return cache->impl;
771 }
772
773 void InvalidateCache()
774 {
775 Cache::Element * const cache = FindCacheElement();
776 if ( cache )
777 cache->Reset();
778 }
779
780 void InvalidateCachedLength()
781 {
782 Cache::Element * const cache = FindCacheElement();
783 if ( cache )
784 cache->len = npos;
785 }
786
787 void SetCachedLength(size_t len)
788 {
789 // we optimistically cache the length here even if the string wasn't
790 // present in the cache before, this seems to do no harm and the
791 // potential for avoiding length recomputation for long strings looks
792 // interesting
793 GetCacheElement()->len = len;
794 }
795
796 void UpdateCachedLength(ptrdiff_t delta)
797 {
798 Cache::Element * const cache = FindCacheElement();
799 if ( cache && cache->len != npos )
800 {
801 wxSTRING_CACHE_ASSERT( (ptrdiff_t)cache->len + delta >= 0 );
802
803 cache->len += delta;
804 }
805 }
806
807 #define wxSTRING_INVALIDATE_CACHE() InvalidateCache()
808 #define wxSTRING_INVALIDATE_CACHED_LENGTH() InvalidateCachedLength()
809 #define wxSTRING_UPDATE_CACHED_LENGTH(n) UpdateCachedLength(n)
810 #define wxSTRING_SET_CACHED_LENGTH(n) SetCachedLength(n)
811 #else // !wxUSE_STRING_POS_CACHE
812 size_t DoPosToImpl(size_t pos) const
813 {
814 return (begin() + pos).impl() - m_impl.begin();
815 }
816
817 #define wxSTRING_INVALIDATE_CACHE()
818 #define wxSTRING_INVALIDATE_CACHED_LENGTH()
819 #define wxSTRING_UPDATE_CACHED_LENGTH(n)
820 #define wxSTRING_SET_CACHED_LENGTH(n)
821 #endif // wxUSE_STRING_POS_CACHE/!wxUSE_STRING_POS_CACHE
822
823 size_t PosToImpl(size_t pos) const
824 {
825 return pos == 0 || pos == npos ? pos : DoPosToImpl(pos);
826 }
827
828 void PosLenToImpl(size_t pos, size_t len, size_t *implPos, size_t *implLen) const;
829
830 size_t LenToImpl(size_t len) const
831 {
832 size_t pos, len2;
833 PosLenToImpl(0, len, &pos, &len2);
834 return len2;
835 }
836
837 size_t PosFromImpl(size_t pos) const
838 {
839 if ( pos == 0 || pos == npos )
840 return pos;
841 else
842 return const_iterator(this, m_impl.begin() + pos) - begin();
843 }
844 #endif // !wxUSE_UNICODE_UTF8/wxUSE_UNICODE_UTF8
845
846 public:
847 // standard types
848 typedef wxUniChar value_type;
849 typedef wxUniChar char_type;
850 typedef wxUniCharRef reference;
851 typedef wxChar* pointer;
852 typedef const wxChar* const_pointer;
853
854 typedef size_t size_type;
855 typedef wxUniChar const_reference;
856
857 #if wxUSE_STD_STRING
858 #if wxUSE_UNICODE_UTF8
859 // random access is not O(1), as required by Random Access Iterator
860 #define WX_STR_ITERATOR_TAG std::bidirectional_iterator_tag
861 #else
862 #define WX_STR_ITERATOR_TAG std::random_access_iterator_tag
863 #endif
864 #define WX_DEFINE_ITERATOR_CATEGORY(cat) typedef cat iterator_category;
865 #else
866 // not defining iterator_category at all in this case is better than defining
867 // it as some dummy type -- at least it results in more intelligible error
868 // messages
869 #define WX_DEFINE_ITERATOR_CATEGORY(cat)
870 #endif
871
872 #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, reference_type) \
873 private: \
874 typedef wxStringImpl::iterator_name underlying_iterator; \
875 public: \
876 WX_DEFINE_ITERATOR_CATEGORY(WX_STR_ITERATOR_TAG) \
877 typedef wxUniChar value_type; \
878 typedef int difference_type; \
879 typedef reference_type reference; \
880 typedef pointer_type pointer; \
881 \
882 reference operator[](size_t n) const { return *(*this + n); } \
883 \
884 iterator_name& operator++() \
885 { wxStringOperations::IncIter(m_cur); return *this; } \
886 iterator_name& operator--() \
887 { wxStringOperations::DecIter(m_cur); return *this; } \
888 iterator_name operator++(int) \
889 { \
890 iterator_name tmp = *this; \
891 wxStringOperations::IncIter(m_cur); \
892 return tmp; \
893 } \
894 iterator_name operator--(int) \
895 { \
896 iterator_name tmp = *this; \
897 wxStringOperations::DecIter(m_cur); \
898 return tmp; \
899 } \
900 \
901 iterator_name& operator+=(ptrdiff_t n) \
902 { \
903 m_cur = wxStringOperations::AddToIter(m_cur, n); \
904 return *this; \
905 } \
906 iterator_name& operator-=(ptrdiff_t n) \
907 { \
908 m_cur = wxStringOperations::AddToIter(m_cur, -n); \
909 return *this; \
910 } \
911 \
912 difference_type operator-(const iterator_name& i) const \
913 { return wxStringOperations::DiffIters(m_cur, i.m_cur); } \
914 \
915 bool operator==(const iterator_name& i) const \
916 { return m_cur == i.m_cur; } \
917 bool operator!=(const iterator_name& i) const \
918 { return m_cur != i.m_cur; } \
919 \
920 bool operator<(const iterator_name& i) const \
921 { return m_cur < i.m_cur; } \
922 bool operator>(const iterator_name& i) const \
923 { return m_cur > i.m_cur; } \
924 bool operator<=(const iterator_name& i) const \
925 { return m_cur <= i.m_cur; } \
926 bool operator>=(const iterator_name& i) const \
927 { return m_cur >= i.m_cur; } \
928 \
929 private: \
930 /* for internal wxString use only: */ \
931 underlying_iterator impl() const { return m_cur; } \
932 \
933 friend class wxString; \
934 friend class wxCStrData; \
935 \
936 private: \
937 underlying_iterator m_cur
938
939 class WXDLLIMPEXP_FWD_BASE const_iterator;
940
941 #if wxUSE_UNICODE_UTF8
942 // NB: In UTF-8 build, (non-const) iterator needs to keep reference
943 // to the underlying wxStringImpl, because UTF-8 is variable-length
944 // encoding and changing the value pointer to by an iterator (using
945 // its operator*) requires calling wxStringImpl::replace() if the old
946 // and new values differ in their encoding's length.
947 //
948 // Furthermore, the replace() call may invalid all iterators for the
949 // string, so we have to keep track of outstanding iterators and update
950 // them if replace() happens.
951 //
952 // This is implemented by maintaining linked list of iterators for every
953 // string and traversing it in wxUniCharRef::operator=(). Head of the
954 // list is stored in wxString. (FIXME-UTF8)
955
956 class WXDLLIMPEXP_BASE iterator
957 {
958 WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef);
959
960 public:
961 iterator() {}
962 iterator(const iterator& i)
963 : m_cur(i.m_cur), m_node(i.str(), &m_cur) {}
964 iterator& operator=(const iterator& i)
965 {
966 if (&i != this)
967 {
968 m_cur = i.m_cur;
969 m_node.set(i.str(), &m_cur);
970 }
971 return *this;
972 }
973
974 reference operator*()
975 { return wxUniCharRef::CreateForString(*str(), m_cur); }
976
977 iterator operator+(ptrdiff_t n) const
978 { return iterator(str(), wxStringOperations::AddToIter(m_cur, n)); }
979 iterator operator-(ptrdiff_t n) const
980 { return iterator(str(), wxStringOperations::AddToIter(m_cur, -n)); }
981
982 private:
983 iterator(wxString *str, underlying_iterator ptr)
984 : m_cur(ptr), m_node(str, &m_cur) {}
985
986 wxString* str() const { return wx_const_cast(wxString*, m_node.m_str); }
987
988 wxStringIteratorNode m_node;
989
990 friend class const_iterator;
991 };
992
993 class WXDLLIMPEXP_BASE const_iterator
994 {
995 // NB: reference_type is intentionally value, not reference, the character
996 // may be encoded differently in wxString data:
997 WX_STR_ITERATOR_IMPL(const_iterator, const wxChar*, wxUniChar);
998
999 public:
1000 const_iterator() {}
1001 const_iterator(const const_iterator& i)
1002 : m_cur(i.m_cur), m_node(i.str(), &m_cur) {}
1003 const_iterator(const iterator& i)
1004 : m_cur(i.m_cur), m_node(i.str(), &m_cur) {}
1005
1006 const_iterator& operator=(const const_iterator& i)
1007 {
1008 if (&i != this)
1009 {
1010 m_cur = i.m_cur;
1011 m_node.set(i.str(), &m_cur);
1012 }
1013 return *this;
1014 }
1015 const_iterator& operator=(const iterator& i)
1016 { m_cur = i.m_cur; m_node.set(i.str(), &m_cur); return *this; }
1017
1018 reference operator*() const
1019 { return wxStringOperations::DecodeChar(m_cur); }
1020
1021 const_iterator operator+(ptrdiff_t n) const
1022 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur, n)); }
1023 const_iterator operator-(ptrdiff_t n) const
1024 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur, -n)); }
1025
1026 private:
1027 // for internal wxString use only:
1028 const_iterator(const wxString *str, underlying_iterator ptr)
1029 : m_cur(ptr), m_node(str, &m_cur) {}
1030
1031 const wxString* str() const { return m_node.m_str; }
1032
1033 wxStringIteratorNode m_node;
1034 };
1035
1036 size_t IterToImplPos(wxString::iterator i) const
1037 { return wxStringImpl::const_iterator(i.impl()) - m_impl.begin(); }
1038
1039 iterator GetIterForNthChar(size_t n)
1040 { return iterator(this, m_impl.begin() + PosToImpl(n)); }
1041 const_iterator GetIterForNthChar(size_t n) const
1042 { return const_iterator(this, m_impl.begin() + PosToImpl(n)); }
1043 #else // !wxUSE_UNICODE_UTF8
1044
1045 class WXDLLIMPEXP_BASE iterator
1046 {
1047 WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef);
1048
1049 public:
1050 iterator() {}
1051 iterator(const iterator& i) : m_cur(i.m_cur) {}
1052
1053 reference operator*()
1054 { return wxUniCharRef::CreateForString(m_cur); }
1055
1056 iterator operator+(ptrdiff_t n) const
1057 { return iterator(wxStringOperations::AddToIter(m_cur, n)); }
1058 iterator operator-(ptrdiff_t n) const
1059 { return iterator(wxStringOperations::AddToIter(m_cur, -n)); }
1060
1061 private:
1062 // for internal wxString use only:
1063 iterator(underlying_iterator ptr) : m_cur(ptr) {}
1064 iterator(wxString *WXUNUSED(str), underlying_iterator ptr) : m_cur(ptr) {}
1065
1066 friend class const_iterator;
1067 };
1068
1069 class WXDLLIMPEXP_BASE const_iterator
1070 {
1071 // NB: reference_type is intentionally value, not reference, the character
1072 // may be encoded differently in wxString data:
1073 WX_STR_ITERATOR_IMPL(const_iterator, const wxChar*, wxUniChar);
1074
1075 public:
1076 const_iterator() {}
1077 const_iterator(const const_iterator& i) : m_cur(i.m_cur) {}
1078 const_iterator(const iterator& i) : m_cur(i.m_cur) {}
1079
1080 reference operator*() const
1081 { return wxStringOperations::DecodeChar(m_cur); }
1082
1083 const_iterator operator+(ptrdiff_t n) const
1084 { return const_iterator(wxStringOperations::AddToIter(m_cur, n)); }
1085 const_iterator operator-(ptrdiff_t n) const
1086 { return const_iterator(wxStringOperations::AddToIter(m_cur, -n)); }
1087
1088 private:
1089 // for internal wxString use only:
1090 const_iterator(underlying_iterator ptr) : m_cur(ptr) {}
1091 const_iterator(const wxString *WXUNUSED(str), underlying_iterator ptr)
1092 : m_cur(ptr) {}
1093 };
1094
1095 iterator GetIterForNthChar(size_t n) { return begin() + n; }
1096 const_iterator GetIterForNthChar(size_t n) const { return begin() + n; }
1097 #endif // wxUSE_UNICODE_UTF8/!wxUSE_UNICODE_UTF8
1098
1099 #undef WX_STR_ITERATOR_TAG
1100 #undef WX_STR_ITERATOR_IMPL
1101
1102 friend class iterator;
1103 friend class const_iterator;
1104
1105 template <typename T>
1106 class reverse_iterator_impl
1107 {
1108 public:
1109 typedef T iterator_type;
1110
1111 WX_DEFINE_ITERATOR_CATEGORY(typename T::iterator_category)
1112 typedef typename T::value_type value_type;
1113 typedef typename T::difference_type difference_type;
1114 typedef typename T::reference reference;
1115 typedef typename T::pointer *pointer;
1116
1117 reverse_iterator_impl() {}
1118 reverse_iterator_impl(iterator_type i) : m_cur(i) {}
1119 reverse_iterator_impl(const reverse_iterator_impl& ri)
1120 : m_cur(ri.m_cur) {}
1121
1122 iterator_type base() const { return m_cur; }
1123
1124 reference operator*() const { return *(m_cur-1); }
1125 reference operator[](size_t n) const { return *(*this + n); }
1126
1127 reverse_iterator_impl& operator++()
1128 { --m_cur; return *this; }
1129 reverse_iterator_impl operator++(int)
1130 { reverse_iterator_impl tmp = *this; --m_cur; return tmp; }
1131 reverse_iterator_impl& operator--()
1132 { ++m_cur; return *this; }
1133 reverse_iterator_impl operator--(int)
1134 { reverse_iterator_impl tmp = *this; ++m_cur; return tmp; }
1135
1136 // NB: explicit <T> in the functions below is to keep BCC 5.5 happy
1137 reverse_iterator_impl operator+(ptrdiff_t n) const
1138 { return reverse_iterator_impl<T>(m_cur - n); }
1139 reverse_iterator_impl operator-(ptrdiff_t n) const
1140 { return reverse_iterator_impl<T>(m_cur + n); }
1141 reverse_iterator_impl operator+=(ptrdiff_t n)
1142 { m_cur -= n; return *this; }
1143 reverse_iterator_impl operator-=(ptrdiff_t n)
1144 { m_cur += n; return *this; }
1145
1146 unsigned operator-(const reverse_iterator_impl& i) const
1147 { return i.m_cur - m_cur; }
1148
1149 bool operator==(const reverse_iterator_impl& ri) const
1150 { return m_cur == ri.m_cur; }
1151 bool operator!=(const reverse_iterator_impl& ri) const
1152 { return !(*this == ri); }
1153
1154 bool operator<(const reverse_iterator_impl& i) const
1155 { return m_cur > i.m_cur; }
1156 bool operator>(const reverse_iterator_impl& i) const
1157 { return m_cur < i.m_cur; }
1158 bool operator<=(const reverse_iterator_impl& i) const
1159 { return m_cur >= i.m_cur; }
1160 bool operator>=(const reverse_iterator_impl& i) const
1161 { return m_cur <= i.m_cur; }
1162
1163 private:
1164 iterator_type m_cur;
1165 };
1166
1167 typedef reverse_iterator_impl<iterator> reverse_iterator;
1168 typedef reverse_iterator_impl<const_iterator> const_reverse_iterator;
1169
1170 private:
1171 // used to transform an expression built using c_str() (and hence of type
1172 // wxCStrData) to an iterator into the string
1173 static const_iterator CreateConstIterator(const wxCStrData& data)
1174 {
1175 return const_iterator(data.m_str,
1176 (data.m_str->begin() + data.m_offset).impl());
1177 }
1178
1179 // in UTF-8 STL build, creation from std::string requires conversion under
1180 // non-UTF8 locales, so we can't have and use wxString(wxStringImpl) ctor;
1181 // instead we define dummy type that lets us have wxString ctor for creation
1182 // from wxStringImpl that couldn't be used by user code (in all other builds,
1183 // "standard" ctors can be used):
1184 #if wxUSE_UNICODE_UTF8 && wxUSE_STL_BASED_WXSTRING
1185 struct CtorFromStringImplTag {};
1186
1187 wxString(CtorFromStringImplTag* WXUNUSED(dummy), const wxStringImpl& src)
1188 : m_impl(src) {}
1189
1190 static wxString FromImpl(const wxStringImpl& src)
1191 { return wxString((CtorFromStringImplTag*)NULL, src); }
1192 #else
1193 #if !wxUSE_STL_BASED_WXSTRING
1194 wxString(const wxStringImpl& src) : m_impl(src) { }
1195 // else: already defined as wxString(wxStdString) below
1196 #endif
1197 static wxString FromImpl(const wxStringImpl& src) { return wxString(src); }
1198 #endif
1199
1200 public:
1201 // constructors and destructor
1202 // ctor for an empty string
1203 wxString() {}
1204
1205 // copy ctor
1206 wxString(const wxString& stringSrc) : m_impl(stringSrc.m_impl) { }
1207
1208 // string containing nRepeat copies of ch
1209 wxString(wxUniChar ch, size_t nRepeat = 1 )
1210 { assign(nRepeat, ch); }
1211 wxString(size_t nRepeat, wxUniChar ch)
1212 { assign(nRepeat, ch); }
1213 wxString(wxUniCharRef ch, size_t nRepeat = 1)
1214 { assign(nRepeat, ch); }
1215 wxString(size_t nRepeat, wxUniCharRef ch)
1216 { assign(nRepeat, ch); }
1217 wxString(char ch, size_t nRepeat = 1)
1218 { assign(nRepeat, ch); }
1219 wxString(size_t nRepeat, char ch)
1220 { assign(nRepeat, ch); }
1221 wxString(wchar_t ch, size_t nRepeat = 1)
1222 { assign(nRepeat, ch); }
1223 wxString(size_t nRepeat, wchar_t ch)
1224 { assign(nRepeat, ch); }
1225
1226 // ctors from char* strings:
1227 wxString(const char *psz)
1228 : m_impl(ImplStr(psz)) {}
1229 wxString(const char *psz, const wxMBConv& conv)
1230 : m_impl(ImplStr(psz, conv)) {}
1231 wxString(const char *psz, size_t nLength)
1232 { assign(psz, nLength); }
1233 wxString(const char *psz, const wxMBConv& conv, size_t nLength)
1234 {
1235 SubstrBufFromMB str(ImplStr(psz, nLength, conv));
1236 m_impl.assign(str.data, str.len);
1237 }
1238
1239 // and unsigned char*:
1240 wxString(const unsigned char *psz)
1241 : m_impl(ImplStr((const char*)psz)) {}
1242 wxString(const unsigned char *psz, const wxMBConv& conv)
1243 : m_impl(ImplStr((const char*)psz, conv)) {}
1244 wxString(const unsigned char *psz, size_t nLength)
1245 { assign((const char*)psz, nLength); }
1246 wxString(const unsigned char *psz, const wxMBConv& conv, size_t nLength)
1247 {
1248 SubstrBufFromMB str(ImplStr((const char*)psz, nLength, conv));
1249 m_impl.assign(str.data, str.len);
1250 }
1251
1252 // ctors from wchar_t* strings:
1253 wxString(const wchar_t *pwz)
1254 : m_impl(ImplStr(pwz)) {}
1255 wxString(const wchar_t *pwz, const wxMBConv& WXUNUSED(conv))
1256 : m_impl(ImplStr(pwz)) {}
1257 wxString(const wchar_t *pwz, size_t nLength)
1258 { assign(pwz, nLength); }
1259 wxString(const wchar_t *pwz, const wxMBConv& WXUNUSED(conv), size_t nLength)
1260 { assign(pwz, nLength); }
1261
1262 wxString(const wxCharBuffer& buf)
1263 { assign(buf.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
1264 wxString(const wxWCharBuffer& buf)
1265 { assign(buf.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
1266
1267 // NB: this version uses m_impl.c_str() to force making a copy of the
1268 // string, so that "wxString(str.c_str())" idiom for passing strings
1269 // between threads works
1270 wxString(const wxCStrData& cstr)
1271 : m_impl(cstr.AsString().m_impl.c_str()) { }
1272
1273 // as we provide both ctors with this signature for both char and unsigned
1274 // char string, we need to provide one for wxCStrData to resolve ambiguity
1275 wxString(const wxCStrData& cstr, size_t nLength)
1276 : m_impl(cstr.AsString().Mid(0, nLength).m_impl) {}
1277
1278 // and because wxString is convertible to wxCStrData and const wxChar *
1279 // we also need to provide this one
1280 wxString(const wxString& str, size_t nLength)
1281 { assign(str, nLength); }
1282
1283
1284 #if wxUSE_STRING_POS_CACHE
1285 ~wxString()
1286 {
1287 // we need to invalidate our cache entry as another string could be
1288 // recreated at the same address (unlikely, but still possible, with the
1289 // heap-allocated strings but perfectly common with stack-allocated ones)
1290 InvalidateCache();
1291 }
1292 #endif // wxUSE_STRING_POS_CACHE
1293
1294 // even if we're not built with wxUSE_STL == 1 it is very convenient to allow
1295 // implicit conversions from std::string to wxString and vice verse as this
1296 // allows to use the same strings in non-GUI and GUI code, however we don't
1297 // want to unconditionally add this ctor as it would make wx lib dependent on
1298 // libstdc++ on some Linux versions which is bad, so instead we ask the
1299 // client code to define this wxUSE_STD_STRING symbol if they need it
1300 #if wxUSE_STD_STRING
1301 #if wxUSE_UNICODE_WCHAR
1302 wxString(const wxStdWideString& str) : m_impl(str) {}
1303 #else // UTF-8 or ANSI
1304 wxString(const wxStdWideString& str)
1305 { assign(str.c_str(), str.length()); }
1306 #endif
1307
1308 #if !wxUSE_UNICODE // ANSI build
1309 // FIXME-UTF8: do this in UTF8 build #if wxUSE_UTF8_LOCALE_ONLY, too
1310 wxString(const std::string& str) : m_impl(str) {}
1311 #else // Unicode
1312 wxString(const std::string& str)
1313 { assign(str.c_str(), str.length()); }
1314 #endif
1315 #endif // wxUSE_STD_STRING
1316
1317 // Unlike ctor from std::string, we provide conversion to std::string only
1318 // if wxUSE_STL and not merely wxUSE_STD_STRING (which is on by default),
1319 // because it conflicts with operator const char/wchar_t*:
1320 #if wxUSE_STL
1321 #if wxUSE_UNICODE_WCHAR && wxUSE_STL_BASED_WXSTRING
1322 // wxStringImpl is std::string in the encoding we want
1323 operator const wxStdWideString&() const { return m_impl; }
1324 #else
1325 // wxStringImpl is either not std::string or needs conversion
1326 operator wxStdWideString() const
1327 // FIXME-UTF8: broken for embedded NULs
1328 { return wxStdWideString(wc_str()); }
1329 #endif
1330
1331 #if (!wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY) && wxUSE_STL_BASED_WXSTRING
1332 // wxStringImpl is std::string in the encoding we want
1333 operator const std::string&() const { return m_impl; }
1334 #else
1335 // wxStringImpl is either not std::string or needs conversion
1336 operator std::string() const
1337 // FIXME-UTF8: broken for embedded NULs
1338 { return std::string(mb_str()); }
1339 #endif
1340 #endif // wxUSE_STL
1341
1342 wxString Clone() const
1343 {
1344 // make a deep copy of the string, i.e. the returned string will have
1345 // ref count = 1 with refcounted implementation
1346 return wxString::FromImpl(wxStringImpl(m_impl.c_str(), m_impl.length()));
1347 }
1348
1349 // first valid index position
1350 const_iterator begin() const { return const_iterator(this, m_impl.begin()); }
1351 iterator begin() { return iterator(this, m_impl.begin()); }
1352 // position one after the last valid one
1353 const_iterator end() const { return const_iterator(this, m_impl.end()); }
1354 iterator end() { return iterator(this, m_impl.end()); }
1355
1356 // first element of the reversed string
1357 const_reverse_iterator rbegin() const
1358 { return const_reverse_iterator(end()); }
1359 reverse_iterator rbegin()
1360 { return reverse_iterator(end()); }
1361 // one beyond the end of the reversed string
1362 const_reverse_iterator rend() const
1363 { return const_reverse_iterator(begin()); }
1364 reverse_iterator rend()
1365 { return reverse_iterator(begin()); }
1366
1367 // std::string methods:
1368 #if wxUSE_UNICODE_UTF8
1369 size_t length() const
1370 {
1371 #if wxUSE_STRING_POS_CACHE
1372 wxCACHE_PROFILE_FIELD_INC(lentot);
1373
1374 Cache::Element * const cache = GetCacheElement();
1375
1376 if ( cache->len == npos )
1377 {
1378 // it's probably not worth trying to be clever and using cache->pos
1379 // here as it's probably 0 anyhow -- you usually call length() before
1380 // starting to index the string
1381 cache->len = end() - begin();
1382 }
1383 else
1384 {
1385 wxCACHE_PROFILE_FIELD_INC(lenhits);
1386
1387 wxSTRING_CACHE_ASSERT( (int)cache->len == end() - begin() );
1388 }
1389
1390 return cache->len;
1391 #else // !wxUSE_STRING_POS_CACHE
1392 return end() - begin();
1393 #endif // wxUSE_STRING_POS_CACHE/!wxUSE_STRING_POS_CACHE
1394 }
1395 #else
1396 size_t length() const { return m_impl.length(); }
1397 #endif
1398
1399 size_type size() const { return length(); }
1400 size_type max_size() const { return npos; }
1401
1402 bool empty() const { return m_impl.empty(); }
1403
1404 // NB: these methods don't have a well-defined meaning in UTF-8 case
1405 size_type capacity() const { return m_impl.capacity(); }
1406 void reserve(size_t sz) { m_impl.reserve(sz); }
1407
1408 void resize(size_t nSize, wxUniChar ch = wxT('\0'))
1409 {
1410 const size_t len = length();
1411 if ( nSize == len)
1412 return;
1413
1414 #if wxUSE_UNICODE_UTF8
1415 if ( nSize < len )
1416 {
1417 wxSTRING_INVALIDATE_CACHE();
1418
1419 // we can't use wxStringImpl::resize() for truncating the string as it
1420 // counts in bytes, not characters
1421 erase(nSize);
1422 return;
1423 }
1424
1425 // we also can't use (presumably more efficient) resize() if we have to
1426 // append characters taking more than one byte
1427 if ( !ch.IsAscii() )
1428 {
1429 append(nSize - len, ch);
1430 }
1431 else // can use (presumably faster) resize() version
1432 #endif // wxUSE_UNICODE_UTF8
1433 {
1434 wxSTRING_INVALIDATE_CACHED_LENGTH();
1435
1436 m_impl.resize(nSize, (wxStringCharType)ch);
1437 }
1438 }
1439
1440 wxString substr(size_t nStart = 0, size_t nLen = npos) const
1441 {
1442 size_t pos, len;
1443 PosLenToImpl(nStart, nLen, &pos, &len);
1444 return FromImpl(m_impl.substr(pos, len));
1445 }
1446
1447 // generic attributes & operations
1448 // as standard strlen()
1449 size_t Len() const { return length(); }
1450 // string contains any characters?
1451 bool IsEmpty() const { return empty(); }
1452 // empty string is "false", so !str will return true
1453 bool operator!() const { return empty(); }
1454 // truncate the string to given length
1455 wxString& Truncate(size_t uiLen);
1456 // empty string contents
1457 void Empty() { clear(); }
1458 // empty the string and free memory
1459 void Clear() { clear(); }
1460
1461 // contents test
1462 // Is an ascii value
1463 bool IsAscii() const;
1464 // Is a number
1465 bool IsNumber() const;
1466 // Is a word
1467 bool IsWord() const;
1468
1469 // data access (all indexes are 0 based)
1470 // read access
1471 wxUniChar at(size_t n) const
1472 { return wxStringOperations::DecodeChar(m_impl.begin() + PosToImpl(n)); }
1473 wxUniChar GetChar(size_t n) const
1474 { return at(n); }
1475 // read/write access
1476 wxUniCharRef at(size_t n)
1477 { return *GetIterForNthChar(n); }
1478 wxUniCharRef GetWritableChar(size_t n)
1479 { return at(n); }
1480 // write access
1481 void SetChar(size_t n, wxUniChar ch)
1482 { at(n) = ch; }
1483
1484 // get last character
1485 wxUniChar Last() const
1486 {
1487 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1488 return *rbegin();
1489 }
1490
1491 // get writable last character
1492 wxUniCharRef Last()
1493 {
1494 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1495 return *rbegin();
1496 }
1497
1498 /*
1499 Note that we we must define all of the overloads below to avoid
1500 ambiguity when using str[0].
1501 */
1502 wxUniChar operator[](int n) const
1503 { return at(n); }
1504 wxUniChar operator[](long n) const
1505 { return at(n); }
1506 wxUniChar operator[](size_t n) const
1507 { return at(n); }
1508 #ifndef wxSIZE_T_IS_UINT
1509 wxUniChar operator[](unsigned int n) const
1510 { return at(n); }
1511 #endif // size_t != unsigned int
1512
1513 // operator versions of GetWriteableChar()
1514 wxUniCharRef operator[](int n)
1515 { return at(n); }
1516 wxUniCharRef operator[](long n)
1517 { return at(n); }
1518 wxUniCharRef operator[](size_t n)
1519 { return at(n); }
1520 #ifndef wxSIZE_T_IS_UINT
1521 wxUniCharRef operator[](unsigned int n)
1522 { return at(n); }
1523 #endif // size_t != unsigned int
1524
1525 // explicit conversion to C string (use this with printf()!)
1526 wxCStrData c_str() const { return wxCStrData(this); }
1527 wxCStrData data() const { return c_str(); }
1528
1529 // implicit conversion to C string
1530 operator wxCStrData() const { return c_str(); }
1531
1532 // the first two operators conflict with operators for conversion to
1533 // std::string and they must be disabled in STL build; the next one only
1534 // makes sense if conversions to char* are also defined and not defining it
1535 // in STL build also helps us to get more clear error messages for the code
1536 // which relies on implicit conversion to char* in STL build
1537 #if !wxUSE_STL
1538 operator const char*() const { return c_str(); }
1539 operator const wchar_t*() const { return c_str(); }
1540
1541 // implicit conversion to untyped pointer for compatibility with previous
1542 // wxWidgets versions: this is the same as conversion to const char * so it
1543 // may fail!
1544 operator const void*() const { return c_str(); }
1545 #endif // wxUSE_STL
1546
1547 // identical to c_str(), for MFC compatibility
1548 const wxCStrData GetData() const { return c_str(); }
1549
1550 // explicit conversion to C string in internal representation (char*,
1551 // wchar_t*, UTF-8-encoded char*, depending on the build):
1552 const wxStringCharType *wx_str() const { return m_impl.c_str(); }
1553
1554 // conversion to *non-const* multibyte or widestring buffer; modifying
1555 // returned buffer won't affect the string, these methods are only useful
1556 // for passing values to const-incorrect functions
1557 wxWritableCharBuffer char_str(const wxMBConv& conv = wxConvLibc) const
1558 { return mb_str(conv); }
1559 wxWritableWCharBuffer wchar_str() const { return wc_str(); }
1560
1561 // conversion to the buffer of the given type T (= char or wchar_t) and
1562 // also optionally return the buffer length
1563 //
1564 // this is mostly/only useful for the template functions
1565 //
1566 // FIXME-VC6: the second argument only exists for VC6 which doesn't support
1567 // explicit template function selection, do not use it unless
1568 // you must support VC6!
1569 template <typename T>
1570 wxCharTypeBuffer<T> tchar_str(size_t *len = NULL,
1571 T * WXUNUSED(dummy) = NULL) const
1572 {
1573 #if wxUSE_UNICODE
1574 // we need a helper dispatcher depending on type
1575 return wxPrivate::wxStringAsBufHelper<T>::Get(*this, len);
1576 #else // ANSI
1577 // T can only be char in ANSI build
1578 if ( len )
1579 *len = length();
1580
1581 return wxCharTypeBuffer<T>::CreateNonOwned(wx_str());
1582 #endif // Unicode build kind
1583 }
1584
1585 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
1586 // converting numbers or strings which are certain not to contain special
1587 // chars (typically system functions, X atoms, environment variables etc.)
1588 //
1589 // the behaviour of these functions with the strings containing anything
1590 // else than 7 bit ASCII characters is undefined, use at your own risk.
1591 #if wxUSE_UNICODE
1592 static wxString FromAscii(const char *ascii, size_t len);
1593 static wxString FromAscii(const char *ascii);
1594 static wxString FromAscii(char ascii);
1595 const wxCharBuffer ToAscii() const;
1596 #else // ANSI
1597 static wxString FromAscii(const char *ascii) { return wxString( ascii ); }
1598 static wxString FromAscii(const char *ascii, size_t len)
1599 { return wxString( ascii, len ); }
1600 static wxString FromAscii(char ascii) { return wxString( ascii ); }
1601 const char *ToAscii() const { return c_str(); }
1602 #endif // Unicode/!Unicode
1603
1604 // also provide unsigned char overloads as signed/unsigned doesn't matter
1605 // for 7 bit ASCII characters
1606 static wxString FromAscii(const unsigned char *ascii)
1607 { return FromAscii((const char *)ascii); }
1608 static wxString FromAscii(const unsigned char *ascii, size_t len)
1609 { return FromAscii((const char *)ascii, len); }
1610
1611 // conversion to/from UTF-8:
1612 #if wxUSE_UNICODE_UTF8
1613 static wxString FromUTF8Unchecked(const char *utf8)
1614 {
1615 if ( !utf8 )
1616 return wxEmptyString;
1617
1618 wxASSERT( wxStringOperations::IsValidUtf8String(utf8) );
1619 return FromImpl(wxStringImpl(utf8));
1620 }
1621 static wxString FromUTF8Unchecked(const char *utf8, size_t len)
1622 {
1623 if ( !utf8 )
1624 return wxEmptyString;
1625 if ( len == npos )
1626 return FromUTF8Unchecked(utf8);
1627
1628 wxASSERT( wxStringOperations::IsValidUtf8String(utf8, len) );
1629 return FromImpl(wxStringImpl(utf8, len));
1630 }
1631
1632 static wxString FromUTF8(const char *utf8)
1633 {
1634 if ( !utf8 || !wxStringOperations::IsValidUtf8String(utf8) )
1635 return "";
1636
1637 return FromImpl(wxStringImpl(utf8));
1638 }
1639 static wxString FromUTF8(const char *utf8, size_t len)
1640 {
1641 if ( len == npos )
1642 return FromUTF8(utf8);
1643
1644 if ( !utf8 || !wxStringOperations::IsValidUtf8String(utf8, len) )
1645 return "";
1646
1647 return FromImpl(wxStringImpl(utf8, len));
1648 }
1649
1650 const char* utf8_str() const { return wx_str(); }
1651 const char* ToUTF8() const { return wx_str(); }
1652
1653 // this function exists in UTF-8 build only and returns the length of the
1654 // internal UTF-8 representation
1655 size_t utf8_length() const { return m_impl.length(); }
1656 #elif wxUSE_UNICODE_WCHAR
1657 static wxString FromUTF8(const char *utf8, size_t len = npos)
1658 { return wxString(utf8, wxMBConvUTF8(), len); }
1659 static wxString FromUTF8Unchecked(const char *utf8, size_t len = npos)
1660 {
1661 const wxString s(utf8, wxMBConvUTF8(), len);
1662 wxASSERT_MSG( !utf8 || !*utf8 || !s.empty(),
1663 "string must be valid UTF-8" );
1664 return s;
1665 }
1666 const wxCharBuffer utf8_str() const { return mb_str(wxMBConvUTF8()); }
1667 const wxCharBuffer ToUTF8() const { return utf8_str(); }
1668 #else // ANSI
1669 static wxString FromUTF8(const char *utf8)
1670 { return wxString(wxMBConvUTF8().cMB2WC(utf8)); }
1671 static wxString FromUTF8(const char *utf8, size_t len)
1672 {
1673 size_t wlen;
1674 wxWCharBuffer buf(wxMBConvUTF8().cMB2WC(utf8, len == npos ? wxNO_LEN : len, &wlen));
1675 return wxString(buf.data(), wlen);
1676 }
1677 static wxString FromUTF8Unchecked(const char *utf8, size_t len = npos)
1678 {
1679 size_t wlen;
1680 wxWCharBuffer buf(wxMBConvUTF8().cMB2WC(utf8,
1681 len == npos ? wxNO_LEN : len,
1682 &wlen));
1683 wxASSERT_MSG( !utf8 || !*utf8 || wlen,
1684 "string must be valid UTF-8" );
1685
1686 return wxString(buf.data(), wlen);
1687 }
1688 const wxCharBuffer utf8_str() const
1689 { return wxMBConvUTF8().cWC2MB(wc_str()); }
1690 const wxCharBuffer ToUTF8() const { return utf8_str(); }
1691 #endif
1692
1693 // functions for storing binary data in wxString:
1694 #if wxUSE_UNICODE
1695 static wxString From8BitData(const char *data, size_t len)
1696 { return wxString(data, wxConvISO8859_1, len); }
1697 // version for NUL-terminated data:
1698 static wxString From8BitData(const char *data)
1699 { return wxString(data, wxConvISO8859_1); }
1700 const wxCharBuffer To8BitData() const { return mb_str(wxConvISO8859_1); }
1701 #else // ANSI
1702 static wxString From8BitData(const char *data, size_t len)
1703 { return wxString(data, len); }
1704 // version for NUL-terminated data:
1705 static wxString From8BitData(const char *data)
1706 { return wxString(data); }
1707 const char *To8BitData() const { return c_str(); }
1708 #endif // Unicode/ANSI
1709
1710 // conversions with (possible) format conversions: have to return a
1711 // buffer with temporary data
1712 //
1713 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
1714 // return an ANSI (multibyte) string, wc_str() to return a wide string and
1715 // fn_str() to return a string which should be used with the OS APIs
1716 // accepting the file names. The return value is always the same, but the
1717 // type differs because a function may either return pointer to the buffer
1718 // directly or have to use intermediate buffer for translation.
1719 #if wxUSE_UNICODE
1720
1721 #if wxUSE_UTF8_LOCALE_ONLY
1722 const char* mb_str() const { return wx_str(); }
1723 const wxCharBuffer mb_str(const wxMBConv& conv) const;
1724 #else
1725 const wxCharBuffer mb_str(const wxMBConv& conv = wxConvLibc) const;
1726 #endif
1727
1728 const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); }
1729
1730 #if wxUSE_UNICODE_WCHAR
1731 const wchar_t* wc_str() const { return wx_str(); }
1732 #elif wxUSE_UNICODE_UTF8
1733 const wxWCharBuffer wc_str() const;
1734 #endif
1735 // for compatibility with !wxUSE_UNICODE version
1736 const wxWX2WCbuf wc_str(const wxMBConv& WXUNUSED(conv)) const
1737 { return wc_str(); }
1738
1739 #if wxMBFILES
1740 const wxCharBuffer fn_str() const { return mb_str(wxConvFile); }
1741 #else // !wxMBFILES
1742 const wxWX2WCbuf fn_str() const { return wc_str(); }
1743 #endif // wxMBFILES/!wxMBFILES
1744
1745 #else // ANSI
1746 const wxChar* mb_str() const { return wx_str(); }
1747
1748 // for compatibility with wxUSE_UNICODE version
1749 const char* mb_str(const wxMBConv& WXUNUSED(conv)) const { return wx_str(); }
1750
1751 const wxWX2MBbuf mbc_str() const { return mb_str(); }
1752
1753 #if wxUSE_WCHAR_T
1754 const wxWCharBuffer wc_str(const wxMBConv& conv = wxConvLibc) const;
1755 #endif // wxUSE_WCHAR_T
1756 const wxCharBuffer fn_str() const { return wxConvFile.cWC2WX( wc_str( wxConvLibc ) ); }
1757 #endif // Unicode/ANSI
1758
1759 #if wxUSE_UNICODE_UTF8
1760 const wxWCharBuffer t_str() const { return wc_str(); }
1761 #elif wxUSE_UNICODE_WCHAR
1762 const wchar_t* t_str() const { return wx_str(); }
1763 #else
1764 const char* t_str() const { return wx_str(); }
1765 #endif
1766
1767
1768 // overloaded assignment
1769 // from another wxString
1770 wxString& operator=(const wxString& stringSrc)
1771 {
1772 if ( this != &stringSrc )
1773 {
1774 wxSTRING_INVALIDATE_CACHE();
1775
1776 m_impl = stringSrc.m_impl;
1777 }
1778
1779 return *this;
1780 }
1781
1782 wxString& operator=(const wxCStrData& cstr)
1783 { return *this = cstr.AsString(); }
1784 // from a character
1785 wxString& operator=(wxUniChar ch)
1786 {
1787 wxSTRING_INVALIDATE_CACHE();
1788
1789 #if wxUSE_UNICODE_UTF8
1790 if ( !ch.IsAscii() )
1791 m_impl = wxStringOperations::EncodeChar(ch);
1792 else
1793 #endif // wxUSE_UNICODE_UTF8
1794 m_impl = (wxStringCharType)ch;
1795 return *this;
1796 }
1797
1798 wxString& operator=(wxUniCharRef ch)
1799 { return operator=((wxUniChar)ch); }
1800 wxString& operator=(char ch)
1801 { return operator=(wxUniChar(ch)); }
1802 wxString& operator=(unsigned char ch)
1803 { return operator=(wxUniChar(ch)); }
1804 wxString& operator=(wchar_t ch)
1805 { return operator=(wxUniChar(ch)); }
1806 // from a C string - STL probably will crash on NULL,
1807 // so we need to compensate in that case
1808 #if wxUSE_STL_BASED_WXSTRING
1809 wxString& operator=(const char *psz)
1810 {
1811 wxSTRING_INVALIDATE_CACHE();
1812
1813 if ( psz )
1814 m_impl = ImplStr(psz);
1815 else
1816 clear();
1817
1818 return *this;
1819 }
1820
1821 wxString& operator=(const wchar_t *pwz)
1822 {
1823 wxSTRING_INVALIDATE_CACHE();
1824
1825 if ( pwz )
1826 m_impl = ImplStr(pwz);
1827 else
1828 clear();
1829
1830 return *this;
1831 }
1832 #else // !wxUSE_STL_BASED_WXSTRING
1833 wxString& operator=(const char *psz)
1834 {
1835 wxSTRING_INVALIDATE_CACHE();
1836
1837 m_impl = ImplStr(psz);
1838
1839 return *this;
1840 }
1841
1842 wxString& operator=(const wchar_t *pwz)
1843 {
1844 wxSTRING_INVALIDATE_CACHE();
1845
1846 m_impl = ImplStr(pwz);
1847
1848 return *this;
1849 }
1850 #endif // wxUSE_STL_BASED_WXSTRING/!wxUSE_STL_BASED_WXSTRING
1851
1852 wxString& operator=(const unsigned char *psz)
1853 { return operator=((const char*)psz); }
1854
1855 // from wxWCharBuffer
1856 wxString& operator=(const wxWCharBuffer& s)
1857 { return operator=(s.data()); } // FIXME-UTF8: fix for embedded NULs
1858 // from wxCharBuffer
1859 wxString& operator=(const wxCharBuffer& s)
1860 { return operator=(s.data()); } // FIXME-UTF8: fix for embedded NULs
1861
1862 // string concatenation
1863 // in place concatenation
1864 /*
1865 Concatenate and return the result. Note that the left to right
1866 associativity of << allows to write things like "str << str1 << str2
1867 << ..." (unlike with +=)
1868 */
1869 // string += string
1870 wxString& operator<<(const wxString& s)
1871 {
1872 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1873 wxASSERT_MSG( s.IsValid(),
1874 _T("did you forget to call UngetWriteBuf()?") );
1875 #endif
1876
1877 append(s);
1878 return *this;
1879 }
1880 // string += C string
1881 wxString& operator<<(const char *psz)
1882 { append(psz); return *this; }
1883 wxString& operator<<(const wchar_t *pwz)
1884 { append(pwz); return *this; }
1885 wxString& operator<<(const wxCStrData& psz)
1886 { append(psz.AsString()); return *this; }
1887 // string += char
1888 wxString& operator<<(wxUniChar ch) { append(1, ch); return *this; }
1889 wxString& operator<<(wxUniCharRef ch) { append(1, ch); return *this; }
1890 wxString& operator<<(char ch) { append(1, ch); return *this; }
1891 wxString& operator<<(unsigned char ch) { append(1, ch); return *this; }
1892 wxString& operator<<(wchar_t ch) { append(1, ch); return *this; }
1893
1894 // string += buffer (i.e. from wxGetString)
1895 wxString& operator<<(const wxWCharBuffer& s)
1896 { return operator<<((const wchar_t *)s); }
1897 wxString& operator<<(const wxCharBuffer& s)
1898 { return operator<<((const char *)s); }
1899
1900 // string += C string
1901 wxString& Append(const wxString& s)
1902 {
1903 // test for empty() to share the string if possible
1904 if ( empty() )
1905 *this = s;
1906 else
1907 append(s);
1908 return *this;
1909 }
1910 wxString& Append(const char* psz)
1911 { append(psz); return *this; }
1912 wxString& Append(const wchar_t* pwz)
1913 { append(pwz); return *this; }
1914 wxString& Append(const wxCStrData& psz)
1915 { append(psz); return *this; }
1916 wxString& Append(const wxCharBuffer& psz)
1917 { append(psz); return *this; }
1918 wxString& Append(const wxWCharBuffer& psz)
1919 { append(psz); return *this; }
1920 wxString& Append(const char* psz, size_t nLen)
1921 { append(psz, nLen); return *this; }
1922 wxString& Append(const wchar_t* pwz, size_t nLen)
1923 { append(pwz, nLen); return *this; }
1924 wxString& Append(const wxCStrData& psz, size_t nLen)
1925 { append(psz, nLen); return *this; }
1926 wxString& Append(const wxCharBuffer& psz, size_t nLen)
1927 { append(psz, nLen); return *this; }
1928 wxString& Append(const wxWCharBuffer& psz, size_t nLen)
1929 { append(psz, nLen); return *this; }
1930 // append count copies of given character
1931 wxString& Append(wxUniChar ch, size_t count = 1u)
1932 { append(count, ch); return *this; }
1933 wxString& Append(wxUniCharRef ch, size_t count = 1u)
1934 { append(count, ch); return *this; }
1935 wxString& Append(char ch, size_t count = 1u)
1936 { append(count, ch); return *this; }
1937 wxString& Append(unsigned char ch, size_t count = 1u)
1938 { append(count, ch); return *this; }
1939 wxString& Append(wchar_t ch, size_t count = 1u)
1940 { append(count, ch); return *this; }
1941
1942 // prepend a string, return the string itself
1943 wxString& Prepend(const wxString& str)
1944 { *this = str + *this; return *this; }
1945
1946 // non-destructive concatenation
1947 // two strings
1948 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string1,
1949 const wxString& string2);
1950 // string with a single char
1951 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch);
1952 // char with a string
1953 friend wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string);
1954 // string with C string
1955 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string,
1956 const char *psz);
1957 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string,
1958 const wchar_t *pwz);
1959 // C string with string
1960 friend wxString WXDLLIMPEXP_BASE operator+(const char *psz,
1961 const wxString& string);
1962 friend wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz,
1963 const wxString& string);
1964
1965 // stream-like functions
1966 // insert an int into string
1967 wxString& operator<<(int i)
1968 { return (*this) << Format(_T("%d"), i); }
1969 // insert an unsigned int into string
1970 wxString& operator<<(unsigned int ui)
1971 { return (*this) << Format(_T("%u"), ui); }
1972 // insert a long into string
1973 wxString& operator<<(long l)
1974 { return (*this) << Format(_T("%ld"), l); }
1975 // insert an unsigned long into string
1976 wxString& operator<<(unsigned long ul)
1977 { return (*this) << Format(_T("%lu"), ul); }
1978 #if defined wxLongLong_t && !defined wxLongLongIsLong
1979 // insert a long long if they exist and aren't longs
1980 wxString& operator<<(wxLongLong_t ll)
1981 {
1982 const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("d");
1983 return (*this) << Format(fmt, ll);
1984 }
1985 // insert an unsigned long long
1986 wxString& operator<<(wxULongLong_t ull)
1987 {
1988 const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("u");
1989 return (*this) << Format(fmt , ull);
1990 }
1991 #endif // wxLongLong_t && !wxLongLongIsLong
1992 // insert a float into string
1993 wxString& operator<<(float f)
1994 { return (*this) << Format(_T("%f"), f); }
1995 // insert a double into string
1996 wxString& operator<<(double d)
1997 { return (*this) << Format(_T("%g"), d); }
1998
1999 // string comparison
2000 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
2001 int Cmp(const char *psz) const
2002 { return compare(psz); }
2003 int Cmp(const wchar_t *pwz) const
2004 { return compare(pwz); }
2005 int Cmp(const wxString& s) const
2006 { return compare(s); }
2007 int Cmp(const wxCStrData& s) const
2008 { return compare(s); }
2009 int Cmp(const wxCharBuffer& s) const
2010 { return compare(s); }
2011 int Cmp(const wxWCharBuffer& s) const
2012 { return compare(s); }
2013 // same as Cmp() but not case-sensitive
2014 int CmpNoCase(const wxString& s) const;
2015
2016 // test for the string equality, either considering case or not
2017 // (if compareWithCase then the case matters)
2018 bool IsSameAs(const wxString& str, bool compareWithCase = true) const
2019 {
2020 #if !wxUSE_UNICODE_UTF8
2021 // in UTF-8 build, length() is O(n) and doing this would be _slower_
2022 if ( length() != str.length() )
2023 return false;
2024 #endif
2025 return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0;
2026 }
2027 bool IsSameAs(const char *str, bool compareWithCase = true) const
2028 { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; }
2029 bool IsSameAs(const wchar_t *str, bool compareWithCase = true) const
2030 { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; }
2031
2032 bool IsSameAs(const wxCStrData& str, bool compareWithCase = true) const
2033 { return IsSameAs(str.AsString(), compareWithCase); }
2034 bool IsSameAs(const wxCharBuffer& str, bool compareWithCase = true) const
2035 { return IsSameAs(str.data(), compareWithCase); }
2036 bool IsSameAs(const wxWCharBuffer& str, bool compareWithCase = true) const
2037 { return IsSameAs(str.data(), compareWithCase); }
2038 // comparison with a single character: returns true if equal
2039 bool IsSameAs(wxUniChar c, bool compareWithCase = true) const;
2040 // FIXME-UTF8: remove these overloads
2041 bool IsSameAs(wxUniCharRef c, bool compareWithCase = true) const
2042 { return IsSameAs(wxUniChar(c), compareWithCase); }
2043 bool IsSameAs(char c, bool compareWithCase = true) const
2044 { return IsSameAs(wxUniChar(c), compareWithCase); }
2045 bool IsSameAs(unsigned char c, bool compareWithCase = true) const
2046 { return IsSameAs(wxUniChar(c), compareWithCase); }
2047 bool IsSameAs(wchar_t c, bool compareWithCase = true) const
2048 { return IsSameAs(wxUniChar(c), compareWithCase); }
2049 bool IsSameAs(int c, bool compareWithCase = true) const
2050 { return IsSameAs(wxUniChar(c), compareWithCase); }
2051
2052 // simple sub-string extraction
2053 // return substring starting at nFirst of length nCount (or till the end
2054 // if nCount = default value)
2055 wxString Mid(size_t nFirst, size_t nCount = npos) const;
2056
2057 // operator version of Mid()
2058 wxString operator()(size_t start, size_t len) const
2059 { return Mid(start, len); }
2060
2061 // check if the string starts with the given prefix and return the rest
2062 // of the string in the provided pointer if it is not NULL; otherwise
2063 // return false
2064 bool StartsWith(const wxString& prefix, wxString *rest = NULL) const;
2065 // check if the string ends with the given suffix and return the
2066 // beginning of the string before the suffix in the provided pointer if
2067 // it is not NULL; otherwise return false
2068 bool EndsWith(const wxString& suffix, wxString *rest = NULL) const;
2069
2070 // get first nCount characters
2071 wxString Left(size_t nCount) const;
2072 // get last nCount characters
2073 wxString Right(size_t nCount) const;
2074 // get all characters before the first occurance of ch
2075 // (returns the whole string if ch not found)
2076 wxString BeforeFirst(wxUniChar ch) const;
2077 // get all characters before the last occurence of ch
2078 // (returns empty string if ch not found)
2079 wxString BeforeLast(wxUniChar ch) const;
2080 // get all characters after the first occurence of ch
2081 // (returns empty string if ch not found)
2082 wxString AfterFirst(wxUniChar ch) const;
2083 // get all characters after the last occurence of ch
2084 // (returns the whole string if ch not found)
2085 wxString AfterLast(wxUniChar ch) const;
2086
2087 // for compatibility only, use more explicitly named functions above
2088 wxString Before(wxUniChar ch) const { return BeforeLast(ch); }
2089 wxString After(wxUniChar ch) const { return AfterFirst(ch); }
2090
2091 // case conversion
2092 // convert to upper case in place, return the string itself
2093 wxString& MakeUpper();
2094 // convert to upper case, return the copy of the string
2095 wxString Upper() const { return wxString(*this).MakeUpper(); }
2096 // convert to lower case in place, return the string itself
2097 wxString& MakeLower();
2098 // convert to lower case, return the copy of the string
2099 wxString Lower() const { return wxString(*this).MakeLower(); }
2100 // convert the first character to the upper case and the rest to the
2101 // lower one, return the modified string itself
2102 wxString& MakeCapitalized();
2103 // convert the first character to the upper case and the rest to the
2104 // lower one, return the copy of the string
2105 wxString Capitalize() const { return wxString(*this).MakeCapitalized(); }
2106
2107 // trimming/padding whitespace (either side) and truncating
2108 // remove spaces from left or from right (default) side
2109 wxString& Trim(bool bFromRight = true);
2110 // add nCount copies chPad in the beginning or at the end (default)
2111 wxString& Pad(size_t nCount, wxUniChar chPad = wxT(' '), bool bFromRight = true);
2112
2113 // searching and replacing
2114 // searching (return starting index, or -1 if not found)
2115 int Find(wxUniChar ch, bool bFromEnd = false) const; // like strchr/strrchr
2116 int Find(wxUniCharRef ch, bool bFromEnd = false) const
2117 { return Find(wxUniChar(ch), bFromEnd); }
2118 int Find(char ch, bool bFromEnd = false) const
2119 { return Find(wxUniChar(ch), bFromEnd); }
2120 int Find(unsigned char ch, bool bFromEnd = false) const
2121 { return Find(wxUniChar(ch), bFromEnd); }
2122 int Find(wchar_t ch, bool bFromEnd = false) const
2123 { return Find(wxUniChar(ch), bFromEnd); }
2124 // searching (return starting index, or -1 if not found)
2125 int Find(const wxString& sub) const // like strstr
2126 {
2127 size_type idx = find(sub);
2128 return (idx == npos) ? wxNOT_FOUND : (int)idx;
2129 }
2130 int Find(const char *sub) const // like strstr
2131 {
2132 size_type idx = find(sub);
2133 return (idx == npos) ? wxNOT_FOUND : (int)idx;
2134 }
2135 int Find(const wchar_t *sub) const // like strstr
2136 {
2137 size_type idx = find(sub);
2138 return (idx == npos) ? wxNOT_FOUND : (int)idx;
2139 }
2140
2141 int Find(const wxCStrData& sub) const
2142 { return Find(sub.AsString()); }
2143 int Find(const wxCharBuffer& sub) const
2144 { return Find(sub.data()); }
2145 int Find(const wxWCharBuffer& sub) const
2146 { return Find(sub.data()); }
2147
2148 // replace first (or all of bReplaceAll) occurences of substring with
2149 // another string, returns the number of replacements made
2150 size_t Replace(const wxString& strOld,
2151 const wxString& strNew,
2152 bool bReplaceAll = true);
2153
2154 // check if the string contents matches a mask containing '*' and '?'
2155 bool Matches(const wxString& mask) const;
2156
2157 // conversion to numbers: all functions return true only if the whole
2158 // string is a number and put the value of this number into the pointer
2159 // provided, the base is the numeric base in which the conversion should be
2160 // done and must be comprised between 2 and 36 or be 0 in which case the
2161 // standard C rules apply (leading '0' => octal, "0x" => hex)
2162 // convert to a signed integer
2163 bool ToLong(long *val, int base = 10) const;
2164 // convert to an unsigned integer
2165 bool ToULong(unsigned long *val, int base = 10) const;
2166 // convert to wxLongLong
2167 #if defined(wxLongLong_t)
2168 bool ToLongLong(wxLongLong_t *val, int base = 10) const;
2169 // convert to wxULongLong
2170 bool ToULongLong(wxULongLong_t *val, int base = 10) const;
2171 #endif // wxLongLong_t
2172 // convert to a double
2173 bool ToDouble(double *val) const;
2174
2175
2176 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2177 // formatted input/output
2178 // as sprintf(), returns the number of characters written or < 0 on error
2179 // (take 'this' into account in attribute parameter count)
2180 // int Printf(const wxString& format, ...);
2181 WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const wxFormatString&),
2182 DoPrintfWchar, DoPrintfUtf8)
2183 #ifdef __WATCOMC__
2184 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
2185 WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wxString&),
2186 (wxFormatString(f1)));
2187 WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wxCStrData&),
2188 (wxFormatString(f1)));
2189 WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const char*),
2190 (wxFormatString(f1)));
2191 WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wchar_t*),
2192 (wxFormatString(f1)));
2193 #endif
2194 #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN
2195 // as vprintf(), returns the number of characters written or < 0 on error
2196 int PrintfV(const wxString& format, va_list argptr);
2197
2198 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2199 // returns the string containing the result of Printf() to it
2200 // static wxString Format(const wxString& format, ...) ATTRIBUTE_PRINTF_1;
2201 WX_DEFINE_VARARG_FUNC(static wxString, Format, 1, (const wxFormatString&),
2202 DoFormatWchar, DoFormatUtf8)
2203 #ifdef __WATCOMC__
2204 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
2205 WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wxString&),
2206 (wxFormatString(f1)));
2207 WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wxCStrData&),
2208 (wxFormatString(f1)));
2209 WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const char*),
2210 (wxFormatString(f1)));
2211 WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wchar_t*),
2212 (wxFormatString(f1)));
2213 #endif
2214 #endif
2215 // the same as above, but takes a va_list
2216 static wxString FormatV(const wxString& format, va_list argptr);
2217
2218 // raw access to string memory
2219 // ensure that string has space for at least nLen characters
2220 // only works if the data of this string is not shared
2221 bool Alloc(size_t nLen) { reserve(nLen); return capacity() >= nLen; }
2222 // minimize the string's memory
2223 // only works if the data of this string is not shared
2224 bool Shrink();
2225 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2226 // These are deprecated, use wxStringBuffer or wxStringBufferLength instead
2227 //
2228 // get writable buffer of at least nLen bytes. Unget() *must* be called
2229 // a.s.a.p. to put string back in a reasonable state!
2230 wxDEPRECATED( wxStringCharType *GetWriteBuf(size_t nLen) );
2231 // call this immediately after GetWriteBuf() has been used
2232 wxDEPRECATED( void UngetWriteBuf() );
2233 wxDEPRECATED( void UngetWriteBuf(size_t nLen) );
2234 #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && wxUSE_UNICODE_UTF8
2235
2236 // wxWidgets version 1 compatibility functions
2237
2238 // use Mid()
2239 wxString SubString(size_t from, size_t to) const
2240 { return Mid(from, (to - from + 1)); }
2241 // values for second parameter of CompareTo function
2242 enum caseCompare {exact, ignoreCase};
2243 // values for first parameter of Strip function
2244 enum stripType {leading = 0x1, trailing = 0x2, both = 0x3};
2245
2246 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2247 // use Printf()
2248 // (take 'this' into account in attribute parameter count)
2249 // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2;
2250 WX_DEFINE_VARARG_FUNC(int, sprintf, 1, (const wxFormatString&),
2251 DoPrintfWchar, DoPrintfUtf8)
2252 #ifdef __WATCOMC__
2253 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
2254 WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wxString&),
2255 (wxFormatString(f1)));
2256 WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wxCStrData&),
2257 (wxFormatString(f1)));
2258 WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const char*),
2259 (wxFormatString(f1)));
2260 WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wchar_t*),
2261 (wxFormatString(f1)));
2262 #endif
2263 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
2264
2265 // use Cmp()
2266 int CompareTo(const wxChar* psz, caseCompare cmp = exact) const
2267 { return cmp == exact ? Cmp(psz) : CmpNoCase(psz); }
2268
2269 // use length()
2270 size_t Length() const { return length(); }
2271 // Count the number of characters
2272 int Freq(wxUniChar ch) const;
2273 // use MakeLower
2274 void LowerCase() { MakeLower(); }
2275 // use MakeUpper
2276 void UpperCase() { MakeUpper(); }
2277 // use Trim except that it doesn't change this string
2278 wxString Strip(stripType w = trailing) const;
2279
2280 // use Find (more general variants not yet supported)
2281 size_t Index(const wxChar* psz) const { return Find(psz); }
2282 size_t Index(wxUniChar ch) const { return Find(ch); }
2283 // use Truncate
2284 wxString& Remove(size_t pos) { return Truncate(pos); }
2285 wxString& RemoveLast(size_t n = 1) { return Truncate(length() - n); }
2286
2287 wxString& Remove(size_t nStart, size_t nLen)
2288 { return (wxString&)erase( nStart, nLen ); }
2289
2290 // use Find()
2291 int First( wxUniChar ch ) const { return Find(ch); }
2292 int First( wxUniCharRef ch ) const { return Find(ch); }
2293 int First( char ch ) const { return Find(ch); }
2294 int First( unsigned char ch ) const { return Find(ch); }
2295 int First( wchar_t ch ) const { return Find(ch); }
2296 int First( const wxString& str ) const { return Find(str); }
2297 int Last( wxUniChar ch ) const { return Find(ch, true); }
2298 bool Contains(const wxString& str) const { return Find(str) != wxNOT_FOUND; }
2299
2300 // use empty()
2301 bool IsNull() const { return empty(); }
2302
2303 // std::string compatibility functions
2304
2305 // take nLen chars starting at nPos
2306 wxString(const wxString& str, size_t nPos, size_t nLen)
2307 { assign(str, nPos, nLen); }
2308 // take all characters from first to last
2309 wxString(const_iterator first, const_iterator last)
2310 : m_impl(first.impl(), last.impl()) { }
2311 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2312 // the 2 overloads below are for compatibility with the existing code using
2313 // pointers instead of iterators
2314 wxString(const char *first, const char *last)
2315 {
2316 SubstrBufFromMB str(ImplStr(first, last - first));
2317 m_impl.assign(str.data, str.len);
2318 }
2319 wxString(const wchar_t *first, const wchar_t *last)
2320 {
2321 SubstrBufFromWC str(ImplStr(first, last - first));
2322 m_impl.assign(str.data, str.len);
2323 }
2324 // and this one is needed to compile code adding offsets to c_str() result
2325 wxString(const wxCStrData& first, const wxCStrData& last)
2326 : m_impl(CreateConstIterator(first).impl(),
2327 CreateConstIterator(last).impl())
2328 {
2329 wxASSERT_MSG( first.m_str == last.m_str,
2330 _T("pointers must be into the same string") );
2331 }
2332 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2333
2334 // lib.string.modifiers
2335 // append elements str[pos], ..., str[pos+n]
2336 wxString& append(const wxString& str, size_t pos, size_t n)
2337 {
2338 wxSTRING_UPDATE_CACHED_LENGTH(n);
2339
2340 size_t from, len;
2341 str.PosLenToImpl(pos, n, &from, &len);
2342 m_impl.append(str.m_impl, from, len);
2343 return *this;
2344 }
2345 // append a string
2346 wxString& append(const wxString& str)
2347 {
2348 wxSTRING_UPDATE_CACHED_LENGTH(str.length());
2349
2350 m_impl.append(str.m_impl);
2351 return *this;
2352 }
2353
2354 // append first n (or all if n == npos) characters of sz
2355 wxString& append(const char *sz)
2356 {
2357 wxSTRING_INVALIDATE_CACHED_LENGTH();
2358
2359 m_impl.append(ImplStr(sz));
2360 return *this;
2361 }
2362
2363 wxString& append(const wchar_t *sz)
2364 {
2365 wxSTRING_INVALIDATE_CACHED_LENGTH();
2366
2367 m_impl.append(ImplStr(sz));
2368 return *this;
2369 }
2370
2371 wxString& append(const char *sz, size_t n)
2372 {
2373 wxSTRING_INVALIDATE_CACHED_LENGTH();
2374
2375 SubstrBufFromMB str(ImplStr(sz, n));
2376 m_impl.append(str.data, str.len);
2377 return *this;
2378 }
2379 wxString& append(const wchar_t *sz, size_t n)
2380 {
2381 wxSTRING_UPDATE_CACHED_LENGTH(n);
2382
2383 SubstrBufFromWC str(ImplStr(sz, n));
2384 m_impl.append(str.data, str.len);
2385 return *this;
2386 }
2387
2388 wxString& append(const wxCStrData& str)
2389 { return append(str.AsString()); }
2390 wxString& append(const wxCharBuffer& str)
2391 { return append(str.data()); }
2392 wxString& append(const wxWCharBuffer& str)
2393 { return append(str.data()); }
2394 wxString& append(const wxCStrData& str, size_t n)
2395 { return append(str.AsString(), 0, n); }
2396 wxString& append(const wxCharBuffer& str, size_t n)
2397 { return append(str.data(), n); }
2398 wxString& append(const wxWCharBuffer& str, size_t n)
2399 { return append(str.data(), n); }
2400
2401 // append n copies of ch
2402 wxString& append(size_t n, wxUniChar ch)
2403 {
2404 #if wxUSE_UNICODE_UTF8
2405 if ( !ch.IsAscii() )
2406 {
2407 wxSTRING_INVALIDATE_CACHED_LENGTH();
2408
2409 m_impl.append(wxStringOperations::EncodeNChars(n, ch));
2410 }
2411 else // ASCII
2412 #endif
2413 {
2414 wxSTRING_UPDATE_CACHED_LENGTH(n);
2415
2416 m_impl.append(n, (wxStringCharType)ch);
2417 }
2418
2419 return *this;
2420 }
2421
2422 wxString& append(size_t n, wxUniCharRef ch)
2423 { return append(n, wxUniChar(ch)); }
2424 wxString& append(size_t n, char ch)
2425 { return append(n, wxUniChar(ch)); }
2426 wxString& append(size_t n, unsigned char ch)
2427 { return append(n, wxUniChar(ch)); }
2428 wxString& append(size_t n, wchar_t ch)
2429 { return append(n, wxUniChar(ch)); }
2430
2431 // append from first to last
2432 wxString& append(const_iterator first, const_iterator last)
2433 {
2434 wxSTRING_INVALIDATE_CACHED_LENGTH();
2435
2436 m_impl.append(first.impl(), last.impl());
2437 return *this;
2438 }
2439 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2440 wxString& append(const char *first, const char *last)
2441 { return append(first, last - first); }
2442 wxString& append(const wchar_t *first, const wchar_t *last)
2443 { return append(first, last - first); }
2444 wxString& append(const wxCStrData& first, const wxCStrData& last)
2445 { return append(CreateConstIterator(first), CreateConstIterator(last)); }
2446 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2447
2448 // same as `this_string = str'
2449 wxString& assign(const wxString& str)
2450 {
2451 wxSTRING_SET_CACHED_LENGTH(str.length());
2452
2453 m_impl = str.m_impl;
2454
2455 return *this;
2456 }
2457
2458 wxString& assign(const wxString& str, size_t len)
2459 {
2460 wxSTRING_SET_CACHED_LENGTH(len);
2461
2462 m_impl.assign(str.m_impl, 0, str.LenToImpl(len));
2463
2464 return *this;
2465 }
2466
2467 // same as ` = str[pos..pos + n]
2468 wxString& assign(const wxString& str, size_t pos, size_t n)
2469 {
2470 size_t from, len;
2471 str.PosLenToImpl(pos, n, &from, &len);
2472 m_impl.assign(str.m_impl, from, len);
2473
2474 // it's important to call this after PosLenToImpl() above in case str is
2475 // the same string as this one
2476 wxSTRING_SET_CACHED_LENGTH(n);
2477
2478 return *this;
2479 }
2480
2481 // same as `= first n (or all if n == npos) characters of sz'
2482 wxString& assign(const char *sz)
2483 {
2484 wxSTRING_INVALIDATE_CACHE();
2485
2486 m_impl.assign(ImplStr(sz));
2487
2488 return *this;
2489 }
2490
2491 wxString& assign(const wchar_t *sz)
2492 {
2493 wxSTRING_INVALIDATE_CACHE();
2494
2495 m_impl.assign(ImplStr(sz));
2496
2497 return *this;
2498 }
2499
2500 wxString& assign(const char *sz, size_t n)
2501 {
2502 wxSTRING_SET_CACHED_LENGTH(n);
2503
2504 SubstrBufFromMB str(ImplStr(sz, n));
2505 m_impl.assign(str.data, str.len);
2506
2507 return *this;
2508 }
2509
2510 wxString& assign(const wchar_t *sz, size_t n)
2511 {
2512 wxSTRING_SET_CACHED_LENGTH(n);
2513
2514 SubstrBufFromWC str(ImplStr(sz, n));
2515 m_impl.assign(str.data, str.len);
2516
2517 return *this;
2518 }
2519
2520 wxString& assign(const wxCStrData& str)
2521 { return assign(str.AsString()); }
2522 wxString& assign(const wxCharBuffer& str)
2523 { return assign(str.data()); }
2524 wxString& assign(const wxWCharBuffer& str)
2525 { return assign(str.data()); }
2526 wxString& assign(const wxCStrData& str, size_t len)
2527 { return assign(str.AsString(), len); }
2528 wxString& assign(const wxCharBuffer& str, size_t len)
2529 { return assign(str.data(), len); }
2530 wxString& assign(const wxWCharBuffer& str, size_t len)
2531 { return assign(str.data(), len); }
2532
2533 // same as `= n copies of ch'
2534 wxString& assign(size_t n, wxUniChar ch)
2535 {
2536 wxSTRING_SET_CACHED_LENGTH(n);
2537
2538 #if wxUSE_UNICODE_UTF8
2539 if ( !ch.IsAscii() )
2540 m_impl.assign(wxStringOperations::EncodeNChars(n, ch));
2541 else
2542 #endif
2543 m_impl.assign(n, (wxStringCharType)ch);
2544
2545 return *this;
2546 }
2547
2548 wxString& assign(size_t n, wxUniCharRef ch)
2549 { return assign(n, wxUniChar(ch)); }
2550 wxString& assign(size_t n, char ch)
2551 { return assign(n, wxUniChar(ch)); }
2552 wxString& assign(size_t n, unsigned char ch)
2553 { return assign(n, wxUniChar(ch)); }
2554 wxString& assign(size_t n, wchar_t ch)
2555 { return assign(n, wxUniChar(ch)); }
2556
2557 // assign from first to last
2558 wxString& assign(const_iterator first, const_iterator last)
2559 {
2560 wxSTRING_INVALIDATE_CACHE();
2561
2562 m_impl.assign(first.impl(), last.impl());
2563
2564 return *this;
2565 }
2566 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2567 wxString& assign(const char *first, const char *last)
2568 { return assign(first, last - first); }
2569 wxString& assign(const wchar_t *first, const wchar_t *last)
2570 { return assign(first, last - first); }
2571 wxString& assign(const wxCStrData& first, const wxCStrData& last)
2572 { return assign(CreateConstIterator(first), CreateConstIterator(last)); }
2573 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2574
2575 // string comparison
2576 int compare(const wxString& str) const;
2577 int compare(const char* sz) const;
2578 int compare(const wchar_t* sz) const;
2579 int compare(const wxCStrData& str) const
2580 { return compare(str.AsString()); }
2581 int compare(const wxCharBuffer& str) const
2582 { return compare(str.data()); }
2583 int compare(const wxWCharBuffer& str) const
2584 { return compare(str.data()); }
2585 // comparison with a substring
2586 int compare(size_t nStart, size_t nLen, const wxString& str) const;
2587 // comparison of 2 substrings
2588 int compare(size_t nStart, size_t nLen,
2589 const wxString& str, size_t nStart2, size_t nLen2) const;
2590 // substring comparison with first nCount characters of sz
2591 int compare(size_t nStart, size_t nLen,
2592 const char* sz, size_t nCount = npos) const;
2593 int compare(size_t nStart, size_t nLen,
2594 const wchar_t* sz, size_t nCount = npos) const;
2595
2596 // insert another string
2597 wxString& insert(size_t nPos, const wxString& str)
2598 { insert(GetIterForNthChar(nPos), str.begin(), str.end()); return *this; }
2599 // insert n chars of str starting at nStart (in str)
2600 wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n)
2601 {
2602 wxSTRING_UPDATE_CACHED_LENGTH(n);
2603
2604 size_t from, len;
2605 str.PosLenToImpl(nStart, n, &from, &len);
2606 m_impl.insert(PosToImpl(nPos), str.m_impl, from, len);
2607
2608 return *this;
2609 }
2610
2611 // insert first n (or all if n == npos) characters of sz
2612 wxString& insert(size_t nPos, const char *sz)
2613 {
2614 wxSTRING_INVALIDATE_CACHE();
2615
2616 m_impl.insert(PosToImpl(nPos), ImplStr(sz));
2617
2618 return *this;
2619 }
2620
2621 wxString& insert(size_t nPos, const wchar_t *sz)
2622 {
2623 wxSTRING_INVALIDATE_CACHE();
2624
2625 m_impl.insert(PosToImpl(nPos), ImplStr(sz)); return *this;
2626 }
2627
2628 wxString& insert(size_t nPos, const char *sz, size_t n)
2629 {
2630 wxSTRING_UPDATE_CACHED_LENGTH(n);
2631
2632 SubstrBufFromMB str(ImplStr(sz, n));
2633 m_impl.insert(PosToImpl(nPos), str.data, str.len);
2634
2635 return *this;
2636 }
2637
2638 wxString& insert(size_t nPos, const wchar_t *sz, size_t n)
2639 {
2640 wxSTRING_UPDATE_CACHED_LENGTH(n);
2641
2642 SubstrBufFromWC str(ImplStr(sz, n));
2643 m_impl.insert(PosToImpl(nPos), str.data, str.len);
2644
2645 return *this;
2646 }
2647
2648 // insert n copies of ch
2649 wxString& insert(size_t nPos, size_t n, wxUniChar ch)
2650 {
2651 wxSTRING_UPDATE_CACHED_LENGTH(n);
2652
2653 #if wxUSE_UNICODE_UTF8
2654 if ( !ch.IsAscii() )
2655 m_impl.insert(PosToImpl(nPos), wxStringOperations::EncodeNChars(n, ch));
2656 else
2657 #endif
2658 m_impl.insert(PosToImpl(nPos), n, (wxStringCharType)ch);
2659 return *this;
2660 }
2661
2662 iterator insert(iterator it, wxUniChar ch)
2663 {
2664 wxSTRING_UPDATE_CACHED_LENGTH(1);
2665
2666 #if wxUSE_UNICODE_UTF8
2667 if ( !ch.IsAscii() )
2668 {
2669 size_t pos = IterToImplPos(it);
2670 m_impl.insert(pos, wxStringOperations::EncodeChar(ch));
2671 return iterator(this, m_impl.begin() + pos);
2672 }
2673 else
2674 #endif
2675 return iterator(this, m_impl.insert(it.impl(), (wxStringCharType)ch));
2676 }
2677
2678 void insert(iterator it, const_iterator first, const_iterator last)
2679 {
2680 wxSTRING_INVALIDATE_CACHE();
2681
2682 m_impl.insert(it.impl(), first.impl(), last.impl());
2683 }
2684
2685 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2686 void insert(iterator it, const char *first, const char *last)
2687 { insert(it - begin(), first, last - first); }
2688 void insert(iterator it, const wchar_t *first, const wchar_t *last)
2689 { insert(it - begin(), first, last - first); }
2690 void insert(iterator it, const wxCStrData& first, const wxCStrData& last)
2691 { insert(it, CreateConstIterator(first), CreateConstIterator(last)); }
2692 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2693
2694 void insert(iterator it, size_type n, wxUniChar ch)
2695 {
2696 wxSTRING_UPDATE_CACHED_LENGTH(n);
2697
2698 #if wxUSE_UNICODE_UTF8
2699 if ( !ch.IsAscii() )
2700 m_impl.insert(IterToImplPos(it), wxStringOperations::EncodeNChars(n, ch));
2701 else
2702 #endif
2703 m_impl.insert(it.impl(), n, (wxStringCharType)ch);
2704 }
2705
2706 // delete characters from nStart to nStart + nLen
2707 wxString& erase(size_type pos = 0, size_type n = npos)
2708 {
2709 wxSTRING_INVALIDATE_CACHE();
2710
2711 size_t from, len;
2712 PosLenToImpl(pos, n, &from, &len);
2713 m_impl.erase(from, len);
2714
2715 return *this;
2716 }
2717
2718 // delete characters from first up to last
2719 iterator erase(iterator first, iterator last)
2720 {
2721 wxSTRING_INVALIDATE_CACHE();
2722
2723 return iterator(this, m_impl.erase(first.impl(), last.impl()));
2724 }
2725
2726 iterator erase(iterator first)
2727 {
2728 wxSTRING_UPDATE_CACHED_LENGTH(-1);
2729
2730 return iterator(this, m_impl.erase(first.impl()));
2731 }
2732
2733 #ifdef wxSTRING_BASE_HASNT_CLEAR
2734 void clear() { erase(); }
2735 #else
2736 void clear()
2737 {
2738 wxSTRING_SET_CACHED_LENGTH(0);
2739
2740 m_impl.clear();
2741 }
2742 #endif
2743
2744 // replaces the substring of length nLen starting at nStart
2745 wxString& replace(size_t nStart, size_t nLen, const char* sz)
2746 {
2747 wxSTRING_INVALIDATE_CACHE();
2748
2749 size_t from, len;
2750 PosLenToImpl(nStart, nLen, &from, &len);
2751 m_impl.replace(from, len, ImplStr(sz));
2752
2753 return *this;
2754 }
2755
2756 wxString& replace(size_t nStart, size_t nLen, const wchar_t* sz)
2757 {
2758 wxSTRING_INVALIDATE_CACHE();
2759
2760 size_t from, len;
2761 PosLenToImpl(nStart, nLen, &from, &len);
2762 m_impl.replace(from, len, ImplStr(sz));
2763
2764 return *this;
2765 }
2766
2767 // replaces the substring of length nLen starting at nStart
2768 wxString& replace(size_t nStart, size_t nLen, const wxString& str)
2769 {
2770 wxSTRING_INVALIDATE_CACHE();
2771
2772 size_t from, len;
2773 PosLenToImpl(nStart, nLen, &from, &len);
2774 m_impl.replace(from, len, str.m_impl);
2775
2776 return *this;
2777 }
2778
2779 // replaces the substring with nCount copies of ch
2780 wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxUniChar ch)
2781 {
2782 wxSTRING_INVALIDATE_CACHE();
2783
2784 size_t from, len;
2785 PosLenToImpl(nStart, nLen, &from, &len);
2786 #if wxUSE_UNICODE_UTF8
2787 if ( !ch.IsAscii() )
2788 m_impl.replace(from, len, wxStringOperations::EncodeNChars(nCount, ch));
2789 else
2790 #endif
2791 m_impl.replace(from, len, nCount, (wxStringCharType)ch);
2792
2793 return *this;
2794 }
2795
2796 // replaces a substring with another substring
2797 wxString& replace(size_t nStart, size_t nLen,
2798 const wxString& str, size_t nStart2, size_t nLen2)
2799 {
2800 wxSTRING_INVALIDATE_CACHE();
2801
2802 size_t from, len;
2803 PosLenToImpl(nStart, nLen, &from, &len);
2804
2805 size_t from2, len2;
2806 str.PosLenToImpl(nStart2, nLen2, &from2, &len2);
2807
2808 m_impl.replace(from, len, str.m_impl, from2, len2);
2809
2810 return *this;
2811 }
2812
2813 // replaces the substring with first nCount chars of sz
2814 wxString& replace(size_t nStart, size_t nLen,
2815 const char* sz, size_t nCount)
2816 {
2817 wxSTRING_INVALIDATE_CACHE();
2818
2819 size_t from, len;
2820 PosLenToImpl(nStart, nLen, &from, &len);
2821
2822 SubstrBufFromMB str(ImplStr(sz, nCount));
2823
2824 m_impl.replace(from, len, str.data, str.len);
2825
2826 return *this;
2827 }
2828
2829 wxString& replace(size_t nStart, size_t nLen,
2830 const wchar_t* sz, size_t nCount)
2831 {
2832 wxSTRING_INVALIDATE_CACHE();
2833
2834 size_t from, len;
2835 PosLenToImpl(nStart, nLen, &from, &len);
2836
2837 SubstrBufFromWC str(ImplStr(sz, nCount));
2838
2839 m_impl.replace(from, len, str.data, str.len);
2840
2841 return *this;
2842 }
2843
2844 wxString& replace(size_t nStart, size_t nLen,
2845 const wxString& s, size_t nCount)
2846 {
2847 wxSTRING_INVALIDATE_CACHE();
2848
2849 size_t from, len;
2850 PosLenToImpl(nStart, nLen, &from, &len);
2851 m_impl.replace(from, len, s.m_impl.c_str(), s.LenToImpl(nCount));
2852
2853 return *this;
2854 }
2855
2856 wxString& replace(iterator first, iterator last, const char* s)
2857 {
2858 wxSTRING_INVALIDATE_CACHE();
2859
2860 m_impl.replace(first.impl(), last.impl(), ImplStr(s));
2861
2862 return *this;
2863 }
2864
2865 wxString& replace(iterator first, iterator last, const wchar_t* s)
2866 {
2867 wxSTRING_INVALIDATE_CACHE();
2868
2869 m_impl.replace(first.impl(), last.impl(), ImplStr(s));
2870
2871 return *this;
2872 }
2873
2874 wxString& replace(iterator first, iterator last, const char* s, size_type n)
2875 {
2876 wxSTRING_INVALIDATE_CACHE();
2877
2878 SubstrBufFromMB str(ImplStr(s, n));
2879 m_impl.replace(first.impl(), last.impl(), str.data, str.len);
2880
2881 return *this;
2882 }
2883
2884 wxString& replace(iterator first, iterator last, const wchar_t* s, size_type n)
2885 {
2886 wxSTRING_INVALIDATE_CACHE();
2887
2888 SubstrBufFromWC str(ImplStr(s, n));
2889 m_impl.replace(first.impl(), last.impl(), str.data, str.len);
2890
2891 return *this;
2892 }
2893
2894 wxString& replace(iterator first, iterator last, const wxString& s)
2895 {
2896 wxSTRING_INVALIDATE_CACHE();
2897
2898 m_impl.replace(first.impl(), last.impl(), s.m_impl);
2899
2900 return *this;
2901 }
2902
2903 wxString& replace(iterator first, iterator last, size_type n, wxUniChar ch)
2904 {
2905 wxSTRING_INVALIDATE_CACHE();
2906
2907 #if wxUSE_UNICODE_UTF8
2908 if ( !ch.IsAscii() )
2909 m_impl.replace(first.impl(), last.impl(),
2910 wxStringOperations::EncodeNChars(n, ch));
2911 else
2912 #endif
2913 m_impl.replace(first.impl(), last.impl(), n, (wxStringCharType)ch);
2914
2915 return *this;
2916 }
2917
2918 wxString& replace(iterator first, iterator last,
2919 const_iterator first1, const_iterator last1)
2920 {
2921 wxSTRING_INVALIDATE_CACHE();
2922
2923 m_impl.replace(first.impl(), last.impl(), first1.impl(), last1.impl());
2924
2925 return *this;
2926 }
2927
2928 wxString& replace(iterator first, iterator last,
2929 const char *first1, const char *last1)
2930 { replace(first, last, first1, last1 - first1); return *this; }
2931 wxString& replace(iterator first, iterator last,
2932 const wchar_t *first1, const wchar_t *last1)
2933 { replace(first, last, first1, last1 - first1); return *this; }
2934
2935 // swap two strings
2936 void swap(wxString& str)
2937 {
2938 #if wxUSE_STRING_POS_CACHE
2939 // we modify not only this string but also the other one directly so we
2940 // need to invalidate cache for both of them (we could also try to
2941 // exchange their cache entries but it seems unlikely to be worth it)
2942 InvalidateCache();
2943 str.InvalidateCache();
2944 #endif // wxUSE_STRING_POS_CACHE
2945
2946 m_impl.swap(str.m_impl);
2947 }
2948
2949 // find a substring
2950 size_t find(const wxString& str, size_t nStart = 0) const
2951 { return PosFromImpl(m_impl.find(str.m_impl, PosToImpl(nStart))); }
2952
2953 // find first n characters of sz
2954 size_t find(const char* sz, size_t nStart = 0, size_t n = npos) const
2955 {
2956 SubstrBufFromMB str(ImplStr(sz, n));
2957 return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len));
2958 }
2959 size_t find(const wchar_t* sz, size_t nStart = 0, size_t n = npos) const
2960 {
2961 SubstrBufFromWC str(ImplStr(sz, n));
2962 return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len));
2963 }
2964 size_t find(const wxCharBuffer& s, size_t nStart = 0, size_t n = npos) const
2965 { return find(s.data(), nStart, n); }
2966 size_t find(const wxWCharBuffer& s, size_t nStart = 0, size_t n = npos) const
2967 { return find(s.data(), nStart, n); }
2968 size_t find(const wxCStrData& s, size_t nStart = 0, size_t n = npos) const
2969 { return find(s.AsWChar(), nStart, n); }
2970
2971 // find the first occurence of character ch after nStart
2972 size_t find(wxUniChar ch, size_t nStart = 0) const
2973 {
2974 #if wxUSE_UNICODE_UTF8
2975 if ( !ch.IsAscii() )
2976 return PosFromImpl(m_impl.find(wxStringOperations::EncodeChar(ch),
2977 PosToImpl(nStart)));
2978 else
2979 #endif
2980 return PosFromImpl(m_impl.find((wxStringCharType)ch,
2981 PosToImpl(nStart)));
2982
2983 }
2984 size_t find(wxUniCharRef ch, size_t nStart = 0) const
2985 { return find(wxUniChar(ch), nStart); }
2986 size_t find(char ch, size_t nStart = 0) const
2987 { return find(wxUniChar(ch), nStart); }
2988 size_t find(unsigned char ch, size_t nStart = 0) const
2989 { return find(wxUniChar(ch), nStart); }
2990 size_t find(wchar_t ch, size_t nStart = 0) const
2991 { return find(wxUniChar(ch), nStart); }
2992
2993 // rfind() family is exactly like find() but works right to left
2994
2995 // as find, but from the end
2996 size_t rfind(const wxString& str, size_t nStart = npos) const
2997 { return PosFromImpl(m_impl.rfind(str.m_impl, PosToImpl(nStart))); }
2998
2999 // as find, but from the end
3000 size_t rfind(const char* sz, size_t nStart = npos, size_t n = npos) const
3001 {
3002 SubstrBufFromMB str(ImplStr(sz, n));
3003 return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len));
3004 }
3005 size_t rfind(const wchar_t* sz, size_t nStart = npos, size_t n = npos) const
3006 {
3007 SubstrBufFromWC str(ImplStr(sz, n));
3008 return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len));
3009 }
3010 size_t rfind(const wxCharBuffer& s, size_t nStart = npos, size_t n = npos) const
3011 { return rfind(s.data(), nStart, n); }
3012 size_t rfind(const wxWCharBuffer& s, size_t nStart = npos, size_t n = npos) const
3013 { return rfind(s.data(), nStart, n); }
3014 size_t rfind(const wxCStrData& s, size_t nStart = npos, size_t n = npos) const
3015 { return rfind(s.AsWChar(), nStart, n); }
3016 // as find, but from the end
3017 size_t rfind(wxUniChar ch, size_t nStart = npos) const
3018 {
3019 #if wxUSE_UNICODE_UTF8
3020 if ( !ch.IsAscii() )
3021 return PosFromImpl(m_impl.rfind(wxStringOperations::EncodeChar(ch),
3022 PosToImpl(nStart)));
3023 else
3024 #endif
3025 return PosFromImpl(m_impl.rfind((wxStringCharType)ch,
3026 PosToImpl(nStart)));
3027 }
3028 size_t rfind(wxUniCharRef ch, size_t nStart = npos) const
3029 { return rfind(wxUniChar(ch), nStart); }
3030 size_t rfind(char ch, size_t nStart = npos) const
3031 { return rfind(wxUniChar(ch), nStart); }
3032 size_t rfind(unsigned char ch, size_t nStart = npos) const
3033 { return rfind(wxUniChar(ch), nStart); }
3034 size_t rfind(wchar_t ch, size_t nStart = npos) const
3035 { return rfind(wxUniChar(ch), nStart); }
3036
3037 // find first/last occurence of any character (not) in the set:
3038 #if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
3039 // FIXME-UTF8: this is not entirely correct, because it doesn't work if
3040 // sizeof(wchar_t)==2 and surrogates are present in the string;
3041 // should we care? Probably not.
3042 size_t find_first_of(const wxString& str, size_t nStart = 0) const
3043 { return m_impl.find_first_of(str.m_impl, nStart); }
3044 size_t find_first_of(const char* sz, size_t nStart = 0) const
3045 { return m_impl.find_first_of(ImplStr(sz), nStart); }
3046 size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const
3047 { return m_impl.find_first_of(ImplStr(sz), nStart); }
3048 size_t find_first_of(const char* sz, size_t nStart, size_t n) const
3049 { return m_impl.find_first_of(ImplStr(sz), nStart, n); }
3050 size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const
3051 { return m_impl.find_first_of(ImplStr(sz), nStart, n); }
3052 size_t find_first_of(wxUniChar c, size_t nStart = 0) const
3053 { return m_impl.find_first_of((wxChar)c, nStart); }
3054
3055 size_t find_last_of(const wxString& str, size_t nStart = npos) const
3056 { return m_impl.find_last_of(str.m_impl, nStart); }
3057 size_t find_last_of(const char* sz, size_t nStart = npos) const
3058 { return m_impl.find_last_of(ImplStr(sz), nStart); }
3059 size_t find_last_of(const wchar_t* sz, size_t nStart = npos) const
3060 { return m_impl.find_last_of(ImplStr(sz), nStart); }
3061 size_t find_last_of(const char* sz, size_t nStart, size_t n) const
3062 { return m_impl.find_last_of(ImplStr(sz), nStart, n); }
3063 size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const
3064 { return m_impl.find_last_of(ImplStr(sz), nStart, n); }
3065 size_t find_last_of(wxUniChar c, size_t nStart = npos) const
3066 { return m_impl.find_last_of((wxChar)c, nStart); }
3067
3068 size_t find_first_not_of(const wxString& str, size_t nStart = 0) const
3069 { return m_impl.find_first_not_of(str.m_impl, nStart); }
3070 size_t find_first_not_of(const char* sz, size_t nStart = 0) const
3071 { return m_impl.find_first_not_of(ImplStr(sz), nStart); }
3072 size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const
3073 { return m_impl.find_first_not_of(ImplStr(sz), nStart); }
3074 size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const
3075 { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); }
3076 size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const
3077 { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); }
3078 size_t find_first_not_of(wxUniChar c, size_t nStart = 0) const
3079 { return m_impl.find_first_not_of((wxChar)c, nStart); }
3080
3081 size_t find_last_not_of(const wxString& str, size_t nStart = npos) const
3082 { return m_impl.find_last_not_of(str.m_impl, nStart); }
3083 size_t find_last_not_of(const char* sz, size_t nStart = npos) const
3084 { return m_impl.find_last_not_of(ImplStr(sz), nStart); }
3085 size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const
3086 { return m_impl.find_last_not_of(ImplStr(sz), nStart); }
3087 size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const
3088 { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); }
3089 size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const
3090 { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); }
3091 size_t find_last_not_of(wxUniChar c, size_t nStart = npos) const
3092 { return m_impl.find_last_not_of((wxChar)c, nStart); }
3093 #else
3094 // we can't use std::string implementation in UTF-8 build, because the
3095 // character sets would be interpreted wrongly:
3096
3097 // as strpbrk() but starts at nStart, returns npos if not found
3098 size_t find_first_of(const wxString& str, size_t nStart = 0) const
3099 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3100 { return find_first_of(str.wc_str(), nStart); }
3101 #else
3102 { return find_first_of(str.mb_str(), nStart); }
3103 #endif
3104 // same as above
3105 size_t find_first_of(const char* sz, size_t nStart = 0) const;
3106 size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const;
3107 size_t find_first_of(const char* sz, size_t nStart, size_t n) const;
3108 size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const;
3109 // same as find(char, size_t)
3110 size_t find_first_of(wxUniChar c, size_t nStart = 0) const
3111 { return find(c, nStart); }
3112 // find the last (starting from nStart) char from str in this string
3113 size_t find_last_of (const wxString& str, size_t nStart = npos) const
3114 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3115 { return find_last_of(str.wc_str(), nStart); }
3116 #else
3117 { return find_last_of(str.mb_str(), nStart); }
3118 #endif
3119 // same as above
3120 size_t find_last_of (const char* sz, size_t nStart = npos) const;
3121 size_t find_last_of (const wchar_t* sz, size_t nStart = npos) const;
3122 size_t find_last_of(const char* sz, size_t nStart, size_t n) const;
3123 size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const;
3124 // same as above
3125 size_t find_last_of(wxUniChar c, size_t nStart = npos) const
3126 { return rfind(c, nStart); }
3127
3128 // find first/last occurence of any character not in the set
3129
3130 // as strspn() (starting from nStart), returns npos on failure
3131 size_t find_first_not_of(const wxString& str, size_t nStart = 0) const
3132 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3133 { return find_first_not_of(str.wc_str(), nStart); }
3134 #else
3135 { return find_first_not_of(str.mb_str(), nStart); }
3136 #endif
3137 // same as above
3138 size_t find_first_not_of(const char* sz, size_t nStart = 0) const;
3139 size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const;
3140 size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const;
3141 size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const;
3142 // same as above
3143 size_t find_first_not_of(wxUniChar ch, size_t nStart = 0) const;
3144 // as strcspn()
3145 size_t find_last_not_of(const wxString& str, size_t nStart = npos) const
3146 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3147 { return find_last_not_of(str.wc_str(), nStart); }
3148 #else
3149 { return find_last_not_of(str.mb_str(), nStart); }
3150 #endif
3151 // same as above
3152 size_t find_last_not_of(const char* sz, size_t nStart = npos) const;
3153 size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const;
3154 size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const;
3155 size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const;
3156 // same as above
3157 size_t find_last_not_of(wxUniChar ch, size_t nStart = npos) const;
3158 #endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not
3159
3160 // provide char/wchar_t/wxUniCharRef overloads for char-finding functions
3161 // above to resolve ambiguities:
3162 size_t find_first_of(wxUniCharRef ch, size_t nStart = 0) const
3163 { return find_first_of(wxUniChar(ch), nStart); }
3164 size_t find_first_of(char ch, size_t nStart = 0) const
3165 { return find_first_of(wxUniChar(ch), nStart); }
3166 size_t find_first_of(unsigned char ch, size_t nStart = 0) const
3167 { return find_first_of(wxUniChar(ch), nStart); }
3168 size_t find_first_of(wchar_t ch, size_t nStart = 0) const
3169 { return find_first_of(wxUniChar(ch), nStart); }
3170 size_t find_last_of(wxUniCharRef ch, size_t nStart = npos) const
3171 { return find_last_of(wxUniChar(ch), nStart); }
3172 size_t find_last_of(char ch, size_t nStart = npos) const
3173 { return find_last_of(wxUniChar(ch), nStart); }
3174 size_t find_last_of(unsigned char ch, size_t nStart = npos) const
3175 { return find_last_of(wxUniChar(ch), nStart); }
3176 size_t find_last_of(wchar_t ch, size_t nStart = npos) const
3177 { return find_last_of(wxUniChar(ch), nStart); }
3178 size_t find_first_not_of(wxUniCharRef ch, size_t nStart = 0) const
3179 { return find_first_not_of(wxUniChar(ch), nStart); }
3180 size_t find_first_not_of(char ch, size_t nStart = 0) const
3181 { return find_first_not_of(wxUniChar(ch), nStart); }
3182 size_t find_first_not_of(unsigned char ch, size_t nStart = 0) const
3183 { return find_first_not_of(wxUniChar(ch), nStart); }
3184 size_t find_first_not_of(wchar_t ch, size_t nStart = 0) const
3185 { return find_first_not_of(wxUniChar(ch), nStart); }
3186 size_t find_last_not_of(wxUniCharRef ch, size_t nStart = npos) const
3187 { return find_last_not_of(wxUniChar(ch), nStart); }
3188 size_t find_last_not_of(char ch, size_t nStart = npos) const
3189 { return find_last_not_of(wxUniChar(ch), nStart); }
3190 size_t find_last_not_of(unsigned char ch, size_t nStart = npos) const
3191 { return find_last_not_of(wxUniChar(ch), nStart); }
3192 size_t find_last_not_of(wchar_t ch, size_t nStart = npos) const
3193 { return find_last_not_of(wxUniChar(ch), nStart); }
3194
3195 // and additional overloads for the versions taking strings:
3196 size_t find_first_of(const wxCStrData& sz, size_t nStart = 0) const
3197 { return find_first_of(sz.AsString(), nStart); }
3198 size_t find_first_of(const wxCharBuffer& sz, size_t nStart = 0) const
3199 { return find_first_of(sz.data(), nStart); }
3200 size_t find_first_of(const wxWCharBuffer& sz, size_t nStart = 0) const
3201 { return find_first_of(sz.data(), nStart); }
3202 size_t find_first_of(const wxCStrData& sz, size_t nStart, size_t n) const
3203 { return find_first_of(sz.AsWChar(), nStart, n); }
3204 size_t find_first_of(const wxCharBuffer& sz, size_t nStart, size_t n) const
3205 { return find_first_of(sz.data(), nStart, n); }
3206 size_t find_first_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const
3207 { return find_first_of(sz.data(), nStart, n); }
3208
3209 size_t find_last_of(const wxCStrData& sz, size_t nStart = 0) const
3210 { return find_last_of(sz.AsString(), nStart); }
3211 size_t find_last_of(const wxCharBuffer& sz, size_t nStart = 0) const
3212 { return find_last_of(sz.data(), nStart); }
3213 size_t find_last_of(const wxWCharBuffer& sz, size_t nStart = 0) const
3214 { return find_last_of(sz.data(), nStart); }
3215 size_t find_last_of(const wxCStrData& sz, size_t nStart, size_t n) const
3216 { return find_last_of(sz.AsWChar(), nStart, n); }
3217 size_t find_last_of(const wxCharBuffer& sz, size_t nStart, size_t n) const
3218 { return find_last_of(sz.data(), nStart, n); }
3219 size_t find_last_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const
3220 { return find_last_of(sz.data(), nStart, n); }
3221
3222 size_t find_first_not_of(const wxCStrData& sz, size_t nStart = 0) const
3223 { return find_first_not_of(sz.AsString(), nStart); }
3224 size_t find_first_not_of(const wxCharBuffer& sz, size_t nStart = 0) const
3225 { return find_first_not_of(sz.data(), nStart); }
3226 size_t find_first_not_of(const wxWCharBuffer& sz, size_t nStart = 0) const
3227 { return find_first_not_of(sz.data(), nStart); }
3228 size_t find_first_not_of(const wxCStrData& sz, size_t nStart, size_t n) const
3229 { return find_first_not_of(sz.AsWChar(), nStart, n); }
3230 size_t find_first_not_of(const wxCharBuffer& sz, size_t nStart, size_t n) const
3231 { return find_first_not_of(sz.data(), nStart, n); }
3232 size_t find_first_not_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const
3233 { return find_first_not_of(sz.data(), nStart, n); }
3234
3235 size_t find_last_not_of(const wxCStrData& sz, size_t nStart = 0) const
3236 { return find_last_not_of(sz.AsString(), nStart); }
3237 size_t find_last_not_of(const wxCharBuffer& sz, size_t nStart = 0) const
3238 { return find_last_not_of(sz.data(), nStart); }
3239 size_t find_last_not_of(const wxWCharBuffer& sz, size_t nStart = 0) const
3240 { return find_last_not_of(sz.data(), nStart); }
3241 size_t find_last_not_of(const wxCStrData& sz, size_t nStart, size_t n) const
3242 { return find_last_not_of(sz.AsWChar(), nStart, n); }
3243 size_t find_last_not_of(const wxCharBuffer& sz, size_t nStart, size_t n) const
3244 { return find_last_not_of(sz.data(), nStart, n); }
3245 size_t find_last_not_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const
3246 { return find_last_not_of(sz.data(), nStart, n); }
3247
3248 // string += string
3249 wxString& operator+=(const wxString& s)
3250 {
3251 wxSTRING_INVALIDATE_CACHED_LENGTH();
3252
3253 m_impl += s.m_impl;
3254 return *this;
3255 }
3256 // string += C string
3257 wxString& operator+=(const char *psz)
3258 {
3259 wxSTRING_INVALIDATE_CACHED_LENGTH();
3260
3261 m_impl += ImplStr(psz);
3262 return *this;
3263 }
3264 wxString& operator+=(const wchar_t *pwz)
3265 {
3266 wxSTRING_INVALIDATE_CACHED_LENGTH();
3267
3268 m_impl += ImplStr(pwz);
3269 return *this;
3270 }
3271 wxString& operator+=(const wxCStrData& s)
3272 {
3273 wxSTRING_INVALIDATE_CACHED_LENGTH();
3274
3275 m_impl += s.AsString().m_impl;
3276 return *this;
3277 }
3278 wxString& operator+=(const wxCharBuffer& s)
3279 { return operator+=(s.data()); }
3280 wxString& operator+=(const wxWCharBuffer& s)
3281 { return operator+=(s.data()); }
3282 // string += char
3283 wxString& operator+=(wxUniChar ch)
3284 {
3285 wxSTRING_UPDATE_CACHED_LENGTH(1);
3286
3287 #if wxUSE_UNICODE_UTF8
3288 if ( !ch.IsAscii() )
3289 m_impl += wxStringOperations::EncodeChar(ch);
3290 else
3291 #endif
3292 m_impl += (wxStringCharType)ch;
3293 return *this;
3294 }
3295 wxString& operator+=(wxUniCharRef ch) { return *this += wxUniChar(ch); }
3296 wxString& operator+=(int ch) { return *this += wxUniChar(ch); }
3297 wxString& operator+=(char ch) { return *this += wxUniChar(ch); }
3298 wxString& operator+=(unsigned char ch) { return *this += wxUniChar(ch); }
3299 wxString& operator+=(wchar_t ch) { return *this += wxUniChar(ch); }
3300
3301 private:
3302 #if !wxUSE_STL_BASED_WXSTRING
3303 // helpers for wxStringBuffer and wxStringBufferLength
3304 wxStringCharType *DoGetWriteBuf(size_t nLen)
3305 {
3306 return m_impl.DoGetWriteBuf(nLen);
3307 }
3308
3309 void DoUngetWriteBuf()
3310 {
3311 wxSTRING_INVALIDATE_CACHE();
3312
3313 m_impl.DoUngetWriteBuf();
3314 }
3315
3316 void DoUngetWriteBuf(size_t nLen)
3317 {
3318 wxSTRING_SET_CACHED_LENGTH(nLen);
3319
3320 m_impl.DoUngetWriteBuf(nLen);
3321 }
3322 #endif // !wxUSE_STL_BASED_WXSTRING
3323
3324 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
3325 #if !wxUSE_UTF8_LOCALE_ONLY
3326 int DoPrintfWchar(const wxChar *format, ...);
3327 static wxString DoFormatWchar(const wxChar *format, ...);
3328 #endif
3329 #if wxUSE_UNICODE_UTF8
3330 int DoPrintfUtf8(const char *format, ...);
3331 static wxString DoFormatUtf8(const char *format, ...);
3332 #endif
3333 #endif
3334
3335 #if !wxUSE_STL_BASED_WXSTRING
3336 // check string's data validity
3337 bool IsValid() const { return m_impl.GetStringData()->IsValid(); }
3338 #endif
3339
3340 private:
3341 wxStringImpl m_impl;
3342
3343 // buffers for compatibility conversion from (char*)c_str() and
3344 // (wchar_t*)c_str():
3345 // FIXME-UTF8: bechmark various approaches to keeping compatibility buffers
3346 template<typename T>
3347 struct ConvertedBuffer
3348 {
3349 ConvertedBuffer() : m_buf(NULL) {}
3350 ~ConvertedBuffer()
3351 { free(m_buf); }
3352
3353 operator T*() const { return m_buf; }
3354
3355 ConvertedBuffer& operator=(T *str)
3356 {
3357 free(m_buf);
3358 m_buf = str;
3359 return *this;
3360 }
3361
3362 T *m_buf;
3363 };
3364 #if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
3365 ConvertedBuffer<char> m_convertedToChar;
3366 #endif
3367 #if !wxUSE_UNICODE_WCHAR
3368 ConvertedBuffer<wchar_t> m_convertedToWChar;
3369 #endif
3370
3371 #if wxUSE_UNICODE_UTF8
3372 // FIXME-UTF8: (try to) move this elsewhere (TLS) or solve differently
3373 // assigning to character pointer to by wxString::interator may
3374 // change the underlying wxStringImpl iterator, so we have to
3375 // keep track of all iterators and update them as necessary:
3376 struct wxStringIteratorNodeHead
3377 {
3378 wxStringIteratorNodeHead() : ptr(NULL) {}
3379 wxStringIteratorNode *ptr;
3380
3381 // copying is disallowed as it would result in more than one pointer into
3382 // the same linked list
3383 DECLARE_NO_COPY_CLASS(wxStringIteratorNodeHead)
3384 };
3385
3386 wxStringIteratorNodeHead m_iterators;
3387
3388 friend class WXDLLIMPEXP_FWD_BASE wxStringIteratorNode;
3389 friend class WXDLLIMPEXP_FWD_BASE wxUniCharRef;
3390 #endif // wxUSE_UNICODE_UTF8
3391
3392 friend class WXDLLIMPEXP_FWD_BASE wxCStrData;
3393 friend class wxStringInternalBuffer;
3394 friend class wxStringInternalBufferLength;
3395 };
3396
3397 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
3398 #pragma warning (default:4275)
3399 #endif
3400
3401 // string iterator operators that satisfy STL Random Access Iterator
3402 // requirements:
3403 inline wxString::iterator operator+(ptrdiff_t n, wxString::iterator i)
3404 { return i + n; }
3405 inline wxString::const_iterator operator+(ptrdiff_t n, wxString::const_iterator i)
3406 { return i + n; }
3407 inline wxString::reverse_iterator operator+(ptrdiff_t n, wxString::reverse_iterator i)
3408 { return i + n; }
3409 inline wxString::const_reverse_iterator operator+(ptrdiff_t n, wxString::const_reverse_iterator i)
3410 { return i + n; }
3411
3412 // notice that even though for many compilers the friend declarations above are
3413 // enough, from the point of view of C++ standard we must have the declarations
3414 // here as friend ones are not injected in the enclosing namespace and without
3415 // them the code fails to compile with conforming compilers such as xlC or g++4
3416 wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2);
3417 wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const char *psz);
3418 wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wchar_t *pwz);
3419 wxString WXDLLIMPEXP_BASE operator+(const char *psz, const wxString& string);
3420 wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz, const wxString& string);
3421
3422 wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch);
3423 wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string);
3424
3425 inline wxString operator+(const wxString& string, wxUniCharRef ch)
3426 { return string + (wxUniChar)ch; }
3427 inline wxString operator+(const wxString& string, char ch)
3428 { return string + wxUniChar(ch); }
3429 inline wxString operator+(const wxString& string, wchar_t ch)
3430 { return string + wxUniChar(ch); }
3431 inline wxString operator+(wxUniCharRef ch, const wxString& string)
3432 { return (wxUniChar)ch + string; }
3433 inline wxString operator+(char ch, const wxString& string)
3434 { return wxUniChar(ch) + string; }
3435 inline wxString operator+(wchar_t ch, const wxString& string)
3436 { return wxUniChar(ch) + string; }
3437
3438
3439 #define wxGetEmptyString() wxString()
3440
3441 // ----------------------------------------------------------------------------
3442 // helper functions which couldn't be defined inline
3443 // ----------------------------------------------------------------------------
3444
3445 namespace wxPrivate
3446 {
3447
3448 #if wxUSE_UNICODE_WCHAR
3449
3450 template <>
3451 struct wxStringAsBufHelper<char>
3452 {
3453 static wxCharBuffer Get(const wxString& s, size_t *len)
3454 {
3455 wxCharBuffer buf(s.mb_str());
3456 if ( len )
3457 *len = buf ? strlen(buf) : 0;
3458 return buf;
3459 }
3460 };
3461
3462 template <>
3463 struct wxStringAsBufHelper<wchar_t>
3464 {
3465 static wxWCharBuffer Get(const wxString& s, size_t *len)
3466 {
3467 if ( len )
3468 *len = s.length();
3469 return wxWCharBuffer::CreateNonOwned(s.wx_str());
3470 }
3471 };
3472
3473 #elif wxUSE_UNICODE_UTF8
3474
3475 template <>
3476 struct wxStringAsBufHelper<char>
3477 {
3478 static wxCharBuffer Get(const wxString& s, size_t *len)
3479 {
3480 if ( len )
3481 *len = s.utf8_length();
3482 return wxCharBuffer::CreateNonOwned(s.wx_str());
3483 }
3484 };
3485
3486 template <>
3487 struct wxStringAsBufHelper<wchar_t>
3488 {
3489 static wxWCharBuffer Get(const wxString& s, size_t *len)
3490 {
3491 wxWCharBuffer wbuf(s.wc_str());
3492 if ( len )
3493 *len = wxWcslen(wbuf);
3494 return wbuf;
3495 }
3496 };
3497
3498 #endif // Unicode build kind
3499
3500 } // namespace wxPrivate
3501
3502 // ----------------------------------------------------------------------------
3503 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
3504 // ----------------------------------------------------------------------------
3505
3506 #if !wxUSE_STL_BASED_WXSTRING
3507 // string buffer for direct access to string data in their native
3508 // representation:
3509 class wxStringInternalBuffer
3510 {
3511 public:
3512 typedef wxStringCharType CharType;
3513
3514 wxStringInternalBuffer(wxString& str, size_t lenWanted = 1024)
3515 : m_str(str), m_buf(NULL)
3516 { m_buf = m_str.DoGetWriteBuf(lenWanted); }
3517
3518 ~wxStringInternalBuffer() { m_str.DoUngetWriteBuf(); }
3519
3520 operator wxStringCharType*() const { return m_buf; }
3521
3522 private:
3523 wxString& m_str;
3524 wxStringCharType *m_buf;
3525
3526 DECLARE_NO_COPY_CLASS(wxStringInternalBuffer)
3527 };
3528
3529 class wxStringInternalBufferLength
3530 {
3531 public:
3532 typedef wxStringCharType CharType;
3533
3534 wxStringInternalBufferLength(wxString& str, size_t lenWanted = 1024)
3535 : m_str(str), m_buf(NULL), m_len(0), m_lenSet(false)
3536 {
3537 m_buf = m_str.DoGetWriteBuf(lenWanted);
3538 wxASSERT(m_buf != NULL);
3539 }
3540
3541 ~wxStringInternalBufferLength()
3542 {
3543 wxASSERT(m_lenSet);
3544 m_str.DoUngetWriteBuf(m_len);
3545 }
3546
3547 operator wxStringCharType*() const { return m_buf; }
3548 void SetLength(size_t length) { m_len = length; m_lenSet = true; }
3549
3550 private:
3551 wxString& m_str;
3552 wxStringCharType *m_buf;
3553 size_t m_len;
3554 bool m_lenSet;
3555
3556 DECLARE_NO_COPY_CLASS(wxStringInternalBufferLength)
3557 };
3558
3559 #endif // !wxUSE_STL_BASED_WXSTRING
3560
3561 template<typename T>
3562 class WXDLLIMPEXP_BASE wxStringTypeBufferBase
3563 {
3564 public:
3565 typedef T CharType;
3566
3567 wxStringTypeBufferBase(wxString& str, size_t lenWanted = 1024)
3568 : m_str(str), m_buf(lenWanted)
3569 {
3570 // for compatibility with old wxStringBuffer which provided direct
3571 // access to wxString internal buffer, initialize ourselves with the
3572 // string initial contents
3573
3574 // FIXME-VC6: remove the ugly (CharType *)NULL and use normal
3575 // tchar_str<CharType>
3576 size_t len;
3577 const wxCharTypeBuffer<CharType> buf(str.tchar_str(&len, (CharType *)NULL));
3578 if ( buf )
3579 {
3580 if ( len > lenWanted )
3581 {
3582 // in this case there is not enough space for terminating NUL,
3583 // ensure that we still put it there
3584 m_buf.data()[lenWanted] = 0;
3585 len = lenWanted - 1;
3586 }
3587
3588 memcpy(m_buf.data(), buf, (len + 1)*sizeof(CharType));
3589 }
3590 //else: conversion failed, this can happen when trying to get Unicode
3591 // string contents into a char string
3592 }
3593
3594 operator CharType*() { return m_buf.data(); }
3595
3596 protected:
3597 wxString& m_str;
3598 wxCharTypeBuffer<CharType> m_buf;
3599 };
3600
3601 template<typename T>
3602 class WXDLLIMPEXP_BASE wxStringTypeBufferLengthBase
3603 : public wxStringTypeBufferBase<T>
3604 {
3605 public:
3606 wxStringTypeBufferLengthBase(wxString& str, size_t lenWanted = 1024)
3607 : wxStringTypeBufferBase<T>(str, lenWanted),
3608 m_len(0),
3609 m_lenSet(false)
3610 { }
3611
3612 ~wxStringTypeBufferLengthBase()
3613 {
3614 wxASSERT_MSG( this->m_lenSet, "forgot to call SetLength()" );
3615 }
3616
3617 void SetLength(size_t length) { m_len = length; m_lenSet = true; }
3618
3619 protected:
3620 size_t m_len;
3621 bool m_lenSet;
3622 };
3623
3624 template<typename T>
3625 class wxStringTypeBuffer : public wxStringTypeBufferBase<T>
3626 {
3627 public:
3628 wxStringTypeBuffer(wxString& str, size_t lenWanted = 1024)
3629 : wxStringTypeBufferBase<T>(str, lenWanted)
3630 { }
3631
3632 ~wxStringTypeBuffer()
3633 {
3634 this->m_str.assign(this->m_buf.data());
3635 }
3636
3637 DECLARE_NO_COPY_CLASS(wxStringTypeBuffer)
3638 };
3639
3640 template<typename T>
3641 class wxStringTypeBufferLength : public wxStringTypeBufferLengthBase<T>
3642 {
3643 public:
3644 wxStringTypeBufferLength(wxString& str, size_t lenWanted = 1024)
3645 : wxStringTypeBufferLengthBase<T>(str, lenWanted)
3646 { }
3647
3648 ~wxStringTypeBufferLength()
3649 {
3650 this->m_str.assign(this->m_buf.data(), this->m_len);
3651 }
3652
3653 DECLARE_NO_COPY_CLASS(wxStringTypeBufferLength)
3654 };
3655
3656 #if wxUSE_STL_BASED_WXSTRING
3657
3658 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase<wxStringCharType> )
3659
3660 class wxStringInternalBuffer : public wxStringTypeBufferBase<wxStringCharType>
3661 {
3662 public:
3663 wxStringInternalBuffer(wxString& str, size_t lenWanted = 1024)
3664 : wxStringTypeBufferBase<wxStringCharType>(str, lenWanted) {}
3665 ~wxStringInternalBuffer()
3666 { m_str.m_impl.assign(m_buf.data()); }
3667
3668 DECLARE_NO_COPY_CLASS(wxStringInternalBuffer)
3669 };
3670
3671 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE(
3672 wxStringTypeBufferLengthBase<wxStringCharType> )
3673
3674 class wxStringInternalBufferLength
3675 : public wxStringTypeBufferLengthBase<wxStringCharType>
3676 {
3677 public:
3678 wxStringInternalBufferLength(wxString& str, size_t lenWanted = 1024)
3679 : wxStringTypeBufferLengthBase<wxStringCharType>(str, lenWanted) {}
3680
3681 ~wxStringInternalBufferLength()
3682 {
3683 m_str.m_impl.assign(m_buf.data(), m_len);
3684 }
3685
3686 DECLARE_NO_COPY_CLASS(wxStringInternalBufferLength)
3687 };
3688
3689 #endif // wxUSE_STL_BASED_WXSTRING
3690
3691
3692 #if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
3693 typedef wxStringTypeBuffer<wxChar> wxStringBuffer;
3694 typedef wxStringTypeBufferLength<wxChar> wxStringBufferLength;
3695 #else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
3696 typedef wxStringInternalBuffer wxStringBuffer;
3697 typedef wxStringInternalBufferLength wxStringBufferLength;
3698 #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
3699
3700 #if wxUSE_UNICODE_UTF8
3701 typedef wxStringInternalBuffer wxUTF8StringBuffer;
3702 typedef wxStringInternalBufferLength wxUTF8StringBufferLength;
3703 #elif wxUSE_UNICODE_WCHAR
3704
3705 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase<char> )
3706
3707 class WXDLLIMPEXP_BASE wxUTF8StringBuffer : public wxStringTypeBufferBase<char>
3708 {
3709 public:
3710 wxUTF8StringBuffer(wxString& str, size_t lenWanted = 1024)
3711 : wxStringTypeBufferBase<char>(str, lenWanted) {}
3712 ~wxUTF8StringBuffer();
3713
3714 DECLARE_NO_COPY_CLASS(wxUTF8StringBuffer)
3715 };
3716
3717 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferLengthBase<char> )
3718
3719 class WXDLLIMPEXP_BASE wxUTF8StringBufferLength
3720 : public wxStringTypeBufferLengthBase<char>
3721 {
3722 public:
3723 wxUTF8StringBufferLength(wxString& str, size_t lenWanted = 1024)
3724 : wxStringTypeBufferLengthBase<char>(str, lenWanted) {}
3725 ~wxUTF8StringBufferLength();
3726
3727 DECLARE_NO_COPY_CLASS(wxUTF8StringBufferLength)
3728 };
3729 #endif // wxUSE_UNICODE_UTF8/wxUSE_UNICODE_WCHAR
3730
3731
3732 // ---------------------------------------------------------------------------
3733 // wxString comparison functions: operator versions are always case sensitive
3734 // ---------------------------------------------------------------------------
3735
3736 #define wxCMP_WXCHAR_STRING(p, s, op) 0 op s.Cmp(p)
3737
3738 wxDEFINE_ALL_COMPARISONS(const wxChar *, const wxString&, wxCMP_WXCHAR_STRING)
3739
3740 #undef wxCMP_WXCHAR_STRING
3741
3742 inline bool operator==(const wxString& s1, const wxString& s2)
3743 { return s1.IsSameAs(s2); }
3744 inline bool operator!=(const wxString& s1, const wxString& s2)
3745 { return !s1.IsSameAs(s2); }
3746 inline bool operator< (const wxString& s1, const wxString& s2)
3747 { return s1.Cmp(s2) < 0; }
3748 inline bool operator> (const wxString& s1, const wxString& s2)
3749 { return s1.Cmp(s2) > 0; }
3750 inline bool operator<=(const wxString& s1, const wxString& s2)
3751 { return s1.Cmp(s2) <= 0; }
3752 inline bool operator>=(const wxString& s1, const wxString& s2)
3753 { return s1.Cmp(s2) >= 0; }
3754
3755 inline bool operator==(const wxString& s1, const wxCStrData& s2)
3756 { return s1 == s2.AsString(); }
3757 inline bool operator==(const wxCStrData& s1, const wxString& s2)
3758 { return s1.AsString() == s2; }
3759 inline bool operator!=(const wxString& s1, const wxCStrData& s2)
3760 { return s1 != s2.AsString(); }
3761 inline bool operator!=(const wxCStrData& s1, const wxString& s2)
3762 { return s1.AsString() != s2; }
3763
3764 inline bool operator==(const wxString& s1, const wxWCharBuffer& s2)
3765 { return (s1.Cmp((const wchar_t *)s2) == 0); }
3766 inline bool operator==(const wxWCharBuffer& s1, const wxString& s2)
3767 { return (s2.Cmp((const wchar_t *)s1) == 0); }
3768 inline bool operator!=(const wxString& s1, const wxWCharBuffer& s2)
3769 { return (s1.Cmp((const wchar_t *)s2) != 0); }
3770 inline bool operator!=(const wxWCharBuffer& s1, const wxString& s2)
3771 { return (s2.Cmp((const wchar_t *)s1) != 0); }
3772
3773 inline bool operator==(const wxString& s1, const wxCharBuffer& s2)
3774 { return (s1.Cmp((const char *)s2) == 0); }
3775 inline bool operator==(const wxCharBuffer& s1, const wxString& s2)
3776 { return (s2.Cmp((const char *)s1) == 0); }
3777 inline bool operator!=(const wxString& s1, const wxCharBuffer& s2)
3778 { return (s1.Cmp((const char *)s2) != 0); }
3779 inline bool operator!=(const wxCharBuffer& s1, const wxString& s2)
3780 { return (s2.Cmp((const char *)s1) != 0); }
3781
3782 inline wxString operator+(const wxString& string, const wxWCharBuffer& buf)
3783 { return string + (const wchar_t *)buf; }
3784 inline wxString operator+(const wxWCharBuffer& buf, const wxString& string)
3785 { return (const wchar_t *)buf + string; }
3786
3787 inline wxString operator+(const wxString& string, const wxCharBuffer& buf)
3788 { return string + (const char *)buf; }
3789 inline wxString operator+(const wxCharBuffer& buf, const wxString& string)
3790 { return (const char *)buf + string; }
3791
3792 // comparison with char
3793 inline bool operator==(const wxUniChar& c, const wxString& s) { return s.IsSameAs(c); }
3794 inline bool operator==(const wxUniCharRef& c, const wxString& s) { return s.IsSameAs(c); }
3795 inline bool operator==(char c, const wxString& s) { return s.IsSameAs(c); }
3796 inline bool operator==(wchar_t c, const wxString& s) { return s.IsSameAs(c); }
3797 inline bool operator==(int c, const wxString& s) { return s.IsSameAs(c); }
3798 inline bool operator==(const wxString& s, const wxUniChar& c) { return s.IsSameAs(c); }
3799 inline bool operator==(const wxString& s, const wxUniCharRef& c) { return s.IsSameAs(c); }
3800 inline bool operator==(const wxString& s, char c) { return s.IsSameAs(c); }
3801 inline bool operator==(const wxString& s, wchar_t c) { return s.IsSameAs(c); }
3802 inline bool operator!=(const wxUniChar& c, const wxString& s) { return !s.IsSameAs(c); }
3803 inline bool operator!=(const wxUniCharRef& c, const wxString& s) { return !s.IsSameAs(c); }
3804 inline bool operator!=(char c, const wxString& s) { return !s.IsSameAs(c); }
3805 inline bool operator!=(wchar_t c, const wxString& s) { return !s.IsSameAs(c); }
3806 inline bool operator!=(int c, const wxString& s) { return !s.IsSameAs(c); }
3807 inline bool operator!=(const wxString& s, const wxUniChar& c) { return !s.IsSameAs(c); }
3808 inline bool operator!=(const wxString& s, const wxUniCharRef& c) { return !s.IsSameAs(c); }
3809 inline bool operator!=(const wxString& s, char c) { return !s.IsSameAs(c); }
3810 inline bool operator!=(const wxString& s, wchar_t c) { return !s.IsSameAs(c); }
3811
3812 // comparison with C string in Unicode build
3813 #if wxUSE_UNICODE
3814
3815 #define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s
3816
3817 wxDEFINE_ALL_COMPARISONS(const char *, const wxString&, wxCMP_CHAR_STRING)
3818
3819 #undef wxCMP_CHAR_STRING
3820
3821 #endif // wxUSE_UNICODE
3822
3823 // we also need to provide the operators for comparison with wxCStrData to
3824 // resolve ambiguity between operator(const wxChar *,const wxString &) and
3825 // operator(const wxChar *, const wxChar *) for "p == s.c_str()"
3826 //
3827 // notice that these are (shallow) pointer comparisons, not (deep) string ones
3828 #define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar()
3829 #define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar()
3830
3831 wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData&, wxCMP_WCHAR_CSTRDATA)
3832 wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData&, wxCMP_CHAR_CSTRDATA)
3833
3834 #undef wxCMP_CHAR_CSTRDATA
3835 #undef wxCMP_WCHAR_CSTRDATA
3836
3837 // ---------------------------------------------------------------------------
3838 // Implementation only from here until the end of file
3839 // ---------------------------------------------------------------------------
3840
3841 #if wxUSE_STD_IOSTREAM
3842
3843 #include "wx/iosfwrap.h"
3844
3845 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxString&);
3846 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCStrData&);
3847 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCharBuffer&);
3848 #ifndef __BORLANDC__
3849 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxWCharBuffer&);
3850 #endif
3851
3852 #if wxUSE_UNICODE && defined(HAVE_WOSTREAM)
3853
3854 WXDLLIMPEXP_BASE wxSTD wostream& operator<<(wxSTD wostream&, const wxString&);
3855 WXDLLIMPEXP_BASE wxSTD wostream& operator<<(wxSTD wostream&, const wxCStrData&);
3856 WXDLLIMPEXP_BASE wxSTD wostream& operator<<(wxSTD wostream&, const wxWCharBuffer&);
3857
3858 #endif // wxUSE_UNICODE && defined(HAVE_WOSTREAM)
3859
3860 #endif // wxUSE_STD_IOSTREAM
3861
3862 // ---------------------------------------------------------------------------
3863 // wxCStrData implementation
3864 // ---------------------------------------------------------------------------
3865
3866 inline wxCStrData::wxCStrData(char *buf)
3867 : m_str(new wxString(buf)), m_offset(0), m_owned(true) {}
3868 inline wxCStrData::wxCStrData(wchar_t *buf)
3869 : m_str(new wxString(buf)), m_offset(0), m_owned(true) {}
3870
3871 inline wxCStrData::wxCStrData(const wxCStrData& data)
3872 : m_str(data.m_owned ? new wxString(*data.m_str) : data.m_str),
3873 m_offset(data.m_offset),
3874 m_owned(data.m_owned)
3875 {
3876 }
3877
3878 inline wxCStrData::~wxCStrData()
3879 {
3880 if ( m_owned )
3881 delete wx_const_cast(wxString*, m_str); // cast to silence warnings
3882 }
3883
3884 // simple cases for AsChar() and AsWChar(), the complicated ones are
3885 // in string.cpp
3886 #if wxUSE_UNICODE_WCHAR
3887 inline const wchar_t* wxCStrData::AsWChar() const
3888 {
3889 return m_str->wx_str() + m_offset;
3890 }
3891 #endif // wxUSE_UNICODE_WCHAR
3892
3893 #if !wxUSE_UNICODE
3894 inline const char* wxCStrData::AsChar() const
3895 {
3896 return m_str->wx_str() + m_offset;
3897 }
3898 #endif // !wxUSE_UNICODE
3899
3900 #if wxUSE_UTF8_LOCALE_ONLY
3901 inline const char* wxCStrData::AsChar() const
3902 {
3903 return wxStringOperations::AddToIter(m_str->wx_str(), m_offset);
3904 }
3905 #endif // wxUSE_UTF8_LOCALE_ONLY
3906
3907 inline const wxCharBuffer wxCStrData::AsCharBuf() const
3908 {
3909 #if !wxUSE_UNICODE
3910 return wxCharBuffer::CreateNonOwned(AsChar());
3911 #else
3912 return AsString().mb_str();
3913 #endif
3914 }
3915
3916 inline const wxWCharBuffer wxCStrData::AsWCharBuf() const
3917 {
3918 #if wxUSE_UNICODE_WCHAR
3919 return wxWCharBuffer::CreateNonOwned(AsWChar());
3920 #else
3921 return AsString().wc_str();
3922 #endif
3923 }
3924
3925 inline wxString wxCStrData::AsString() const
3926 {
3927 if ( m_offset == 0 )
3928 return *m_str;
3929 else
3930 return m_str->Mid(m_offset);
3931 }
3932
3933 inline const wxStringCharType *wxCStrData::AsInternal() const
3934 {
3935 #if wxUSE_UNICODE_UTF8
3936 return wxStringOperations::AddToIter(m_str->wx_str(), m_offset);
3937 #else
3938 return m_str->wx_str() + m_offset;
3939 #endif
3940 }
3941
3942 inline wxUniChar wxCStrData::operator*() const
3943 {
3944 if ( m_str->empty() )
3945 return wxUniChar(_T('\0'));
3946 else
3947 return (*m_str)[m_offset];
3948 }
3949
3950 inline wxUniChar wxCStrData::operator[](size_t n) const
3951 {
3952 // NB: we intentionally use operator[] and not at() here because the former
3953 // works for the terminating NUL while the latter does not
3954 return (*m_str)[m_offset + n];
3955 }
3956
3957 // ----------------------------------------------------------------------------
3958 // more wxCStrData operators
3959 // ----------------------------------------------------------------------------
3960
3961 // we need to define those to allow "size_t pos = p - s.c_str()" where p is
3962 // some pointer into the string
3963 inline size_t operator-(const char *p, const wxCStrData& cs)
3964 {
3965 return p - cs.AsChar();
3966 }
3967
3968 inline size_t operator-(const wchar_t *p, const wxCStrData& cs)
3969 {
3970 return p - cs.AsWChar();
3971 }
3972
3973 // ----------------------------------------------------------------------------
3974 // implementation of wx[W]CharBuffer inline methods using wxCStrData
3975 // ----------------------------------------------------------------------------
3976
3977 // FIXME-UTF8: move this to buffer.h
3978 inline wxCharBuffer::wxCharBuffer(const wxCStrData& cstr)
3979 : wxCharTypeBufferBase(cstr.AsCharBuf())
3980 {
3981 }
3982
3983 inline wxWCharBuffer::wxWCharBuffer(const wxCStrData& cstr)
3984 : wxCharTypeBufferBase(cstr.AsWCharBuf())
3985 {
3986 }
3987
3988 #if wxUSE_UNICODE_UTF8
3989 // ----------------------------------------------------------------------------
3990 // implementation of wxStringIteratorNode inline methods
3991 // ----------------------------------------------------------------------------
3992
3993 void wxStringIteratorNode::DoSet(const wxString *str,
3994 wxStringImpl::const_iterator *citer,
3995 wxStringImpl::iterator *iter)
3996 {
3997 m_prev = NULL;
3998 m_iter = iter;
3999 m_citer = citer;
4000 m_str = str;
4001 if ( str )
4002 {
4003 m_next = str->m_iterators.ptr;
4004 wx_const_cast(wxString*, m_str)->m_iterators.ptr = this;
4005 if ( m_next )
4006 m_next->m_prev = this;
4007 }
4008 else
4009 {
4010 m_next = NULL;
4011 }
4012 }
4013
4014 void wxStringIteratorNode::clear()
4015 {
4016 if ( m_next )
4017 m_next->m_prev = m_prev;
4018 if ( m_prev )
4019 m_prev->m_next = m_next;
4020 else if ( m_str ) // first in the list
4021 wx_const_cast(wxString*, m_str)->m_iterators.ptr = m_next;
4022
4023 m_next = m_prev = NULL;
4024 m_citer = NULL;
4025 m_iter = NULL;
4026 m_str = NULL;
4027 }
4028 #endif // wxUSE_UNICODE_UTF8
4029
4030 #if WXWIN_COMPATIBILITY_2_8
4031 // lot of code out there doesn't explicitly include wx/crt.h, but uses
4032 // CRT wrappers that are now declared in wx/wxcrt.h and wx/wxcrtvararg.h,
4033 // so let's include this header now that wxString is defined and it's safe
4034 // to do it:
4035 #include "wx/crt.h"
4036 #endif
4037
4038 #endif // _WX_WXSTRING_H_