]> git.saurik.com Git - wxWidgets.git/blame - include/wx/string.h
reverted accidental commit
[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
1bfcb0b6 49
4055ed82 50#ifdef __WXPALMOS__
ffecfa5a
JS
51 #include <StringMgr.h>
52#endif
53
2523e9b7 54#include "wx/wxcrt.h" // for wxChar, wxStrlen() etc.
c9f78968 55#include "wx/strvararg.h"
e90c1d2a
VZ
56#include "wx/buffer.h" // for wxCharBuffer
57#include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes
a7ea63e2 58#include "wx/stringimpl.h"
467175ab 59#include "wx/stringops.h"
a7ea63e2 60#include "wx/unichar.h"
3f4a0c5b 61
fb3c9042
VZ
62class WXDLLIMPEXP_BASE wxString;
63
67cff9dc
VZ
64// unless this symbol is predefined to disable the compatibility functions, do
65// use them
66#ifndef WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
67 #define WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER 1
68#endif
69
c801d85f
KB
70// ---------------------------------------------------------------------------
71// macros
72// ---------------------------------------------------------------------------
73
5737d05f
VZ
74// casts [unfortunately!] needed to call some broken functions which require
75// "char *" instead of "const char *"
2bb67b80 76#define WXSTRINGCAST (wxChar *)(const wxChar *)
e90c1d2a
VZ
77#define wxCSTRINGCAST (wxChar *)(const wxChar *)
78#define wxMBSTRINGCAST (char *)(const char *)
79#define wxWCSTRINGCAST (wchar_t *)(const wchar_t *)
c801d85f 80
c7dc0057
VS
81// like _T(), but for literals in wxString's internal representation, i.e.
82// char* in UTF-8 build and wxChar* otherwise:
83#if wxUSE_UNICODE_UTF8
84 #define wxSTRING_TEXT(str) str
85#else
86 #define wxSTRING_TEXT(str) _T(str)
87#endif
88
e90c1d2a
VZ
89// ----------------------------------------------------------------------------
90// constants
91// ----------------------------------------------------------------------------
92
13874b5c
VZ
93#if WXWIN_COMPATIBILITY_2_6
94
95// deprecated in favour of wxString::npos, don't use in new code
96//
e90c1d2a 97// maximum possible length for a string means "take all string" everywhere
8f93a29f 98#define wxSTRING_MAXLEN wxString::npos
66b6b045 99
13874b5c
VZ
100#endif // WXWIN_COMPATIBILITY_2_6
101
c801d85f 102// ---------------------------------------------------------------------------
e90c1d2a 103// global functions complementing standard C string library replacements for
3c67202d
VZ
104// strlen() and portable strcasecmp()
105//---------------------------------------------------------------------------
e90c1d2a 106
c7554da8 107#if WXWIN_COMPATIBILITY_2_8
e3f6cbd9 108// Use wxXXX() functions from wxcrt.h instead! These functions are for
e90c1d2a 109// backwards compatibility only.
88150e60 110
3c67202d 111// checks whether the passed in pointer is NULL and if the string is empty
c7554da8 112wxDEPRECATED( inline bool IsEmpty(const char *p) );
6d56eb5c 113inline bool IsEmpty(const char *p) { return (!p || !*p); }
c801d85f 114
3c67202d 115// safe version of strlen() (returns 0 if passed NULL pointer)
c7554da8 116wxDEPRECATED( inline size_t Strlen(const char *psz) );
6d56eb5c 117inline size_t Strlen(const char *psz)
c801d85f
KB
118 { return psz ? strlen(psz) : 0; }
119
3c67202d 120// portable strcasecmp/_stricmp
c7554da8 121wxDEPRECATED( inline int Stricmp(const char *psz1, const char *psz2) );
6d56eb5c 122inline int Stricmp(const char *psz1, const char *psz2)
dd1eaa89 123{
7f0586ef
JS
124#if defined(__VISUALC__) && defined(__WXWINCE__)
125 register char c1, c2;
126 do {
127 c1 = tolower(*psz1++);
128 c2 = tolower(*psz2++);
129 } while ( c1 && (c1 == c2) );
130
131 return c1 - c2;
132#elif defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) )
dd1eaa89 133 return _stricmp(psz1, psz2);
91b8de8d 134#elif defined(__SC__)
2432b92d 135 return _stricmp(psz1, psz2);
91b8de8d 136#elif defined(__SALFORDC__)
a3ef5bf5 137 return stricmp(psz1, psz2);
dd1eaa89
VZ
138#elif defined(__BORLANDC__)
139 return stricmp(psz1, psz2);
7be1f0d9
JS
140#elif defined(__WATCOMC__)
141 return stricmp(psz1, psz2);
14704334
VS
142#elif defined(__DJGPP__)
143 return stricmp(psz1, psz2);
91b8de8d
RR
144#elif defined(__EMX__)
145 return stricmp(psz1, psz2);
e2c87f4c 146#elif defined(__WXPM__)
1777b9bb 147 return stricmp(psz1, psz2);
a0be6908
WS
148#elif defined(__WXPALMOS__) || \
149 defined(HAVE_STRCASECMP_IN_STRING_H) || \
1c2d1459
VZ
150 defined(HAVE_STRCASECMP_IN_STRINGS_H) || \
151 defined(__GNUWIN32__)
dd1eaa89 152 return strcasecmp(psz1, psz2);
8be97d65 153#elif defined(__MWERKS__) && !defined(__INTEL__)
17dff81c
SC
154 register char c1, c2;
155 do {
156 c1 = tolower(*psz1++);
157 c2 = tolower(*psz2++);
158 } while ( c1 && (c1 == c2) );
159
160 return c1 - c2;
dd1eaa89
VZ
161#else
162 // almost all compilers/libraries provide this function (unfortunately under
163 // different names), that's why we don't implement our own which will surely
164 // be more efficient than this code (uncomment to use):
165 /*
166 register char c1, c2;
167 do {
168 c1 = tolower(*psz1++);
169 c2 = tolower(*psz2++);
170 } while ( c1 && (c1 == c2) );
171
172 return c1 - c2;
173 */
174
175 #error "Please define string case-insensitive compare for your OS/compiler"
176#endif // OS/compiler
177}
c801d85f 178
c7554da8
VS
179#endif // WXWIN_COMPATIBILITY_2_8
180
c9f78968
VS
181// ----------------------------------------------------------------------------
182// wxCStrData
183// ----------------------------------------------------------------------------
184
185// Lightweight object returned by wxString::c_str() and implicitly convertible
186// to either const char* or const wchar_t*.
9aee0061 187class WXDLLIMPEXP_BASE wxCStrData
c9f78968
VS
188{
189private:
190 // Ctors; for internal use by wxString and wxCStrData only
191 wxCStrData(const wxString *str, size_t offset = 0, bool owned = false)
192 : m_str(str), m_offset(offset), m_owned(owned) {}
193
194public:
195 // Ctor constructs the object from char literal; they are needed to make
196 // operator?: compile and they intentionally take char*, not const char*
92a17abc
VS
197 inline wxCStrData(char *buf);
198 inline wxCStrData(wchar_t *buf);
199 inline wxCStrData(const wxCStrData& data);
c9f78968 200
9aee0061 201 inline ~wxCStrData();
c9f78968 202
9aee0061
VZ
203 // methods defined inline below must be declared inline or mingw32 3.4.5
204 // warns about "<symbol> defined locally after being referenced with
205 // dllimport linkage"
206#if wxUSE_UNICODE_WCHAR
207 inline
208#endif
c9f78968
VS
209 const wchar_t* AsWChar() const;
210 operator const wchar_t*() const { return AsWChar(); }
11aac4ba 211
111d9948 212#if !wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY
9aee0061
VZ
213 inline
214#endif
c9f78968 215 const char* AsChar() const;
dbea442a
VZ
216 const unsigned char* AsUnsignedChar() const
217 { return (const unsigned char *) AsChar(); }
c9f78968 218 operator const char*() const { return AsChar(); }
dbea442a 219 operator const unsigned char*() const { return AsUnsignedChar(); }
11aac4ba
VS
220
221 operator const void*() const { return AsChar(); }
c9f78968 222
681e4412
VS
223 inline const wxCharBuffer AsCharBuf() const;
224 inline const wxWCharBuffer AsWCharBuf() const;
225
9aee0061 226 inline wxString AsString() const;
c9f78968 227
2523e9b7
VS
228 // returns the value as C string in internal representation (equivalent
229 // to AsString().wx_str(), but more efficient)
230 const wxStringCharType *AsInternal() const;
231
c9f78968 232 // allow expressions like "c_str()[0]":
9aee0061 233 inline wxUniChar operator[](size_t n) const;
c9f78968 234 wxUniChar operator[](int n) const { return operator[](size_t(n)); }
c1df0a3b 235 wxUniChar operator[](long n) const { return operator[](size_t(n)); }
c9f78968
VS
236#ifndef wxSIZE_T_IS_UINT
237 wxUniChar operator[](unsigned int n) const { return operator[](size_t(n)); }
238#endif // size_t != unsigned int
239
acf0c9ac 240 // these operators are needed to emulate the pointer semantics of c_str():
c9f78968
VS
241 // expressions like "wxChar *p = str.c_str() + 1;" should continue to work
242 // (we need both versions to resolve ambiguities):
243 wxCStrData operator+(int n) const
244 { return wxCStrData(m_str, m_offset + n, m_owned); }
acf0c9ac
VZ
245 wxCStrData operator+(long n) const
246 { return wxCStrData(m_str, m_offset + n, m_owned); }
c9f78968
VS
247 wxCStrData operator+(size_t n) const
248 { return wxCStrData(m_str, m_offset + n, m_owned); }
249
f4c90fdf
VS
250 // and these for "str.c_str() + n - 2":
251 wxCStrData operator-(int n) const
252 {
253 wxASSERT_MSG( n <= (int)m_offset,
254 _T("attempt to construct address before the beginning of the string") );
255 return wxCStrData(m_str, m_offset - n, m_owned);
256 }
257 wxCStrData operator-(long n) const
258 {
259 wxASSERT_MSG( n <= (int)m_offset,
260 _T("attempt to construct address before the beginning of the string") );
261 return wxCStrData(m_str, m_offset - n, m_owned);
262 }
263 wxCStrData operator-(size_t n) const
264 {
759d51f2 265 wxASSERT_MSG( n <= m_offset,
f4c90fdf
VS
266 _T("attempt to construct address before the beginning of the string") );
267 return wxCStrData(m_str, m_offset - n, m_owned);
268 }
269
b77afb41 270 // this operator is needed to make expressions like "*c_str()" or
c9f78968 271 // "*(c_str() + 2)" work
9aee0061 272 inline wxUniChar operator*() const;
c9f78968
VS
273
274private:
275 const wxString *m_str;
276 size_t m_offset;
277 bool m_owned;
278
279 friend class WXDLLIMPEXP_BASE wxString;
280};
281
282// ----------------------------------------------------------------------------
283// wxStringPrintfMixin
284// ---------------------------------------------------------------------------
285
286// NB: VC6 has a bug that causes linker errors if you have template methods
287// in a class using __declspec(dllimport). The solution is to split such
288// class into two classes, one that contains the template methods and does
289// *not* use WXDLLIMPEXP_BASE and another class that contains the rest
290// (with DLL linkage).
291//
292// We only do this for VC6 here, because the code is less efficient
293// (Printf() has to use dynamic_cast<>) and because OpenWatcom compiler
294// cannot compile this code.
295
296#if defined(__VISUALC__) && __VISUALC__ < 1300
297 #define wxNEEDS_WXSTRING_PRINTF_MIXIN
298#endif
299
300#ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
301// this class contains implementation of wxString's vararg methods, it's
302// exported from wxBase DLL
303class WXDLLIMPEXP_BASE wxStringPrintfMixinBase
304{
305protected:
306 wxStringPrintfMixinBase() {}
307
d1f6e2cf
VS
308#if !wxUSE_UTF8_LOCALE_ONLY
309 int DoPrintfWchar(const wxChar *format, ...);
310 static wxString DoFormatWchar(const wxChar *format, ...);
311#endif
312#if wxUSE_UNICODE_UTF8
313 int DoPrintfUtf8(const char *format, ...);
314 static wxString DoFormatUtf8(const char *format, ...);
315#endif
c9f78968
VS
316};
317
318// this class contains template wrappers for wxString's vararg methods, it's
319// intentionally *not* exported from the DLL in order to fix the VC6 bug
320// described above
321class wxStringPrintfMixin : public wxStringPrintfMixinBase
322{
323private:
324 // to further complicate things, we can't return wxString from
325 // wxStringPrintfMixin::Format() because wxString is not yet declared at
326 // this point; the solution is to use this fake type trait template - this
327 // way the compiler won't know the return type until Format() is used
328 // (this doesn't compile with Watcom, but VC6 compiles it just fine):
329 template<typename T> struct StringReturnType
330 {
331 typedef wxString type;
332 };
333
334public:
335 // these are duplicated wxString methods, they're also declared below
336 // if !wxNEEDS_WXSTRING_PRINTF_MIXIN:
337
2523e9b7 338 // static wxString Format(const wString& format, ...) ATTRIBUTE_PRINTF_1;
d1f6e2cf 339 WX_DEFINE_VARARG_FUNC_SANS_N0(static typename StringReturnType<T1>::type,
1528e0b8 340 Format, 1, (const wxFormatString&),
d1f6e2cf 341 DoFormatWchar, DoFormatUtf8)
2523e9b7
VS
342 // We have to implement the version without template arguments manually
343 // because of the StringReturnType<> hack, although WX_DEFINE_VARARG_FUNC
344 // normally does it itself. It has to be a template so that we can use
345 // the hack, even though there's no real template parameter:
346 struct FormatDummyArg {} ;
347
348 template<typename T>
349 inline static typename StringReturnType<T>::type
1528e0b8 350 Format(const wxFormatString& fmt, FormatDummyArg dummy = FormatDummyArg())
2523e9b7 351 {
1528e0b8 352 return DoFormatWchar(fmt);
2523e9b7
VS
353 }
354
355 // int Printf(const wxString& format, ...);
1528e0b8 356 WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const wxFormatString&),
d1f6e2cf 357 DoPrintfWchar, DoPrintfUtf8)
2523e9b7 358 // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2;
1528e0b8 359 WX_DEFINE_VARARG_FUNC(int, sprintf, 1, (const wxFormatString&),
d1f6e2cf 360 DoPrintfWchar, DoPrintfUtf8)
c9f78968
VS
361
362protected:
363 wxStringPrintfMixin() : wxStringPrintfMixinBase() {}
364};
365#endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
366
367
b6339dde
VZ
368// ----------------------------------------------------------------------------
369// wxString: string class trying to be compatible with std::string, MFC
370// CString and wxWindows 1.x wxString all at once
e87b7833
MB
371// ---------------------------------------------------------------------------
372
c9f78968
VS
373#ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
374 // "non dll-interface class 'wxStringPrintfMixin' used as base interface
375 // for dll-interface class 'wxString'" -- this is OK in our case
376 #pragma warning (disable:4275)
e87b7833
MB
377#endif
378
8f93a29f 379class WXDLLIMPEXP_BASE wxString
c9f78968 380#ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
8f93a29f 381 : public wxStringPrintfMixin
c9f78968
VS
382#endif
383{
e87b7833
MB
384 // NB: special care was taken in arranging the member functions in such order
385 // that all inline functions can be effectively inlined, verify that all
20aa779e 386 // performance critical functions are still inlined if you change order!
8f93a29f 387public:
8f93a29f
VS
388 // an 'invalid' value for string index, moved to this place due to a CW bug
389 static const size_t npos;
8f93a29f 390
e87b7833
MB
391private:
392 // if we hadn't made these operators private, it would be possible to
393 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
394 // converted to char in C and we do have operator=(char)
395 //
396 // NB: we don't need other versions (short/long and unsigned) as attempt
397 // to assign another numeric type to wxString will now result in
398 // ambiguity between operator=(char) and operator=(int)
399 wxString& operator=(int);
400
8f93a29f
VS
401 // these methods are not implemented - there is _no_ conversion from int to
402 // string, you're doing something wrong if the compiler wants to call it!
403 //
404 // try `s << i' or `s.Printf("%d", i)' instead
405 wxString(int);
406
407
408 // buffer for holding temporary substring when using any of the methods
409 // that take (char*,size_t) or (wchar_t*,size_t) arguments:
8f93a29f
VS
410 template<typename T>
411 struct SubstrBufFromType
412 {
413 T data;
414 size_t len;
415
8f93a29f
VS
416 SubstrBufFromType(const T& data_, size_t len_)
417 : data(data_), len(len_) {}
418 };
419
420#if wxUSE_UNICODE_UTF8
81727065
VS
421 // even char* -> char* needs conversion, from locale charset to UTF-8
422 typedef SubstrBufFromType<wxCharBuffer> SubstrBufFromWC;
423 typedef SubstrBufFromType<wxCharBuffer> SubstrBufFromMB;
8f93a29f
VS
424#elif wxUSE_UNICODE_WCHAR
425 typedef SubstrBufFromType<const wchar_t*> SubstrBufFromWC;
426 typedef SubstrBufFromType<wxWCharBuffer> SubstrBufFromMB;
8f93a29f
VS
427#else
428 typedef SubstrBufFromType<const char*> SubstrBufFromMB;
429 typedef SubstrBufFromType<wxCharBuffer> SubstrBufFromWC;
8f93a29f
VS
430#endif
431
432
433 // Functions implementing primitive operations on string data; wxString
434 // methods and iterators are implemented in terms of it. The differences
435 // between UTF-8 and wchar_t* representations of the string are mostly
436 // contained here.
437
81727065
VS
438#if wxUSE_UNICODE_UTF8
439 static SubstrBufFromMB ConvertStr(const char *psz, size_t nLength,
440 const wxMBConv& conv);
441 static SubstrBufFromWC ConvertStr(const wchar_t *pwz, size_t nLength,
442 const wxMBConv& conv);
443#elif wxUSE_UNICODE_WCHAR
8f93a29f 444 static SubstrBufFromMB ConvertStr(const char *psz, size_t nLength,
a962cdf4 445 const wxMBConv& conv);
8f93a29f
VS
446#else
447 static SubstrBufFromWC ConvertStr(const wchar_t *pwz, size_t nLength,
a962cdf4 448 const wxMBConv& conv);
8f93a29f
VS
449#endif
450
451#if !wxUSE_UNICODE_UTF8 // wxUSE_UNICODE_WCHAR or !wxUSE_UNICODE
a962cdf4 452 // returns C string encoded as the implementation expects:
8f93a29f 453 #if wxUSE_UNICODE
a962cdf4 454 static const wchar_t* ImplStr(const wchar_t* str)
e8f59039 455 { return str ? str : wxT(""); }
a962cdf4 456 static const SubstrBufFromWC ImplStr(const wchar_t* str, size_t n)
e8f59039
VS
457 { return SubstrBufFromWC(str, (str && n == npos) ? wxWcslen(str) : n); }
458 static wxWCharBuffer ImplStr(const char* str,
459 const wxMBConv& conv = wxConvLibc)
460 { return ConvertStr(str, npos, conv).data; }
461 static SubstrBufFromMB ImplStr(const char* str, size_t n,
462 const wxMBConv& conv = wxConvLibc)
463 { return ConvertStr(str, n, conv); }
8f93a29f 464 #else
e8f59039
VS
465 static const char* ImplStr(const char* str,
466 const wxMBConv& WXUNUSED(conv) = wxConvLibc)
467 { return str ? str : ""; }
468 static const SubstrBufFromMB ImplStr(const char* str, size_t n,
469 const wxMBConv& WXUNUSED(conv) = wxConvLibc)
470 { return SubstrBufFromMB(str, (str && n == npos) ? wxStrlen(str) : n); }
8f93a29f
VS
471 static wxCharBuffer ImplStr(const wchar_t* str)
472 { return ConvertStr(str, npos, wxConvLibc).data; }
473 static SubstrBufFromWC ImplStr(const wchar_t* str, size_t n)
474 { return ConvertStr(str, n, wxConvLibc); }
475 #endif
476
8f93a29f
VS
477 // translates position index in wxString to/from index in underlying
478 // wxStringImpl:
479 static size_t PosToImpl(size_t pos) { return pos; }
480 static void PosLenToImpl(size_t pos, size_t len,
481 size_t *implPos, size_t *implLen)
482 { *implPos = pos; *implLen = len; }
11aac4ba 483 static size_t LenToImpl(size_t len) { return len; }
8f93a29f
VS
484 static size_t PosFromImpl(size_t pos) { return pos; }
485
486#else // wxUSE_UNICODE_UTF8
487
467175ab
VS
488 static wxCharBuffer ImplStr(const char* str,
489 const wxMBConv& conv = wxConvLibc)
490 { return ConvertStr(str, npos, conv).data; }
491 static SubstrBufFromMB ImplStr(const char* str, size_t n,
492 const wxMBConv& conv = wxConvLibc)
493 { return ConvertStr(str, n, conv); }
81727065 494
467175ab 495 static wxCharBuffer ImplStr(const wchar_t* str)
5487ff0f 496 { return ConvertStr(str, npos, wxMBConvUTF8()).data; }
467175ab 497 static SubstrBufFromWC ImplStr(const wchar_t* str, size_t n)
5487ff0f 498 { return ConvertStr(str, n, wxMBConvUTF8()); }
81727065 499
8f93a29f
VS
500 size_t PosToImpl(size_t pos) const
501 {
502 if ( pos == 0 || pos == npos )
503 return pos;
504 else
cf9a878b 505 return (begin() + pos).impl() - m_impl.begin();
8f93a29f
VS
506 }
507
81727065
VS
508 void PosLenToImpl(size_t pos, size_t len, size_t *implPos, size_t *implLen) const;
509
510 size_t LenToImpl(size_t len) const
511 {
512 size_t pos, len2;
513 PosLenToImpl(0, len, &pos, &len2);
514 return len2;
515 }
516
8f93a29f
VS
517 size_t PosFromImpl(size_t pos) const
518 {
519 if ( pos == 0 || pos == npos )
520 return pos;
521 else
c67b782d 522 return const_iterator(m_impl.begin() + pos) - begin();
8f93a29f 523 }
8f93a29f
VS
524#endif // !wxUSE_UNICODE_UTF8/wxUSE_UNICODE_UTF8
525
8f93a29f
VS
526public:
527 // standard types
528 typedef wxUniChar value_type;
529 typedef wxUniChar char_type;
530 typedef wxUniCharRef reference;
531 typedef wxChar* pointer;
532 typedef const wxChar* const_pointer;
533
534 typedef size_t size_type;
535 typedef wxUniChar const_reference;
536
a962cdf4 537#if wxUSE_STL
81727065
VS
538 #if wxUSE_UNICODE_UTF8
539 // random access is not O(1), as required by Random Access Iterator
540 #define WX_STR_ITERATOR_TAG std::bidirectional_iterator_tag
541 #else
542 #define WX_STR_ITERATOR_TAG std::random_access_iterator_tag
543 #endif
a962cdf4
VS
544#else
545 #define WX_STR_ITERATOR_TAG void /* dummy type */
546#endif
547
c67b782d
VS
548 #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, \
549 reference_type, reference_ctor) \
8f93a29f
VS
550 private: \
551 typedef wxStringImpl::iterator_name underlying_iterator; \
552 public: \
a962cdf4 553 typedef WX_STR_ITERATOR_TAG iterator_category; \
8f93a29f 554 typedef wxUniChar value_type; \
a962cdf4 555 typedef int difference_type; \
8f93a29f
VS
556 typedef reference_type reference; \
557 typedef pointer_type pointer; \
558 \
c67b782d 559 reference operator*() const { return reference_ctor; } \
a962cdf4 560 reference operator[](size_t n) const { return *(*this + n); } \
8f93a29f
VS
561 \
562 iterator_name& operator++() \
467175ab 563 { wxStringOperations::IncIter(m_cur); return *this; } \
8f93a29f 564 iterator_name& operator--() \
467175ab 565 { wxStringOperations::DecIter(m_cur); return *this; } \
8f93a29f
VS
566 iterator_name operator++(int) \
567 { \
568 iterator_name tmp = *this; \
467175ab 569 wxStringOperations::IncIter(m_cur); \
8f93a29f
VS
570 return tmp; \
571 } \
572 iterator_name operator--(int) \
573 { \
574 iterator_name tmp = *this; \
467175ab 575 wxStringOperations::DecIter(m_cur); \
8f93a29f
VS
576 return tmp; \
577 } \
578 \
30d560f4 579 iterator_name& operator+=(int n) \
467175ab
VS
580 { \
581 m_cur = wxStringOperations::AddToIter(m_cur, n); \
582 return *this; \
583 } \
30d560f4 584 iterator_name& operator+=(size_t n) \
467175ab
VS
585 { \
586 m_cur = wxStringOperations::AddToIter(m_cur, (int)n); \
587 return *this; \
588 } \
30d560f4 589 iterator_name& operator-=(int n) \
467175ab
VS
590 { \
591 m_cur = wxStringOperations::AddToIter(m_cur, -n); \
592 return *this; \
593 } \
30d560f4 594 iterator_name& operator-=(size_t n) \
467175ab
VS
595 { \
596 m_cur = wxStringOperations::AddToIter(m_cur, -(int)n); \
597 return *this; \
598 } \
8f93a29f 599 \
89360a8c 600 difference_type operator-(const iterator_name& i) const \
467175ab 601 { return wxStringOperations::DiffIters(m_cur, i.m_cur); } \
8f93a29f 602 \
f2a1b1bd 603 bool operator==(const iterator_name& i) const \
8f93a29f
VS
604 { return m_cur == i.m_cur; } \
605 bool operator!=(const iterator_name& i) const \
606 { return m_cur != i.m_cur; } \
607 \
608 bool operator<(const iterator_name& i) const \
609 { return m_cur < i.m_cur; } \
610 bool operator>(const iterator_name& i) const \
611 { return m_cur > i.m_cur; } \
612 bool operator<=(const iterator_name& i) const \
613 { return m_cur <= i.m_cur; } \
614 bool operator>=(const iterator_name& i) const \
615 { return m_cur >= i.m_cur; } \
616 \
617 private: \
618 /* for internal wxString use only: */ \
cf9a878b 619 underlying_iterator impl() const { return m_cur; } \
8f93a29f
VS
620 \
621 friend class WXDLLIMPEXP_BASE wxString; \
8f93a29f
VS
622 friend class WXDLLIMPEXP_BASE wxCStrData; \
623 \
624 private: \
89360a8c 625 underlying_iterator m_cur
8f93a29f
VS
626
627 class const_iterator;
628
81727065
VS
629#if wxUSE_UNICODE_UTF8
630 class iterator
631 {
c67b782d
VS
632 // NB: In UTF-8 build, (non-const) iterator needs to keep reference
633 // to the underlying wxStringImpl, because UTF-8 is variable-length
634 // encoding and changing the value pointer to by an iterator using
635 // its operator* requires calling wxStringImpl::replace() if the old
636 // and new values differ in their encoding's length.
81727065 637
c67b782d
VS
638 WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef,
639 wxUniCharRef::CreateForString(m_str, m_cur));
c7dc0057 640
c67b782d
VS
641 public:
642 iterator(const iterator& i) : m_cur(i.m_cur), m_str(i.m_str) {}
81727065
VS
643
644 iterator operator+(int n) const
c67b782d 645 { return iterator(m_str, wxStringOperations::AddToIter(m_cur, n)); }
81727065 646 iterator operator+(size_t n) const
c67b782d 647 { return iterator(m_str, wxStringOperations::AddToIter(m_cur, (int)n)); }
81727065 648 iterator operator-(int n) const
c67b782d 649 { return iterator(m_str, wxStringOperations::AddToIter(m_cur, -n)); }
81727065 650 iterator operator-(size_t n) const
c67b782d 651 { return iterator(m_str, wxStringOperations::AddToIter(m_cur, -(int)n)); }
81727065
VS
652
653 private:
654 iterator(wxString *str, underlying_iterator ptr)
c67b782d
VS
655 : m_cur(ptr), m_str(str->m_impl) {}
656 iterator(wxStringImpl& str, underlying_iterator ptr)
657 : m_cur(ptr), m_str(str) {}
c7dc0057 658
c67b782d 659 wxStringImpl& m_str;
81727065
VS
660
661 friend class const_iterator;
662 };
cf9a878b
VS
663
664 size_t IterToImplPos(wxString::iterator i) const
665 { return wxStringImpl::const_iterator(i.impl()) - m_impl.begin(); }
666
81727065 667#else // !wxUSE_UNICODE_UTF8
cf9a878b 668
8f93a29f
VS
669 class iterator
670 {
c67b782d
VS
671 WX_STR_ITERATOR_IMPL(iterator, wxChar*, wxUniCharRef,
672 wxUniCharRef::CreateForString(m_cur));
8f93a29f 673
81727065
VS
674 public:
675 iterator(const iterator& i) : m_cur(i.m_cur) {}
676
677 iterator operator+(int n) const
467175ab 678 { return iterator(wxStringOperations::AddToIter(m_cur, n)); }
81727065 679 iterator operator+(size_t n) const
467175ab 680 { return iterator(wxStringOperations::AddToIter(m_cur, (int)n)); }
81727065 681 iterator operator-(int n) const
467175ab 682 { return iterator(wxStringOperations::AddToIter(m_cur, -n)); }
81727065 683 iterator operator-(size_t n) const
467175ab 684 { return iterator(wxStringOperations::AddToIter(m_cur, -(int)n)); }
81727065
VS
685
686 private:
687 // for internal wxString use only:
688 iterator(underlying_iterator ptr) : m_cur(ptr) {}
689 iterator(wxString *WXUNUSED(str), underlying_iterator ptr) : m_cur(ptr) {}
690
8f93a29f
VS
691 friend class const_iterator;
692 };
c67b782d 693#endif // wxUSE_UNICODE_UTF8/!wxUSE_UNICODE_UTF8
8f93a29f
VS
694
695 class const_iterator
696 {
697 // NB: reference_type is intentionally value, not reference, the character
698 // may be encoded differently in wxString data:
c67b782d
VS
699 WX_STR_ITERATOR_IMPL(const_iterator, const wxChar*, wxUniChar,
700 wxStringOperations::DecodeChar(m_cur));
8f93a29f
VS
701
702 public:
81727065 703 const_iterator(const const_iterator& i) : m_cur(i.m_cur) {}
8f93a29f 704 const_iterator(const iterator& i) : m_cur(i.m_cur) {}
81727065
VS
705
706 const_iterator operator+(int n) const
467175ab 707 { return const_iterator(wxStringOperations::AddToIter(m_cur, n)); }
81727065 708 const_iterator operator+(size_t n) const
467175ab 709 { return const_iterator(wxStringOperations::AddToIter(m_cur, (int)n)); }
81727065 710 const_iterator operator-(int n) const
467175ab 711 { return const_iterator(wxStringOperations::AddToIter(m_cur, -n)); }
81727065 712 const_iterator operator-(size_t n) const
467175ab 713 { return const_iterator(wxStringOperations::AddToIter(m_cur, -(int)n)); }
81727065
VS
714
715 private:
716 // for internal wxString use only:
717 const_iterator(underlying_iterator ptr) : m_cur(ptr) {}
8f93a29f
VS
718 };
719
a962cdf4 720 #undef WX_STR_ITERATOR_TAG
8f93a29f
VS
721 #undef WX_STR_ITERATOR_IMPL
722
723 friend class iterator;
724 friend class const_iterator;
725
726 template <typename T>
727 class reverse_iterator_impl
728 {
729 public:
730 typedef T iterator_type;
a962cdf4
VS
731
732 typedef typename T::iterator_category iterator_category;
8f93a29f 733 typedef typename T::value_type value_type;
a962cdf4 734 typedef typename T::difference_type difference_type;
8f93a29f
VS
735 typedef typename T::reference reference;
736 typedef typename T::pointer *pointer;
737
738 reverse_iterator_impl(iterator_type i) : m_cur(i) {}
739 reverse_iterator_impl(const reverse_iterator_impl& ri)
740 : m_cur(ri.m_cur) {}
741
742 iterator_type base() const { return m_cur; }
743
744 reference operator*() const { return *(m_cur-1); }
a962cdf4 745 reference operator[](size_t n) const { return *(*this + n); }
8f93a29f
VS
746
747 reverse_iterator_impl& operator++()
748 { --m_cur; return *this; }
749 reverse_iterator_impl operator++(int)
750 { reverse_iterator_impl tmp = *this; --m_cur; return tmp; }
751 reverse_iterator_impl& operator--()
752 { ++m_cur; return *this; }
753 reverse_iterator_impl operator--(int)
754 { reverse_iterator_impl tmp = *this; ++m_cur; return tmp; }
755
50eeb96f 756 // NB: explicit <T> in the functions below is to keep BCC 5.5 happy
8f93a29f 757 reverse_iterator_impl operator+(int n) const
50eeb96f 758 { return reverse_iterator_impl<T>(m_cur - n); }
8f93a29f 759 reverse_iterator_impl operator+(size_t n) const
50eeb96f 760 { return reverse_iterator_impl<T>(m_cur - n); }
8f93a29f 761 reverse_iterator_impl operator-(int n) const
50eeb96f 762 { return reverse_iterator_impl<T>(m_cur + n); }
8f93a29f 763 reverse_iterator_impl operator-(size_t n) const
50eeb96f 764 { return reverse_iterator_impl<T>(m_cur + n); }
8f93a29f
VS
765 reverse_iterator_impl operator+=(int n)
766 { m_cur -= n; return *this; }
767 reverse_iterator_impl operator+=(size_t n)
768 { m_cur -= n; return *this; }
769 reverse_iterator_impl operator-=(int n)
770 { m_cur += n; return *this; }
771 reverse_iterator_impl operator-=(size_t n)
772 { m_cur += n; return *this; }
773
774 unsigned operator-(const reverse_iterator_impl& i) const
775 { return i.m_cur - m_cur; }
776
777 bool operator==(const reverse_iterator_impl& ri) const
778 { return m_cur == ri.m_cur; }
779 bool operator!=(const reverse_iterator_impl& ri) const
780 { return !(*this == ri); }
781
782 bool operator<(const reverse_iterator_impl& i) const
783 { return m_cur > i.m_cur; }
784 bool operator>(const reverse_iterator_impl& i) const
785 { return m_cur < i.m_cur; }
786 bool operator<=(const reverse_iterator_impl& i) const
787 { return m_cur >= i.m_cur; }
788 bool operator>=(const reverse_iterator_impl& i) const
789 { return m_cur <= i.m_cur; }
790
791 private:
792 iterator_type m_cur;
793 };
e87b7833 794
8f93a29f
VS
795 typedef reverse_iterator_impl<iterator> reverse_iterator;
796 typedef reverse_iterator_impl<const_iterator> const_reverse_iterator;
e90c1d2a 797
67cff9dc
VZ
798private:
799 // used to transform an expression built using c_str() (and hence of type
800 // wxCStrData) to an iterator into the string
801 static const_iterator CreateConstIterator(const wxCStrData& data)
802 {
c67b782d 803 return const_iterator(data.m_str->begin() + data.m_offset);
67cff9dc
VZ
804 }
805
59953bf4
VS
806 // in UTF-8 STL build, creation from std::string requires conversion under
807 // non-UTF8 locales, so we can't have and use wxString(wxStringImpl) ctor;
808 // instead we define dummy type that lets us have wxString ctor for creation
809 // from wxStringImpl that couldn't be used by user code (in all other builds,
810 // "standard" ctors can be used):
811#if wxUSE_UNICODE_UTF8 && wxUSE_STL_BASED_WXSTRING
812 struct CtorFromStringImplTag {};
813
814 wxString(CtorFromStringImplTag* WXUNUSED(dummy), const wxStringImpl& src)
815 : m_impl(src) {}
816
817 static wxString FromImpl(const wxStringImpl& src)
818 { return wxString((CtorFromStringImplTag*)NULL, src); }
819#else
82a99c69 820 #if !wxUSE_STL_BASED_WXSTRING
59953bf4 821 wxString(const wxStringImpl& src) : m_impl(src) { }
82a99c69
VS
822 // else: already defined as wxString(wxStdString) below
823 #endif
59953bf4
VS
824 static wxString FromImpl(const wxStringImpl& src) { return wxString(src); }
825#endif
826
67cff9dc
VZ
827public:
828 // constructors and destructor
829 // ctor for an empty string
830 wxString() {}
831
832 // copy ctor
67cff9dc
VZ
833 wxString(const wxString& stringSrc) : m_impl(stringSrc.m_impl) { }
834
835 // string containing nRepeat copies of ch
836 wxString(wxUniChar ch, size_t nRepeat = 1)
837 { assign(nRepeat, ch); }
838 wxString(size_t nRepeat, wxUniChar ch)
839 { assign(nRepeat, ch); }
840 wxString(wxUniCharRef ch, size_t nRepeat = 1)
841 { assign(nRepeat, ch); }
842 wxString(size_t nRepeat, wxUniCharRef ch)
843 { assign(nRepeat, ch); }
844 wxString(char ch, size_t nRepeat = 1)
845 { assign(nRepeat, ch); }
846 wxString(size_t nRepeat, char ch)
847 { assign(nRepeat, ch); }
848 wxString(wchar_t ch, size_t nRepeat = 1)
849 { assign(nRepeat, ch); }
850 wxString(size_t nRepeat, wchar_t ch)
851 { assign(nRepeat, ch); }
852
853 // ctors from char* strings:
854 wxString(const char *psz)
855 : m_impl(ImplStr(psz)) {}
856 wxString(const char *psz, const wxMBConv& conv)
857 : m_impl(ImplStr(psz, conv)) {}
858 wxString(const char *psz, size_t nLength)
859 { assign(psz, nLength); }
860 wxString(const char *psz, const wxMBConv& conv, size_t nLength)
861 {
862 SubstrBufFromMB str(ImplStr(psz, nLength, conv));
863 m_impl.assign(str.data, str.len);
864 }
865
866 // and unsigned char*:
867 wxString(const unsigned char *psz)
868 : m_impl(ImplStr((const char*)psz)) {}
869 wxString(const unsigned char *psz, const wxMBConv& conv)
870 : m_impl(ImplStr((const char*)psz, conv)) {}
871 wxString(const unsigned char *psz, size_t nLength)
872 { assign((const char*)psz, nLength); }
873 wxString(const unsigned char *psz, const wxMBConv& conv, size_t nLength)
874 {
875 SubstrBufFromMB str(ImplStr((const char*)psz, nLength, conv));
876 m_impl.assign(str.data, str.len);
877 }
878
879 // ctors from wchar_t* strings:
880 wxString(const wchar_t *pwz)
881 : m_impl(ImplStr(pwz)) {}
882 wxString(const wchar_t *pwz, const wxMBConv& WXUNUSED(conv))
883 : m_impl(ImplStr(pwz)) {}
884 wxString(const wchar_t *pwz, size_t nLength)
885 { assign(pwz, nLength); }
886 wxString(const wchar_t *pwz, const wxMBConv& WXUNUSED(conv), size_t nLength)
887 { assign(pwz, nLength); }
888
889 wxString(const wxCharBuffer& buf)
890 { assign(buf.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
891 wxString(const wxWCharBuffer& buf)
892 { assign(buf.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
893
894 wxString(const wxCStrData& cstr)
895 : m_impl(cstr.AsString().m_impl) { }
896
897 // as we provide both ctors with this signature for both char and unsigned
898 // char string, we need to provide one for wxCStrData to resolve ambiguity
899 wxString(const wxCStrData& cstr, size_t nLength)
900 : m_impl(cstr.AsString().Mid(0, nLength).m_impl) {}
901
902 // and because wxString is convertible to wxCStrData and const wxChar *
903 // we also need to provide this one
904 wxString(const wxString& str, size_t nLength)
1545d942 905 { assign(str, nLength); }
67cff9dc
VZ
906
907 // even if we're not built with wxUSE_STL == 1 it is very convenient to allow
59953bf4
VS
908 // implicit conversions from std::string to wxString and vice verse as this
909 // allows to use the same strings in non-GUI and GUI code, however we don't
910 // want to unconditionally add this ctor as it would make wx lib dependent on
67cff9dc
VZ
911 // libstdc++ on some Linux versions which is bad, so instead we ask the
912 // client code to define this wxUSE_STD_STRING symbol if they need it
59953bf4 913#if wxUSE_STD_STRING
59953bf4
VS
914 #if wxUSE_UNICODE_WCHAR
915 wxString(const wxStdWideString& str) : m_impl(str) {}
916 #else // UTF-8 or ANSI
917 wxString(const wxStdWideString& str)
918 { assign(str.c_str(), str.length()); }
919 #endif
920
921 #if !wxUSE_UNICODE // ANSI build
922 // FIXME-UTF8: do this in UTF8 build #if wxUSE_UTF8_LOCALE_ONLY, too
923 wxString(const std::string& str) : m_impl(str) {}
924 #else // Unicode
925 wxString(const std::string& str)
926 { assign(str.c_str(), str.length()); }
927 #endif
928
929 #if wxUSE_UNICODE_WCHAR && wxUSE_STL_BASED_WXSTRING
930 // wxStringImpl is std::string in the encoding we want
931 operator const wxStdWideString&() const { return m_impl; }
932 #else
933 // wxStringImpl is either not std::string or needs conversion
934 operator wxStdWideString() const
935 // FIXME-UTF8: broken for embedded NULs
936 { return wxStdWideString(wc_str()); }
937 #endif
938
111d9948 939 #if (!wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY) && wxUSE_STL_BASED_WXSTRING
59953bf4
VS
940 // wxStringImpl is std::string in the encoding we want
941 operator const std::string&() const { return m_impl; }
942 #else
943 // wxStringImpl is either not std::string or needs conversion
944 operator std::string() const
945 // FIXME-UTF8: broken for embedded NULs
946 { return std::string(mb_str()); }
947 #endif
111d9948 948#endif // wxUSE_STL
67cff9dc 949
8f93a29f 950 // first valid index position
c67b782d 951 const_iterator begin() const { return const_iterator(m_impl.begin()); }
81727065 952 iterator begin() { return iterator(this, m_impl.begin()); }
8f93a29f 953 // position one after the last valid one
c67b782d 954 const_iterator end() const { return const_iterator(m_impl.end()); }
81727065 955 iterator end() { return iterator(this, m_impl.end()); }
8f93a29f
VS
956
957 // first element of the reversed string
958 const_reverse_iterator rbegin() const
959 { return const_reverse_iterator(end()); }
960 reverse_iterator rbegin()
961 { return reverse_iterator(end()); }
962 // one beyond the end of the reversed string
963 const_reverse_iterator rend() const
964 { return const_reverse_iterator(begin()); }
965 reverse_iterator rend()
966 { return reverse_iterator(begin()); }
967
968 // std::string methods:
969#if wxUSE_UNICODE_UTF8
970 size_t length() const { return end() - begin(); } // FIXME-UTF8: optimize!
971#else
972 size_t length() const { return m_impl.length(); }
973#endif
ce76f779 974
8f93a29f
VS
975 size_type size() const { return length(); }
976 size_type max_size() const { return npos; }
e90c1d2a 977
8f93a29f 978 bool empty() const { return m_impl.empty(); }
b77afb41 979
8f93a29f
VS
980 size_type capacity() const { return m_impl.capacity(); } // FIXME-UTF8
981 void reserve(size_t sz) { m_impl.reserve(sz); } // FIXME-UTF8
b77afb41 982
8f93a29f
VS
983 void resize(size_t nSize, wxUniChar ch = wxT('\0'))
984 {
985#if wxUSE_UNICODE_UTF8
986 if ( !ch.IsAscii() )
987 {
988 size_t len = length();
989 if ( nSize == len)
990 return;
991 else if ( nSize < len )
992 erase(nSize);
993 else
994 append(nSize - len, ch);
995 }
996 else
997#endif
998 m_impl.resize(nSize, (wxStringCharType)ch);
999 }
e90c1d2a 1000
8f93a29f
VS
1001 wxString substr(size_t nStart = 0, size_t nLen = npos) const
1002 {
1003 size_t pos, len;
1004 PosLenToImpl(nStart, nLen, &pos, &len);
59953bf4 1005 return FromImpl(m_impl.substr(pos, len));
8f93a29f 1006 }
e90c1d2a 1007
3c67202d
VZ
1008 // generic attributes & operations
1009 // as standard strlen()
e87b7833 1010 size_t Len() const { return length(); }
3c67202d 1011 // string contains any characters?
e87b7833 1012 bool IsEmpty() const { return empty(); }
d775fa82 1013 // empty string is "false", so !str will return true
20aa779e 1014 bool operator!() const { return empty(); }
735d1db6
VZ
1015 // truncate the string to given length
1016 wxString& Truncate(size_t uiLen);
3c67202d 1017 // empty string contents
dd1eaa89
VZ
1018 void Empty()
1019 {
735d1db6 1020 Truncate(0);
dd1eaa89 1021
5a8231ef 1022 wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") );
7be07660 1023 }
3c67202d 1024 // empty the string and free memory
7be07660
VZ
1025 void Clear()
1026 {
e87b7833
MB
1027 wxString tmp(wxEmptyString);
1028 swap(tmp);
dd1eaa89
VZ
1029 }
1030
3c67202d
VZ
1031 // contents test
1032 // Is an ascii value
c801d85f 1033 bool IsAscii() const;
3c67202d 1034 // Is a number
c801d85f 1035 bool IsNumber() const;
3c67202d 1036 // Is a word
c801d85f 1037 bool IsWord() const;
c801d85f 1038
3c67202d
VZ
1039 // data access (all indexes are 0 based)
1040 // read access
8f93a29f
VS
1041 wxUniChar at(size_t n) const
1042 { return *(begin() + n); } // FIXME-UTF8: optimize?
c9f78968 1043 wxUniChar GetChar(size_t n) const
9d9ad673 1044 { return at(n); }
3c67202d 1045 // read/write access
8f93a29f
VS
1046 wxUniCharRef at(size_t n)
1047 { return *(begin() + n); } // FIXME-UTF8: optimize?
c9f78968 1048 wxUniCharRef GetWritableChar(size_t n)
9d9ad673 1049 { return at(n); }
3c67202d 1050 // write access
c9f78968 1051 void SetChar(size_t n, wxUniChar ch)
9d9ad673 1052 { at(n) = ch; }
c801d85f 1053
3c67202d 1054 // get last character
8f93a29f 1055 wxUniChar Last() const
564ab31a
VS
1056 {
1057 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1058 return *rbegin();
1059 }
09443a26 1060
3c67202d 1061 // get writable last character
c9f78968 1062 wxUniCharRef Last()
564ab31a
VS
1063 {
1064 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1065 return *rbegin();
1066 }
c801d85f 1067
d836ee96 1068 /*
9d9ad673 1069 Note that we we must define all of the overloads below to avoid
c9f78968 1070 ambiguity when using str[0].
d836ee96 1071 */
c9f78968 1072 wxUniChar operator[](int n) const
8f93a29f 1073 { return at(n); }
c1df0a3b 1074 wxUniChar operator[](long n) const
8f93a29f 1075 { return at(n); }
c9f78968 1076 wxUniChar operator[](size_t n) const
8f93a29f 1077 { return at(n); }
01398433 1078#ifndef wxSIZE_T_IS_UINT
c9f78968 1079 wxUniChar operator[](unsigned int n) const
8f93a29f 1080 { return at(n); }
01398433 1081#endif // size_t != unsigned int
01398433 1082
9d9ad673 1083 // operator versions of GetWriteableChar()
c9f78968 1084 wxUniCharRef operator[](int n)
8f93a29f 1085 { return at(n); }
c1df0a3b 1086 wxUniCharRef operator[](long n)
8f93a29f 1087 { return at(n); }
c9f78968 1088 wxUniCharRef operator[](size_t n)
8f93a29f 1089 { return at(n); }
d836ee96 1090#ifndef wxSIZE_T_IS_UINT
c9f78968 1091 wxUniCharRef operator[](unsigned int n)
8f93a29f 1092 { return at(n); }
d836ee96 1093#endif // size_t != unsigned int
c801d85f 1094
c9f78968
VS
1095 // explicit conversion to C string (use this with printf()!)
1096 wxCStrData c_str() const { return wxCStrData(this); }
8f93a29f 1097 wxCStrData data() const { return c_str(); }
c9f78968 1098
3c67202d 1099 // implicit conversion to C string
c9f78968 1100 operator wxCStrData() const { return c_str(); }
11aac4ba
VS
1101 operator const char*() const { return c_str(); }
1102 operator const wchar_t*() const { return c_str(); }
e015c2a3 1103
353a4edc
VZ
1104 // implicit conversion to untyped pointer for compatibility with previous
1105 // wxWidgets versions: this is the same as conversion to const char * so it
1106 // may fail!
1107 operator const void*() const { return c_str(); }
1108
e015c2a3 1109 // identical to c_str(), for MFC compatibility
c9f78968
VS
1110 const wxCStrData GetData() const { return c_str(); }
1111
1112 // explicit conversion to C string in internal representation (char*,
1113 // wchar_t*, UTF-8-encoded char*, depending on the build):
81727065 1114 const wxStringCharType *wx_str() const { return m_impl.c_str(); }
e90c1d2a 1115
ef0f1387 1116 // conversion to *non-const* multibyte or widestring buffer; modifying
c67b782d
VS
1117 // returned buffer won't affect the string, these methods are only useful
1118 // for passing values to const-incorrect functions
8060b0be 1119 wxWritableCharBuffer char_str(const wxMBConv& conv = wxConvLibc) const
c67b782d
VS
1120 { return mb_str(conv); }
1121 wxWritableWCharBuffer wchar_str() const { return wc_str(); }
ef0f1387 1122
e015c2a3
VZ
1123 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
1124 // converting numbers or strings which are certain not to contain special
1125 // chars (typically system functions, X atoms, environment variables etc.)
1126 //
1127 // the behaviour of these functions with the strings containing anything
1128 // else than 7 bit ASCII characters is undefined, use at your own risk.
b1ac3b56 1129#if wxUSE_UNICODE
2b5f62a0
VZ
1130 static wxString FromAscii(const char *ascii); // string
1131 static wxString FromAscii(const char ascii); // char
b1ac3b56 1132 const wxCharBuffer ToAscii() const;
e015c2a3
VZ
1133#else // ANSI
1134 static wxString FromAscii(const char *ascii) { return wxString( ascii ); }
2b5f62a0 1135 static wxString FromAscii(const char ascii) { return wxString( ascii ); }
e015c2a3
VZ
1136 const char *ToAscii() const { return c_str(); }
1137#endif // Unicode/!Unicode
b1ac3b56 1138
5f167b77
VS
1139 // conversion to/from UTF-8:
1140#if wxUSE_UNICODE_UTF8
1141 static wxString FromUTF8(const char *utf8)
1142 {
1143 wxASSERT( wxStringOperations::IsValidUtf8String(utf8) );
1144 return FromImpl(wxStringImpl(utf8));
1145 }
1146 static wxString FromUTF8(const char *utf8, size_t len)
1147 {
1148 wxASSERT( wxStringOperations::IsValidUtf8String(utf8, len) );
1149 return FromImpl(wxStringImpl(utf8, len));
1150 }
1151 const char* utf8_str() const { return wx_str(); }
1152 const char* ToUTF8() const { return wx_str(); }
1153#elif wxUSE_UNICODE_WCHAR
1154 static wxString FromUTF8(const char *utf8)
5487ff0f 1155 { return wxString(utf8, wxMBConvUTF8()); }
5f167b77 1156 static wxString FromUTF8(const char *utf8, size_t len)
5487ff0f
VS
1157 { return wxString(utf8, wxMBConvUTF8(), len); }
1158 const wxCharBuffer utf8_str() const { return mb_str(wxMBConvUTF8()); }
5f167b77
VS
1159 const wxCharBuffer ToUTF8() const { return utf8_str(); }
1160#else // ANSI
1161 static wxString FromUTF8(const char *utf8)
5487ff0f 1162 { return wxString(wxMBConvUTF8().cMB2WC(utf8)); }
5f167b77 1163 static wxString FromUTF8(const char *utf8, size_t len)
06c73b98
VS
1164 {
1165 size_t wlen;
5487ff0f 1166 wxWCharBuffer buf(wxMBConvUTF8().cMB2WC(utf8, len == npos ? wxNO_LEN : len, &wlen));
06c73b98
VS
1167 return wxString(buf.data(), wlen);
1168 }
5487ff0f
VS
1169 const wxCharBuffer utf8_str() const
1170 { return wxMBConvUTF8().cWC2MB(wc_str()); }
5f167b77
VS
1171 const wxCharBuffer ToUTF8() const { return utf8_str(); }
1172#endif
1173
cb2f2135
VS
1174 // functions for storing binary data in wxString:
1175#if wxUSE_UNICODE
1176 static wxString From8BitData(const char *data, size_t len)
1177 { return wxString(data, wxConvISO8859_1, len); }
1178 // version for NUL-terminated data:
1179 static wxString From8BitData(const char *data)
1180 { return wxString(data, wxConvISO8859_1); }
1181 const wxCharBuffer To8BitData() const { return mb_str(wxConvISO8859_1); }
1182#else // ANSI
1183 static wxString From8BitData(const char *data, size_t len)
1184 { return wxString(data, len); }
1185 // version for NUL-terminated data:
1186 static wxString From8BitData(const char *data)
1187 { return wxString(data); }
1188 const char *To8BitData() const { return c_str(); }
1189#endif // Unicode/ANSI
1190
2b5f62a0 1191 // conversions with (possible) format conversions: have to return a
e90c1d2a 1192 // buffer with temporary data
f6bcfd97
BP
1193 //
1194 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
1195 // return an ANSI (multibyte) string, wc_str() to return a wide string and
1196 // fn_str() to return a string which should be used with the OS APIs
1197 // accepting the file names. The return value is always the same, but the
1198 // type differs because a function may either return pointer to the buffer
1199 // directly or have to use intermediate buffer for translation.
2bb67b80 1200#if wxUSE_UNICODE
111d9948
VS
1201
1202#if wxUSE_UTF8_LOCALE_ONLY
1203 const char* mb_str() const { return wx_str(); }
1204 const wxCharBuffer mb_str(const wxMBConv& conv) const;
1205#else
830f8f11 1206 const wxCharBuffer mb_str(const wxMBConv& conv = wxConvLibc) const;
111d9948 1207#endif
f6bcfd97 1208
e90c1d2a
VZ
1209 const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); }
1210
81727065
VS
1211#if wxUSE_UNICODE_WCHAR
1212 const wxChar* wc_str() const { return wx_str(); }
1213#elif wxUSE_UNICODE_UTF8
1214 const wxWCharBuffer wc_str() const;
1215#endif
f6bcfd97 1216 // for compatibility with !wxUSE_UNICODE version
81727065
VS
1217 const wxWX2WCbuf wc_str(const wxMBConv& WXUNUSED(conv)) const
1218 { return wc_str(); }
e90c1d2a 1219
2bb67b80 1220#if wxMBFILES
5f709e67 1221 const wxCharBuffer fn_str() const { return mb_str(wxConvFile); }
e90c1d2a 1222#else // !wxMBFILES
81727065 1223 const wxWX2WCbuf fn_str() const { return wc_str(); }
e90c1d2a 1224#endif // wxMBFILES/!wxMBFILES
81727065 1225
e90c1d2a 1226#else // ANSI
81727065 1227 const wxChar* mb_str() const { return wx_str(); }
f6bcfd97
BP
1228
1229 // for compatibility with wxUSE_UNICODE version
81727065 1230 const wxChar* mb_str(const wxMBConv& WXUNUSED(conv)) const { return wx_str(); }
f6bcfd97 1231
e90c1d2a 1232 const wxWX2MBbuf mbc_str() const { return mb_str(); }
f6bcfd97 1233
6f841509 1234#if wxUSE_WCHAR_T
ef0f1387 1235 const wxWCharBuffer wc_str(const wxMBConv& conv = wxConvLibc) const;
e90c1d2a 1236#endif // wxUSE_WCHAR_T
d5c8817c
SC
1237#ifdef __WXOSX__
1238 const wxCharBuffer fn_str() const { return wxConvFile.cWC2WX( wc_str( wxConvLocal ) ); }
1239#else
e87b7833 1240 const wxChar* fn_str() const { return c_str(); }
d5c8817c 1241#endif
e90c1d2a 1242#endif // Unicode/ANSI
c801d85f 1243
3c67202d
VZ
1244 // overloaded assignment
1245 // from another wxString
04abe4bc
VS
1246 wxString& operator=(const wxString& stringSrc)
1247 { m_impl = stringSrc.m_impl; return *this; }
8f93a29f
VS
1248 wxString& operator=(const wxCStrData& cstr)
1249 { return *this = cstr.AsString(); }
3c67202d 1250 // from a character
c9f78968 1251 wxString& operator=(wxUniChar ch)
467175ab 1252 { m_impl = wxStringOperations::EncodeChar(ch); return *this; }
c9f78968 1253 wxString& operator=(wxUniCharRef ch)
8f93a29f 1254 { return operator=((wxUniChar)ch); }
c9f78968 1255 wxString& operator=(char ch)
8f93a29f 1256 { return operator=(wxUniChar(ch)); }
50e02008
VS
1257 wxString& operator=(unsigned char ch)
1258 { return operator=(wxUniChar(ch)); }
c9f78968 1259 wxString& operator=(wchar_t ch)
8f93a29f 1260 { return operator=(wxUniChar(ch)); }
c57e3bd5
RN
1261 // from a C string - STL probably will crash on NULL,
1262 // so we need to compensate in that case
992527a5 1263#if wxUSE_STL_BASED_WXSTRING
04abe4bc
VS
1264 wxString& operator=(const char *psz)
1265 { if (psz) m_impl = ImplStr(psz); else Clear(); return *this; }
1266 wxString& operator=(const wchar_t *pwz)
1267 { if (pwz) m_impl = ImplStr(pwz); else Clear(); return *this; }
c57e3bd5 1268#else
04abe4bc
VS
1269 wxString& operator=(const char *psz)
1270 { m_impl = ImplStr(psz); return *this; }
1271 wxString& operator=(const wchar_t *pwz)
1272 { m_impl = ImplStr(pwz); return *this; }
c57e3bd5 1273#endif
04abe4bc
VS
1274 wxString& operator=(const unsigned char *psz)
1275 { return operator=((const char*)psz); }
c57e3bd5 1276
111bb7f2 1277 // from wxWCharBuffer
8f93a29f 1278 wxString& operator=(const wxWCharBuffer& s)
04abe4bc 1279 { return operator=(s.data()); } // FIXME-UTF8: fix for embedded NULs
111bb7f2 1280 // from wxCharBuffer
04abe4bc
VS
1281 wxString& operator=(const wxCharBuffer& s)
1282 { return operator=(s.data()); } // FIXME-UTF8: fix for embedded NULs
3c67202d
VZ
1283
1284 // string concatenation
1285 // in place concatenation
1286 /*
1287 Concatenate and return the result. Note that the left to right
1288 associativity of << allows to write things like "str << str1 << str2
1289 << ..." (unlike with +=)
1290 */
1291 // string += string
dd1eaa89
VZ
1292 wxString& operator<<(const wxString& s)
1293 {
8f93a29f
VS
1294#if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1295 wxASSERT_MSG( s.IsValid(),
09443a26 1296 _T("did you forget to call UngetWriteBuf()?") );
e87b7833 1297#endif
dd1eaa89 1298
e87b7833 1299 append(s);
dd1eaa89
VZ
1300 return *this;
1301 }
3c67202d 1302 // string += C string
8f93a29f 1303 wxString& operator<<(const char *psz)
c9f78968 1304 { append(psz); return *this; }
8f93a29f
VS
1305 wxString& operator<<(const wchar_t *pwz)
1306 { append(pwz); return *this; }
c9f78968 1307 wxString& operator<<(const wxCStrData& psz)
8f93a29f 1308 { append(psz.AsString()); return *this; }
3c67202d 1309 // string += char
c9f78968
VS
1310 wxString& operator<<(wxUniChar ch) { append(1, ch); return *this; }
1311 wxString& operator<<(wxUniCharRef ch) { append(1, ch); return *this; }
1312 wxString& operator<<(char ch) { append(1, ch); return *this; }
50e02008 1313 wxString& operator<<(unsigned char ch) { append(1, ch); return *this; }
c9f78968 1314 wxString& operator<<(wchar_t ch) { append(1, ch); return *this; }
2bb67b80
OK
1315
1316 // string += buffer (i.e. from wxGetString)
09443a26 1317 wxString& operator<<(const wxWCharBuffer& s)
d7330233 1318 { return operator<<((const wchar_t *)s); }
09443a26 1319 wxString& operator<<(const wxCharBuffer& s)
d7330233 1320 { return operator<<((const char *)s); }
d7330233 1321
3c67202d 1322 // string += C string
09443a26
VZ
1323 wxString& Append(const wxString& s)
1324 {
5a8231ef
WS
1325 // test for empty() to share the string if possible
1326 if ( empty() )
09443a26
VZ
1327 *this = s;
1328 else
e87b7833 1329 append(s);
09443a26
VZ
1330 return *this;
1331 }
8f93a29f 1332 wxString& Append(const char* psz)
e87b7833 1333 { append(psz); return *this; }
8f93a29f
VS
1334 wxString& Append(const wchar_t* pwz)
1335 { append(pwz); return *this; }
d3cefe87
VS
1336 wxString& Append(const wxCStrData& psz)
1337 { append(psz); return *this; }
1338 wxString& Append(const wxCharBuffer& psz)
1339 { append(psz); return *this; }
1340 wxString& Append(const wxWCharBuffer& psz)
1341 { append(psz); return *this; }
3c67202d 1342 // append count copies of given character
c9f78968
VS
1343 wxString& Append(wxUniChar ch, size_t count = 1u)
1344 { append(count, ch); return *this; }
1345 wxString& Append(wxUniCharRef ch, size_t count = 1u)
1346 { append(count, ch); return *this; }
1347 wxString& Append(char ch, size_t count = 1u)
1348 { append(count, ch); return *this; }
50e02008
VS
1349 wxString& Append(unsigned char ch, size_t count = 1u)
1350 { append(count, ch); return *this; }
c9f78968 1351 wxString& Append(wchar_t ch, size_t count = 1u)
e87b7833 1352 { append(count, ch); return *this; }
8f93a29f 1353 wxString& Append(const char* psz, size_t nLen)
e87b7833 1354 { append(psz, nLen); return *this; }
8f93a29f
VS
1355 wxString& Append(const wchar_t* pwz, size_t nLen)
1356 { append(pwz, nLen); return *this; }
3c67202d
VZ
1357
1358 // prepend a string, return the string itself
1359 wxString& Prepend(const wxString& str)
1360 { *this = str + *this; return *this; }
1361
1362 // non-destructive concatenation
1fc8cfbd
VZ
1363 // two strings
1364 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string1,
1365 const wxString& string2);
1366 // string with a single char
c9f78968 1367 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch);
1fc8cfbd 1368 // char with a string
c9f78968 1369 friend wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string);
1fc8cfbd
VZ
1370 // string with C string
1371 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string,
8f93a29f
VS
1372 const char *psz);
1373 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string,
1374 const wchar_t *pwz);
1fc8cfbd 1375 // C string with string
8f93a29f
VS
1376 friend wxString WXDLLIMPEXP_BASE operator+(const char *psz,
1377 const wxString& string);
1378 friend wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz,
1fc8cfbd 1379 const wxString& string);
3c67202d
VZ
1380
1381 // stream-like functions
1382 // insert an int into string
3ce65f6c
VZ
1383 wxString& operator<<(int i)
1384 { return (*this) << Format(_T("%d"), i); }
1385 // insert an unsigned int into string
1386 wxString& operator<<(unsigned int ui)
1387 { return (*this) << Format(_T("%u"), ui); }
1388 // insert a long into string
1389 wxString& operator<<(long l)
1390 { return (*this) << Format(_T("%ld"), l); }
1391 // insert an unsigned long into string
1392 wxString& operator<<(unsigned long ul)
1393 { return (*this) << Format(_T("%lu"), ul); }
3fc1adbd
MW
1394#if defined wxLongLong_t && !defined wxLongLongIsLong
1395 // insert a long long if they exist and aren't longs
1396 wxString& operator<<(wxLongLong_t ll)
b7859132
MW
1397 {
1398 const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("d");
1399 return (*this) << Format(fmt, ll);
1400 }
3fc1adbd
MW
1401 // insert an unsigned long long
1402 wxString& operator<<(wxULongLong_t ull)
b7859132
MW
1403 {
1404 const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("u");
1405 return (*this) << Format(fmt , ull);
1406 }
3fc1adbd 1407#endif
3c67202d 1408 // insert a float into string
3ce65f6c
VZ
1409 wxString& operator<<(float f)
1410 { return (*this) << Format(_T("%f"), f); }
3c67202d 1411 // insert a double into string
3ce65f6c
VZ
1412 wxString& operator<<(double d)
1413 { return (*this) << Format(_T("%g"), d); }
c84c52de 1414
3c67202d 1415 // string comparison
30b21f9a 1416 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
8f93a29f
VS
1417 int Cmp(const char *psz) const
1418 { return compare(psz); }
1419 int Cmp(const wchar_t *pwz) const
1420 { return compare(pwz); }
1421 int Cmp(const wxString& s) const
1422 { return compare(s); }
d3cefe87
VS
1423 int Cmp(const wxCStrData& s) const
1424 { return compare(s); }
1425 int Cmp(const wxCharBuffer& s) const
1426 { return compare(s); }
1427 int Cmp(const wxWCharBuffer& s) const
1428 { return compare(s); }
3c67202d 1429 // same as Cmp() but not case-sensitive
dcb68102 1430 int CmpNoCase(const wxString& s) const;
3c67202d
VZ
1431 // test for the string equality, either considering case or not
1432 // (if compareWithCase then the case matters)
11aac4ba
VS
1433 bool IsSameAs(const wxString& str, bool compareWithCase = true) const
1434 { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; }
1435 bool IsSameAs(const char *str, bool compareWithCase = true) const
1436 { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; }
1437 bool IsSameAs(const wchar_t *str, bool compareWithCase = true) const
1438 { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; }
d3cefe87
VS
1439 bool IsSameAs(const wxCStrData& str, bool compareWithCase = true) const
1440 { return IsSameAs(str.AsString(), compareWithCase); }
1441 bool IsSameAs(const wxCharBuffer& str, bool compareWithCase = true) const
1442 { return IsSameAs(str.data(), compareWithCase); }
1443 bool IsSameAs(const wxWCharBuffer& str, bool compareWithCase = true) const
1444 { return IsSameAs(str.data(), compareWithCase); }
20aa779e 1445 // comparison with a single character: returns true if equal
c9f78968 1446 bool IsSameAs(wxUniChar c, bool compareWithCase = true) const
f33fee2a 1447 {
e87b7833 1448 return (length() == 1) && (compareWithCase ? GetChar(0u) == c
f33fee2a
VZ
1449 : wxToupper(GetChar(0u)) == wxToupper(c));
1450 }
11aac4ba
VS
1451 // FIXME-UTF8: remove these overloads
1452 bool IsSameAs(wxUniCharRef c, bool compareWithCase = true) const
1453 { return IsSameAs(wxUniChar(c), compareWithCase); }
1454 bool IsSameAs(char c, bool compareWithCase = true) const
1455 { return IsSameAs(wxUniChar(c), compareWithCase); }
1456 bool IsSameAs(unsigned char c, bool compareWithCase = true) const
1457 { return IsSameAs(wxUniChar(c), compareWithCase); }
1458 bool IsSameAs(wchar_t c, bool compareWithCase = true) const
1459 { return IsSameAs(wxUniChar(c), compareWithCase); }
1460 bool IsSameAs(int c, bool compareWithCase = true) const
1461 { return IsSameAs(wxUniChar(c), compareWithCase); }
3c67202d
VZ
1462
1463 // simple sub-string extraction
1464 // return substring starting at nFirst of length nCount (or till the end
1465 // if nCount = default value)
e87b7833 1466 wxString Mid(size_t nFirst, size_t nCount = npos) const;
3c67202d 1467
f6bcfd97 1468 // operator version of Mid()
3c67202d
VZ
1469 wxString operator()(size_t start, size_t len) const
1470 { return Mid(start, len); }
1471
3affcd07
VZ
1472 // check if the string starts with the given prefix and return the rest
1473 // of the string in the provided pointer if it is not NULL; otherwise
1474 // return false
c5e7a7d7 1475 bool StartsWith(const wxString& prefix, wxString *rest = NULL) const;
3affcd07
VZ
1476 // check if the string ends with the given suffix and return the
1477 // beginning of the string before the suffix in the provided pointer if
1478 // it is not NULL; otherwise return false
c5e7a7d7 1479 bool EndsWith(const wxString& suffix, wxString *rest = NULL) const;
f6bcfd97 1480
3c67202d 1481 // get first nCount characters
c801d85f 1482 wxString Left(size_t nCount) const;
3c67202d 1483 // get last nCount characters
c801d85f 1484 wxString Right(size_t nCount) const;
c0881dc3 1485 // get all characters before the first occurance of ch
3c67202d 1486 // (returns the whole string if ch not found)
c9f78968 1487 wxString BeforeFirst(wxUniChar ch) const;
3c67202d
VZ
1488 // get all characters before the last occurence of ch
1489 // (returns empty string if ch not found)
c9f78968 1490 wxString BeforeLast(wxUniChar ch) const;
3c67202d
VZ
1491 // get all characters after the first occurence of ch
1492 // (returns empty string if ch not found)
c9f78968 1493 wxString AfterFirst(wxUniChar ch) const;
3c67202d
VZ
1494 // get all characters after the last occurence of ch
1495 // (returns the whole string if ch not found)
c9f78968 1496 wxString AfterLast(wxUniChar ch) const;
3c67202d
VZ
1497
1498 // for compatibility only, use more explicitly named functions above
c9f78968
VS
1499 wxString Before(wxUniChar ch) const { return BeforeLast(ch); }
1500 wxString After(wxUniChar ch) const { return AfterFirst(ch); }
3c67202d
VZ
1501
1502 // case conversion
c84c52de 1503 // convert to upper case in place, return the string itself
c801d85f 1504 wxString& MakeUpper();
c84c52de 1505 // convert to upper case, return the copy of the string
03ab016d
JS
1506 // Here's something to remember: BC++ doesn't like returns in inlines.
1507 wxString Upper() const ;
c84c52de 1508 // convert to lower case in place, return the string itself
c801d85f 1509 wxString& MakeLower();
c84c52de 1510 // convert to lower case, return the copy of the string
03ab016d 1511 wxString Lower() const ;
c801d85f 1512
3c67202d
VZ
1513 // trimming/padding whitespace (either side) and truncating
1514 // remove spaces from left or from right (default) side
d775fa82 1515 wxString& Trim(bool bFromRight = true);
3c67202d 1516 // add nCount copies chPad in the beginning or at the end (default)
c9f78968 1517 wxString& Pad(size_t nCount, wxUniChar chPad = wxT(' '), bool bFromRight = true);
dd1eaa89 1518
3c67202d
VZ
1519 // searching and replacing
1520 // searching (return starting index, or -1 if not found)
c9f78968 1521 int Find(wxUniChar ch, bool bFromEnd = false) const; // like strchr/strrchr
8a540c88
VS
1522 int Find(wxUniCharRef ch, bool bFromEnd = false) const
1523 { return Find(wxUniChar(ch), bFromEnd); }
1524 int Find(char ch, bool bFromEnd = false) const
1525 { return Find(wxUniChar(ch), bFromEnd); }
1526 int Find(unsigned char ch, bool bFromEnd = false) const
1527 { return Find(wxUniChar(ch), bFromEnd); }
1528 int Find(wchar_t ch, bool bFromEnd = false) const
1529 { return Find(wxUniChar(ch), bFromEnd); }
3c67202d 1530 // searching (return starting index, or -1 if not found)
8a540c88
VS
1531 int Find(const wxString& sub) const // like strstr
1532 {
1533 size_type idx = find(sub);
1534 return (idx == npos) ? wxNOT_FOUND : (int)idx;
1535 }
1536 int Find(const char *sub) const // like strstr
1537 {
1538 size_type idx = find(sub);
1539 return (idx == npos) ? wxNOT_FOUND : (int)idx;
1540 }
1541 int Find(const wchar_t *sub) const // like strstr
1542 {
1543 size_type idx = find(sub);
1544 return (idx == npos) ? wxNOT_FOUND : (int)idx;
1545 }
1546
d3cefe87
VS
1547 int Find(const wxCStrData& sub) const
1548 { return Find(sub.AsString()); }
1549 int Find(const wxCharBuffer& sub) const
1550 { return Find(sub.data()); }
1551 int Find(const wxWCharBuffer& sub) const
1552 { return Find(sub.data()); }
1553
3c67202d
VZ
1554 // replace first (or all of bReplaceAll) occurences of substring with
1555 // another string, returns the number of replacements made
8a540c88
VS
1556 size_t Replace(const wxString& strOld,
1557 const wxString& strNew,
d775fa82 1558 bool bReplaceAll = true);
3c67202d
VZ
1559
1560 // check if the string contents matches a mask containing '*' and '?'
8a540c88 1561 bool Matches(const wxString& mask) const;
c801d85f 1562
d775fa82 1563 // conversion to numbers: all functions return true only if the whole
538f35cc
VZ
1564 // string is a number and put the value of this number into the pointer
1565 // provided, the base is the numeric base in which the conversion should be
1566 // done and must be comprised between 2 and 36 or be 0 in which case the
1567 // standard C rules apply (leading '0' => octal, "0x" => hex)
cd0b1709 1568 // convert to a signed integer
538f35cc 1569 bool ToLong(long *val, int base = 10) const;
cd0b1709 1570 // convert to an unsigned integer
538f35cc 1571 bool ToULong(unsigned long *val, int base = 10) const;
d6718dd1
VZ
1572 // convert to wxLongLong
1573#if defined(wxLongLong_t)
1574 bool ToLongLong(wxLongLong_t *val, int base = 10) const;
1575 // convert to wxULongLong
1576 bool ToULongLong(wxULongLong_t *val, int base = 10) const;
1577#endif // wxLongLong_t
cd0b1709
VZ
1578 // convert to a double
1579 bool ToDouble(double *val) const;
1580
d6718dd1 1581
c9f78968 1582#ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
90e572f1 1583 // formatted input/output
3c67202d 1584 // as sprintf(), returns the number of characters written or < 0 on error
7357f981 1585 // (take 'this' into account in attribute parameter count)
2523e9b7 1586 // int Printf(const wxString& format, ...);
1528e0b8 1587 WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const wxFormatString&),
d1f6e2cf 1588 DoPrintfWchar, DoPrintfUtf8)
2523e9b7 1589#ifdef __WATCOMC__
59a14f69
VS
1590 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1591 WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wxString&),
1592 (wxFormatString(f1)));
1593 WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wxCStrData&),
1594 (wxFormatString(f1)));
1595 WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const char*),
1596 (wxFormatString(f1)));
1597 WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wchar_t*),
1598 (wxFormatString(f1)));
2523e9b7 1599#endif
c9f78968 1600#endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN
3c67202d 1601 // as vprintf(), returns the number of characters written or < 0 on error
c9f78968 1602 int PrintfV(const wxString& format, va_list argptr);
dd1eaa89 1603
c9f78968 1604#ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
341e7d28 1605 // returns the string containing the result of Printf() to it
2523e9b7 1606 // static wxString Format(const wxString& format, ...) ATTRIBUTE_PRINTF_1;
1528e0b8 1607 WX_DEFINE_VARARG_FUNC(static wxString, Format, 1, (const wxFormatString&),
d1f6e2cf 1608 DoFormatWchar, DoFormatUtf8)
2523e9b7
VS
1609#ifdef __WATCOMC__
1610 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
59a14f69
VS
1611 WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wxString&),
1612 (wxFormatString(f1)));
1613 WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wxCStrData&),
1614 (wxFormatString(f1)));
1615 WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const char*),
1616 (wxFormatString(f1)));
1617 WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wchar_t*),
1618 (wxFormatString(f1)));
2523e9b7 1619#endif
c9f78968 1620#endif
341e7d28 1621 // the same as above, but takes a va_list
c9f78968 1622 static wxString FormatV(const wxString& format, va_list argptr);
341e7d28 1623
3c67202d
VZ
1624 // raw access to string memory
1625 // ensure that string has space for at least nLen characters
dd1eaa89 1626 // only works if the data of this string is not shared
e87b7833 1627 bool Alloc(size_t nLen) { reserve(nLen); /*return capacity() >= nLen;*/ return true; }
3c67202d 1628 // minimize the string's memory
dd1eaa89 1629 // only works if the data of this string is not shared
b1801e0e 1630 bool Shrink();
8f93a29f 1631#if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
d8a4b666
VS
1632 // These are deprecated, use wxStringBuffer or wxStringBufferLength instead
1633 //
3c67202d
VZ
1634 // get writable buffer of at least nLen bytes. Unget() *must* be called
1635 // a.s.a.p. to put string back in a reasonable state!
c87a0bc8 1636 wxDEPRECATED( wxStringCharType *GetWriteBuf(size_t nLen) );
3c67202d 1637 // call this immediately after GetWriteBuf() has been used
d8a4b666
VS
1638 wxDEPRECATED( void UngetWriteBuf() );
1639 wxDEPRECATED( void UngetWriteBuf(size_t nLen) );
8f93a29f 1640#endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && wxUSE_UNICODE_UTF8
c801d85f 1641
77ffb593 1642 // wxWidgets version 1 compatibility functions
3c67202d
VZ
1643
1644 // use Mid()
1645 wxString SubString(size_t from, size_t to) const
1646 { return Mid(from, (to - from + 1)); }
1647 // values for second parameter of CompareTo function
c801d85f 1648 enum caseCompare {exact, ignoreCase};
3c67202d 1649 // values for first parameter of Strip function
c801d85f 1650 enum stripType {leading = 0x1, trailing = 0x2, both = 0x3};
8870c26e 1651
c9f78968 1652#ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
7357f981
GD
1653 // use Printf()
1654 // (take 'this' into account in attribute parameter count)
2523e9b7 1655 // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2;
1528e0b8 1656 WX_DEFINE_VARARG_FUNC(int, sprintf, 1, (const wxFormatString&),
d1f6e2cf 1657 DoPrintfWchar, DoPrintfUtf8)
2523e9b7
VS
1658#ifdef __WATCOMC__
1659 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
59a14f69
VS
1660 WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wxString&),
1661 (wxFormatString(f1)));
1662 WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wxCStrData&),
1663 (wxFormatString(f1)));
1664 WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const char*),
1665 (wxFormatString(f1)));
1666 WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wchar_t*),
1667 (wxFormatString(f1)));
2523e9b7 1668#endif
c9f78968 1669#endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
c801d85f 1670
3c67202d 1671 // use Cmp()
2bb67b80 1672 inline int CompareTo(const wxChar* psz, caseCompare cmp = exact) const
6b95b20d 1673 { return cmp == exact ? Cmp(psz) : CmpNoCase(psz); }
c801d85f 1674
8f93a29f 1675 // use length()
e87b7833 1676 size_t Length() const { return length(); }
3c67202d 1677 // Count the number of characters
c9f78968 1678 int Freq(wxUniChar ch) const;
3c67202d 1679 // use MakeLower
c801d85f 1680 void LowerCase() { MakeLower(); }
3c67202d 1681 // use MakeUpper
c801d85f 1682 void UpperCase() { MakeUpper(); }
3c67202d 1683 // use Trim except that it doesn't change this string
c801d85f
KB
1684 wxString Strip(stripType w = trailing) const;
1685
3c67202d 1686 // use Find (more general variants not yet supported)
2bb67b80 1687 size_t Index(const wxChar* psz) const { return Find(psz); }
c9f78968 1688 size_t Index(wxUniChar ch) const { return Find(ch); }
3c67202d 1689 // use Truncate
c801d85f 1690 wxString& Remove(size_t pos) { return Truncate(pos); }
e87b7833 1691 wxString& RemoveLast(size_t n = 1) { return Truncate(length() - n); }
c801d85f 1692
e87b7833
MB
1693 wxString& Remove(size_t nStart, size_t nLen)
1694 { return (wxString&)erase( nStart, nLen ); }
dd1eaa89 1695
3c67202d 1696 // use Find()
8f93a29f 1697 int First( wxUniChar ch ) const { return Find(ch); }
8a540c88 1698 int First( wxUniCharRef ch ) const { return Find(ch); }
c9f78968 1699 int First( char ch ) const { return Find(ch); }
50e02008 1700 int First( unsigned char ch ) const { return Find(ch); }
c9f78968 1701 int First( wchar_t ch ) const { return Find(ch); }
8a540c88 1702 int First( const wxString& str ) const { return Find(str); }
8f93a29f 1703 int Last( wxUniChar ch ) const { return Find(ch, true); }
d775fa82 1704 bool Contains(const wxString& str) const { return Find(str) != wxNOT_FOUND; }
c801d85f 1705
5a8231ef
WS
1706 // use empty()
1707 bool IsNull() const { return empty(); }
c801d85f 1708
3c67202d 1709 // std::string compatibility functions
dd1eaa89 1710
3c67202d
VZ
1711 // take nLen chars starting at nPos
1712 wxString(const wxString& str, size_t nPos, size_t nLen)
1545d942 1713 { assign(str, nPos, nLen); }
f2a1b1bd 1714 // take all characters from first to last
e87b7833 1715 wxString(const_iterator first, const_iterator last)
cf9a878b 1716 : m_impl(first.impl(), last.impl()) { }
67cff9dc
VZ
1717#if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1718 // the 2 overloads below are for compatibility with the existing code using
1719 // pointers instead of iterators
f2a1b1bd
VZ
1720 wxString(const char *first, const char *last)
1721 {
1722 SubstrBufFromMB str(ImplStr(first, last - first));
1723 m_impl.assign(str.data, str.len);
1724 }
1725 wxString(const wchar_t *first, const wchar_t *last)
1726 {
1727 SubstrBufFromWC str(ImplStr(first, last - first));
1728 m_impl.assign(str.data, str.len);
1729 }
67cff9dc
VZ
1730 // and this one is needed to compile code adding offsets to c_str() result
1731 wxString(const wxCStrData& first, const wxCStrData& last)
cf9a878b
VS
1732 : m_impl(CreateConstIterator(first).impl(),
1733 CreateConstIterator(last).impl())
67cff9dc
VZ
1734 {
1735 wxASSERT_MSG( first.m_str == last.m_str,
1736 _T("pointers must be into the same string") );
1737 }
1738#endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
5460b9e3 1739
3c67202d 1740 // lib.string.modifiers
3c67202d
VZ
1741 // append elements str[pos], ..., str[pos+n]
1742 wxString& append(const wxString& str, size_t pos, size_t n)
8f93a29f
VS
1743 {
1744 size_t from, len;
1745 str.PosLenToImpl(pos, n, &from, &len);
1746 m_impl.append(str.m_impl, from, len);
1747 return *this;
1748 }
e87b7833
MB
1749 // append a string
1750 wxString& append(const wxString& str)
8f93a29f 1751 { m_impl.append(str.m_impl); return *this; }
3c67202d 1752 // append first n (or all if n == npos) characters of sz
8f93a29f
VS
1753 wxString& append(const char *sz)
1754 { m_impl.append(ImplStr(sz)); return *this; }
1755 wxString& append(const wchar_t *sz)
1756 { m_impl.append(ImplStr(sz)); return *this; }
1757 wxString& append(const char *sz, size_t n)
1758 {
1759 SubstrBufFromMB str(ImplStr(sz, n));
1760 m_impl.append(str.data, str.len);
1761 return *this;
1762 }
1763 wxString& append(const wchar_t *sz, size_t n)
1764 {
1765 SubstrBufFromWC str(ImplStr(sz, n));
1766 m_impl.append(str.data, str.len);
1767 return *this;
1768 }
d3cefe87
VS
1769
1770 wxString& append(const wxCStrData& str)
1771 { return append(str.AsString()); }
1772 wxString& append(const wxCharBuffer& str)
1773 { return append(str.data()); }
1774 wxString& append(const wxWCharBuffer& str)
1775 { return append(str.data()); }
1776
3c67202d 1777 // append n copies of ch
c9f78968 1778 wxString& append(size_t n, wxUniChar ch)
8f93a29f
VS
1779 {
1780#if wxUSE_UNICODE_UTF8
1781 if ( !ch.IsAscii() )
467175ab 1782 m_impl.append(wxStringOperations::EncodeNChars(n, ch));
8f93a29f
VS
1783 else
1784#endif
1785 m_impl.append(n, (wxStringCharType)ch);
1786 return *this;
1787 }
e87b7833
MB
1788 // append from first to last
1789 wxString& append(const_iterator first, const_iterator last)
cf9a878b 1790 { m_impl.append(first.impl(), last.impl()); return *this; }
67cff9dc 1791#if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
f2a1b1bd
VZ
1792 wxString& append(const char *first, const char *last)
1793 { return append(first, last - first); }
1794 wxString& append(const wchar_t *first, const wchar_t *last)
1795 { return append(first, last - first); }
67cff9dc
VZ
1796 wxString& append(const wxCStrData& first, const wxCStrData& last)
1797 { return append(CreateConstIterator(first), CreateConstIterator(last)); }
1798#endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
3c67202d
VZ
1799
1800 // same as `this_string = str'
735d1db6 1801 wxString& assign(const wxString& str)
8f93a29f 1802 { m_impl = str.m_impl; return *this; }
11aac4ba
VS
1803 wxString& assign(const wxString& str, size_t len)
1804 {
1805 m_impl.assign(str.m_impl, 0, str.LenToImpl(len));
1806 return *this;
1807 }
3c67202d
VZ
1808 // same as ` = str[pos..pos + n]
1809 wxString& assign(const wxString& str, size_t pos, size_t n)
8f93a29f
VS
1810 {
1811 size_t from, len;
1812 str.PosLenToImpl(pos, n, &from, &len);
1813 m_impl.assign(str.m_impl, from, len);
1814 return *this;
1815 }
3c67202d 1816 // same as `= first n (or all if n == npos) characters of sz'
8f93a29f
VS
1817 wxString& assign(const char *sz)
1818 { m_impl.assign(ImplStr(sz)); return *this; }
1819 wxString& assign(const wchar_t *sz)
1820 { m_impl.assign(ImplStr(sz)); return *this; }
1821 wxString& assign(const char *sz, size_t n)
1822 {
1823 SubstrBufFromMB str(ImplStr(sz, n));
1824 m_impl.assign(str.data, str.len);
1825 return *this;
1826 }
1827 wxString& assign(const wchar_t *sz, size_t n)
1828 {
1829 SubstrBufFromWC str(ImplStr(sz, n));
1830 m_impl.assign(str.data, str.len);
1831 return *this;
1832 }
d3cefe87
VS
1833
1834 wxString& assign(const wxCStrData& str)
1835 { return assign(str.AsString()); }
1836 wxString& assign(const wxCharBuffer& str)
1837 { return assign(str.data()); }
1838 wxString& assign(const wxWCharBuffer& str)
1839 { return assign(str.data()); }
1840 wxString& assign(const wxCStrData& str, size_t len)
1841 { return assign(str.AsString(), len); }
1842 wxString& assign(const wxCharBuffer& str, size_t len)
1843 { return assign(str.data(), len); }
1844 wxString& assign(const wxWCharBuffer& str, size_t len)
1845 { return assign(str.data(), len); }
1846
3c67202d 1847 // same as `= n copies of ch'
c9f78968 1848 wxString& assign(size_t n, wxUniChar ch)
8f93a29f
VS
1849 {
1850#if wxUSE_UNICODE_UTF8
1851 if ( !ch.IsAscii() )
467175ab 1852 m_impl.assign(wxStringOperations::EncodeNChars(n, ch));
8f93a29f
VS
1853 else
1854#endif
1855 m_impl.assign(n, (wxStringCharType)ch);
1856 return *this;
1857 }
11aac4ba
VS
1858
1859 wxString& assign(size_t n, wxUniCharRef ch)
1860 { return assign(n, wxUniChar(ch)); }
1861 wxString& assign(size_t n, char ch)
1862 { return assign(n, wxUniChar(ch)); }
1863 wxString& assign(size_t n, unsigned char ch)
1864 { return assign(n, wxUniChar(ch)); }
1865 wxString& assign(size_t n, wchar_t ch)
1866 { return assign(n, wxUniChar(ch)); }
1867
e87b7833
MB
1868 // assign from first to last
1869 wxString& assign(const_iterator first, const_iterator last)
cf9a878b 1870 { m_impl.assign(first.impl(), last.impl()); return *this; }
67cff9dc 1871#if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
f2a1b1bd
VZ
1872 wxString& assign(const char *first, const char *last)
1873 { return assign(first, last - first); }
1874 wxString& assign(const wchar_t *first, const wchar_t *last)
1875 { return assign(first, last - first); }
67cff9dc
VZ
1876 wxString& assign(const wxCStrData& first, const wxCStrData& last)
1877 { return assign(CreateConstIterator(first), CreateConstIterator(last)); }
1878#endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
e87b7833
MB
1879
1880 // string comparison
8f93a29f 1881 int compare(const wxString& str) const;
d3cefe87
VS
1882 int compare(const char* sz) const;
1883 int compare(const wchar_t* sz) const;
1884 int compare(const wxCStrData& str) const
1885 { return compare(str.AsString()); }
1886 int compare(const wxCharBuffer& str) const
1887 { return compare(str.data()); }
1888 int compare(const wxWCharBuffer& str) const
1889 { return compare(str.data()); }
e87b7833 1890 // comparison with a substring
8f93a29f 1891 int compare(size_t nStart, size_t nLen, const wxString& str) const;
e87b7833
MB
1892 // comparison of 2 substrings
1893 int compare(size_t nStart, size_t nLen,
8f93a29f 1894 const wxString& str, size_t nStart2, size_t nLen2) const;
e87b7833
MB
1895 // substring comparison with first nCount characters of sz
1896 int compare(size_t nStart, size_t nLen,
8f93a29f
VS
1897 const char* sz, size_t nCount = npos) const;
1898 int compare(size_t nStart, size_t nLen,
1899 const wchar_t* sz, size_t nCount = npos) const;
3c67202d
VZ
1900
1901 // insert another string
e87b7833 1902 wxString& insert(size_t nPos, const wxString& str)
8f93a29f 1903 { insert(begin() + nPos, str.begin(), str.end()); return *this; }
3c67202d
VZ
1904 // insert n chars of str starting at nStart (in str)
1905 wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n)
8f93a29f
VS
1906 {
1907 size_t from, len;
1908 str.PosLenToImpl(nStart, n, &from, &len);
1909 m_impl.insert(PosToImpl(nPos), str.m_impl, from, len);
1910 return *this;
1911 }
3c67202d 1912 // insert first n (or all if n == npos) characters of sz
8f93a29f
VS
1913 wxString& insert(size_t nPos, const char *sz)
1914 { m_impl.insert(PosToImpl(nPos), ImplStr(sz)); return *this; }
1915 wxString& insert(size_t nPos, const wchar_t *sz)
1916 { m_impl.insert(PosToImpl(nPos), ImplStr(sz)); return *this; }
1917 wxString& insert(size_t nPos, const char *sz, size_t n)
1918 {
1919 SubstrBufFromMB str(ImplStr(sz, n));
1920 m_impl.insert(PosToImpl(nPos), str.data, str.len);
1921 return *this;
1922 }
1923 wxString& insert(size_t nPos, const wchar_t *sz, size_t n)
1924 {
1925 SubstrBufFromWC str(ImplStr(sz, n));
1926 m_impl.insert(PosToImpl(nPos), str.data, str.len);
1927 return *this;
1928 }
3c67202d 1929 // insert n copies of ch
c9f78968 1930 wxString& insert(size_t nPos, size_t n, wxUniChar ch)
8f93a29f
VS
1931 {
1932#if wxUSE_UNICODE_UTF8
1933 if ( !ch.IsAscii() )
467175ab 1934 m_impl.insert(PosToImpl(nPos), wxStringOperations::EncodeNChars(n, ch));
8f93a29f
VS
1935 else
1936#endif
81727065 1937 m_impl.insert(PosToImpl(nPos), n, (wxStringCharType)ch);
8f93a29f
VS
1938 return *this;
1939 }
c9f78968 1940 iterator insert(iterator it, wxUniChar ch)
81727065
VS
1941 {
1942#if wxUSE_UNICODE_UTF8
1943 if ( !ch.IsAscii() )
1944 {
1945 size_t pos = IterToImplPos(it);
467175ab 1946 m_impl.insert(pos, wxStringOperations::EncodeChar(ch));
81727065
VS
1947 return iterator(this, m_impl.begin() + pos);
1948 }
1949 else
1950#endif
cf9a878b 1951 return iterator(this, m_impl.insert(it.impl(), (wxStringCharType)ch));
81727065 1952 }
e87b7833 1953 void insert(iterator it, const_iterator first, const_iterator last)
cf9a878b 1954 { m_impl.insert(it.impl(), first.impl(), last.impl()); }
67cff9dc 1955#if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
f2a1b1bd
VZ
1956 void insert(iterator it, const char *first, const char *last)
1957 { insert(it - begin(), first, last - first); }
67cff9dc 1958 void insert(iterator it, const wchar_t *first, const wchar_t *last)
f2a1b1bd 1959 { insert(it - begin(), first, last - first); }
67cff9dc
VZ
1960 void insert(iterator it, const wxCStrData& first, const wxCStrData& last)
1961 { insert(it, CreateConstIterator(first), CreateConstIterator(last)); }
1962#endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1963
c9f78968 1964 void insert(iterator it, size_type n, wxUniChar ch)
8f93a29f
VS
1965 {
1966#if wxUSE_UNICODE_UTF8
1967 if ( !ch.IsAscii() )
467175ab 1968 m_impl.insert(IterToImplPos(it), wxStringOperations::EncodeNChars(n, ch));
8f93a29f
VS
1969 else
1970#endif
cf9a878b 1971 m_impl.insert(it.impl(), n, (wxStringCharType)ch);
8f93a29f 1972 }
3c67202d
VZ
1973
1974 // delete characters from nStart to nStart + nLen
e87b7833 1975 wxString& erase(size_type pos = 0, size_type n = npos)
8f93a29f
VS
1976 {
1977 size_t from, len;
1978 PosLenToImpl(pos, n, &from, &len);
1979 m_impl.erase(from, len);
1980 return *this;
1981 }
f2a1b1bd 1982 // delete characters from first up to last
e87b7833 1983 iterator erase(iterator first, iterator last)
cf9a878b 1984 { return iterator(this, m_impl.erase(first.impl(), last.impl())); }
e87b7833 1985 iterator erase(iterator first)
cf9a878b 1986 { return iterator(this, m_impl.erase(first.impl())); }
e87b7833
MB
1987
1988#ifdef wxSTRING_BASE_HASNT_CLEAR
1989 void clear() { erase(); }
8f93a29f
VS
1990#else
1991 void clear() { m_impl.clear(); }
e87b7833 1992#endif
3c67202d
VZ
1993
1994 // replaces the substring of length nLen starting at nStart
8f93a29f
VS
1995 wxString& replace(size_t nStart, size_t nLen, const char* sz)
1996 {
1997 size_t from, len;
1998 PosLenToImpl(nStart, nLen, &from, &len);
1999 m_impl.replace(from, len, ImplStr(sz));
2000 return *this;
2001 }
2002 wxString& replace(size_t nStart, size_t nLen, const wchar_t* sz)
2003 {
2004 size_t from, len;
2005 PosLenToImpl(nStart, nLen, &from, &len);
2006 m_impl.replace(from, len, ImplStr(sz));
2007 return *this;
2008 }
e87b7833
MB
2009 // replaces the substring of length nLen starting at nStart
2010 wxString& replace(size_t nStart, size_t nLen, const wxString& str)
8f93a29f
VS
2011 {
2012 size_t from, len;
2013 PosLenToImpl(nStart, nLen, &from, &len);
2014 m_impl.replace(from, len, str.m_impl);
2015 return *this;
2016 }
3c67202d 2017 // replaces the substring with nCount copies of ch
c9f78968 2018 wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxUniChar ch)
8f93a29f
VS
2019 {
2020 size_t from, len;
2021 PosLenToImpl(nStart, nLen, &from, &len);
2022#if wxUSE_UNICODE_UTF8
2023 if ( !ch.IsAscii() )
467175ab 2024 m_impl.replace(from, len, wxStringOperations::EncodeNChars(nCount, ch));
8f93a29f
VS
2025 else
2026#endif
2027 m_impl.replace(from, len, nCount, (wxStringCharType)ch);
2028 return *this;
2029 }
3c67202d
VZ
2030 // replaces a substring with another substring
2031 wxString& replace(size_t nStart, size_t nLen,
e87b7833 2032 const wxString& str, size_t nStart2, size_t nLen2)
8f93a29f
VS
2033 {
2034 size_t from, len;
2035 PosLenToImpl(nStart, nLen, &from, &len);
2036
2037 size_t from2, len2;
2038 str.PosLenToImpl(nStart2, nLen2, &from2, &len2);
2039
2040 m_impl.replace(from, len, str.m_impl, from2, len2);
2041 return *this;
2042 }
e87b7833 2043 // replaces the substring with first nCount chars of sz
fb20fa43 2044 wxString& replace(size_t nStart, size_t nLen,
8f93a29f
VS
2045 const char* sz, size_t nCount)
2046 {
2047 size_t from, len;
2048 PosLenToImpl(nStart, nLen, &from, &len);
2049
2050 SubstrBufFromMB str(ImplStr(sz, nCount));
2051
2052 m_impl.replace(from, len, str.data, str.len);
2053 return *this;
2054 }
2055 wxString& replace(size_t nStart, size_t nLen,
2056 const wchar_t* sz, size_t nCount)
2057 {
2058 size_t from, len;
2059 PosLenToImpl(nStart, nLen, &from, &len);
2060
2061 SubstrBufFromWC str(ImplStr(sz, nCount));
2062
2063 m_impl.replace(from, len, str.data, str.len);
2064 return *this;
2065 }
11aac4ba
VS
2066 wxString& replace(size_t nStart, size_t nLen,
2067 const wxString& s, size_t nCount)
2068 {
2069 size_t from, len;
2070 PosLenToImpl(nStart, nLen, &from, &len);
2071 m_impl.replace(from, len, s.m_impl.c_str(), s.LenToImpl(nCount));
2072 return *this;
2073 }
2074
8f93a29f 2075 wxString& replace(iterator first, iterator last, const char* s)
cf9a878b 2076 { m_impl.replace(first.impl(), last.impl(), ImplStr(s)); return *this; }
8f93a29f 2077 wxString& replace(iterator first, iterator last, const wchar_t* s)
cf9a878b 2078 { m_impl.replace(first.impl(), last.impl(), ImplStr(s)); return *this; }
8f93a29f
VS
2079 wxString& replace(iterator first, iterator last, const char* s, size_type n)
2080 {
2081 SubstrBufFromMB str(ImplStr(s, n));
cf9a878b 2082 m_impl.replace(first.impl(), last.impl(), str.data, str.len);
8f93a29f
VS
2083 return *this;
2084 }
2085 wxString& replace(iterator first, iterator last, const wchar_t* s, size_type n)
2086 {
2087 SubstrBufFromWC str(ImplStr(s, n));
cf9a878b 2088 m_impl.replace(first.impl(), last.impl(), str.data, str.len);
8f93a29f
VS
2089 return *this;
2090 }
e87b7833 2091 wxString& replace(iterator first, iterator last, const wxString& s)
cf9a878b 2092 { m_impl.replace(first.impl(), last.impl(), s.m_impl); return *this; }
8f93a29f
VS
2093 wxString& replace(iterator first, iterator last, size_type n, wxUniChar ch)
2094 {
2095#if wxUSE_UNICODE_UTF8
2096 if ( !ch.IsAscii() )
467175ab
VS
2097 m_impl.replace(first.impl(), last.impl(),
2098 wxStringOperations::EncodeNChars(n, ch));
8f93a29f
VS
2099 else
2100#endif
cf9a878b 2101 m_impl.replace(first.impl(), last.impl(), n, (wxStringCharType)ch);
8f93a29f
VS
2102 return *this;
2103 }
e87b7833
MB
2104 wxString& replace(iterator first, iterator last,
2105 const_iterator first1, const_iterator last1)
cf9a878b
VS
2106 {
2107 m_impl.replace(first.impl(), last.impl(), first1.impl(), last1.impl());
2108 return *this;
2109 }
f2a1b1bd
VZ
2110 wxString& replace(iterator first, iterator last,
2111 const char *first1, const char *last1)
2112 { replace(first, last, first1, last1 - first1); return *this; }
2113 wxString& replace(iterator first, iterator last,
2114 const wchar_t *first1, const wchar_t *last1)
2115 { replace(first, last, first1, last1 - first1); return *this; }
8f93a29f
VS
2116
2117 // swap two strings
2118 void swap(wxString& str)
2119 { m_impl.swap(str.m_impl); }
2120
2121 // find a substring
2122 size_t find(const wxString& str, size_t nStart = 0) const
2123 { return PosFromImpl(m_impl.find(str.m_impl, PosToImpl(nStart))); }
2124
2125 // find first n characters of sz
2126 size_t find(const char* sz, size_t nStart = 0, size_t n = npos) const
2127 {
2128 SubstrBufFromMB str(ImplStr(sz, n));
2129 return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len));
2130 }
2131 size_t find(const wchar_t* sz, size_t nStart = 0, size_t n = npos) const
2132 {
2133 SubstrBufFromWC str(ImplStr(sz, n));
2134 return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len));
2135 }
d3cefe87
VS
2136 size_t find(const wxCharBuffer& s, size_t nStart = 0, size_t n = npos) const
2137 { return find(s.data(), nStart, n); }
2138 size_t find(const wxWCharBuffer& s, size_t nStart = 0, size_t n = npos) const
2139 { return find(s.data(), nStart, n); }
2140 size_t find(const wxCStrData& s, size_t nStart = 0, size_t n = npos) const
2141 { return find(s.AsWChar(), nStart, n); }
8f93a29f
VS
2142
2143 // find the first occurence of character ch after nStart
2144 size_t find(wxUniChar ch, size_t nStart = 0) const
467175ab
VS
2145 {
2146 return PosFromImpl(m_impl.find(wxStringOperations::EncodeChar(ch),
2147 PosToImpl(nStart)));
2148 }
8f93a29f
VS
2149 size_t find(wxUniCharRef ch, size_t nStart = 0) const
2150 { return find(wxUniChar(ch), nStart); }
2151 size_t find(char ch, size_t nStart = 0) const
2152 { return find(wxUniChar(ch), nStart); }
50e02008
VS
2153 size_t find(unsigned char ch, size_t nStart = 0) const
2154 { return find(wxUniChar(ch), nStart); }
8f93a29f
VS
2155 size_t find(wchar_t ch, size_t nStart = 0) const
2156 { return find(wxUniChar(ch), nStart); }
2157
2158 // rfind() family is exactly like find() but works right to left
2159
2160 // as find, but from the end
2161 size_t rfind(const wxString& str, size_t nStart = npos) const
2162 { return PosFromImpl(m_impl.rfind(str.m_impl, PosToImpl(nStart))); }
2163
2164 // as find, but from the end
2165 size_t rfind(const char* sz, size_t nStart = npos, size_t n = npos) const
2166 {
2167 SubstrBufFromMB str(ImplStr(sz, n));
2168 return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len));
2169 }
2170 size_t rfind(const wchar_t* sz, size_t nStart = npos, size_t n = npos) const
2171 {
2172 SubstrBufFromWC str(ImplStr(sz, n));
2173 return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len));
2174 }
d3cefe87
VS
2175 size_t rfind(const wxCharBuffer& s, size_t nStart = npos, size_t n = npos) const
2176 { return rfind(s.data(), nStart, n); }
2177 size_t rfind(const wxWCharBuffer& s, size_t nStart = npos, size_t n = npos) const
2178 { return rfind(s.data(), nStart, n); }
2179 size_t rfind(const wxCStrData& s, size_t nStart = npos, size_t n = npos) const
2180 { return rfind(s.AsWChar(), nStart, n); }
8f93a29f
VS
2181 // as find, but from the end
2182 size_t rfind(wxUniChar ch, size_t nStart = npos) const
467175ab
VS
2183 {
2184 return PosFromImpl(m_impl.rfind(wxStringOperations::EncodeChar(ch),
2185 PosToImpl(nStart)));
2186 }
8f93a29f
VS
2187 size_t rfind(wxUniCharRef ch, size_t nStart = npos) const
2188 { return rfind(wxUniChar(ch), nStart); }
2189 size_t rfind(char ch, size_t nStart = npos) const
2190 { return rfind(wxUniChar(ch), nStart); }
50e02008
VS
2191 size_t rfind(unsigned char ch, size_t nStart = npos) const
2192 { return rfind(wxUniChar(ch), nStart); }
8f93a29f
VS
2193 size_t rfind(wchar_t ch, size_t nStart = npos) const
2194 { return rfind(wxUniChar(ch), nStart); }
2195
2196 // find first/last occurence of any character (not) in the set:
2197#if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2198 // FIXME-UTF8: this is not entirely correct, because it doesn't work if
2199 // sizeof(wchar_t)==2 and surrogates are present in the string;
2200 // should we care? Probably not.
2201 size_t find_first_of(const wxString& str, size_t nStart = 0) const
a962cdf4 2202 { return m_impl.find_first_of(str.m_impl, nStart); }
8f93a29f
VS
2203 size_t find_first_of(const char* sz, size_t nStart = 0) const
2204 { return m_impl.find_first_of(ImplStr(sz), nStart); }
2205 size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const
2206 { return m_impl.find_first_of(ImplStr(sz), nStart); }
a962cdf4 2207 size_t find_first_of(const char* sz, size_t nStart, size_t n) const
8f93a29f 2208 { return m_impl.find_first_of(ImplStr(sz), nStart, n); }
a962cdf4 2209 size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const
8f93a29f
VS
2210 { return m_impl.find_first_of(ImplStr(sz), nStart, n); }
2211 size_t find_first_of(wxUniChar c, size_t nStart = 0) const
2212 { return m_impl.find_first_of((wxChar)c, nStart); }
2213
a962cdf4
VS
2214 size_t find_last_of(const wxString& str, size_t nStart = npos) const
2215 { return m_impl.find_last_of(str.m_impl, nStart); }
8f93a29f
VS
2216 size_t find_last_of(const char* sz, size_t nStart = npos) const
2217 { return m_impl.find_last_of(ImplStr(sz), nStart); }
2218 size_t find_last_of(const wchar_t* sz, size_t nStart = npos) const
2219 { return m_impl.find_last_of(ImplStr(sz), nStart); }
2220 size_t find_last_of(const char* sz, size_t nStart, size_t n) const
2221 { return m_impl.find_last_of(ImplStr(sz), nStart, n); }
2222 size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const
2223 { return m_impl.find_last_of(ImplStr(sz), nStart, n); }
2224 size_t find_last_of(wxUniChar c, size_t nStart = npos) const
2225 { return m_impl.find_last_of((wxChar)c, nStart); }
2226
a962cdf4 2227 size_t find_first_not_of(const wxString& str, size_t nStart = 0) const
8f93a29f 2228 { return m_impl.find_first_not_of(str.m_impl, nStart); }
a962cdf4 2229 size_t find_first_not_of(const char* sz, size_t nStart = 0) const
8f93a29f 2230 { return m_impl.find_first_not_of(ImplStr(sz), nStart); }
a962cdf4 2231 size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const
8f93a29f 2232 { return m_impl.find_first_not_of(ImplStr(sz), nStart); }
a962cdf4 2233 size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const
8f93a29f 2234 { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); }
a962cdf4 2235 size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const
8f93a29f 2236 { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); }
a962cdf4 2237 size_t find_first_not_of(wxUniChar c, size_t nStart = 0) const
8f93a29f
VS
2238 { return m_impl.find_first_not_of((wxChar)c, nStart); }
2239
a962cdf4 2240 size_t find_last_not_of(const wxString& str, size_t nStart = npos) const
8f93a29f 2241 { return m_impl.find_last_not_of(str.m_impl, nStart); }
a962cdf4 2242 size_t find_last_not_of(const char* sz, size_t nStart = npos) const
8f93a29f 2243 { return m_impl.find_last_not_of(ImplStr(sz), nStart); }
a962cdf4 2244 size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const
8f93a29f 2245 { return m_impl.find_last_not_of(ImplStr(sz), nStart); }
a962cdf4 2246 size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const
8f93a29f 2247 { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); }
a962cdf4 2248 size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const
8f93a29f 2249 { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); }
a962cdf4 2250 size_t find_last_not_of(wxUniChar c, size_t nStart = npos) const
8f93a29f
VS
2251 { return m_impl.find_last_not_of((wxChar)c, nStart); }
2252#else
2253 // we can't use std::string implementation in UTF-8 build, because the
2254 // character sets would be interpreted wrongly:
2255
2256 // as strpbrk() but starts at nStart, returns npos if not found
2257 size_t find_first_of(const wxString& str, size_t nStart = 0) const
81727065 2258#if wxUSE_UNICODE // FIXME-UTF8: temporary
d3cefe87 2259 { return find_first_of(str.wc_str(), nStart); }
81727065 2260#else
d3cefe87 2261 { return find_first_of(str.mb_str(), nStart); }
81727065 2262#endif
8f93a29f
VS
2263 // same as above
2264 size_t find_first_of(const char* sz, size_t nStart = 0) const;
2265 size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const;
2266 size_t find_first_of(const char* sz, size_t nStart, size_t n) const;
2267 size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const;
2268 // same as find(char, size_t)
2269 size_t find_first_of(wxUniChar c, size_t nStart = 0) const
2270 { return find(c, nStart); }
2271 // find the last (starting from nStart) char from str in this string
2272 size_t find_last_of (const wxString& str, size_t nStart = npos) const
81727065 2273#if wxUSE_UNICODE // FIXME-UTF8: temporary
d3cefe87 2274 { return find_last_of(str.wc_str(), nStart); }
81727065 2275#else
d3cefe87 2276 { return find_last_of(str.mb_str(), nStart); }
81727065 2277#endif
8f93a29f
VS
2278 // same as above
2279 size_t find_last_of (const char* sz, size_t nStart = npos) const;
2280 size_t find_last_of (const wchar_t* sz, size_t nStart = npos) const;
2281 size_t find_last_of(const char* sz, size_t nStart, size_t n) const;
2282 size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const;
2283 // same as above
2284 size_t find_last_of(wxUniChar c, size_t nStart = npos) const
2285 { return rfind(c, nStart); }
2286
2287 // find first/last occurence of any character not in the set
2288
2289 // as strspn() (starting from nStart), returns npos on failure
2290 size_t find_first_not_of(const wxString& str, size_t nStart = 0) const
81727065 2291#if wxUSE_UNICODE // FIXME-UTF8: temporary
d3cefe87 2292 { return find_first_not_of(str.wc_str(), nStart); }
81727065 2293#else
d3cefe87 2294 { return find_first_not_of(str.mb_str(), nStart); }
81727065 2295#endif
8f93a29f
VS
2296 // same as above
2297 size_t find_first_not_of(const char* sz, size_t nStart = 0) const;
2298 size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const;
2299 size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const;
2300 size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const;
2301 // same as above
2302 size_t find_first_not_of(wxUniChar ch, size_t nStart = 0) const;
2303 // as strcspn()
2304 size_t find_last_not_of(const wxString& str, size_t nStart = npos) const
81727065 2305#if wxUSE_UNICODE // FIXME-UTF8: temporary
d3cefe87 2306 { return find_last_not_of(str.wc_str(), nStart); }
81727065 2307#else
d3cefe87 2308 { return find_last_not_of(str.mb_str(), nStart); }
81727065 2309#endif
8f93a29f
VS
2310 // same as above
2311 size_t find_last_not_of(const char* sz, size_t nStart = npos) const;
2312 size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const;
2313 size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const;
2314 size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const;
2315 // same as above
2316 size_t find_last_not_of(wxUniChar ch, size_t nStart = npos) const;
2317#endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not
2318
2319 // provide char/wchar_t/wxUniCharRef overloads for char-finding functions
2320 // above to resolve ambiguities:
2321 size_t find_first_of(wxUniCharRef ch, size_t nStart = 0) const
2322 { return find_first_of(wxUniChar(ch), nStart); }
2323 size_t find_first_of(char ch, size_t nStart = 0) const
2324 { return find_first_of(wxUniChar(ch), nStart); }
50e02008
VS
2325 size_t find_first_of(unsigned char ch, size_t nStart = 0) const
2326 { return find_first_of(wxUniChar(ch), nStart); }
8f93a29f
VS
2327 size_t find_first_of(wchar_t ch, size_t nStart = 0) const
2328 { return find_first_of(wxUniChar(ch), nStart); }
2329 size_t find_last_of(wxUniCharRef ch, size_t nStart = npos) const
2330 { return find_last_of(wxUniChar(ch), nStart); }
2331 size_t find_last_of(char ch, size_t nStart = npos) const
2332 { return find_last_of(wxUniChar(ch), nStart); }
50e02008
VS
2333 size_t find_last_of(unsigned char ch, size_t nStart = npos) const
2334 { return find_last_of(wxUniChar(ch), nStart); }
8f93a29f
VS
2335 size_t find_last_of(wchar_t ch, size_t nStart = npos) const
2336 { return find_last_of(wxUniChar(ch), nStart); }
2337 size_t find_first_not_of(wxUniCharRef ch, size_t nStart = 0) const
2338 { return find_first_not_of(wxUniChar(ch), nStart); }
2339 size_t find_first_not_of(char ch, size_t nStart = 0) const
2340 { return find_first_not_of(wxUniChar(ch), nStart); }
50e02008
VS
2341 size_t find_first_not_of(unsigned char ch, size_t nStart = 0) const
2342 { return find_first_not_of(wxUniChar(ch), nStart); }
8f93a29f
VS
2343 size_t find_first_not_of(wchar_t ch, size_t nStart = 0) const
2344 { return find_first_not_of(wxUniChar(ch), nStart); }
2345 size_t find_last_not_of(wxUniCharRef ch, size_t nStart = npos) const
2346 { return find_last_not_of(wxUniChar(ch), nStart); }
2347 size_t find_last_not_of(char ch, size_t nStart = npos) const
2348 { return find_last_not_of(wxUniChar(ch), nStart); }
50e02008
VS
2349 size_t find_last_not_of(unsigned char ch, size_t nStart = npos) const
2350 { return find_last_not_of(wxUniChar(ch), nStart); }
8f93a29f
VS
2351 size_t find_last_not_of(wchar_t ch, size_t nStart = npos) const
2352 { return find_last_not_of(wxUniChar(ch), nStart); }
3c67202d 2353
d3cefe87
VS
2354 // and additional overloads for the versions taking strings:
2355 size_t find_first_of(const wxCStrData& sz, size_t nStart = 0) const
2356 { return find_first_of(sz.AsString(), nStart); }
2357 size_t find_first_of(const wxCharBuffer& sz, size_t nStart = 0) const
2358 { return find_first_of(sz.data(), nStart); }
2359 size_t find_first_of(const wxWCharBuffer& sz, size_t nStart = 0) const
2360 { return find_first_of(sz.data(), nStart); }
2361 size_t find_first_of(const wxCStrData& sz, size_t nStart, size_t n) const
2362 { return find_first_of(sz.AsWChar(), nStart, n); }
2363 size_t find_first_of(const wxCharBuffer& sz, size_t nStart, size_t n) const
2364 { return find_first_of(sz.data(), nStart, n); }
2365 size_t find_first_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const
2366 { return find_first_of(sz.data(), nStart, n); }
2367
2368 size_t find_last_of(const wxCStrData& sz, size_t nStart = 0) const
2369 { return find_last_of(sz.AsString(), nStart); }
2370 size_t find_last_of(const wxCharBuffer& sz, size_t nStart = 0) const
2371 { return find_last_of(sz.data(), nStart); }
2372 size_t find_last_of(const wxWCharBuffer& sz, size_t nStart = 0) const
2373 { return find_last_of(sz.data(), nStart); }
2374 size_t find_last_of(const wxCStrData& sz, size_t nStart, size_t n) const
2375 { return find_last_of(sz.AsWChar(), nStart, n); }
2376 size_t find_last_of(const wxCharBuffer& sz, size_t nStart, size_t n) const
2377 { return find_last_of(sz.data(), nStart, n); }
2378 size_t find_last_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const
2379 { return find_last_of(sz.data(), nStart, n); }
2380
2381 size_t find_first_not_of(const wxCStrData& sz, size_t nStart = 0) const
2382 { return find_first_not_of(sz.AsString(), nStart); }
2383 size_t find_first_not_of(const wxCharBuffer& sz, size_t nStart = 0) const
2384 { return find_first_not_of(sz.data(), nStart); }
2385 size_t find_first_not_of(const wxWCharBuffer& sz, size_t nStart = 0) const
2386 { return find_first_not_of(sz.data(), nStart); }
2387 size_t find_first_not_of(const wxCStrData& sz, size_t nStart, size_t n) const
2388 { return find_first_not_of(sz.AsWChar(), nStart, n); }
2389 size_t find_first_not_of(const wxCharBuffer& sz, size_t nStart, size_t n) const
2390 { return find_first_not_of(sz.data(), nStart, n); }
2391 size_t find_first_not_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const
2392 { return find_first_not_of(sz.data(), nStart, n); }
2393
2394 size_t find_last_not_of(const wxCStrData& sz, size_t nStart = 0) const
2395 { return find_last_not_of(sz.AsString(), nStart); }
2396 size_t find_last_not_of(const wxCharBuffer& sz, size_t nStart = 0) const
2397 { return find_last_not_of(sz.data(), nStart); }
2398 size_t find_last_not_of(const wxWCharBuffer& sz, size_t nStart = 0) const
2399 { return find_last_not_of(sz.data(), nStart); }
2400 size_t find_last_not_of(const wxCStrData& sz, size_t nStart, size_t n) const
2401 { return find_last_not_of(sz.AsWChar(), nStart, n); }
2402 size_t find_last_not_of(const wxCharBuffer& sz, size_t nStart, size_t n) const
2403 { return find_last_not_of(sz.data(), nStart, n); }
2404 size_t find_last_not_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const
2405 { return find_last_not_of(sz.data(), nStart, n); }
2406
e87b7833
MB
2407 // string += string
2408 wxString& operator+=(const wxString& s)
8f93a29f 2409 { m_impl += s.m_impl; return *this; }
e87b7833 2410 // string += C string
8f93a29f
VS
2411 wxString& operator+=(const char *psz)
2412 { m_impl += ImplStr(psz); return *this; }
2413 wxString& operator+=(const wchar_t *pwz)
2414 { m_impl += ImplStr(pwz); return *this; }
c9f78968 2415 wxString& operator+=(const wxCStrData& s)
8f93a29f 2416 { m_impl += s.AsString().m_impl; return *this; }
d3cefe87
VS
2417 wxString& operator+=(const wxCharBuffer& s)
2418 { return operator+=(s.data()); }
2419 wxString& operator+=(const wxWCharBuffer& s)
2420 { return operator+=(s.data()); }
e87b7833 2421 // string += char
c9f78968 2422 wxString& operator+=(wxUniChar ch)
467175ab 2423 { m_impl += wxStringOperations::EncodeChar(ch); return *this; }
c9f78968 2424 wxString& operator+=(wxUniCharRef ch) { return *this += wxUniChar(ch); }
b7d40341 2425 wxString& operator+=(int ch) { return *this += wxUniChar(ch); }
c9f78968 2426 wxString& operator+=(char ch) { return *this += wxUniChar(ch); }
b77afb41 2427 wxString& operator+=(unsigned char ch) { return *this += wxUniChar(ch); }
c9f78968 2428 wxString& operator+=(wchar_t ch) { return *this += wxUniChar(ch); }
d8a4b666
VS
2429
2430private:
2523e9b7 2431#if !wxUSE_STL_BASED_WXSTRING
d8a4b666 2432 // helpers for wxStringBuffer and wxStringBufferLength
8f93a29f
VS
2433 wxStringCharType *DoGetWriteBuf(size_t nLen)
2434 { return m_impl.DoGetWriteBuf(nLen); }
2435 void DoUngetWriteBuf()
2436 { m_impl.DoUngetWriteBuf(); }
2437 void DoUngetWriteBuf(size_t nLen)
2438 { m_impl.DoUngetWriteBuf(nLen); }
2523e9b7 2439#endif // !wxUSE_STL_BASED_WXSTRING
c9f78968
VS
2440
2441#ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
d1f6e2cf
VS
2442 #if !wxUSE_UTF8_LOCALE_ONLY
2443 int DoPrintfWchar(const wxChar *format, ...);
2444 static wxString DoFormatWchar(const wxChar *format, ...);
2445 #endif
2446 #if wxUSE_UNICODE_UTF8
2447 int DoPrintfUtf8(const char *format, ...);
2448 static wxString DoFormatUtf8(const char *format, ...);
2449 #endif
c9f78968 2450#endif
8f93a29f
VS
2451
2452#if !wxUSE_STL_BASED_WXSTRING
2453 // check string's data validity
2454 bool IsValid() const { return m_impl.GetStringData()->IsValid(); }
2455#endif
2456
2457private:
2458 wxStringImpl m_impl;
132276cf
VS
2459
2460 // buffers for compatibility conversion from (char*)c_str() and
2461 // (wchar_t*)c_str():
2462 // FIXME-UTF8: bechmark various approaches to keeping compatibility buffers
2463 template<typename T>
2464 struct ConvertedBuffer
2465 {
2466 ConvertedBuffer() : m_buf(NULL) {}
2467 ~ConvertedBuffer()
2468 { free(m_buf); }
2469
05f32fc3 2470 operator T*() const { return m_buf; }
132276cf
VS
2471
2472 ConvertedBuffer& operator=(T *str)
2473 {
2474 free(m_buf);
2475 m_buf = str;
2476 return *this;
2477 }
2478
2479 T *m_buf;
2480 };
111d9948 2481#if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
132276cf
VS
2482 ConvertedBuffer<char> m_convertedToChar;
2483#endif
2484#if !wxUSE_UNICODE_WCHAR
2485 ConvertedBuffer<wchar_t> m_convertedToWChar;
2486#endif
2523e9b7 2487
132276cf 2488 friend class WXDLLIMPEXP_BASE wxCStrData;
2523e9b7
VS
2489 friend class wxImplStringBuffer;
2490 friend class wxImplStringBufferLength;
c801d85f
KB
2491};
2492
c9f78968
VS
2493#ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
2494 #pragma warning (default:4275)
2495#endif
2496
a962cdf4
VS
2497// string iterator operators that satisfy STL Random Access Iterator
2498// requirements:
2499inline wxString::iterator operator+(int n, wxString::iterator i)
2500 { return i + n; }
2501inline wxString::iterator operator+(size_t n, wxString::iterator i)
2502 { return i + n; }
2503inline wxString::const_iterator operator+(int n, wxString::const_iterator i)
2504 { return i + n; }
2505inline wxString::const_iterator operator+(size_t n, wxString::const_iterator i)
2506 { return i + n; }
2507inline wxString::reverse_iterator operator+(int n, wxString::reverse_iterator i)
2508 { return i + n; }
2509inline wxString::reverse_iterator operator+(size_t n, wxString::reverse_iterator i)
2510 { return i + n; }
2511inline wxString::const_reverse_iterator operator+(int n, wxString::const_reverse_iterator i)
2512 { return i + n; }
2513inline wxString::const_reverse_iterator operator+(size_t n, wxString::const_reverse_iterator i)
2514 { return i + n; }
2515
1fc8cfbd
VZ
2516// notice that even though for many compilers the friend declarations above are
2517// enough, from the point of view of C++ standard we must have the declarations
2518// here as friend ones are not injected in the enclosing namespace and without
2519// them the code fails to compile with conforming compilers such as xlC or g++4
c9f78968 2520wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2);
8f93a29f
VS
2521wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const char *psz);
2522wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wchar_t *pwz);
2523wxString WXDLLIMPEXP_BASE operator+(const char *psz, const wxString& string);
2524wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz, const wxString& string);
1fc8cfbd 2525
c9f78968
VS
2526wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch);
2527wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string);
2528
2529inline wxString operator+(const wxString& string, wxUniCharRef ch)
2530 { return string + (wxUniChar)ch; }
2531inline wxString operator+(const wxString& string, char ch)
2532 { return string + wxUniChar(ch); }
2533inline wxString operator+(const wxString& string, wchar_t ch)
2534 { return string + wxUniChar(ch); }
2535inline wxString operator+(wxUniCharRef ch, const wxString& string)
2536 { return (wxUniChar)ch + string; }
2537inline wxString operator+(char ch, const wxString& string)
2538 { return wxUniChar(ch) + string; }
2539inline wxString operator+(wchar_t ch, const wxString& string)
2540 { return wxUniChar(ch) + string; }
2541
b7146cbe 2542
992527a5 2543#if wxUSE_STL_BASED_WXSTRING
07170120 2544 // return an empty wxString (not very useful with wxUSE_STL == 1)
12f99210 2545 inline const wxString wxGetEmptyString() { return wxString(); }
992527a5 2546#else // !wxUSE_STL_BASED_WXSTRING
07170120
VZ
2547 // return an empty wxString (more efficient than wxString() here)
2548 inline const wxString& wxGetEmptyString()
2549 {
2550 return *(wxString *)&wxEmptyString;
2551 }
992527a5 2552#endif // wxUSE_STL_BASED_WXSTRING/!wxUSE_STL_BASED_WXSTRING
07170120 2553
1d218550
VZ
2554// ----------------------------------------------------------------------------
2555// wxStringBuffer: a tiny class allowing to get a writable pointer into string
2556// ----------------------------------------------------------------------------
2557
2523e9b7
VS
2558#if !wxUSE_STL_BASED_WXSTRING
2559// string buffer for direct access to string data in their native
2560// representation:
2561class wxImplStringBuffer
2562{
2563public:
2564 typedef wxStringCharType CharType;
2565
2566 wxImplStringBuffer(wxString& str, size_t lenWanted = 1024)
2567 : m_str(str), m_buf(NULL)
2568 { m_buf = m_str.DoGetWriteBuf(lenWanted); }
2569
2570 ~wxImplStringBuffer() { m_str.DoUngetWriteBuf(); }
2571
2572 operator wxStringCharType*() const { return m_buf; }
941b78cc 2573
2523e9b7
VS
2574private:
2575 wxString& m_str;
2576 wxStringCharType *m_buf;
2577
2578 DECLARE_NO_COPY_CLASS(wxImplStringBuffer)
2579};
2580
2581class wxImplStringBufferLength
941b78cc
MB
2582{
2583public:
2523e9b7
VS
2584 typedef wxStringCharType CharType;
2585
2586 wxImplStringBufferLength(wxString& str, size_t lenWanted = 1024)
2587 : m_str(str), m_buf(NULL), m_len(0), m_lenSet(false)
2588 {
2589 m_buf = m_str.DoGetWriteBuf(lenWanted);
2590 wxASSERT(m_buf != NULL);
2591 }
2592
2593 ~wxImplStringBufferLength()
2594 {
2595 wxASSERT(m_lenSet);
2596 m_str.DoUngetWriteBuf(m_len);
2597 }
2598
2599 operator wxStringCharType*() const { return m_buf; }
2600 void SetLength(size_t length) { m_len = length; m_lenSet = true; }
2601
2602private:
2603 wxString& m_str;
2604 wxStringCharType *m_buf;
2605 size_t m_len;
2606 bool m_lenSet;
2607
2608 DECLARE_NO_COPY_CLASS(wxImplStringBufferLength)
2609};
2610
2611#endif // !wxUSE_STL_BASED_WXSTRING
2612
2613template<typename T>
2614class wxStringTypeBufferBase
2615{
2616public:
2617 typedef T CharType;
2618
2619 wxStringTypeBufferBase(wxString& str, size_t lenWanted = 1024)
e87b7833 2620 : m_str(str), m_buf(lenWanted)
941b78cc
MB
2621 { }
2622
941b78cc 2623
2523e9b7 2624 operator CharType*() { return m_buf.data(); }
941b78cc 2625
2523e9b7 2626protected:
941b78cc 2627 wxString& m_str;
2523e9b7 2628 wxCharTypeBuffer<CharType> m_buf;
941b78cc
MB
2629};
2630
2523e9b7
VS
2631template<typename T>
2632class wxStringTypeBufferLengthBase
941b78cc
MB
2633{
2634public:
2523e9b7
VS
2635 typedef T CharType;
2636
2637 wxStringTypeBufferLengthBase(wxString& str, size_t lenWanted = 1024)
941b78cc
MB
2638 : m_str(str), m_buf(lenWanted), m_len(0), m_lenSet(false)
2639 { }
2640
2523e9b7 2641 ~wxStringTypeBufferLengthBase()
941b78cc
MB
2642 {
2643 wxASSERT(m_lenSet);
2644 m_str.assign(m_buf.data(), m_len);
2645 }
2646
2523e9b7 2647 operator CharType*() { return m_buf.data(); }
941b78cc
MB
2648 void SetLength(size_t length) { m_len = length; m_lenSet = true; }
2649
2523e9b7 2650protected:
941b78cc 2651 wxString& m_str;
2523e9b7 2652 wxCharTypeBuffer<CharType> m_buf;
941b78cc
MB
2653 size_t m_len;
2654 bool m_lenSet;
941b78cc
MB
2655};
2656
2523e9b7
VS
2657template<typename T>
2658class wxStringTypeBuffer : public wxStringTypeBufferBase<T>
2659{
2660public:
2661 wxStringTypeBuffer(wxString& str, size_t lenWanted = 1024)
2662 : wxStringTypeBufferBase<T>(str, lenWanted) {}
2663 ~wxStringTypeBuffer()
2664 {
2665 this->m_str.assign(this->m_buf.data());
2666 }
941b78cc 2667
2523e9b7
VS
2668 DECLARE_NO_COPY_CLASS(wxStringTypeBuffer)
2669};
2670
2671template<typename T>
2672class wxStringTypeBufferLength : public wxStringTypeBufferLengthBase<T>
1d218550
VZ
2673{
2674public:
2523e9b7
VS
2675 wxStringTypeBufferLength(wxString& str, size_t lenWanted = 1024)
2676 : wxStringTypeBufferLengthBase<T>(str, lenWanted) {}
e015c2a3 2677
2523e9b7
VS
2678 ~wxStringTypeBufferLength()
2679 {
2680 wxASSERT(this->m_lenSet);
2681 this->m_str.assign(this->m_buf.data(), this->m_len);
2682 }
e015c2a3 2683
2523e9b7
VS
2684 DECLARE_NO_COPY_CLASS(wxStringTypeBufferLength)
2685};
e015c2a3 2686
2523e9b7
VS
2687#if wxUSE_STL_BASED_WXSTRING
2688class wxImplStringBuffer : public wxStringTypeBufferBase<wxStringCharType>
2689{
2690public:
2691 wxImplStringBuffer(wxString& str, size_t lenWanted = 1024)
2692 : wxStringTypeBufferBase<wxStringCharType>(str, lenWanted) {}
2693 ~wxImplStringBuffer()
2694 { m_str.m_impl.assign(m_buf.data()); }
e015c2a3 2695
2523e9b7 2696 DECLARE_NO_COPY_CLASS(wxImplStringBuffer)
1d218550
VZ
2697};
2698
2523e9b7 2699class wxImplStringBufferLength : public wxStringTypeBufferLengthBase<wxStringCharType>
941b78cc
MB
2700{
2701public:
2523e9b7
VS
2702 wxImplStringBufferLength(wxString& str, size_t lenWanted = 1024)
2703 : wxStringTypeBufferLengthBase<wxStringCharType>(str, lenWanted) {}
941b78cc 2704
2523e9b7 2705 ~wxImplStringBufferLength()
941b78cc
MB
2706 {
2707 wxASSERT(m_lenSet);
2523e9b7 2708 m_str.m_impl.assign(m_buf.data(), m_len);
941b78cc
MB
2709 }
2710
2523e9b7 2711 DECLARE_NO_COPY_CLASS(wxImplStringBufferLength)
941b78cc 2712};
2523e9b7
VS
2713#endif // wxUSE_STL_BASED_WXSTRING
2714
941b78cc 2715
2523e9b7
VS
2716#if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
2717typedef wxStringTypeBuffer<wxChar> wxStringBuffer;
2718typedef wxStringTypeBufferLength<wxChar> wxStringBufferLength;
2719#else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2720typedef wxImplStringBuffer wxStringBuffer;
2721typedef wxImplStringBufferLength wxStringBufferLength;
8f93a29f 2722#endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
941b78cc 2723
c801d85f 2724// ---------------------------------------------------------------------------
3c67202d 2725// wxString comparison functions: operator versions are always case sensitive
c801d85f 2726// ---------------------------------------------------------------------------
f33fee2a 2727
831faf97 2728#define wxCMP_WXCHAR_STRING(p, s, op) 0 op s.Cmp(p)
fb52c2b6
VZ
2729
2730wxDEFINE_ALL_COMPARISONS(const wxChar *, const wxString&, wxCMP_WXCHAR_STRING)
2731
2732#undef wxCMP_WXCHAR_STRING
2733
2734// note that there is an optimization in operator==() and !=(): we (quickly)
2735// checks the strings length first, before comparing their data
0cbe5ee3
VZ
2736inline bool operator==(const wxString& s1, const wxString& s2)
2737 { return (s1.Len() == s2.Len()) && (s1.Cmp(s2) == 0); }
0cbe5ee3
VZ
2738inline bool operator!=(const wxString& s1, const wxString& s2)
2739 { return (s1.Len() != s2.Len()) || (s1.Cmp(s2) != 0); }
0cbe5ee3
VZ
2740inline bool operator< (const wxString& s1, const wxString& s2)
2741 { return s1.Cmp(s2) < 0; }
0cbe5ee3
VZ
2742inline bool operator> (const wxString& s1, const wxString& s2)
2743 { return s1.Cmp(s2) > 0; }
0cbe5ee3
VZ
2744inline bool operator<=(const wxString& s1, const wxString& s2)
2745 { return s1.Cmp(s2) <= 0; }
0cbe5ee3
VZ
2746inline bool operator>=(const wxString& s1, const wxString& s2)
2747 { return s1.Cmp(s2) >= 0; }
3c67202d 2748
5ca07d0f
OK
2749#if wxUSE_UNICODE
2750inline bool operator==(const wxString& s1, const wxWCharBuffer& s2)
f33fee2a 2751 { return (s1.Cmp((const wchar_t *)s2) == 0); }
5ca07d0f 2752inline bool operator==(const wxWCharBuffer& s1, const wxString& s2)
f33fee2a 2753 { return (s2.Cmp((const wchar_t *)s1) == 0); }
f6bcfd97
BP
2754inline bool operator!=(const wxString& s1, const wxWCharBuffer& s2)
2755 { return (s1.Cmp((const wchar_t *)s2) != 0); }
2756inline bool operator!=(const wxWCharBuffer& s1, const wxString& s2)
2757 { return (s2.Cmp((const wchar_t *)s1) != 0); }
0cbe5ee3 2758#else // !wxUSE_UNICODE
5ca07d0f 2759inline bool operator==(const wxString& s1, const wxCharBuffer& s2)
f33fee2a 2760 { return (s1.Cmp((const char *)s2) == 0); }
5ca07d0f 2761inline bool operator==(const wxCharBuffer& s1, const wxString& s2)
f33fee2a 2762 { return (s2.Cmp((const char *)s1) == 0); }
f6bcfd97
BP
2763inline bool operator!=(const wxString& s1, const wxCharBuffer& s2)
2764 { return (s1.Cmp((const char *)s2) != 0); }
2765inline bool operator!=(const wxCharBuffer& s1, const wxString& s2)
2766 { return (s2.Cmp((const char *)s1) != 0); }
0cbe5ee3 2767#endif // wxUSE_UNICODE/!wxUSE_UNICODE
5ca07d0f 2768
028a2b5d 2769#if wxUSE_UNICODE
6d56eb5c 2770inline wxString operator+(const wxString& string, const wxWCharBuffer& buf)
e90c1d2a 2771 { return string + (const wchar_t *)buf; }
6d56eb5c 2772inline wxString operator+(const wxWCharBuffer& buf, const wxString& string)
e90c1d2a 2773 { return (const wchar_t *)buf + string; }
0cbe5ee3 2774#else // !wxUSE_UNICODE
6d56eb5c 2775inline wxString operator+(const wxString& string, const wxCharBuffer& buf)
e90c1d2a 2776 { return string + (const char *)buf; }
6d56eb5c 2777inline wxString operator+(const wxCharBuffer& buf, const wxString& string)
e90c1d2a 2778 { return (const char *)buf + string; }
0cbe5ee3 2779#endif // wxUSE_UNICODE/!wxUSE_UNICODE
f04f3991 2780
a962cdf4 2781// comparison with char
c9f78968
VS
2782inline bool operator==(const wxUniChar& c, const wxString& s) { return s.IsSameAs(c); }
2783inline bool operator==(const wxUniCharRef& c, const wxString& s) { return s.IsSameAs(c); }
2784inline bool operator==(char c, const wxString& s) { return s.IsSameAs(c); }
2785inline bool operator==(wchar_t c, const wxString& s) { return s.IsSameAs(c); }
2786inline bool operator==(int c, const wxString& s) { return s.IsSameAs(c); }
2787inline bool operator==(const wxString& s, const wxUniChar& c) { return s.IsSameAs(c); }
2788inline bool operator==(const wxString& s, const wxUniCharRef& c) { return s.IsSameAs(c); }
2789inline bool operator==(const wxString& s, char c) { return s.IsSameAs(c); }
2790inline bool operator==(const wxString& s, wchar_t c) { return s.IsSameAs(c); }
2791inline bool operator!=(const wxUniChar& c, const wxString& s) { return !s.IsSameAs(c); }
2792inline bool operator!=(const wxUniCharRef& c, const wxString& s) { return !s.IsSameAs(c); }
2793inline bool operator!=(char c, const wxString& s) { return !s.IsSameAs(c); }
2794inline bool operator!=(wchar_t c, const wxString& s) { return !s.IsSameAs(c); }
2795inline bool operator!=(int c, const wxString& s) { return !s.IsSameAs(c); }
2796inline bool operator!=(const wxString& s, const wxUniChar& c) { return !s.IsSameAs(c); }
2797inline bool operator!=(const wxString& s, const wxUniCharRef& c) { return !s.IsSameAs(c); }
2798inline bool operator!=(const wxString& s, char c) { return !s.IsSameAs(c); }
2799inline bool operator!=(const wxString& s, wchar_t c) { return !s.IsSameAs(c); }
e51b17a1 2800
d7330233
VS
2801// comparison with C string in Unicode build
2802#if wxUSE_UNICODE
fb52c2b6
VZ
2803
2804#define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s
2805
2806wxDEFINE_ALL_COMPARISONS(const char *, const wxString&, wxCMP_CHAR_STRING)
2807
2808#undef wxCMP_CHAR_STRING
2809
d7330233
VS
2810#endif // wxUSE_UNICODE
2811
fb52c2b6
VZ
2812// we also need to provide the operators for comparison with wxCStrData to
2813// resolve ambiguity between operator(const wxChar *,const wxString &) and
2814// operator(const wxChar *, const wxChar *) for "p == s.c_str()"
2815//
2816// notice that these are (shallow) pointer comparisons, not (deep) string ones
2817#define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar()
2818#define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar()
2819
11aac4ba
VS
2820wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData&, wxCMP_WCHAR_CSTRDATA)
2821wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData&, wxCMP_CHAR_CSTRDATA)
fb52c2b6
VZ
2822
2823#undef wxCMP_CHAR_CSTRDATA
2824#undef wxCMP_WCHAR_CSTRDATA
2825
c801d85f 2826// ---------------------------------------------------------------------------
3c67202d 2827// Implementation only from here until the end of file
c801d85f
KB
2828// ---------------------------------------------------------------------------
2829
e87b7833 2830#if wxUSE_STD_IOSTREAM
c801d85f 2831
65f19af1 2832#include "wx/iosfwrap.h"
c801d85f 2833
bddd7a8d 2834WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxString&);
c9f78968 2835WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCStrData&);
04abe4bc
VS
2836WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCharBuffer&);
2837#ifndef __BORLANDC__
2838WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxWCharBuffer&);
2839#endif
c801d85f 2840
3c67202d 2841#endif // wxSTD_STRING_COMPATIBILITY
c801d85f 2842
c9f78968
VS
2843// ---------------------------------------------------------------------------
2844// wxCStrData implementation
2845// ---------------------------------------------------------------------------
2846
2847inline wxCStrData::wxCStrData(char *buf)
2848 : m_str(new wxString(buf)), m_offset(0), m_owned(true) {}
2849inline wxCStrData::wxCStrData(wchar_t *buf)
2850 : m_str(new wxString(buf)), m_offset(0), m_owned(true) {}
2851
92a17abc
VS
2852inline wxCStrData::wxCStrData(const wxCStrData& data)
2853 : m_str(data.m_owned ? new wxString(*data.m_str) : data.m_str),
2854 m_offset(data.m_offset),
2855 m_owned(data.m_owned)
2856{
2857}
2858
c9f78968
VS
2859inline wxCStrData::~wxCStrData()
2860{
2861 if ( m_owned )
2862 delete m_str;
2863}
2864
11aac4ba
VS
2865// simple cases for AsChar() and AsWChar(), the complicated ones are
2866// in string.cpp
2867#if wxUSE_UNICODE_WCHAR
c9f78968 2868inline const wchar_t* wxCStrData::AsWChar() const
11aac4ba
VS
2869{
2870 return m_str->wx_str() + m_offset;
2871}
2872#endif // wxUSE_UNICODE_WCHAR
2873
bfb6847d 2874#if !wxUSE_UNICODE
c9f78968 2875inline const char* wxCStrData::AsChar() const
c9f78968 2876{
bfb6847d 2877 return m_str->wx_str() + m_offset;
c9f78968 2878}
11aac4ba 2879#endif // !wxUSE_UNICODE
c9f78968 2880
bfb6847d
VS
2881#if wxUSE_UTF8_LOCALE_ONLY
2882inline const char* wxCStrData::AsChar() const
2883{
2884 return wxStringOperations::AddToIter(m_str->wx_str(), m_offset);
2885}
2886#endif // wxUSE_UTF8_LOCALE_ONLY
2887
681e4412
VS
2888inline const wxCharBuffer wxCStrData::AsCharBuf() const
2889{
2890#if !wxUSE_UNICODE
2891 return wxCharBuffer::CreateNonOwned(AsChar());
2892#else
2893 return AsString().mb_str();
2894#endif
2895}
2896
2897inline const wxWCharBuffer wxCStrData::AsWCharBuf() const
2898{
2899#if wxUSE_UNICODE_WCHAR
2900 return wxWCharBuffer::CreateNonOwned(AsWChar());
2901#else
2902 return AsString().wc_str();
2903#endif
2904}
2905
c9f78968
VS
2906inline wxString wxCStrData::AsString() const
2907{
2908 if ( m_offset == 0 )
2909 return *m_str;
2910 else
2911 return m_str->Mid(m_offset);
2912}
2913
2523e9b7
VS
2914inline const wxStringCharType *wxCStrData::AsInternal() const
2915{
bfb6847d 2916#if wxUSE_UNICODE_UTF8
2523e9b7 2917 return wxStringOperations::AddToIter(m_str->wx_str(), m_offset);
bfb6847d
VS
2918#else
2919 return m_str->wx_str() + m_offset;
2920#endif
2523e9b7
VS
2921}
2922
c9f78968
VS
2923inline wxUniChar wxCStrData::operator*() const
2924{
2925 if ( m_str->empty() )
2926 return wxUniChar(_T('\0'));
2927 else
2928 return (*m_str)[m_offset];
2929}
2930
2931inline wxUniChar wxCStrData::operator[](size_t n) const
2932{
378964d4
VS
2933 // NB: we intentionally use operator[] and not at() here because the former
2934 // works for the terminating NUL while the latter does not
2935 return (*m_str)[m_offset + n];
c9f78968
VS
2936}
2937
cc5bd48e
VZ
2938// ----------------------------------------------------------------------------
2939// more wxCStrData operators
2940// ----------------------------------------------------------------------------
2941
2942// we need to define those to allow "size_t pos = p - s.c_str()" where p is
2943// some pointer into the string
2944inline size_t operator-(const char *p, const wxCStrData& cs)
2945{
2946 return p - cs.AsChar();
2947}
2948
2949inline size_t operator-(const wchar_t *p, const wxCStrData& cs)
2950{
2951 return p - cs.AsWChar();
2952}
2953
414b7b40
VZ
2954// ----------------------------------------------------------------------------
2955// implementation of wx[W]CharBuffer inline methods using wxCStrData
2956// ----------------------------------------------------------------------------
2957
11aac4ba
VS
2958// FIXME-UTF8: move this to buffer.h
2959inline wxCharBuffer::wxCharBuffer(const wxCStrData& cstr)
681e4412 2960 : wxCharTypeBufferBase(cstr.AsCharBuf())
11aac4ba
VS
2961{
2962}
2963
2964inline wxWCharBuffer::wxWCharBuffer(const wxCStrData& cstr)
681e4412 2965 : wxCharTypeBufferBase(cstr.AsWCharBuf())
414b7b40
VZ
2966{
2967}
2968
e3e8e3c0
VS
2969#if WXWIN_COMPATIBILITY_2_8
2970 // lot of code out there doesn't explicitly include wx/wxchar.h, but uses
2971 // CRT wrappers that are now declared in wx/wxcrt.h and wx/wxcrtvararg.h,
2972 // so let's include this header now that wxString is defined and it's safe
2973 // to do it:
2974 #include "wx/wxchar.h"
2975#endif
2976
11aac4ba 2977#endif // _WX_WXSTRING_H_