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