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