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