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