]> git.saurik.com Git - wxWidgets.git/blob - include/wx/string.h
added wxCStrData::As[W]CharBuf() that doesn't keep converted string in memory for...
[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 { return mb_str(); }
1098 wxWritableWCharBuffer wchar_str() const { return wc_str(); }
1099
1100 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
1101 // converting numbers or strings which are certain not to contain special
1102 // chars (typically system functions, X atoms, environment variables etc.)
1103 //
1104 // the behaviour of these functions with the strings containing anything
1105 // else than 7 bit ASCII characters is undefined, use at your own risk.
1106 #if wxUSE_UNICODE
1107 static wxString FromAscii(const char *ascii); // string
1108 static wxString FromAscii(const char ascii); // char
1109 const wxCharBuffer ToAscii() const;
1110 #else // ANSI
1111 static wxString FromAscii(const char *ascii) { return wxString( ascii ); }
1112 static wxString FromAscii(const char ascii) { return wxString( ascii ); }
1113 const char *ToAscii() const { return c_str(); }
1114 #endif // Unicode/!Unicode
1115
1116 // conversions with (possible) format conversions: have to return a
1117 // buffer with temporary data
1118 //
1119 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
1120 // return an ANSI (multibyte) string, wc_str() to return a wide string and
1121 // fn_str() to return a string which should be used with the OS APIs
1122 // accepting the file names. The return value is always the same, but the
1123 // type differs because a function may either return pointer to the buffer
1124 // directly or have to use intermediate buffer for translation.
1125 #if wxUSE_UNICODE
1126 const wxCharBuffer mb_str(const wxMBConv& conv = wxConvLibc) const;
1127
1128 const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); }
1129
1130 #if wxUSE_UNICODE_WCHAR
1131 const wxChar* wc_str() const { return wx_str(); }
1132 #elif wxUSE_UNICODE_UTF8
1133 const wxWCharBuffer wc_str() const;
1134 #endif
1135 // for compatibility with !wxUSE_UNICODE version
1136 const wxWX2WCbuf wc_str(const wxMBConv& WXUNUSED(conv)) const
1137 { return wc_str(); }
1138
1139 #if wxMBFILES
1140 const wxCharBuffer fn_str() const { return mb_str(wxConvFile); }
1141 #else // !wxMBFILES
1142 const wxWX2WCbuf fn_str() const { return wc_str(); }
1143 #endif // wxMBFILES/!wxMBFILES
1144
1145 #else // ANSI
1146 const wxChar* mb_str() const { return wx_str(); }
1147
1148 // for compatibility with wxUSE_UNICODE version
1149 const wxChar* mb_str(const wxMBConv& WXUNUSED(conv)) const { return wx_str(); }
1150
1151 const wxWX2MBbuf mbc_str() const { return mb_str(); }
1152
1153 #if wxUSE_WCHAR_T
1154 const wxWCharBuffer wc_str(const wxMBConv& conv = wxConvLibc) const;
1155 #endif // wxUSE_WCHAR_T
1156 #ifdef __WXOSX__
1157 const wxCharBuffer fn_str() const { return wxConvFile.cWC2WX( wc_str( wxConvLocal ) ); }
1158 #else
1159 const wxChar* fn_str() const { return c_str(); }
1160 #endif
1161 #endif // Unicode/ANSI
1162
1163 // overloaded assignment
1164 // from another wxString
1165 wxString& operator=(const wxString& stringSrc)
1166 { m_impl = stringSrc.m_impl; return *this; }
1167 wxString& operator=(const wxCStrData& cstr)
1168 { return *this = cstr.AsString(); }
1169 // from a character
1170 wxString& operator=(wxUniChar ch)
1171 { m_impl = EncodeChar(ch); return *this; }
1172 wxString& operator=(wxUniCharRef ch)
1173 { return operator=((wxUniChar)ch); }
1174 wxString& operator=(char ch)
1175 { return operator=(wxUniChar(ch)); }
1176 wxString& operator=(unsigned char ch)
1177 { return operator=(wxUniChar(ch)); }
1178 wxString& operator=(wchar_t ch)
1179 { return operator=(wxUniChar(ch)); }
1180 // from a C string - STL probably will crash on NULL,
1181 // so we need to compensate in that case
1182 #if wxUSE_STL_BASED_WXSTRING
1183 wxString& operator=(const char *psz)
1184 { if (psz) m_impl = ImplStr(psz); else Clear(); return *this; }
1185 wxString& operator=(const wchar_t *pwz)
1186 { if (pwz) m_impl = ImplStr(pwz); else Clear(); return *this; }
1187 #else
1188 wxString& operator=(const char *psz)
1189 { m_impl = ImplStr(psz); return *this; }
1190 wxString& operator=(const wchar_t *pwz)
1191 { m_impl = ImplStr(pwz); return *this; }
1192 #endif
1193 wxString& operator=(const unsigned char *psz)
1194 { return operator=((const char*)psz); }
1195
1196 // from wxWCharBuffer
1197 wxString& operator=(const wxWCharBuffer& s)
1198 { return operator=(s.data()); } // FIXME-UTF8: fix for embedded NULs
1199 // from wxCharBuffer
1200 wxString& operator=(const wxCharBuffer& s)
1201 { return operator=(s.data()); } // FIXME-UTF8: fix for embedded NULs
1202
1203 // string concatenation
1204 // in place concatenation
1205 /*
1206 Concatenate and return the result. Note that the left to right
1207 associativity of << allows to write things like "str << str1 << str2
1208 << ..." (unlike with +=)
1209 */
1210 // string += string
1211 wxString& operator<<(const wxString& s)
1212 {
1213 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1214 wxASSERT_MSG( s.IsValid(),
1215 _T("did you forget to call UngetWriteBuf()?") );
1216 #endif
1217
1218 append(s);
1219 return *this;
1220 }
1221 // string += C string
1222 wxString& operator<<(const char *psz)
1223 { append(psz); return *this; }
1224 wxString& operator<<(const wchar_t *pwz)
1225 { append(pwz); return *this; }
1226 wxString& operator<<(const wxCStrData& psz)
1227 { append(psz.AsString()); return *this; }
1228 // string += char
1229 wxString& operator<<(wxUniChar ch) { append(1, ch); return *this; }
1230 wxString& operator<<(wxUniCharRef ch) { append(1, ch); return *this; }
1231 wxString& operator<<(char ch) { append(1, ch); return *this; }
1232 wxString& operator<<(unsigned char ch) { append(1, ch); return *this; }
1233 wxString& operator<<(wchar_t ch) { append(1, ch); return *this; }
1234
1235 // string += buffer (i.e. from wxGetString)
1236 wxString& operator<<(const wxWCharBuffer& s)
1237 { return operator<<((const wchar_t *)s); }
1238 wxString& operator+=(const wxWCharBuffer& s)
1239 { return operator<<((const wchar_t *)s); }
1240
1241 wxString& operator<<(const wxCharBuffer& s)
1242 { return operator<<((const char *)s); }
1243 wxString& operator+=(const wxCharBuffer& s)
1244 { return operator<<((const char *)s); }
1245
1246 // string += C string
1247 wxString& Append(const wxString& s)
1248 {
1249 // test for empty() to share the string if possible
1250 if ( empty() )
1251 *this = s;
1252 else
1253 append(s);
1254 return *this;
1255 }
1256 wxString& Append(const wxCStrData& psz)
1257 { append(psz); return *this; }
1258 wxString& Append(const char* psz)
1259 { append(psz); return *this; }
1260 wxString& Append(const wchar_t* pwz)
1261 { append(pwz); return *this; }
1262 // append count copies of given character
1263 wxString& Append(wxUniChar ch, size_t count = 1u)
1264 { append(count, ch); return *this; }
1265 wxString& Append(wxUniCharRef ch, size_t count = 1u)
1266 { append(count, ch); return *this; }
1267 wxString& Append(char ch, size_t count = 1u)
1268 { append(count, ch); return *this; }
1269 wxString& Append(unsigned char ch, size_t count = 1u)
1270 { append(count, ch); return *this; }
1271 wxString& Append(wchar_t ch, size_t count = 1u)
1272 { append(count, ch); return *this; }
1273 wxString& Append(const char* psz, size_t nLen)
1274 { append(psz, nLen); return *this; }
1275 wxString& Append(const wchar_t* pwz, size_t nLen)
1276 { append(pwz, nLen); return *this; }
1277
1278 // prepend a string, return the string itself
1279 wxString& Prepend(const wxString& str)
1280 { *this = str + *this; return *this; }
1281
1282 // non-destructive concatenation
1283 // two strings
1284 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string1,
1285 const wxString& string2);
1286 // string with a single char
1287 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch);
1288 // char with a string
1289 friend wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string);
1290 // string with C string
1291 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string,
1292 const char *psz);
1293 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string,
1294 const wchar_t *pwz);
1295 // C string with string
1296 friend wxString WXDLLIMPEXP_BASE operator+(const char *psz,
1297 const wxString& string);
1298 friend wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz,
1299 const wxString& string);
1300
1301 // stream-like functions
1302 // insert an int into string
1303 wxString& operator<<(int i)
1304 { return (*this) << Format(_T("%d"), i); }
1305 // insert an unsigned int into string
1306 wxString& operator<<(unsigned int ui)
1307 { return (*this) << Format(_T("%u"), ui); }
1308 // insert a long into string
1309 wxString& operator<<(long l)
1310 { return (*this) << Format(_T("%ld"), l); }
1311 // insert an unsigned long into string
1312 wxString& operator<<(unsigned long ul)
1313 { return (*this) << Format(_T("%lu"), ul); }
1314 #if defined wxLongLong_t && !defined wxLongLongIsLong
1315 // insert a long long if they exist and aren't longs
1316 wxString& operator<<(wxLongLong_t ll)
1317 {
1318 const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("d");
1319 return (*this) << Format(fmt, ll);
1320 }
1321 // insert an unsigned long long
1322 wxString& operator<<(wxULongLong_t ull)
1323 {
1324 const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("u");
1325 return (*this) << Format(fmt , ull);
1326 }
1327 #endif
1328 // insert a float into string
1329 wxString& operator<<(float f)
1330 { return (*this) << Format(_T("%f"), f); }
1331 // insert a double into string
1332 wxString& operator<<(double d)
1333 { return (*this) << Format(_T("%g"), d); }
1334
1335 // string comparison
1336 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
1337 int Cmp(const char *psz) const
1338 { return compare(psz); }
1339 int Cmp(const wchar_t *pwz) const
1340 { return compare(pwz); }
1341 int Cmp(const wxString& s) const
1342 { return compare(s); }
1343 // same as Cmp() but not case-sensitive
1344 int CmpNoCase(const wxString& s) const;
1345 int CmpNoCase(const char *psz) const
1346 { return CmpNoCase(wxString(psz)); }
1347 int CmpNoCase(const wchar_t *pwz) const
1348 { return CmpNoCase(wxString(pwz)); }
1349 // test for the string equality, either considering case or not
1350 // (if compareWithCase then the case matters)
1351 bool IsSameAs(const wxString& str, bool compareWithCase = true) const
1352 { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; }
1353 bool IsSameAs(const char *str, bool compareWithCase = true) const
1354 { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; }
1355 bool IsSameAs(const wchar_t *str, bool compareWithCase = true) const
1356 { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; }
1357 // comparison with a single character: returns true if equal
1358 bool IsSameAs(wxUniChar c, bool compareWithCase = true) const
1359 {
1360 return (length() == 1) && (compareWithCase ? GetChar(0u) == c
1361 : wxToupper(GetChar(0u)) == wxToupper(c));
1362 }
1363 // FIXME-UTF8: remove these overloads
1364 bool IsSameAs(wxUniCharRef c, bool compareWithCase = true) const
1365 { return IsSameAs(wxUniChar(c), compareWithCase); }
1366 bool IsSameAs(char c, bool compareWithCase = true) const
1367 { return IsSameAs(wxUniChar(c), compareWithCase); }
1368 bool IsSameAs(unsigned char c, bool compareWithCase = true) const
1369 { return IsSameAs(wxUniChar(c), compareWithCase); }
1370 bool IsSameAs(wchar_t c, bool compareWithCase = true) const
1371 { return IsSameAs(wxUniChar(c), compareWithCase); }
1372 bool IsSameAs(int c, bool compareWithCase = true) const
1373 { return IsSameAs(wxUniChar(c), compareWithCase); }
1374
1375 // simple sub-string extraction
1376 // return substring starting at nFirst of length nCount (or till the end
1377 // if nCount = default value)
1378 wxString Mid(size_t nFirst, size_t nCount = npos) const;
1379
1380 // operator version of Mid()
1381 wxString operator()(size_t start, size_t len) const
1382 { return Mid(start, len); }
1383
1384 // check if the string starts with the given prefix and return the rest
1385 // of the string in the provided pointer if it is not NULL; otherwise
1386 // return false
1387 bool StartsWith(const wxChar *prefix, wxString *rest = NULL) const;
1388 // check if the string ends with the given suffix and return the
1389 // beginning of the string before the suffix in the provided pointer if
1390 // it is not NULL; otherwise return false
1391 bool EndsWith(const wxChar *suffix, wxString *rest = NULL) const;
1392
1393 // get first nCount characters
1394 wxString Left(size_t nCount) const;
1395 // get last nCount characters
1396 wxString Right(size_t nCount) const;
1397 // get all characters before the first occurance of ch
1398 // (returns the whole string if ch not found)
1399 wxString BeforeFirst(wxUniChar ch) const;
1400 // get all characters before the last occurence of ch
1401 // (returns empty string if ch not found)
1402 wxString BeforeLast(wxUniChar ch) const;
1403 // get all characters after the first occurence of ch
1404 // (returns empty string if ch not found)
1405 wxString AfterFirst(wxUniChar ch) const;
1406 // get all characters after the last occurence of ch
1407 // (returns the whole string if ch not found)
1408 wxString AfterLast(wxUniChar ch) const;
1409
1410 // for compatibility only, use more explicitly named functions above
1411 wxString Before(wxUniChar ch) const { return BeforeLast(ch); }
1412 wxString After(wxUniChar ch) const { return AfterFirst(ch); }
1413
1414 // case conversion
1415 // convert to upper case in place, return the string itself
1416 wxString& MakeUpper();
1417 // convert to upper case, return the copy of the string
1418 // Here's something to remember: BC++ doesn't like returns in inlines.
1419 wxString Upper() const ;
1420 // convert to lower case in place, return the string itself
1421 wxString& MakeLower();
1422 // convert to lower case, return the copy of the string
1423 wxString Lower() const ;
1424
1425 // trimming/padding whitespace (either side) and truncating
1426 // remove spaces from left or from right (default) side
1427 wxString& Trim(bool bFromRight = true);
1428 // add nCount copies chPad in the beginning or at the end (default)
1429 wxString& Pad(size_t nCount, wxUniChar chPad = wxT(' '), bool bFromRight = true);
1430
1431 // searching and replacing
1432 // searching (return starting index, or -1 if not found)
1433 int Find(wxUniChar ch, bool bFromEnd = false) const; // like strchr/strrchr
1434 int Find(wxUniCharRef ch, bool bFromEnd = false) const
1435 { return Find(wxUniChar(ch), bFromEnd); }
1436 int Find(char ch, bool bFromEnd = false) const
1437 { return Find(wxUniChar(ch), bFromEnd); }
1438 int Find(unsigned char ch, bool bFromEnd = false) const
1439 { return Find(wxUniChar(ch), bFromEnd); }
1440 int Find(wchar_t ch, bool bFromEnd = false) const
1441 { return Find(wxUniChar(ch), bFromEnd); }
1442 // searching (return starting index, or -1 if not found)
1443 // FIXME-UTF8: keep wxString overload only
1444 int Find(const wxString& sub) const // like strstr
1445 {
1446 size_type idx = find(sub);
1447 return (idx == npos) ? wxNOT_FOUND : (int)idx;
1448 }
1449 int Find(const char *sub) const // like strstr
1450 {
1451 size_type idx = find(sub);
1452 return (idx == npos) ? wxNOT_FOUND : (int)idx;
1453 }
1454 int Find(const wchar_t *sub) const // like strstr
1455 {
1456 size_type idx = find(sub);
1457 return (idx == npos) ? wxNOT_FOUND : (int)idx;
1458 }
1459
1460 // replace first (or all of bReplaceAll) occurences of substring with
1461 // another string, returns the number of replacements made
1462 size_t Replace(const wxString& strOld,
1463 const wxString& strNew,
1464 bool bReplaceAll = true);
1465
1466 // check if the string contents matches a mask containing '*' and '?'
1467 bool Matches(const wxString& mask) const;
1468
1469 // conversion to numbers: all functions return true only if the whole
1470 // string is a number and put the value of this number into the pointer
1471 // provided, the base is the numeric base in which the conversion should be
1472 // done and must be comprised between 2 and 36 or be 0 in which case the
1473 // standard C rules apply (leading '0' => octal, "0x" => hex)
1474 // convert to a signed integer
1475 bool ToLong(long *val, int base = 10) const;
1476 // convert to an unsigned integer
1477 bool ToULong(unsigned long *val, int base = 10) const;
1478 // convert to wxLongLong
1479 #if defined(wxLongLong_t)
1480 bool ToLongLong(wxLongLong_t *val, int base = 10) const;
1481 // convert to wxULongLong
1482 bool ToULongLong(wxULongLong_t *val, int base = 10) const;
1483 #endif // wxLongLong_t
1484 // convert to a double
1485 bool ToDouble(double *val) const;
1486
1487
1488 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1489 // formatted input/output
1490 // as sprintf(), returns the number of characters written or < 0 on error
1491 // (take 'this' into account in attribute parameter count)
1492 // int Printf(const wxChar *pszFormat, ...);
1493 WX_DEFINE_VARARG_FUNC(int, Printf, DoPrintf)
1494 #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN
1495 // as vprintf(), returns the number of characters written or < 0 on error
1496 int PrintfV(const wxString& format, va_list argptr);
1497
1498 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1499 // returns the string containing the result of Printf() to it
1500 // static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1;
1501 WX_DEFINE_VARARG_FUNC(static wxString, Format, DoFormat)
1502 #endif
1503 // the same as above, but takes a va_list
1504 static wxString FormatV(const wxString& format, va_list argptr);
1505
1506 // raw access to string memory
1507 // ensure that string has space for at least nLen characters
1508 // only works if the data of this string is not shared
1509 bool Alloc(size_t nLen) { reserve(nLen); /*return capacity() >= nLen;*/ return true; }
1510 // minimize the string's memory
1511 // only works if the data of this string is not shared
1512 bool Shrink();
1513 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1514 // These are deprecated, use wxStringBuffer or wxStringBufferLength instead
1515 //
1516 // get writable buffer of at least nLen bytes. Unget() *must* be called
1517 // a.s.a.p. to put string back in a reasonable state!
1518 wxDEPRECATED( wxChar *GetWriteBuf(size_t nLen) );
1519 // call this immediately after GetWriteBuf() has been used
1520 wxDEPRECATED( void UngetWriteBuf() );
1521 wxDEPRECATED( void UngetWriteBuf(size_t nLen) );
1522 #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && wxUSE_UNICODE_UTF8
1523
1524 // wxWidgets version 1 compatibility functions
1525
1526 // use Mid()
1527 wxString SubString(size_t from, size_t to) const
1528 { return Mid(from, (to - from + 1)); }
1529 // values for second parameter of CompareTo function
1530 enum caseCompare {exact, ignoreCase};
1531 // values for first parameter of Strip function
1532 enum stripType {leading = 0x1, trailing = 0x2, both = 0x3};
1533
1534 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1535 // use Printf()
1536 // (take 'this' into account in attribute parameter count)
1537 // int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2;
1538 WX_DEFINE_VARARG_FUNC(int, sprintf, DoPrintf)
1539 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
1540
1541 // use Cmp()
1542 inline int CompareTo(const wxChar* psz, caseCompare cmp = exact) const
1543 { return cmp == exact ? Cmp(psz) : CmpNoCase(psz); }
1544
1545 // use length()
1546 size_t Length() const { return length(); }
1547 // Count the number of characters
1548 int Freq(wxUniChar ch) const;
1549 // use MakeLower
1550 void LowerCase() { MakeLower(); }
1551 // use MakeUpper
1552 void UpperCase() { MakeUpper(); }
1553 // use Trim except that it doesn't change this string
1554 wxString Strip(stripType w = trailing) const;
1555
1556 // use Find (more general variants not yet supported)
1557 size_t Index(const wxChar* psz) const { return Find(psz); }
1558 size_t Index(wxUniChar ch) const { return Find(ch); }
1559 // use Truncate
1560 wxString& Remove(size_t pos) { return Truncate(pos); }
1561 wxString& RemoveLast(size_t n = 1) { return Truncate(length() - n); }
1562
1563 wxString& Remove(size_t nStart, size_t nLen)
1564 { return (wxString&)erase( nStart, nLen ); }
1565
1566 // use Find()
1567 int First( wxUniChar ch ) const { return Find(ch); }
1568 int First( wxUniCharRef ch ) const { return Find(ch); }
1569 int First( char ch ) const { return Find(ch); }
1570 int First( unsigned char ch ) const { return Find(ch); }
1571 int First( wchar_t ch ) const { return Find(ch); }
1572 int First( const wxChar* psz ) const { return Find(psz); }
1573 int First( const wxString& str ) const { return Find(str); }
1574 int Last( wxUniChar ch ) const { return Find(ch, true); }
1575 bool Contains(const wxString& str) const { return Find(str) != wxNOT_FOUND; }
1576
1577 // use empty()
1578 bool IsNull() const { return empty(); }
1579
1580 // std::string compatibility functions
1581
1582 // take nLen chars starting at nPos
1583 wxString(const wxString& str, size_t nPos, size_t nLen)
1584 : m_impl(str.m_impl, nPos, nLen) { }
1585 // take all characters from first to last
1586 wxString(const_iterator first, const_iterator last)
1587 : m_impl(first.impl(), last.impl()) { }
1588 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1589 // the 2 overloads below are for compatibility with the existing code using
1590 // pointers instead of iterators
1591 wxString(const char *first, const char *last)
1592 {
1593 SubstrBufFromMB str(ImplStr(first, last - first));
1594 m_impl.assign(str.data, str.len);
1595 }
1596 wxString(const wchar_t *first, const wchar_t *last)
1597 {
1598 SubstrBufFromWC str(ImplStr(first, last - first));
1599 m_impl.assign(str.data, str.len);
1600 }
1601 // and this one is needed to compile code adding offsets to c_str() result
1602 wxString(const wxCStrData& first, const wxCStrData& last)
1603 : m_impl(CreateConstIterator(first).impl(),
1604 CreateConstIterator(last).impl())
1605 {
1606 wxASSERT_MSG( first.m_str == last.m_str,
1607 _T("pointers must be into the same string") );
1608 }
1609 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1610
1611 // lib.string.modifiers
1612 // append elements str[pos], ..., str[pos+n]
1613 wxString& append(const wxString& str, size_t pos, size_t n)
1614 {
1615 size_t from, len;
1616 str.PosLenToImpl(pos, n, &from, &len);
1617 m_impl.append(str.m_impl, from, len);
1618 return *this;
1619 }
1620 // append a string
1621 wxString& append(const wxString& str)
1622 { m_impl.append(str.m_impl); return *this; }
1623 wxString& append(const wxCStrData& str)
1624 { m_impl.append(str.AsString().m_impl); return *this; }
1625 // append first n (or all if n == npos) characters of sz
1626 wxString& append(const char *sz)
1627 { m_impl.append(ImplStr(sz)); return *this; }
1628 wxString& append(const wchar_t *sz)
1629 { m_impl.append(ImplStr(sz)); return *this; }
1630 wxString& append(const char *sz, size_t n)
1631 {
1632 SubstrBufFromMB str(ImplStr(sz, n));
1633 m_impl.append(str.data, str.len);
1634 return *this;
1635 }
1636 wxString& append(const wchar_t *sz, size_t n)
1637 {
1638 SubstrBufFromWC str(ImplStr(sz, n));
1639 m_impl.append(str.data, str.len);
1640 return *this;
1641 }
1642 // append n copies of ch
1643 wxString& append(size_t n, wxUniChar ch)
1644 {
1645 #if wxUSE_UNICODE_UTF8
1646 if ( !ch.IsAscii() )
1647 m_impl.append(EncodeNChars(n, ch));
1648 else
1649 #endif
1650 m_impl.append(n, (wxStringCharType)ch);
1651 return *this;
1652 }
1653 // append from first to last
1654 wxString& append(const_iterator first, const_iterator last)
1655 { m_impl.append(first.impl(), last.impl()); return *this; }
1656 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1657 wxString& append(const char *first, const char *last)
1658 { return append(first, last - first); }
1659 wxString& append(const wchar_t *first, const wchar_t *last)
1660 { return append(first, last - first); }
1661 wxString& append(const wxCStrData& first, const wxCStrData& last)
1662 { return append(CreateConstIterator(first), CreateConstIterator(last)); }
1663 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1664
1665 // same as `this_string = str'
1666 wxString& assign(const wxString& str)
1667 { m_impl = str.m_impl; return *this; }
1668 wxString& assign(const wxString& str, size_t len)
1669 {
1670 m_impl.assign(str.m_impl, 0, str.LenToImpl(len));
1671 return *this;
1672 }
1673 // same as ` = str[pos..pos + n]
1674 wxString& assign(const wxString& str, size_t pos, size_t n)
1675 {
1676 size_t from, len;
1677 str.PosLenToImpl(pos, n, &from, &len);
1678 m_impl.assign(str.m_impl, from, len);
1679 return *this;
1680 }
1681 // same as `= first n (or all if n == npos) characters of sz'
1682 wxString& assign(const char *sz)
1683 { m_impl.assign(ImplStr(sz)); return *this; }
1684 wxString& assign(const wchar_t *sz)
1685 { m_impl.assign(ImplStr(sz)); return *this; }
1686 wxString& assign(const char *sz, size_t n)
1687 {
1688 SubstrBufFromMB str(ImplStr(sz, n));
1689 m_impl.assign(str.data, str.len);
1690 return *this;
1691 }
1692 wxString& assign(const wchar_t *sz, size_t n)
1693 {
1694 SubstrBufFromWC str(ImplStr(sz, n));
1695 m_impl.assign(str.data, str.len);
1696 return *this;
1697 }
1698 // same as `= n copies of ch'
1699 wxString& assign(size_t n, wxUniChar ch)
1700 {
1701 #if wxUSE_UNICODE_UTF8
1702 if ( !ch.IsAscii() )
1703 m_impl.assign(EncodeNChars(n, ch));
1704 else
1705 #endif
1706 m_impl.assign(n, (wxStringCharType)ch);
1707 return *this;
1708 }
1709
1710 wxString& assign(size_t n, wxUniCharRef ch)
1711 { return assign(n, wxUniChar(ch)); }
1712 wxString& assign(size_t n, char ch)
1713 { return assign(n, wxUniChar(ch)); }
1714 wxString& assign(size_t n, unsigned char ch)
1715 { return assign(n, wxUniChar(ch)); }
1716 wxString& assign(size_t n, wchar_t ch)
1717 { return assign(n, wxUniChar(ch)); }
1718
1719 // assign from first to last
1720 wxString& assign(const_iterator first, const_iterator last)
1721 { m_impl.assign(first.impl(), last.impl()); return *this; }
1722 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1723 wxString& assign(const char *first, const char *last)
1724 { return assign(first, last - first); }
1725 wxString& assign(const wchar_t *first, const wchar_t *last)
1726 { return assign(first, last - first); }
1727 wxString& assign(const wxCStrData& first, const wxCStrData& last)
1728 { return assign(CreateConstIterator(first), CreateConstIterator(last)); }
1729 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1730
1731 // string comparison
1732 int compare(const wxString& str) const;
1733 // comparison with a substring
1734 int compare(size_t nStart, size_t nLen, const wxString& str) const;
1735 // comparison of 2 substrings
1736 int compare(size_t nStart, size_t nLen,
1737 const wxString& str, size_t nStart2, size_t nLen2) const;
1738 // just like strcmp()
1739 int compare(const char* sz) const;
1740 int compare(const wchar_t* sz) const;
1741 // substring comparison with first nCount characters of sz
1742 int compare(size_t nStart, size_t nLen,
1743 const char* sz, size_t nCount = npos) const;
1744 int compare(size_t nStart, size_t nLen,
1745 const wchar_t* sz, size_t nCount = npos) const;
1746
1747 // insert another string
1748 wxString& insert(size_t nPos, const wxString& str)
1749 { insert(begin() + nPos, str.begin(), str.end()); return *this; }
1750 // insert n chars of str starting at nStart (in str)
1751 wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n)
1752 {
1753 size_t from, len;
1754 str.PosLenToImpl(nStart, n, &from, &len);
1755 m_impl.insert(PosToImpl(nPos), str.m_impl, from, len);
1756 return *this;
1757 }
1758 // insert first n (or all if n == npos) characters of sz
1759 wxString& insert(size_t nPos, const char *sz)
1760 { m_impl.insert(PosToImpl(nPos), ImplStr(sz)); return *this; }
1761 wxString& insert(size_t nPos, const wchar_t *sz)
1762 { m_impl.insert(PosToImpl(nPos), ImplStr(sz)); return *this; }
1763 wxString& insert(size_t nPos, const char *sz, size_t n)
1764 {
1765 SubstrBufFromMB str(ImplStr(sz, n));
1766 m_impl.insert(PosToImpl(nPos), str.data, str.len);
1767 return *this;
1768 }
1769 wxString& insert(size_t nPos, const wchar_t *sz, size_t n)
1770 {
1771 SubstrBufFromWC str(ImplStr(sz, n));
1772 m_impl.insert(PosToImpl(nPos), str.data, str.len);
1773 return *this;
1774 }
1775 // insert n copies of ch
1776 wxString& insert(size_t nPos, size_t n, wxUniChar ch)
1777 {
1778 #if wxUSE_UNICODE_UTF8
1779 if ( !ch.IsAscii() )
1780 m_impl.insert(PosToImpl(nPos), EncodeNChars(n, ch));
1781 else
1782 #endif
1783 m_impl.insert(PosToImpl(nPos), n, (wxStringCharType)ch);
1784 return *this;
1785 }
1786 iterator insert(iterator it, wxUniChar ch)
1787 {
1788 #if wxUSE_UNICODE_UTF8
1789 if ( !ch.IsAscii() )
1790 {
1791 size_t pos = IterToImplPos(it);
1792 m_impl.insert(pos, EncodeChar(ch));
1793 return iterator(this, m_impl.begin() + pos);
1794 }
1795 else
1796 #endif
1797 return iterator(this, m_impl.insert(it.impl(), (wxStringCharType)ch));
1798 }
1799 void insert(iterator it, const_iterator first, const_iterator last)
1800 { m_impl.insert(it.impl(), first.impl(), last.impl()); }
1801 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1802 void insert(iterator it, const char *first, const char *last)
1803 { insert(it - begin(), first, last - first); }
1804 void insert(iterator it, const wchar_t *first, const wchar_t *last)
1805 { insert(it - begin(), first, last - first); }
1806 void insert(iterator it, const wxCStrData& first, const wxCStrData& last)
1807 { insert(it, CreateConstIterator(first), CreateConstIterator(last)); }
1808 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1809
1810 void insert(iterator it, size_type n, wxUniChar ch)
1811 {
1812 #if wxUSE_UNICODE_UTF8
1813 if ( !ch.IsAscii() )
1814 m_impl.insert(IterToImplPos(it), EncodeNChars(n, ch));
1815 else
1816 #endif
1817 m_impl.insert(it.impl(), n, (wxStringCharType)ch);
1818 }
1819
1820 // delete characters from nStart to nStart + nLen
1821 wxString& erase(size_type pos = 0, size_type n = npos)
1822 {
1823 size_t from, len;
1824 PosLenToImpl(pos, n, &from, &len);
1825 m_impl.erase(from, len);
1826 return *this;
1827 }
1828 // delete characters from first up to last
1829 iterator erase(iterator first, iterator last)
1830 { return iterator(this, m_impl.erase(first.impl(), last.impl())); }
1831 iterator erase(iterator first)
1832 { return iterator(this, m_impl.erase(first.impl())); }
1833
1834 #ifdef wxSTRING_BASE_HASNT_CLEAR
1835 void clear() { erase(); }
1836 #else
1837 void clear() { m_impl.clear(); }
1838 #endif
1839
1840 // replaces the substring of length nLen starting at nStart
1841 wxString& replace(size_t nStart, size_t nLen, const char* sz)
1842 {
1843 size_t from, len;
1844 PosLenToImpl(nStart, nLen, &from, &len);
1845 m_impl.replace(from, len, ImplStr(sz));
1846 return *this;
1847 }
1848 wxString& replace(size_t nStart, size_t nLen, const wchar_t* sz)
1849 {
1850 size_t from, len;
1851 PosLenToImpl(nStart, nLen, &from, &len);
1852 m_impl.replace(from, len, ImplStr(sz));
1853 return *this;
1854 }
1855 // replaces the substring of length nLen starting at nStart
1856 wxString& replace(size_t nStart, size_t nLen, const wxString& str)
1857 {
1858 size_t from, len;
1859 PosLenToImpl(nStart, nLen, &from, &len);
1860 m_impl.replace(from, len, str.m_impl);
1861 return *this;
1862 }
1863 // replaces the substring with nCount copies of ch
1864 wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxUniChar ch)
1865 {
1866 size_t from, len;
1867 PosLenToImpl(nStart, nLen, &from, &len);
1868 #if wxUSE_UNICODE_UTF8
1869 if ( !ch.IsAscii() )
1870 m_impl.replace(from, len, EncodeNChars(nCount, ch));
1871 else
1872 #endif
1873 m_impl.replace(from, len, nCount, (wxStringCharType)ch);
1874 return *this;
1875 }
1876 // replaces a substring with another substring
1877 wxString& replace(size_t nStart, size_t nLen,
1878 const wxString& str, size_t nStart2, size_t nLen2)
1879 {
1880 size_t from, len;
1881 PosLenToImpl(nStart, nLen, &from, &len);
1882
1883 size_t from2, len2;
1884 str.PosLenToImpl(nStart2, nLen2, &from2, &len2);
1885
1886 m_impl.replace(from, len, str.m_impl, from2, len2);
1887 return *this;
1888 }
1889 // replaces the substring with first nCount chars of sz
1890 wxString& replace(size_t nStart, size_t nLen,
1891 const char* sz, size_t nCount)
1892 {
1893 size_t from, len;
1894 PosLenToImpl(nStart, nLen, &from, &len);
1895
1896 SubstrBufFromMB str(ImplStr(sz, nCount));
1897
1898 m_impl.replace(from, len, str.data, str.len);
1899 return *this;
1900 }
1901 wxString& replace(size_t nStart, size_t nLen,
1902 const wchar_t* sz, size_t nCount)
1903 {
1904 size_t from, len;
1905 PosLenToImpl(nStart, nLen, &from, &len);
1906
1907 SubstrBufFromWC str(ImplStr(sz, nCount));
1908
1909 m_impl.replace(from, len, str.data, str.len);
1910 return *this;
1911 }
1912 wxString& replace(size_t nStart, size_t nLen,
1913 const wxString& s, size_t nCount)
1914 {
1915 size_t from, len;
1916 PosLenToImpl(nStart, nLen, &from, &len);
1917 m_impl.replace(from, len, s.m_impl.c_str(), s.LenToImpl(nCount));
1918 return *this;
1919 }
1920
1921 wxString& replace(iterator first, iterator last, const char* s)
1922 { m_impl.replace(first.impl(), last.impl(), ImplStr(s)); return *this; }
1923 wxString& replace(iterator first, iterator last, const wchar_t* s)
1924 { m_impl.replace(first.impl(), last.impl(), ImplStr(s)); return *this; }
1925 wxString& replace(iterator first, iterator last, const char* s, size_type n)
1926 {
1927 SubstrBufFromMB str(ImplStr(s, n));
1928 m_impl.replace(first.impl(), last.impl(), str.data, str.len);
1929 return *this;
1930 }
1931 wxString& replace(iterator first, iterator last, const wchar_t* s, size_type n)
1932 {
1933 SubstrBufFromWC str(ImplStr(s, n));
1934 m_impl.replace(first.impl(), last.impl(), str.data, str.len);
1935 return *this;
1936 }
1937 wxString& replace(iterator first, iterator last, const wxString& s)
1938 { m_impl.replace(first.impl(), last.impl(), s.m_impl); return *this; }
1939 wxString& replace(iterator first, iterator last, size_type n, wxUniChar ch)
1940 {
1941 #if wxUSE_UNICODE_UTF8
1942 if ( !ch.IsAscii() )
1943 m_impl.replace(first.impl(), last.impl(), EncodeNChars(n, ch));
1944 else
1945 #endif
1946 m_impl.replace(first.impl(), last.impl(), n, (wxStringCharType)ch);
1947 return *this;
1948 }
1949 wxString& replace(iterator first, iterator last,
1950 const_iterator first1, const_iterator last1)
1951 {
1952 m_impl.replace(first.impl(), last.impl(), first1.impl(), last1.impl());
1953 return *this;
1954 }
1955 wxString& replace(iterator first, iterator last,
1956 const char *first1, const char *last1)
1957 { replace(first, last, first1, last1 - first1); return *this; }
1958 wxString& replace(iterator first, iterator last,
1959 const wchar_t *first1, const wchar_t *last1)
1960 { replace(first, last, first1, last1 - first1); return *this; }
1961
1962 // swap two strings
1963 void swap(wxString& str)
1964 { m_impl.swap(str.m_impl); }
1965
1966 // find a substring
1967 size_t find(const wxString& str, size_t nStart = 0) const
1968 { return PosFromImpl(m_impl.find(str.m_impl, PosToImpl(nStart))); }
1969
1970 // find first n characters of sz
1971 size_t find(const char* sz, size_t nStart = 0, size_t n = npos) const
1972 {
1973 SubstrBufFromMB str(ImplStr(sz, n));
1974 return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len));
1975 }
1976 size_t find(const wchar_t* sz, size_t nStart = 0, size_t n = npos) const
1977 {
1978 SubstrBufFromWC str(ImplStr(sz, n));
1979 return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len));
1980 }
1981
1982 // find the first occurence of character ch after nStart
1983 size_t find(wxUniChar ch, size_t nStart = 0) const
1984 { return PosFromImpl(m_impl.find(EncodeChar(ch), PosToImpl(nStart))); }
1985 size_t find(wxUniCharRef ch, size_t nStart = 0) const
1986 { return find(wxUniChar(ch), nStart); }
1987 size_t find(char ch, size_t nStart = 0) const
1988 { return find(wxUniChar(ch), nStart); }
1989 size_t find(unsigned char ch, size_t nStart = 0) const
1990 { return find(wxUniChar(ch), nStart); }
1991 size_t find(wchar_t ch, size_t nStart = 0) const
1992 { return find(wxUniChar(ch), nStart); }
1993
1994 // rfind() family is exactly like find() but works right to left
1995
1996 // as find, but from the end
1997 size_t rfind(const wxString& str, size_t nStart = npos) const
1998 { return PosFromImpl(m_impl.rfind(str.m_impl, PosToImpl(nStart))); }
1999
2000 // as find, but from the end
2001 size_t rfind(const char* sz, size_t nStart = npos, size_t n = npos) const
2002 {
2003 SubstrBufFromMB str(ImplStr(sz, n));
2004 return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len));
2005 }
2006 size_t rfind(const wchar_t* sz, size_t nStart = npos, size_t n = npos) const
2007 {
2008 SubstrBufFromWC str(ImplStr(sz, n));
2009 return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len));
2010 }
2011 // as find, but from the end
2012 size_t rfind(wxUniChar ch, size_t nStart = npos) const
2013 { return PosFromImpl(m_impl.rfind(EncodeChar(ch), PosToImpl(nStart))); }
2014 size_t rfind(wxUniCharRef ch, size_t nStart = npos) const
2015 { return rfind(wxUniChar(ch), nStart); }
2016 size_t rfind(char ch, size_t nStart = npos) const
2017 { return rfind(wxUniChar(ch), nStart); }
2018 size_t rfind(unsigned char ch, size_t nStart = npos) const
2019 { return rfind(wxUniChar(ch), nStart); }
2020 size_t rfind(wchar_t ch, size_t nStart = npos) const
2021 { return rfind(wxUniChar(ch), nStart); }
2022
2023 // find first/last occurence of any character (not) in the set:
2024 #if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2025 // FIXME-UTF8: this is not entirely correct, because it doesn't work if
2026 // sizeof(wchar_t)==2 and surrogates are present in the string;
2027 // should we care? Probably not.
2028 size_t find_first_of(const wxString& str, size_t nStart = 0) const
2029 { return m_impl.find_first_of(str.m_impl, nStart); }
2030 size_t find_first_of(const char* sz, size_t nStart = 0) const
2031 { return m_impl.find_first_of(ImplStr(sz), nStart); }
2032 size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const
2033 { return m_impl.find_first_of(ImplStr(sz), nStart); }
2034 size_t find_first_of(const char* sz, size_t nStart, size_t n) const
2035 { return m_impl.find_first_of(ImplStr(sz), nStart, n); }
2036 size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const
2037 { return m_impl.find_first_of(ImplStr(sz), nStart, n); }
2038 size_t find_first_of(wxUniChar c, size_t nStart = 0) const
2039 { return m_impl.find_first_of((wxChar)c, nStart); }
2040
2041 size_t find_last_of(const wxString& str, size_t nStart = npos) const
2042 { return m_impl.find_last_of(str.m_impl, nStart); }
2043 size_t find_last_of(const char* sz, size_t nStart = npos) const
2044 { return m_impl.find_last_of(ImplStr(sz), nStart); }
2045 size_t find_last_of(const wchar_t* sz, size_t nStart = npos) const
2046 { return m_impl.find_last_of(ImplStr(sz), nStart); }
2047 size_t find_last_of(const char* sz, size_t nStart, size_t n) const
2048 { return m_impl.find_last_of(ImplStr(sz), nStart, n); }
2049 size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const
2050 { return m_impl.find_last_of(ImplStr(sz), nStart, n); }
2051 size_t find_last_of(wxUniChar c, size_t nStart = npos) const
2052 { return m_impl.find_last_of((wxChar)c, nStart); }
2053
2054 size_t find_first_not_of(const wxString& str, size_t nStart = 0) const
2055 { return m_impl.find_first_not_of(str.m_impl, nStart); }
2056 size_t find_first_not_of(const char* sz, size_t nStart = 0) const
2057 { return m_impl.find_first_not_of(ImplStr(sz), nStart); }
2058 size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const
2059 { return m_impl.find_first_not_of(ImplStr(sz), nStart); }
2060 size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const
2061 { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); }
2062 size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const
2063 { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); }
2064 size_t find_first_not_of(wxUniChar c, size_t nStart = 0) const
2065 { return m_impl.find_first_not_of((wxChar)c, nStart); }
2066
2067 size_t find_last_not_of(const wxString& str, size_t nStart = npos) const
2068 { return m_impl.find_last_not_of(str.m_impl, nStart); }
2069 size_t find_last_not_of(const char* sz, size_t nStart = npos) const
2070 { return m_impl.find_last_not_of(ImplStr(sz), nStart); }
2071 size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const
2072 { return m_impl.find_last_not_of(ImplStr(sz), nStart); }
2073 size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const
2074 { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); }
2075 size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const
2076 { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); }
2077 size_t find_last_not_of(wxUniChar c, size_t nStart = npos) const
2078 { return m_impl.find_last_not_of((wxChar)c, nStart); }
2079 #else
2080 // we can't use std::string implementation in UTF-8 build, because the
2081 // character sets would be interpreted wrongly:
2082
2083 // as strpbrk() but starts at nStart, returns npos if not found
2084 size_t find_first_of(const wxString& str, size_t nStart = 0) const
2085 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2086 { return find_first_of(str.mb_str().data(), nStart); }
2087 #else
2088 { return find_first_of((const wxChar*)str.c_str(), nStart); }
2089 #endif
2090 // same as above
2091 size_t find_first_of(const char* sz, size_t nStart = 0) const;
2092 size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const;
2093 size_t find_first_of(const char* sz, size_t nStart, size_t n) const;
2094 size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const;
2095 // same as find(char, size_t)
2096 size_t find_first_of(wxUniChar c, size_t nStart = 0) const
2097 { return find(c, nStart); }
2098 // find the last (starting from nStart) char from str in this string
2099 size_t find_last_of (const wxString& str, size_t nStart = npos) const
2100 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2101 { return find_last_of(str.mb_str().data(), nStart); }
2102 #else
2103 { return find_last_of((const wxChar*)str.c_str(), nStart); }
2104 #endif
2105 // same as above
2106 size_t find_last_of (const char* sz, size_t nStart = npos) const;
2107 size_t find_last_of (const wchar_t* sz, size_t nStart = npos) const;
2108 size_t find_last_of(const char* sz, size_t nStart, size_t n) const;
2109 size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const;
2110 // same as above
2111 size_t find_last_of(wxUniChar c, size_t nStart = npos) const
2112 { return rfind(c, nStart); }
2113
2114 // find first/last occurence of any character not in the set
2115
2116 // as strspn() (starting from nStart), returns npos on failure
2117 size_t find_first_not_of(const wxString& str, size_t nStart = 0) const
2118 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2119 { return find_first_not_of(str.mb_str().data(), nStart); }
2120 #else
2121 { return find_first_not_of((const wxChar*)str.c_str(), nStart); }
2122 #endif
2123 // same as above
2124 size_t find_first_not_of(const char* sz, size_t nStart = 0) const;
2125 size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const;
2126 size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const;
2127 size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const;
2128 // same as above
2129 size_t find_first_not_of(wxUniChar ch, size_t nStart = 0) const;
2130 // as strcspn()
2131 size_t find_last_not_of(const wxString& str, size_t nStart = npos) const
2132 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2133 { return find_last_not_of(str.mb_str().data(), nStart); }
2134 #else
2135 { return find_last_not_of((const wxChar*)str.c_str(), nStart); }
2136 #endif
2137 // same as above
2138 size_t find_last_not_of(const char* sz, size_t nStart = npos) const;
2139 size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const;
2140 size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const;
2141 size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const;
2142 // same as above
2143 size_t find_last_not_of(wxUniChar ch, size_t nStart = npos) const;
2144 #endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not
2145
2146 // provide char/wchar_t/wxUniCharRef overloads for char-finding functions
2147 // above to resolve ambiguities:
2148 size_t find_first_of(wxUniCharRef ch, size_t nStart = 0) const
2149 { return find_first_of(wxUniChar(ch), nStart); }
2150 size_t find_first_of(char ch, size_t nStart = 0) const
2151 { return find_first_of(wxUniChar(ch), nStart); }
2152 size_t find_first_of(unsigned char ch, size_t nStart = 0) const
2153 { return find_first_of(wxUniChar(ch), nStart); }
2154 size_t find_first_of(wchar_t ch, size_t nStart = 0) const
2155 { return find_first_of(wxUniChar(ch), nStart); }
2156 size_t find_last_of(wxUniCharRef ch, size_t nStart = npos) const
2157 { return find_last_of(wxUniChar(ch), nStart); }
2158 size_t find_last_of(char ch, size_t nStart = npos) const
2159 { return find_last_of(wxUniChar(ch), nStart); }
2160 size_t find_last_of(unsigned char ch, size_t nStart = npos) const
2161 { return find_last_of(wxUniChar(ch), nStart); }
2162 size_t find_last_of(wchar_t ch, size_t nStart = npos) const
2163 { return find_last_of(wxUniChar(ch), nStart); }
2164 size_t find_first_not_of(wxUniCharRef ch, size_t nStart = 0) const
2165 { return find_first_not_of(wxUniChar(ch), nStart); }
2166 size_t find_first_not_of(char ch, size_t nStart = 0) const
2167 { return find_first_not_of(wxUniChar(ch), nStart); }
2168 size_t find_first_not_of(unsigned char ch, size_t nStart = 0) const
2169 { return find_first_not_of(wxUniChar(ch), nStart); }
2170 size_t find_first_not_of(wchar_t ch, size_t nStart = 0) const
2171 { return find_first_not_of(wxUniChar(ch), nStart); }
2172 size_t find_last_not_of(wxUniCharRef ch, size_t nStart = npos) const
2173 { return find_last_not_of(wxUniChar(ch), nStart); }
2174 size_t find_last_not_of(char ch, size_t nStart = npos) const
2175 { return find_last_not_of(wxUniChar(ch), nStart); }
2176 size_t find_last_not_of(unsigned char ch, size_t nStart = npos) const
2177 { return find_last_not_of(wxUniChar(ch), nStart); }
2178 size_t find_last_not_of(wchar_t ch, size_t nStart = npos) const
2179 { return find_last_not_of(wxUniChar(ch), nStart); }
2180
2181 // string += string
2182 wxString& operator+=(const wxString& s)
2183 { m_impl += s.m_impl; return *this; }
2184 // string += C string
2185 wxString& operator+=(const char *psz)
2186 { m_impl += ImplStr(psz); return *this; }
2187 wxString& operator+=(const wchar_t *pwz)
2188 { m_impl += ImplStr(pwz); return *this; }
2189 wxString& operator+=(const wxCStrData& s)
2190 { m_impl += s.AsString().m_impl; return *this; }
2191 // string += char
2192 wxString& operator+=(wxUniChar ch)
2193 { m_impl += EncodeChar(ch); return *this; }
2194 wxString& operator+=(wxUniCharRef ch) { return *this += wxUniChar(ch); }
2195 wxString& operator+=(int ch) { return *this += wxUniChar(ch); }
2196 wxString& operator+=(char ch) { return *this += wxUniChar(ch); }
2197 wxString& operator+=(unsigned char ch) { return *this += wxUniChar(ch); }
2198 wxString& operator+=(wchar_t ch) { return *this += wxUniChar(ch); }
2199
2200 private:
2201 #if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2202 // helpers for wxStringBuffer and wxStringBufferLength
2203 wxStringCharType *DoGetWriteBuf(size_t nLen)
2204 { return m_impl.DoGetWriteBuf(nLen); }
2205 void DoUngetWriteBuf()
2206 { m_impl.DoUngetWriteBuf(); }
2207 void DoUngetWriteBuf(size_t nLen)
2208 { m_impl.DoUngetWriteBuf(nLen); }
2209
2210 friend class WXDLLIMPEXP_BASE wxStringBuffer;
2211 friend class WXDLLIMPEXP_BASE wxStringBufferLength;
2212 #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2213
2214 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2215 int DoPrintf(const wxChar *format, ...) ATTRIBUTE_PRINTF_2;
2216 static wxString DoFormat(const wxChar *format, ...) ATTRIBUTE_PRINTF_1;
2217 #endif
2218
2219 #if !wxUSE_STL_BASED_WXSTRING
2220 // check string's data validity
2221 bool IsValid() const { return m_impl.GetStringData()->IsValid(); }
2222 #endif
2223
2224 private:
2225 wxStringImpl m_impl;
2226
2227 // buffers for compatibility conversion from (char*)c_str() and
2228 // (wchar_t*)c_str():
2229 // FIXME-UTF8: bechmark various approaches to keeping compatibility buffers
2230 template<typename T>
2231 struct ConvertedBuffer
2232 {
2233 ConvertedBuffer() : m_buf(NULL) {}
2234 ~ConvertedBuffer()
2235 { free(m_buf); }
2236
2237 operator T*() const { return m_buf; }
2238
2239 ConvertedBuffer& operator=(T *str)
2240 {
2241 free(m_buf);
2242 m_buf = str;
2243 return *this;
2244 }
2245
2246 T *m_buf;
2247 };
2248 #if wxUSE_UNICODE
2249 ConvertedBuffer<char> m_convertedToChar;
2250 #endif
2251 #if !wxUSE_UNICODE_WCHAR
2252 ConvertedBuffer<wchar_t> m_convertedToWChar;
2253 #endif
2254 friend class WXDLLIMPEXP_BASE wxCStrData;
2255 };
2256
2257 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
2258 #pragma warning (default:4275)
2259 #endif
2260
2261 // string iterator operators that satisfy STL Random Access Iterator
2262 // requirements:
2263 inline wxString::iterator operator+(int n, wxString::iterator i)
2264 { return i + n; }
2265 inline wxString::iterator operator+(size_t n, wxString::iterator i)
2266 { return i + n; }
2267 inline wxString::const_iterator operator+(int n, wxString::const_iterator i)
2268 { return i + n; }
2269 inline wxString::const_iterator operator+(size_t n, wxString::const_iterator i)
2270 { return i + n; }
2271 inline wxString::reverse_iterator operator+(int n, wxString::reverse_iterator i)
2272 { return i + n; }
2273 inline wxString::reverse_iterator operator+(size_t n, wxString::reverse_iterator i)
2274 { return i + n; }
2275 inline wxString::const_reverse_iterator operator+(int n, wxString::const_reverse_iterator i)
2276 { return i + n; }
2277 inline wxString::const_reverse_iterator operator+(size_t n, wxString::const_reverse_iterator i)
2278 { return i + n; }
2279
2280 // notice that even though for many compilers the friend declarations above are
2281 // enough, from the point of view of C++ standard we must have the declarations
2282 // here as friend ones are not injected in the enclosing namespace and without
2283 // them the code fails to compile with conforming compilers such as xlC or g++4
2284 wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2);
2285 wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const char *psz);
2286 wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wchar_t *pwz);
2287 wxString WXDLLIMPEXP_BASE operator+(const char *psz, const wxString& string);
2288 wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz, const wxString& string);
2289
2290 wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch);
2291 wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string);
2292
2293 inline wxString operator+(const wxString& string, wxUniCharRef ch)
2294 { return string + (wxUniChar)ch; }
2295 inline wxString operator+(const wxString& string, char ch)
2296 { return string + wxUniChar(ch); }
2297 inline wxString operator+(const wxString& string, wchar_t ch)
2298 { return string + wxUniChar(ch); }
2299 inline wxString operator+(wxUniCharRef ch, const wxString& string)
2300 { return (wxUniChar)ch + string; }
2301 inline wxString operator+(char ch, const wxString& string)
2302 { return wxUniChar(ch) + string; }
2303 inline wxString operator+(wchar_t ch, const wxString& string)
2304 { return wxUniChar(ch) + string; }
2305
2306
2307 #if wxUSE_STL_BASED_WXSTRING
2308 // return an empty wxString (not very useful with wxUSE_STL == 1)
2309 inline const wxString wxGetEmptyString() { return wxString(); }
2310 #else // !wxUSE_STL_BASED_WXSTRING
2311 // return an empty wxString (more efficient than wxString() here)
2312 inline const wxString& wxGetEmptyString()
2313 {
2314 return *(wxString *)&wxEmptyString;
2315 }
2316 #endif // wxUSE_STL_BASED_WXSTRING/!wxUSE_STL_BASED_WXSTRING
2317
2318 // ----------------------------------------------------------------------------
2319 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
2320 // ----------------------------------------------------------------------------
2321
2322 #if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
2323
2324 class WXDLLIMPEXP_BASE wxStringBuffer
2325 {
2326 public:
2327 wxStringBuffer(wxString& str, size_t lenWanted = 1024)
2328 : m_str(str), m_buf(lenWanted)
2329 { }
2330
2331 ~wxStringBuffer() { m_str.assign(m_buf.data(), wxStrlen(m_buf.data())); }
2332
2333 operator wxChar*() { return m_buf.data(); }
2334
2335 private:
2336 wxString& m_str;
2337 #if wxUSE_UNICODE
2338 wxWCharBuffer m_buf;
2339 #else
2340 wxCharBuffer m_buf;
2341 #endif
2342
2343 DECLARE_NO_COPY_CLASS(wxStringBuffer)
2344 };
2345
2346 class WXDLLIMPEXP_BASE wxStringBufferLength
2347 {
2348 public:
2349 wxStringBufferLength(wxString& str, size_t lenWanted = 1024)
2350 : m_str(str), m_buf(lenWanted), m_len(0), m_lenSet(false)
2351 { }
2352
2353 ~wxStringBufferLength()
2354 {
2355 wxASSERT(m_lenSet);
2356 m_str.assign(m_buf.data(), m_len);
2357 }
2358
2359 operator wxChar*() { return m_buf.data(); }
2360 void SetLength(size_t length) { m_len = length; m_lenSet = true; }
2361
2362 private:
2363 wxString& m_str;
2364 #if wxUSE_UNICODE
2365 wxWCharBuffer m_buf;
2366 #else
2367 wxCharBuffer m_buf;
2368 #endif
2369 size_t m_len;
2370 bool m_lenSet;
2371
2372 DECLARE_NO_COPY_CLASS(wxStringBufferLength)
2373 };
2374
2375 #else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2376
2377 class WXDLLIMPEXP_BASE wxStringBuffer
2378 {
2379 public:
2380 wxStringBuffer(wxString& str, size_t lenWanted = 1024)
2381 : m_str(str), m_buf(NULL)
2382 { m_buf = m_str.DoGetWriteBuf(lenWanted); }
2383
2384 ~wxStringBuffer() { m_str.DoUngetWriteBuf(); }
2385
2386 operator wxChar*() const { return m_buf; }
2387
2388 private:
2389 wxString& m_str;
2390 wxChar *m_buf;
2391
2392 DECLARE_NO_COPY_CLASS(wxStringBuffer)
2393 };
2394
2395 class WXDLLIMPEXP_BASE wxStringBufferLength
2396 {
2397 public:
2398 wxStringBufferLength(wxString& str, size_t lenWanted = 1024)
2399 : m_str(str), m_buf(NULL), m_len(0), m_lenSet(false)
2400 {
2401 m_buf = m_str.DoGetWriteBuf(lenWanted);
2402 wxASSERT(m_buf != NULL);
2403 }
2404
2405 ~wxStringBufferLength()
2406 {
2407 wxASSERT(m_lenSet);
2408 m_str.DoUngetWriteBuf(m_len);
2409 }
2410
2411 operator wxChar*() const { return m_buf; }
2412 void SetLength(size_t length) { m_len = length; m_lenSet = true; }
2413
2414 private:
2415 wxString& m_str;
2416 wxChar *m_buf;
2417 size_t m_len;
2418 bool m_lenSet;
2419
2420 DECLARE_NO_COPY_CLASS(wxStringBufferLength)
2421 };
2422
2423 #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2424
2425 // ---------------------------------------------------------------------------
2426 // wxString comparison functions: operator versions are always case sensitive
2427 // ---------------------------------------------------------------------------
2428
2429 #define wxCMP_WXCHAR_STRING(p, s, op) s.Cmp(p) op 0
2430
2431 wxDEFINE_ALL_COMPARISONS(const wxChar *, const wxString&, wxCMP_WXCHAR_STRING)
2432
2433 #undef wxCMP_WXCHAR_STRING
2434
2435 // note that there is an optimization in operator==() and !=(): we (quickly)
2436 // checks the strings length first, before comparing their data
2437 inline bool operator==(const wxString& s1, const wxString& s2)
2438 { return (s1.Len() == s2.Len()) && (s1.Cmp(s2) == 0); }
2439 inline bool operator!=(const wxString& s1, const wxString& s2)
2440 { return (s1.Len() != s2.Len()) || (s1.Cmp(s2) != 0); }
2441 inline bool operator< (const wxString& s1, const wxString& s2)
2442 { return s1.Cmp(s2) < 0; }
2443 inline bool operator> (const wxString& s1, const wxString& s2)
2444 { return s1.Cmp(s2) > 0; }
2445 inline bool operator<=(const wxString& s1, const wxString& s2)
2446 { return s1.Cmp(s2) <= 0; }
2447 inline bool operator>=(const wxString& s1, const wxString& s2)
2448 { return s1.Cmp(s2) >= 0; }
2449
2450 #if wxUSE_UNICODE
2451 inline bool operator==(const wxString& s1, const wxWCharBuffer& s2)
2452 { return (s1.Cmp((const wchar_t *)s2) == 0); }
2453 inline bool operator==(const wxWCharBuffer& s1, const wxString& s2)
2454 { return (s2.Cmp((const wchar_t *)s1) == 0); }
2455 inline bool operator!=(const wxString& s1, const wxWCharBuffer& s2)
2456 { return (s1.Cmp((const wchar_t *)s2) != 0); }
2457 inline bool operator!=(const wxWCharBuffer& s1, const wxString& s2)
2458 { return (s2.Cmp((const wchar_t *)s1) != 0); }
2459 #else // !wxUSE_UNICODE
2460 inline bool operator==(const wxString& s1, const wxCharBuffer& s2)
2461 { return (s1.Cmp((const char *)s2) == 0); }
2462 inline bool operator==(const wxCharBuffer& s1, const wxString& s2)
2463 { return (s2.Cmp((const char *)s1) == 0); }
2464 inline bool operator!=(const wxString& s1, const wxCharBuffer& s2)
2465 { return (s1.Cmp((const char *)s2) != 0); }
2466 inline bool operator!=(const wxCharBuffer& s1, const wxString& s2)
2467 { return (s2.Cmp((const char *)s1) != 0); }
2468 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
2469
2470 #if wxUSE_UNICODE
2471 inline wxString operator+(const wxString& string, const wxWCharBuffer& buf)
2472 { return string + (const wchar_t *)buf; }
2473 inline wxString operator+(const wxWCharBuffer& buf, const wxString& string)
2474 { return (const wchar_t *)buf + string; }
2475 #else // !wxUSE_UNICODE
2476 inline wxString operator+(const wxString& string, const wxCharBuffer& buf)
2477 { return string + (const char *)buf; }
2478 inline wxString operator+(const wxCharBuffer& buf, const wxString& string)
2479 { return (const char *)buf + string; }
2480 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
2481
2482 // comparison with char
2483 inline bool operator==(const wxUniChar& c, const wxString& s) { return s.IsSameAs(c); }
2484 inline bool operator==(const wxUniCharRef& c, const wxString& s) { return s.IsSameAs(c); }
2485 inline bool operator==(char c, const wxString& s) { return s.IsSameAs(c); }
2486 inline bool operator==(wchar_t c, const wxString& s) { return s.IsSameAs(c); }
2487 inline bool operator==(int c, const wxString& s) { return s.IsSameAs(c); }
2488 inline bool operator==(const wxString& s, const wxUniChar& c) { return s.IsSameAs(c); }
2489 inline bool operator==(const wxString& s, const wxUniCharRef& c) { return s.IsSameAs(c); }
2490 inline bool operator==(const wxString& s, char c) { return s.IsSameAs(c); }
2491 inline bool operator==(const wxString& s, wchar_t c) { return s.IsSameAs(c); }
2492 inline bool operator!=(const wxUniChar& c, const wxString& s) { return !s.IsSameAs(c); }
2493 inline bool operator!=(const wxUniCharRef& c, const wxString& s) { return !s.IsSameAs(c); }
2494 inline bool operator!=(char c, const wxString& s) { return !s.IsSameAs(c); }
2495 inline bool operator!=(wchar_t c, const wxString& s) { return !s.IsSameAs(c); }
2496 inline bool operator!=(int c, const wxString& s) { return !s.IsSameAs(c); }
2497 inline bool operator!=(const wxString& s, const wxUniChar& c) { return !s.IsSameAs(c); }
2498 inline bool operator!=(const wxString& s, const wxUniCharRef& c) { return !s.IsSameAs(c); }
2499 inline bool operator!=(const wxString& s, char c) { return !s.IsSameAs(c); }
2500 inline bool operator!=(const wxString& s, wchar_t c) { return !s.IsSameAs(c); }
2501
2502 // comparison with C string in Unicode build
2503 #if wxUSE_UNICODE
2504
2505 #define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s
2506
2507 wxDEFINE_ALL_COMPARISONS(const char *, const wxString&, wxCMP_CHAR_STRING)
2508
2509 #undef wxCMP_CHAR_STRING
2510
2511 #endif // wxUSE_UNICODE
2512
2513 // we also need to provide the operators for comparison with wxCStrData to
2514 // resolve ambiguity between operator(const wxChar *,const wxString &) and
2515 // operator(const wxChar *, const wxChar *) for "p == s.c_str()"
2516 //
2517 // notice that these are (shallow) pointer comparisons, not (deep) string ones
2518 #define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar()
2519 #define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar()
2520
2521 wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData&, wxCMP_WCHAR_CSTRDATA)
2522 wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData&, wxCMP_CHAR_CSTRDATA)
2523
2524 #undef wxCMP_CHAR_CSTRDATA
2525 #undef wxCMP_WCHAR_CSTRDATA
2526
2527 // ---------------------------------------------------------------------------
2528 // Implementation only from here until the end of file
2529 // ---------------------------------------------------------------------------
2530
2531 #if wxUSE_STD_IOSTREAM
2532
2533 #include "wx/iosfwrap.h"
2534
2535 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxString&);
2536 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCStrData&);
2537 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCharBuffer&);
2538 #ifndef __BORLANDC__
2539 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxWCharBuffer&);
2540 #endif
2541
2542 #endif // wxSTD_STRING_COMPATIBILITY
2543
2544 // ---------------------------------------------------------------------------
2545 // wxCStrData implementation
2546 // ---------------------------------------------------------------------------
2547
2548 inline wxCStrData::wxCStrData(char *buf)
2549 : m_str(new wxString(buf)), m_offset(0), m_owned(true) {}
2550 inline wxCStrData::wxCStrData(wchar_t *buf)
2551 : m_str(new wxString(buf)), m_offset(0), m_owned(true) {}
2552
2553 inline wxCStrData::~wxCStrData()
2554 {
2555 if ( m_owned )
2556 delete m_str;
2557 }
2558
2559 inline wxCStrData::operator bool() const
2560 {
2561 return !m_str->empty();
2562 }
2563
2564 // simple cases for AsChar() and AsWChar(), the complicated ones are
2565 // in string.cpp
2566 #if wxUSE_UNICODE_WCHAR
2567 inline const wchar_t* wxCStrData::AsWChar() const
2568 {
2569 return m_str->wx_str() + m_offset;
2570 }
2571 #endif // wxUSE_UNICODE_WCHAR
2572
2573 #if !wxUSE_UNICODE
2574 inline const char* wxCStrData::AsChar() const
2575 {
2576 return m_str->wx_str() + m_offset;
2577 }
2578 #endif // !wxUSE_UNICODE
2579
2580 inline const wxCharBuffer wxCStrData::AsCharBuf() const
2581 {
2582 #if !wxUSE_UNICODE
2583 return wxCharBuffer::CreateNonOwned(AsChar());
2584 #else
2585 return AsString().mb_str();
2586 #endif
2587 }
2588
2589 inline const wxWCharBuffer wxCStrData::AsWCharBuf() const
2590 {
2591 #if wxUSE_UNICODE_WCHAR
2592 return wxWCharBuffer::CreateNonOwned(AsWChar());
2593 #else
2594 return AsString().wc_str();
2595 #endif
2596 }
2597
2598 inline wxString wxCStrData::AsString() const
2599 {
2600 if ( m_offset == 0 )
2601 return *m_str;
2602 else
2603 return m_str->Mid(m_offset);
2604 }
2605
2606 inline wxUniChar wxCStrData::operator*() const
2607 {
2608 if ( m_str->empty() )
2609 return wxUniChar(_T('\0'));
2610 else
2611 return (*m_str)[m_offset];
2612 }
2613
2614 inline wxUniChar wxCStrData::operator[](size_t n) const
2615 {
2616 return m_str->at(m_offset + n);
2617 }
2618
2619 // ----------------------------------------------------------------------------
2620 // more wxCStrData operators
2621 // ----------------------------------------------------------------------------
2622
2623 // we need to define those to allow "size_t pos = p - s.c_str()" where p is
2624 // some pointer into the string
2625 inline size_t operator-(const char *p, const wxCStrData& cs)
2626 {
2627 return p - cs.AsChar();
2628 }
2629
2630 inline size_t operator-(const wchar_t *p, const wxCStrData& cs)
2631 {
2632 return p - cs.AsWChar();
2633 }
2634
2635 // ----------------------------------------------------------------------------
2636 // implementation of wx[W]CharBuffer inline methods using wxCStrData
2637 // ----------------------------------------------------------------------------
2638
2639 // FIXME-UTF8: move this to buffer.h
2640 inline wxCharBuffer::wxCharBuffer(const wxCStrData& cstr)
2641 : wxCharTypeBufferBase(cstr.AsCharBuf())
2642 {
2643 }
2644
2645 inline wxWCharBuffer::wxWCharBuffer(const wxCStrData& cstr)
2646 : wxCharTypeBufferBase(cstr.AsWCharBuf())
2647 {
2648 }
2649
2650 #endif // _WX_WXSTRING_H_