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