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