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