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