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 WXDLLEXPORT_DATA(extern const char*) wxEmptyString
;
78 /// checks whether the passed in pointer is NULL and if the string is empty
79 inline bool WXDLLEXPORT
IsEmpty(const char *p
) { return !p
|| !*p
; }
81 /// safe version of strlen() (returns 0 if passed NULL pointer)
82 inline size_t WXDLLEXPORT
Strlen(const char *psz
)
83 { return psz
? strlen(psz
) : 0; }
85 /// portable strcasecmp/_stricmp
86 inline int WXDLLEXPORT
Stricmp(const char *psz1
, const char *psz2
)
89 return _stricmp(psz1
, psz2
);
90 #elif defined(__BORLANDC__)
91 return stricmp(psz1
, psz2
);
92 #elif defined(__WATCOMC__)
93 return stricmp(psz1
, psz2
);
94 #elif defined(__UNIX__) || defined(__GNUWIN32__)
95 return strcasecmp(psz1
, psz2
);
97 // almost all compilers/libraries provide this function (unfortunately under
98 // different names), that's why we don't implement our own which will surely
99 // be more efficient than this code (uncomment to use):
101 register char c1, c2;
103 c1 = tolower(*psz1++);
104 c2 = tolower(*psz2++);
105 } while ( c1 && (c1 == c2) );
110 #error "Please define string case-insensitive compare for your OS/compiler"
111 #endif // OS/compiler
114 // ----------------------------------------------------------------------------
116 // ----------------------------------------------------------------------------
118 // global pointer to empty string
119 WXDLLEXPORT_DATA(extern const char*) g_szNul
;
121 // return an empty wxString
122 class WXDLLEXPORT wxString
; // not yet defined
123 inline const wxString
& wxGetEmptyString() { return *(wxString
*)&g_szNul
; }
125 // ---------------------------------------------------------------------------
126 // string data prepended with some housekeeping info (used by wxString class),
127 // is never used directly (but had to be put here to allow inlining)
128 // ---------------------------------------------------------------------------
129 struct WXDLLEXPORT wxStringData
131 int nRefs
; // reference count
132 size_t nDataLength
, // actual string length
133 nAllocLength
; // allocated memory size
135 // mimics declaration 'char data[nAllocLength]'
136 char* data() const { return (char*)(this + 1); }
138 // empty string has a special ref count so it's never deleted
139 bool IsEmpty() const { return nRefs
== -1; }
140 bool IsShared() const { return nRefs
> 1; }
143 void Lock() { if ( !IsEmpty() ) nRefs
++; }
144 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) free(this); }
146 // if we had taken control over string memory (GetWriteBuf), it's
147 // intentionally put in invalid state
148 void Validate(bool b
) { nRefs
= b
? 1 : 0; }
149 bool IsValid() const { return nRefs
!= 0; }
152 // ---------------------------------------------------------------------------
154 This is (yet another one) String class for C++ programmers. It doesn't use
155 any of "advanced" C++ features (i.e. templates, exceptions, namespaces...)
156 thus you should be able to compile it with practicaly any C++ compiler.
157 This class uses copy-on-write technique, i.e. identical strings share the
158 same memory as long as neither of them is changed.
160 This class aims to be as compatible as possible with the new standard
161 std::string class, but adds some additional functions and should be
162 at least as efficient than the standard implementation.
164 Performance note: it's more efficient to write functions which take
165 "const String&" arguments than "const char *" if you assign the argument
168 It was compiled and tested under Win32, Linux (libc 5 & 6), Solaris 5.5.
171 - ressource support (string tables in ressources)
172 - more wide character (UNICODE) support
173 - regular expressions support
175 @memo A non-template portable wxString class implementing copy-on-write.
179 // ---------------------------------------------------------------------------
180 #ifdef WXSTRING_IS_WXOBJECT
181 class WXDLLEXPORT wxString
: public wxObject
183 DECLARE_DYNAMIC_CLASS(wxString
)
184 #else //WXSTRING_IS_WXOBJECT
185 class WXDLLEXPORT wxString
187 #endif //WXSTRING_IS_WXOBJECT
189 friend class WXDLLEXPORT wxArrayString
;
191 // NB: special care was taken in arrangin the member functions in such order
192 // that all inline functions can be effectively inlined
194 // points to data preceded by wxStringData structure with ref count info
197 // accessor to string data
198 wxStringData
* GetStringData() const { return (wxStringData
*)m_pchData
- 1; }
200 // string (re)initialization functions
201 // initializes the string to the empty value (must be called only from
202 // ctors, use Reinit() otherwise)
203 void Init() { m_pchData
= (char *)g_szNul
; }
204 // initializaes the string with (a part of) C-string
205 void InitWith(const char *psz
, size_t nPos
= 0, size_t nLen
= STRING_MAXLEN
);
206 // as Init, but also frees old data
207 void Reinit() { GetStringData()->Unlock(); Init(); }
210 // allocates memory for string of lenght nLen
211 void AllocBuffer(size_t nLen
);
212 // copies data to another string
213 void AllocCopy(wxString
&, int, int) const;
214 // effectively copies data to string
215 void AssignCopy(size_t, const char *);
217 // append a (sub)string
218 void ConcatSelf(int nLen
, const char *src
);
220 // functions called before writing to the string: they copy it if there
221 // are other references to our data (should be the only owner when writing)
222 void CopyBeforeWrite();
223 void AllocBeforeWrite(size_t);
226 /** @name constructors & dtor */
228 /// ctor for an empty string
229 wxString() { Init(); }
231 wxString(const wxString
& stringSrc
)
233 wxASSERT( stringSrc
.GetStringData()->IsValid() );
235 if ( stringSrc
.IsEmpty() ) {
236 // nothing to do for an empty string
240 m_pchData
= stringSrc
.m_pchData
; // share same data
241 GetStringData()->Lock(); // => one more copy
244 /// string containing nRepeat copies of ch
245 wxString(char ch
, size_t nRepeat
= 1);
246 /// ctor takes first nLength characters from C string
247 // (default value of STRING_MAXLEN means take all the string)
248 wxString(const char *psz
, size_t nLength
= STRING_MAXLEN
)
249 { InitWith(psz
, 0, nLength
); }
250 /// from C string (for compilers using unsigned char)
251 wxString(const unsigned char* psz
, size_t nLength
= STRING_MAXLEN
);
252 /// from wide (UNICODE) string
253 wxString(const wchar_t *pwz
);
254 /// dtor is not virtual, this class must not be inherited from!
255 ~wxString() { GetStringData()->Unlock(); }
258 /** @name generic attributes & operations */
260 /// as standard strlen()
261 size_t Len() const { return GetStringData()->nDataLength
; }
262 /// string contains any characters?
263 bool IsEmpty() const { return Len() == 0; }
264 /// empty string contents
271 wxASSERT( GetStringData()->nDataLength
== 0 );
273 /// empty the string and free memory
276 if ( !GetStringData()->IsEmpty() )
279 wxASSERT( GetStringData()->nDataLength
== 0 ); // should be empty
280 wxASSERT( GetStringData()->nAllocLength
== 0 ); // and not own any memory
283 /// Is an ascii value
284 bool IsAscii() const;
286 bool IsNumber() const;
291 /** @name data access (all indexes are 0 based) */
294 char GetChar(size_t n
) const
295 { ASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
296 /// read/write access
297 char& GetWritableChar(size_t n
)
298 { ASSERT_VALID_INDEX( n
); CopyBeforeWrite(); return m_pchData
[n
]; }
300 void SetChar(size_t n
, char ch
)
301 { ASSERT_VALID_INDEX( n
); CopyBeforeWrite(); m_pchData
[n
] = ch
; }
303 /// get last character
305 { wxASSERT( !IsEmpty() ); return m_pchData
[Len() - 1]; }
306 /// get writable last character
308 { wxASSERT( !IsEmpty() ); CopyBeforeWrite(); return m_pchData
[Len()-1]; }
310 // on alpha-linux this gives overload problems:
311 // Also on Solaris, so removing for now (JACS)
312 #if ! defined(__ALPHA__)
313 /// operator version of GetChar
314 char operator[](size_t n
) const
315 { ASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
318 /// operator version of GetChar
319 char operator[](int n
) const
320 { ASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
321 /// operator version of GetWritableChar
322 char& operator[](size_t n
)
323 { ASSERT_VALID_INDEX( n
); CopyBeforeWrite(); return m_pchData
[n
]; }
325 /// implicit conversion to C string
326 operator const char*() const { return m_pchData
; }
327 /// explicit conversion to C string (use this with printf()!)
328 const char* c_str() const { return m_pchData
; }
330 const char* GetData() const { return m_pchData
; }
333 /** @name overloaded assignment */
336 wxString
& operator=(const wxString
& stringSrc
);
338 wxString
& operator=(char ch
);
340 wxString
& operator=(const char *psz
);
342 wxString
& operator=(const unsigned char* psz
);
344 wxString
& operator=(const wchar_t *pwz
);
347 /** @name string concatenation */
349 /** @name in place concatenation */
350 /** @name concatenate and return the result
351 left to right associativity of << allows to write
352 things like "str << str1 << str2 << ..." */
355 wxString
& operator<<(const wxString
& s
)
357 wxASSERT( s
.GetStringData()->IsValid() );
359 ConcatSelf(s
.Len(), s
);
363 wxString
& operator<<(const char *psz
)
364 { ConcatSelf(Strlen(psz
), psz
); return *this; }
366 wxString
& operator<<(char ch
) { ConcatSelf(1, &ch
); return *this; }
371 void operator+=(const wxString
& s
) { (void)operator<<(s
); }
372 /// string += C string
373 void operator+=(const char *psz
) { (void)operator<<(psz
); }
375 void operator+=(char ch
) { (void)operator<<(ch
); }
378 /** @name return resulting string */
381 friend wxString WXDLLEXPORT
operator+(const wxString
& string1
, const wxString
& string2
);
383 friend wxString WXDLLEXPORT
operator+(const wxString
& string
, char ch
);
385 friend wxString WXDLLEXPORT
operator+(char ch
, const wxString
& string
);
387 friend wxString WXDLLEXPORT
operator+(const wxString
& string
, const char *psz
);
389 friend wxString WXDLLEXPORT
operator+(const char *psz
, const wxString
& string
);
393 /** @name stream-like functions */
395 /// insert an int into string
396 wxString
& operator<<(int i
);
397 /// insert a float into string
398 wxString
& operator<<(float f
);
399 /// insert a double into string
400 wxString
& operator<<(double d
);
403 /** @name string comparison */
406 case-sensitive comparison
407 @return 0 if equal, +1 if greater or -1 if less
408 @see CmpNoCase, IsSameAs
410 int Cmp(const char *psz
) const { return strcmp(c_str(), psz
); }
412 case-insensitive comparison, return code as for wxString::Cmp()
415 int CmpNoCase(const char *psz
) const { return Stricmp(c_str(), psz
); }
417 test for string equality, case-sensitive (default) or not
418 @param bCase is TRUE by default (case matters)
419 @return TRUE if strings are equal, FALSE otherwise
422 bool IsSameAs(const char *psz
, bool bCase
= TRUE
) const
423 { return !(bCase
? Cmp(psz
) : CmpNoCase(psz
)); }
426 /** @name other standard string operations */
428 /** @name simple sub-string extraction
432 return substring starting at nFirst of length
433 nCount (or till the end if nCount = default value)
435 wxString
Mid(size_t nFirst
, size_t nCount
= STRING_MAXLEN
) const;
436 /// get first nCount characters
437 wxString
Left(size_t nCount
) const;
438 /// get all characters before the first occurence of ch
439 /// (returns the whole string if ch not found)
440 wxString
Left(char ch
) const;
441 /// get all characters before the last occurence of ch
442 /// (returns empty string if ch not found)
443 wxString
Before(char ch
) const;
444 /// get all characters after the first occurence of ch
445 /// (returns empty string if ch not found)
446 wxString
After(char ch
) const;
447 /// get last nCount characters
448 wxString
Right(size_t nCount
) const;
449 /// get all characters after the last occurence of ch
450 /// (returns the whole string if ch not found)
451 wxString
Right(char ch
) const;
454 /** @name case conversion */
457 wxString
& MakeUpper();
459 wxString
& MakeLower();
462 /** @name trimming/padding whitespace (either side) and truncating */
464 /// remove spaces from left or from right (default) side
465 wxString
& Trim(bool bFromRight
= TRUE
);
466 /// add nCount copies chPad in the beginning or at the end (default)
467 wxString
& Pad(size_t nCount
, char chPad
= ' ', bool bFromRight
= TRUE
);
468 /// truncate string to given length
469 wxString
& Truncate(size_t uiLen
);
472 /** @name searching and replacing */
474 /// searching (return starting index, or -1 if not found)
475 int Find(char ch
, bool bFromEnd
= FALSE
) const; // like strchr/strrchr
476 /// searching (return starting index, or -1 if not found)
477 int Find(const char *pszSub
) const; // like strstr
479 replace first (or all) occurences of substring with another one
480 @param bReplaceAll: global replace (default) or only the first occurence
481 @return the number of replacements made
483 size_t Replace(const char *szOld
, const char *szNew
, bool bReplaceAll
= TRUE
);
486 /// check if the string contents matches a mask containing '*' and '?'
487 bool Matches(const char *szMask
) const;
490 /** @name formated input/output */
492 /// as sprintf(), returns the number of characters written or < 0 on error
493 int Printf(const char *pszFormat
, ...);
494 /// as vprintf(), returns the number of characters written or < 0 on error
495 int PrintfV(const char* pszFormat
, va_list argptr
);
498 /** @name raw access to string memory */
500 /// ensure that string has space for at least nLen characters
501 // only works if the data of this string is not shared
502 void Alloc(size_t nLen
);
503 /// minimize the string's memory
504 // only works if the data of this string is not shared
507 get writable buffer of at least nLen bytes.
508 Unget() *must* be called a.s.a.p. to put string back in a reasonable
511 char *GetWriteBuf(size_t nLen
);
512 /// call this immediately after GetWriteBuf() has been used
513 void UngetWriteBuf();
516 /** @name wxWindows compatibility functions */
518 /// values for second parameter of CompareTo function
519 enum caseCompare
{exact
, ignoreCase
};
520 /// values for first parameter of Strip function
521 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
523 inline int sprintf(const char *pszFormat
, ...)
526 va_start(argptr
, pszFormat
);
527 int iLen
= PrintfV(pszFormat
, argptr
);
533 inline int CompareTo(const char* psz
, caseCompare cmp
= exact
) const
534 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
536 /// same as Mid (substring extraction)
537 inline wxString
operator()(size_t start
, size_t len
) const
538 { return Mid(start
, len
); }
541 inline wxString
& Append(const char* psz
) { return *this << psz
; }
542 inline wxString
& Append(char ch
, int count
= 1)
543 { wxString
str(ch
, count
); (*this) += str
; return *this; }
546 wxString
& Prepend(const wxString
& str
)
547 { *this = str
+ *this; return *this; }
549 size_t Length() const { return Len(); }
550 /// same as MakeLower
551 void LowerCase() { MakeLower(); }
552 /// same as MakeUpper
553 void UpperCase() { MakeUpper(); }
554 /// same as Trim except that it doesn't change this string
555 wxString
Strip(stripType w
= trailing
) const;
557 /// same as Find (more general variants not yet supported)
558 size_t Index(const char* psz
) const { return Find(psz
); }
559 size_t Index(char ch
) const { return Find(ch
); }
561 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
562 wxString
& RemoveLast() { return Truncate(Len() - 1); }
564 wxString
& Remove(size_t nStart
, size_t nLen
) { return erase( nStart
, nLen
); }
566 int First( const char ch
) const { return Find(ch
); }
567 int First( const char* psz
) const { return Find(psz
); }
568 int First( const wxString
&str
) const { return Find(str
); }
570 int Last( const char ch
) const { return Find(ch
, TRUE
); }
573 bool IsNull() const { return IsEmpty(); }
576 #ifdef STD_STRING_COMPATIBILITY
577 /** @name std::string compatibility functions */
579 /// an 'invalid' value for string index
580 static const size_t npos
;
583 /** @name constructors */
585 /// take nLen chars starting at nPos
586 wxString(const wxString
& str
, size_t nPos
, size_t nLen
= npos
)
588 wxASSERT( str
.GetStringData()->IsValid() );
589 InitWith(str
.c_str(), nPos
, nLen
== npos
? 0 : nLen
);
591 /// take all characters from pStart to pEnd
592 wxString(const void *pStart
, const void *pEnd
);
594 /** @name lib.string.capacity */
596 /// return the length of the string
597 size_t size() const { return Len(); }
598 /// return the length of the string
599 size_t length() const { return Len(); }
600 /// return the maximum size of the string
601 size_t max_size() const { return STRING_MAXLEN
; }
602 /// resize the string, filling the space with c if c != 0
603 void resize(size_t nSize
, char ch
= '\0');
604 /// delete the contents of the string
605 void clear() { Empty(); }
606 /// returns true if the string is empty
607 bool empty() const { return IsEmpty(); }
609 /** @name lib.string.access */
611 /// return the character at position n
612 char at(size_t n
) const { return GetChar(n
); }
613 /// returns the writable character at position n
614 char& at(size_t n
) { return GetWritableChar(n
); }
616 /** @name lib.string.modifiers */
618 /** @name append something to the end of this one */
621 wxString
& append(const wxString
& str
)
622 { *this += str
; return *this; }
623 /// append elements str[pos], ..., str[pos+n]
624 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
625 { ConcatSelf(n
, str
.c_str() + pos
); return *this; }
626 /// append first n (or all if n == npos) characters of sz
627 wxString
& append(const char *sz
, size_t n
= npos
)
628 { ConcatSelf(n
== npos
? Strlen(sz
) : n
, sz
); return *this; }
630 /// append n copies of ch
631 wxString
& append(size_t n
, char ch
) { return Pad(n
, ch
); }
634 /** @name replaces the contents of this string with another one */
636 /// same as `this_string = str'
637 wxString
& assign(const wxString
& str
) { return (*this) = str
; }
638 /// same as ` = str[pos..pos + n]
639 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
640 { return *this = wxString((const char *)str
+ pos
, n
); }
641 /// same as `= first n (or all if n == npos) characters of sz'
642 wxString
& assign(const char *sz
, size_t n
= npos
)
643 { return *this = wxString(sz
, n
); }
644 /// same as `= n copies of ch'
645 wxString
& assign(size_t n
, char ch
)
646 { return *this = wxString(ch
, n
); }
650 /** @name inserts something at position nPos into this one */
652 /// insert another string
653 wxString
& insert(size_t nPos
, const wxString
& str
);
654 /// insert n chars of str starting at nStart (in str)
655 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
656 { return insert(nPos
, wxString((const char *)str
+ nStart
, n
)); }
658 /// insert first n (or all if n == npos) characters of sz
659 wxString
& insert(size_t nPos
, const char *sz
, size_t n
= npos
)
660 { return insert(nPos
, wxString(sz
, n
)); }
661 /// insert n copies of ch
662 wxString
& insert(size_t nPos
, size_t n
, char ch
)
663 { return insert(nPos
, wxString(ch
, n
)); }
667 /** @name deletes a part of the string */
669 /// delete characters from nStart to nStart + nLen
670 wxString
& erase(size_t nStart
= 0, size_t nLen
= npos
);
673 /** @name replaces a substring of this string with another one */
675 /// replaces the substring of length nLen starting at nStart
676 wxString
& replace(size_t nStart
, size_t nLen
, const char* sz
);
677 /// replaces the substring with nCount copies of ch
678 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, char ch
);
679 /// replaces a substring with another substring
680 wxString
& replace(size_t nStart
, size_t nLen
,
681 const wxString
& str
, size_t nStart2
, size_t nLen2
);
682 /// replaces the substring with first nCount chars of sz
683 wxString
& replace(size_t nStart
, size_t nLen
,
684 const char* sz
, size_t nCount
);
689 void swap(wxString
& str
);
691 /** @name string operations */
693 /** All find() functions take the nStart argument which specifies
694 the position to start the search on, the default value is 0.
696 All functions return npos if there were no match.
702 @name find a match for the string/character in this string
706 size_t find(const wxString
& str
, size_t nStart
= 0) const;
708 // VC++ 1.5 can't cope with this syntax.
709 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
710 /// find first n characters of sz
711 size_t find(const char* sz
, size_t nStart
= 0, size_t n
= npos
) const;
713 // Gives a duplicate symbol (presumably a case-insensitivity problem)
714 #if !defined(__BORLANDC__)
715 /// find the first occurence of character ch after nStart
716 size_t find(char ch
, size_t nStart
= 0) const;
718 // wxWin compatibility
719 inline bool Contains(const wxString
& str
) const { return Find(str
) != -1; }
724 @name rfind() family is exactly like find() but works right to left
727 /// as find, but from the end
728 size_t rfind(const wxString
& str
, size_t nStart
= npos
) const;
729 /// as find, but from the end
730 // VC++ 1.5 can't cope with this syntax.
731 #if ! (defined(_MSC_VER) && !defined(__WIN32__))
732 size_t rfind(const char* sz
, size_t nStart
= npos
,
733 size_t n
= npos
) const;
734 /// as find, but from the end
735 size_t rfind(char ch
, size_t nStart
= npos
) const;
740 @name find first/last occurence of any character in the set
744 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const;
746 size_t find_first_of(const char* sz
, size_t nStart
= 0) const;
747 /// same as find(char, size_t)
748 size_t find_first_of(char c
, size_t nStart
= 0) const;
751 size_t find_last_of (const wxString
& str
, size_t nStart
= npos
) const;
753 size_t find_last_of (const char* s
, size_t nStart
= npos
) const;
754 /// same as rfind(char, size_t)
755 size_t find_last_of (char c
, size_t nStart
= npos
) const;
759 @name find first/last occurence of any character not in the set
763 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const;
765 size_t find_first_not_of(const char* s
, size_t nStart
= 0) const;
767 size_t find_first_not_of(char ch
, size_t nStart
= 0) const;
770 size_t find_last_not_of(const wxString
& str
, size_t nStart
=npos
) const;
772 size_t find_last_not_of(const char* s
, size_t nStart
= npos
) const;
774 size_t find_last_not_of(char ch
, size_t nStart
= npos
) const;
779 All compare functions return -1, 0 or 1 if the [sub]string
780 is less, equal or greater than the compare() argument.
785 /// just like strcmp()
786 int compare(const wxString
& str
) const { return Cmp(str
); }
787 /// comparison with a substring
788 int compare(size_t nStart
, size_t nLen
, const wxString
& str
) const;
789 /// comparison of 2 substrings
790 int compare(size_t nStart
, size_t nLen
,
791 const wxString
& str
, size_t nStart2
, size_t nLen2
) const;
792 /// just like strcmp()
793 int compare(const char* sz
) const { return Cmp(sz
); }
794 /// substring comparison with first nCount characters of sz
795 int compare(size_t nStart
, size_t nLen
,
796 const char* sz
, size_t nCount
= npos
) const;
798 wxString
substr(size_t nStart
= 0, size_t nLen
= npos
) const;
803 // ----------------------------------------------------------------------------
804 /** The string array uses it's knowledge of internal structure of the String
805 class to optimize string storage. Normally, we would store pointers to
806 string, but as String is, in fact, itself a pointer (sizeof(String) is
807 sizeof(char *)) we store these pointers instead. The cast to "String *"
808 is really all we need to turn such pointer into a string!
810 Of course, it can be called a dirty hack, but we use twice less memory
811 and this approach is also more speed efficient, so it's probably worth it.
813 Usage notes: when a string is added/inserted, a new copy of it is created,
814 so the original string may be safely deleted. When a string is retrieved
815 from the array (operator[] or Item() method), a reference is returned.
818 @memo probably the most commonly used array type - array of strings
820 // ----------------------------------------------------------------------------
821 class WXDLLEXPORT wxArrayString
824 /** @name ctors and dtor */
829 wxArrayString(const wxArrayString
& array
);
830 /// assignment operator
831 wxArrayString
& operator=(const wxArrayString
& src
);
832 /// not virtual, this class can't be derived from
836 /** @name memory management */
838 /// empties the list, but doesn't release memory
840 /// empties the list and releases memory
842 /// preallocates memory for given number of items
843 void Alloc(size_t nCount
);
844 /// minimzes the memory usage (by freeing all extra memory)
848 /** @name simple accessors */
850 /// number of elements in the array
851 size_t Count() const { return m_nCount
; }
853 bool IsEmpty() const { return m_nCount
== 0; }
856 /** @name items access (range checking is done in debug version) */
858 /// get item at position uiIndex
859 wxString
& Item(size_t nIndex
) const
860 { wxASSERT( nIndex
< m_nCount
); return *(wxString
*)&(m_pItems
[nIndex
]); }
862 wxString
& operator[](size_t nIndex
) const { return Item(nIndex
); }
864 wxString
& Last() const { wxASSERT( !IsEmpty() ); return Item(Count() - 1); }
867 /** @name item management */
870 Search the element in the array, starting from the either side
871 @param if bFromEnd reverse search direction
872 @param if bCase, comparison is case sensitive (default)
873 @return index of the first item matched or NOT_FOUND
876 int Index (const char *sz
, bool bCase
= TRUE
, bool bFromEnd
= FALSE
) const;
877 /// add new element at the end
878 void Add (const wxString
& str
);
879 /// add new element at given position
880 void Insert(const wxString
& str
, size_t uiIndex
);
881 /// remove first item matching this value
882 void Remove(const char *sz
);
883 /// remove item by index
884 void Remove(size_t nIndex
);
887 /// sort array elements
888 void Sort(bool bCase
= TRUE
, bool bReverse
= FALSE
);
891 void Grow(); // makes array bigger if needed
892 void Free(); // free the string stored
894 size_t m_nSize
, // current size of the array
895 m_nCount
; // current number of elements
897 char **m_pItems
; // pointer to data
900 // ---------------------------------------------------------------------------
901 /** @name wxString comparison functions
902 @memo Comparisons are case sensitive
904 // ---------------------------------------------------------------------------
906 inline bool operator==(const wxString
& s1
, const wxString
& s2
) { return s1
.Cmp(s2
) == 0; }
908 inline bool operator==(const wxString
& s1
, const char * s2
) { return s1
.Cmp(s2
) == 0; }
910 inline bool operator==(const char * s1
, const wxString
& s2
) { return s2
.Cmp(s1
) == 0; }
912 inline bool operator!=(const wxString
& s1
, const wxString
& s2
) { return s1
.Cmp(s2
) != 0; }
914 inline bool operator!=(const wxString
& s1
, const char * s2
) { return s1
.Cmp(s2
) != 0; }
916 inline bool operator!=(const char * s1
, const wxString
& s2
) { return s2
.Cmp(s1
) != 0; }
918 inline bool operator< (const wxString
& s1
, const wxString
& s2
) { return s1
.Cmp(s2
) < 0; }
920 inline bool operator< (const wxString
& s1
, const char * s2
) { return s1
.Cmp(s2
) < 0; }
922 inline bool operator< (const char * s1
, const wxString
& s2
) { return s2
.Cmp(s1
) > 0; }
924 inline bool operator> (const wxString
& s1
, const wxString
& s2
) { return s1
.Cmp(s2
) > 0; }
926 inline bool operator> (const wxString
& s1
, const char * s2
) { return s1
.Cmp(s2
) > 0; }
928 inline bool operator> (const char * s1
, const wxString
& s2
) { return s2
.Cmp(s1
) < 0; }
930 inline bool operator<=(const wxString
& s1
, const wxString
& s2
) { return s1
.Cmp(s2
) <= 0; }
932 inline bool operator<=(const wxString
& s1
, const char * s2
) { return s1
.Cmp(s2
) <= 0; }
934 inline bool operator<=(const char * s1
, const wxString
& s2
) { return s2
.Cmp(s1
) >= 0; }
936 inline bool operator>=(const wxString
& s1
, const wxString
& s2
) { return s1
.Cmp(s2
) >= 0; }
938 inline bool operator>=(const wxString
& s1
, const char * s2
) { return s1
.Cmp(s2
) >= 0; }
940 inline bool operator>=(const char * s1
, const wxString
& s2
) { return s2
.Cmp(s1
) <= 0; }
942 wxString WXDLLEXPORT
operator+(const wxString
& string1
, const wxString
& string2
);
943 wxString WXDLLEXPORT
operator+(const wxString
& string
, char ch
);
944 wxString WXDLLEXPORT
operator+(char ch
, const wxString
& string
);
945 wxString WXDLLEXPORT
operator+(const wxString
& string
, const char *psz
);
946 wxString WXDLLEXPORT
operator+(const char *psz
, const wxString
& string
);
948 // ---------------------------------------------------------------------------
949 /** @name Global functions complementing standard C string library
950 @memo replacements for strlen() and portable strcasecmp()
952 // ---------------------------------------------------------------------------
954 #ifdef STD_STRING_COMPATIBILITY
957 // Known not to work with wxUSE_IOSTREAMH set to 0, so
958 // replacing with includes (on advice of ungod@pasdex.com.au)
959 // class WXDLLEXPORT istream;
961 // N.B. BC++ doesn't have istream.h, ostream.h
962 #include <iostream.h>
970 WXDLLEXPORT istream
& operator>>(istream
& is
, wxString
& str
);
972 #endif //std::string compatibility
974 #endif // _WX_WXSTRINGH__