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