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