1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxString and wxArrayString classes
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows license
10 ///////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_WXSTRINGH__
13 #define _WX_WXSTRINGH__
16 #pragma interface "string.h"
19 /* Dependencies (should be included before this header):
35 #include "wx/defs.h" // Robert Roebling
36 #ifdef WXSTRING_IS_WXOBJECT
37 #include "wx/object.h"
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.
49 // ---------------------------------------------------------------------------
51 // ---------------------------------------------------------------------------
53 // compile the std::string compatibility functions if defined
54 #define wxSTD_STRING_COMPATIBILITY
56 // define to derive wxString from wxObject
57 #ifdef WXSTRING_IS_WXOBJECT
58 #undef WXSTRING_IS_WXOBJECT
61 // maximum possible length for a string means "take all string" everywhere
62 // (as sizeof(StringData) is unknown here we substract 100)
63 #define STRING_MAXLEN (UINT_MAX - 100)
66 #define WXSTRINGCAST (char *)(const char *)
68 // implementation only
69 #define ASSERT_VALID_INDEX(i) wxASSERT( (unsigned)(i) < Len() )
71 // ---------------------------------------------------------------------------
72 // Global functions complementing standard C string library replacements for
73 // strlen() and portable strcasecmp()
74 //---------------------------------------------------------------------------
76 // checks whether the passed in pointer is NULL and if the string is empty
77 inline bool WXDLLEXPORT
IsEmpty(const char *p
) { return !p
|| !*p
; }
79 // safe version of strlen() (returns 0 if passed NULL pointer)
80 inline size_t WXDLLEXPORT
Strlen(const char *psz
)
81 { return psz
? strlen(psz
) : 0; }
83 // portable strcasecmp/_stricmp
84 inline int WXDLLEXPORT
Stricmp(const char *psz1
, const char *psz2
)
87 return _stricmp(psz1
, psz2
);
89 return _stricmp(psz1
, psz2
);
90 #elif defined(__SALFORDC__)
91 return stricmp(psz1
, psz2
);
92 #elif defined(__BORLANDC__)
93 return stricmp(psz1
, psz2
);
94 #elif defined(__WATCOMC__)
95 return stricmp(psz1
, psz2
);
96 #elif defined(__UNIX__) || defined(__GNUWIN32__)
97 return strcasecmp(psz1
, psz2
);
98 #elif defined(__MWERKS__) && !defined(_MSC_VER)
101 c1
= tolower(*psz1
++);
102 c2
= tolower(*psz2
++);
103 } while ( c1
&& (c1
== c2
) );
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):
111 register char c1, c2;
113 c1 = tolower(*psz1++);
114 c2 = tolower(*psz2++);
115 } while ( c1 && (c1 == c2) );
120 #error "Please define string case-insensitive compare for your OS/compiler"
121 #endif // OS/compiler
124 // ----------------------------------------------------------------------------
126 // ----------------------------------------------------------------------------
128 WXDLLEXPORT_DATA(extern const char*) wxEmptyString
;
130 // global pointer to empty string
131 WXDLLEXPORT_DATA(extern const char*) g_szNul
;
133 // return an empty wxString
134 class WXDLLEXPORT wxString
; // not yet defined
135 inline const wxString
& wxGetEmptyString() { return *(wxString
*)&g_szNul
; }
137 // ---------------------------------------------------------------------------
138 // string data prepended with some housekeeping info (used by wxString class),
139 // is never used directly (but had to be put here to allow inlining)
140 // ---------------------------------------------------------------------------
141 struct WXDLLEXPORT wxStringData
143 int nRefs
; // reference count
144 size_t nDataLength
, // actual string length
145 nAllocLength
; // allocated memory size
147 // mimics declaration 'char data[nAllocLength]'
148 char* data() const { return (char*)(this + 1); }
150 // empty string has a special ref count so it's never deleted
151 bool IsEmpty() const { return nRefs
== -1; }
152 bool IsShared() const { return nRefs
> 1; }
155 void Lock() { if ( !IsEmpty() ) nRefs
++; }
156 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) free(this); }
158 // if we had taken control over string memory (GetWriteBuf), it's
159 // intentionally put in invalid state
160 void Validate(bool b
) { nRefs
= b
? 1 : 0; }
161 bool IsValid() const { return nRefs
!= 0; }
164 // ---------------------------------------------------------------------------
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.
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.
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
179 // It was compiled and tested under Win32, Linux (libc 5 & 6), Solaris 5.5.
182 // - ressource support (string tables in ressources)
183 // - more wide character (UNICODE) support
184 // - regular expressions support
185 // ---------------------------------------------------------------------------
187 #ifdef WXSTRING_IS_WXOBJECT
188 class WXDLLEXPORT wxString
: public wxObject
190 DECLARE_DYNAMIC_CLASS(wxString
)
191 #else //WXSTRING_IS_WXOBJECT
192 class WXDLLEXPORT wxString
194 #endif //WXSTRING_IS_WXOBJECT
196 friend class WXDLLEXPORT wxArrayString
;
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!
202 // points to data preceded by wxStringData structure with ref count info
205 // accessor to string data
206 wxStringData
* GetStringData() const { return (wxStringData
*)m_pchData
- 1; }
208 // string (re)initialization functions
209 // initializes the string to the empty value (must be called only from
210 // ctors, use Reinit() otherwise)
211 void Init() { m_pchData
= (char *)g_szNul
; }
212 // initializaes the string with (a part of) C-string
213 void InitWith(const char *psz
, size_t nPos
= 0, size_t nLen
= STRING_MAXLEN
);
214 // as Init, but also frees old data
215 void Reinit() { GetStringData()->Unlock(); Init(); }
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 *);
225 // append a (sub)string
226 void ConcatSelf(int nLen
, const char *src
);
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);
234 // constructors and destructor
235 // ctor for an empty string
236 wxString() { Init(); }
238 wxString(const wxString
& stringSrc
)
240 wxASSERT( stringSrc
.GetStringData()->IsValid() );
242 if ( stringSrc
.IsEmpty() ) {
243 // nothing to do for an empty string
247 m_pchData
= stringSrc
.m_pchData
; // share same data
248 GetStringData()->Lock(); // => one more copy
251 // string containing nRepeat copies of ch
252 wxString(char ch
, size_t nRepeat
= 1);
253 // ctor takes first nLength characters from C string
254 // (default value of STRING_MAXLEN means take all the string)
255 wxString(const char *psz
, size_t nLength
= STRING_MAXLEN
)
256 { InitWith(psz
, 0, nLength
); }
257 // from C string (for compilers using unsigned char)
258 wxString(const unsigned char* psz
, size_t nLength
= STRING_MAXLEN
);
259 // from wide (UNICODE) string
260 wxString(const wchar_t *pwz
);
261 // dtor is not virtual, this class must not be inherited from!
262 ~wxString() { GetStringData()->Unlock(); }
264 // generic attributes & operations
265 // as standard strlen()
266 size_t Len() const { return GetStringData()->nDataLength
; }
267 // string contains any characters?
268 bool IsEmpty() const { return Len() == 0; }
269 // empty string is "FALSE", so !str will return TRUE
270 bool operator!() const { return IsEmpty(); }
271 // empty string contents
278 wxASSERT( GetStringData()->nDataLength
== 0 );
280 // empty the string and free memory
283 if ( !GetStringData()->IsEmpty() )
286 wxASSERT( GetStringData()->nDataLength
== 0 ); // should be empty
287 wxASSERT( GetStringData()->nAllocLength
== 0 ); // and not own any memory
292 bool IsAscii() const;
294 bool IsNumber() const;
298 // data access (all indexes are 0 based)
300 char GetChar(size_t n
) const
301 { ASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
303 char& GetWritableChar(size_t n
)
304 { ASSERT_VALID_INDEX( n
); CopyBeforeWrite(); return m_pchData
[n
]; }
306 void SetChar(size_t n
, char ch
)
307 { ASSERT_VALID_INDEX( n
); CopyBeforeWrite(); m_pchData
[n
] = ch
; }
309 // get last character
311 { wxASSERT( !IsEmpty() ); return m_pchData
[Len() - 1]; }
312 // get writable last character
314 { wxASSERT( !IsEmpty() ); CopyBeforeWrite(); return m_pchData
[Len()-1]; }
316 // on alpha-linux this gives overload problems:
317 // Also on Solaris, so removing for now (JACS)
318 #if ! defined(__ALPHA__)
319 // operator version of GetChar
320 char operator[](size_t n
) const
321 { ASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
324 // operator version of GetChar
325 char operator[](int n
) const
326 { ASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
327 // operator version of GetWritableChar
328 char& operator[](size_t n
)
329 { ASSERT_VALID_INDEX( n
); CopyBeforeWrite(); return m_pchData
[n
]; }
331 // implicit conversion to C string
332 operator const char*() const { return m_pchData
; }
333 // explicit conversion to C string (use this with printf()!)
334 const char* c_str() const { return m_pchData
; }
336 const char* GetData() const { return m_pchData
; }
338 // overloaded assignment
339 // from another wxString
340 wxString
& operator=(const wxString
& stringSrc
);
342 wxString
& operator=(char ch
);
344 wxString
& operator=(const char *psz
);
345 // from another kind of C string
346 wxString
& operator=(const unsigned char* psz
);
347 // from a wide string
348 wxString
& operator=(const wchar_t *pwz
);
350 // string concatenation
351 // in place concatenation
353 Concatenate and return the result. Note that the left to right
354 associativity of << allows to write things like "str << str1 << str2
355 << ..." (unlike with +=)
358 wxString
& operator<<(const wxString
& s
)
360 wxASSERT( s
.GetStringData()->IsValid() );
362 ConcatSelf(s
.Len(), s
);
365 // string += C string
366 wxString
& operator<<(const char *psz
)
367 { ConcatSelf(Strlen(psz
), psz
); return *this; }
369 wxString
& operator<<(char ch
) { ConcatSelf(1, &ch
); return *this; }
372 void operator+=(const wxString
& s
) { (void)operator<<(s
); }
373 // string += C string
374 void operator+=(const char *psz
) { (void)operator<<(psz
); }
376 void operator+=(char ch
) { (void)operator<<(ch
); }
378 // string += C string
379 wxString
& Append(const char* psz
)
380 { ConcatSelf(Strlen(psz
), psz
); return *this; }
381 // append count copies of given character
382 wxString
& Append(char ch
, size_t count
= 1u)
383 { wxString
str(ch
, count
); return *this << str
; }
385 // prepend a string, return the string itself
386 wxString
& Prepend(const wxString
& str
)
387 { *this = str
+ *this; return *this; }
389 // non-destructive concatenation
391 friend wxString WXDLLEXPORT
operator+(const wxString
& string1
, const wxString
& string2
);
393 friend wxString WXDLLEXPORT
operator+(const wxString
& string
, char ch
);
395 friend wxString WXDLLEXPORT
operator+(char ch
, const wxString
& string
);
397 friend wxString WXDLLEXPORT
operator+(const wxString
& string
, const char *psz
);
399 friend wxString WXDLLEXPORT
operator+(const char *psz
, const wxString
& string
);
401 // stream-like functions
402 // insert an int into string
403 wxString
& operator<<(int i
);
404 // insert a float into string
405 wxString
& operator<<(float f
);
406 // insert a double into string
407 wxString
& operator<<(double d
);
410 // case-sensitive comparison: return 0 if =, +1 if > or -1 if <
411 int Cmp(const char *psz
) const { return strcmp(c_str(), psz
); }
412 // same as Cmp() but not case-sensitive
413 int CmpNoCase(const char *psz
) const { return Stricmp(c_str(), psz
); }
414 // test for the string equality, either considering case or not
415 // (if compareWithCase then the case matters)
416 bool IsSameAs(const char *psz
, bool compareWithCase
= TRUE
) const
417 { return (compareWithCase
? Cmp(psz
) : CmpNoCase(psz
)) == 0; }
419 // simple sub-string extraction
420 // return substring starting at nFirst of length nCount (or till the end
421 // if nCount = default value)
422 wxString
Mid(size_t nFirst
, size_t nCount
= STRING_MAXLEN
) const;
424 // operator version of Mid()
425 wxString
operator()(size_t start
, size_t len
) const
426 { return Mid(start
, len
); }
428 // get first nCount characters
429 wxString
Left(size_t nCount
) const;
430 // get last nCount characters
431 wxString
Right(size_t nCount
) const;
432 // get all characters before the first occurence of ch
433 // (returns the whole string if ch not found)
434 wxString
BeforeFirst(char ch
) const;
435 // get all characters before the last occurence of ch
436 // (returns empty string if ch not found)
437 wxString
BeforeLast(char ch
) const;
438 // get all characters after the first occurence of ch
439 // (returns empty string if ch not found)
440 wxString
AfterFirst(char ch
) const;
441 // get all characters after the last occurence of ch
442 // (returns the whole string if ch not found)
443 wxString
AfterLast(char ch
) const;
445 // for compatibility only, use more explicitly named functions above
446 wxString
Before(char ch
) const { return BeforeLast(ch
); }
447 wxString
After(char ch
) const { return AfterFirst(ch
); }
450 // convert to upper case in place, return the string itself
451 wxString
& MakeUpper();
452 // convert to upper case, return the copy of the string
453 // Here's something to remember: BC++ doesn't like returns in inlines.
454 wxString
Upper() const ;
455 // convert to lower case in place, return the string itself
456 wxString
& MakeLower();
457 // convert to lower case, return the copy of the string
458 wxString
Lower() const ;
460 // trimming/padding whitespace (either side) and truncating
461 // remove spaces from left or from right (default) side
462 wxString
& Trim(bool bFromRight
= TRUE
);
463 // add nCount copies chPad in the beginning or at the end (default)
464 wxString
& Pad(size_t nCount
, char chPad
= ' ', bool bFromRight
= TRUE
);
465 // truncate string to given length
466 wxString
& Truncate(size_t uiLen
);
468 // searching and replacing
469 // searching (return starting index, or -1 if not found)
470 int Find(char ch
, bool bFromEnd
= FALSE
) const; // like strchr/strrchr
471 // searching (return starting index, or -1 if not found)
472 int Find(const char *pszSub
) const; // like strstr
473 // replace first (or all of bReplaceAll) occurences of substring with
474 // another string, returns the number of replacements made
475 size_t Replace(const char *szOld
,
477 bool bReplaceAll
= TRUE
);
479 // check if the string contents matches a mask containing '*' and '?'
480 bool Matches(const char *szMask
) const;
482 // formated input/output
483 // as sprintf(), returns the number of characters written or < 0 on error
484 int Printf(const char *pszFormat
, ...);
485 // as vprintf(), returns the number of characters written or < 0 on error
486 int PrintfV(const char* pszFormat
, va_list argptr
);
488 // raw access to string memory
489 // ensure that string has space for at least nLen characters
490 // only works if the data of this string is not shared
491 void Alloc(size_t nLen
);
492 // minimize the string's memory
493 // only works if the data of this string is not shared
495 // get writable buffer of at least nLen bytes. Unget() *must* be called
496 // a.s.a.p. to put string back in a reasonable state!
497 char *GetWriteBuf(size_t nLen
);
498 // call this immediately after GetWriteBuf() has been used
499 void UngetWriteBuf();
501 // wxWindows version 1 compatibility functions
504 wxString
SubString(size_t from
, size_t to
) const
505 { return Mid(from
, (to
- from
+ 1)); }
506 // values for second parameter of CompareTo function
507 enum caseCompare
{exact
, ignoreCase
};
508 // values for first parameter of Strip function
509 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
512 int sprintf(const char *pszFormat
, ...);
515 inline int CompareTo(const char* psz
, caseCompare cmp
= exact
) const
516 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
519 size_t Length() const { return Len(); }
520 // Count the number of characters
521 int Freq(char ch
) const;
523 void LowerCase() { MakeLower(); }
525 void UpperCase() { MakeUpper(); }
526 // use Trim except that it doesn't change this string
527 wxString
Strip(stripType w
= trailing
) const;
529 // use Find (more general variants not yet supported)
530 size_t Index(const char* psz
) const { return Find(psz
); }
531 size_t Index(char ch
) const { return Find(ch
); }
533 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
534 wxString
& RemoveLast() { return Truncate(Len() - 1); }
536 wxString
& Remove(size_t nStart
, size_t nLen
) { return erase( nStart
, nLen
); }
539 int First( const char ch
) const { return Find(ch
); }
540 int First( const char* psz
) const { return Find(psz
); }
541 int First( const wxString
&str
) const { return Find(str
); }
542 int Last( const char ch
) const { return Find(ch
, TRUE
); }
543 bool Contains(const wxString
& str
) const { return Find(str
) != -1; }
546 bool IsNull() const { return IsEmpty(); }
548 #ifdef wxSTD_STRING_COMPATIBILITY
549 // std::string compatibility functions
551 // an 'invalid' value for string index
552 static const size_t npos
;
555 // take nLen chars starting at nPos
556 wxString(const wxString
& str
, size_t nPos
, size_t nLen
)
558 wxASSERT( str
.GetStringData()->IsValid() );
559 InitWith(str
.c_str(), nPos
, nLen
== npos
? 0 : nLen
);
561 // take all characters from pStart to pEnd
562 wxString(const void *pStart
, const void *pEnd
);
564 // lib.string.capacity
565 // return the length of the string
566 size_t size() const { return Len(); }
567 // return the length of the string
568 size_t length() const { return Len(); }
569 // return the maximum size of the string
570 size_t max_size() const { return STRING_MAXLEN
; }
571 // resize the string, filling the space with c if c != 0
572 void resize(size_t nSize
, char ch
= '\0');
573 // delete the contents of the string
574 void clear() { Empty(); }
575 // returns true if the string is empty
576 bool empty() const { return IsEmpty(); }
579 // return the character at position n
580 char at(size_t n
) const { return GetChar(n
); }
581 // returns the writable character at position n
582 char& at(size_t n
) { return GetWritableChar(n
); }
584 // lib.string.modifiers
586 wxString
& append(const wxString
& str
)
587 { *this += str
; return *this; }
588 // append elements str[pos], ..., str[pos+n]
589 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
590 { ConcatSelf(n
, str
.c_str() + pos
); return *this; }
591 // append first n (or all if n == npos) characters of sz
592 wxString
& append(const char *sz
, size_t n
= npos
)
593 { ConcatSelf(n
== npos
? Strlen(sz
) : n
, sz
); return *this; }
595 // append n copies of ch
596 wxString
& append(size_t n
, char ch
) { return Pad(n
, ch
); }
598 // same as `this_string = str'
599 wxString
& assign(const wxString
& str
) { return (*this) = str
; }
600 // same as ` = str[pos..pos + n]
601 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
602 { return *this = wxString((const char *)str
+ pos
, n
); }
603 // same as `= first n (or all if n == npos) characters of sz'
604 wxString
& assign(const char *sz
, size_t n
= npos
)
605 { return *this = wxString(sz
, n
); }
606 // same as `= n copies of ch'
607 wxString
& assign(size_t n
, char ch
)
608 { return *this = wxString(ch
, n
); }
610 // insert another string
611 wxString
& insert(size_t nPos
, const wxString
& str
);
612 // insert n chars of str starting at nStart (in str)
613 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
614 { return insert(nPos
, wxString((const char *)str
+ nStart
, n
)); }
616 // insert first n (or all if n == npos) characters of sz
617 wxString
& insert(size_t nPos
, const char *sz
, size_t n
= npos
)
618 { return insert(nPos
, wxString(sz
, n
)); }
619 // insert n copies of ch
620 wxString
& insert(size_t nPos
, size_t n
, char ch
)
621 { return insert(nPos
, wxString(ch
, n
)); }
623 // delete characters from nStart to nStart + nLen
624 wxString
& erase(size_t nStart
= 0, size_t nLen
= npos
);
626 // replaces the substring of length nLen starting at nStart
627 wxString
& replace(size_t nStart
, size_t nLen
, const char* sz
);
628 // replaces the substring with nCount copies of ch
629 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, char ch
);
630 // replaces a substring with another substring
631 wxString
& replace(size_t nStart
, size_t nLen
,
632 const wxString
& str
, size_t nStart2
, size_t nLen2
);
633 // replaces the substring with first nCount chars of sz
634 wxString
& replace(size_t nStart
, size_t nLen
,
635 const char* sz
, size_t nCount
);
638 void swap(wxString
& str
);
640 // All find() functions take the nStart argument which specifies the
641 // position to start the search on, the default value is 0. All functions
642 // return npos if there were no match.
645 size_t find(const wxString
& str
, size_t nStart
= 0) const;
647 // VC++ 1.5 can't cope with this syntax.
648 #if !(defined(_MSC_VER) && !defined(__WIN32__))
649 // find first n characters of sz
650 size_t find(const char* sz
, size_t nStart
= 0, size_t n
= npos
) const;
653 // Gives a duplicate symbol (presumably a case-insensitivity problem)
654 #if !defined(__BORLANDC__)
655 // find the first occurence of character ch after nStart
656 size_t find(char ch
, size_t nStart
= 0) const;
658 // rfind() family is exactly like find() but works right to left
660 // as find, but from the end
661 size_t rfind(const wxString
& str
, size_t nStart
= npos
) const;
663 // VC++ 1.5 can't cope with this syntax.
664 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
665 // as find, but from the end
666 size_t rfind(const char* sz
, size_t nStart
= npos
,
667 size_t n
= npos
) const;
668 // as find, but from the end
669 size_t rfind(char ch
, size_t nStart
= npos
) const;
672 // find first/last occurence of any character in the set
675 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const;
677 size_t find_first_of(const char* sz
, size_t nStart
= 0) const;
678 // same as find(char, size_t)
679 size_t find_first_of(char c
, size_t nStart
= 0) const;
681 size_t find_last_of (const wxString
& str
, size_t nStart
= npos
) const;
683 size_t find_last_of (const char* s
, size_t nStart
= npos
) const;
684 // same as rfind(char, size_t)
685 size_t find_last_of (char c
, size_t nStart
= npos
) const;
687 // find first/last occurence of any character not in the set
690 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const;
692 size_t find_first_not_of(const char* s
, size_t nStart
= 0) const;
694 size_t find_first_not_of(char ch
, size_t nStart
= 0) const;
696 size_t find_last_not_of(const wxString
& str
, size_t nStart
=npos
) const;
698 size_t find_last_not_of(const char* s
, size_t nStart
= npos
) const;
700 size_t find_last_not_of(char ch
, size_t nStart
= npos
) const;
702 // All compare functions return -1, 0 or 1 if the [sub]string is less,
703 // equal or greater than the compare() argument.
705 // just like strcmp()
706 int compare(const wxString
& str
) const { return Cmp(str
); }
707 // comparison with a substring
708 int compare(size_t nStart
, size_t nLen
, const wxString
& str
) const;
709 // comparison of 2 substrings
710 int compare(size_t nStart
, size_t nLen
,
711 const wxString
& str
, size_t nStart2
, size_t nLen2
) const;
712 // just like strcmp()
713 int compare(const char* sz
) const { return Cmp(sz
); }
714 // substring comparison with first nCount characters of sz
715 int compare(size_t nStart
, size_t nLen
,
716 const char* sz
, size_t nCount
= npos
) const;
718 // substring extraction
719 wxString
substr(size_t nStart
= 0, size_t nLen
= npos
) const;
720 #endif // wxSTD_STRING_COMPATIBILITY
723 // ----------------------------------------------------------------------------
724 // The string array uses it's knowledge of internal structure of the wxString
725 // class to optimize string storage. Normally, we would store pointers to
726 // string, but as wxString is, in fact, itself a pointer (sizeof(wxString) is
727 // sizeof(char *)) we store these pointers instead. The cast to "wxString *" is
728 // really all we need to turn such pointer into a string!
730 // Of course, it can be called a dirty hack, but we use twice less memory and
731 // this approach is also more speed efficient, so it's probably worth it.
733 // Usage notes: when a string is added/inserted, a new copy of it is created,
734 // so the original string may be safely deleted. When a string is retrieved
735 // from the array (operator[] or Item() method), a reference is returned.
736 // ----------------------------------------------------------------------------
737 class WXDLLEXPORT wxArrayString
740 // constructors and destructor
744 wxArrayString(const wxArrayString
& array
);
745 // assignment operator
746 wxArrayString
& operator=(const wxArrayString
& src
);
747 // not virtual, this class should not be derived from
751 // empties the list, but doesn't release memory
753 // empties the list and releases memory
755 // preallocates memory for given number of items
756 void Alloc(size_t nCount
);
757 // minimzes the memory usage (by freeing all extra memory)
761 // number of elements in the array
762 size_t GetCount() const { return m_nCount
; }
764 bool IsEmpty() const { return m_nCount
== 0; }
765 // number of elements in the array (GetCount is preferred API)
766 size_t Count() const { return m_nCount
; }
768 // items access (range checking is done in debug version)
769 // get item at position uiIndex
770 wxString
& Item(size_t nIndex
) const
771 { wxASSERT( nIndex
< m_nCount
); return *(wxString
*)&(m_pItems
[nIndex
]); }
773 wxString
& operator[](size_t nIndex
) const { return Item(nIndex
); }
775 wxString
& Last() const { wxASSERT( !IsEmpty() ); return Item(Count() - 1); }
778 // Search the element in the array, starting from the beginning if
779 // bFromEnd is FALSE or from end otherwise. If bCase, comparison is case
780 // sensitive (default). Returns index of the first item matched or
782 int Index (const char *sz
, bool bCase
= TRUE
, bool bFromEnd
= FALSE
) const;
783 // add new element at the end
784 void Add(const wxString
& str
);
785 // add new element at given position
786 void Insert(const wxString
& str
, size_t uiIndex
);
787 // remove first item matching this value
788 void Remove(const char *sz
);
789 // remove item by index
790 void Remove(size_t nIndex
);
792 // sort array elements
793 void Sort(bool bCase
= TRUE
, bool bReverse
= FALSE
);
796 void Grow(); // makes array bigger if needed
797 void Free(); // free the string stored
799 size_t m_nSize
, // current size of the array
800 m_nCount
; // current number of elements
802 char **m_pItems
; // pointer to data
805 // ---------------------------------------------------------------------------
806 // wxString comparison functions: operator versions are always case sensitive
807 // ---------------------------------------------------------------------------
809 inline bool operator==(const wxString
& s1
, const wxString
& s2
) { return (s1
.Cmp(s2
) == 0); }
811 inline bool operator==(const wxString
& s1
, const char * s2
) { return (s1
.Cmp(s2
) == 0); }
813 inline bool operator==(const char * s1
, const wxString
& s2
) { return (s2
.Cmp(s1
) == 0); }
815 inline bool operator!=(const wxString
& s1
, const wxString
& s2
) { return (s1
.Cmp(s2
) != 0); }
817 inline bool operator!=(const wxString
& s1
, const char * s2
) { return (s1
.Cmp(s2
) != 0); }
819 inline bool operator!=(const char * s1
, const wxString
& s2
) { return (s2
.Cmp(s1
) != 0); }
821 inline bool operator< (const wxString
& s1
, const wxString
& s2
) { return (s1
.Cmp(s2
) < 0); }
823 inline bool operator< (const wxString
& s1
, const char * s2
) { return (s1
.Cmp(s2
) < 0); }
825 inline bool operator< (const char * s1
, const wxString
& s2
) { return (s2
.Cmp(s1
) > 0); }
827 inline bool operator> (const wxString
& s1
, const wxString
& s2
) { return (s1
.Cmp(s2
) > 0); }
829 inline bool operator> (const wxString
& s1
, const char * s2
) { return (s1
.Cmp(s2
) > 0); }
831 inline bool operator> (const char * s1
, const wxString
& s2
) { return (s2
.Cmp(s1
) < 0); }
833 inline bool operator<=(const wxString
& s1
, const wxString
& s2
) { return (s1
.Cmp(s2
) <= 0); }
835 inline bool operator<=(const wxString
& s1
, const char * s2
) { return (s1
.Cmp(s2
) <= 0); }
837 inline bool operator<=(const char * s1
, const wxString
& s2
) { return (s2
.Cmp(s1
) >= 0); }
839 inline bool operator>=(const wxString
& s1
, const wxString
& s2
) { return (s1
.Cmp(s2
) >= 0); }
841 inline bool operator>=(const wxString
& s1
, const char * s2
) { return (s1
.Cmp(s2
) >= 0); }
843 inline bool operator>=(const char * s1
, const wxString
& s2
) { return (s2
.Cmp(s1
) <= 0); }
845 wxString WXDLLEXPORT
operator+(const wxString
& string1
, const wxString
& string2
);
846 wxString WXDLLEXPORT
operator+(const wxString
& string
, char ch
);
847 wxString WXDLLEXPORT
operator+(char ch
, const wxString
& string
);
848 wxString WXDLLEXPORT
operator+(const wxString
& string
, const char *psz
);
849 wxString WXDLLEXPORT
operator+(const char *psz
, const wxString
& string
);
851 // ---------------------------------------------------------------------------
852 // Implementation only from here until the end of file
853 // ---------------------------------------------------------------------------
855 #ifdef wxSTD_STRING_COMPATIBILITY
857 // forward declare iostream
858 // Known not to work with wxUSE_IOSTREAMH set to 0, so
859 // replacing with includes (on advice of ungod@pasdex.com.au)
860 // class WXDLLEXPORT istream;
862 // N.B. BC++ doesn't have istream.h, ostream.h
863 #include <iostream.h>
871 WXDLLEXPORT istream
& operator>>(istream
& is
, wxString
& str
);
873 #endif // wxSTD_STRING_COMPATIBILITY
875 #endif // _WX_WXSTRINGH__