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