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