]> git.saurik.com Git - wxWidgets.git/blame - include/wx/string.h
It now compiles all these files without warnings in Unicode mode.
[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
34138703
JS
12#ifndef _WX_WXSTRINGH__
13#define _WX_WXSTRINGH__
c801d85f
KB
14
15#ifdef __GNUG__
0d3820b3 16#pragma interface "string.h"
c801d85f
KB
17#endif
18
17dff81c 19#ifdef __WXMAC__
3f4a0c5b 20 #include <ctype.h>
17dff81c 21#endif
3f4a0c5b 22
c801d85f
KB
23#include <string.h>
24#include <stdio.h>
25#include <stdarg.h>
26#include <limits.h>
dd1eaa89 27#include <stdlib.h>
c801d85f 28
57493f9f 29#ifdef HAVE_STRINGS_H
1bfcb0b6
VZ
30 #include <strings.h> // for strcasecmp()
31#endif // AIX
32
8fd0f20b 33#ifndef WX_PRECOMP
3f4a0c5b
VZ
34 #include "wx/defs.h"
35
dd1eaa89
VZ
36 #ifdef WXSTRING_IS_WXOBJECT
37 #include "wx/object.h"
38 #endif
3f4a0c5b 39#endif // !PCH
8fd0f20b 40
c801d85f 41#include "wx/debug.h"
2bb67b80
OK
42#include "wx/wxchar.h"
43#include "wx/buffer.h"
c801d85f 44
3c67202d
VZ
45/*
46 Efficient string class [more or less] compatible with MFC CString,
47 wxWindows version 1 wxString and std::string and some handy functions
48 missing from string.h.
49*/
c801d85f
KB
50
51// ---------------------------------------------------------------------------
52// macros
53// ---------------------------------------------------------------------------
54
3c67202d
VZ
55// compile the std::string compatibility functions if defined
56#define wxSTD_STRING_COMPATIBILITY
c801d85f 57
3c67202d 58// define to derive wxString from wxObject
a3ef5bf5 59#ifdef WXSTRING_IS_WXOBJECT
c801d85f 60#undef WXSTRING_IS_WXOBJECT
a3ef5bf5 61#endif
c801d85f 62
3c67202d 63// maximum possible length for a string means "take all string" everywhere
c801d85f 64// (as sizeof(StringData) is unknown here we substract 100)
566b84d2 65const unsigned int wxSTRING_MAXLEN = UINT_MAX - 100;
c801d85f
KB
66
67// 'naughty' cast
2bb67b80
OK
68#define WXSTRINGCAST (wxChar *)(const wxChar *)
69#define MBSTRINGCAST (char *)(const char *)
70#define WCSTRINGCAST (wchar_t *)(const wchar_t *)
c801d85f 71
3c67202d 72// implementation only
b1269691 73#define ASSERT_VALID_INDEX(i) wxASSERT( (unsigned)(i) <= Len() )
c801d85f
KB
74
75// ---------------------------------------------------------------------------
3c67202d
VZ
76// Global functions complementing standard C string library replacements for
77// strlen() and portable strcasecmp()
78//---------------------------------------------------------------------------
2bb67b80 79// USE wx* FUNCTIONS IN wx/wxchar.h INSTEAD - THIS IS ONLY FOR BINARY COMPATIBILITY
88150e60 80
3c67202d 81// checks whether the passed in pointer is NULL and if the string is empty
dbda9e86 82inline bool WXDLLEXPORT IsEmpty(const char *p) { return (!p || !*p); }
c801d85f 83
3c67202d
VZ
84// safe version of strlen() (returns 0 if passed NULL pointer)
85inline size_t WXDLLEXPORT Strlen(const char *psz)
c801d85f
KB
86 { return psz ? strlen(psz) : 0; }
87
3c67202d 88// portable strcasecmp/_stricmp
dd1eaa89
VZ
89inline int WXDLLEXPORT Stricmp(const char *psz1, const char *psz2)
90{
8be97d65 91#if defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) )
dd1eaa89 92 return _stricmp(psz1, psz2);
2432b92d
JS
93#elif defined(__SC__)
94 return _stricmp(psz1, psz2);
a3ef5bf5
JS
95#elif defined(__SALFORDC__)
96 return stricmp(psz1, psz2);
dd1eaa89
VZ
97#elif defined(__BORLANDC__)
98 return stricmp(psz1, psz2);
7be1f0d9
JS
99#elif defined(__WATCOMC__)
100 return stricmp(psz1, psz2);
d4b67f95 101#elif defined(__UNIX__) || defined(__GNUWIN32__)
dd1eaa89 102 return strcasecmp(psz1, psz2);
8be97d65 103#elif defined(__MWERKS__) && !defined(__INTEL__)
17dff81c
SC
104 register char c1, c2;
105 do {
106 c1 = tolower(*psz1++);
107 c2 = tolower(*psz2++);
108 } while ( c1 && (c1 == c2) );
109
110 return c1 - c2;
dd1eaa89
VZ
111#else
112 // almost all compilers/libraries provide this function (unfortunately under
113 // different names), that's why we don't implement our own which will surely
114 // be more efficient than this code (uncomment to use):
115 /*
116 register char c1, c2;
117 do {
118 c1 = tolower(*psz1++);
119 c2 = tolower(*psz2++);
120 } while ( c1 && (c1 == c2) );
121
122 return c1 - c2;
123 */
124
125 #error "Please define string case-insensitive compare for your OS/compiler"
126#endif // OS/compiler
127}
c801d85f 128
f04f3991
VZ
129// ----------------------------------------------------------------------------
130// global data
131// ----------------------------------------------------------------------------
132
2bb67b80 133WXDLLEXPORT_DATA(extern const wxChar*) wxEmptyString;
3c67202d 134
f04f3991 135// global pointer to empty string
2bb67b80 136WXDLLEXPORT_DATA(extern const wxChar*) g_szNul;
f04f3991
VZ
137
138// return an empty wxString
f7bd2698 139class WXDLLEXPORT wxString; // not yet defined
f04f3991
VZ
140inline const wxString& wxGetEmptyString() { return *(wxString *)&g_szNul; }
141
c801d85f 142// ---------------------------------------------------------------------------
f04f3991 143// string data prepended with some housekeeping info (used by wxString class),
c801d85f
KB
144// is never used directly (but had to be put here to allow inlining)
145// ---------------------------------------------------------------------------
146struct WXDLLEXPORT wxStringData
147{
148 int nRefs; // reference count
3c024cc2 149 size_t nDataLength, // actual string length
c801d85f
KB
150 nAllocLength; // allocated memory size
151
2bb67b80
OK
152 // mimics declaration 'wxChar data[nAllocLength]'
153 wxChar* data() const { return (wxChar*)(this + 1); }
c801d85f
KB
154
155 // empty string has a special ref count so it's never deleted
dbda9e86
JS
156 bool IsEmpty() const { return (nRefs == -1); }
157 bool IsShared() const { return (nRefs > 1); }
c801d85f
KB
158
159 // lock/unlock
dd1eaa89
VZ
160 void Lock() { if ( !IsEmpty() ) nRefs++; }
161 void Unlock() { if ( !IsEmpty() && --nRefs == 0) free(this); }
8fd0f20b 162
dd1eaa89 163 // if we had taken control over string memory (GetWriteBuf), it's
8fd0f20b 164 // intentionally put in invalid state
dbda9e86
JS
165 void Validate(bool b) { nRefs = (b ? 1 : 0); }
166 bool IsValid() const { return (nRefs != 0); }
c801d85f
KB
167};
168
2bb67b80
OK
169// ---------------------------------------------------------------------------
170// types of multibyte<->Unicode conversions
171// ---------------------------------------------------------------------------
172class WXDLLEXPORT wxMBConv
173{
174 public:
175 virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n) const;
176 virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n) const;
177 const wxWCharBuffer cMB2WC(const char *psz) const
178 {
179 size_t nLen = MB2WC((wchar_t *) NULL, psz, 0);
180 wxWCharBuffer buf(nLen);
181 MB2WC(WCSTRINGCAST buf, psz, nLen);
182 return buf;
183 }
184 const wxCharBuffer cWC2MB(const wchar_t *psz) const
185 {
186 size_t nLen = WC2MB((char *) NULL, psz, 0);
187 wxCharBuffer buf(nLen);
188 WC2MB(MBSTRINGCAST buf, psz, nLen);
189 return buf;
190 }
191#if wxUSE_UNICODE
192 const wxWCharBuffer cMB2WX(const char *psz) const { return cMB2WC(psz); }
193 const wxCharBuffer cWX2MB(const wchar_t *psz) const { return cWC2MB(psz); }
194 const wchar_t* cWC2WX(const wchar_t *psz) const { return psz; }
195 const wchar_t* cMB2WC(const wchar_t *psz) const { return psz; }
196#else
197 const char* cMB2WX(const char *psz) const { return psz; }
198 const char* cWX2MB(const char *psz) const { return psz; }
199 const wxCharBuffer cWC2WX(const wchar_t *psz) const { return cWC2MB(psz); }
200 const wxWCharBuffer cWX2WC(const char *psz) const { return cMB2WC(psz); }
201#endif
202};
203WXDLLEXPORT_DATA(extern wxMBConv) wxConv_libc, wxConv_UTF7, wxConv_UTF8;
204#define wxConv_file wxConv_libc
205
206class WXDLLEXPORT wxCSConv : wxMBConv
207{
208 private:
209 wxChar *data;
210 public:
211 wxCSConv(const wxChar *charset);
212 virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n);
213 virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n);
214};
215
216// filenames are multibyte on Unix and probably widechar on Windows?
217#ifdef __UNIX__
218#define wxMBFILES 1
219#else
220#define wxMBFILES 0
221#endif
222
223#if wxMBFILES
224#define wxFNCONV(name) wxConv_file.cWX2MB(name)
225#define FNSTRINGCAST MBSTRINGCAST
226#else
227#define wxFNCONV(name) name
228#define FNSTRINGCAST WXSTRINGCAST
229#endif
230
c801d85f 231// ---------------------------------------------------------------------------
3c67202d
VZ
232// This is (yet another one) String class for C++ programmers. It doesn't use
233// any of "advanced" C++ features (i.e. templates, exceptions, namespaces...)
234// thus you should be able to compile it with practicaly any C++ compiler.
235// This class uses copy-on-write technique, i.e. identical strings share the
236// same memory as long as neither of them is changed.
237//
238// This class aims to be as compatible as possible with the new standard
239// std::string class, but adds some additional functions and should be at
240// least as efficient than the standard implementation.
241//
242// Performance note: it's more efficient to write functions which take "const
243// String&" arguments than "const char *" if you assign the argument to
244// another string.
245//
246// It was compiled and tested under Win32, Linux (libc 5 & 6), Solaris 5.5.
247//
248// To do:
249// - ressource support (string tables in ressources)
250// - more wide character (UNICODE) support
251// - regular expressions support
c801d85f 252// ---------------------------------------------------------------------------
3c67202d 253
c801d85f 254#ifdef WXSTRING_IS_WXOBJECT
3c67202d
VZ
255class WXDLLEXPORT wxString : public wxObject
256{
c801d85f
KB
257 DECLARE_DYNAMIC_CLASS(wxString)
258#else //WXSTRING_IS_WXOBJECT
3c67202d
VZ
259class WXDLLEXPORT wxString
260{
c801d85f
KB
261#endif //WXSTRING_IS_WXOBJECT
262
fbcb4166 263friend class WXDLLEXPORT wxArrayString;
c801d85f 264
3c67202d
VZ
265 // NB: special care was taken in arranging the member functions in such order
266 // that all inline functions can be effectively inlined, verify that all
267 // performace critical functions are still inlined if you change order!
dd1eaa89
VZ
268private:
269 // points to data preceded by wxStringData structure with ref count info
2bb67b80 270 wxChar *m_pchData;
dd1eaa89
VZ
271
272 // accessor to string data
273 wxStringData* GetStringData() const { return (wxStringData*)m_pchData - 1; }
274
6b95b20d
VZ
275 // string (re)initialization functions
276 // initializes the string to the empty value (must be called only from
277 // ctors, use Reinit() otherwise)
2bb67b80 278 void Init() { m_pchData = (wxChar *)g_szNul; }
6b95b20d 279 // initializaes the string with (a part of) C-string
2bb67b80 280 void InitWith(const wxChar *psz, size_t nPos = 0, size_t nLen = wxSTRING_MAXLEN);
6b95b20d
VZ
281 // as Init, but also frees old data
282 void Reinit() { GetStringData()->Unlock(); Init(); }
283
284 // memory allocation
285 // allocates memory for string of lenght nLen
286 void AllocBuffer(size_t nLen);
287 // copies data to another string
288 void AllocCopy(wxString&, int, int) const;
289 // effectively copies data to string
2bb67b80 290 void AssignCopy(size_t, const wxChar *);
6b95b20d
VZ
291
292 // append a (sub)string
2bb67b80 293 void ConcatSelf(int nLen, const wxChar *src);
6b95b20d
VZ
294
295 // functions called before writing to the string: they copy it if there
296 // are other references to our data (should be the only owner when writing)
297 void CopyBeforeWrite();
298 void AllocBeforeWrite(size_t);
299
c606a9a4
VZ
300 // this method is not implemented - there is _no_ conversion from int to
301 // string, you're doing something wrong if the compiler wants to call it!
302 //
303 // try `s << i' or `s.Printf("%d", i)' instead
304 wxString(int);
305 wxString(long);
306
c801d85f 307public:
3c67202d
VZ
308 // constructors and destructor
309 // ctor for an empty string
6b95b20d 310 wxString() { Init(); }
3c67202d 311 // copy ctor
6b95b20d
VZ
312 wxString(const wxString& stringSrc)
313 {
314 wxASSERT( stringSrc.GetStringData()->IsValid() );
315
316 if ( stringSrc.IsEmpty() ) {
317 // nothing to do for an empty string
318 Init();
319 }
320 else {
321 m_pchData = stringSrc.m_pchData; // share same data
322 GetStringData()->Lock(); // => one more copy
323 }
324 }
3c67202d 325 // string containing nRepeat copies of ch
2bb67b80 326 wxString(wxChar ch, size_t nRepeat = 1);
3c67202d 327 // ctor takes first nLength characters from C string
566b84d2 328 // (default value of wxSTRING_MAXLEN means take all the string)
2bb67b80 329 wxString(const wxChar *psz, size_t nLength = wxSTRING_MAXLEN)
6b95b20d 330 { InitWith(psz, 0, nLength); }
2bb67b80
OK
331#if wxUSE_UNICODE
332 // from multibyte string
333 // (NB: nLength is right now number of Unicode characters, not
334 // characters in psz! So try not to use it yet!)
335 wxString(const char *psz, wxMBConv& conv = wxConv_libc, size_t nLength = wxSTRING_MAXLEN);
336 // from wxWCharBuffer (i.e. return from wxGetString)
337 wxString(const wxWCharBuffer& psz)
338 { InitWith(psz, 0, wxSTRING_MAXLEN); }
339#else
3c67202d 340 // from C string (for compilers using unsigned char)
2bb67b80
OK
341 wxString(const unsigned char* psz, size_t nLength = wxSTRING_MAXLEN)
342 { InitWith((const char*)psz, 0, nLength); }
343 // from wide (Unicode) string
c801d85f 344 wxString(const wchar_t *pwz);
2bb67b80
OK
345 // from wxCharBuffer
346 wxString(const wxCharBuffer& psz)
347 { InitWith(psz, 0, wxSTRING_MAXLEN); }
348#endif
3c67202d 349 // dtor is not virtual, this class must not be inherited from!
6b95b20d 350 ~wxString() { GetStringData()->Unlock(); }
c801d85f 351
3c67202d
VZ
352 // generic attributes & operations
353 // as standard strlen()
47d67540 354 size_t Len() const { return GetStringData()->nDataLength; }
3c67202d 355 // string contains any characters?
dd1eaa89 356 bool IsEmpty() const { return Len() == 0; }
dcfde592
VZ
357 // empty string is "FALSE", so !str will return TRUE
358 bool operator!() const { return IsEmpty(); }
3c67202d 359 // empty string contents
dd1eaa89
VZ
360 void Empty()
361 {
2c3b684c 362 if ( !IsEmpty() )
dd1eaa89
VZ
363 Reinit();
364
7be07660 365 // should be empty
dd1eaa89 366 wxASSERT( GetStringData()->nDataLength == 0 );
7be07660 367 }
3c67202d 368 // empty the string and free memory
7be07660
VZ
369 void Clear()
370 {
371 if ( !GetStringData()->IsEmpty() )
372 Reinit();
373
374 wxASSERT( GetStringData()->nDataLength == 0 ); // should be empty
375 wxASSERT( GetStringData()->nAllocLength == 0 ); // and not own any memory
dd1eaa89
VZ
376 }
377
3c67202d
VZ
378 // contents test
379 // Is an ascii value
c801d85f 380 bool IsAscii() const;
3c67202d 381 // Is a number
c801d85f 382 bool IsNumber() const;
3c67202d 383 // Is a word
c801d85f 384 bool IsWord() const;
c801d85f 385
3c67202d
VZ
386 // data access (all indexes are 0 based)
387 // read access
2bb67b80 388 wxChar GetChar(size_t n) const
dd1eaa89 389 { ASSERT_VALID_INDEX( n ); return m_pchData[n]; }
3c67202d 390 // read/write access
2bb67b80 391 wxChar& GetWritableChar(size_t n)
dd1eaa89 392 { ASSERT_VALID_INDEX( n ); CopyBeforeWrite(); return m_pchData[n]; }
3c67202d 393 // write access
2bb67b80 394 void SetChar(size_t n, wxChar ch)
c801d85f
KB
395 { ASSERT_VALID_INDEX( n ); CopyBeforeWrite(); m_pchData[n] = ch; }
396
3c67202d 397 // get last character
2bb67b80 398 wxChar Last() const
c801d85f 399 { wxASSERT( !IsEmpty() ); return m_pchData[Len() - 1]; }
3c67202d 400 // get writable last character
2bb67b80 401 wxChar& Last()
c801d85f
KB
402 { wxASSERT( !IsEmpty() ); CopyBeforeWrite(); return m_pchData[Len()-1]; }
403
57493f9f
VZ
404 // under Unix it is tested with configure, assume it works on other
405 // platforms (there might be overloading problems if size_t and int are
406 // the same type)
227b5cd7 407#if !defined(__UNIX__) || wxUSE_SIZE_T_STRING_OPERATOR
3c67202d 408 // operator version of GetChar
2bb67b80 409 wxChar operator[](size_t n) const
c801d85f 410 { ASSERT_VALID_INDEX( n ); return m_pchData[n]; }
22552603 411#endif
c5248639 412
3c67202d 413 // operator version of GetChar
2bb67b80 414 wxChar operator[](int n) const
c801d85f 415 { ASSERT_VALID_INDEX( n ); return m_pchData[n]; }
3c67202d 416 // operator version of GetWritableChar
2bb67b80 417 wxChar& operator[](size_t n)
c801d85f
KB
418 { ASSERT_VALID_INDEX( n ); CopyBeforeWrite(); return m_pchData[n]; }
419
3c67202d 420 // implicit conversion to C string
2bb67b80 421 operator const wxChar*() const { return m_pchData; }
3c67202d 422 // explicit conversion to C string (use this with printf()!)
2bb67b80 423 const wxChar* c_str() const { return m_pchData; }
3c67202d 424 //
2bb67b80
OK
425 const wxChar* GetData() const { return m_pchData; }
426#if wxUSE_UNICODE
427 const wxCharBuffer mb_str(wxMBConv& conv = wxConv_libc) const { return conv.cWC2MB(m_pchData); }
428 const wxChar* wc_str(wxMBConv& WXUNUSED(conv) = wxConv_libc) const { return m_pchData; }
429#if wxMBFILES
430 const wxCharBuffer fn_str() const { return mb_str(wxConv_file); }
431#else
432 const wxChar* fn_str() const { return m_pchData; }
433#endif
434#else
435 const wxChar* mb_str(wxMBConv& WXUNUSED(conv) = wxConv_libc ) const { return m_pchData; }
436 const wxWCharBuffer wc_str(wxMBConv& conv) const { return conv.cMB2WC(m_pchData); }
437 const wxChar* fn_str() const { return m_pchData; }
438#endif
c801d85f 439
3c67202d
VZ
440 // overloaded assignment
441 // from another wxString
c801d85f 442 wxString& operator=(const wxString& stringSrc);
3c67202d 443 // from a character
2bb67b80 444 wxString& operator=(wxChar ch);
3c67202d 445 // from a C string
2bb67b80
OK
446 wxString& operator=(const wxChar *psz);
447#if !wxUSE_UNICODE
3c67202d 448 // from another kind of C string
c801d85f 449 wxString& operator=(const unsigned char* psz);
3c67202d 450 // from a wide string
c801d85f 451 wxString& operator=(const wchar_t *pwz);
2bb67b80 452#endif
3c67202d
VZ
453
454 // string concatenation
455 // in place concatenation
456 /*
457 Concatenate and return the result. Note that the left to right
458 associativity of << allows to write things like "str << str1 << str2
459 << ..." (unlike with +=)
460 */
461 // string += string
dd1eaa89
VZ
462 wxString& operator<<(const wxString& s)
463 {
464 wxASSERT( s.GetStringData()->IsValid() );
465
466 ConcatSelf(s.Len(), s);
467 return *this;
468 }
3c67202d 469 // string += C string
2bb67b80
OK
470 wxString& operator<<(const wxChar *psz)
471 { ConcatSelf(wxStrlen(psz), psz); return *this; }
3c67202d 472 // string += char
2bb67b80 473 wxString& operator<<(wxChar ch) { ConcatSelf(1, &ch); return *this; }
dd1eaa89 474
3c67202d 475 // string += string
6b95b20d 476 void operator+=(const wxString& s) { (void)operator<<(s); }
3c67202d 477 // string += C string
2bb67b80 478 void operator+=(const wxChar *psz) { (void)operator<<(psz); }
3c67202d 479 // string += char
2bb67b80
OK
480 void operator+=(wxChar ch) { (void)operator<<(ch); }
481
482 // string += buffer (i.e. from wxGetString)
483#if wxUSE_UNICODE
484 wxString& operator<<(const wxWCharBuffer& s) { (void)operator<<((const wchar_t *)s); return *this; }
485 void operator+=(const wxWCharBuffer& s) { (void)operator<<((const wchar_t *)s); }
486#else
487 wxString& operator<<(const wxCharBuffer& s) { (void)operator<<((const char *)s); return *this; }
488 void operator+=(const wxCharBuffer& s) { (void)operator<<((const char *)s); }
489#endif
6b95b20d 490
3c67202d 491 // string += C string
2bb67b80
OK
492 wxString& Append(const wxChar* psz)
493 { ConcatSelf(wxStrlen(psz), psz); return *this; }
3c67202d 494 // append count copies of given character
2bb67b80 495 wxString& Append(wxChar ch, size_t count = 1u)
3c67202d
VZ
496 { wxString str(ch, count); return *this << str; }
497
498 // prepend a string, return the string itself
499 wxString& Prepend(const wxString& str)
500 { *this = str + *this; return *this; }
501
502 // non-destructive concatenation
503 //
c33534e5 504 friend wxString WXDLLEXPORT operator+(const wxString& string1, const wxString& string2);
3c67202d 505 //
2bb67b80 506 friend wxString WXDLLEXPORT operator+(const wxString& string, wxChar ch);
3c67202d 507 //
2bb67b80 508 friend wxString WXDLLEXPORT operator+(wxChar ch, const wxString& string);
3c67202d 509 //
2bb67b80 510 friend wxString WXDLLEXPORT operator+(const wxString& string, const wxChar *psz);
3c67202d 511 //
2bb67b80 512 friend wxString WXDLLEXPORT operator+(const wxChar *psz, const wxString& string);
3c67202d
VZ
513
514 // stream-like functions
515 // insert an int into string
516 wxString& operator<<(int i);
517 // insert a float into string
518 wxString& operator<<(float f);
519 // insert a double into string
520 wxString& operator<<(double d);
c84c52de 521
3c67202d 522 // string comparison
30b21f9a 523 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
2bb67b80 524 int Cmp(const wxChar *psz) const { return wxStrcmp(c_str(), psz); }
3c67202d 525 // same as Cmp() but not case-sensitive
2bb67b80 526 int CmpNoCase(const wxChar *psz) const { return wxStricmp(c_str(), psz); }
3c67202d
VZ
527 // test for the string equality, either considering case or not
528 // (if compareWithCase then the case matters)
2bb67b80 529 bool IsSameAs(const wxChar *psz, bool compareWithCase = TRUE) const
3c67202d
VZ
530 { return (compareWithCase ? Cmp(psz) : CmpNoCase(psz)) == 0; }
531
532 // simple sub-string extraction
533 // return substring starting at nFirst of length nCount (or till the end
534 // if nCount = default value)
566b84d2 535 wxString Mid(size_t nFirst, size_t nCount = wxSTRING_MAXLEN) const;
3c67202d
VZ
536
537 // operator version of Mid()
538 wxString operator()(size_t start, size_t len) const
539 { return Mid(start, len); }
540
541 // get first nCount characters
c801d85f 542 wxString Left(size_t nCount) const;
3c67202d 543 // get last nCount characters
c801d85f 544 wxString Right(size_t nCount) const;
3c67202d
VZ
545 // get all characters before the first occurence of ch
546 // (returns the whole string if ch not found)
2bb67b80 547 wxString BeforeFirst(wxChar ch) const;
3c67202d
VZ
548 // get all characters before the last occurence of ch
549 // (returns empty string if ch not found)
2bb67b80 550 wxString BeforeLast(wxChar ch) const;
3c67202d
VZ
551 // get all characters after the first occurence of ch
552 // (returns empty string if ch not found)
2bb67b80 553 wxString AfterFirst(wxChar ch) const;
3c67202d
VZ
554 // get all characters after the last occurence of ch
555 // (returns the whole string if ch not found)
2bb67b80 556 wxString AfterLast(wxChar ch) const;
3c67202d
VZ
557
558 // for compatibility only, use more explicitly named functions above
2bb67b80
OK
559 wxString Before(wxChar ch) const { return BeforeLast(ch); }
560 wxString After(wxChar ch) const { return AfterFirst(ch); }
3c67202d
VZ
561
562 // case conversion
c84c52de 563 // convert to upper case in place, return the string itself
c801d85f 564 wxString& MakeUpper();
c84c52de 565 // convert to upper case, return the copy of the string
03ab016d
JS
566 // Here's something to remember: BC++ doesn't like returns in inlines.
567 wxString Upper() const ;
c84c52de 568 // convert to lower case in place, return the string itself
c801d85f 569 wxString& MakeLower();
c84c52de 570 // convert to lower case, return the copy of the string
03ab016d 571 wxString Lower() const ;
c801d85f 572
3c67202d
VZ
573 // trimming/padding whitespace (either side) and truncating
574 // remove spaces from left or from right (default) side
c801d85f 575 wxString& Trim(bool bFromRight = TRUE);
3c67202d 576 // add nCount copies chPad in the beginning or at the end (default)
2bb67b80 577 wxString& Pad(size_t nCount, wxChar chPad = _T(' '), bool bFromRight = TRUE);
3c67202d 578 // truncate string to given length
c801d85f 579 wxString& Truncate(size_t uiLen);
dd1eaa89 580
3c67202d
VZ
581 // searching and replacing
582 // searching (return starting index, or -1 if not found)
2bb67b80 583 int Find(wxChar ch, bool bFromEnd = FALSE) const; // like strchr/strrchr
3c67202d 584 // searching (return starting index, or -1 if not found)
2bb67b80 585 int Find(const wxChar *pszSub) const; // like strstr
3c67202d
VZ
586 // replace first (or all of bReplaceAll) occurences of substring with
587 // another string, returns the number of replacements made
2bb67b80
OK
588 size_t Replace(const wxChar *szOld,
589 const wxChar *szNew,
3c67202d
VZ
590 bool bReplaceAll = TRUE);
591
592 // check if the string contents matches a mask containing '*' and '?'
2bb67b80 593 bool Matches(const wxChar *szMask) const;
c801d85f 594
3c67202d
VZ
595 // formated input/output
596 // as sprintf(), returns the number of characters written or < 0 on error
2bb67b80 597 int Printf(const wxChar *pszFormat, ...);
3c67202d 598 // as vprintf(), returns the number of characters written or < 0 on error
2bb67b80 599 int PrintfV(const wxChar* pszFormat, va_list argptr);
dd1eaa89 600
3c67202d
VZ
601 // raw access to string memory
602 // ensure that string has space for at least nLen characters
dd1eaa89 603 // only works if the data of this string is not shared
c86f1403 604 void Alloc(size_t nLen);
3c67202d 605 // minimize the string's memory
dd1eaa89
VZ
606 // only works if the data of this string is not shared
607 void Shrink();
3c67202d
VZ
608 // get writable buffer of at least nLen bytes. Unget() *must* be called
609 // a.s.a.p. to put string back in a reasonable state!
2bb67b80 610 wxChar *GetWriteBuf(size_t nLen);
3c67202d 611 // call this immediately after GetWriteBuf() has been used
8fd0f20b 612 void UngetWriteBuf();
c801d85f 613
3c67202d
VZ
614 // wxWindows version 1 compatibility functions
615
616 // use Mid()
617 wxString SubString(size_t from, size_t to) const
618 { return Mid(from, (to - from + 1)); }
619 // values for second parameter of CompareTo function
c801d85f 620 enum caseCompare {exact, ignoreCase};
3c67202d 621 // values for first parameter of Strip function
c801d85f 622 enum stripType {leading = 0x1, trailing = 0x2, both = 0x3};
8870c26e 623
3c67202d 624 // use Printf()
2bb67b80 625 int sprintf(const wxChar *pszFormat, ...);
c801d85f 626
3c67202d 627 // use Cmp()
2bb67b80 628 inline int CompareTo(const wxChar* psz, caseCompare cmp = exact) const
6b95b20d 629 { return cmp == exact ? Cmp(psz) : CmpNoCase(psz); }
c801d85f 630
3c67202d 631 // use Len
c801d85f 632 size_t Length() const { return Len(); }
3c67202d 633 // Count the number of characters
2bb67b80 634 int Freq(wxChar ch) const;
3c67202d 635 // use MakeLower
c801d85f 636 void LowerCase() { MakeLower(); }
3c67202d 637 // use MakeUpper
c801d85f 638 void UpperCase() { MakeUpper(); }
3c67202d 639 // use Trim except that it doesn't change this string
c801d85f
KB
640 wxString Strip(stripType w = trailing) const;
641
3c67202d 642 // use Find (more general variants not yet supported)
2bb67b80
OK
643 size_t Index(const wxChar* psz) const { return Find(psz); }
644 size_t Index(wxChar ch) const { return Find(ch); }
3c67202d 645 // use Truncate
c801d85f
KB
646 wxString& Remove(size_t pos) { return Truncate(pos); }
647 wxString& RemoveLast() { return Truncate(Len() - 1); }
648
3ed358cb 649 wxString& Remove(size_t nStart, size_t nLen) { return erase( nStart, nLen ); }
dd1eaa89 650
3c67202d 651 // use Find()
2bb67b80
OK
652 int First( const wxChar ch ) const { return Find(ch); }
653 int First( const wxChar* psz ) const { return Find(psz); }
3ed358cb 654 int First( const wxString &str ) const { return Find(str); }
2bb67b80 655 int Last( const wxChar ch ) const { return Find(ch, TRUE); }
3c67202d 656 bool Contains(const wxString& str) const { return Find(str) != -1; }
c801d85f 657
3c67202d 658 // use IsEmpty()
c801d85f 659 bool IsNull() const { return IsEmpty(); }
c801d85f 660
3c67202d
VZ
661#ifdef wxSTD_STRING_COMPATIBILITY
662 // std::string compatibility functions
dd1eaa89 663
3c67202d 664 // an 'invalid' value for string index
c801d85f 665 static const size_t npos;
dd1eaa89 666
3c67202d
VZ
667 // constructors
668 // take nLen chars starting at nPos
669 wxString(const wxString& str, size_t nPos, size_t nLen)
670 {
671 wxASSERT( str.GetStringData()->IsValid() );
672 InitWith(str.c_str(), nPos, nLen == npos ? 0 : nLen);
673 }
674 // take all characters from pStart to pEnd
675 wxString(const void *pStart, const void *pEnd);
676
677 // lib.string.capacity
678 // return the length of the string
679 size_t size() const { return Len(); }
680 // return the length of the string
681 size_t length() const { return Len(); }
682 // return the maximum size of the string
566b84d2 683 size_t max_size() const { return wxSTRING_MAXLEN; }
3c67202d 684 // resize the string, filling the space with c if c != 0
2bb67b80 685 void resize(size_t nSize, wxChar ch = _T('\0'));
3c67202d
VZ
686 // delete the contents of the string
687 void clear() { Empty(); }
688 // returns true if the string is empty
689 bool empty() const { return IsEmpty(); }
690
691 // lib.string.access
692 // return the character at position n
2bb67b80 693 wxChar at(size_t n) const { return GetChar(n); }
3c67202d 694 // returns the writable character at position n
2bb67b80 695 wxChar& at(size_t n) { return GetWritableChar(n); }
3c67202d
VZ
696
697 // lib.string.modifiers
698 // append a string
699 wxString& append(const wxString& str)
700 { *this += str; return *this; }
701 // append elements str[pos], ..., str[pos+n]
702 wxString& append(const wxString& str, size_t pos, size_t n)
703 { ConcatSelf(n, str.c_str() + pos); return *this; }
704 // append first n (or all if n == npos) characters of sz
2bb67b80
OK
705 wxString& append(const wxChar *sz, size_t n = npos)
706 { ConcatSelf(n == npos ? wxStrlen(sz) : n, sz); return *this; }
3c67202d
VZ
707
708 // append n copies of ch
2bb67b80 709 wxString& append(size_t n, wxChar ch) { return Pad(n, ch); }
3c67202d
VZ
710
711 // same as `this_string = str'
712 wxString& assign(const wxString& str) { return (*this) = str; }
713 // same as ` = str[pos..pos + n]
714 wxString& assign(const wxString& str, size_t pos, size_t n)
2bb67b80 715 { return *this = wxString((const wxChar *)str + pos, n); }
3c67202d 716 // same as `= first n (or all if n == npos) characters of sz'
2bb67b80 717 wxString& assign(const wxChar *sz, size_t n = npos)
3c67202d
VZ
718 { return *this = wxString(sz, n); }
719 // same as `= n copies of ch'
2bb67b80 720 wxString& assign(size_t n, wxChar ch)
3c67202d
VZ
721 { return *this = wxString(ch, n); }
722
723 // insert another string
724 wxString& insert(size_t nPos, const wxString& str);
725 // insert n chars of str starting at nStart (in str)
726 wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n)
2bb67b80 727 { return insert(nPos, wxString((const wxChar *)str + nStart, n)); }
3c67202d
VZ
728
729 // insert first n (or all if n == npos) characters of sz
2bb67b80 730 wxString& insert(size_t nPos, const wxChar *sz, size_t n = npos)
3c67202d
VZ
731 { return insert(nPos, wxString(sz, n)); }
732 // insert n copies of ch
2bb67b80 733 wxString& insert(size_t nPos, size_t n, wxChar ch)
3c67202d
VZ
734 { return insert(nPos, wxString(ch, n)); }
735
736 // delete characters from nStart to nStart + nLen
737 wxString& erase(size_t nStart = 0, size_t nLen = npos);
738
739 // replaces the substring of length nLen starting at nStart
2bb67b80 740 wxString& replace(size_t nStart, size_t nLen, const wxChar* sz);
3c67202d 741 // replaces the substring with nCount copies of ch
2bb67b80 742 wxString& replace(size_t nStart, size_t nLen, size_t nCount, wxChar ch);
3c67202d
VZ
743 // replaces a substring with another substring
744 wxString& replace(size_t nStart, size_t nLen,
745 const wxString& str, size_t nStart2, size_t nLen2);
746 // replaces the substring with first nCount chars of sz
747 wxString& replace(size_t nStart, size_t nLen,
2bb67b80 748 const wxChar* sz, size_t nCount);
3c67202d
VZ
749
750 // swap two strings
751 void swap(wxString& str);
752
753 // All find() functions take the nStart argument which specifies the
754 // position to start the search on, the default value is 0. All functions
755 // return npos if there were no match.
756
757 // find a substring
758 size_t find(const wxString& str, size_t nStart = 0) const;
759
760 // VC++ 1.5 can't cope with this syntax.
3f4a0c5b 761#if !defined(__VISUALC__) || defined(__WIN32__)
3c67202d 762 // find first n characters of sz
2bb67b80 763 size_t find(const wxChar* sz, size_t nStart = 0, size_t n = npos) const;
6b0eb19f 764#endif
3c67202d
VZ
765
766 // Gives a duplicate symbol (presumably a case-insensitivity problem)
62448488 767#if !defined(__BORLANDC__)
3c67202d 768 // find the first occurence of character ch after nStart
2bb67b80 769 size_t find(wxChar ch, size_t nStart = 0) const;
62448488 770#endif
3c67202d
VZ
771 // rfind() family is exactly like find() but works right to left
772
773 // as find, but from the end
774 size_t rfind(const wxString& str, size_t nStart = npos) const;
775
776 // VC++ 1.5 can't cope with this syntax.
3f4a0c5b 777#if !defined(__VISUALC__) || defined(__WIN32__)
3c67202d 778 // as find, but from the end
2bb67b80 779 size_t rfind(const wxChar* sz, size_t nStart = npos,
3c67202d
VZ
780 size_t n = npos) const;
781 // as find, but from the end
2bb67b80 782 size_t rfind(wxChar ch, size_t nStart = npos) const;
c801d85f 783#endif
3c67202d
VZ
784
785 // find first/last occurence of any character in the set
786
787 //
788 size_t find_first_of(const wxString& str, size_t nStart = 0) const;
789 //
2bb67b80 790 size_t find_first_of(const wxChar* sz, size_t nStart = 0) const;
3c67202d 791 // same as find(char, size_t)
2bb67b80 792 size_t find_first_of(wxChar c, size_t nStart = 0) const;
3c67202d
VZ
793 //
794 size_t find_last_of (const wxString& str, size_t nStart = npos) const;
795 //
2bb67b80 796 size_t find_last_of (const wxChar* s, size_t nStart = npos) const;
3c67202d 797 // same as rfind(char, size_t)
2bb67b80 798 size_t find_last_of (wxChar c, size_t nStart = npos) const;
3c67202d
VZ
799
800 // find first/last occurence of any character not in the set
801
802 //
803 size_t find_first_not_of(const wxString& str, size_t nStart = 0) const;
804 //
2bb67b80 805 size_t find_first_not_of(const wxChar* s, size_t nStart = 0) const;
3c67202d 806 //
2bb67b80 807 size_t find_first_not_of(wxChar ch, size_t nStart = 0) const;
3c67202d
VZ
808 //
809 size_t find_last_not_of(const wxString& str, size_t nStart=npos) const;
810 //
2bb67b80 811 size_t find_last_not_of(const wxChar* s, size_t nStart = npos) const;
3c67202d 812 //
2bb67b80 813 size_t find_last_not_of(wxChar ch, size_t nStart = npos) const;
3c67202d
VZ
814
815 // All compare functions return -1, 0 or 1 if the [sub]string is less,
816 // equal or greater than the compare() argument.
817
818 // just like strcmp()
819 int compare(const wxString& str) const { return Cmp(str); }
820 // comparison with a substring
821 int compare(size_t nStart, size_t nLen, const wxString& str) const;
822 // comparison of 2 substrings
823 int compare(size_t nStart, size_t nLen,
824 const wxString& str, size_t nStart2, size_t nLen2) const;
825 // just like strcmp()
2bb67b80 826 int compare(const wxChar* sz) const { return Cmp(sz); }
3c67202d
VZ
827 // substring comparison with first nCount characters of sz
828 int compare(size_t nStart, size_t nLen,
2bb67b80 829 const wxChar* sz, size_t nCount = npos) const;
3c67202d
VZ
830
831 // substring extraction
832 wxString substr(size_t nStart = 0, size_t nLen = npos) const;
833#endif // wxSTD_STRING_COMPATIBILITY
c801d85f
KB
834};
835
836// ----------------------------------------------------------------------------
3c67202d
VZ
837// The string array uses it's knowledge of internal structure of the wxString
838// class to optimize string storage. Normally, we would store pointers to
839// string, but as wxString is, in fact, itself a pointer (sizeof(wxString) is
840// sizeof(char *)) we store these pointers instead. The cast to "wxString *" is
841// really all we need to turn such pointer into a string!
842//
843// Of course, it can be called a dirty hack, but we use twice less memory and
844// this approach is also more speed efficient, so it's probably worth it.
845//
846// Usage notes: when a string is added/inserted, a new copy of it is created,
847// so the original string may be safely deleted. When a string is retrieved
848// from the array (operator[] or Item() method), a reference is returned.
c801d85f 849// ----------------------------------------------------------------------------
fbcb4166 850class WXDLLEXPORT wxArrayString
c801d85f
KB
851{
852public:
30b21f9a
VZ
853 // type of function used by wxArrayString::Sort()
854 typedef int (*CompareFunction)(const wxString& first,
855 const wxString& second);
856
3c67202d
VZ
857 // constructors and destructor
858 // default ctor
c801d85f 859 wxArrayString();
3c67202d 860 // copy ctor
c801d85f 861 wxArrayString(const wxArrayString& array);
3c67202d 862 // assignment operator
c801d85f 863 wxArrayString& operator=(const wxArrayString& src);
3c67202d 864 // not virtual, this class should not be derived from
c801d85f 865 ~wxArrayString();
c801d85f 866
3c67202d
VZ
867 // memory management
868 // empties the list, but doesn't release memory
c801d85f 869 void Empty();
3c67202d 870 // empties the list and releases memory
c801d85f 871 void Clear();
3c67202d 872 // preallocates memory for given number of items
c801d85f 873 void Alloc(size_t nCount);
3c67202d 874 // minimzes the memory usage (by freeing all extra memory)
dd1eaa89 875 void Shrink();
3c67202d
VZ
876
877 // simple accessors
878 // number of elements in the array
879 size_t GetCount() const { return m_nCount; }
880 // is it empty?
881 bool IsEmpty() const { return m_nCount == 0; }
882 // number of elements in the array (GetCount is preferred API)
883 size_t Count() const { return m_nCount; }
884
885 // items access (range checking is done in debug version)
886 // get item at position uiIndex
c801d85f
KB
887 wxString& Item(size_t nIndex) const
888 { wxASSERT( nIndex < m_nCount ); return *(wxString *)&(m_pItems[nIndex]); }
3c67202d 889 // same as Item()
c801d85f 890 wxString& operator[](size_t nIndex) const { return Item(nIndex); }
3c67202d 891 // get last item
c801d85f 892 wxString& Last() const { wxASSERT( !IsEmpty() ); return Item(Count() - 1); }
3c67202d
VZ
893
894 // item management
895 // Search the element in the array, starting from the beginning if
896 // bFromEnd is FALSE or from end otherwise. If bCase, comparison is case
897 // sensitive (default). Returns index of the first item matched or
898 // wxNOT_FOUND
2bb67b80 899 int Index (const wxChar *sz, bool bCase = TRUE, bool bFromEnd = FALSE) const;
3c67202d
VZ
900 // add new element at the end
901 void Add(const wxString& str);
902 // add new element at given position
c86f1403 903 void Insert(const wxString& str, size_t uiIndex);
3c67202d 904 // remove first item matching this value
2bb67b80 905 void Remove(const wxChar *sz);
3c67202d 906 // remove item by index
c801d85f 907 void Remove(size_t nIndex);
c801d85f 908
30b21f9a
VZ
909 // sorting
910 // sort array elements in alphabetical order (or reversed alphabetical
911 // order if reverseOrder parameter is TRUE)
912 void Sort(bool reverseOrder = FALSE);
913 // sort array elements using specified comparaison function
914 void Sort(CompareFunction compareFunction);
c801d85f
KB
915
916private:
917 void Grow(); // makes array bigger if needed
918 void Free(); // free the string stored
919
30b21f9a
VZ
920 void DoSort(); // common part of all Sort() variants
921
3c67202d 922 size_t m_nSize, // current size of the array
c801d85f
KB
923 m_nCount; // current number of elements
924
2bb67b80 925 wxChar **m_pItems; // pointer to data
c801d85f
KB
926};
927
c801d85f 928// ---------------------------------------------------------------------------
3c67202d 929// wxString comparison functions: operator versions are always case sensitive
c801d85f 930// ---------------------------------------------------------------------------
3c67202d 931//
a3ef5bf5 932inline bool operator==(const wxString& s1, const wxString& s2) { return (s1.Cmp(s2) == 0); }
3c67202d 933//
2bb67b80 934inline bool operator==(const wxString& s1, const wxChar * s2) { return (s1.Cmp(s2) == 0); }
3c67202d 935//
2bb67b80 936inline bool operator==(const wxChar * s1, const wxString& s2) { return (s2.Cmp(s1) == 0); }
3c67202d 937//
a3ef5bf5 938inline bool operator!=(const wxString& s1, const wxString& s2) { return (s1.Cmp(s2) != 0); }
3c67202d 939//
2bb67b80 940inline bool operator!=(const wxString& s1, const wxChar * s2) { return (s1.Cmp(s2) != 0); }
3c67202d 941//
2bb67b80 942inline bool operator!=(const wxChar * s1, const wxString& s2) { return (s2.Cmp(s1) != 0); }
3c67202d 943//
a3ef5bf5 944inline bool operator< (const wxString& s1, const wxString& s2) { return (s1.Cmp(s2) < 0); }
3c67202d 945//
2bb67b80 946inline bool operator< (const wxString& s1, const wxChar * s2) { return (s1.Cmp(s2) < 0); }
3c67202d 947//
2bb67b80 948inline bool operator< (const wxChar * s1, const wxString& s2) { return (s2.Cmp(s1) > 0); }
3c67202d 949//
a3ef5bf5 950inline bool operator> (const wxString& s1, const wxString& s2) { return (s1.Cmp(s2) > 0); }
3c67202d 951//
2bb67b80 952inline bool operator> (const wxString& s1, const wxChar * s2) { return (s1.Cmp(s2) > 0); }
3c67202d 953//
2bb67b80 954inline bool operator> (const wxChar * s1, const wxString& s2) { return (s2.Cmp(s1) < 0); }
3c67202d 955//
a3ef5bf5 956inline bool operator<=(const wxString& s1, const wxString& s2) { return (s1.Cmp(s2) <= 0); }
3c67202d 957//
2bb67b80 958inline bool operator<=(const wxString& s1, const wxChar * s2) { return (s1.Cmp(s2) <= 0); }
3c67202d 959//
2bb67b80 960inline bool operator<=(const wxChar * s1, const wxString& s2) { return (s2.Cmp(s1) >= 0); }
3c67202d 961//
a3ef5bf5 962inline bool operator>=(const wxString& s1, const wxString& s2) { return (s1.Cmp(s2) >= 0); }
3c67202d 963//
2bb67b80 964inline bool operator>=(const wxString& s1, const wxChar * s2) { return (s1.Cmp(s2) >= 0); }
3c67202d 965//
2bb67b80 966inline bool operator>=(const wxChar * s1, const wxString& s2) { return (s2.Cmp(s1) <= 0); }
3c67202d 967
ed7174ba 968wxString WXDLLEXPORT operator+(const wxString& string1, const wxString& string2);
2bb67b80
OK
969wxString WXDLLEXPORT operator+(const wxString& string, wxChar ch);
970wxString WXDLLEXPORT operator+(wxChar ch, const wxString& string);
971wxString WXDLLEXPORT operator+(const wxString& string, const wxChar *psz);
972wxString WXDLLEXPORT operator+(const wxChar *psz, const wxString& string);
f04f3991 973
c801d85f 974// ---------------------------------------------------------------------------
3c67202d 975// Implementation only from here until the end of file
c801d85f
KB
976// ---------------------------------------------------------------------------
977
3c67202d 978#ifdef wxSTD_STRING_COMPATIBILITY
c801d85f 979
3f4a0c5b 980#include "wx/ioswrap.h"
c801d85f 981
184b5d99 982WXDLLEXPORT istream& operator>>(istream& is, wxString& str);
c801d85f 983
3c67202d 984#endif // wxSTD_STRING_COMPATIBILITY
c801d85f 985
34138703 986#endif // _WX_WXSTRINGH__