fixed VC6 warnings about non-dllexported members in dllexported classes
[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 #if defined(__WXMAC__) || defined(__VISAGECPP__)
28 #include <ctype.h>
29 #endif
30
31 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
32 // problem in VACPP V4 with including stdlib.h multiple times
33 // strconv includes it anyway
34 # include <stdio.h>
35 # include <string.h>
36 # include <stdarg.h>
37 # include <limits.h>
38 #else
39 # include <string.h>
40 # include <stdio.h>
41 # include <stdarg.h>
42 # include <limits.h>
43 # include <stdlib.h>
44 #endif
45
46 #ifdef HAVE_STRCASECMP_IN_STRINGS_H
47 #include <strings.h> // for strcasecmp()
48 #endif // HAVE_STRCASECMP_IN_STRINGS_H
49
50 #ifdef __WXPALMOS__
51 #include <StringMgr.h>
52 #endif
53
54 #include "wx/wxcrtbase.h" // for wxChar, wxStrlen() etc.
55 #include "wx/strvararg.h"
56 #include "wx/buffer.h" // for wxCharBuffer
57 #include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes
58 #include "wx/stringimpl.h"
59 #include "wx/stringops.h"
60 #include "wx/unichar.h"
61
62 class WXDLLIMPEXP_BASE wxString;
63
64 // unless this symbol is predefined to disable the compatibility functions, do
65 // use them
66 #ifndef WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
67 #define WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER 1
68 #endif
69
70 // ---------------------------------------------------------------------------
71 // macros
72 // ---------------------------------------------------------------------------
73
74 // casts [unfortunately!] needed to call some broken functions which require
75 // "char *" instead of "const char *"
76 #define WXSTRINGCAST (wxChar *)(const wxChar *)
77 #define wxCSTRINGCAST (wxChar *)(const wxChar *)
78 #define wxMBSTRINGCAST (char *)(const char *)
79 #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *)
80
81 // like _T(), but for literals in wxString's internal representation, i.e.
82 // char* in UTF-8 build and wxChar* otherwise:
83 #if wxUSE_UNICODE_UTF8
84 #define wxSTRING_TEXT(str) str
85 #else
86 #define wxSTRING_TEXT(str) _T(str)
87 #endif
88
89 // ----------------------------------------------------------------------------
90 // constants
91 // ----------------------------------------------------------------------------
92
93 #if WXWIN_COMPATIBILITY_2_6
94
95 // deprecated in favour of wxString::npos, don't use in new code
96 //
97 // maximum possible length for a string means "take all string" everywhere
98 #define wxSTRING_MAXLEN wxString::npos
99
100 #endif // WXWIN_COMPATIBILITY_2_6
101
102 // ---------------------------------------------------------------------------
103 // global functions complementing standard C string library replacements for
104 // strlen() and portable strcasecmp()
105 //---------------------------------------------------------------------------
106
107 #if WXWIN_COMPATIBILITY_2_8
108 // Use wxXXX() functions from wxcrt.h instead! These functions are for
109 // backwards compatibility only.
110
111 // checks whether the passed in pointer is NULL and if the string is empty
112 wxDEPRECATED( inline bool IsEmpty(const char *p) );
113 inline bool IsEmpty(const char *p) { return (!p || !*p); }
114
115 // safe version of strlen() (returns 0 if passed NULL pointer)
116 wxDEPRECATED( inline size_t Strlen(const char *psz) );
117 inline size_t Strlen(const char *psz)
118 { return psz ? strlen(psz) : 0; }
119
120 // portable strcasecmp/_stricmp
121 wxDEPRECATED( inline int Stricmp(const char *psz1, const char *psz2) );
122 inline int Stricmp(const char *psz1, const char *psz2)
123 {
124 #if defined(__VISUALC__) && defined(__WXWINCE__)
125 register char c1, c2;
126 do {
127 c1 = tolower(*psz1++);
128 c2 = tolower(*psz2++);
129 } while ( c1 && (c1 == c2) );
130
131 return c1 - c2;
132 #elif defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) )
133 return _stricmp(psz1, psz2);
134 #elif defined(__SC__)
135 return _stricmp(psz1, psz2);
136 #elif defined(__SALFORDC__)
137 return stricmp(psz1, psz2);
138 #elif defined(__BORLANDC__)
139 return stricmp(psz1, psz2);
140 #elif defined(__WATCOMC__)
141 return stricmp(psz1, psz2);
142 #elif defined(__DJGPP__)
143 return stricmp(psz1, psz2);
144 #elif defined(__EMX__)
145 return stricmp(psz1, psz2);
146 #elif defined(__WXPM__)
147 return stricmp(psz1, psz2);
148 #elif defined(__WXPALMOS__) || \
149 defined(HAVE_STRCASECMP_IN_STRING_H) || \
150 defined(HAVE_STRCASECMP_IN_STRINGS_H) || \
151 defined(__GNUWIN32__)
152 return strcasecmp(psz1, psz2);
153 #elif defined(__MWERKS__) && !defined(__INTEL__)
154 register char c1, c2;
155 do {
156 c1 = tolower(*psz1++);
157 c2 = tolower(*psz2++);
158 } while ( c1 && (c1 == c2) );
159
160 return c1 - c2;
161 #else
162 // almost all compilers/libraries provide this function (unfortunately under
163 // different names), that's why we don't implement our own which will surely
164 // be more efficient than this code (uncomment to use):
165 /*
166 register char c1, c2;
167 do {
168 c1 = tolower(*psz1++);
169 c2 = tolower(*psz2++);
170 } while ( c1 && (c1 == c2) );
171
172 return c1 - c2;
173 */
174
175 #error "Please define string case-insensitive compare for your OS/compiler"
176 #endif // OS/compiler
177 }
178
179 #endif // WXWIN_COMPATIBILITY_2_8
180
181 // ----------------------------------------------------------------------------
182 // wxCStrData
183 // ----------------------------------------------------------------------------
184
185 // Lightweight object returned by wxString::c_str() and implicitly convertible
186 // to either const char* or const wchar_t*.
187 class WXDLLIMPEXP_BASE wxCStrData
188 {
189 private:
190 // Ctors; for internal use by wxString and wxCStrData only
191 wxCStrData(const wxString *str, size_t offset = 0, bool owned = false)
192 : m_str(str), m_offset(offset), m_owned(owned) {}
193
194 public:
195 // Ctor constructs the object from char literal; they are needed to make
196 // operator?: compile and they intentionally take char*, not const char*
197 inline wxCStrData(char *buf);
198 inline wxCStrData(wchar_t *buf);
199 inline wxCStrData(const wxCStrData& data);
200
201 inline ~wxCStrData();
202
203 // methods defined inline below must be declared inline or mingw32 3.4.5
204 // warns about "<symbol> defined locally after being referenced with
205 // dllimport linkage"
206 #if wxUSE_UNICODE_WCHAR
207 inline
208 #endif
209 const wchar_t* AsWChar() const;
210 operator const wchar_t*() const { return AsWChar(); }
211
212 #if !wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY
213 inline
214 #endif
215 const char* AsChar() const;
216 const unsigned char* AsUnsignedChar() const
217 { return (const unsigned char *) AsChar(); }
218 operator const char*() const { return AsChar(); }
219 operator const unsigned char*() const { return AsUnsignedChar(); }
220
221 operator const void*() const { return AsChar(); }
222
223 inline const wxCharBuffer AsCharBuf() const;
224 inline const wxWCharBuffer AsWCharBuf() const;
225
226 inline wxString AsString() const;
227
228 // returns the value as C string in internal representation (equivalent
229 // to AsString().wx_str(), but more efficient)
230 const wxStringCharType *AsInternal() const;
231
232 // allow expressions like "c_str()[0]":
233 inline wxUniChar operator[](size_t n) const;
234 wxUniChar operator[](int n) const { return operator[](size_t(n)); }
235 wxUniChar operator[](long n) const { return operator[](size_t(n)); }
236 #ifndef wxSIZE_T_IS_UINT
237 wxUniChar operator[](unsigned int n) const { return operator[](size_t(n)); }
238 #endif // size_t != unsigned int
239
240 // these operators are needed to emulate the pointer semantics of c_str():
241 // expressions like "wxChar *p = str.c_str() + 1;" should continue to work
242 // (we need both versions to resolve ambiguities):
243 wxCStrData operator+(int n) const
244 { return wxCStrData(m_str, m_offset + n, m_owned); }
245 wxCStrData operator+(long n) const
246 { return wxCStrData(m_str, m_offset + n, m_owned); }
247 wxCStrData operator+(size_t n) const
248 { return wxCStrData(m_str, m_offset + n, m_owned); }
249
250 // and these for "str.c_str() + n - 2":
251 wxCStrData operator-(int n) const
252 {
253 wxASSERT_MSG( n <= (int)m_offset,
254 _T("attempt to construct address before the beginning of the string") );
255 return wxCStrData(m_str, m_offset - n, m_owned);
256 }
257 wxCStrData operator-(long n) const
258 {
259 wxASSERT_MSG( n <= (int)m_offset,
260 _T("attempt to construct address before the beginning of the string") );
261 return wxCStrData(m_str, m_offset - n, m_owned);
262 }
263 wxCStrData operator-(size_t n) const
264 {
265 wxASSERT_MSG( n <= m_offset,
266 _T("attempt to construct address before the beginning of the string") );
267 return wxCStrData(m_str, m_offset - n, m_owned);
268 }
269
270 // this operator is needed to make expressions like "*c_str()" or
271 // "*(c_str() + 2)" work
272 inline wxUniChar operator*() const;
273
274 private:
275 const wxString *m_str;
276 size_t m_offset;
277 bool m_owned;
278
279 friend class WXDLLIMPEXP_BASE wxString;
280 };
281
282 // ----------------------------------------------------------------------------
283 // wxStringPrintfMixin
284 // ---------------------------------------------------------------------------
285
286 // NB: VC6 has a bug that causes linker errors if you have template methods
287 // in a class using __declspec(dllimport). The solution is to split such
288 // class into two classes, one that contains the template methods and does
289 // *not* use WXDLLIMPEXP_BASE and another class that contains the rest
290 // (with DLL linkage).
291 //
292 // We only do this for VC6 here, because the code is less efficient
293 // (Printf() has to use dynamic_cast<>) and because OpenWatcom compiler
294 // cannot compile this code.
295
296 #if defined(__VISUALC__) && __VISUALC__ < 1300
297 #define wxNEEDS_WXSTRING_PRINTF_MIXIN
298 #endif
299
300 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
301 // this class contains implementation of wxString's vararg methods, it's
302 // exported from wxBase DLL
303 class WXDLLIMPEXP_BASE wxStringPrintfMixinBase
304 {
305 protected:
306 wxStringPrintfMixinBase() {}
307
308 #if !wxUSE_UTF8_LOCALE_ONLY
309 int DoPrintfWchar(const wxChar *format, ...);
310 static wxString DoFormatWchar(const wxChar *format, ...);
311 #endif
312 #if wxUSE_UNICODE_UTF8
313 int DoPrintfUtf8(const char *format, ...);
314 static wxString DoFormatUtf8(const char *format, ...);
315 #endif
316 };
317
318 // this class contains template wrappers for wxString's vararg methods, it's
319 // intentionally *not* exported from the DLL in order to fix the VC6 bug
320 // described above
321 class wxStringPrintfMixin : public wxStringPrintfMixinBase
322 {
323 private:
324 // to further complicate things, we can't return wxString from
325 // wxStringPrintfMixin::Format() because wxString is not yet declared at
326 // this point; the solution is to use this fake type trait template - this
327 // way the compiler won't know the return type until Format() is used
328 // (this doesn't compile with Watcom, but VC6 compiles it just fine):
329 template<typename T> struct StringReturnType
330 {
331 typedef wxString type;
332 };
333
334 public:
335 // these are duplicated wxString methods, they're also declared below
336 // if !wxNEEDS_WXSTRING_PRINTF_MIXIN:
337
338 // static wxString Format(const wString& format, ...) ATTRIBUTE_PRINTF_1;
339 WX_DEFINE_VARARG_FUNC_SANS_N0(static typename StringReturnType<T1>::type,
340 Format, 1, (const wxFormatString&),
341 DoFormatWchar, DoFormatUtf8)
342 // We have to implement the version without template arguments manually
343 // because of the StringReturnType<> hack, although WX_DEFINE_VARARG_FUNC
344 // normally does it itself. It has to be a template so that we can use
345 // the hack, even though there's no real template parameter:
346 struct FormatDummyArg {} ;
347
348 template<typename T>
349 inline static typename StringReturnType<T>::type
350 Format(const wxFormatString& fmt, FormatDummyArg dummy = FormatDummyArg())
351 {
352 return DoFormatWchar(fmt);
353 }
354
355 // int Printf(const wxString& format, ...);
356 WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const wxFormatString&),
357 DoPrintfWchar, DoPrintfUtf8)
358 // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2;
359 WX_DEFINE_VARARG_FUNC(int, sprintf, 1, (const wxFormatString&),
360 DoPrintfWchar, DoPrintfUtf8)
361
362 protected:
363 wxStringPrintfMixin() : wxStringPrintfMixinBase() {}
364 };
365 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
366
367
368 // ----------------------------------------------------------------------------
369 // wxString: string class trying to be compatible with std::string, MFC
370 // CString and wxWindows 1.x wxString all at once
371 // ---------------------------------------------------------------------------
372
373 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
374 // "non dll-interface class 'wxStringPrintfMixin' used as base interface
375 // for dll-interface class 'wxString'" -- this is OK in our case
376 #pragma warning (disable:4275)
377 #endif
378
379 #if wxUSE_UNICODE_UTF8
380 // see the comment near wxString::iterator for why we need this
381 struct WXDLLIMPEXP_BASE wxStringIteratorNode
382 {
383 inline wxStringIteratorNode(const wxString *str,
384 wxStringImpl::const_iterator *citer);
385 inline wxStringIteratorNode(const wxString *str,
386 wxStringImpl::iterator *iter);
387 inline ~wxStringIteratorNode();
388
389 const wxString *m_str;
390 wxStringImpl::const_iterator *m_citer;
391 wxStringImpl::iterator *m_iter;
392 wxStringIteratorNode *m_prev, *m_next;
393 };
394 #endif // wxUSE_UNICODE_UTF8
395
396 class WXDLLIMPEXP_BASE wxString
397 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
398 : public wxStringPrintfMixin
399 #endif
400 {
401 // NB: special care was taken in arranging the member functions in such order
402 // that all inline functions can be effectively inlined, verify that all
403 // performance critical functions are still inlined if you change order!
404 public:
405 // an 'invalid' value for string index, moved to this place due to a CW bug
406 static const size_t npos;
407
408 private:
409 // if we hadn't made these operators private, it would be possible to
410 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
411 // converted to char in C and we do have operator=(char)
412 //
413 // NB: we don't need other versions (short/long and unsigned) as attempt
414 // to assign another numeric type to wxString will now result in
415 // ambiguity between operator=(char) and operator=(int)
416 wxString& operator=(int);
417
418 // these methods are not implemented - there is _no_ conversion from int to
419 // string, you're doing something wrong if the compiler wants to call it!
420 //
421 // try `s << i' or `s.Printf("%d", i)' instead
422 wxString(int);
423
424
425 // buffer for holding temporary substring when using any of the methods
426 // that take (char*,size_t) or (wchar_t*,size_t) arguments:
427 template<typename T>
428 struct SubstrBufFromType
429 {
430 T data;
431 size_t len;
432
433 SubstrBufFromType(const T& data_, size_t len_)
434 : data(data_), len(len_) {}
435 };
436
437 #if wxUSE_UNICODE_UTF8
438 // even char* -> char* needs conversion, from locale charset to UTF-8
439 typedef SubstrBufFromType<wxCharBuffer> SubstrBufFromWC;
440 typedef SubstrBufFromType<wxCharBuffer> SubstrBufFromMB;
441 #elif wxUSE_UNICODE_WCHAR
442 typedef SubstrBufFromType<const wchar_t*> SubstrBufFromWC;
443 typedef SubstrBufFromType<wxWCharBuffer> SubstrBufFromMB;
444 #else
445 typedef SubstrBufFromType<const char*> SubstrBufFromMB;
446 typedef SubstrBufFromType<wxCharBuffer> SubstrBufFromWC;
447 #endif
448
449
450 // Functions implementing primitive operations on string data; wxString
451 // methods and iterators are implemented in terms of it. The differences
452 // between UTF-8 and wchar_t* representations of the string are mostly
453 // contained here.
454
455 #if wxUSE_UNICODE_UTF8
456 static SubstrBufFromMB ConvertStr(const char *psz, size_t nLength,
457 const wxMBConv& conv);
458 static SubstrBufFromWC ConvertStr(const wchar_t *pwz, size_t nLength,
459 const wxMBConv& conv);
460 #elif wxUSE_UNICODE_WCHAR
461 static SubstrBufFromMB ConvertStr(const char *psz, size_t nLength,
462 const wxMBConv& conv);
463 #else
464 static SubstrBufFromWC ConvertStr(const wchar_t *pwz, size_t nLength,
465 const wxMBConv& conv);
466 #endif
467
468 #if !wxUSE_UNICODE_UTF8 // wxUSE_UNICODE_WCHAR or !wxUSE_UNICODE
469 // returns C string encoded as the implementation expects:
470 #if wxUSE_UNICODE
471 static const wchar_t* ImplStr(const wchar_t* str)
472 { return str ? str : wxT(""); }
473 static const SubstrBufFromWC ImplStr(const wchar_t* str, size_t n)
474 { return SubstrBufFromWC(str, (str && n == npos) ? wxWcslen(str) : n); }
475 static wxWCharBuffer ImplStr(const char* str,
476 const wxMBConv& conv = wxConvLibc)
477 { return ConvertStr(str, npos, conv).data; }
478 static SubstrBufFromMB ImplStr(const char* str, size_t n,
479 const wxMBConv& conv = wxConvLibc)
480 { return ConvertStr(str, n, conv); }
481 #else
482 static const char* ImplStr(const char* str,
483 const wxMBConv& WXUNUSED(conv) = wxConvLibc)
484 { return str ? str : ""; }
485 static const SubstrBufFromMB ImplStr(const char* str, size_t n,
486 const wxMBConv& WXUNUSED(conv) = wxConvLibc)
487 { return SubstrBufFromMB(str, (str && n == npos) ? wxStrlen(str) : n); }
488 static wxCharBuffer ImplStr(const wchar_t* str)
489 { return ConvertStr(str, npos, wxConvLibc).data; }
490 static SubstrBufFromWC ImplStr(const wchar_t* str, size_t n)
491 { return ConvertStr(str, n, wxConvLibc); }
492 #endif
493
494 // translates position index in wxString to/from index in underlying
495 // wxStringImpl:
496 static size_t PosToImpl(size_t pos) { return pos; }
497 static void PosLenToImpl(size_t pos, size_t len,
498 size_t *implPos, size_t *implLen)
499 { *implPos = pos; *implLen = len; }
500 static size_t LenToImpl(size_t len) { return len; }
501 static size_t PosFromImpl(size_t pos) { return pos; }
502
503 #else // wxUSE_UNICODE_UTF8
504
505 static wxCharBuffer ImplStr(const char* str,
506 const wxMBConv& conv = wxConvLibc)
507 { return ConvertStr(str, npos, conv).data; }
508 static SubstrBufFromMB ImplStr(const char* str, size_t n,
509 const wxMBConv& conv = wxConvLibc)
510 { return ConvertStr(str, n, conv); }
511
512 static wxCharBuffer ImplStr(const wchar_t* str)
513 { return ConvertStr(str, npos, wxMBConvUTF8()).data; }
514 static SubstrBufFromWC ImplStr(const wchar_t* str, size_t n)
515 { return ConvertStr(str, n, wxMBConvUTF8()); }
516
517 size_t PosToImpl(size_t pos) const
518 {
519 if ( pos == 0 || pos == npos )
520 return pos;
521 else
522 return (begin() + pos).impl() - m_impl.begin();
523 }
524
525 void PosLenToImpl(size_t pos, size_t len, size_t *implPos, size_t *implLen) const;
526
527 size_t LenToImpl(size_t len) const
528 {
529 size_t pos, len2;
530 PosLenToImpl(0, len, &pos, &len2);
531 return len2;
532 }
533
534 size_t PosFromImpl(size_t pos) const
535 {
536 if ( pos == 0 || pos == npos )
537 return pos;
538 else
539 return const_iterator(this, m_impl.begin() + pos) - begin();
540 }
541 #endif // !wxUSE_UNICODE_UTF8/wxUSE_UNICODE_UTF8
542
543 public:
544 // standard types
545 typedef wxUniChar value_type;
546 typedef wxUniChar char_type;
547 typedef wxUniCharRef reference;
548 typedef wxChar* pointer;
549 typedef const wxChar* const_pointer;
550
551 typedef size_t size_type;
552 typedef wxUniChar const_reference;
553
554 #if wxUSE_STL
555 #if wxUSE_UNICODE_UTF8
556 // random access is not O(1), as required by Random Access Iterator
557 #define WX_STR_ITERATOR_TAG std::bidirectional_iterator_tag
558 #else
559 #define WX_STR_ITERATOR_TAG std::random_access_iterator_tag
560 #endif
561 #else
562 #define WX_STR_ITERATOR_TAG void /* dummy type */
563 #endif
564
565 #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, reference_type) \
566 private: \
567 typedef wxStringImpl::iterator_name underlying_iterator; \
568 public: \
569 typedef WX_STR_ITERATOR_TAG iterator_category; \
570 typedef wxUniChar value_type; \
571 typedef int difference_type; \
572 typedef reference_type reference; \
573 typedef pointer_type pointer; \
574 \
575 reference operator[](size_t n) const { return *(*this + n); } \
576 \
577 iterator_name& operator++() \
578 { wxStringOperations::IncIter(m_cur); return *this; } \
579 iterator_name& operator--() \
580 { wxStringOperations::DecIter(m_cur); return *this; } \
581 iterator_name operator++(int) \
582 { \
583 iterator_name tmp = *this; \
584 wxStringOperations::IncIter(m_cur); \
585 return tmp; \
586 } \
587 iterator_name operator--(int) \
588 { \
589 iterator_name tmp = *this; \
590 wxStringOperations::DecIter(m_cur); \
591 return tmp; \
592 } \
593 \
594 iterator_name& operator+=(int n) \
595 { \
596 m_cur = wxStringOperations::AddToIter(m_cur, n); \
597 return *this; \
598 } \
599 iterator_name& operator+=(size_t n) \
600 { \
601 m_cur = wxStringOperations::AddToIter(m_cur, (int)n); \
602 return *this; \
603 } \
604 iterator_name& operator-=(int n) \
605 { \
606 m_cur = wxStringOperations::AddToIter(m_cur, -n); \
607 return *this; \
608 } \
609 iterator_name& operator-=(size_t n) \
610 { \
611 m_cur = wxStringOperations::AddToIter(m_cur, -(int)n); \
612 return *this; \
613 } \
614 \
615 difference_type operator-(const iterator_name& i) const \
616 { return wxStringOperations::DiffIters(m_cur, i.m_cur); } \
617 \
618 bool operator==(const iterator_name& i) const \
619 { return m_cur == i.m_cur; } \
620 bool operator!=(const iterator_name& i) const \
621 { return m_cur != i.m_cur; } \
622 \
623 bool operator<(const iterator_name& i) const \
624 { return m_cur < i.m_cur; } \
625 bool operator>(const iterator_name& i) const \
626 { return m_cur > i.m_cur; } \
627 bool operator<=(const iterator_name& i) const \
628 { return m_cur <= i.m_cur; } \
629 bool operator>=(const iterator_name& i) const \
630 { return m_cur >= i.m_cur; } \
631 \
632 private: \
633 /* for internal wxString use only: */ \
634 underlying_iterator impl() const { return m_cur; } \
635 \
636 friend class WXDLLIMPEXP_BASE wxString; \
637 friend class WXDLLIMPEXP_BASE wxCStrData; \
638 \
639 private: \
640 underlying_iterator m_cur
641
642 class WXDLLIMPEXP_BASE const_iterator;
643
644 #if wxUSE_UNICODE_UTF8
645 // NB: In UTF-8 build, (non-const) iterator needs to keep reference
646 // to the underlying wxStringImpl, because UTF-8 is variable-length
647 // encoding and changing the value pointer to by an iterator (using
648 // its operator*) requires calling wxStringImpl::replace() if the old
649 // and new values differ in their encoding's length.
650 //
651 // Furthermore, the replace() call may invalid all iterators for the
652 // string, so we have to keep track of outstanding iterators and update
653 // them if replace() happens.
654 //
655 // This is implemented by maintaining linked list of iterators for every
656 // string and traversing it in wxUniCharRef::operator=(). Head of the
657 // list is stored in wxString. (FIXME-UTF8)
658
659 class WXDLLIMPEXP_BASE iterator
660 {
661 WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef);
662
663 public:
664 iterator(const iterator& i)
665 : m_cur(i.m_cur), m_node(i.str(), &m_cur) {}
666
667 reference operator*()
668 { return wxUniCharRef::CreateForString(m_node, m_cur); }
669
670 iterator operator+(int n) const
671 { return iterator(str(), wxStringOperations::AddToIter(m_cur, n)); }
672 iterator operator+(size_t n) const
673 { return iterator(str(), wxStringOperations::AddToIter(m_cur, (int)n)); }
674 iterator operator-(int n) const
675 { return iterator(str(), wxStringOperations::AddToIter(m_cur, -n)); }
676 iterator operator-(size_t n) const
677 { return iterator(str(), wxStringOperations::AddToIter(m_cur, -(int)n)); }
678
679 private:
680 iterator(wxString *str, underlying_iterator ptr)
681 : m_cur(ptr), m_node(str, &m_cur) {}
682
683 wxString* str() const { return wx_const_cast(wxString*, m_node.m_str); }
684
685 wxStringIteratorNode m_node;
686
687 friend class const_iterator;
688 };
689
690 class WXDLLIMPEXP_BASE const_iterator
691 {
692 // NB: reference_type is intentionally value, not reference, the character
693 // may be encoded differently in wxString data:
694 WX_STR_ITERATOR_IMPL(const_iterator, const wxChar*, wxUniChar);
695
696 public:
697 const_iterator(const const_iterator& i)
698 : m_cur(i.m_cur), m_node(i.str(), &m_cur) {}
699 const_iterator(const iterator& i)
700 : m_cur(i.m_cur), m_node(i.str(), &m_cur) {}
701
702 reference operator*() const
703 { return wxStringOperations::DecodeChar(m_cur); }
704
705 const_iterator operator+(int n) const
706 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur, n)); }
707 const_iterator operator+(size_t n) const
708 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur, (int)n)); }
709 const_iterator operator-(int n) const
710 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur, -n)); }
711 const_iterator operator-(size_t n) const
712 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur, -(int)n)); }
713
714 private:
715 // for internal wxString use only:
716 const_iterator(const wxString *str, underlying_iterator ptr)
717 : m_cur(ptr), m_node(str, &m_cur) {}
718
719 const wxString* str() const { return m_node.m_str; }
720
721 wxStringIteratorNode m_node;
722 };
723
724 size_t IterToImplPos(wxString::iterator i) const
725 { return wxStringImpl::const_iterator(i.impl()) - m_impl.begin(); }
726
727 #else // !wxUSE_UNICODE_UTF8
728
729 class WXDLLIMPEXP_BASE iterator
730 {
731 WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef);
732
733 public:
734 iterator(const iterator& i) : m_cur(i.m_cur) {}
735
736 reference operator*()
737 { return wxUniCharRef::CreateForString(m_cur); }
738
739 iterator operator+(int n) const
740 { return iterator(wxStringOperations::AddToIter(m_cur, n)); }
741 iterator operator+(size_t n) const
742 { return iterator(wxStringOperations::AddToIter(m_cur, (int)n)); }
743 iterator operator-(int n) const
744 { return iterator(wxStringOperations::AddToIter(m_cur, -n)); }
745 iterator operator-(size_t n) const
746 { return iterator(wxStringOperations::AddToIter(m_cur, -(int)n)); }
747
748 private:
749 // for internal wxString use only:
750 iterator(underlying_iterator ptr) : m_cur(ptr) {}
751 iterator(wxString *WXUNUSED(str), underlying_iterator ptr) : m_cur(ptr) {}
752
753 friend class const_iterator;
754 };
755
756 class WXDLLIMPEXP_BASE const_iterator
757 {
758 // NB: reference_type is intentionally value, not reference, the character
759 // may be encoded differently in wxString data:
760 WX_STR_ITERATOR_IMPL(const_iterator, const wxChar*, wxUniChar);
761
762 public:
763 const_iterator(const const_iterator& i) : m_cur(i.m_cur) {}
764 const_iterator(const iterator& i) : m_cur(i.m_cur) {}
765
766 reference operator*() const
767 { return wxStringOperations::DecodeChar(m_cur); }
768
769 const_iterator operator+(int n) const
770 { return const_iterator(wxStringOperations::AddToIter(m_cur, n)); }
771 const_iterator operator+(size_t n) const
772 { return const_iterator(wxStringOperations::AddToIter(m_cur, (int)n)); }
773 const_iterator operator-(int n) const
774 { return const_iterator(wxStringOperations::AddToIter(m_cur, -n)); }
775 const_iterator operator-(size_t n) const
776 { return const_iterator(wxStringOperations::AddToIter(m_cur, -(int)n)); }
777
778 private:
779 // for internal wxString use only:
780 const_iterator(underlying_iterator ptr) : m_cur(ptr) {}
781 const_iterator(const wxString *WXUNUSED(str), underlying_iterator ptr)
782 : m_cur(ptr) {}
783 };
784 #endif // wxUSE_UNICODE_UTF8/!wxUSE_UNICODE_UTF8
785
786 #undef WX_STR_ITERATOR_TAG
787 #undef WX_STR_ITERATOR_IMPL
788
789 friend class iterator;
790 friend class const_iterator;
791
792 template <typename T>
793 class reverse_iterator_impl
794 {
795 public:
796 typedef T iterator_type;
797
798 typedef typename T::iterator_category iterator_category;
799 typedef typename T::value_type value_type;
800 typedef typename T::difference_type difference_type;
801 typedef typename T::reference reference;
802 typedef typename T::pointer *pointer;
803
804 reverse_iterator_impl(iterator_type i) : m_cur(i) {}
805 reverse_iterator_impl(const reverse_iterator_impl& ri)
806 : m_cur(ri.m_cur) {}
807
808 iterator_type base() const { return m_cur; }
809
810 reference operator*() const { return *(m_cur-1); }
811 reference operator[](size_t n) const { return *(*this + n); }
812
813 reverse_iterator_impl& operator++()
814 { --m_cur; return *this; }
815 reverse_iterator_impl operator++(int)
816 { reverse_iterator_impl tmp = *this; --m_cur; return tmp; }
817 reverse_iterator_impl& operator--()
818 { ++m_cur; return *this; }
819 reverse_iterator_impl operator--(int)
820 { reverse_iterator_impl tmp = *this; ++m_cur; return tmp; }
821
822 // NB: explicit <T> in the functions below is to keep BCC 5.5 happy
823 reverse_iterator_impl operator+(int n) const
824 { return reverse_iterator_impl<T>(m_cur - n); }
825 reverse_iterator_impl operator+(size_t n) const
826 { return reverse_iterator_impl<T>(m_cur - n); }
827 reverse_iterator_impl operator-(int n) const
828 { return reverse_iterator_impl<T>(m_cur + n); }
829 reverse_iterator_impl operator-(size_t n) const
830 { return reverse_iterator_impl<T>(m_cur + n); }
831 reverse_iterator_impl operator+=(int n)
832 { m_cur -= n; return *this; }
833 reverse_iterator_impl operator+=(size_t n)
834 { m_cur -= n; return *this; }
835 reverse_iterator_impl operator-=(int n)
836 { m_cur += n; return *this; }
837 reverse_iterator_impl operator-=(size_t n)
838 { m_cur += n; return *this; }
839
840 unsigned operator-(const reverse_iterator_impl& i) const
841 { return i.m_cur - m_cur; }
842
843 bool operator==(const reverse_iterator_impl& ri) const
844 { return m_cur == ri.m_cur; }
845 bool operator!=(const reverse_iterator_impl& ri) const
846 { return !(*this == ri); }
847
848 bool operator<(const reverse_iterator_impl& i) const
849 { return m_cur > i.m_cur; }
850 bool operator>(const reverse_iterator_impl& i) const
851 { return m_cur < i.m_cur; }
852 bool operator<=(const reverse_iterator_impl& i) const
853 { return m_cur >= i.m_cur; }
854 bool operator>=(const reverse_iterator_impl& i) const
855 { return m_cur <= i.m_cur; }
856
857 private:
858 iterator_type m_cur;
859 };
860
861 typedef reverse_iterator_impl<iterator> reverse_iterator;
862 typedef reverse_iterator_impl<const_iterator> const_reverse_iterator;
863
864 private:
865 // used to transform an expression built using c_str() (and hence of type
866 // wxCStrData) to an iterator into the string
867 static const_iterator CreateConstIterator(const wxCStrData& data)
868 {
869 return const_iterator(data.m_str,
870 (data.m_str->begin() + data.m_offset).impl());
871 }
872
873 // in UTF-8 STL build, creation from std::string requires conversion under
874 // non-UTF8 locales, so we can't have and use wxString(wxStringImpl) ctor;
875 // instead we define dummy type that lets us have wxString ctor for creation
876 // from wxStringImpl that couldn't be used by user code (in all other builds,
877 // "standard" ctors can be used):
878 #if wxUSE_UNICODE_UTF8 && wxUSE_STL_BASED_WXSTRING
879 struct CtorFromStringImplTag {};
880
881 wxString(CtorFromStringImplTag* WXUNUSED(dummy), const wxStringImpl& src)
882 : m_impl(src) {}
883
884 static wxString FromImpl(const wxStringImpl& src)
885 { return wxString((CtorFromStringImplTag*)NULL, src); }
886 #else
887 #if !wxUSE_STL_BASED_WXSTRING
888 wxString(const wxStringImpl& src) : m_impl(src) { }
889 // else: already defined as wxString(wxStdString) below
890 #endif
891 static wxString FromImpl(const wxStringImpl& src) { return wxString(src); }
892 #endif
893
894 public:
895 // constructors and destructor
896 // ctor for an empty string
897 wxString() {}
898
899 // copy ctor
900 wxString(const wxString& stringSrc) : m_impl(stringSrc.m_impl) { }
901
902 // string containing nRepeat copies of ch
903 wxString(wxUniChar ch, size_t nRepeat = 1)
904 { assign(nRepeat, ch); }
905 wxString(size_t nRepeat, wxUniChar ch)
906 { assign(nRepeat, ch); }
907 wxString(wxUniCharRef ch, size_t nRepeat = 1)
908 { assign(nRepeat, ch); }
909 wxString(size_t nRepeat, wxUniCharRef ch)
910 { assign(nRepeat, ch); }
911 wxString(char ch, size_t nRepeat = 1)
912 { assign(nRepeat, ch); }
913 wxString(size_t nRepeat, char ch)
914 { assign(nRepeat, ch); }
915 wxString(wchar_t ch, size_t nRepeat = 1)
916 { assign(nRepeat, ch); }
917 wxString(size_t nRepeat, wchar_t ch)
918 { assign(nRepeat, ch); }
919
920 // ctors from char* strings:
921 wxString(const char *psz)
922 : m_impl(ImplStr(psz)) {}
923 wxString(const char *psz, const wxMBConv& conv)
924 : m_impl(ImplStr(psz, conv)) {}
925 wxString(const char *psz, size_t nLength)
926 { assign(psz, nLength); }
927 wxString(const char *psz, const wxMBConv& conv, size_t nLength)
928 {
929 SubstrBufFromMB str(ImplStr(psz, nLength, conv));
930 m_impl.assign(str.data, str.len);
931 }
932
933 // and unsigned char*:
934 wxString(const unsigned char *psz)
935 : m_impl(ImplStr((const char*)psz)) {}
936 wxString(const unsigned char *psz, const wxMBConv& conv)
937 : m_impl(ImplStr((const char*)psz, conv)) {}
938 wxString(const unsigned char *psz, size_t nLength)
939 { assign((const char*)psz, nLength); }
940 wxString(const unsigned char *psz, const wxMBConv& conv, size_t nLength)
941 {
942 SubstrBufFromMB str(ImplStr((const char*)psz, nLength, conv));
943 m_impl.assign(str.data, str.len);
944 }
945
946 // ctors from wchar_t* strings:
947 wxString(const wchar_t *pwz)
948 : m_impl(ImplStr(pwz)) {}
949 wxString(const wchar_t *pwz, const wxMBConv& WXUNUSED(conv))
950 : m_impl(ImplStr(pwz)) {}
951 wxString(const wchar_t *pwz, size_t nLength)
952 { assign(pwz, nLength); }
953 wxString(const wchar_t *pwz, const wxMBConv& WXUNUSED(conv), size_t nLength)
954 { assign(pwz, nLength); }
955
956 wxString(const wxCharBuffer& buf)
957 { assign(buf.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
958 wxString(const wxWCharBuffer& buf)
959 { assign(buf.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
960
961 wxString(const wxCStrData& cstr)
962 : m_impl(cstr.AsString().m_impl) { }
963
964 // as we provide both ctors with this signature for both char and unsigned
965 // char string, we need to provide one for wxCStrData to resolve ambiguity
966 wxString(const wxCStrData& cstr, size_t nLength)
967 : m_impl(cstr.AsString().Mid(0, nLength).m_impl) {}
968
969 // and because wxString is convertible to wxCStrData and const wxChar *
970 // we also need to provide this one
971 wxString(const wxString& str, size_t nLength)
972 { assign(str, nLength); }
973
974 // even if we're not built with wxUSE_STL == 1 it is very convenient to allow
975 // implicit conversions from std::string to wxString and vice verse as this
976 // allows to use the same strings in non-GUI and GUI code, however we don't
977 // want to unconditionally add this ctor as it would make wx lib dependent on
978 // libstdc++ on some Linux versions which is bad, so instead we ask the
979 // client code to define this wxUSE_STD_STRING symbol if they need it
980 #if wxUSE_STD_STRING
981 #if wxUSE_UNICODE_WCHAR
982 wxString(const wxStdWideString& str) : m_impl(str) {}
983 #else // UTF-8 or ANSI
984 wxString(const wxStdWideString& str)
985 { assign(str.c_str(), str.length()); }
986 #endif
987
988 #if !wxUSE_UNICODE // ANSI build
989 // FIXME-UTF8: do this in UTF8 build #if wxUSE_UTF8_LOCALE_ONLY, too
990 wxString(const std::string& str) : m_impl(str) {}
991 #else // Unicode
992 wxString(const std::string& str)
993 { assign(str.c_str(), str.length()); }
994 #endif
995
996 #if wxUSE_UNICODE_WCHAR && wxUSE_STL_BASED_WXSTRING
997 // wxStringImpl is std::string in the encoding we want
998 operator const wxStdWideString&() const { return m_impl; }
999 #else
1000 // wxStringImpl is either not std::string or needs conversion
1001 operator wxStdWideString() const
1002 // FIXME-UTF8: broken for embedded NULs
1003 { return wxStdWideString(wc_str()); }
1004 #endif
1005
1006 #if (!wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY) && wxUSE_STL_BASED_WXSTRING
1007 // wxStringImpl is std::string in the encoding we want
1008 operator const std::string&() const { return m_impl; }
1009 #else
1010 // wxStringImpl is either not std::string or needs conversion
1011 operator std::string() const
1012 // FIXME-UTF8: broken for embedded NULs
1013 { return std::string(mb_str()); }
1014 #endif
1015 #endif // wxUSE_STL
1016
1017 // first valid index position
1018 const_iterator begin() const { return const_iterator(this, m_impl.begin()); }
1019 iterator begin() { return iterator(this, m_impl.begin()); }
1020 // position one after the last valid one
1021 const_iterator end() const { return const_iterator(this, m_impl.end()); }
1022 iterator end() { return iterator(this, m_impl.end()); }
1023
1024 // first element of the reversed string
1025 const_reverse_iterator rbegin() const
1026 { return const_reverse_iterator(end()); }
1027 reverse_iterator rbegin()
1028 { return reverse_iterator(end()); }
1029 // one beyond the end of the reversed string
1030 const_reverse_iterator rend() const
1031 { return const_reverse_iterator(begin()); }
1032 reverse_iterator rend()
1033 { return reverse_iterator(begin()); }
1034
1035 // std::string methods:
1036 #if wxUSE_UNICODE_UTF8
1037 size_t length() const { return end() - begin(); } // FIXME-UTF8: optimize!
1038 #else
1039 size_t length() const { return m_impl.length(); }
1040 #endif
1041
1042 size_type size() const { return length(); }
1043 size_type max_size() const { return npos; }
1044
1045 bool empty() const { return m_impl.empty(); }
1046
1047 size_type capacity() const { return m_impl.capacity(); } // FIXME-UTF8
1048 void reserve(size_t sz) { m_impl.reserve(sz); } // FIXME-UTF8
1049
1050 void resize(size_t nSize, wxUniChar ch = wxT('\0'))
1051 {
1052 #if wxUSE_UNICODE_UTF8
1053 if ( !ch.IsAscii() )
1054 {
1055 size_t len = length();
1056 if ( nSize == len)
1057 return;
1058 else if ( nSize < len )
1059 erase(nSize);
1060 else
1061 append(nSize - len, ch);
1062 }
1063 else
1064 #endif
1065 m_impl.resize(nSize, (wxStringCharType)ch);
1066 }
1067
1068 wxString substr(size_t nStart = 0, size_t nLen = npos) const
1069 {
1070 size_t pos, len;
1071 PosLenToImpl(nStart, nLen, &pos, &len);
1072 return FromImpl(m_impl.substr(pos, len));
1073 }
1074
1075 // generic attributes & operations
1076 // as standard strlen()
1077 size_t Len() const { return length(); }
1078 // string contains any characters?
1079 bool IsEmpty() const { return empty(); }
1080 // empty string is "false", so !str will return true
1081 bool operator!() const { return empty(); }
1082 // truncate the string to given length
1083 wxString& Truncate(size_t uiLen);
1084 // empty string contents
1085 void Empty()
1086 {
1087 Truncate(0);
1088
1089 wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") );
1090 }
1091 // empty the string and free memory
1092 void Clear()
1093 {
1094 wxString tmp(wxEmptyString);
1095 swap(tmp);
1096 }
1097
1098 // contents test
1099 // Is an ascii value
1100 bool IsAscii() const;
1101 // Is a number
1102 bool IsNumber() const;
1103 // Is a word
1104 bool IsWord() const;
1105
1106 // data access (all indexes are 0 based)
1107 // read access
1108 wxUniChar at(size_t n) const
1109 { return *(begin() + n); } // FIXME-UTF8: optimize?
1110 wxUniChar GetChar(size_t n) const
1111 { return at(n); }
1112 // read/write access
1113 wxUniCharRef at(size_t n)
1114 { return *(begin() + n); } // FIXME-UTF8: optimize?
1115 wxUniCharRef GetWritableChar(size_t n)
1116 { return at(n); }
1117 // write access
1118 void SetChar(size_t n, wxUniChar ch)
1119 { at(n) = ch; }
1120
1121 // get last character
1122 wxUniChar Last() const
1123 {
1124 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1125 return *rbegin();
1126 }
1127
1128 // get writable last character
1129 wxUniCharRef Last()
1130 {
1131 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1132 return *rbegin();
1133 }
1134
1135 /*
1136 Note that we we must define all of the overloads below to avoid
1137 ambiguity when using str[0].
1138 */
1139 wxUniChar operator[](int n) const
1140 { return at(n); }
1141 wxUniChar operator[](long n) const
1142 { return at(n); }
1143 wxUniChar operator[](size_t n) const
1144 { return at(n); }
1145 #ifndef wxSIZE_T_IS_UINT
1146 wxUniChar operator[](unsigned int n) const
1147 { return at(n); }
1148 #endif // size_t != unsigned int
1149
1150 // operator versions of GetWriteableChar()
1151 wxUniCharRef operator[](int n)
1152 { return at(n); }
1153 wxUniCharRef operator[](long n)
1154 { return at(n); }
1155 wxUniCharRef operator[](size_t n)
1156 { return at(n); }
1157 #ifndef wxSIZE_T_IS_UINT
1158 wxUniCharRef operator[](unsigned int n)
1159 { return at(n); }
1160 #endif // size_t != unsigned int
1161
1162 // explicit conversion to C string (use this with printf()!)
1163 wxCStrData c_str() const { return wxCStrData(this); }
1164 wxCStrData data() const { return c_str(); }
1165
1166 // implicit conversion to C string
1167 operator wxCStrData() const { return c_str(); }
1168 operator const char*() const { return c_str(); }
1169 operator const wchar_t*() const { return c_str(); }
1170
1171 // implicit conversion to untyped pointer for compatibility with previous
1172 // wxWidgets versions: this is the same as conversion to const char * so it
1173 // may fail!
1174 operator const void*() const { return c_str(); }
1175
1176 // identical to c_str(), for MFC compatibility
1177 const wxCStrData GetData() const { return c_str(); }
1178
1179 // explicit conversion to C string in internal representation (char*,
1180 // wchar_t*, UTF-8-encoded char*, depending on the build):
1181 const wxStringCharType *wx_str() const { return m_impl.c_str(); }
1182
1183 // conversion to *non-const* multibyte or widestring buffer; modifying
1184 // returned buffer won't affect the string, these methods are only useful
1185 // for passing values to const-incorrect functions
1186 wxWritableCharBuffer char_str(const wxMBConv& conv = wxConvLibc) const
1187 { return mb_str(conv); }
1188 wxWritableWCharBuffer wchar_str() const { return wc_str(); }
1189
1190 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
1191 // converting numbers or strings which are certain not to contain special
1192 // chars (typically system functions, X atoms, environment variables etc.)
1193 //
1194 // the behaviour of these functions with the strings containing anything
1195 // else than 7 bit ASCII characters is undefined, use at your own risk.
1196 #if wxUSE_UNICODE
1197 static wxString FromAscii(const char *ascii); // string
1198 static wxString FromAscii(const char ascii); // char
1199 const wxCharBuffer ToAscii() const;
1200 #else // ANSI
1201 static wxString FromAscii(const char *ascii) { return wxString( ascii ); }
1202 static wxString FromAscii(const char ascii) { return wxString( ascii ); }
1203 const char *ToAscii() const { return c_str(); }
1204 #endif // Unicode/!Unicode
1205
1206 // conversion to/from UTF-8:
1207 #if wxUSE_UNICODE_UTF8
1208 static wxString FromUTF8(const char *utf8)
1209 {
1210 wxASSERT( wxStringOperations::IsValidUtf8String(utf8) );
1211 return FromImpl(wxStringImpl(utf8));
1212 }
1213 static wxString FromUTF8(const char *utf8, size_t len)
1214 {
1215 wxASSERT( wxStringOperations::IsValidUtf8String(utf8, len) );
1216 return FromImpl(wxStringImpl(utf8, len));
1217 }
1218 const char* utf8_str() const { return wx_str(); }
1219 const char* ToUTF8() const { return wx_str(); }
1220 #elif wxUSE_UNICODE_WCHAR
1221 static wxString FromUTF8(const char *utf8)
1222 { return wxString(utf8, wxMBConvUTF8()); }
1223 static wxString FromUTF8(const char *utf8, size_t len)
1224 { return wxString(utf8, wxMBConvUTF8(), len); }
1225 const wxCharBuffer utf8_str() const { return mb_str(wxMBConvUTF8()); }
1226 const wxCharBuffer ToUTF8() const { return utf8_str(); }
1227 #else // ANSI
1228 static wxString FromUTF8(const char *utf8)
1229 { return wxString(wxMBConvUTF8().cMB2WC(utf8)); }
1230 static wxString FromUTF8(const char *utf8, size_t len)
1231 {
1232 size_t wlen;
1233 wxWCharBuffer buf(wxMBConvUTF8().cMB2WC(utf8, len == npos ? wxNO_LEN : len, &wlen));
1234 return wxString(buf.data(), wlen);
1235 }
1236 const wxCharBuffer utf8_str() const
1237 { return wxMBConvUTF8().cWC2MB(wc_str()); }
1238 const wxCharBuffer ToUTF8() const { return utf8_str(); }
1239 #endif
1240
1241 // functions for storing binary data in wxString:
1242 #if wxUSE_UNICODE
1243 static wxString From8BitData(const char *data, size_t len)
1244 { return wxString(data, wxConvISO8859_1, len); }
1245 // version for NUL-terminated data:
1246 static wxString From8BitData(const char *data)
1247 { return wxString(data, wxConvISO8859_1); }
1248 const wxCharBuffer To8BitData() const { return mb_str(wxConvISO8859_1); }
1249 #else // ANSI
1250 static wxString From8BitData(const char *data, size_t len)
1251 { return wxString(data, len); }
1252 // version for NUL-terminated data:
1253 static wxString From8BitData(const char *data)
1254 { return wxString(data); }
1255 const char *To8BitData() const { return c_str(); }
1256 #endif // Unicode/ANSI
1257
1258 // conversions with (possible) format conversions: have to return a
1259 // buffer with temporary data
1260 //
1261 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
1262 // return an ANSI (multibyte) string, wc_str() to return a wide string and
1263 // fn_str() to return a string which should be used with the OS APIs
1264 // accepting the file names. The return value is always the same, but the
1265 // type differs because a function may either return pointer to the buffer
1266 // directly or have to use intermediate buffer for translation.
1267 #if wxUSE_UNICODE
1268
1269 #if wxUSE_UTF8_LOCALE_ONLY
1270 const char* mb_str() const { return wx_str(); }
1271 const wxCharBuffer mb_str(const wxMBConv& conv) const;
1272 #else
1273 const wxCharBuffer mb_str(const wxMBConv& conv = wxConvLibc) const;
1274 #endif
1275
1276 const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); }
1277
1278 #if wxUSE_UNICODE_WCHAR
1279 const wxChar* wc_str() const { return wx_str(); }
1280 #elif wxUSE_UNICODE_UTF8
1281 const wxWCharBuffer wc_str() const;
1282 #endif
1283 // for compatibility with !wxUSE_UNICODE version
1284 const wxWX2WCbuf wc_str(const wxMBConv& WXUNUSED(conv)) const
1285 { return wc_str(); }
1286
1287 #if wxMBFILES
1288 const wxCharBuffer fn_str() const { return mb_str(wxConvFile); }
1289 #else // !wxMBFILES
1290 const wxWX2WCbuf fn_str() const { return wc_str(); }
1291 #endif // wxMBFILES/!wxMBFILES
1292
1293 #else // ANSI
1294 const wxChar* mb_str() const { return wx_str(); }
1295
1296 // for compatibility with wxUSE_UNICODE version
1297 const wxChar* mb_str(const wxMBConv& WXUNUSED(conv)) const { return wx_str(); }
1298
1299 const wxWX2MBbuf mbc_str() const { return mb_str(); }
1300
1301 #if wxUSE_WCHAR_T
1302 const wxWCharBuffer wc_str(const wxMBConv& conv = wxConvLibc) const;
1303 #endif // wxUSE_WCHAR_T
1304 #ifdef __WXOSX__
1305 const wxCharBuffer fn_str() const { return wxConvFile.cWC2WX( wc_str( wxConvLocal ) ); }
1306 #else
1307 const wxChar* fn_str() const { return c_str(); }
1308 #endif
1309 #endif // Unicode/ANSI
1310
1311 // overloaded assignment
1312 // from another wxString
1313 wxString& operator=(const wxString& stringSrc)
1314 { m_impl = stringSrc.m_impl; return *this; }
1315 wxString& operator=(const wxCStrData& cstr)
1316 { return *this = cstr.AsString(); }
1317 // from a character
1318 wxString& operator=(wxUniChar ch)
1319 { m_impl = wxStringOperations::EncodeChar(ch); return *this; }
1320 wxString& operator=(wxUniCharRef ch)
1321 { return operator=((wxUniChar)ch); }
1322 wxString& operator=(char ch)
1323 { return operator=(wxUniChar(ch)); }
1324 wxString& operator=(unsigned char ch)
1325 { return operator=(wxUniChar(ch)); }
1326 wxString& operator=(wchar_t ch)
1327 { return operator=(wxUniChar(ch)); }
1328 // from a C string - STL probably will crash on NULL,
1329 // so we need to compensate in that case
1330 #if wxUSE_STL_BASED_WXSTRING
1331 wxString& operator=(const char *psz)
1332 { if (psz) m_impl = ImplStr(psz); else Clear(); return *this; }
1333 wxString& operator=(const wchar_t *pwz)
1334 { if (pwz) m_impl = ImplStr(pwz); else Clear(); return *this; }
1335 #else
1336 wxString& operator=(const char *psz)
1337 { m_impl = ImplStr(psz); return *this; }
1338 wxString& operator=(const wchar_t *pwz)
1339 { m_impl = ImplStr(pwz); return *this; }
1340 #endif
1341 wxString& operator=(const unsigned char *psz)
1342 { return operator=((const char*)psz); }
1343
1344 // from wxWCharBuffer
1345 wxString& operator=(const wxWCharBuffer& s)
1346 { return operator=(s.data()); } // FIXME-UTF8: fix for embedded NULs
1347 // from wxCharBuffer
1348 wxString& operator=(const wxCharBuffer& s)
1349 { return operator=(s.data()); } // FIXME-UTF8: fix for embedded NULs
1350
1351 // string concatenation
1352 // in place concatenation
1353 /*
1354 Concatenate and return the result. Note that the left to right
1355 associativity of << allows to write things like "str << str1 << str2
1356 << ..." (unlike with +=)
1357 */
1358 // string += string
1359 wxString& operator<<(const wxString& s)
1360 {
1361 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1362 wxASSERT_MSG( s.IsValid(),
1363 _T("did you forget to call UngetWriteBuf()?") );
1364 #endif
1365
1366 append(s);
1367 return *this;
1368 }
1369 // string += C string
1370 wxString& operator<<(const char *psz)
1371 { append(psz); return *this; }
1372 wxString& operator<<(const wchar_t *pwz)
1373 { append(pwz); return *this; }
1374 wxString& operator<<(const wxCStrData& psz)
1375 { append(psz.AsString()); return *this; }
1376 // string += char
1377 wxString& operator<<(wxUniChar ch) { append(1, ch); return *this; }
1378 wxString& operator<<(wxUniCharRef ch) { append(1, ch); return *this; }
1379 wxString& operator<<(char ch) { append(1, ch); return *this; }
1380 wxString& operator<<(unsigned char ch) { append(1, ch); return *this; }
1381 wxString& operator<<(wchar_t ch) { append(1, ch); return *this; }
1382
1383 // string += buffer (i.e. from wxGetString)
1384 wxString& operator<<(const wxWCharBuffer& s)
1385 { return operator<<((const wchar_t *)s); }
1386 wxString& operator<<(const wxCharBuffer& s)
1387 { return operator<<((const char *)s); }
1388
1389 // string += C string
1390 wxString& Append(const wxString& s)
1391 {
1392 // test for empty() to share the string if possible
1393 if ( empty() )
1394 *this = s;
1395 else
1396 append(s);
1397 return *this;
1398 }
1399 wxString& Append(const char* psz)
1400 { append(psz); return *this; }
1401 wxString& Append(const wchar_t* pwz)
1402 { append(pwz); return *this; }
1403 wxString& Append(const wxCStrData& psz)
1404 { append(psz); return *this; }
1405 wxString& Append(const wxCharBuffer& psz)
1406 { append(psz); return *this; }
1407 wxString& Append(const wxWCharBuffer& psz)
1408 { append(psz); return *this; }
1409 // append count copies of given character
1410 wxString& Append(wxUniChar ch, size_t count = 1u)
1411 { append(count, ch); return *this; }
1412 wxString& Append(wxUniCharRef ch, size_t count = 1u)
1413 { append(count, ch); return *this; }
1414 wxString& Append(char ch, size_t count = 1u)
1415 { append(count, ch); return *this; }
1416 wxString& Append(unsigned char ch, size_t count = 1u)
1417 { append(count, ch); return *this; }
1418 wxString& Append(wchar_t ch, size_t count = 1u)
1419 { append(count, ch); return *this; }
1420 wxString& Append(const char* psz, size_t nLen)
1421 { append(psz, nLen); return *this; }
1422 wxString& Append(const wchar_t* pwz, size_t nLen)
1423 { append(pwz, nLen); return *this; }
1424
1425 // prepend a string, return the string itself
1426 wxString& Prepend(const wxString& str)
1427 { *this = str + *this; return *this; }
1428
1429 // non-destructive concatenation
1430 // two strings
1431 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string1,
1432 const wxString& string2);
1433 // string with a single char
1434 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch);
1435 // char with a string
1436 friend wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string);
1437 // string with C string
1438 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string,
1439 const char *psz);
1440 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string,
1441 const wchar_t *pwz);
1442 // C string with string
1443 friend wxString WXDLLIMPEXP_BASE operator+(const char *psz,
1444 const wxString& string);
1445 friend wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz,
1446 const wxString& string);
1447
1448 // stream-like functions
1449 // insert an int into string
1450 wxString& operator<<(int i)
1451 { return (*this) << Format(_T("%d"), i); }
1452 // insert an unsigned int into string
1453 wxString& operator<<(unsigned int ui)
1454 { return (*this) << Format(_T("%u"), ui); }
1455 // insert a long into string
1456 wxString& operator<<(long l)
1457 { return (*this) << Format(_T("%ld"), l); }
1458 // insert an unsigned long into string
1459 wxString& operator<<(unsigned long ul)
1460 { return (*this) << Format(_T("%lu"), ul); }
1461 #if defined wxLongLong_t && !defined wxLongLongIsLong
1462 // insert a long long if they exist and aren't longs
1463 wxString& operator<<(wxLongLong_t ll)
1464 {
1465 const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("d");
1466 return (*this) << Format(fmt, ll);
1467 }
1468 // insert an unsigned long long
1469 wxString& operator<<(wxULongLong_t ull)
1470 {
1471 const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("u");
1472 return (*this) << Format(fmt , ull);
1473 }
1474 #endif
1475 // insert a float into string
1476 wxString& operator<<(float f)
1477 { return (*this) << Format(_T("%f"), f); }
1478 // insert a double into string
1479 wxString& operator<<(double d)
1480 { return (*this) << Format(_T("%g"), d); }
1481
1482 // string comparison
1483 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
1484 int Cmp(const char *psz) const
1485 { return compare(psz); }
1486 int Cmp(const wchar_t *pwz) const
1487 { return compare(pwz); }
1488 int Cmp(const wxString& s) const
1489 { return compare(s); }
1490 int Cmp(const wxCStrData& s) const
1491 { return compare(s); }
1492 int Cmp(const wxCharBuffer& s) const
1493 { return compare(s); }
1494 int Cmp(const wxWCharBuffer& s) const
1495 { return compare(s); }
1496 // same as Cmp() but not case-sensitive
1497 int CmpNoCase(const wxString& s) const;
1498 // test for the string equality, either considering case or not
1499 // (if compareWithCase then the case matters)
1500 bool IsSameAs(const wxString& str, bool compareWithCase = true) const
1501 { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; }
1502 bool IsSameAs(const char *str, bool compareWithCase = true) const
1503 { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; }
1504 bool IsSameAs(const wchar_t *str, bool compareWithCase = true) const
1505 { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; }
1506 bool IsSameAs(const wxCStrData& str, bool compareWithCase = true) const
1507 { return IsSameAs(str.AsString(), compareWithCase); }
1508 bool IsSameAs(const wxCharBuffer& str, bool compareWithCase = true) const
1509 { return IsSameAs(str.data(), compareWithCase); }
1510 bool IsSameAs(const wxWCharBuffer& str, bool compareWithCase = true) const
1511 { return IsSameAs(str.data(), compareWithCase); }
1512 // comparison with a single character: returns true if equal
1513 bool IsSameAs(wxUniChar c, bool compareWithCase = true) const;
1514 // FIXME-UTF8: remove these overloads
1515 bool IsSameAs(wxUniCharRef c, bool compareWithCase = true) const
1516 { return IsSameAs(wxUniChar(c), compareWithCase); }
1517 bool IsSameAs(char c, bool compareWithCase = true) const
1518 { return IsSameAs(wxUniChar(c), compareWithCase); }
1519 bool IsSameAs(unsigned char c, bool compareWithCase = true) const
1520 { return IsSameAs(wxUniChar(c), compareWithCase); }
1521 bool IsSameAs(wchar_t c, bool compareWithCase = true) const
1522 { return IsSameAs(wxUniChar(c), compareWithCase); }
1523 bool IsSameAs(int c, bool compareWithCase = true) const
1524 { return IsSameAs(wxUniChar(c), compareWithCase); }
1525
1526 // simple sub-string extraction
1527 // return substring starting at nFirst of length nCount (or till the end
1528 // if nCount = default value)
1529 wxString Mid(size_t nFirst, size_t nCount = npos) const;
1530
1531 // operator version of Mid()
1532 wxString operator()(size_t start, size_t len) const
1533 { return Mid(start, len); }
1534
1535 // check if the string starts with the given prefix and return the rest
1536 // of the string in the provided pointer if it is not NULL; otherwise
1537 // return false
1538 bool StartsWith(const wxString& prefix, wxString *rest = NULL) const;
1539 // check if the string ends with the given suffix and return the
1540 // beginning of the string before the suffix in the provided pointer if
1541 // it is not NULL; otherwise return false
1542 bool EndsWith(const wxString& suffix, wxString *rest = NULL) const;
1543
1544 // get first nCount characters
1545 wxString Left(size_t nCount) const;
1546 // get last nCount characters
1547 wxString Right(size_t nCount) const;
1548 // get all characters before the first occurance of ch
1549 // (returns the whole string if ch not found)
1550 wxString BeforeFirst(wxUniChar ch) const;
1551 // get all characters before the last occurence of ch
1552 // (returns empty string if ch not found)
1553 wxString BeforeLast(wxUniChar ch) const;
1554 // get all characters after the first occurence of ch
1555 // (returns empty string if ch not found)
1556 wxString AfterFirst(wxUniChar ch) const;
1557 // get all characters after the last occurence of ch
1558 // (returns the whole string if ch not found)
1559 wxString AfterLast(wxUniChar ch) const;
1560
1561 // for compatibility only, use more explicitly named functions above
1562 wxString Before(wxUniChar ch) const { return BeforeLast(ch); }
1563 wxString After(wxUniChar ch) const { return AfterFirst(ch); }
1564
1565 // case conversion
1566 // convert to upper case in place, return the string itself
1567 wxString& MakeUpper();
1568 // convert to upper case, return the copy of the string
1569 // Here's something to remember: BC++ doesn't like returns in inlines.
1570 wxString Upper() const ;
1571 // convert to lower case in place, return the string itself
1572 wxString& MakeLower();
1573 // convert to lower case, return the copy of the string
1574 wxString Lower() const ;
1575
1576 // trimming/padding whitespace (either side) and truncating
1577 // remove spaces from left or from right (default) side
1578 wxString& Trim(bool bFromRight = true);
1579 // add nCount copies chPad in the beginning or at the end (default)
1580 wxString& Pad(size_t nCount, wxUniChar chPad = wxT(' '), bool bFromRight = true);
1581
1582 // searching and replacing
1583 // searching (return starting index, or -1 if not found)
1584 int Find(wxUniChar ch, bool bFromEnd = false) const; // like strchr/strrchr
1585 int Find(wxUniCharRef ch, bool bFromEnd = false) const
1586 { return Find(wxUniChar(ch), bFromEnd); }
1587 int Find(char ch, bool bFromEnd = false) const
1588 { return Find(wxUniChar(ch), bFromEnd); }
1589 int Find(unsigned char ch, bool bFromEnd = false) const
1590 { return Find(wxUniChar(ch), bFromEnd); }
1591 int Find(wchar_t ch, bool bFromEnd = false) const
1592 { return Find(wxUniChar(ch), bFromEnd); }
1593 // searching (return starting index, or -1 if not found)
1594 int Find(const wxString& sub) const // like strstr
1595 {
1596 size_type idx = find(sub);
1597 return (idx == npos) ? wxNOT_FOUND : (int)idx;
1598 }
1599 int Find(const char *sub) const // like strstr
1600 {
1601 size_type idx = find(sub);
1602 return (idx == npos) ? wxNOT_FOUND : (int)idx;
1603 }
1604 int Find(const wchar_t *sub) const // like strstr
1605 {
1606 size_type idx = find(sub);
1607 return (idx == npos) ? wxNOT_FOUND : (int)idx;
1608 }
1609
1610 int Find(const wxCStrData& sub) const
1611 { return Find(sub.AsString()); }
1612 int Find(const wxCharBuffer& sub) const
1613 { return Find(sub.data()); }
1614 int Find(const wxWCharBuffer& sub) const
1615 { return Find(sub.data()); }
1616
1617 // replace first (or all of bReplaceAll) occurences of substring with
1618 // another string, returns the number of replacements made
1619 size_t Replace(const wxString& strOld,
1620 const wxString& strNew,
1621 bool bReplaceAll = true);
1622
1623 // check if the string contents matches a mask containing '*' and '?'
1624 bool Matches(const wxString& mask) const;
1625
1626 // conversion to numbers: all functions return true only if the whole
1627 // string is a number and put the value of this number into the pointer
1628 // provided, the base is the numeric base in which the conversion should be
1629 // done and must be comprised between 2 and 36 or be 0 in which case the
1630 // standard C rules apply (leading '0' => octal, "0x" => hex)
1631 // convert to a signed integer
1632 bool ToLong(long *val, int base = 10) const;
1633 // convert to an unsigned integer
1634 bool ToULong(unsigned long *val, int base = 10) const;
1635 // convert to wxLongLong
1636 #if defined(wxLongLong_t)
1637 bool ToLongLong(wxLongLong_t *val, int base = 10) const;
1638 // convert to wxULongLong
1639 bool ToULongLong(wxULongLong_t *val, int base = 10) const;
1640 #endif // wxLongLong_t
1641 // convert to a double
1642 bool ToDouble(double *val) const;
1643
1644
1645 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1646 // formatted input/output
1647 // as sprintf(), returns the number of characters written or < 0 on error
1648 // (take 'this' into account in attribute parameter count)
1649 // int Printf(const wxString& format, ...);
1650 WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const wxFormatString&),
1651 DoPrintfWchar, DoPrintfUtf8)
1652 #ifdef __WATCOMC__
1653 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1654 WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wxString&),
1655 (wxFormatString(f1)));
1656 WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wxCStrData&),
1657 (wxFormatString(f1)));
1658 WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const char*),
1659 (wxFormatString(f1)));
1660 WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wchar_t*),
1661 (wxFormatString(f1)));
1662 #endif
1663 #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN
1664 // as vprintf(), returns the number of characters written or < 0 on error
1665 int PrintfV(const wxString& format, va_list argptr);
1666
1667 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1668 // returns the string containing the result of Printf() to it
1669 // static wxString Format(const wxString& format, ...) ATTRIBUTE_PRINTF_1;
1670 WX_DEFINE_VARARG_FUNC(static wxString, Format, 1, (const wxFormatString&),
1671 DoFormatWchar, DoFormatUtf8)
1672 #ifdef __WATCOMC__
1673 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1674 WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wxString&),
1675 (wxFormatString(f1)));
1676 WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wxCStrData&),
1677 (wxFormatString(f1)));
1678 WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const char*),
1679 (wxFormatString(f1)));
1680 WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wchar_t*),
1681 (wxFormatString(f1)));
1682 #endif
1683 #endif
1684 // the same as above, but takes a va_list
1685 static wxString FormatV(const wxString& format, va_list argptr);
1686
1687 // raw access to string memory
1688 // ensure that string has space for at least nLen characters
1689 // only works if the data of this string is not shared
1690 bool Alloc(size_t nLen) { reserve(nLen); /*return capacity() >= nLen;*/ return true; }
1691 // minimize the string's memory
1692 // only works if the data of this string is not shared
1693 bool Shrink();
1694 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1695 // These are deprecated, use wxStringBuffer or wxStringBufferLength instead
1696 //
1697 // get writable buffer of at least nLen bytes. Unget() *must* be called
1698 // a.s.a.p. to put string back in a reasonable state!
1699 wxDEPRECATED( wxStringCharType *GetWriteBuf(size_t nLen) );
1700 // call this immediately after GetWriteBuf() has been used
1701 wxDEPRECATED( void UngetWriteBuf() );
1702 wxDEPRECATED( void UngetWriteBuf(size_t nLen) );
1703 #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && wxUSE_UNICODE_UTF8
1704
1705 // wxWidgets version 1 compatibility functions
1706
1707 // use Mid()
1708 wxString SubString(size_t from, size_t to) const
1709 { return Mid(from, (to - from + 1)); }
1710 // values for second parameter of CompareTo function
1711 enum caseCompare {exact, ignoreCase};
1712 // values for first parameter of Strip function
1713 enum stripType {leading = 0x1, trailing = 0x2, both = 0x3};
1714
1715 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1716 // use Printf()
1717 // (take 'this' into account in attribute parameter count)
1718 // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2;
1719 WX_DEFINE_VARARG_FUNC(int, sprintf, 1, (const wxFormatString&),
1720 DoPrintfWchar, DoPrintfUtf8)
1721 #ifdef __WATCOMC__
1722 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1723 WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wxString&),
1724 (wxFormatString(f1)));
1725 WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wxCStrData&),
1726 (wxFormatString(f1)));
1727 WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const char*),
1728 (wxFormatString(f1)));
1729 WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wchar_t*),
1730 (wxFormatString(f1)));
1731 #endif
1732 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
1733
1734 // use Cmp()
1735 inline int CompareTo(const wxChar* psz, caseCompare cmp = exact) const
1736 { return cmp == exact ? Cmp(psz) : CmpNoCase(psz); }
1737
1738 // use length()
1739 size_t Length() const { return length(); }
1740 // Count the number of characters
1741 int Freq(wxUniChar ch) const;
1742 // use MakeLower
1743 void LowerCase() { MakeLower(); }
1744 // use MakeUpper
1745 void UpperCase() { MakeUpper(); }
1746 // use Trim except that it doesn't change this string
1747 wxString Strip(stripType w = trailing) const;
1748
1749 // use Find (more general variants not yet supported)
1750 size_t Index(const wxChar* psz) const { return Find(psz); }
1751 size_t Index(wxUniChar ch) const { return Find(ch); }
1752 // use Truncate
1753 wxString& Remove(size_t pos) { return Truncate(pos); }
1754 wxString& RemoveLast(size_t n = 1) { return Truncate(length() - n); }
1755
1756 wxString& Remove(size_t nStart, size_t nLen)
1757 { return (wxString&)erase( nStart, nLen ); }
1758
1759 // use Find()
1760 int First( wxUniChar ch ) const { return Find(ch); }
1761 int First( wxUniCharRef ch ) const { return Find(ch); }
1762 int First( char ch ) const { return Find(ch); }
1763 int First( unsigned char ch ) const { return Find(ch); }
1764 int First( wchar_t ch ) const { return Find(ch); }
1765 int First( const wxString& str ) const { return Find(str); }
1766 int Last( wxUniChar ch ) const { return Find(ch, true); }
1767 bool Contains(const wxString& str) const { return Find(str) != wxNOT_FOUND; }
1768
1769 // use empty()
1770 bool IsNull() const { return empty(); }
1771
1772 // std::string compatibility functions
1773
1774 // take nLen chars starting at nPos
1775 wxString(const wxString& str, size_t nPos, size_t nLen)
1776 { assign(str, nPos, nLen); }
1777 // take all characters from first to last
1778 wxString(const_iterator first, const_iterator last)
1779 : m_impl(first.impl(), last.impl()) { }
1780 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1781 // the 2 overloads below are for compatibility with the existing code using
1782 // pointers instead of iterators
1783 wxString(const char *first, const char *last)
1784 {
1785 SubstrBufFromMB str(ImplStr(first, last - first));
1786 m_impl.assign(str.data, str.len);
1787 }
1788 wxString(const wchar_t *first, const wchar_t *last)
1789 {
1790 SubstrBufFromWC str(ImplStr(first, last - first));
1791 m_impl.assign(str.data, str.len);
1792 }
1793 // and this one is needed to compile code adding offsets to c_str() result
1794 wxString(const wxCStrData& first, const wxCStrData& last)
1795 : m_impl(CreateConstIterator(first).impl(),
1796 CreateConstIterator(last).impl())
1797 {
1798 wxASSERT_MSG( first.m_str == last.m_str,
1799 _T("pointers must be into the same string") );
1800 }
1801 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1802
1803 // lib.string.modifiers
1804 // append elements str[pos], ..., str[pos+n]
1805 wxString& append(const wxString& str, size_t pos, size_t n)
1806 {
1807 size_t from, len;
1808 str.PosLenToImpl(pos, n, &from, &len);
1809 m_impl.append(str.m_impl, from, len);
1810 return *this;
1811 }
1812 // append a string
1813 wxString& append(const wxString& str)
1814 { m_impl.append(str.m_impl); return *this; }
1815 // append first n (or all if n == npos) characters of sz
1816 wxString& append(const char *sz)
1817 { m_impl.append(ImplStr(sz)); return *this; }
1818 wxString& append(const wchar_t *sz)
1819 { m_impl.append(ImplStr(sz)); return *this; }
1820 wxString& append(const char *sz, size_t n)
1821 {
1822 SubstrBufFromMB str(ImplStr(sz, n));
1823 m_impl.append(str.data, str.len);
1824 return *this;
1825 }
1826 wxString& append(const wchar_t *sz, size_t n)
1827 {
1828 SubstrBufFromWC str(ImplStr(sz, n));
1829 m_impl.append(str.data, str.len);
1830 return *this;
1831 }
1832
1833 wxString& append(const wxCStrData& str)
1834 { return append(str.AsString()); }
1835 wxString& append(const wxCharBuffer& str)
1836 { return append(str.data()); }
1837 wxString& append(const wxWCharBuffer& str)
1838 { return append(str.data()); }
1839
1840 // append n copies of ch
1841 wxString& append(size_t n, wxUniChar ch)
1842 {
1843 #if wxUSE_UNICODE_UTF8
1844 if ( !ch.IsAscii() )
1845 m_impl.append(wxStringOperations::EncodeNChars(n, ch));
1846 else
1847 #endif
1848 m_impl.append(n, (wxStringCharType)ch);
1849 return *this;
1850 }
1851 // append from first to last
1852 wxString& append(const_iterator first, const_iterator last)
1853 { m_impl.append(first.impl(), last.impl()); return *this; }
1854 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1855 wxString& append(const char *first, const char *last)
1856 { return append(first, last - first); }
1857 wxString& append(const wchar_t *first, const wchar_t *last)
1858 { return append(first, last - first); }
1859 wxString& append(const wxCStrData& first, const wxCStrData& last)
1860 { return append(CreateConstIterator(first), CreateConstIterator(last)); }
1861 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1862
1863 // same as `this_string = str'
1864 wxString& assign(const wxString& str)
1865 { m_impl = str.m_impl; return *this; }
1866 wxString& assign(const wxString& str, size_t len)
1867 {
1868 m_impl.assign(str.m_impl, 0, str.LenToImpl(len));
1869 return *this;
1870 }
1871 // same as ` = str[pos..pos + n]
1872 wxString& assign(const wxString& str, size_t pos, size_t n)
1873 {
1874 size_t from, len;
1875 str.PosLenToImpl(pos, n, &from, &len);
1876 m_impl.assign(str.m_impl, from, len);
1877 return *this;
1878 }
1879 // same as `= first n (or all if n == npos) characters of sz'
1880 wxString& assign(const char *sz)
1881 { m_impl.assign(ImplStr(sz)); return *this; }
1882 wxString& assign(const wchar_t *sz)
1883 { m_impl.assign(ImplStr(sz)); return *this; }
1884 wxString& assign(const char *sz, size_t n)
1885 {
1886 SubstrBufFromMB str(ImplStr(sz, n));
1887 m_impl.assign(str.data, str.len);
1888 return *this;
1889 }
1890 wxString& assign(const wchar_t *sz, size_t n)
1891 {
1892 SubstrBufFromWC str(ImplStr(sz, n));
1893 m_impl.assign(str.data, str.len);
1894 return *this;
1895 }
1896
1897 wxString& assign(const wxCStrData& str)
1898 { return assign(str.AsString()); }
1899 wxString& assign(const wxCharBuffer& str)
1900 { return assign(str.data()); }
1901 wxString& assign(const wxWCharBuffer& str)
1902 { return assign(str.data()); }
1903 wxString& assign(const wxCStrData& str, size_t len)
1904 { return assign(str.AsString(), len); }
1905 wxString& assign(const wxCharBuffer& str, size_t len)
1906 { return assign(str.data(), len); }
1907 wxString& assign(const wxWCharBuffer& str, size_t len)
1908 { return assign(str.data(), len); }
1909
1910 // same as `= n copies of ch'
1911 wxString& assign(size_t n, wxUniChar ch)
1912 {
1913 #if wxUSE_UNICODE_UTF8
1914 if ( !ch.IsAscii() )
1915 m_impl.assign(wxStringOperations::EncodeNChars(n, ch));
1916 else
1917 #endif
1918 m_impl.assign(n, (wxStringCharType)ch);
1919 return *this;
1920 }
1921
1922 wxString& assign(size_t n, wxUniCharRef ch)
1923 { return assign(n, wxUniChar(ch)); }
1924 wxString& assign(size_t n, char ch)
1925 { return assign(n, wxUniChar(ch)); }
1926 wxString& assign(size_t n, unsigned char ch)
1927 { return assign(n, wxUniChar(ch)); }
1928 wxString& assign(size_t n, wchar_t ch)
1929 { return assign(n, wxUniChar(ch)); }
1930
1931 // assign from first to last
1932 wxString& assign(const_iterator first, const_iterator last)
1933 { m_impl.assign(first.impl(), last.impl()); return *this; }
1934 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1935 wxString& assign(const char *first, const char *last)
1936 { return assign(first, last - first); }
1937 wxString& assign(const wchar_t *first, const wchar_t *last)
1938 { return assign(first, last - first); }
1939 wxString& assign(const wxCStrData& first, const wxCStrData& last)
1940 { return assign(CreateConstIterator(first), CreateConstIterator(last)); }
1941 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1942
1943 // string comparison
1944 int compare(const wxString& str) const;
1945 int compare(const char* sz) const;
1946 int compare(const wchar_t* sz) const;
1947 int compare(const wxCStrData& str) const
1948 { return compare(str.AsString()); }
1949 int compare(const wxCharBuffer& str) const
1950 { return compare(str.data()); }
1951 int compare(const wxWCharBuffer& str) const
1952 { return compare(str.data()); }
1953 // comparison with a substring
1954 int compare(size_t nStart, size_t nLen, const wxString& str) const;
1955 // comparison of 2 substrings
1956 int compare(size_t nStart, size_t nLen,
1957 const wxString& str, size_t nStart2, size_t nLen2) const;
1958 // substring comparison with first nCount characters of sz
1959 int compare(size_t nStart, size_t nLen,
1960 const char* sz, size_t nCount = npos) const;
1961 int compare(size_t nStart, size_t nLen,
1962 const wchar_t* sz, size_t nCount = npos) const;
1963
1964 // insert another string
1965 wxString& insert(size_t nPos, const wxString& str)
1966 { insert(begin() + nPos, str.begin(), str.end()); return *this; }
1967 // insert n chars of str starting at nStart (in str)
1968 wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n)
1969 {
1970 size_t from, len;
1971 str.PosLenToImpl(nStart, n, &from, &len);
1972 m_impl.insert(PosToImpl(nPos), str.m_impl, from, len);
1973 return *this;
1974 }
1975 // insert first n (or all if n == npos) characters of sz
1976 wxString& insert(size_t nPos, const char *sz)
1977 { m_impl.insert(PosToImpl(nPos), ImplStr(sz)); return *this; }
1978 wxString& insert(size_t nPos, const wchar_t *sz)
1979 { m_impl.insert(PosToImpl(nPos), ImplStr(sz)); return *this; }
1980 wxString& insert(size_t nPos, const char *sz, size_t n)
1981 {
1982 SubstrBufFromMB str(ImplStr(sz, n));
1983 m_impl.insert(PosToImpl(nPos), str.data, str.len);
1984 return *this;
1985 }
1986 wxString& insert(size_t nPos, const wchar_t *sz, size_t n)
1987 {
1988 SubstrBufFromWC str(ImplStr(sz, n));
1989 m_impl.insert(PosToImpl(nPos), str.data, str.len);
1990 return *this;
1991 }
1992 // insert n copies of ch
1993 wxString& insert(size_t nPos, size_t n, wxUniChar ch)
1994 {
1995 #if wxUSE_UNICODE_UTF8
1996 if ( !ch.IsAscii() )
1997 m_impl.insert(PosToImpl(nPos), wxStringOperations::EncodeNChars(n, ch));
1998 else
1999 #endif
2000 m_impl.insert(PosToImpl(nPos), n, (wxStringCharType)ch);
2001 return *this;
2002 }
2003 iterator insert(iterator it, wxUniChar ch)
2004 {
2005 #if wxUSE_UNICODE_UTF8
2006 if ( !ch.IsAscii() )
2007 {
2008 size_t pos = IterToImplPos(it);
2009 m_impl.insert(pos, wxStringOperations::EncodeChar(ch));
2010 return iterator(this, m_impl.begin() + pos);
2011 }
2012 else
2013 #endif
2014 return iterator(this, m_impl.insert(it.impl(), (wxStringCharType)ch));
2015 }
2016 void insert(iterator it, const_iterator first, const_iterator last)
2017 { m_impl.insert(it.impl(), first.impl(), last.impl()); }
2018 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2019 void insert(iterator it, const char *first, const char *last)
2020 { insert(it - begin(), first, last - first); }
2021 void insert(iterator it, const wchar_t *first, const wchar_t *last)
2022 { insert(it - begin(), first, last - first); }
2023 void insert(iterator it, const wxCStrData& first, const wxCStrData& last)
2024 { insert(it, CreateConstIterator(first), CreateConstIterator(last)); }
2025 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2026
2027 void insert(iterator it, size_type n, wxUniChar ch)
2028 {
2029 #if wxUSE_UNICODE_UTF8
2030 if ( !ch.IsAscii() )
2031 m_impl.insert(IterToImplPos(it), wxStringOperations::EncodeNChars(n, ch));
2032 else
2033 #endif
2034 m_impl.insert(it.impl(), n, (wxStringCharType)ch);
2035 }
2036
2037 // delete characters from nStart to nStart + nLen
2038 wxString& erase(size_type pos = 0, size_type n = npos)
2039 {
2040 size_t from, len;
2041 PosLenToImpl(pos, n, &from, &len);
2042 m_impl.erase(from, len);
2043 return *this;
2044 }
2045 // delete characters from first up to last
2046 iterator erase(iterator first, iterator last)
2047 { return iterator(this, m_impl.erase(first.impl(), last.impl())); }
2048 iterator erase(iterator first)
2049 { return iterator(this, m_impl.erase(first.impl())); }
2050
2051 #ifdef wxSTRING_BASE_HASNT_CLEAR
2052 void clear() { erase(); }
2053 #else
2054 void clear() { m_impl.clear(); }
2055 #endif
2056
2057 // replaces the substring of length nLen starting at nStart
2058 wxString& replace(size_t nStart, size_t nLen, const char* sz)
2059 {
2060 size_t from, len;
2061 PosLenToImpl(nStart, nLen, &from, &len);
2062 m_impl.replace(from, len, ImplStr(sz));
2063 return *this;
2064 }
2065 wxString& replace(size_t nStart, size_t nLen, const wchar_t* sz)
2066 {
2067 size_t from, len;
2068 PosLenToImpl(nStart, nLen, &from, &len);
2069 m_impl.replace(from, len, ImplStr(sz));
2070 return *this;
2071 }
2072 // replaces the substring of length nLen starting at nStart
2073 wxString& replace(size_t nStart, size_t nLen, const wxString& str)
2074 {
2075 size_t from, len;
2076 PosLenToImpl(nStart, nLen, &from, &len);
2077 m_impl.replace(from, len, str.m_impl);
2078 return *this;
2079 }
2080 // replaces the substring with nCount copies of ch
2081 wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxUniChar ch)
2082 {
2083 size_t from, len;
2084 PosLenToImpl(nStart, nLen, &from, &len);
2085 #if wxUSE_UNICODE_UTF8
2086 if ( !ch.IsAscii() )
2087 m_impl.replace(from, len, wxStringOperations::EncodeNChars(nCount, ch));
2088 else
2089 #endif
2090 m_impl.replace(from, len, nCount, (wxStringCharType)ch);
2091 return *this;
2092 }
2093 // replaces a substring with another substring
2094 wxString& replace(size_t nStart, size_t nLen,
2095 const wxString& str, size_t nStart2, size_t nLen2)
2096 {
2097 size_t from, len;
2098 PosLenToImpl(nStart, nLen, &from, &len);
2099
2100 size_t from2, len2;
2101 str.PosLenToImpl(nStart2, nLen2, &from2, &len2);
2102
2103 m_impl.replace(from, len, str.m_impl, from2, len2);
2104 return *this;
2105 }
2106 // replaces the substring with first nCount chars of sz
2107 wxString& replace(size_t nStart, size_t nLen,
2108 const char* sz, size_t nCount)
2109 {
2110 size_t from, len;
2111 PosLenToImpl(nStart, nLen, &from, &len);
2112
2113 SubstrBufFromMB str(ImplStr(sz, nCount));
2114
2115 m_impl.replace(from, len, str.data, str.len);
2116 return *this;
2117 }
2118 wxString& replace(size_t nStart, size_t nLen,
2119 const wchar_t* sz, size_t nCount)
2120 {
2121 size_t from, len;
2122 PosLenToImpl(nStart, nLen, &from, &len);
2123
2124 SubstrBufFromWC str(ImplStr(sz, nCount));
2125
2126 m_impl.replace(from, len, str.data, str.len);
2127 return *this;
2128 }
2129 wxString& replace(size_t nStart, size_t nLen,
2130 const wxString& s, size_t nCount)
2131 {
2132 size_t from, len;
2133 PosLenToImpl(nStart, nLen, &from, &len);
2134 m_impl.replace(from, len, s.m_impl.c_str(), s.LenToImpl(nCount));
2135 return *this;
2136 }
2137
2138 wxString& replace(iterator first, iterator last, const char* s)
2139 { m_impl.replace(first.impl(), last.impl(), ImplStr(s)); return *this; }
2140 wxString& replace(iterator first, iterator last, const wchar_t* s)
2141 { m_impl.replace(first.impl(), last.impl(), ImplStr(s)); return *this; }
2142 wxString& replace(iterator first, iterator last, const char* s, size_type n)
2143 {
2144 SubstrBufFromMB str(ImplStr(s, n));
2145 m_impl.replace(first.impl(), last.impl(), str.data, str.len);
2146 return *this;
2147 }
2148 wxString& replace(iterator first, iterator last, const wchar_t* s, size_type n)
2149 {
2150 SubstrBufFromWC str(ImplStr(s, n));
2151 m_impl.replace(first.impl(), last.impl(), str.data, str.len);
2152 return *this;
2153 }
2154 wxString& replace(iterator first, iterator last, const wxString& s)
2155 { m_impl.replace(first.impl(), last.impl(), s.m_impl); return *this; }
2156 wxString& replace(iterator first, iterator last, size_type n, wxUniChar ch)
2157 {
2158 #if wxUSE_UNICODE_UTF8
2159 if ( !ch.IsAscii() )
2160 m_impl.replace(first.impl(), last.impl(),
2161 wxStringOperations::EncodeNChars(n, ch));
2162 else
2163 #endif
2164 m_impl.replace(first.impl(), last.impl(), n, (wxStringCharType)ch);
2165 return *this;
2166 }
2167 wxString& replace(iterator first, iterator last,
2168 const_iterator first1, const_iterator last1)
2169 {
2170 m_impl.replace(first.impl(), last.impl(), first1.impl(), last1.impl());
2171 return *this;
2172 }
2173 wxString& replace(iterator first, iterator last,
2174 const char *first1, const char *last1)
2175 { replace(first, last, first1, last1 - first1); return *this; }
2176 wxString& replace(iterator first, iterator last,
2177 const wchar_t *first1, const wchar_t *last1)
2178 { replace(first, last, first1, last1 - first1); return *this; }
2179
2180 // swap two strings
2181 void swap(wxString& str)
2182 { m_impl.swap(str.m_impl); }
2183
2184 // find a substring
2185 size_t find(const wxString& str, size_t nStart = 0) const
2186 { return PosFromImpl(m_impl.find(str.m_impl, PosToImpl(nStart))); }
2187
2188 // find first n characters of sz
2189 size_t find(const char* sz, size_t nStart = 0, size_t n = npos) const
2190 {
2191 SubstrBufFromMB str(ImplStr(sz, n));
2192 return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len));
2193 }
2194 size_t find(const wchar_t* sz, size_t nStart = 0, size_t n = npos) const
2195 {
2196 SubstrBufFromWC str(ImplStr(sz, n));
2197 return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len));
2198 }
2199 size_t find(const wxCharBuffer& s, size_t nStart = 0, size_t n = npos) const
2200 { return find(s.data(), nStart, n); }
2201 size_t find(const wxWCharBuffer& s, size_t nStart = 0, size_t n = npos) const
2202 { return find(s.data(), nStart, n); }
2203 size_t find(const wxCStrData& s, size_t nStart = 0, size_t n = npos) const
2204 { return find(s.AsWChar(), nStart, n); }
2205
2206 // find the first occurence of character ch after nStart
2207 size_t find(wxUniChar ch, size_t nStart = 0) const
2208 {
2209 return PosFromImpl(m_impl.find(wxStringOperations::EncodeChar(ch),
2210 PosToImpl(nStart)));
2211 }
2212 size_t find(wxUniCharRef ch, size_t nStart = 0) const
2213 { return find(wxUniChar(ch), nStart); }
2214 size_t find(char ch, size_t nStart = 0) const
2215 { return find(wxUniChar(ch), nStart); }
2216 size_t find(unsigned char ch, size_t nStart = 0) const
2217 { return find(wxUniChar(ch), nStart); }
2218 size_t find(wchar_t ch, size_t nStart = 0) const
2219 { return find(wxUniChar(ch), nStart); }
2220
2221 // rfind() family is exactly like find() but works right to left
2222
2223 // as find, but from the end
2224 size_t rfind(const wxString& str, size_t nStart = npos) const
2225 { return PosFromImpl(m_impl.rfind(str.m_impl, PosToImpl(nStart))); }
2226
2227 // as find, but from the end
2228 size_t rfind(const char* sz, size_t nStart = npos, size_t n = npos) const
2229 {
2230 SubstrBufFromMB str(ImplStr(sz, n));
2231 return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len));
2232 }
2233 size_t rfind(const wchar_t* sz, size_t nStart = npos, size_t n = npos) const
2234 {
2235 SubstrBufFromWC str(ImplStr(sz, n));
2236 return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len));
2237 }
2238 size_t rfind(const wxCharBuffer& s, size_t nStart = npos, size_t n = npos) const
2239 { return rfind(s.data(), nStart, n); }
2240 size_t rfind(const wxWCharBuffer& s, size_t nStart = npos, size_t n = npos) const
2241 { return rfind(s.data(), nStart, n); }
2242 size_t rfind(const wxCStrData& s, size_t nStart = npos, size_t n = npos) const
2243 { return rfind(s.AsWChar(), nStart, n); }
2244 // as find, but from the end
2245 size_t rfind(wxUniChar ch, size_t nStart = npos) const
2246 {
2247 return PosFromImpl(m_impl.rfind(wxStringOperations::EncodeChar(ch),
2248 PosToImpl(nStart)));
2249 }
2250 size_t rfind(wxUniCharRef ch, size_t nStart = npos) const
2251 { return rfind(wxUniChar(ch), nStart); }
2252 size_t rfind(char ch, size_t nStart = npos) const
2253 { return rfind(wxUniChar(ch), nStart); }
2254 size_t rfind(unsigned char ch, size_t nStart = npos) const
2255 { return rfind(wxUniChar(ch), nStart); }
2256 size_t rfind(wchar_t ch, size_t nStart = npos) const
2257 { return rfind(wxUniChar(ch), nStart); }
2258
2259 // find first/last occurence of any character (not) in the set:
2260 #if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2261 // FIXME-UTF8: this is not entirely correct, because it doesn't work if
2262 // sizeof(wchar_t)==2 and surrogates are present in the string;
2263 // should we care? Probably not.
2264 size_t find_first_of(const wxString& str, size_t nStart = 0) const
2265 { return m_impl.find_first_of(str.m_impl, nStart); }
2266 size_t find_first_of(const char* sz, size_t nStart = 0) const
2267 { return m_impl.find_first_of(ImplStr(sz), nStart); }
2268 size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const
2269 { return m_impl.find_first_of(ImplStr(sz), nStart); }
2270 size_t find_first_of(const char* sz, size_t nStart, size_t n) const
2271 { return m_impl.find_first_of(ImplStr(sz), nStart, n); }
2272 size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const
2273 { return m_impl.find_first_of(ImplStr(sz), nStart, n); }
2274 size_t find_first_of(wxUniChar c, size_t nStart = 0) const
2275 { return m_impl.find_first_of((wxChar)c, nStart); }
2276
2277 size_t find_last_of(const wxString& str, size_t nStart = npos) const
2278 { return m_impl.find_last_of(str.m_impl, nStart); }
2279 size_t find_last_of(const char* sz, size_t nStart = npos) const
2280 { return m_impl.find_last_of(ImplStr(sz), nStart); }
2281 size_t find_last_of(const wchar_t* sz, size_t nStart = npos) const
2282 { return m_impl.find_last_of(ImplStr(sz), nStart); }
2283 size_t find_last_of(const char* sz, size_t nStart, size_t n) const
2284 { return m_impl.find_last_of(ImplStr(sz), nStart, n); }
2285 size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const
2286 { return m_impl.find_last_of(ImplStr(sz), nStart, n); }
2287 size_t find_last_of(wxUniChar c, size_t nStart = npos) const
2288 { return m_impl.find_last_of((wxChar)c, nStart); }
2289
2290 size_t find_first_not_of(const wxString& str, size_t nStart = 0) const
2291 { return m_impl.find_first_not_of(str.m_impl, nStart); }
2292 size_t find_first_not_of(const char* sz, size_t nStart = 0) const
2293 { return m_impl.find_first_not_of(ImplStr(sz), nStart); }
2294 size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const
2295 { return m_impl.find_first_not_of(ImplStr(sz), nStart); }
2296 size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const
2297 { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); }
2298 size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const
2299 { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); }
2300 size_t find_first_not_of(wxUniChar c, size_t nStart = 0) const
2301 { return m_impl.find_first_not_of((wxChar)c, nStart); }
2302
2303 size_t find_last_not_of(const wxString& str, size_t nStart = npos) const
2304 { return m_impl.find_last_not_of(str.m_impl, nStart); }
2305 size_t find_last_not_of(const char* sz, size_t nStart = npos) const
2306 { return m_impl.find_last_not_of(ImplStr(sz), nStart); }
2307 size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const
2308 { return m_impl.find_last_not_of(ImplStr(sz), nStart); }
2309 size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const
2310 { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); }
2311 size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const
2312 { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); }
2313 size_t find_last_not_of(wxUniChar c, size_t nStart = npos) const
2314 { return m_impl.find_last_not_of((wxChar)c, nStart); }
2315 #else
2316 // we can't use std::string implementation in UTF-8 build, because the
2317 // character sets would be interpreted wrongly:
2318
2319 // as strpbrk() but starts at nStart, returns npos if not found
2320 size_t find_first_of(const wxString& str, size_t nStart = 0) const
2321 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2322 { return find_first_of(str.wc_str(), nStart); }
2323 #else
2324 { return find_first_of(str.mb_str(), nStart); }
2325 #endif
2326 // same as above
2327 size_t find_first_of(const char* sz, size_t nStart = 0) const;
2328 size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const;
2329 size_t find_first_of(const char* sz, size_t nStart, size_t n) const;
2330 size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const;
2331 // same as find(char, size_t)
2332 size_t find_first_of(wxUniChar c, size_t nStart = 0) const
2333 { return find(c, nStart); }
2334 // find the last (starting from nStart) char from str in this string
2335 size_t find_last_of (const wxString& str, size_t nStart = npos) const
2336 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2337 { return find_last_of(str.wc_str(), nStart); }
2338 #else
2339 { return find_last_of(str.mb_str(), nStart); }
2340 #endif
2341 // same as above
2342 size_t find_last_of (const char* sz, size_t nStart = npos) const;
2343 size_t find_last_of (const wchar_t* sz, size_t nStart = npos) const;
2344 size_t find_last_of(const char* sz, size_t nStart, size_t n) const;
2345 size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const;
2346 // same as above
2347 size_t find_last_of(wxUniChar c, size_t nStart = npos) const
2348 { return rfind(c, nStart); }
2349
2350 // find first/last occurence of any character not in the set
2351
2352 // as strspn() (starting from nStart), returns npos on failure
2353 size_t find_first_not_of(const wxString& str, size_t nStart = 0) const
2354 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2355 { return find_first_not_of(str.wc_str(), nStart); }
2356 #else
2357 { return find_first_not_of(str.mb_str(), nStart); }
2358 #endif
2359 // same as above
2360 size_t find_first_not_of(const char* sz, size_t nStart = 0) const;
2361 size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const;
2362 size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const;
2363 size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const;
2364 // same as above
2365 size_t find_first_not_of(wxUniChar ch, size_t nStart = 0) const;
2366 // as strcspn()
2367 size_t find_last_not_of(const wxString& str, size_t nStart = npos) const
2368 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2369 { return find_last_not_of(str.wc_str(), nStart); }
2370 #else
2371 { return find_last_not_of(str.mb_str(), nStart); }
2372 #endif
2373 // same as above
2374 size_t find_last_not_of(const char* sz, size_t nStart = npos) const;
2375 size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const;
2376 size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const;
2377 size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const;
2378 // same as above
2379 size_t find_last_not_of(wxUniChar ch, size_t nStart = npos) const;
2380 #endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not
2381
2382 // provide char/wchar_t/wxUniCharRef overloads for char-finding functions
2383 // above to resolve ambiguities:
2384 size_t find_first_of(wxUniCharRef ch, size_t nStart = 0) const
2385 { return find_first_of(wxUniChar(ch), nStart); }
2386 size_t find_first_of(char ch, size_t nStart = 0) const
2387 { return find_first_of(wxUniChar(ch), nStart); }
2388 size_t find_first_of(unsigned char ch, size_t nStart = 0) const
2389 { return find_first_of(wxUniChar(ch), nStart); }
2390 size_t find_first_of(wchar_t ch, size_t nStart = 0) const
2391 { return find_first_of(wxUniChar(ch), nStart); }
2392 size_t find_last_of(wxUniCharRef ch, size_t nStart = npos) const
2393 { return find_last_of(wxUniChar(ch), nStart); }
2394 size_t find_last_of(char ch, size_t nStart = npos) const
2395 { return find_last_of(wxUniChar(ch), nStart); }
2396 size_t find_last_of(unsigned char ch, size_t nStart = npos) const
2397 { return find_last_of(wxUniChar(ch), nStart); }
2398 size_t find_last_of(wchar_t ch, size_t nStart = npos) const
2399 { return find_last_of(wxUniChar(ch), nStart); }
2400 size_t find_first_not_of(wxUniCharRef ch, size_t nStart = 0) const
2401 { return find_first_not_of(wxUniChar(ch), nStart); }
2402 size_t find_first_not_of(char ch, size_t nStart = 0) const
2403 { return find_first_not_of(wxUniChar(ch), nStart); }
2404 size_t find_first_not_of(unsigned char ch, size_t nStart = 0) const
2405 { return find_first_not_of(wxUniChar(ch), nStart); }
2406 size_t find_first_not_of(wchar_t ch, size_t nStart = 0) const
2407 { return find_first_not_of(wxUniChar(ch), nStart); }
2408 size_t find_last_not_of(wxUniCharRef ch, size_t nStart = npos) const
2409 { return find_last_not_of(wxUniChar(ch), nStart); }
2410 size_t find_last_not_of(char ch, size_t nStart = npos) const
2411 { return find_last_not_of(wxUniChar(ch), nStart); }
2412 size_t find_last_not_of(unsigned char ch, size_t nStart = npos) const
2413 { return find_last_not_of(wxUniChar(ch), nStart); }
2414 size_t find_last_not_of(wchar_t ch, size_t nStart = npos) const
2415 { return find_last_not_of(wxUniChar(ch), nStart); }
2416
2417 // and additional overloads for the versions taking strings:
2418 size_t find_first_of(const wxCStrData& sz, size_t nStart = 0) const
2419 { return find_first_of(sz.AsString(), nStart); }
2420 size_t find_first_of(const wxCharBuffer& sz, size_t nStart = 0) const
2421 { return find_first_of(sz.data(), nStart); }
2422 size_t find_first_of(const wxWCharBuffer& sz, size_t nStart = 0) const
2423 { return find_first_of(sz.data(), nStart); }
2424 size_t find_first_of(const wxCStrData& sz, size_t nStart, size_t n) const
2425 { return find_first_of(sz.AsWChar(), nStart, n); }
2426 size_t find_first_of(const wxCharBuffer& sz, size_t nStart, size_t n) const
2427 { return find_first_of(sz.data(), nStart, n); }
2428 size_t find_first_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const
2429 { return find_first_of(sz.data(), nStart, n); }
2430
2431 size_t find_last_of(const wxCStrData& sz, size_t nStart = 0) const
2432 { return find_last_of(sz.AsString(), nStart); }
2433 size_t find_last_of(const wxCharBuffer& sz, size_t nStart = 0) const
2434 { return find_last_of(sz.data(), nStart); }
2435 size_t find_last_of(const wxWCharBuffer& sz, size_t nStart = 0) const
2436 { return find_last_of(sz.data(), nStart); }
2437 size_t find_last_of(const wxCStrData& sz, size_t nStart, size_t n) const
2438 { return find_last_of(sz.AsWChar(), nStart, n); }
2439 size_t find_last_of(const wxCharBuffer& sz, size_t nStart, size_t n) const
2440 { return find_last_of(sz.data(), nStart, n); }
2441 size_t find_last_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const
2442 { return find_last_of(sz.data(), nStart, n); }
2443
2444 size_t find_first_not_of(const wxCStrData& sz, size_t nStart = 0) const
2445 { return find_first_not_of(sz.AsString(), nStart); }
2446 size_t find_first_not_of(const wxCharBuffer& sz, size_t nStart = 0) const
2447 { return find_first_not_of(sz.data(), nStart); }
2448 size_t find_first_not_of(const wxWCharBuffer& sz, size_t nStart = 0) const
2449 { return find_first_not_of(sz.data(), nStart); }
2450 size_t find_first_not_of(const wxCStrData& sz, size_t nStart, size_t n) const
2451 { return find_first_not_of(sz.AsWChar(), nStart, n); }
2452 size_t find_first_not_of(const wxCharBuffer& sz, size_t nStart, size_t n) const
2453 { return find_first_not_of(sz.data(), nStart, n); }
2454 size_t find_first_not_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const
2455 { return find_first_not_of(sz.data(), nStart, n); }
2456
2457 size_t find_last_not_of(const wxCStrData& sz, size_t nStart = 0) const
2458 { return find_last_not_of(sz.AsString(), nStart); }
2459 size_t find_last_not_of(const wxCharBuffer& sz, size_t nStart = 0) const
2460 { return find_last_not_of(sz.data(), nStart); }
2461 size_t find_last_not_of(const wxWCharBuffer& sz, size_t nStart = 0) const
2462 { return find_last_not_of(sz.data(), nStart); }
2463 size_t find_last_not_of(const wxCStrData& sz, size_t nStart, size_t n) const
2464 { return find_last_not_of(sz.AsWChar(), nStart, n); }
2465 size_t find_last_not_of(const wxCharBuffer& sz, size_t nStart, size_t n) const
2466 { return find_last_not_of(sz.data(), nStart, n); }
2467 size_t find_last_not_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const
2468 { return find_last_not_of(sz.data(), nStart, n); }
2469
2470 // string += string
2471 wxString& operator+=(const wxString& s)
2472 { m_impl += s.m_impl; return *this; }
2473 // string += C string
2474 wxString& operator+=(const char *psz)
2475 { m_impl += ImplStr(psz); return *this; }
2476 wxString& operator+=(const wchar_t *pwz)
2477 { m_impl += ImplStr(pwz); return *this; }
2478 wxString& operator+=(const wxCStrData& s)
2479 { m_impl += s.AsString().m_impl; return *this; }
2480 wxString& operator+=(const wxCharBuffer& s)
2481 { return operator+=(s.data()); }
2482 wxString& operator+=(const wxWCharBuffer& s)
2483 { return operator+=(s.data()); }
2484 // string += char
2485 wxString& operator+=(wxUniChar ch)
2486 { m_impl += wxStringOperations::EncodeChar(ch); return *this; }
2487 wxString& operator+=(wxUniCharRef ch) { return *this += wxUniChar(ch); }
2488 wxString& operator+=(int ch) { return *this += wxUniChar(ch); }
2489 wxString& operator+=(char ch) { return *this += wxUniChar(ch); }
2490 wxString& operator+=(unsigned char ch) { return *this += wxUniChar(ch); }
2491 wxString& operator+=(wchar_t ch) { return *this += wxUniChar(ch); }
2492
2493 private:
2494 #if !wxUSE_STL_BASED_WXSTRING
2495 // helpers for wxStringBuffer and wxStringBufferLength
2496 wxStringCharType *DoGetWriteBuf(size_t nLen)
2497 { return m_impl.DoGetWriteBuf(nLen); }
2498 void DoUngetWriteBuf()
2499 { m_impl.DoUngetWriteBuf(); }
2500 void DoUngetWriteBuf(size_t nLen)
2501 { m_impl.DoUngetWriteBuf(nLen); }
2502 #endif // !wxUSE_STL_BASED_WXSTRING
2503
2504 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2505 #if !wxUSE_UTF8_LOCALE_ONLY
2506 int DoPrintfWchar(const wxChar *format, ...);
2507 static wxString DoFormatWchar(const wxChar *format, ...);
2508 #endif
2509 #if wxUSE_UNICODE_UTF8
2510 int DoPrintfUtf8(const char *format, ...);
2511 static wxString DoFormatUtf8(const char *format, ...);
2512 #endif
2513 #endif
2514
2515 #if !wxUSE_STL_BASED_WXSTRING
2516 // check string's data validity
2517 bool IsValid() const { return m_impl.GetStringData()->IsValid(); }
2518 #endif
2519
2520 private:
2521 wxStringImpl m_impl;
2522
2523 #ifdef __VISUALC__
2524 // "struct 'ConvertedBuffer<T>' needs to have dll-interface to be used by
2525 // clients of class 'wxString'" - this is private, we don't care
2526 #pragma warning (disable:4251)
2527 #endif
2528
2529 // buffers for compatibility conversion from (char*)c_str() and
2530 // (wchar_t*)c_str():
2531 // FIXME-UTF8: bechmark various approaches to keeping compatibility buffers
2532 template<typename T>
2533 struct ConvertedBuffer
2534 {
2535 ConvertedBuffer() : m_buf(NULL) {}
2536 ~ConvertedBuffer()
2537 { free(m_buf); }
2538
2539 operator T*() const { return m_buf; }
2540
2541 ConvertedBuffer& operator=(T *str)
2542 {
2543 free(m_buf);
2544 m_buf = str;
2545 return *this;
2546 }
2547
2548 T *m_buf;
2549 };
2550 #if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
2551 ConvertedBuffer<char> m_convertedToChar;
2552 #endif
2553 #if !wxUSE_UNICODE_WCHAR
2554 ConvertedBuffer<wchar_t> m_convertedToWChar;
2555 #endif
2556
2557 #ifdef __VISUALC__
2558 #pragma warning (default:4251)
2559 #endif
2560
2561 #if wxUSE_UNICODE_UTF8
2562 // FIXME-UTF8: (try to) move this elsewhere (TLS) or solve differently
2563 // assigning to character pointer to by wxString::interator may
2564 // change the underlying wxStringImpl iterator, so we have to
2565 // keep track of all iterators and update them as necessary:
2566 struct wxStringIteratorNodeHead
2567 {
2568 wxStringIteratorNodeHead() : ptr(NULL) {}
2569 wxStringIteratorNode *ptr;
2570 };
2571
2572 wxStringIteratorNodeHead m_iterators;
2573
2574 friend class WXDLLIMPEXP_BASE wxStringIteratorNode;
2575 friend class WXDLLIMPEXP_BASE wxUniCharRef;
2576 #endif // wxUSE_UNICODE_UTF8
2577
2578 friend class WXDLLIMPEXP_BASE wxCStrData;
2579 friend class wxImplStringBuffer;
2580 friend class wxImplStringBufferLength;
2581 };
2582
2583 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
2584 #pragma warning (default:4275)
2585 #endif
2586
2587 // string iterator operators that satisfy STL Random Access Iterator
2588 // requirements:
2589 inline wxString::iterator operator+(int n, wxString::iterator i)
2590 { return i + n; }
2591 inline wxString::iterator operator+(size_t n, wxString::iterator i)
2592 { return i + n; }
2593 inline wxString::const_iterator operator+(int n, wxString::const_iterator i)
2594 { return i + n; }
2595 inline wxString::const_iterator operator+(size_t n, wxString::const_iterator i)
2596 { return i + n; }
2597 inline wxString::reverse_iterator operator+(int n, wxString::reverse_iterator i)
2598 { return i + n; }
2599 inline wxString::reverse_iterator operator+(size_t n, wxString::reverse_iterator i)
2600 { return i + n; }
2601 inline wxString::const_reverse_iterator operator+(int n, wxString::const_reverse_iterator i)
2602 { return i + n; }
2603 inline wxString::const_reverse_iterator operator+(size_t n, wxString::const_reverse_iterator i)
2604 { return i + n; }
2605
2606 // notice that even though for many compilers the friend declarations above are
2607 // enough, from the point of view of C++ standard we must have the declarations
2608 // here as friend ones are not injected in the enclosing namespace and without
2609 // them the code fails to compile with conforming compilers such as xlC or g++4
2610 wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2);
2611 wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const char *psz);
2612 wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wchar_t *pwz);
2613 wxString WXDLLIMPEXP_BASE operator+(const char *psz, const wxString& string);
2614 wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz, const wxString& string);
2615
2616 wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch);
2617 wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string);
2618
2619 inline wxString operator+(const wxString& string, wxUniCharRef ch)
2620 { return string + (wxUniChar)ch; }
2621 inline wxString operator+(const wxString& string, char ch)
2622 { return string + wxUniChar(ch); }
2623 inline wxString operator+(const wxString& string, wchar_t ch)
2624 { return string + wxUniChar(ch); }
2625 inline wxString operator+(wxUniCharRef ch, const wxString& string)
2626 { return (wxUniChar)ch + string; }
2627 inline wxString operator+(char ch, const wxString& string)
2628 { return wxUniChar(ch) + string; }
2629 inline wxString operator+(wchar_t ch, const wxString& string)
2630 { return wxUniChar(ch) + string; }
2631
2632
2633 #if wxUSE_STL_BASED_WXSTRING
2634 // return an empty wxString (not very useful with wxUSE_STL == 1)
2635 inline const wxString wxGetEmptyString() { return wxString(); }
2636 #else // !wxUSE_STL_BASED_WXSTRING
2637 // return an empty wxString (more efficient than wxString() here)
2638 inline const wxString& wxGetEmptyString()
2639 {
2640 return *(wxString *)&wxEmptyString;
2641 }
2642 #endif // wxUSE_STL_BASED_WXSTRING/!wxUSE_STL_BASED_WXSTRING
2643
2644 // ----------------------------------------------------------------------------
2645 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
2646 // ----------------------------------------------------------------------------
2647
2648 #if !wxUSE_STL_BASED_WXSTRING
2649 // string buffer for direct access to string data in their native
2650 // representation:
2651 class wxImplStringBuffer
2652 {
2653 public:
2654 typedef wxStringCharType CharType;
2655
2656 wxImplStringBuffer(wxString& str, size_t lenWanted = 1024)
2657 : m_str(str), m_buf(NULL)
2658 { m_buf = m_str.DoGetWriteBuf(lenWanted); }
2659
2660 ~wxImplStringBuffer() { m_str.DoUngetWriteBuf(); }
2661
2662 operator wxStringCharType*() const { return m_buf; }
2663
2664 private:
2665 wxString& m_str;
2666 wxStringCharType *m_buf;
2667
2668 DECLARE_NO_COPY_CLASS(wxImplStringBuffer)
2669 };
2670
2671 class wxImplStringBufferLength
2672 {
2673 public:
2674 typedef wxStringCharType CharType;
2675
2676 wxImplStringBufferLength(wxString& str, size_t lenWanted = 1024)
2677 : m_str(str), m_buf(NULL), m_len(0), m_lenSet(false)
2678 {
2679 m_buf = m_str.DoGetWriteBuf(lenWanted);
2680 wxASSERT(m_buf != NULL);
2681 }
2682
2683 ~wxImplStringBufferLength()
2684 {
2685 wxASSERT(m_lenSet);
2686 m_str.DoUngetWriteBuf(m_len);
2687 }
2688
2689 operator wxStringCharType*() const { return m_buf; }
2690 void SetLength(size_t length) { m_len = length; m_lenSet = true; }
2691
2692 private:
2693 wxString& m_str;
2694 wxStringCharType *m_buf;
2695 size_t m_len;
2696 bool m_lenSet;
2697
2698 DECLARE_NO_COPY_CLASS(wxImplStringBufferLength)
2699 };
2700
2701 #endif // !wxUSE_STL_BASED_WXSTRING
2702
2703 template<typename T>
2704 class wxStringTypeBufferBase
2705 {
2706 public:
2707 typedef T CharType;
2708
2709 wxStringTypeBufferBase(wxString& str, size_t lenWanted = 1024)
2710 : m_str(str), m_buf(lenWanted)
2711 { }
2712
2713
2714 operator CharType*() { return m_buf.data(); }
2715
2716 protected:
2717 wxString& m_str;
2718 wxCharTypeBuffer<CharType> m_buf;
2719 };
2720
2721 template<typename T>
2722 class wxStringTypeBufferLengthBase
2723 {
2724 public:
2725 typedef T CharType;
2726
2727 wxStringTypeBufferLengthBase(wxString& str, size_t lenWanted = 1024)
2728 : m_str(str), m_buf(lenWanted), m_len(0), m_lenSet(false)
2729 { }
2730
2731 ~wxStringTypeBufferLengthBase()
2732 {
2733 wxASSERT(m_lenSet);
2734 m_str.assign(m_buf.data(), m_len);
2735 }
2736
2737 operator CharType*() { return m_buf.data(); }
2738 void SetLength(size_t length) { m_len = length; m_lenSet = true; }
2739
2740 protected:
2741 wxString& m_str;
2742 wxCharTypeBuffer<CharType> m_buf;
2743 size_t m_len;
2744 bool m_lenSet;
2745 };
2746
2747 template<typename T>
2748 class wxStringTypeBuffer : public wxStringTypeBufferBase<T>
2749 {
2750 public:
2751 wxStringTypeBuffer(wxString& str, size_t lenWanted = 1024)
2752 : wxStringTypeBufferBase<T>(str, lenWanted) {}
2753 ~wxStringTypeBuffer()
2754 {
2755 this->m_str.assign(this->m_buf.data());
2756 }
2757
2758 DECLARE_NO_COPY_CLASS(wxStringTypeBuffer)
2759 };
2760
2761 template<typename T>
2762 class wxStringTypeBufferLength : public wxStringTypeBufferLengthBase<T>
2763 {
2764 public:
2765 wxStringTypeBufferLength(wxString& str, size_t lenWanted = 1024)
2766 : wxStringTypeBufferLengthBase<T>(str, lenWanted) {}
2767
2768 ~wxStringTypeBufferLength()
2769 {
2770 wxASSERT(this->m_lenSet);
2771 this->m_str.assign(this->m_buf.data(), this->m_len);
2772 }
2773
2774 DECLARE_NO_COPY_CLASS(wxStringTypeBufferLength)
2775 };
2776
2777 #if wxUSE_STL_BASED_WXSTRING
2778 class wxImplStringBuffer : public wxStringTypeBufferBase<wxStringCharType>
2779 {
2780 public:
2781 wxImplStringBuffer(wxString& str, size_t lenWanted = 1024)
2782 : wxStringTypeBufferBase<wxStringCharType>(str, lenWanted) {}
2783 ~wxImplStringBuffer()
2784 { m_str.m_impl.assign(m_buf.data()); }
2785
2786 DECLARE_NO_COPY_CLASS(wxImplStringBuffer)
2787 };
2788
2789 class wxImplStringBufferLength : public wxStringTypeBufferLengthBase<wxStringCharType>
2790 {
2791 public:
2792 wxImplStringBufferLength(wxString& str, size_t lenWanted = 1024)
2793 : wxStringTypeBufferLengthBase<wxStringCharType>(str, lenWanted) {}
2794
2795 ~wxImplStringBufferLength()
2796 {
2797 wxASSERT(m_lenSet);
2798 m_str.m_impl.assign(m_buf.data(), m_len);
2799 }
2800
2801 DECLARE_NO_COPY_CLASS(wxImplStringBufferLength)
2802 };
2803 #endif // wxUSE_STL_BASED_WXSTRING
2804
2805
2806 #if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
2807 typedef wxStringTypeBuffer<wxChar> wxStringBuffer;
2808 typedef wxStringTypeBufferLength<wxChar> wxStringBufferLength;
2809 #else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2810 typedef wxImplStringBuffer wxStringBuffer;
2811 typedef wxImplStringBufferLength wxStringBufferLength;
2812 #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2813
2814 // ---------------------------------------------------------------------------
2815 // wxString comparison functions: operator versions are always case sensitive
2816 // ---------------------------------------------------------------------------
2817
2818 #define wxCMP_WXCHAR_STRING(p, s, op) 0 op s.Cmp(p)
2819
2820 wxDEFINE_ALL_COMPARISONS(const wxChar *, const wxString&, wxCMP_WXCHAR_STRING)
2821
2822 #undef wxCMP_WXCHAR_STRING
2823
2824 // note that there is an optimization in operator==() and !=(): we (quickly)
2825 // checks the strings length first, before comparing their data
2826 inline bool operator==(const wxString& s1, const wxString& s2)
2827 { return (s1.Len() == s2.Len()) && (s1.Cmp(s2) == 0); }
2828 inline bool operator!=(const wxString& s1, const wxString& s2)
2829 { return (s1.Len() != s2.Len()) || (s1.Cmp(s2) != 0); }
2830 inline bool operator< (const wxString& s1, const wxString& s2)
2831 { return s1.Cmp(s2) < 0; }
2832 inline bool operator> (const wxString& s1, const wxString& s2)
2833 { return s1.Cmp(s2) > 0; }
2834 inline bool operator<=(const wxString& s1, const wxString& s2)
2835 { return s1.Cmp(s2) <= 0; }
2836 inline bool operator>=(const wxString& s1, const wxString& s2)
2837 { return s1.Cmp(s2) >= 0; }
2838
2839 inline bool operator==(const wxString& s1, const wxCStrData& s2)
2840 { return s1 == s2.AsString(); }
2841 inline bool operator==(const wxCStrData& s1, const wxString& s2)
2842 { return s1.AsString() == s2; }
2843 inline bool operator!=(const wxString& s1, const wxCStrData& s2)
2844 { return s1 != s2.AsString(); }
2845 inline bool operator!=(const wxCStrData& s1, const wxString& s2)
2846 { return s1.AsString() != s2; }
2847
2848 inline bool operator==(const wxString& s1, const wxWCharBuffer& s2)
2849 { return (s1.Cmp((const wchar_t *)s2) == 0); }
2850 inline bool operator==(const wxWCharBuffer& s1, const wxString& s2)
2851 { return (s2.Cmp((const wchar_t *)s1) == 0); }
2852 inline bool operator!=(const wxString& s1, const wxWCharBuffer& s2)
2853 { return (s1.Cmp((const wchar_t *)s2) != 0); }
2854 inline bool operator!=(const wxWCharBuffer& s1, const wxString& s2)
2855 { return (s2.Cmp((const wchar_t *)s1) != 0); }
2856
2857 inline bool operator==(const wxString& s1, const wxCharBuffer& s2)
2858 { return (s1.Cmp((const char *)s2) == 0); }
2859 inline bool operator==(const wxCharBuffer& s1, const wxString& s2)
2860 { return (s2.Cmp((const char *)s1) == 0); }
2861 inline bool operator!=(const wxString& s1, const wxCharBuffer& s2)
2862 { return (s1.Cmp((const char *)s2) != 0); }
2863 inline bool operator!=(const wxCharBuffer& s1, const wxString& s2)
2864 { return (s2.Cmp((const char *)s1) != 0); }
2865
2866 inline wxString operator+(const wxString& string, const wxWCharBuffer& buf)
2867 { return string + (const wchar_t *)buf; }
2868 inline wxString operator+(const wxWCharBuffer& buf, const wxString& string)
2869 { return (const wchar_t *)buf + string; }
2870
2871 inline wxString operator+(const wxString& string, const wxCharBuffer& buf)
2872 { return string + (const char *)buf; }
2873 inline wxString operator+(const wxCharBuffer& buf, const wxString& string)
2874 { return (const char *)buf + string; }
2875
2876 // comparison with char
2877 inline bool operator==(const wxUniChar& c, const wxString& s) { return s.IsSameAs(c); }
2878 inline bool operator==(const wxUniCharRef& c, const wxString& s) { return s.IsSameAs(c); }
2879 inline bool operator==(char c, const wxString& s) { return s.IsSameAs(c); }
2880 inline bool operator==(wchar_t c, const wxString& s) { return s.IsSameAs(c); }
2881 inline bool operator==(int c, const wxString& s) { return s.IsSameAs(c); }
2882 inline bool operator==(const wxString& s, const wxUniChar& c) { return s.IsSameAs(c); }
2883 inline bool operator==(const wxString& s, const wxUniCharRef& c) { return s.IsSameAs(c); }
2884 inline bool operator==(const wxString& s, char c) { return s.IsSameAs(c); }
2885 inline bool operator==(const wxString& s, wchar_t c) { return s.IsSameAs(c); }
2886 inline bool operator!=(const wxUniChar& c, const wxString& s) { return !s.IsSameAs(c); }
2887 inline bool operator!=(const wxUniCharRef& c, const wxString& s) { return !s.IsSameAs(c); }
2888 inline bool operator!=(char c, const wxString& s) { return !s.IsSameAs(c); }
2889 inline bool operator!=(wchar_t c, const wxString& s) { return !s.IsSameAs(c); }
2890 inline bool operator!=(int c, const wxString& s) { return !s.IsSameAs(c); }
2891 inline bool operator!=(const wxString& s, const wxUniChar& c) { return !s.IsSameAs(c); }
2892 inline bool operator!=(const wxString& s, const wxUniCharRef& c) { return !s.IsSameAs(c); }
2893 inline bool operator!=(const wxString& s, char c) { return !s.IsSameAs(c); }
2894 inline bool operator!=(const wxString& s, wchar_t c) { return !s.IsSameAs(c); }
2895
2896 // comparison with C string in Unicode build
2897 #if wxUSE_UNICODE
2898
2899 #define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s
2900
2901 wxDEFINE_ALL_COMPARISONS(const char *, const wxString&, wxCMP_CHAR_STRING)
2902
2903 #undef wxCMP_CHAR_STRING
2904
2905 #endif // wxUSE_UNICODE
2906
2907 // we also need to provide the operators for comparison with wxCStrData to
2908 // resolve ambiguity between operator(const wxChar *,const wxString &) and
2909 // operator(const wxChar *, const wxChar *) for "p == s.c_str()"
2910 //
2911 // notice that these are (shallow) pointer comparisons, not (deep) string ones
2912 #define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar()
2913 #define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar()
2914
2915 wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData&, wxCMP_WCHAR_CSTRDATA)
2916 wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData&, wxCMP_CHAR_CSTRDATA)
2917
2918 #undef wxCMP_CHAR_CSTRDATA
2919 #undef wxCMP_WCHAR_CSTRDATA
2920
2921 // ---------------------------------------------------------------------------
2922 // Implementation only from here until the end of file
2923 // ---------------------------------------------------------------------------
2924
2925 #if wxUSE_STD_IOSTREAM
2926
2927 #include "wx/iosfwrap.h"
2928
2929 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxString&);
2930 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCStrData&);
2931 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCharBuffer&);
2932 #ifndef __BORLANDC__
2933 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxWCharBuffer&);
2934 #endif
2935
2936 #endif // wxSTD_STRING_COMPATIBILITY
2937
2938 // ---------------------------------------------------------------------------
2939 // wxCStrData implementation
2940 // ---------------------------------------------------------------------------
2941
2942 inline wxCStrData::wxCStrData(char *buf)
2943 : m_str(new wxString(buf)), m_offset(0), m_owned(true) {}
2944 inline wxCStrData::wxCStrData(wchar_t *buf)
2945 : m_str(new wxString(buf)), m_offset(0), m_owned(true) {}
2946
2947 inline wxCStrData::wxCStrData(const wxCStrData& data)
2948 : m_str(data.m_owned ? new wxString(*data.m_str) : data.m_str),
2949 m_offset(data.m_offset),
2950 m_owned(data.m_owned)
2951 {
2952 }
2953
2954 inline wxCStrData::~wxCStrData()
2955 {
2956 if ( m_owned )
2957 delete m_str;
2958 }
2959
2960 // simple cases for AsChar() and AsWChar(), the complicated ones are
2961 // in string.cpp
2962 #if wxUSE_UNICODE_WCHAR
2963 inline const wchar_t* wxCStrData::AsWChar() const
2964 {
2965 return m_str->wx_str() + m_offset;
2966 }
2967 #endif // wxUSE_UNICODE_WCHAR
2968
2969 #if !wxUSE_UNICODE
2970 inline const char* wxCStrData::AsChar() const
2971 {
2972 return m_str->wx_str() + m_offset;
2973 }
2974 #endif // !wxUSE_UNICODE
2975
2976 #if wxUSE_UTF8_LOCALE_ONLY
2977 inline const char* wxCStrData::AsChar() const
2978 {
2979 return wxStringOperations::AddToIter(m_str->wx_str(), m_offset);
2980 }
2981 #endif // wxUSE_UTF8_LOCALE_ONLY
2982
2983 inline const wxCharBuffer wxCStrData::AsCharBuf() const
2984 {
2985 #if !wxUSE_UNICODE
2986 return wxCharBuffer::CreateNonOwned(AsChar());
2987 #else
2988 return AsString().mb_str();
2989 #endif
2990 }
2991
2992 inline const wxWCharBuffer wxCStrData::AsWCharBuf() const
2993 {
2994 #if wxUSE_UNICODE_WCHAR
2995 return wxWCharBuffer::CreateNonOwned(AsWChar());
2996 #else
2997 return AsString().wc_str();
2998 #endif
2999 }
3000
3001 inline wxString wxCStrData::AsString() const
3002 {
3003 if ( m_offset == 0 )
3004 return *m_str;
3005 else
3006 return m_str->Mid(m_offset);
3007 }
3008
3009 inline const wxStringCharType *wxCStrData::AsInternal() const
3010 {
3011 #if wxUSE_UNICODE_UTF8
3012 return wxStringOperations::AddToIter(m_str->wx_str(), m_offset);
3013 #else
3014 return m_str->wx_str() + m_offset;
3015 #endif
3016 }
3017
3018 inline wxUniChar wxCStrData::operator*() const
3019 {
3020 if ( m_str->empty() )
3021 return wxUniChar(_T('\0'));
3022 else
3023 return (*m_str)[m_offset];
3024 }
3025
3026 inline wxUniChar wxCStrData::operator[](size_t n) const
3027 {
3028 // NB: we intentionally use operator[] and not at() here because the former
3029 // works for the terminating NUL while the latter does not
3030 return (*m_str)[m_offset + n];
3031 }
3032
3033 // ----------------------------------------------------------------------------
3034 // more wxCStrData operators
3035 // ----------------------------------------------------------------------------
3036
3037 // we need to define those to allow "size_t pos = p - s.c_str()" where p is
3038 // some pointer into the string
3039 inline size_t operator-(const char *p, const wxCStrData& cs)
3040 {
3041 return p - cs.AsChar();
3042 }
3043
3044 inline size_t operator-(const wchar_t *p, const wxCStrData& cs)
3045 {
3046 return p - cs.AsWChar();
3047 }
3048
3049 // ----------------------------------------------------------------------------
3050 // implementation of wx[W]CharBuffer inline methods using wxCStrData
3051 // ----------------------------------------------------------------------------
3052
3053 // FIXME-UTF8: move this to buffer.h
3054 inline wxCharBuffer::wxCharBuffer(const wxCStrData& cstr)
3055 : wxCharTypeBufferBase(cstr.AsCharBuf())
3056 {
3057 }
3058
3059 inline wxWCharBuffer::wxWCharBuffer(const wxCStrData& cstr)
3060 : wxCharTypeBufferBase(cstr.AsWCharBuf())
3061 {
3062 }
3063
3064 #if wxUSE_UNICODE_UTF8
3065 // ----------------------------------------------------------------------------
3066 // implementation of wxStringIteratorNode inline methods
3067 // ----------------------------------------------------------------------------
3068
3069 wxStringIteratorNode::wxStringIteratorNode(const wxString *str,
3070 wxStringImpl::const_iterator *citer)
3071 : m_str(str),
3072 m_citer(citer),
3073 m_iter(NULL),
3074 m_prev(NULL),
3075 m_next(str->m_iterators.ptr)
3076 {
3077 wx_const_cast(wxString*, m_str)->m_iterators.ptr = this;
3078 if ( m_next )
3079 m_next->m_prev = this;
3080 }
3081
3082 wxStringIteratorNode::wxStringIteratorNode(const wxString *str,
3083 wxStringImpl::iterator *iter)
3084 : m_str(str),
3085 m_citer(NULL),
3086 m_iter(iter),
3087 m_prev(NULL),
3088 m_next(str->m_iterators.ptr)
3089 {
3090 wx_const_cast(wxString*, m_str)->m_iterators.ptr = this;
3091 if ( m_next)
3092 m_next->m_prev = this;
3093 }
3094
3095 wxStringIteratorNode::~wxStringIteratorNode()
3096 {
3097 if ( m_next )
3098 m_next->m_prev = m_prev;
3099 if ( m_prev )
3100 m_prev->m_next = m_next;
3101 else // first in the list
3102 wx_const_cast(wxString*, m_str)->m_iterators.ptr = m_next;
3103 }
3104 #endif // wxUSE_UNICODE_UTF8
3105
3106 #if WXWIN_COMPATIBILITY_2_8
3107 // lot of code out there doesn't explicitly include wx/crt.h, but uses
3108 // CRT wrappers that are now declared in wx/wxcrt.h and wx/wxcrtvararg.h,
3109 // so let's include this header now that wxString is defined and it's safe
3110 // to do it:
3111 #include "wx/crt.h"
3112 #endif
3113
3114 #endif // _WX_WXSTRING_H_