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