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