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