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