]> git.saurik.com Git - wxWidgets.git/blame - include/wx/string.h
char handling fixed again
[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
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 62// (as sizeof(StringData) is unknown here we substract 100)
566b84d2 63const unsigned int wxSTRING_MAXLEN = UINT_MAX - 100;
c801d85f
KB
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
dbda9e86 77inline bool WXDLLEXPORT IsEmpty(const char *p) { return (!p || !*p); }
c801d85f 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{
8be97d65 86#if defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) )
dd1eaa89 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);
8be97d65 98#elif defined(__MWERKS__) && !defined(__INTEL__)
17dff81c
SC
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
dbda9e86
JS
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 159 // intentionally put in invalid state
dbda9e86
JS
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
566b84d2 213 void InitWith(const char *psz, size_t nPos = 0, size_t nLen = wxSTRING_MAXLEN);
6b95b20d
VZ
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
c606a9a4
VZ
233 // this method is not implemented - there is _no_ conversion from int to
234 // string, you're doing something wrong if the compiler wants to call it!
235 //
236 // try `s << i' or `s.Printf("%d", i)' instead
237 wxString(int);
238 wxString(long);
239
c801d85f 240public:
3c67202d
VZ
241 // constructors and destructor
242 // ctor for an empty string
6b95b20d 243 wxString() { Init(); }
3c67202d 244 // copy ctor
6b95b20d
VZ
245 wxString(const wxString& stringSrc)
246 {
247 wxASSERT( stringSrc.GetStringData()->IsValid() );
248
249 if ( stringSrc.IsEmpty() ) {
250 // nothing to do for an empty string
251 Init();
252 }
253 else {
254 m_pchData = stringSrc.m_pchData; // share same data
255 GetStringData()->Lock(); // => one more copy
256 }
257 }
3c67202d 258 // string containing nRepeat copies of ch
dd1eaa89 259 wxString(char ch, size_t nRepeat = 1);
3c67202d 260 // ctor takes first nLength characters from C string
566b84d2
VZ
261 // (default value of wxSTRING_MAXLEN means take all the string)
262 wxString(const char *psz, size_t nLength = wxSTRING_MAXLEN)
6b95b20d 263 { InitWith(psz, 0, nLength); }
3c67202d 264 // from C string (for compilers using unsigned char)
566b84d2 265 wxString(const unsigned char* psz, size_t nLength = wxSTRING_MAXLEN);
3c67202d 266 // from wide (UNICODE) string
c801d85f 267 wxString(const wchar_t *pwz);
3c67202d 268 // dtor is not virtual, this class must not be inherited from!
6b95b20d 269 ~wxString() { GetStringData()->Unlock(); }
c801d85f 270
3c67202d
VZ
271 // generic attributes & operations
272 // as standard strlen()
47d67540 273 size_t Len() const { return GetStringData()->nDataLength; }
3c67202d 274 // string contains any characters?
dd1eaa89 275 bool IsEmpty() const { return Len() == 0; }
dcfde592
VZ
276 // empty string is "FALSE", so !str will return TRUE
277 bool operator!() const { return IsEmpty(); }
3c67202d 278 // empty string contents
dd1eaa89
VZ
279 void Empty()
280 {
2c3b684c 281 if ( !IsEmpty() )
dd1eaa89
VZ
282 Reinit();
283
7be07660 284 // should be empty
dd1eaa89 285 wxASSERT( GetStringData()->nDataLength == 0 );
7be07660 286 }
3c67202d 287 // empty the string and free memory
7be07660
VZ
288 void Clear()
289 {
290 if ( !GetStringData()->IsEmpty() )
291 Reinit();
292
293 wxASSERT( GetStringData()->nDataLength == 0 ); // should be empty
294 wxASSERT( GetStringData()->nAllocLength == 0 ); // and not own any memory
dd1eaa89
VZ
295 }
296
3c67202d
VZ
297 // contents test
298 // Is an ascii value
c801d85f 299 bool IsAscii() const;
3c67202d 300 // Is a number
c801d85f 301 bool IsNumber() const;
3c67202d 302 // Is a word
c801d85f 303 bool IsWord() const;
c801d85f 304
3c67202d
VZ
305 // data access (all indexes are 0 based)
306 // read access
c801d85f 307 char GetChar(size_t n) const
dd1eaa89 308 { ASSERT_VALID_INDEX( n ); return m_pchData[n]; }
3c67202d 309 // read/write access
c801d85f 310 char& GetWritableChar(size_t n)
dd1eaa89 311 { ASSERT_VALID_INDEX( n ); CopyBeforeWrite(); return m_pchData[n]; }
3c67202d 312 // write access
c801d85f
KB
313 void SetChar(size_t n, char ch)
314 { ASSERT_VALID_INDEX( n ); CopyBeforeWrite(); m_pchData[n] = ch; }
315
3c67202d 316 // get last character
c801d85f
KB
317 char Last() const
318 { wxASSERT( !IsEmpty() ); return m_pchData[Len() - 1]; }
3c67202d 319 // get writable last character
dd1eaa89 320 char& Last()
c801d85f
KB
321 { wxASSERT( !IsEmpty() ); CopyBeforeWrite(); return m_pchData[Len()-1]; }
322
57493f9f
VZ
323 // under Unix it is tested with configure, assume it works on other
324 // platforms (there might be overloading problems if size_t and int are
325 // the same type)
326#if !defined(__UNIX__) || defined(wxUSE_SIZE_T_STRING_OPERATOR)
3c67202d 327 // operator version of GetChar
c801d85f
KB
328 char operator[](size_t n) const
329 { ASSERT_VALID_INDEX( n ); return m_pchData[n]; }
22552603 330#endif
c5248639 331
3c67202d 332 // operator version of GetChar
c801d85f
KB
333 char operator[](int n) const
334 { ASSERT_VALID_INDEX( n ); return m_pchData[n]; }
3c67202d 335 // operator version of GetWritableChar
c801d85f
KB
336 char& operator[](size_t n)
337 { ASSERT_VALID_INDEX( n ); CopyBeforeWrite(); return m_pchData[n]; }
338
3c67202d 339 // implicit conversion to C string
dd1eaa89 340 operator const char*() const { return m_pchData; }
3c67202d 341 // explicit conversion to C string (use this with printf()!)
c801d85f 342 const char* c_str() const { return m_pchData; }
3c67202d 343 //
c801d85f 344 const char* GetData() const { return m_pchData; }
c801d85f 345
3c67202d
VZ
346 // overloaded assignment
347 // from another wxString
c801d85f 348 wxString& operator=(const wxString& stringSrc);
3c67202d 349 // from a character
c801d85f 350 wxString& operator=(char ch);
3c67202d 351 // from a C string
c801d85f 352 wxString& operator=(const char *psz);
3c67202d 353 // from another kind of C string
c801d85f 354 wxString& operator=(const unsigned char* psz);
3c67202d 355 // from a wide string
c801d85f 356 wxString& operator=(const wchar_t *pwz);
3c67202d
VZ
357
358 // string concatenation
359 // in place concatenation
360 /*
361 Concatenate and return the result. Note that the left to right
362 associativity of << allows to write things like "str << str1 << str2
363 << ..." (unlike with +=)
364 */
365 // string += string
dd1eaa89
VZ
366 wxString& operator<<(const wxString& s)
367 {
368 wxASSERT( s.GetStringData()->IsValid() );
369
370 ConcatSelf(s.Len(), s);
371 return *this;
372 }
3c67202d 373 // string += C string
dd1eaa89
VZ
374 wxString& operator<<(const char *psz)
375 { ConcatSelf(Strlen(psz), psz); return *this; }
3c67202d 376 // string += char
dd1eaa89 377 wxString& operator<<(char ch) { ConcatSelf(1, &ch); return *this; }
dd1eaa89 378
3c67202d 379 // string += string
6b95b20d 380 void operator+=(const wxString& s) { (void)operator<<(s); }
3c67202d 381 // string += C string
6b95b20d 382 void operator+=(const char *psz) { (void)operator<<(psz); }
3c67202d 383 // string += char
6b95b20d 384 void operator+=(char ch) { (void)operator<<(ch); }
6b95b20d 385
3c67202d
VZ
386 // string += C string
387 wxString& Append(const char* psz)
388 { ConcatSelf(Strlen(psz), psz); return *this; }
389 // append count copies of given character
390 wxString& Append(char ch, size_t count = 1u)
391 { wxString str(ch, count); return *this << str; }
392
393 // prepend a string, return the string itself
394 wxString& Prepend(const wxString& str)
395 { *this = str + *this; return *this; }
396
397 // non-destructive concatenation
398 //
c33534e5 399 friend wxString WXDLLEXPORT operator+(const wxString& string1, const wxString& string2);
3c67202d 400 //
c33534e5 401 friend wxString WXDLLEXPORT operator+(const wxString& string, char ch);
3c67202d 402 //
c33534e5 403 friend wxString WXDLLEXPORT operator+(char ch, const wxString& string);
3c67202d 404 //
c33534e5 405 friend wxString WXDLLEXPORT operator+(const wxString& string, const char *psz);
3c67202d 406 //
c33534e5 407 friend wxString WXDLLEXPORT operator+(const char *psz, const wxString& string);
3c67202d
VZ
408
409 // stream-like functions
410 // insert an int into string
411 wxString& operator<<(int i);
412 // insert a float into string
413 wxString& operator<<(float f);
414 // insert a double into string
415 wxString& operator<<(double d);
c84c52de 416
3c67202d 417 // string comparison
30b21f9a 418 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
c801d85f 419 int Cmp(const char *psz) const { return strcmp(c_str(), psz); }
3c67202d 420 // same as Cmp() but not case-sensitive
c801d85f 421 int CmpNoCase(const char *psz) const { return Stricmp(c_str(), psz); }
3c67202d
VZ
422 // test for the string equality, either considering case or not
423 // (if compareWithCase then the case matters)
424 bool IsSameAs(const char *psz, bool compareWithCase = TRUE) const
425 { return (compareWithCase ? Cmp(psz) : CmpNoCase(psz)) == 0; }
426
427 // simple sub-string extraction
428 // return substring starting at nFirst of length nCount (or till the end
429 // if nCount = default value)
566b84d2 430 wxString Mid(size_t nFirst, size_t nCount = wxSTRING_MAXLEN) const;
3c67202d
VZ
431
432 // operator version of Mid()
433 wxString operator()(size_t start, size_t len) const
434 { return Mid(start, len); }
435
436 // get first nCount characters
c801d85f 437 wxString Left(size_t nCount) const;
3c67202d 438 // get last nCount characters
c801d85f 439 wxString Right(size_t nCount) const;
3c67202d
VZ
440 // get all characters before the first occurence of ch
441 // (returns the whole string if ch not found)
442 wxString BeforeFirst(char ch) const;
443 // get all characters before the last occurence of ch
444 // (returns empty string if ch not found)
445 wxString BeforeLast(char ch) const;
446 // get all characters after the first occurence of ch
447 // (returns empty string if ch not found)
448 wxString AfterFirst(char ch) const;
449 // get all characters after the last occurence of ch
450 // (returns the whole string if ch not found)
451 wxString AfterLast(char ch) const;
452
453 // for compatibility only, use more explicitly named functions above
c84c52de
VZ
454 wxString Before(char ch) const { return BeforeLast(ch); }
455 wxString After(char ch) const { return AfterFirst(ch); }
3c67202d
VZ
456
457 // case conversion
c84c52de 458 // convert to upper case in place, return the string itself
c801d85f 459 wxString& MakeUpper();
c84c52de 460 // convert to upper case, return the copy of the string
03ab016d
JS
461 // Here's something to remember: BC++ doesn't like returns in inlines.
462 wxString Upper() const ;
c84c52de 463 // convert to lower case in place, return the string itself
c801d85f 464 wxString& MakeLower();
c84c52de 465 // convert to lower case, return the copy of the string
03ab016d 466 wxString Lower() const ;
c801d85f 467
3c67202d
VZ
468 // trimming/padding whitespace (either side) and truncating
469 // remove spaces from left or from right (default) side
c801d85f 470 wxString& Trim(bool bFromRight = TRUE);
3c67202d 471 // add nCount copies chPad in the beginning or at the end (default)
c801d85f 472 wxString& Pad(size_t nCount, char chPad = ' ', bool bFromRight = TRUE);
3c67202d 473 // truncate string to given length
c801d85f 474 wxString& Truncate(size_t uiLen);
dd1eaa89 475
3c67202d
VZ
476 // searching and replacing
477 // searching (return starting index, or -1 if not found)
c801d85f 478 int Find(char ch, bool bFromEnd = FALSE) const; // like strchr/strrchr
3c67202d 479 // searching (return starting index, or -1 if not found)
c801d85f 480 int Find(const char *pszSub) const; // like strstr
3c67202d
VZ
481 // replace first (or all of bReplaceAll) occurences of substring with
482 // another string, returns the number of replacements made
483 size_t Replace(const char *szOld,
484 const char *szNew,
485 bool bReplaceAll = TRUE);
486
487 // check if the string contents matches a mask containing '*' and '?'
8fd0f20b 488 bool Matches(const char *szMask) const;
c801d85f 489
3c67202d
VZ
490 // formated input/output
491 // as sprintf(), returns the number of characters written or < 0 on error
c801d85f 492 int Printf(const char *pszFormat, ...);
3c67202d 493 // as vprintf(), returns the number of characters written or < 0 on error
c801d85f 494 int PrintfV(const char* pszFormat, va_list argptr);
dd1eaa89 495
3c67202d
VZ
496 // raw access to string memory
497 // ensure that string has space for at least nLen characters
dd1eaa89 498 // only works if the data of this string is not shared
c86f1403 499 void Alloc(size_t nLen);
3c67202d 500 // minimize the string's memory
dd1eaa89
VZ
501 // only works if the data of this string is not shared
502 void Shrink();
3c67202d
VZ
503 // get writable buffer of at least nLen bytes. Unget() *must* be called
504 // a.s.a.p. to put string back in a reasonable state!
c86f1403 505 char *GetWriteBuf(size_t nLen);
3c67202d 506 // call this immediately after GetWriteBuf() has been used
8fd0f20b 507 void UngetWriteBuf();
c801d85f 508
3c67202d
VZ
509 // wxWindows version 1 compatibility functions
510
511 // use Mid()
512 wxString SubString(size_t from, size_t to) const
513 { return Mid(from, (to - from + 1)); }
514 // values for second parameter of CompareTo function
c801d85f 515 enum caseCompare {exact, ignoreCase};
3c67202d 516 // values for first parameter of Strip function
c801d85f 517 enum stripType {leading = 0x1, trailing = 0x2, both = 0x3};
8870c26e 518
3c67202d 519 // use Printf()
8870c26e 520 int sprintf(const char *pszFormat, ...);
c801d85f 521
3c67202d 522 // use Cmp()
c801d85f 523 inline int CompareTo(const char* psz, caseCompare cmp = exact) const
6b95b20d 524 { return cmp == exact ? Cmp(psz) : CmpNoCase(psz); }
c801d85f 525
3c67202d 526 // use Len
c801d85f 527 size_t Length() const { return Len(); }
3c67202d 528 // Count the number of characters
1fc5dd6f 529 int Freq(char ch) const;
3c67202d 530 // use MakeLower
c801d85f 531 void LowerCase() { MakeLower(); }
3c67202d 532 // use MakeUpper
c801d85f 533 void UpperCase() { MakeUpper(); }
3c67202d 534 // use Trim except that it doesn't change this string
c801d85f
KB
535 wxString Strip(stripType w = trailing) const;
536
3c67202d 537 // use Find (more general variants not yet supported)
c801d85f
KB
538 size_t Index(const char* psz) const { return Find(psz); }
539 size_t Index(char ch) const { return Find(ch); }
3c67202d 540 // use Truncate
c801d85f
KB
541 wxString& Remove(size_t pos) { return Truncate(pos); }
542 wxString& RemoveLast() { return Truncate(Len() - 1); }
543
3ed358cb 544 wxString& Remove(size_t nStart, size_t nLen) { return erase( nStart, nLen ); }
dd1eaa89 545
3c67202d 546 // use Find()
3ed358cb
VZ
547 int First( const char ch ) const { return Find(ch); }
548 int First( const char* psz ) const { return Find(psz); }
549 int First( const wxString &str ) const { return Find(str); }
3ed358cb 550 int Last( const char ch ) const { return Find(ch, TRUE); }
3c67202d 551 bool Contains(const wxString& str) const { return Find(str) != -1; }
c801d85f 552
3c67202d 553 // use IsEmpty()
c801d85f 554 bool IsNull() const { return IsEmpty(); }
c801d85f 555
3c67202d
VZ
556#ifdef wxSTD_STRING_COMPATIBILITY
557 // std::string compatibility functions
dd1eaa89 558
3c67202d 559 // an 'invalid' value for string index
c801d85f 560 static const size_t npos;
dd1eaa89 561
3c67202d
VZ
562 // constructors
563 // take nLen chars starting at nPos
564 wxString(const wxString& str, size_t nPos, size_t nLen)
565 {
566 wxASSERT( str.GetStringData()->IsValid() );
567 InitWith(str.c_str(), nPos, nLen == npos ? 0 : nLen);
568 }
569 // take all characters from pStart to pEnd
570 wxString(const void *pStart, const void *pEnd);
571
572 // lib.string.capacity
573 // return the length of the string
574 size_t size() const { return Len(); }
575 // return the length of the string
576 size_t length() const { return Len(); }
577 // return the maximum size of the string
566b84d2 578 size_t max_size() const { return wxSTRING_MAXLEN; }
3c67202d
VZ
579 // resize the string, filling the space with c if c != 0
580 void resize(size_t nSize, char ch = '\0');
581 // delete the contents of the string
582 void clear() { Empty(); }
583 // returns true if the string is empty
584 bool empty() const { return IsEmpty(); }
585
586 // lib.string.access
587 // return the character at position n
588 char at(size_t n) const { return GetChar(n); }
589 // returns the writable character at position n
590 char& at(size_t n) { return GetWritableChar(n); }
591
592 // lib.string.modifiers
593 // append a string
594 wxString& append(const wxString& str)
595 { *this += str; return *this; }
596 // append elements str[pos], ..., str[pos+n]
597 wxString& append(const wxString& str, size_t pos, size_t n)
598 { ConcatSelf(n, str.c_str() + pos); return *this; }
599 // append first n (or all if n == npos) characters of sz
600 wxString& append(const char *sz, size_t n = npos)
601 { ConcatSelf(n == npos ? Strlen(sz) : n, sz); return *this; }
602
603 // append n copies of ch
604 wxString& append(size_t n, char ch) { return Pad(n, ch); }
605
606 // same as `this_string = str'
607 wxString& assign(const wxString& str) { return (*this) = str; }
608 // same as ` = str[pos..pos + n]
609 wxString& assign(const wxString& str, size_t pos, size_t n)
610 { return *this = wxString((const char *)str + pos, n); }
611 // same as `= first n (or all if n == npos) characters of sz'
612 wxString& assign(const char *sz, size_t n = npos)
613 { return *this = wxString(sz, n); }
614 // same as `= n copies of ch'
615 wxString& assign(size_t n, char ch)
616 { return *this = wxString(ch, n); }
617
618 // insert another string
619 wxString& insert(size_t nPos, const wxString& str);
620 // insert n chars of str starting at nStart (in str)
621 wxString& insert(size_t nPos, const wxString& str, size_t nStart, size_t n)
622 { return insert(nPos, wxString((const char *)str + nStart, n)); }
623
624 // insert first n (or all if n == npos) characters of sz
625 wxString& insert(size_t nPos, const char *sz, size_t n = npos)
626 { return insert(nPos, wxString(sz, n)); }
627 // insert n copies of ch
628 wxString& insert(size_t nPos, size_t n, char ch)
629 { return insert(nPos, wxString(ch, n)); }
630
631 // delete characters from nStart to nStart + nLen
632 wxString& erase(size_t nStart = 0, size_t nLen = npos);
633
634 // replaces the substring of length nLen starting at nStart
635 wxString& replace(size_t nStart, size_t nLen, const char* sz);
636 // replaces the substring with nCount copies of ch
637 wxString& replace(size_t nStart, size_t nLen, size_t nCount, char ch);
638 // replaces a substring with another substring
639 wxString& replace(size_t nStart, size_t nLen,
640 const wxString& str, size_t nStart2, size_t nLen2);
641 // replaces the substring with first nCount chars of sz
642 wxString& replace(size_t nStart, size_t nLen,
643 const char* sz, size_t nCount);
644
645 // swap two strings
646 void swap(wxString& str);
647
648 // All find() functions take the nStart argument which specifies the
649 // position to start the search on, the default value is 0. All functions
650 // return npos if there were no match.
651
652 // find a substring
653 size_t find(const wxString& str, size_t nStart = 0) const;
654
655 // VC++ 1.5 can't cope with this syntax.
3f4a0c5b 656#if !defined(__VISUALC__) || defined(__WIN32__)
3c67202d
VZ
657 // find first n characters of sz
658 size_t find(const char* sz, size_t nStart = 0, size_t n = npos) const;
6b0eb19f 659#endif
3c67202d
VZ
660
661 // Gives a duplicate symbol (presumably a case-insensitivity problem)
62448488 662#if !defined(__BORLANDC__)
3c67202d
VZ
663 // find the first occurence of character ch after nStart
664 size_t find(char ch, size_t nStart = 0) const;
62448488 665#endif
3c67202d
VZ
666 // rfind() family is exactly like find() but works right to left
667
668 // as find, but from the end
669 size_t rfind(const wxString& str, size_t nStart = npos) const;
670
671 // VC++ 1.5 can't cope with this syntax.
3f4a0c5b 672#if !defined(__VISUALC__) || defined(__WIN32__)
3c67202d
VZ
673 // as find, but from the end
674 size_t rfind(const char* sz, size_t nStart = npos,
675 size_t n = npos) const;
676 // as find, but from the end
677 size_t rfind(char ch, size_t nStart = npos) const;
c801d85f 678#endif
3c67202d
VZ
679
680 // find first/last occurence of any character in the set
681
682 //
683 size_t find_first_of(const wxString& str, size_t nStart = 0) const;
684 //
685 size_t find_first_of(const char* sz, size_t nStart = 0) const;
686 // same as find(char, size_t)
687 size_t find_first_of(char c, size_t nStart = 0) const;
688 //
689 size_t find_last_of (const wxString& str, size_t nStart = npos) const;
690 //
691 size_t find_last_of (const char* s, size_t nStart = npos) const;
692 // same as rfind(char, size_t)
693 size_t find_last_of (char c, size_t nStart = npos) const;
694
695 // find first/last occurence of any character not in the set
696
697 //
698 size_t find_first_not_of(const wxString& str, size_t nStart = 0) const;
699 //
700 size_t find_first_not_of(const char* s, size_t nStart = 0) const;
701 //
702 size_t find_first_not_of(char ch, size_t nStart = 0) const;
703 //
704 size_t find_last_not_of(const wxString& str, size_t nStart=npos) const;
705 //
706 size_t find_last_not_of(const char* s, size_t nStart = npos) const;
707 //
708 size_t find_last_not_of(char ch, size_t nStart = npos) const;
709
710 // All compare functions return -1, 0 or 1 if the [sub]string is less,
711 // equal or greater than the compare() argument.
712
713 // just like strcmp()
714 int compare(const wxString& str) const { return Cmp(str); }
715 // comparison with a substring
716 int compare(size_t nStart, size_t nLen, const wxString& str) const;
717 // comparison of 2 substrings
718 int compare(size_t nStart, size_t nLen,
719 const wxString& str, size_t nStart2, size_t nLen2) const;
720 // just like strcmp()
721 int compare(const char* sz) const { return Cmp(sz); }
722 // substring comparison with first nCount characters of sz
723 int compare(size_t nStart, size_t nLen,
724 const char* sz, size_t nCount = npos) const;
725
726 // substring extraction
727 wxString substr(size_t nStart = 0, size_t nLen = npos) const;
728#endif // wxSTD_STRING_COMPATIBILITY
c801d85f
KB
729};
730
731// ----------------------------------------------------------------------------
3c67202d
VZ
732// The string array uses it's knowledge of internal structure of the wxString
733// class to optimize string storage. Normally, we would store pointers to
734// string, but as wxString is, in fact, itself a pointer (sizeof(wxString) is
735// sizeof(char *)) we store these pointers instead. The cast to "wxString *" is
736// really all we need to turn such pointer into a string!
737//
738// Of course, it can be called a dirty hack, but we use twice less memory and
739// this approach is also more speed efficient, so it's probably worth it.
740//
741// Usage notes: when a string is added/inserted, a new copy of it is created,
742// so the original string may be safely deleted. When a string is retrieved
743// from the array (operator[] or Item() method), a reference is returned.
c801d85f 744// ----------------------------------------------------------------------------
fbcb4166 745class WXDLLEXPORT wxArrayString
c801d85f
KB
746{
747public:
30b21f9a
VZ
748 // type of function used by wxArrayString::Sort()
749 typedef int (*CompareFunction)(const wxString& first,
750 const wxString& second);
751
3c67202d
VZ
752 // constructors and destructor
753 // default ctor
c801d85f 754 wxArrayString();
3c67202d 755 // copy ctor
c801d85f 756 wxArrayString(const wxArrayString& array);
3c67202d 757 // assignment operator
c801d85f 758 wxArrayString& operator=(const wxArrayString& src);
3c67202d 759 // not virtual, this class should not be derived from
c801d85f 760 ~wxArrayString();
c801d85f 761
3c67202d
VZ
762 // memory management
763 // empties the list, but doesn't release memory
c801d85f 764 void Empty();
3c67202d 765 // empties the list and releases memory
c801d85f 766 void Clear();
3c67202d 767 // preallocates memory for given number of items
c801d85f 768 void Alloc(size_t nCount);
3c67202d 769 // minimzes the memory usage (by freeing all extra memory)
dd1eaa89 770 void Shrink();
3c67202d
VZ
771
772 // simple accessors
773 // number of elements in the array
774 size_t GetCount() const { return m_nCount; }
775 // is it empty?
776 bool IsEmpty() const { return m_nCount == 0; }
777 // number of elements in the array (GetCount is preferred API)
778 size_t Count() const { return m_nCount; }
779
780 // items access (range checking is done in debug version)
781 // get item at position uiIndex
c801d85f
KB
782 wxString& Item(size_t nIndex) const
783 { wxASSERT( nIndex < m_nCount ); return *(wxString *)&(m_pItems[nIndex]); }
3c67202d 784 // same as Item()
c801d85f 785 wxString& operator[](size_t nIndex) const { return Item(nIndex); }
3c67202d 786 // get last item
c801d85f 787 wxString& Last() const { wxASSERT( !IsEmpty() ); return Item(Count() - 1); }
3c67202d
VZ
788
789 // item management
790 // Search the element in the array, starting from the beginning if
791 // bFromEnd is FALSE or from end otherwise. If bCase, comparison is case
792 // sensitive (default). Returns index of the first item matched or
793 // wxNOT_FOUND
c801d85f 794 int Index (const char *sz, bool bCase = TRUE, bool bFromEnd = FALSE) const;
3c67202d
VZ
795 // add new element at the end
796 void Add(const wxString& str);
797 // add new element at given position
c86f1403 798 void Insert(const wxString& str, size_t uiIndex);
3c67202d 799 // remove first item matching this value
c801d85f 800 void Remove(const char *sz);
3c67202d 801 // remove item by index
c801d85f 802 void Remove(size_t nIndex);
c801d85f 803
30b21f9a
VZ
804 // sorting
805 // sort array elements in alphabetical order (or reversed alphabetical
806 // order if reverseOrder parameter is TRUE)
807 void Sort(bool reverseOrder = FALSE);
808 // sort array elements using specified comparaison function
809 void Sort(CompareFunction compareFunction);
c801d85f
KB
810
811private:
812 void Grow(); // makes array bigger if needed
813 void Free(); // free the string stored
814
30b21f9a
VZ
815 void DoSort(); // common part of all Sort() variants
816
3c67202d 817 size_t m_nSize, // current size of the array
c801d85f
KB
818 m_nCount; // current number of elements
819
820 char **m_pItems; // pointer to data
821};
822
c801d85f 823// ---------------------------------------------------------------------------
3c67202d 824// wxString comparison functions: operator versions are always case sensitive
c801d85f 825// ---------------------------------------------------------------------------
3c67202d 826//
a3ef5bf5 827inline bool operator==(const wxString& s1, const wxString& s2) { return (s1.Cmp(s2) == 0); }
3c67202d 828//
a3ef5bf5 829inline bool operator==(const wxString& s1, const char * s2) { return (s1.Cmp(s2) == 0); }
3c67202d 830//
a3ef5bf5 831inline bool operator==(const char * s1, const wxString& s2) { return (s2.Cmp(s1) == 0); }
3c67202d 832//
a3ef5bf5 833inline bool operator!=(const wxString& s1, const wxString& s2) { return (s1.Cmp(s2) != 0); }
3c67202d 834//
a3ef5bf5 835inline bool operator!=(const wxString& s1, const char * s2) { return (s1.Cmp(s2) != 0); }
3c67202d 836//
a3ef5bf5 837inline bool operator!=(const char * s1, const wxString& s2) { return (s2.Cmp(s1) != 0); }
3c67202d 838//
a3ef5bf5 839inline bool operator< (const wxString& s1, const wxString& s2) { return (s1.Cmp(s2) < 0); }
3c67202d 840//
a3ef5bf5 841inline bool operator< (const wxString& s1, const char * s2) { return (s1.Cmp(s2) < 0); }
3c67202d 842//
a3ef5bf5 843inline bool operator< (const char * s1, const wxString& s2) { return (s2.Cmp(s1) > 0); }
3c67202d 844//
a3ef5bf5 845inline bool operator> (const wxString& s1, const wxString& s2) { return (s1.Cmp(s2) > 0); }
3c67202d 846//
a3ef5bf5 847inline bool operator> (const wxString& s1, const char * s2) { return (s1.Cmp(s2) > 0); }
3c67202d 848//
a3ef5bf5 849inline bool operator> (const char * s1, const wxString& s2) { return (s2.Cmp(s1) < 0); }
3c67202d 850//
a3ef5bf5 851inline bool operator<=(const wxString& s1, const wxString& s2) { return (s1.Cmp(s2) <= 0); }
3c67202d 852//
a3ef5bf5 853inline bool operator<=(const wxString& s1, const char * s2) { return (s1.Cmp(s2) <= 0); }
3c67202d 854//
a3ef5bf5 855inline bool operator<=(const char * s1, const wxString& s2) { return (s2.Cmp(s1) >= 0); }
3c67202d 856//
a3ef5bf5 857inline bool operator>=(const wxString& s1, const wxString& s2) { return (s1.Cmp(s2) >= 0); }
3c67202d 858//
a3ef5bf5 859inline bool operator>=(const wxString& s1, const char * s2) { return (s1.Cmp(s2) >= 0); }
3c67202d 860//
a3ef5bf5 861inline bool operator>=(const char * s1, const wxString& s2) { return (s2.Cmp(s1) <= 0); }
3c67202d 862
ed7174ba
UU
863wxString WXDLLEXPORT operator+(const wxString& string1, const wxString& string2);
864wxString WXDLLEXPORT operator+(const wxString& string, char ch);
865wxString WXDLLEXPORT operator+(char ch, const wxString& string);
866wxString WXDLLEXPORT operator+(const wxString& string, const char *psz);
867wxString WXDLLEXPORT operator+(const char *psz, const wxString& string);
f04f3991 868
c801d85f 869// ---------------------------------------------------------------------------
3c67202d 870// Implementation only from here until the end of file
c801d85f
KB
871// ---------------------------------------------------------------------------
872
3c67202d 873#ifdef wxSTD_STRING_COMPATIBILITY
c801d85f 874
3f4a0c5b 875#include "wx/ioswrap.h"
c801d85f 876
184b5d99 877WXDLLEXPORT istream& operator>>(istream& is, wxString& str);
c801d85f 878
3c67202d 879#endif // wxSTD_STRING_COMPATIBILITY
c801d85f 880
34138703 881#endif // _WX_WXSTRINGH__