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