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