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