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