]> git.saurik.com Git - wxWidgets.git/blame - include/wx/string.h
converting temporary wxString to const wxChar* and storing that doesn't work (and...
[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)
467175ab 1346 { m_impl = wxStringOperations::EncodeChar(ch); return *this; }
c9f78968 1347 wxString& operator=(wxUniCharRef ch)
8f93a29f 1348 { return operator=((wxUniChar)ch); }
c9f78968 1349 wxString& operator=(char ch)
8f93a29f 1350 { return operator=(wxUniChar(ch)); }
50e02008
VS
1351 wxString& operator=(unsigned char ch)
1352 { return operator=(wxUniChar(ch)); }
c9f78968 1353 wxString& operator=(wchar_t ch)
8f93a29f 1354 { return operator=(wxUniChar(ch)); }
c57e3bd5
RN
1355 // from a C string - STL probably will crash on NULL,
1356 // so we need to compensate in that case
992527a5 1357#if wxUSE_STL_BASED_WXSTRING
04abe4bc
VS
1358 wxString& operator=(const char *psz)
1359 { if (psz) m_impl = ImplStr(psz); else Clear(); return *this; }
1360 wxString& operator=(const wchar_t *pwz)
1361 { if (pwz) m_impl = ImplStr(pwz); else Clear(); return *this; }
c57e3bd5 1362#else
04abe4bc
VS
1363 wxString& operator=(const char *psz)
1364 { m_impl = ImplStr(psz); return *this; }
1365 wxString& operator=(const wchar_t *pwz)
1366 { m_impl = ImplStr(pwz); return *this; }
c57e3bd5 1367#endif
04abe4bc
VS
1368 wxString& operator=(const unsigned char *psz)
1369 { return operator=((const char*)psz); }
c57e3bd5 1370
111bb7f2 1371 // from wxWCharBuffer
8f93a29f 1372 wxString& operator=(const wxWCharBuffer& s)
04abe4bc 1373 { return operator=(s.data()); } // FIXME-UTF8: fix for embedded NULs
111bb7f2 1374 // from wxCharBuffer
04abe4bc
VS
1375 wxString& operator=(const wxCharBuffer& s)
1376 { return operator=(s.data()); } // FIXME-UTF8: fix for embedded NULs
3c67202d
VZ
1377
1378 // string concatenation
1379 // in place concatenation
1380 /*
1381 Concatenate and return the result. Note that the left to right
1382 associativity of << allows to write things like "str << str1 << str2
1383 << ..." (unlike with +=)
1384 */
1385 // string += string
dd1eaa89
VZ
1386 wxString& operator<<(const wxString& s)
1387 {
8f93a29f
VS
1388#if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1389 wxASSERT_MSG( s.IsValid(),
09443a26 1390 _T("did you forget to call UngetWriteBuf()?") );
e87b7833 1391#endif
dd1eaa89 1392
e87b7833 1393 append(s);
dd1eaa89
VZ
1394 return *this;
1395 }
3c67202d 1396 // string += C string
8f93a29f 1397 wxString& operator<<(const char *psz)
c9f78968 1398 { append(psz); return *this; }
8f93a29f
VS
1399 wxString& operator<<(const wchar_t *pwz)
1400 { append(pwz); return *this; }
c9f78968 1401 wxString& operator<<(const wxCStrData& psz)
8f93a29f 1402 { append(psz.AsString()); return *this; }
3c67202d 1403 // string += char
c9f78968
VS
1404 wxString& operator<<(wxUniChar ch) { append(1, ch); return *this; }
1405 wxString& operator<<(wxUniCharRef ch) { append(1, ch); return *this; }
1406 wxString& operator<<(char ch) { append(1, ch); return *this; }
50e02008 1407 wxString& operator<<(unsigned char ch) { append(1, ch); return *this; }
c9f78968 1408 wxString& operator<<(wchar_t ch) { append(1, ch); return *this; }
2bb67b80
OK
1409
1410 // string += buffer (i.e. from wxGetString)
09443a26 1411 wxString& operator<<(const wxWCharBuffer& s)
d7330233 1412 { return operator<<((const wchar_t *)s); }
09443a26 1413 wxString& operator<<(const wxCharBuffer& s)
d7330233 1414 { return operator<<((const char *)s); }
d7330233 1415
3c67202d 1416 // string += C string
09443a26
VZ
1417 wxString& Append(const wxString& s)
1418 {
5a8231ef
WS
1419 // test for empty() to share the string if possible
1420 if ( empty() )
09443a26
VZ
1421 *this = s;
1422 else
e87b7833 1423 append(s);
09443a26
VZ
1424 return *this;
1425 }
8f93a29f 1426 wxString& Append(const char* psz)
e87b7833 1427 { append(psz); return *this; }
8f93a29f
VS
1428 wxString& Append(const wchar_t* pwz)
1429 { append(pwz); return *this; }
d3cefe87
VS
1430 wxString& Append(const wxCStrData& psz)
1431 { append(psz); return *this; }
1432 wxString& Append(const wxCharBuffer& psz)
1433 { append(psz); return *this; }
1434 wxString& Append(const wxWCharBuffer& psz)
1435 { append(psz); return *this; }
3c67202d 1436 // append count copies of given character
c9f78968
VS
1437 wxString& Append(wxUniChar ch, size_t count = 1u)
1438 { append(count, ch); return *this; }
1439 wxString& Append(wxUniCharRef ch, size_t count = 1u)
1440 { append(count, ch); return *this; }
1441 wxString& Append(char ch, size_t count = 1u)
1442 { append(count, ch); return *this; }
50e02008
VS
1443 wxString& Append(unsigned char ch, size_t count = 1u)
1444 { append(count, ch); return *this; }
c9f78968 1445 wxString& Append(wchar_t ch, size_t count = 1u)
e87b7833 1446 { append(count, ch); return *this; }
8f93a29f 1447 wxString& Append(const char* psz, size_t nLen)
e87b7833 1448 { append(psz, nLen); return *this; }
8f93a29f
VS
1449 wxString& Append(const wchar_t* pwz, size_t nLen)
1450 { append(pwz, nLen); return *this; }
3c67202d
VZ
1451
1452 // prepend a string, return the string itself
1453 wxString& Prepend(const wxString& str)
1454 { *this = str + *this; return *this; }
1455
1456 // non-destructive concatenation
1fc8cfbd
VZ
1457 // two strings
1458 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string1,
1459 const wxString& string2);
1460 // string with a single char
c9f78968 1461 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch);
1fc8cfbd 1462 // char with a string
c9f78968 1463 friend wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string);
1fc8cfbd
VZ
1464 // string with C string
1465 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string,
8f93a29f
VS
1466 const char *psz);
1467 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string,
1468 const wchar_t *pwz);
1fc8cfbd 1469 // C string with string
8f93a29f
VS
1470 friend wxString WXDLLIMPEXP_BASE operator+(const char *psz,
1471 const wxString& string);
1472 friend wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz,
1fc8cfbd 1473 const wxString& string);
3c67202d
VZ
1474
1475 // stream-like functions
1476 // insert an int into string
3ce65f6c
VZ
1477 wxString& operator<<(int i)
1478 { return (*this) << Format(_T("%d"), i); }
1479 // insert an unsigned int into string
1480 wxString& operator<<(unsigned int ui)
1481 { return (*this) << Format(_T("%u"), ui); }
1482 // insert a long into string
1483 wxString& operator<<(long l)
1484 { return (*this) << Format(_T("%ld"), l); }
1485 // insert an unsigned long into string
1486 wxString& operator<<(unsigned long ul)
1487 { return (*this) << Format(_T("%lu"), ul); }
3fc1adbd
MW
1488#if defined wxLongLong_t && !defined wxLongLongIsLong
1489 // insert a long long if they exist and aren't longs
1490 wxString& operator<<(wxLongLong_t ll)
b7859132
MW
1491 {
1492 const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("d");
1493 return (*this) << Format(fmt, ll);
1494 }
3fc1adbd
MW
1495 // insert an unsigned long long
1496 wxString& operator<<(wxULongLong_t ull)
b7859132
MW
1497 {
1498 const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("u");
1499 return (*this) << Format(fmt , ull);
1500 }
3fc1adbd 1501#endif
3c67202d 1502 // insert a float into string
3ce65f6c
VZ
1503 wxString& operator<<(float f)
1504 { return (*this) << Format(_T("%f"), f); }
3c67202d 1505 // insert a double into string
3ce65f6c
VZ
1506 wxString& operator<<(double d)
1507 { return (*this) << Format(_T("%g"), d); }
c84c52de 1508
3c67202d 1509 // string comparison
30b21f9a 1510 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
8f93a29f
VS
1511 int Cmp(const char *psz) const
1512 { return compare(psz); }
1513 int Cmp(const wchar_t *pwz) const
1514 { return compare(pwz); }
1515 int Cmp(const wxString& s) const
1516 { return compare(s); }
d3cefe87
VS
1517 int Cmp(const wxCStrData& s) const
1518 { return compare(s); }
1519 int Cmp(const wxCharBuffer& s) const
1520 { return compare(s); }
1521 int Cmp(const wxWCharBuffer& s) const
1522 { return compare(s); }
3c67202d 1523 // same as Cmp() but not case-sensitive
dcb68102 1524 int CmpNoCase(const wxString& s) const;
2712e317 1525
3c67202d
VZ
1526 // test for the string equality, either considering case or not
1527 // (if compareWithCase then the case matters)
11aac4ba 1528 bool IsSameAs(const wxString& str, bool compareWithCase = true) const
2712e317
VS
1529 {
1530#if !wxUSE_UNICODE_UTF8
1531 // in UTF-8 build, length() is O(n) and doing this would be _slower_
1532 if ( length() != str.length() )
1533 return false;
1534#endif
1535 return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0;
1536 }
11aac4ba
VS
1537 bool IsSameAs(const char *str, bool compareWithCase = true) const
1538 { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; }
1539 bool IsSameAs(const wchar_t *str, bool compareWithCase = true) const
1540 { return (compareWithCase ? Cmp(str) : CmpNoCase(str)) == 0; }
2712e317 1541
d3cefe87
VS
1542 bool IsSameAs(const wxCStrData& str, bool compareWithCase = true) const
1543 { return IsSameAs(str.AsString(), compareWithCase); }
1544 bool IsSameAs(const wxCharBuffer& str, bool compareWithCase = true) const
1545 { return IsSameAs(str.data(), compareWithCase); }
1546 bool IsSameAs(const wxWCharBuffer& str, bool compareWithCase = true) const
1547 { return IsSameAs(str.data(), compareWithCase); }
20aa779e 1548 // comparison with a single character: returns true if equal
52de37c7 1549 bool IsSameAs(wxUniChar c, bool compareWithCase = true) const;
11aac4ba
VS
1550 // FIXME-UTF8: remove these overloads
1551 bool IsSameAs(wxUniCharRef c, bool compareWithCase = true) const
1552 { return IsSameAs(wxUniChar(c), compareWithCase); }
1553 bool IsSameAs(char c, bool compareWithCase = true) const
1554 { return IsSameAs(wxUniChar(c), compareWithCase); }
1555 bool IsSameAs(unsigned char c, bool compareWithCase = true) const
1556 { return IsSameAs(wxUniChar(c), compareWithCase); }
1557 bool IsSameAs(wchar_t c, bool compareWithCase = true) const
1558 { return IsSameAs(wxUniChar(c), compareWithCase); }
1559 bool IsSameAs(int c, bool compareWithCase = true) const
1560 { return IsSameAs(wxUniChar(c), compareWithCase); }
3c67202d
VZ
1561
1562 // simple sub-string extraction
1563 // return substring starting at nFirst of length nCount (or till the end
1564 // if nCount = default value)
e87b7833 1565 wxString Mid(size_t nFirst, size_t nCount = npos) const;
3c67202d 1566
f6bcfd97 1567 // operator version of Mid()
3c67202d
VZ
1568 wxString operator()(size_t start, size_t len) const
1569 { return Mid(start, len); }
1570
3affcd07
VZ
1571 // check if the string starts with the given prefix and return the rest
1572 // of the string in the provided pointer if it is not NULL; otherwise
1573 // return false
c5e7a7d7 1574 bool StartsWith(const wxString& prefix, wxString *rest = NULL) const;
3affcd07
VZ
1575 // check if the string ends with the given suffix and return the
1576 // beginning of the string before the suffix in the provided pointer if
1577 // it is not NULL; otherwise return false
c5e7a7d7 1578 bool EndsWith(const wxString& suffix, wxString *rest = NULL) const;
f6bcfd97 1579
3c67202d 1580 // get first nCount characters
c801d85f 1581 wxString Left(size_t nCount) const;
3c67202d 1582 // get last nCount characters
c801d85f 1583 wxString Right(size_t nCount) const;
c0881dc3 1584 // get all characters before the first occurance of ch
3c67202d 1585 // (returns the whole string if ch not found)
c9f78968 1586 wxString BeforeFirst(wxUniChar ch) const;
3c67202d
VZ
1587 // get all characters before the last occurence of ch
1588 // (returns empty string if ch not found)
c9f78968 1589 wxString BeforeLast(wxUniChar ch) const;
3c67202d
VZ
1590 // get all characters after the first occurence of ch
1591 // (returns empty string if ch not found)
c9f78968 1592 wxString AfterFirst(wxUniChar ch) const;
3c67202d
VZ
1593 // get all characters after the last occurence of ch
1594 // (returns the whole string if ch not found)
c9f78968 1595 wxString AfterLast(wxUniChar ch) const;
3c67202d
VZ
1596
1597 // for compatibility only, use more explicitly named functions above
c9f78968
VS
1598 wxString Before(wxUniChar ch) const { return BeforeLast(ch); }
1599 wxString After(wxUniChar ch) const { return AfterFirst(ch); }
3c67202d
VZ
1600
1601 // case conversion
c84c52de 1602 // convert to upper case in place, return the string itself
c801d85f 1603 wxString& MakeUpper();
c84c52de 1604 // convert to upper case, return the copy of the string
03ab016d
JS
1605 // Here's something to remember: BC++ doesn't like returns in inlines.
1606 wxString Upper() const ;
c84c52de 1607 // convert to lower case in place, return the string itself
c801d85f 1608 wxString& MakeLower();
c84c52de 1609 // convert to lower case, return the copy of the string
03ab016d 1610 wxString Lower() const ;
c801d85f 1611
3c67202d
VZ
1612 // trimming/padding whitespace (either side) and truncating
1613 // remove spaces from left or from right (default) side
d775fa82 1614 wxString& Trim(bool bFromRight = true);
3c67202d 1615 // add nCount copies chPad in the beginning or at the end (default)
c9f78968 1616 wxString& Pad(size_t nCount, wxUniChar chPad = wxT(' '), bool bFromRight = true);
dd1eaa89 1617
3c67202d
VZ
1618 // searching and replacing
1619 // searching (return starting index, or -1 if not found)
c9f78968 1620 int Find(wxUniChar ch, bool bFromEnd = false) const; // like strchr/strrchr
8a540c88
VS
1621 int Find(wxUniCharRef ch, bool bFromEnd = false) const
1622 { return Find(wxUniChar(ch), bFromEnd); }
1623 int Find(char ch, bool bFromEnd = false) const
1624 { return Find(wxUniChar(ch), bFromEnd); }
1625 int Find(unsigned char ch, bool bFromEnd = false) const
1626 { return Find(wxUniChar(ch), bFromEnd); }
1627 int Find(wchar_t ch, bool bFromEnd = false) const
1628 { return Find(wxUniChar(ch), bFromEnd); }
3c67202d 1629 // searching (return starting index, or -1 if not found)
8a540c88
VS
1630 int Find(const wxString& sub) const // like strstr
1631 {
1632 size_type idx = find(sub);
1633 return (idx == npos) ? wxNOT_FOUND : (int)idx;
1634 }
1635 int Find(const char *sub) const // like strstr
1636 {
1637 size_type idx = find(sub);
1638 return (idx == npos) ? wxNOT_FOUND : (int)idx;
1639 }
1640 int Find(const wchar_t *sub) const // like strstr
1641 {
1642 size_type idx = find(sub);
1643 return (idx == npos) ? wxNOT_FOUND : (int)idx;
1644 }
1645
d3cefe87
VS
1646 int Find(const wxCStrData& sub) const
1647 { return Find(sub.AsString()); }
1648 int Find(const wxCharBuffer& sub) const
1649 { return Find(sub.data()); }
1650 int Find(const wxWCharBuffer& sub) const
1651 { return Find(sub.data()); }
1652
3c67202d
VZ
1653 // replace first (or all of bReplaceAll) occurences of substring with
1654 // another string, returns the number of replacements made
8a540c88
VS
1655 size_t Replace(const wxString& strOld,
1656 const wxString& strNew,
d775fa82 1657 bool bReplaceAll = true);
3c67202d
VZ
1658
1659 // check if the string contents matches a mask containing '*' and '?'
8a540c88 1660 bool Matches(const wxString& mask) const;
c801d85f 1661
d775fa82 1662 // conversion to numbers: all functions return true only if the whole
538f35cc
VZ
1663 // string is a number and put the value of this number into the pointer
1664 // provided, the base is the numeric base in which the conversion should be
1665 // done and must be comprised between 2 and 36 or be 0 in which case the
1666 // standard C rules apply (leading '0' => octal, "0x" => hex)
cd0b1709 1667 // convert to a signed integer
538f35cc 1668 bool ToLong(long *val, int base = 10) const;
cd0b1709 1669 // convert to an unsigned integer
538f35cc 1670 bool ToULong(unsigned long *val, int base = 10) const;
d6718dd1
VZ
1671 // convert to wxLongLong
1672#if defined(wxLongLong_t)
1673 bool ToLongLong(wxLongLong_t *val, int base = 10) const;
1674 // convert to wxULongLong
1675 bool ToULongLong(wxULongLong_t *val, int base = 10) const;
1676#endif // wxLongLong_t
cd0b1709
VZ
1677 // convert to a double
1678 bool ToDouble(double *val) const;
1679
d6718dd1 1680
c9f78968 1681#ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
90e572f1 1682 // formatted input/output
3c67202d 1683 // as sprintf(), returns the number of characters written or < 0 on error
7357f981 1684 // (take 'this' into account in attribute parameter count)
2523e9b7 1685 // int Printf(const wxString& format, ...);
1528e0b8 1686 WX_DEFINE_VARARG_FUNC(int, Printf, 1, (const wxFormatString&),
d1f6e2cf 1687 DoPrintfWchar, DoPrintfUtf8)
2523e9b7 1688#ifdef __WATCOMC__
59a14f69
VS
1689 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1690 WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wxString&),
1691 (wxFormatString(f1)));
1692 WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wxCStrData&),
1693 (wxFormatString(f1)));
1694 WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const char*),
1695 (wxFormatString(f1)));
1696 WX_VARARG_WATCOM_WORKAROUND(int, Printf, 1, (const wchar_t*),
1697 (wxFormatString(f1)));
2523e9b7 1698#endif
c9f78968 1699#endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN
3c67202d 1700 // as vprintf(), returns the number of characters written or < 0 on error
c9f78968 1701 int PrintfV(const wxString& format, va_list argptr);
dd1eaa89 1702
c9f78968 1703#ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
341e7d28 1704 // returns the string containing the result of Printf() to it
2523e9b7 1705 // static wxString Format(const wxString& format, ...) ATTRIBUTE_PRINTF_1;
1528e0b8 1706 WX_DEFINE_VARARG_FUNC(static wxString, Format, 1, (const wxFormatString&),
d1f6e2cf 1707 DoFormatWchar, DoFormatUtf8)
2523e9b7
VS
1708#ifdef __WATCOMC__
1709 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
59a14f69
VS
1710 WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wxString&),
1711 (wxFormatString(f1)));
1712 WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wxCStrData&),
1713 (wxFormatString(f1)));
1714 WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const char*),
1715 (wxFormatString(f1)));
1716 WX_VARARG_WATCOM_WORKAROUND(static wxString, Format, 1, (const wchar_t*),
1717 (wxFormatString(f1)));
2523e9b7 1718#endif
c9f78968 1719#endif
341e7d28 1720 // the same as above, but takes a va_list
c9f78968 1721 static wxString FormatV(const wxString& format, va_list argptr);
341e7d28 1722
3c67202d
VZ
1723 // raw access to string memory
1724 // ensure that string has space for at least nLen characters
dd1eaa89 1725 // only works if the data of this string is not shared
e87b7833 1726 bool Alloc(size_t nLen) { reserve(nLen); /*return capacity() >= nLen;*/ return true; }
3c67202d 1727 // minimize the string's memory
dd1eaa89 1728 // only works if the data of this string is not shared
b1801e0e 1729 bool Shrink();
8f93a29f 1730#if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
d8a4b666
VS
1731 // These are deprecated, use wxStringBuffer or wxStringBufferLength instead
1732 //
3c67202d
VZ
1733 // get writable buffer of at least nLen bytes. Unget() *must* be called
1734 // a.s.a.p. to put string back in a reasonable state!
c87a0bc8 1735 wxDEPRECATED( wxStringCharType *GetWriteBuf(size_t nLen) );
3c67202d 1736 // call this immediately after GetWriteBuf() has been used
d8a4b666
VS
1737 wxDEPRECATED( void UngetWriteBuf() );
1738 wxDEPRECATED( void UngetWriteBuf(size_t nLen) );
8f93a29f 1739#endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && wxUSE_UNICODE_UTF8
c801d85f 1740
77ffb593 1741 // wxWidgets version 1 compatibility functions
3c67202d
VZ
1742
1743 // use Mid()
1744 wxString SubString(size_t from, size_t to) const
1745 { return Mid(from, (to - from + 1)); }
1746 // values for second parameter of CompareTo function
c801d85f 1747 enum caseCompare {exact, ignoreCase};
3c67202d 1748 // values for first parameter of Strip function
c801d85f 1749 enum stripType {leading = 0x1, trailing = 0x2, both = 0x3};
8870c26e 1750
c9f78968 1751#ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
7357f981
GD
1752 // use Printf()
1753 // (take 'this' into account in attribute parameter count)
2523e9b7 1754 // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2;
1528e0b8 1755 WX_DEFINE_VARARG_FUNC(int, sprintf, 1, (const wxFormatString&),
d1f6e2cf 1756 DoPrintfWchar, DoPrintfUtf8)
2523e9b7
VS
1757#ifdef __WATCOMC__
1758 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
59a14f69
VS
1759 WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wxString&),
1760 (wxFormatString(f1)));
1761 WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wxCStrData&),
1762 (wxFormatString(f1)));
1763 WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const char*),
1764 (wxFormatString(f1)));
1765 WX_VARARG_WATCOM_WORKAROUND(int, sprintf, 1, (const wchar_t*),
1766 (wxFormatString(f1)));
2523e9b7 1767#endif
c9f78968 1768#endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
c801d85f 1769
3c67202d 1770 // use Cmp()
2bb67b80 1771 inline int CompareTo(const wxChar* psz, caseCompare cmp = exact) const
6b95b20d 1772 { return cmp == exact ? Cmp(psz) : CmpNoCase(psz); }
c801d85f 1773
8f93a29f 1774 // use length()
e87b7833 1775 size_t Length() const { return length(); }
3c67202d 1776 // Count the number of characters
c9f78968 1777 int Freq(wxUniChar ch) const;
3c67202d 1778 // use MakeLower
c801d85f 1779 void LowerCase() { MakeLower(); }
3c67202d 1780 // use MakeUpper
c801d85f 1781 void UpperCase() { MakeUpper(); }
3c67202d 1782 // use Trim except that it doesn't change this string
c801d85f
KB
1783 wxString Strip(stripType w = trailing) const;
1784
3c67202d 1785 // use Find (more general variants not yet supported)
2bb67b80 1786 size_t Index(const wxChar* psz) const { return Find(psz); }
c9f78968 1787 size_t Index(wxUniChar ch) const { return Find(ch); }
3c67202d 1788 // use Truncate
c801d85f 1789 wxString& Remove(size_t pos) { return Truncate(pos); }
e87b7833 1790 wxString& RemoveLast(size_t n = 1) { return Truncate(length() - n); }
c801d85f 1791
e87b7833
MB
1792 wxString& Remove(size_t nStart, size_t nLen)
1793 { return (wxString&)erase( nStart, nLen ); }
dd1eaa89 1794
3c67202d 1795 // use Find()
8f93a29f 1796 int First( wxUniChar ch ) const { return Find(ch); }
8a540c88 1797 int First( wxUniCharRef ch ) const { return Find(ch); }
c9f78968 1798 int First( char ch ) const { return Find(ch); }
50e02008 1799 int First( unsigned char ch ) const { return Find(ch); }
c9f78968 1800 int First( wchar_t ch ) const { return Find(ch); }
8a540c88 1801 int First( const wxString& str ) const { return Find(str); }
8f93a29f 1802 int Last( wxUniChar ch ) const { return Find(ch, true); }
d775fa82 1803 bool Contains(const wxString& str) const { return Find(str) != wxNOT_FOUND; }
c801d85f 1804
5a8231ef
WS
1805 // use empty()
1806 bool IsNull() const { return empty(); }
c801d85f 1807
3c67202d 1808 // std::string compatibility functions
dd1eaa89 1809
3c67202d
VZ
1810 // take nLen chars starting at nPos
1811 wxString(const wxString& str, size_t nPos, size_t nLen)
1545d942 1812 { assign(str, nPos, nLen); }
f2a1b1bd 1813 // take all characters from first to last
e87b7833 1814 wxString(const_iterator first, const_iterator last)
cf9a878b 1815 : m_impl(first.impl(), last.impl()) { }
67cff9dc
VZ
1816#if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1817 // the 2 overloads below are for compatibility with the existing code using
1818 // pointers instead of iterators
f2a1b1bd
VZ
1819 wxString(const char *first, const char *last)
1820 {
1821 SubstrBufFromMB str(ImplStr(first, last - first));
1822 m_impl.assign(str.data, str.len);
1823 }
1824 wxString(const wchar_t *first, const wchar_t *last)
1825 {
1826 SubstrBufFromWC str(ImplStr(first, last - first));
1827 m_impl.assign(str.data, str.len);
1828 }
67cff9dc
VZ
1829 // and this one is needed to compile code adding offsets to c_str() result
1830 wxString(const wxCStrData& first, const wxCStrData& last)
cf9a878b
VS
1831 : m_impl(CreateConstIterator(first).impl(),
1832 CreateConstIterator(last).impl())
67cff9dc
VZ
1833 {
1834 wxASSERT_MSG( first.m_str == last.m_str,
1835 _T("pointers must be into the same string") );
1836 }
1837#endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
5460b9e3 1838
3c67202d 1839 // lib.string.modifiers
3c67202d
VZ
1840 // append elements str[pos], ..., str[pos+n]
1841 wxString& append(const wxString& str, size_t pos, size_t n)
8f93a29f
VS
1842 {
1843 size_t from, len;
04848a06 1844 str.PosLenToImpl(pos, n, &from, &len);
8f93a29f
VS
1845 m_impl.append(str.m_impl, from, len);
1846 return *this;
1847 }
e87b7833
MB
1848 // append a string
1849 wxString& append(const wxString& str)
8f93a29f 1850 { m_impl.append(str.m_impl); return *this; }
3c67202d 1851 // append first n (or all if n == npos) characters of sz
8f93a29f
VS
1852 wxString& append(const char *sz)
1853 { m_impl.append(ImplStr(sz)); return *this; }
1854 wxString& append(const wchar_t *sz)
1855 { m_impl.append(ImplStr(sz)); return *this; }
1856 wxString& append(const char *sz, size_t n)
1857 {
1858 SubstrBufFromMB str(ImplStr(sz, n));
1859 m_impl.append(str.data, str.len);
1860 return *this;
1861 }
1862 wxString& append(const wchar_t *sz, size_t n)
1863 {
1864 SubstrBufFromWC str(ImplStr(sz, n));
1865 m_impl.append(str.data, str.len);
1866 return *this;
1867 }
d3cefe87
VS
1868
1869 wxString& append(const wxCStrData& str)
1870 { return append(str.AsString()); }
1871 wxString& append(const wxCharBuffer& str)
1872 { return append(str.data()); }
1873 wxString& append(const wxWCharBuffer& str)
1874 { return append(str.data()); }
1875
3c67202d 1876 // append n copies of ch
c9f78968 1877 wxString& append(size_t n, wxUniChar ch)
8f93a29f
VS
1878 {
1879#if wxUSE_UNICODE_UTF8
1880 if ( !ch.IsAscii() )
467175ab 1881 m_impl.append(wxStringOperations::EncodeNChars(n, ch));
8f93a29f
VS
1882 else
1883#endif
1884 m_impl.append(n, (wxStringCharType)ch);
1885 return *this;
1886 }
e87b7833
MB
1887 // append from first to last
1888 wxString& append(const_iterator first, const_iterator last)
cf9a878b 1889 { m_impl.append(first.impl(), last.impl()); return *this; }
67cff9dc 1890#if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
f2a1b1bd
VZ
1891 wxString& append(const char *first, const char *last)
1892 { return append(first, last - first); }
1893 wxString& append(const wchar_t *first, const wchar_t *last)
1894 { return append(first, last - first); }
67cff9dc
VZ
1895 wxString& append(const wxCStrData& first, const wxCStrData& last)
1896 { return append(CreateConstIterator(first), CreateConstIterator(last)); }
1897#endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
3c67202d
VZ
1898
1899 // same as `this_string = str'
735d1db6 1900 wxString& assign(const wxString& str)
8f93a29f 1901 { m_impl = str.m_impl; return *this; }
11aac4ba
VS
1902 wxString& assign(const wxString& str, size_t len)
1903 {
1904 m_impl.assign(str.m_impl, 0, str.LenToImpl(len));
1905 return *this;
1906 }
3c67202d
VZ
1907 // same as ` = str[pos..pos + n]
1908 wxString& assign(const wxString& str, size_t pos, size_t n)
8f93a29f
VS
1909 {
1910 size_t from, len;
04848a06 1911 str.PosLenToImpl(pos, n, &from, &len);
8f93a29f
VS
1912 m_impl.assign(str.m_impl, from, len);
1913 return *this;
1914 }
3c67202d 1915 // same as `= first n (or all if n == npos) characters of sz'
8f93a29f
VS
1916 wxString& assign(const char *sz)
1917 { m_impl.assign(ImplStr(sz)); return *this; }
1918 wxString& assign(const wchar_t *sz)
1919 { m_impl.assign(ImplStr(sz)); return *this; }
1920 wxString& assign(const char *sz, size_t n)
1921 {
1922 SubstrBufFromMB str(ImplStr(sz, n));
1923 m_impl.assign(str.data, str.len);
1924 return *this;
1925 }
1926 wxString& assign(const wchar_t *sz, size_t n)
1927 {
1928 SubstrBufFromWC str(ImplStr(sz, n));
1929 m_impl.assign(str.data, str.len);
1930 return *this;
1931 }
d3cefe87
VS
1932
1933 wxString& assign(const wxCStrData& str)
1934 { return assign(str.AsString()); }
1935 wxString& assign(const wxCharBuffer& str)
1936 { return assign(str.data()); }
1937 wxString& assign(const wxWCharBuffer& str)
1938 { return assign(str.data()); }
1939 wxString& assign(const wxCStrData& str, size_t len)
1940 { return assign(str.AsString(), len); }
1941 wxString& assign(const wxCharBuffer& str, size_t len)
1942 { return assign(str.data(), len); }
1943 wxString& assign(const wxWCharBuffer& str, size_t len)
1944 { return assign(str.data(), len); }
1945
3c67202d 1946 // same as `= n copies of ch'
c9f78968 1947 wxString& assign(size_t n, wxUniChar ch)
8f93a29f
VS
1948 {
1949#if wxUSE_UNICODE_UTF8
1950 if ( !ch.IsAscii() )
467175ab 1951 m_impl.assign(wxStringOperations::EncodeNChars(n, ch));
8f93a29f
VS
1952 else
1953#endif
1954 m_impl.assign(n, (wxStringCharType)ch);
1955 return *this;
1956 }
11aac4ba
VS
1957
1958 wxString& assign(size_t n, wxUniCharRef ch)
1959 { return assign(n, wxUniChar(ch)); }
1960 wxString& assign(size_t n, char ch)
1961 { return assign(n, wxUniChar(ch)); }
1962 wxString& assign(size_t n, unsigned char ch)
1963 { return assign(n, wxUniChar(ch)); }
1964 wxString& assign(size_t n, wchar_t ch)
1965 { return assign(n, wxUniChar(ch)); }
1966
e87b7833
MB
1967 // assign from first to last
1968 wxString& assign(const_iterator first, const_iterator last)
cf9a878b 1969 { m_impl.assign(first.impl(), last.impl()); return *this; }
67cff9dc 1970#if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
f2a1b1bd
VZ
1971 wxString& assign(const char *first, const char *last)
1972 { return assign(first, last - first); }
1973 wxString& assign(const wchar_t *first, const wchar_t *last)
1974 { return assign(first, last - first); }
67cff9dc
VZ
1975 wxString& assign(const wxCStrData& first, const wxCStrData& last)
1976 { return assign(CreateConstIterator(first), CreateConstIterator(last)); }
1977#endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
e87b7833
MB
1978
1979 // string comparison
8f93a29f 1980 int compare(const wxString& str) const;
d3cefe87
VS
1981 int compare(const char* sz) const;
1982 int compare(const wchar_t* sz) const;
1983 int compare(const wxCStrData& str) const
1984 { return compare(str.AsString()); }
1985 int compare(const wxCharBuffer& str) const
1986 { return compare(str.data()); }
1987 int compare(const wxWCharBuffer& str) const
1988 { return compare(str.data()); }
e87b7833 1989 // comparison with a substring
8f93a29f 1990 int compare(size_t nStart, size_t nLen, const wxString& str) const;
e87b7833
MB
1991 // comparison of 2 substrings
1992 int compare(size_t nStart, size_t nLen,
8f93a29f 1993 const wxString& str, size_t nStart2, size_t nLen2) const;
e87b7833
MB
1994 // substring comparison with first nCount characters of sz
1995 int compare(size_t nStart, size_t nLen,
8f93a29f
VS
1996 const char* sz, size_t nCount = npos) const;
1997 int compare(size_t nStart, size_t nLen,
1998 const wchar_t* sz, size_t nCount = npos) const;
3c67202d
VZ
1999
2000 // insert another string
e87b7833 2001 wxString& insert(size_t nPos, const wxString& str)
8f93a29f 2002 { insert(begin() + nPos, str.begin(), str.end()); return *this; }
3c67202d
VZ
2003 // insert n chars of str starting at nStart (in str)
2004 wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n)
8f93a29f
VS
2005 {
2006 size_t from, len;
04848a06 2007 str.PosLenToImpl(nStart, n, &from, &len);
8f93a29f
VS
2008 m_impl.insert(PosToImpl(nPos), str.m_impl, from, len);
2009 return *this;
2010 }
3c67202d 2011 // insert first n (or all if n == npos) characters of sz
8f93a29f
VS
2012 wxString& insert(size_t nPos, const char *sz)
2013 { m_impl.insert(PosToImpl(nPos), ImplStr(sz)); return *this; }
2014 wxString& insert(size_t nPos, const wchar_t *sz)
2015 { m_impl.insert(PosToImpl(nPos), ImplStr(sz)); return *this; }
2016 wxString& insert(size_t nPos, const char *sz, size_t n)
2017 {
2018 SubstrBufFromMB str(ImplStr(sz, n));
2019 m_impl.insert(PosToImpl(nPos), str.data, str.len);
2020 return *this;
2021 }
2022 wxString& insert(size_t nPos, const wchar_t *sz, size_t n)
2023 {
2024 SubstrBufFromWC str(ImplStr(sz, n));
2025 m_impl.insert(PosToImpl(nPos), str.data, str.len);
2026 return *this;
2027 }
3c67202d 2028 // insert n copies of ch
c9f78968 2029 wxString& insert(size_t nPos, size_t n, wxUniChar ch)
8f93a29f
VS
2030 {
2031#if wxUSE_UNICODE_UTF8
2032 if ( !ch.IsAscii() )
467175ab 2033 m_impl.insert(PosToImpl(nPos), wxStringOperations::EncodeNChars(n, ch));
8f93a29f
VS
2034 else
2035#endif
81727065 2036 m_impl.insert(PosToImpl(nPos), n, (wxStringCharType)ch);
8f93a29f
VS
2037 return *this;
2038 }
c9f78968 2039 iterator insert(iterator it, wxUniChar ch)
81727065
VS
2040 {
2041#if wxUSE_UNICODE_UTF8
2042 if ( !ch.IsAscii() )
2043 {
2044 size_t pos = IterToImplPos(it);
467175ab 2045 m_impl.insert(pos, wxStringOperations::EncodeChar(ch));
81727065
VS
2046 return iterator(this, m_impl.begin() + pos);
2047 }
2048 else
2049#endif
cf9a878b 2050 return iterator(this, m_impl.insert(it.impl(), (wxStringCharType)ch));
81727065 2051 }
e87b7833 2052 void insert(iterator it, const_iterator first, const_iterator last)
cf9a878b 2053 { m_impl.insert(it.impl(), first.impl(), last.impl()); }
67cff9dc 2054#if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
f2a1b1bd
VZ
2055 void insert(iterator it, const char *first, const char *last)
2056 { insert(it - begin(), first, last - first); }
67cff9dc 2057 void insert(iterator it, const wchar_t *first, const wchar_t *last)
f2a1b1bd 2058 { insert(it - begin(), first, last - first); }
67cff9dc
VZ
2059 void insert(iterator it, const wxCStrData& first, const wxCStrData& last)
2060 { insert(it, CreateConstIterator(first), CreateConstIterator(last)); }
2061#endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2062
c9f78968 2063 void insert(iterator it, size_type n, wxUniChar ch)
8f93a29f
VS
2064 {
2065#if wxUSE_UNICODE_UTF8
2066 if ( !ch.IsAscii() )
467175ab 2067 m_impl.insert(IterToImplPos(it), wxStringOperations::EncodeNChars(n, ch));
8f93a29f
VS
2068 else
2069#endif
cf9a878b 2070 m_impl.insert(it.impl(), n, (wxStringCharType)ch);
8f93a29f 2071 }
3c67202d
VZ
2072
2073 // delete characters from nStart to nStart + nLen
e87b7833 2074 wxString& erase(size_type pos = 0, size_type n = npos)
8f93a29f
VS
2075 {
2076 size_t from, len;
2077 PosLenToImpl(pos, n, &from, &len);
2078 m_impl.erase(from, len);
2079 return *this;
2080 }
f2a1b1bd 2081 // delete characters from first up to last
e87b7833 2082 iterator erase(iterator first, iterator last)
cf9a878b 2083 { return iterator(this, m_impl.erase(first.impl(), last.impl())); }
e87b7833 2084 iterator erase(iterator first)
cf9a878b 2085 { return iterator(this, m_impl.erase(first.impl())); }
e87b7833
MB
2086
2087#ifdef wxSTRING_BASE_HASNT_CLEAR
2088 void clear() { erase(); }
8f93a29f
VS
2089#else
2090 void clear() { m_impl.clear(); }
e87b7833 2091#endif
3c67202d
VZ
2092
2093 // replaces the substring of length nLen starting at nStart
8f93a29f
VS
2094 wxString& replace(size_t nStart, size_t nLen, const char* sz)
2095 {
2096 size_t from, len;
2097 PosLenToImpl(nStart, nLen, &from, &len);
2098 m_impl.replace(from, len, ImplStr(sz));
2099 return *this;
2100 }
2101 wxString& replace(size_t nStart, size_t nLen, const wchar_t* sz)
2102 {
2103 size_t from, len;
2104 PosLenToImpl(nStart, nLen, &from, &len);
2105 m_impl.replace(from, len, ImplStr(sz));
2106 return *this;
2107 }
e87b7833
MB
2108 // replaces the substring of length nLen starting at nStart
2109 wxString& replace(size_t nStart, size_t nLen, const wxString& str)
8f93a29f
VS
2110 {
2111 size_t from, len;
2112 PosLenToImpl(nStart, nLen, &from, &len);
2113 m_impl.replace(from, len, str.m_impl);
2114 return *this;
2115 }
3c67202d 2116 // replaces the substring with nCount copies of ch
c9f78968 2117 wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxUniChar ch)
8f93a29f
VS
2118 {
2119 size_t from, len;
2120 PosLenToImpl(nStart, nLen, &from, &len);
2121#if wxUSE_UNICODE_UTF8
2122 if ( !ch.IsAscii() )
467175ab 2123 m_impl.replace(from, len, wxStringOperations::EncodeNChars(nCount, ch));
8f93a29f
VS
2124 else
2125#endif
2126 m_impl.replace(from, len, nCount, (wxStringCharType)ch);
2127 return *this;
2128 }
3c67202d
VZ
2129 // replaces a substring with another substring
2130 wxString& replace(size_t nStart, size_t nLen,
e87b7833 2131 const wxString& str, size_t nStart2, size_t nLen2)
8f93a29f
VS
2132 {
2133 size_t from, len;
2134 PosLenToImpl(nStart, nLen, &from, &len);
2135
2136 size_t from2, len2;
04848a06 2137 str.PosLenToImpl(nStart2, nLen2, &from2, &len2);
8f93a29f
VS
2138
2139 m_impl.replace(from, len, str.m_impl, from2, len2);
2140 return *this;
2141 }
e87b7833 2142 // replaces the substring with first nCount chars of sz
fb20fa43 2143 wxString& replace(size_t nStart, size_t nLen,
8f93a29f
VS
2144 const char* sz, size_t nCount)
2145 {
2146 size_t from, len;
2147 PosLenToImpl(nStart, nLen, &from, &len);
2148
2149 SubstrBufFromMB str(ImplStr(sz, nCount));
2150
2151 m_impl.replace(from, len, str.data, str.len);
2152 return *this;
2153 }
2154 wxString& replace(size_t nStart, size_t nLen,
2155 const wchar_t* sz, size_t nCount)
2156 {
2157 size_t from, len;
2158 PosLenToImpl(nStart, nLen, &from, &len);
2159
2160 SubstrBufFromWC str(ImplStr(sz, nCount));
2161
2162 m_impl.replace(from, len, str.data, str.len);
2163 return *this;
2164 }
11aac4ba
VS
2165 wxString& replace(size_t nStart, size_t nLen,
2166 const wxString& s, size_t nCount)
2167 {
2168 size_t from, len;
2169 PosLenToImpl(nStart, nLen, &from, &len);
2170 m_impl.replace(from, len, s.m_impl.c_str(), s.LenToImpl(nCount));
2171 return *this;
2172 }
2173
8f93a29f 2174 wxString& replace(iterator first, iterator last, const char* s)
cf9a878b 2175 { m_impl.replace(first.impl(), last.impl(), ImplStr(s)); return *this; }
8f93a29f 2176 wxString& replace(iterator first, iterator last, const wchar_t* s)
cf9a878b 2177 { m_impl.replace(first.impl(), last.impl(), ImplStr(s)); return *this; }
8f93a29f
VS
2178 wxString& replace(iterator first, iterator last, const char* s, size_type n)
2179 {
2180 SubstrBufFromMB str(ImplStr(s, n));
cf9a878b 2181 m_impl.replace(first.impl(), last.impl(), str.data, str.len);
8f93a29f
VS
2182 return *this;
2183 }
2184 wxString& replace(iterator first, iterator last, const wchar_t* s, size_type n)
2185 {
2186 SubstrBufFromWC str(ImplStr(s, n));
cf9a878b 2187 m_impl.replace(first.impl(), last.impl(), str.data, str.len);
8f93a29f
VS
2188 return *this;
2189 }
e87b7833 2190 wxString& replace(iterator first, iterator last, const wxString& s)
cf9a878b 2191 { m_impl.replace(first.impl(), last.impl(), s.m_impl); return *this; }
8f93a29f
VS
2192 wxString& replace(iterator first, iterator last, size_type n, wxUniChar ch)
2193 {
2194#if wxUSE_UNICODE_UTF8
2195 if ( !ch.IsAscii() )
467175ab
VS
2196 m_impl.replace(first.impl(), last.impl(),
2197 wxStringOperations::EncodeNChars(n, ch));
8f93a29f
VS
2198 else
2199#endif
cf9a878b 2200 m_impl.replace(first.impl(), last.impl(), n, (wxStringCharType)ch);
8f93a29f
VS
2201 return *this;
2202 }
e87b7833
MB
2203 wxString& replace(iterator first, iterator last,
2204 const_iterator first1, const_iterator last1)
cf9a878b
VS
2205 {
2206 m_impl.replace(first.impl(), last.impl(), first1.impl(), last1.impl());
2207 return *this;
2208 }
f2a1b1bd
VZ
2209 wxString& replace(iterator first, iterator last,
2210 const char *first1, const char *last1)
2211 { replace(first, last, first1, last1 - first1); return *this; }
2212 wxString& replace(iterator first, iterator last,
2213 const wchar_t *first1, const wchar_t *last1)
2214 { replace(first, last, first1, last1 - first1); return *this; }
8f93a29f
VS
2215
2216 // swap two strings
2217 void swap(wxString& str)
2218 { m_impl.swap(str.m_impl); }
2219
2220 // find a substring
2221 size_t find(const wxString& str, size_t nStart = 0) const
2222 { return PosFromImpl(m_impl.find(str.m_impl, PosToImpl(nStart))); }
2223
2224 // find first n characters of sz
2225 size_t find(const char* sz, size_t nStart = 0, size_t n = npos) const
2226 {
2227 SubstrBufFromMB str(ImplStr(sz, n));
2228 return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len));
2229 }
2230 size_t find(const wchar_t* sz, size_t nStart = 0, size_t n = npos) const
2231 {
2232 SubstrBufFromWC str(ImplStr(sz, n));
2233 return PosFromImpl(m_impl.find(str.data, PosToImpl(nStart), str.len));
2234 }
d3cefe87
VS
2235 size_t find(const wxCharBuffer& s, size_t nStart = 0, size_t n = npos) const
2236 { return find(s.data(), nStart, n); }
2237 size_t find(const wxWCharBuffer& s, size_t nStart = 0, size_t n = npos) const
2238 { return find(s.data(), nStart, n); }
2239 size_t find(const wxCStrData& s, size_t nStart = 0, size_t n = npos) const
2240 { return find(s.AsWChar(), nStart, n); }
8f93a29f
VS
2241
2242 // find the first occurence of character ch after nStart
2243 size_t find(wxUniChar ch, size_t nStart = 0) const
467175ab
VS
2244 {
2245 return PosFromImpl(m_impl.find(wxStringOperations::EncodeChar(ch),
2246 PosToImpl(nStart)));
2247 }
8f93a29f
VS
2248 size_t find(wxUniCharRef ch, size_t nStart = 0) const
2249 { return find(wxUniChar(ch), nStart); }
2250 size_t find(char ch, size_t nStart = 0) const
2251 { return find(wxUniChar(ch), nStart); }
50e02008
VS
2252 size_t find(unsigned char ch, size_t nStart = 0) const
2253 { return find(wxUniChar(ch), nStart); }
8f93a29f
VS
2254 size_t find(wchar_t ch, size_t nStart = 0) const
2255 { return find(wxUniChar(ch), nStart); }
2256
2257 // rfind() family is exactly like find() but works right to left
2258
2259 // as find, but from the end
2260 size_t rfind(const wxString& str, size_t nStart = npos) const
2261 { return PosFromImpl(m_impl.rfind(str.m_impl, PosToImpl(nStart))); }
2262
2263 // as find, but from the end
2264 size_t rfind(const char* sz, size_t nStart = npos, size_t n = npos) const
2265 {
2266 SubstrBufFromMB str(ImplStr(sz, n));
2267 return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len));
2268 }
2269 size_t rfind(const wchar_t* sz, size_t nStart = npos, size_t n = npos) const
2270 {
2271 SubstrBufFromWC str(ImplStr(sz, n));
2272 return PosFromImpl(m_impl.rfind(str.data, PosToImpl(nStart), str.len));
2273 }
d3cefe87
VS
2274 size_t rfind(const wxCharBuffer& s, size_t nStart = npos, size_t n = npos) const
2275 { return rfind(s.data(), nStart, n); }
2276 size_t rfind(const wxWCharBuffer& s, size_t nStart = npos, size_t n = npos) const
2277 { return rfind(s.data(), nStart, n); }
2278 size_t rfind(const wxCStrData& s, size_t nStart = npos, size_t n = npos) const
2279 { return rfind(s.AsWChar(), nStart, n); }
8f93a29f
VS
2280 // as find, but from the end
2281 size_t rfind(wxUniChar ch, size_t nStart = npos) const
467175ab
VS
2282 {
2283 return PosFromImpl(m_impl.rfind(wxStringOperations::EncodeChar(ch),
2284 PosToImpl(nStart)));
2285 }
8f93a29f
VS
2286 size_t rfind(wxUniCharRef ch, size_t nStart = npos) const
2287 { return rfind(wxUniChar(ch), nStart); }
2288 size_t rfind(char ch, size_t nStart = npos) const
2289 { return rfind(wxUniChar(ch), nStart); }
50e02008
VS
2290 size_t rfind(unsigned char ch, size_t nStart = npos) const
2291 { return rfind(wxUniChar(ch), nStart); }
8f93a29f
VS
2292 size_t rfind(wchar_t ch, size_t nStart = npos) const
2293 { return rfind(wxUniChar(ch), nStart); }
2294
2295 // find first/last occurence of any character (not) in the set:
2296#if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2297 // FIXME-UTF8: this is not entirely correct, because it doesn't work if
2298 // sizeof(wchar_t)==2 and surrogates are present in the string;
2299 // should we care? Probably not.
2300 size_t find_first_of(const wxString& str, size_t nStart = 0) const
a962cdf4 2301 { return m_impl.find_first_of(str.m_impl, nStart); }
8f93a29f
VS
2302 size_t find_first_of(const char* sz, size_t nStart = 0) const
2303 { return m_impl.find_first_of(ImplStr(sz), nStart); }
2304 size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const
2305 { return m_impl.find_first_of(ImplStr(sz), nStart); }
a962cdf4 2306 size_t find_first_of(const char* sz, size_t nStart, size_t n) const
8f93a29f 2307 { return m_impl.find_first_of(ImplStr(sz), nStart, n); }
a962cdf4 2308 size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const
8f93a29f
VS
2309 { return m_impl.find_first_of(ImplStr(sz), nStart, n); }
2310 size_t find_first_of(wxUniChar c, size_t nStart = 0) const
2311 { return m_impl.find_first_of((wxChar)c, nStart); }
2312
a962cdf4
VS
2313 size_t find_last_of(const wxString& str, size_t nStart = npos) const
2314 { return m_impl.find_last_of(str.m_impl, nStart); }
8f93a29f
VS
2315 size_t find_last_of(const char* sz, size_t nStart = npos) const
2316 { return m_impl.find_last_of(ImplStr(sz), nStart); }
2317 size_t find_last_of(const wchar_t* sz, size_t nStart = npos) const
2318 { return m_impl.find_last_of(ImplStr(sz), nStart); }
2319 size_t find_last_of(const char* sz, size_t nStart, size_t n) const
2320 { return m_impl.find_last_of(ImplStr(sz), nStart, n); }
2321 size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const
2322 { return m_impl.find_last_of(ImplStr(sz), nStart, n); }
2323 size_t find_last_of(wxUniChar c, size_t nStart = npos) const
2324 { return m_impl.find_last_of((wxChar)c, nStart); }
2325
a962cdf4 2326 size_t find_first_not_of(const wxString& str, size_t nStart = 0) const
8f93a29f 2327 { return m_impl.find_first_not_of(str.m_impl, nStart); }
a962cdf4 2328 size_t find_first_not_of(const char* sz, size_t nStart = 0) const
8f93a29f 2329 { return m_impl.find_first_not_of(ImplStr(sz), nStart); }
a962cdf4 2330 size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const
8f93a29f 2331 { return m_impl.find_first_not_of(ImplStr(sz), nStart); }
a962cdf4 2332 size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const
8f93a29f 2333 { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); }
a962cdf4 2334 size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const
8f93a29f 2335 { return m_impl.find_first_not_of(ImplStr(sz), nStart, n); }
a962cdf4 2336 size_t find_first_not_of(wxUniChar c, size_t nStart = 0) const
8f93a29f
VS
2337 { return m_impl.find_first_not_of((wxChar)c, nStart); }
2338
a962cdf4 2339 size_t find_last_not_of(const wxString& str, size_t nStart = npos) const
8f93a29f 2340 { return m_impl.find_last_not_of(str.m_impl, nStart); }
a962cdf4 2341 size_t find_last_not_of(const char* sz, size_t nStart = npos) const
8f93a29f 2342 { return m_impl.find_last_not_of(ImplStr(sz), nStart); }
a962cdf4 2343 size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const
8f93a29f 2344 { return m_impl.find_last_not_of(ImplStr(sz), nStart); }
a962cdf4 2345 size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const
8f93a29f 2346 { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); }
a962cdf4 2347 size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const
8f93a29f 2348 { return m_impl.find_last_not_of(ImplStr(sz), nStart, n); }
a962cdf4 2349 size_t find_last_not_of(wxUniChar c, size_t nStart = npos) const
8f93a29f
VS
2350 { return m_impl.find_last_not_of((wxChar)c, nStart); }
2351#else
2352 // we can't use std::string implementation in UTF-8 build, because the
2353 // character sets would be interpreted wrongly:
2354
2355 // as strpbrk() but starts at nStart, returns npos if not found
2356 size_t find_first_of(const wxString& str, size_t nStart = 0) const
81727065 2357#if wxUSE_UNICODE // FIXME-UTF8: temporary
d3cefe87 2358 { return find_first_of(str.wc_str(), nStart); }
81727065 2359#else
d3cefe87 2360 { return find_first_of(str.mb_str(), nStart); }
81727065 2361#endif
8f93a29f
VS
2362 // same as above
2363 size_t find_first_of(const char* sz, size_t nStart = 0) const;
2364 size_t find_first_of(const wchar_t* sz, size_t nStart = 0) const;
2365 size_t find_first_of(const char* sz, size_t nStart, size_t n) const;
2366 size_t find_first_of(const wchar_t* sz, size_t nStart, size_t n) const;
2367 // same as find(char, size_t)
2368 size_t find_first_of(wxUniChar c, size_t nStart = 0) const
2369 { return find(c, nStart); }
2370 // find the last (starting from nStart) char from str in this string
2371 size_t find_last_of (const wxString& str, size_t nStart = npos) const
81727065 2372#if wxUSE_UNICODE // FIXME-UTF8: temporary
d3cefe87 2373 { return find_last_of(str.wc_str(), nStart); }
81727065 2374#else
d3cefe87 2375 { return find_last_of(str.mb_str(), nStart); }
81727065 2376#endif
8f93a29f
VS
2377 // same as above
2378 size_t find_last_of (const char* sz, size_t nStart = npos) const;
2379 size_t find_last_of (const wchar_t* sz, size_t nStart = npos) const;
2380 size_t find_last_of(const char* sz, size_t nStart, size_t n) const;
2381 size_t find_last_of(const wchar_t* sz, size_t nStart, size_t n) const;
2382 // same as above
2383 size_t find_last_of(wxUniChar c, size_t nStart = npos) const
2384 { return rfind(c, nStart); }
2385
2386 // find first/last occurence of any character not in the set
2387
2388 // as strspn() (starting from nStart), returns npos on failure
2389 size_t find_first_not_of(const wxString& str, size_t nStart = 0) const
81727065 2390#if wxUSE_UNICODE // FIXME-UTF8: temporary
d3cefe87 2391 { return find_first_not_of(str.wc_str(), nStart); }
81727065 2392#else
d3cefe87 2393 { return find_first_not_of(str.mb_str(), nStart); }
81727065 2394#endif
8f93a29f
VS
2395 // same as above
2396 size_t find_first_not_of(const char* sz, size_t nStart = 0) const;
2397 size_t find_first_not_of(const wchar_t* sz, size_t nStart = 0) const;
2398 size_t find_first_not_of(const char* sz, size_t nStart, size_t n) const;
2399 size_t find_first_not_of(const wchar_t* sz, size_t nStart, size_t n) const;
2400 // same as above
2401 size_t find_first_not_of(wxUniChar ch, size_t nStart = 0) const;
2402 // as strcspn()
2403 size_t find_last_not_of(const wxString& str, size_t nStart = npos) const
81727065 2404#if wxUSE_UNICODE // FIXME-UTF8: temporary
d3cefe87 2405 { return find_last_not_of(str.wc_str(), nStart); }
81727065 2406#else
d3cefe87 2407 { return find_last_not_of(str.mb_str(), nStart); }
81727065 2408#endif
8f93a29f
VS
2409 // same as above
2410 size_t find_last_not_of(const char* sz, size_t nStart = npos) const;
2411 size_t find_last_not_of(const wchar_t* sz, size_t nStart = npos) const;
2412 size_t find_last_not_of(const char* sz, size_t nStart, size_t n) const;
2413 size_t find_last_not_of(const wchar_t* sz, size_t nStart, size_t n) const;
2414 // same as above
2415 size_t find_last_not_of(wxUniChar ch, size_t nStart = npos) const;
2416#endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not
2417
2418 // provide char/wchar_t/wxUniCharRef overloads for char-finding functions
2419 // above to resolve ambiguities:
2420 size_t find_first_of(wxUniCharRef ch, size_t nStart = 0) const
2421 { return find_first_of(wxUniChar(ch), nStart); }
2422 size_t find_first_of(char ch, size_t nStart = 0) const
2423 { return find_first_of(wxUniChar(ch), nStart); }
50e02008
VS
2424 size_t find_first_of(unsigned char ch, size_t nStart = 0) const
2425 { return find_first_of(wxUniChar(ch), nStart); }
8f93a29f
VS
2426 size_t find_first_of(wchar_t ch, size_t nStart = 0) const
2427 { return find_first_of(wxUniChar(ch), nStart); }
2428 size_t find_last_of(wxUniCharRef ch, size_t nStart = npos) const
2429 { return find_last_of(wxUniChar(ch), nStart); }
2430 size_t find_last_of(char ch, size_t nStart = npos) const
2431 { return find_last_of(wxUniChar(ch), nStart); }
50e02008
VS
2432 size_t find_last_of(unsigned char ch, size_t nStart = npos) const
2433 { return find_last_of(wxUniChar(ch), nStart); }
8f93a29f
VS
2434 size_t find_last_of(wchar_t ch, size_t nStart = npos) const
2435 { return find_last_of(wxUniChar(ch), nStart); }
2436 size_t find_first_not_of(wxUniCharRef ch, size_t nStart = 0) const
2437 { return find_first_not_of(wxUniChar(ch), nStart); }
2438 size_t find_first_not_of(char ch, size_t nStart = 0) const
2439 { return find_first_not_of(wxUniChar(ch), nStart); }
50e02008
VS
2440 size_t find_first_not_of(unsigned char ch, size_t nStart = 0) const
2441 { return find_first_not_of(wxUniChar(ch), nStart); }
8f93a29f
VS
2442 size_t find_first_not_of(wchar_t ch, size_t nStart = 0) const
2443 { return find_first_not_of(wxUniChar(ch), nStart); }
2444 size_t find_last_not_of(wxUniCharRef ch, size_t nStart = npos) const
2445 { return find_last_not_of(wxUniChar(ch), nStart); }
2446 size_t find_last_not_of(char ch, size_t nStart = npos) const
2447 { return find_last_not_of(wxUniChar(ch), nStart); }
50e02008
VS
2448 size_t find_last_not_of(unsigned char ch, size_t nStart = npos) const
2449 { return find_last_not_of(wxUniChar(ch), nStart); }
8f93a29f
VS
2450 size_t find_last_not_of(wchar_t ch, size_t nStart = npos) const
2451 { return find_last_not_of(wxUniChar(ch), nStart); }
3c67202d 2452
d3cefe87
VS
2453 // and additional overloads for the versions taking strings:
2454 size_t find_first_of(const wxCStrData& sz, size_t nStart = 0) const
2455 { return find_first_of(sz.AsString(), nStart); }
2456 size_t find_first_of(const wxCharBuffer& sz, size_t nStart = 0) const
2457 { return find_first_of(sz.data(), nStart); }
2458 size_t find_first_of(const wxWCharBuffer& sz, size_t nStart = 0) const
2459 { return find_first_of(sz.data(), nStart); }
2460 size_t find_first_of(const wxCStrData& sz, size_t nStart, size_t n) const
2461 { return find_first_of(sz.AsWChar(), nStart, n); }
2462 size_t find_first_of(const wxCharBuffer& sz, size_t nStart, size_t n) const
2463 { return find_first_of(sz.data(), nStart, n); }
2464 size_t find_first_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const
2465 { return find_first_of(sz.data(), nStart, n); }
2466
2467 size_t find_last_of(const wxCStrData& sz, size_t nStart = 0) const
2468 { return find_last_of(sz.AsString(), nStart); }
2469 size_t find_last_of(const wxCharBuffer& sz, size_t nStart = 0) const
2470 { return find_last_of(sz.data(), nStart); }
2471 size_t find_last_of(const wxWCharBuffer& sz, size_t nStart = 0) const
2472 { return find_last_of(sz.data(), nStart); }
2473 size_t find_last_of(const wxCStrData& sz, size_t nStart, size_t n) const
2474 { return find_last_of(sz.AsWChar(), nStart, n); }
2475 size_t find_last_of(const wxCharBuffer& sz, size_t nStart, size_t n) const
2476 { return find_last_of(sz.data(), nStart, n); }
2477 size_t find_last_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const
2478 { return find_last_of(sz.data(), nStart, n); }
2479
2480 size_t find_first_not_of(const wxCStrData& sz, size_t nStart = 0) const
2481 { return find_first_not_of(sz.AsString(), nStart); }
2482 size_t find_first_not_of(const wxCharBuffer& sz, size_t nStart = 0) const
2483 { return find_first_not_of(sz.data(), nStart); }
2484 size_t find_first_not_of(const wxWCharBuffer& sz, size_t nStart = 0) const
2485 { return find_first_not_of(sz.data(), nStart); }
2486 size_t find_first_not_of(const wxCStrData& sz, size_t nStart, size_t n) const
2487 { return find_first_not_of(sz.AsWChar(), nStart, n); }
2488 size_t find_first_not_of(const wxCharBuffer& sz, size_t nStart, size_t n) const
2489 { return find_first_not_of(sz.data(), nStart, n); }
2490 size_t find_first_not_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const
2491 { return find_first_not_of(sz.data(), nStart, n); }
2492
2493 size_t find_last_not_of(const wxCStrData& sz, size_t nStart = 0) const
2494 { return find_last_not_of(sz.AsString(), nStart); }
2495 size_t find_last_not_of(const wxCharBuffer& sz, size_t nStart = 0) const
2496 { return find_last_not_of(sz.data(), nStart); }
2497 size_t find_last_not_of(const wxWCharBuffer& sz, size_t nStart = 0) const
2498 { return find_last_not_of(sz.data(), nStart); }
2499 size_t find_last_not_of(const wxCStrData& sz, size_t nStart, size_t n) const
2500 { return find_last_not_of(sz.AsWChar(), nStart, n); }
2501 size_t find_last_not_of(const wxCharBuffer& sz, size_t nStart, size_t n) const
2502 { return find_last_not_of(sz.data(), nStart, n); }
2503 size_t find_last_not_of(const wxWCharBuffer& sz, size_t nStart, size_t n) const
2504 { return find_last_not_of(sz.data(), nStart, n); }
2505
e87b7833
MB
2506 // string += string
2507 wxString& operator+=(const wxString& s)
8f93a29f 2508 { m_impl += s.m_impl; return *this; }
e87b7833 2509 // string += C string
8f93a29f
VS
2510 wxString& operator+=(const char *psz)
2511 { m_impl += ImplStr(psz); return *this; }
2512 wxString& operator+=(const wchar_t *pwz)
2513 { m_impl += ImplStr(pwz); return *this; }
c9f78968 2514 wxString& operator+=(const wxCStrData& s)
8f93a29f 2515 { m_impl += s.AsString().m_impl; return *this; }
d3cefe87
VS
2516 wxString& operator+=(const wxCharBuffer& s)
2517 { return operator+=(s.data()); }
2518 wxString& operator+=(const wxWCharBuffer& s)
2519 { return operator+=(s.data()); }
e87b7833 2520 // string += char
c9f78968 2521 wxString& operator+=(wxUniChar ch)
467175ab 2522 { m_impl += wxStringOperations::EncodeChar(ch); return *this; }
c9f78968 2523 wxString& operator+=(wxUniCharRef ch) { return *this += wxUniChar(ch); }
b7d40341 2524 wxString& operator+=(int ch) { return *this += wxUniChar(ch); }
c9f78968 2525 wxString& operator+=(char ch) { return *this += wxUniChar(ch); }
b77afb41 2526 wxString& operator+=(unsigned char ch) { return *this += wxUniChar(ch); }
c9f78968 2527 wxString& operator+=(wchar_t ch) { return *this += wxUniChar(ch); }
d8a4b666
VS
2528
2529private:
2523e9b7 2530#if !wxUSE_STL_BASED_WXSTRING
d8a4b666 2531 // helpers for wxStringBuffer and wxStringBufferLength
8f93a29f
VS
2532 wxStringCharType *DoGetWriteBuf(size_t nLen)
2533 { return m_impl.DoGetWriteBuf(nLen); }
2534 void DoUngetWriteBuf()
2535 { m_impl.DoUngetWriteBuf(); }
2536 void DoUngetWriteBuf(size_t nLen)
2537 { m_impl.DoUngetWriteBuf(nLen); }
2523e9b7 2538#endif // !wxUSE_STL_BASED_WXSTRING
c9f78968
VS
2539
2540#ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
d1f6e2cf
VS
2541 #if !wxUSE_UTF8_LOCALE_ONLY
2542 int DoPrintfWchar(const wxChar *format, ...);
2543 static wxString DoFormatWchar(const wxChar *format, ...);
2544 #endif
2545 #if wxUSE_UNICODE_UTF8
2546 int DoPrintfUtf8(const char *format, ...);
2547 static wxString DoFormatUtf8(const char *format, ...);
2548 #endif
c9f78968 2549#endif
8f93a29f
VS
2550
2551#if !wxUSE_STL_BASED_WXSTRING
2552 // check string's data validity
2553 bool IsValid() const { return m_impl.GetStringData()->IsValid(); }
2554#endif
2555
2556private:
2557 wxStringImpl m_impl;
132276cf
VS
2558
2559 // buffers for compatibility conversion from (char*)c_str() and
2560 // (wchar_t*)c_str():
2561 // FIXME-UTF8: bechmark various approaches to keeping compatibility buffers
2562 template<typename T>
2563 struct ConvertedBuffer
2564 {
2565 ConvertedBuffer() : m_buf(NULL) {}
2566 ~ConvertedBuffer()
2567 { free(m_buf); }
2568
05f32fc3 2569 operator T*() const { return m_buf; }
132276cf
VS
2570
2571 ConvertedBuffer& operator=(T *str)
2572 {
2573 free(m_buf);
2574 m_buf = str;
2575 return *this;
2576 }
2577
2578 T *m_buf;
2579 };
111d9948 2580#if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
132276cf
VS
2581 ConvertedBuffer<char> m_convertedToChar;
2582#endif
2583#if !wxUSE_UNICODE_WCHAR
2584 ConvertedBuffer<wchar_t> m_convertedToWChar;
2585#endif
2523e9b7 2586
b0c4d5d7
VS
2587#if wxUSE_UNICODE_UTF8
2588 // FIXME-UTF8: (try to) move this elsewhere (TLS) or solve differently
2589 // assigning to character pointer to by wxString::interator may
2590 // change the underlying wxStringImpl iterator, so we have to
2591 // keep track of all iterators and update them as necessary:
2592 struct wxStringIteratorNodeHead
2593 {
2594 wxStringIteratorNodeHead() : ptr(NULL) {}
2595 wxStringIteratorNode *ptr;
300b44a9
VS
2596
2597 // copying is disallowed as it would result in more than one pointer into
2598 // the same linked list
2599 DECLARE_NO_COPY_CLASS(wxStringIteratorNodeHead)
b0c4d5d7
VS
2600 };
2601
2602 wxStringIteratorNodeHead m_iterators;
2603
b5dbe15d
VS
2604 friend class WXDLLIMPEXP_FWD_BASE wxStringIteratorNode;
2605 friend class WXDLLIMPEXP_FWD_BASE wxUniCharRef;
b0c4d5d7
VS
2606#endif // wxUSE_UNICODE_UTF8
2607
b5dbe15d 2608 friend class WXDLLIMPEXP_FWD_BASE wxCStrData;
6798451b
VS
2609 friend class wxStringInternalBuffer;
2610 friend class wxStringInternalBufferLength;
c801d85f
KB
2611};
2612
c9f78968
VS
2613#ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
2614 #pragma warning (default:4275)
2615#endif
2616
a962cdf4
VS
2617// string iterator operators that satisfy STL Random Access Iterator
2618// requirements:
b5343e06 2619inline wxString::iterator operator+(ptrdiff_t n, wxString::iterator i)
a962cdf4 2620 { return i + n; }
b5343e06 2621inline wxString::const_iterator operator+(ptrdiff_t n, wxString::const_iterator i)
a962cdf4 2622 { return i + n; }
b5343e06 2623inline wxString::reverse_iterator operator+(ptrdiff_t n, wxString::reverse_iterator i)
a962cdf4 2624 { return i + n; }
b5343e06 2625inline wxString::const_reverse_iterator operator+(ptrdiff_t n, wxString::const_reverse_iterator i)
a962cdf4
VS
2626 { return i + n; }
2627
1fc8cfbd
VZ
2628// notice that even though for many compilers the friend declarations above are
2629// enough, from the point of view of C++ standard we must have the declarations
2630// here as friend ones are not injected in the enclosing namespace and without
2631// them the code fails to compile with conforming compilers such as xlC or g++4
c9f78968 2632wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2);
8f93a29f
VS
2633wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const char *psz);
2634wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wchar_t *pwz);
2635wxString WXDLLIMPEXP_BASE operator+(const char *psz, const wxString& string);
2636wxString WXDLLIMPEXP_BASE operator+(const wchar_t *pwz, const wxString& string);
1fc8cfbd 2637
c9f78968
VS
2638wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxUniChar ch);
2639wxString WXDLLIMPEXP_BASE operator+(wxUniChar ch, const wxString& string);
2640
2641inline wxString operator+(const wxString& string, wxUniCharRef ch)
2642 { return string + (wxUniChar)ch; }
2643inline wxString operator+(const wxString& string, char ch)
2644 { return string + wxUniChar(ch); }
2645inline wxString operator+(const wxString& string, wchar_t ch)
2646 { return string + wxUniChar(ch); }
2647inline wxString operator+(wxUniCharRef ch, const wxString& string)
2648 { return (wxUniChar)ch + string; }
2649inline wxString operator+(char ch, const wxString& string)
2650 { return wxUniChar(ch) + string; }
2651inline wxString operator+(wchar_t ch, const wxString& string)
2652 { return wxUniChar(ch) + string; }
2653
b7146cbe 2654
a0f63de9 2655#define wxGetEmptyString() wxString()
07170120 2656
1d218550
VZ
2657// ----------------------------------------------------------------------------
2658// wxStringBuffer: a tiny class allowing to get a writable pointer into string
2659// ----------------------------------------------------------------------------
2660
2523e9b7
VS
2661#if !wxUSE_STL_BASED_WXSTRING
2662// string buffer for direct access to string data in their native
2663// representation:
6798451b 2664class wxStringInternalBuffer
2523e9b7
VS
2665{
2666public:
2667 typedef wxStringCharType CharType;
2668
6798451b 2669 wxStringInternalBuffer(wxString& str, size_t lenWanted = 1024)
2523e9b7
VS
2670 : m_str(str), m_buf(NULL)
2671 { m_buf = m_str.DoGetWriteBuf(lenWanted); }
2672
6798451b 2673 ~wxStringInternalBuffer() { m_str.DoUngetWriteBuf(); }
2523e9b7
VS
2674
2675 operator wxStringCharType*() const { return m_buf; }
941b78cc 2676
2523e9b7
VS
2677private:
2678 wxString& m_str;
2679 wxStringCharType *m_buf;
2680
6798451b 2681 DECLARE_NO_COPY_CLASS(wxStringInternalBuffer)
2523e9b7
VS
2682};
2683
6798451b 2684class wxStringInternalBufferLength
941b78cc
MB
2685{
2686public:
2523e9b7
VS
2687 typedef wxStringCharType CharType;
2688
6798451b 2689 wxStringInternalBufferLength(wxString& str, size_t lenWanted = 1024)
2523e9b7
VS
2690 : m_str(str), m_buf(NULL), m_len(0), m_lenSet(false)
2691 {
2692 m_buf = m_str.DoGetWriteBuf(lenWanted);
2693 wxASSERT(m_buf != NULL);
2694 }
2695
6798451b 2696 ~wxStringInternalBufferLength()
2523e9b7
VS
2697 {
2698 wxASSERT(m_lenSet);
2699 m_str.DoUngetWriteBuf(m_len);
2700 }
2701
2702 operator wxStringCharType*() const { return m_buf; }
2703 void SetLength(size_t length) { m_len = length; m_lenSet = true; }
2704
2705private:
2706 wxString& m_str;
2707 wxStringCharType *m_buf;
2708 size_t m_len;
2709 bool m_lenSet;
2710
6798451b 2711 DECLARE_NO_COPY_CLASS(wxStringInternalBufferLength)
2523e9b7
VS
2712};
2713
2714#endif // !wxUSE_STL_BASED_WXSTRING
2715
2716template<typename T>
4ec4d861 2717class WXDLLIMPEXP_BASE wxStringTypeBufferBase
2523e9b7
VS
2718{
2719public:
2720 typedef T CharType;
2721
2722 wxStringTypeBufferBase(wxString& str, size_t lenWanted = 1024)
e87b7833 2723 : m_str(str), m_buf(lenWanted)
941b78cc
MB
2724 { }
2725
941b78cc 2726
2523e9b7 2727 operator CharType*() { return m_buf.data(); }
941b78cc 2728
2523e9b7 2729protected:
941b78cc 2730 wxString& m_str;
2523e9b7 2731 wxCharTypeBuffer<CharType> m_buf;
941b78cc
MB
2732};
2733
2523e9b7 2734template<typename T>
4ec4d861 2735class WXDLLIMPEXP_BASE wxStringTypeBufferLengthBase
941b78cc
MB
2736{
2737public:
2523e9b7
VS
2738 typedef T CharType;
2739
2740 wxStringTypeBufferLengthBase(wxString& str, size_t lenWanted = 1024)
941b78cc
MB
2741 : m_str(str), m_buf(lenWanted), m_len(0), m_lenSet(false)
2742 { }
2743
2523e9b7 2744 operator CharType*() { return m_buf.data(); }
941b78cc
MB
2745 void SetLength(size_t length) { m_len = length; m_lenSet = true; }
2746
2523e9b7 2747protected:
941b78cc 2748 wxString& m_str;
2523e9b7 2749 wxCharTypeBuffer<CharType> m_buf;
941b78cc
MB
2750 size_t m_len;
2751 bool m_lenSet;
941b78cc
MB
2752};
2753
2523e9b7
VS
2754template<typename T>
2755class wxStringTypeBuffer : public wxStringTypeBufferBase<T>
2756{
2757public:
2758 wxStringTypeBuffer(wxString& str, size_t lenWanted = 1024)
2759 : wxStringTypeBufferBase<T>(str, lenWanted) {}
2760 ~wxStringTypeBuffer()
2761 {
2762 this->m_str.assign(this->m_buf.data());
2763 }
941b78cc 2764
2523e9b7
VS
2765 DECLARE_NO_COPY_CLASS(wxStringTypeBuffer)
2766};
2767
2768template<typename T>
2769class wxStringTypeBufferLength : public wxStringTypeBufferLengthBase<T>
1d218550
VZ
2770{
2771public:
2523e9b7
VS
2772 wxStringTypeBufferLength(wxString& str, size_t lenWanted = 1024)
2773 : wxStringTypeBufferLengthBase<T>(str, lenWanted) {}
e015c2a3 2774
2523e9b7
VS
2775 ~wxStringTypeBufferLength()
2776 {
2777 wxASSERT(this->m_lenSet);
2778 this->m_str.assign(this->m_buf.data(), this->m_len);
2779 }
e015c2a3 2780
2523e9b7
VS
2781 DECLARE_NO_COPY_CLASS(wxStringTypeBufferLength)
2782};
e015c2a3 2783
2523e9b7 2784#if wxUSE_STL_BASED_WXSTRING
7c77f334
VZ
2785
2786WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase<wxStringCharType> )
2787
6798451b 2788class wxStringInternalBuffer : public wxStringTypeBufferBase<wxStringCharType>
2523e9b7
VS
2789{
2790public:
6798451b 2791 wxStringInternalBuffer(wxString& str, size_t lenWanted = 1024)
2523e9b7 2792 : wxStringTypeBufferBase<wxStringCharType>(str, lenWanted) {}
6798451b 2793 ~wxStringInternalBuffer()
2523e9b7 2794 { m_str.m_impl.assign(m_buf.data()); }
e015c2a3 2795
6798451b 2796 DECLARE_NO_COPY_CLASS(wxStringInternalBuffer)
1d218550
VZ
2797};
2798
7c77f334
VZ
2799WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE(
2800 wxStringTypeBufferLengthBase<wxStringCharType> )
2801
2802class wxStringInternalBufferLength
2803 : public wxStringTypeBufferLengthBase<wxStringCharType>
941b78cc
MB
2804{
2805public:
6798451b 2806 wxStringInternalBufferLength(wxString& str, size_t lenWanted = 1024)
2523e9b7 2807 : wxStringTypeBufferLengthBase<wxStringCharType>(str, lenWanted) {}
941b78cc 2808
6798451b 2809 ~wxStringInternalBufferLength()
941b78cc
MB
2810 {
2811 wxASSERT(m_lenSet);
2523e9b7 2812 m_str.m_impl.assign(m_buf.data(), m_len);
941b78cc
MB
2813 }
2814
6798451b 2815 DECLARE_NO_COPY_CLASS(wxStringInternalBufferLength)
941b78cc 2816};
2523e9b7
VS
2817#endif // wxUSE_STL_BASED_WXSTRING
2818
941b78cc 2819
2523e9b7
VS
2820#if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
2821typedef wxStringTypeBuffer<wxChar> wxStringBuffer;
2822typedef wxStringTypeBufferLength<wxChar> wxStringBufferLength;
2823#else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
6798451b
VS
2824typedef wxStringInternalBuffer wxStringBuffer;
2825typedef wxStringInternalBufferLength wxStringBufferLength;
8f93a29f 2826#endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
941b78cc 2827
7d46f92b 2828#if wxUSE_UNICODE_UTF8
628f87da
VS
2829typedef wxStringInternalBuffer wxUTF8StringBuffer;
2830typedef wxStringInternalBufferLength wxUTF8StringBufferLength;
7d46f92b 2831#elif wxUSE_UNICODE_WCHAR
7c77f334
VZ
2832
2833WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase<char> )
2834
628f87da
VS
2835class WXDLLIMPEXP_BASE wxUTF8StringBuffer : public wxStringTypeBufferBase<char>
2836{
2837public:
2838 wxUTF8StringBuffer(wxString& str, size_t lenWanted = 1024)
2839 : wxStringTypeBufferBase<char>(str, lenWanted) {}
2840 ~wxUTF8StringBuffer();
2841
2842 DECLARE_NO_COPY_CLASS(wxUTF8StringBuffer)
2843};
2844
7c77f334
VZ
2845WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferLengthBase<char> )
2846
628f87da
VS
2847class WXDLLIMPEXP_BASE wxUTF8StringBufferLength
2848 : public wxStringTypeBufferLengthBase<char>
2849{
2850public:
2851 wxUTF8StringBufferLength(wxString& str, size_t lenWanted = 1024)
2852 : wxStringTypeBufferLengthBase<char>(str, lenWanted) {}
2853 ~wxUTF8StringBufferLength();
2854
2855 DECLARE_NO_COPY_CLASS(wxUTF8StringBufferLength)
2856};
7d46f92b 2857#endif // wxUSE_UNICODE_UTF8/wxUSE_UNICODE_WCHAR
628f87da
VS
2858
2859
c801d85f 2860// ---------------------------------------------------------------------------
3c67202d 2861// wxString comparison functions: operator versions are always case sensitive
c801d85f 2862// ---------------------------------------------------------------------------
f33fee2a 2863
831faf97 2864#define wxCMP_WXCHAR_STRING(p, s, op) 0 op s.Cmp(p)
fb52c2b6
VZ
2865
2866wxDEFINE_ALL_COMPARISONS(const wxChar *, const wxString&, wxCMP_WXCHAR_STRING)
2867
2868#undef wxCMP_WXCHAR_STRING
2869
0cbe5ee3 2870inline bool operator==(const wxString& s1, const wxString& s2)
2712e317 2871 { return s1.IsSameAs(s2); }
0cbe5ee3 2872inline bool operator!=(const wxString& s1, const wxString& s2)
2712e317 2873 { return !s1.IsSameAs(s2); }
0cbe5ee3
VZ
2874inline bool operator< (const wxString& s1, const wxString& s2)
2875 { return s1.Cmp(s2) < 0; }
0cbe5ee3
VZ
2876inline bool operator> (const wxString& s1, const wxString& s2)
2877 { return s1.Cmp(s2) > 0; }
0cbe5ee3
VZ
2878inline bool operator<=(const wxString& s1, const wxString& s2)
2879 { return s1.Cmp(s2) <= 0; }
0cbe5ee3
VZ
2880inline bool operator>=(const wxString& s1, const wxString& s2)
2881 { return s1.Cmp(s2) >= 0; }
3c67202d 2882
5d0fac27
VS
2883inline bool operator==(const wxString& s1, const wxCStrData& s2)
2884 { return s1 == s2.AsString(); }
2885inline bool operator==(const wxCStrData& s1, const wxString& s2)
2886 { return s1.AsString() == s2; }
2887inline bool operator!=(const wxString& s1, const wxCStrData& s2)
2888 { return s1 != s2.AsString(); }
2889inline bool operator!=(const wxCStrData& s1, const wxString& s2)
2890 { return s1.AsString() != s2; }
2891
5ca07d0f 2892inline bool operator==(const wxString& s1, const wxWCharBuffer& s2)
f33fee2a 2893 { return (s1.Cmp((const wchar_t *)s2) == 0); }
5ca07d0f 2894inline bool operator==(const wxWCharBuffer& s1, const wxString& s2)
f33fee2a 2895 { return (s2.Cmp((const wchar_t *)s1) == 0); }
f6bcfd97
BP
2896inline bool operator!=(const wxString& s1, const wxWCharBuffer& s2)
2897 { return (s1.Cmp((const wchar_t *)s2) != 0); }
2898inline bool operator!=(const wxWCharBuffer& s1, const wxString& s2)
2899 { return (s2.Cmp((const wchar_t *)s1) != 0); }
be39d6f5 2900
5ca07d0f 2901inline bool operator==(const wxString& s1, const wxCharBuffer& s2)
f33fee2a 2902 { return (s1.Cmp((const char *)s2) == 0); }
5ca07d0f 2903inline bool operator==(const wxCharBuffer& s1, const wxString& s2)
f33fee2a 2904 { return (s2.Cmp((const char *)s1) == 0); }
f6bcfd97
BP
2905inline bool operator!=(const wxString& s1, const wxCharBuffer& s2)
2906 { return (s1.Cmp((const char *)s2) != 0); }
2907inline bool operator!=(const wxCharBuffer& s1, const wxString& s2)
2908 { return (s2.Cmp((const char *)s1) != 0); }
5ca07d0f 2909
6d56eb5c 2910inline wxString operator+(const wxString& string, const wxWCharBuffer& buf)
e90c1d2a 2911 { return string + (const wchar_t *)buf; }
6d56eb5c 2912inline wxString operator+(const wxWCharBuffer& buf, const wxString& string)
e90c1d2a 2913 { return (const wchar_t *)buf + string; }
be39d6f5 2914
6d56eb5c 2915inline wxString operator+(const wxString& string, const wxCharBuffer& buf)
e90c1d2a 2916 { return string + (const char *)buf; }
6d56eb5c 2917inline wxString operator+(const wxCharBuffer& buf, const wxString& string)
e90c1d2a 2918 { return (const char *)buf + string; }
f04f3991 2919
a962cdf4 2920// comparison with char
c9f78968
VS
2921inline bool operator==(const wxUniChar& c, const wxString& s) { return s.IsSameAs(c); }
2922inline bool operator==(const wxUniCharRef& c, const wxString& s) { return s.IsSameAs(c); }
2923inline bool operator==(char c, const wxString& s) { return s.IsSameAs(c); }
2924inline bool operator==(wchar_t c, const wxString& s) { return s.IsSameAs(c); }
2925inline bool operator==(int c, const wxString& s) { return s.IsSameAs(c); }
2926inline bool operator==(const wxString& s, const wxUniChar& c) { return s.IsSameAs(c); }
2927inline bool operator==(const wxString& s, const wxUniCharRef& c) { return s.IsSameAs(c); }
2928inline bool operator==(const wxString& s, char c) { return s.IsSameAs(c); }
2929inline bool operator==(const wxString& s, wchar_t c) { return s.IsSameAs(c); }
2930inline bool operator!=(const wxUniChar& c, const wxString& s) { return !s.IsSameAs(c); }
2931inline bool operator!=(const wxUniCharRef& c, const wxString& s) { return !s.IsSameAs(c); }
2932inline bool operator!=(char c, const wxString& s) { return !s.IsSameAs(c); }
2933inline bool operator!=(wchar_t c, const wxString& s) { return !s.IsSameAs(c); }
2934inline bool operator!=(int c, const wxString& s) { return !s.IsSameAs(c); }
2935inline bool operator!=(const wxString& s, const wxUniChar& c) { return !s.IsSameAs(c); }
2936inline bool operator!=(const wxString& s, const wxUniCharRef& c) { return !s.IsSameAs(c); }
2937inline bool operator!=(const wxString& s, char c) { return !s.IsSameAs(c); }
2938inline bool operator!=(const wxString& s, wchar_t c) { return !s.IsSameAs(c); }
e51b17a1 2939
d7330233
VS
2940// comparison with C string in Unicode build
2941#if wxUSE_UNICODE
fb52c2b6
VZ
2942
2943#define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s
2944
2945wxDEFINE_ALL_COMPARISONS(const char *, const wxString&, wxCMP_CHAR_STRING)
2946
2947#undef wxCMP_CHAR_STRING
2948
d7330233
VS
2949#endif // wxUSE_UNICODE
2950
fb52c2b6
VZ
2951// we also need to provide the operators for comparison with wxCStrData to
2952// resolve ambiguity between operator(const wxChar *,const wxString &) and
2953// operator(const wxChar *, const wxChar *) for "p == s.c_str()"
2954//
2955// notice that these are (shallow) pointer comparisons, not (deep) string ones
2956#define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar()
2957#define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar()
2958
11aac4ba
VS
2959wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData&, wxCMP_WCHAR_CSTRDATA)
2960wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData&, wxCMP_CHAR_CSTRDATA)
fb52c2b6
VZ
2961
2962#undef wxCMP_CHAR_CSTRDATA
2963#undef wxCMP_WCHAR_CSTRDATA
2964
c801d85f 2965// ---------------------------------------------------------------------------
3c67202d 2966// Implementation only from here until the end of file
c801d85f
KB
2967// ---------------------------------------------------------------------------
2968
e87b7833 2969#if wxUSE_STD_IOSTREAM
c801d85f 2970
65f19af1 2971#include "wx/iosfwrap.h"
c801d85f 2972
bddd7a8d 2973WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxString&);
c9f78968 2974WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCStrData&);
04abe4bc
VS
2975WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxCharBuffer&);
2976#ifndef __BORLANDC__
2977WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxWCharBuffer&);
2978#endif
c801d85f 2979
6a6ea041 2980#if wxUSE_UNICODE && defined(HAVE_WOSTREAM)
6b61b594
VZ
2981
2982WXDLLIMPEXP_BASE wxSTD wostream& operator<<(wxSTD wostream&, const wxString&);
2983WXDLLIMPEXP_BASE wxSTD wostream& operator<<(wxSTD wostream&, const wxCStrData&);
2984WXDLLIMPEXP_BASE wxSTD wostream& operator<<(wxSTD wostream&, const wxWCharBuffer&);
2985
6a6ea041 2986#endif // wxUSE_UNICODE && defined(HAVE_WOSTREAM)
6b61b594 2987
7a906e1a 2988#endif // wxUSE_STD_IOSTREAM
c801d85f 2989
c9f78968
VS
2990// ---------------------------------------------------------------------------
2991// wxCStrData implementation
2992// ---------------------------------------------------------------------------
2993
2994inline wxCStrData::wxCStrData(char *buf)
2995 : m_str(new wxString(buf)), m_offset(0), m_owned(true) {}
2996inline wxCStrData::wxCStrData(wchar_t *buf)
2997 : m_str(new wxString(buf)), m_offset(0), m_owned(true) {}
2998
92a17abc
VS
2999inline wxCStrData::wxCStrData(const wxCStrData& data)
3000 : m_str(data.m_owned ? new wxString(*data.m_str) : data.m_str),
3001 m_offset(data.m_offset),
3002 m_owned(data.m_owned)
3003{
3004}
3005
c9f78968
VS
3006inline wxCStrData::~wxCStrData()
3007{
3008 if ( m_owned )
b052a864 3009 delete wx_const_cast(wxString*, m_str); // cast to silence warnings
c9f78968
VS
3010}
3011
11aac4ba
VS
3012// simple cases for AsChar() and AsWChar(), the complicated ones are
3013// in string.cpp
3014#if wxUSE_UNICODE_WCHAR
c9f78968 3015inline const wchar_t* wxCStrData::AsWChar() const
11aac4ba
VS
3016{
3017 return m_str->wx_str() + m_offset;
3018}
3019#endif // wxUSE_UNICODE_WCHAR
3020
bfb6847d 3021#if !wxUSE_UNICODE
c9f78968 3022inline const char* wxCStrData::AsChar() const
c9f78968 3023{
bfb6847d 3024 return m_str->wx_str() + m_offset;
c9f78968 3025}
11aac4ba 3026#endif // !wxUSE_UNICODE
c9f78968 3027
bfb6847d
VS
3028#if wxUSE_UTF8_LOCALE_ONLY
3029inline const char* wxCStrData::AsChar() const
3030{
3031 return wxStringOperations::AddToIter(m_str->wx_str(), m_offset);
3032}
3033#endif // wxUSE_UTF8_LOCALE_ONLY
3034
681e4412
VS
3035inline const wxCharBuffer wxCStrData::AsCharBuf() const
3036{
3037#if !wxUSE_UNICODE
3038 return wxCharBuffer::CreateNonOwned(AsChar());
3039#else
3040 return AsString().mb_str();
3041#endif
3042}
3043
3044inline const wxWCharBuffer wxCStrData::AsWCharBuf() const
3045{
3046#if wxUSE_UNICODE_WCHAR
3047 return wxWCharBuffer::CreateNonOwned(AsWChar());
3048#else
3049 return AsString().wc_str();
3050#endif
3051}
3052
c9f78968
VS
3053inline wxString wxCStrData::AsString() const
3054{
3055 if ( m_offset == 0 )
3056 return *m_str;
3057 else
3058 return m_str->Mid(m_offset);
3059}
3060
2523e9b7
VS
3061inline const wxStringCharType *wxCStrData::AsInternal() const
3062{
bfb6847d 3063#if wxUSE_UNICODE_UTF8
2523e9b7 3064 return wxStringOperations::AddToIter(m_str->wx_str(), m_offset);
bfb6847d
VS
3065#else
3066 return m_str->wx_str() + m_offset;
3067#endif
2523e9b7
VS
3068}
3069
c9f78968
VS
3070inline wxUniChar wxCStrData::operator*() const
3071{
3072 if ( m_str->empty() )
3073 return wxUniChar(_T('\0'));
3074 else
3075 return (*m_str)[m_offset];
3076}
3077
3078inline wxUniChar wxCStrData::operator[](size_t n) const
3079{
378964d4
VS
3080 // NB: we intentionally use operator[] and not at() here because the former
3081 // works for the terminating NUL while the latter does not
3082 return (*m_str)[m_offset + n];
c9f78968
VS
3083}
3084
cc5bd48e
VZ
3085// ----------------------------------------------------------------------------
3086// more wxCStrData operators
3087// ----------------------------------------------------------------------------
3088
3089// we need to define those to allow "size_t pos = p - s.c_str()" where p is
3090// some pointer into the string
3091inline size_t operator-(const char *p, const wxCStrData& cs)
3092{
3093 return p - cs.AsChar();
3094}
3095
3096inline size_t operator-(const wchar_t *p, const wxCStrData& cs)
3097{
3098 return p - cs.AsWChar();
3099}
3100
414b7b40
VZ
3101// ----------------------------------------------------------------------------
3102// implementation of wx[W]CharBuffer inline methods using wxCStrData
3103// ----------------------------------------------------------------------------
3104
11aac4ba
VS
3105// FIXME-UTF8: move this to buffer.h
3106inline wxCharBuffer::wxCharBuffer(const wxCStrData& cstr)
681e4412 3107 : wxCharTypeBufferBase(cstr.AsCharBuf())
11aac4ba
VS
3108{
3109}
3110
3111inline wxWCharBuffer::wxWCharBuffer(const wxCStrData& cstr)
681e4412 3112 : wxCharTypeBufferBase(cstr.AsWCharBuf())
414b7b40
VZ
3113{
3114}
3115
b0c4d5d7
VS
3116#if wxUSE_UNICODE_UTF8
3117// ----------------------------------------------------------------------------
3118// implementation of wxStringIteratorNode inline methods
3119// ----------------------------------------------------------------------------
3120
39c20230
VS
3121void wxStringIteratorNode::DoSet(const wxString *str,
3122 wxStringImpl::const_iterator *citer,
3123 wxStringImpl::iterator *iter)
b0c4d5d7 3124{
42fc0309 3125 m_prev = NULL;
39c20230
VS
3126 m_iter = iter;
3127 m_citer = citer;
3128 m_str = str;
3129 if ( str )
3130 {
3131 m_next = str->m_iterators.ptr;
3132 wx_const_cast(wxString*, m_str)->m_iterators.ptr = this;
3133 if ( m_next )
3134 m_next->m_prev = this;
3135 }
42fc0309
VS
3136 else
3137 {
3138 m_next = NULL;
3139 }
b0c4d5d7
VS
3140}
3141
39c20230 3142void wxStringIteratorNode::clear()
b0c4d5d7
VS
3143{
3144 if ( m_next )
3145 m_next->m_prev = m_prev;
3146 if ( m_prev )
3147 m_prev->m_next = m_next;
39c20230 3148 else if ( m_str ) // first in the list
b0c4d5d7 3149 wx_const_cast(wxString*, m_str)->m_iterators.ptr = m_next;
39c20230
VS
3150
3151 m_next = m_prev = NULL;
3152 m_citer = NULL;
3153 m_iter = NULL;
3154 m_str = NULL;
b0c4d5d7
VS
3155}
3156#endif // wxUSE_UNICODE_UTF8
3157
e3e8e3c0 3158#if WXWIN_COMPATIBILITY_2_8
3a3dde0d 3159 // lot of code out there doesn't explicitly include wx/crt.h, but uses
e3e8e3c0
VS
3160 // CRT wrappers that are now declared in wx/wxcrt.h and wx/wxcrtvararg.h,
3161 // so let's include this header now that wxString is defined and it's safe
3162 // to do it:
3a3dde0d 3163 #include "wx/crt.h"
e3e8e3c0
VS
3164#endif
3165
11aac4ba 3166#endif // _WX_WXSTRING_H_