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