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