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