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