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