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