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