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