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