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