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