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