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