added WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE() macro and use it to work around VC6 warnin...
[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 const size_t len = length();
1091 if ( nSize == len)
1092 return;
1093
1094 #if wxUSE_UNICODE_UTF8
1095 if ( nSize < len )
1096 {
1097 // we can't use wxStringImpl::resize() for truncating the string as it
1098 // counts in bytes, not characters
1099 erase(nSize);
1100 return;
1101 }
1102
1103 // we also can't use (presumably more efficient) resize() if we have to
1104 // append characters taking more than one byte
1105 if ( !ch.IsAscii() )
1106 append(nSize - len, ch);
1107 else
1108 #endif // wxUSE_UNICODE_UTF8
1109 m_impl.resize(nSize, (wxStringCharType)ch);
1110 }
1111
1112 wxString substr(size_t nStart = 0, size_t nLen = npos) const
1113 {
1114 size_t pos, len;
1115 PosLenToImpl(nStart, nLen, &pos, &len);
1116 return FromImpl(m_impl.substr(pos, len));
1117 }
1118
1119 // generic attributes & operations
1120 // as standard strlen()
1121 size_t Len() const { return length(); }
1122 // string contains any characters?
1123 bool IsEmpty() const { return empty(); }
1124 // empty string is "false", so !str will return true
1125 bool operator!() const { return empty(); }
1126 // truncate the string to given length
1127 wxString& Truncate(size_t uiLen);
1128 // empty string contents
1129 void Empty()
1130 {
1131 Truncate(0);
1132
1133 wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") );
1134 }
1135 // empty the string and free memory
1136 void Clear()
1137 {
1138 wxString tmp(wxEmptyString);
1139 swap(tmp);
1140 }
1141
1142 // contents test
1143 // Is an ascii value
1144 bool IsAscii() const;
1145 // Is a number
1146 bool IsNumber() const;
1147 // Is a word
1148 bool IsWord() const;
1149
1150 // data access (all indexes are 0 based)
1151 // read access
1152 wxUniChar at(size_t n) const
1153 { return *(begin() + n); } // FIXME-UTF8: optimize?
1154 wxUniChar GetChar(size_t n) const
1155 { return at(n); }
1156 // read/write access
1157 wxUniCharRef at(size_t n)
1158 { return *(begin() + n); } // FIXME-UTF8: optimize?
1159 wxUniCharRef GetWritableChar(size_t n)
1160 { return at(n); }
1161 // write access
1162 void SetChar(size_t n, wxUniChar ch)
1163 { at(n) = ch; }
1164
1165 // get last character
1166 wxUniChar Last() const
1167 {
1168 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1169 return *rbegin();
1170 }
1171
1172 // get writable last character
1173 wxUniCharRef Last()
1174 {
1175 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1176 return *rbegin();
1177 }
1178
1179 /*
1180 Note that we we must define all of the overloads below to avoid
1181 ambiguity when using str[0].
1182 */
1183 wxUniChar operator[](int n) const
1184 { return at(n); }
1185 wxUniChar operator[](long n) const
1186 { return at(n); }
1187 wxUniChar operator[](size_t n) const
1188 { return at(n); }
1189 #ifndef wxSIZE_T_IS_UINT
1190 wxUniChar operator[](unsigned int n) const
1191 { return at(n); }
1192 #endif // size_t != unsigned int
1193
1194 // operator versions of GetWriteableChar()
1195 wxUniCharRef operator[](int n)
1196 { return at(n); }
1197 wxUniCharRef operator[](long n)
1198 { return at(n); }
1199 wxUniCharRef operator[](size_t n)
1200 { return at(n); }
1201 #ifndef wxSIZE_T_IS_UINT
1202 wxUniCharRef operator[](unsigned int n)
1203 { return at(n); }
1204 #endif // size_t != unsigned int
1205
1206 // explicit conversion to C string (use this with printf()!)
1207 wxCStrData c_str() const { return wxCStrData(this); }
1208 wxCStrData data() const { return c_str(); }
1209
1210 // implicit conversion to C string
1211 operator wxCStrData() const { return c_str(); }
1212
1213 // the first two operators conflict with operators for conversion to
1214 // std::string and they must be disabled in STL build; the next one only
1215 // makes sense if conversions to char* are also defined and not defining it
1216 // in STL build also helps us to get more clear error messages for the code
1217 // which relies on implicit conversion to char* in STL build
1218 #if !wxUSE_STL
1219 operator const char*() const { return c_str(); }
1220 operator const wchar_t*() const { return c_str(); }
1221
1222 // implicit conversion to untyped pointer for compatibility with previous
1223 // wxWidgets versions: this is the same as conversion to const char * so it
1224 // may fail!
1225 operator const void*() const { return c_str(); }
1226 #endif // wxUSE_STL
1227
1228 // identical to c_str(), for MFC compatibility
1229 const wxCStrData GetData() const { return c_str(); }
1230
1231 // explicit conversion to C string in internal representation (char*,
1232 // wchar_t*, UTF-8-encoded char*, depending on the build):
1233 const wxStringCharType *wx_str() const { return m_impl.c_str(); }
1234
1235 // conversion to *non-const* multibyte or widestring buffer; modifying
1236 // returned buffer won't affect the string, these methods are only useful
1237 // for passing values to const-incorrect functions
1238 wxWritableCharBuffer char_str(const wxMBConv& conv = wxConvLibc) const
1239 { return mb_str(conv); }
1240 wxWritableWCharBuffer wchar_str() const { return wc_str(); }
1241
1242 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
1243 // converting numbers or strings which are certain not to contain special
1244 // chars (typically system functions, X atoms, environment variables etc.)
1245 //
1246 // the behaviour of these functions with the strings containing anything
1247 // else than 7 bit ASCII characters is undefined, use at your own risk.
1248 #if wxUSE_UNICODE
1249 static wxString FromAscii(const char *ascii, size_t len);
1250 static wxString FromAscii(const char *ascii);
1251 static wxString FromAscii(char ascii);
1252 const wxCharBuffer ToAscii() const;
1253 #else // ANSI
1254 static wxString FromAscii(const char *ascii) { return wxString( ascii ); }
1255 static wxString FromAscii(const char *ascii, size_t len)
1256 { return wxString( ascii, len ); }
1257 static wxString FromAscii(char ascii) { return wxString( ascii ); }
1258 const char *ToAscii() const { return c_str(); }
1259 #endif // Unicode/!Unicode
1260
1261 // also provide unsigned char overloads as signed/unsigned doesn't matter
1262 // for 7 bit ASCII characters
1263 static wxString FromAscii(const unsigned char *ascii)
1264 { return FromAscii((const char *)ascii); }
1265 static wxString FromAscii(const unsigned char *ascii, size_t len)
1266 { return FromAscii((const char *)ascii, len); }
1267
1268 // conversion to/from UTF-8:
1269 #if wxUSE_UNICODE_UTF8
1270 static wxString FromUTF8(const char *utf8)
1271 {
1272 if ( !utf8 )
1273 return wxEmptyString;
1274
1275 wxASSERT( wxStringOperations::IsValidUtf8String(utf8) );
1276 return FromImpl(wxStringImpl(utf8));
1277 }
1278 static wxString FromUTF8(const char *utf8, size_t len)
1279 {
1280 if ( !utf8 )
1281 return wxEmptyString;
1282 if ( len == npos )
1283 return FromUTF8(utf8);
1284
1285 wxASSERT( wxStringOperations::IsValidUtf8String(utf8, len) );
1286 return FromImpl(wxStringImpl(utf8, len));
1287 }
1288 const char* utf8_str() const { return wx_str(); }
1289 const char* ToUTF8() const { return wx_str(); }
1290 #elif wxUSE_UNICODE_WCHAR
1291 static wxString FromUTF8(const char *utf8)
1292 { return wxString(utf8, wxMBConvUTF8()); }
1293 static wxString FromUTF8(const char *utf8, size_t len)
1294 { return wxString(utf8, wxMBConvUTF8(), len); }
1295 const wxCharBuffer utf8_str() const { return mb_str(wxMBConvUTF8()); }
1296 const wxCharBuffer ToUTF8() const { return utf8_str(); }
1297 #else // ANSI
1298 static wxString FromUTF8(const char *utf8)
1299 { return wxString(wxMBConvUTF8().cMB2WC(utf8)); }
1300 static wxString FromUTF8(const char *utf8, size_t len)
1301 {
1302 size_t wlen;
1303 wxWCharBuffer buf(wxMBConvUTF8().cMB2WC(utf8, len == npos ? wxNO_LEN : len, &wlen));
1304 return wxString(buf.data(), wlen);
1305 }
1306 const wxCharBuffer utf8_str() const
1307 { return wxMBConvUTF8().cWC2MB(wc_str()); }
1308 const wxCharBuffer ToUTF8() const { return utf8_str(); }
1309 #endif
1310
1311 // functions for storing binary data in wxString:
1312 #if wxUSE_UNICODE
1313 static wxString From8BitData(const char *data, size_t len)
1314 { return wxString(data, wxConvISO8859_1, len); }
1315 // version for NUL-terminated data:
1316 static wxString From8BitData(const char *data)
1317 { return wxString(data, wxConvISO8859_1); }
1318 const wxCharBuffer To8BitData() const { return mb_str(wxConvISO8859_1); }
1319 #else // ANSI
1320 static wxString From8BitData(const char *data, size_t len)
1321 { return wxString(data, len); }
1322 // version for NUL-terminated data:
1323 static wxString From8BitData(const char *data)
1324 { return wxString(data); }
1325 const char *To8BitData() const { return c_str(); }
1326 #endif // Unicode/ANSI
1327
1328 // conversions with (possible) format conversions: have to return a
1329 // buffer with temporary data
1330 //
1331 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
1332 // return an ANSI (multibyte) string, wc_str() to return a wide string and
1333 // fn_str() to return a string which should be used with the OS APIs
1334 // accepting the file names. The return value is always the same, but the
1335 // type differs because a function may either return pointer to the buffer
1336 // directly or have to use intermediate buffer for translation.
1337 #if wxUSE_UNICODE
1338
1339 #if wxUSE_UTF8_LOCALE_ONLY
1340 const char* mb_str() const { return wx_str(); }
1341 const wxCharBuffer mb_str(const wxMBConv& conv) const;
1342 #else
1343 const wxCharBuffer mb_str(const wxMBConv& conv = wxConvLibc) const;
1344 #endif
1345
1346 const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); }
1347
1348 #if wxUSE_UNICODE_WCHAR
1349 const wxChar* wc_str() const { return wx_str(); }
1350 #elif wxUSE_UNICODE_UTF8
1351 const wxWCharBuffer wc_str() const;
1352 #endif
1353 // for compatibility with !wxUSE_UNICODE version
1354 const wxWX2WCbuf wc_str(const wxMBConv& WXUNUSED(conv)) const
1355 { return wc_str(); }
1356
1357 #if wxMBFILES
1358 const wxCharBuffer fn_str() const { return mb_str(wxConvFile); }
1359 #else // !wxMBFILES
1360 const wxWX2WCbuf fn_str() const { return wc_str(); }
1361 #endif // wxMBFILES/!wxMBFILES
1362
1363 #else // ANSI
1364 const wxChar* mb_str() const { return wx_str(); }
1365
1366 // for compatibility with wxUSE_UNICODE version
1367 const wxChar* mb_str(const wxMBConv& WXUNUSED(conv)) const { return wx_str(); }
1368
1369 const wxWX2MBbuf mbc_str() const { return mb_str(); }
1370
1371 #if wxUSE_WCHAR_T
1372 const wxWCharBuffer wc_str(const wxMBConv& conv = wxConvLibc) const;
1373 #endif // wxUSE_WCHAR_T
1374 #ifdef __WXOSX__
1375 const wxCharBuffer fn_str() const { return wxConvFile.cWC2WX( wc_str( wxConvLocal ) ); }
1376 #else
1377 const wxChar* fn_str() const { return c_str(); }
1378 #endif
1379 #endif // Unicode/ANSI
1380
1381 // overloaded assignment
1382 // from another wxString
1383 wxString& operator=(const wxString& stringSrc)
1384 { m_impl = stringSrc.m_impl; return *this; }
1385 wxString& operator=(const wxCStrData& cstr)
1386 { return *this = cstr.AsString(); }
1387 // from a character
1388 wxString& operator=(wxUniChar ch)
1389 { m_impl = wxStringOperations::EncodeChar(ch); return *this; }
1390 wxString& operator=(wxUniCharRef ch)
1391 { return operator=((wxUniChar)ch); }
1392 wxString& operator=(char ch)
1393 { return operator=(wxUniChar(ch)); }
1394 wxString& operator=(unsigned char ch)
1395 { return operator=(wxUniChar(ch)); }
1396 wxString& operator=(wchar_t ch)
1397 { return operator=(wxUniChar(ch)); }
1398 // from a C string - STL probably will crash on NULL,
1399 // so we need to compensate in that case
1400 #if wxUSE_STL_BASED_WXSTRING
1401 wxString& operator=(const char *psz)
1402 { if (psz) m_impl = ImplStr(psz); else Clear(); return *this; }
1403 wxString& operator=(const wchar_t *pwz)
1404 { if (pwz) m_impl = ImplStr(pwz); else Clear(); return *this; }
1405 #else
1406 wxString& operator=(const char *psz)
1407 { m_impl = ImplStr(psz); return *this; }
1408 wxString& operator=(const wchar_t *pwz)
1409 { m_impl = ImplStr(pwz); return *this; }
1410 #endif
1411 wxString& operator=(const unsigned char *psz)
1412 { return operator=((const char*)psz); }
1413
1414 // from wxWCharBuffer
1415 wxString& operator=(const wxWCharBuffer& s)
1416 { return operator=(s.data()); } // FIXME-UTF8: fix for embedded NULs
1417 // from wxCharBuffer
1418 wxString& operator=(const wxCharBuffer& s)
1419 { return operator=(s.data()); } // FIXME-UTF8: fix for embedded NULs
1420
1421 // string concatenation
1422 // in place concatenation
1423 /*
1424 Concatenate and return the result. Note that the left to right
1425 associativity of << allows to write things like "str << str1 << str2
1426 << ..." (unlike with +=)
1427 */
1428 // string += string
1429 wxString& operator<<(const wxString& s)
1430 {
1431 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1432 wxASSERT_MSG( s.IsValid(),
1433 _T("did you forget to call UngetWriteBuf()?") );
1434 #endif
1435
1436 append(s);
1437 return *this;
1438 }
1439 // string += C string
1440 wxString& operator<<(const char *psz)
1441 { append(psz); return *this; }
1442 wxString& operator<<(const wchar_t *pwz)
1443 { append(pwz); return *this; }
1444 wxString& operator<<(const wxCStrData& psz)
1445 { append(psz.AsString()); return *this; }
1446 // string += char
1447 wxString& operator<<(wxUniChar ch) { append(1, ch); return *this; }
1448 wxString& operator<<(wxUniCharRef ch) { append(1, ch); return *this; }
1449 wxString& operator<<(char ch) { append(1, ch); return *this; }
1450 wxString& operator<<(unsigned char ch) { append(1, ch); return *this; }
1451 wxString& operator<<(wchar_t ch) { append(1, ch); return *this; }
1452
1453 // string += buffer (i.e. from wxGetString)
1454 wxString& operator<<(const wxWCharBuffer& s)
1455 { return operator<<((const wchar_t *)s); }
1456 wxString& operator<<(const wxCharBuffer& s)
1457 { return operator<<((const char *)s); }
1458
1459 // string += C string
1460 wxString& Append(const wxString& s)
1461 {
1462 // test for empty() to share the string if possible
1463 if ( empty() )
1464 *this = s;
1465 else
1466 append(s);
1467 return *this;
1468 }
1469 wxString& Append(const char* psz)
1470 { append(psz); return *this; }
1471 wxString& Append(const wchar_t* pwz)
1472 { append(pwz); return *this; }
1473 wxString& Append(const wxCStrData& psz)
1474 { append(psz); return *this; }
1475 wxString& Append(const wxCharBuffer& psz)
1476 { append(psz); return *this; }
1477 wxString& Append(const wxWCharBuffer& psz)
1478 { append(psz); return *this; }
1479 // append count copies of given character
1480 wxString& Append(wxUniChar ch, size_t count = 1u)
1481 { append(count, ch); return *this; }
1482 wxString& Append(wxUniCharRef ch, size_t count = 1u)
1483 { append(count, ch); return *this; }
1484 wxString& Append(char ch, size_t count = 1u)
1485 { append(count, ch); return *this; }
1486 wxString& Append(unsigned char ch, size_t count = 1u)
1487 { append(count, ch); return *this; }
1488 wxString& Append(wchar_t ch, size_t count = 1u)
1489 { append(count, ch); return *this; }
1490 wxString& Append(const char* psz, size_t nLen)
1491 { append(psz, nLen); return *this; }
1492 wxString& Append(const wchar_t* pwz, size_t nLen)
1493 { append(pwz, nLen); return *this; }
1494
1495 // prepend a string, return the string itself
1496 wxString& Prepend(const wxString& str)
1497 { *this = str + *this; return *this; }
1498
1499 // non-destructive concatenation
1500 // two strings
1501 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string1,
1502 const wxString& string2);
1503 // string with a single char
1504 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch);
1505 // char with a string
1506 friend wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string);
1507 // string with C string
1508 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string,
1509 const char *psz);
1510 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string,
1511 const wchar_t *pwz);
1512 // C string with string
1513 friend wxString WXDLLIMPEXP_BASE operator+(const char *psz,
1514 const wxString& string);
1515 friend wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz,
1516 const wxString& string);
1517
1518 // stream-like functions
1519 // insert an int into string
1520 wxString& operator<<(int i)
1521 { return (*this) << Format(_T("%d"), i); }
1522 // insert an unsigned int into string
1523 wxString& operator<<(unsigned int ui)
1524 { return (*this) << Format(_T("%u"), ui); }
1525 // insert a long into string
1526 wxString& operator<<(long l)
1527 { return (*this) << Format(_T("%ld"), l); }
1528 // insert an unsigned long into string
1529 wxString& operator<<(unsigned long ul)
1530 { return (*this) << Format(_T("%lu"), ul); }
1531 #if defined wxLongLong_t && !defined wxLongLongIsLong
1532 // insert a long long if they exist and aren't longs
1533 wxString& operator<<(wxLongLong_t ll)
1534 {
1535 const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("d");
1536 return (*this) << Format(fmt, ll);
1537 }
1538 // insert an unsigned long long
1539 wxString& operator<<(wxULongLong_t ull)
1540 {
1541 const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("u");
1542 return (*this) << Format(fmt , ull);
1543 }
1544 #endif
1545 // insert a float into string
1546 wxString& operator<<(float f)
1547 { return (*this) << Format(_T("%f"), f); }
1548 // insert a double into string
1549 wxString& operator<<(double d)
1550 { return (*this) << Format(_T("%g"), d); }
1551
1552 // string comparison
1553 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
1554 int Cmp(const char *psz) const
1555 { return compare(psz); }
1556 int Cmp(const wchar_t *pwz) const
1557 { return compare(pwz); }
1558 int Cmp(const wxString& s) const
1559 { return compare(s); }
1560 int Cmp(const wxCStrData& s) const
1561 { return compare(s); }
1562 int Cmp(const wxCharBuffer& s) const
1563 { return compare(s); }
1564 int Cmp(const wxWCharBuffer& s) const
1565 { return compare(s); }
1566 // same as Cmp() but not case-sensitive
1567 int CmpNoCase(const wxString& s) const;
1568 // test for the string equality, either considering case or not
1569 // (if compareWithCase then the case matters)
1570 bool IsSameAs(const wxString& str, bool compareWithCase = true) const
1571 { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; }
1572 bool IsSameAs(const char *str, bool compareWithCase = true) const
1573 { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; }
1574 bool IsSameAs(const wchar_t *str, bool compareWithCase = true) const
1575 { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; }
1576 bool IsSameAs(const wxCStrData& str, bool compareWithCase = true) const
1577 { return IsSameAs(str.AsString(), compareWithCase); }
1578 bool IsSameAs(const wxCharBuffer& str, bool compareWithCase = true) const
1579 { return IsSameAs(str.data(), compareWithCase); }
1580 bool IsSameAs(const wxWCharBuffer& str, bool compareWithCase = true) const
1581 { return IsSameAs(str.data(), compareWithCase); }
1582 // comparison with a single character: returns true if equal
1583 bool IsSameAs(wxUniChar c, bool compareWithCase = true) const;
1584 // FIXME-UTF8: remove these overloads
1585 bool IsSameAs(wxUniCharRef c, bool compareWithCase = true) const
1586 { return IsSameAs(wxUniChar(c), compareWithCase); }
1587 bool IsSameAs(char c, bool compareWithCase = true) const
1588 { return IsSameAs(wxUniChar(c), compareWithCase); }
1589 bool IsSameAs(unsigned char c, bool compareWithCase = true) const
1590 { return IsSameAs(wxUniChar(c), compareWithCase); }
1591 bool IsSameAs(wchar_t c, bool compareWithCase = true) const
1592 { return IsSameAs(wxUniChar(c), compareWithCase); }
1593 bool IsSameAs(int c, bool compareWithCase = true) const
1594 { return IsSameAs(wxUniChar(c), compareWithCase); }
1595
1596 // simple sub-string extraction
1597 // return substring starting at nFirst of length nCount (or till the end
1598 // if nCount = default value)
1599 wxString Mid(size_t nFirst, size_t nCount = npos) const;
1600
1601 // operator version of Mid()
1602 wxString operator()(size_t start, size_t len) const
1603 { return Mid(start, len); }
1604
1605 // check if the string starts with the given prefix and return the rest
1606 // of the string in the provided pointer if it is not NULL; otherwise
1607 // return false
1608 bool StartsWith(const wxString& prefix, wxString *rest = NULL) const;
1609 // check if the string ends with the given suffix and return the
1610 // beginning of the string before the suffix in the provided pointer if
1611 // it is not NULL; otherwise return false
1612 bool EndsWith(const wxString& suffix, wxString *rest = NULL) const;
1613
1614 // get first nCount characters
1615 wxString Left(size_t nCount) const;
1616 // get last nCount characters
1617 wxString Right(size_t nCount) const;
1618 // get all characters before the first occurance of ch
1619 // (returns the whole string if ch not found)
1620 wxString BeforeFirst(wxUniChar ch) const;
1621 // get all characters before the last occurence of ch
1622 // (returns empty string if ch not found)
1623 wxString BeforeLast(wxUniChar ch) const;
1624 // get all characters after the first occurence of ch
1625 // (returns empty string if ch not found)
1626 wxString AfterFirst(wxUniChar ch) const;
1627 // get all characters after the last occurence of ch
1628 // (returns the whole string if ch not found)
1629 wxString AfterLast(wxUniChar ch) const;
1630
1631 // for compatibility only, use more explicitly named functions above
1632 wxString Before(wxUniChar ch) const { return BeforeLast(ch); }
1633 wxString After(wxUniChar ch) const { return AfterFirst(ch); }
1634
1635 // case conversion
1636 // convert to upper case in place, return the string itself
1637 wxString& MakeUpper();
1638 // convert to upper case, return the copy of the string
1639 // Here's something to remember: BC++ doesn't like returns in inlines.
1640 wxString Upper() const ;
1641 // convert to lower case in place, return the string itself
1642 wxString& MakeLower();
1643 // convert to lower case, return the copy of the string
1644 wxString Lower() const ;
1645
1646 // trimming/padding whitespace (either side) and truncating
1647 // remove spaces from left or from right (default) side
1648 wxString& Trim(bool bFromRight = true);
1649 // add nCount copies chPad in the beginning or at the end (default)
1650 wxString& Pad(size_t nCount, wxUniChar chPad = wxT(' '), bool bFromRight = true);
1651
1652 // searching and replacing
1653 // searching (return starting index, or -1 if not found)
1654 int Find(wxUniChar ch, bool bFromEnd = false) const; // like strchr/strrchr
1655 int Find(wxUniCharRef ch, bool bFromEnd = false) const
1656 { return Find(wxUniChar(ch), bFromEnd); }
1657 int Find(char ch, bool bFromEnd = false) const
1658 { return Find(wxUniChar(ch), bFromEnd); }
1659 int Find(unsigned char ch, bool bFromEnd = false) const
1660 { return Find(wxUniChar(ch), bFromEnd); }
1661 int Find(wchar_t ch, bool bFromEnd = false) const
1662 { return Find(wxUniChar(ch), bFromEnd); }
1663 // searching (return starting index, or -1 if not found)
1664 int Find(const wxString& sub) const // like strstr
1665 {
1666 size_type idx = find(sub);
1667 return (idx == npos) ? wxNOT_FOUND : (int)idx;
1668 }
1669 int Find(const char *sub) const // like strstr
1670 {
1671 size_type idx = find(sub);
1672 return (idx == npos) ? wxNOT_FOUND : (int)idx;
1673 }
1674 int Find(const wchar_t *sub) const // like strstr
1675 {
1676 size_type idx = find(sub);
1677 return (idx == npos) ? wxNOT_FOUND : (int)idx;
1678 }
1679
1680 int Find(const wxCStrData& sub) const
1681 { return Find(sub.AsString()); }
1682 int Find(const wxCharBuffer& sub) const
1683 { return Find(sub.data()); }
1684 int Find(const wxWCharBuffer& sub) const
1685 { return Find(sub.data()); }
1686
1687 // replace first (or all of bReplaceAll) occurences of substring with
1688 // another string, returns the number of replacements made
1689 size_t Replace(const wxString& strOld,
1690 const wxString& strNew,
1691 bool bReplaceAll = true);
1692
1693 // check if the string contents matches a mask containing '*' and '?'
1694 bool Matches(const wxString& mask) const;
1695
1696 // conversion to numbers: all functions return true only if the whole
1697 // string is a number and put the value of this number into the pointer
1698 // provided, the base is the numeric base in which the conversion should be
1699 // done and must be comprised between 2 and 36 or be 0 in which case the
1700 // standard C rules apply (leading '0' => octal, "0x" => hex)
1701 // convert to a signed integer
1702 bool ToLong(long *val, int base = 10) const;
1703 // convert to an unsigned integer
1704 bool ToULong(unsigned long *val, int base = 10) const;
1705 // convert to wxLongLong
1706 #if defined(wxLongLong_t)
1707 bool ToLongLong(wxLongLong_t *val, int base = 10) const;
1708 // convert to wxULongLong
1709 bool ToULongLong(wxULongLong_t *val, int base = 10) const;
1710 #endif // wxLongLong_t
1711 // convert to a double
1712 bool ToDouble(double *val) const;
1713
1714
1715 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1716 // formatted input/output
1717 // as sprintf(), returns the number of characters written or < 0 on error
1718 // (take 'this' into account in attribute parameter count)
1719 // int Printf(const wxString& format, ...);
1720 WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const wxFormatString&),
1721 DoPrintfWchar, DoPrintfUtf8)
1722 #ifdef __WATCOMC__
1723 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1724 WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wxString&),
1725 (wxFormatString(f1)));
1726 WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wxCStrData&),
1727 (wxFormatString(f1)));
1728 WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const char*),
1729 (wxFormatString(f1)));
1730 WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wchar_t*),
1731 (wxFormatString(f1)));
1732 #endif
1733 #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN
1734 // as vprintf(), returns the number of characters written or < 0 on error
1735 int PrintfV(const wxString& format, va_list argptr);
1736
1737 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1738 // returns the string containing the result of Printf() to it
1739 // static wxString Format(const wxString& format, ...) ATTRIBUTE_PRINTF_1;
1740 WX_DEFINE_VARARG_FUNC(static wxString, Format, 1, (const wxFormatString&),
1741 DoFormatWchar, DoFormatUtf8)
1742 #ifdef __WATCOMC__
1743 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1744 WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wxString&),
1745 (wxFormatString(f1)));
1746 WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wxCStrData&),
1747 (wxFormatString(f1)));
1748 WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const char*),
1749 (wxFormatString(f1)));
1750 WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wchar_t*),
1751 (wxFormatString(f1)));
1752 #endif
1753 #endif
1754 // the same as above, but takes a va_list
1755 static wxString FormatV(const wxString& format, va_list argptr);
1756
1757 // raw access to string memory
1758 // ensure that string has space for at least nLen characters
1759 // only works if the data of this string is not shared
1760 bool Alloc(size_t nLen) { reserve(nLen); /*return capacity() >= nLen;*/ return true; }
1761 // minimize the string's memory
1762 // only works if the data of this string is not shared
1763 bool Shrink();
1764 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1765 // These are deprecated, use wxStringBuffer or wxStringBufferLength instead
1766 //
1767 // get writable buffer of at least nLen bytes. Unget() *must* be called
1768 // a.s.a.p. to put string back in a reasonable state!
1769 wxDEPRECATED( wxStringCharType *GetWriteBuf(size_t nLen) );
1770 // call this immediately after GetWriteBuf() has been used
1771 wxDEPRECATED( void UngetWriteBuf() );
1772 wxDEPRECATED( void UngetWriteBuf(size_t nLen) );
1773 #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && wxUSE_UNICODE_UTF8
1774
1775 // wxWidgets version 1 compatibility functions
1776
1777 // use Mid()
1778 wxString SubString(size_t from, size_t to) const
1779 { return Mid(from, (to - from + 1)); }
1780 // values for second parameter of CompareTo function
1781 enum caseCompare {exact, ignoreCase};
1782 // values for first parameter of Strip function
1783 enum stripType {leading = 0x1, trailing = 0x2, both = 0x3};
1784
1785 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1786 // use Printf()
1787 // (take 'this' into account in attribute parameter count)
1788 // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2;
1789 WX_DEFINE_VARARG_FUNC(int, sprintf, 1, (const wxFormatString&),
1790 DoPrintfWchar, DoPrintfUtf8)
1791 #ifdef __WATCOMC__
1792 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1793 WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wxString&),
1794 (wxFormatString(f1)));
1795 WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wxCStrData&),
1796 (wxFormatString(f1)));
1797 WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const char*),
1798 (wxFormatString(f1)));
1799 WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wchar_t*),
1800 (wxFormatString(f1)));
1801 #endif
1802 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
1803
1804 // use Cmp()
1805 inline int CompareTo(const wxChar* psz, caseCompare cmp = exact) const
1806 { return cmp == exact ? Cmp(psz) : CmpNoCase(psz); }
1807
1808 // use length()
1809 size_t Length() const { return length(); }
1810 // Count the number of characters
1811 int Freq(wxUniChar ch) const;
1812 // use MakeLower
1813 void LowerCase() { MakeLower(); }
1814 // use MakeUpper
1815 void UpperCase() { MakeUpper(); }
1816 // use Trim except that it doesn't change this string
1817 wxString Strip(stripType w = trailing) const;
1818
1819 // use Find (more general variants not yet supported)
1820 size_t Index(const wxChar* psz) const { return Find(psz); }
1821 size_t Index(wxUniChar ch) const { return Find(ch); }
1822 // use Truncate
1823 wxString& Remove(size_t pos) { return Truncate(pos); }
1824 wxString& RemoveLast(size_t n = 1) { return Truncate(length() - n); }
1825
1826 wxString& Remove(size_t nStart, size_t nLen)
1827 { return (wxString&)erase( nStart, nLen ); }
1828
1829 // use Find()
1830 int First( wxUniChar ch ) const { return Find(ch); }
1831 int First( wxUniCharRef ch ) const { return Find(ch); }
1832 int First( char ch ) const { return Find(ch); }
1833 int First( unsigned char ch ) const { return Find(ch); }
1834 int First( wchar_t ch ) const { return Find(ch); }
1835 int First( const wxString& str ) const { return Find(str); }
1836 int Last( wxUniChar ch ) const { return Find(ch, true); }
1837 bool Contains(const wxString& str) const { return Find(str) != wxNOT_FOUND; }
1838
1839 // use empty()
1840 bool IsNull() const { return empty(); }
1841
1842 // std::string compatibility functions
1843
1844 // take nLen chars starting at nPos
1845 wxString(const wxString& str, size_t nPos, size_t nLen)
1846 { assign(str, nPos, nLen); }
1847 // take all characters from first to last
1848 wxString(const_iterator first, const_iterator last)
1849 : m_impl(first.impl(), last.impl()) { }
1850 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1851 // the 2 overloads below are for compatibility with the existing code using
1852 // pointers instead of iterators
1853 wxString(const char *first, const char *last)
1854 {
1855 SubstrBufFromMB str(ImplStr(first, last - first));
1856 m_impl.assign(str.data, str.len);
1857 }
1858 wxString(const wchar_t *first, const wchar_t *last)
1859 {
1860 SubstrBufFromWC str(ImplStr(first, last - first));
1861 m_impl.assign(str.data, str.len);
1862 }
1863 // and this one is needed to compile code adding offsets to c_str() result
1864 wxString(const wxCStrData& first, const wxCStrData& last)
1865 : m_impl(CreateConstIterator(first).impl(),
1866 CreateConstIterator(last).impl())
1867 {
1868 wxASSERT_MSG( first.m_str == last.m_str,
1869 _T("pointers must be into the same string") );
1870 }
1871 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1872
1873 // lib.string.modifiers
1874 // append elements str[pos], ..., str[pos+n]
1875 wxString& append(const wxString& str, size_t pos, size_t n)
1876 {
1877 size_t from, len;
1878 str.PosLenToImpl(pos, n, &from, &len);
1879 m_impl.append(str.m_impl, from, len);
1880 return *this;
1881 }
1882 // append a string
1883 wxString& append(const wxString& str)
1884 { m_impl.append(str.m_impl); return *this; }
1885 // append first n (or all if n == npos) characters of sz
1886 wxString& append(const char *sz)
1887 { m_impl.append(ImplStr(sz)); return *this; }
1888 wxString& append(const wchar_t *sz)
1889 { m_impl.append(ImplStr(sz)); return *this; }
1890 wxString& append(const char *sz, size_t n)
1891 {
1892 SubstrBufFromMB str(ImplStr(sz, n));
1893 m_impl.append(str.data, str.len);
1894 return *this;
1895 }
1896 wxString& append(const wchar_t *sz, size_t n)
1897 {
1898 SubstrBufFromWC str(ImplStr(sz, n));
1899 m_impl.append(str.data, str.len);
1900 return *this;
1901 }
1902
1903 wxString& append(const wxCStrData& str)
1904 { return append(str.AsString()); }
1905 wxString& append(const wxCharBuffer& str)
1906 { return append(str.data()); }
1907 wxString& append(const wxWCharBuffer& str)
1908 { return append(str.data()); }
1909
1910 // append n copies of ch
1911 wxString& append(size_t n, wxUniChar ch)
1912 {
1913 #if wxUSE_UNICODE_UTF8
1914 if ( !ch.IsAscii() )
1915 m_impl.append(wxStringOperations::EncodeNChars(n, ch));
1916 else
1917 #endif
1918 m_impl.append(n, (wxStringCharType)ch);
1919 return *this;
1920 }
1921 // append from first to last
1922 wxString& append(const_iterator first, const_iterator last)
1923 { m_impl.append(first.impl(), last.impl()); return *this; }
1924 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1925 wxString& append(const char *first, const char *last)
1926 { return append(first, last - first); }
1927 wxString& append(const wchar_t *first, const wchar_t *last)
1928 { return append(first, last - first); }
1929 wxString& append(const wxCStrData& first, const wxCStrData& last)
1930 { return append(CreateConstIterator(first), CreateConstIterator(last)); }
1931 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1932
1933 // same as `this_string = str'
1934 wxString& assign(const wxString& str)
1935 { m_impl = str.m_impl; return *this; }
1936 wxString& assign(const wxString& str, size_t len)
1937 {
1938 m_impl.assign(str.m_impl, 0, str.LenToImpl(len));
1939 return *this;
1940 }
1941 // same as ` = str[pos..pos + n]
1942 wxString& assign(const wxString& str, size_t pos, size_t n)
1943 {
1944 size_t from, len;
1945 str.PosLenToImpl(pos, n, &from, &len);
1946 m_impl.assign(str.m_impl, from, len);
1947 return *this;
1948 }
1949 // same as `= first n (or all if n == npos) characters of sz'
1950 wxString& assign(const char *sz)
1951 { m_impl.assign(ImplStr(sz)); return *this; }
1952 wxString& assign(const wchar_t *sz)
1953 { m_impl.assign(ImplStr(sz)); return *this; }
1954 wxString& assign(const char *sz, size_t n)
1955 {
1956 SubstrBufFromMB str(ImplStr(sz, n));
1957 m_impl.assign(str.data, str.len);
1958 return *this;
1959 }
1960 wxString& assign(const wchar_t *sz, size_t n)
1961 {
1962 SubstrBufFromWC str(ImplStr(sz, n));
1963 m_impl.assign(str.data, str.len);
1964 return *this;
1965 }
1966
1967 wxString& assign(const wxCStrData& str)
1968 { return assign(str.AsString()); }
1969 wxString& assign(const wxCharBuffer& str)
1970 { return assign(str.data()); }
1971 wxString& assign(const wxWCharBuffer& str)
1972 { return assign(str.data()); }
1973 wxString& assign(const wxCStrData& str, size_t len)
1974 { return assign(str.AsString(), len); }
1975 wxString& assign(const wxCharBuffer& str, size_t len)
1976 { return assign(str.data(), len); }
1977 wxString& assign(const wxWCharBuffer& str, size_t len)
1978 { return assign(str.data(), len); }
1979
1980 // same as `= n copies of ch'
1981 wxString& assign(size_t n, wxUniChar ch)
1982 {
1983 #if wxUSE_UNICODE_UTF8
1984 if ( !ch.IsAscii() )
1985 m_impl.assign(wxStringOperations::EncodeNChars(n, ch));
1986 else
1987 #endif
1988 m_impl.assign(n, (wxStringCharType)ch);
1989 return *this;
1990 }
1991
1992 wxString& assign(size_t n, wxUniCharRef ch)
1993 { return assign(n, wxUniChar(ch)); }
1994 wxString& assign(size_t n, char ch)
1995 { return assign(n, wxUniChar(ch)); }
1996 wxString& assign(size_t n, unsigned char ch)
1997 { return assign(n, wxUniChar(ch)); }
1998 wxString& assign(size_t n, wchar_t ch)
1999 { return assign(n, wxUniChar(ch)); }
2000
2001 // assign from first to last
2002 wxString& assign(const_iterator first, const_iterator last)
2003 { m_impl.assign(first.impl(), last.impl()); return *this; }
2004 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2005 wxString& assign(const char *first, const char *last)
2006 { return assign(first, last - first); }
2007 wxString& assign(const wchar_t *first, const wchar_t *last)
2008 { return assign(first, last - first); }
2009 wxString& assign(const wxCStrData& first, const wxCStrData& last)
2010 { return assign(CreateConstIterator(first), CreateConstIterator(last)); }
2011 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2012
2013 // string comparison
2014 int compare(const wxString& str) const;
2015 int compare(const char* sz) const;
2016 int compare(const wchar_t* sz) const;
2017 int compare(const wxCStrData& str) const
2018 { return compare(str.AsString()); }
2019 int compare(const wxCharBuffer& str) const
2020 { return compare(str.data()); }
2021 int compare(const wxWCharBuffer& str) const
2022 { return compare(str.data()); }
2023 // comparison with a substring
2024 int compare(size_t nStart, size_t nLen, const wxString& str) const;
2025 // comparison of 2 substrings
2026 int compare(size_t nStart, size_t nLen,
2027 const wxString& str, size_t nStart2, size_t nLen2) const;
2028 // substring comparison with first nCount characters of sz
2029 int compare(size_t nStart, size_t nLen,
2030 const char* sz, size_t nCount = npos) const;
2031 int compare(size_t nStart, size_t nLen,
2032 const wchar_t* sz, size_t nCount = npos) const;
2033
2034 // insert another string
2035 wxString& insert(size_t nPos, const wxString& str)
2036 { insert(begin() + nPos, str.begin(), str.end()); return *this; }
2037 // insert n chars of str starting at nStart (in str)
2038 wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n)
2039 {
2040 size_t from, len;
2041 str.PosLenToImpl(nStart, n, &from, &len);
2042 m_impl.insert(PosToImpl(nPos), str.m_impl, from, len);
2043 return *this;
2044 }
2045 // insert first n (or all if n == npos) characters of sz
2046 wxString& insert(size_t nPos, const char *sz)
2047 { m_impl.insert(PosToImpl(nPos), ImplStr(sz)); return *this; }
2048 wxString& insert(size_t nPos, const wchar_t *sz)
2049 { m_impl.insert(PosToImpl(nPos), ImplStr(sz)); return *this; }
2050 wxString& insert(size_t nPos, const char *sz, size_t n)
2051 {
2052 SubstrBufFromMB str(ImplStr(sz, n));
2053 m_impl.insert(PosToImpl(nPos), str.data, str.len);
2054 return *this;
2055 }
2056 wxString& insert(size_t nPos, const wchar_t *sz, size_t n)
2057 {
2058 SubstrBufFromWC str(ImplStr(sz, n));
2059 m_impl.insert(PosToImpl(nPos), str.data, str.len);
2060 return *this;
2061 }
2062 // insert n copies of ch
2063 wxString& insert(size_t nPos, size_t n, wxUniChar ch)
2064 {
2065 #if wxUSE_UNICODE_UTF8
2066 if ( !ch.IsAscii() )
2067 m_impl.insert(PosToImpl(nPos), wxStringOperations::EncodeNChars(n, ch));
2068 else
2069 #endif
2070 m_impl.insert(PosToImpl(nPos), n, (wxStringCharType)ch);
2071 return *this;
2072 }
2073 iterator insert(iterator it, wxUniChar ch)
2074 {
2075 #if wxUSE_UNICODE_UTF8
2076 if ( !ch.IsAscii() )
2077 {
2078 size_t pos = IterToImplPos(it);
2079 m_impl.insert(pos, wxStringOperations::EncodeChar(ch));
2080 return iterator(this, m_impl.begin() + pos);
2081 }
2082 else
2083 #endif
2084 return iterator(this, m_impl.insert(it.impl(), (wxStringCharType)ch));
2085 }
2086 void insert(iterator it, const_iterator first, const_iterator last)
2087 { m_impl.insert(it.impl(), first.impl(), last.impl()); }
2088 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2089 void insert(iterator it, const char *first, const char *last)
2090 { insert(it - begin(), first, last - first); }
2091 void insert(iterator it, const wchar_t *first, const wchar_t *last)
2092 { insert(it - begin(), first, last - first); }
2093 void insert(iterator it, const wxCStrData& first, const wxCStrData& last)
2094 { insert(it, CreateConstIterator(first), CreateConstIterator(last)); }
2095 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2096
2097 void insert(iterator it, size_type n, wxUniChar ch)
2098 {
2099 #if wxUSE_UNICODE_UTF8
2100 if ( !ch.IsAscii() )
2101 m_impl.insert(IterToImplPos(it), wxStringOperations::EncodeNChars(n, ch));
2102 else
2103 #endif
2104 m_impl.insert(it.impl(), n, (wxStringCharType)ch);
2105 }
2106
2107 // delete characters from nStart to nStart + nLen
2108 wxString& erase(size_type pos = 0, size_type n = npos)
2109 {
2110 size_t from, len;
2111 PosLenToImpl(pos, n, &from, &len);
2112 m_impl.erase(from, len);
2113 return *this;
2114 }
2115 // delete characters from first up to last
2116 iterator erase(iterator first, iterator last)
2117 { return iterator(this, m_impl.erase(first.impl(), last.impl())); }
2118 iterator erase(iterator first)
2119 { return iterator(this, m_impl.erase(first.impl())); }
2120
2121 #ifdef wxSTRING_BASE_HASNT_CLEAR
2122 void clear() { erase(); }
2123 #else
2124 void clear() { m_impl.clear(); }
2125 #endif
2126
2127 // replaces the substring of length nLen starting at nStart
2128 wxString& replace(size_t nStart, size_t nLen, const char* sz)
2129 {
2130 size_t from, len;
2131 PosLenToImpl(nStart, nLen, &from, &len);
2132 m_impl.replace(from, len, ImplStr(sz));
2133 return *this;
2134 }
2135 wxString& replace(size_t nStart, size_t nLen, const wchar_t* sz)
2136 {
2137 size_t from, len;
2138 PosLenToImpl(nStart, nLen, &from, &len);
2139 m_impl.replace(from, len, ImplStr(sz));
2140 return *this;
2141 }
2142 // replaces the substring of length nLen starting at nStart
2143 wxString& replace(size_t nStart, size_t nLen, const wxString& str)
2144 {
2145 size_t from, len;
2146 PosLenToImpl(nStart, nLen, &from, &len);
2147 m_impl.replace(from, len, str.m_impl);
2148 return *this;
2149 }
2150 // replaces the substring with nCount copies of ch
2151 wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxUniChar ch)
2152 {
2153 size_t from, len;
2154 PosLenToImpl(nStart, nLen, &from, &len);
2155 #if wxUSE_UNICODE_UTF8
2156 if ( !ch.IsAscii() )
2157 m_impl.replace(from, len, wxStringOperations::EncodeNChars(nCount, ch));
2158 else
2159 #endif
2160 m_impl.replace(from, len, nCount, (wxStringCharType)ch);
2161 return *this;
2162 }
2163 // replaces a substring with another substring
2164 wxString& replace(size_t nStart, size_t nLen,
2165 const wxString& str, size_t nStart2, size_t nLen2)
2166 {
2167 size_t from, len;
2168 PosLenToImpl(nStart, nLen, &from, &len);
2169
2170 size_t from2, len2;
2171 str.PosLenToImpl(nStart2, nLen2, &from2, &len2);
2172
2173 m_impl.replace(from, len, str.m_impl, from2, len2);
2174 return *this;
2175 }
2176 // replaces the substring with first nCount chars of sz
2177 wxString& replace(size_t nStart, size_t nLen,
2178 const char* sz, size_t nCount)
2179 {
2180 size_t from, len;
2181 PosLenToImpl(nStart, nLen, &from, &len);
2182
2183 SubstrBufFromMB str(ImplStr(sz, nCount));
2184
2185 m_impl.replace(from, len, str.data, str.len);
2186 return *this;
2187 }
2188 wxString& replace(size_t nStart, size_t nLen,
2189 const wchar_t* sz, size_t nCount)
2190 {
2191 size_t from, len;
2192 PosLenToImpl(nStart, nLen, &from, &len);
2193
2194 SubstrBufFromWC str(ImplStr(sz, nCount));
2195
2196 m_impl.replace(from, len, str.data, str.len);
2197 return *this;
2198 }
2199 wxString& replace(size_t nStart, size_t nLen,
2200 const wxString& s, size_t nCount)
2201 {
2202 size_t from, len;
2203 PosLenToImpl(nStart, nLen, &from, &len);
2204 m_impl.replace(from, len, s.m_impl.c_str(), s.LenToImpl(nCount));
2205 return *this;
2206 }
2207
2208 wxString& replace(iterator first, iterator last, const char* s)
2209 { m_impl.replace(first.impl(), last.impl(), ImplStr(s)); return *this; }
2210 wxString& replace(iterator first, iterator last, const wchar_t* s)
2211 { m_impl.replace(first.impl(), last.impl(), ImplStr(s)); return *this; }
2212 wxString& replace(iterator first, iterator last, const char* s, size_type n)
2213 {
2214 SubstrBufFromMB 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 wchar_t* s, size_type n)
2219 {
2220 SubstrBufFromWC str(ImplStr(s, n));
2221 m_impl.replace(first.impl(), last.impl(), str.data, str.len);
2222 return *this;
2223 }
2224 wxString& replace(iterator first, iterator last, const wxString& s)
2225 { m_impl.replace(first.impl(), last.impl(), s.m_impl); return *this; }
2226 wxString& replace(iterator first, iterator last, size_type n, wxUniChar ch)
2227 {
2228 #if wxUSE_UNICODE_UTF8
2229 if ( !ch.IsAscii() )
2230 m_impl.replace(first.impl(), last.impl(),
2231 wxStringOperations::EncodeNChars(n, ch));
2232 else
2233 #endif
2234 m_impl.replace(first.impl(), last.impl(), n, (wxStringCharType)ch);
2235 return *this;
2236 }
2237 wxString& replace(iterator first, iterator last,
2238 const_iterator first1, const_iterator last1)
2239 {
2240 m_impl.replace(first.impl(), last.impl(), first1.impl(), last1.impl());
2241 return *this;
2242 }
2243 wxString& replace(iterator first, iterator last,
2244 const char *first1, const char *last1)
2245 { replace(first, last, first1, last1 - first1); return *this; }
2246 wxString& replace(iterator first, iterator last,
2247 const wchar_t *first1, const wchar_t *last1)
2248 { replace(first, last, first1, last1 - first1); return *this; }
2249
2250 // swap two strings
2251 void swap(wxString& str)
2252 { m_impl.swap(str.m_impl); }
2253
2254 // find a substring
2255 size_t find(const wxString& str, size_t nStart = 0) const
2256 { return PosFromImpl(m_impl.find(str.m_impl, PosToImpl(nStart))); }
2257
2258 // find first n characters of sz
2259 size_t find(const char* sz, size_t nStart = 0, size_t n = npos) const
2260 {
2261 SubstrBufFromMB str(ImplStr(sz, n));
2262 return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len));
2263 }
2264 size_t find(const wchar_t* sz, size_t nStart = 0, size_t n = npos) const
2265 {
2266 SubstrBufFromWC str(ImplStr(sz, n));
2267 return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len));
2268 }
2269 size_t find(const wxCharBuffer& s, size_t nStart = 0, size_t n = npos) const
2270 { return find(s.data(), nStart, n); }
2271 size_t find(const wxWCharBuffer& s, size_t nStart = 0, size_t n = npos) const
2272 { return find(s.data(), nStart, n); }
2273 size_t find(const wxCStrData& s, size_t nStart = 0, size_t n = npos) const
2274 { return find(s.AsWChar(), nStart, n); }
2275
2276 // find the first occurence of character ch after nStart
2277 size_t find(wxUniChar ch, size_t nStart = 0) const
2278 {
2279 return PosFromImpl(m_impl.find(wxStringOperations::EncodeChar(ch),
2280 PosToImpl(nStart)));
2281 }
2282 size_t find(wxUniCharRef ch, size_t nStart = 0) const
2283 { return find(wxUniChar(ch), nStart); }
2284 size_t find(char ch, size_t nStart = 0) const
2285 { return find(wxUniChar(ch), nStart); }
2286 size_t find(unsigned char ch, size_t nStart = 0) const
2287 { return find(wxUniChar(ch), nStart); }
2288 size_t find(wchar_t ch, size_t nStart = 0) const
2289 { return find(wxUniChar(ch), nStart); }
2290
2291 // rfind() family is exactly like find() but works right to left
2292
2293 // as find, but from the end
2294 size_t rfind(const wxString& str, size_t nStart = npos) const
2295 { return PosFromImpl(m_impl.rfind(str.m_impl, PosToImpl(nStart))); }
2296
2297 // as find, but from the end
2298 size_t rfind(const char* sz, size_t nStart = npos, size_t n = npos) const
2299 {
2300 SubstrBufFromMB str(ImplStr(sz, n));
2301 return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len));
2302 }
2303 size_t rfind(const wchar_t* sz, size_t nStart = npos, size_t n = npos) const
2304 {
2305 SubstrBufFromWC str(ImplStr(sz, n));
2306 return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len));
2307 }
2308 size_t rfind(const wxCharBuffer& s, size_t nStart = npos, size_t n = npos) const
2309 { return rfind(s.data(), nStart, n); }
2310 size_t rfind(const wxWCharBuffer& s, size_t nStart = npos, size_t n = npos) const
2311 { return rfind(s.data(), nStart, n); }
2312 size_t rfind(const wxCStrData& s, size_t nStart = npos, size_t n = npos) const
2313 { return rfind(s.AsWChar(), nStart, n); }
2314 // as find, but from the end
2315 size_t rfind(wxUniChar ch, size_t nStart = npos) const
2316 {
2317 return PosFromImpl(m_impl.rfind(wxStringOperations::EncodeChar(ch),
2318 PosToImpl(nStart)));
2319 }
2320 size_t rfind(wxUniCharRef ch, size_t nStart = npos) const
2321 { return rfind(wxUniChar(ch), nStart); }
2322 size_t rfind(char ch, size_t nStart = npos) const
2323 { return rfind(wxUniChar(ch), nStart); }
2324 size_t rfind(unsigned char ch, size_t nStart = npos) const
2325 { return rfind(wxUniChar(ch), nStart); }
2326 size_t rfind(wchar_t ch, size_t nStart = npos) const
2327 { return rfind(wxUniChar(ch), nStart); }
2328
2329 // find first/last occurence of any character (not) in the set:
2330 #if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2331 // FIXME-UTF8: this is not entirely correct, because it doesn't work if
2332 // sizeof(wchar_t)==2 and surrogates are present in the string;
2333 // should we care? Probably not.
2334 size_t find_first_of(const wxString& str, size_t nStart = 0) const
2335 { return m_impl.find_first_of(str.m_impl, nStart); }
2336 size_t find_first_of(const char* sz, size_t nStart = 0) const
2337 { return m_impl.find_first_of(ImplStr(sz), nStart); }
2338 size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const
2339 { return m_impl.find_first_of(ImplStr(sz), nStart); }
2340 size_t find_first_of(const char* sz, size_t nStart, size_t n) const
2341 { return m_impl.find_first_of(ImplStr(sz), nStart, n); }
2342 size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const
2343 { return m_impl.find_first_of(ImplStr(sz), nStart, n); }
2344 size_t find_first_of(wxUniChar c, size_t nStart = 0) const
2345 { return m_impl.find_first_of((wxChar)c, nStart); }
2346
2347 size_t find_last_of(const wxString& str, size_t nStart = npos) const
2348 { return m_impl.find_last_of(str.m_impl, nStart); }
2349 size_t find_last_of(const char* sz, size_t nStart = npos) const
2350 { return m_impl.find_last_of(ImplStr(sz), nStart); }
2351 size_t find_last_of(const wchar_t* sz, size_t nStart = npos) const
2352 { return m_impl.find_last_of(ImplStr(sz), nStart); }
2353 size_t find_last_of(const char* sz, size_t nStart, size_t n) const
2354 { return m_impl.find_last_of(ImplStr(sz), nStart, n); }
2355 size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const
2356 { return m_impl.find_last_of(ImplStr(sz), nStart, n); }
2357 size_t find_last_of(wxUniChar c, size_t nStart = npos) const
2358 { return m_impl.find_last_of((wxChar)c, nStart); }
2359
2360 size_t find_first_not_of(const wxString& str, size_t nStart = 0) const
2361 { return m_impl.find_first_not_of(str.m_impl, nStart); }
2362 size_t find_first_not_of(const char* sz, size_t nStart = 0) const
2363 { return m_impl.find_first_not_of(ImplStr(sz), nStart); }
2364 size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const
2365 { return m_impl.find_first_not_of(ImplStr(sz), nStart); }
2366 size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const
2367 { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); }
2368 size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const
2369 { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); }
2370 size_t find_first_not_of(wxUniChar c, size_t nStart = 0) const
2371 { return m_impl.find_first_not_of((wxChar)c, nStart); }
2372
2373 size_t find_last_not_of(const wxString& str, size_t nStart = npos) const
2374 { return m_impl.find_last_not_of(str.m_impl, nStart); }
2375 size_t find_last_not_of(const char* sz, size_t nStart = npos) const
2376 { return m_impl.find_last_not_of(ImplStr(sz), nStart); }
2377 size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const
2378 { return m_impl.find_last_not_of(ImplStr(sz), nStart); }
2379 size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const
2380 { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); }
2381 size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const
2382 { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); }
2383 size_t find_last_not_of(wxUniChar c, size_t nStart = npos) const
2384 { return m_impl.find_last_not_of((wxChar)c, nStart); }
2385 #else
2386 // we can't use std::string implementation in UTF-8 build, because the
2387 // character sets would be interpreted wrongly:
2388
2389 // as strpbrk() but starts at nStart, returns npos if not found
2390 size_t find_first_of(const wxString& str, size_t nStart = 0) const
2391 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2392 { return find_first_of(str.wc_str(), nStart); }
2393 #else
2394 { return find_first_of(str.mb_str(), nStart); }
2395 #endif
2396 // same as above
2397 size_t find_first_of(const char* sz, size_t nStart = 0) const;
2398 size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const;
2399 size_t find_first_of(const char* sz, size_t nStart, size_t n) const;
2400 size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const;
2401 // same as find(char, size_t)
2402 size_t find_first_of(wxUniChar c, size_t nStart = 0) const
2403 { return find(c, nStart); }
2404 // find the last (starting from nStart) char from str in this string
2405 size_t find_last_of (const wxString& str, size_t nStart = npos) const
2406 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2407 { return find_last_of(str.wc_str(), nStart); }
2408 #else
2409 { return find_last_of(str.mb_str(), nStart); }
2410 #endif
2411 // same as above
2412 size_t find_last_of (const char* sz, size_t nStart = npos) const;
2413 size_t find_last_of (const wchar_t* sz, size_t nStart = npos) const;
2414 size_t find_last_of(const char* sz, size_t nStart, size_t n) const;
2415 size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const;
2416 // same as above
2417 size_t find_last_of(wxUniChar c, size_t nStart = npos) const
2418 { return rfind(c, nStart); }
2419
2420 // find first/last occurence of any character not in the set
2421
2422 // as strspn() (starting from nStart), returns npos on failure
2423 size_t find_first_not_of(const wxString& str, size_t nStart = 0) const
2424 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2425 { return find_first_not_of(str.wc_str(), nStart); }
2426 #else
2427 { return find_first_not_of(str.mb_str(), nStart); }
2428 #endif
2429 // same as above
2430 size_t find_first_not_of(const char* sz, size_t nStart = 0) const;
2431 size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const;
2432 size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const;
2433 size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const;
2434 // same as above
2435 size_t find_first_not_of(wxUniChar ch, size_t nStart = 0) const;
2436 // as strcspn()
2437 size_t find_last_not_of(const wxString& str, size_t nStart = npos) const
2438 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2439 { return find_last_not_of(str.wc_str(), nStart); }
2440 #else
2441 { return find_last_not_of(str.mb_str(), nStart); }
2442 #endif
2443 // same as above
2444 size_t find_last_not_of(const char* sz, size_t nStart = npos) const;
2445 size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const;
2446 size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const;
2447 size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const;
2448 // same as above
2449 size_t find_last_not_of(wxUniChar ch, size_t nStart = npos) const;
2450 #endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not
2451
2452 // provide char/wchar_t/wxUniCharRef overloads for char-finding functions
2453 // above to resolve ambiguities:
2454 size_t find_first_of(wxUniCharRef ch, size_t nStart = 0) const
2455 { return find_first_of(wxUniChar(ch), nStart); }
2456 size_t find_first_of(char ch, size_t nStart = 0) const
2457 { return find_first_of(wxUniChar(ch), nStart); }
2458 size_t find_first_of(unsigned char ch, size_t nStart = 0) const
2459 { return find_first_of(wxUniChar(ch), nStart); }
2460 size_t find_first_of(wchar_t ch, size_t nStart = 0) const
2461 { return find_first_of(wxUniChar(ch), nStart); }
2462 size_t find_last_of(wxUniCharRef ch, size_t nStart = npos) const
2463 { return find_last_of(wxUniChar(ch), nStart); }
2464 size_t find_last_of(char ch, size_t nStart = npos) const
2465 { return find_last_of(wxUniChar(ch), nStart); }
2466 size_t find_last_of(unsigned char ch, size_t nStart = npos) const
2467 { return find_last_of(wxUniChar(ch), nStart); }
2468 size_t find_last_of(wchar_t ch, size_t nStart = npos) const
2469 { return find_last_of(wxUniChar(ch), nStart); }
2470 size_t find_first_not_of(wxUniCharRef ch, size_t nStart = 0) const
2471 { return find_first_not_of(wxUniChar(ch), nStart); }
2472 size_t find_first_not_of(char ch, size_t nStart = 0) const
2473 { return find_first_not_of(wxUniChar(ch), nStart); }
2474 size_t find_first_not_of(unsigned char ch, size_t nStart = 0) const
2475 { return find_first_not_of(wxUniChar(ch), nStart); }
2476 size_t find_first_not_of(wchar_t ch, size_t nStart = 0) const
2477 { return find_first_not_of(wxUniChar(ch), nStart); }
2478 size_t find_last_not_of(wxUniCharRef ch, size_t nStart = npos) const
2479 { return find_last_not_of(wxUniChar(ch), nStart); }
2480 size_t find_last_not_of(char ch, size_t nStart = npos) const
2481 { return find_last_not_of(wxUniChar(ch), nStart); }
2482 size_t find_last_not_of(unsigned char ch, size_t nStart = npos) const
2483 { return find_last_not_of(wxUniChar(ch), nStart); }
2484 size_t find_last_not_of(wchar_t ch, size_t nStart = npos) const
2485 { return find_last_not_of(wxUniChar(ch), nStart); }
2486
2487 // and additional overloads for the versions taking strings:
2488 size_t find_first_of(const wxCStrData& sz, size_t nStart = 0) const
2489 { return find_first_of(sz.AsString(), nStart); }
2490 size_t find_first_of(const wxCharBuffer& sz, size_t nStart = 0) const
2491 { return find_first_of(sz.data(), nStart); }
2492 size_t find_first_of(const wxWCharBuffer& sz, size_t nStart = 0) const
2493 { return find_first_of(sz.data(), nStart); }
2494 size_t find_first_of(const wxCStrData& sz, size_t nStart, size_t n) const
2495 { return find_first_of(sz.AsWChar(), nStart, n); }
2496 size_t find_first_of(const wxCharBuffer& sz, size_t nStart, size_t n) const
2497 { return find_first_of(sz.data(), nStart, n); }
2498 size_t find_first_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const
2499 { return find_first_of(sz.data(), nStart, n); }
2500
2501 size_t find_last_of(const wxCStrData& sz, size_t nStart = 0) const
2502 { return find_last_of(sz.AsString(), nStart); }
2503 size_t find_last_of(const wxCharBuffer& sz, size_t nStart = 0) const
2504 { return find_last_of(sz.data(), nStart); }
2505 size_t find_last_of(const wxWCharBuffer& sz, size_t nStart = 0) const
2506 { return find_last_of(sz.data(), nStart); }
2507 size_t find_last_of(const wxCStrData& sz, size_t nStart, size_t n) const
2508 { return find_last_of(sz.AsWChar(), nStart, n); }
2509 size_t find_last_of(const wxCharBuffer& sz, size_t nStart, size_t n) const
2510 { return find_last_of(sz.data(), nStart, n); }
2511 size_t find_last_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const
2512 { return find_last_of(sz.data(), nStart, n); }
2513
2514 size_t find_first_not_of(const wxCStrData& sz, size_t nStart = 0) const
2515 { return find_first_not_of(sz.AsString(), nStart); }
2516 size_t find_first_not_of(const wxCharBuffer& sz, size_t nStart = 0) const
2517 { return find_first_not_of(sz.data(), nStart); }
2518 size_t find_first_not_of(const wxWCharBuffer& sz, size_t nStart = 0) const
2519 { return find_first_not_of(sz.data(), nStart); }
2520 size_t find_first_not_of(const wxCStrData& sz, size_t nStart, size_t n) const
2521 { return find_first_not_of(sz.AsWChar(), nStart, n); }
2522 size_t find_first_not_of(const wxCharBuffer& sz, size_t nStart, size_t n) const
2523 { return find_first_not_of(sz.data(), nStart, n); }
2524 size_t find_first_not_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const
2525 { return find_first_not_of(sz.data(), nStart, n); }
2526
2527 size_t find_last_not_of(const wxCStrData& sz, size_t nStart = 0) const
2528 { return find_last_not_of(sz.AsString(), nStart); }
2529 size_t find_last_not_of(const wxCharBuffer& sz, size_t nStart = 0) const
2530 { return find_last_not_of(sz.data(), nStart); }
2531 size_t find_last_not_of(const wxWCharBuffer& sz, size_t nStart = 0) const
2532 { return find_last_not_of(sz.data(), nStart); }
2533 size_t find_last_not_of(const wxCStrData& sz, size_t nStart, size_t n) const
2534 { return find_last_not_of(sz.AsWChar(), nStart, n); }
2535 size_t find_last_not_of(const wxCharBuffer& sz, size_t nStart, size_t n) const
2536 { return find_last_not_of(sz.data(), nStart, n); }
2537 size_t find_last_not_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const
2538 { return find_last_not_of(sz.data(), nStart, n); }
2539
2540 // string += string
2541 wxString& operator+=(const wxString& s)
2542 { m_impl += s.m_impl; return *this; }
2543 // string += C string
2544 wxString& operator+=(const char *psz)
2545 { m_impl += ImplStr(psz); return *this; }
2546 wxString& operator+=(const wchar_t *pwz)
2547 { m_impl += ImplStr(pwz); return *this; }
2548 wxString& operator+=(const wxCStrData& s)
2549 { m_impl += s.AsString().m_impl; return *this; }
2550 wxString& operator+=(const wxCharBuffer& s)
2551 { return operator+=(s.data()); }
2552 wxString& operator+=(const wxWCharBuffer& s)
2553 { return operator+=(s.data()); }
2554 // string += char
2555 wxString& operator+=(wxUniChar ch)
2556 { m_impl += wxStringOperations::EncodeChar(ch); return *this; }
2557 wxString& operator+=(wxUniCharRef ch) { return *this += wxUniChar(ch); }
2558 wxString& operator+=(int ch) { return *this += wxUniChar(ch); }
2559 wxString& operator+=(char ch) { return *this += wxUniChar(ch); }
2560 wxString& operator+=(unsigned char ch) { return *this += wxUniChar(ch); }
2561 wxString& operator+=(wchar_t ch) { return *this += wxUniChar(ch); }
2562
2563 private:
2564 #if !wxUSE_STL_BASED_WXSTRING
2565 // helpers for wxStringBuffer and wxStringBufferLength
2566 wxStringCharType *DoGetWriteBuf(size_t nLen)
2567 { return m_impl.DoGetWriteBuf(nLen); }
2568 void DoUngetWriteBuf()
2569 { m_impl.DoUngetWriteBuf(); }
2570 void DoUngetWriteBuf(size_t nLen)
2571 { m_impl.DoUngetWriteBuf(nLen); }
2572 #endif // !wxUSE_STL_BASED_WXSTRING
2573
2574 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2575 #if !wxUSE_UTF8_LOCALE_ONLY
2576 int DoPrintfWchar(const wxChar *format, ...);
2577 static wxString DoFormatWchar(const wxChar *format, ...);
2578 #endif
2579 #if wxUSE_UNICODE_UTF8
2580 int DoPrintfUtf8(const char *format, ...);
2581 static wxString DoFormatUtf8(const char *format, ...);
2582 #endif
2583 #endif
2584
2585 #if !wxUSE_STL_BASED_WXSTRING
2586 // check string's data validity
2587 bool IsValid() const { return m_impl.GetStringData()->IsValid(); }
2588 #endif
2589
2590 private:
2591 wxStringImpl m_impl;
2592
2593 #ifdef __VISUALC__
2594 // "struct 'ConvertedBuffer<T>' needs to have dll-interface to be used by
2595 // clients of class 'wxString'" - this is private, we don't care
2596 #pragma warning (disable:4251)
2597 #endif
2598
2599 // buffers for compatibility conversion from (char*)c_str() and
2600 // (wchar_t*)c_str():
2601 // FIXME-UTF8: bechmark various approaches to keeping compatibility buffers
2602 template<typename T>
2603 struct ConvertedBuffer
2604 {
2605 ConvertedBuffer() : m_buf(NULL) {}
2606 ~ConvertedBuffer()
2607 { free(m_buf); }
2608
2609 operator T*() const { return m_buf; }
2610
2611 ConvertedBuffer& operator=(T *str)
2612 {
2613 free(m_buf);
2614 m_buf = str;
2615 return *this;
2616 }
2617
2618 T *m_buf;
2619 };
2620 #if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
2621 ConvertedBuffer<char> m_convertedToChar;
2622 #endif
2623 #if !wxUSE_UNICODE_WCHAR
2624 ConvertedBuffer<wchar_t> m_convertedToWChar;
2625 #endif
2626
2627 #ifdef __VISUALC__
2628 #pragma warning (default:4251)
2629 #endif
2630
2631 #if wxUSE_UNICODE_UTF8
2632 // FIXME-UTF8: (try to) move this elsewhere (TLS) or solve differently
2633 // assigning to character pointer to by wxString::interator may
2634 // change the underlying wxStringImpl iterator, so we have to
2635 // keep track of all iterators and update them as necessary:
2636 struct wxStringIteratorNodeHead
2637 {
2638 wxStringIteratorNodeHead() : ptr(NULL) {}
2639 wxStringIteratorNode *ptr;
2640
2641 // copying is disallowed as it would result in more than one pointer into
2642 // the same linked list
2643 DECLARE_NO_COPY_CLASS(wxStringIteratorNodeHead)
2644 };
2645
2646 wxStringIteratorNodeHead m_iterators;
2647
2648 friend class WXDLLIMPEXP_FWD_BASE wxStringIteratorNode;
2649 friend class WXDLLIMPEXP_FWD_BASE wxUniCharRef;
2650 #endif // wxUSE_UNICODE_UTF8
2651
2652 friend class WXDLLIMPEXP_FWD_BASE wxCStrData;
2653 friend class wxStringInternalBuffer;
2654 friend class wxStringInternalBufferLength;
2655 };
2656
2657 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
2658 #pragma warning (default:4275)
2659 #endif
2660
2661 // string iterator operators that satisfy STL Random Access Iterator
2662 // requirements:
2663 inline wxString::iterator operator+(int n, wxString::iterator i)
2664 { return i + n; }
2665 inline wxString::iterator operator+(size_t n, wxString::iterator i)
2666 { return i + n; }
2667 inline wxString::const_iterator operator+(int n, wxString::const_iterator i)
2668 { return i + n; }
2669 inline wxString::const_iterator operator+(size_t n, wxString::const_iterator i)
2670 { return i + n; }
2671 inline wxString::reverse_iterator operator+(int n, wxString::reverse_iterator i)
2672 { return i + n; }
2673 inline wxString::reverse_iterator operator+(size_t n, wxString::reverse_iterator i)
2674 { return i + n; }
2675 inline wxString::const_reverse_iterator operator+(int n, wxString::const_reverse_iterator i)
2676 { return i + n; }
2677 inline wxString::const_reverse_iterator operator+(size_t n, wxString::const_reverse_iterator i)
2678 { return i + n; }
2679
2680 // notice that even though for many compilers the friend declarations above are
2681 // enough, from the point of view of C++ standard we must have the declarations
2682 // here as friend ones are not injected in the enclosing namespace and without
2683 // them the code fails to compile with conforming compilers such as xlC or g++4
2684 wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2);
2685 wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const char *psz);
2686 wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wchar_t *pwz);
2687 wxString WXDLLIMPEXP_BASE operator+(const char *psz, const wxString& string);
2688 wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz, const wxString& string);
2689
2690 wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch);
2691 wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string);
2692
2693 inline wxString operator+(const wxString& string, wxUniCharRef ch)
2694 { return string + (wxUniChar)ch; }
2695 inline wxString operator+(const wxString& string, char ch)
2696 { return string + wxUniChar(ch); }
2697 inline wxString operator+(const wxString& string, wchar_t ch)
2698 { return string + wxUniChar(ch); }
2699 inline wxString operator+(wxUniCharRef ch, const wxString& string)
2700 { return (wxUniChar)ch + string; }
2701 inline wxString operator+(char ch, const wxString& string)
2702 { return wxUniChar(ch) + string; }
2703 inline wxString operator+(wchar_t ch, const wxString& string)
2704 { return wxUniChar(ch) + string; }
2705
2706
2707 #define wxGetEmptyString() wxString()
2708
2709 // ----------------------------------------------------------------------------
2710 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
2711 // ----------------------------------------------------------------------------
2712
2713 #if !wxUSE_STL_BASED_WXSTRING
2714 // string buffer for direct access to string data in their native
2715 // representation:
2716 class wxStringInternalBuffer
2717 {
2718 public:
2719 typedef wxStringCharType CharType;
2720
2721 wxStringInternalBuffer(wxString& str, size_t lenWanted = 1024)
2722 : m_str(str), m_buf(NULL)
2723 { m_buf = m_str.DoGetWriteBuf(lenWanted); }
2724
2725 ~wxStringInternalBuffer() { m_str.DoUngetWriteBuf(); }
2726
2727 operator wxStringCharType*() const { return m_buf; }
2728
2729 private:
2730 wxString& m_str;
2731 wxStringCharType *m_buf;
2732
2733 DECLARE_NO_COPY_CLASS(wxStringInternalBuffer)
2734 };
2735
2736 class wxStringInternalBufferLength
2737 {
2738 public:
2739 typedef wxStringCharType CharType;
2740
2741 wxStringInternalBufferLength(wxString& str, size_t lenWanted = 1024)
2742 : m_str(str), m_buf(NULL), m_len(0), m_lenSet(false)
2743 {
2744 m_buf = m_str.DoGetWriteBuf(lenWanted);
2745 wxASSERT(m_buf != NULL);
2746 }
2747
2748 ~wxStringInternalBufferLength()
2749 {
2750 wxASSERT(m_lenSet);
2751 m_str.DoUngetWriteBuf(m_len);
2752 }
2753
2754 operator wxStringCharType*() const { return m_buf; }
2755 void SetLength(size_t length) { m_len = length; m_lenSet = true; }
2756
2757 private:
2758 wxString& m_str;
2759 wxStringCharType *m_buf;
2760 size_t m_len;
2761 bool m_lenSet;
2762
2763 DECLARE_NO_COPY_CLASS(wxStringInternalBufferLength)
2764 };
2765
2766 #endif // !wxUSE_STL_BASED_WXSTRING
2767
2768 template<typename T>
2769 class WXDLLIMPEXP_BASE wxStringTypeBufferBase
2770 {
2771 public:
2772 typedef T CharType;
2773
2774 wxStringTypeBufferBase(wxString& str, size_t lenWanted = 1024)
2775 : m_str(str), m_buf(lenWanted)
2776 { }
2777
2778
2779 operator CharType*() { return m_buf.data(); }
2780
2781 protected:
2782 wxString& m_str;
2783 wxCharTypeBuffer<CharType> m_buf;
2784 };
2785
2786 template<typename T>
2787 class WXDLLIMPEXP_BASE wxStringTypeBufferLengthBase
2788 {
2789 public:
2790 typedef T CharType;
2791
2792 wxStringTypeBufferLengthBase(wxString& str, size_t lenWanted = 1024)
2793 : m_str(str), m_buf(lenWanted), m_len(0), m_lenSet(false)
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
2838 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase<wxStringCharType> )
2839
2840 class wxStringInternalBuffer : public wxStringTypeBufferBase<wxStringCharType>
2841 {
2842 public:
2843 wxStringInternalBuffer(wxString& str, size_t lenWanted = 1024)
2844 : wxStringTypeBufferBase<wxStringCharType>(str, lenWanted) {}
2845 ~wxStringInternalBuffer()
2846 { m_str.m_impl.assign(m_buf.data()); }
2847
2848 DECLARE_NO_COPY_CLASS(wxStringInternalBuffer)
2849 };
2850
2851 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE(
2852 wxStringTypeBufferLengthBase<wxStringCharType> )
2853
2854 class wxStringInternalBufferLength
2855 : public wxStringTypeBufferLengthBase<wxStringCharType>
2856 {
2857 public:
2858 wxStringInternalBufferLength(wxString& str, size_t lenWanted = 1024)
2859 : wxStringTypeBufferLengthBase<wxStringCharType>(str, lenWanted) {}
2860
2861 ~wxStringInternalBufferLength()
2862 {
2863 wxASSERT(m_lenSet);
2864 m_str.m_impl.assign(m_buf.data(), m_len);
2865 }
2866
2867 DECLARE_NO_COPY_CLASS(wxStringInternalBufferLength)
2868 };
2869 #endif // wxUSE_STL_BASED_WXSTRING
2870
2871
2872 #if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
2873 typedef wxStringTypeBuffer<wxChar> wxStringBuffer;
2874 typedef wxStringTypeBufferLength<wxChar> wxStringBufferLength;
2875 #else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2876 typedef wxStringInternalBuffer wxStringBuffer;
2877 typedef wxStringInternalBufferLength wxStringBufferLength;
2878 #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2879
2880 #if wxUSE_UNICODE_UTF8
2881 typedef wxStringInternalBuffer wxUTF8StringBuffer;
2882 typedef wxStringInternalBufferLength wxUTF8StringBufferLength;
2883 #elif wxUSE_UNICODE_WCHAR
2884
2885 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase<char> )
2886
2887 class WXDLLIMPEXP_BASE wxUTF8StringBuffer : public wxStringTypeBufferBase<char>
2888 {
2889 public:
2890 wxUTF8StringBuffer(wxString& str, size_t lenWanted = 1024)
2891 : wxStringTypeBufferBase<char>(str, lenWanted) {}
2892 ~wxUTF8StringBuffer();
2893
2894 DECLARE_NO_COPY_CLASS(wxUTF8StringBuffer)
2895 };
2896
2897 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferLengthBase<char> )
2898
2899 class WXDLLIMPEXP_BASE wxUTF8StringBufferLength
2900 : public wxStringTypeBufferLengthBase<char>
2901 {
2902 public:
2903 wxUTF8StringBufferLength(wxString& str, size_t lenWanted = 1024)
2904 : wxStringTypeBufferLengthBase<char>(str, lenWanted) {}
2905 ~wxUTF8StringBufferLength();
2906
2907 DECLARE_NO_COPY_CLASS(wxUTF8StringBufferLength)
2908 };
2909 #endif // wxUSE_UNICODE_UTF8/wxUSE_UNICODE_WCHAR
2910
2911
2912 // ---------------------------------------------------------------------------
2913 // wxString comparison functions: operator versions are always case sensitive
2914 // ---------------------------------------------------------------------------
2915
2916 #define wxCMP_WXCHAR_STRING(p, s, op) 0 op s.Cmp(p)
2917
2918 wxDEFINE_ALL_COMPARISONS(const wxChar *, const wxString&, wxCMP_WXCHAR_STRING)
2919
2920 #undef wxCMP_WXCHAR_STRING
2921
2922 // note that there is an optimization in operator==() and !=(): we (quickly)
2923 // checks the strings length first, before comparing their data
2924 inline bool operator==(const wxString& s1, const wxString& s2)
2925 { return (s1.Len() == s2.Len()) && (s1.Cmp(s2) == 0); }
2926 inline bool operator!=(const wxString& s1, const wxString& s2)
2927 { return (s1.Len() != s2.Len()) || (s1.Cmp(s2) != 0); }
2928 inline bool operator< (const wxString& s1, const wxString& s2)
2929 { return s1.Cmp(s2) < 0; }
2930 inline bool operator> (const wxString& s1, const wxString& s2)
2931 { return s1.Cmp(s2) > 0; }
2932 inline bool operator<=(const wxString& s1, const wxString& s2)
2933 { return s1.Cmp(s2) <= 0; }
2934 inline bool operator>=(const wxString& s1, const wxString& s2)
2935 { return s1.Cmp(s2) >= 0; }
2936
2937 inline bool operator==(const wxString& s1, const wxCStrData& s2)
2938 { return s1 == s2.AsString(); }
2939 inline bool operator==(const wxCStrData& s1, const wxString& s2)
2940 { return s1.AsString() == s2; }
2941 inline bool operator!=(const wxString& s1, const wxCStrData& s2)
2942 { return s1 != s2.AsString(); }
2943 inline bool operator!=(const wxCStrData& s1, const wxString& s2)
2944 { return s1.AsString() != s2; }
2945
2946 inline bool operator==(const wxString& s1, const wxWCharBuffer& s2)
2947 { return (s1.Cmp((const wchar_t *)s2) == 0); }
2948 inline bool operator==(const wxWCharBuffer& s1, const wxString& s2)
2949 { return (s2.Cmp((const wchar_t *)s1) == 0); }
2950 inline bool operator!=(const wxString& s1, const wxWCharBuffer& s2)
2951 { return (s1.Cmp((const wchar_t *)s2) != 0); }
2952 inline bool operator!=(const wxWCharBuffer& s1, const wxString& s2)
2953 { return (s2.Cmp((const wchar_t *)s1) != 0); }
2954
2955 inline bool operator==(const wxString& s1, const wxCharBuffer& s2)
2956 { return (s1.Cmp((const char *)s2) == 0); }
2957 inline bool operator==(const wxCharBuffer& s1, const wxString& s2)
2958 { return (s2.Cmp((const char *)s1) == 0); }
2959 inline bool operator!=(const wxString& s1, const wxCharBuffer& s2)
2960 { return (s1.Cmp((const char *)s2) != 0); }
2961 inline bool operator!=(const wxCharBuffer& s1, const wxString& s2)
2962 { return (s2.Cmp((const char *)s1) != 0); }
2963
2964 inline wxString operator+(const wxString& string, const wxWCharBuffer& buf)
2965 { return string + (const wchar_t *)buf; }
2966 inline wxString operator+(const wxWCharBuffer& buf, const wxString& string)
2967 { return (const wchar_t *)buf + string; }
2968
2969 inline wxString operator+(const wxString& string, const wxCharBuffer& buf)
2970 { return string + (const char *)buf; }
2971 inline wxString operator+(const wxCharBuffer& buf, const wxString& string)
2972 { return (const char *)buf + string; }
2973
2974 // comparison with char
2975 inline bool operator==(const wxUniChar& c, const wxString& s) { return s.IsSameAs(c); }
2976 inline bool operator==(const wxUniCharRef& c, const wxString& s) { return s.IsSameAs(c); }
2977 inline bool operator==(char c, const wxString& s) { return s.IsSameAs(c); }
2978 inline bool operator==(wchar_t c, const wxString& s) { return s.IsSameAs(c); }
2979 inline bool operator==(int c, const wxString& s) { return s.IsSameAs(c); }
2980 inline bool operator==(const wxString& s, const wxUniChar& c) { return s.IsSameAs(c); }
2981 inline bool operator==(const wxString& s, const wxUniCharRef& c) { return s.IsSameAs(c); }
2982 inline bool operator==(const wxString& s, char c) { return s.IsSameAs(c); }
2983 inline bool operator==(const wxString& s, wchar_t c) { return s.IsSameAs(c); }
2984 inline bool operator!=(const wxUniChar& c, const wxString& s) { return !s.IsSameAs(c); }
2985 inline bool operator!=(const wxUniCharRef& c, const wxString& s) { return !s.IsSameAs(c); }
2986 inline bool operator!=(char c, const wxString& s) { return !s.IsSameAs(c); }
2987 inline bool operator!=(wchar_t c, const wxString& s) { return !s.IsSameAs(c); }
2988 inline bool operator!=(int c, const wxString& s) { return !s.IsSameAs(c); }
2989 inline bool operator!=(const wxString& s, const wxUniChar& c) { return !s.IsSameAs(c); }
2990 inline bool operator!=(const wxString& s, const wxUniCharRef& c) { return !s.IsSameAs(c); }
2991 inline bool operator!=(const wxString& s, char c) { return !s.IsSameAs(c); }
2992 inline bool operator!=(const wxString& s, wchar_t c) { return !s.IsSameAs(c); }
2993
2994 // comparison with C string in Unicode build
2995 #if wxUSE_UNICODE
2996
2997 #define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s
2998
2999 wxDEFINE_ALL_COMPARISONS(const char *, const wxString&, wxCMP_CHAR_STRING)
3000
3001 #undef wxCMP_CHAR_STRING
3002
3003 #endif // wxUSE_UNICODE
3004
3005 // we also need to provide the operators for comparison with wxCStrData to
3006 // resolve ambiguity between operator(const wxChar *,const wxString &) and
3007 // operator(const wxChar *, const wxChar *) for "p == s.c_str()"
3008 //
3009 // notice that these are (shallow) pointer comparisons, not (deep) string ones
3010 #define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar()
3011 #define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar()
3012
3013 wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData&, wxCMP_WCHAR_CSTRDATA)
3014 wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData&, wxCMP_CHAR_CSTRDATA)
3015
3016 #undef wxCMP_CHAR_CSTRDATA
3017 #undef wxCMP_WCHAR_CSTRDATA
3018
3019 // ---------------------------------------------------------------------------
3020 // Implementation only from here until the end of file
3021 // ---------------------------------------------------------------------------
3022
3023 #if wxUSE_STD_IOSTREAM
3024
3025 #include "wx/iosfwrap.h"
3026
3027 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxString&);
3028 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCStrData&);
3029 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCharBuffer&);
3030 #ifndef __BORLANDC__
3031 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxWCharBuffer&);
3032 #endif
3033
3034 #endif // wxSTD_STRING_COMPATIBILITY
3035
3036 // ---------------------------------------------------------------------------
3037 // wxCStrData implementation
3038 // ---------------------------------------------------------------------------
3039
3040 inline wxCStrData::wxCStrData(char *buf)
3041 : m_str(new wxString(buf)), m_offset(0), m_owned(true) {}
3042 inline wxCStrData::wxCStrData(wchar_t *buf)
3043 : m_str(new wxString(buf)), m_offset(0), m_owned(true) {}
3044
3045 inline wxCStrData::wxCStrData(const wxCStrData& data)
3046 : m_str(data.m_owned ? new wxString(*data.m_str) : data.m_str),
3047 m_offset(data.m_offset),
3048 m_owned(data.m_owned)
3049 {
3050 }
3051
3052 inline wxCStrData::~wxCStrData()
3053 {
3054 if ( m_owned )
3055 delete wx_const_cast(wxString*, m_str); // cast to silence warnings
3056 }
3057
3058 // simple cases for AsChar() and AsWChar(), the complicated ones are
3059 // in string.cpp
3060 #if wxUSE_UNICODE_WCHAR
3061 inline const wchar_t* wxCStrData::AsWChar() const
3062 {
3063 return m_str->wx_str() + m_offset;
3064 }
3065 #endif // wxUSE_UNICODE_WCHAR
3066
3067 #if !wxUSE_UNICODE
3068 inline const char* wxCStrData::AsChar() const
3069 {
3070 return m_str->wx_str() + m_offset;
3071 }
3072 #endif // !wxUSE_UNICODE
3073
3074 #if wxUSE_UTF8_LOCALE_ONLY
3075 inline const char* wxCStrData::AsChar() const
3076 {
3077 return wxStringOperations::AddToIter(m_str->wx_str(), m_offset);
3078 }
3079 #endif // wxUSE_UTF8_LOCALE_ONLY
3080
3081 inline const wxCharBuffer wxCStrData::AsCharBuf() const
3082 {
3083 #if !wxUSE_UNICODE
3084 return wxCharBuffer::CreateNonOwned(AsChar());
3085 #else
3086 return AsString().mb_str();
3087 #endif
3088 }
3089
3090 inline const wxWCharBuffer wxCStrData::AsWCharBuf() const
3091 {
3092 #if wxUSE_UNICODE_WCHAR
3093 return wxWCharBuffer::CreateNonOwned(AsWChar());
3094 #else
3095 return AsString().wc_str();
3096 #endif
3097 }
3098
3099 inline wxString wxCStrData::AsString() const
3100 {
3101 if ( m_offset == 0 )
3102 return *m_str;
3103 else
3104 return m_str->Mid(m_offset);
3105 }
3106
3107 inline const wxStringCharType *wxCStrData::AsInternal() const
3108 {
3109 #if wxUSE_UNICODE_UTF8
3110 return wxStringOperations::AddToIter(m_str->wx_str(), m_offset);
3111 #else
3112 return m_str->wx_str() + m_offset;
3113 #endif
3114 }
3115
3116 inline wxUniChar wxCStrData::operator*() const
3117 {
3118 if ( m_str->empty() )
3119 return wxUniChar(_T('\0'));
3120 else
3121 return (*m_str)[m_offset];
3122 }
3123
3124 inline wxUniChar wxCStrData::operator[](size_t n) const
3125 {
3126 // NB: we intentionally use operator[] and not at() here because the former
3127 // works for the terminating NUL while the latter does not
3128 return (*m_str)[m_offset + n];
3129 }
3130
3131 // ----------------------------------------------------------------------------
3132 // more wxCStrData operators
3133 // ----------------------------------------------------------------------------
3134
3135 // we need to define those to allow "size_t pos = p - s.c_str()" where p is
3136 // some pointer into the string
3137 inline size_t operator-(const char *p, const wxCStrData& cs)
3138 {
3139 return p - cs.AsChar();
3140 }
3141
3142 inline size_t operator-(const wchar_t *p, const wxCStrData& cs)
3143 {
3144 return p - cs.AsWChar();
3145 }
3146
3147 // ----------------------------------------------------------------------------
3148 // implementation of wx[W]CharBuffer inline methods using wxCStrData
3149 // ----------------------------------------------------------------------------
3150
3151 // FIXME-UTF8: move this to buffer.h
3152 inline wxCharBuffer::wxCharBuffer(const wxCStrData& cstr)
3153 : wxCharTypeBufferBase(cstr.AsCharBuf())
3154 {
3155 }
3156
3157 inline wxWCharBuffer::wxWCharBuffer(const wxCStrData& cstr)
3158 : wxCharTypeBufferBase(cstr.AsWCharBuf())
3159 {
3160 }
3161
3162 #if wxUSE_UNICODE_UTF8
3163 // ----------------------------------------------------------------------------
3164 // implementation of wxStringIteratorNode inline methods
3165 // ----------------------------------------------------------------------------
3166
3167 void wxStringIteratorNode::DoSet(const wxString *str,
3168 wxStringImpl::const_iterator *citer,
3169 wxStringImpl::iterator *iter)
3170 {
3171 m_prev = NULL;
3172 m_iter = iter;
3173 m_citer = citer;
3174 m_str = str;
3175 if ( str )
3176 {
3177 m_next = str->m_iterators.ptr;
3178 wx_const_cast(wxString*, m_str)->m_iterators.ptr = this;
3179 if ( m_next )
3180 m_next->m_prev = this;
3181 }
3182 else
3183 {
3184 m_next = NULL;
3185 }
3186 }
3187
3188 void wxStringIteratorNode::clear()
3189 {
3190 if ( m_next )
3191 m_next->m_prev = m_prev;
3192 if ( m_prev )
3193 m_prev->m_next = m_next;
3194 else if ( m_str ) // first in the list
3195 wx_const_cast(wxString*, m_str)->m_iterators.ptr = m_next;
3196
3197 m_next = m_prev = NULL;
3198 m_citer = NULL;
3199 m_iter = NULL;
3200 m_str = NULL;
3201 }
3202 #endif // wxUSE_UNICODE_UTF8
3203
3204 #if WXWIN_COMPATIBILITY_2_8
3205 // lot of code out there doesn't explicitly include wx/crt.h, but uses
3206 // CRT wrappers that are now declared in wx/wxcrt.h and wx/wxcrtvararg.h,
3207 // so let's include this header now that wxString is defined and it's safe
3208 // to do it:
3209 #include "wx/crt.h"
3210 #endif
3211
3212 #endif // _WX_WXSTRING_H_