1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxString class
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):
32 #include "wx/defs.h" // Robert Roebling
33 #ifdef WXSTRING_IS_WXOBJECT
34 #include "wx/object.h"
40 /** @name wxString library
41 @memo Efficient wxString class [more or less] compatible with MFC CString,
42 wxWindows wxString and std::string and some handy functions
43 missing from string.h.
47 // ---------------------------------------------------------------------------
49 // ---------------------------------------------------------------------------
52 @memo You can switch off wxString/std::string compatibility if desired
54 /// compile the std::string compatibility functions
55 #define STD_STRING_COMPATIBILITY
57 /// define to derive wxString from wxObject
58 #undef WXSTRING_IS_WXOBJECT
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)
65 #define WXSTRINGCAST (char *)(const char *)
67 // NB: works only inside wxString class
68 #define ASSERT_VALID_INDEX(i) wxASSERT( (unsigned)(i) < Len() )
70 // ---------------------------------------------------------------------------
71 /** @name Global functions complementing standard C string library
72 @memo replacements for 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
);
88 #elif defined(__BORLANDC__)
89 return stricmp(psz1
, psz2
);
90 #elif defined(__UNIX__) || defined(__GNUWIN32__)
91 return strcasecmp(psz1
, psz2
);
93 // almost all compilers/libraries provide this function (unfortunately under
94 // different names), that's why we don't implement our own which will surely
95 // be more efficient than this code (uncomment to use):
99 c1 = tolower(*psz1++);
100 c2 = tolower(*psz2++);
101 } while ( c1 && (c1 == c2) );
106 #error "Please define string case-insensitive compare for your OS/compiler"
107 #endif // OS/compiler
109 wxString WXDLLEXPORT
operator+(const wxString
& string1
, const wxString
& string2
);
110 wxString WXDLLEXPORT
operator+(const wxString
& string
, char ch
);
111 wxString WXDLLEXPORT
operator+(char ch
, const wxString
& string
);
112 wxString WXDLLEXPORT
operator+(const wxString
& string
, const char *psz
);
113 wxString WXDLLEXPORT
operator+(const char *psz
, const wxString
& string
);
115 // ----------------------------------------------------------------------------
117 // ----------------------------------------------------------------------------
119 // global pointer to empty string
120 WXDLLEXPORT_DATA(extern const char*) g_szNul
;
122 // return an empty wxString
123 class WXDLLEXPORT wxString
; // not yet defined
124 inline const wxString
& wxGetEmptyString() { return *(wxString
*)&g_szNul
; }
126 // ---------------------------------------------------------------------------
127 // string data prepended with some housekeeping info (used by wxString class),
128 // is never used directly (but had to be put here to allow inlining)
129 // ---------------------------------------------------------------------------
130 struct WXDLLEXPORT wxStringData
132 int nRefs
; // reference count
133 size_t nDataLength
, // actual string length
134 nAllocLength
; // allocated memory size
136 // mimics declaration 'char data[nAllocLength]'
137 char* data() const { return (char*)(this + 1); }
139 // empty string has a special ref count so it's never deleted
140 bool IsEmpty() const { return nRefs
== -1; }
141 bool IsShared() const { return nRefs
> 1; }
144 void Lock() { if ( !IsEmpty() ) nRefs
++; }
145 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) free(this); }
147 // if we had taken control over string memory (GetWriteBuf), it's
148 // intentionally put in invalid state
149 void Validate(bool b
) { nRefs
= b
? 1 : 0; }
150 bool IsValid() const { return nRefs
!= 0; }
153 // ---------------------------------------------------------------------------
155 This is (yet another one) String class for C++ programmers. It doesn't use
156 any of "advanced" C++ features (i.e. templates, exceptions, namespaces...)
157 thus you should be able to compile it with practicaly any C++ compiler.
158 This class uses copy-on-write technique, i.e. identical strings share the
159 same memory as long as neither of them is changed.
161 This class aims to be as compatible as possible with the new standard
162 std::string class, but adds some additional functions and should be
163 at least as efficient than the standard implementation.
165 Performance note: it's more efficient to write functions which take
166 "const String&" arguments than "const char *" if you assign the argument
169 It was compiled and tested under Win32, Linux (libc 5 & 6), Solaris 5.5.
172 - ressource support (string tables in ressources)
173 - more wide character (UNICODE) support
174 - regular expressions support
176 @memo A non-template portable wxString class implementing copy-on-write.
180 // ---------------------------------------------------------------------------
181 #ifdef WXSTRING_IS_WXOBJECT
182 class WXDLLEXPORT wxString
: public wxObject
184 DECLARE_DYNAMIC_CLASS(wxString
)
185 #else //WXSTRING_IS_WXOBJECT
186 class WXDLLEXPORT wxString
188 #endif //WXSTRING_IS_WXOBJECT
190 friend class WXDLLEXPORT wxArrayString
;
192 // NB: special care was taken in arrangin the member functions in such order
193 // that all inline functions can be effectively inlined
195 // points to data preceded by wxStringData structure with ref count info
198 // accessor to string data
199 wxStringData
* GetStringData() const { return (wxStringData
*)m_pchData
- 1; }
201 // string (re)initialization functions
202 // initializes the string to the empty value (must be called only from
203 // ctors, use Reinit() otherwise)
204 void Init() { m_pchData
= (char *)g_szNul
; }
205 // initializaes the string with (a part of) C-string
206 void InitWith(const char *psz
, size_t nPos
= 0, size_t nLen
= STRING_MAXLEN
);
207 // as Init, but also frees old data
208 void Reinit() { GetStringData()->Unlock(); Init(); }
211 // allocates memory for string of lenght nLen
212 void AllocBuffer(size_t nLen
);
213 // copies data to another string
214 void AllocCopy(wxString
&, int, int) const;
215 // effectively copies data to string
216 void AssignCopy(size_t, const char *);
218 // append a (sub)string
219 void ConcatSelf(int nLen
, const char *src
);
221 // functions called before writing to the string: they copy it if there
222 // are other references to our data (should be the only owner when writing)
223 void CopyBeforeWrite();
224 void AllocBeforeWrite(size_t);
227 /** @name constructors & dtor */
229 /// ctor for an empty string
230 wxString() { Init(); }
232 wxString(const wxString
& stringSrc
)
234 wxASSERT( stringSrc
.GetStringData()->IsValid() );
236 if ( stringSrc
.IsEmpty() ) {
237 // nothing to do for an empty string
241 m_pchData
= stringSrc
.m_pchData
; // share same data
242 GetStringData()->Lock(); // => one more copy
245 /// string containing nRepeat copies of ch
246 wxString(char ch
, size_t nRepeat
= 1);
247 /// ctor takes first nLength characters from C string
248 // (default value of STRING_MAXLEN means take all the string)
249 wxString(const char *psz
, size_t nLength
= STRING_MAXLEN
)
250 { InitWith(psz
, 0, nLength
); }
251 /// from C string (for compilers using unsigned char)
252 wxString(const unsigned char* psz
, size_t nLength
= STRING_MAXLEN
);
253 /// from wide (UNICODE) string
254 wxString(const wchar_t *pwz
);
255 /// dtor is not virtual, this class must not be inherited from!
256 ~wxString() { GetStringData()->Unlock(); }
259 /** @name generic attributes & operations */
261 /// as standard strlen()
262 size_t Len() const { return GetStringData()->nDataLength
; }
263 /// string contains any characters?
264 bool IsEmpty() const { return Len() == 0; }
265 /// reinitialize string (and free memory)
271 wxASSERT( GetStringData()->nDataLength
== 0 );
272 wxASSERT( GetStringData()->nAllocLength
== 0 );
275 /// Is an ascii value
276 bool IsAscii() const;
278 bool IsNumber() const;
283 /** @name data access (all indexes are 0 based) */
286 char GetChar(size_t n
) const
287 { ASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
288 /// read/write access
289 char& GetWritableChar(size_t n
)
290 { ASSERT_VALID_INDEX( n
); CopyBeforeWrite(); return m_pchData
[n
]; }
292 void SetChar(size_t n
, char ch
)
293 { ASSERT_VALID_INDEX( n
); CopyBeforeWrite(); m_pchData
[n
] = ch
; }
295 /// get last character
297 { wxASSERT( !IsEmpty() ); return m_pchData
[Len() - 1]; }
298 /// get writable last character
300 { wxASSERT( !IsEmpty() ); CopyBeforeWrite(); return m_pchData
[Len()-1]; }
302 /// operator version of GetChar
303 char operator[](size_t n
) const
304 { ASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
305 /// operator version of GetChar
306 char operator[](int n
) const
307 { ASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
308 /// operator version of GetWritableChar
309 char& operator[](size_t n
)
310 { ASSERT_VALID_INDEX( n
); CopyBeforeWrite(); return m_pchData
[n
]; }
312 /// implicit conversion to C string
313 operator const char*() const { return m_pchData
; }
314 /// explicit conversion to C string (use this with printf()!)
315 const char* c_str() const { return m_pchData
; }
317 const char* GetData() const { return m_pchData
; }
320 /** @name overloaded assignment */
323 wxString
& operator=(const wxString
& stringSrc
);
325 wxString
& operator=(char ch
);
327 wxString
& operator=(const char *psz
);
329 wxString
& operator=(const unsigned char* psz
);
331 wxString
& operator=(const wchar_t *pwz
);
334 /** @name string concatenation */
336 /** @name in place concatenation */
337 /** @name concatenate and return the result
338 left to right associativity of << allows to write
339 things like "str << str1 << str2 << ..." */
342 wxString
& operator<<(const wxString
& s
)
344 wxASSERT( s
.GetStringData()->IsValid() );
346 ConcatSelf(s
.Len(), s
);
350 wxString
& operator<<(const char *psz
)
351 { ConcatSelf(Strlen(psz
), psz
); return *this; }
353 wxString
& operator<<(char ch
) { ConcatSelf(1, &ch
); return *this; }
358 void operator+=(const wxString
& s
) { (void)operator<<(s
); }
359 /// string += C string
360 void operator+=(const char *psz
) { (void)operator<<(psz
); }
362 void operator+=(char ch
) { (void)operator<<(ch
); }
365 /** @name return resulting string */
368 friend wxString WXDLLEXPORT
operator+(const wxString
& string1
, const wxString
& string2
);
370 friend wxString WXDLLEXPORT
operator+(const wxString
& string
, char ch
);
372 friend wxString WXDLLEXPORT
operator+(char ch
, const wxString
& string
);
374 friend wxString WXDLLEXPORT
operator+(const wxString
& string
, const char *psz
);
376 friend wxString WXDLLEXPORT
operator+(const char *psz
, const wxString
& string
);
380 /** @name string comparison */
383 case-sensitive comparison
384 @return 0 if equal, +1 if greater or -1 if less
385 @see CmpNoCase, IsSameAs
387 int Cmp(const char *psz
) const { return strcmp(c_str(), psz
); }
389 case-insensitive comparison, return code as for wxString::Cmp()
392 int CmpNoCase(const char *psz
) const { return Stricmp(c_str(), psz
); }
394 test for string equality, case-sensitive (default) or not
395 @param bCase is TRUE by default (case matters)
396 @return TRUE if strings are equal, FALSE otherwise
399 bool IsSameAs(const char *psz
, bool bCase
= TRUE
) const
400 { return !(bCase
? Cmp(psz
) : CmpNoCase(psz
)); }
403 /** @name other standard string operations */
405 /** @name simple sub-string extraction
409 return substring starting at nFirst of length
410 nCount (or till the end if nCount = default value)
412 wxString
Mid(size_t nFirst
, size_t nCount
= STRING_MAXLEN
) const;
413 /// get first nCount characters
414 wxString
Left(size_t nCount
) const;
415 /// get all characters before the first occurence of ch
416 /// (returns the whole string if ch not found)
417 wxString
Left(char ch
) const;
418 /// get all characters before the last occurence of ch
419 /// (returns empty string if ch not found)
420 wxString
Before(char ch
) const;
421 /// get all characters after the first occurence of ch
422 /// (returns empty string if ch not found)
423 wxString
After(char ch
) const;
424 /// get last nCount characters
425 wxString
Right(size_t nCount
) const;
426 /// get all characters after the last occurence of ch
427 /// (returns the whole string if ch not found)
428 wxString
Right(char ch
) const;
431 /** @name case conversion */
434 wxString
& MakeUpper();
436 wxString
& MakeLower();
439 /** @name trimming/padding whitespace (either side) and truncating */
441 /// remove spaces from left or from right (default) side
442 wxString
& Trim(bool bFromRight
= TRUE
);
443 /// add nCount copies chPad in the beginning or at the end (default)
444 wxString
& Pad(size_t nCount
, char chPad
= ' ', bool bFromRight
= TRUE
);
445 /// truncate string to given length
446 wxString
& Truncate(size_t uiLen
);
449 /** @name searching and replacing */
451 /// searching (return starting index, or -1 if not found)
452 int Find(char ch
, bool bFromEnd
= FALSE
) const; // like strchr/strrchr
453 /// searching (return starting index, or -1 if not found)
454 int Find(const char *pszSub
) const; // like strstr
456 replace first (or all) occurences of substring with another one
457 @param bReplaceAll: global replace (default) or only the first occurence
458 @return the number of replacements made
460 size_t Replace(const char *szOld
, const char *szNew
, bool bReplaceAll
= TRUE
);
463 /// check if the string contents matches a mask containing '*' and '?'
464 bool Matches(const char *szMask
) const;
467 /** @name formated input/output */
469 /// as sprintf(), returns the number of characters written or < 0 on error
470 int Printf(const char *pszFormat
, ...);
471 /// as vprintf(), returns the number of characters written or < 0 on error
472 int PrintfV(const char* pszFormat
, va_list argptr
);
475 /** @name raw access to string memory */
477 /// ensure that string has space for at least nLen characters
478 // only works if the data of this string is not shared
479 void Alloc(size_t nLen
);
480 /// minimize the string's memory
481 // only works if the data of this string is not shared
484 get writable buffer of at least nLen bytes.
485 Unget() *must* be called a.s.a.p. to put string back in a reasonable
488 char *GetWriteBuf(size_t nLen
);
489 /// call this immediately after GetWriteBuf() has been used
490 void UngetWriteBuf();
493 /** @name wxWindows compatibility functions */
495 /// values for second parameter of CompareTo function
496 enum caseCompare
{exact
, ignoreCase
};
497 /// values for first parameter of Strip function
498 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
500 inline int sprintf(const char *pszFormat
, ...)
503 va_start(argptr
, pszFormat
);
504 int iLen
= PrintfV(pszFormat
, argptr
);
510 inline int CompareTo(const char* psz
, caseCompare cmp
= exact
) const
511 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
513 /// same as Mid (substring extraction)
514 inline wxString
operator()(size_t start
, size_t len
) const
515 { return Mid(start
, len
); }
518 inline wxString
& Append(const char* psz
) { return *this << psz
; }
519 inline wxString
& Append(char ch
, int count
= 1)
520 { wxString
str(ch
, count
); (*this) += str
; return *this; }
523 wxString
& Prepend(const wxString
& str
)
524 { *this = str
+ *this; return *this; }
526 size_t Length() const { return Len(); }
527 /// same as MakeLower
528 void LowerCase() { MakeLower(); }
529 /// same as MakeUpper
530 void UpperCase() { MakeUpper(); }
531 /// same as Trim except that it doesn't change this string
532 wxString
Strip(stripType w
= trailing
) const;
534 /// same as Find (more general variants not yet supported)
535 size_t Index(const char* psz
) const { return Find(psz
); }
536 size_t Index(char ch
) const { return Find(ch
); }
538 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
539 wxString
& RemoveLast() { return Truncate(Len() - 1); }
541 wxString
& Remove(size_t nStart
, size_t nLen
) { return erase( nStart
, nLen
); }
543 int First( const char ch
) const { return Find(ch
); }
544 int First( const char* psz
) const { return Find(psz
); }
545 int First( const wxString
&str
) const { return Find(str
); }
547 int Last( const char ch
) const { return Find(ch
, TRUE
); }
550 bool IsNull() const { return IsEmpty(); }
553 #ifdef STD_STRING_COMPATIBILITY
554 /** @name std::string compatibility functions */
556 /// an 'invalid' value for string index
557 static const size_t npos
;
560 /** @name constructors */
562 /// take nLen chars starting at nPos
563 wxString(const wxString
& str
, size_t nPos
, size_t nLen
= npos
)
565 wxASSERT( str
.GetStringData()->IsValid() );
566 InitWith(str
.c_str(), nPos
, nLen
== npos
? 0 : nLen
);
568 /// take all characters from pStart to pEnd
569 wxString(const void *pStart
, const void *pEnd
);
571 /** @name 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
578 size_t max_size() const { return STRING_MAXLEN
; }
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(); }
586 /** @name lib.string.access */
588 /// return the character at position n
589 char at(size_t n
) const { return GetChar(n
); }
590 /// returns the writable character at position n
591 char& at(size_t n
) { return GetWritableChar(n
); }
593 /** @name lib.string.modifiers */
595 /** @name append something to the end of this one */
598 wxString
& append(const wxString
& str
)
599 { *this += str
; return *this; }
600 /// append elements str[pos], ..., str[pos+n]
601 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
602 { ConcatSelf(n
, str
.c_str() + pos
); return *this; }
603 /// append first n (or all if n == npos) characters of sz
604 wxString
& append(const char *sz
, size_t n
= npos
)
605 { ConcatSelf(n
== npos
? Strlen(sz
) : n
, sz
); return *this; }
607 /// append n copies of ch
608 wxString
& append(size_t n
, char ch
) { return Pad(n
, ch
); }
611 /** @name replaces the contents of this string with another one */
613 /// same as `this_string = str'
614 wxString
& assign(const wxString
& str
) { return (*this) = str
; }
615 /// same as ` = str[pos..pos + n]
616 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
617 { return *this = wxString((const char *)str
+ pos
, n
); }
618 /// same as `= first n (or all if n == npos) characters of sz'
619 wxString
& assign(const char *sz
, size_t n
= npos
)
620 { return *this = wxString(sz
, n
); }
621 /// same as `= n copies of ch'
622 wxString
& assign(size_t n
, char ch
)
623 { return *this = wxString(ch
, n
); }
627 /** @name inserts something at position nPos into this one */
629 /// insert another string
630 wxString
& insert(size_t nPos
, const wxString
& str
);
631 /// insert n chars of str starting at nStart (in str)
632 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
633 { return insert(nPos
, wxString((const char *)str
+ nStart
, n
)); }
635 /// insert first n (or all if n == npos) characters of sz
636 wxString
& insert(size_t nPos
, const char *sz
, size_t n
= npos
)
637 { return insert(nPos
, wxString(sz
, n
)); }
638 /// insert n copies of ch
639 wxString
& insert(size_t nPos
, size_t n
, char ch
)
640 { return insert(nPos
, wxString(ch
, n
)); }
644 /** @name deletes a part of the string */
646 /// delete characters from nStart to nStart + nLen
647 wxString
& erase(size_t nStart
= 0, size_t nLen
= npos
);
650 /** @name replaces a substring of this string with another one */
652 /// replaces the substring of length nLen starting at nStart
653 wxString
& replace(size_t nStart
, size_t nLen
, const char* sz
);
654 /// replaces the substring with nCount copies of ch
655 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, char ch
);
656 /// replaces a substring with another substring
657 wxString
& replace(size_t nStart
, size_t nLen
,
658 const wxString
& str
, size_t nStart2
, size_t nLen2
);
659 /// replaces the substring with first nCount chars of sz
660 wxString
& replace(size_t nStart
, size_t nLen
,
661 const char* sz
, size_t nCount
);
666 void swap(wxString
& str
);
668 /** @name string operations */
670 /** All find() functions take the nStart argument which specifies
671 the position to start the search on, the default value is 0.
673 All functions return npos if there were no match.
679 @name find a match for the string/character in this string
683 size_t find(const wxString
& str
, size_t nStart
= 0) const;
685 // VC++ 1.5 can't cope with this syntax.
686 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
687 /// find first n characters of sz
688 size_t find(const char* sz
, size_t nStart
= 0, size_t n
= npos
) const;
690 /// find the first occurence of character ch after nStart
691 size_t find(char ch
, size_t nStart
= 0) const;
693 // wxWin compatibility
694 inline bool Contains(const wxString
& str
) { return Find(str
) != -1; }
699 @name rfind() family is exactly like find() but works right to left
702 /// as find, but from the end
703 size_t rfind(const wxString
& str
, size_t nStart
= npos
) const;
704 /// as find, but from the end
705 // VC++ 1.5 can't cope with this syntax.
706 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
707 size_t rfind(const char* sz
, size_t nStart
= npos
,
708 size_t n
= npos
) const;
709 /// as find, but from the end
710 size_t rfind(char ch
, size_t nStart
= npos
) const;
715 @name find first/last occurence of any character in the set
719 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const;
721 size_t find_first_of(const char* sz
, size_t nStart
= 0) const;
722 /// same as find(char, size_t)
723 size_t find_first_of(char c
, size_t nStart
= 0) const;
726 size_t find_last_of (const wxString
& str
, size_t nStart
= npos
) const;
728 size_t find_last_of (const char* s
, size_t nStart
= npos
) const;
729 /// same as rfind(char, size_t)
730 size_t find_last_of (char c
, size_t nStart
= npos
) const;
734 @name find first/last occurence of any character not in the set
738 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const;
740 size_t find_first_not_of(const char* s
, size_t nStart
= 0) const;
742 size_t find_first_not_of(char ch
, size_t nStart
= 0) const;
745 size_t find_last_not_of(const wxString
& str
, size_t nStart
=npos
) const;
747 size_t find_last_not_of(const char* s
, size_t nStart
= npos
) const;
749 size_t find_last_not_of(char ch
, size_t nStart
= npos
) const;
754 All compare functions return -1, 0 or 1 if the [sub]string
755 is less, equal or greater than the compare() argument.
760 /// just like strcmp()
761 int compare(const wxString
& str
) const { return Cmp(str
); }
762 /// comparison with a substring
763 int compare(size_t nStart
, size_t nLen
, const wxString
& str
) const;
764 /// comparison of 2 substrings
765 int compare(size_t nStart
, size_t nLen
,
766 const wxString
& str
, size_t nStart2
, size_t nLen2
) const;
767 /// just like strcmp()
768 int compare(const char* sz
) const { return Cmp(sz
); }
769 /// substring comparison with first nCount characters of sz
770 int compare(size_t nStart
, size_t nLen
,
771 const char* sz
, size_t nCount
= npos
) const;
773 wxString
substr(size_t nStart
= 0, size_t nLen
= npos
) const;
778 // ----------------------------------------------------------------------------
779 /** The string array uses it's knowledge of internal structure of the String
780 class to optimize string storage. Normally, we would store pointers to
781 string, but as String is, in fact, itself a pointer (sizeof(String) is
782 sizeof(char *)) we store these pointers instead. The cast to "String *"
783 is really all we need to turn such pointer into a string!
785 Of course, it can be called a dirty hack, but we use twice less memory
786 and this approach is also more speed efficient, so it's probably worth it.
788 Usage notes: when a string is added/inserted, a new copy of it is created,
789 so the original string may be safely deleted. When a string is retrieved
790 from the array (operator[] or Item() method), a reference is returned.
793 @memo probably the most commonly used array type - array of strings
795 // ----------------------------------------------------------------------------
796 class WXDLLEXPORT wxArrayString
799 /** @name ctors and dtor */
804 wxArrayString(const wxArrayString
& array
);
805 /// assignment operator
806 wxArrayString
& operator=(const wxArrayString
& src
);
807 /// not virtual, this class can't be derived from
811 /** @name memory management */
813 /// empties the list, but doesn't release memory
815 /// empties the list and releases memory
817 /// preallocates memory for given number of items
818 void Alloc(size_t nCount
);
819 /// minimzes the memory usage (by freeing all extra memory)
823 /** @name simple accessors */
825 /// number of elements in the array
826 size_t Count() const { return m_nCount
; }
828 bool IsEmpty() const { return m_nCount
== 0; }
831 /** @name items access (range checking is done in debug version) */
833 /// get item at position uiIndex
834 wxString
& Item(size_t nIndex
) const
835 { wxASSERT( nIndex
< m_nCount
); return *(wxString
*)&(m_pItems
[nIndex
]); }
837 wxString
& operator[](size_t nIndex
) const { return Item(nIndex
); }
839 wxString
& Last() const { wxASSERT( !IsEmpty() ); return Item(Count() - 1); }
842 /** @name item management */
845 Search the element in the array, starting from the either side
846 @param if bFromEnd reverse search direction
847 @param if bCase, comparison is case sensitive (default)
848 @return index of the first item matched or NOT_FOUND
851 int Index (const char *sz
, bool bCase
= TRUE
, bool bFromEnd
= FALSE
) const;
852 /// add new element at the end
853 void Add (const wxString
& str
);
854 /// add new element at given position
855 void Insert(const wxString
& str
, size_t uiIndex
);
856 /// remove first item matching this value
857 void Remove(const char *sz
);
858 /// remove item by index
859 void Remove(size_t nIndex
);
862 /// sort array elements
863 void Sort(bool bCase
= TRUE
, bool bReverse
= FALSE
);
866 void Grow(); // makes array bigger if needed
867 void Free(); // free the string stored
869 size_t m_nSize
, // current size of the array
870 m_nCount
; // current number of elements
872 char **m_pItems
; // pointer to data
875 // ---------------------------------------------------------------------------
876 /** @name wxString comparison functions
877 @memo Comparisons are case sensitive
879 // ---------------------------------------------------------------------------
881 inline bool operator==(const wxString
& s1
, const wxString
& s2
) { return s1
.Cmp(s2
) == 0; }
883 inline bool operator==(const wxString
& s1
, const char * s2
) { return s1
.Cmp(s2
) == 0; }
885 inline bool operator==(const char * s1
, const wxString
& s2
) { return s2
.Cmp(s1
) == 0; }
887 inline bool operator!=(const wxString
& s1
, const wxString
& s2
) { return s1
.Cmp(s2
) != 0; }
889 inline bool operator!=(const wxString
& s1
, const char * s2
) { return s1
.Cmp(s2
) != 0; }
891 inline bool operator!=(const char * s1
, const wxString
& s2
) { return s2
.Cmp(s1
) != 0; }
893 inline bool operator< (const wxString
& s1
, const wxString
& s2
) { return s1
.Cmp(s2
) < 0; }
895 inline bool operator< (const wxString
& s1
, const char * s2
) { return s1
.Cmp(s2
) < 0; }
897 inline bool operator< (const char * s1
, const wxString
& s2
) { return s2
.Cmp(s1
) > 0; }
899 inline bool operator> (const wxString
& s1
, const wxString
& s2
) { return s1
.Cmp(s2
) > 0; }
901 inline bool operator> (const wxString
& s1
, const char * s2
) { return s1
.Cmp(s2
) > 0; }
903 inline bool operator> (const char * s1
, const wxString
& s2
) { return s2
.Cmp(s1
) < 0; }
905 inline bool operator<=(const wxString
& s1
, const wxString
& s2
) { return s1
.Cmp(s2
) <= 0; }
907 inline bool operator<=(const wxString
& s1
, const char * s2
) { return s1
.Cmp(s2
) <= 0; }
909 inline bool operator<=(const char * s1
, const wxString
& s2
) { return s2
.Cmp(s1
) >= 0; }
911 inline bool operator>=(const wxString
& s1
, const wxString
& s2
) { return s1
.Cmp(s2
) >= 0; }
913 inline bool operator>=(const wxString
& s1
, const char * s2
) { return s1
.Cmp(s2
) >= 0; }
915 inline bool operator>=(const char * s1
, const wxString
& s2
) { return s2
.Cmp(s1
) <= 0; }
918 // ---------------------------------------------------------------------------
919 /** @name Global functions complementing standard C string library
920 @memo replacements for strlen() and portable strcasecmp()
922 // ---------------------------------------------------------------------------
924 #ifdef STD_STRING_COMPATIBILITY
927 // Known not to work with wxUSE_IOSTREAMH set to 0, so
928 // replacing with includes (on advice of ungod@pasdex.com.au)
929 // class WXDLLEXPORT istream;
931 // N.B. BC++ doesn't have istream.h, ostream.h
932 #include <iostream.h>
940 istream
& WXDLLEXPORT
operator>>(istream
& is
, wxString
& str
);
942 #endif //std::string compatibility
944 #endif // _WX_WXSTRINGH__