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