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