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