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