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