]> git.saurik.com Git - wxWidgets.git/blob - include/wx/string.h
converting temporary wxString to const wxChar* and storing that doesn't work (and...
[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 { m_impl = wxStringOperations::EncodeChar(ch); return *this; }
1347 wxString& operator=(wxUniCharRef ch)
1348 { return operator=((wxUniChar)ch); }
1349 wxString& operator=(char ch)
1350 { return operator=(wxUniChar(ch)); }
1351 wxString& operator=(unsigned char ch)
1352 { return operator=(wxUniChar(ch)); }
1353 wxString& operator=(wchar_t ch)
1354 { return operator=(wxUniChar(ch)); }
1355 // from a C string - STL probably will crash on NULL,
1356 // so we need to compensate in that case
1357 #if wxUSE_STL_BASED_WXSTRING
1358 wxString& operator=(const char *psz)
1359 { if (psz) m_impl = ImplStr(psz); else Clear(); return *this; }
1360 wxString& operator=(const wchar_t *pwz)
1361 { if (pwz) m_impl = ImplStr(pwz); else Clear(); return *this; }
1362 #else
1363 wxString& operator=(const char *psz)
1364 { m_impl = ImplStr(psz); return *this; }
1365 wxString& operator=(const wchar_t *pwz)
1366 { m_impl = ImplStr(pwz); return *this; }
1367 #endif
1368 wxString& operator=(const unsigned char *psz)
1369 { return operator=((const char*)psz); }
1370
1371 // from wxWCharBuffer
1372 wxString& operator=(const wxWCharBuffer& s)
1373 { return operator=(s.data()); } // FIXME-UTF8: fix for embedded NULs
1374 // from wxCharBuffer
1375 wxString& operator=(const wxCharBuffer& s)
1376 { return operator=(s.data()); } // FIXME-UTF8: fix for embedded NULs
1377
1378 // string concatenation
1379 // in place concatenation
1380 /*
1381 Concatenate and return the result. Note that the left to right
1382 associativity of << allows to write things like "str << str1 << str2
1383 << ..." (unlike with +=)
1384 */
1385 // string += string
1386 wxString& operator<<(const wxString& s)
1387 {
1388 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1389 wxASSERT_MSG( s.IsValid(),
1390 _T("did you forget to call UngetWriteBuf()?") );
1391 #endif
1392
1393 append(s);
1394 return *this;
1395 }
1396 // string += C string
1397 wxString& operator<<(const char *psz)
1398 { append(psz); return *this; }
1399 wxString& operator<<(const wchar_t *pwz)
1400 { append(pwz); return *this; }
1401 wxString& operator<<(const wxCStrData& psz)
1402 { append(psz.AsString()); return *this; }
1403 // string += char
1404 wxString& operator<<(wxUniChar ch) { append(1, ch); return *this; }
1405 wxString& operator<<(wxUniCharRef ch) { append(1, ch); return *this; }
1406 wxString& operator<<(char ch) { append(1, ch); return *this; }
1407 wxString& operator<<(unsigned char ch) { append(1, ch); return *this; }
1408 wxString& operator<<(wchar_t ch) { append(1, ch); return *this; }
1409
1410 // string += buffer (i.e. from wxGetString)
1411 wxString& operator<<(const wxWCharBuffer& s)
1412 { return operator<<((const wchar_t *)s); }
1413 wxString& operator<<(const wxCharBuffer& s)
1414 { return operator<<((const char *)s); }
1415
1416 // string += C string
1417 wxString& Append(const wxString& s)
1418 {
1419 // test for empty() to share the string if possible
1420 if ( empty() )
1421 *this = s;
1422 else
1423 append(s);
1424 return *this;
1425 }
1426 wxString& Append(const char* psz)
1427 { append(psz); return *this; }
1428 wxString& Append(const wchar_t* pwz)
1429 { append(pwz); return *this; }
1430 wxString& Append(const wxCStrData& psz)
1431 { append(psz); return *this; }
1432 wxString& Append(const wxCharBuffer& psz)
1433 { append(psz); return *this; }
1434 wxString& Append(const wxWCharBuffer& psz)
1435 { append(psz); return *this; }
1436 // append count copies of given character
1437 wxString& Append(wxUniChar ch, size_t count = 1u)
1438 { append(count, ch); return *this; }
1439 wxString& Append(wxUniCharRef ch, size_t count = 1u)
1440 { append(count, ch); return *this; }
1441 wxString& Append(char ch, size_t count = 1u)
1442 { append(count, ch); return *this; }
1443 wxString& Append(unsigned char ch, size_t count = 1u)
1444 { append(count, ch); return *this; }
1445 wxString& Append(wchar_t ch, size_t count = 1u)
1446 { append(count, ch); return *this; }
1447 wxString& Append(const char* psz, size_t nLen)
1448 { append(psz, nLen); return *this; }
1449 wxString& Append(const wchar_t* pwz, size_t nLen)
1450 { append(pwz, nLen); return *this; }
1451
1452 // prepend a string, return the string itself
1453 wxString& Prepend(const wxString& str)
1454 { *this = str + *this; return *this; }
1455
1456 // non-destructive concatenation
1457 // two strings
1458 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string1,
1459 const wxString& string2);
1460 // string with a single char
1461 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch);
1462 // char with a string
1463 friend wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string);
1464 // string with C string
1465 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string,
1466 const char *psz);
1467 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string,
1468 const wchar_t *pwz);
1469 // C string with string
1470 friend wxString WXDLLIMPEXP_BASE operator+(const char *psz,
1471 const wxString& string);
1472 friend wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz,
1473 const wxString& string);
1474
1475 // stream-like functions
1476 // insert an int into string
1477 wxString& operator<<(int i)
1478 { return (*this) << Format(_T("%d"), i); }
1479 // insert an unsigned int into string
1480 wxString& operator<<(unsigned int ui)
1481 { return (*this) << Format(_T("%u"), ui); }
1482 // insert a long into string
1483 wxString& operator<<(long l)
1484 { return (*this) << Format(_T("%ld"), l); }
1485 // insert an unsigned long into string
1486 wxString& operator<<(unsigned long ul)
1487 { return (*this) << Format(_T("%lu"), ul); }
1488 #if defined wxLongLong_t && !defined wxLongLongIsLong
1489 // insert a long long if they exist and aren't longs
1490 wxString& operator<<(wxLongLong_t ll)
1491 {
1492 const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("d");
1493 return (*this) << Format(fmt, ll);
1494 }
1495 // insert an unsigned long long
1496 wxString& operator<<(wxULongLong_t ull)
1497 {
1498 const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("u");
1499 return (*this) << Format(fmt , ull);
1500 }
1501 #endif
1502 // insert a float into string
1503 wxString& operator<<(float f)
1504 { return (*this) << Format(_T("%f"), f); }
1505 // insert a double into string
1506 wxString& operator<<(double d)
1507 { return (*this) << Format(_T("%g"), d); }
1508
1509 // string comparison
1510 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
1511 int Cmp(const char *psz) const
1512 { return compare(psz); }
1513 int Cmp(const wchar_t *pwz) const
1514 { return compare(pwz); }
1515 int Cmp(const wxString& s) const
1516 { return compare(s); }
1517 int Cmp(const wxCStrData& s) const
1518 { return compare(s); }
1519 int Cmp(const wxCharBuffer& s) const
1520 { return compare(s); }
1521 int Cmp(const wxWCharBuffer& s) const
1522 { return compare(s); }
1523 // same as Cmp() but not case-sensitive
1524 int CmpNoCase(const wxString& s) const;
1525
1526 // test for the string equality, either considering case or not
1527 // (if compareWithCase then the case matters)
1528 bool IsSameAs(const wxString& str, bool compareWithCase = true) const
1529 {
1530 #if !wxUSE_UNICODE_UTF8
1531 // in UTF-8 build, length() is O(n) and doing this would be _slower_
1532 if ( length() != str.length() )
1533 return false;
1534 #endif
1535 return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0;
1536 }
1537 bool IsSameAs(const char *str, bool compareWithCase = true) const
1538 { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; }
1539 bool IsSameAs(const wchar_t *str, bool compareWithCase = true) const
1540 { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; }
1541
1542 bool IsSameAs(const wxCStrData& str, bool compareWithCase = true) const
1543 { return IsSameAs(str.AsString(), compareWithCase); }
1544 bool IsSameAs(const wxCharBuffer& str, bool compareWithCase = true) const
1545 { return IsSameAs(str.data(), compareWithCase); }
1546 bool IsSameAs(const wxWCharBuffer& str, bool compareWithCase = true) const
1547 { return IsSameAs(str.data(), compareWithCase); }
1548 // comparison with a single character: returns true if equal
1549 bool IsSameAs(wxUniChar c, bool compareWithCase = true) const;
1550 // FIXME-UTF8: remove these overloads
1551 bool IsSameAs(wxUniCharRef c, bool compareWithCase = true) const
1552 { return IsSameAs(wxUniChar(c), compareWithCase); }
1553 bool IsSameAs(char c, bool compareWithCase = true) const
1554 { return IsSameAs(wxUniChar(c), compareWithCase); }
1555 bool IsSameAs(unsigned char c, bool compareWithCase = true) const
1556 { return IsSameAs(wxUniChar(c), compareWithCase); }
1557 bool IsSameAs(wchar_t c, bool compareWithCase = true) const
1558 { return IsSameAs(wxUniChar(c), compareWithCase); }
1559 bool IsSameAs(int c, bool compareWithCase = true) const
1560 { return IsSameAs(wxUniChar(c), compareWithCase); }
1561
1562 // simple sub-string extraction
1563 // return substring starting at nFirst of length nCount (or till the end
1564 // if nCount = default value)
1565 wxString Mid(size_t nFirst, size_t nCount = npos) const;
1566
1567 // operator version of Mid()
1568 wxString operator()(size_t start, size_t len) const
1569 { return Mid(start, len); }
1570
1571 // check if the string starts with the given prefix and return the rest
1572 // of the string in the provided pointer if it is not NULL; otherwise
1573 // return false
1574 bool StartsWith(const wxString& prefix, wxString *rest = NULL) const;
1575 // check if the string ends with the given suffix and return the
1576 // beginning of the string before the suffix in the provided pointer if
1577 // it is not NULL; otherwise return false
1578 bool EndsWith(const wxString& suffix, wxString *rest = NULL) const;
1579
1580 // get first nCount characters
1581 wxString Left(size_t nCount) const;
1582 // get last nCount characters
1583 wxString Right(size_t nCount) const;
1584 // get all characters before the first occurance of ch
1585 // (returns the whole string if ch not found)
1586 wxString BeforeFirst(wxUniChar ch) const;
1587 // get all characters before the last occurence of ch
1588 // (returns empty string if ch not found)
1589 wxString BeforeLast(wxUniChar ch) const;
1590 // get all characters after the first occurence of ch
1591 // (returns empty string if ch not found)
1592 wxString AfterFirst(wxUniChar ch) const;
1593 // get all characters after the last occurence of ch
1594 // (returns the whole string if ch not found)
1595 wxString AfterLast(wxUniChar ch) const;
1596
1597 // for compatibility only, use more explicitly named functions above
1598 wxString Before(wxUniChar ch) const { return BeforeLast(ch); }
1599 wxString After(wxUniChar ch) const { return AfterFirst(ch); }
1600
1601 // case conversion
1602 // convert to upper case in place, return the string itself
1603 wxString& MakeUpper();
1604 // convert to upper case, return the copy of the string
1605 // Here's something to remember: BC++ doesn't like returns in inlines.
1606 wxString Upper() const ;
1607 // convert to lower case in place, return the string itself
1608 wxString& MakeLower();
1609 // convert to lower case, return the copy of the string
1610 wxString Lower() const ;
1611
1612 // trimming/padding whitespace (either side) and truncating
1613 // remove spaces from left or from right (default) side
1614 wxString& Trim(bool bFromRight = true);
1615 // add nCount copies chPad in the beginning or at the end (default)
1616 wxString& Pad(size_t nCount, wxUniChar chPad = wxT(' '), bool bFromRight = true);
1617
1618 // searching and replacing
1619 // searching (return starting index, or -1 if not found)
1620 int Find(wxUniChar ch, bool bFromEnd = false) const; // like strchr/strrchr
1621 int Find(wxUniCharRef ch, bool bFromEnd = false) const
1622 { return Find(wxUniChar(ch), bFromEnd); }
1623 int Find(char ch, bool bFromEnd = false) const
1624 { return Find(wxUniChar(ch), bFromEnd); }
1625 int Find(unsigned char ch, bool bFromEnd = false) const
1626 { return Find(wxUniChar(ch), bFromEnd); }
1627 int Find(wchar_t ch, bool bFromEnd = false) const
1628 { return Find(wxUniChar(ch), bFromEnd); }
1629 // searching (return starting index, or -1 if not found)
1630 int Find(const wxString& sub) const // like strstr
1631 {
1632 size_type idx = find(sub);
1633 return (idx == npos) ? wxNOT_FOUND : (int)idx;
1634 }
1635 int Find(const char *sub) const // like strstr
1636 {
1637 size_type idx = find(sub);
1638 return (idx == npos) ? wxNOT_FOUND : (int)idx;
1639 }
1640 int Find(const wchar_t *sub) const // like strstr
1641 {
1642 size_type idx = find(sub);
1643 return (idx == npos) ? wxNOT_FOUND : (int)idx;
1644 }
1645
1646 int Find(const wxCStrData& sub) const
1647 { return Find(sub.AsString()); }
1648 int Find(const wxCharBuffer& sub) const
1649 { return Find(sub.data()); }
1650 int Find(const wxWCharBuffer& sub) const
1651 { return Find(sub.data()); }
1652
1653 // replace first (or all of bReplaceAll) occurences of substring with
1654 // another string, returns the number of replacements made
1655 size_t Replace(const wxString& strOld,
1656 const wxString& strNew,
1657 bool bReplaceAll = true);
1658
1659 // check if the string contents matches a mask containing '*' and '?'
1660 bool Matches(const wxString& mask) const;
1661
1662 // conversion to numbers: all functions return true only if the whole
1663 // string is a number and put the value of this number into the pointer
1664 // provided, the base is the numeric base in which the conversion should be
1665 // done and must be comprised between 2 and 36 or be 0 in which case the
1666 // standard C rules apply (leading '0' => octal, "0x" => hex)
1667 // convert to a signed integer
1668 bool ToLong(long *val, int base = 10) const;
1669 // convert to an unsigned integer
1670 bool ToULong(unsigned long *val, int base = 10) const;
1671 // convert to wxLongLong
1672 #if defined(wxLongLong_t)
1673 bool ToLongLong(wxLongLong_t *val, int base = 10) const;
1674 // convert to wxULongLong
1675 bool ToULongLong(wxULongLong_t *val, int base = 10) const;
1676 #endif // wxLongLong_t
1677 // convert to a double
1678 bool ToDouble(double *val) const;
1679
1680
1681 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1682 // formatted input/output
1683 // as sprintf(), returns the number of characters written or < 0 on error
1684 // (take 'this' into account in attribute parameter count)
1685 // int Printf(const wxString& format, ...);
1686 WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const wxFormatString&),
1687 DoPrintfWchar, DoPrintfUtf8)
1688 #ifdef __WATCOMC__
1689 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1690 WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wxString&),
1691 (wxFormatString(f1)));
1692 WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wxCStrData&),
1693 (wxFormatString(f1)));
1694 WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const char*),
1695 (wxFormatString(f1)));
1696 WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wchar_t*),
1697 (wxFormatString(f1)));
1698 #endif
1699 #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN
1700 // as vprintf(), returns the number of characters written or < 0 on error
1701 int PrintfV(const wxString& format, va_list argptr);
1702
1703 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1704 // returns the string containing the result of Printf() to it
1705 // static wxString Format(const wxString& format, ...) ATTRIBUTE_PRINTF_1;
1706 WX_DEFINE_VARARG_FUNC(static wxString, Format, 1, (const wxFormatString&),
1707 DoFormatWchar, DoFormatUtf8)
1708 #ifdef __WATCOMC__
1709 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1710 WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wxString&),
1711 (wxFormatString(f1)));
1712 WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wxCStrData&),
1713 (wxFormatString(f1)));
1714 WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const char*),
1715 (wxFormatString(f1)));
1716 WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wchar_t*),
1717 (wxFormatString(f1)));
1718 #endif
1719 #endif
1720 // the same as above, but takes a va_list
1721 static wxString FormatV(const wxString& format, va_list argptr);
1722
1723 // raw access to string memory
1724 // ensure that string has space for at least nLen characters
1725 // only works if the data of this string is not shared
1726 bool Alloc(size_t nLen) { reserve(nLen); /*return capacity() >= nLen;*/ return true; }
1727 // minimize the string's memory
1728 // only works if the data of this string is not shared
1729 bool Shrink();
1730 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1731 // These are deprecated, use wxStringBuffer or wxStringBufferLength instead
1732 //
1733 // get writable buffer of at least nLen bytes. Unget() *must* be called
1734 // a.s.a.p. to put string back in a reasonable state!
1735 wxDEPRECATED( wxStringCharType *GetWriteBuf(size_t nLen) );
1736 // call this immediately after GetWriteBuf() has been used
1737 wxDEPRECATED( void UngetWriteBuf() );
1738 wxDEPRECATED( void UngetWriteBuf(size_t nLen) );
1739 #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && wxUSE_UNICODE_UTF8
1740
1741 // wxWidgets version 1 compatibility functions
1742
1743 // use Mid()
1744 wxString SubString(size_t from, size_t to) const
1745 { return Mid(from, (to - from + 1)); }
1746 // values for second parameter of CompareTo function
1747 enum caseCompare {exact, ignoreCase};
1748 // values for first parameter of Strip function
1749 enum stripType {leading = 0x1, trailing = 0x2, both = 0x3};
1750
1751 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1752 // use Printf()
1753 // (take 'this' into account in attribute parameter count)
1754 // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2;
1755 WX_DEFINE_VARARG_FUNC(int, sprintf, 1, (const wxFormatString&),
1756 DoPrintfWchar, DoPrintfUtf8)
1757 #ifdef __WATCOMC__
1758 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1759 WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wxString&),
1760 (wxFormatString(f1)));
1761 WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wxCStrData&),
1762 (wxFormatString(f1)));
1763 WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const char*),
1764 (wxFormatString(f1)));
1765 WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wchar_t*),
1766 (wxFormatString(f1)));
1767 #endif
1768 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
1769
1770 // use Cmp()
1771 inline int CompareTo(const wxChar* psz, caseCompare cmp = exact) const
1772 { return cmp == exact ? Cmp(psz) : CmpNoCase(psz); }
1773
1774 // use length()
1775 size_t Length() const { return length(); }
1776 // Count the number of characters
1777 int Freq(wxUniChar ch) const;
1778 // use MakeLower
1779 void LowerCase() { MakeLower(); }
1780 // use MakeUpper
1781 void UpperCase() { MakeUpper(); }
1782 // use Trim except that it doesn't change this string
1783 wxString Strip(stripType w = trailing) const;
1784
1785 // use Find (more general variants not yet supported)
1786 size_t Index(const wxChar* psz) const { return Find(psz); }
1787 size_t Index(wxUniChar ch) const { return Find(ch); }
1788 // use Truncate
1789 wxString& Remove(size_t pos) { return Truncate(pos); }
1790 wxString& RemoveLast(size_t n = 1) { return Truncate(length() - n); }
1791
1792 wxString& Remove(size_t nStart, size_t nLen)
1793 { return (wxString&)erase( nStart, nLen ); }
1794
1795 // use Find()
1796 int First( wxUniChar ch ) const { return Find(ch); }
1797 int First( wxUniCharRef ch ) const { return Find(ch); }
1798 int First( char ch ) const { return Find(ch); }
1799 int First( unsigned char ch ) const { return Find(ch); }
1800 int First( wchar_t ch ) const { return Find(ch); }
1801 int First( const wxString& str ) const { return Find(str); }
1802 int Last( wxUniChar ch ) const { return Find(ch, true); }
1803 bool Contains(const wxString& str) const { return Find(str) != wxNOT_FOUND; }
1804
1805 // use empty()
1806 bool IsNull() const { return empty(); }
1807
1808 // std::string compatibility functions
1809
1810 // take nLen chars starting at nPos
1811 wxString(const wxString& str, size_t nPos, size_t nLen)
1812 { assign(str, nPos, nLen); }
1813 // take all characters from first to last
1814 wxString(const_iterator first, const_iterator last)
1815 : m_impl(first.impl(), last.impl()) { }
1816 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1817 // the 2 overloads below are for compatibility with the existing code using
1818 // pointers instead of iterators
1819 wxString(const char *first, const char *last)
1820 {
1821 SubstrBufFromMB str(ImplStr(first, last - first));
1822 m_impl.assign(str.data, str.len);
1823 }
1824 wxString(const wchar_t *first, const wchar_t *last)
1825 {
1826 SubstrBufFromWC str(ImplStr(first, last - first));
1827 m_impl.assign(str.data, str.len);
1828 }
1829 // and this one is needed to compile code adding offsets to c_str() result
1830 wxString(const wxCStrData& first, const wxCStrData& last)
1831 : m_impl(CreateConstIterator(first).impl(),
1832 CreateConstIterator(last).impl())
1833 {
1834 wxASSERT_MSG( first.m_str == last.m_str,
1835 _T("pointers must be into the same string") );
1836 }
1837 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1838
1839 // lib.string.modifiers
1840 // append elements str[pos], ..., str[pos+n]
1841 wxString& append(const wxString& str, size_t pos, size_t n)
1842 {
1843 size_t from, len;
1844 str.PosLenToImpl(pos, n, &from, &len);
1845 m_impl.append(str.m_impl, from, len);
1846 return *this;
1847 }
1848 // append a string
1849 wxString& append(const wxString& str)
1850 { m_impl.append(str.m_impl); return *this; }
1851 // append first n (or all if n == npos) characters of sz
1852 wxString& append(const char *sz)
1853 { m_impl.append(ImplStr(sz)); return *this; }
1854 wxString& append(const wchar_t *sz)
1855 { m_impl.append(ImplStr(sz)); return *this; }
1856 wxString& append(const char *sz, size_t n)
1857 {
1858 SubstrBufFromMB str(ImplStr(sz, n));
1859 m_impl.append(str.data, str.len);
1860 return *this;
1861 }
1862 wxString& append(const wchar_t *sz, size_t n)
1863 {
1864 SubstrBufFromWC str(ImplStr(sz, n));
1865 m_impl.append(str.data, str.len);
1866 return *this;
1867 }
1868
1869 wxString& append(const wxCStrData& str)
1870 { return append(str.AsString()); }
1871 wxString& append(const wxCharBuffer& str)
1872 { return append(str.data()); }
1873 wxString& append(const wxWCharBuffer& str)
1874 { return append(str.data()); }
1875
1876 // append n copies of ch
1877 wxString& append(size_t n, wxUniChar ch)
1878 {
1879 #if wxUSE_UNICODE_UTF8
1880 if ( !ch.IsAscii() )
1881 m_impl.append(wxStringOperations::EncodeNChars(n, ch));
1882 else
1883 #endif
1884 m_impl.append(n, (wxStringCharType)ch);
1885 return *this;
1886 }
1887 // append from first to last
1888 wxString& append(const_iterator first, const_iterator last)
1889 { m_impl.append(first.impl(), last.impl()); return *this; }
1890 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1891 wxString& append(const char *first, const char *last)
1892 { return append(first, last - first); }
1893 wxString& append(const wchar_t *first, const wchar_t *last)
1894 { return append(first, last - first); }
1895 wxString& append(const wxCStrData& first, const wxCStrData& last)
1896 { return append(CreateConstIterator(first), CreateConstIterator(last)); }
1897 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1898
1899 // same as `this_string = str'
1900 wxString& assign(const wxString& str)
1901 { m_impl = str.m_impl; return *this; }
1902 wxString& assign(const wxString& str, size_t len)
1903 {
1904 m_impl.assign(str.m_impl, 0, str.LenToImpl(len));
1905 return *this;
1906 }
1907 // same as ` = str[pos..pos + n]
1908 wxString& assign(const wxString& str, size_t pos, size_t n)
1909 {
1910 size_t from, len;
1911 str.PosLenToImpl(pos, n, &from, &len);
1912 m_impl.assign(str.m_impl, from, len);
1913 return *this;
1914 }
1915 // same as `= first n (or all if n == npos) characters of sz'
1916 wxString& assign(const char *sz)
1917 { m_impl.assign(ImplStr(sz)); return *this; }
1918 wxString& assign(const wchar_t *sz)
1919 { m_impl.assign(ImplStr(sz)); return *this; }
1920 wxString& assign(const char *sz, size_t n)
1921 {
1922 SubstrBufFromMB str(ImplStr(sz, n));
1923 m_impl.assign(str.data, str.len);
1924 return *this;
1925 }
1926 wxString& assign(const wchar_t *sz, size_t n)
1927 {
1928 SubstrBufFromWC str(ImplStr(sz, n));
1929 m_impl.assign(str.data, str.len);
1930 return *this;
1931 }
1932
1933 wxString& assign(const wxCStrData& str)
1934 { return assign(str.AsString()); }
1935 wxString& assign(const wxCharBuffer& str)
1936 { return assign(str.data()); }
1937 wxString& assign(const wxWCharBuffer& str)
1938 { return assign(str.data()); }
1939 wxString& assign(const wxCStrData& str, size_t len)
1940 { return assign(str.AsString(), len); }
1941 wxString& assign(const wxCharBuffer& str, size_t len)
1942 { return assign(str.data(), len); }
1943 wxString& assign(const wxWCharBuffer& str, size_t len)
1944 { return assign(str.data(), len); }
1945
1946 // same as `= n copies of ch'
1947 wxString& assign(size_t n, wxUniChar ch)
1948 {
1949 #if wxUSE_UNICODE_UTF8
1950 if ( !ch.IsAscii() )
1951 m_impl.assign(wxStringOperations::EncodeNChars(n, ch));
1952 else
1953 #endif
1954 m_impl.assign(n, (wxStringCharType)ch);
1955 return *this;
1956 }
1957
1958 wxString& assign(size_t n, wxUniCharRef ch)
1959 { return assign(n, wxUniChar(ch)); }
1960 wxString& assign(size_t n, char ch)
1961 { return assign(n, wxUniChar(ch)); }
1962 wxString& assign(size_t n, unsigned char ch)
1963 { return assign(n, wxUniChar(ch)); }
1964 wxString& assign(size_t n, wchar_t ch)
1965 { return assign(n, wxUniChar(ch)); }
1966
1967 // assign from first to last
1968 wxString& assign(const_iterator first, const_iterator last)
1969 { m_impl.assign(first.impl(), last.impl()); return *this; }
1970 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1971 wxString& assign(const char *first, const char *last)
1972 { return assign(first, last - first); }
1973 wxString& assign(const wchar_t *first, const wchar_t *last)
1974 { return assign(first, last - first); }
1975 wxString& assign(const wxCStrData& first, const wxCStrData& last)
1976 { return assign(CreateConstIterator(first), CreateConstIterator(last)); }
1977 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1978
1979 // string comparison
1980 int compare(const wxString& str) const;
1981 int compare(const char* sz) const;
1982 int compare(const wchar_t* sz) const;
1983 int compare(const wxCStrData& str) const
1984 { return compare(str.AsString()); }
1985 int compare(const wxCharBuffer& str) const
1986 { return compare(str.data()); }
1987 int compare(const wxWCharBuffer& str) const
1988 { return compare(str.data()); }
1989 // comparison with a substring
1990 int compare(size_t nStart, size_t nLen, const wxString& str) const;
1991 // comparison of 2 substrings
1992 int compare(size_t nStart, size_t nLen,
1993 const wxString& str, size_t nStart2, size_t nLen2) const;
1994 // substring comparison with first nCount characters of sz
1995 int compare(size_t nStart, size_t nLen,
1996 const char* sz, size_t nCount = npos) const;
1997 int compare(size_t nStart, size_t nLen,
1998 const wchar_t* sz, size_t nCount = npos) const;
1999
2000 // insert another string
2001 wxString& insert(size_t nPos, const wxString& str)
2002 { insert(begin() + nPos, str.begin(), str.end()); return *this; }
2003 // insert n chars of str starting at nStart (in str)
2004 wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n)
2005 {
2006 size_t from, len;
2007 str.PosLenToImpl(nStart, n, &from, &len);
2008 m_impl.insert(PosToImpl(nPos), str.m_impl, from, len);
2009 return *this;
2010 }
2011 // insert first n (or all if n == npos) characters of sz
2012 wxString& insert(size_t nPos, const char *sz)
2013 { m_impl.insert(PosToImpl(nPos), ImplStr(sz)); return *this; }
2014 wxString& insert(size_t nPos, const wchar_t *sz)
2015 { m_impl.insert(PosToImpl(nPos), ImplStr(sz)); return *this; }
2016 wxString& insert(size_t nPos, const char *sz, size_t n)
2017 {
2018 SubstrBufFromMB str(ImplStr(sz, n));
2019 m_impl.insert(PosToImpl(nPos), str.data, str.len);
2020 return *this;
2021 }
2022 wxString& insert(size_t nPos, const wchar_t *sz, size_t n)
2023 {
2024 SubstrBufFromWC str(ImplStr(sz, n));
2025 m_impl.insert(PosToImpl(nPos), str.data, str.len);
2026 return *this;
2027 }
2028 // insert n copies of ch
2029 wxString& insert(size_t nPos, size_t n, wxUniChar ch)
2030 {
2031 #if wxUSE_UNICODE_UTF8
2032 if ( !ch.IsAscii() )
2033 m_impl.insert(PosToImpl(nPos), wxStringOperations::EncodeNChars(n, ch));
2034 else
2035 #endif
2036 m_impl.insert(PosToImpl(nPos), n, (wxStringCharType)ch);
2037 return *this;
2038 }
2039 iterator insert(iterator it, wxUniChar ch)
2040 {
2041 #if wxUSE_UNICODE_UTF8
2042 if ( !ch.IsAscii() )
2043 {
2044 size_t pos = IterToImplPos(it);
2045 m_impl.insert(pos, wxStringOperations::EncodeChar(ch));
2046 return iterator(this, m_impl.begin() + pos);
2047 }
2048 else
2049 #endif
2050 return iterator(this, m_impl.insert(it.impl(), (wxStringCharType)ch));
2051 }
2052 void insert(iterator it, const_iterator first, const_iterator last)
2053 { m_impl.insert(it.impl(), first.impl(), last.impl()); }
2054 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2055 void insert(iterator it, const char *first, const char *last)
2056 { insert(it - begin(), first, last - first); }
2057 void insert(iterator it, const wchar_t *first, const wchar_t *last)
2058 { insert(it - begin(), first, last - first); }
2059 void insert(iterator it, const wxCStrData& first, const wxCStrData& last)
2060 { insert(it, CreateConstIterator(first), CreateConstIterator(last)); }
2061 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2062
2063 void insert(iterator it, size_type n, wxUniChar ch)
2064 {
2065 #if wxUSE_UNICODE_UTF8
2066 if ( !ch.IsAscii() )
2067 m_impl.insert(IterToImplPos(it), wxStringOperations::EncodeNChars(n, ch));
2068 else
2069 #endif
2070 m_impl.insert(it.impl(), n, (wxStringCharType)ch);
2071 }
2072
2073 // delete characters from nStart to nStart + nLen
2074 wxString& erase(size_type pos = 0, size_type n = npos)
2075 {
2076 size_t from, len;
2077 PosLenToImpl(pos, n, &from, &len);
2078 m_impl.erase(from, len);
2079 return *this;
2080 }
2081 // delete characters from first up to last
2082 iterator erase(iterator first, iterator last)
2083 { return iterator(this, m_impl.erase(first.impl(), last.impl())); }
2084 iterator erase(iterator first)
2085 { return iterator(this, m_impl.erase(first.impl())); }
2086
2087 #ifdef wxSTRING_BASE_HASNT_CLEAR
2088 void clear() { erase(); }
2089 #else
2090 void clear() { m_impl.clear(); }
2091 #endif
2092
2093 // replaces the substring of length nLen starting at nStart
2094 wxString& replace(size_t nStart, size_t nLen, const char* sz)
2095 {
2096 size_t from, len;
2097 PosLenToImpl(nStart, nLen, &from, &len);
2098 m_impl.replace(from, len, ImplStr(sz));
2099 return *this;
2100 }
2101 wxString& replace(size_t nStart, size_t nLen, const wchar_t* sz)
2102 {
2103 size_t from, len;
2104 PosLenToImpl(nStart, nLen, &from, &len);
2105 m_impl.replace(from, len, ImplStr(sz));
2106 return *this;
2107 }
2108 // replaces the substring of length nLen starting at nStart
2109 wxString& replace(size_t nStart, size_t nLen, const wxString& str)
2110 {
2111 size_t from, len;
2112 PosLenToImpl(nStart, nLen, &from, &len);
2113 m_impl.replace(from, len, str.m_impl);
2114 return *this;
2115 }
2116 // replaces the substring with nCount copies of ch
2117 wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxUniChar ch)
2118 {
2119 size_t from, len;
2120 PosLenToImpl(nStart, nLen, &from, &len);
2121 #if wxUSE_UNICODE_UTF8
2122 if ( !ch.IsAscii() )
2123 m_impl.replace(from, len, wxStringOperations::EncodeNChars(nCount, ch));
2124 else
2125 #endif
2126 m_impl.replace(from, len, nCount, (wxStringCharType)ch);
2127 return *this;
2128 }
2129 // replaces a substring with another substring
2130 wxString& replace(size_t nStart, size_t nLen,
2131 const wxString& str, size_t nStart2, size_t nLen2)
2132 {
2133 size_t from, len;
2134 PosLenToImpl(nStart, nLen, &from, &len);
2135
2136 size_t from2, len2;
2137 str.PosLenToImpl(nStart2, nLen2, &from2, &len2);
2138
2139 m_impl.replace(from, len, str.m_impl, from2, len2);
2140 return *this;
2141 }
2142 // replaces the substring with first nCount chars of sz
2143 wxString& replace(size_t nStart, size_t nLen,
2144 const char* sz, size_t nCount)
2145 {
2146 size_t from, len;
2147 PosLenToImpl(nStart, nLen, &from, &len);
2148
2149 SubstrBufFromMB str(ImplStr(sz, nCount));
2150
2151 m_impl.replace(from, len, str.data, str.len);
2152 return *this;
2153 }
2154 wxString& replace(size_t nStart, size_t nLen,
2155 const wchar_t* sz, size_t nCount)
2156 {
2157 size_t from, len;
2158 PosLenToImpl(nStart, nLen, &from, &len);
2159
2160 SubstrBufFromWC str(ImplStr(sz, nCount));
2161
2162 m_impl.replace(from, len, str.data, str.len);
2163 return *this;
2164 }
2165 wxString& replace(size_t nStart, size_t nLen,
2166 const wxString& s, size_t nCount)
2167 {
2168 size_t from, len;
2169 PosLenToImpl(nStart, nLen, &from, &len);
2170 m_impl.replace(from, len, s.m_impl.c_str(), s.LenToImpl(nCount));
2171 return *this;
2172 }
2173
2174 wxString& replace(iterator first, iterator last, const char* s)
2175 { m_impl.replace(first.impl(), last.impl(), ImplStr(s)); return *this; }
2176 wxString& replace(iterator first, iterator last, const wchar_t* s)
2177 { m_impl.replace(first.impl(), last.impl(), ImplStr(s)); return *this; }
2178 wxString& replace(iterator first, iterator last, const char* s, size_type n)
2179 {
2180 SubstrBufFromMB str(ImplStr(s, n));
2181 m_impl.replace(first.impl(), last.impl(), str.data, str.len);
2182 return *this;
2183 }
2184 wxString& replace(iterator first, iterator last, const wchar_t* s, size_type n)
2185 {
2186 SubstrBufFromWC str(ImplStr(s, n));
2187 m_impl.replace(first.impl(), last.impl(), str.data, str.len);
2188 return *this;
2189 }
2190 wxString& replace(iterator first, iterator last, const wxString& s)
2191 { m_impl.replace(first.impl(), last.impl(), s.m_impl); return *this; }
2192 wxString& replace(iterator first, iterator last, size_type n, wxUniChar ch)
2193 {
2194 #if wxUSE_UNICODE_UTF8
2195 if ( !ch.IsAscii() )
2196 m_impl.replace(first.impl(), last.impl(),
2197 wxStringOperations::EncodeNChars(n, ch));
2198 else
2199 #endif
2200 m_impl.replace(first.impl(), last.impl(), n, (wxStringCharType)ch);
2201 return *this;
2202 }
2203 wxString& replace(iterator first, iterator last,
2204 const_iterator first1, const_iterator last1)
2205 {
2206 m_impl.replace(first.impl(), last.impl(), first1.impl(), last1.impl());
2207 return *this;
2208 }
2209 wxString& replace(iterator first, iterator last,
2210 const char *first1, const char *last1)
2211 { replace(first, last, first1, last1 - first1); return *this; }
2212 wxString& replace(iterator first, iterator last,
2213 const wchar_t *first1, const wchar_t *last1)
2214 { replace(first, last, first1, last1 - first1); return *this; }
2215
2216 // swap two strings
2217 void swap(wxString& str)
2218 { m_impl.swap(str.m_impl); }
2219
2220 // find a substring
2221 size_t find(const wxString& str, size_t nStart = 0) const
2222 { return PosFromImpl(m_impl.find(str.m_impl, PosToImpl(nStart))); }
2223
2224 // find first n characters of sz
2225 size_t find(const char* sz, size_t nStart = 0, size_t n = npos) const
2226 {
2227 SubstrBufFromMB str(ImplStr(sz, n));
2228 return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len));
2229 }
2230 size_t find(const wchar_t* sz, size_t nStart = 0, size_t n = npos) const
2231 {
2232 SubstrBufFromWC str(ImplStr(sz, n));
2233 return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len));
2234 }
2235 size_t find(const wxCharBuffer& s, size_t nStart = 0, size_t n = npos) const
2236 { return find(s.data(), nStart, n); }
2237 size_t find(const wxWCharBuffer& s, size_t nStart = 0, size_t n = npos) const
2238 { return find(s.data(), nStart, n); }
2239 size_t find(const wxCStrData& s, size_t nStart = 0, size_t n = npos) const
2240 { return find(s.AsWChar(), nStart, n); }
2241
2242 // find the first occurence of character ch after nStart
2243 size_t find(wxUniChar ch, size_t nStart = 0) const
2244 {
2245 return PosFromImpl(m_impl.find(wxStringOperations::EncodeChar(ch),
2246 PosToImpl(nStart)));
2247 }
2248 size_t find(wxUniCharRef ch, size_t nStart = 0) const
2249 { return find(wxUniChar(ch), nStart); }
2250 size_t find(char ch, size_t nStart = 0) const
2251 { return find(wxUniChar(ch), nStart); }
2252 size_t find(unsigned char ch, size_t nStart = 0) const
2253 { return find(wxUniChar(ch), nStart); }
2254 size_t find(wchar_t ch, size_t nStart = 0) const
2255 { return find(wxUniChar(ch), nStart); }
2256
2257 // rfind() family is exactly like find() but works right to left
2258
2259 // as find, but from the end
2260 size_t rfind(const wxString& str, size_t nStart = npos) const
2261 { return PosFromImpl(m_impl.rfind(str.m_impl, PosToImpl(nStart))); }
2262
2263 // as find, but from the end
2264 size_t rfind(const char* sz, size_t nStart = npos, size_t n = npos) const
2265 {
2266 SubstrBufFromMB str(ImplStr(sz, n));
2267 return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len));
2268 }
2269 size_t rfind(const wchar_t* sz, size_t nStart = npos, size_t n = npos) const
2270 {
2271 SubstrBufFromWC str(ImplStr(sz, n));
2272 return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len));
2273 }
2274 size_t rfind(const wxCharBuffer& s, size_t nStart = npos, size_t n = npos) const
2275 { return rfind(s.data(), nStart, n); }
2276 size_t rfind(const wxWCharBuffer& s, size_t nStart = npos, size_t n = npos) const
2277 { return rfind(s.data(), nStart, n); }
2278 size_t rfind(const wxCStrData& s, size_t nStart = npos, size_t n = npos) const
2279 { return rfind(s.AsWChar(), nStart, n); }
2280 // as find, but from the end
2281 size_t rfind(wxUniChar ch, size_t nStart = npos) const
2282 {
2283 return PosFromImpl(m_impl.rfind(wxStringOperations::EncodeChar(ch),
2284 PosToImpl(nStart)));
2285 }
2286 size_t rfind(wxUniCharRef ch, size_t nStart = npos) const
2287 { return rfind(wxUniChar(ch), nStart); }
2288 size_t rfind(char ch, size_t nStart = npos) const
2289 { return rfind(wxUniChar(ch), nStart); }
2290 size_t rfind(unsigned char ch, size_t nStart = npos) const
2291 { return rfind(wxUniChar(ch), nStart); }
2292 size_t rfind(wchar_t ch, size_t nStart = npos) const
2293 { return rfind(wxUniChar(ch), nStart); }
2294
2295 // find first/last occurence of any character (not) in the set:
2296 #if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2297 // FIXME-UTF8: this is not entirely correct, because it doesn't work if
2298 // sizeof(wchar_t)==2 and surrogates are present in the string;
2299 // should we care? Probably not.
2300 size_t find_first_of(const wxString& str, size_t nStart = 0) const
2301 { return m_impl.find_first_of(str.m_impl, nStart); }
2302 size_t find_first_of(const char* sz, size_t nStart = 0) const
2303 { return m_impl.find_first_of(ImplStr(sz), nStart); }
2304 size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const
2305 { return m_impl.find_first_of(ImplStr(sz), nStart); }
2306 size_t find_first_of(const char* sz, size_t nStart, size_t n) const
2307 { return m_impl.find_first_of(ImplStr(sz), nStart, n); }
2308 size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const
2309 { return m_impl.find_first_of(ImplStr(sz), nStart, n); }
2310 size_t find_first_of(wxUniChar c, size_t nStart = 0) const
2311 { return m_impl.find_first_of((wxChar)c, nStart); }
2312
2313 size_t find_last_of(const wxString& str, size_t nStart = npos) const
2314 { return m_impl.find_last_of(str.m_impl, nStart); }
2315 size_t find_last_of(const char* sz, size_t nStart = npos) const
2316 { return m_impl.find_last_of(ImplStr(sz), nStart); }
2317 size_t find_last_of(const wchar_t* sz, size_t nStart = npos) const
2318 { return m_impl.find_last_of(ImplStr(sz), nStart); }
2319 size_t find_last_of(const char* sz, size_t nStart, size_t n) const
2320 { return m_impl.find_last_of(ImplStr(sz), nStart, n); }
2321 size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const
2322 { return m_impl.find_last_of(ImplStr(sz), nStart, n); }
2323 size_t find_last_of(wxUniChar c, size_t nStart = npos) const
2324 { return m_impl.find_last_of((wxChar)c, nStart); }
2325
2326 size_t find_first_not_of(const wxString& str, size_t nStart = 0) const
2327 { return m_impl.find_first_not_of(str.m_impl, nStart); }
2328 size_t find_first_not_of(const char* sz, size_t nStart = 0) const
2329 { return m_impl.find_first_not_of(ImplStr(sz), nStart); }
2330 size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const
2331 { return m_impl.find_first_not_of(ImplStr(sz), nStart); }
2332 size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const
2333 { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); }
2334 size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const
2335 { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); }
2336 size_t find_first_not_of(wxUniChar c, size_t nStart = 0) const
2337 { return m_impl.find_first_not_of((wxChar)c, nStart); }
2338
2339 size_t find_last_not_of(const wxString& str, size_t nStart = npos) const
2340 { return m_impl.find_last_not_of(str.m_impl, nStart); }
2341 size_t find_last_not_of(const char* sz, size_t nStart = npos) const
2342 { return m_impl.find_last_not_of(ImplStr(sz), nStart); }
2343 size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const
2344 { return m_impl.find_last_not_of(ImplStr(sz), nStart); }
2345 size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const
2346 { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); }
2347 size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const
2348 { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); }
2349 size_t find_last_not_of(wxUniChar c, size_t nStart = npos) const
2350 { return m_impl.find_last_not_of((wxChar)c, nStart); }
2351 #else
2352 // we can't use std::string implementation in UTF-8 build, because the
2353 // character sets would be interpreted wrongly:
2354
2355 // as strpbrk() but starts at nStart, returns npos if not found
2356 size_t find_first_of(const wxString& str, size_t nStart = 0) const
2357 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2358 { return find_first_of(str.wc_str(), nStart); }
2359 #else
2360 { return find_first_of(str.mb_str(), nStart); }
2361 #endif
2362 // same as above
2363 size_t find_first_of(const char* sz, size_t nStart = 0) const;
2364 size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const;
2365 size_t find_first_of(const char* sz, size_t nStart, size_t n) const;
2366 size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const;
2367 // same as find(char, size_t)
2368 size_t find_first_of(wxUniChar c, size_t nStart = 0) const
2369 { return find(c, nStart); }
2370 // find the last (starting from nStart) char from str in this string
2371 size_t find_last_of (const wxString& str, size_t nStart = npos) const
2372 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2373 { return find_last_of(str.wc_str(), nStart); }
2374 #else
2375 { return find_last_of(str.mb_str(), nStart); }
2376 #endif
2377 // same as above
2378 size_t find_last_of (const char* sz, size_t nStart = npos) const;
2379 size_t find_last_of (const wchar_t* sz, size_t nStart = npos) const;
2380 size_t find_last_of(const char* sz, size_t nStart, size_t n) const;
2381 size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const;
2382 // same as above
2383 size_t find_last_of(wxUniChar c, size_t nStart = npos) const
2384 { return rfind(c, nStart); }
2385
2386 // find first/last occurence of any character not in the set
2387
2388 // as strspn() (starting from nStart), returns npos on failure
2389 size_t find_first_not_of(const wxString& str, size_t nStart = 0) const
2390 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2391 { return find_first_not_of(str.wc_str(), nStart); }
2392 #else
2393 { return find_first_not_of(str.mb_str(), nStart); }
2394 #endif
2395 // same as above
2396 size_t find_first_not_of(const char* sz, size_t nStart = 0) const;
2397 size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const;
2398 size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const;
2399 size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const;
2400 // same as above
2401 size_t find_first_not_of(wxUniChar ch, size_t nStart = 0) const;
2402 // as strcspn()
2403 size_t find_last_not_of(const wxString& str, size_t nStart = npos) const
2404 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2405 { return find_last_not_of(str.wc_str(), nStart); }
2406 #else
2407 { return find_last_not_of(str.mb_str(), nStart); }
2408 #endif
2409 // same as above
2410 size_t find_last_not_of(const char* sz, size_t nStart = npos) const;
2411 size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const;
2412 size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const;
2413 size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const;
2414 // same as above
2415 size_t find_last_not_of(wxUniChar ch, size_t nStart = npos) const;
2416 #endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not
2417
2418 // provide char/wchar_t/wxUniCharRef overloads for char-finding functions
2419 // above to resolve ambiguities:
2420 size_t find_first_of(wxUniCharRef ch, size_t nStart = 0) const
2421 { return find_first_of(wxUniChar(ch), nStart); }
2422 size_t find_first_of(char ch, size_t nStart = 0) const
2423 { return find_first_of(wxUniChar(ch), nStart); }
2424 size_t find_first_of(unsigned char ch, size_t nStart = 0) const
2425 { return find_first_of(wxUniChar(ch), nStart); }
2426 size_t find_first_of(wchar_t ch, size_t nStart = 0) const
2427 { return find_first_of(wxUniChar(ch), nStart); }
2428 size_t find_last_of(wxUniCharRef ch, size_t nStart = npos) const
2429 { return find_last_of(wxUniChar(ch), nStart); }
2430 size_t find_last_of(char ch, size_t nStart = npos) const
2431 { return find_last_of(wxUniChar(ch), nStart); }
2432 size_t find_last_of(unsigned char ch, size_t nStart = npos) const
2433 { return find_last_of(wxUniChar(ch), nStart); }
2434 size_t find_last_of(wchar_t ch, size_t nStart = npos) const
2435 { return find_last_of(wxUniChar(ch), nStart); }
2436 size_t find_first_not_of(wxUniCharRef ch, size_t nStart = 0) const
2437 { return find_first_not_of(wxUniChar(ch), nStart); }
2438 size_t find_first_not_of(char ch, size_t nStart = 0) const
2439 { return find_first_not_of(wxUniChar(ch), nStart); }
2440 size_t find_first_not_of(unsigned char ch, size_t nStart = 0) const
2441 { return find_first_not_of(wxUniChar(ch), nStart); }
2442 size_t find_first_not_of(wchar_t ch, size_t nStart = 0) const
2443 { return find_first_not_of(wxUniChar(ch), nStart); }
2444 size_t find_last_not_of(wxUniCharRef ch, size_t nStart = npos) const
2445 { return find_last_not_of(wxUniChar(ch), nStart); }
2446 size_t find_last_not_of(char ch, size_t nStart = npos) const
2447 { return find_last_not_of(wxUniChar(ch), nStart); }
2448 size_t find_last_not_of(unsigned char ch, size_t nStart = npos) const
2449 { return find_last_not_of(wxUniChar(ch), nStart); }
2450 size_t find_last_not_of(wchar_t ch, size_t nStart = npos) const
2451 { return find_last_not_of(wxUniChar(ch), nStart); }
2452
2453 // and additional overloads for the versions taking strings:
2454 size_t find_first_of(const wxCStrData& sz, size_t nStart = 0) const
2455 { return find_first_of(sz.AsString(), nStart); }
2456 size_t find_first_of(const wxCharBuffer& sz, size_t nStart = 0) const
2457 { return find_first_of(sz.data(), nStart); }
2458 size_t find_first_of(const wxWCharBuffer& sz, size_t nStart = 0) const
2459 { return find_first_of(sz.data(), nStart); }
2460 size_t find_first_of(const wxCStrData& sz, size_t nStart, size_t n) const
2461 { return find_first_of(sz.AsWChar(), nStart, n); }
2462 size_t find_first_of(const wxCharBuffer& sz, size_t nStart, size_t n) const
2463 { return find_first_of(sz.data(), nStart, n); }
2464 size_t find_first_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const
2465 { return find_first_of(sz.data(), nStart, n); }
2466
2467 size_t find_last_of(const wxCStrData& sz, size_t nStart = 0) const
2468 { return find_last_of(sz.AsString(), nStart); }
2469 size_t find_last_of(const wxCharBuffer& sz, size_t nStart = 0) const
2470 { return find_last_of(sz.data(), nStart); }
2471 size_t find_last_of(const wxWCharBuffer& sz, size_t nStart = 0) const
2472 { return find_last_of(sz.data(), nStart); }
2473 size_t find_last_of(const wxCStrData& sz, size_t nStart, size_t n) const
2474 { return find_last_of(sz.AsWChar(), nStart, n); }
2475 size_t find_last_of(const wxCharBuffer& sz, size_t nStart, size_t n) const
2476 { return find_last_of(sz.data(), nStart, n); }
2477 size_t find_last_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const
2478 { return find_last_of(sz.data(), nStart, n); }
2479
2480 size_t find_first_not_of(const wxCStrData& sz, size_t nStart = 0) const
2481 { return find_first_not_of(sz.AsString(), nStart); }
2482 size_t find_first_not_of(const wxCharBuffer& sz, size_t nStart = 0) const
2483 { return find_first_not_of(sz.data(), nStart); }
2484 size_t find_first_not_of(const wxWCharBuffer& sz, size_t nStart = 0) const
2485 { return find_first_not_of(sz.data(), nStart); }
2486 size_t find_first_not_of(const wxCStrData& sz, size_t nStart, size_t n) const
2487 { return find_first_not_of(sz.AsWChar(), nStart, n); }
2488 size_t find_first_not_of(const wxCharBuffer& sz, size_t nStart, size_t n) const
2489 { return find_first_not_of(sz.data(), nStart, n); }
2490 size_t find_first_not_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const
2491 { return find_first_not_of(sz.data(), nStart, n); }
2492
2493 size_t find_last_not_of(const wxCStrData& sz, size_t nStart = 0) const
2494 { return find_last_not_of(sz.AsString(), nStart); }
2495 size_t find_last_not_of(const wxCharBuffer& sz, size_t nStart = 0) const
2496 { return find_last_not_of(sz.data(), nStart); }
2497 size_t find_last_not_of(const wxWCharBuffer& sz, size_t nStart = 0) const
2498 { return find_last_not_of(sz.data(), nStart); }
2499 size_t find_last_not_of(const wxCStrData& sz, size_t nStart, size_t n) const
2500 { return find_last_not_of(sz.AsWChar(), nStart, n); }
2501 size_t find_last_not_of(const wxCharBuffer& sz, size_t nStart, size_t n) const
2502 { return find_last_not_of(sz.data(), nStart, n); }
2503 size_t find_last_not_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const
2504 { return find_last_not_of(sz.data(), nStart, n); }
2505
2506 // string += string
2507 wxString& operator+=(const wxString& s)
2508 { m_impl += s.m_impl; return *this; }
2509 // string += C string
2510 wxString& operator+=(const char *psz)
2511 { m_impl += ImplStr(psz); return *this; }
2512 wxString& operator+=(const wchar_t *pwz)
2513 { m_impl += ImplStr(pwz); return *this; }
2514 wxString& operator+=(const wxCStrData& s)
2515 { m_impl += s.AsString().m_impl; return *this; }
2516 wxString& operator+=(const wxCharBuffer& s)
2517 { return operator+=(s.data()); }
2518 wxString& operator+=(const wxWCharBuffer& s)
2519 { return operator+=(s.data()); }
2520 // string += char
2521 wxString& operator+=(wxUniChar ch)
2522 { m_impl += wxStringOperations::EncodeChar(ch); return *this; }
2523 wxString& operator+=(wxUniCharRef ch) { return *this += wxUniChar(ch); }
2524 wxString& operator+=(int ch) { return *this += wxUniChar(ch); }
2525 wxString& operator+=(char ch) { return *this += wxUniChar(ch); }
2526 wxString& operator+=(unsigned char ch) { return *this += wxUniChar(ch); }
2527 wxString& operator+=(wchar_t ch) { return *this += wxUniChar(ch); }
2528
2529 private:
2530 #if !wxUSE_STL_BASED_WXSTRING
2531 // helpers for wxStringBuffer and wxStringBufferLength
2532 wxStringCharType *DoGetWriteBuf(size_t nLen)
2533 { return m_impl.DoGetWriteBuf(nLen); }
2534 void DoUngetWriteBuf()
2535 { m_impl.DoUngetWriteBuf(); }
2536 void DoUngetWriteBuf(size_t nLen)
2537 { m_impl.DoUngetWriteBuf(nLen); }
2538 #endif // !wxUSE_STL_BASED_WXSTRING
2539
2540 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2541 #if !wxUSE_UTF8_LOCALE_ONLY
2542 int DoPrintfWchar(const wxChar *format, ...);
2543 static wxString DoFormatWchar(const wxChar *format, ...);
2544 #endif
2545 #if wxUSE_UNICODE_UTF8
2546 int DoPrintfUtf8(const char *format, ...);
2547 static wxString DoFormatUtf8(const char *format, ...);
2548 #endif
2549 #endif
2550
2551 #if !wxUSE_STL_BASED_WXSTRING
2552 // check string's data validity
2553 bool IsValid() const { return m_impl.GetStringData()->IsValid(); }
2554 #endif
2555
2556 private:
2557 wxStringImpl m_impl;
2558
2559 // buffers for compatibility conversion from (char*)c_str() and
2560 // (wchar_t*)c_str():
2561 // FIXME-UTF8: bechmark various approaches to keeping compatibility buffers
2562 template<typename T>
2563 struct ConvertedBuffer
2564 {
2565 ConvertedBuffer() : m_buf(NULL) {}
2566 ~ConvertedBuffer()
2567 { free(m_buf); }
2568
2569 operator T*() const { return m_buf; }
2570
2571 ConvertedBuffer& operator=(T *str)
2572 {
2573 free(m_buf);
2574 m_buf = str;
2575 return *this;
2576 }
2577
2578 T *m_buf;
2579 };
2580 #if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
2581 ConvertedBuffer<char> m_convertedToChar;
2582 #endif
2583 #if !wxUSE_UNICODE_WCHAR
2584 ConvertedBuffer<wchar_t> m_convertedToWChar;
2585 #endif
2586
2587 #if wxUSE_UNICODE_UTF8
2588 // FIXME-UTF8: (try to) move this elsewhere (TLS) or solve differently
2589 // assigning to character pointer to by wxString::interator may
2590 // change the underlying wxStringImpl iterator, so we have to
2591 // keep track of all iterators and update them as necessary:
2592 struct wxStringIteratorNodeHead
2593 {
2594 wxStringIteratorNodeHead() : ptr(NULL) {}
2595 wxStringIteratorNode *ptr;
2596
2597 // copying is disallowed as it would result in more than one pointer into
2598 // the same linked list
2599 DECLARE_NO_COPY_CLASS(wxStringIteratorNodeHead)
2600 };
2601
2602 wxStringIteratorNodeHead m_iterators;
2603
2604 friend class WXDLLIMPEXP_FWD_BASE wxStringIteratorNode;
2605 friend class WXDLLIMPEXP_FWD_BASE wxUniCharRef;
2606 #endif // wxUSE_UNICODE_UTF8
2607
2608 friend class WXDLLIMPEXP_FWD_BASE wxCStrData;
2609 friend class wxStringInternalBuffer;
2610 friend class wxStringInternalBufferLength;
2611 };
2612
2613 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
2614 #pragma warning (default:4275)
2615 #endif
2616
2617 // string iterator operators that satisfy STL Random Access Iterator
2618 // requirements:
2619 inline wxString::iterator operator+(ptrdiff_t n, wxString::iterator i)
2620 { return i + n; }
2621 inline wxString::const_iterator operator+(ptrdiff_t n, wxString::const_iterator i)
2622 { return i + n; }
2623 inline wxString::reverse_iterator operator+(ptrdiff_t n, wxString::reverse_iterator i)
2624 { return i + n; }
2625 inline wxString::const_reverse_iterator operator+(ptrdiff_t n, wxString::const_reverse_iterator i)
2626 { return i + n; }
2627
2628 // notice that even though for many compilers the friend declarations above are
2629 // enough, from the point of view of C++ standard we must have the declarations
2630 // here as friend ones are not injected in the enclosing namespace and without
2631 // them the code fails to compile with conforming compilers such as xlC or g++4
2632 wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2);
2633 wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const char *psz);
2634 wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wchar_t *pwz);
2635 wxString WXDLLIMPEXP_BASE operator+(const char *psz, const wxString& string);
2636 wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz, const wxString& string);
2637
2638 wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch);
2639 wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string);
2640
2641 inline wxString operator+(const wxString& string, wxUniCharRef ch)
2642 { return string + (wxUniChar)ch; }
2643 inline wxString operator+(const wxString& string, char ch)
2644 { return string + wxUniChar(ch); }
2645 inline wxString operator+(const wxString& string, wchar_t ch)
2646 { return string + wxUniChar(ch); }
2647 inline wxString operator+(wxUniCharRef ch, const wxString& string)
2648 { return (wxUniChar)ch + string; }
2649 inline wxString operator+(char ch, const wxString& string)
2650 { return wxUniChar(ch) + string; }
2651 inline wxString operator+(wchar_t ch, const wxString& string)
2652 { return wxUniChar(ch) + string; }
2653
2654
2655 #define wxGetEmptyString() wxString()
2656
2657 // ----------------------------------------------------------------------------
2658 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
2659 // ----------------------------------------------------------------------------
2660
2661 #if !wxUSE_STL_BASED_WXSTRING
2662 // string buffer for direct access to string data in their native
2663 // representation:
2664 class wxStringInternalBuffer
2665 {
2666 public:
2667 typedef wxStringCharType CharType;
2668
2669 wxStringInternalBuffer(wxString& str, size_t lenWanted = 1024)
2670 : m_str(str), m_buf(NULL)
2671 { m_buf = m_str.DoGetWriteBuf(lenWanted); }
2672
2673 ~wxStringInternalBuffer() { m_str.DoUngetWriteBuf(); }
2674
2675 operator wxStringCharType*() const { return m_buf; }
2676
2677 private:
2678 wxString& m_str;
2679 wxStringCharType *m_buf;
2680
2681 DECLARE_NO_COPY_CLASS(wxStringInternalBuffer)
2682 };
2683
2684 class wxStringInternalBufferLength
2685 {
2686 public:
2687 typedef wxStringCharType CharType;
2688
2689 wxStringInternalBufferLength(wxString& str, size_t lenWanted = 1024)
2690 : m_str(str), m_buf(NULL), m_len(0), m_lenSet(false)
2691 {
2692 m_buf = m_str.DoGetWriteBuf(lenWanted);
2693 wxASSERT(m_buf != NULL);
2694 }
2695
2696 ~wxStringInternalBufferLength()
2697 {
2698 wxASSERT(m_lenSet);
2699 m_str.DoUngetWriteBuf(m_len);
2700 }
2701
2702 operator wxStringCharType*() const { return m_buf; }
2703 void SetLength(size_t length) { m_len = length; m_lenSet = true; }
2704
2705 private:
2706 wxString& m_str;
2707 wxStringCharType *m_buf;
2708 size_t m_len;
2709 bool m_lenSet;
2710
2711 DECLARE_NO_COPY_CLASS(wxStringInternalBufferLength)
2712 };
2713
2714 #endif // !wxUSE_STL_BASED_WXSTRING
2715
2716 template<typename T>
2717 class WXDLLIMPEXP_BASE wxStringTypeBufferBase
2718 {
2719 public:
2720 typedef T CharType;
2721
2722 wxStringTypeBufferBase(wxString& str, size_t lenWanted = 1024)
2723 : m_str(str), m_buf(lenWanted)
2724 { }
2725
2726
2727 operator CharType*() { return m_buf.data(); }
2728
2729 protected:
2730 wxString& m_str;
2731 wxCharTypeBuffer<CharType> m_buf;
2732 };
2733
2734 template<typename T>
2735 class WXDLLIMPEXP_BASE wxStringTypeBufferLengthBase
2736 {
2737 public:
2738 typedef T CharType;
2739
2740 wxStringTypeBufferLengthBase(wxString& str, size_t lenWanted = 1024)
2741 : m_str(str), m_buf(lenWanted), m_len(0), m_lenSet(false)
2742 { }
2743
2744 operator CharType*() { return m_buf.data(); }
2745 void SetLength(size_t length) { m_len = length; m_lenSet = true; }
2746
2747 protected:
2748 wxString& m_str;
2749 wxCharTypeBuffer<CharType> m_buf;
2750 size_t m_len;
2751 bool m_lenSet;
2752 };
2753
2754 template<typename T>
2755 class wxStringTypeBuffer : public wxStringTypeBufferBase<T>
2756 {
2757 public:
2758 wxStringTypeBuffer(wxString& str, size_t lenWanted = 1024)
2759 : wxStringTypeBufferBase<T>(str, lenWanted) {}
2760 ~wxStringTypeBuffer()
2761 {
2762 this->m_str.assign(this->m_buf.data());
2763 }
2764
2765 DECLARE_NO_COPY_CLASS(wxStringTypeBuffer)
2766 };
2767
2768 template<typename T>
2769 class wxStringTypeBufferLength : public wxStringTypeBufferLengthBase<T>
2770 {
2771 public:
2772 wxStringTypeBufferLength(wxString& str, size_t lenWanted = 1024)
2773 : wxStringTypeBufferLengthBase<T>(str, lenWanted) {}
2774
2775 ~wxStringTypeBufferLength()
2776 {
2777 wxASSERT(this->m_lenSet);
2778 this->m_str.assign(this->m_buf.data(), this->m_len);
2779 }
2780
2781 DECLARE_NO_COPY_CLASS(wxStringTypeBufferLength)
2782 };
2783
2784 #if wxUSE_STL_BASED_WXSTRING
2785
2786 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase<wxStringCharType> )
2787
2788 class wxStringInternalBuffer : public wxStringTypeBufferBase<wxStringCharType>
2789 {
2790 public:
2791 wxStringInternalBuffer(wxString& str, size_t lenWanted = 1024)
2792 : wxStringTypeBufferBase<wxStringCharType>(str, lenWanted) {}
2793 ~wxStringInternalBuffer()
2794 { m_str.m_impl.assign(m_buf.data()); }
2795
2796 DECLARE_NO_COPY_CLASS(wxStringInternalBuffer)
2797 };
2798
2799 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE(
2800 wxStringTypeBufferLengthBase<wxStringCharType> )
2801
2802 class wxStringInternalBufferLength
2803 : public wxStringTypeBufferLengthBase<wxStringCharType>
2804 {
2805 public:
2806 wxStringInternalBufferLength(wxString& str, size_t lenWanted = 1024)
2807 : wxStringTypeBufferLengthBase<wxStringCharType>(str, lenWanted) {}
2808
2809 ~wxStringInternalBufferLength()
2810 {
2811 wxASSERT(m_lenSet);
2812 m_str.m_impl.assign(m_buf.data(), m_len);
2813 }
2814
2815 DECLARE_NO_COPY_CLASS(wxStringInternalBufferLength)
2816 };
2817 #endif // wxUSE_STL_BASED_WXSTRING
2818
2819
2820 #if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
2821 typedef wxStringTypeBuffer<wxChar> wxStringBuffer;
2822 typedef wxStringTypeBufferLength<wxChar> wxStringBufferLength;
2823 #else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2824 typedef wxStringInternalBuffer wxStringBuffer;
2825 typedef wxStringInternalBufferLength wxStringBufferLength;
2826 #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2827
2828 #if wxUSE_UNICODE_UTF8
2829 typedef wxStringInternalBuffer wxUTF8StringBuffer;
2830 typedef wxStringInternalBufferLength wxUTF8StringBufferLength;
2831 #elif wxUSE_UNICODE_WCHAR
2832
2833 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase<char> )
2834
2835 class WXDLLIMPEXP_BASE wxUTF8StringBuffer : public wxStringTypeBufferBase<char>
2836 {
2837 public:
2838 wxUTF8StringBuffer(wxString& str, size_t lenWanted = 1024)
2839 : wxStringTypeBufferBase<char>(str, lenWanted) {}
2840 ~wxUTF8StringBuffer();
2841
2842 DECLARE_NO_COPY_CLASS(wxUTF8StringBuffer)
2843 };
2844
2845 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferLengthBase<char> )
2846
2847 class WXDLLIMPEXP_BASE wxUTF8StringBufferLength
2848 : public wxStringTypeBufferLengthBase<char>
2849 {
2850 public:
2851 wxUTF8StringBufferLength(wxString& str, size_t lenWanted = 1024)
2852 : wxStringTypeBufferLengthBase<char>(str, lenWanted) {}
2853 ~wxUTF8StringBufferLength();
2854
2855 DECLARE_NO_COPY_CLASS(wxUTF8StringBufferLength)
2856 };
2857 #endif // wxUSE_UNICODE_UTF8/wxUSE_UNICODE_WCHAR
2858
2859
2860 // ---------------------------------------------------------------------------
2861 // wxString comparison functions: operator versions are always case sensitive
2862 // ---------------------------------------------------------------------------
2863
2864 #define wxCMP_WXCHAR_STRING(p, s, op) 0 op s.Cmp(p)
2865
2866 wxDEFINE_ALL_COMPARISONS(const wxChar *, const wxString&, wxCMP_WXCHAR_STRING)
2867
2868 #undef wxCMP_WXCHAR_STRING
2869
2870 inline bool operator==(const wxString& s1, const wxString& s2)
2871 { return s1.IsSameAs(s2); }
2872 inline bool operator!=(const wxString& s1, const wxString& s2)
2873 { return !s1.IsSameAs(s2); }
2874 inline bool operator< (const wxString& s1, const wxString& s2)
2875 { return s1.Cmp(s2) < 0; }
2876 inline bool operator> (const wxString& s1, const wxString& s2)
2877 { return s1.Cmp(s2) > 0; }
2878 inline bool operator<=(const wxString& s1, const wxString& s2)
2879 { return s1.Cmp(s2) <= 0; }
2880 inline bool operator>=(const wxString& s1, const wxString& s2)
2881 { return s1.Cmp(s2) >= 0; }
2882
2883 inline bool operator==(const wxString& s1, const wxCStrData& s2)
2884 { return s1 == s2.AsString(); }
2885 inline bool operator==(const wxCStrData& s1, const wxString& s2)
2886 { return s1.AsString() == s2; }
2887 inline bool operator!=(const wxString& s1, const wxCStrData& s2)
2888 { return s1 != s2.AsString(); }
2889 inline bool operator!=(const wxCStrData& s1, const wxString& s2)
2890 { return s1.AsString() != s2; }
2891
2892 inline bool operator==(const wxString& s1, const wxWCharBuffer& s2)
2893 { return (s1.Cmp((const wchar_t *)s2) == 0); }
2894 inline bool operator==(const wxWCharBuffer& s1, const wxString& s2)
2895 { return (s2.Cmp((const wchar_t *)s1) == 0); }
2896 inline bool operator!=(const wxString& s1, const wxWCharBuffer& s2)
2897 { return (s1.Cmp((const wchar_t *)s2) != 0); }
2898 inline bool operator!=(const wxWCharBuffer& s1, const wxString& s2)
2899 { return (s2.Cmp((const wchar_t *)s1) != 0); }
2900
2901 inline bool operator==(const wxString& s1, const wxCharBuffer& s2)
2902 { return (s1.Cmp((const char *)s2) == 0); }
2903 inline bool operator==(const wxCharBuffer& s1, const wxString& s2)
2904 { return (s2.Cmp((const char *)s1) == 0); }
2905 inline bool operator!=(const wxString& s1, const wxCharBuffer& s2)
2906 { return (s1.Cmp((const char *)s2) != 0); }
2907 inline bool operator!=(const wxCharBuffer& s1, const wxString& s2)
2908 { return (s2.Cmp((const char *)s1) != 0); }
2909
2910 inline wxString operator+(const wxString& string, const wxWCharBuffer& buf)
2911 { return string + (const wchar_t *)buf; }
2912 inline wxString operator+(const wxWCharBuffer& buf, const wxString& string)
2913 { return (const wchar_t *)buf + string; }
2914
2915 inline wxString operator+(const wxString& string, const wxCharBuffer& buf)
2916 { return string + (const char *)buf; }
2917 inline wxString operator+(const wxCharBuffer& buf, const wxString& string)
2918 { return (const char *)buf + string; }
2919
2920 // comparison with char
2921 inline bool operator==(const wxUniChar& c, const wxString& s) { return s.IsSameAs(c); }
2922 inline bool operator==(const wxUniCharRef& c, const wxString& s) { return s.IsSameAs(c); }
2923 inline bool operator==(char c, const wxString& s) { return s.IsSameAs(c); }
2924 inline bool operator==(wchar_t c, const wxString& s) { return s.IsSameAs(c); }
2925 inline bool operator==(int c, const wxString& s) { return s.IsSameAs(c); }
2926 inline bool operator==(const wxString& s, const wxUniChar& c) { return s.IsSameAs(c); }
2927 inline bool operator==(const wxString& s, const wxUniCharRef& c) { return s.IsSameAs(c); }
2928 inline bool operator==(const wxString& s, char c) { return s.IsSameAs(c); }
2929 inline bool operator==(const wxString& s, wchar_t c) { return s.IsSameAs(c); }
2930 inline bool operator!=(const wxUniChar& c, const wxString& s) { return !s.IsSameAs(c); }
2931 inline bool operator!=(const wxUniCharRef& c, const wxString& s) { return !s.IsSameAs(c); }
2932 inline bool operator!=(char c, const wxString& s) { return !s.IsSameAs(c); }
2933 inline bool operator!=(wchar_t c, const wxString& s) { return !s.IsSameAs(c); }
2934 inline bool operator!=(int c, const wxString& s) { return !s.IsSameAs(c); }
2935 inline bool operator!=(const wxString& s, const wxUniChar& c) { return !s.IsSameAs(c); }
2936 inline bool operator!=(const wxString& s, const wxUniCharRef& c) { return !s.IsSameAs(c); }
2937 inline bool operator!=(const wxString& s, char c) { return !s.IsSameAs(c); }
2938 inline bool operator!=(const wxString& s, wchar_t c) { return !s.IsSameAs(c); }
2939
2940 // comparison with C string in Unicode build
2941 #if wxUSE_UNICODE
2942
2943 #define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s
2944
2945 wxDEFINE_ALL_COMPARISONS(const char *, const wxString&, wxCMP_CHAR_STRING)
2946
2947 #undef wxCMP_CHAR_STRING
2948
2949 #endif // wxUSE_UNICODE
2950
2951 // we also need to provide the operators for comparison with wxCStrData to
2952 // resolve ambiguity between operator(const wxChar *,const wxString &) and
2953 // operator(const wxChar *, const wxChar *) for "p == s.c_str()"
2954 //
2955 // notice that these are (shallow) pointer comparisons, not (deep) string ones
2956 #define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar()
2957 #define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar()
2958
2959 wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData&, wxCMP_WCHAR_CSTRDATA)
2960 wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData&, wxCMP_CHAR_CSTRDATA)
2961
2962 #undef wxCMP_CHAR_CSTRDATA
2963 #undef wxCMP_WCHAR_CSTRDATA
2964
2965 // ---------------------------------------------------------------------------
2966 // Implementation only from here until the end of file
2967 // ---------------------------------------------------------------------------
2968
2969 #if wxUSE_STD_IOSTREAM
2970
2971 #include "wx/iosfwrap.h"
2972
2973 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxString&);
2974 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCStrData&);
2975 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCharBuffer&);
2976 #ifndef __BORLANDC__
2977 WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxWCharBuffer&);
2978 #endif
2979
2980 #if wxUSE_UNICODE && defined(HAVE_WOSTREAM)
2981
2982 WXDLLIMPEXP_BASE wxSTD wostream& operator<<(wxSTD wostream&, const wxString&);
2983 WXDLLIMPEXP_BASE wxSTD wostream& operator<<(wxSTD wostream&, const wxCStrData&);
2984 WXDLLIMPEXP_BASE wxSTD wostream& operator<<(wxSTD wostream&, const wxWCharBuffer&);
2985
2986 #endif // wxUSE_UNICODE && defined(HAVE_WOSTREAM)
2987
2988 #endif // wxUSE_STD_IOSTREAM
2989
2990 // ---------------------------------------------------------------------------
2991 // wxCStrData implementation
2992 // ---------------------------------------------------------------------------
2993
2994 inline wxCStrData::wxCStrData(char *buf)
2995 : m_str(new wxString(buf)), m_offset(0), m_owned(true) {}
2996 inline wxCStrData::wxCStrData(wchar_t *buf)
2997 : m_str(new wxString(buf)), m_offset(0), m_owned(true) {}
2998
2999 inline wxCStrData::wxCStrData(const wxCStrData& data)
3000 : m_str(data.m_owned ? new wxString(*data.m_str) : data.m_str),
3001 m_offset(data.m_offset),
3002 m_owned(data.m_owned)
3003 {
3004 }
3005
3006 inline wxCStrData::~wxCStrData()
3007 {
3008 if ( m_owned )
3009 delete wx_const_cast(wxString*, m_str); // cast to silence warnings
3010 }
3011
3012 // simple cases for AsChar() and AsWChar(), the complicated ones are
3013 // in string.cpp
3014 #if wxUSE_UNICODE_WCHAR
3015 inline const wchar_t* wxCStrData::AsWChar() const
3016 {
3017 return m_str->wx_str() + m_offset;
3018 }
3019 #endif // wxUSE_UNICODE_WCHAR
3020
3021 #if !wxUSE_UNICODE
3022 inline const char* wxCStrData::AsChar() const
3023 {
3024 return m_str->wx_str() + m_offset;
3025 }
3026 #endif // !wxUSE_UNICODE
3027
3028 #if wxUSE_UTF8_LOCALE_ONLY
3029 inline const char* wxCStrData::AsChar() const
3030 {
3031 return wxStringOperations::AddToIter(m_str->wx_str(), m_offset);
3032 }
3033 #endif // wxUSE_UTF8_LOCALE_ONLY
3034
3035 inline const wxCharBuffer wxCStrData::AsCharBuf() const
3036 {
3037 #if !wxUSE_UNICODE
3038 return wxCharBuffer::CreateNonOwned(AsChar());
3039 #else
3040 return AsString().mb_str();
3041 #endif
3042 }
3043
3044 inline const wxWCharBuffer wxCStrData::AsWCharBuf() const
3045 {
3046 #if wxUSE_UNICODE_WCHAR
3047 return wxWCharBuffer::CreateNonOwned(AsWChar());
3048 #else
3049 return AsString().wc_str();
3050 #endif
3051 }
3052
3053 inline wxString wxCStrData::AsString() const
3054 {
3055 if ( m_offset == 0 )
3056 return *m_str;
3057 else
3058 return m_str->Mid(m_offset);
3059 }
3060
3061 inline const wxStringCharType *wxCStrData::AsInternal() const
3062 {
3063 #if wxUSE_UNICODE_UTF8
3064 return wxStringOperations::AddToIter(m_str->wx_str(), m_offset);
3065 #else
3066 return m_str->wx_str() + m_offset;
3067 #endif
3068 }
3069
3070 inline wxUniChar wxCStrData::operator*() const
3071 {
3072 if ( m_str->empty() )
3073 return wxUniChar(_T('\0'));
3074 else
3075 return (*m_str)[m_offset];
3076 }
3077
3078 inline wxUniChar wxCStrData::operator[](size_t n) const
3079 {
3080 // NB: we intentionally use operator[] and not at() here because the former
3081 // works for the terminating NUL while the latter does not
3082 return (*m_str)[m_offset + n];
3083 }
3084
3085 // ----------------------------------------------------------------------------
3086 // more wxCStrData operators
3087 // ----------------------------------------------------------------------------
3088
3089 // we need to define those to allow "size_t pos = p - s.c_str()" where p is
3090 // some pointer into the string
3091 inline size_t operator-(const char *p, const wxCStrData& cs)
3092 {
3093 return p - cs.AsChar();
3094 }
3095
3096 inline size_t operator-(const wchar_t *p, const wxCStrData& cs)
3097 {
3098 return p - cs.AsWChar();
3099 }
3100
3101 // ----------------------------------------------------------------------------
3102 // implementation of wx[W]CharBuffer inline methods using wxCStrData
3103 // ----------------------------------------------------------------------------
3104
3105 // FIXME-UTF8: move this to buffer.h
3106 inline wxCharBuffer::wxCharBuffer(const wxCStrData& cstr)
3107 : wxCharTypeBufferBase(cstr.AsCharBuf())
3108 {
3109 }
3110
3111 inline wxWCharBuffer::wxWCharBuffer(const wxCStrData& cstr)
3112 : wxCharTypeBufferBase(cstr.AsWCharBuf())
3113 {
3114 }
3115
3116 #if wxUSE_UNICODE_UTF8
3117 // ----------------------------------------------------------------------------
3118 // implementation of wxStringIteratorNode inline methods
3119 // ----------------------------------------------------------------------------
3120
3121 void wxStringIteratorNode::DoSet(const wxString *str,
3122 wxStringImpl::const_iterator *citer,
3123 wxStringImpl::iterator *iter)
3124 {
3125 m_prev = NULL;
3126 m_iter = iter;
3127 m_citer = citer;
3128 m_str = str;
3129 if ( str )
3130 {
3131 m_next = str->m_iterators.ptr;
3132 wx_const_cast(wxString*, m_str)->m_iterators.ptr = this;
3133 if ( m_next )
3134 m_next->m_prev = this;
3135 }
3136 else
3137 {
3138 m_next = NULL;
3139 }
3140 }
3141
3142 void wxStringIteratorNode::clear()
3143 {
3144 if ( m_next )
3145 m_next->m_prev = m_prev;
3146 if ( m_prev )
3147 m_prev->m_next = m_next;
3148 else if ( m_str ) // first in the list
3149 wx_const_cast(wxString*, m_str)->m_iterators.ptr = m_next;
3150
3151 m_next = m_prev = NULL;
3152 m_citer = NULL;
3153 m_iter = NULL;
3154 m_str = NULL;
3155 }
3156 #endif // wxUSE_UNICODE_UTF8
3157
3158 #if WXWIN_COMPATIBILITY_2_8
3159 // lot of code out there doesn't explicitly include wx/crt.h, but uses
3160 // CRT wrappers that are now declared in wx/wxcrt.h and wx/wxcrtvararg.h,
3161 // so let's include this header now that wxString is defined and it's safe
3162 // to do it:
3163 #include "wx/crt.h"
3164 #endif
3165
3166 #endif // _WX_WXSTRING_H_