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