]> git.saurik.com Git - wxWidgets.git/blob - include/wx/string.h
bringing back utf32 encoding for wchar, fixes #10666
[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(wx_str()); }
1715 const wxScopedCharBuffer ToUTF8() const
1716 { return wxCharBuffer::CreateNonOwned(wx_str()); }
1717
1718 // this function exists in UTF-8 build only and returns the length of the
1719 // internal UTF-8 representation
1720 size_t utf8_length() const { return m_impl.length(); }
1721 #elif wxUSE_UNICODE_WCHAR
1722 static wxString FromUTF8(const char *utf8, size_t len = npos)
1723 { return wxString(utf8, wxMBConvUTF8(), len); }
1724 static wxString FromUTF8Unchecked(const char *utf8, size_t len = npos)
1725 {
1726 const wxString s(utf8, wxMBConvUTF8(), len);
1727 wxASSERT_MSG( !utf8 || !*utf8 || !s.empty(),
1728 "string must be valid UTF-8" );
1729 return s;
1730 }
1731 const wxScopedCharBuffer utf8_str() const { return mb_str(wxMBConvUTF8()); }
1732 const wxScopedCharBuffer ToUTF8() const { return utf8_str(); }
1733 #else // ANSI
1734 static wxString FromUTF8(const char *utf8)
1735 { return wxString(wxMBConvUTF8().cMB2WC(utf8)); }
1736 static wxString FromUTF8(const char *utf8, size_t len)
1737 {
1738 size_t wlen;
1739 wxScopedWCharBuffer buf(wxMBConvUTF8().cMB2WC(utf8, len == npos ? wxNO_LEN : len, &wlen));
1740 return wxString(buf.data(), wlen);
1741 }
1742 static wxString FromUTF8Unchecked(const char *utf8, size_t len = npos)
1743 {
1744 size_t wlen;
1745 wxScopedWCharBuffer buf
1746 (
1747 wxMBConvUTF8().cMB2WC
1748 (
1749 utf8,
1750 len == npos ? wxNO_LEN : len,
1751 &wlen
1752 )
1753 );
1754 wxASSERT_MSG( !utf8 || !*utf8 || wlen,
1755 "string must be valid UTF-8" );
1756
1757 return wxString(buf.data(), wlen);
1758 }
1759 const wxScopedCharBuffer utf8_str() const
1760 { return wxMBConvUTF8().cWC2MB(wc_str()); }
1761 const wxScopedCharBuffer ToUTF8() const { return utf8_str(); }
1762 #endif
1763
1764 // functions for storing binary data in wxString:
1765 #if wxUSE_UNICODE
1766 static wxString From8BitData(const char *data, size_t len)
1767 { return wxString(data, wxConvISO8859_1, len); }
1768 // version for NUL-terminated data:
1769 static wxString From8BitData(const char *data)
1770 { return wxString(data, wxConvISO8859_1); }
1771 const wxScopedCharBuffer To8BitData() const
1772 { return mb_str(wxConvISO8859_1); }
1773 #else // ANSI
1774 static wxString From8BitData(const char *data, size_t len)
1775 { return wxString(data, len); }
1776 // version for NUL-terminated data:
1777 static wxString From8BitData(const char *data)
1778 { return wxString(data); }
1779 const char *To8BitData() const { return c_str(); }
1780 #endif // Unicode/ANSI
1781
1782 // conversions with (possible) format conversions: have to return a
1783 // buffer with temporary data
1784 //
1785 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
1786 // return an ANSI (multibyte) string, wc_str() to return a wide string and
1787 // fn_str() to return a string which should be used with the OS APIs
1788 // accepting the file names. The return value is always the same, but the
1789 // type differs because a function may either return pointer to the buffer
1790 // directly or have to use intermediate buffer for translation.
1791 #if wxUSE_UNICODE
1792
1793 #if wxUSE_UTF8_LOCALE_ONLY
1794 const char* mb_str() const { return wx_str(); }
1795 const wxScopedCharBuffer mb_str(const wxMBConv& conv) const;
1796 #else
1797 const wxScopedCharBuffer mb_str(const wxMBConv& conv = wxConvLibc) const;
1798 #endif
1799
1800 const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); }
1801
1802 #if wxUSE_UNICODE_WCHAR
1803 const wchar_t* wc_str() const { return wx_str(); }
1804 #elif wxUSE_UNICODE_UTF8
1805 const wxScopedWCharBuffer wc_str() const;
1806 #endif
1807 // for compatibility with !wxUSE_UNICODE version
1808 const wxWX2WCbuf wc_str(const wxMBConv& WXUNUSED(conv)) const
1809 { return wc_str(); }
1810
1811 #if wxMBFILES
1812 const wxScopedCharBuffer fn_str() const { return mb_str(wxConvFile); }
1813 #else // !wxMBFILES
1814 const wxWX2WCbuf fn_str() const { return wc_str(); }
1815 #endif // wxMBFILES/!wxMBFILES
1816
1817 #else // ANSI
1818 const wxChar* mb_str() const { return wx_str(); }
1819
1820 // for compatibility with wxUSE_UNICODE version
1821 const char* mb_str(const wxMBConv& WXUNUSED(conv)) const { return wx_str(); }
1822
1823 const wxWX2MBbuf mbc_str() const { return mb_str(); }
1824
1825 #if wxUSE_WCHAR_T
1826 const wxScopedWCharBuffer wc_str(const wxMBConv& conv = wxConvLibc) const;
1827 #endif // wxUSE_WCHAR_T
1828 const wxScopedCharBuffer fn_str() const
1829 { return wxConvFile.cWC2WX( wc_str( wxConvLibc ) ); }
1830 #endif // Unicode/ANSI
1831
1832 #if wxUSE_UNICODE_UTF8
1833 const wxScopedWCharBuffer t_str() const { return wc_str(); }
1834 #elif wxUSE_UNICODE_WCHAR
1835 const wchar_t* t_str() const { return wx_str(); }
1836 #else
1837 const char* t_str() const { return wx_str(); }
1838 #endif
1839
1840
1841 // overloaded assignment
1842 // from another wxString
1843 wxString& operator=(const wxString& stringSrc)
1844 {
1845 if ( this != &stringSrc )
1846 {
1847 wxSTRING_INVALIDATE_CACHE();
1848
1849 m_impl = stringSrc.m_impl;
1850 }
1851
1852 return *this;
1853 }
1854
1855 wxString& operator=(const wxCStrData& cstr)
1856 { return *this = cstr.AsString(); }
1857 // from a character
1858 wxString& operator=(wxUniChar ch)
1859 {
1860 wxSTRING_INVALIDATE_CACHE();
1861
1862 #if wxUSE_UNICODE_UTF8
1863 if ( !ch.IsAscii() )
1864 m_impl = wxStringOperations::EncodeChar(ch);
1865 else
1866 #endif // wxUSE_UNICODE_UTF8
1867 m_impl = (wxStringCharType)ch;
1868 return *this;
1869 }
1870
1871 wxString& operator=(wxUniCharRef ch)
1872 { return operator=((wxUniChar)ch); }
1873 wxString& operator=(char ch)
1874 { return operator=(wxUniChar(ch)); }
1875 wxString& operator=(unsigned char ch)
1876 { return operator=(wxUniChar(ch)); }
1877 wxString& operator=(wchar_t ch)
1878 { return operator=(wxUniChar(ch)); }
1879 // from a C string - STL probably will crash on NULL,
1880 // so we need to compensate in that case
1881 #if wxUSE_STL_BASED_WXSTRING
1882 wxString& operator=(const char *psz)
1883 {
1884 wxSTRING_INVALIDATE_CACHE();
1885
1886 if ( psz )
1887 m_impl = ImplStr(psz);
1888 else
1889 clear();
1890
1891 return *this;
1892 }
1893
1894 wxString& operator=(const wchar_t *pwz)
1895 {
1896 wxSTRING_INVALIDATE_CACHE();
1897
1898 if ( pwz )
1899 m_impl = ImplStr(pwz);
1900 else
1901 clear();
1902
1903 return *this;
1904 }
1905 #else // !wxUSE_STL_BASED_WXSTRING
1906 wxString& operator=(const char *psz)
1907 {
1908 wxSTRING_INVALIDATE_CACHE();
1909
1910 m_impl = ImplStr(psz);
1911
1912 return *this;
1913 }
1914
1915 wxString& operator=(const wchar_t *pwz)
1916 {
1917 wxSTRING_INVALIDATE_CACHE();
1918
1919 m_impl = ImplStr(pwz);
1920
1921 return *this;
1922 }
1923 #endif // wxUSE_STL_BASED_WXSTRING/!wxUSE_STL_BASED_WXSTRING
1924
1925 wxString& operator=(const unsigned char *psz)
1926 { return operator=((const char*)psz); }
1927
1928 // from wxScopedWCharBuffer
1929 wxString& operator=(const wxScopedWCharBuffer& s)
1930 { return assign(s); }
1931 // from wxScopedCharBuffer
1932 wxString& operator=(const wxScopedCharBuffer& s)
1933 { return assign(s); }
1934
1935 // string concatenation
1936 // in place concatenation
1937 /*
1938 Concatenate and return the result. Note that the left to right
1939 associativity of << allows to write things like "str << str1 << str2
1940 << ..." (unlike with +=)
1941 */
1942 // string += string
1943 wxString& operator<<(const wxString& s)
1944 {
1945 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1946 wxASSERT_MSG( s.IsValid(),
1947 _T("did you forget to call UngetWriteBuf()?") );
1948 #endif
1949
1950 append(s);
1951 return *this;
1952 }
1953 // string += C string
1954 wxString& operator<<(const char *psz)
1955 { append(psz); return *this; }
1956 wxString& operator<<(const wchar_t *pwz)
1957 { append(pwz); return *this; }
1958 wxString& operator<<(const wxCStrData& psz)
1959 { append(psz.AsString()); return *this; }
1960 // string += char
1961 wxString& operator<<(wxUniChar ch) { append(1, ch); return *this; }
1962 wxString& operator<<(wxUniCharRef ch) { append(1, ch); return *this; }
1963 wxString& operator<<(char ch) { append(1, ch); return *this; }
1964 wxString& operator<<(unsigned char ch) { append(1, ch); return *this; }
1965 wxString& operator<<(wchar_t ch) { append(1, ch); return *this; }
1966
1967 // string += buffer (i.e. from wxGetString)
1968 wxString& operator<<(const wxScopedWCharBuffer& s)
1969 { return append(s); }
1970 wxString& operator<<(const wxScopedCharBuffer& s)
1971 { return append(s); }
1972
1973 // string += C string
1974 wxString& Append(const wxString& s)
1975 {
1976 // test for empty() to share the string if possible
1977 if ( empty() )
1978 *this = s;
1979 else
1980 append(s);
1981 return *this;
1982 }
1983 wxString& Append(const char* psz)
1984 { append(psz); return *this; }
1985 wxString& Append(const wchar_t* pwz)
1986 { append(pwz); return *this; }
1987 wxString& Append(const wxCStrData& psz)
1988 { append(psz); return *this; }
1989 wxString& Append(const wxScopedCharBuffer& psz)
1990 { append(psz); return *this; }
1991 wxString& Append(const wxScopedWCharBuffer& psz)
1992 { append(psz); return *this; }
1993 wxString& Append(const char* psz, size_t nLen)
1994 { append(psz, nLen); return *this; }
1995 wxString& Append(const wchar_t* pwz, size_t nLen)
1996 { append(pwz, nLen); return *this; }
1997 wxString& Append(const wxCStrData& psz, size_t nLen)
1998 { append(psz, nLen); return *this; }
1999 wxString& Append(const wxScopedCharBuffer& psz, size_t nLen)
2000 { append(psz, nLen); return *this; }
2001 wxString& Append(const wxScopedWCharBuffer& psz, size_t nLen)
2002 { append(psz, nLen); return *this; }
2003 // append count copies of given character
2004 wxString& Append(wxUniChar ch, size_t count = 1u)
2005 { append(count, ch); return *this; }
2006 wxString& Append(wxUniCharRef ch, size_t count = 1u)
2007 { append(count, ch); return *this; }
2008 wxString& Append(char ch, size_t count = 1u)
2009 { append(count, ch); return *this; }
2010 wxString& Append(unsigned char ch, size_t count = 1u)
2011 { append(count, ch); return *this; }
2012 wxString& Append(wchar_t ch, size_t count = 1u)
2013 { append(count, ch); return *this; }
2014
2015 // prepend a string, return the string itself
2016 wxString& Prepend(const wxString& str)
2017 { *this = str + *this; return *this; }
2018
2019 // non-destructive concatenation
2020 // two strings
2021 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string1,
2022 const wxString& string2);
2023 // string with a single char
2024 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch);
2025 // char with a string
2026 friend wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string);
2027 // string with C string
2028 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string,
2029 const char *psz);
2030 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string,
2031 const wchar_t *pwz);
2032 // C string with string
2033 friend wxString WXDLLIMPEXP_BASE operator+(const char *psz,
2034 const wxString& string);
2035 friend wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz,
2036 const wxString& string);
2037
2038 // stream-like functions
2039 // insert an int into string
2040 wxString& operator<<(int i)
2041 { return (*this) << Format(_T("%d"), i); }
2042 // insert an unsigned int into string
2043 wxString& operator<<(unsigned int ui)
2044 { return (*this) << Format(_T("%u"), ui); }
2045 // insert a long into string
2046 wxString& operator<<(long l)
2047 { return (*this) << Format(_T("%ld"), l); }
2048 // insert an unsigned long into string
2049 wxString& operator<<(unsigned long ul)
2050 { return (*this) << Format(_T("%lu"), ul); }
2051 #if defined wxLongLong_t && !defined wxLongLongIsLong
2052 // insert a long long if they exist and aren't longs
2053 wxString& operator<<(wxLongLong_t ll)
2054 {
2055 const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("d");
2056 return (*this) << Format(fmt, ll);
2057 }
2058 // insert an unsigned long long
2059 wxString& operator<<(wxULongLong_t ull)
2060 {
2061 const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("u");
2062 return (*this) << Format(fmt , ull);
2063 }
2064 #endif // wxLongLong_t && !wxLongLongIsLong
2065 // insert a float into string
2066 wxString& operator<<(float f)
2067 { return (*this) << Format(_T("%f"), f); }
2068 // insert a double into string
2069 wxString& operator<<(double d)
2070 { return (*this) << Format(_T("%g"), d); }
2071
2072 // string comparison
2073 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
2074 int Cmp(const char *psz) const
2075 { return compare(psz); }
2076 int Cmp(const wchar_t *pwz) const
2077 { return compare(pwz); }
2078 int Cmp(const wxString& s) const
2079 { return compare(s); }
2080 int Cmp(const wxCStrData& s) const
2081 { return compare(s); }
2082 int Cmp(const wxScopedCharBuffer& s) const
2083 { return compare(s); }
2084 int Cmp(const wxScopedWCharBuffer& s) const
2085 { return compare(s); }
2086 // same as Cmp() but not case-sensitive
2087 int CmpNoCase(const wxString& s) const;
2088
2089 // test for the string equality, either considering case or not
2090 // (if compareWithCase then the case matters)
2091 bool IsSameAs(const wxString& str, bool compareWithCase = true) const
2092 {
2093 #if !wxUSE_UNICODE_UTF8
2094 // in UTF-8 build, length() is O(n) and doing this would be _slower_
2095 if ( length() != str.length() )
2096 return false;
2097 #endif
2098 return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0;
2099 }
2100 bool IsSameAs(const char *str, bool compareWithCase = true) const
2101 { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; }
2102 bool IsSameAs(const wchar_t *str, bool compareWithCase = true) const
2103 { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; }
2104
2105 bool IsSameAs(const wxCStrData& str, bool compareWithCase = true) const
2106 { return IsSameAs(str.AsString(), compareWithCase); }
2107 bool IsSameAs(const wxScopedCharBuffer& str, bool compareWithCase = true) const
2108 { return IsSameAs(str.data(), compareWithCase); }
2109 bool IsSameAs(const wxScopedWCharBuffer& str, bool compareWithCase = true) const
2110 { return IsSameAs(str.data(), compareWithCase); }
2111 // comparison with a single character: returns true if equal
2112 bool IsSameAs(wxUniChar c, bool compareWithCase = true) const;
2113 // FIXME-UTF8: remove these overloads
2114 bool IsSameAs(wxUniCharRef c, bool compareWithCase = true) const
2115 { return IsSameAs(wxUniChar(c), compareWithCase); }
2116 bool IsSameAs(char c, bool compareWithCase = true) const
2117 { return IsSameAs(wxUniChar(c), compareWithCase); }
2118 bool IsSameAs(unsigned char c, bool compareWithCase = true) const
2119 { return IsSameAs(wxUniChar(c), compareWithCase); }
2120 bool IsSameAs(wchar_t c, bool compareWithCase = true) const
2121 { return IsSameAs(wxUniChar(c), compareWithCase); }
2122 bool IsSameAs(int c, bool compareWithCase = true) const
2123 { return IsSameAs(wxUniChar(c), compareWithCase); }
2124
2125 // simple sub-string extraction
2126 // return substring starting at nFirst of length nCount (or till the end
2127 // if nCount = default value)
2128 wxString Mid(size_t nFirst, size_t nCount = npos) const;
2129
2130 // operator version of Mid()
2131 wxString operator()(size_t start, size_t len) const
2132 { return Mid(start, len); }
2133
2134 // check if the string starts with the given prefix and return the rest
2135 // of the string in the provided pointer if it is not NULL; otherwise
2136 // return false
2137 bool StartsWith(const wxString& prefix, wxString *rest = NULL) const;
2138 // check if the string ends with the given suffix and return the
2139 // beginning of the string before the suffix in the provided pointer if
2140 // it is not NULL; otherwise return false
2141 bool EndsWith(const wxString& suffix, wxString *rest = NULL) const;
2142
2143 // get first nCount characters
2144 wxString Left(size_t nCount) const;
2145 // get last nCount characters
2146 wxString Right(size_t nCount) const;
2147 // get all characters before the first occurrence of ch
2148 // (returns the whole string if ch not found)
2149 wxString BeforeFirst(wxUniChar ch) const;
2150 // get all characters before the last occurrence of ch
2151 // (returns empty string if ch not found)
2152 wxString BeforeLast(wxUniChar ch) const;
2153 // get all characters after the first occurrence of ch
2154 // (returns empty string if ch not found)
2155 wxString AfterFirst(wxUniChar ch) const;
2156 // get all characters after the last occurrence of ch
2157 // (returns the whole string if ch not found)
2158 wxString AfterLast(wxUniChar ch) const;
2159
2160 // for compatibility only, use more explicitly named functions above
2161 wxString Before(wxUniChar ch) const { return BeforeLast(ch); }
2162 wxString After(wxUniChar ch) const { return AfterFirst(ch); }
2163
2164 // case conversion
2165 // convert to upper case in place, return the string itself
2166 wxString& MakeUpper();
2167 // convert to upper case, return the copy of the string
2168 wxString Upper() const { return wxString(*this).MakeUpper(); }
2169 // convert to lower case in place, return the string itself
2170 wxString& MakeLower();
2171 // convert to lower case, return the copy of the string
2172 wxString Lower() const { return wxString(*this).MakeLower(); }
2173 // convert the first character to the upper case and the rest to the
2174 // lower one, return the modified string itself
2175 wxString& MakeCapitalized();
2176 // convert the first character to the upper case and the rest to the
2177 // lower one, return the copy of the string
2178 wxString Capitalize() const { return wxString(*this).MakeCapitalized(); }
2179
2180 // trimming/padding whitespace (either side) and truncating
2181 // remove spaces from left or from right (default) side
2182 wxString& Trim(bool bFromRight = true);
2183 // add nCount copies chPad in the beginning or at the end (default)
2184 wxString& Pad(size_t nCount, wxUniChar chPad = wxT(' '), bool bFromRight = true);
2185
2186 // searching and replacing
2187 // searching (return starting index, or -1 if not found)
2188 int Find(wxUniChar ch, bool bFromEnd = false) const; // like strchr/strrchr
2189 int Find(wxUniCharRef ch, bool bFromEnd = false) const
2190 { return Find(wxUniChar(ch), bFromEnd); }
2191 int Find(char ch, bool bFromEnd = false) const
2192 { return Find(wxUniChar(ch), bFromEnd); }
2193 int Find(unsigned char ch, bool bFromEnd = false) const
2194 { return Find(wxUniChar(ch), bFromEnd); }
2195 int Find(wchar_t ch, bool bFromEnd = false) const
2196 { return Find(wxUniChar(ch), bFromEnd); }
2197 // searching (return starting index, or -1 if not found)
2198 int Find(const wxString& sub) const // like strstr
2199 {
2200 size_type idx = find(sub);
2201 return (idx == npos) ? wxNOT_FOUND : (int)idx;
2202 }
2203 int Find(const char *sub) const // like strstr
2204 {
2205 size_type idx = find(sub);
2206 return (idx == npos) ? wxNOT_FOUND : (int)idx;
2207 }
2208 int Find(const wchar_t *sub) const // like strstr
2209 {
2210 size_type idx = find(sub);
2211 return (idx == npos) ? wxNOT_FOUND : (int)idx;
2212 }
2213
2214 int Find(const wxCStrData& sub) const
2215 { return Find(sub.AsString()); }
2216 int Find(const wxScopedCharBuffer& sub) const
2217 { return Find(sub.data()); }
2218 int Find(const wxScopedWCharBuffer& sub) const
2219 { return Find(sub.data()); }
2220
2221 // replace first (or all of bReplaceAll) occurrences of substring with
2222 // another string, returns the number of replacements made
2223 size_t Replace(const wxString& strOld,
2224 const wxString& strNew,
2225 bool bReplaceAll = true);
2226
2227 // check if the string contents matches a mask containing '*' and '?'
2228 bool Matches(const wxString& mask) const;
2229
2230 // conversion to numbers: all functions return true only if the whole
2231 // string is a number and put the value of this number into the pointer
2232 // provided, the base is the numeric base in which the conversion should be
2233 // done and must be comprised between 2 and 36 or be 0 in which case the
2234 // standard C rules apply (leading '0' => octal, "0x" => hex)
2235 // convert to a signed integer
2236 bool ToLong(long *val, int base = 10) const;
2237 // convert to an unsigned integer
2238 bool ToULong(unsigned long *val, int base = 10) const;
2239 // convert to wxLongLong
2240 #if defined(wxLongLong_t)
2241 bool ToLongLong(wxLongLong_t *val, int base = 10) const;
2242 // convert to wxULongLong
2243 bool ToULongLong(wxULongLong_t *val, int base = 10) const;
2244 #endif // wxLongLong_t
2245 // convert to a double
2246 bool ToDouble(double *val) const;
2247
2248 #if wxUSE_XLOCALE
2249 // conversions to numbers using C locale
2250 // convert to a signed integer
2251 bool ToCLong(long *val, int base = 10) const;
2252 // convert to an unsigned integer
2253 bool ToCULong(unsigned long *val, int base = 10) const;
2254 // convert to a double
2255 bool ToCDouble(double *val) const;
2256 #endif
2257
2258 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2259 // formatted input/output
2260 // as sprintf(), returns the number of characters written or < 0 on error
2261 // (take 'this' into account in attribute parameter count)
2262 // int Printf(const wxString& format, ...);
2263 WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const wxFormatString&),
2264 DoPrintfWchar, DoPrintfUtf8)
2265 #ifdef __WATCOMC__
2266 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
2267 WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wxString&),
2268 (wxFormatString(f1)));
2269 WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wxCStrData&),
2270 (wxFormatString(f1)));
2271 WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const char*),
2272 (wxFormatString(f1)));
2273 WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wchar_t*),
2274 (wxFormatString(f1)));
2275 #endif
2276 #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN
2277 // as vprintf(), returns the number of characters written or < 0 on error
2278 int PrintfV(const wxString& format, va_list argptr);
2279
2280 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2281 // returns the string containing the result of Printf() to it
2282 // static wxString Format(const wxString& format, ...) WX_ATTRIBUTE_PRINTF_1;
2283 WX_DEFINE_VARARG_FUNC(static wxString, Format, 1, (const wxFormatString&),
2284 DoFormatWchar, DoFormatUtf8)
2285 #ifdef __WATCOMC__
2286 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
2287 WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wxString&),
2288 (wxFormatString(f1)));
2289 WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wxCStrData&),
2290 (wxFormatString(f1)));
2291 WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const char*),
2292 (wxFormatString(f1)));
2293 WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wchar_t*),
2294 (wxFormatString(f1)));
2295 #endif
2296 #endif
2297 // the same as above, but takes a va_list
2298 static wxString FormatV(const wxString& format, va_list argptr);
2299
2300 // raw access to string memory
2301 // ensure that string has space for at least nLen characters
2302 // only works if the data of this string is not shared
2303 bool Alloc(size_t nLen) { reserve(nLen); return capacity() >= nLen; }
2304 // minimize the string's memory
2305 // only works if the data of this string is not shared
2306 bool Shrink();
2307 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2308 // These are deprecated, use wxStringBuffer or wxStringBufferLength instead
2309 //
2310 // get writable buffer of at least nLen bytes. Unget() *must* be called
2311 // a.s.a.p. to put string back in a reasonable state!
2312 wxDEPRECATED( wxStringCharType *GetWriteBuf(size_t nLen) );
2313 // call this immediately after GetWriteBuf() has been used
2314 wxDEPRECATED( void UngetWriteBuf() );
2315 wxDEPRECATED( void UngetWriteBuf(size_t nLen) );
2316 #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && wxUSE_UNICODE_UTF8
2317
2318 // wxWidgets version 1 compatibility functions
2319
2320 // use Mid()
2321 wxString SubString(size_t from, size_t to) const
2322 { return Mid(from, (to - from + 1)); }
2323 // values for second parameter of CompareTo function
2324 enum caseCompare {exact, ignoreCase};
2325 // values for first parameter of Strip function
2326 enum stripType {leading = 0x1, trailing = 0x2, both = 0x3};
2327
2328 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2329 // use Printf()
2330 // (take 'this' into account in attribute parameter count)
2331 // int sprintf(const wxString& format, ...) WX_ATTRIBUTE_PRINTF_2;
2332 WX_DEFINE_VARARG_FUNC(int, sprintf, 1, (const wxFormatString&),
2333 DoPrintfWchar, DoPrintfUtf8)
2334 #ifdef __WATCOMC__
2335 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
2336 WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wxString&),
2337 (wxFormatString(f1)));
2338 WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wxCStrData&),
2339 (wxFormatString(f1)));
2340 WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const char*),
2341 (wxFormatString(f1)));
2342 WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wchar_t*),
2343 (wxFormatString(f1)));
2344 #endif
2345 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
2346
2347 // use Cmp()
2348 int CompareTo(const wxChar* psz, caseCompare cmp = exact) const
2349 { return cmp == exact ? Cmp(psz) : CmpNoCase(psz); }
2350
2351 // use length()
2352 size_t Length() const { return length(); }
2353 // Count the number of characters
2354 int Freq(wxUniChar ch) const;
2355 // use MakeLower
2356 void LowerCase() { MakeLower(); }
2357 // use MakeUpper
2358 void UpperCase() { MakeUpper(); }
2359 // use Trim except that it doesn't change this string
2360 wxString Strip(stripType w = trailing) const;
2361
2362 // use Find (more general variants not yet supported)
2363 size_t Index(const wxChar* psz) const { return Find(psz); }
2364 size_t Index(wxUniChar ch) const { return Find(ch); }
2365 // use Truncate
2366 wxString& Remove(size_t pos) { return Truncate(pos); }
2367 wxString& RemoveLast(size_t n = 1) { return Truncate(length() - n); }
2368
2369 wxString& Remove(size_t nStart, size_t nLen)
2370 { return (wxString&)erase( nStart, nLen ); }
2371
2372 // use Find()
2373 int First( wxUniChar ch ) const { return Find(ch); }
2374 int First( wxUniCharRef ch ) const { return Find(ch); }
2375 int First( char ch ) const { return Find(ch); }
2376 int First( unsigned char ch ) const { return Find(ch); }
2377 int First( wchar_t ch ) const { return Find(ch); }
2378 int First( const wxString& str ) const { return Find(str); }
2379 int Last( wxUniChar ch ) const { return Find(ch, true); }
2380 bool Contains(const wxString& str) const { return Find(str) != wxNOT_FOUND; }
2381
2382 // use empty()
2383 bool IsNull() const { return empty(); }
2384
2385 // std::string compatibility functions
2386
2387 // take nLen chars starting at nPos
2388 wxString(const wxString& str, size_t nPos, size_t nLen)
2389 { assign(str, nPos, nLen); }
2390 // take all characters from first to last
2391 wxString(const_iterator first, const_iterator last)
2392 : m_impl(first.impl(), last.impl()) { }
2393 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2394 // the 2 overloads below are for compatibility with the existing code using
2395 // pointers instead of iterators
2396 wxString(const char *first, const char *last)
2397 {
2398 SubstrBufFromMB str(ImplStr(first, last - first));
2399 m_impl.assign(str.data, str.len);
2400 }
2401 wxString(const wchar_t *first, const wchar_t *last)
2402 {
2403 SubstrBufFromWC str(ImplStr(first, last - first));
2404 m_impl.assign(str.data, str.len);
2405 }
2406 // and this one is needed to compile code adding offsets to c_str() result
2407 wxString(const wxCStrData& first, const wxCStrData& last)
2408 : m_impl(CreateConstIterator(first).impl(),
2409 CreateConstIterator(last).impl())
2410 {
2411 wxASSERT_MSG( first.m_str == last.m_str,
2412 _T("pointers must be into the same string") );
2413 }
2414 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2415
2416 // lib.string.modifiers
2417 // append elements str[pos], ..., str[pos+n]
2418 wxString& append(const wxString& str, size_t pos, size_t n)
2419 {
2420 wxSTRING_UPDATE_CACHED_LENGTH(n);
2421
2422 size_t from, len;
2423 str.PosLenToImpl(pos, n, &from, &len);
2424 m_impl.append(str.m_impl, from, len);
2425 return *this;
2426 }
2427 // append a string
2428 wxString& append(const wxString& str)
2429 {
2430 wxSTRING_UPDATE_CACHED_LENGTH(str.length());
2431
2432 m_impl.append(str.m_impl);
2433 return *this;
2434 }
2435
2436 // append first n (or all if n == npos) characters of sz
2437 wxString& append(const char *sz)
2438 {
2439 wxSTRING_INVALIDATE_CACHED_LENGTH();
2440
2441 m_impl.append(ImplStr(sz));
2442 return *this;
2443 }
2444
2445 wxString& append(const wchar_t *sz)
2446 {
2447 wxSTRING_INVALIDATE_CACHED_LENGTH();
2448
2449 m_impl.append(ImplStr(sz));
2450 return *this;
2451 }
2452
2453 wxString& append(const char *sz, size_t n)
2454 {
2455 wxSTRING_INVALIDATE_CACHED_LENGTH();
2456
2457 SubstrBufFromMB str(ImplStr(sz, n));
2458 m_impl.append(str.data, str.len);
2459 return *this;
2460 }
2461 wxString& append(const wchar_t *sz, size_t n)
2462 {
2463 wxSTRING_UPDATE_CACHED_LENGTH(n);
2464
2465 SubstrBufFromWC str(ImplStr(sz, n));
2466 m_impl.append(str.data, str.len);
2467 return *this;
2468 }
2469
2470 wxString& append(const wxCStrData& str)
2471 { return append(str.AsString()); }
2472 wxString& append(const wxScopedCharBuffer& str)
2473 { return append(str.data(), str.length()); }
2474 wxString& append(const wxScopedWCharBuffer& str)
2475 { return append(str.data(), str.length()); }
2476 wxString& append(const wxCStrData& str, size_t n)
2477 { return append(str.AsString(), 0, n); }
2478 wxString& append(const wxScopedCharBuffer& str, size_t n)
2479 { return append(str.data(), n); }
2480 wxString& append(const wxScopedWCharBuffer& str, size_t n)
2481 { return append(str.data(), n); }
2482
2483 // append n copies of ch
2484 wxString& append(size_t n, wxUniChar ch)
2485 {
2486 #if wxUSE_UNICODE_UTF8
2487 if ( !ch.IsAscii() )
2488 {
2489 wxSTRING_INVALIDATE_CACHED_LENGTH();
2490
2491 m_impl.append(wxStringOperations::EncodeNChars(n, ch));
2492 }
2493 else // ASCII
2494 #endif
2495 {
2496 wxSTRING_UPDATE_CACHED_LENGTH(n);
2497
2498 m_impl.append(n, (wxStringCharType)ch);
2499 }
2500
2501 return *this;
2502 }
2503
2504 wxString& append(size_t n, wxUniCharRef ch)
2505 { return append(n, wxUniChar(ch)); }
2506 wxString& append(size_t n, char ch)
2507 { return append(n, wxUniChar(ch)); }
2508 wxString& append(size_t n, unsigned char ch)
2509 { return append(n, wxUniChar(ch)); }
2510 wxString& append(size_t n, wchar_t ch)
2511 { return append(n, wxUniChar(ch)); }
2512
2513 // append from first to last
2514 wxString& append(const_iterator first, const_iterator last)
2515 {
2516 wxSTRING_INVALIDATE_CACHED_LENGTH();
2517
2518 m_impl.append(first.impl(), last.impl());
2519 return *this;
2520 }
2521 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2522 wxString& append(const char *first, const char *last)
2523 { return append(first, last - first); }
2524 wxString& append(const wchar_t *first, const wchar_t *last)
2525 { return append(first, last - first); }
2526 wxString& append(const wxCStrData& first, const wxCStrData& last)
2527 { return append(CreateConstIterator(first), CreateConstIterator(last)); }
2528 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2529
2530 // same as `this_string = str'
2531 wxString& assign(const wxString& str)
2532 {
2533 wxSTRING_SET_CACHED_LENGTH(str.length());
2534
2535 m_impl = str.m_impl;
2536
2537 return *this;
2538 }
2539
2540 wxString& assign(const wxString& str, size_t len)
2541 {
2542 wxSTRING_SET_CACHED_LENGTH(len);
2543
2544 m_impl.assign(str.m_impl, 0, str.LenToImpl(len));
2545
2546 return *this;
2547 }
2548
2549 // same as ` = str[pos..pos + n]
2550 wxString& assign(const wxString& str, size_t pos, size_t n)
2551 {
2552 size_t from, len;
2553 str.PosLenToImpl(pos, n, &from, &len);
2554 m_impl.assign(str.m_impl, from, len);
2555
2556 // it's important to call this after PosLenToImpl() above in case str is
2557 // the same string as this one
2558 wxSTRING_SET_CACHED_LENGTH(n);
2559
2560 return *this;
2561 }
2562
2563 // same as `= first n (or all if n == npos) characters of sz'
2564 wxString& assign(const char *sz)
2565 {
2566 wxSTRING_INVALIDATE_CACHE();
2567
2568 m_impl.assign(ImplStr(sz));
2569
2570 return *this;
2571 }
2572
2573 wxString& assign(const wchar_t *sz)
2574 {
2575 wxSTRING_INVALIDATE_CACHE();
2576
2577 m_impl.assign(ImplStr(sz));
2578
2579 return *this;
2580 }
2581
2582 wxString& assign(const char *sz, size_t n)
2583 {
2584 wxSTRING_SET_CACHED_LENGTH(n);
2585
2586 SubstrBufFromMB str(ImplStr(sz, n));
2587 m_impl.assign(str.data, str.len);
2588
2589 return *this;
2590 }
2591
2592 wxString& assign(const wchar_t *sz, size_t n)
2593 {
2594 wxSTRING_SET_CACHED_LENGTH(n);
2595
2596 SubstrBufFromWC str(ImplStr(sz, n));
2597 m_impl.assign(str.data, str.len);
2598
2599 return *this;
2600 }
2601
2602 wxString& assign(const wxCStrData& str)
2603 { return assign(str.AsString()); }
2604 wxString& assign(const wxScopedCharBuffer& str)
2605 { return assign(str.data(), str.length()); }
2606 wxString& assign(const wxScopedWCharBuffer& str)
2607 { return assign(str.data(), str.length()); }
2608 wxString& assign(const wxCStrData& str, size_t len)
2609 { return assign(str.AsString(), len); }
2610 wxString& assign(const wxScopedCharBuffer& str, size_t len)
2611 { return assign(str.data(), len); }
2612 wxString& assign(const wxScopedWCharBuffer& str, size_t len)
2613 { return assign(str.data(), len); }
2614
2615 // same as `= n copies of ch'
2616 wxString& assign(size_t n, wxUniChar ch)
2617 {
2618 wxSTRING_SET_CACHED_LENGTH(n);
2619
2620 #if wxUSE_UNICODE_UTF8
2621 if ( !ch.IsAscii() )
2622 m_impl.assign(wxStringOperations::EncodeNChars(n, ch));
2623 else
2624 #endif
2625 m_impl.assign(n, (wxStringCharType)ch);
2626
2627 return *this;
2628 }
2629
2630 wxString& assign(size_t n, wxUniCharRef ch)
2631 { return assign(n, wxUniChar(ch)); }
2632 wxString& assign(size_t n, char ch)
2633 { return assign(n, wxUniChar(ch)); }
2634 wxString& assign(size_t n, unsigned char ch)
2635 { return assign(n, wxUniChar(ch)); }
2636 wxString& assign(size_t n, wchar_t ch)
2637 { return assign(n, wxUniChar(ch)); }
2638
2639 // assign from first to last
2640 wxString& assign(const_iterator first, const_iterator last)
2641 {
2642 wxSTRING_INVALIDATE_CACHE();
2643
2644 m_impl.assign(first.impl(), last.impl());
2645
2646 return *this;
2647 }
2648 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2649 wxString& assign(const char *first, const char *last)
2650 { return assign(first, last - first); }
2651 wxString& assign(const wchar_t *first, const wchar_t *last)
2652 { return assign(first, last - first); }
2653 wxString& assign(const wxCStrData& first, const wxCStrData& last)
2654 { return assign(CreateConstIterator(first), CreateConstIterator(last)); }
2655 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2656
2657 // string comparison
2658 int compare(const wxString& str) const;
2659 int compare(const char* sz) const;
2660 int compare(const wchar_t* sz) const;
2661 int compare(const wxCStrData& str) const
2662 { return compare(str.AsString()); }
2663 int compare(const wxScopedCharBuffer& str) const
2664 { return compare(str.data()); }
2665 int compare(const wxScopedWCharBuffer& str) const
2666 { return compare(str.data()); }
2667 // comparison with a substring
2668 int compare(size_t nStart, size_t nLen, const wxString& str) const;
2669 // comparison of 2 substrings
2670 int compare(size_t nStart, size_t nLen,
2671 const wxString& str, size_t nStart2, size_t nLen2) const;
2672 // substring comparison with first nCount characters of sz
2673 int compare(size_t nStart, size_t nLen,
2674 const char* sz, size_t nCount = npos) const;
2675 int compare(size_t nStart, size_t nLen,
2676 const wchar_t* sz, size_t nCount = npos) const;
2677
2678 // insert another string
2679 wxString& insert(size_t nPos, const wxString& str)
2680 { insert(GetIterForNthChar(nPos), str.begin(), str.end()); return *this; }
2681 // insert n chars of str starting at nStart (in str)
2682 wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n)
2683 {
2684 wxSTRING_UPDATE_CACHED_LENGTH(n);
2685
2686 size_t from, len;
2687 str.PosLenToImpl(nStart, n, &from, &len);
2688 m_impl.insert(PosToImpl(nPos), str.m_impl, from, len);
2689
2690 return *this;
2691 }
2692
2693 // insert first n (or all if n == npos) characters of sz
2694 wxString& insert(size_t nPos, const char *sz)
2695 {
2696 wxSTRING_INVALIDATE_CACHE();
2697
2698 m_impl.insert(PosToImpl(nPos), ImplStr(sz));
2699
2700 return *this;
2701 }
2702
2703 wxString& insert(size_t nPos, const wchar_t *sz)
2704 {
2705 wxSTRING_INVALIDATE_CACHE();
2706
2707 m_impl.insert(PosToImpl(nPos), ImplStr(sz)); return *this;
2708 }
2709
2710 wxString& insert(size_t nPos, const char *sz, size_t n)
2711 {
2712 wxSTRING_UPDATE_CACHED_LENGTH(n);
2713
2714 SubstrBufFromMB str(ImplStr(sz, n));
2715 m_impl.insert(PosToImpl(nPos), str.data, str.len);
2716
2717 return *this;
2718 }
2719
2720 wxString& insert(size_t nPos, const wchar_t *sz, size_t n)
2721 {
2722 wxSTRING_UPDATE_CACHED_LENGTH(n);
2723
2724 SubstrBufFromWC str(ImplStr(sz, n));
2725 m_impl.insert(PosToImpl(nPos), str.data, str.len);
2726
2727 return *this;
2728 }
2729
2730 // insert n copies of ch
2731 wxString& insert(size_t nPos, size_t n, wxUniChar ch)
2732 {
2733 wxSTRING_UPDATE_CACHED_LENGTH(n);
2734
2735 #if wxUSE_UNICODE_UTF8
2736 if ( !ch.IsAscii() )
2737 m_impl.insert(PosToImpl(nPos), wxStringOperations::EncodeNChars(n, ch));
2738 else
2739 #endif
2740 m_impl.insert(PosToImpl(nPos), n, (wxStringCharType)ch);
2741 return *this;
2742 }
2743
2744 iterator insert(iterator it, wxUniChar ch)
2745 {
2746 wxSTRING_UPDATE_CACHED_LENGTH(1);
2747
2748 #if wxUSE_UNICODE_UTF8
2749 if ( !ch.IsAscii() )
2750 {
2751 size_t pos = IterToImplPos(it);
2752 m_impl.insert(pos, wxStringOperations::EncodeChar(ch));
2753 return iterator(this, m_impl.begin() + pos);
2754 }
2755 else
2756 #endif
2757 return iterator(this, m_impl.insert(it.impl(), (wxStringCharType)ch));
2758 }
2759
2760 void insert(iterator it, const_iterator first, const_iterator last)
2761 {
2762 wxSTRING_INVALIDATE_CACHE();
2763
2764 m_impl.insert(it.impl(), first.impl(), last.impl());
2765 }
2766
2767 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2768 void insert(iterator it, const char *first, const char *last)
2769 { insert(it - begin(), first, last - first); }
2770 void insert(iterator it, const wchar_t *first, const wchar_t *last)
2771 { insert(it - begin(), first, last - first); }
2772 void insert(iterator it, const wxCStrData& first, const wxCStrData& last)
2773 { insert(it, CreateConstIterator(first), CreateConstIterator(last)); }
2774 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2775
2776 void insert(iterator it, size_type n, wxUniChar ch)
2777 {
2778 wxSTRING_UPDATE_CACHED_LENGTH(n);
2779
2780 #if wxUSE_UNICODE_UTF8
2781 if ( !ch.IsAscii() )
2782 m_impl.insert(IterToImplPos(it), wxStringOperations::EncodeNChars(n, ch));
2783 else
2784 #endif
2785 m_impl.insert(it.impl(), n, (wxStringCharType)ch);
2786 }
2787
2788 // delete characters from nStart to nStart + nLen
2789 wxString& erase(size_type pos = 0, size_type n = npos)
2790 {
2791 wxSTRING_INVALIDATE_CACHE();
2792
2793 size_t from, len;
2794 PosLenToImpl(pos, n, &from, &len);
2795 m_impl.erase(from, len);
2796
2797 return *this;
2798 }
2799
2800 // delete characters from first up to last
2801 iterator erase(iterator first, iterator last)
2802 {
2803 wxSTRING_INVALIDATE_CACHE();
2804
2805 return iterator(this, m_impl.erase(first.impl(), last.impl()));
2806 }
2807
2808 iterator erase(iterator first)
2809 {
2810 wxSTRING_UPDATE_CACHED_LENGTH(-1);
2811
2812 return iterator(this, m_impl.erase(first.impl()));
2813 }
2814
2815 #ifdef wxSTRING_BASE_HASNT_CLEAR
2816 void clear() { erase(); }
2817 #else
2818 void clear()
2819 {
2820 wxSTRING_SET_CACHED_LENGTH(0);
2821
2822 m_impl.clear();
2823 }
2824 #endif
2825
2826 // replaces the substring of length nLen starting at nStart
2827 wxString& replace(size_t nStart, size_t nLen, const char* sz)
2828 {
2829 wxSTRING_INVALIDATE_CACHE();
2830
2831 size_t from, len;
2832 PosLenToImpl(nStart, nLen, &from, &len);
2833 m_impl.replace(from, len, ImplStr(sz));
2834
2835 return *this;
2836 }
2837
2838 wxString& replace(size_t nStart, size_t nLen, const wchar_t* sz)
2839 {
2840 wxSTRING_INVALIDATE_CACHE();
2841
2842 size_t from, len;
2843 PosLenToImpl(nStart, nLen, &from, &len);
2844 m_impl.replace(from, len, ImplStr(sz));
2845
2846 return *this;
2847 }
2848
2849 // replaces the substring of length nLen starting at nStart
2850 wxString& replace(size_t nStart, size_t nLen, const wxString& str)
2851 {
2852 wxSTRING_INVALIDATE_CACHE();
2853
2854 size_t from, len;
2855 PosLenToImpl(nStart, nLen, &from, &len);
2856 m_impl.replace(from, len, str.m_impl);
2857
2858 return *this;
2859 }
2860
2861 // replaces the substring with nCount copies of ch
2862 wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxUniChar ch)
2863 {
2864 wxSTRING_INVALIDATE_CACHE();
2865
2866 size_t from, len;
2867 PosLenToImpl(nStart, nLen, &from, &len);
2868 #if wxUSE_UNICODE_UTF8
2869 if ( !ch.IsAscii() )
2870 m_impl.replace(from, len, wxStringOperations::EncodeNChars(nCount, ch));
2871 else
2872 #endif
2873 m_impl.replace(from, len, nCount, (wxStringCharType)ch);
2874
2875 return *this;
2876 }
2877
2878 // replaces a substring with another substring
2879 wxString& replace(size_t nStart, size_t nLen,
2880 const wxString& str, size_t nStart2, size_t nLen2)
2881 {
2882 wxSTRING_INVALIDATE_CACHE();
2883
2884 size_t from, len;
2885 PosLenToImpl(nStart, nLen, &from, &len);
2886
2887 size_t from2, len2;
2888 str.PosLenToImpl(nStart2, nLen2, &from2, &len2);
2889
2890 m_impl.replace(from, len, str.m_impl, from2, len2);
2891
2892 return *this;
2893 }
2894
2895 // replaces the substring with first nCount chars of sz
2896 wxString& replace(size_t nStart, size_t nLen,
2897 const char* sz, size_t nCount)
2898 {
2899 wxSTRING_INVALIDATE_CACHE();
2900
2901 size_t from, len;
2902 PosLenToImpl(nStart, nLen, &from, &len);
2903
2904 SubstrBufFromMB str(ImplStr(sz, nCount));
2905
2906 m_impl.replace(from, len, str.data, str.len);
2907
2908 return *this;
2909 }
2910
2911 wxString& replace(size_t nStart, size_t nLen,
2912 const wchar_t* sz, size_t nCount)
2913 {
2914 wxSTRING_INVALIDATE_CACHE();
2915
2916 size_t from, len;
2917 PosLenToImpl(nStart, nLen, &from, &len);
2918
2919 SubstrBufFromWC str(ImplStr(sz, nCount));
2920
2921 m_impl.replace(from, len, str.data, str.len);
2922
2923 return *this;
2924 }
2925
2926 wxString& replace(size_t nStart, size_t nLen,
2927 const wxString& s, size_t nCount)
2928 {
2929 wxSTRING_INVALIDATE_CACHE();
2930
2931 size_t from, len;
2932 PosLenToImpl(nStart, nLen, &from, &len);
2933 m_impl.replace(from, len, s.m_impl.c_str(), s.LenToImpl(nCount));
2934
2935 return *this;
2936 }
2937
2938 wxString& replace(iterator first, iterator last, const char* s)
2939 {
2940 wxSTRING_INVALIDATE_CACHE();
2941
2942 m_impl.replace(first.impl(), last.impl(), ImplStr(s));
2943
2944 return *this;
2945 }
2946
2947 wxString& replace(iterator first, iterator last, const wchar_t* s)
2948 {
2949 wxSTRING_INVALIDATE_CACHE();
2950
2951 m_impl.replace(first.impl(), last.impl(), ImplStr(s));
2952
2953 return *this;
2954 }
2955
2956 wxString& replace(iterator first, iterator last, const char* s, size_type n)
2957 {
2958 wxSTRING_INVALIDATE_CACHE();
2959
2960 SubstrBufFromMB str(ImplStr(s, n));
2961 m_impl.replace(first.impl(), last.impl(), str.data, str.len);
2962
2963 return *this;
2964 }
2965
2966 wxString& replace(iterator first, iterator last, const wchar_t* s, size_type n)
2967 {
2968 wxSTRING_INVALIDATE_CACHE();
2969
2970 SubstrBufFromWC str(ImplStr(s, n));
2971 m_impl.replace(first.impl(), last.impl(), str.data, str.len);
2972
2973 return *this;
2974 }
2975
2976 wxString& replace(iterator first, iterator last, const wxString& s)
2977 {
2978 wxSTRING_INVALIDATE_CACHE();
2979
2980 m_impl.replace(first.impl(), last.impl(), s.m_impl);
2981
2982 return *this;
2983 }
2984
2985 wxString& replace(iterator first, iterator last, size_type n, wxUniChar ch)
2986 {
2987 wxSTRING_INVALIDATE_CACHE();
2988
2989 #if wxUSE_UNICODE_UTF8
2990 if ( !ch.IsAscii() )
2991 m_impl.replace(first.impl(), last.impl(),
2992 wxStringOperations::EncodeNChars(n, ch));
2993 else
2994 #endif
2995 m_impl.replace(first.impl(), last.impl(), n, (wxStringCharType)ch);
2996
2997 return *this;
2998 }
2999
3000 wxString& replace(iterator first, iterator last,
3001 const_iterator first1, const_iterator last1)
3002 {
3003 wxSTRING_INVALIDATE_CACHE();
3004
3005 m_impl.replace(first.impl(), last.impl(), first1.impl(), last1.impl());
3006
3007 return *this;
3008 }
3009
3010 wxString& replace(iterator first, iterator last,
3011 const char *first1, const char *last1)
3012 { replace(first, last, first1, last1 - first1); return *this; }
3013 wxString& replace(iterator first, iterator last,
3014 const wchar_t *first1, const wchar_t *last1)
3015 { replace(first, last, first1, last1 - first1); return *this; }
3016
3017 // swap two strings
3018 void swap(wxString& str)
3019 {
3020 #if wxUSE_STRING_POS_CACHE
3021 // we modify not only this string but also the other one directly so we
3022 // need to invalidate cache for both of them (we could also try to
3023 // exchange their cache entries but it seems unlikely to be worth it)
3024 InvalidateCache();
3025 str.InvalidateCache();
3026 #endif // wxUSE_STRING_POS_CACHE
3027
3028 m_impl.swap(str.m_impl);
3029 }
3030
3031 // find a substring
3032 size_t find(const wxString& str, size_t nStart = 0) const
3033 { return PosFromImpl(m_impl.find(str.m_impl, PosToImpl(nStart))); }
3034
3035 // find first n characters of sz
3036 size_t find(const char* sz, size_t nStart = 0, size_t n = npos) const
3037 {
3038 SubstrBufFromMB str(ImplStr(sz, n));
3039 return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len));
3040 }
3041 size_t find(const wchar_t* sz, size_t nStart = 0, size_t n = npos) const
3042 {
3043 SubstrBufFromWC str(ImplStr(sz, n));
3044 return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len));
3045 }
3046 size_t find(const wxScopedCharBuffer& s, size_t nStart = 0, size_t n = npos) const
3047 { return find(s.data(), nStart, n); }
3048 size_t find(const wxScopedWCharBuffer& s, size_t nStart = 0, size_t n = npos) const
3049 { return find(s.data(), nStart, n); }
3050 size_t find(const wxCStrData& s, size_t nStart = 0, size_t n = npos) const
3051 { return find(s.AsWChar(), nStart, n); }
3052
3053 // find the first occurrence of character ch after nStart
3054 size_t find(wxUniChar ch, size_t nStart = 0) const
3055 {
3056 #if wxUSE_UNICODE_UTF8
3057 if ( !ch.IsAscii() )
3058 return PosFromImpl(m_impl.find(wxStringOperations::EncodeChar(ch),
3059 PosToImpl(nStart)));
3060 else
3061 #endif
3062 return PosFromImpl(m_impl.find((wxStringCharType)ch,
3063 PosToImpl(nStart)));
3064
3065 }
3066 size_t find(wxUniCharRef ch, size_t nStart = 0) const
3067 { return find(wxUniChar(ch), nStart); }
3068 size_t find(char ch, size_t nStart = 0) const
3069 { return find(wxUniChar(ch), nStart); }
3070 size_t find(unsigned char ch, size_t nStart = 0) const
3071 { return find(wxUniChar(ch), nStart); }
3072 size_t find(wchar_t ch, size_t nStart = 0) const
3073 { return find(wxUniChar(ch), nStart); }
3074
3075 // rfind() family is exactly like find() but works right to left
3076
3077 // as find, but from the end
3078 size_t rfind(const wxString& str, size_t nStart = npos) const
3079 { return PosFromImpl(m_impl.rfind(str.m_impl, PosToImpl(nStart))); }
3080
3081 // as find, but from the end
3082 size_t rfind(const char* sz, size_t nStart = npos, size_t n = npos) const
3083 {
3084 SubstrBufFromMB str(ImplStr(sz, n));
3085 return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len));
3086 }
3087 size_t rfind(const wchar_t* sz, size_t nStart = npos, size_t n = npos) const
3088 {
3089 SubstrBufFromWC str(ImplStr(sz, n));
3090 return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len));
3091 }
3092 size_t rfind(const wxScopedCharBuffer& s, size_t nStart = npos, size_t n = npos) const
3093 { return rfind(s.data(), nStart, n); }
3094 size_t rfind(const wxScopedWCharBuffer& s, size_t nStart = npos, size_t n = npos) const
3095 { return rfind(s.data(), nStart, n); }
3096 size_t rfind(const wxCStrData& s, size_t nStart = npos, size_t n = npos) const
3097 { return rfind(s.AsWChar(), nStart, n); }
3098 // as find, but from the end
3099 size_t rfind(wxUniChar ch, size_t nStart = npos) const
3100 {
3101 #if wxUSE_UNICODE_UTF8
3102 if ( !ch.IsAscii() )
3103 return PosFromImpl(m_impl.rfind(wxStringOperations::EncodeChar(ch),
3104 PosToImpl(nStart)));
3105 else
3106 #endif
3107 return PosFromImpl(m_impl.rfind((wxStringCharType)ch,
3108 PosToImpl(nStart)));
3109 }
3110 size_t rfind(wxUniCharRef ch, size_t nStart = npos) const
3111 { return rfind(wxUniChar(ch), nStart); }
3112 size_t rfind(char ch, size_t nStart = npos) const
3113 { return rfind(wxUniChar(ch), nStart); }
3114 size_t rfind(unsigned char ch, size_t nStart = npos) const
3115 { return rfind(wxUniChar(ch), nStart); }
3116 size_t rfind(wchar_t ch, size_t nStart = npos) const
3117 { return rfind(wxUniChar(ch), nStart); }
3118
3119 // find first/last occurrence of any character (not) in the set:
3120 #if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
3121 // FIXME-UTF8: this is not entirely correct, because it doesn't work if
3122 // sizeof(wchar_t)==2 and surrogates are present in the string;
3123 // should we care? Probably not.
3124 size_t find_first_of(const wxString& str, size_t nStart = 0) const
3125 { return m_impl.find_first_of(str.m_impl, nStart); }
3126 size_t find_first_of(const char* sz, size_t nStart = 0) const
3127 { return m_impl.find_first_of(ImplStr(sz), nStart); }
3128 size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const
3129 { return m_impl.find_first_of(ImplStr(sz), nStart); }
3130 size_t find_first_of(const char* 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(const wchar_t* sz, size_t nStart, size_t n) const
3133 { return m_impl.find_first_of(ImplStr(sz), nStart, n); }
3134 size_t find_first_of(wxUniChar c, size_t nStart = 0) const
3135 { return m_impl.find_first_of((wxChar)c, nStart); }
3136
3137 size_t find_last_of(const wxString& str, size_t nStart = npos) const
3138 { return m_impl.find_last_of(str.m_impl, nStart); }
3139 size_t find_last_of(const char* sz, size_t nStart = npos) const
3140 { return m_impl.find_last_of(ImplStr(sz), nStart); }
3141 size_t find_last_of(const wchar_t* sz, size_t nStart = npos) const
3142 { return m_impl.find_last_of(ImplStr(sz), nStart); }
3143 size_t find_last_of(const char* 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(const wchar_t* sz, size_t nStart, size_t n) const
3146 { return m_impl.find_last_of(ImplStr(sz), nStart, n); }
3147 size_t find_last_of(wxUniChar c, size_t nStart = npos) const
3148 { return m_impl.find_last_of((wxChar)c, nStart); }
3149
3150 size_t find_first_not_of(const wxString& str, size_t nStart = 0) const
3151 { return m_impl.find_first_not_of(str.m_impl, nStart); }
3152 size_t find_first_not_of(const char* 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 wchar_t* sz, size_t nStart = 0) const
3155 { return m_impl.find_first_not_of(ImplStr(sz), nStart); }
3156 size_t find_first_not_of(const char* 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(const wchar_t* sz, size_t nStart, size_t n) const
3159 { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); }
3160 size_t find_first_not_of(wxUniChar c, size_t nStart = 0) const
3161 { return m_impl.find_first_not_of((wxChar)c, nStart); }
3162
3163 size_t find_last_not_of(const wxString& str, size_t nStart = npos) const
3164 { return m_impl.find_last_not_of(str.m_impl, nStart); }
3165 size_t find_last_not_of(const char* 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 wchar_t* sz, size_t nStart = npos) const
3168 { return m_impl.find_last_not_of(ImplStr(sz), nStart); }
3169 size_t find_last_not_of(const char* 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(const wchar_t* sz, size_t nStart, size_t n) const
3172 { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); }
3173 size_t find_last_not_of(wxUniChar c, size_t nStart = npos) const
3174 { return m_impl.find_last_not_of((wxChar)c, nStart); }
3175 #else
3176 // we can't use std::string implementation in UTF-8 build, because the
3177 // character sets would be interpreted wrongly:
3178
3179 // as strpbrk() but starts at nStart, returns npos if not found
3180 size_t find_first_of(const wxString& str, size_t nStart = 0) const
3181 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3182 { return find_first_of(str.wc_str(), nStart); }
3183 #else
3184 { return find_first_of(str.mb_str(), nStart); }
3185 #endif
3186 // same as above
3187 size_t find_first_of(const char* sz, size_t nStart = 0) const;
3188 size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const;
3189 size_t find_first_of(const char* sz, size_t nStart, size_t n) const;
3190 size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const;
3191 // same as find(char, size_t)
3192 size_t find_first_of(wxUniChar c, size_t nStart = 0) const
3193 { return find(c, nStart); }
3194 // find the last (starting from nStart) char from str in this string
3195 size_t find_last_of (const wxString& str, size_t nStart = npos) const
3196 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3197 { return find_last_of(str.wc_str(), nStart); }
3198 #else
3199 { return find_last_of(str.mb_str(), nStart); }
3200 #endif
3201 // same as above
3202 size_t find_last_of (const char* sz, size_t nStart = npos) const;
3203 size_t find_last_of (const wchar_t* sz, size_t nStart = npos) const;
3204 size_t find_last_of(const char* sz, size_t nStart, size_t n) const;
3205 size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const;
3206 // same as above
3207 size_t find_last_of(wxUniChar c, size_t nStart = npos) const
3208 { return rfind(c, nStart); }
3209
3210 // find first/last occurrence of any character not in the set
3211
3212 // as strspn() (starting from nStart), returns npos on failure
3213 size_t find_first_not_of(const wxString& str, size_t nStart = 0) const
3214 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3215 { return find_first_not_of(str.wc_str(), nStart); }
3216 #else
3217 { return find_first_not_of(str.mb_str(), nStart); }
3218 #endif
3219 // same as above
3220 size_t find_first_not_of(const char* sz, size_t nStart = 0) const;
3221 size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const;
3222 size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const;
3223 size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const;
3224 // same as above
3225 size_t find_first_not_of(wxUniChar ch, size_t nStart = 0) const;
3226 // as strcspn()
3227 size_t find_last_not_of(const wxString& str, size_t nStart = npos) const
3228 #if wxUSE_UNICODE // FIXME-UTF8: temporary
3229 { return find_last_not_of(str.wc_str(), nStart); }
3230 #else
3231 { return find_last_not_of(str.mb_str(), nStart); }
3232 #endif
3233 // same as above
3234 size_t find_last_not_of(const char* sz, size_t nStart = npos) const;
3235 size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const;
3236 size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const;
3237 size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const;
3238 // same as above
3239 size_t find_last_not_of(wxUniChar ch, size_t nStart = npos) const;
3240 #endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not
3241
3242 // provide char/wchar_t/wxUniCharRef overloads for char-finding functions
3243 // above to resolve ambiguities:
3244 size_t find_first_of(wxUniCharRef ch, size_t nStart = 0) const
3245 { return find_first_of(wxUniChar(ch), nStart); }
3246 size_t find_first_of(char ch, size_t nStart = 0) const
3247 { return find_first_of(wxUniChar(ch), nStart); }
3248 size_t find_first_of(unsigned char ch, size_t nStart = 0) const
3249 { return find_first_of(wxUniChar(ch), nStart); }
3250 size_t find_first_of(wchar_t ch, size_t nStart = 0) const
3251 { return find_first_of(wxUniChar(ch), nStart); }
3252 size_t find_last_of(wxUniCharRef ch, size_t nStart = npos) const
3253 { return find_last_of(wxUniChar(ch), nStart); }
3254 size_t find_last_of(char ch, size_t nStart = npos) const
3255 { return find_last_of(wxUniChar(ch), nStart); }
3256 size_t find_last_of(unsigned char ch, size_t nStart = npos) const
3257 { return find_last_of(wxUniChar(ch), nStart); }
3258 size_t find_last_of(wchar_t ch, size_t nStart = npos) const
3259 { return find_last_of(wxUniChar(ch), nStart); }
3260 size_t find_first_not_of(wxUniCharRef ch, size_t nStart = 0) const
3261 { return find_first_not_of(wxUniChar(ch), nStart); }
3262 size_t find_first_not_of(char ch, size_t nStart = 0) const
3263 { return find_first_not_of(wxUniChar(ch), nStart); }
3264 size_t find_first_not_of(unsigned char ch, size_t nStart = 0) const
3265 { return find_first_not_of(wxUniChar(ch), nStart); }
3266 size_t find_first_not_of(wchar_t ch, size_t nStart = 0) const
3267 { return find_first_not_of(wxUniChar(ch), nStart); }
3268 size_t find_last_not_of(wxUniCharRef ch, size_t nStart = npos) const
3269 { return find_last_not_of(wxUniChar(ch), nStart); }
3270 size_t find_last_not_of(char ch, size_t nStart = npos) const
3271 { return find_last_not_of(wxUniChar(ch), nStart); }
3272 size_t find_last_not_of(unsigned char ch, size_t nStart = npos) const
3273 { return find_last_not_of(wxUniChar(ch), nStart); }
3274 size_t find_last_not_of(wchar_t ch, size_t nStart = npos) const
3275 { return find_last_not_of(wxUniChar(ch), nStart); }
3276
3277 // and additional overloads for the versions taking strings:
3278 size_t find_first_of(const wxCStrData& sz, size_t nStart = 0) const
3279 { return find_first_of(sz.AsString(), nStart); }
3280 size_t find_first_of(const wxScopedCharBuffer& sz, size_t nStart = 0) const
3281 { return find_first_of(sz.data(), nStart); }
3282 size_t find_first_of(const wxScopedWCharBuffer& sz, size_t nStart = 0) const
3283 { return find_first_of(sz.data(), nStart); }
3284 size_t find_first_of(const wxCStrData& sz, size_t nStart, size_t n) const
3285 { return find_first_of(sz.AsWChar(), nStart, n); }
3286 size_t find_first_of(const wxScopedCharBuffer& sz, size_t nStart, size_t n) const
3287 { return find_first_of(sz.data(), nStart, n); }
3288 size_t find_first_of(const wxScopedWCharBuffer& sz, size_t nStart, size_t n) const
3289 { return find_first_of(sz.data(), nStart, n); }
3290
3291 size_t find_last_of(const wxCStrData& sz, size_t nStart = 0) const
3292 { return find_last_of(sz.AsString(), nStart); }
3293 size_t find_last_of(const wxScopedCharBuffer& sz, size_t nStart = 0) const
3294 { return find_last_of(sz.data(), nStart); }
3295 size_t find_last_of(const wxScopedWCharBuffer& sz, size_t nStart = 0) const
3296 { return find_last_of(sz.data(), nStart); }
3297 size_t find_last_of(const wxCStrData& sz, size_t nStart, size_t n) const
3298 { return find_last_of(sz.AsWChar(), nStart, n); }
3299 size_t find_last_of(const wxScopedCharBuffer& sz, size_t nStart, size_t n) const
3300 { return find_last_of(sz.data(), nStart, n); }
3301 size_t find_last_of(const wxScopedWCharBuffer& sz, size_t nStart, size_t n) const
3302 { return find_last_of(sz.data(), nStart, n); }
3303
3304 size_t find_first_not_of(const wxCStrData& sz, size_t nStart = 0) const
3305 { return find_first_not_of(sz.AsString(), nStart); }
3306 size_t find_first_not_of(const wxScopedCharBuffer& sz, size_t nStart = 0) const
3307 { return find_first_not_of(sz.data(), nStart); }
3308 size_t find_first_not_of(const wxScopedWCharBuffer& sz, size_t nStart = 0) const
3309 { return find_first_not_of(sz.data(), nStart); }
3310 size_t find_first_not_of(const wxCStrData& sz, size_t nStart, size_t n) const
3311 { return find_first_not_of(sz.AsWChar(), nStart, n); }
3312 size_t find_first_not_of(const wxScopedCharBuffer& sz, size_t nStart, size_t n) const
3313 { return find_first_not_of(sz.data(), nStart, n); }
3314 size_t find_first_not_of(const wxScopedWCharBuffer& sz, size_t nStart, size_t n) const
3315 { return find_first_not_of(sz.data(), nStart, n); }
3316
3317 size_t find_last_not_of(const wxCStrData& sz, size_t nStart = 0) const
3318 { return find_last_not_of(sz.AsString(), nStart); }
3319 size_t find_last_not_of(const wxScopedCharBuffer& sz, size_t nStart = 0) const
3320 { return find_last_not_of(sz.data(), nStart); }
3321 size_t find_last_not_of(const wxScopedWCharBuffer& sz, size_t nStart = 0) const
3322 { return find_last_not_of(sz.data(), nStart); }
3323 size_t find_last_not_of(const wxCStrData& sz, size_t nStart, size_t n) const
3324 { return find_last_not_of(sz.AsWChar(), nStart, n); }
3325 size_t find_last_not_of(const wxScopedCharBuffer& sz, size_t nStart, size_t n) const
3326 { return find_last_not_of(sz.data(), nStart, n); }
3327 size_t find_last_not_of(const wxScopedWCharBuffer& sz, size_t nStart, size_t n) const
3328 { return find_last_not_of(sz.data(), nStart, n); }
3329
3330 // string += string
3331 wxString& operator+=(const wxString& s)
3332 {
3333 wxSTRING_INVALIDATE_CACHED_LENGTH();
3334
3335 m_impl += s.m_impl;
3336 return *this;
3337 }
3338 // string += C string
3339 wxString& operator+=(const char *psz)
3340 {
3341 wxSTRING_INVALIDATE_CACHED_LENGTH();
3342
3343 m_impl += ImplStr(psz);
3344 return *this;
3345 }
3346 wxString& operator+=(const wchar_t *pwz)
3347 {
3348 wxSTRING_INVALIDATE_CACHED_LENGTH();
3349
3350 m_impl += ImplStr(pwz);
3351 return *this;
3352 }
3353 wxString& operator+=(const wxCStrData& s)
3354 {
3355 wxSTRING_INVALIDATE_CACHED_LENGTH();
3356
3357 m_impl += s.AsString().m_impl;
3358 return *this;
3359 }
3360 wxString& operator+=(const wxScopedCharBuffer& s)
3361 { return append(s); }
3362 wxString& operator+=(const wxScopedWCharBuffer& s)
3363 { return append(s); }
3364 // string += char
3365 wxString& operator+=(wxUniChar ch)
3366 {
3367 wxSTRING_UPDATE_CACHED_LENGTH(1);
3368
3369 #if wxUSE_UNICODE_UTF8
3370 if ( !ch.IsAscii() )
3371 m_impl += wxStringOperations::EncodeChar(ch);
3372 else
3373 #endif
3374 m_impl += (wxStringCharType)ch;
3375 return *this;
3376 }
3377 wxString& operator+=(wxUniCharRef ch) { return *this += wxUniChar(ch); }
3378 wxString& operator+=(int ch) { return *this += wxUniChar(ch); }
3379 wxString& operator+=(char ch) { return *this += wxUniChar(ch); }
3380 wxString& operator+=(unsigned char ch) { return *this += wxUniChar(ch); }
3381 wxString& operator+=(wchar_t ch) { return *this += wxUniChar(ch); }
3382
3383 private:
3384 #if !wxUSE_STL_BASED_WXSTRING
3385 // helpers for wxStringBuffer and wxStringBufferLength
3386 wxStringCharType *DoGetWriteBuf(size_t nLen)
3387 {
3388 return m_impl.DoGetWriteBuf(nLen);
3389 }
3390
3391 void DoUngetWriteBuf()
3392 {
3393 wxSTRING_INVALIDATE_CACHE();
3394
3395 m_impl.DoUngetWriteBuf();
3396 }
3397
3398 void DoUngetWriteBuf(size_t nLen)
3399 {
3400 wxSTRING_SET_CACHED_LENGTH(nLen);
3401
3402 m_impl.DoUngetWriteBuf(nLen);
3403 }
3404 #endif // !wxUSE_STL_BASED_WXSTRING
3405
3406 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
3407 #if !wxUSE_UTF8_LOCALE_ONLY
3408 int DoPrintfWchar(const wxChar *format, ...);
3409 static wxString DoFormatWchar(const wxChar *format, ...);
3410 #endif
3411 #if wxUSE_UNICODE_UTF8
3412 int DoPrintfUtf8(const char *format, ...);
3413 static wxString DoFormatUtf8(const char *format, ...);
3414 #endif
3415 #endif
3416
3417 #if !wxUSE_STL_BASED_WXSTRING
3418 // check string's data validity
3419 bool IsValid() const { return m_impl.GetStringData()->IsValid(); }
3420 #endif
3421
3422 private:
3423 wxStringImpl m_impl;
3424
3425 // buffers for compatibility conversion from (char*)c_str() and
3426 // (wchar_t*)c_str():
3427 // FIXME-UTF8: bechmark various approaches to keeping compatibility buffers
3428 template<typename T>
3429 struct ConvertedBuffer
3430 {
3431 ConvertedBuffer() : m_buf(NULL) {}
3432 ~ConvertedBuffer()
3433 { free(m_buf); }
3434
3435 operator T*() const { return m_buf; }
3436
3437 ConvertedBuffer& operator=(T *str)
3438 {
3439 free(m_buf);
3440 m_buf = str;
3441 return *this;
3442 }
3443
3444 T *m_buf;
3445 };
3446 #if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
3447 ConvertedBuffer<char> m_convertedToChar;
3448 #endif
3449 #if !wxUSE_UNICODE_WCHAR
3450 ConvertedBuffer<wchar_t> m_convertedToWChar;
3451 #endif
3452
3453 #if wxUSE_UNICODE_UTF8
3454 // FIXME-UTF8: (try to) move this elsewhere (TLS) or solve differently
3455 // assigning to character pointer to by wxString::interator may
3456 // change the underlying wxStringImpl iterator, so we have to
3457 // keep track of all iterators and update them as necessary:
3458 struct wxStringIteratorNodeHead
3459 {
3460 wxStringIteratorNodeHead() : ptr(NULL) {}
3461 wxStringIteratorNode *ptr;
3462
3463 // copying is disallowed as it would result in more than one pointer into
3464 // the same linked list
3465 wxDECLARE_NO_COPY_CLASS(wxStringIteratorNodeHead);
3466 };
3467
3468 wxStringIteratorNodeHead m_iterators;
3469
3470 friend class WXDLLIMPEXP_FWD_BASE wxStringIteratorNode;
3471 friend class WXDLLIMPEXP_FWD_BASE wxUniCharRef;
3472 #endif // wxUSE_UNICODE_UTF8
3473
3474 friend class WXDLLIMPEXP_FWD_BASE wxCStrData;
3475 friend class wxStringInternalBuffer;
3476 friend class wxStringInternalBufferLength;
3477 };
3478
3479 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
3480 #pragma warning (default:4275)
3481 #endif
3482
3483 // string iterator operators that satisfy STL Random Access Iterator
3484 // requirements:
3485 inline wxString::iterator operator+(ptrdiff_t n, wxString::iterator i)
3486 { return i + n; }
3487 inline wxString::const_iterator operator+(ptrdiff_t n, wxString::const_iterator i)
3488 { return i + n; }
3489 inline wxString::reverse_iterator operator+(ptrdiff_t n, wxString::reverse_iterator i)
3490 { return i + n; }
3491 inline wxString::const_reverse_iterator operator+(ptrdiff_t n, wxString::const_reverse_iterator i)
3492 { return i + n; }
3493
3494 // notice that even though for many compilers the friend declarations above are
3495 // enough, from the point of view of C++ standard we must have the declarations
3496 // here as friend ones are not injected in the enclosing namespace and without
3497 // them the code fails to compile with conforming compilers such as xlC or g++4
3498 wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2);
3499 wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const char *psz);
3500 wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wchar_t *pwz);
3501 wxString WXDLLIMPEXP_BASE operator+(const char *psz, const wxString& string);
3502 wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz, const wxString& string);
3503
3504 wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch);
3505 wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string);
3506
3507 inline wxString operator+(const wxString& string, wxUniCharRef ch)
3508 { return string + (wxUniChar)ch; }
3509 inline wxString operator+(const wxString& string, char ch)
3510 { return string + wxUniChar(ch); }
3511 inline wxString operator+(const wxString& string, wchar_t ch)
3512 { return string + wxUniChar(ch); }
3513 inline wxString operator+(wxUniCharRef ch, const wxString& string)
3514 { return (wxUniChar)ch + string; }
3515 inline wxString operator+(char ch, const wxString& string)
3516 { return wxUniChar(ch) + string; }
3517 inline wxString operator+(wchar_t ch, const wxString& string)
3518 { return wxUniChar(ch) + string; }
3519
3520
3521 #define wxGetEmptyString() wxString()
3522
3523 // ----------------------------------------------------------------------------
3524 // helper functions which couldn't be defined inline
3525 // ----------------------------------------------------------------------------
3526
3527 namespace wxPrivate
3528 {
3529
3530 #if wxUSE_UNICODE_WCHAR
3531
3532 template <>
3533 struct wxStringAsBufHelper<char>
3534 {
3535 static wxScopedCharBuffer Get(const wxString& s, size_t *len)
3536 {
3537 wxScopedCharBuffer buf(s.mb_str());
3538 if ( len )
3539 *len = buf ? strlen(buf) : 0;
3540 return buf;
3541 }
3542 };
3543
3544 template <>
3545 struct wxStringAsBufHelper<wchar_t>
3546 {
3547 static wxScopedWCharBuffer Get(const wxString& s, size_t *len)
3548 {
3549 const size_t length = s.length();
3550 if ( len )
3551 *len = length;
3552 return wxScopedWCharBuffer::CreateNonOwned(s.wx_str(), length);
3553 }
3554 };
3555
3556 #elif wxUSE_UNICODE_UTF8
3557
3558 template <>
3559 struct wxStringAsBufHelper<char>
3560 {
3561 static wxScopedCharBuffer Get(const wxString& s, size_t *len)
3562 {
3563 const size_t length = s.utf8_length();
3564 if ( len )
3565 *len = length;
3566 return wxScopedCharBuffer::CreateNonOwned(s.wx_str(), length);
3567 }
3568 };
3569
3570 template <>
3571 struct wxStringAsBufHelper<wchar_t>
3572 {
3573 static wxScopedWCharBuffer Get(const wxString& s, size_t *len)
3574 {
3575 wxScopedWCharBuffer wbuf(s.wc_str());
3576 if ( len )
3577 *len = wxWcslen(wbuf);
3578 return wbuf;
3579 }
3580 };
3581
3582 #endif // Unicode build kind
3583
3584 } // namespace wxPrivate
3585
3586 // ----------------------------------------------------------------------------
3587 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
3588 // ----------------------------------------------------------------------------
3589
3590 #if !wxUSE_STL_BASED_WXSTRING
3591 // string buffer for direct access to string data in their native
3592 // representation:
3593 class wxStringInternalBuffer
3594 {
3595 public:
3596 typedef wxStringCharType CharType;
3597
3598 wxStringInternalBuffer(wxString& str, size_t lenWanted = 1024)
3599 : m_str(str), m_buf(NULL)
3600 { m_buf = m_str.DoGetWriteBuf(lenWanted); }
3601
3602 ~wxStringInternalBuffer() { m_str.DoUngetWriteBuf(); }
3603
3604 operator wxStringCharType*() const { return m_buf; }
3605
3606 private:
3607 wxString& m_str;
3608 wxStringCharType *m_buf;
3609
3610 wxDECLARE_NO_COPY_CLASS(wxStringInternalBuffer);
3611 };
3612
3613 class wxStringInternalBufferLength
3614 {
3615 public:
3616 typedef wxStringCharType CharType;
3617
3618 wxStringInternalBufferLength(wxString& str, size_t lenWanted = 1024)
3619 : m_str(str), m_buf(NULL), m_len(0), m_lenSet(false)
3620 {
3621 m_buf = m_str.DoGetWriteBuf(lenWanted);
3622 wxASSERT(m_buf != NULL);
3623 }
3624
3625 ~wxStringInternalBufferLength()
3626 {
3627 wxASSERT(m_lenSet);
3628 m_str.DoUngetWriteBuf(m_len);
3629 }
3630
3631 operator wxStringCharType*() const { return m_buf; }
3632 void SetLength(size_t length) { m_len = length; m_lenSet = true; }
3633
3634 private:
3635 wxString& m_str;
3636 wxStringCharType *m_buf;
3637 size_t m_len;
3638 bool m_lenSet;
3639
3640 wxDECLARE_NO_COPY_CLASS(wxStringInternalBufferLength);
3641 };
3642
3643 #endif // !wxUSE_STL_BASED_WXSTRING
3644
3645 template<typename T>
3646 class wxStringTypeBufferBase
3647 {
3648 public:
3649 typedef T CharType;
3650
3651 wxStringTypeBufferBase(wxString& str, size_t lenWanted = 1024)
3652 : m_str(str), m_buf(lenWanted)
3653 {
3654 // for compatibility with old wxStringBuffer which provided direct
3655 // access to wxString internal buffer, initialize ourselves with the
3656 // string initial contents
3657
3658 // FIXME-VC6: remove the ugly (CharType *)NULL and use normal
3659 // tchar_str<CharType>
3660 size_t len;
3661 const wxCharTypeBuffer<CharType> buf(str.tchar_str(&len, (CharType *)NULL));
3662 if ( buf )
3663 {
3664 if ( len > lenWanted )
3665 {
3666 // in this case there is not enough space for terminating NUL,
3667 // ensure that we still put it there
3668 m_buf.data()[lenWanted] = 0;
3669 len = lenWanted - 1;
3670 }
3671
3672 memcpy(m_buf.data(), buf, (len + 1)*sizeof(CharType));
3673 }
3674 //else: conversion failed, this can happen when trying to get Unicode
3675 // string contents into a char string
3676 }
3677
3678 operator CharType*() { return m_buf.data(); }
3679
3680 protected:
3681 wxString& m_str;
3682 wxCharTypeBuffer<CharType> m_buf;
3683 };
3684
3685 template<typename T>
3686 class wxStringTypeBufferLengthBase : public wxStringTypeBufferBase<T>
3687 {
3688 public:
3689 wxStringTypeBufferLengthBase(wxString& str, size_t lenWanted = 1024)
3690 : wxStringTypeBufferBase<T>(str, lenWanted),
3691 m_len(0),
3692 m_lenSet(false)
3693 { }
3694
3695 ~wxStringTypeBufferLengthBase()
3696 {
3697 wxASSERT_MSG( this->m_lenSet, "forgot to call SetLength()" );
3698 }
3699
3700 void SetLength(size_t length) { m_len = length; m_lenSet = true; }
3701
3702 protected:
3703 size_t m_len;
3704 bool m_lenSet;
3705 };
3706
3707 template<typename T>
3708 class wxStringTypeBuffer : public wxStringTypeBufferBase<T>
3709 {
3710 public:
3711 wxStringTypeBuffer(wxString& str, size_t lenWanted = 1024)
3712 : wxStringTypeBufferBase<T>(str, lenWanted)
3713 { }
3714
3715 ~wxStringTypeBuffer()
3716 {
3717 this->m_str.assign(this->m_buf.data());
3718 }
3719
3720 wxDECLARE_NO_COPY_CLASS(wxStringTypeBuffer);
3721 };
3722
3723 template<typename T>
3724 class wxStringTypeBufferLength : public wxStringTypeBufferLengthBase<T>
3725 {
3726 public:
3727 wxStringTypeBufferLength(wxString& str, size_t lenWanted = 1024)
3728 : wxStringTypeBufferLengthBase<T>(str, lenWanted)
3729 { }
3730
3731 ~wxStringTypeBufferLength()
3732 {
3733 this->m_str.assign(this->m_buf.data(), this->m_len);
3734 }
3735
3736 wxDECLARE_NO_COPY_CLASS(wxStringTypeBufferLength);
3737 };
3738
3739 #if wxUSE_STL_BASED_WXSTRING
3740
3741 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase<wxStringCharType> )
3742
3743 class wxStringInternalBuffer : public wxStringTypeBufferBase<wxStringCharType>
3744 {
3745 public:
3746 wxStringInternalBuffer(wxString& str, size_t lenWanted = 1024)
3747 : wxStringTypeBufferBase<wxStringCharType>(str, lenWanted) {}
3748 ~wxStringInternalBuffer()
3749 { m_str.m_impl.assign(m_buf.data()); }
3750
3751 wxDECLARE_NO_COPY_CLASS(wxStringInternalBuffer);
3752 };
3753
3754 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE(
3755 wxStringTypeBufferLengthBase<wxStringCharType> )
3756
3757 class wxStringInternalBufferLength
3758 : public wxStringTypeBufferLengthBase<wxStringCharType>
3759 {
3760 public:
3761 wxStringInternalBufferLength(wxString& str, size_t lenWanted = 1024)
3762 : wxStringTypeBufferLengthBase<wxStringCharType>(str, lenWanted) {}
3763
3764 ~wxStringInternalBufferLength()
3765 {
3766 m_str.m_impl.assign(m_buf.data(), m_len);
3767 }
3768
3769 wxDECLARE_NO_COPY_CLASS(wxStringInternalBufferLength);
3770 };
3771
3772 #endif // wxUSE_STL_BASED_WXSTRING
3773
3774
3775 #if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
3776 typedef wxStringTypeBuffer<wxChar> wxStringBuffer;
3777 typedef wxStringTypeBufferLength<wxChar> wxStringBufferLength;
3778 #else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
3779 typedef wxStringInternalBuffer wxStringBuffer;
3780 typedef wxStringInternalBufferLength wxStringBufferLength;
3781 #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
3782
3783 #if wxUSE_UNICODE_UTF8
3784 typedef wxStringInternalBuffer wxUTF8StringBuffer;
3785 typedef wxStringInternalBufferLength wxUTF8StringBufferLength;
3786 #elif wxUSE_UNICODE_WCHAR
3787
3788 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase<char> )
3789
3790 // Note about inlined dtors in the classes below: this is done not for
3791 // performance reasons but just to avoid linking errors in the MSVC DLL build
3792 // under Windows: if a class has non-inline methods it must be declared as
3793 // being DLL-exported but, due to an extremely interesting feature of MSVC 7
3794 // and later, any template class which is used as a base of a DLL-exported
3795 // class is implicitly made DLL-exported too, as explained at the bottom of
3796 // http://msdn.microsoft.com/en-us/library/twa2aw10.aspx (just to confirm: yes,
3797 // _inheriting_ from a class can change whether it is being exported from DLL)
3798 //
3799 // But this results in link errors because the base template class is not DLL-
3800 // exported, whether it is declared with WXDLLIMPEXP_BASE or not, because it
3801 // does have only inline functions. So the simplest fix is to just make all the
3802 // functions of these classes inline too.
3803
3804 class wxUTF8StringBuffer : public wxStringTypeBufferBase<char>
3805 {
3806 public:
3807 wxUTF8StringBuffer(wxString& str, size_t lenWanted = 1024)
3808 : wxStringTypeBufferBase<char>(str, lenWanted) {}
3809 ~wxUTF8StringBuffer()
3810 {
3811 wxMBConvStrictUTF8 conv;
3812 size_t wlen = conv.ToWChar(NULL, 0, m_buf);
3813 wxCHECK_RET( wlen != wxCONV_FAILED, "invalid UTF-8 data in string buffer?" );
3814
3815 wxStringInternalBuffer wbuf(m_str, wlen);
3816 conv.ToWChar(wbuf, wlen, m_buf);
3817 }
3818
3819 wxDECLARE_NO_COPY_CLASS(wxUTF8StringBuffer);
3820 };
3821
3822 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferLengthBase<char> )
3823
3824 class wxUTF8StringBufferLength : public wxStringTypeBufferLengthBase<char>
3825 {
3826 public:
3827 wxUTF8StringBufferLength(wxString& str, size_t lenWanted = 1024)
3828 : wxStringTypeBufferLengthBase<char>(str, lenWanted) {}
3829 ~wxUTF8StringBufferLength()
3830 {
3831 wxCHECK_RET(m_lenSet, "length not set");
3832
3833 wxMBConvStrictUTF8 conv;
3834 size_t wlen = conv.ToWChar(NULL, 0, m_buf, m_len);
3835 wxCHECK_RET( wlen != wxCONV_FAILED, "invalid UTF-8 data in string buffer?" );
3836
3837 wxStringInternalBufferLength wbuf(m_str, wlen);
3838 conv.ToWChar(wbuf, wlen, m_buf, m_len);
3839 wbuf.SetLength(wlen);
3840 }
3841
3842 wxDECLARE_NO_COPY_CLASS(wxUTF8StringBufferLength);
3843 };
3844 #endif // wxUSE_UNICODE_UTF8/wxUSE_UNICODE_WCHAR
3845
3846
3847 // ---------------------------------------------------------------------------
3848 // wxString comparison functions: operator versions are always case sensitive
3849 // ---------------------------------------------------------------------------
3850
3851 #define wxCMP_WXCHAR_STRING(p, s, op) 0 op s.Cmp(p)
3852
3853 wxDEFINE_ALL_COMPARISONS(const wxChar *, const wxString&, wxCMP_WXCHAR_STRING)
3854
3855 #undef wxCMP_WXCHAR_STRING
3856
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.IsSameAs(s2); }
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 inline bool operator>=(const wxString& s1, const wxString& s2)
3868 { return s1.Cmp(s2) >= 0; }
3869
3870 inline bool operator==(const wxString& s1, const wxCStrData& s2)
3871 { return s1 == s2.AsString(); }
3872 inline bool operator==(const wxCStrData& s1, const wxString& s2)
3873 { return s1.AsString() == s2; }
3874 inline bool operator!=(const wxString& s1, const wxCStrData& s2)
3875 { return s1 != s2.AsString(); }
3876 inline bool operator!=(const wxCStrData& s1, const wxString& s2)
3877 { return s1.AsString() != s2; }
3878
3879 inline bool operator==(const wxString& s1, const wxScopedWCharBuffer& s2)
3880 { return (s1.Cmp((const wchar_t *)s2) == 0); }
3881 inline bool operator==(const wxScopedWCharBuffer& s1, const wxString& s2)
3882 { return (s2.Cmp((const wchar_t *)s1) == 0); }
3883 inline bool operator!=(const wxString& s1, const wxScopedWCharBuffer& s2)
3884 { return (s1.Cmp((const wchar_t *)s2) != 0); }
3885 inline bool operator!=(const wxScopedWCharBuffer& s1, const wxString& s2)
3886 { return (s2.Cmp((const wchar_t *)s1) != 0); }
3887
3888 inline bool operator==(const wxString& s1, const wxScopedCharBuffer& s2)
3889 { return (s1.Cmp((const char *)s2) == 0); }
3890 inline bool operator==(const wxScopedCharBuffer& s1, const wxString& s2)
3891 { return (s2.Cmp((const char *)s1) == 0); }
3892 inline bool operator!=(const wxString& s1, const wxScopedCharBuffer& s2)
3893 { return (s1.Cmp((const char *)s2) != 0); }
3894 inline bool operator!=(const wxScopedCharBuffer& s1, const wxString& s2)
3895 { return (s2.Cmp((const char *)s1) != 0); }
3896
3897 inline wxString operator+(const wxString& string, const wxScopedWCharBuffer& buf)
3898 { return string + (const wchar_t *)buf; }
3899 inline wxString operator+(const wxScopedWCharBuffer& buf, const wxString& string)
3900 { return (const wchar_t *)buf + string; }
3901
3902 inline wxString operator+(const wxString& string, const wxScopedCharBuffer& buf)
3903 { return string + (const char *)buf; }
3904 inline wxString operator+(const wxScopedCharBuffer& buf, const wxString& string)
3905 { return (const char *)buf + string; }
3906
3907 // comparison with char
3908 inline bool operator==(const wxUniChar& c, const wxString& s) { return s.IsSameAs(c); }
3909 inline bool operator==(const wxUniCharRef& c, const wxString& s) { return s.IsSameAs(c); }
3910 inline bool operator==(char c, const wxString& s) { return s.IsSameAs(c); }
3911 inline bool operator==(wchar_t c, const wxString& s) { return s.IsSameAs(c); }
3912 inline bool operator==(int c, const wxString& s) { return s.IsSameAs(c); }
3913 inline bool operator==(const wxString& s, const wxUniChar& c) { return s.IsSameAs(c); }
3914 inline bool operator==(const wxString& s, const wxUniCharRef& c) { return s.IsSameAs(c); }
3915 inline bool operator==(const wxString& s, char c) { return s.IsSameAs(c); }
3916 inline bool operator==(const wxString& s, wchar_t c) { return s.IsSameAs(c); }
3917 inline bool operator!=(const wxUniChar& c, const wxString& s) { return !s.IsSameAs(c); }
3918 inline bool operator!=(const wxUniCharRef& c, const wxString& s) { return !s.IsSameAs(c); }
3919 inline bool operator!=(char c, const wxString& s) { return !s.IsSameAs(c); }
3920 inline bool operator!=(wchar_t c, const wxString& s) { return !s.IsSameAs(c); }
3921 inline bool operator!=(int c, const wxString& s) { return !s.IsSameAs(c); }
3922 inline bool operator!=(const wxString& s, const wxUniChar& c) { return !s.IsSameAs(c); }
3923 inline bool operator!=(const wxString& s, const wxUniCharRef& c) { return !s.IsSameAs(c); }
3924 inline bool operator!=(const wxString& s, char c) { return !s.IsSameAs(c); }
3925 inline bool operator!=(const wxString& s, wchar_t c) { return !s.IsSameAs(c); }
3926
3927 // comparison with C string in Unicode build
3928 #if wxUSE_UNICODE
3929
3930 #define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s
3931
3932 wxDEFINE_ALL_COMPARISONS(const char *, const wxString&, wxCMP_CHAR_STRING)
3933
3934 #undef wxCMP_CHAR_STRING
3935
3936 #endif // wxUSE_UNICODE
3937
3938 // we also need to provide the operators for comparison with wxCStrData to
3939 // resolve ambiguity between operator(const wxChar *,const wxString &) and
3940 // operator(const wxChar *, const wxChar *) for "p == s.c_str()"
3941 //
3942 // notice that these are (shallow) pointer comparisons, not (deep) string ones
3943 #define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar()
3944 #define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar()
3945
3946 wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData&, wxCMP_WCHAR_CSTRDATA)
3947 wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData&, wxCMP_CHAR_CSTRDATA)
3948
3949 #undef wxCMP_CHAR_CSTRDATA
3950 #undef wxCMP_WCHAR_CSTRDATA
3951
3952 // ---------------------------------------------------------------------------
3953 // Implementation only from here until the end of file
3954 // ---------------------------------------------------------------------------
3955
3956 #if wxUSE_STD_IOSTREAM
3957
3958 #include "wx/iosfwrap.h"
3959
3960 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxString&);
3961 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCStrData&);
3962 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxScopedCharBuffer&);
3963 #ifndef __BORLANDC__
3964 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxScopedWCharBuffer&);
3965 #endif
3966
3967 #if wxUSE_UNICODE && defined(HAVE_WOSTREAM)
3968
3969 WXDLLIMPEXP_BASE wxSTD wostream& operator<<(wxSTD wostream&, const wxString&);
3970 WXDLLIMPEXP_BASE wxSTD wostream& operator<<(wxSTD wostream&, const wxCStrData&);
3971 WXDLLIMPEXP_BASE wxSTD wostream& operator<<(wxSTD wostream&, const wxScopedWCharBuffer&);
3972
3973 #endif // wxUSE_UNICODE && defined(HAVE_WOSTREAM)
3974
3975 #endif // wxUSE_STD_IOSTREAM
3976
3977 // ---------------------------------------------------------------------------
3978 // wxCStrData implementation
3979 // ---------------------------------------------------------------------------
3980
3981 inline wxCStrData::wxCStrData(char *buf)
3982 : m_str(new wxString(buf)), m_offset(0), m_owned(true) {}
3983 inline wxCStrData::wxCStrData(wchar_t *buf)
3984 : m_str(new wxString(buf)), m_offset(0), m_owned(true) {}
3985
3986 inline wxCStrData::wxCStrData(const wxCStrData& data)
3987 : m_str(data.m_owned ? new wxString(*data.m_str) : data.m_str),
3988 m_offset(data.m_offset),
3989 m_owned(data.m_owned)
3990 {
3991 }
3992
3993 inline wxCStrData::~wxCStrData()
3994 {
3995 if ( m_owned )
3996 delete const_cast<wxString*>(m_str); // cast to silence warnings
3997 }
3998
3999 // simple cases for AsChar() and AsWChar(), the complicated ones are
4000 // in string.cpp
4001 #if wxUSE_UNICODE_WCHAR
4002 inline const wchar_t* wxCStrData::AsWChar() const
4003 {
4004 return m_str->wx_str() + m_offset;
4005 }
4006 #endif // wxUSE_UNICODE_WCHAR
4007
4008 #if !wxUSE_UNICODE
4009 inline const char* wxCStrData::AsChar() const
4010 {
4011 return m_str->wx_str() + m_offset;
4012 }
4013 #endif // !wxUSE_UNICODE
4014
4015 #if wxUSE_UTF8_LOCALE_ONLY
4016 inline const char* wxCStrData::AsChar() const
4017 {
4018 return wxStringOperations::AddToIter(m_str->wx_str(), m_offset);
4019 }
4020 #endif // wxUSE_UTF8_LOCALE_ONLY
4021
4022 inline const wxScopedCharBuffer wxCStrData::AsCharBuf() const
4023 {
4024 #if !wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY
4025 return wxScopedCharBuffer::CreateNonOwned(AsChar());
4026 #else
4027 return AsString().mb_str();
4028 #endif
4029 }
4030
4031 inline const wxScopedWCharBuffer wxCStrData::AsWCharBuf() const
4032 {
4033 #if wxUSE_UNICODE_WCHAR
4034 return wxScopedWCharBuffer::CreateNonOwned(AsWChar());
4035 #else
4036 return AsString().wc_str();
4037 #endif
4038 }
4039
4040 inline wxString wxCStrData::AsString() const
4041 {
4042 if ( m_offset == 0 )
4043 return *m_str;
4044 else
4045 return m_str->Mid(m_offset);
4046 }
4047
4048 inline const wxStringCharType *wxCStrData::AsInternal() const
4049 {
4050 #if wxUSE_UNICODE_UTF8
4051 return wxStringOperations::AddToIter(m_str->wx_str(), m_offset);
4052 #else
4053 return m_str->wx_str() + m_offset;
4054 #endif
4055 }
4056
4057 inline wxUniChar wxCStrData::operator*() const
4058 {
4059 if ( m_str->empty() )
4060 return wxUniChar(_T('\0'));
4061 else
4062 return (*m_str)[m_offset];
4063 }
4064
4065 inline wxUniChar wxCStrData::operator[](size_t n) const
4066 {
4067 // NB: we intentionally use operator[] and not at() here because the former
4068 // works for the terminating NUL while the latter does not
4069 return (*m_str)[m_offset + n];
4070 }
4071
4072 // ----------------------------------------------------------------------------
4073 // more wxCStrData operators
4074 // ----------------------------------------------------------------------------
4075
4076 // we need to define those to allow "size_t pos = p - s.c_str()" where p is
4077 // some pointer into the string
4078 inline size_t operator-(const char *p, const wxCStrData& cs)
4079 {
4080 return p - cs.AsChar();
4081 }
4082
4083 inline size_t operator-(const wchar_t *p, const wxCStrData& cs)
4084 {
4085 return p - cs.AsWChar();
4086 }
4087
4088 // ----------------------------------------------------------------------------
4089 // implementation of wx[W]CharBuffer inline methods using wxCStrData
4090 // ----------------------------------------------------------------------------
4091
4092 // FIXME-UTF8: move this to buffer.h
4093 inline wxCharBuffer::wxCharBuffer(const wxCStrData& cstr)
4094 : wxCharTypeBufferBase(cstr.AsCharBuf())
4095 {
4096 }
4097
4098 inline wxWCharBuffer::wxWCharBuffer(const wxCStrData& cstr)
4099 : wxCharTypeBufferBase(cstr.AsWCharBuf())
4100 {
4101 }
4102
4103 #if wxUSE_UNICODE_UTF8
4104 // ----------------------------------------------------------------------------
4105 // implementation of wxStringIteratorNode inline methods
4106 // ----------------------------------------------------------------------------
4107
4108 void wxStringIteratorNode::DoSet(const wxString *str,
4109 wxStringImpl::const_iterator *citer,
4110 wxStringImpl::iterator *iter)
4111 {
4112 m_prev = NULL;
4113 m_iter = iter;
4114 m_citer = citer;
4115 m_str = str;
4116 if ( str )
4117 {
4118 m_next = str->m_iterators.ptr;
4119 const_cast<wxString*>(m_str)->m_iterators.ptr = this;
4120 if ( m_next )
4121 m_next->m_prev = this;
4122 }
4123 else
4124 {
4125 m_next = NULL;
4126 }
4127 }
4128
4129 void wxStringIteratorNode::clear()
4130 {
4131 if ( m_next )
4132 m_next->m_prev = m_prev;
4133 if ( m_prev )
4134 m_prev->m_next = m_next;
4135 else if ( m_str ) // first in the list
4136 const_cast<wxString*>(m_str)->m_iterators.ptr = m_next;
4137
4138 m_next = m_prev = NULL;
4139 m_citer = NULL;
4140 m_iter = NULL;
4141 m_str = NULL;
4142 }
4143 #endif // wxUSE_UNICODE_UTF8
4144
4145 #if WXWIN_COMPATIBILITY_2_8
4146 // lot of code out there doesn't explicitly include wx/crt.h, but uses
4147 // CRT wrappers that are now declared in wx/wxcrt.h and wx/wxcrtvararg.h,
4148 // so let's include this header now that wxString is defined and it's safe
4149 // to do it:
4150 #include "wx/crt.h"
4151 #endif
4152
4153 // ----------------------------------------------------------------------------
4154 // Checks on wxString characters
4155 // ----------------------------------------------------------------------------
4156
4157 template<bool (T)(const wxUniChar& c)>
4158 inline bool wxStringCheck(const wxString& val)
4159 {
4160 for ( wxString::const_iterator i = val.begin();
4161 i != val.end();
4162 ++i )
4163 if (T(*i) == 0)
4164 return false;
4165 return true;
4166 }
4167
4168 #endif // _WX_WXSTRING_H_