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