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