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