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