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