]> git.saurik.com Git - wxWidgets.git/blame - include/wx/string.h
signned/unsigned fix
[wxWidgets.git] / include / wx / string.h
CommitLineData
3c67202d 1///////////////////////////////////////////////////////////////////////////////
c801d85f 2// Name: 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>
dd1eaa89 9// Licence: wxWindows license
3c67202d 10///////////////////////////////////////////////////////////////////////////////
c801d85f 11
e90c1d2a
VZ
12/*
13 Efficient string class [more or less] compatible with MFC CString,
14 wxWindows version 1 wxString and std::string and some handy functions
15 missing from string.h.
16*/
17
34138703
JS
18#ifndef _WX_WXSTRINGH__
19#define _WX_WXSTRINGH__
c801d85f
KB
20
21#ifdef __GNUG__
e90c1d2a
VZ
22 #pragma interface "string.h"
23#endif
24
25// ----------------------------------------------------------------------------
26// conditinal compilation
27// ----------------------------------------------------------------------------
28
29// compile the std::string compatibility functions if defined
30#define wxSTD_STRING_COMPATIBILITY
31
e90c1d2a
VZ
32// ----------------------------------------------------------------------------
33// headers
34// ----------------------------------------------------------------------------
35
5737d05f
VZ
36#include "wx/defs.h" // everybody should include this
37
9dea36ef 38#if defined(__WXMAC__) || defined(__VISAGECPP__)
3f4a0c5b 39 #include <ctype.h>
17dff81c 40#endif
3f4a0c5b 41
91b8de8d
RR
42#ifdef __EMX__
43 #include <std.h>
44#endif
45
9dea36ef
DW
46#if defined(__VISAGECPP__) && __IBMCPP__ >= 400
47 // problem in VACPP V4 with including stdlib.h multiple times
48 // strconv includes it anyway
49# include <stdio.h>
50# include <string.h>
51# include <stdarg.h>
52# include <limits.h>
53#else
54# include <string.h>
55# include <stdio.h>
56# include <stdarg.h>
57# include <limits.h>
58# include <stdlib.h>
59#endif
c801d85f 60
57493f9f 61#ifdef HAVE_STRINGS_H
1bfcb0b6 62 #include <strings.h> // for strcasecmp()
d739313f 63#endif // HAVE_STRINGS_H
1bfcb0b6 64
e90c1d2a
VZ
65#include "wx/wxchar.h" // for wxChar
66#include "wx/buffer.h" // for wxCharBuffer
67#include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes
3f4a0c5b 68
c801d85f
KB
69// ---------------------------------------------------------------------------
70// macros
71// ---------------------------------------------------------------------------
72
5737d05f
VZ
73// casts [unfortunately!] needed to call some broken functions which require
74// "char *" instead of "const char *"
2bb67b80 75#define WXSTRINGCAST (wxChar *)(const wxChar *)
e90c1d2a
VZ
76#define wxCSTRINGCAST (wxChar *)(const wxChar *)
77#define wxMBSTRINGCAST (char *)(const char *)
78#define wxWCSTRINGCAST (wchar_t *)(const wchar_t *)
c801d85f 79
3c67202d 80// implementation only
5737d05f 81#define wxASSERT_VALID_INDEX(i) \
b1801e0e 82 wxASSERT_MSG( (size_t)(i) <= Len(), _T("invalid index in wxString") )
c801d85f 83
e90c1d2a
VZ
84// ----------------------------------------------------------------------------
85// constants
86// ----------------------------------------------------------------------------
87
66b6b045 88#if defined(__VISAGECPP__) && __IBMCPP__ >= 400
67b81440 89// must define this static for VA or else you get multiply defined symbols everywhere
5d33ed2c 90extern const unsigned int wxSTRING_MAXLEN;
66b6b045
DW
91
92#else
e90c1d2a
VZ
93// maximum possible length for a string means "take all string" everywhere
94// (as sizeof(StringData) is unknown here, we substract 100)
95const unsigned int wxSTRING_MAXLEN = UINT_MAX - 100;
96
66b6b045
DW
97#endif
98
e90c1d2a
VZ
99// ----------------------------------------------------------------------------
100// global data
101// ----------------------------------------------------------------------------
102
103// global pointer to empty string
104WXDLLEXPORT_DATA(extern const wxChar*) wxEmptyString;
6001e347 105
c801d85f 106// ---------------------------------------------------------------------------
e90c1d2a 107// global functions complementing standard C string library replacements for
3c67202d
VZ
108// strlen() and portable strcasecmp()
109//---------------------------------------------------------------------------
e90c1d2a
VZ
110
111// Use wxXXX() functions from wxchar.h instead! These functions are for
112// backwards compatibility only.
88150e60 113
3c67202d 114// checks whether the passed in pointer is NULL and if the string is empty
6d56eb5c 115inline bool IsEmpty(const char *p) { return (!p || !*p); }
c801d85f 116
3c67202d 117// safe version of strlen() (returns 0 if passed NULL pointer)
6d56eb5c 118inline size_t Strlen(const char *psz)
c801d85f
KB
119 { return psz ? strlen(psz) : 0; }
120
3c67202d 121// portable strcasecmp/_stricmp
6d56eb5c 122inline int Stricmp(const char *psz1, const char *psz2)
dd1eaa89 123{
91b8de8d 124#if defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) )
dd1eaa89 125 return _stricmp(psz1, psz2);
91b8de8d 126#elif defined(__SC__)
2432b92d 127 return _stricmp(psz1, psz2);
91b8de8d 128#elif defined(__SALFORDC__)
a3ef5bf5 129 return stricmp(psz1, psz2);
dd1eaa89
VZ
130#elif defined(__BORLANDC__)
131 return stricmp(psz1, psz2);
7be1f0d9
JS
132#elif defined(__WATCOMC__)
133 return stricmp(psz1, psz2);
14704334
VS
134#elif defined(__DJGPP__)
135 return stricmp(psz1, psz2);
91b8de8d
RR
136#elif defined(__EMX__)
137 return stricmp(psz1, psz2);
e2c87f4c 138#elif defined(__WXPM__)
1777b9bb 139 return stricmp(psz1, psz2);
91b8de8d 140#elif defined(__UNIX__) || 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
378b05f7
VZ
168// wxSnprintf() is like snprintf() if it's available and sprintf() (always
169// available, but dangerous!) if not
170extern int WXDLLEXPORT wxSnprintf(wxChar *buf, size_t len,
7357f981
GD
171 const wxChar *format,
172 ...) ATTRIBUTE_PRINTF_3;
378b05f7
VZ
173
174// and wxVsnprintf() is like vsnprintf() or vsprintf()
175extern int WXDLLEXPORT wxVsnprintf(wxChar *buf, size_t len,
7357f981
GD
176 const wxChar *format,
177 va_list argptr);
378b05f7 178
f04f3991 179// return an empty wxString
f7bd2698 180class WXDLLEXPORT wxString; // not yet defined
e90c1d2a 181inline const wxString& wxGetEmptyString() { return *(wxString *)&wxEmptyString; }
f04f3991 182
c801d85f 183// ---------------------------------------------------------------------------
f04f3991 184// string data prepended with some housekeeping info (used by wxString class),
c801d85f
KB
185// is never used directly (but had to be put here to allow inlining)
186// ---------------------------------------------------------------------------
e90c1d2a 187
c801d85f
KB
188struct WXDLLEXPORT wxStringData
189{
190 int nRefs; // reference count
3c024cc2 191 size_t nDataLength, // actual string length
c801d85f
KB
192 nAllocLength; // allocated memory size
193
2bb67b80
OK
194 // mimics declaration 'wxChar data[nAllocLength]'
195 wxChar* data() const { return (wxChar*)(this + 1); }
c801d85f
KB
196
197 // empty string has a special ref count so it's never deleted
dbda9e86
JS
198 bool IsEmpty() const { return (nRefs == -1); }
199 bool IsShared() const { return (nRefs > 1); }
c801d85f
KB
200
201 // lock/unlock
dd1eaa89 202 void Lock() { if ( !IsEmpty() ) nRefs++; }
f6bcfd97
BP
203
204 // VC++ will refuse to inline this function but profiling shows that it
205 // is wrong
206#if defined(__VISUALC__) && (__VISUALC__ >= 1200)
207 __forceinline
208#endif
dd1eaa89 209 void Unlock() { if ( !IsEmpty() && --nRefs == 0) free(this); }
8fd0f20b 210
dd1eaa89 211 // if we had taken control over string memory (GetWriteBuf), it's
8fd0f20b 212 // intentionally put in invalid state
dbda9e86
JS
213 void Validate(bool b) { nRefs = (b ? 1 : 0); }
214 bool IsValid() const { return (nRefs != 0); }
c801d85f
KB
215};
216
c801d85f 217// ---------------------------------------------------------------------------
3c67202d
VZ
218// This is (yet another one) String class for C++ programmers. It doesn't use
219// any of "advanced" C++ features (i.e. templates, exceptions, namespaces...)
220// thus you should be able to compile it with practicaly any C++ compiler.
221// This class uses copy-on-write technique, i.e. identical strings share the
222// same memory as long as neither of them is changed.
223//
224// This class aims to be as compatible as possible with the new standard
225// std::string class, but adds some additional functions and should be at
226// least as efficient than the standard implementation.
227//
228// Performance note: it's more efficient to write functions which take "const
229// String&" arguments than "const char *" if you assign the argument to
230// another string.
231//
232// It was compiled and tested under Win32, Linux (libc 5 & 6), Solaris 5.5.
233//
234// To do:
235// - ressource support (string tables in ressources)
236// - more wide character (UNICODE) support
237// - regular expressions support
c801d85f 238// ---------------------------------------------------------------------------
3c67202d 239
3c67202d
VZ
240class WXDLLEXPORT wxString
241{
fbcb4166 242friend class WXDLLEXPORT wxArrayString;
c801d85f 243
3c67202d
VZ
244 // NB: special care was taken in arranging the member functions in such order
245 // that all inline functions can be effectively inlined, verify that all
246 // performace critical functions are still inlined if you change order!
dd1eaa89
VZ
247private:
248 // points to data preceded by wxStringData structure with ref count info
2bb67b80 249 wxChar *m_pchData;
dd1eaa89
VZ
250
251 // accessor to string data
252 wxStringData* GetStringData() const { return (wxStringData*)m_pchData - 1; }
253
6b95b20d
VZ
254 // string (re)initialization functions
255 // initializes the string to the empty value (must be called only from
256 // ctors, use Reinit() otherwise)
e90c1d2a 257 void Init() { m_pchData = (wxChar *)wxEmptyString; }
6b95b20d 258 // initializaes the string with (a part of) C-string
2bb67b80 259 void InitWith(const wxChar *psz, size_t nPos = 0, size_t nLen = wxSTRING_MAXLEN);
6b95b20d
VZ
260 // as Init, but also frees old data
261 void Reinit() { GetStringData()->Unlock(); Init(); }
262
263 // memory allocation
b1801e0e
GD
264 // allocates memory for string of length nLen
265 bool AllocBuffer(size_t nLen);
6b95b20d 266 // copies data to another string
b1801e0e 267 bool AllocCopy(wxString&, int, int) const;
6b95b20d 268 // effectively copies data to string
b1801e0e 269 bool AssignCopy(size_t, const wxChar *);
6b95b20d
VZ
270
271 // append a (sub)string
b1801e0e 272 bool ConcatSelf(int nLen, const wxChar *src);
6b95b20d
VZ
273
274 // functions called before writing to the string: they copy it if there
275 // are other references to our data (should be the only owner when writing)
b1801e0e
GD
276 bool CopyBeforeWrite();
277 bool AllocBeforeWrite(size_t);
6b95b20d 278
00b4a13e
VZ
279 // if we hadn't made these operators private, it would be possible to
280 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
281 // converted to char in C and we do have operator=(char)
282 //
283 // NB: we don't need other versions (short/long and unsigned) as attempt
284 // to assign another numeric type to wxString will now result in
285 // ambiguity between operator=(char) and operator=(int)
286 wxString& operator=(int);
287
288 // these methods are not implemented - there is _no_ conversion from int to
c606a9a4
VZ
289 // string, you're doing something wrong if the compiler wants to call it!
290 //
291 // try `s << i' or `s.Printf("%d", i)' instead
292 wxString(int);
c606a9a4 293
c801d85f 294public:
3c67202d
VZ
295 // constructors and destructor
296 // ctor for an empty string
b1801e0e 297 wxString() : m_pchData(NULL) { Init(); }
3c67202d 298 // copy ctor
b1801e0e 299 wxString(const wxString& stringSrc) : m_pchData(NULL)
6b95b20d 300 {
09443a26
VZ
301 wxASSERT_MSG( stringSrc.GetStringData()->IsValid(),
302 _T("did you forget to call UngetWriteBuf()?") );
6b95b20d
VZ
303
304 if ( stringSrc.IsEmpty() ) {
305 // nothing to do for an empty string
306 Init();
307 }
308 else {
309 m_pchData = stringSrc.m_pchData; // share same data
310 GetStringData()->Lock(); // => one more copy
311 }
312 }
3c67202d 313 // string containing nRepeat copies of ch
2bb67b80 314 wxString(wxChar ch, size_t nRepeat = 1);
3c67202d 315 // ctor takes first nLength characters from C string
566b84d2 316 // (default value of wxSTRING_MAXLEN means take all the string)
2bb67b80 317 wxString(const wxChar *psz, size_t nLength = wxSTRING_MAXLEN)
b1801e0e
GD
318 : m_pchData(NULL)
319 { InitWith(psz, 0, nLength); }
f6bcfd97 320 wxString(const wxChar *psz, wxMBConv& WXUNUSED(conv), size_t nLength = wxSTRING_MAXLEN)
b1801e0e
GD
321 : m_pchData(NULL)
322 { InitWith(psz, 0, nLength); }
e90c1d2a 323
2bb67b80
OK
324#if wxUSE_UNICODE
325 // from multibyte string
326 // (NB: nLength is right now number of Unicode characters, not
327 // characters in psz! So try not to use it yet!)
5f709e67 328 wxString(const char *psz, wxMBConv& conv = wxConvLibc, size_t nLength = wxSTRING_MAXLEN);
2bb67b80
OK
329 // from wxWCharBuffer (i.e. return from wxGetString)
330 wxString(const wxWCharBuffer& psz)
331 { InitWith(psz, 0, wxSTRING_MAXLEN); }
e90c1d2a 332#else // ANSI
3c67202d 333 // from C string (for compilers using unsigned char)
2bb67b80 334 wxString(const unsigned char* psz, size_t nLength = wxSTRING_MAXLEN)
b1801e0e
GD
335 : m_pchData(NULL)
336 { InitWith((const char*)psz, 0, nLength); }
e90c1d2a 337
6f841509 338#if wxUSE_WCHAR_T
2bb67b80 339 // from wide (Unicode) string
1c2e6a28 340 wxString(const wchar_t *pwz, wxMBConv& conv = wxConvLibc, size_t nLength = wxSTRING_MAXLEN);
e90c1d2a
VZ
341#endif // !wxUSE_WCHAR_T
342
2bb67b80
OK
343 // from wxCharBuffer
344 wxString(const wxCharBuffer& psz)
b1801e0e
GD
345 : m_pchData(NULL)
346 { InitWith(psz, 0, wxSTRING_MAXLEN); }
e90c1d2a
VZ
347#endif // Unicode/ANSI
348
3c67202d 349 // dtor is not virtual, this class must not be inherited from!
6b95b20d 350 ~wxString() { GetStringData()->Unlock(); }
c801d85f 351
3c67202d
VZ
352 // generic attributes & operations
353 // as standard strlen()
47d67540 354 size_t Len() const { return GetStringData()->nDataLength; }
3c67202d 355 // string contains any characters?
dd1eaa89 356 bool IsEmpty() const { return Len() == 0; }
dcfde592
VZ
357 // empty string is "FALSE", so !str will return TRUE
358 bool operator!() const { return IsEmpty(); }
735d1db6
VZ
359 // truncate the string to given length
360 wxString& Truncate(size_t uiLen);
3c67202d 361 // empty string contents
dd1eaa89
VZ
362 void Empty()
363 {
735d1db6 364 Truncate(0);
dd1eaa89 365
735d1db6 366 wxASSERT_MSG( IsEmpty(), _T("string not empty after call to Empty()?") );
7be07660 367 }
3c67202d 368 // empty the string and free memory
7be07660
VZ
369 void Clear()
370 {
371 if ( !GetStringData()->IsEmpty() )
372 Reinit();
373
09443a26
VZ
374 wxASSERT_MSG( !GetStringData()->nDataLength &&
375 !GetStringData()->nAllocLength,
376 _T("string should be empty after Clear()") );
dd1eaa89
VZ
377 }
378
3c67202d
VZ
379 // contents test
380 // Is an ascii value
c801d85f 381 bool IsAscii() const;
3c67202d 382 // Is a number
c801d85f 383 bool IsNumber() const;
3c67202d 384 // Is a word
c801d85f 385 bool IsWord() const;
c801d85f 386
3c67202d
VZ
387 // data access (all indexes are 0 based)
388 // read access
2bb67b80 389 wxChar GetChar(size_t n) const
5737d05f 390 { wxASSERT_VALID_INDEX( n ); return m_pchData[n]; }
3c67202d 391 // read/write access
2bb67b80 392 wxChar& GetWritableChar(size_t n)
5737d05f 393 { wxASSERT_VALID_INDEX( n ); CopyBeforeWrite(); return m_pchData[n]; }
3c67202d 394 // write access
2bb67b80 395 void SetChar(size_t n, wxChar ch)
5737d05f 396 { wxASSERT_VALID_INDEX( n ); CopyBeforeWrite(); m_pchData[n] = ch; }
c801d85f 397
3c67202d 398 // get last character
2bb67b80 399 wxChar Last() const
09443a26
VZ
400 {
401 wxASSERT_MSG( !IsEmpty(), _T("wxString: index out of bounds") );
402
403 return m_pchData[Len() - 1];
404 }
405
3c67202d 406 // get writable last character
2bb67b80 407 wxChar& Last()
09443a26
VZ
408 {
409 wxASSERT_MSG( !IsEmpty(), _T("wxString: index out of bounds") );
410 CopyBeforeWrite();
411 return m_pchData[Len()-1];
412 }
c801d85f 413
d836ee96
VZ
414 /*
415 So why do we have all these overloaded operator[]s? A bit of history:
416 initially there was only one of them, taking size_t. Then people
417 started complaining because they wanted to use ints as indices (I
418 wonder why) and compilers were giving warnings about it, so we had to
419 add the operator[](int). Then it became apparent that you couldn't
420 write str[0] any longer because there was ambiguity between two
421 overloads and so you now had to write str[0u] (or, of course, use the
422 explicit casts to either int or size_t but nobody did this).
423
424 Finally, someone decided to compile wxWin on an Alpha machine and got
425 a surprize: str[0u] didn't compile there because it is of type
426 unsigned int and size_t is unsigned _long_ on Alpha and so there was
427 ambiguity between converting uint to int or ulong. To fix this one we
428 now add operator[](uint) for the machines where size_t is not already
429 the same as unsigned int - hopefully this fixes the problem (for some
430 time)
431
432 The only real fix is, of course, to remove all versions but the one
433 taking size_t...
434 */
435
3c67202d 436 // operator version of GetChar
2bb67b80 437 wxChar operator[](size_t n) const
5737d05f 438 { wxASSERT_VALID_INDEX( n ); return m_pchData[n]; }
c5248639 439
3c67202d 440 // operator version of GetChar
2bb67b80 441 wxChar operator[](int n) const
5737d05f 442 { wxASSERT_VALID_INDEX( n ); return m_pchData[n]; }
cd0b1709 443
f83db661 444 // operator version of GetWriteableChar
2bb67b80 445 wxChar& operator[](size_t n)
5737d05f 446 { wxASSERT_VALID_INDEX( n ); CopyBeforeWrite(); return m_pchData[n]; }
d836ee96
VZ
447
448#ifndef wxSIZE_T_IS_UINT
449 // operator version of GetChar
450 wxChar operator[](unsigned int n) const
5737d05f 451 { wxASSERT_VALID_INDEX( n ); return m_pchData[n]; }
d836ee96 452
f83db661 453 // operator version of GetWriteableChar
d836ee96 454 wxChar& operator[](unsigned int n)
5737d05f 455 { wxASSERT_VALID_INDEX( n ); CopyBeforeWrite(); return m_pchData[n]; }
d836ee96 456#endif // size_t != unsigned int
c801d85f 457
3c67202d 458 // implicit conversion to C string
2bb67b80 459 operator const wxChar*() const { return m_pchData; }
3c67202d 460 // explicit conversion to C string (use this with printf()!)
2bb67b80 461 const wxChar* c_str() const { return m_pchData; }
f6bcfd97 462 // identical to c_str()
6f841509 463 const wxChar* wx_str() const { return m_pchData; }
e90c1d2a 464 // identical to c_str()
2bb67b80 465 const wxChar* GetData() const { return m_pchData; }
e90c1d2a
VZ
466
467 // conversions with (possible) format convertions: have to return a
468 // buffer with temporary data
f6bcfd97
BP
469 //
470 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
471 // return an ANSI (multibyte) string, wc_str() to return a wide string and
472 // fn_str() to return a string which should be used with the OS APIs
473 // accepting the file names. The return value is always the same, but the
474 // type differs because a function may either return pointer to the buffer
475 // directly or have to use intermediate buffer for translation.
2bb67b80 476#if wxUSE_UNICODE
f6bcfd97
BP
477 const wxCharBuffer mb_str(wxMBConv& conv = wxConvLibc) const
478 { return conv.cWC2MB(m_pchData); }
479
e90c1d2a
VZ
480 const wxWX2MBbuf mbc_str() const { return mb_str(*wxConvCurrent); }
481
f6bcfd97
BP
482 const wxChar* wc_str() const { return m_pchData; }
483
484 // for compatibility with !wxUSE_UNICODE version
485 const wxChar* wc_str(wxMBConv& WXUNUSED(conv)) const { return m_pchData; }
e90c1d2a 486
2bb67b80 487#if wxMBFILES
5f709e67 488 const wxCharBuffer fn_str() const { return mb_str(wxConvFile); }
e90c1d2a 489#else // !wxMBFILES
2bb67b80 490 const wxChar* fn_str() const { return m_pchData; }
e90c1d2a
VZ
491#endif // wxMBFILES/!wxMBFILES
492#else // ANSI
e90c1d2a 493 const wxChar* mb_str() const { return m_pchData; }
f6bcfd97
BP
494
495 // for compatibility with wxUSE_UNICODE version
496 const wxChar* mb_str(wxMBConv& WXUNUSED(conv)) const { return m_pchData; }
497
e90c1d2a 498 const wxWX2MBbuf mbc_str() const { return mb_str(); }
f6bcfd97 499
6f841509 500#if wxUSE_WCHAR_T
f6bcfd97
BP
501 const wxWCharBuffer wc_str(wxMBConv& conv) const
502 { return conv.cMB2WC(m_pchData); }
e90c1d2a 503#endif // wxUSE_WCHAR_T
f6bcfd97 504
2bb67b80 505 const wxChar* fn_str() const { return m_pchData; }
e90c1d2a 506#endif // Unicode/ANSI
c801d85f 507
3c67202d
VZ
508 // overloaded assignment
509 // from another wxString
c801d85f 510 wxString& operator=(const wxString& stringSrc);
3c67202d 511 // from a character
2bb67b80 512 wxString& operator=(wxChar ch);
3c67202d 513 // from a C string
2bb67b80 514 wxString& operator=(const wxChar *psz);
111bb7f2
OK
515#if wxUSE_UNICODE
516 // from wxWCharBuffer
b1801e0e
GD
517 wxString& operator=(const wxWCharBuffer& psz)
518 { (void) operator=((const wchar_t *)psz); return *this; }
e90c1d2a 519#else // ANSI
3c67202d 520 // from another kind of C string
c801d85f 521 wxString& operator=(const unsigned char* psz);
6f841509 522#if wxUSE_WCHAR_T
3c67202d 523 // from a wide string
c801d85f 524 wxString& operator=(const wchar_t *pwz);
6f841509 525#endif
111bb7f2 526 // from wxCharBuffer
b1801e0e
GD
527 wxString& operator=(const wxCharBuffer& psz)
528 { (void) operator=((const char *)psz); return *this; }
e90c1d2a 529#endif // Unicode/ANSI
3c67202d
VZ
530
531 // string concatenation
532 // in place concatenation
533 /*
534 Concatenate and return the result. Note that the left to right
535 associativity of << allows to write things like "str << str1 << str2
536 << ..." (unlike with +=)
537 */
538 // string += string
dd1eaa89
VZ
539 wxString& operator<<(const wxString& s)
540 {
09443a26
VZ
541 wxASSERT_MSG( s.GetStringData()->IsValid(),
542 _T("did you forget to call UngetWriteBuf()?") );
dd1eaa89
VZ
543
544 ConcatSelf(s.Len(), s);
545 return *this;
546 }
3c67202d 547 // string += C string
2bb67b80
OK
548 wxString& operator<<(const wxChar *psz)
549 { ConcatSelf(wxStrlen(psz), psz); return *this; }
3c67202d 550 // string += char
2bb67b80 551 wxString& operator<<(wxChar ch) { ConcatSelf(1, &ch); return *this; }
dd1eaa89 552
3c67202d 553 // string += string
6b95b20d 554 void operator+=(const wxString& s) { (void)operator<<(s); }
3c67202d 555 // string += C string
2bb67b80 556 void operator+=(const wxChar *psz) { (void)operator<<(psz); }
3c67202d 557 // string += char
2bb67b80
OK
558 void operator+=(wxChar ch) { (void)operator<<(ch); }
559
560 // string += buffer (i.e. from wxGetString)
561#if wxUSE_UNICODE
09443a26
VZ
562 wxString& operator<<(const wxWCharBuffer& s)
563 { (void)operator<<((const wchar_t *)s); return *this; }
564 void operator+=(const wxWCharBuffer& s)
565 { (void)operator<<((const wchar_t *)s); }
566#else // !wxUSE_UNICODE
567 wxString& operator<<(const wxCharBuffer& s)
568 { (void)operator<<((const char *)s); return *this; }
569 void operator+=(const wxCharBuffer& s)
570 { (void)operator<<((const char *)s); }
571#endif // wxUSE_UNICODE/!wxUSE_UNICODE
6b95b20d 572
3c67202d 573 // string += C string
09443a26
VZ
574 wxString& Append(const wxString& s)
575 {
576 // test for IsEmpty() to share the string if possible
577 if ( IsEmpty() )
578 *this = s;
579 else
580 ConcatSelf(s.Length(), s.c_str());
581 return *this;
582 }
2bb67b80
OK
583 wxString& Append(const wxChar* psz)
584 { ConcatSelf(wxStrlen(psz), psz); return *this; }
3c67202d 585 // append count copies of given character
2bb67b80 586 wxString& Append(wxChar ch, size_t count = 1u)
3c67202d 587 { wxString str(ch, count); return *this << str; }
8f06a017
RD
588 wxString& Append(const wxChar* psz, size_t nLen)
589 { ConcatSelf(nLen, psz); return *this; }
3c67202d
VZ
590
591 // prepend a string, return the string itself
592 wxString& Prepend(const wxString& str)
593 { *this = str + *this; return *this; }
594
595 // non-destructive concatenation
596 //
c33534e5 597 friend wxString WXDLLEXPORT operator+(const wxString& string1, const wxString& string2);
3c67202d 598 //
2bb67b80 599 friend wxString WXDLLEXPORT operator+(const wxString& string, wxChar ch);
3c67202d 600 //
2bb67b80 601 friend wxString WXDLLEXPORT operator+(wxChar ch, const wxString& string);
3c67202d 602 //
2bb67b80 603 friend wxString WXDLLEXPORT operator+(const wxString& string, const wxChar *psz);
3c67202d 604 //
2bb67b80 605 friend wxString WXDLLEXPORT operator+(const wxChar *psz, const wxString& string);
3c67202d
VZ
606
607 // stream-like functions
608 // insert an int into string
3ce65f6c
VZ
609 wxString& operator<<(int i)
610 { return (*this) << Format(_T("%d"), i); }
611 // insert an unsigned int into string
612 wxString& operator<<(unsigned int ui)
613 { return (*this) << Format(_T("%u"), ui); }
614 // insert a long into string
615 wxString& operator<<(long l)
616 { return (*this) << Format(_T("%ld"), l); }
617 // insert an unsigned long into string
618 wxString& operator<<(unsigned long ul)
619 { return (*this) << Format(_T("%lu"), ul); }
3c67202d 620 // insert a float into string
3ce65f6c
VZ
621 wxString& operator<<(float f)
622 { return (*this) << Format(_T("%f"), f); }
3c67202d 623 // insert a double into string
3ce65f6c
VZ
624 wxString& operator<<(double d)
625 { return (*this) << Format(_T("%g"), d); }
c84c52de 626
3c67202d 627 // string comparison
30b21f9a 628 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
f33fee2a 629 int Cmp(const wxChar *psz) const { return wxStrcmp(c_str(), psz); }
3c67202d 630 // same as Cmp() but not case-sensitive
f33fee2a 631 int CmpNoCase(const wxChar *psz) const { return wxStricmp(c_str(), psz); }
3c67202d
VZ
632 // test for the string equality, either considering case or not
633 // (if compareWithCase then the case matters)
2bb67b80 634 bool IsSameAs(const wxChar *psz, bool compareWithCase = TRUE) const
3c67202d 635 { return (compareWithCase ? Cmp(psz) : CmpNoCase(psz)) == 0; }
f33fee2a
VZ
636 // comparison with a signle character: returns TRUE if equal
637 bool IsSameAs(wxChar c, bool compareWithCase = TRUE) const
638 {
639 return (Len() == 1) && (compareWithCase ? GetChar(0u) == c
640 : wxToupper(GetChar(0u)) == wxToupper(c));
641 }
3c67202d
VZ
642
643 // simple sub-string extraction
644 // return substring starting at nFirst of length nCount (or till the end
645 // if nCount = default value)
566b84d2 646 wxString Mid(size_t nFirst, size_t nCount = wxSTRING_MAXLEN) const;
3c67202d 647
f6bcfd97 648 // operator version of Mid()
3c67202d
VZ
649 wxString operator()(size_t start, size_t len) const
650 { return Mid(start, len); }
651
c0881dc3 652 // check that the string starts with prefix and return the rest of the
f6bcfd97
BP
653 // string in the provided pointer if it is not NULL, otherwise return
654 // FALSE
655 bool StartsWith(const wxChar *prefix, wxString *rest = NULL) const;
656
3c67202d 657 // get first nCount characters
c801d85f 658 wxString Left(size_t nCount) const;
3c67202d 659 // get last nCount characters
c801d85f 660 wxString Right(size_t nCount) const;
c0881dc3 661 // get all characters before the first occurance of ch
3c67202d 662 // (returns the whole string if ch not found)
2bb67b80 663 wxString BeforeFirst(wxChar ch) const;
3c67202d
VZ
664 // get all characters before the last occurence of ch
665 // (returns empty string if ch not found)
2bb67b80 666 wxString BeforeLast(wxChar ch) const;
3c67202d
VZ
667 // get all characters after the first occurence of ch
668 // (returns empty string if ch not found)
2bb67b80 669 wxString AfterFirst(wxChar ch) const;
3c67202d
VZ
670 // get all characters after the last occurence of ch
671 // (returns the whole string if ch not found)
2bb67b80 672 wxString AfterLast(wxChar ch) const;
3c67202d
VZ
673
674 // for compatibility only, use more explicitly named functions above
2bb67b80
OK
675 wxString Before(wxChar ch) const { return BeforeLast(ch); }
676 wxString After(wxChar ch) const { return AfterFirst(ch); }
3c67202d
VZ
677
678 // case conversion
c84c52de 679 // convert to upper case in place, return the string itself
c801d85f 680 wxString& MakeUpper();
c84c52de 681 // convert to upper case, return the copy of the string
03ab016d
JS
682 // Here's something to remember: BC++ doesn't like returns in inlines.
683 wxString Upper() const ;
c84c52de 684 // convert to lower case in place, return the string itself
c801d85f 685 wxString& MakeLower();
c84c52de 686 // convert to lower case, return the copy of the string
03ab016d 687 wxString Lower() const ;
c801d85f 688
3c67202d
VZ
689 // trimming/padding whitespace (either side) and truncating
690 // remove spaces from left or from right (default) side
c801d85f 691 wxString& Trim(bool bFromRight = TRUE);
3c67202d 692 // add nCount copies chPad in the beginning or at the end (default)
223d09f6 693 wxString& Pad(size_t nCount, wxChar chPad = wxT(' '), bool bFromRight = TRUE);
dd1eaa89 694
3c67202d
VZ
695 // searching and replacing
696 // searching (return starting index, or -1 if not found)
2bb67b80 697 int Find(wxChar ch, bool bFromEnd = FALSE) const; // like strchr/strrchr
3c67202d 698 // searching (return starting index, or -1 if not found)
2bb67b80 699 int Find(const wxChar *pszSub) const; // like strstr
3c67202d
VZ
700 // replace first (or all of bReplaceAll) occurences of substring with
701 // another string, returns the number of replacements made
2bb67b80
OK
702 size_t Replace(const wxChar *szOld,
703 const wxChar *szNew,
3c67202d
VZ
704 bool bReplaceAll = TRUE);
705
706 // check if the string contents matches a mask containing '*' and '?'
2bb67b80 707 bool Matches(const wxChar *szMask) const;
c801d85f 708
538f35cc
VZ
709 // conversion to numbers: all functions return TRUE only if the whole
710 // string is a number and put the value of this number into the pointer
711 // provided, the base is the numeric base in which the conversion should be
712 // done and must be comprised between 2 and 36 or be 0 in which case the
713 // standard C rules apply (leading '0' => octal, "0x" => hex)
cd0b1709 714 // convert to a signed integer
538f35cc 715 bool ToLong(long *val, int base = 10) const;
cd0b1709 716 // convert to an unsigned integer
538f35cc 717 bool ToULong(unsigned long *val, int base = 10) const;
cd0b1709
VZ
718 // convert to a double
719 bool ToDouble(double *val) const;
720
3c67202d
VZ
721 // formated input/output
722 // as sprintf(), returns the number of characters written or < 0 on error
7357f981
GD
723 // (take 'this' into account in attribute parameter count)
724 int Printf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2;
3c67202d 725 // as vprintf(), returns the number of characters written or < 0 on error
2bb67b80 726 int PrintfV(const wxChar* pszFormat, va_list argptr);
dd1eaa89 727
341e7d28 728 // returns the string containing the result of Printf() to it
7357f981 729 static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1;
341e7d28
VZ
730 // the same as above, but takes a va_list
731 static wxString FormatV(const wxChar *pszFormat, va_list argptr);
732
3c67202d
VZ
733 // raw access to string memory
734 // ensure that string has space for at least nLen characters
dd1eaa89 735 // only works if the data of this string is not shared
b1801e0e 736 bool Alloc(size_t nLen);
3c67202d 737 // minimize the string's memory
dd1eaa89 738 // only works if the data of this string is not shared
b1801e0e 739 bool Shrink();
3c67202d
VZ
740 // get writable buffer of at least nLen bytes. Unget() *must* be called
741 // a.s.a.p. to put string back in a reasonable state!
2bb67b80 742 wxChar *GetWriteBuf(size_t nLen);
3c67202d 743 // call this immediately after GetWriteBuf() has been used
8fd0f20b 744 void UngetWriteBuf();
8f06a017 745 void UngetWriteBuf(size_t nLen);
c801d85f 746
3c67202d
VZ
747 // wxWindows version 1 compatibility functions
748
749 // use Mid()
750 wxString SubString(size_t from, size_t to) const
751 { return Mid(from, (to - from + 1)); }
752 // values for second parameter of CompareTo function
c801d85f 753 enum caseCompare {exact, ignoreCase};
3c67202d 754 // values for first parameter of Strip function
c801d85f 755 enum stripType {leading = 0x1, trailing = 0x2, both = 0x3};
8870c26e 756
7357f981
GD
757 // use Printf()
758 // (take 'this' into account in attribute parameter count)
759 int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2;
c801d85f 760
3c67202d 761 // use Cmp()
2bb67b80 762 inline int CompareTo(const wxChar* psz, caseCompare cmp = exact) const
6b95b20d 763 { return cmp == exact ? Cmp(psz) : CmpNoCase(psz); }
c801d85f 764
3c67202d 765 // use Len
c801d85f 766 size_t Length() const { return Len(); }
3c67202d 767 // Count the number of characters
2bb67b80 768 int Freq(wxChar ch) const;
3c67202d 769 // use MakeLower
c801d85f 770 void LowerCase() { MakeLower(); }
3c67202d 771 // use MakeUpper
c801d85f 772 void UpperCase() { MakeUpper(); }
3c67202d 773 // use Trim except that it doesn't change this string
c801d85f
KB
774 wxString Strip(stripType w = trailing) const;
775
3c67202d 776 // use Find (more general variants not yet supported)
2bb67b80
OK
777 size_t Index(const wxChar* psz) const { return Find(psz); }
778 size_t Index(wxChar ch) const { return Find(ch); }
3c67202d 779 // use Truncate
c801d85f 780 wxString& Remove(size_t pos) { return Truncate(pos); }
28a156b1 781 wxString& RemoveLast(size_t n = 1) { return Truncate(Len() - n); }
c801d85f 782
3ed358cb 783 wxString& Remove(size_t nStart, size_t nLen) { return erase( nStart, nLen ); }
dd1eaa89 784
3c67202d 785 // use Find()
2bb67b80
OK
786 int First( const wxChar ch ) const { return Find(ch); }
787 int First( const wxChar* psz ) const { return Find(psz); }
3ed358cb 788 int First( const wxString &str ) const { return Find(str); }
2bb67b80 789 int Last( const wxChar ch ) const { return Find(ch, TRUE); }
3c67202d 790 bool Contains(const wxString& str) const { return Find(str) != -1; }
c801d85f 791
3c67202d 792 // use IsEmpty()
c801d85f 793 bool IsNull() const { return IsEmpty(); }
c801d85f 794
3c67202d
VZ
795#ifdef wxSTD_STRING_COMPATIBILITY
796 // std::string compatibility functions
dd1eaa89 797
2bc07607
VZ
798 // standard types
799 typedef wxChar value_type;
800 typedef const value_type *const_iterator;
801
3c67202d 802 // an 'invalid' value for string index
c801d85f 803 static const size_t npos;
dd1eaa89 804
3c67202d
VZ
805 // constructors
806 // take nLen chars starting at nPos
807 wxString(const wxString& str, size_t nPos, size_t nLen)
b1801e0e 808 : m_pchData(NULL)
3c67202d 809 {
09443a26
VZ
810 wxASSERT_MSG( str.GetStringData()->IsValid(),
811 _T("did you forget to call UngetWriteBuf()?") );
812
3c67202d
VZ
813 InitWith(str.c_str(), nPos, nLen == npos ? 0 : nLen);
814 }
815 // take all characters from pStart to pEnd
816 wxString(const void *pStart, const void *pEnd);
817
818 // lib.string.capacity
819 // return the length of the string
820 size_t size() const { return Len(); }
821 // return the length of the string
822 size_t length() const { return Len(); }
823 // return the maximum size of the string
566b84d2 824 size_t max_size() const { return wxSTRING_MAXLEN; }
3c67202d 825 // resize the string, filling the space with c if c != 0
223d09f6 826 void resize(size_t nSize, wxChar ch = wxT('\0'));
3c67202d
VZ
827 // delete the contents of the string
828 void clear() { Empty(); }
829 // returns true if the string is empty
830 bool empty() const { return IsEmpty(); }
252a752e
VZ
831 // inform string about planned change in size
832 void reserve(size_t size) { Alloc(size); }
3c67202d
VZ
833
834 // lib.string.access
835 // return the character at position n
2bb67b80 836 wxChar at(size_t n) const { return GetChar(n); }
3c67202d 837 // returns the writable character at position n
2bb67b80 838 wxChar& at(size_t n) { return GetWritableChar(n); }
3c67202d 839
2bc07607
VZ
840 // first valid index position
841 const_iterator begin() const { return wx_str(); }
842 // position one after the last valid one
843 const_iterator end() const { return wx_str() + length(); }
844
3c67202d
VZ
845 // lib.string.modifiers
846 // append a string
847 wxString& append(const wxString& str)
848 { *this += str; return *this; }
849 // append elements str[pos], ..., str[pos+n]
850 wxString& append(const wxString& str, size_t pos, size_t n)
851 { ConcatSelf(n, str.c_str() + pos); return *this; }
852 // append first n (or all if n == npos) characters of sz
2bb67b80
OK
853 wxString& append(const wxChar *sz, size_t n = npos)
854 { ConcatSelf(n == npos ? wxStrlen(sz) : n, sz); return *this; }
3c67202d
VZ
855
856 // append n copies of ch
2bb67b80 857 wxString& append(size_t n, wxChar ch) { return Pad(n, ch); }
3c67202d
VZ
858
859 // same as `this_string = str'
735d1db6
VZ
860 wxString& assign(const wxString& str)
861 { return *this = str; }
3c67202d
VZ
862 // same as ` = str[pos..pos + n]
863 wxString& assign(const wxString& str, size_t pos, size_t n)
735d1db6 864 { Empty(); return Append(str.c_str() + pos, n); }
3c67202d 865 // same as `= first n (or all if n == npos) characters of sz'
2bb67b80 866 wxString& assign(const wxChar *sz, size_t n = npos)
735d1db6 867 { Empty(); return Append(sz, n == npos ? wxStrlen(sz) : n); }
3c67202d 868 // same as `= n copies of ch'
2bb67b80 869 wxString& assign(size_t n, wxChar ch)
735d1db6 870 { Empty(); return Append(ch, n); }
3c67202d
VZ
871
872 // insert another string
873 wxString& insert(size_t nPos, const wxString& str);
874 // insert n chars of str starting at nStart (in str)
875 wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n)
2bb67b80 876 { return insert(nPos, wxString((const wxChar *)str + nStart, n)); }
3c67202d
VZ
877
878 // insert first n (or all if n == npos) characters of sz
2bb67b80 879 wxString& insert(size_t nPos, const wxChar *sz, size_t n = npos)
3c67202d
VZ
880 { return insert(nPos, wxString(sz, n)); }
881 // insert n copies of ch
2bb67b80 882 wxString& insert(size_t nPos, size_t n, wxChar ch)
3c67202d
VZ
883 { return insert(nPos, wxString(ch, n)); }
884
885 // delete characters from nStart to nStart + nLen
886 wxString& erase(size_t nStart = 0, size_t nLen = npos);
887
888 // replaces the substring of length nLen starting at nStart
2bb67b80 889 wxString& replace(size_t nStart, size_t nLen, const wxChar* sz);
3c67202d 890 // replaces the substring with nCount copies of ch
2bb67b80 891 wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxChar ch);
3c67202d
VZ
892 // replaces a substring with another substring
893 wxString& replace(size_t nStart, size_t nLen,
894 const wxString& str, size_t nStart2, size_t nLen2);
895 // replaces the substring with first nCount chars of sz
fb20fa43
VZ
896 wxString& replace(size_t nStart, size_t nLen,
897 const wxChar* sz, size_t nCount);
3c67202d
VZ
898
899 // swap two strings
900 void swap(wxString& str);
901
902 // All find() functions take the nStart argument which specifies the
903 // position to start the search on, the default value is 0. All functions
904 // return npos if there were no match.
905
906 // find a substring
907 size_t find(const wxString& str, size_t nStart = 0) const;
908
909 // VC++ 1.5 can't cope with this syntax.
3f4a0c5b 910#if !defined(__VISUALC__) || defined(__WIN32__)
3c67202d 911 // find first n characters of sz
2bb67b80 912 size_t find(const wxChar* sz, size_t nStart = 0, size_t n = npos) const;
fb20fa43 913#endif // VC++ 1.5
3c67202d
VZ
914
915 // Gives a duplicate symbol (presumably a case-insensitivity problem)
62448488 916#if !defined(__BORLANDC__)
3c67202d 917 // find the first occurence of character ch after nStart
2bb67b80 918 size_t find(wxChar ch, size_t nStart = 0) const;
62448488 919#endif
3c67202d
VZ
920 // rfind() family is exactly like find() but works right to left
921
922 // as find, but from the end
923 size_t rfind(const wxString& str, size_t nStart = npos) const;
924
925 // VC++ 1.5 can't cope with this syntax.
3f4a0c5b 926#if !defined(__VISUALC__) || defined(__WIN32__)
3c67202d 927 // as find, but from the end
2bb67b80 928 size_t rfind(const wxChar* sz, size_t nStart = npos,
3c67202d
VZ
929 size_t n = npos) const;
930 // as find, but from the end
2bb67b80 931 size_t rfind(wxChar ch, size_t nStart = npos) const;
fb20fa43 932#endif // VC++ 1.5
3c67202d
VZ
933
934 // find first/last occurence of any character in the set
935
969d318c
VZ
936 // as strpbrk() but starts at nStart, returns npos if not found
937 size_t find_first_of(const wxString& str, size_t nStart = 0) const
938 { return find_first_of(str.c_str(), nStart); }
939 // same as above
2bb67b80 940 size_t find_first_of(const wxChar* sz, size_t nStart = 0) const;
3c67202d 941 // same as find(char, size_t)
969d318c
VZ
942 size_t find_first_of(wxChar c, size_t nStart = 0) const
943 { return find(c, nStart); }
944 // find the last (starting from nStart) char from str in this string
945 size_t find_last_of (const wxString& str, size_t nStart = npos) const
946 { return find_last_of(str.c_str(), nStart); }
947 // same as above
948 size_t find_last_of (const wxChar* sz, size_t nStart = npos) const;
949 // same as above
950 size_t find_last_of(wxChar c, size_t nStart = npos) const
951 { return rfind(c, nStart); }
3c67202d
VZ
952
953 // find first/last occurence of any character not in the set
954
969d318c
VZ
955 // as strspn() (starting from nStart), returns npos on failure
956 size_t find_first_not_of(const wxString& str, size_t nStart = 0) const
957 { return find_first_not_of(str.c_str(), nStart); }
958 // same as above
959 size_t find_first_not_of(const wxChar* sz, size_t nStart = 0) const;
960 // same as above
2bb67b80 961 size_t find_first_not_of(wxChar ch, size_t nStart = 0) const;
969d318c 962 // as strcspn()
fb20fa43
VZ
963 size_t find_last_not_of(const wxString& str, size_t nStart = npos) const
964 { return find_first_not_of(str.c_str(), nStart); }
969d318c
VZ
965 // same as above
966 size_t find_last_not_of(const wxChar* sz, size_t nStart = npos) const;
967 // same as above
2bb67b80 968 size_t find_last_not_of(wxChar ch, size_t nStart = npos) const;
3c67202d
VZ
969
970 // All compare functions return -1, 0 or 1 if the [sub]string is less,
971 // equal or greater than the compare() argument.
972
973 // just like strcmp()
974 int compare(const wxString& str) const { return Cmp(str); }
975 // comparison with a substring
fb20fa43
VZ
976 int compare(size_t nStart, size_t nLen, const wxString& str) const
977 { return Mid(nStart, nLen).Cmp(str); }
3c67202d
VZ
978 // comparison of 2 substrings
979 int compare(size_t nStart, size_t nLen,
fb20fa43
VZ
980 const wxString& str, size_t nStart2, size_t nLen2) const
981 { return Mid(nStart, nLen).Cmp(str.Mid(nStart2, nLen2)); }
3c67202d 982 // just like strcmp()
2bb67b80 983 int compare(const wxChar* sz) const { return Cmp(sz); }
3c67202d
VZ
984 // substring comparison with first nCount characters of sz
985 int compare(size_t nStart, size_t nLen,
fb20fa43
VZ
986 const wxChar* sz, size_t nCount = npos) const
987 { return Mid(nStart, nLen).Cmp(wxString(sz, nCount)); }
3c67202d
VZ
988
989 // substring extraction
8c264b23
VZ
990 wxString substr(size_t nStart = 0, size_t nLen = npos) const
991 { return Mid(nStart, nLen); }
3c67202d 992#endif // wxSTD_STRING_COMPATIBILITY
c801d85f
KB
993};
994
995// ----------------------------------------------------------------------------
3c67202d
VZ
996// The string array uses it's knowledge of internal structure of the wxString
997// class to optimize string storage. Normally, we would store pointers to
998// string, but as wxString is, in fact, itself a pointer (sizeof(wxString) is
999// sizeof(char *)) we store these pointers instead. The cast to "wxString *" is
1000// really all we need to turn such pointer into a string!
1001//
1002// Of course, it can be called a dirty hack, but we use twice less memory and
1003// this approach is also more speed efficient, so it's probably worth it.
1004//
1005// Usage notes: when a string is added/inserted, a new copy of it is created,
1006// so the original string may be safely deleted. When a string is retrieved
1007// from the array (operator[] or Item() method), a reference is returned.
c801d85f 1008// ----------------------------------------------------------------------------
e90c1d2a 1009
fbcb4166 1010class WXDLLEXPORT wxArrayString
c801d85f
KB
1011{
1012public:
30b21f9a
VZ
1013 // type of function used by wxArrayString::Sort()
1014 typedef int (*CompareFunction)(const wxString& first,
1015 const wxString& second);
1016
3c67202d 1017 // constructors and destructor
0fe1f685 1018 // default ctor
b1801e0e
GD
1019 wxArrayString()
1020 : m_nSize(0), m_nCount(0), m_pItems(NULL), m_autoSort(FALSE)
1021 { Init(FALSE); }
0fe1f685
VZ
1022 // if autoSort is TRUE, the array is always sorted (in alphabetical order)
1023 //
1024 // NB: the reason for using int and not bool is that like this we can avoid
1025 // using this ctor for implicit conversions from "const char *" (which
1026 // we'd like to be implicitly converted to wxString instead!)
1027 //
1028 // of course, using explicit would be even better - if all compilers
1029 // supported it...
b1801e0e
GD
1030 wxArrayString(int autoSort)
1031 : m_nSize(0), m_nCount(0), m_pItems(NULL), m_autoSort(FALSE)
1032 { Init(autoSort != 0); }
3c67202d 1033 // copy ctor
c801d85f 1034 wxArrayString(const wxArrayString& array);
3c67202d 1035 // assignment operator
c801d85f 1036 wxArrayString& operator=(const wxArrayString& src);
3c67202d 1037 // not virtual, this class should not be derived from
c801d85f 1038 ~wxArrayString();
c801d85f 1039
3c67202d
VZ
1040 // memory management
1041 // empties the list, but doesn't release memory
c801d85f 1042 void Empty();
3c67202d 1043 // empties the list and releases memory
c801d85f 1044 void Clear();
3c67202d 1045 // preallocates memory for given number of items
c801d85f 1046 void Alloc(size_t nCount);
3c67202d 1047 // minimzes the memory usage (by freeing all extra memory)
dd1eaa89 1048 void Shrink();
3c67202d
VZ
1049
1050 // simple accessors
1051 // number of elements in the array
1052 size_t GetCount() const { return m_nCount; }
1053 // is it empty?
1054 bool IsEmpty() const { return m_nCount == 0; }
1055 // number of elements in the array (GetCount is preferred API)
1056 size_t Count() const { return m_nCount; }
1057
1058 // items access (range checking is done in debug version)
1059 // get item at position uiIndex
c801d85f 1060 wxString& Item(size_t nIndex) const
09443a26
VZ
1061 {
1062 wxASSERT_MSG( nIndex < m_nCount,
1063 _T("wxArrayString: index out of bounds") );
1064
1065 return *(wxString *)&(m_pItems[nIndex]);
1066 }
1067
3c67202d 1068 // same as Item()
c801d85f 1069 wxString& operator[](size_t nIndex) const { return Item(nIndex); }
3c67202d 1070 // get last item
09443a26
VZ
1071 wxString& Last() const
1072 {
1073 wxASSERT_MSG( !IsEmpty(),
1074 _T("wxArrayString: index out of bounds") );
1075 return Item(Count() - 1);
1076 }
3c67202d 1077
9d155f50
RL
1078 // return a wxString[], useful for the controls which
1079 // take one in their ctor. You must delete[] it yourself
1080 // once you are done with it. Will return NULL if the
1081 // ArrayString was empty.
1082 wxString* GetStringArray() const;
1083
3c67202d
VZ
1084 // item management
1085 // Search the element in the array, starting from the beginning if
1086 // bFromEnd is FALSE or from end otherwise. If bCase, comparison is case
1087 // sensitive (default). Returns index of the first item matched or
1088 // wxNOT_FOUND
2bb67b80 1089 int Index (const wxChar *sz, bool bCase = TRUE, bool bFromEnd = FALSE) const;
e01c8145
VZ
1090 // add new element at the end (if the array is not sorted), return its
1091 // index
2a39c386 1092 size_t Add(const wxString& str, size_t nInsert = 1);
3c67202d 1093 // add new element at given position
2a39c386 1094 void Insert(const wxString& str, size_t uiIndex, size_t nInsert = 1);
71e03035
VZ
1095 // expand the array to have count elements
1096 void SetCount(size_t count);
3c67202d 1097 // remove first item matching this value
2bb67b80 1098 void Remove(const wxChar *sz);
3c67202d 1099 // remove item by index
2a39c386
VZ
1100 void Remove(size_t nIndex, size_t nRemove = 1);
1101 void RemoveAt(size_t nIndex, size_t nRemove = 1) { Remove(nIndex, nRemove); }
c801d85f 1102
30b21f9a
VZ
1103 // sorting
1104 // sort array elements in alphabetical order (or reversed alphabetical
1105 // order if reverseOrder parameter is TRUE)
1106 void Sort(bool reverseOrder = FALSE);
1107 // sort array elements using specified comparaison function
1108 void Sort(CompareFunction compareFunction);
c801d85f 1109
f6bcfd97
BP
1110 // comparison
1111 // compare two arrays case sensitively
1112 bool operator==(const wxArrayString& a) const;
1113 // compare two arrays case sensitively
1114 bool operator!=(const wxArrayString& a) const { return !(*this == a); }
1115
e87271f3 1116protected:
0fe1f685 1117 void Init(bool autoSort); // common part of all ctors
e87271f3
VZ
1118 void Copy(const wxArrayString& src); // copies the contents of another array
1119
c801d85f 1120private:
2a39c386 1121 void Grow(size_t nIncrement = 0); // makes array bigger if needed
e87271f3 1122 void Free(); // free all the strings stored
c801d85f 1123
e87271f3 1124 void DoSort(); // common part of all Sort() variants
30b21f9a 1125
3c67202d 1126 size_t m_nSize, // current size of the array
c801d85f
KB
1127 m_nCount; // current number of elements
1128
e87271f3
VZ
1129 wxChar **m_pItems; // pointer to data
1130
1131 bool m_autoSort; // if TRUE, keep the array always sorted
1132};
1133
1134class WXDLLEXPORT wxSortedArrayString : public wxArrayString
1135{
1136public:
1137 wxSortedArrayString() : wxArrayString(TRUE)
1138 { }
1139 wxSortedArrayString(const wxArrayString& array) : wxArrayString(TRUE)
1140 { Copy(array); }
c801d85f
KB
1141};
1142
1d218550
VZ
1143// ----------------------------------------------------------------------------
1144// wxStringBuffer: a tiny class allowing to get a writable pointer into string
1145// ----------------------------------------------------------------------------
1146
1147class WXDLLEXPORT wxStringBuffer
1148{
b1801e0e
GD
1149 DECLARE_NO_COPY_CLASS(wxStringBuffer)
1150
1d218550
VZ
1151public:
1152 wxStringBuffer(wxString& str, size_t lenWanted = 1024)
b1801e0e
GD
1153 : m_str(str), m_buf(NULL)
1154 { m_buf = m_str.GetWriteBuf(lenWanted); }
1155
1d218550 1156 ~wxStringBuffer() { m_str.UngetWriteBuf(); }
b1801e0e 1157
1d218550 1158 operator wxChar*() const { return m_buf; }
b1801e0e 1159
1d218550
VZ
1160private:
1161 wxString& m_str;
1162 wxChar *m_buf;
1163};
1164
c801d85f 1165// ---------------------------------------------------------------------------
3c67202d 1166// wxString comparison functions: operator versions are always case sensitive
c801d85f 1167// ---------------------------------------------------------------------------
f33fee2a 1168
0cbe5ee3
VZ
1169inline bool operator==(const wxString& s1, const wxString& s2)
1170 { return (s1.Len() == s2.Len()) && (s1.Cmp(s2) == 0); }
1171inline bool operator==(const wxString& s1, const wxChar * s2)
1172 { return s1.Cmp(s2) == 0; }
1173inline bool operator==(const wxChar * s1, const wxString& s2)
1174 { return s2.Cmp(s1) == 0; }
1175inline bool operator!=(const wxString& s1, const wxString& s2)
1176 { return (s1.Len() != s2.Len()) || (s1.Cmp(s2) != 0); }
1177inline bool operator!=(const wxString& s1, const wxChar * s2)
1178 { return s1.Cmp(s2) != 0; }
1179inline bool operator!=(const wxChar * s1, const wxString& s2)
1180 { return s2.Cmp(s1) != 0; }
1181inline bool operator< (const wxString& s1, const wxString& s2)
1182 { return s1.Cmp(s2) < 0; }
1183inline bool operator< (const wxString& s1, const wxChar * s2)
1184 { return s1.Cmp(s2) < 0; }
1185inline bool operator< (const wxChar * s1, const wxString& s2)
1186 { return s2.Cmp(s1) > 0; }
1187inline bool operator> (const wxString& s1, const wxString& s2)
1188 { return s1.Cmp(s2) > 0; }
1189inline bool operator> (const wxString& s1, const wxChar * s2)
1190 { return s1.Cmp(s2) > 0; }
1191inline bool operator> (const wxChar * s1, const wxString& s2)
1192 { return s2.Cmp(s1) < 0; }
1193inline bool operator<=(const wxString& s1, const wxString& s2)
1194 { return s1.Cmp(s2) <= 0; }
1195inline bool operator<=(const wxString& s1, const wxChar * s2)
1196 { return s1.Cmp(s2) <= 0; }
1197inline bool operator<=(const wxChar * s1, const wxString& s2)
1198 { return s2.Cmp(s1) >= 0; }
1199inline bool operator>=(const wxString& s1, const wxString& s2)
1200 { return s1.Cmp(s2) >= 0; }
1201inline bool operator>=(const wxString& s1, const wxChar * s2)
1202 { return s1.Cmp(s2) >= 0; }
1203inline bool operator>=(const wxChar * s1, const wxString& s2)
1204 { return s2.Cmp(s1) <= 0; }
3c67202d 1205
f33fee2a
VZ
1206// comparison with char
1207inline bool operator==(wxChar c, const wxString& s) { return s.IsSameAs(c); }
1208inline bool operator==(const wxString& s, wxChar c) { return s.IsSameAs(c); }
1209inline bool operator!=(wxChar c, const wxString& s) { return !s.IsSameAs(c); }
1210inline bool operator!=(const wxString& s, wxChar c) { return !s.IsSameAs(c); }
1211
5ca07d0f
OK
1212#if wxUSE_UNICODE
1213inline bool operator==(const wxString& s1, const wxWCharBuffer& s2)
f33fee2a 1214 { return (s1.Cmp((const wchar_t *)s2) == 0); }
5ca07d0f 1215inline bool operator==(const wxWCharBuffer& s1, const wxString& s2)
f33fee2a 1216 { return (s2.Cmp((const wchar_t *)s1) == 0); }
f6bcfd97
BP
1217inline bool operator!=(const wxString& s1, const wxWCharBuffer& s2)
1218 { return (s1.Cmp((const wchar_t *)s2) != 0); }
1219inline bool operator!=(const wxWCharBuffer& s1, const wxString& s2)
1220 { return (s2.Cmp((const wchar_t *)s1) != 0); }
0cbe5ee3 1221#else // !wxUSE_UNICODE
5ca07d0f 1222inline bool operator==(const wxString& s1, const wxCharBuffer& s2)
f33fee2a 1223 { return (s1.Cmp((const char *)s2) == 0); }
5ca07d0f 1224inline bool operator==(const wxCharBuffer& s1, const wxString& s2)
f33fee2a 1225 { return (s2.Cmp((const char *)s1) == 0); }
f6bcfd97
BP
1226inline bool operator!=(const wxString& s1, const wxCharBuffer& s2)
1227 { return (s1.Cmp((const char *)s2) != 0); }
1228inline bool operator!=(const wxCharBuffer& s1, const wxString& s2)
1229 { return (s2.Cmp((const char *)s1) != 0); }
0cbe5ee3 1230#endif // wxUSE_UNICODE/!wxUSE_UNICODE
5ca07d0f 1231
ed7174ba 1232wxString WXDLLEXPORT operator+(const wxString& string1, const wxString& string2);
2bb67b80
OK
1233wxString WXDLLEXPORT operator+(const wxString& string, wxChar ch);
1234wxString WXDLLEXPORT operator+(wxChar ch, const wxString& string);
1235wxString WXDLLEXPORT operator+(const wxString& string, const wxChar *psz);
1236wxString WXDLLEXPORT operator+(const wxChar *psz, const wxString& string);
028a2b5d 1237#if wxUSE_UNICODE
6d56eb5c 1238inline wxString operator+(const wxString& string, const wxWCharBuffer& buf)
e90c1d2a 1239 { return string + (const wchar_t *)buf; }
6d56eb5c 1240inline wxString operator+(const wxWCharBuffer& buf, const wxString& string)
e90c1d2a 1241 { return (const wchar_t *)buf + string; }
0cbe5ee3 1242#else // !wxUSE_UNICODE
6d56eb5c 1243inline wxString operator+(const wxString& string, const wxCharBuffer& buf)
e90c1d2a 1244 { return string + (const char *)buf; }
6d56eb5c 1245inline wxString operator+(const wxCharBuffer& buf, const wxString& string)
e90c1d2a 1246 { return (const char *)buf + string; }
0cbe5ee3 1247#endif // wxUSE_UNICODE/!wxUSE_UNICODE
f04f3991 1248
c801d85f 1249// ---------------------------------------------------------------------------
3c67202d 1250// Implementation only from here until the end of file
c801d85f
KB
1251// ---------------------------------------------------------------------------
1252
e90c1d2a 1253// don't pollute the library user's name space
5737d05f 1254#undef wxASSERT_VALID_INDEX
e90c1d2a 1255
38830220 1256#if defined(wxSTD_STRING_COMPATIBILITY) && wxUSE_STD_IOSTREAM
c801d85f 1257
3f4a0c5b 1258#include "wx/ioswrap.h"
c801d85f 1259
dd107c50
VZ
1260WXDLLEXPORT wxSTD istream& operator>>(wxSTD istream&, wxString&);
1261WXDLLEXPORT wxSTD ostream& operator<<(wxSTD ostream&, const wxString&);
c801d85f 1262
3c67202d 1263#endif // wxSTD_STRING_COMPATIBILITY
c801d85f 1264
34138703 1265#endif // _WX_WXSTRINGH__