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