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