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