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