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