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