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