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