]> git.saurik.com Git - wxWidgets.git/blame - include/wx/string.h
removed wx/wave.h which was deprecated since 2.4
[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
e90c1d2a
VZ
54#include "wx/wxchar.h" // for wxChar
55#include "wx/buffer.h" // for wxCharBuffer
56#include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes
3f4a0c5b 57
fb3c9042
VZ
58class WXDLLIMPEXP_BASE wxString;
59
c801d85f
KB
60// ---------------------------------------------------------------------------
61// macros
62// ---------------------------------------------------------------------------
63
5737d05f
VZ
64// casts [unfortunately!] needed to call some broken functions which require
65// "char *" instead of "const char *"
2bb67b80 66#define WXSTRINGCAST (wxChar *)(const wxChar *)
e90c1d2a
VZ
67#define wxCSTRINGCAST (wxChar *)(const wxChar *)
68#define wxMBSTRINGCAST (char *)(const char *)
69#define wxWCSTRINGCAST (wchar_t *)(const wchar_t *)
c801d85f 70
3c67202d 71// implementation only
5737d05f 72#define wxASSERT_VALID_INDEX(i) \
e87b7833 73 wxASSERT_MSG( (size_t)(i) <= length(), _T("invalid index in wxString") )
c801d85f 74
e90c1d2a
VZ
75// ----------------------------------------------------------------------------
76// constants
77// ----------------------------------------------------------------------------
78
13874b5c
VZ
79#if WXWIN_COMPATIBILITY_2_6
80
81// deprecated in favour of wxString::npos, don't use in new code
82//
e90c1d2a 83// maximum possible length for a string means "take all string" everywhere
2cbfa061 84#define wxSTRING_MAXLEN wxStringBase::npos
66b6b045 85
13874b5c
VZ
86#endif // WXWIN_COMPATIBILITY_2_6
87
e90c1d2a
VZ
88// ----------------------------------------------------------------------------
89// global data
90// ----------------------------------------------------------------------------
91
92// global pointer to empty string
bddd7a8d 93extern WXDLLIMPEXP_DATA_BASE(const wxChar*) wxEmptyString;
6001e347 94
c801d85f 95// ---------------------------------------------------------------------------
e90c1d2a 96// global functions complementing standard C string library replacements for
3c67202d
VZ
97// strlen() and portable strcasecmp()
98//---------------------------------------------------------------------------
e90c1d2a
VZ
99
100// Use wxXXX() functions from wxchar.h instead! These functions are for
101// backwards compatibility only.
88150e60 102
3c67202d 103// checks whether the passed in pointer is NULL and if the string is empty
6d56eb5c 104inline bool IsEmpty(const char *p) { return (!p || !*p); }
c801d85f 105
3c67202d 106// safe version of strlen() (returns 0 if passed NULL pointer)
6d56eb5c 107inline size_t Strlen(const char *psz)
c801d85f
KB
108 { return psz ? strlen(psz) : 0; }
109
3c67202d 110// portable strcasecmp/_stricmp
6d56eb5c 111inline int Stricmp(const char *psz1, const char *psz2)
dd1eaa89 112{
7f0586ef
JS
113#if defined(__VISUALC__) && defined(__WXWINCE__)
114 register char c1, c2;
115 do {
116 c1 = tolower(*psz1++);
117 c2 = tolower(*psz2++);
118 } while ( c1 && (c1 == c2) );
119
120 return c1 - c2;
121#elif defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) )
dd1eaa89 122 return _stricmp(psz1, psz2);
91b8de8d 123#elif defined(__SC__)
2432b92d 124 return _stricmp(psz1, psz2);
91b8de8d 125#elif defined(__SALFORDC__)
a3ef5bf5 126 return stricmp(psz1, psz2);
dd1eaa89
VZ
127#elif defined(__BORLANDC__)
128 return stricmp(psz1, psz2);
7be1f0d9
JS
129#elif defined(__WATCOMC__)
130 return stricmp(psz1, psz2);
14704334
VS
131#elif defined(__DJGPP__)
132 return stricmp(psz1, psz2);
91b8de8d
RR
133#elif defined(__EMX__)
134 return stricmp(psz1, psz2);
e2c87f4c 135#elif defined(__WXPM__)
1777b9bb 136 return stricmp(psz1, psz2);
a0be6908
WS
137#elif defined(__WXPALMOS__) || \
138 defined(HAVE_STRCASECMP_IN_STRING_H) || \
1c2d1459
VZ
139 defined(HAVE_STRCASECMP_IN_STRINGS_H) || \
140 defined(__GNUWIN32__)
dd1eaa89 141 return strcasecmp(psz1, psz2);
8be97d65 142#elif defined(__MWERKS__) && !defined(__INTEL__)
17dff81c
SC
143 register char c1, c2;
144 do {
145 c1 = tolower(*psz1++);
146 c2 = tolower(*psz2++);
147 } while ( c1 && (c1 == c2) );
148
149 return c1 - c2;
dd1eaa89
VZ
150#else
151 // almost all compilers/libraries provide this function (unfortunately under
152 // different names), that's why we don't implement our own which will surely
153 // be more efficient than this code (uncomment to use):
154 /*
155 register char c1, c2;
156 do {
157 c1 = tolower(*psz1++);
158 c2 = tolower(*psz2++);
159 } while ( c1 && (c1 == c2) );
160
161 return c1 - c2;
162 */
163
164 #error "Please define string case-insensitive compare for your OS/compiler"
165#endif // OS/compiler
166}
c801d85f 167
ce76f779
VZ
168// ----------------------------------------------------------------------------
169// deal with STL/non-STL/non-STL-but-wxUSE_STD_STRING
170// ----------------------------------------------------------------------------
171
172// in both cases we need to define wxStdString
47561b0d 173#if wxUSE_STL || wxUSE_STD_STRING
e87b7833
MB
174
175#include "wx/beforestd.h"
176#include <string>
177#include "wx/afterstd.h"
178
179#if wxUSE_UNICODE
0c7cfd99 180 #ifdef HAVE_STD_WSTRING
ce76f779 181 typedef std::wstring wxStdString;
e87b7833 182 #else
ce76f779 183 typedef std::basic_string<wxChar> wxStdString;
e87b7833
MB
184 #endif
185#else
ce76f779 186 typedef std::string wxStdString;
e87b7833
MB
187#endif
188
ce76f779 189#endif // need <string>
e87b7833 190
ce76f779
VZ
191#if wxUSE_STL
192
193 // we don't need an extra ctor from std::string when copy ctor already does
194 // the work
195 #undef wxUSE_STD_STRING
7147aa47 196 #define wxUSE_STD_STRING 0
ce76f779
VZ
197
198 #if (defined(__GNUG__) && (__GNUG__ < 3)) || \
199 (defined(_MSC_VER) && (_MSC_VER <= 1200))
200 #define wxSTRING_BASE_HASNT_CLEAR
201 #endif
202
203 typedef wxStdString wxStringBase;
e87b7833
MB
204#else // if !wxUSE_STL
205
47561b0d
MB
206#if !defined(HAVE_STD_STRING_COMPARE) && \
207 (!defined(__WX_SETUP_H__) || wxUSE_STL == 0)
e87b7833
MB
208 #define HAVE_STD_STRING_COMPARE
209#endif
210
c801d85f 211// ---------------------------------------------------------------------------
f04f3991 212// string data prepended with some housekeeping info (used by wxString class),
c801d85f
KB
213// is never used directly (but had to be put here to allow inlining)
214// ---------------------------------------------------------------------------
e90c1d2a 215
bddd7a8d 216struct WXDLLIMPEXP_BASE wxStringData
c801d85f
KB
217{
218 int nRefs; // reference count
3c024cc2 219 size_t nDataLength, // actual string length
c801d85f
KB
220 nAllocLength; // allocated memory size
221
2bb67b80
OK
222 // mimics declaration 'wxChar data[nAllocLength]'
223 wxChar* data() const { return (wxChar*)(this + 1); }
c801d85f
KB
224
225 // empty string has a special ref count so it's never deleted
dbda9e86
JS
226 bool IsEmpty() const { return (nRefs == -1); }
227 bool IsShared() const { return (nRefs > 1); }
c801d85f
KB
228
229 // lock/unlock
dd1eaa89 230 void Lock() { if ( !IsEmpty() ) nRefs++; }
f6bcfd97 231
8ecf21b7 232 // VC++ will refuse to inline Unlock but profiling shows that it is wrong
f6bcfd97 233#if defined(__VISUALC__) && (__VISUALC__ >= 1200)
1c2d1459 234 __forceinline
f6bcfd97 235#endif
ca5e07c7
GD
236 // VC++ free must take place in same DLL as allocation when using non dll
237 // run-time library (e.g. Multithreaded instead of Multithreaded DLL)
8ecf21b7
GD
238#if defined(__VISUALC__) && defined(_MT) && !defined(_DLL)
239 void Unlock() { if ( !IsEmpty() && --nRefs == 0) Free(); }
ca5e07c7
GD
240 // we must not inline deallocation since allocation is not inlined
241 void Free();
8ecf21b7 242#else
f1317a5d 243 void Unlock() { if ( !IsEmpty() && --nRefs == 0) free(this); }
8ecf21b7 244#endif
8fd0f20b 245
dd1eaa89 246 // if we had taken control over string memory (GetWriteBuf), it's
8fd0f20b 247 // intentionally put in invalid state
dbda9e86
JS
248 void Validate(bool b) { nRefs = (b ? 1 : 0); }
249 bool IsValid() const { return (nRefs != 0); }
c801d85f
KB
250};
251
e87b7833 252class WXDLLIMPEXP_BASE wxStringBase
3c67202d 253{
df5168c4 254#if !wxUSE_STL
bddd7a8d 255friend class WXDLLIMPEXP_BASE wxArrayString;
df5168c4 256#endif
5c0217af
SC
257public :
258 // an 'invalid' value for string index, moved to this place due to a CW bug
259 static const size_t npos;
e87b7833 260protected:
dd1eaa89 261 // points to data preceded by wxStringData structure with ref count info
2bb67b80 262 wxChar *m_pchData;
dd1eaa89
VZ
263
264 // accessor to string data
265 wxStringData* GetStringData() const { return (wxStringData*)m_pchData - 1; }
266
6b95b20d
VZ
267 // string (re)initialization functions
268 // initializes the string to the empty value (must be called only from
269 // ctors, use Reinit() otherwise)
e90c1d2a 270 void Init() { m_pchData = (wxChar *)wxEmptyString; }
90e572f1 271 // initializes the string with (a part of) C-string
e87b7833 272 void InitWith(const wxChar *psz, size_t nPos = 0, size_t nLen = npos);
6b95b20d
VZ
273 // as Init, but also frees old data
274 void Reinit() { GetStringData()->Unlock(); Init(); }
275
276 // memory allocation
b1801e0e
GD
277 // allocates memory for string of length nLen
278 bool AllocBuffer(size_t nLen);
6b95b20d 279 // copies data to another string
b1801e0e 280 bool AllocCopy(wxString&, int, int) const;
6b95b20d 281 // effectively copies data to string
b1801e0e 282 bool AssignCopy(size_t, const wxChar *);
6b95b20d
VZ
283
284 // append a (sub)string
e87b7833
MB
285 bool ConcatSelf(size_t nLen, const wxChar *src, size_t nMaxLen);
286 bool ConcatSelf(size_t nLen, const wxChar *src)
287 { return ConcatSelf(nLen, src, nLen); }
6b95b20d
VZ
288
289 // functions called before writing to the string: they copy it if there
290 // are other references to our data (should be the only owner when writing)
b1801e0e
GD
291 bool CopyBeforeWrite();
292 bool AllocBeforeWrite(size_t);
6b95b20d 293
e87b7833
MB
294 // compatibility with wxString
295 bool Alloc(size_t nLen);
296public:
297 // standard types
298 typedef wxChar value_type;
299 typedef wxChar char_type;
300 typedef size_t size_type;
301 typedef value_type& reference;
302 typedef const value_type& const_reference;
303 typedef value_type* pointer;
304 typedef const value_type* const_pointer;
305 typedef value_type *iterator;
306 typedef const value_type *const_iterator;
00b4a13e 307
968f291b
VZ
308#define wxSTRING_REVERSE_ITERATOR(name, const_or_not) \
309 class name \
310 { \
311 public: \
312 typedef wxChar value_type; \
313 typedef const_or_not value_type& reference; \
314 typedef const_or_not value_type *pointer; \
315 typedef const_or_not value_type *iterator_type; \
316 \
317 name(iterator_type i) : m_cur(i) { } \
318 name(const name& ri) : m_cur(ri.m_cur) { } \
319 \
320 iterator_type base() const { return m_cur; } \
321 \
322 reference operator*() const { return *(m_cur - 1); } \
968f291b
VZ
323 \
324 name& operator++() { --m_cur; return *this; } \
325 name operator++(int) { name tmp = *this; --m_cur; return tmp; } \
326 name& operator--() { ++m_cur; return *this; } \
327 name operator--(int) { name tmp = *this; ++m_cur; return tmp; } \
328 \
329 bool operator==(name ri) const { return m_cur == ri.m_cur; } \
330 bool operator!=(name ri) const { return !(*this == ri); } \
331 \
332 private: \
333 iterator_type m_cur; \
334 }
335
336 wxSTRING_REVERSE_ITERATOR(const_reverse_iterator, const);
337
338 #define wxSTRING_CONST
339 wxSTRING_REVERSE_ITERATOR(reverse_iterator, wxSTRING_CONST);
340 #undef wxSTRING_CONST
341
342 #undef wxSTRING_REVERSE_ITERATOR
343
344
3c67202d
VZ
345 // constructors and destructor
346 // ctor for an empty string
e87b7833 347 wxStringBase() { Init(); }
3c67202d 348 // copy ctor
e87b7833 349 wxStringBase(const wxStringBase& stringSrc)
6b95b20d 350 {
09443a26
VZ
351 wxASSERT_MSG( stringSrc.GetStringData()->IsValid(),
352 _T("did you forget to call UngetWriteBuf()?") );
6b95b20d 353
e87b7833 354 if ( stringSrc.empty() ) {
6b95b20d
VZ
355 // nothing to do for an empty string
356 Init();
357 }
358 else {
359 m_pchData = stringSrc.m_pchData; // share same data
360 GetStringData()->Lock(); // => one more copy
361 }
362 }
3c67202d 363 // string containing nRepeat copies of ch
e87b7833 364 wxStringBase(size_type nRepeat, wxChar ch);
3c67202d 365 // ctor takes first nLength characters from C string
e87b7833
MB
366 // (default value of npos means take all the string)
367 wxStringBase(const wxChar *psz)
368 { InitWith(psz, 0, npos); }
369 wxStringBase(const wxChar *psz, size_t nLength)
b1801e0e 370 { InitWith(psz, 0, nLength); }
830f8f11
VZ
371 wxStringBase(const wxChar *psz,
372 const wxMBConv& WXUNUSED(conv),
373 size_t nLength = npos)
b1801e0e 374 { InitWith(psz, 0, nLength); }
e87b7833
MB
375 // take nLen chars starting at nPos
376 wxStringBase(const wxStringBase& str, size_t nPos, size_t nLen)
377 {
378 wxASSERT_MSG( str.GetStringData()->IsValid(),
379 _T("did you forget to call UngetWriteBuf()?") );
380 Init();
381 size_t strLen = str.length() - nPos; nLen = strLen < nLen ? strLen : nLen;
382 InitWith(str.c_str(), nPos, nLen);
383 }
384 // take all characters from pStart to pEnd
385 wxStringBase(const void *pStart, const void *pEnd);
386
387 // dtor is not virtual, this class must not be inherited from!
1c2d1459
VZ
388 ~wxStringBase()
389 {
7b758e8f 390#if defined(__VISUALC__) && (__VISUALC__ >= 1200)
1c2d1459
VZ
391 //RN - according to the above VC++ does indeed inline this,
392 //even though it spits out two warnings
393 #pragma warning (disable:4714)
7b758e8f 394#endif
e87b7833 395
1c2d1459 396 GetStringData()->Unlock();
7b758e8f
RN
397 }
398
399#if defined(__VISUALC__) && (__VISUALC__ >= 1200)
1c2d1459
VZ
400 //re-enable inlining warning
401 #pragma warning (default:4714)
402#endif
e87b7833
MB
403 // overloaded assignment
404 // from another wxString
405 wxStringBase& operator=(const wxStringBase& stringSrc);
406 // from a character
407 wxStringBase& operator=(wxChar ch);
408 // from a C string
409 wxStringBase& operator=(const wxChar *psz);
410
411 // return the length of the string
20aa779e 412 size_type length() const { return GetStringData()->nDataLength; }
e87b7833 413 // return the length of the string
20aa779e 414 size_type size() const { return length(); }
e87b7833 415 // return the maximum size of the string
13874b5c 416 size_type max_size() const { return npos; }
e87b7833
MB
417 // resize the string, filling the space with c if c != 0
418 void resize(size_t nSize, wxChar ch = wxT('\0'));
419 // delete the contents of the string
420 void clear() { erase(0, npos); }
421 // returns true if the string is empty
20aa779e 422 bool empty() const { return length() == 0; }
e87b7833
MB
423 // inform string about planned change in size
424 void reserve(size_t sz) { Alloc(sz); }
425 size_type capacity() const { return GetStringData()->nAllocLength; }
426
427 // lib.string.access
428 // return the character at position n
429 value_type at(size_type n) const
430 { wxASSERT_VALID_INDEX( n ); return m_pchData[n]; }
e87b7833
MB
431 // returns the writable character at position n
432 reference at(size_type n)
433 { wxASSERT_VALID_INDEX( n ); CopyBeforeWrite(); return m_pchData[n]; }
e87b7833
MB
434
435 // lib.string.modifiers
436 // append elements str[pos], ..., str[pos+n]
437 wxStringBase& append(const wxStringBase& str, size_t pos, size_t n)
438 {
439 wxASSERT(pos <= str.length());
440 ConcatSelf(n, str.c_str() + pos, str.length() - pos);
441 return *this;
442 }
443 // append a string
444 wxStringBase& append(const wxStringBase& str)
445 { ConcatSelf(str.length(), str.c_str()); return *this; }
446 // append first n (or all if n == npos) characters of sz
447 wxStringBase& append(const wxChar *sz)
448 { ConcatSelf(wxStrlen(sz), sz); return *this; }
449 wxStringBase& append(const wxChar *sz, size_t n)
450 { ConcatSelf(n, sz); return *this; }
451 // append n copies of ch
452 wxStringBase& append(size_t n, wxChar ch);
453 // append from first to last
454 wxStringBase& append(const_iterator first, const_iterator last)
455 { ConcatSelf(last - first, first); return *this; }
456
457 // same as `this_string = str'
458 wxStringBase& assign(const wxStringBase& str)
459 { return *this = str; }
460 // same as ` = str[pos..pos + n]
461 wxStringBase& assign(const wxStringBase& str, size_t pos, size_t n)
462 { clear(); return append(str, pos, n); }
463 // same as `= first n (or all if n == npos) characters of sz'
464 wxStringBase& assign(const wxChar *sz)
465 { clear(); return append(sz, wxStrlen(sz)); }
466 wxStringBase& assign(const wxChar *sz, size_t n)
467 { clear(); return append(sz, n); }
468 // same as `= n copies of ch'
469 wxStringBase& assign(size_t n, wxChar ch)
470 { clear(); return append(n, ch); }
471 // assign from first to last
472 wxStringBase& assign(const_iterator first, const_iterator last)
473 { clear(); return append(first, last); }
474
475 // first valid index position
476 const_iterator begin() const { return m_pchData; }
968f291b 477 iterator begin();
e87b7833
MB
478 // position one after the last valid one
479 const_iterator end() const { return m_pchData + length(); }
ac3c86ee 480 iterator end();
e87b7833 481
968f291b
VZ
482 // first element of the reversed string
483 const_reverse_iterator rbegin() const { return const_reverse_iterator(end()); }
484 reverse_iterator rbegin() { return reverse_iterator(end()); }
485 // one beyond the end of the reversed string
486 const_reverse_iterator rend() const { return const_reverse_iterator(begin()); }
487 reverse_iterator rend() { return reverse_iterator(begin()); }
488
e87b7833
MB
489 // insert another string
490 wxStringBase& insert(size_t nPos, const wxStringBase& str)
491 {
492 wxASSERT( str.GetStringData()->IsValid() );
493 return insert(nPos, str.c_str(), str.length());
494 }
495 // insert n chars of str starting at nStart (in str)
496 wxStringBase& insert(size_t nPos, const wxStringBase& str, size_t nStart, size_t n)
497 {
498 wxASSERT( str.GetStringData()->IsValid() );
499 wxASSERT( nStart < str.length() );
500 size_t strLen = str.length() - nStart;
501 n = strLen < n ? strLen : n;
502 return insert(nPos, str.c_str() + nStart, n);
503 }
504 // insert first n (or all if n == npos) characters of sz
505 wxStringBase& insert(size_t nPos, const wxChar *sz, size_t n = npos);
506 // insert n copies of ch
507 wxStringBase& insert(size_t nPos, size_t n, wxChar ch)
508 { return insert(nPos, wxStringBase(n, ch)); }
509 iterator insert(iterator it, wxChar ch)
1c2d1459 510 { size_t idx = it - begin(); insert(idx, 1, ch); return begin() + idx; }
e87b7833
MB
511 void insert(iterator it, const_iterator first, const_iterator last)
512 { insert(it - begin(), first, last - first); }
513 void insert(iterator it, size_type n, wxChar ch)
514 { insert(it - begin(), n, ch); }
515
516 // delete characters from nStart to nStart + nLen
517 wxStringBase& erase(size_type pos = 0, size_type n = npos);
518 iterator erase(iterator first, iterator last)
519 {
520 size_t idx = first - begin();
521 erase(idx, last - first);
522 return begin() + idx;
523 }
524 iterator erase(iterator first);
525
526 // explicit conversion to C string (use this with printf()!)
527 const wxChar* c_str() const { return m_pchData; }
528 const wxChar* data() const { return m_pchData; }
529
530 // replaces the substring of length nLen starting at nStart
531 wxStringBase& replace(size_t nStart, size_t nLen, const wxChar* sz);
532 // replaces the substring of length nLen starting at nStart
533 wxStringBase& replace(size_t nStart, size_t nLen, const wxStringBase& str)
534 { return replace(nStart, nLen, str.c_str()); }
535 // replaces the substring with nCount copies of ch
536 wxStringBase& replace(size_t nStart, size_t nLen, size_t nCount, wxChar ch);
537 // replaces a substring with another substring
538 wxStringBase& replace(size_t nStart, size_t nLen,
539 const wxStringBase& str, size_t nStart2, size_t nLen2);
540 // replaces the substring with first nCount chars of sz
541 wxStringBase& replace(size_t nStart, size_t nLen,
542 const wxChar* sz, size_t nCount);
543 wxStringBase& replace(iterator first, iterator last, const_pointer s)
544 { return replace(first - begin(), last - first, s); }
545 wxStringBase& replace(iterator first, iterator last, const_pointer s,
546 size_type n)
547 { return replace(first - begin(), last - first, s, n); }
548 wxStringBase& replace(iterator first, iterator last, const wxStringBase& s)
549 { return replace(first - begin(), last - first, s); }
550 wxStringBase& replace(iterator first, iterator last, size_type n, wxChar c)
551 { return replace(first - begin(), last - first, n, c); }
552 wxStringBase& replace(iterator first, iterator last,
553 const_iterator first1, const_iterator last1)
554 { return replace(first - begin(), last - first, first1, last1 - first1); }
555
556 // swap two strings
557 void swap(wxStringBase& str);
558
559 // All find() functions take the nStart argument which specifies the
560 // position to start the search on, the default value is 0. All functions
561 // return npos if there were no match.
562
563 // find a substring
564 size_t find(const wxStringBase& str, size_t nStart = 0) const;
565
e87b7833
MB
566 // find first n characters of sz
567 size_t find(const wxChar* sz, size_t nStart = 0, size_t n = npos) const;
e87b7833 568
e87b7833
MB
569 // find the first occurence of character ch after nStart
570 size_t find(wxChar ch, size_t nStart = 0) const;
1412634b 571
e87b7833
MB
572 // rfind() family is exactly like find() but works right to left
573
574 // as find, but from the end
575 size_t rfind(const wxStringBase& str, size_t nStart = npos) const;
576
e87b7833
MB
577 // as find, but from the end
578 size_t rfind(const wxChar* sz, size_t nStart = npos,
579 size_t n = npos) const;
580 // as find, but from the end
581 size_t rfind(wxChar ch, size_t nStart = npos) const;
582
583 // find first/last occurence of any character in the set
584
585 // as strpbrk() but starts at nStart, returns npos if not found
586 size_t find_first_of(const wxStringBase& str, size_t nStart = 0) const
587 { return find_first_of(str.c_str(), nStart); }
588 // same as above
589 size_t find_first_of(const wxChar* sz, size_t nStart = 0) const;
e2101186 590 size_t find_first_of(const wxChar* sz, size_t nStart, size_t n) const;
e87b7833
MB
591 // same as find(char, size_t)
592 size_t find_first_of(wxChar c, size_t nStart = 0) const
593 { return find(c, nStart); }
594 // find the last (starting from nStart) char from str in this string
595 size_t find_last_of (const wxStringBase& str, size_t nStart = npos) const
596 { return find_last_of(str.c_str(), nStart); }
597 // same as above
598 size_t find_last_of (const wxChar* sz, size_t nStart = npos) const;
e2101186 599 size_t find_last_of(const wxChar* sz, size_t nStart, size_t n) const;
e87b7833
MB
600 // same as above
601 size_t find_last_of(wxChar c, size_t nStart = npos) const
602 { return rfind(c, nStart); }
603
604 // find first/last occurence of any character not in the set
605
606 // as strspn() (starting from nStart), returns npos on failure
607 size_t find_first_not_of(const wxStringBase& str, size_t nStart = 0) const
608 { return find_first_not_of(str.c_str(), nStart); }
609 // same as above
610 size_t find_first_not_of(const wxChar* sz, size_t nStart = 0) const;
e2101186 611 size_t find_first_not_of(const wxChar* sz, size_t nStart, size_t n) const;
e87b7833
MB
612 // same as above
613 size_t find_first_not_of(wxChar ch, size_t nStart = 0) const;
614 // as strcspn()
615 size_t find_last_not_of(const wxStringBase& str, size_t nStart = npos) const
e2101186 616 { return find_last_not_of(str.c_str(), nStart); }
e87b7833
MB
617 // same as above
618 size_t find_last_not_of(const wxChar* sz, size_t nStart = npos) const;
e2101186 619 size_t find_last_not_of(const wxChar* sz, size_t nStart, size_t n) const;
e87b7833
MB
620 // same as above
621 size_t find_last_not_of(wxChar ch, size_t nStart = npos) const;
622
623 // All compare functions return -1, 0 or 1 if the [sub]string is less,
624 // equal or greater than the compare() argument.
625
dcb68102
RN
626 // comparison with another string
627 int compare(const wxStringBase& str) const;
e87b7833
MB
628 // comparison with a substring
629 int compare(size_t nStart, size_t nLen, const wxStringBase& str) const;
630 // comparison of 2 substrings
631 int compare(size_t nStart, size_t nLen,
632 const wxStringBase& str, size_t nStart2, size_t nLen2) const;
dcb68102
RN
633 // comparison with a c string
634 int compare(const wxChar* sz) const;
e87b7833
MB
635 // substring comparison with first nCount characters of sz
636 int compare(size_t nStart, size_t nLen,
637 const wxChar* sz, size_t nCount = npos) const;
638
639 size_type copy(wxChar* s, size_type n, size_type pos = 0);
640
641 // substring extraction
642 wxStringBase substr(size_t nStart = 0, size_t nLen = npos) const;
643
644 // string += string
645 wxStringBase& operator+=(const wxStringBase& s) { return append(s); }
646 // string += C string
647 wxStringBase& operator+=(const wxChar *psz) { return append(psz); }
648 // string += char
649 wxStringBase& operator+=(wxChar ch) { return append(1, ch); }
650};
651
652#endif // !wxUSE_STL
653
b6339dde
VZ
654// ----------------------------------------------------------------------------
655// wxString: string class trying to be compatible with std::string, MFC
656// CString and wxWindows 1.x wxString all at once
e87b7833
MB
657// ---------------------------------------------------------------------------
658
659class WXDLLIMPEXP_BASE wxString : public wxStringBase
660{
661#if !wxUSE_STL
662friend class WXDLLIMPEXP_BASE wxArrayString;
663#endif
664
665 // NB: special care was taken in arranging the member functions in such order
666 // that all inline functions can be effectively inlined, verify that all
20aa779e 667 // performance critical functions are still inlined if you change order!
e87b7833
MB
668private:
669 // if we hadn't made these operators private, it would be possible to
670 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
671 // converted to char in C and we do have operator=(char)
672 //
673 // NB: we don't need other versions (short/long and unsigned) as attempt
674 // to assign another numeric type to wxString will now result in
675 // ambiguity between operator=(char) and operator=(int)
676 wxString& operator=(int);
677
678 // these methods are not implemented - there is _no_ conversion from int to
679 // string, you're doing something wrong if the compiler wants to call it!
680 //
681 // try `s << i' or `s.Printf("%d", i)' instead
682 wxString(int);
683
684public:
685 // constructors and destructor
686 // ctor for an empty string
687 wxString() : wxStringBase() { }
688 // copy ctor
689 wxString(const wxStringBase& stringSrc) : wxStringBase(stringSrc) { }
690 wxString(const wxString& stringSrc) : wxStringBase(stringSrc) { }
691 // string containing nRepeat copies of ch
692 wxString(wxChar ch, size_t nRepeat = 1)
693 : wxStringBase(nRepeat, ch) { }
694 wxString(size_t nRepeat, wxChar ch)
695 : wxStringBase(nRepeat, ch) { }
696 // ctor takes first nLength characters from C string
697 // (default value of npos means take all the string)
698 wxString(const wxChar *psz)
699 : wxStringBase(psz ? psz : wxT("")) { }
700 wxString(const wxChar *psz, size_t nLength)
701 : wxStringBase(psz, nLength) { }
830f8f11
VZ
702 wxString(const wxChar *psz,
703 const wxMBConv& WXUNUSED(conv),
704 size_t nLength = npos)
e87b7833 705 : wxStringBase(psz, nLength == npos ? wxStrlen(psz) : nLength) { }
e90c1d2a 706
20aa779e 707 // even if we're not built with wxUSE_STL == 1 it is very convenient to allow
ce76f779
VZ
708 // implicit conversions from std::string to wxString as this allows to use
709 // the same strings in non-GUI and GUI code, however we don't want to
710 // unconditionally add this ctor as it would make wx lib dependent on
711 // libstdc++ on some Linux versions which is bad, so instead we ask the
712 // client code to define this wxUSE_STD_STRING symbol if they need it
47561b0d 713#if wxUSE_STD_STRING
ce76f779
VZ
714 wxString(const wxStdString& s)
715 : wxStringBase(s.c_str()) { }
716#endif // wxUSE_STD_STRING
717
2bb67b80
OK
718#if wxUSE_UNICODE
719 // from multibyte string
d7330233
VS
720 wxString(const char *psz,
721 const wxMBConv& conv = wxConvLibc,
722 size_t nLength = npos);
2bb67b80 723 // from wxWCharBuffer (i.e. return from wxGetString)
e87b7833 724 wxString(const wxWCharBuffer& psz) : wxStringBase(psz.data()) { }
e90c1d2a 725#else // ANSI
3c67202d 726 // from C string (for compilers using unsigned char)
153958f7
VZ
727 wxString(const unsigned char* psz)
728 : wxStringBase((const char*)psz) { }
729 // from part of C string (for compilers using unsigned char)
730 wxString(const unsigned char* psz, size_t nLength)
e87b7833 731 : wxStringBase((const char*)psz, nLength) { }
e90c1d2a 732
6f841509 733#if wxUSE_WCHAR_T
2bb67b80 734 // from wide (Unicode) string
830f8f11
VZ
735 wxString(const wchar_t *pwz,
736 const wxMBConv& conv = wxConvLibc,
737 size_t nLength = npos);
e90c1d2a
VZ
738#endif // !wxUSE_WCHAR_T
739
2bb67b80
OK
740 // from wxCharBuffer
741 wxString(const wxCharBuffer& psz)
051aa330 742 : wxStringBase(psz) { }
e90c1d2a
VZ
743#endif // Unicode/ANSI
744
3c67202d
VZ
745 // generic attributes & operations
746 // as standard strlen()
e87b7833 747 size_t Len() const { return length(); }
3c67202d 748 // string contains any characters?
e87b7833 749 bool IsEmpty() const { return empty(); }
d775fa82 750 // empty string is "false", so !str will return true
20aa779e 751 bool operator!() const { return empty(); }
735d1db6
VZ
752 // truncate the string to given length
753 wxString& Truncate(size_t uiLen);
3c67202d 754 // empty string contents
dd1eaa89
VZ
755 void Empty()
756 {
735d1db6 757 Truncate(0);
dd1eaa89 758
5a8231ef 759 wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") );
7be07660 760 }
3c67202d 761 // empty the string and free memory
7be07660
VZ
762 void Clear()
763 {
e87b7833
MB
764 wxString tmp(wxEmptyString);
765 swap(tmp);
dd1eaa89
VZ
766 }
767
3c67202d
VZ
768 // contents test
769 // Is an ascii value
c801d85f 770 bool IsAscii() const;
3c67202d 771 // Is a number
c801d85f 772 bool IsNumber() const;
3c67202d 773 // Is a word
c801d85f 774 bool IsWord() const;
c801d85f 775
3c67202d
VZ
776 // data access (all indexes are 0 based)
777 // read access
2bb67b80 778 wxChar GetChar(size_t n) const
9d9ad673 779 { return at(n); }
3c67202d 780 // read/write access
2bb67b80 781 wxChar& GetWritableChar(size_t n)
9d9ad673 782 { return at(n); }
3c67202d 783 // write access
2bb67b80 784 void SetChar(size_t n, wxChar ch)
9d9ad673 785 { at(n) = ch; }
c801d85f 786
3c67202d 787 // get last character
2bb67b80 788 wxChar Last() const
09443a26 789 {
5a8231ef 790 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
09443a26 791
9d9ad673 792 return at(length() - 1);
09443a26
VZ
793 }
794
3c67202d 795 // get writable last character
2bb67b80 796 wxChar& Last()
09443a26 797 {
5a8231ef 798 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
9d9ad673 799 return at(length() - 1);
09443a26 800 }
c801d85f 801
d836ee96 802 /*
9d9ad673 803 Note that we we must define all of the overloads below to avoid
01398433
VZ
804 ambiguity when using str[0]. Also note that for a conforming compiler we
805 don't need const version of operatorp[] at all as indexed access to
806 const string is provided by implicit conversion to "const wxChar *"
807 below and defining them would only result in ambiguities, but some other
808 compilers refuse to compile "str[0]" without them.
d836ee96
VZ
809 */
810
01398433
VZ
811#if defined(__BORLANDC__) || defined(__WATCOMC__) || defined(__MWERKS__)
812 wxChar operator[](int n) const
813 { return wxStringBase::at(n); }
814 wxChar operator[](size_type n) const
815 { return wxStringBase::at(n); }
816#ifndef wxSIZE_T_IS_UINT
817 wxChar operator[](unsigned int n) const
818 { return wxStringBase::at(n); }
819#endif // size_t != unsigned int
820#endif // broken compiler
821
822
9d9ad673
VZ
823 // operator versions of GetWriteableChar()
824 wxChar& operator[](int n)
825 { return wxStringBase::at(n); }
e87b7833 826 wxChar& operator[](size_type n)
9d9ad673 827 { return wxStringBase::at(n); }
d836ee96 828#ifndef wxSIZE_T_IS_UINT
d836ee96 829 wxChar& operator[](unsigned int n)
9d9ad673 830 { return wxStringBase::at(n); }
d836ee96 831#endif // size_t != unsigned int
c801d85f 832
3c67202d 833 // implicit conversion to C string
e87b7833 834 operator const wxChar*() const { return c_str(); }
e015c2a3 835
e015c2a3 836 // identical to c_str(), for wxWin 1.6x compatibility
e87b7833 837 const wxChar* wx_str() const { return c_str(); }
e015c2a3 838 // identical to c_str(), for MFC compatibility
e87b7833 839 const wxChar* GetData() const { return c_str(); }
e90c1d2a 840
e015c2a3
VZ
841 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
842 // converting numbers or strings which are certain not to contain special
843 // chars (typically system functions, X atoms, environment variables etc.)
844 //
845 // the behaviour of these functions with the strings containing anything
846 // else than 7 bit ASCII characters is undefined, use at your own risk.
b1ac3b56 847#if wxUSE_UNICODE
2b5f62a0
VZ
848 static wxString FromAscii(const char *ascii); // string
849 static wxString FromAscii(const char ascii); // char
b1ac3b56 850 const wxCharBuffer ToAscii() const;
e015c2a3
VZ
851#else // ANSI
852 static wxString FromAscii(const char *ascii) { return wxString( ascii ); }
2b5f62a0 853 static wxString FromAscii(const char ascii) { return wxString( ascii ); }
e015c2a3
VZ
854 const char *ToAscii() const { return c_str(); }
855#endif // Unicode/!Unicode
b1ac3b56 856
2b5f62a0 857 // conversions with (possible) format conversions: have to return a
e90c1d2a 858 // buffer with temporary data
f6bcfd97
BP
859 //
860 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
861 // return an ANSI (multibyte) string, wc_str() to return a wide string and
862 // fn_str() to return a string which should be used with the OS APIs
863 // accepting the file names. The return value is always the same, but the
864 // type differs because a function may either return pointer to the buffer
865 // directly or have to use intermediate buffer for translation.
2bb67b80 866#if wxUSE_UNICODE
830f8f11 867 const wxCharBuffer mb_str(const wxMBConv& conv = wxConvLibc) const;
f6bcfd97 868
e90c1d2a
VZ
869 const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); }
870
e87b7833 871 const wxChar* wc_str() const { return c_str(); }
f6bcfd97
BP
872
873 // for compatibility with !wxUSE_UNICODE version
830f8f11 874 const wxChar* wc_str(const wxMBConv& WXUNUSED(conv)) const { return c_str(); }
e90c1d2a 875
2bb67b80 876#if wxMBFILES
5f709e67 877 const wxCharBuffer fn_str() const { return mb_str(wxConvFile); }
e90c1d2a 878#else // !wxMBFILES
e87b7833 879 const wxChar* fn_str() const { return c_str(); }
e90c1d2a
VZ
880#endif // wxMBFILES/!wxMBFILES
881#else // ANSI
e87b7833 882 const wxChar* mb_str() const { return c_str(); }
f6bcfd97
BP
883
884 // for compatibility with wxUSE_UNICODE version
830f8f11 885 const wxChar* mb_str(const wxMBConv& WXUNUSED(conv)) const { return c_str(); }
f6bcfd97 886
e90c1d2a 887 const wxWX2MBbuf mbc_str() const { return mb_str(); }
f6bcfd97 888
6f841509 889#if wxUSE_WCHAR_T
830f8f11 890 const wxWCharBuffer wc_str(const wxMBConv& conv) const;
e90c1d2a 891#endif // wxUSE_WCHAR_T
d5c8817c
SC
892#ifdef __WXOSX__
893 const wxCharBuffer fn_str() const { return wxConvFile.cWC2WX( wc_str( wxConvLocal ) ); }
894#else
e87b7833 895 const wxChar* fn_str() const { return c_str(); }
d5c8817c 896#endif
e90c1d2a 897#endif // Unicode/ANSI
c801d85f 898
3c67202d
VZ
899 // overloaded assignment
900 // from another wxString
e87b7833
MB
901 wxString& operator=(const wxStringBase& stringSrc)
902 { return (wxString&)wxStringBase::operator=(stringSrc); }
3c67202d 903 // from a character
e87b7833
MB
904 wxString& operator=(wxChar ch)
905 { return (wxString&)wxStringBase::operator=(ch); }
c57e3bd5
RN
906 // from a C string - STL probably will crash on NULL,
907 // so we need to compensate in that case
908#if wxUSE_STL
909 wxString& operator=(const wxChar *psz)
910 { if(psz) wxStringBase::operator=(psz); else Clear(); return *this; }
911#else
e87b7833
MB
912 wxString& operator=(const wxChar *psz)
913 { return (wxString&)wxStringBase::operator=(psz); }
c57e3bd5
RN
914#endif
915
111bb7f2
OK
916#if wxUSE_UNICODE
917 // from wxWCharBuffer
b1801e0e 918 wxString& operator=(const wxWCharBuffer& psz)
e87b7833 919 { (void) operator=((const wchar_t *)psz); return *this; }
d7330233
VS
920 // from C string
921 wxString& operator=(const char* psz)
922 { return operator=(wxString(psz)); }
e90c1d2a 923#else // ANSI
3c67202d 924 // from another kind of C string
c801d85f 925 wxString& operator=(const unsigned char* psz);
6f841509 926#if wxUSE_WCHAR_T
3c67202d 927 // from a wide string
c801d85f 928 wxString& operator=(const wchar_t *pwz);
6f841509 929#endif
111bb7f2 930 // from wxCharBuffer
b1801e0e 931 wxString& operator=(const wxCharBuffer& psz)
e87b7833 932 { (void) operator=((const char *)psz); return *this; }
e90c1d2a 933#endif // Unicode/ANSI
3c67202d
VZ
934
935 // string concatenation
936 // in place concatenation
937 /*
938 Concatenate and return the result. Note that the left to right
939 associativity of << allows to write things like "str << str1 << str2
940 << ..." (unlike with +=)
941 */
942 // string += string
dd1eaa89
VZ
943 wxString& operator<<(const wxString& s)
944 {
e87b7833 945#if !wxUSE_STL
09443a26
VZ
946 wxASSERT_MSG( s.GetStringData()->IsValid(),
947 _T("did you forget to call UngetWriteBuf()?") );
e87b7833 948#endif
dd1eaa89 949
e87b7833 950 append(s);
dd1eaa89
VZ
951 return *this;
952 }
3c67202d 953 // string += C string
2bb67b80 954 wxString& operator<<(const wxChar *psz)
e87b7833 955 { append(psz); return *this; }
3c67202d 956 // string += char
e87b7833 957 wxString& operator<<(wxChar ch) { append(1, ch); return *this; }
2bb67b80
OK
958
959 // string += buffer (i.e. from wxGetString)
960#if wxUSE_UNICODE
09443a26 961 wxString& operator<<(const wxWCharBuffer& s)
d7330233
VS
962 { return operator<<((const wchar_t *)s); }
963 wxString& operator+=(const wxWCharBuffer& s)
964 { return operator<<((const wchar_t *)s); }
09443a26
VZ
965#else // !wxUSE_UNICODE
966 wxString& operator<<(const wxCharBuffer& s)
d7330233
VS
967 { return operator<<((const char *)s); }
968 wxString& operator+=(const wxCharBuffer& s)
969 { return operator<<((const char *)s); }
09443a26 970#endif // wxUSE_UNICODE/!wxUSE_UNICODE
6b95b20d 971
d7330233
VS
972#if wxUSE_UNICODE
973 // string += C string in Unicode build (with conversion)
974 wxString& operator<<(const char *s)
975 { return operator<<(wxString(s)); }
976 wxString& operator+=(const char *s)
977 { return operator+=(wxString(s)); }
978#endif // wxUSE_UNICODE
979
3c67202d 980 // string += C string
09443a26
VZ
981 wxString& Append(const wxString& s)
982 {
5a8231ef
WS
983 // test for empty() to share the string if possible
984 if ( empty() )
09443a26
VZ
985 *this = s;
986 else
e87b7833 987 append(s);
09443a26
VZ
988 return *this;
989 }
2bb67b80 990 wxString& Append(const wxChar* psz)
e87b7833 991 { append(psz); return *this; }
3c67202d 992 // append count copies of given character
2bb67b80 993 wxString& Append(wxChar ch, size_t count = 1u)
e87b7833 994 { append(count, ch); return *this; }
8f06a017 995 wxString& Append(const wxChar* psz, size_t nLen)
e87b7833 996 { append(psz, nLen); return *this; }
3c67202d
VZ
997
998 // prepend a string, return the string itself
999 wxString& Prepend(const wxString& str)
1000 { *this = str + *this; return *this; }
1001
1002 // non-destructive concatenation
1fc8cfbd
VZ
1003 // two strings
1004 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string1,
1005 const wxString& string2);
1006 // string with a single char
bddd7a8d 1007 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxChar ch);
1fc8cfbd 1008 // char with a string
bddd7a8d 1009 friend wxString WXDLLIMPEXP_BASE operator+(wxChar ch, const wxString& string);
1fc8cfbd
VZ
1010 // string with C string
1011 friend wxString WXDLLIMPEXP_BASE operator+(const wxString& string,
1012 const wxChar *psz);
1013 // C string with string
1014 friend wxString WXDLLIMPEXP_BASE operator+(const wxChar *psz,
1015 const wxString& string);
3c67202d
VZ
1016
1017 // stream-like functions
1018 // insert an int into string
3ce65f6c
VZ
1019 wxString& operator<<(int i)
1020 { return (*this) << Format(_T("%d"), i); }
1021 // insert an unsigned int into string
1022 wxString& operator<<(unsigned int ui)
1023 { return (*this) << Format(_T("%u"), ui); }
1024 // insert a long into string
1025 wxString& operator<<(long l)
1026 { return (*this) << Format(_T("%ld"), l); }
1027 // insert an unsigned long into string
1028 wxString& operator<<(unsigned long ul)
1029 { return (*this) << Format(_T("%lu"), ul); }
3fc1adbd
MW
1030#if defined wxLongLong_t && !defined wxLongLongIsLong
1031 // insert a long long if they exist and aren't longs
1032 wxString& operator<<(wxLongLong_t ll)
b7859132
MW
1033 {
1034 const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("d");
1035 return (*this) << Format(fmt, ll);
1036 }
3fc1adbd
MW
1037 // insert an unsigned long long
1038 wxString& operator<<(wxULongLong_t ull)
b7859132
MW
1039 {
1040 const wxChar *fmt = _T("%") wxLongLongFmtSpec _T("u");
1041 return (*this) << Format(fmt , ull);
1042 }
3fc1adbd 1043#endif
3c67202d 1044 // insert a float into string
3ce65f6c
VZ
1045 wxString& operator<<(float f)
1046 { return (*this) << Format(_T("%f"), f); }
3c67202d 1047 // insert a double into string
3ce65f6c
VZ
1048 wxString& operator<<(double d)
1049 { return (*this) << Format(_T("%g"), d); }
c84c52de 1050
3c67202d 1051 // string comparison
30b21f9a 1052 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
dcb68102
RN
1053 int Cmp(const wxChar *psz) const;
1054 int Cmp(const wxString& s) const;
3c67202d 1055 // same as Cmp() but not case-sensitive
dcb68102
RN
1056 int CmpNoCase(const wxChar *psz) const;
1057 int CmpNoCase(const wxString& s) const;
3c67202d
VZ
1058 // test for the string equality, either considering case or not
1059 // (if compareWithCase then the case matters)
d775fa82 1060 bool IsSameAs(const wxChar *psz, bool compareWithCase = true) const
3c67202d 1061 { return (compareWithCase ? Cmp(psz) : CmpNoCase(psz)) == 0; }
20aa779e 1062 // comparison with a single character: returns true if equal
d775fa82 1063 bool IsSameAs(wxChar c, bool compareWithCase = true) const
f33fee2a 1064 {
e87b7833 1065 return (length() == 1) && (compareWithCase ? GetChar(0u) == c
f33fee2a
VZ
1066 : wxToupper(GetChar(0u)) == wxToupper(c));
1067 }
3c67202d
VZ
1068
1069 // simple sub-string extraction
1070 // return substring starting at nFirst of length nCount (or till the end
1071 // if nCount = default value)
e87b7833 1072 wxString Mid(size_t nFirst, size_t nCount = npos) const;
3c67202d 1073
f6bcfd97 1074 // operator version of Mid()
3c67202d
VZ
1075 wxString operator()(size_t start, size_t len) const
1076 { return Mid(start, len); }
1077
3affcd07
VZ
1078 // check if the string starts with the given prefix and return the rest
1079 // of the string in the provided pointer if it is not NULL; otherwise
1080 // return false
f6bcfd97 1081 bool StartsWith(const wxChar *prefix, wxString *rest = NULL) const;
3affcd07
VZ
1082 // check if the string ends with the given suffix and return the
1083 // beginning of the string before the suffix in the provided pointer if
1084 // it is not NULL; otherwise return false
1085 bool EndsWith(const wxChar *suffix, wxString *rest = NULL) const;
f6bcfd97 1086
3c67202d 1087 // get first nCount characters
c801d85f 1088 wxString Left(size_t nCount) const;
3c67202d 1089 // get last nCount characters
c801d85f 1090 wxString Right(size_t nCount) const;
c0881dc3 1091 // get all characters before the first occurance of ch
3c67202d 1092 // (returns the whole string if ch not found)
2bb67b80 1093 wxString BeforeFirst(wxChar ch) const;
3c67202d
VZ
1094 // get all characters before the last occurence of ch
1095 // (returns empty string if ch not found)
2bb67b80 1096 wxString BeforeLast(wxChar ch) const;
3c67202d
VZ
1097 // get all characters after the first occurence of ch
1098 // (returns empty string if ch not found)
2bb67b80 1099 wxString AfterFirst(wxChar ch) const;
3c67202d
VZ
1100 // get all characters after the last occurence of ch
1101 // (returns the whole string if ch not found)
2bb67b80 1102 wxString AfterLast(wxChar ch) const;
3c67202d
VZ
1103
1104 // for compatibility only, use more explicitly named functions above
2bb67b80
OK
1105 wxString Before(wxChar ch) const { return BeforeLast(ch); }
1106 wxString After(wxChar ch) const { return AfterFirst(ch); }
3c67202d
VZ
1107
1108 // case conversion
c84c52de 1109 // convert to upper case in place, return the string itself
c801d85f 1110 wxString& MakeUpper();
c84c52de 1111 // convert to upper case, return the copy of the string
03ab016d
JS
1112 // Here's something to remember: BC++ doesn't like returns in inlines.
1113 wxString Upper() const ;
c84c52de 1114 // convert to lower case in place, return the string itself
c801d85f 1115 wxString& MakeLower();
c84c52de 1116 // convert to lower case, return the copy of the string
03ab016d 1117 wxString Lower() const ;
c801d85f 1118
3c67202d
VZ
1119 // trimming/padding whitespace (either side) and truncating
1120 // remove spaces from left or from right (default) side
d775fa82 1121 wxString& Trim(bool bFromRight = true);
3c67202d 1122 // add nCount copies chPad in the beginning or at the end (default)
d775fa82 1123 wxString& Pad(size_t nCount, wxChar chPad = wxT(' '), bool bFromRight = true);
dd1eaa89 1124
3c67202d
VZ
1125 // searching and replacing
1126 // searching (return starting index, or -1 if not found)
d775fa82 1127 int Find(wxChar ch, bool bFromEnd = false) const; // like strchr/strrchr
3c67202d 1128 // searching (return starting index, or -1 if not found)
2bb67b80 1129 int Find(const wxChar *pszSub) const; // like strstr
3c67202d
VZ
1130 // replace first (or all of bReplaceAll) occurences of substring with
1131 // another string, returns the number of replacements made
2bb67b80
OK
1132 size_t Replace(const wxChar *szOld,
1133 const wxChar *szNew,
d775fa82 1134 bool bReplaceAll = true);
3c67202d
VZ
1135
1136 // check if the string contents matches a mask containing '*' and '?'
2bb67b80 1137 bool Matches(const wxChar *szMask) const;
c801d85f 1138
d775fa82 1139 // conversion to numbers: all functions return true only if the whole
538f35cc
VZ
1140 // string is a number and put the value of this number into the pointer
1141 // provided, the base is the numeric base in which the conversion should be
1142 // done and must be comprised between 2 and 36 or be 0 in which case the
1143 // standard C rules apply (leading '0' => octal, "0x" => hex)
cd0b1709 1144 // convert to a signed integer
538f35cc 1145 bool ToLong(long *val, int base = 10) const;
cd0b1709 1146 // convert to an unsigned integer
538f35cc 1147 bool ToULong(unsigned long *val, int base = 10) const;
d6718dd1
VZ
1148 // convert to wxLongLong
1149#if defined(wxLongLong_t)
1150 bool ToLongLong(wxLongLong_t *val, int base = 10) const;
1151 // convert to wxULongLong
1152 bool ToULongLong(wxULongLong_t *val, int base = 10) const;
1153#endif // wxLongLong_t
cd0b1709
VZ
1154 // convert to a double
1155 bool ToDouble(double *val) const;
1156
d6718dd1
VZ
1157
1158
90e572f1 1159 // formatted input/output
3c67202d 1160 // as sprintf(), returns the number of characters written or < 0 on error
7357f981
GD
1161 // (take 'this' into account in attribute parameter count)
1162 int Printf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2;
3c67202d 1163 // as vprintf(), returns the number of characters written or < 0 on error
2bb67b80 1164 int PrintfV(const wxChar* pszFormat, va_list argptr);
dd1eaa89 1165
341e7d28 1166 // returns the string containing the result of Printf() to it
7357f981 1167 static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1;
341e7d28
VZ
1168 // the same as above, but takes a va_list
1169 static wxString FormatV(const wxChar *pszFormat, va_list argptr);
1170
3c67202d
VZ
1171 // raw access to string memory
1172 // ensure that string has space for at least nLen characters
dd1eaa89 1173 // only works if the data of this string is not shared
e87b7833 1174 bool Alloc(size_t nLen) { reserve(nLen); /*return capacity() >= nLen;*/ return true; }
3c67202d 1175 // minimize the string's memory
dd1eaa89 1176 // only works if the data of this string is not shared
b1801e0e 1177 bool Shrink();
e87b7833 1178#if !wxUSE_STL
3c67202d
VZ
1179 // get writable buffer of at least nLen bytes. Unget() *must* be called
1180 // a.s.a.p. to put string back in a reasonable state!
2bb67b80 1181 wxChar *GetWriteBuf(size_t nLen);
3c67202d 1182 // call this immediately after GetWriteBuf() has been used
8fd0f20b 1183 void UngetWriteBuf();
8f06a017 1184 void UngetWriteBuf(size_t nLen);
e87b7833 1185#endif
c801d85f 1186
77ffb593 1187 // wxWidgets version 1 compatibility functions
3c67202d
VZ
1188
1189 // use Mid()
1190 wxString SubString(size_t from, size_t to) const
1191 { return Mid(from, (to - from + 1)); }
1192 // values for second parameter of CompareTo function
c801d85f 1193 enum caseCompare {exact, ignoreCase};
3c67202d 1194 // values for first parameter of Strip function
c801d85f 1195 enum stripType {leading = 0x1, trailing = 0x2, both = 0x3};
8870c26e 1196
7357f981
GD
1197 // use Printf()
1198 // (take 'this' into account in attribute parameter count)
1199 int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2;
c801d85f 1200
3c67202d 1201 // use Cmp()
2bb67b80 1202 inline int CompareTo(const wxChar* psz, caseCompare cmp = exact) const
6b95b20d 1203 { return cmp == exact ? Cmp(psz) : CmpNoCase(psz); }
c801d85f 1204
3c67202d 1205 // use Len
e87b7833 1206 size_t Length() const { return length(); }
3c67202d 1207 // Count the number of characters
2bb67b80 1208 int Freq(wxChar ch) const;
3c67202d 1209 // use MakeLower
c801d85f 1210 void LowerCase() { MakeLower(); }
3c67202d 1211 // use MakeUpper
c801d85f 1212 void UpperCase() { MakeUpper(); }
3c67202d 1213 // use Trim except that it doesn't change this string
c801d85f
KB
1214 wxString Strip(stripType w = trailing) const;
1215
3c67202d 1216 // use Find (more general variants not yet supported)
2bb67b80
OK
1217 size_t Index(const wxChar* psz) const { return Find(psz); }
1218 size_t Index(wxChar ch) const { return Find(ch); }
3c67202d 1219 // use Truncate
c801d85f 1220 wxString& Remove(size_t pos) { return Truncate(pos); }
e87b7833 1221 wxString& RemoveLast(size_t n = 1) { return Truncate(length() - n); }
c801d85f 1222
e87b7833
MB
1223 wxString& Remove(size_t nStart, size_t nLen)
1224 { return (wxString&)erase( nStart, nLen ); }
dd1eaa89 1225
3c67202d 1226 // use Find()
2bb67b80
OK
1227 int First( const wxChar ch ) const { return Find(ch); }
1228 int First( const wxChar* psz ) const { return Find(psz); }
3ed358cb 1229 int First( const wxString &str ) const { return Find(str); }
d775fa82
WS
1230 int Last( const wxChar ch ) const { return Find(ch, true); }
1231 bool Contains(const wxString& str) const { return Find(str) != wxNOT_FOUND; }
c801d85f 1232
5a8231ef
WS
1233 // use empty()
1234 bool IsNull() const { return empty(); }
c801d85f 1235
3c67202d 1236 // std::string compatibility functions
dd1eaa89 1237
3c67202d
VZ
1238 // take nLen chars starting at nPos
1239 wxString(const wxString& str, size_t nPos, size_t nLen)
e87b7833 1240 : wxStringBase(str, nPos, nLen) { }
3c67202d 1241 // take all characters from pStart to pEnd
e87b7833 1242 wxString(const void *pStart, const void *pEnd)
6e94fff9 1243 : wxStringBase((const wxChar*)pStart, (const wxChar*)pEnd) { }
e87b7833
MB
1244#if wxUSE_STL
1245 wxString(const_iterator first, const_iterator last)
1246 : wxStringBase(first, last) { }
1247#endif
5460b9e3 1248
3c67202d 1249 // lib.string.modifiers
3c67202d
VZ
1250 // append elements str[pos], ..., str[pos+n]
1251 wxString& append(const wxString& str, size_t pos, size_t n)
e87b7833
MB
1252 { return (wxString&)wxStringBase::append(str, pos, n); }
1253 // append a string
1254 wxString& append(const wxString& str)
1255 { return (wxString&)wxStringBase::append(str); }
3c67202d 1256 // append first n (or all if n == npos) characters of sz
e87b7833
MB
1257 wxString& append(const wxChar *sz)
1258 { return (wxString&)wxStringBase::append(sz); }
1259 wxString& append(const wxChar *sz, size_t n)
1260 { return (wxString&)wxStringBase::append(sz, n); }
3c67202d 1261 // append n copies of ch
e87b7833
MB
1262 wxString& append(size_t n, wxChar ch)
1263 { return (wxString&)wxStringBase::append(n, ch); }
1264 // append from first to last
1265 wxString& append(const_iterator first, const_iterator last)
1266 { return (wxString&)wxStringBase::append(first, last); }
3c67202d
VZ
1267
1268 // same as `this_string = str'
735d1db6 1269 wxString& assign(const wxString& str)
e87b7833 1270 { return (wxString&)wxStringBase::assign(str); }
3c67202d
VZ
1271 // same as ` = str[pos..pos + n]
1272 wxString& assign(const wxString& str, size_t pos, size_t n)
e87b7833 1273 { return (wxString&)wxStringBase::assign(str, pos, n); }
3c67202d 1274 // same as `= first n (or all if n == npos) characters of sz'
e87b7833
MB
1275 wxString& assign(const wxChar *sz)
1276 { return (wxString&)wxStringBase::assign(sz); }
1277 wxString& assign(const wxChar *sz, size_t n)
1278 { return (wxString&)wxStringBase::assign(sz, n); }
3c67202d 1279 // same as `= n copies of ch'
2bb67b80 1280 wxString& assign(size_t n, wxChar ch)
e87b7833
MB
1281 { return (wxString&)wxStringBase::assign(n, ch); }
1282 // assign from first to last
1283 wxString& assign(const_iterator first, const_iterator last)
1284 { return (wxString&)wxStringBase::assign(first, last); }
1285
1286 // string comparison
963ad140 1287#if !defined(HAVE_STD_STRING_COMPARE)
e87b7833
MB
1288 int compare(const wxStringBase& str) const;
1289 // comparison with a substring
1290 int compare(size_t nStart, size_t nLen, const wxStringBase& str) const;
1291 // comparison of 2 substrings
1292 int compare(size_t nStart, size_t nLen,
1293 const wxStringBase& str, size_t nStart2, size_t nLen2) const;
1294 // just like strcmp()
1295 int compare(const wxChar* sz) const;
1296 // substring comparison with first nCount characters of sz
1297 int compare(size_t nStart, size_t nLen,
1298 const wxChar* sz, size_t nCount = npos) const;
1299#endif // !defined HAVE_STD_STRING_COMPARE
3c67202d
VZ
1300
1301 // insert another string
e87b7833
MB
1302 wxString& insert(size_t nPos, const wxString& str)
1303 { return (wxString&)wxStringBase::insert(nPos, str); }
3c67202d
VZ
1304 // insert n chars of str starting at nStart (in str)
1305 wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n)
e87b7833 1306 { return (wxString&)wxStringBase::insert(nPos, str, nStart, n); }
3c67202d 1307 // insert first n (or all if n == npos) characters of sz
e87b7833
MB
1308 wxString& insert(size_t nPos, const wxChar *sz)
1309 { return (wxString&)wxStringBase::insert(nPos, sz); }
1310 wxString& insert(size_t nPos, const wxChar *sz, size_t n)
1311 { return (wxString&)wxStringBase::insert(nPos, sz, n); }
3c67202d 1312 // insert n copies of ch
2bb67b80 1313 wxString& insert(size_t nPos, size_t n, wxChar ch)
e87b7833
MB
1314 { return (wxString&)wxStringBase::insert(nPos, n, ch); }
1315 iterator insert(iterator it, wxChar ch)
1c2d1459 1316 { return wxStringBase::insert(it, ch); }
e87b7833
MB
1317 void insert(iterator it, const_iterator first, const_iterator last)
1318 { wxStringBase::insert(it, first, last); }
1319 void insert(iterator it, size_type n, wxChar ch)
1320 { wxStringBase::insert(it, n, ch); }
3c67202d
VZ
1321
1322 // delete characters from nStart to nStart + nLen
e87b7833
MB
1323 wxString& erase(size_type pos = 0, size_type n = npos)
1324 { return (wxString&)wxStringBase::erase(pos, n); }
1325 iterator erase(iterator first, iterator last)
1326 { return wxStringBase::erase(first, last); }
1327 iterator erase(iterator first)
1328 { return wxStringBase::erase(first); }
1329
1330#ifdef wxSTRING_BASE_HASNT_CLEAR
1331 void clear() { erase(); }
1332#endif
3c67202d
VZ
1333
1334 // replaces the substring of length nLen starting at nStart
e87b7833
MB
1335 wxString& replace(size_t nStart, size_t nLen, const wxChar* sz)
1336 { return (wxString&)wxStringBase::replace(nStart, nLen, sz); }
1337 // replaces the substring of length nLen starting at nStart
1338 wxString& replace(size_t nStart, size_t nLen, const wxString& str)
1339 { return (wxString&)wxStringBase::replace(nStart, nLen, str); }
3c67202d 1340 // replaces the substring with nCount copies of ch
e87b7833
MB
1341 wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxChar ch)
1342 { return (wxString&)wxStringBase::replace(nStart, nLen, nCount, ch); }
3c67202d
VZ
1343 // replaces a substring with another substring
1344 wxString& replace(size_t nStart, size_t nLen,
e87b7833
MB
1345 const wxString& str, size_t nStart2, size_t nLen2)
1346 { return (wxString&)wxStringBase::replace(nStart, nLen, str,
1347 nStart2, nLen2); }
1348 // replaces the substring with first nCount chars of sz
fb20fa43 1349 wxString& replace(size_t nStart, size_t nLen,
e87b7833
MB
1350 const wxChar* sz, size_t nCount)
1351 { return (wxString&)wxStringBase::replace(nStart, nLen, sz, nCount); }
1352 wxString& replace(iterator first, iterator last, const_pointer s)
1353 { return (wxString&)wxStringBase::replace(first, last, s); }
1354 wxString& replace(iterator first, iterator last, const_pointer s,
1355 size_type n)
1356 { return (wxString&)wxStringBase::replace(first, last, s, n); }
1357 wxString& replace(iterator first, iterator last, const wxString& s)
1358 { return (wxString&)wxStringBase::replace(first, last, s); }
1359 wxString& replace(iterator first, iterator last, size_type n, wxChar c)
1360 { return (wxString&)wxStringBase::replace(first, last, n, c); }
1361 wxString& replace(iterator first, iterator last,
1362 const_iterator first1, const_iterator last1)
1363 { return (wxString&)wxStringBase::replace(first, last, first1, last1); }
3c67202d 1364
e87b7833
MB
1365 // string += string
1366 wxString& operator+=(const wxString& s)
1367 { return (wxString&)wxStringBase::operator+=(s); }
1368 // string += C string
1369 wxString& operator+=(const wxChar *psz)
1370 { return (wxString&)wxStringBase::operator+=(psz); }
1371 // string += char
1372 wxString& operator+=(wxChar ch)
1373 { return (wxString&)wxStringBase::operator+=(ch); }
c801d85f
KB
1374};
1375
1fc8cfbd
VZ
1376// notice that even though for many compilers the friend declarations above are
1377// enough, from the point of view of C++ standard we must have the declarations
1378// here as friend ones are not injected in the enclosing namespace and without
1379// them the code fails to compile with conforming compilers such as xlC or g++4
b7146cbe
VZ
1380wxString WXDLLIMPEXP_BASE operator+(const wxString& string1, const wxString& string2);
1381wxString WXDLLIMPEXP_BASE operator+(const wxString& string, wxChar ch);
1382wxString WXDLLIMPEXP_BASE operator+(wxChar ch, const wxString& string);
1383wxString WXDLLIMPEXP_BASE operator+(const wxString& string, const wxChar *psz);
1384wxString WXDLLIMPEXP_BASE operator+(const wxChar *psz, const wxString& string);
1fc8cfbd 1385
b7146cbe 1386
07170120
VZ
1387#if wxUSE_STL
1388 // return an empty wxString (not very useful with wxUSE_STL == 1)
12f99210 1389 inline const wxString wxGetEmptyString() { return wxString(); }
07170120
VZ
1390#else // !wxUSE_STL
1391 // return an empty wxString (more efficient than wxString() here)
1392 inline const wxString& wxGetEmptyString()
1393 {
1394 return *(wxString *)&wxEmptyString;
1395 }
1396#endif // wxUSE_STL/!wxUSE_STL
1397
1d218550
VZ
1398// ----------------------------------------------------------------------------
1399// wxStringBuffer: a tiny class allowing to get a writable pointer into string
1400// ----------------------------------------------------------------------------
1401
941b78cc
MB
1402#if wxUSE_STL
1403
1404class WXDLLIMPEXP_BASE wxStringBuffer
1405{
1406public:
1407 wxStringBuffer(wxString& str, size_t lenWanted = 1024)
e87b7833 1408 : m_str(str), m_buf(lenWanted)
941b78cc
MB
1409 { }
1410
fe5fc682 1411 ~wxStringBuffer() { m_str.assign(m_buf.data(), wxStrlen(m_buf.data())); }
941b78cc
MB
1412
1413 operator wxChar*() { return m_buf.data(); }
1414
1415private:
1416 wxString& m_str;
1417#if wxUSE_UNICODE
1418 wxWCharBuffer m_buf;
1419#else
1420 wxCharBuffer m_buf;
1421#endif
941b78cc
MB
1422
1423 DECLARE_NO_COPY_CLASS(wxStringBuffer)
1424};
1425
1426class WXDLLIMPEXP_BASE wxStringBufferLength
1427{
1428public:
1429 wxStringBufferLength(wxString& str, size_t lenWanted = 1024)
1430 : m_str(str), m_buf(lenWanted), m_len(0), m_lenSet(false)
1431 { }
1432
1433 ~wxStringBufferLength()
1434 {
1435 wxASSERT(m_lenSet);
1436 m_str.assign(m_buf.data(), m_len);
1437 }
1438
1439 operator wxChar*() { return m_buf.data(); }
1440 void SetLength(size_t length) { m_len = length; m_lenSet = true; }
1441
1442private:
1443 wxString& m_str;
1444#if wxUSE_UNICODE
1445 wxWCharBuffer m_buf;
1446#else
1447 wxCharBuffer m_buf;
1448#endif
1449 size_t m_len;
1450 bool m_lenSet;
1451
1452 DECLARE_NO_COPY_CLASS(wxStringBufferLength)
1453};
1454
1455#else // if !wxUSE_STL
1456
bddd7a8d 1457class WXDLLIMPEXP_BASE wxStringBuffer
1d218550
VZ
1458{
1459public:
1460 wxStringBuffer(wxString& str, size_t lenWanted = 1024)
b1801e0e
GD
1461 : m_str(str), m_buf(NULL)
1462 { m_buf = m_str.GetWriteBuf(lenWanted); }
e015c2a3 1463
1d218550 1464 ~wxStringBuffer() { m_str.UngetWriteBuf(); }
e015c2a3 1465
1d218550 1466 operator wxChar*() const { return m_buf; }
e015c2a3 1467
1d218550
VZ
1468private:
1469 wxString& m_str;
1470 wxChar *m_buf;
e015c2a3
VZ
1471
1472 DECLARE_NO_COPY_CLASS(wxStringBuffer)
1d218550
VZ
1473};
1474
941b78cc
MB
1475class WXDLLIMPEXP_BASE wxStringBufferLength
1476{
1477public:
1478 wxStringBufferLength(wxString& str, size_t lenWanted = 1024)
1479 : m_str(str), m_buf(NULL), m_len(0), m_lenSet(false)
4055ed82
WS
1480 {
1481 m_buf = m_str.GetWriteBuf(lenWanted);
494b978f
RN
1482 wxASSERT(m_buf != NULL);
1483 }
941b78cc
MB
1484
1485 ~wxStringBufferLength()
1486 {
1487 wxASSERT(m_lenSet);
1488 m_str.UngetWriteBuf(m_len);
1489 }
1490
1491 operator wxChar*() const { return m_buf; }
1492 void SetLength(size_t length) { m_len = length; m_lenSet = true; }
1493
1494private:
1495 wxString& m_str;
1496 wxChar *m_buf;
1497 size_t m_len;
1498 bool m_lenSet;
1499
1500 DECLARE_NO_COPY_CLASS(wxStringBufferLength)
1501};
1502
1503#endif // !wxUSE_STL
1504
c801d85f 1505// ---------------------------------------------------------------------------
3c67202d 1506// wxString comparison functions: operator versions are always case sensitive
c801d85f 1507// ---------------------------------------------------------------------------
f33fee2a 1508
b6339dde
VZ
1509// note that when wxUSE_STL == 1 the comparison operators taking std::string
1510// are used and defining them also for wxString would only result in
1511// compilation ambiguities when comparing std::string and wxString
1512#if !wxUSE_STL
e87b7833 1513
0cbe5ee3
VZ
1514inline bool operator==(const wxString& s1, const wxString& s2)
1515 { return (s1.Len() == s2.Len()) && (s1.Cmp(s2) == 0); }
1516inline bool operator==(const wxString& s1, const wxChar * s2)
1517 { return s1.Cmp(s2) == 0; }
1518inline bool operator==(const wxChar * s1, const wxString& s2)
1519 { return s2.Cmp(s1) == 0; }
1520inline bool operator!=(const wxString& s1, const wxString& s2)
1521 { return (s1.Len() != s2.Len()) || (s1.Cmp(s2) != 0); }
1522inline bool operator!=(const wxString& s1, const wxChar * s2)
1523 { return s1.Cmp(s2) != 0; }
1524inline bool operator!=(const wxChar * s1, const wxString& s2)
1525 { return s2.Cmp(s1) != 0; }
1526inline bool operator< (const wxString& s1, const wxString& s2)
1527 { return s1.Cmp(s2) < 0; }
1528inline bool operator< (const wxString& s1, const wxChar * s2)
1529 { return s1.Cmp(s2) < 0; }
1530inline bool operator< (const wxChar * s1, const wxString& s2)
1531 { return s2.Cmp(s1) > 0; }
1532inline bool operator> (const wxString& s1, const wxString& s2)
1533 { return s1.Cmp(s2) > 0; }
1534inline bool operator> (const wxString& s1, const wxChar * s2)
1535 { return s1.Cmp(s2) > 0; }
1536inline bool operator> (const wxChar * s1, const wxString& s2)
1537 { return s2.Cmp(s1) < 0; }
1538inline bool operator<=(const wxString& s1, const wxString& s2)
1539 { return s1.Cmp(s2) <= 0; }
1540inline bool operator<=(const wxString& s1, const wxChar * s2)
1541 { return s1.Cmp(s2) <= 0; }
1542inline bool operator<=(const wxChar * s1, const wxString& s2)
1543 { return s2.Cmp(s1) >= 0; }
1544inline bool operator>=(const wxString& s1, const wxString& s2)
1545 { return s1.Cmp(s2) >= 0; }
1546inline bool operator>=(const wxString& s1, const wxChar * s2)
1547 { return s1.Cmp(s2) >= 0; }
1548inline bool operator>=(const wxChar * s1, const wxString& s2)
1549 { return s2.Cmp(s1) <= 0; }
3c67202d 1550
5ca07d0f
OK
1551#if wxUSE_UNICODE
1552inline bool operator==(const wxString& s1, const wxWCharBuffer& s2)
f33fee2a 1553 { return (s1.Cmp((const wchar_t *)s2) == 0); }
5ca07d0f 1554inline bool operator==(const wxWCharBuffer& s1, const wxString& s2)
f33fee2a 1555 { return (s2.Cmp((const wchar_t *)s1) == 0); }
f6bcfd97
BP
1556inline bool operator!=(const wxString& s1, const wxWCharBuffer& s2)
1557 { return (s1.Cmp((const wchar_t *)s2) != 0); }
1558inline bool operator!=(const wxWCharBuffer& s1, const wxString& s2)
1559 { return (s2.Cmp((const wchar_t *)s1) != 0); }
0cbe5ee3 1560#else // !wxUSE_UNICODE
5ca07d0f 1561inline bool operator==(const wxString& s1, const wxCharBuffer& s2)
f33fee2a 1562 { return (s1.Cmp((const char *)s2) == 0); }
5ca07d0f 1563inline bool operator==(const wxCharBuffer& s1, const wxString& s2)
f33fee2a 1564 { return (s2.Cmp((const char *)s1) == 0); }
f6bcfd97
BP
1565inline bool operator!=(const wxString& s1, const wxCharBuffer& s2)
1566 { return (s1.Cmp((const char *)s2) != 0); }
1567inline bool operator!=(const wxCharBuffer& s1, const wxString& s2)
1568 { return (s2.Cmp((const char *)s1) != 0); }
0cbe5ee3 1569#endif // wxUSE_UNICODE/!wxUSE_UNICODE
5ca07d0f 1570
028a2b5d 1571#if wxUSE_UNICODE
6d56eb5c 1572inline wxString operator+(const wxString& string, const wxWCharBuffer& buf)
e90c1d2a 1573 { return string + (const wchar_t *)buf; }
6d56eb5c 1574inline wxString operator+(const wxWCharBuffer& buf, const wxString& string)
e90c1d2a 1575 { return (const wchar_t *)buf + string; }
0cbe5ee3 1576#else // !wxUSE_UNICODE
6d56eb5c 1577inline wxString operator+(const wxString& string, const wxCharBuffer& buf)
e90c1d2a 1578 { return string + (const char *)buf; }
6d56eb5c 1579inline wxString operator+(const wxCharBuffer& buf, const wxString& string)
e90c1d2a 1580 { return (const char *)buf + string; }
0cbe5ee3 1581#endif // wxUSE_UNICODE/!wxUSE_UNICODE
f04f3991 1582
e51b17a1
VZ
1583#endif // !wxUSE_STL
1584
1585// comparison with char (those are not defined by std::[w]string and so should
1586// be always available)
1587inline bool operator==(wxChar c, const wxString& s) { return s.IsSameAs(c); }
1588inline bool operator==(const wxString& s, wxChar c) { return s.IsSameAs(c); }
1589inline bool operator!=(wxChar c, const wxString& s) { return !s.IsSameAs(c); }
1590inline bool operator!=(const wxString& s, wxChar c) { return !s.IsSameAs(c); }
1591
d7330233
VS
1592// comparison with C string in Unicode build
1593#if wxUSE_UNICODE
1594inline bool operator==(const wxString& s1, const char* s2)
1595 { return s1 == wxString(s2); }
1596inline bool operator==(const char* s1, const wxString& s2)
1597 { return wxString(s1) == s2; }
1598inline bool operator!=(const wxString& s1, const char* s2)
1599 { return s1 != wxString(s2); }
1600inline bool operator!=(const char* s1, const wxString& s2)
1601 { return wxString(s1) != s2; }
1602inline bool operator< (const wxString& s1, const char* s2)
1603 { return s1 < wxString(s2); }
1604inline bool operator< (const char* s1, const wxString& s2)
1605 { return wxString(s1) < s2; }
1606inline bool operator> (const wxString& s1, const char* s2)
1607 { return s1 > wxString(s2); }
1608inline bool operator> (const char* s1, const wxString& s2)
1609 { return wxString(s1) > s2; }
1610inline bool operator<=(const wxString& s1, const char* s2)
1611 { return s1 <= wxString(s2); }
1612inline bool operator<=(const char* s1, const wxString& s2)
1613 { return wxString(s1) <= s2; }
1614inline bool operator>=(const wxString& s1, const char* s2)
1615 { return s1 >= wxString(s2); }
1616inline bool operator>=(const char* s1, const wxString& s2)
1617 { return wxString(s1) >= s2; }
1618#endif // wxUSE_UNICODE
1619
c801d85f 1620// ---------------------------------------------------------------------------
3c67202d 1621// Implementation only from here until the end of file
c801d85f
KB
1622// ---------------------------------------------------------------------------
1623
e90c1d2a 1624// don't pollute the library user's name space
5737d05f 1625#undef wxASSERT_VALID_INDEX
e90c1d2a 1626
e87b7833 1627#if wxUSE_STD_IOSTREAM
c801d85f 1628
65f19af1 1629#include "wx/iosfwrap.h"
c801d85f 1630
bddd7a8d 1631WXDLLIMPEXP_BASE wxSTD ostream& operator<<(wxSTD ostream&, const wxString&);
c801d85f 1632
3c67202d 1633#endif // wxSTD_STRING_COMPATIBILITY
c801d85f 1634
34138703 1635#endif // _WX_WXSTRINGH__