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