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