]> git.saurik.com Git - wxWidgets.git/blame - include/wx/string.h
use bool expression rather than implicit conversion
[wxWidgets.git] / include / wx / string.h
CommitLineData
3c67202d 1///////////////////////////////////////////////////////////////////////////////
1c2d1459 2// Name: wx/string.h
a7ea63e2 3// Purpose: wxString class
c801d85f
KB
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>
65571936 9// Licence: wxWindows licence
3c67202d 10///////////////////////////////////////////////////////////////////////////////
c801d85f 11
e90c1d2a
VZ
12/*
13 Efficient string class [more or less] compatible with MFC CString,
77ffb593 14 wxWidgets version 1 wxString and std::string and some handy functions
e90c1d2a
VZ
15 missing from string.h.
16*/
17
a7ea63e2
VS
18#ifndef _WX_WXSTRING_H__
19#define _WX_WXSTRING_H__
c801d85f 20
e90c1d2a
VZ
21// ----------------------------------------------------------------------------
22// headers
23// ----------------------------------------------------------------------------
24
5737d05f
VZ
25#include "wx/defs.h" // everybody should include this
26
9b4da627 27#ifndef __WXPALMOS5__
9dea36ef 28#if defined(__WXMAC__) || defined(__VISAGECPP__)
3f4a0c5b 29 #include <ctype.h>
17dff81c 30#endif
3f4a0c5b 31
9dea36ef
DW
32#if defined(__VISAGECPP__) && __IBMCPP__ >= 400
33 // problem in VACPP V4 with including stdlib.h multiple times
34 // strconv includes it anyway
35# include <stdio.h>
36# include <string.h>
37# include <stdarg.h>
38# include <limits.h>
39#else
40# include <string.h>
41# include <stdio.h>
42# include <stdarg.h>
43# include <limits.h>
44# include <stdlib.h>
45#endif
c801d85f 46
1c2d1459 47#ifdef HAVE_STRCASECMP_IN_STRINGS_H
1bfcb0b6 48 #include <strings.h> // for strcasecmp()
1c2d1459 49#endif // HAVE_STRCASECMP_IN_STRINGS_H
9b4da627 50#endif // ! __WXPALMOS5__
ffecfa5a 51
52de37c7 52#include "wx/wxcrtbase.h" // for wxChar, wxStrlen() etc.
c9f78968 53#include "wx/strvararg.h"
e90c1d2a
VZ
54#include "wx/buffer.h" // for wxCharBuffer
55#include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes
a7ea63e2 56#include "wx/stringimpl.h"
467175ab 57#include "wx/stringops.h"
a7ea63e2 58#include "wx/unichar.h"
3f4a0c5b 59
68482dc5
VZ
60// by default we cache the mapping of the positions in UTF-8 string to the byte
61// offset as this results in noticeable performance improvements for loops over
62// strings using indices; comment out this line to disable this
63//
64// notice that this optimization is well worth using even in debug builds as it
65// changes asymptotic complexity of algorithms using indices to iterate over
66// wxString back to expected linear from quadratic
67//
68// also notice that wxTLS_TYPE() (__declspec(thread) in this case) is unsafe to
69// use in DLL build under pre-Vista Windows so we disable this code for now, if
70// anybody really needs to use UTF-8 build under Windows with this optimization
71// it would have to be re-tested and probably corrected
b69470e4
SC
72// CS: under OSX release builds the string destructor/cache cleanup sometimes
73// crashes, disable until we find the true reason or a better workaround
74#if wxUSE_UNICODE_UTF8 && !defined(__WXMSW__) && !defined(__WXOSX__)
68482dc5
VZ
75 #define wxUSE_STRING_POS_CACHE 1
76#else
77 #define wxUSE_STRING_POS_CACHE 0
78#endif
79
80#if wxUSE_STRING_POS_CACHE
81 #include "wx/tls.h"
82
83 // change this 0 to 1 to enable additional (very expensive) asserts
84 // verifying that string caching logic works as expected
85 #if 0
86 #define wxSTRING_CACHE_ASSERT(cond) wxASSERT(cond)
87 #else
88 #define wxSTRING_CACHE_ASSERT(cond)
89 #endif
90#endif // wxUSE_STRING_POS_CACHE
91
b5dbe15d 92class WXDLLIMPEXP_FWD_BASE wxString;
fb3c9042 93
67cff9dc
VZ
94// unless this symbol is predefined to disable the compatibility functions, do
95// use them
96#ifndef WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
97 #define WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER 1
98#endif
99
062dc5fc
VZ
100namespace wxPrivate
101{
102 template <typename T> struct wxStringAsBufHelper;
103}
104
c801d85f
KB
105// ---------------------------------------------------------------------------
106// macros
107// ---------------------------------------------------------------------------
108
5737d05f
VZ
109// casts [unfortunately!] needed to call some broken functions which require
110// "char *" instead of "const char *"
2bb67b80 111#define WXSTRINGCAST (wxChar *)(const wxChar *)
e90c1d2a
VZ
112#define wxCSTRINGCAST (wxChar *)(const wxChar *)
113#define wxMBSTRINGCAST (char *)(const char *)
114#define wxWCSTRINGCAST (wchar_t *)(const wchar_t *)
c801d85f 115
e90c1d2a
VZ
116// ----------------------------------------------------------------------------
117// constants
118// ----------------------------------------------------------------------------
119
13874b5c
VZ
120#if WXWIN_COMPATIBILITY_2_6
121
122// deprecated in favour of wxString::npos, don't use in new code
123//
e90c1d2a 124// maximum possible length for a string means "take all string" everywhere
8f93a29f 125#define wxSTRING_MAXLEN wxString::npos
66b6b045 126
13874b5c
VZ
127#endif // WXWIN_COMPATIBILITY_2_6
128
c801d85f 129// ---------------------------------------------------------------------------
e90c1d2a 130// global functions complementing standard C string library replacements for
3c67202d
VZ
131// strlen() and portable strcasecmp()
132//---------------------------------------------------------------------------
e90c1d2a 133
c7554da8 134#if WXWIN_COMPATIBILITY_2_8
e3f6cbd9 135// Use wxXXX() functions from wxcrt.h instead! These functions are for
e90c1d2a 136// backwards compatibility only.
88150e60 137
3c67202d 138// checks whether the passed in pointer is NULL and if the string is empty
c7554da8 139wxDEPRECATED( inline bool IsEmpty(const char *p) );
6d56eb5c 140inline bool IsEmpty(const char *p) { return (!p || !*p); }
c801d85f 141
3c67202d 142// safe version of strlen() (returns 0 if passed NULL pointer)
c7554da8 143wxDEPRECATED( inline size_t Strlen(const char *psz) );
6d56eb5c 144inline size_t Strlen(const char *psz)
c801d85f
KB
145 { return psz ? strlen(psz) : 0; }
146
3c67202d 147// portable strcasecmp/_stricmp
c7554da8 148wxDEPRECATED( inline int Stricmp(const char *psz1, const char *psz2) );
6d56eb5c 149inline int Stricmp(const char *psz1, const char *psz2)
dd1eaa89 150{
7f0586ef
JS
151#if defined(__VISUALC__) && defined(__WXWINCE__)
152 register char c1, c2;
153 do {
154 c1 = tolower(*psz1++);
155 c2 = tolower(*psz2++);
156 } while ( c1 && (c1 == c2) );
157
158 return c1 - c2;
159#elif defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) )
dd1eaa89 160 return _stricmp(psz1, psz2);
91b8de8d 161#elif defined(__SC__)
2432b92d 162 return _stricmp(psz1, psz2);
dd1eaa89
VZ
163#elif defined(__BORLANDC__)
164 return stricmp(psz1, psz2);
7be1f0d9
JS
165#elif defined(__WATCOMC__)
166 return stricmp(psz1, psz2);
14704334
VS
167#elif defined(__DJGPP__)
168 return stricmp(psz1, psz2);
91b8de8d
RR
169#elif defined(__EMX__)
170 return stricmp(psz1, psz2);
e2c87f4c 171#elif defined(__WXPM__)
1777b9bb 172 return stricmp(psz1, psz2);
a0be6908
WS
173#elif defined(__WXPALMOS__) || \
174 defined(HAVE_STRCASECMP_IN_STRING_H) || \
1c2d1459
VZ
175 defined(HAVE_STRCASECMP_IN_STRINGS_H) || \
176 defined(__GNUWIN32__)
dd1eaa89 177 return strcasecmp(psz1, psz2);
8be97d65 178#elif defined(__MWERKS__) && !defined(__INTEL__)
17dff81c
SC
179 register char c1, c2;
180 do {
181 c1 = tolower(*psz1++);
182 c2 = tolower(*psz2++);
183 } while ( c1 && (c1 == c2) );
184
185 return c1 - c2;
dd1eaa89
VZ
186#else
187 // almost all compilers/libraries provide this function (unfortunately under
188 // different names), that's why we don't implement our own which will surely
189 // be more efficient than this code (uncomment to use):
190 /*
191 register char c1, c2;
192 do {
193 c1 = tolower(*psz1++);
194 c2 = tolower(*psz2++);
195 } while ( c1 && (c1 == c2) );
196
197 return c1 - c2;
198 */
199
200 #error "Please define string case-insensitive compare for your OS/compiler"
201#endif // OS/compiler
202}
c801d85f 203
c7554da8
VS
204#endif // WXWIN_COMPATIBILITY_2_8
205
c9f78968
VS
206// ----------------------------------------------------------------------------
207// wxCStrData
208// ----------------------------------------------------------------------------
209
210// Lightweight object returned by wxString::c_str() and implicitly convertible
211// to either const char* or const wchar_t*.
9aee0061 212class WXDLLIMPEXP_BASE wxCStrData
c9f78968
VS
213{
214private:
215 // Ctors; for internal use by wxString and wxCStrData only
216 wxCStrData(const wxString *str, size_t offset = 0, bool owned = false)
217 : m_str(str), m_offset(offset), m_owned(owned) {}
218
219public:
220 // Ctor constructs the object from char literal; they are needed to make
221 // operator?: compile and they intentionally take char*, not const char*
92a17abc
VS
222 inline wxCStrData(char *buf);
223 inline wxCStrData(wchar_t *buf);
224 inline wxCStrData(const wxCStrData& data);
c9f78968 225
9aee0061 226 inline ~wxCStrData();
c9f78968 227
9aee0061
VZ
228 // methods defined inline below must be declared inline or mingw32 3.4.5
229 // warns about "<symbol> defined locally after being referenced with
230 // dllimport linkage"
231#if wxUSE_UNICODE_WCHAR
232 inline
233#endif
c9f78968
VS
234 const wchar_t* AsWChar() const;
235 operator const wchar_t*() const { return AsWChar(); }
11aac4ba 236
111d9948 237#if !wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY
9aee0061
VZ
238 inline
239#endif
c9f78968 240 const char* AsChar() const;
dbea442a
VZ
241 const unsigned char* AsUnsignedChar() const
242 { return (const unsigned char *) AsChar(); }
c9f78968 243 operator const char*() const { return AsChar(); }
dbea442a 244 operator const unsigned char*() const { return AsUnsignedChar(); }
11aac4ba
VS
245
246 operator const void*() const { return AsChar(); }
c9f78968 247
681e4412
VS
248 inline const wxCharBuffer AsCharBuf() const;
249 inline const wxWCharBuffer AsWCharBuf() const;
250
9aee0061 251 inline wxString AsString() const;
c9f78968 252
2523e9b7
VS
253 // returns the value as C string in internal representation (equivalent
254 // to AsString().wx_str(), but more efficient)
255 const wxStringCharType *AsInternal() const;
256
c9f78968 257 // allow expressions like "c_str()[0]":
9aee0061 258 inline wxUniChar operator[](size_t n) const;
c9f78968 259 wxUniChar operator[](int n) const { return operator[](size_t(n)); }
c1df0a3b 260 wxUniChar operator[](long n) const { return operator[](size_t(n)); }
c9f78968
VS
261#ifndef wxSIZE_T_IS_UINT
262 wxUniChar operator[](unsigned int n) const { return operator[](size_t(n)); }
263#endif // size_t != unsigned int
264
acf0c9ac 265 // these operators are needed to emulate the pointer semantics of c_str():
c9f78968
VS
266 // expressions like "wxChar *p = str.c_str() + 1;" should continue to work
267 // (we need both versions to resolve ambiguities):
268 wxCStrData operator+(int n) const
269 { return wxCStrData(m_str, m_offset + n, m_owned); }
acf0c9ac
VZ
270 wxCStrData operator+(long n) const
271 { return wxCStrData(m_str, m_offset + n, m_owned); }
c9f78968
VS
272 wxCStrData operator+(size_t n) const
273 { return wxCStrData(m_str, m_offset + n, m_owned); }
274
b5343e06
VZ
275 // and these for "str.c_str() + (p2 - p1)" (it also works for any integer
276 // expression but it must be ptrdiff_t and not e.g. int to work in this
277 // example):
278 wxCStrData operator-(ptrdiff_t n) const
f4c90fdf 279 {
b5343e06 280 wxASSERT_MSG( n <= (ptrdiff_t)m_offset,
f4c90fdf
VS
281 _T("attempt to construct address before the beginning of the string") );
282 return wxCStrData(m_str, m_offset - n, m_owned);
283 }
284
b77afb41 285 // this operator is needed to make expressions like "*c_str()" or
c9f78968 286 // "*(c_str() + 2)" work
9aee0061 287 inline wxUniChar operator*() const;
c9f78968
VS
288
289private:
290 const wxString *m_str;
291 size_t m_offset;
292 bool m_owned;
293
b5dbe15d 294 friend class WXDLLIMPEXP_FWD_BASE wxString;
c9f78968
VS
295};
296
297// ----------------------------------------------------------------------------
298// wxStringPrintfMixin
299// ---------------------------------------------------------------------------
300
301// NB: VC6 has a bug that causes linker errors if you have template methods
302// in a class using __declspec(dllimport). The solution is to split such
303// class into two classes, one that contains the template methods and does
304// *not* use WXDLLIMPEXP_BASE and another class that contains the rest
305// (with DLL linkage).
306//
307// We only do this for VC6 here, because the code is less efficient
308// (Printf() has to use dynamic_cast<>) and because OpenWatcom compiler
309// cannot compile this code.
310
311#if defined(__VISUALC__) && __VISUALC__ < 1300
312 #define wxNEEDS_WXSTRING_PRINTF_MIXIN
313#endif
314
315#ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
316// this class contains implementation of wxString's vararg methods, it's
317// exported from wxBase DLL
318class WXDLLIMPEXP_BASE wxStringPrintfMixinBase
319{
320protected:
321 wxStringPrintfMixinBase() {}
322
d1f6e2cf
VS
323#if !wxUSE_UTF8_LOCALE_ONLY
324 int DoPrintfWchar(const wxChar *format, ...);
325 static wxString DoFormatWchar(const wxChar *format, ...);
326#endif
327#if wxUSE_UNICODE_UTF8
328 int DoPrintfUtf8(const char *format, ...);
329 static wxString DoFormatUtf8(const char *format, ...);
330#endif
c9f78968
VS
331};
332
333// this class contains template wrappers for wxString's vararg methods, it's
334// intentionally *not* exported from the DLL in order to fix the VC6 bug
335// described above
336class wxStringPrintfMixin : public wxStringPrintfMixinBase
337{
338private:
339 // to further complicate things, we can't return wxString from
340 // wxStringPrintfMixin::Format() because wxString is not yet declared at
341 // this point; the solution is to use this fake type trait template - this
342 // way the compiler won't know the return type until Format() is used
343 // (this doesn't compile with Watcom, but VC6 compiles it just fine):
344 template<typename T> struct StringReturnType
345 {
346 typedef wxString type;
347 };
348
349public:
350 // these are duplicated wxString methods, they're also declared below
351 // if !wxNEEDS_WXSTRING_PRINTF_MIXIN:
352
2523e9b7 353 // static wxString Format(const wString& format, ...) ATTRIBUTE_PRINTF_1;
d1f6e2cf 354 WX_DEFINE_VARARG_FUNC_SANS_N0(static typename StringReturnType<T1>::type,
1528e0b8 355 Format, 1, (const wxFormatString&),
d1f6e2cf 356 DoFormatWchar, DoFormatUtf8)
2523e9b7
VS
357 // We have to implement the version without template arguments manually
358 // because of the StringReturnType<> hack, although WX_DEFINE_VARARG_FUNC
359 // normally does it itself. It has to be a template so that we can use
81051829
VS
360 // the hack, even though there's no real template parameter. We can't move
361 // it to wxStrig, because it would shadow these versions of Format() then.
2523e9b7
VS
362 template<typename T>
363 inline static typename StringReturnType<T>::type
81051829 364 Format(const T& fmt)
2523e9b7 365 {
81051829
VS
366 // NB: this doesn't compile if T is not (some form of) a string;
367 // this makes Format's prototype equivalent to
368 // Format(const wxFormatString& fmt)
369 return DoFormatWchar(wxFormatString(fmt));
2523e9b7
VS
370 }
371
372 // int Printf(const wxString& format, ...);
1528e0b8 373 WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const wxFormatString&),
d1f6e2cf 374 DoPrintfWchar, DoPrintfUtf8)
2523e9b7 375 // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2;
1528e0b8 376 WX_DEFINE_VARARG_FUNC(int, sprintf, 1, (const wxFormatString&),
d1f6e2cf 377 DoPrintfWchar, DoPrintfUtf8)
c9f78968
VS
378
379protected:
380 wxStringPrintfMixin() : wxStringPrintfMixinBase() {}
381};
382#endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
383
384
b6339dde
VZ
385// ----------------------------------------------------------------------------
386// wxString: string class trying to be compatible with std::string, MFC
387// CString and wxWindows 1.x wxString all at once
e87b7833
MB
388// ---------------------------------------------------------------------------
389
c9f78968
VS
390#ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
391 // "non dll-interface class 'wxStringPrintfMixin' used as base interface
392 // for dll-interface class 'wxString'" -- this is OK in our case
393 #pragma warning (disable:4275)
e87b7833
MB
394#endif
395
b0c4d5d7
VS
396#if wxUSE_UNICODE_UTF8
397// see the comment near wxString::iterator for why we need this
326a863a 398class WXDLLIMPEXP_BASE wxStringIteratorNode
b0c4d5d7 399{
326a863a 400public:
39c20230
VS
401 wxStringIteratorNode()
402 : m_str(NULL), m_citer(NULL), m_iter(NULL), m_prev(NULL), m_next(NULL) {}
403 wxStringIteratorNode(const wxString *str,
404 wxStringImpl::const_iterator *citer)
405 { DoSet(str, citer, NULL); }
406 wxStringIteratorNode(const wxString *str, wxStringImpl::iterator *iter)
407 { DoSet(str, NULL, iter); }
408 ~wxStringIteratorNode()
409 { clear(); }
410
411 inline void set(const wxString *str, wxStringImpl::const_iterator *citer)
412 { clear(); DoSet(str, citer, NULL); }
413 inline void set(const wxString *str, wxStringImpl::iterator *iter)
414 { clear(); DoSet(str, NULL, iter); }
b0c4d5d7
VS
415
416 const wxString *m_str;
417 wxStringImpl::const_iterator *m_citer;
418 wxStringImpl::iterator *m_iter;
419 wxStringIteratorNode *m_prev, *m_next;
300b44a9 420
39c20230
VS
421private:
422 inline void clear();
423 inline void DoSet(const wxString *str,
424 wxStringImpl::const_iterator *citer,
425 wxStringImpl::iterator *iter);
426
300b44a9
VS
427 // the node belongs to a particular iterator instance, it's not copied
428 // when a copy of the iterator is made
c0c133e1 429 wxDECLARE_NO_COPY_CLASS(wxStringIteratorNode);
b0c4d5d7
VS
430};
431#endif // wxUSE_UNICODE_UTF8
432
8f93a29f 433class WXDLLIMPEXP_BASE wxString
c9f78968 434#ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
8f93a29f 435 : public wxStringPrintfMixin
c9f78968
VS
436#endif
437{
e87b7833
MB
438 // NB: special care was taken in arranging the member functions in such order
439 // that all inline functions can be effectively inlined, verify that all
20aa779e 440 // performance critical functions are still inlined if you change order!
8f93a29f 441public:
8f93a29f
VS
442 // an 'invalid' value for string index, moved to this place due to a CW bug
443 static const size_t npos;
8f93a29f 444
e87b7833
MB
445private:
446 // if we hadn't made these operators private, it would be possible to
447 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
448 // converted to char in C and we do have operator=(char)
449 //
450 // NB: we don't need other versions (short/long and unsigned) as attempt
451 // to assign another numeric type to wxString will now result in
452 // ambiguity between operator=(char) and operator=(int)
453 wxString& operator=(int);
454
8f93a29f
VS
455 // these methods are not implemented - there is _no_ conversion from int to
456 // string, you're doing something wrong if the compiler wants to call it!
457 //
458 // try `s << i' or `s.Printf("%d", i)' instead
459 wxString(int);
460
461
462 // buffer for holding temporary substring when using any of the methods
463 // that take (char*,size_t) or (wchar_t*,size_t) arguments:
8f93a29f
VS
464 template<typename T>
465 struct SubstrBufFromType
466 {
467 T data;
468 size_t len;
469
8f93a29f 470 SubstrBufFromType(const T& data_, size_t len_)
9ef1ad0d
VZ
471 : data(data_), len(len_)
472 {
473 wxASSERT_MSG( len != npos, "must have real length" );
474 }
8f93a29f
VS
475 };
476
477#if wxUSE_UNICODE_UTF8
81727065
VS
478 // even char* -> char* needs conversion, from locale charset to UTF-8
479 typedef SubstrBufFromType<wxCharBuffer> SubstrBufFromWC;
480 typedef SubstrBufFromType<wxCharBuffer> SubstrBufFromMB;
8f93a29f
VS
481#elif wxUSE_UNICODE_WCHAR
482 typedef SubstrBufFromType<const wchar_t*> SubstrBufFromWC;
483 typedef SubstrBufFromType<wxWCharBuffer> SubstrBufFromMB;
8f93a29f
VS
484#else
485 typedef SubstrBufFromType<const char*> SubstrBufFromMB;
486 typedef SubstrBufFromType<wxCharBuffer> SubstrBufFromWC;
8f93a29f
VS
487#endif
488
489
490 // Functions implementing primitive operations on string data; wxString
491 // methods and iterators are implemented in terms of it. The differences
492 // between UTF-8 and wchar_t* representations of the string are mostly
493 // contained here.
494
81727065
VS
495#if wxUSE_UNICODE_UTF8
496 static SubstrBufFromMB ConvertStr(const char *psz, size_t nLength,
497 const wxMBConv& conv);
498 static SubstrBufFromWC ConvertStr(const wchar_t *pwz, size_t nLength,
499 const wxMBConv& conv);
500#elif wxUSE_UNICODE_WCHAR
8f93a29f 501 static SubstrBufFromMB ConvertStr(const char *psz, size_t nLength,
a962cdf4 502 const wxMBConv& conv);
8f93a29f
VS
503#else
504 static SubstrBufFromWC ConvertStr(const wchar_t *pwz, size_t nLength,
a962cdf4 505 const wxMBConv& conv);
8f93a29f
VS
506#endif
507
508#if !wxUSE_UNICODE_UTF8 // wxUSE_UNICODE_WCHAR or !wxUSE_UNICODE
a962cdf4 509 // returns C string encoded as the implementation expects:
8f93a29f 510 #if wxUSE_UNICODE
a962cdf4 511 static const wchar_t* ImplStr(const wchar_t* str)
e8f59039 512 { return str ? str : wxT(""); }
a962cdf4 513 static const SubstrBufFromWC ImplStr(const wchar_t* str, size_t n)
e8f59039
VS
514 { return SubstrBufFromWC(str, (str && n == npos) ? wxWcslen(str) : n); }
515 static wxWCharBuffer ImplStr(const char* str,
516 const wxMBConv& conv = wxConvLibc)
517 { return ConvertStr(str, npos, conv).data; }
518 static SubstrBufFromMB ImplStr(const char* str, size_t n,
519 const wxMBConv& conv = wxConvLibc)
520 { return ConvertStr(str, n, conv); }
8f93a29f 521 #else
e8f59039
VS
522 static const char* ImplStr(const char* str,
523 const wxMBConv& WXUNUSED(conv) = wxConvLibc)
524 { return str ? str : ""; }
525 static const SubstrBufFromMB ImplStr(const char* str, size_t n,
526 const wxMBConv& WXUNUSED(conv) = wxConvLibc)
527 { return SubstrBufFromMB(str, (str && n == npos) ? wxStrlen(str) : n); }
8f93a29f
VS
528 static wxCharBuffer ImplStr(const wchar_t* str)
529 { return ConvertStr(str, npos, wxConvLibc).data; }
530 static SubstrBufFromWC ImplStr(const wchar_t* str, size_t n)
531 { return ConvertStr(str, n, wxConvLibc); }
532 #endif
533
8f93a29f
VS
534 // translates position index in wxString to/from index in underlying
535 // wxStringImpl:
536 static size_t PosToImpl(size_t pos) { return pos; }
537 static void PosLenToImpl(size_t pos, size_t len,
538 size_t *implPos, size_t *implLen)
539 { *implPos = pos; *implLen = len; }
11aac4ba 540 static size_t LenToImpl(size_t len) { return len; }
8f93a29f
VS
541 static size_t PosFromImpl(size_t pos) { return pos; }
542
68482dc5
VZ
543 // we don't want to define these as empty inline functions as it could
544 // result in noticeable (and quite unnecessary in non-UTF-8 build) slowdown
545 // in debug build where the inline functions are not effectively inlined
546 #define wxSTRING_INVALIDATE_CACHE()
547 #define wxSTRING_INVALIDATE_CACHED_LENGTH()
548 #define wxSTRING_UPDATE_CACHED_LENGTH(n)
549 #define wxSTRING_SET_CACHED_LENGTH(n)
550
8f93a29f
VS
551#else // wxUSE_UNICODE_UTF8
552
467175ab
VS
553 static wxCharBuffer ImplStr(const char* str,
554 const wxMBConv& conv = wxConvLibc)
555 { return ConvertStr(str, npos, conv).data; }
556 static SubstrBufFromMB ImplStr(const char* str, size_t n,
557 const wxMBConv& conv = wxConvLibc)
558 { return ConvertStr(str, n, conv); }
81727065 559
467175ab 560 static wxCharBuffer ImplStr(const wchar_t* str)
5487ff0f 561 { return ConvertStr(str, npos, wxMBConvUTF8()).data; }
467175ab 562 static SubstrBufFromWC ImplStr(const wchar_t* str, size_t n)
5487ff0f 563 { return ConvertStr(str, n, wxMBConvUTF8()); }
81727065 564
68482dc5
VZ
565#if wxUSE_STRING_POS_CACHE
566 // this is an extremely simple cache used by PosToImpl(): each cache element
567 // contains the string it applies to and the index corresponding to the last
568 // used position in this wxString in its m_impl string
569 //
570 // NB: notice that this struct (and nested Element one) must be a POD or we
571 // wouldn't be able to use a thread-local variable of this type, in
572 // particular it should have no ctor -- we rely on statics being
573 // initialized to 0 instead
574 struct Cache
575 {
576 enum { SIZE = 8 };
577
578 struct Element
579 {
580 const wxString *str; // the string to which this element applies
581 size_t pos, // the cached index in this string
582 impl, // the corresponding position in its m_impl
583 len; // cached length or npos if unknown
584
585 // reset cached index to 0
586 void ResetPos() { pos = impl = 0; }
587
588 // reset position and length
589 void Reset() { ResetPos(); len = npos; }
590 };
591
592 // cache the indices mapping for the last few string used
593 Element cached[SIZE];
594
595 // the last used index
596 unsigned lastUsed;
597 };
598
e810df36
VZ
599#ifndef wxHAS_COMPILER_TLS
600 // we must use an accessor function and not a static variable when the TLS
601 // variables support is implemented in the library (and not by the compiler)
602 // because the global s_cache variable could be not yet initialized when a
603 // ctor of another global object is executed and if that ctor uses any
604 // wxString methods, bad things happen
605 //
606 // however notice that this approach does not work when compiler TLS is used,
607 // at least not with g++ 4.1.2 under amd64 as it apparently compiles code
608 // using this accessor incorrectly when optimizations are enabled (-O2 is
609 // enough) -- luckily we don't need it then neither as static __thread
610 // variables are initialized by 0 anyhow then and so we can use the variable
611 // directly
e317bd3f 612 WXEXPORT static Cache& GetCache()
8b73c531
VZ
613 {
614 static wxTLS_TYPE(Cache) s_cache;
615
616 return wxTLS_VALUE(s_cache);
617 }
cbec0f40 618
ad8ae788
VZ
619 // this helper struct is used to ensure that GetCache() is called during
620 // static initialization time, i.e. before any threads creation, as otherwise
621 // the static s_cache construction inside GetCache() wouldn't be MT-safe
622 friend struct wxStrCacheInitializer;
e810df36
VZ
623#else // wxHAS_COMPILER_TLS
624 static wxTLS_TYPE(Cache) ms_cache;
625 static Cache& GetCache() { return wxTLS_VALUE(ms_cache); }
626#endif // !wxHAS_COMPILER_TLS/wxHAS_COMPILER_TLS
627
628 static Cache::Element *GetCacheBegin() { return GetCache().cached; }
629 static Cache::Element *GetCacheEnd() { return GetCacheBegin() + Cache::SIZE; }
630 static unsigned& LastUsedCacheElement() { return GetCache().lastUsed; }
ad8ae788
VZ
631
632 // this is used in debug builds only to provide a convenient function,
633 // callable from a debugger, to show the cache contents
68482dc5
VZ
634 friend struct wxStrCacheDumper;
635
636 // uncomment this to have access to some profiling statistics on program
637 // termination
638 //#define wxPROFILE_STRING_CACHE
639
640#ifdef wxPROFILE_STRING_CACHE
641 static struct PosToImplCacheStats
642 {
643 unsigned postot, // total non-trivial calls to PosToImpl
644 poshits, // cache hits from PosToImpl()
645 mishits, // cached position beyond the needed one
646 sumpos, // sum of all positions, used to compute the
647 // average position after dividing by postot
648 sumofs, // sum of all offsets after using the cache, used to
649 // compute the average after dividing by hits
650 lentot, // number of total calls to length()
651 lenhits; // number of cache hits in length()
652 } ms_cacheStats;
653
8c3b65d9 654 friend struct wxStrCacheStatsDumper;
68482dc5
VZ
655
656 #define wxCACHE_PROFILE_FIELD_INC(field) ms_cacheStats.field++
657 #define wxCACHE_PROFILE_FIELD_ADD(field, val) ms_cacheStats.field += (val)
658#else // !wxPROFILE_STRING_CACHE
659 #define wxCACHE_PROFILE_FIELD_INC(field)
660 #define wxCACHE_PROFILE_FIELD_ADD(field, val)
661#endif // wxPROFILE_STRING_CACHE/!wxPROFILE_STRING_CACHE
662
663 // note: it could seem that the functions below shouldn't be inline because
664 // they are big, contain loops and so the compiler shouldn't be able to
665 // inline them anyhow, however moving them into string.cpp does decrease the
666 // code performance by ~5%, at least when using g++ 4.1 so do keep them here
667 // unless tests show that it's not advantageous any more
668
669 // return the pointer to the cache element for this string or NULL if not
670 // cached
671 Cache::Element *FindCacheElement() const
672 {
673 // profiling seems to show a small but consistent gain if we use this
674 // simple loop instead of starting from the last used element (there are
675 // a lot of misses in this function...)
e317bd3f 676 Cache::Element * const cacheBegin = GetCacheBegin();
6e42b980 677#ifndef wxHAS_COMPILER_TLS
cbec0f40 678 // during destruction tls calls may return NULL, in this case return NULL
e317bd3f
SC
679 // immediately without accessing anything else
680 if ( cacheBegin == NULL )
c1158f7b 681 return NULL;
e317bd3f
SC
682#endif
683 Cache::Element * const cacheEnd = GetCacheEnd();
684 for ( Cache::Element *c = cacheBegin; c != cacheEnd; c++ )
68482dc5
VZ
685 {
686 if ( c->str == this )
687 return c;
688 }
689
690 return NULL;
691 }
692
693 // unlike FindCacheElement(), this one always returns a valid pointer to the
694 // cache element for this string, it may have valid last cached position and
695 // its corresponding index in the byte string or not
696 Cache::Element *GetCacheElement() const
697 {
8b73c531
VZ
698 Cache::Element * const cacheBegin = GetCacheBegin();
699 Cache::Element * const cacheEnd = GetCacheEnd();
700 Cache::Element * const cacheStart = cacheBegin + LastUsedCacheElement();
68482dc5
VZ
701
702 // check the last used first, this does no (measurable) harm for a miss
703 // but does help for simple loops addressing the same string all the time
704 if ( cacheStart->str == this )
705 return cacheStart;
706
707 // notice that we're going to check cacheStart again inside this call but
708 // profiling shows that it's still faster to use a simple loop like
709 // inside FindCacheElement() than manually looping with wrapping starting
710 // from the cache entry after the start one
711 Cache::Element *c = FindCacheElement();
712 if ( !c )
713 {
714 // claim the next cache entry for this string
715 c = cacheStart;
716 if ( ++c == cacheEnd )
717 c = cacheBegin;
718
719 c->str = this;
720 c->Reset();
721
722 // and remember the last used element
8b73c531 723 LastUsedCacheElement() = c - cacheBegin;
68482dc5
VZ
724 }
725
726 return c;
727 }
728
729 size_t DoPosToImpl(size_t pos) const
730 {
731 wxCACHE_PROFILE_FIELD_INC(postot);
732
733 // NB: although the case of pos == 1 (and offset from cached position
734 // equal to 1) are common, nothing is gained by writing special code
735 // for handling them, the compiler (at least g++ 4.1 used) seems to
736 // optimize the code well enough on its own
737
738 wxCACHE_PROFILE_FIELD_ADD(sumpos, pos);
739
740 Cache::Element * const cache = GetCacheElement();
741
742 // cached position can't be 0 so if it is, it means that this entry was
743 // used for length caching only so far, i.e. it doesn't count as a hit
744 // from our point of view
745 if ( cache->pos )
1224b8a3 746 {
68482dc5 747 wxCACHE_PROFILE_FIELD_INC(poshits);
1224b8a3 748 }
68482dc5
VZ
749
750 if ( pos == cache->pos )
751 return cache->impl;
752
753 // this seems to happen only rarely so just reset the cache in this case
754 // instead of complicating code even further by seeking backwards in this
755 // case
756 if ( cache->pos > pos )
757 {
758 wxCACHE_PROFILE_FIELD_INC(mishits);
759
760 cache->ResetPos();
761 }
762
763 wxCACHE_PROFILE_FIELD_ADD(sumofs, pos - cache->pos);
764
765
766 wxStringImpl::const_iterator i(m_impl.begin() + cache->impl);
767 for ( size_t n = cache->pos; n < pos; n++ )
768 wxStringOperations::IncIter(i);
769
770 cache->pos = pos;
771 cache->impl = i - m_impl.begin();
772
773 wxSTRING_CACHE_ASSERT(
774 (int)cache->impl == (begin() + pos).impl() - m_impl.begin() );
775
776 return cache->impl;
777 }
778
779 void InvalidateCache()
780 {
781 Cache::Element * const cache = FindCacheElement();
782 if ( cache )
783 cache->Reset();
784 }
785
786 void InvalidateCachedLength()
787 {
788 Cache::Element * const cache = FindCacheElement();
789 if ( cache )
790 cache->len = npos;
791 }
792
793 void SetCachedLength(size_t len)
794 {
795 // we optimistically cache the length here even if the string wasn't
796 // present in the cache before, this seems to do no harm and the
797 // potential for avoiding length recomputation for long strings looks
798 // interesting
799 GetCacheElement()->len = len;
800 }
801
802 void UpdateCachedLength(ptrdiff_t delta)
803 {
804 Cache::Element * const cache = FindCacheElement();
805 if ( cache && cache->len != npos )
806 {
807 wxSTRING_CACHE_ASSERT( (ptrdiff_t)cache->len + delta >= 0 );
808
809 cache->len += delta;
810 }
811 }
812
813 #define wxSTRING_INVALIDATE_CACHE() InvalidateCache()
814 #define wxSTRING_INVALIDATE_CACHED_LENGTH() InvalidateCachedLength()
815 #define wxSTRING_UPDATE_CACHED_LENGTH(n) UpdateCachedLength(n)
816 #define wxSTRING_SET_CACHED_LENGTH(n) SetCachedLength(n)
817#else // !wxUSE_STRING_POS_CACHE
818 size_t DoPosToImpl(size_t pos) const
819 {
820 return (begin() + pos).impl() - m_impl.begin();
821 }
822
823 #define wxSTRING_INVALIDATE_CACHE()
824 #define wxSTRING_INVALIDATE_CACHED_LENGTH()
825 #define wxSTRING_UPDATE_CACHED_LENGTH(n)
826 #define wxSTRING_SET_CACHED_LENGTH(n)
827#endif // wxUSE_STRING_POS_CACHE/!wxUSE_STRING_POS_CACHE
828
8f93a29f
VS
829 size_t PosToImpl(size_t pos) const
830 {
68482dc5 831 return pos == 0 || pos == npos ? pos : DoPosToImpl(pos);
64a044d5
VZ
832 }
833
81727065
VS
834 void PosLenToImpl(size_t pos, size_t len, size_t *implPos, size_t *implLen) const;
835
836 size_t LenToImpl(size_t len) const
837 {
838 size_t pos, len2;
839 PosLenToImpl(0, len, &pos, &len2);
840 return len2;
841 }
842
8f93a29f
VS
843 size_t PosFromImpl(size_t pos) const
844 {
845 if ( pos == 0 || pos == npos )
846 return pos;
847 else
b0c4d5d7 848 return const_iterator(this, m_impl.begin() + pos) - begin();
8f93a29f 849 }
8f93a29f
VS
850#endif // !wxUSE_UNICODE_UTF8/wxUSE_UNICODE_UTF8
851
8f93a29f
VS
852public:
853 // standard types
854 typedef wxUniChar value_type;
855 typedef wxUniChar char_type;
856 typedef wxUniCharRef reference;
857 typedef wxChar* pointer;
858 typedef const wxChar* const_pointer;
859
860 typedef size_t size_type;
861 typedef wxUniChar const_reference;
862
302acc45 863#if wxUSE_STD_STRING
81727065
VS
864 #if wxUSE_UNICODE_UTF8
865 // random access is not O(1), as required by Random Access Iterator
866 #define WX_STR_ITERATOR_TAG std::bidirectional_iterator_tag
867 #else
868 #define WX_STR_ITERATOR_TAG std::random_access_iterator_tag
869 #endif
0514adaf 870 #define WX_DEFINE_ITERATOR_CATEGORY(cat) typedef cat iterator_category;
a962cdf4 871#else
302acc45
VZ
872 // not defining iterator_category at all in this case is better than defining
873 // it as some dummy type -- at least it results in more intelligible error
874 // messages
0514adaf 875 #define WX_DEFINE_ITERATOR_CATEGORY(cat)
a962cdf4
VS
876#endif
877
b0c4d5d7 878 #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, reference_type) \
8f93a29f
VS
879 private: \
880 typedef wxStringImpl::iterator_name underlying_iterator; \
881 public: \
0514adaf 882 WX_DEFINE_ITERATOR_CATEGORY(WX_STR_ITERATOR_TAG) \
8f93a29f 883 typedef wxUniChar value_type; \
a962cdf4 884 typedef int difference_type; \
8f93a29f
VS
885 typedef reference_type reference; \
886 typedef pointer_type pointer; \
887 \
a962cdf4 888 reference operator[](size_t n) const { return *(*this + n); } \
8f93a29f
VS
889 \
890 iterator_name& operator++() \
467175ab 891 { wxStringOperations::IncIter(m_cur); return *this; } \
8f93a29f 892 iterator_name& operator--() \
467175ab 893 { wxStringOperations::DecIter(m_cur); return *this; } \
8f93a29f
VS
894 iterator_name operator++(int) \
895 { \
896 iterator_name tmp = *this; \
467175ab 897 wxStringOperations::IncIter(m_cur); \
8f93a29f
VS
898 return tmp; \
899 } \
900 iterator_name operator--(int) \
901 { \
902 iterator_name tmp = *this; \
467175ab 903 wxStringOperations::DecIter(m_cur); \
8f93a29f
VS
904 return tmp; \
905 } \
906 \
b5343e06 907 iterator_name& operator+=(ptrdiff_t n) \
467175ab
VS
908 { \
909 m_cur = wxStringOperations::AddToIter(m_cur, n); \
910 return *this; \
911 } \
b5343e06 912 iterator_name& operator-=(ptrdiff_t n) \
467175ab
VS
913 { \
914 m_cur = wxStringOperations::AddToIter(m_cur, -n); \
915 return *this; \
467175ab 916 } \
8f93a29f 917 \
89360a8c 918 difference_type operator-(const iterator_name& i) const \
467175ab 919 { return wxStringOperations::DiffIters(m_cur, i.m_cur); } \
8f93a29f 920 \
f2a1b1bd 921 bool operator==(const iterator_name& i) const \
8f93a29f
VS
922 { return m_cur == i.m_cur; } \
923 bool operator!=(const iterator_name& i) const \
924 { return m_cur != i.m_cur; } \
925 \
926 bool operator<(const iterator_name& i) const \
927 { return m_cur < i.m_cur; } \
928 bool operator>(const iterator_name& i) const \
929 { return m_cur > i.m_cur; } \
930 bool operator<=(const iterator_name& i) const \
931 { return m_cur <= i.m_cur; } \
932 bool operator>=(const iterator_name& i) const \
933 { return m_cur >= i.m_cur; } \
934 \
935 private: \
936 /* for internal wxString use only: */ \
cf9a878b 937 underlying_iterator impl() const { return m_cur; } \
8f93a29f 938 \
b5dbe15d
VS
939 friend class wxString; \
940 friend class wxCStrData; \
8f93a29f
VS
941 \
942 private: \
89360a8c 943 underlying_iterator m_cur
8f93a29f 944
b5dbe15d 945 class WXDLLIMPEXP_FWD_BASE const_iterator;
8f93a29f 946
81727065 947#if wxUSE_UNICODE_UTF8
b0c4d5d7
VS
948 // NB: In UTF-8 build, (non-const) iterator needs to keep reference
949 // to the underlying wxStringImpl, because UTF-8 is variable-length
950 // encoding and changing the value pointer to by an iterator (using
951 // its operator*) requires calling wxStringImpl::replace() if the old
952 // and new values differ in their encoding's length.
953 //
954 // Furthermore, the replace() call may invalid all iterators for the
955 // string, so we have to keep track of outstanding iterators and update
956 // them if replace() happens.
957 //
958 // This is implemented by maintaining linked list of iterators for every
959 // string and traversing it in wxUniCharRef::operator=(). Head of the
960 // list is stored in wxString. (FIXME-UTF8)
961
541aa821 962 class WXDLLIMPEXP_BASE iterator
81727065 963 {
b0c4d5d7 964 WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef);
c7dc0057 965
c67b782d 966 public:
39c20230 967 iterator() {}
b0c4d5d7
VS
968 iterator(const iterator& i)
969 : m_cur(i.m_cur), m_node(i.str(), &m_cur) {}
300b44a9 970 iterator& operator=(const iterator& i)
162e998c
PC
971 {
972 if (&i != this)
973 {
974 m_cur = i.m_cur;
975 m_node.set(i.str(), &m_cur);
976 }
977 return *this;
978 }
b0c4d5d7
VS
979
980 reference operator*()
6bd4f281 981 { return wxUniCharRef::CreateForString(*str(), m_cur); }
81727065 982
b5343e06 983 iterator operator+(ptrdiff_t n) const
b0c4d5d7 984 { return iterator(str(), wxStringOperations::AddToIter(m_cur, n)); }
b5343e06 985 iterator operator-(ptrdiff_t n) const
b0c4d5d7 986 { return iterator(str(), wxStringOperations::AddToIter(m_cur, -n)); }
81727065
VS
987
988 private:
989 iterator(wxString *str, underlying_iterator ptr)
b0c4d5d7 990 : m_cur(ptr), m_node(str, &m_cur) {}
c7dc0057 991
5c33522f 992 wxString* str() const { return const_cast<wxString*>(m_node.m_str); }
b0c4d5d7
VS
993
994 wxStringIteratorNode m_node;
81727065
VS
995
996 friend class const_iterator;
997 };
cf9a878b 998
541aa821 999 class WXDLLIMPEXP_BASE const_iterator
b0c4d5d7
VS
1000 {
1001 // NB: reference_type is intentionally value, not reference, the character
1002 // may be encoded differently in wxString data:
1003 WX_STR_ITERATOR_IMPL(const_iterator, const wxChar*, wxUniChar);
1004
1005 public:
39c20230 1006 const_iterator() {}
b0c4d5d7
VS
1007 const_iterator(const const_iterator& i)
1008 : m_cur(i.m_cur), m_node(i.str(), &m_cur) {}
1009 const_iterator(const iterator& i)
1010 : m_cur(i.m_cur), m_node(i.str(), &m_cur) {}
1011
300b44a9 1012 const_iterator& operator=(const const_iterator& i)
162e998c
PC
1013 {
1014 if (&i != this)
1015 {
1016 m_cur = i.m_cur;
1017 m_node.set(i.str(), &m_cur);
1018 }
1019 return *this;
1020 }
300b44a9 1021 const_iterator& operator=(const iterator& i)
39c20230 1022 { m_cur = i.m_cur; m_node.set(i.str(), &m_cur); return *this; }
300b44a9 1023
b0c4d5d7
VS
1024 reference operator*() const
1025 { return wxStringOperations::DecodeChar(m_cur); }
1026
b5343e06 1027 const_iterator operator+(ptrdiff_t n) const
b0c4d5d7 1028 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur, n)); }
b5343e06 1029 const_iterator operator-(ptrdiff_t n) const
b0c4d5d7 1030 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur, -n)); }
b0c4d5d7
VS
1031
1032 private:
1033 // for internal wxString use only:
1034 const_iterator(const wxString *str, underlying_iterator ptr)
1035 : m_cur(ptr), m_node(str, &m_cur) {}
b0c4d5d7
VS
1036
1037 const wxString* str() const { return m_node.m_str; }
1038
1039 wxStringIteratorNode m_node;
1040 };
1041
cf9a878b
VS
1042 size_t IterToImplPos(wxString::iterator i) const
1043 { return wxStringImpl::const_iterator(i.impl()) - m_impl.begin(); }
1044
68482dc5
VZ
1045 iterator GetIterForNthChar(size_t n)
1046 { return iterator(this, m_impl.begin() + PosToImpl(n)); }
1047 const_iterator GetIterForNthChar(size_t n) const
1048 { return const_iterator(this, m_impl.begin() + PosToImpl(n)); }
81727065 1049#else // !wxUSE_UNICODE_UTF8
cf9a878b 1050
541aa821 1051 class WXDLLIMPEXP_BASE iterator
8f93a29f 1052 {
b0c4d5d7 1053 WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef);
8f93a29f 1054
81727065 1055 public:
39c20230 1056 iterator() {}
81727065
VS
1057 iterator(const iterator& i) : m_cur(i.m_cur) {}
1058
b0c4d5d7
VS
1059 reference operator*()
1060 { return wxUniCharRef::CreateForString(m_cur); }
1061
b5343e06 1062 iterator operator+(ptrdiff_t n) const
467175ab 1063 { return iterator(wxStringOperations::AddToIter(m_cur, n)); }
b5343e06 1064 iterator operator-(ptrdiff_t n) const
467175ab 1065 { return iterator(wxStringOperations::AddToIter(m_cur, -n)); }
81727065
VS
1066
1067 private:
1068 // for internal wxString use only:
1069 iterator(underlying_iterator ptr) : m_cur(ptr) {}
1070 iterator(wxString *WXUNUSED(str), underlying_iterator ptr) : m_cur(ptr) {}
1071
8f93a29f
VS
1072 friend class const_iterator;
1073 };
1074
541aa821 1075 class WXDLLIMPEXP_BASE const_iterator
8f93a29f
VS
1076 {
1077 // NB: reference_type is intentionally value, not reference, the character
1078 // may be encoded differently in wxString data:
b0c4d5d7 1079 WX_STR_ITERATOR_IMPL(const_iterator, const wxChar*, wxUniChar);
8f93a29f
VS
1080
1081 public:
39c20230 1082 const_iterator() {}
81727065 1083 const_iterator(const const_iterator& i) : m_cur(i.m_cur) {}
8f93a29f 1084 const_iterator(const iterator& i) : m_cur(i.m_cur) {}
81727065 1085
b0c4d5d7
VS
1086 reference operator*() const
1087 { return wxStringOperations::DecodeChar(m_cur); }
1088
b5343e06 1089 const_iterator operator+(ptrdiff_t n) const
467175ab 1090 { return const_iterator(wxStringOperations::AddToIter(m_cur, n)); }
b5343e06 1091 const_iterator operator-(ptrdiff_t n) const
467175ab 1092 { return const_iterator(wxStringOperations::AddToIter(m_cur, -n)); }
81727065
VS
1093
1094 private:
1095 // for internal wxString use only:
1096 const_iterator(underlying_iterator ptr) : m_cur(ptr) {}
b0c4d5d7
VS
1097 const_iterator(const wxString *WXUNUSED(str), underlying_iterator ptr)
1098 : m_cur(ptr) {}
8f93a29f 1099 };
68482dc5
VZ
1100
1101 iterator GetIterForNthChar(size_t n) { return begin() + n; }
1102 const_iterator GetIterForNthChar(size_t n) const { return begin() + n; }
b0c4d5d7 1103#endif // wxUSE_UNICODE_UTF8/!wxUSE_UNICODE_UTF8
8f93a29f 1104
a962cdf4 1105 #undef WX_STR_ITERATOR_TAG
8f93a29f
VS
1106 #undef WX_STR_ITERATOR_IMPL
1107
1108 friend class iterator;
1109 friend class const_iterator;
1110
1111 template <typename T>
1112 class reverse_iterator_impl
1113 {
1114 public:
1115 typedef T iterator_type;
a962cdf4 1116
0514adaf 1117 WX_DEFINE_ITERATOR_CATEGORY(typename T::iterator_category)
8f93a29f 1118 typedef typename T::value_type value_type;
a962cdf4 1119 typedef typename T::difference_type difference_type;
8f93a29f
VS
1120 typedef typename T::reference reference;
1121 typedef typename T::pointer *pointer;
1122
39c20230 1123 reverse_iterator_impl() {}
8f93a29f
VS
1124 reverse_iterator_impl(iterator_type i) : m_cur(i) {}
1125 reverse_iterator_impl(const reverse_iterator_impl& ri)
1126 : m_cur(ri.m_cur) {}
1127
1128 iterator_type base() const { return m_cur; }
1129
1130 reference operator*() const { return *(m_cur-1); }
a962cdf4 1131 reference operator[](size_t n) const { return *(*this + n); }
8f93a29f
VS
1132
1133 reverse_iterator_impl& operator++()
1134 { --m_cur; return *this; }
1135 reverse_iterator_impl operator++(int)
1136 { reverse_iterator_impl tmp = *this; --m_cur; return tmp; }
1137 reverse_iterator_impl& operator--()
1138 { ++m_cur; return *this; }
1139 reverse_iterator_impl operator--(int)
1140 { reverse_iterator_impl tmp = *this; ++m_cur; return tmp; }
1141
50eeb96f 1142 // NB: explicit <T> in the functions below is to keep BCC 5.5 happy
b5343e06 1143 reverse_iterator_impl operator+(ptrdiff_t n) const
50eeb96f 1144 { return reverse_iterator_impl<T>(m_cur - n); }
b5343e06 1145 reverse_iterator_impl operator-(ptrdiff_t n) const
50eeb96f 1146 { return reverse_iterator_impl<T>(m_cur + n); }
b5343e06 1147 reverse_iterator_impl operator+=(ptrdiff_t n)
8f93a29f 1148 { m_cur -= n; return *this; }
b5343e06 1149 reverse_iterator_impl operator-=(ptrdiff_t n)
8f93a29f
VS
1150 { m_cur += n; return *this; }
1151
1152 unsigned operator-(const reverse_iterator_impl& i) const
1153 { return i.m_cur - m_cur; }
1154
1155 bool operator==(const reverse_iterator_impl& ri) const
1156 { return m_cur == ri.m_cur; }
1157 bool operator!=(const reverse_iterator_impl& ri) const
1158 { return !(*this == ri); }
1159
1160 bool operator<(const reverse_iterator_impl& i) const
1161 { return m_cur > i.m_cur; }
1162 bool operator>(const reverse_iterator_impl& i) const
1163 { return m_cur < i.m_cur; }
1164 bool operator<=(const reverse_iterator_impl& i) const
1165 { return m_cur >= i.m_cur; }
1166 bool operator>=(const reverse_iterator_impl& i) const
1167 { return m_cur <= i.m_cur; }
1168
1169 private:
1170 iterator_type m_cur;
1171 };
e87b7833 1172
8f93a29f
VS
1173 typedef reverse_iterator_impl<iterator> reverse_iterator;
1174 typedef reverse_iterator_impl<const_iterator> const_reverse_iterator;
e90c1d2a 1175
67cff9dc
VZ
1176private:
1177 // used to transform an expression built using c_str() (and hence of type
1178 // wxCStrData) to an iterator into the string
1179 static const_iterator CreateConstIterator(const wxCStrData& data)
1180 {
b0c4d5d7
VS
1181 return const_iterator(data.m_str,
1182 (data.m_str->begin() + data.m_offset).impl());
67cff9dc
VZ
1183 }
1184
59953bf4
VS
1185 // in UTF-8 STL build, creation from std::string requires conversion under
1186 // non-UTF8 locales, so we can't have and use wxString(wxStringImpl) ctor;
1187 // instead we define dummy type that lets us have wxString ctor for creation
1188 // from wxStringImpl that couldn't be used by user code (in all other builds,
1189 // "standard" ctors can be used):
1190#if wxUSE_UNICODE_UTF8 && wxUSE_STL_BASED_WXSTRING
1191 struct CtorFromStringImplTag {};
1192
1193 wxString(CtorFromStringImplTag* WXUNUSED(dummy), const wxStringImpl& src)
1194 : m_impl(src) {}
1195
1196 static wxString FromImpl(const wxStringImpl& src)
1197 { return wxString((CtorFromStringImplTag*)NULL, src); }
1198#else
82a99c69 1199 #if !wxUSE_STL_BASED_WXSTRING
59953bf4 1200 wxString(const wxStringImpl& src) : m_impl(src) { }
82a99c69
VS
1201 // else: already defined as wxString(wxStdString) below
1202 #endif
59953bf4
VS
1203 static wxString FromImpl(const wxStringImpl& src) { return wxString(src); }
1204#endif
1205
67cff9dc
VZ
1206public:
1207 // constructors and destructor
1208 // ctor for an empty string
1209 wxString() {}
1210
1211 // copy ctor
67cff9dc
VZ
1212 wxString(const wxString& stringSrc) : m_impl(stringSrc.m_impl) { }
1213
1214 // string containing nRepeat copies of ch
623633ee 1215 wxString(wxUniChar ch, size_t nRepeat = 1 )
67cff9dc
VZ
1216 { assign(nRepeat, ch); }
1217 wxString(size_t nRepeat, wxUniChar ch)
1218 { assign(nRepeat, ch); }
1219 wxString(wxUniCharRef ch, size_t nRepeat = 1)
1220 { assign(nRepeat, ch); }
1221 wxString(size_t nRepeat, wxUniCharRef ch)
1222 { assign(nRepeat, ch); }
1223 wxString(char ch, size_t nRepeat = 1)
1224 { assign(nRepeat, ch); }
1225 wxString(size_t nRepeat, char ch)
1226 { assign(nRepeat, ch); }
1227 wxString(wchar_t ch, size_t nRepeat = 1)
1228 { assign(nRepeat, ch); }
1229 wxString(size_t nRepeat, wchar_t ch)
1230 { assign(nRepeat, ch); }
1231
1232 // ctors from char* strings:
1233 wxString(const char *psz)
1234 : m_impl(ImplStr(psz)) {}
1235 wxString(const char *psz, const wxMBConv& conv)
1236 : m_impl(ImplStr(psz, conv)) {}
1237 wxString(const char *psz, size_t nLength)
1238 { assign(psz, nLength); }
1239 wxString(const char *psz, const wxMBConv& conv, size_t nLength)
1240 {
1241 SubstrBufFromMB str(ImplStr(psz, nLength, conv));
1242 m_impl.assign(str.data, str.len);
1243 }
1244
1245 // and unsigned char*:
1246 wxString(const unsigned char *psz)
1247 : m_impl(ImplStr((const char*)psz)) {}
1248 wxString(const unsigned char *psz, const wxMBConv& conv)
1249 : m_impl(ImplStr((const char*)psz, conv)) {}
1250 wxString(const unsigned char *psz, size_t nLength)
1251 { assign((const char*)psz, nLength); }
1252 wxString(const unsigned char *psz, const wxMBConv& conv, size_t nLength)
1253 {
1254 SubstrBufFromMB str(ImplStr((const char*)psz, nLength, conv));
1255 m_impl.assign(str.data, str.len);
1256 }
1257
1258 // ctors from wchar_t* strings:
1259 wxString(const wchar_t *pwz)
1260 : m_impl(ImplStr(pwz)) {}
1261 wxString(const wchar_t *pwz, const wxMBConv& WXUNUSED(conv))
1262 : m_impl(ImplStr(pwz)) {}
1263 wxString(const wchar_t *pwz, size_t nLength)
1264 { assign(pwz, nLength); }
1265 wxString(const wchar_t *pwz, const wxMBConv& WXUNUSED(conv), size_t nLength)
1266 { assign(pwz, nLength); }
1267
1268 wxString(const wxCharBuffer& buf)
1269 { assign(buf.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
1270 wxString(const wxWCharBuffer& buf)
1271 { assign(buf.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
1272
06e9cf13
VS
1273 // NB: this version uses m_impl.c_str() to force making a copy of the
1274 // string, so that "wxString(str.c_str())" idiom for passing strings
1275 // between threads works
67cff9dc 1276 wxString(const wxCStrData& cstr)
06e9cf13 1277 : m_impl(cstr.AsString().m_impl.c_str()) { }
67cff9dc
VZ
1278
1279 // as we provide both ctors with this signature for both char and unsigned
1280 // char string, we need to provide one for wxCStrData to resolve ambiguity
1281 wxString(const wxCStrData& cstr, size_t nLength)
1282 : m_impl(cstr.AsString().Mid(0, nLength).m_impl) {}
1283
1284 // and because wxString is convertible to wxCStrData and const wxChar *
1285 // we also need to provide this one
1286 wxString(const wxString& str, size_t nLength)
1545d942 1287 { assign(str, nLength); }
67cff9dc 1288
68482dc5
VZ
1289
1290#if wxUSE_STRING_POS_CACHE
1291 ~wxString()
1292 {
1293 // we need to invalidate our cache entry as another string could be
1294 // recreated at the same address (unlikely, but still possible, with the
1295 // heap-allocated strings but perfectly common with stack-allocated ones)
1296 InvalidateCache();
1297 }
1298#endif // wxUSE_STRING_POS_CACHE
1299
67cff9dc 1300 // even if we're not built with wxUSE_STL == 1 it is very convenient to allow
59953bf4
VS
1301 // implicit conversions from std::string to wxString and vice verse as this
1302 // allows to use the same strings in non-GUI and GUI code, however we don't
1303 // want to unconditionally add this ctor as it would make wx lib dependent on
67cff9dc
VZ
1304 // libstdc++ on some Linux versions which is bad, so instead we ask the
1305 // client code to define this wxUSE_STD_STRING symbol if they need it
59953bf4 1306#if wxUSE_STD_STRING
59953bf4
VS
1307 #if wxUSE_UNICODE_WCHAR
1308 wxString(const wxStdWideString& str) : m_impl(str) {}
1309 #else // UTF-8 or ANSI
1310 wxString(const wxStdWideString& str)
1311 { assign(str.c_str(), str.length()); }
1312 #endif
1313
1314 #if !wxUSE_UNICODE // ANSI build
1315 // FIXME-UTF8: do this in UTF8 build #if wxUSE_UTF8_LOCALE_ONLY, too
1316 wxString(const std::string& str) : m_impl(str) {}
1317 #else // Unicode
1318 wxString(const std::string& str)
1319 { assign(str.c_str(), str.length()); }
1320 #endif
7978bc72 1321#endif // wxUSE_STD_STRING
59953bf4 1322
7978bc72
VS
1323 // Unlike ctor from std::string, we provide conversion to std::string only
1324 // if wxUSE_STL and not merely wxUSE_STD_STRING (which is on by default),
1325 // because it conflicts with operator const char/wchar_t*:
1326#if wxUSE_STL
59953bf4
VS
1327 #if wxUSE_UNICODE_WCHAR && wxUSE_STL_BASED_WXSTRING
1328 // wxStringImpl is std::string in the encoding we want
1329 operator const wxStdWideString&() const { return m_impl; }
1330 #else
1331 // wxStringImpl is either not std::string or needs conversion
1332 operator wxStdWideString() const
1333 // FIXME-UTF8: broken for embedded NULs
1334 { return wxStdWideString(wc_str()); }
1335 #endif
1336
111d9948 1337 #if (!wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY) && wxUSE_STL_BASED_WXSTRING
59953bf4
VS
1338 // wxStringImpl is std::string in the encoding we want
1339 operator const std::string&() const { return m_impl; }
1340 #else
1341 // wxStringImpl is either not std::string or needs conversion
1342 operator std::string() const
1343 // FIXME-UTF8: broken for embedded NULs
1344 { return std::string(mb_str()); }
1345 #endif
111d9948 1346#endif // wxUSE_STL
67cff9dc 1347
06e9cf13
VS
1348 wxString Clone() const
1349 {
1350 // make a deep copy of the string, i.e. the returned string will have
1351 // ref count = 1 with refcounted implementation
1352 return wxString::FromImpl(wxStringImpl(m_impl.c_str(), m_impl.length()));
1353 }
1354
8f93a29f 1355 // first valid index position
b0c4d5d7 1356 const_iterator begin() const { return const_iterator(this, m_impl.begin()); }
81727065 1357 iterator begin() { return iterator(this, m_impl.begin()); }
8f93a29f 1358 // position one after the last valid one
b0c4d5d7 1359 const_iterator end() const { return const_iterator(this, m_impl.end()); }
81727065 1360 iterator end() { return iterator(this, m_impl.end()); }
8f93a29f
VS
1361
1362 // first element of the reversed string
1363 const_reverse_iterator rbegin() const
1364 { return const_reverse_iterator(end()); }
1365 reverse_iterator rbegin()
1366 { return reverse_iterator(end()); }
1367 // one beyond the end of the reversed string
1368 const_reverse_iterator rend() const
1369 { return const_reverse_iterator(begin()); }
1370 reverse_iterator rend()
1371 { return reverse_iterator(begin()); }
1372
1373 // std::string methods:
1374#if wxUSE_UNICODE_UTF8
68482dc5
VZ
1375 size_t length() const
1376 {
1377#if wxUSE_STRING_POS_CACHE
1378 wxCACHE_PROFILE_FIELD_INC(lentot);
1379
1380 Cache::Element * const cache = GetCacheElement();
1381
1382 if ( cache->len == npos )
1383 {
1384 // it's probably not worth trying to be clever and using cache->pos
1385 // here as it's probably 0 anyhow -- you usually call length() before
1386 // starting to index the string
1387 cache->len = end() - begin();
1388 }
1389 else
1390 {
1391 wxCACHE_PROFILE_FIELD_INC(lenhits);
1392
1393 wxSTRING_CACHE_ASSERT( (int)cache->len == end() - begin() );
1394 }
1395
1396 return cache->len;
1397#else // !wxUSE_STRING_POS_CACHE
1398 return end() - begin();
1399#endif // wxUSE_STRING_POS_CACHE/!wxUSE_STRING_POS_CACHE
1400 }
8f93a29f
VS
1401#else
1402 size_t length() const { return m_impl.length(); }
1403#endif
ce76f779 1404
8f93a29f
VS
1405 size_type size() const { return length(); }
1406 size_type max_size() const { return npos; }
e90c1d2a 1407
8f93a29f 1408 bool empty() const { return m_impl.empty(); }
b77afb41 1409
68482dc5
VZ
1410 // NB: these methods don't have a well-defined meaning in UTF-8 case
1411 size_type capacity() const { return m_impl.capacity(); }
1412 void reserve(size_t sz) { m_impl.reserve(sz); }
b77afb41 1413
8f93a29f
VS
1414 void resize(size_t nSize, wxUniChar ch = wxT('\0'))
1415 {
fe1b98f5
VZ
1416 const size_t len = length();
1417 if ( nSize == len)
1418 return;
1419
8f93a29f 1420#if wxUSE_UNICODE_UTF8
fe1b98f5 1421 if ( nSize < len )
8f93a29f 1422 {
68482dc5
VZ
1423 wxSTRING_INVALIDATE_CACHE();
1424
fe1b98f5
VZ
1425 // we can't use wxStringImpl::resize() for truncating the string as it
1426 // counts in bytes, not characters
1427 erase(nSize);
1428 return;
8f93a29f 1429 }
fe1b98f5
VZ
1430
1431 // we also can't use (presumably more efficient) resize() if we have to
1432 // append characters taking more than one byte
1433 if ( !ch.IsAscii() )
68482dc5 1434 {
fe1b98f5 1435 append(nSize - len, ch);
68482dc5
VZ
1436 }
1437 else // can use (presumably faster) resize() version
fe1b98f5 1438#endif // wxUSE_UNICODE_UTF8
68482dc5
VZ
1439 {
1440 wxSTRING_INVALIDATE_CACHED_LENGTH();
1441
8f93a29f 1442 m_impl.resize(nSize, (wxStringCharType)ch);
68482dc5 1443 }
8f93a29f 1444 }
e90c1d2a 1445
8f93a29f
VS
1446 wxString substr(size_t nStart = 0, size_t nLen = npos) const
1447 {
1448 size_t pos, len;
1449 PosLenToImpl(nStart, nLen, &pos, &len);
59953bf4 1450 return FromImpl(m_impl.substr(pos, len));
8f93a29f 1451 }
e90c1d2a 1452
3c67202d
VZ
1453 // generic attributes & operations
1454 // as standard strlen()
e87b7833 1455 size_t Len() const { return length(); }
3c67202d 1456 // string contains any characters?
e87b7833 1457 bool IsEmpty() const { return empty(); }
d775fa82 1458 // empty string is "false", so !str will return true
20aa779e 1459 bool operator!() const { return empty(); }
735d1db6
VZ
1460 // truncate the string to given length
1461 wxString& Truncate(size_t uiLen);
3c67202d 1462 // empty string contents
24f86c43 1463 void Empty() { clear(); }
3c67202d 1464 // empty the string and free memory
68482dc5 1465 void Clear() { clear(); }
dd1eaa89 1466
3c67202d
VZ
1467 // contents test
1468 // Is an ascii value
c801d85f 1469 bool IsAscii() const;
3c67202d 1470 // Is a number
c801d85f 1471 bool IsNumber() const;
3c67202d 1472 // Is a word
c801d85f 1473 bool IsWord() const;
c801d85f 1474
3c67202d
VZ
1475 // data access (all indexes are 0 based)
1476 // read access
8f93a29f 1477 wxUniChar at(size_t n) const
68482dc5 1478 { return wxStringOperations::DecodeChar(m_impl.begin() + PosToImpl(n)); }
c9f78968 1479 wxUniChar GetChar(size_t n) const
9d9ad673 1480 { return at(n); }
3c67202d 1481 // read/write access
8f93a29f 1482 wxUniCharRef at(size_t n)
68482dc5 1483 { return *GetIterForNthChar(n); }
c9f78968 1484 wxUniCharRef GetWritableChar(size_t n)
9d9ad673 1485 { return at(n); }
3c67202d 1486 // write access
68482dc5 1487 void SetChar(size_t n, wxUniChar ch)
dd79ca2f 1488 { at(n) = ch; }
c801d85f 1489
3c67202d 1490 // get last character
8f93a29f 1491 wxUniChar Last() const
564ab31a
VS
1492 {
1493 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1494 return *rbegin();
1495 }
09443a26 1496
3c67202d 1497 // get writable last character
c9f78968 1498 wxUniCharRef Last()
564ab31a
VS
1499 {
1500 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1501 return *rbegin();
1502 }
c801d85f 1503
d836ee96 1504 /*
9d9ad673 1505 Note that we we must define all of the overloads below to avoid
c9f78968 1506 ambiguity when using str[0].
d836ee96 1507 */
c9f78968 1508 wxUniChar operator[](int n) const
8f93a29f 1509 { return at(n); }
c1df0a3b 1510 wxUniChar operator[](long n) const
8f93a29f 1511 { return at(n); }
c9f78968 1512 wxUniChar operator[](size_t n) const
8f93a29f 1513 { return at(n); }
01398433 1514#ifndef wxSIZE_T_IS_UINT
c9f78968 1515 wxUniChar operator[](unsigned int n) const
8f93a29f 1516 { return at(n); }
01398433 1517#endif // size_t != unsigned int
01398433 1518
9d9ad673 1519 // operator versions of GetWriteableChar()
c9f78968 1520 wxUniCharRef operator[](int n)
8f93a29f 1521 { return at(n); }
c1df0a3b 1522 wxUniCharRef operator[](long n)
8f93a29f 1523 { return at(n); }
c9f78968 1524 wxUniCharRef operator[](size_t n)
8f93a29f 1525 { return at(n); }
d836ee96 1526#ifndef wxSIZE_T_IS_UINT
c9f78968 1527 wxUniCharRef operator[](unsigned int n)
8f93a29f 1528 { return at(n); }
d836ee96 1529#endif // size_t != unsigned int
c801d85f 1530
18666b42
VZ
1531
1532 /*
1533 Overview of wxString conversions, implicit and explicit:
1534
1535 - wxString has a std::[w]string-like c_str() method, however it does
1536 not return a C-style string directly but instead returns wxCStrData
1537 helper object which is convertible to either "char *" narrow string
1538 or "wchar_t *" wide string. Usually the correct conversion will be
1539 applied by the compiler automatically but if this doesn't happen you
1540 need to explicitly choose one using wxCStrData::AsChar() or AsWChar()
1541 methods or another wxString conversion function.
1542
1543 - One of the places where the conversion does *NOT* happen correctly is
1544 when c_str() is passed to a vararg function such as printf() so you
1545 must *NOT* use c_str() with them. Either use wxPrintf() (all wx
1546 functions do handle c_str() correctly, even if they appear to be
1547 vararg (but they're not, really)) or add an explicit AsChar() or, if
1548 compatibility with previous wxWidgets versions is important, add a
1549 cast to "const char *".
1550
1551 - In non-STL mode only, wxString is also implicitly convertible to
1552 wxCStrData. The same warning as above applies.
1553
1554 - c_str() is polymorphic as it can be converted to either narrow or
1555 wide string. If you explicitly need one or the other, choose to use
1556 mb_str() (for narrow) or wc_str() (for wide) instead. Notice that
1557 these functions can return either the pointer to string directly (if
1558 this is what the string uses internally) or a temporary buffer
1559 containing the string and convertible to it. Again, conversion will
1560 usually be done automatically by the compiler but beware of the
1561 vararg functions: you need an explicit cast when using them.
1562
1563 - There are also non-const versions of mb_str() and wc_str() called
1564 char_str() and wchar_str(). They are only meant to be used with
1565 non-const-correct functions and they always return buffers.
1566
1567 - Finally wx_str() returns whatever string representation is used by
1568 wxString internally. It may be either a narrow or wide string
1569 depending on wxWidgets build mode but it will always be a raw pointer
1570 (and not a buffer).
1571 */
1572
1573 // explicit conversion to wxCStrData
c9f78968 1574 wxCStrData c_str() const { return wxCStrData(this); }
8f93a29f 1575 wxCStrData data() const { return c_str(); }
c9f78968 1576
18666b42 1577 // implicit conversion to wxCStrData
c9f78968 1578 operator wxCStrData() const { return c_str(); }
7978bc72 1579
4f167b46
VZ
1580 // the first two operators conflict with operators for conversion to
1581 // std::string and they must be disabled in STL build; the next one only
1582 // makes sense if conversions to char* are also defined and not defining it
1583 // in STL build also helps us to get more clear error messages for the code
1584 // which relies on implicit conversion to char* in STL build
7978bc72 1585#if !wxUSE_STL
11aac4ba
VS
1586 operator const char*() const { return c_str(); }
1587 operator const wchar_t*() const { return c_str(); }
e015c2a3 1588
353a4edc
VZ
1589 // implicit conversion to untyped pointer for compatibility with previous
1590 // wxWidgets versions: this is the same as conversion to const char * so it
1591 // may fail!
1592 operator const void*() const { return c_str(); }
4f167b46 1593#endif // wxUSE_STL
353a4edc 1594
e015c2a3 1595 // identical to c_str(), for MFC compatibility
c9f78968
VS
1596 const wxCStrData GetData() const { return c_str(); }
1597
1598 // explicit conversion to C string in internal representation (char*,
1599 // wchar_t*, UTF-8-encoded char*, depending on the build):
81727065 1600 const wxStringCharType *wx_str() const { return m_impl.c_str(); }
e90c1d2a 1601
ef0f1387 1602 // conversion to *non-const* multibyte or widestring buffer; modifying
c67b782d
VS
1603 // returned buffer won't affect the string, these methods are only useful
1604 // for passing values to const-incorrect functions
8060b0be 1605 wxWritableCharBuffer char_str(const wxMBConv& conv = wxConvLibc) const
c67b782d
VS
1606 { return mb_str(conv); }
1607 wxWritableWCharBuffer wchar_str() const { return wc_str(); }
ef0f1387 1608
062dc5fc
VZ
1609 // conversion to the buffer of the given type T (= char or wchar_t) and
1610 // also optionally return the buffer length
1611 //
1612 // this is mostly/only useful for the template functions
1613 //
1614 // FIXME-VC6: the second argument only exists for VC6 which doesn't support
1615 // explicit template function selection, do not use it unless
1616 // you must support VC6!
1617 template <typename T>
1618 wxCharTypeBuffer<T> tchar_str(size_t *len = NULL,
1619 T * WXUNUSED(dummy) = NULL) const
1620 {
1621#if wxUSE_UNICODE
1622 // we need a helper dispatcher depending on type
1623 return wxPrivate::wxStringAsBufHelper<T>::Get(*this, len);
1624#else // ANSI
1625 // T can only be char in ANSI build
1626 if ( len )
1627 *len = length();
1628
1629 return wxCharTypeBuffer<T>::CreateNonOwned(wx_str());
1630#endif // Unicode build kind
1631 }
1632
e015c2a3
VZ
1633 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
1634 // converting numbers or strings which are certain not to contain special
1635 // chars (typically system functions, X atoms, environment variables etc.)
1636 //
1637 // the behaviour of these functions with the strings containing anything
1638 // else than 7 bit ASCII characters is undefined, use at your own risk.
b1ac3b56 1639#if wxUSE_UNICODE
628f9e95
VZ
1640 static wxString FromAscii(const char *ascii, size_t len);
1641 static wxString FromAscii(const char *ascii);
1642 static wxString FromAscii(char ascii);
b1ac3b56 1643 const wxCharBuffer ToAscii() const;
e015c2a3
VZ
1644#else // ANSI
1645 static wxString FromAscii(const char *ascii) { return wxString( ascii ); }
e6310bbc
VS
1646 static wxString FromAscii(const char *ascii, size_t len)
1647 { return wxString( ascii, len ); }
628f9e95 1648 static wxString FromAscii(char ascii) { return wxString( ascii ); }
e015c2a3
VZ
1649 const char *ToAscii() const { return c_str(); }
1650#endif // Unicode/!Unicode
b1ac3b56 1651
628f9e95
VZ
1652 // also provide unsigned char overloads as signed/unsigned doesn't matter
1653 // for 7 bit ASCII characters
1654 static wxString FromAscii(const unsigned char *ascii)
1655 { return FromAscii((const char *)ascii); }
1656 static wxString FromAscii(const unsigned char *ascii, size_t len)
1657 { return FromAscii((const char *)ascii, len); }
1658
5f167b77
VS
1659 // conversion to/from UTF-8:
1660#if wxUSE_UNICODE_UTF8
cc209a51 1661 static wxString FromUTF8Unchecked(const char *utf8)
5f167b77 1662 {
f966a73d
VS
1663 if ( !utf8 )
1664 return wxEmptyString;
1665
5f167b77
VS
1666 wxASSERT( wxStringOperations::IsValidUtf8String(utf8) );
1667 return FromImpl(wxStringImpl(utf8));
1668 }
cc209a51 1669 static wxString FromUTF8Unchecked(const char *utf8, size_t len)
5f167b77 1670 {
f966a73d
VS
1671 if ( !utf8 )
1672 return wxEmptyString;
1673 if ( len == npos )
cc209a51 1674 return FromUTF8Unchecked(utf8);
f966a73d 1675
5f167b77
VS
1676 wxASSERT( wxStringOperations::IsValidUtf8String(utf8, len) );
1677 return FromImpl(wxStringImpl(utf8, len));
1678 }
cc209a51
VZ
1679
1680 static wxString FromUTF8(const char *utf8)
1681 {
1682 if ( !utf8 || !wxStringOperations::IsValidUtf8String(utf8) )
1683 return "";
1684
1685 return FromImpl(wxStringImpl(utf8));
1686 }
1687 static wxString FromUTF8(const char *utf8, size_t len)
1688 {
1689 if ( len == npos )
1690 return FromUTF8(utf8);
1691
1692 if ( !utf8 || !wxStringOperations::IsValidUtf8String(utf8, len) )
1693 return "";
1694
1695 return FromImpl(wxStringImpl(utf8, len));
1696 }
1697
5f167b77
VS
1698 const char* utf8_str() const { return wx_str(); }
1699 const char* ToUTF8() const { return wx_str(); }
062dc5fc
VZ
1700
1701 // this function exists in UTF-8 build only and returns the length of the
1702 // internal UTF-8 representation
1703 size_t utf8_length() const { return m_impl.length(); }
5f167b77 1704#elif wxUSE_UNICODE_WCHAR
cc209a51 1705 static wxString FromUTF8(const char *utf8, size_t len = npos)
5487ff0f 1706 { return wxString(utf8, wxMBConvUTF8(), len); }
cc209a51
VZ
1707 static wxString FromUTF8Unchecked(const char *utf8, size_t len = npos)
1708 {
1709 const wxString s(utf8, wxMBConvUTF8(), len);
1710 wxASSERT_MSG( !utf8 || !*utf8 || !s.empty(),
1711 "string must be valid UTF-8" );
1712 return s;
1713 }
5487ff0f 1714 const wxCharBuffer utf8_str() const { return mb_str(wxMBConvUTF8()); }
5f167b77
VS
1715 const wxCharBuffer ToUTF8() const { return utf8_str(); }
1716#else // ANSI
1717 static wxString FromUTF8(const char *utf8)
5487ff0f 1718 { return wxString(wxMBConvUTF8().cMB2WC(utf8)); }
5f167b77 1719 static wxString FromUTF8(const char *utf8, size_t len)
06c73b98 1720 {
96e93f5b
VZ
1721 size_t wlen;
1722 wxWCharBuffer buf(wxMBConvUTF8().cMB2WC(utf8, len == npos ? wxNO_LEN : len, &wlen));
1723 return wxString(buf.data(), wlen);
1724 }
1725 static wxString FromUTF8Unchecked(const char *utf8, size_t len = npos)
1726 {
1727 size_t wlen;
1728 wxWCharBuffer buf(wxMBConvUTF8().cMB2WC(utf8,
1729 len == npos ? wxNO_LEN : len,
1730 &wlen));
1731 wxASSERT_MSG( !utf8 || !*utf8 || wlen,
1732 "string must be valid UTF-8" );
1733
1734 return wxString(buf.data(), wlen);
06c73b98 1735 }
5487ff0f
VS
1736 const wxCharBuffer utf8_str() const
1737 { return wxMBConvUTF8().cWC2MB(wc_str()); }
5f167b77
VS
1738 const wxCharBuffer ToUTF8() const { return utf8_str(); }
1739#endif
1740
cb2f2135
VS
1741 // functions for storing binary data in wxString:
1742#if wxUSE_UNICODE
1743 static wxString From8BitData(const char *data, size_t len)
1744 { return wxString(data, wxConvISO8859_1, len); }
1745 // version for NUL-terminated data:
1746 static wxString From8BitData(const char *data)
1747 { return wxString(data, wxConvISO8859_1); }
1748 const wxCharBuffer To8BitData() const { return mb_str(wxConvISO8859_1); }
1749#else // ANSI
1750 static wxString From8BitData(const char *data, size_t len)
1751 { return wxString(data, len); }
1752 // version for NUL-terminated data:
1753 static wxString From8BitData(const char *data)
1754 { return wxString(data); }
1755 const char *To8BitData() const { return c_str(); }
1756#endif // Unicode/ANSI
1757
2b5f62a0 1758 // conversions with (possible) format conversions: have to return a
e90c1d2a 1759 // buffer with temporary data
f6bcfd97
BP
1760 //
1761 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
1762 // return an ANSI (multibyte) string, wc_str() to return a wide string and
1763 // fn_str() to return a string which should be used with the OS APIs
1764 // accepting the file names. The return value is always the same, but the
1765 // type differs because a function may either return pointer to the buffer
1766 // directly or have to use intermediate buffer for translation.
2bb67b80 1767#if wxUSE_UNICODE
111d9948
VS
1768
1769#if wxUSE_UTF8_LOCALE_ONLY
1770 const char* mb_str() const { return wx_str(); }
1771 const wxCharBuffer mb_str(const wxMBConv& conv) const;
1772#else
830f8f11 1773 const wxCharBuffer mb_str(const wxMBConv& conv = wxConvLibc) const;
111d9948 1774#endif
f6bcfd97 1775
e90c1d2a
VZ
1776 const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); }
1777
81727065 1778#if wxUSE_UNICODE_WCHAR
6ad68ad8 1779 const wchar_t* wc_str() const { return wx_str(); }
81727065
VS
1780#elif wxUSE_UNICODE_UTF8
1781 const wxWCharBuffer wc_str() const;
1782#endif
f6bcfd97 1783 // for compatibility with !wxUSE_UNICODE version
81727065
VS
1784 const wxWX2WCbuf wc_str(const wxMBConv& WXUNUSED(conv)) const
1785 { return wc_str(); }
e90c1d2a 1786
2bb67b80 1787#if wxMBFILES
5f709e67 1788 const wxCharBuffer fn_str() const { return mb_str(wxConvFile); }
e90c1d2a 1789#else // !wxMBFILES
81727065 1790 const wxWX2WCbuf fn_str() const { return wc_str(); }
e90c1d2a 1791#endif // wxMBFILES/!wxMBFILES
81727065 1792
e90c1d2a 1793#else // ANSI
81727065 1794 const wxChar* mb_str() const { return wx_str(); }
f6bcfd97
BP
1795
1796 // for compatibility with wxUSE_UNICODE version
6ad68ad8 1797 const char* mb_str(const wxMBConv& WXUNUSED(conv)) const { return wx_str(); }
f6bcfd97 1798
e90c1d2a 1799 const wxWX2MBbuf mbc_str() const { return mb_str(); }
f6bcfd97 1800
6f841509 1801#if wxUSE_WCHAR_T
ef0f1387 1802 const wxWCharBuffer wc_str(const wxMBConv& conv = wxConvLibc) const;
e90c1d2a 1803#endif // wxUSE_WCHAR_T
c47e732c 1804 const wxCharBuffer fn_str() const { return wxConvFile.cWC2WX( wc_str( wxConvLibc ) ); }
e90c1d2a 1805#endif // Unicode/ANSI
c801d85f 1806
6ad68ad8
RR
1807#if wxUSE_UNICODE_UTF8
1808 const wxWCharBuffer t_str() const { return wc_str(); }
1809#elif wxUSE_UNICODE_WCHAR
1810 const wchar_t* t_str() const { return wx_str(); }
1811#else
1812 const char* t_str() const { return wx_str(); }
cbec0f40 1813#endif
6ad68ad8
RR
1814
1815
3c67202d
VZ
1816 // overloaded assignment
1817 // from another wxString
04abe4bc 1818 wxString& operator=(const wxString& stringSrc)
68482dc5
VZ
1819 {
1820 if ( this != &stringSrc )
1821 {
1822 wxSTRING_INVALIDATE_CACHE();
1823
1824 m_impl = stringSrc.m_impl;
1825 }
1826
1827 return *this;
1828 }
1829
8f93a29f
VS
1830 wxString& operator=(const wxCStrData& cstr)
1831 { return *this = cstr.AsString(); }
3c67202d 1832 // from a character
c9f78968 1833 wxString& operator=(wxUniChar ch)
84d2877f 1834 {
68482dc5
VZ
1835 wxSTRING_INVALIDATE_CACHE();
1836
84d2877f
VS
1837#if wxUSE_UNICODE_UTF8
1838 if ( !ch.IsAscii() )
1839 m_impl = wxStringOperations::EncodeChar(ch);
1840 else
68482dc5 1841#endif // wxUSE_UNICODE_UTF8
84d2877f
VS
1842 m_impl = (wxStringCharType)ch;
1843 return *this;
1844 }
68482dc5 1845
c9f78968 1846 wxString& operator=(wxUniCharRef ch)
8f93a29f 1847 { return operator=((wxUniChar)ch); }
c9f78968 1848 wxString& operator=(char ch)
8f93a29f 1849 { return operator=(wxUniChar(ch)); }
50e02008
VS
1850 wxString& operator=(unsigned char ch)
1851 { return operator=(wxUniChar(ch)); }
c9f78968 1852 wxString& operator=(wchar_t ch)
8f93a29f 1853 { return operator=(wxUniChar(ch)); }
c57e3bd5
RN
1854 // from a C string - STL probably will crash on NULL,
1855 // so we need to compensate in that case
992527a5 1856#if wxUSE_STL_BASED_WXSTRING
04abe4bc 1857 wxString& operator=(const char *psz)
68482dc5
VZ
1858 {
1859 wxSTRING_INVALIDATE_CACHE();
1860
1861 if ( psz )
1862 m_impl = ImplStr(psz);
1863 else
1864 clear();
1865
1866 return *this;
1867 }
1868
04abe4bc 1869 wxString& operator=(const wchar_t *pwz)
68482dc5
VZ
1870 {
1871 wxSTRING_INVALIDATE_CACHE();
1872
1873 if ( pwz )
1874 m_impl = ImplStr(pwz);
1875 else
1876 clear();
1877
1878 return *this;
1879 }
1880#else // !wxUSE_STL_BASED_WXSTRING
04abe4bc 1881 wxString& operator=(const char *psz)
68482dc5
VZ
1882 {
1883 wxSTRING_INVALIDATE_CACHE();
1884
1885 m_impl = ImplStr(psz);
1886
1887 return *this;
1888 }
1889
04abe4bc 1890 wxString& operator=(const wchar_t *pwz)
68482dc5
VZ
1891 {
1892 wxSTRING_INVALIDATE_CACHE();
1893
1894 m_impl = ImplStr(pwz);
1895
1896 return *this;
1897 }
1898#endif // wxUSE_STL_BASED_WXSTRING/!wxUSE_STL_BASED_WXSTRING
1899
04abe4bc
VS
1900 wxString& operator=(const unsigned char *psz)
1901 { return operator=((const char*)psz); }
c57e3bd5 1902
111bb7f2 1903 // from wxWCharBuffer
8f93a29f 1904 wxString& operator=(const wxWCharBuffer& s)
04abe4bc 1905 { return operator=(s.data()); } // FIXME-UTF8: fix for embedded NULs
111bb7f2 1906 // from wxCharBuffer
04abe4bc
VS
1907 wxString& operator=(const wxCharBuffer& s)
1908 { return operator=(s.data()); } // FIXME-UTF8: fix for embedded NULs
3c67202d
VZ
1909
1910 // string concatenation
1911 // in place concatenation
1912 /*
1913 Concatenate and return the result. Note that the left to right
1914 associativity of << allows to write things like "str << str1 << str2
1915 << ..." (unlike with +=)
1916 */
1917 // string += string
dd1eaa89
VZ
1918 wxString& operator<<(const wxString& s)
1919 {
8f93a29f
VS
1920#if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1921 wxASSERT_MSG( s.IsValid(),
09443a26 1922 _T("did you forget to call UngetWriteBuf()?") );
e87b7833 1923#endif
dd1eaa89 1924
e87b7833 1925 append(s);
dd1eaa89
VZ
1926 return *this;
1927 }
3c67202d 1928 // string += C string
8f93a29f 1929 wxString& operator<<(const char *psz)
c9f78968 1930 { append(psz); return *this; }
8f93a29f
VS
1931 wxString& operator<<(const wchar_t *pwz)
1932 { append(pwz); return *this; }
c9f78968 1933 wxString& operator<<(const wxCStrData& psz)
8f93a29f 1934 { append(psz.AsString()); return *this; }
3c67202d 1935 // string += char
c9f78968
VS
1936 wxString& operator<<(wxUniChar ch) { append(1, ch); return *this; }
1937 wxString& operator<<(wxUniCharRef ch) { append(1, ch); return *this; }
1938 wxString& operator<<(char ch) { append(1, ch); return *this; }
50e02008 1939 wxString& operator<<(unsigned char ch) { append(1, ch); return *this; }
c9f78968 1940 wxString& operator<<(wchar_t ch) { append(1, ch); return *this; }
2bb67b80
OK
1941
1942 // string += buffer (i.e. from wxGetString)
09443a26 1943 wxString& operator<<(const wxWCharBuffer& s)
d7330233 1944 { return operator<<((const wchar_t *)s); }
09443a26 1945 wxString& operator<<(const wxCharBuffer& s)
d7330233 1946 { return operator<<((const char *)s); }
d7330233 1947
3c67202d 1948 // string += C string
09443a26
VZ
1949 wxString& Append(const wxString& s)
1950 {
5a8231ef
WS
1951 // test for empty() to share the string if possible
1952 if ( empty() )
09443a26
VZ
1953 *this = s;
1954 else
e87b7833 1955 append(s);
09443a26
VZ
1956 return *this;
1957 }
8f93a29f 1958 wxString& Append(const char* psz)
e87b7833 1959 { append(psz); return *this; }
8f93a29f
VS
1960 wxString& Append(const wchar_t* pwz)
1961 { append(pwz); return *this; }
d3cefe87
VS
1962 wxString& Append(const wxCStrData& psz)
1963 { append(psz); return *this; }
1964 wxString& Append(const wxCharBuffer& psz)
1965 { append(psz); return *this; }
1966 wxString& Append(const wxWCharBuffer& psz)
1967 { append(psz); return *this; }
f416695a
VS
1968 wxString& Append(const char* psz, size_t nLen)
1969 { append(psz, nLen); return *this; }
1970 wxString& Append(const wchar_t* pwz, size_t nLen)
1971 { append(pwz, nLen); return *this; }
1972 wxString& Append(const wxCStrData& psz, size_t nLen)
1973 { append(psz, nLen); return *this; }
1974 wxString& Append(const wxCharBuffer& psz, size_t nLen)
1975 { append(psz, nLen); return *this; }
1976 wxString& Append(const wxWCharBuffer& psz, size_t nLen)
1977 { append(psz, nLen); return *this; }
3c67202d 1978 // append count copies of given character
c9f78968
VS
1979 wxString& Append(wxUniChar ch, size_t count = 1u)
1980 { append(count, ch); return *this; }
1981 wxString& Append(wxUniCharRef ch, size_t count = 1u)
1982 { append(count, ch); return *this; }
1983 wxString& Append(char ch, size_t count = 1u)
1984 { append(count, ch); return *this; }
50e02008
VS
1985 wxString& Append(unsigned char ch, size_t count = 1u)
1986 { append(count, ch); return *this; }
c9f78968 1987 wxString& Append(wchar_t ch, size_t count = 1u)
e87b7833 1988 { append(count, ch); return *this; }
3c67202d
VZ
1989
1990 // prepend a string, return the string itself
1991 wxString& Prepend(const wxString& str)
1992 { *this = str + *this; return *this; }
1993
1994 // non-destructive concatenation
1fc8cfbd
VZ
1995 // two strings
1996 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string1,
1997 const wxString& string2);
1998 // string with a single char
c9f78968 1999 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch);
1fc8cfbd 2000 // char with a string
c9f78968 2001 friend wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string);
1fc8cfbd
VZ
2002 // string with C string
2003 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string,
8f93a29f
VS
2004 const char *psz);
2005 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string,
2006 const wchar_t *pwz);
1fc8cfbd 2007 // C string with string
8f93a29f
VS
2008 friend wxString WXDLLIMPEXP_BASE operator+(const char *psz,
2009 const wxString& string);
2010 friend wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz,
1fc8cfbd 2011 const wxString& string);
3c67202d
VZ
2012
2013 // stream-like functions
2014 // insert an int into string
3ce65f6c
VZ
2015 wxString& operator<<(int i)
2016 { return (*this) << Format(_T("%d"), i); }
2017 // insert an unsigned int into string
2018 wxString& operator<<(unsigned int ui)
2019 { return (*this) << Format(_T("%u"), ui); }
2020 // insert a long into string
2021 wxString& operator<<(long l)
2022 { return (*this) << Format(_T("%ld"), l); }
2023 // insert an unsigned long into string
2024 wxString& operator<<(unsigned long ul)
2025 { return (*this) << Format(_T("%lu"), ul); }
3fc1adbd
MW
2026#if defined wxLongLong_t && !defined wxLongLongIsLong
2027 // insert a long long if they exist and aren't longs
2028 wxString& operator<<(wxLongLong_t ll)
b7859132
MW
2029 {
2030 const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("d");
2031 return (*this) << Format(fmt, ll);
2032 }
3fc1adbd
MW
2033 // insert an unsigned long long
2034 wxString& operator<<(wxULongLong_t ull)
b7859132
MW
2035 {
2036 const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("u");
2037 return (*this) << Format(fmt , ull);
2038 }
68482dc5 2039#endif // wxLongLong_t && !wxLongLongIsLong
3c67202d 2040 // insert a float into string
3ce65f6c
VZ
2041 wxString& operator<<(float f)
2042 { return (*this) << Format(_T("%f"), f); }
3c67202d 2043 // insert a double into string
3ce65f6c
VZ
2044 wxString& operator<<(double d)
2045 { return (*this) << Format(_T("%g"), d); }
c84c52de 2046
3c67202d 2047 // string comparison
30b21f9a 2048 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
8f93a29f
VS
2049 int Cmp(const char *psz) const
2050 { return compare(psz); }
2051 int Cmp(const wchar_t *pwz) const
2052 { return compare(pwz); }
2053 int Cmp(const wxString& s) const
2054 { return compare(s); }
d3cefe87
VS
2055 int Cmp(const wxCStrData& s) const
2056 { return compare(s); }
2057 int Cmp(const wxCharBuffer& s) const
2058 { return compare(s); }
2059 int Cmp(const wxWCharBuffer& s) const
2060 { return compare(s); }
3c67202d 2061 // same as Cmp() but not case-sensitive
dcb68102 2062 int CmpNoCase(const wxString& s) const;
2712e317 2063
3c67202d
VZ
2064 // test for the string equality, either considering case or not
2065 // (if compareWithCase then the case matters)
11aac4ba 2066 bool IsSameAs(const wxString& str, bool compareWithCase = true) const
2712e317
VS
2067 {
2068#if !wxUSE_UNICODE_UTF8
2069 // in UTF-8 build, length() is O(n) and doing this would be _slower_
2070 if ( length() != str.length() )
2071 return false;
2072#endif
2073 return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0;
2074 }
11aac4ba
VS
2075 bool IsSameAs(const char *str, bool compareWithCase = true) const
2076 { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; }
2077 bool IsSameAs(const wchar_t *str, bool compareWithCase = true) const
2078 { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; }
2712e317 2079
d3cefe87
VS
2080 bool IsSameAs(const wxCStrData& str, bool compareWithCase = true) const
2081 { return IsSameAs(str.AsString(), compareWithCase); }
2082 bool IsSameAs(const wxCharBuffer& str, bool compareWithCase = true) const
2083 { return IsSameAs(str.data(), compareWithCase); }
2084 bool IsSameAs(const wxWCharBuffer& str, bool compareWithCase = true) const
2085 { return IsSameAs(str.data(), compareWithCase); }
20aa779e 2086 // comparison with a single character: returns true if equal
52de37c7 2087 bool IsSameAs(wxUniChar c, bool compareWithCase = true) const;
11aac4ba
VS
2088 // FIXME-UTF8: remove these overloads
2089 bool IsSameAs(wxUniCharRef c, bool compareWithCase = true) const
2090 { return IsSameAs(wxUniChar(c), compareWithCase); }
2091 bool IsSameAs(char c, bool compareWithCase = true) const
2092 { return IsSameAs(wxUniChar(c), compareWithCase); }
2093 bool IsSameAs(unsigned char c, bool compareWithCase = true) const
2094 { return IsSameAs(wxUniChar(c), compareWithCase); }
2095 bool IsSameAs(wchar_t c, bool compareWithCase = true) const
2096 { return IsSameAs(wxUniChar(c), compareWithCase); }
2097 bool IsSameAs(int c, bool compareWithCase = true) const
2098 { return IsSameAs(wxUniChar(c), compareWithCase); }
3c67202d
VZ
2099
2100 // simple sub-string extraction
2101 // return substring starting at nFirst of length nCount (or till the end
2102 // if nCount = default value)
e87b7833 2103 wxString Mid(size_t nFirst, size_t nCount = npos) const;
3c67202d 2104
f6bcfd97 2105 // operator version of Mid()
3c67202d
VZ
2106 wxString operator()(size_t start, size_t len) const
2107 { return Mid(start, len); }
2108
3affcd07
VZ
2109 // check if the string starts with the given prefix and return the rest
2110 // of the string in the provided pointer if it is not NULL; otherwise
2111 // return false
c5e7a7d7 2112 bool StartsWith(const wxString& prefix, wxString *rest = NULL) const;
3affcd07
VZ
2113 // check if the string ends with the given suffix and return the
2114 // beginning of the string before the suffix in the provided pointer if
2115 // it is not NULL; otherwise return false
c5e7a7d7 2116 bool EndsWith(const wxString& suffix, wxString *rest = NULL) const;
f6bcfd97 2117
3c67202d 2118 // get first nCount characters
c801d85f 2119 wxString Left(size_t nCount) const;
3c67202d 2120 // get last nCount characters
c801d85f 2121 wxString Right(size_t nCount) const;
7929902d 2122 // get all characters before the first occurrence of ch
3c67202d 2123 // (returns the whole string if ch not found)
c9f78968 2124 wxString BeforeFirst(wxUniChar ch) const;
7929902d 2125 // get all characters before the last occurrence of ch
3c67202d 2126 // (returns empty string if ch not found)
c9f78968 2127 wxString BeforeLast(wxUniChar ch) const;
7929902d 2128 // get all characters after the first occurrence of ch
3c67202d 2129 // (returns empty string if ch not found)
c9f78968 2130 wxString AfterFirst(wxUniChar ch) const;
7929902d 2131 // get all characters after the last occurrence of ch
3c67202d 2132 // (returns the whole string if ch not found)
c9f78968 2133 wxString AfterLast(wxUniChar ch) const;
3c67202d
VZ
2134
2135 // for compatibility only, use more explicitly named functions above
c9f78968
VS
2136 wxString Before(wxUniChar ch) const { return BeforeLast(ch); }
2137 wxString After(wxUniChar ch) const { return AfterFirst(ch); }
3c67202d
VZ
2138
2139 // case conversion
c84c52de 2140 // convert to upper case in place, return the string itself
c801d85f 2141 wxString& MakeUpper();
c84c52de 2142 // convert to upper case, return the copy of the string
0c7db140 2143 wxString Upper() const { return wxString(*this).MakeUpper(); }
c84c52de 2144 // convert to lower case in place, return the string itself
c801d85f 2145 wxString& MakeLower();
c84c52de 2146 // convert to lower case, return the copy of the string
0c7db140
VZ
2147 wxString Lower() const { return wxString(*this).MakeLower(); }
2148 // convert the first character to the upper case and the rest to the
2149 // lower one, return the modified string itself
2150 wxString& MakeCapitalized();
2151 // convert the first character to the upper case and the rest to the
2152 // lower one, return the copy of the string
2153 wxString Capitalize() const { return wxString(*this).MakeCapitalized(); }
c801d85f 2154
3c67202d
VZ
2155 // trimming/padding whitespace (either side) and truncating
2156 // remove spaces from left or from right (default) side
d775fa82 2157 wxString& Trim(bool bFromRight = true);
3c67202d 2158 // add nCount copies chPad in the beginning or at the end (default)
c9f78968 2159 wxString& Pad(size_t nCount, wxUniChar chPad = wxT(' '), bool bFromRight = true);
dd1eaa89 2160
3c67202d
VZ
2161 // searching and replacing
2162 // searching (return starting index, or -1 if not found)
c9f78968 2163 int Find(wxUniChar ch, bool bFromEnd = false) const; // like strchr/strrchr
8a540c88
VS
2164 int Find(wxUniCharRef ch, bool bFromEnd = false) const
2165 { return Find(wxUniChar(ch), bFromEnd); }
2166 int Find(char ch, bool bFromEnd = false) const
2167 { return Find(wxUniChar(ch), bFromEnd); }
2168 int Find(unsigned char ch, bool bFromEnd = false) const
2169 { return Find(wxUniChar(ch), bFromEnd); }
2170 int Find(wchar_t ch, bool bFromEnd = false) const
2171 { return Find(wxUniChar(ch), bFromEnd); }
3c67202d 2172 // searching (return starting index, or -1 if not found)
8a540c88
VS
2173 int Find(const wxString& sub) const // like strstr
2174 {
2175 size_type idx = find(sub);
2176 return (idx == npos) ? wxNOT_FOUND : (int)idx;
2177 }
2178 int Find(const char *sub) const // like strstr
2179 {
2180 size_type idx = find(sub);
2181 return (idx == npos) ? wxNOT_FOUND : (int)idx;
2182 }
2183 int Find(const wchar_t *sub) const // like strstr
2184 {
2185 size_type idx = find(sub);
2186 return (idx == npos) ? wxNOT_FOUND : (int)idx;
2187 }
2188
d3cefe87
VS
2189 int Find(const wxCStrData& sub) const
2190 { return Find(sub.AsString()); }
2191 int Find(const wxCharBuffer& sub) const
2192 { return Find(sub.data()); }
2193 int Find(const wxWCharBuffer& sub) const
2194 { return Find(sub.data()); }
2195
7929902d 2196 // replace first (or all of bReplaceAll) occurrences of substring with
3c67202d 2197 // another string, returns the number of replacements made
8a540c88
VS
2198 size_t Replace(const wxString& strOld,
2199 const wxString& strNew,
d775fa82 2200 bool bReplaceAll = true);
3c67202d
VZ
2201
2202 // check if the string contents matches a mask containing '*' and '?'
8a540c88 2203 bool Matches(const wxString& mask) const;
c801d85f 2204
d775fa82 2205 // conversion to numbers: all functions return true only if the whole
538f35cc
VZ
2206 // string is a number and put the value of this number into the pointer
2207 // provided, the base is the numeric base in which the conversion should be
2208 // done and must be comprised between 2 and 36 or be 0 in which case the
2209 // standard C rules apply (leading '0' => octal, "0x" => hex)
cd0b1709 2210 // convert to a signed integer
538f35cc 2211 bool ToLong(long *val, int base = 10) const;
cd0b1709 2212 // convert to an unsigned integer
538f35cc 2213 bool ToULong(unsigned long *val, int base = 10) const;
d6718dd1
VZ
2214 // convert to wxLongLong
2215#if defined(wxLongLong_t)
2216 bool ToLongLong(wxLongLong_t *val, int base = 10) const;
2217 // convert to wxULongLong
2218 bool ToULongLong(wxULongLong_t *val, int base = 10) const;
2219#endif // wxLongLong_t
cd0b1709
VZ
2220 // convert to a double
2221 bool ToDouble(double *val) const;
2222
d6718dd1 2223
c9f78968 2224#ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
90e572f1 2225 // formatted input/output
3c67202d 2226 // as sprintf(), returns the number of characters written or < 0 on error
7357f981 2227 // (take 'this' into account in attribute parameter count)
2523e9b7 2228 // int Printf(const wxString& format, ...);
1528e0b8 2229 WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const wxFormatString&),
d1f6e2cf 2230 DoPrintfWchar, DoPrintfUtf8)
2523e9b7 2231#ifdef __WATCOMC__
59a14f69
VS
2232 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
2233 WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wxString&),
2234 (wxFormatString(f1)));
2235 WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wxCStrData&),
2236 (wxFormatString(f1)));
2237 WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const char*),
2238 (wxFormatString(f1)));
2239 WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wchar_t*),
2240 (wxFormatString(f1)));
2523e9b7 2241#endif
c9f78968 2242#endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN
3c67202d 2243 // as vprintf(), returns the number of characters written or < 0 on error
c9f78968 2244 int PrintfV(const wxString& format, va_list argptr);
dd1eaa89 2245
c9f78968 2246#ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
341e7d28 2247 // returns the string containing the result of Printf() to it
2523e9b7 2248 // static wxString Format(const wxString& format, ...) ATTRIBUTE_PRINTF_1;
1528e0b8 2249 WX_DEFINE_VARARG_FUNC(static wxString, Format, 1, (const wxFormatString&),
d1f6e2cf 2250 DoFormatWchar, DoFormatUtf8)
2523e9b7
VS
2251#ifdef __WATCOMC__
2252 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
59a14f69
VS
2253 WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wxString&),
2254 (wxFormatString(f1)));
2255 WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wxCStrData&),
2256 (wxFormatString(f1)));
2257 WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const char*),
2258 (wxFormatString(f1)));
2259 WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wchar_t*),
2260 (wxFormatString(f1)));
2523e9b7 2261#endif
c9f78968 2262#endif
341e7d28 2263 // the same as above, but takes a va_list
c9f78968 2264 static wxString FormatV(const wxString& format, va_list argptr);
341e7d28 2265
3c67202d
VZ
2266 // raw access to string memory
2267 // ensure that string has space for at least nLen characters
dd1eaa89 2268 // only works if the data of this string is not shared
0367b928 2269 bool Alloc(size_t nLen) { reserve(nLen); return capacity() >= nLen; }
3c67202d 2270 // minimize the string's memory
dd1eaa89 2271 // only works if the data of this string is not shared
b1801e0e 2272 bool Shrink();
8f93a29f 2273#if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
d8a4b666
VS
2274 // These are deprecated, use wxStringBuffer or wxStringBufferLength instead
2275 //
3c67202d
VZ
2276 // get writable buffer of at least nLen bytes. Unget() *must* be called
2277 // a.s.a.p. to put string back in a reasonable state!
c87a0bc8 2278 wxDEPRECATED( wxStringCharType *GetWriteBuf(size_t nLen) );
3c67202d 2279 // call this immediately after GetWriteBuf() has been used
d8a4b666
VS
2280 wxDEPRECATED( void UngetWriteBuf() );
2281 wxDEPRECATED( void UngetWriteBuf(size_t nLen) );
8f93a29f 2282#endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && wxUSE_UNICODE_UTF8
c801d85f 2283
77ffb593 2284 // wxWidgets version 1 compatibility functions
3c67202d
VZ
2285
2286 // use Mid()
2287 wxString SubString(size_t from, size_t to) const
2288 { return Mid(from, (to - from + 1)); }
2289 // values for second parameter of CompareTo function
c801d85f 2290 enum caseCompare {exact, ignoreCase};
3c67202d 2291 // values for first parameter of Strip function
c801d85f 2292 enum stripType {leading = 0x1, trailing = 0x2, both = 0x3};
8870c26e 2293
c9f78968 2294#ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
7357f981
GD
2295 // use Printf()
2296 // (take 'this' into account in attribute parameter count)
2523e9b7 2297 // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2;
1528e0b8 2298 WX_DEFINE_VARARG_FUNC(int, sprintf, 1, (const wxFormatString&),
d1f6e2cf 2299 DoPrintfWchar, DoPrintfUtf8)
2523e9b7
VS
2300#ifdef __WATCOMC__
2301 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
59a14f69
VS
2302 WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wxString&),
2303 (wxFormatString(f1)));
2304 WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wxCStrData&),
2305 (wxFormatString(f1)));
2306 WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const char*),
2307 (wxFormatString(f1)));
2308 WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wchar_t*),
2309 (wxFormatString(f1)));
2523e9b7 2310#endif
c9f78968 2311#endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
c801d85f 2312
3c67202d 2313 // use Cmp()
68482dc5 2314 int CompareTo(const wxChar* psz, caseCompare cmp = exact) const
6b95b20d 2315 { return cmp == exact ? Cmp(psz) : CmpNoCase(psz); }
c801d85f 2316
8f93a29f 2317 // use length()
e87b7833 2318 size_t Length() const { return length(); }
3c67202d 2319 // Count the number of characters
c9f78968 2320 int Freq(wxUniChar ch) const;
3c67202d 2321 // use MakeLower
c801d85f 2322 void LowerCase() { MakeLower(); }
3c67202d 2323 // use MakeUpper
c801d85f 2324 void UpperCase() { MakeUpper(); }
3c67202d 2325 // use Trim except that it doesn't change this string
c801d85f
KB
2326 wxString Strip(stripType w = trailing) const;
2327
3c67202d 2328 // use Find (more general variants not yet supported)
2bb67b80 2329 size_t Index(const wxChar* psz) const { return Find(psz); }
c9f78968 2330 size_t Index(wxUniChar ch) const { return Find(ch); }
3c67202d 2331 // use Truncate
c801d85f 2332 wxString& Remove(size_t pos) { return Truncate(pos); }
e87b7833 2333 wxString& RemoveLast(size_t n = 1) { return Truncate(length() - n); }
c801d85f 2334
e87b7833
MB
2335 wxString& Remove(size_t nStart, size_t nLen)
2336 { return (wxString&)erase( nStart, nLen ); }
dd1eaa89 2337
3c67202d 2338 // use Find()
8f93a29f 2339 int First( wxUniChar ch ) const { return Find(ch); }
8a540c88 2340 int First( wxUniCharRef ch ) const { return Find(ch); }
c9f78968 2341 int First( char ch ) const { return Find(ch); }
50e02008 2342 int First( unsigned char ch ) const { return Find(ch); }
c9f78968 2343 int First( wchar_t ch ) const { return Find(ch); }
8a540c88 2344 int First( const wxString& str ) const { return Find(str); }
8f93a29f 2345 int Last( wxUniChar ch ) const { return Find(ch, true); }
d775fa82 2346 bool Contains(const wxString& str) const { return Find(str) != wxNOT_FOUND; }
c801d85f 2347
5a8231ef
WS
2348 // use empty()
2349 bool IsNull() const { return empty(); }
c801d85f 2350
3c67202d 2351 // std::string compatibility functions
dd1eaa89 2352
3c67202d
VZ
2353 // take nLen chars starting at nPos
2354 wxString(const wxString& str, size_t nPos, size_t nLen)
1545d942 2355 { assign(str, nPos, nLen); }
f2a1b1bd 2356 // take all characters from first to last
e87b7833 2357 wxString(const_iterator first, const_iterator last)
cf9a878b 2358 : m_impl(first.impl(), last.impl()) { }
67cff9dc
VZ
2359#if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2360 // the 2 overloads below are for compatibility with the existing code using
2361 // pointers instead of iterators
f2a1b1bd
VZ
2362 wxString(const char *first, const char *last)
2363 {
2364 SubstrBufFromMB str(ImplStr(first, last - first));
2365 m_impl.assign(str.data, str.len);
2366 }
2367 wxString(const wchar_t *first, const wchar_t *last)
2368 {
2369 SubstrBufFromWC str(ImplStr(first, last - first));
2370 m_impl.assign(str.data, str.len);
2371 }
67cff9dc
VZ
2372 // and this one is needed to compile code adding offsets to c_str() result
2373 wxString(const wxCStrData& first, const wxCStrData& last)
cf9a878b
VS
2374 : m_impl(CreateConstIterator(first).impl(),
2375 CreateConstIterator(last).impl())
67cff9dc
VZ
2376 {
2377 wxASSERT_MSG( first.m_str == last.m_str,
2378 _T("pointers must be into the same string") );
2379 }
2380#endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
5460b9e3 2381
3c67202d 2382 // lib.string.modifiers
3c67202d
VZ
2383 // append elements str[pos], ..., str[pos+n]
2384 wxString& append(const wxString& str, size_t pos, size_t n)
8f93a29f 2385 {
68482dc5
VZ
2386 wxSTRING_UPDATE_CACHED_LENGTH(n);
2387
2388 size_t from, len;
2389 str.PosLenToImpl(pos, n, &from, &len);
2390 m_impl.append(str.m_impl, from, len);
2391 return *this;
8f93a29f 2392 }
e87b7833
MB
2393 // append a string
2394 wxString& append(const wxString& str)
68482dc5
VZ
2395 {
2396 wxSTRING_UPDATE_CACHED_LENGTH(str.length());
2397
2398 m_impl.append(str.m_impl);
2399 return *this;
2400 }
2401
3c67202d 2402 // append first n (or all if n == npos) characters of sz
8f93a29f 2403 wxString& append(const char *sz)
68482dc5
VZ
2404 {
2405 wxSTRING_INVALIDATE_CACHED_LENGTH();
2406
2407 m_impl.append(ImplStr(sz));
2408 return *this;
2409 }
2410
8f93a29f 2411 wxString& append(const wchar_t *sz)
68482dc5
VZ
2412 {
2413 wxSTRING_INVALIDATE_CACHED_LENGTH();
2414
2415 m_impl.append(ImplStr(sz));
2416 return *this;
2417 }
2418
8f93a29f
VS
2419 wxString& append(const char *sz, size_t n)
2420 {
68482dc5
VZ
2421 wxSTRING_INVALIDATE_CACHED_LENGTH();
2422
2423 SubstrBufFromMB str(ImplStr(sz, n));
2424 m_impl.append(str.data, str.len);
2425 return *this;
8f93a29f
VS
2426 }
2427 wxString& append(const wchar_t *sz, size_t n)
2428 {
68482dc5
VZ
2429 wxSTRING_UPDATE_CACHED_LENGTH(n);
2430
2431 SubstrBufFromWC str(ImplStr(sz, n));
2432 m_impl.append(str.data, str.len);
2433 return *this;
8f93a29f 2434 }
d3cefe87
VS
2435
2436 wxString& append(const wxCStrData& str)
2437 { return append(str.AsString()); }
2438 wxString& append(const wxCharBuffer& str)
2439 { return append(str.data()); }
2440 wxString& append(const wxWCharBuffer& str)
2441 { return append(str.data()); }
f416695a
VS
2442 wxString& append(const wxCStrData& str, size_t n)
2443 { return append(str.AsString(), 0, n); }
2444 wxString& append(const wxCharBuffer& str, size_t n)
2445 { return append(str.data(), n); }
2446 wxString& append(const wxWCharBuffer& str, size_t n)
2447 { return append(str.data(), n); }
d3cefe87 2448
3c67202d 2449 // append n copies of ch
c9f78968 2450 wxString& append(size_t n, wxUniChar ch)
8f93a29f
VS
2451 {
2452#if wxUSE_UNICODE_UTF8
68482dc5
VZ
2453 if ( !ch.IsAscii() )
2454 {
2455 wxSTRING_INVALIDATE_CACHED_LENGTH();
2456
2457 m_impl.append(wxStringOperations::EncodeNChars(n, ch));
2458 }
2459 else // ASCII
8f93a29f 2460#endif
68482dc5
VZ
2461 {
2462 wxSTRING_UPDATE_CACHED_LENGTH(n);
2463
2464 m_impl.append(n, (wxStringCharType)ch);
2465 }
2466
2467 return *this;
8f93a29f 2468 }
68482dc5 2469
f416695a
VS
2470 wxString& append(size_t n, wxUniCharRef ch)
2471 { return append(n, wxUniChar(ch)); }
2472 wxString& append(size_t n, char ch)
2473 { return append(n, wxUniChar(ch)); }
2474 wxString& append(size_t n, unsigned char ch)
2475 { return append(n, wxUniChar(ch)); }
2476 wxString& append(size_t n, wchar_t ch)
2477 { return append(n, wxUniChar(ch)); }
2478
e87b7833
MB
2479 // append from first to last
2480 wxString& append(const_iterator first, const_iterator last)
68482dc5
VZ
2481 {
2482 wxSTRING_INVALIDATE_CACHED_LENGTH();
2483
2484 m_impl.append(first.impl(), last.impl());
2485 return *this;
2486 }
67cff9dc 2487#if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
f2a1b1bd
VZ
2488 wxString& append(const char *first, const char *last)
2489 { return append(first, last - first); }
2490 wxString& append(const wchar_t *first, const wchar_t *last)
2491 { return append(first, last - first); }
67cff9dc
VZ
2492 wxString& append(const wxCStrData& first, const wxCStrData& last)
2493 { return append(CreateConstIterator(first), CreateConstIterator(last)); }
2494#endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
3c67202d
VZ
2495
2496 // same as `this_string = str'
735d1db6 2497 wxString& assign(const wxString& str)
68482dc5
VZ
2498 {
2499 wxSTRING_SET_CACHED_LENGTH(str.length());
2500
2501 m_impl = str.m_impl;
2502
2503 return *this;
2504 }
2505
11aac4ba
VS
2506 wxString& assign(const wxString& str, size_t len)
2507 {
68482dc5
VZ
2508 wxSTRING_SET_CACHED_LENGTH(len);
2509
2510 m_impl.assign(str.m_impl, 0, str.LenToImpl(len));
2511
2512 return *this;
11aac4ba 2513 }
68482dc5 2514
3c67202d
VZ
2515 // same as ` = str[pos..pos + n]
2516 wxString& assign(const wxString& str, size_t pos, size_t n)
8f93a29f 2517 {
68482dc5
VZ
2518 size_t from, len;
2519 str.PosLenToImpl(pos, n, &from, &len);
2520 m_impl.assign(str.m_impl, from, len);
2521
2522 // it's important to call this after PosLenToImpl() above in case str is
2523 // the same string as this one
2524 wxSTRING_SET_CACHED_LENGTH(n);
2525
2526 return *this;
8f93a29f 2527 }
68482dc5 2528
3c67202d 2529 // same as `= first n (or all if n == npos) characters of sz'
8f93a29f 2530 wxString& assign(const char *sz)
68482dc5
VZ
2531 {
2532 wxSTRING_INVALIDATE_CACHE();
2533
2534 m_impl.assign(ImplStr(sz));
2535
2536 return *this;
2537 }
2538
8f93a29f 2539 wxString& assign(const wchar_t *sz)
68482dc5
VZ
2540 {
2541 wxSTRING_INVALIDATE_CACHE();
2542
2543 m_impl.assign(ImplStr(sz));
2544
2545 return *this;
2546 }
2547
8f93a29f
VS
2548 wxString& assign(const char *sz, size_t n)
2549 {
68482dc5
VZ
2550 wxSTRING_SET_CACHED_LENGTH(n);
2551
2552 SubstrBufFromMB str(ImplStr(sz, n));
2553 m_impl.assign(str.data, str.len);
2554
2555 return *this;
8f93a29f 2556 }
68482dc5 2557
8f93a29f
VS
2558 wxString& assign(const wchar_t *sz, size_t n)
2559 {
68482dc5
VZ
2560 wxSTRING_SET_CACHED_LENGTH(n);
2561
2562 SubstrBufFromWC str(ImplStr(sz, n));
2563 m_impl.assign(str.data, str.len);
2564
2565 return *this;
8f93a29f 2566 }
d3cefe87
VS
2567
2568 wxString& assign(const wxCStrData& str)
2569 { return assign(str.AsString()); }
2570 wxString& assign(const wxCharBuffer& str)
2571 { return assign(str.data()); }
2572 wxString& assign(const wxWCharBuffer& str)
2573 { return assign(str.data()); }
2574 wxString& assign(const wxCStrData& str, size_t len)
2575 { return assign(str.AsString(), len); }
2576 wxString& assign(const wxCharBuffer& str, size_t len)
2577 { return assign(str.data(), len); }
2578 wxString& assign(const wxWCharBuffer& str, size_t len)
2579 { return assign(str.data(), len); }
2580
3c67202d 2581 // same as `= n copies of ch'
c9f78968 2582 wxString& assign(size_t n, wxUniChar ch)
8f93a29f 2583 {
68482dc5
VZ
2584 wxSTRING_SET_CACHED_LENGTH(n);
2585
8f93a29f 2586#if wxUSE_UNICODE_UTF8
68482dc5
VZ
2587 if ( !ch.IsAscii() )
2588 m_impl.assign(wxStringOperations::EncodeNChars(n, ch));
2589 else
8f93a29f 2590#endif
68482dc5
VZ
2591 m_impl.assign(n, (wxStringCharType)ch);
2592
2593 return *this;
8f93a29f 2594 }
11aac4ba
VS
2595
2596 wxString& assign(size_t n, wxUniCharRef ch)
2597 { return assign(n, wxUniChar(ch)); }
2598 wxString& assign(size_t n, char ch)
2599 { return assign(n, wxUniChar(ch)); }
2600 wxString& assign(size_t n, unsigned char ch)
2601 { return assign(n, wxUniChar(ch)); }
2602 wxString& assign(size_t n, wchar_t ch)
2603 { return assign(n, wxUniChar(ch)); }
2604
e87b7833
MB
2605 // assign from first to last
2606 wxString& assign(const_iterator first, const_iterator last)
68482dc5
VZ
2607 {
2608 wxSTRING_INVALIDATE_CACHE();
2609
2610 m_impl.assign(first.impl(), last.impl());
2611
2612 return *this;
2613 }
67cff9dc 2614#if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
f2a1b1bd
VZ
2615 wxString& assign(const char *first, const char *last)
2616 { return assign(first, last - first); }
2617 wxString& assign(const wchar_t *first, const wchar_t *last)
2618 { return assign(first, last - first); }
67cff9dc
VZ
2619 wxString& assign(const wxCStrData& first, const wxCStrData& last)
2620 { return assign(CreateConstIterator(first), CreateConstIterator(last)); }
2621#endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
e87b7833
MB
2622
2623 // string comparison
8f93a29f 2624 int compare(const wxString& str) const;
d3cefe87
VS
2625 int compare(const char* sz) const;
2626 int compare(const wchar_t* sz) const;
2627 int compare(const wxCStrData& str) const
2628 { return compare(str.AsString()); }
2629 int compare(const wxCharBuffer& str) const
2630 { return compare(str.data()); }
2631 int compare(const wxWCharBuffer& str) const
2632 { return compare(str.data()); }
e87b7833 2633 // comparison with a substring
8f93a29f 2634 int compare(size_t nStart, size_t nLen, const wxString& str) const;
e87b7833
MB
2635 // comparison of 2 substrings
2636 int compare(size_t nStart, size_t nLen,
8f93a29f 2637 const wxString& str, size_t nStart2, size_t nLen2) const;
e87b7833
MB
2638 // substring comparison with first nCount characters of sz
2639 int compare(size_t nStart, size_t nLen,
8f93a29f
VS
2640 const char* sz, size_t nCount = npos) const;
2641 int compare(size_t nStart, size_t nLen,
2642 const wchar_t* sz, size_t nCount = npos) const;
3c67202d
VZ
2643
2644 // insert another string
e87b7833 2645 wxString& insert(size_t nPos, const wxString& str)
68482dc5 2646 { insert(GetIterForNthChar(nPos), str.begin(), str.end()); return *this; }
3c67202d
VZ
2647 // insert n chars of str starting at nStart (in str)
2648 wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n)
8f93a29f 2649 {
68482dc5
VZ
2650 wxSTRING_UPDATE_CACHED_LENGTH(n);
2651
2652 size_t from, len;
2653 str.PosLenToImpl(nStart, n, &from, &len);
2654 m_impl.insert(PosToImpl(nPos), str.m_impl, from, len);
2655
2656 return *this;
8f93a29f 2657 }
68482dc5 2658
3c67202d 2659 // insert first n (or all if n == npos) characters of sz
8f93a29f 2660 wxString& insert(size_t nPos, const char *sz)
68482dc5
VZ
2661 {
2662 wxSTRING_INVALIDATE_CACHE();
2663
2664 m_impl.insert(PosToImpl(nPos), ImplStr(sz));
2665
2666 return *this;
2667 }
2668
8f93a29f 2669 wxString& insert(size_t nPos, const wchar_t *sz)
68482dc5
VZ
2670 {
2671 wxSTRING_INVALIDATE_CACHE();
2672
2673 m_impl.insert(PosToImpl(nPos), ImplStr(sz)); return *this;
2674 }
2675
8f93a29f
VS
2676 wxString& insert(size_t nPos, const char *sz, size_t n)
2677 {
68482dc5
VZ
2678 wxSTRING_UPDATE_CACHED_LENGTH(n);
2679
2680 SubstrBufFromMB str(ImplStr(sz, n));
2681 m_impl.insert(PosToImpl(nPos), str.data, str.len);
2682
2683 return *this;
8f93a29f 2684 }
68482dc5 2685
8f93a29f
VS
2686 wxString& insert(size_t nPos, const wchar_t *sz, size_t n)
2687 {
68482dc5
VZ
2688 wxSTRING_UPDATE_CACHED_LENGTH(n);
2689
2690 SubstrBufFromWC str(ImplStr(sz, n));
2691 m_impl.insert(PosToImpl(nPos), str.data, str.len);
2692
2693 return *this;
8f93a29f 2694 }
68482dc5 2695
3c67202d 2696 // insert n copies of ch
c9f78968 2697 wxString& insert(size_t nPos, size_t n, wxUniChar ch)
8f93a29f 2698 {
68482dc5
VZ
2699 wxSTRING_UPDATE_CACHED_LENGTH(n);
2700
8f93a29f 2701#if wxUSE_UNICODE_UTF8
68482dc5
VZ
2702 if ( !ch.IsAscii() )
2703 m_impl.insert(PosToImpl(nPos), wxStringOperations::EncodeNChars(n, ch));
2704 else
8f93a29f 2705#endif
68482dc5
VZ
2706 m_impl.insert(PosToImpl(nPos), n, (wxStringCharType)ch);
2707 return *this;
8f93a29f 2708 }
68482dc5 2709
c9f78968 2710 iterator insert(iterator it, wxUniChar ch)
81727065 2711 {
68482dc5
VZ
2712 wxSTRING_UPDATE_CACHED_LENGTH(1);
2713
81727065 2714#if wxUSE_UNICODE_UTF8
68482dc5
VZ
2715 if ( !ch.IsAscii() )
2716 {
2717 size_t pos = IterToImplPos(it);
2718 m_impl.insert(pos, wxStringOperations::EncodeChar(ch));
2719 return iterator(this, m_impl.begin() + pos);
2720 }
2721 else
81727065 2722#endif
68482dc5 2723 return iterator(this, m_impl.insert(it.impl(), (wxStringCharType)ch));
81727065 2724 }
68482dc5 2725
e87b7833 2726 void insert(iterator it, const_iterator first, const_iterator last)
68482dc5
VZ
2727 {
2728 wxSTRING_INVALIDATE_CACHE();
2729
2730 m_impl.insert(it.impl(), first.impl(), last.impl());
2731 }
2732
67cff9dc 2733#if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
f2a1b1bd
VZ
2734 void insert(iterator it, const char *first, const char *last)
2735 { insert(it - begin(), first, last - first); }
67cff9dc 2736 void insert(iterator it, const wchar_t *first, const wchar_t *last)
f2a1b1bd 2737 { insert(it - begin(), first, last - first); }
67cff9dc
VZ
2738 void insert(iterator it, const wxCStrData& first, const wxCStrData& last)
2739 { insert(it, CreateConstIterator(first), CreateConstIterator(last)); }
2740#endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2741
c9f78968 2742 void insert(iterator it, size_type n, wxUniChar ch)
8f93a29f 2743 {
68482dc5
VZ
2744 wxSTRING_UPDATE_CACHED_LENGTH(n);
2745
8f93a29f 2746#if wxUSE_UNICODE_UTF8
68482dc5
VZ
2747 if ( !ch.IsAscii() )
2748 m_impl.insert(IterToImplPos(it), wxStringOperations::EncodeNChars(n, ch));
2749 else
8f93a29f 2750#endif
68482dc5 2751 m_impl.insert(it.impl(), n, (wxStringCharType)ch);
8f93a29f 2752 }
3c67202d
VZ
2753
2754 // delete characters from nStart to nStart + nLen
e87b7833 2755 wxString& erase(size_type pos = 0, size_type n = npos)
8f93a29f 2756 {
68482dc5
VZ
2757 wxSTRING_INVALIDATE_CACHE();
2758
2759 size_t from, len;
2760 PosLenToImpl(pos, n, &from, &len);
2761 m_impl.erase(from, len);
2762
2763 return *this;
8f93a29f 2764 }
68482dc5 2765
f2a1b1bd 2766 // delete characters from first up to last
e87b7833 2767 iterator erase(iterator first, iterator last)
68482dc5
VZ
2768 {
2769 wxSTRING_INVALIDATE_CACHE();
2770
2771 return iterator(this, m_impl.erase(first.impl(), last.impl()));
2772 }
2773
e87b7833 2774 iterator erase(iterator first)
68482dc5
VZ
2775 {
2776 wxSTRING_UPDATE_CACHED_LENGTH(-1);
2777
2778 return iterator(this, m_impl.erase(first.impl()));
2779 }
e87b7833
MB
2780
2781#ifdef wxSTRING_BASE_HASNT_CLEAR
2782 void clear() { erase(); }
8f93a29f 2783#else
68482dc5
VZ
2784 void clear()
2785 {
2786 wxSTRING_SET_CACHED_LENGTH(0);
2787
2788 m_impl.clear();
2789 }
e87b7833 2790#endif
3c67202d
VZ
2791
2792 // replaces the substring of length nLen starting at nStart
8f93a29f
VS
2793 wxString& replace(size_t nStart, size_t nLen, const char* sz)
2794 {
68482dc5
VZ
2795 wxSTRING_INVALIDATE_CACHE();
2796
2797 size_t from, len;
2798 PosLenToImpl(nStart, nLen, &from, &len);
2799 m_impl.replace(from, len, ImplStr(sz));
2800
2801 return *this;
8f93a29f 2802 }
68482dc5 2803
8f93a29f
VS
2804 wxString& replace(size_t nStart, size_t nLen, const wchar_t* sz)
2805 {
68482dc5
VZ
2806 wxSTRING_INVALIDATE_CACHE();
2807
2808 size_t from, len;
2809 PosLenToImpl(nStart, nLen, &from, &len);
2810 m_impl.replace(from, len, ImplStr(sz));
2811
2812 return *this;
8f93a29f 2813 }
68482dc5 2814
e87b7833
MB
2815 // replaces the substring of length nLen starting at nStart
2816 wxString& replace(size_t nStart, size_t nLen, const wxString& str)
8f93a29f 2817 {
68482dc5
VZ
2818 wxSTRING_INVALIDATE_CACHE();
2819
2820 size_t from, len;
2821 PosLenToImpl(nStart, nLen, &from, &len);
2822 m_impl.replace(from, len, str.m_impl);
2823
2824 return *this;
8f93a29f 2825 }
68482dc5 2826
3c67202d 2827 // replaces the substring with nCount copies of ch
c9f78968 2828 wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxUniChar ch)
8f93a29f 2829 {
68482dc5
VZ
2830 wxSTRING_INVALIDATE_CACHE();
2831
2832 size_t from, len;
2833 PosLenToImpl(nStart, nLen, &from, &len);
8f93a29f 2834#if wxUSE_UNICODE_UTF8
68482dc5
VZ
2835 if ( !ch.IsAscii() )
2836 m_impl.replace(from, len, wxStringOperations::EncodeNChars(nCount, ch));
2837 else
8f93a29f 2838#endif
68482dc5
VZ
2839 m_impl.replace(from, len, nCount, (wxStringCharType)ch);
2840
2841 return *this;
8f93a29f 2842 }
68482dc5 2843
3c67202d
VZ
2844 // replaces a substring with another substring
2845 wxString& replace(size_t nStart, size_t nLen,
e87b7833 2846 const wxString& str, size_t nStart2, size_t nLen2)
8f93a29f 2847 {
68482dc5 2848 wxSTRING_INVALIDATE_CACHE();
64a044d5 2849
68482dc5
VZ
2850 size_t from, len;
2851 PosLenToImpl(nStart, nLen, &from, &len);
64a044d5 2852
68482dc5
VZ
2853 size_t from2, len2;
2854 str.PosLenToImpl(nStart2, nLen2, &from2, &len2);
2855
2856 m_impl.replace(from, len, str.m_impl, from2, len2);
2857
2858 return *this;
8f93a29f 2859 }
68482dc5 2860
e87b7833 2861 // replaces the substring with first nCount chars of sz
fb20fa43 2862 wxString& replace(size_t nStart, size_t nLen,
8f93a29f
VS
2863 const char* sz, size_t nCount)
2864 {
68482dc5 2865 wxSTRING_INVALIDATE_CACHE();
64a044d5 2866
68482dc5
VZ
2867 size_t from, len;
2868 PosLenToImpl(nStart, nLen, &from, &len);
64a044d5 2869
68482dc5
VZ
2870 SubstrBufFromMB str(ImplStr(sz, nCount));
2871
2872 m_impl.replace(from, len, str.data, str.len);
2873
2874 return *this;
8f93a29f 2875 }
68482dc5 2876
8f93a29f
VS
2877 wxString& replace(size_t nStart, size_t nLen,
2878 const wchar_t* sz, size_t nCount)
2879 {
68482dc5 2880 wxSTRING_INVALIDATE_CACHE();
64a044d5 2881
68482dc5
VZ
2882 size_t from, len;
2883 PosLenToImpl(nStart, nLen, &from, &len);
64a044d5 2884
68482dc5
VZ
2885 SubstrBufFromWC str(ImplStr(sz, nCount));
2886
2887 m_impl.replace(from, len, str.data, str.len);
2888
2889 return *this;
8f93a29f 2890 }
68482dc5 2891
11aac4ba
VS
2892 wxString& replace(size_t nStart, size_t nLen,
2893 const wxString& s, size_t nCount)
2894 {
68482dc5
VZ
2895 wxSTRING_INVALIDATE_CACHE();
2896
2897 size_t from, len;
2898 PosLenToImpl(nStart, nLen, &from, &len);
2899 m_impl.replace(from, len, s.m_impl.c_str(), s.LenToImpl(nCount));
2900
2901 return *this;
11aac4ba
VS
2902 }
2903
8f93a29f 2904 wxString& replace(iterator first, iterator last, const char* s)
68482dc5
VZ
2905 {
2906 wxSTRING_INVALIDATE_CACHE();
2907
2908 m_impl.replace(first.impl(), last.impl(), ImplStr(s));
2909
2910 return *this;
2911 }
2912
8f93a29f 2913 wxString& replace(iterator first, iterator last, const wchar_t* s)
68482dc5
VZ
2914 {
2915 wxSTRING_INVALIDATE_CACHE();
2916
2917 m_impl.replace(first.impl(), last.impl(), ImplStr(s));
2918
2919 return *this;
2920 }
2921
8f93a29f
VS
2922 wxString& replace(iterator first, iterator last, const char* s, size_type n)
2923 {
68482dc5
VZ
2924 wxSTRING_INVALIDATE_CACHE();
2925
2926 SubstrBufFromMB str(ImplStr(s, n));
2927 m_impl.replace(first.impl(), last.impl(), str.data, str.len);
2928
2929 return *this;
8f93a29f 2930 }
68482dc5 2931
8f93a29f
VS
2932 wxString& replace(iterator first, iterator last, const wchar_t* s, size_type n)
2933 {
68482dc5
VZ
2934 wxSTRING_INVALIDATE_CACHE();
2935
2936 SubstrBufFromWC str(ImplStr(s, n));
2937 m_impl.replace(first.impl(), last.impl(), str.data, str.len);
2938
2939 return *this;
8f93a29f 2940 }
68482dc5 2941
e87b7833 2942 wxString& replace(iterator first, iterator last, const wxString& s)
68482dc5
VZ
2943 {
2944 wxSTRING_INVALIDATE_CACHE();
2945
2946 m_impl.replace(first.impl(), last.impl(), s.m_impl);
2947
2948 return *this;
2949 }
2950
8f93a29f
VS
2951 wxString& replace(iterator first, iterator last, size_type n, wxUniChar ch)
2952 {
68482dc5
VZ
2953 wxSTRING_INVALIDATE_CACHE();
2954
8f93a29f 2955#if wxUSE_UNICODE_UTF8
68482dc5
VZ
2956 if ( !ch.IsAscii() )
2957 m_impl.replace(first.impl(), last.impl(),
2958 wxStringOperations::EncodeNChars(n, ch));
2959 else
8f93a29f 2960#endif
68482dc5
VZ
2961 m_impl.replace(first.impl(), last.impl(), n, (wxStringCharType)ch);
2962
2963 return *this;
8f93a29f 2964 }
68482dc5 2965
e87b7833
MB
2966 wxString& replace(iterator first, iterator last,
2967 const_iterator first1, const_iterator last1)
cf9a878b 2968 {
68482dc5
VZ
2969 wxSTRING_INVALIDATE_CACHE();
2970
2971 m_impl.replace(first.impl(), last.impl(), first1.impl(), last1.impl());
2972
2973 return *this;
cf9a878b 2974 }
68482dc5 2975
f2a1b1bd
VZ
2976 wxString& replace(iterator first, iterator last,
2977 const char *first1, const char *last1)
2978 { replace(first, last, first1, last1 - first1); return *this; }
2979 wxString& replace(iterator first, iterator last,
2980 const wchar_t *first1, const wchar_t *last1)
2981 { replace(first, last, first1, last1 - first1); return *this; }
8f93a29f
VS
2982
2983 // swap two strings
2984 void swap(wxString& str)
68482dc5 2985 {
af912213
VZ
2986#if wxUSE_STRING_POS_CACHE
2987 // we modify not only this string but also the other one directly so we
2988 // need to invalidate cache for both of them (we could also try to
2989 // exchange their cache entries but it seems unlikely to be worth it)
2990 InvalidateCache();
2991 str.InvalidateCache();
2992#endif // wxUSE_STRING_POS_CACHE
68482dc5
VZ
2993
2994 m_impl.swap(str.m_impl);
2995 }
8f93a29f
VS
2996
2997 // find a substring
2998 size_t find(const wxString& str, size_t nStart = 0) const
2999 { return PosFromImpl(m_impl.find(str.m_impl, PosToImpl(nStart))); }
3000
3001 // find first n characters of sz
3002 size_t find(const char* sz, size_t nStart = 0, size_t n = npos) const
3003 {
3004 SubstrBufFromMB str(ImplStr(sz, n));
3005 return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len));
3006 }
3007 size_t find(const wchar_t* sz, size_t nStart = 0, size_t n = npos) const
3008 {
3009 SubstrBufFromWC str(ImplStr(sz, n));
3010 return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len));
3011 }
d3cefe87
VS
3012 size_t find(const wxCharBuffer& s, size_t nStart = 0, size_t n = npos) const
3013 { return find(s.data(), nStart, n); }
3014 size_t find(const wxWCharBuffer& s, size_t nStart = 0, size_t n = npos) const
3015 { return find(s.data(), nStart, n); }
3016 size_t find(const wxCStrData& s, size_t nStart = 0, size_t n = npos) const
3017 { return find(s.AsWChar(), nStart, n); }
8f93a29f 3018
7929902d 3019 // find the first occurrence of character ch after nStart
8f93a29f 3020 size_t find(wxUniChar ch, size_t nStart = 0) const
467175ab 3021 {
84d2877f
VS
3022#if wxUSE_UNICODE_UTF8
3023 if ( !ch.IsAscii() )
3024 return PosFromImpl(m_impl.find(wxStringOperations::EncodeChar(ch),
3025 PosToImpl(nStart)));
3026 else
3027#endif
3028 return PosFromImpl(m_impl.find((wxStringCharType)ch,
3029 PosToImpl(nStart)));
3030
467175ab 3031 }
8f93a29f
VS
3032 size_t find(wxUniCharRef ch, size_t nStart = 0) const
3033 { return find(wxUniChar(ch), nStart); }
3034 size_t find(char ch, size_t nStart = 0) const
3035 { return find(wxUniChar(ch), nStart); }
50e02008
VS
3036 size_t find(unsigned char ch, size_t nStart = 0) const
3037 { return find(wxUniChar(ch), nStart); }
8f93a29f
VS
3038 size_t find(wchar_t ch, size_t nStart = 0) const
3039 { return find(wxUniChar(ch), nStart); }
3040
3041 // rfind() family is exactly like find() but works right to left
3042
3043 // as find, but from the end
3044 size_t rfind(const wxString& str, size_t nStart = npos) const
3045 { return PosFromImpl(m_impl.rfind(str.m_impl, PosToImpl(nStart))); }
3046
3047 // as find, but from the end
3048 size_t rfind(const char* sz, size_t nStart = npos, size_t n = npos) const
3049 {
3050 SubstrBufFromMB str(ImplStr(sz, n));
3051 return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len));
3052 }
3053 size_t rfind(const wchar_t* sz, size_t nStart = npos, size_t n = npos) const
3054 {
3055 SubstrBufFromWC str(ImplStr(sz, n));
3056 return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len));
3057 }
d3cefe87
VS
3058 size_t rfind(const wxCharBuffer& s, size_t nStart = npos, size_t n = npos) const
3059 { return rfind(s.data(), nStart, n); }
3060 size_t rfind(const wxWCharBuffer& s, size_t nStart = npos, size_t n = npos) const
3061 { return rfind(s.data(), nStart, n); }
3062 size_t rfind(const wxCStrData& s, size_t nStart = npos, size_t n = npos) const
3063 { return rfind(s.AsWChar(), nStart, n); }
8f93a29f
VS
3064 // as find, but from the end
3065 size_t rfind(wxUniChar ch, size_t nStart = npos) const
467175ab 3066 {
84d2877f
VS
3067#if wxUSE_UNICODE_UTF8
3068 if ( !ch.IsAscii() )
3069 return PosFromImpl(m_impl.rfind(wxStringOperations::EncodeChar(ch),
3070 PosToImpl(nStart)));
3071 else
3072#endif
3073 return PosFromImpl(m_impl.rfind((wxStringCharType)ch,
3074 PosToImpl(nStart)));
467175ab 3075 }
8f93a29f
VS
3076 size_t rfind(wxUniCharRef ch, size_t nStart = npos) const
3077 { return rfind(wxUniChar(ch), nStart); }
3078 size_t rfind(char ch, size_t nStart = npos) const
3079 { return rfind(wxUniChar(ch), nStart); }
50e02008
VS
3080 size_t rfind(unsigned char ch, size_t nStart = npos) const
3081 { return rfind(wxUniChar(ch), nStart); }
8f93a29f
VS
3082 size_t rfind(wchar_t ch, size_t nStart = npos) const
3083 { return rfind(wxUniChar(ch), nStart); }
3084
7929902d 3085 // find first/last occurrence of any character (not) in the set:
8f93a29f
VS
3086#if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
3087 // FIXME-UTF8: this is not entirely correct, because it doesn't work if
3088 // sizeof(wchar_t)==2 and surrogates are present in the string;
3089 // should we care? Probably not.
3090 size_t find_first_of(const wxString& str, size_t nStart = 0) const
a962cdf4 3091 { return m_impl.find_first_of(str.m_impl, nStart); }
8f93a29f
VS
3092 size_t find_first_of(const char* sz, size_t nStart = 0) const
3093 { return m_impl.find_first_of(ImplStr(sz), nStart); }
3094 size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const
3095 { return m_impl.find_first_of(ImplStr(sz), nStart); }
a962cdf4 3096 size_t find_first_of(const char* sz, size_t nStart, size_t n) const
8f93a29f 3097 { return m_impl.find_first_of(ImplStr(sz), nStart, n); }
a962cdf4 3098 size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const
8f93a29f
VS
3099 { return m_impl.find_first_of(ImplStr(sz), nStart, n); }
3100 size_t find_first_of(wxUniChar c, size_t nStart = 0) const
3101 { return m_impl.find_first_of((wxChar)c, nStart); }
3102
a962cdf4
VS
3103 size_t find_last_of(const wxString& str, size_t nStart = npos) const
3104 { return m_impl.find_last_of(str.m_impl, nStart); }
8f93a29f
VS
3105 size_t find_last_of(const char* sz, size_t nStart = npos) const
3106 { return m_impl.find_last_of(ImplStr(sz), nStart); }
3107 size_t find_last_of(const wchar_t* sz, size_t nStart = npos) const
3108 { return m_impl.find_last_of(ImplStr(sz), nStart); }
3109 size_t find_last_of(const char* sz, size_t nStart, size_t n) const
3110 { return m_impl.find_last_of(ImplStr(sz), nStart, n); }
3111 size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const
3112 { return m_impl.find_last_of(ImplStr(sz), nStart, n); }
3113 size_t find_last_of(wxUniChar c, size_t nStart = npos) const
3114 { return m_impl.find_last_of((wxChar)c, nStart); }
3115
a962cdf4 3116 size_t find_first_not_of(const wxString& str, size_t nStart = 0) const
8f93a29f 3117 { return m_impl.find_first_not_of(str.m_impl, nStart); }
a962cdf4 3118 size_t find_first_not_of(const char* sz, size_t nStart = 0) const
8f93a29f 3119 { return m_impl.find_first_not_of(ImplStr(sz), nStart); }
a962cdf4 3120 size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const
8f93a29f 3121 { return m_impl.find_first_not_of(ImplStr(sz), nStart); }
a962cdf4 3122 size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const
8f93a29f 3123 { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); }
a962cdf4 3124 size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const
8f93a29f 3125 { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); }
a962cdf4 3126 size_t find_first_not_of(wxUniChar c, size_t nStart = 0) const
8f93a29f
VS
3127 { return m_impl.find_first_not_of((wxChar)c, nStart); }
3128
a962cdf4 3129 size_t find_last_not_of(const wxString& str, size_t nStart = npos) const
8f93a29f 3130 { return m_impl.find_last_not_of(str.m_impl, nStart); }
a962cdf4 3131 size_t find_last_not_of(const char* sz, size_t nStart = npos) const
8f93a29f 3132 { return m_impl.find_last_not_of(ImplStr(sz), nStart); }
a962cdf4 3133 size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const
8f93a29f 3134 { return m_impl.find_last_not_of(ImplStr(sz), nStart); }
a962cdf4 3135 size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const
8f93a29f 3136 { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); }
a962cdf4 3137 size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const
8f93a29f 3138 { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); }
a962cdf4 3139 size_t find_last_not_of(wxUniChar c, size_t nStart = npos) const
8f93a29f
VS
3140 { return m_impl.find_last_not_of((wxChar)c, nStart); }
3141#else
3142 // we can't use std::string implementation in UTF-8 build, because the
3143 // character sets would be interpreted wrongly:
3144
3145 // as strpbrk() but starts at nStart, returns npos if not found
3146 size_t find_first_of(const wxString& str, size_t nStart = 0) const
81727065 3147#if wxUSE_UNICODE // FIXME-UTF8: temporary
d3cefe87 3148 { return find_first_of(str.wc_str(), nStart); }
81727065 3149#else
d3cefe87 3150 { return find_first_of(str.mb_str(), nStart); }
81727065 3151#endif
8f93a29f
VS
3152 // same as above
3153 size_t find_first_of(const char* sz, size_t nStart = 0) const;
3154 size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const;
3155 size_t find_first_of(const char* sz, size_t nStart, size_t n) const;
3156 size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const;
3157 // same as find(char, size_t)
3158 size_t find_first_of(wxUniChar c, size_t nStart = 0) const
3159 { return find(c, nStart); }
3160 // find the last (starting from nStart) char from str in this string
3161 size_t find_last_of (const wxString& str, size_t nStart = npos) const
81727065 3162#if wxUSE_UNICODE // FIXME-UTF8: temporary
d3cefe87 3163 { return find_last_of(str.wc_str(), nStart); }
81727065 3164#else
d3cefe87 3165 { return find_last_of(str.mb_str(), nStart); }
81727065 3166#endif
8f93a29f
VS
3167 // same as above
3168 size_t find_last_of (const char* sz, size_t nStart = npos) const;
3169 size_t find_last_of (const wchar_t* sz, size_t nStart = npos) const;
3170 size_t find_last_of(const char* sz, size_t nStart, size_t n) const;
3171 size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const;
3172 // same as above
3173 size_t find_last_of(wxUniChar c, size_t nStart = npos) const
3174 { return rfind(c, nStart); }
3175
7929902d 3176 // find first/last occurrence of any character not in the set
8f93a29f
VS
3177
3178 // as strspn() (starting from nStart), returns npos on failure
3179 size_t find_first_not_of(const wxString& str, size_t nStart = 0) const
81727065 3180#if wxUSE_UNICODE // FIXME-UTF8: temporary
d3cefe87 3181 { return find_first_not_of(str.wc_str(), nStart); }
81727065 3182#else
d3cefe87 3183 { return find_first_not_of(str.mb_str(), nStart); }
81727065 3184#endif
8f93a29f
VS
3185 // same as above
3186 size_t find_first_not_of(const char* sz, size_t nStart = 0) const;
3187 size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const;
3188 size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const;
3189 size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const;
3190 // same as above
3191 size_t find_first_not_of(wxUniChar ch, size_t nStart = 0) const;
3192 // as strcspn()
3193 size_t find_last_not_of(const wxString& str, size_t nStart = npos) const
81727065 3194#if wxUSE_UNICODE // FIXME-UTF8: temporary
d3cefe87 3195 { return find_last_not_of(str.wc_str(), nStart); }
81727065 3196#else
d3cefe87 3197 { return find_last_not_of(str.mb_str(), nStart); }
81727065 3198#endif
8f93a29f
VS
3199 // same as above
3200 size_t find_last_not_of(const char* sz, size_t nStart = npos) const;
3201 size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const;
3202 size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const;
3203 size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const;
3204 // same as above
3205 size_t find_last_not_of(wxUniChar ch, size_t nStart = npos) const;
3206#endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not
3207
3208 // provide char/wchar_t/wxUniCharRef overloads for char-finding functions
3209 // above to resolve ambiguities:
3210 size_t find_first_of(wxUniCharRef ch, size_t nStart = 0) const
3211 { return find_first_of(wxUniChar(ch), nStart); }
3212 size_t find_first_of(char ch, size_t nStart = 0) const
3213 { return find_first_of(wxUniChar(ch), nStart); }
50e02008
VS
3214 size_t find_first_of(unsigned char ch, size_t nStart = 0) const
3215 { return find_first_of(wxUniChar(ch), nStart); }
8f93a29f
VS
3216 size_t find_first_of(wchar_t ch, size_t nStart = 0) const
3217 { return find_first_of(wxUniChar(ch), nStart); }
3218 size_t find_last_of(wxUniCharRef ch, size_t nStart = npos) const
3219 { return find_last_of(wxUniChar(ch), nStart); }
3220 size_t find_last_of(char ch, size_t nStart = npos) const
3221 { return find_last_of(wxUniChar(ch), nStart); }
50e02008
VS
3222 size_t find_last_of(unsigned char ch, size_t nStart = npos) const
3223 { return find_last_of(wxUniChar(ch), nStart); }
8f93a29f
VS
3224 size_t find_last_of(wchar_t ch, size_t nStart = npos) const
3225 { return find_last_of(wxUniChar(ch), nStart); }
3226 size_t find_first_not_of(wxUniCharRef ch, size_t nStart = 0) const
3227 { return find_first_not_of(wxUniChar(ch), nStart); }
3228 size_t find_first_not_of(char ch, size_t nStart = 0) const
3229 { return find_first_not_of(wxUniChar(ch), nStart); }
50e02008
VS
3230 size_t find_first_not_of(unsigned char ch, size_t nStart = 0) const
3231 { return find_first_not_of(wxUniChar(ch), nStart); }
8f93a29f
VS
3232 size_t find_first_not_of(wchar_t ch, size_t nStart = 0) const
3233 { return find_first_not_of(wxUniChar(ch), nStart); }
3234 size_t find_last_not_of(wxUniCharRef ch, size_t nStart = npos) const
3235 { return find_last_not_of(wxUniChar(ch), nStart); }
3236 size_t find_last_not_of(char ch, size_t nStart = npos) const
3237 { return find_last_not_of(wxUniChar(ch), nStart); }
50e02008
VS
3238 size_t find_last_not_of(unsigned char ch, size_t nStart = npos) const
3239 { return find_last_not_of(wxUniChar(ch), nStart); }
8f93a29f
VS
3240 size_t find_last_not_of(wchar_t ch, size_t nStart = npos) const
3241 { return find_last_not_of(wxUniChar(ch), nStart); }
3c67202d 3242
d3cefe87
VS
3243 // and additional overloads for the versions taking strings:
3244 size_t find_first_of(const wxCStrData& sz, size_t nStart = 0) const
3245 { return find_first_of(sz.AsString(), nStart); }
3246 size_t find_first_of(const wxCharBuffer& sz, size_t nStart = 0) const
3247 { return find_first_of(sz.data(), nStart); }
3248 size_t find_first_of(const wxWCharBuffer& sz, size_t nStart = 0) const
3249 { return find_first_of(sz.data(), nStart); }
3250 size_t find_first_of(const wxCStrData& sz, size_t nStart, size_t n) const
3251 { return find_first_of(sz.AsWChar(), nStart, n); }
3252 size_t find_first_of(const wxCharBuffer& sz, size_t nStart, size_t n) const
3253 { return find_first_of(sz.data(), nStart, n); }
3254 size_t find_first_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const
3255 { return find_first_of(sz.data(), nStart, n); }
3256
3257 size_t find_last_of(const wxCStrData& sz, size_t nStart = 0) const
3258 { return find_last_of(sz.AsString(), nStart); }
3259 size_t find_last_of(const wxCharBuffer& sz, size_t nStart = 0) const
3260 { return find_last_of(sz.data(), nStart); }
3261 size_t find_last_of(const wxWCharBuffer& sz, size_t nStart = 0) const
3262 { return find_last_of(sz.data(), nStart); }
3263 size_t find_last_of(const wxCStrData& sz, size_t nStart, size_t n) const
3264 { return find_last_of(sz.AsWChar(), nStart, n); }
3265 size_t find_last_of(const wxCharBuffer& sz, size_t nStart, size_t n) const
3266 { return find_last_of(sz.data(), nStart, n); }
3267 size_t find_last_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const
3268 { return find_last_of(sz.data(), nStart, n); }
3269
3270 size_t find_first_not_of(const wxCStrData& sz, size_t nStart = 0) const
3271 { return find_first_not_of(sz.AsString(), nStart); }
3272 size_t find_first_not_of(const wxCharBuffer& sz, size_t nStart = 0) const
3273 { return find_first_not_of(sz.data(), nStart); }
3274 size_t find_first_not_of(const wxWCharBuffer& sz, size_t nStart = 0) const
3275 { return find_first_not_of(sz.data(), nStart); }
3276 size_t find_first_not_of(const wxCStrData& sz, size_t nStart, size_t n) const
3277 { return find_first_not_of(sz.AsWChar(), nStart, n); }
3278 size_t find_first_not_of(const wxCharBuffer& sz, size_t nStart, size_t n) const
3279 { return find_first_not_of(sz.data(), nStart, n); }
3280 size_t find_first_not_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const
3281 { return find_first_not_of(sz.data(), nStart, n); }
3282
3283 size_t find_last_not_of(const wxCStrData& sz, size_t nStart = 0) const
3284 { return find_last_not_of(sz.AsString(), nStart); }
3285 size_t find_last_not_of(const wxCharBuffer& sz, size_t nStart = 0) const
3286 { return find_last_not_of(sz.data(), nStart); }
3287 size_t find_last_not_of(const wxWCharBuffer& sz, size_t nStart = 0) const
3288 { return find_last_not_of(sz.data(), nStart); }
3289 size_t find_last_not_of(const wxCStrData& sz, size_t nStart, size_t n) const
3290 { return find_last_not_of(sz.AsWChar(), nStart, n); }
3291 size_t find_last_not_of(const wxCharBuffer& sz, size_t nStart, size_t n) const
3292 { return find_last_not_of(sz.data(), nStart, n); }
3293 size_t find_last_not_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const
3294 { return find_last_not_of(sz.data(), nStart, n); }
3295
e87b7833
MB
3296 // string += string
3297 wxString& operator+=(const wxString& s)
68482dc5
VZ
3298 {
3299 wxSTRING_INVALIDATE_CACHED_LENGTH();
3300
3301 m_impl += s.m_impl;
3302 return *this;
3303 }
e87b7833 3304 // string += C string
8f93a29f 3305 wxString& operator+=(const char *psz)
68482dc5
VZ
3306 {
3307 wxSTRING_INVALIDATE_CACHED_LENGTH();
3308
3309 m_impl += ImplStr(psz);
3310 return *this;
3311 }
8f93a29f 3312 wxString& operator+=(const wchar_t *pwz)
68482dc5
VZ
3313 {
3314 wxSTRING_INVALIDATE_CACHED_LENGTH();
3315
3316 m_impl += ImplStr(pwz);
3317 return *this;
3318 }
c9f78968 3319 wxString& operator+=(const wxCStrData& s)
68482dc5
VZ
3320 {
3321 wxSTRING_INVALIDATE_CACHED_LENGTH();
3322
3323 m_impl += s.AsString().m_impl;
3324 return *this;
3325 }
d3cefe87
VS
3326 wxString& operator+=(const wxCharBuffer& s)
3327 { return operator+=(s.data()); }
3328 wxString& operator+=(const wxWCharBuffer& s)
3329 { return operator+=(s.data()); }
e87b7833 3330 // string += char
c9f78968 3331 wxString& operator+=(wxUniChar ch)
84d2877f 3332 {
68482dc5
VZ
3333 wxSTRING_UPDATE_CACHED_LENGTH(1);
3334
84d2877f 3335#if wxUSE_UNICODE_UTF8
68482dc5
VZ
3336 if ( !ch.IsAscii() )
3337 m_impl += wxStringOperations::EncodeChar(ch);
3338 else
84d2877f 3339#endif
68482dc5
VZ
3340 m_impl += (wxStringCharType)ch;
3341 return *this;
84d2877f 3342 }
c9f78968 3343 wxString& operator+=(wxUniCharRef ch) { return *this += wxUniChar(ch); }
b7d40341 3344 wxString& operator+=(int ch) { return *this += wxUniChar(ch); }
c9f78968 3345 wxString& operator+=(char ch) { return *this += wxUniChar(ch); }
b77afb41 3346 wxString& operator+=(unsigned char ch) { return *this += wxUniChar(ch); }
c9f78968 3347 wxString& operator+=(wchar_t ch) { return *this += wxUniChar(ch); }
d8a4b666
VS
3348
3349private:
2523e9b7 3350#if !wxUSE_STL_BASED_WXSTRING
d8a4b666 3351 // helpers for wxStringBuffer and wxStringBufferLength
8f93a29f 3352 wxStringCharType *DoGetWriteBuf(size_t nLen)
68482dc5
VZ
3353 {
3354 return m_impl.DoGetWriteBuf(nLen);
3355 }
3356
8f93a29f 3357 void DoUngetWriteBuf()
68482dc5
VZ
3358 {
3359 wxSTRING_INVALIDATE_CACHE();
3360
3361 m_impl.DoUngetWriteBuf();
3362 }
3363
8f93a29f 3364 void DoUngetWriteBuf(size_t nLen)
68482dc5
VZ
3365 {
3366 wxSTRING_SET_CACHED_LENGTH(nLen);
3367
3368 m_impl.DoUngetWriteBuf(nLen);
3369 }
2523e9b7 3370#endif // !wxUSE_STL_BASED_WXSTRING
c9f78968
VS
3371
3372#ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
d1f6e2cf
VS
3373 #if !wxUSE_UTF8_LOCALE_ONLY
3374 int DoPrintfWchar(const wxChar *format, ...);
3375 static wxString DoFormatWchar(const wxChar *format, ...);
3376 #endif
3377 #if wxUSE_UNICODE_UTF8
3378 int DoPrintfUtf8(const char *format, ...);
3379 static wxString DoFormatUtf8(const char *format, ...);
3380 #endif
c9f78968 3381#endif
8f93a29f
VS
3382
3383#if !wxUSE_STL_BASED_WXSTRING
3384 // check string's data validity
3385 bool IsValid() const { return m_impl.GetStringData()->IsValid(); }
3386#endif
3387
3388private:
3389 wxStringImpl m_impl;
132276cf
VS
3390
3391 // buffers for compatibility conversion from (char*)c_str() and
3392 // (wchar_t*)c_str():
3393 // FIXME-UTF8: bechmark various approaches to keeping compatibility buffers
3394 template<typename T>
3395 struct ConvertedBuffer
3396 {
3397 ConvertedBuffer() : m_buf(NULL) {}
3398 ~ConvertedBuffer()
3399 { free(m_buf); }
3400
05f32fc3 3401 operator T*() const { return m_buf; }
132276cf
VS
3402
3403 ConvertedBuffer& operator=(T *str)
3404 {
3405 free(m_buf);
3406 m_buf = str;
3407 return *this;
3408 }
3409
3410 T *m_buf;
3411 };
111d9948 3412#if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
132276cf
VS
3413 ConvertedBuffer<char> m_convertedToChar;
3414#endif
3415#if !wxUSE_UNICODE_WCHAR
3416 ConvertedBuffer<wchar_t> m_convertedToWChar;
3417#endif
2523e9b7 3418
b0c4d5d7
VS
3419#if wxUSE_UNICODE_UTF8
3420 // FIXME-UTF8: (try to) move this elsewhere (TLS) or solve differently
3421 // assigning to character pointer to by wxString::interator may
3422 // change the underlying wxStringImpl iterator, so we have to
3423 // keep track of all iterators and update them as necessary:
3424 struct wxStringIteratorNodeHead
3425 {
3426 wxStringIteratorNodeHead() : ptr(NULL) {}
3427 wxStringIteratorNode *ptr;
300b44a9
VS
3428
3429 // copying is disallowed as it would result in more than one pointer into
3430 // the same linked list
c0c133e1 3431 wxDECLARE_NO_COPY_CLASS(wxStringIteratorNodeHead);
b0c4d5d7
VS
3432 };
3433
3434 wxStringIteratorNodeHead m_iterators;
3435
b5dbe15d
VS
3436 friend class WXDLLIMPEXP_FWD_BASE wxStringIteratorNode;
3437 friend class WXDLLIMPEXP_FWD_BASE wxUniCharRef;
b0c4d5d7
VS
3438#endif // wxUSE_UNICODE_UTF8
3439
b5dbe15d 3440 friend class WXDLLIMPEXP_FWD_BASE wxCStrData;
6798451b
VS
3441 friend class wxStringInternalBuffer;
3442 friend class wxStringInternalBufferLength;
c801d85f
KB
3443};
3444
c9f78968
VS
3445#ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
3446 #pragma warning (default:4275)
3447#endif
3448
a962cdf4
VS
3449// string iterator operators that satisfy STL Random Access Iterator
3450// requirements:
b5343e06 3451inline wxString::iterator operator+(ptrdiff_t n, wxString::iterator i)
a962cdf4 3452 { return i + n; }
b5343e06 3453inline wxString::const_iterator operator+(ptrdiff_t n, wxString::const_iterator i)
a962cdf4 3454 { return i + n; }
b5343e06 3455inline wxString::reverse_iterator operator+(ptrdiff_t n, wxString::reverse_iterator i)
a962cdf4 3456 { return i + n; }
b5343e06 3457inline wxString::const_reverse_iterator operator+(ptrdiff_t n, wxString::const_reverse_iterator i)
a962cdf4
VS
3458 { return i + n; }
3459
1fc8cfbd
VZ
3460// notice that even though for many compilers the friend declarations above are
3461// enough, from the point of view of C++ standard we must have the declarations
3462// here as friend ones are not injected in the enclosing namespace and without
3463// them the code fails to compile with conforming compilers such as xlC or g++4
c9f78968 3464wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2);
8f93a29f
VS
3465wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const char *psz);
3466wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wchar_t *pwz);
3467wxString WXDLLIMPEXP_BASE operator+(const char *psz, const wxString& string);
3468wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz, const wxString& string);
1fc8cfbd 3469
c9f78968
VS
3470wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch);
3471wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string);
3472
3473inline wxString operator+(const wxString& string, wxUniCharRef ch)
3474 { return string + (wxUniChar)ch; }
3475inline wxString operator+(const wxString& string, char ch)
3476 { return string + wxUniChar(ch); }
3477inline wxString operator+(const wxString& string, wchar_t ch)
3478 { return string + wxUniChar(ch); }
3479inline wxString operator+(wxUniCharRef ch, const wxString& string)
3480 { return (wxUniChar)ch + string; }
3481inline wxString operator+(char ch, const wxString& string)
3482 { return wxUniChar(ch) + string; }
3483inline wxString operator+(wchar_t ch, const wxString& string)
3484 { return wxUniChar(ch) + string; }
3485
b7146cbe 3486
a0f63de9 3487#define wxGetEmptyString() wxString()
07170120 3488
062dc5fc
VZ
3489// ----------------------------------------------------------------------------
3490// helper functions which couldn't be defined inline
3491// ----------------------------------------------------------------------------
3492
3493namespace wxPrivate
3494{
3495
3496#if wxUSE_UNICODE_WCHAR
3497
3498template <>
3499struct wxStringAsBufHelper<char>
3500{
3501 static wxCharBuffer Get(const wxString& s, size_t *len)
3502 {
3503 wxCharBuffer buf(s.mb_str());
3504 if ( len )
3505 *len = buf ? strlen(buf) : 0;
3506 return buf;
3507 }
3508};
3509
3510template <>
3511struct wxStringAsBufHelper<wchar_t>
3512{
3513 static wxWCharBuffer Get(const wxString& s, size_t *len)
3514 {
3515 if ( len )
3516 *len = s.length();
3517 return wxWCharBuffer::CreateNonOwned(s.wx_str());
3518 }
3519};
3520
3521#elif wxUSE_UNICODE_UTF8
3522
3523template <>
3524struct wxStringAsBufHelper<char>
3525{
3526 static wxCharBuffer Get(const wxString& s, size_t *len)
3527 {
3528 if ( len )
3529 *len = s.utf8_length();
3530 return wxCharBuffer::CreateNonOwned(s.wx_str());
3531 }
3532};
3533
3534template <>
3535struct wxStringAsBufHelper<wchar_t>
3536{
3537 static wxWCharBuffer Get(const wxString& s, size_t *len)
3538 {
3539 wxWCharBuffer wbuf(s.wc_str());
3540 if ( len )
3541 *len = wxWcslen(wbuf);
3542 return wbuf;
3543 }
3544};
3545
3546#endif // Unicode build kind
3547
3548} // namespace wxPrivate
3549
1d218550
VZ
3550// ----------------------------------------------------------------------------
3551// wxStringBuffer: a tiny class allowing to get a writable pointer into string
3552// ----------------------------------------------------------------------------
3553
2523e9b7
VS
3554#if !wxUSE_STL_BASED_WXSTRING
3555// string buffer for direct access to string data in their native
3556// representation:
6798451b 3557class wxStringInternalBuffer
2523e9b7
VS
3558{
3559public:
3560 typedef wxStringCharType CharType;
3561
6798451b 3562 wxStringInternalBuffer(wxString& str, size_t lenWanted = 1024)
2523e9b7
VS
3563 : m_str(str), m_buf(NULL)
3564 { m_buf = m_str.DoGetWriteBuf(lenWanted); }
3565
6798451b 3566 ~wxStringInternalBuffer() { m_str.DoUngetWriteBuf(); }
2523e9b7
VS
3567
3568 operator wxStringCharType*() const { return m_buf; }
941b78cc 3569
2523e9b7
VS
3570private:
3571 wxString& m_str;
3572 wxStringCharType *m_buf;
3573
c0c133e1 3574 wxDECLARE_NO_COPY_CLASS(wxStringInternalBuffer);
2523e9b7
VS
3575};
3576
6798451b 3577class wxStringInternalBufferLength
941b78cc
MB
3578{
3579public:
2523e9b7
VS
3580 typedef wxStringCharType CharType;
3581
6798451b 3582 wxStringInternalBufferLength(wxString& str, size_t lenWanted = 1024)
2523e9b7
VS
3583 : m_str(str), m_buf(NULL), m_len(0), m_lenSet(false)
3584 {
3585 m_buf = m_str.DoGetWriteBuf(lenWanted);
3586 wxASSERT(m_buf != NULL);
3587 }
3588
6798451b 3589 ~wxStringInternalBufferLength()
2523e9b7
VS
3590 {
3591 wxASSERT(m_lenSet);
3592 m_str.DoUngetWriteBuf(m_len);
3593 }
3594
3595 operator wxStringCharType*() const { return m_buf; }
3596 void SetLength(size_t length) { m_len = length; m_lenSet = true; }
3597
3598private:
3599 wxString& m_str;
3600 wxStringCharType *m_buf;
3601 size_t m_len;
3602 bool m_lenSet;
3603
c0c133e1 3604 wxDECLARE_NO_COPY_CLASS(wxStringInternalBufferLength);
2523e9b7
VS
3605};
3606
3607#endif // !wxUSE_STL_BASED_WXSTRING
3608
3609template<typename T>
6b583d40 3610class wxStringTypeBufferBase
2523e9b7
VS
3611{
3612public:
3613 typedef T CharType;
3614
3615 wxStringTypeBufferBase(wxString& str, size_t lenWanted = 1024)
e87b7833 3616 : m_str(str), m_buf(lenWanted)
062dc5fc
VZ
3617 {
3618 // for compatibility with old wxStringBuffer which provided direct
3619 // access to wxString internal buffer, initialize ourselves with the
3620 // string initial contents
3621
3622 // FIXME-VC6: remove the ugly (CharType *)NULL and use normal
3623 // tchar_str<CharType>
3624 size_t len;
3625 const wxCharTypeBuffer<CharType> buf(str.tchar_str(&len, (CharType *)NULL));
3626 if ( buf )
3627 {
3628 if ( len > lenWanted )
3629 {
3630 // in this case there is not enough space for terminating NUL,
3631 // ensure that we still put it there
3632 m_buf.data()[lenWanted] = 0;
3633 len = lenWanted - 1;
3634 }
3635
39329d2e 3636 memcpy(m_buf.data(), buf, (len + 1)*sizeof(CharType));
062dc5fc
VZ
3637 }
3638 //else: conversion failed, this can happen when trying to get Unicode
3639 // string contents into a char string
3640 }
941b78cc 3641
2523e9b7 3642 operator CharType*() { return m_buf.data(); }
941b78cc 3643
2523e9b7 3644protected:
941b78cc 3645 wxString& m_str;
2523e9b7 3646 wxCharTypeBuffer<CharType> m_buf;
941b78cc
MB
3647};
3648
2523e9b7 3649template<typename T>
6b583d40 3650class wxStringTypeBufferLengthBase : public wxStringTypeBufferBase<T>
941b78cc
MB
3651{
3652public:
2523e9b7 3653 wxStringTypeBufferLengthBase(wxString& str, size_t lenWanted = 1024)
062dc5fc
VZ
3654 : wxStringTypeBufferBase<T>(str, lenWanted),
3655 m_len(0),
3656 m_lenSet(false)
941b78cc
MB
3657 { }
3658
062dc5fc
VZ
3659 ~wxStringTypeBufferLengthBase()
3660 {
3661 wxASSERT_MSG( this->m_lenSet, "forgot to call SetLength()" );
3662 }
3663
941b78cc
MB
3664 void SetLength(size_t length) { m_len = length; m_lenSet = true; }
3665
2523e9b7 3666protected:
062dc5fc
VZ
3667 size_t m_len;
3668 bool m_lenSet;
941b78cc
MB
3669};
3670
2523e9b7
VS
3671template<typename T>
3672class wxStringTypeBuffer : public wxStringTypeBufferBase<T>
3673{
3674public:
3675 wxStringTypeBuffer(wxString& str, size_t lenWanted = 1024)
062dc5fc
VZ
3676 : wxStringTypeBufferBase<T>(str, lenWanted)
3677 { }
3678
2523e9b7
VS
3679 ~wxStringTypeBuffer()
3680 {
3681 this->m_str.assign(this->m_buf.data());
3682 }
941b78cc 3683
c0c133e1 3684 wxDECLARE_NO_COPY_CLASS(wxStringTypeBuffer);
2523e9b7
VS
3685};
3686
3687template<typename T>
3688class wxStringTypeBufferLength : public wxStringTypeBufferLengthBase<T>
1d218550
VZ
3689{
3690public:
2523e9b7 3691 wxStringTypeBufferLength(wxString& str, size_t lenWanted = 1024)
062dc5fc
VZ
3692 : wxStringTypeBufferLengthBase<T>(str, lenWanted)
3693 { }
e015c2a3 3694
2523e9b7
VS
3695 ~wxStringTypeBufferLength()
3696 {
2523e9b7
VS
3697 this->m_str.assign(this->m_buf.data(), this->m_len);
3698 }
e015c2a3 3699
c0c133e1 3700 wxDECLARE_NO_COPY_CLASS(wxStringTypeBufferLength);
2523e9b7 3701};
e015c2a3 3702
2523e9b7 3703#if wxUSE_STL_BASED_WXSTRING
7c77f334
VZ
3704
3705WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase<wxStringCharType> )
3706
6798451b 3707class wxStringInternalBuffer : public wxStringTypeBufferBase<wxStringCharType>
2523e9b7
VS
3708{
3709public:
6798451b 3710 wxStringInternalBuffer(wxString& str, size_t lenWanted = 1024)
2523e9b7 3711 : wxStringTypeBufferBase<wxStringCharType>(str, lenWanted) {}
6798451b 3712 ~wxStringInternalBuffer()
2523e9b7 3713 { m_str.m_impl.assign(m_buf.data()); }
e015c2a3 3714
c0c133e1 3715 wxDECLARE_NO_COPY_CLASS(wxStringInternalBuffer);
1d218550
VZ
3716};
3717
7c77f334
VZ
3718WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE(
3719 wxStringTypeBufferLengthBase<wxStringCharType> )
3720
3721class wxStringInternalBufferLength
3722 : public wxStringTypeBufferLengthBase<wxStringCharType>
941b78cc
MB
3723{
3724public:
6798451b 3725 wxStringInternalBufferLength(wxString& str, size_t lenWanted = 1024)
2523e9b7 3726 : wxStringTypeBufferLengthBase<wxStringCharType>(str, lenWanted) {}
941b78cc 3727
6798451b 3728 ~wxStringInternalBufferLength()
941b78cc 3729 {
2523e9b7 3730 m_str.m_impl.assign(m_buf.data(), m_len);
941b78cc
MB
3731 }
3732
c0c133e1 3733 wxDECLARE_NO_COPY_CLASS(wxStringInternalBufferLength);
941b78cc 3734};
062dc5fc 3735
2523e9b7
VS
3736#endif // wxUSE_STL_BASED_WXSTRING
3737
941b78cc 3738
2523e9b7
VS
3739#if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
3740typedef wxStringTypeBuffer<wxChar> wxStringBuffer;
3741typedef wxStringTypeBufferLength<wxChar> wxStringBufferLength;
3742#else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
6798451b
VS
3743typedef wxStringInternalBuffer wxStringBuffer;
3744typedef wxStringInternalBufferLength wxStringBufferLength;
8f93a29f 3745#endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
941b78cc 3746
7d46f92b 3747#if wxUSE_UNICODE_UTF8
628f87da
VS
3748typedef wxStringInternalBuffer wxUTF8StringBuffer;
3749typedef wxStringInternalBufferLength wxUTF8StringBufferLength;
7d46f92b 3750#elif wxUSE_UNICODE_WCHAR
7c77f334
VZ
3751
3752WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase<char> )
3753
6b583d40
VZ
3754// Note about inlined dtors in the classes below: this is done not for
3755// performance reasons but just to avoid linking errors in the MSVC DLL build
3756// under Windows: if a class has non-inline methods it must be declared as
3757// being DLL-exported but, due to an extremely interesting feature of MSVC 7
3758// and later, any template class which is used as a base of a DLL-exported
3759// class is implicitly made DLL-exported too, as explained at the bottom of
3760// http://msdn.microsoft.com/en-us/library/twa2aw10.aspx (just to confirm: yes,
3761// _inheriting_ from a class can change whether it is being exported from DLL)
3762//
3763// But this results in link errors because the base template class is not DLL-
3764// exported, whether it is declared with WXDLLIMPEXP_BASE or not, because it
3765// does have only inline functions. So the simplest fix is to just make all the
3766// functions of these classes inline too.
3767
3768class wxUTF8StringBuffer : public wxStringTypeBufferBase<char>
628f87da
VS
3769{
3770public:
3771 wxUTF8StringBuffer(wxString& str, size_t lenWanted = 1024)
3772 : wxStringTypeBufferBase<char>(str, lenWanted) {}
6b583d40
VZ
3773 ~wxUTF8StringBuffer()
3774 {
3775 wxMBConvStrictUTF8 conv;
3776 size_t wlen = conv.ToWChar(NULL, 0, m_buf);
3777 wxCHECK_RET( wlen != wxCONV_FAILED, "invalid UTF-8 data in string buffer?" );
3778
3779 wxStringInternalBuffer wbuf(m_str, wlen);
3780 conv.ToWChar(wbuf, wlen, m_buf);
3781 }
628f87da 3782
c0c133e1 3783 wxDECLARE_NO_COPY_CLASS(wxUTF8StringBuffer);
628f87da
VS
3784};
3785
7c77f334
VZ
3786WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferLengthBase<char> )
3787
6b583d40 3788class wxUTF8StringBufferLength : public wxStringTypeBufferLengthBase<char>
628f87da
VS
3789{
3790public:
3791 wxUTF8StringBufferLength(wxString& str, size_t lenWanted = 1024)
3792 : wxStringTypeBufferLengthBase<char>(str, lenWanted) {}
6b583d40
VZ
3793 ~wxUTF8StringBufferLength()
3794 {
3795 wxCHECK_RET(m_lenSet, "length not set");
3796
3797 wxMBConvStrictUTF8 conv;
3798 size_t wlen = conv.ToWChar(NULL, 0, m_buf, m_len);
3799 wxCHECK_RET( wlen != wxCONV_FAILED, "invalid UTF-8 data in string buffer?" );
3800
3801 wxStringInternalBufferLength wbuf(m_str, wlen);
3802 conv.ToWChar(wbuf, wlen, m_buf, m_len);
3803 wbuf.SetLength(wlen);
3804 }
628f87da 3805
c0c133e1 3806 wxDECLARE_NO_COPY_CLASS(wxUTF8StringBufferLength);
628f87da 3807};
7d46f92b 3808#endif // wxUSE_UNICODE_UTF8/wxUSE_UNICODE_WCHAR
628f87da
VS
3809
3810
c801d85f 3811// ---------------------------------------------------------------------------
3c67202d 3812// wxString comparison functions: operator versions are always case sensitive
c801d85f 3813// ---------------------------------------------------------------------------
f33fee2a 3814
831faf97 3815#define wxCMP_WXCHAR_STRING(p, s, op) 0 op s.Cmp(p)
fb52c2b6
VZ
3816
3817wxDEFINE_ALL_COMPARISONS(const wxChar *, const wxString&, wxCMP_WXCHAR_STRING)
3818
3819#undef wxCMP_WXCHAR_STRING
3820
0cbe5ee3 3821inline bool operator==(const wxString& s1, const wxString& s2)
2712e317 3822 { return s1.IsSameAs(s2); }
0cbe5ee3 3823inline bool operator!=(const wxString& s1, const wxString& s2)
2712e317 3824 { return !s1.IsSameAs(s2); }
0cbe5ee3
VZ
3825inline bool operator< (const wxString& s1, const wxString& s2)
3826 { return s1.Cmp(s2) < 0; }
0cbe5ee3
VZ
3827inline bool operator> (const wxString& s1, const wxString& s2)
3828 { return s1.Cmp(s2) > 0; }
0cbe5ee3
VZ
3829inline bool operator<=(const wxString& s1, const wxString& s2)
3830 { return s1.Cmp(s2) <= 0; }
0cbe5ee3
VZ
3831inline bool operator>=(const wxString& s1, const wxString& s2)
3832 { return s1.Cmp(s2) >= 0; }
3c67202d 3833
5d0fac27
VS
3834inline bool operator==(const wxString& s1, const wxCStrData& s2)
3835 { return s1 == s2.AsString(); }
3836inline bool operator==(const wxCStrData& s1, const wxString& s2)
3837 { return s1.AsString() == s2; }
3838inline bool operator!=(const wxString& s1, const wxCStrData& s2)
3839 { return s1 != s2.AsString(); }
3840inline bool operator!=(const wxCStrData& s1, const wxString& s2)
3841 { return s1.AsString() != s2; }
3842
5ca07d0f 3843inline bool operator==(const wxString& s1, const wxWCharBuffer& s2)
f33fee2a 3844 { return (s1.Cmp((const wchar_t *)s2) == 0); }
5ca07d0f 3845inline bool operator==(const wxWCharBuffer& s1, const wxString& s2)
f33fee2a 3846 { return (s2.Cmp((const wchar_t *)s1) == 0); }
f6bcfd97
BP
3847inline bool operator!=(const wxString& s1, const wxWCharBuffer& s2)
3848 { return (s1.Cmp((const wchar_t *)s2) != 0); }
3849inline bool operator!=(const wxWCharBuffer& s1, const wxString& s2)
3850 { return (s2.Cmp((const wchar_t *)s1) != 0); }
be39d6f5 3851
5ca07d0f 3852inline bool operator==(const wxString& s1, const wxCharBuffer& s2)
f33fee2a 3853 { return (s1.Cmp((const char *)s2) == 0); }
5ca07d0f 3854inline bool operator==(const wxCharBuffer& s1, const wxString& s2)
f33fee2a 3855 { return (s2.Cmp((const char *)s1) == 0); }
f6bcfd97
BP
3856inline bool operator!=(const wxString& s1, const wxCharBuffer& s2)
3857 { return (s1.Cmp((const char *)s2) != 0); }
3858inline bool operator!=(const wxCharBuffer& s1, const wxString& s2)
3859 { return (s2.Cmp((const char *)s1) != 0); }
5ca07d0f 3860
6d56eb5c 3861inline wxString operator+(const wxString& string, const wxWCharBuffer& buf)
e90c1d2a 3862 { return string + (const wchar_t *)buf; }
6d56eb5c 3863inline wxString operator+(const wxWCharBuffer& buf, const wxString& string)
e90c1d2a 3864 { return (const wchar_t *)buf + string; }
be39d6f5 3865
6d56eb5c 3866inline wxString operator+(const wxString& string, const wxCharBuffer& buf)
e90c1d2a 3867 { return string + (const char *)buf; }
6d56eb5c 3868inline wxString operator+(const wxCharBuffer& buf, const wxString& string)
e90c1d2a 3869 { return (const char *)buf + string; }
f04f3991 3870
a962cdf4 3871// comparison with char
c9f78968
VS
3872inline bool operator==(const wxUniChar& c, const wxString& s) { return s.IsSameAs(c); }
3873inline bool operator==(const wxUniCharRef& c, const wxString& s) { return s.IsSameAs(c); }
3874inline bool operator==(char c, const wxString& s) { return s.IsSameAs(c); }
3875inline bool operator==(wchar_t c, const wxString& s) { return s.IsSameAs(c); }
3876inline bool operator==(int c, const wxString& s) { return s.IsSameAs(c); }
3877inline bool operator==(const wxString& s, const wxUniChar& c) { return s.IsSameAs(c); }
3878inline bool operator==(const wxString& s, const wxUniCharRef& c) { return s.IsSameAs(c); }
3879inline bool operator==(const wxString& s, char c) { return s.IsSameAs(c); }
3880inline bool operator==(const wxString& s, wchar_t c) { return s.IsSameAs(c); }
3881inline bool operator!=(const wxUniChar& c, const wxString& s) { return !s.IsSameAs(c); }
3882inline bool operator!=(const wxUniCharRef& c, const wxString& s) { return !s.IsSameAs(c); }
3883inline bool operator!=(char c, const wxString& s) { return !s.IsSameAs(c); }
3884inline bool operator!=(wchar_t c, const wxString& s) { return !s.IsSameAs(c); }
3885inline bool operator!=(int c, const wxString& s) { return !s.IsSameAs(c); }
3886inline bool operator!=(const wxString& s, const wxUniChar& c) { return !s.IsSameAs(c); }
3887inline bool operator!=(const wxString& s, const wxUniCharRef& c) { return !s.IsSameAs(c); }
3888inline bool operator!=(const wxString& s, char c) { return !s.IsSameAs(c); }
3889inline bool operator!=(const wxString& s, wchar_t c) { return !s.IsSameAs(c); }
e51b17a1 3890
d7330233
VS
3891// comparison with C string in Unicode build
3892#if wxUSE_UNICODE
fb52c2b6
VZ
3893
3894#define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s
3895
3896wxDEFINE_ALL_COMPARISONS(const char *, const wxString&, wxCMP_CHAR_STRING)
3897
3898#undef wxCMP_CHAR_STRING
3899
d7330233
VS
3900#endif // wxUSE_UNICODE
3901
fb52c2b6
VZ
3902// we also need to provide the operators for comparison with wxCStrData to
3903// resolve ambiguity between operator(const wxChar *,const wxString &) and
3904// operator(const wxChar *, const wxChar *) for "p == s.c_str()"
3905//
3906// notice that these are (shallow) pointer comparisons, not (deep) string ones
3907#define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar()
3908#define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar()
3909
11aac4ba
VS
3910wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData&, wxCMP_WCHAR_CSTRDATA)
3911wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData&, wxCMP_CHAR_CSTRDATA)
fb52c2b6
VZ
3912
3913#undef wxCMP_CHAR_CSTRDATA
3914#undef wxCMP_WCHAR_CSTRDATA
3915
c801d85f 3916// ---------------------------------------------------------------------------
3c67202d 3917// Implementation only from here until the end of file
c801d85f
KB
3918// ---------------------------------------------------------------------------
3919
e87b7833 3920#if wxUSE_STD_IOSTREAM
c801d85f 3921
65f19af1 3922#include "wx/iosfwrap.h"
c801d85f 3923
bddd7a8d 3924WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxString&);
c9f78968 3925WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCStrData&);
04abe4bc
VS
3926WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCharBuffer&);
3927#ifndef __BORLANDC__
3928WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxWCharBuffer&);
3929#endif
c801d85f 3930
6a6ea041 3931#if wxUSE_UNICODE && defined(HAVE_WOSTREAM)
6b61b594
VZ
3932
3933WXDLLIMPEXP_BASE wxSTD wostream& operator<<(wxSTD wostream&, const wxString&);
3934WXDLLIMPEXP_BASE wxSTD wostream& operator<<(wxSTD wostream&, const wxCStrData&);
3935WXDLLIMPEXP_BASE wxSTD wostream& operator<<(wxSTD wostream&, const wxWCharBuffer&);
3936
6a6ea041 3937#endif // wxUSE_UNICODE && defined(HAVE_WOSTREAM)
6b61b594 3938
7a906e1a 3939#endif // wxUSE_STD_IOSTREAM
c801d85f 3940
c9f78968
VS
3941// ---------------------------------------------------------------------------
3942// wxCStrData implementation
3943// ---------------------------------------------------------------------------
3944
3945inline wxCStrData::wxCStrData(char *buf)
3946 : m_str(new wxString(buf)), m_offset(0), m_owned(true) {}
3947inline wxCStrData::wxCStrData(wchar_t *buf)
3948 : m_str(new wxString(buf)), m_offset(0), m_owned(true) {}
3949
92a17abc
VS
3950inline wxCStrData::wxCStrData(const wxCStrData& data)
3951 : m_str(data.m_owned ? new wxString(*data.m_str) : data.m_str),
3952 m_offset(data.m_offset),
3953 m_owned(data.m_owned)
3954{
3955}
3956
c9f78968
VS
3957inline wxCStrData::~wxCStrData()
3958{
3959 if ( m_owned )
5c33522f 3960 delete const_cast<wxString*>(m_str); // cast to silence warnings
c9f78968
VS
3961}
3962
11aac4ba
VS
3963// simple cases for AsChar() and AsWChar(), the complicated ones are
3964// in string.cpp
3965#if wxUSE_UNICODE_WCHAR
c9f78968 3966inline const wchar_t* wxCStrData::AsWChar() const
11aac4ba
VS
3967{
3968 return m_str->wx_str() + m_offset;
3969}
3970#endif // wxUSE_UNICODE_WCHAR
3971
bfb6847d 3972#if !wxUSE_UNICODE
c9f78968 3973inline const char* wxCStrData::AsChar() const
c9f78968 3974{
bfb6847d 3975 return m_str->wx_str() + m_offset;
c9f78968 3976}
11aac4ba 3977#endif // !wxUSE_UNICODE
c9f78968 3978
bfb6847d
VS
3979#if wxUSE_UTF8_LOCALE_ONLY
3980inline const char* wxCStrData::AsChar() const
3981{
3982 return wxStringOperations::AddToIter(m_str->wx_str(), m_offset);
3983}
3984#endif // wxUSE_UTF8_LOCALE_ONLY
3985
681e4412
VS
3986inline const wxCharBuffer wxCStrData::AsCharBuf() const
3987{
3988#if !wxUSE_UNICODE
3989 return wxCharBuffer::CreateNonOwned(AsChar());
3990#else
3991 return AsString().mb_str();
3992#endif
3993}
3994
3995inline const wxWCharBuffer wxCStrData::AsWCharBuf() const
3996{
3997#if wxUSE_UNICODE_WCHAR
3998 return wxWCharBuffer::CreateNonOwned(AsWChar());
3999#else
4000 return AsString().wc_str();
4001#endif
4002}
4003
c9f78968
VS
4004inline wxString wxCStrData::AsString() const
4005{
4006 if ( m_offset == 0 )
4007 return *m_str;
4008 else
4009 return m_str->Mid(m_offset);
4010}
4011
2523e9b7
VS
4012inline const wxStringCharType *wxCStrData::AsInternal() const
4013{
bfb6847d 4014#if wxUSE_UNICODE_UTF8
2523e9b7 4015 return wxStringOperations::AddToIter(m_str->wx_str(), m_offset);
bfb6847d
VS
4016#else
4017 return m_str->wx_str() + m_offset;
4018#endif
2523e9b7
VS
4019}
4020
c9f78968
VS
4021inline wxUniChar wxCStrData::operator*() const
4022{
4023 if ( m_str->empty() )
4024 return wxUniChar(_T('\0'));
4025 else
4026 return (*m_str)[m_offset];
4027}
4028
4029inline wxUniChar wxCStrData::operator[](size_t n) const
4030{
378964d4
VS
4031 // NB: we intentionally use operator[] and not at() here because the former
4032 // works for the terminating NUL while the latter does not
4033 return (*m_str)[m_offset + n];
c9f78968
VS
4034}
4035
cc5bd48e
VZ
4036// ----------------------------------------------------------------------------
4037// more wxCStrData operators
4038// ----------------------------------------------------------------------------
4039
4040// we need to define those to allow "size_t pos = p - s.c_str()" where p is
4041// some pointer into the string
4042inline size_t operator-(const char *p, const wxCStrData& cs)
4043{
4044 return p - cs.AsChar();
4045}
4046
4047inline size_t operator-(const wchar_t *p, const wxCStrData& cs)
4048{
4049 return p - cs.AsWChar();
4050}
4051
414b7b40
VZ
4052// ----------------------------------------------------------------------------
4053// implementation of wx[W]CharBuffer inline methods using wxCStrData
4054// ----------------------------------------------------------------------------
4055
11aac4ba
VS
4056// FIXME-UTF8: move this to buffer.h
4057inline wxCharBuffer::wxCharBuffer(const wxCStrData& cstr)
681e4412 4058 : wxCharTypeBufferBase(cstr.AsCharBuf())
11aac4ba
VS
4059{
4060}
4061
4062inline wxWCharBuffer::wxWCharBuffer(const wxCStrData& cstr)
681e4412 4063 : wxCharTypeBufferBase(cstr.AsWCharBuf())
414b7b40
VZ
4064{
4065}
4066
b0c4d5d7
VS
4067#if wxUSE_UNICODE_UTF8
4068// ----------------------------------------------------------------------------
4069// implementation of wxStringIteratorNode inline methods
4070// ----------------------------------------------------------------------------
4071
39c20230
VS
4072void wxStringIteratorNode::DoSet(const wxString *str,
4073 wxStringImpl::const_iterator *citer,
4074 wxStringImpl::iterator *iter)
b0c4d5d7 4075{
42fc0309 4076 m_prev = NULL;
39c20230
VS
4077 m_iter = iter;
4078 m_citer = citer;
4079 m_str = str;
4080 if ( str )
4081 {
4082 m_next = str->m_iterators.ptr;
5c33522f 4083 const_cast<wxString*>(m_str)->m_iterators.ptr = this;
39c20230
VS
4084 if ( m_next )
4085 m_next->m_prev = this;
4086 }
42fc0309
VS
4087 else
4088 {
4089 m_next = NULL;
4090 }
b0c4d5d7
VS
4091}
4092
39c20230 4093void wxStringIteratorNode::clear()
b0c4d5d7
VS
4094{
4095 if ( m_next )
4096 m_next->m_prev = m_prev;
4097 if ( m_prev )
4098 m_prev->m_next = m_next;
39c20230 4099 else if ( m_str ) // first in the list
5c33522f 4100 const_cast<wxString*>(m_str)->m_iterators.ptr = m_next;
39c20230
VS
4101
4102 m_next = m_prev = NULL;
4103 m_citer = NULL;
4104 m_iter = NULL;
4105 m_str = NULL;
b0c4d5d7
VS
4106}
4107#endif // wxUSE_UNICODE_UTF8
4108
e3e8e3c0 4109#if WXWIN_COMPATIBILITY_2_8
3a3dde0d 4110 // lot of code out there doesn't explicitly include wx/crt.h, but uses
e3e8e3c0
VS
4111 // CRT wrappers that are now declared in wx/wxcrt.h and wx/wxcrtvararg.h,
4112 // so let's include this header now that wxString is defined and it's safe
4113 // to do it:
3a3dde0d 4114 #include "wx/crt.h"
e3e8e3c0
VS
4115#endif
4116
cbec0f40
FM
4117// ----------------------------------------------------------------------------
4118// Checks on wxString characters
4119// ----------------------------------------------------------------------------
4120
4121template<bool (T)(const wxUniChar& c)>
4122 inline bool wxStringCheck(const wxString& val)
4123 {
4124 for ( wxString::const_iterator i = val.begin();
4125 i != val.end();
4126 ++i )
4127 if (T(*i) == 0)
4128 return false;
4129 return true;
4130 }
4131
11aac4ba 4132#endif // _WX_WXSTRING_H_