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 ///////////////////////////////////////////////////////////////////////////////
13 Efficient string class [more or less] compatible with MFC CString,
14 wxWindows version 1 wxString and std::string and some handy functions
15 missing from string.h.
18 #ifndef _WX_WXSTRINGH__
19 #define _WX_WXSTRINGH__
22 #pragma interface "string.h"
25 // ----------------------------------------------------------------------------
26 // conditinal compilation
27 // ----------------------------------------------------------------------------
29 // compile the std::string compatibility functions if defined
30 #define wxSTD_STRING_COMPATIBILITY
32 // define to derive wxString from wxObject (deprecated!)
33 #ifdef WXSTRING_IS_WXOBJECT
34 #undef WXSTRING_IS_WXOBJECT
37 // ----------------------------------------------------------------------------
39 // ----------------------------------------------------------------------------
41 #if defined(__WXMAC__) || defined(__VISAGECPP__)
49 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
50 // problem in VACPP V4 with including stdlib.h multiple times
51 // strconv includes it anyway
65 #include <strings.h> // for strcasecmp()
68 #include "wx/defs.h" // everybody should include this
69 #include "wx/debug.h" // for wxASSERT()
70 #include "wx/wxchar.h" // for wxChar
71 #include "wx/buffer.h" // for wxCharBuffer
72 #include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes
75 #ifdef WXSTRING_IS_WXOBJECT
76 #include "wx/object.h" // base class
80 // ---------------------------------------------------------------------------
82 // ---------------------------------------------------------------------------
85 #define WXSTRINGCAST (wxChar *)(const wxChar *)
86 #define wxCSTRINGCAST (wxChar *)(const wxChar *)
87 #define wxMBSTRINGCAST (char *)(const char *)
88 #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *)
90 // implementation only
91 #define ASSERT_VALID_INDEX(i) wxASSERT( (unsigned)(i) <= Len() )
93 // ----------------------------------------------------------------------------
95 // ----------------------------------------------------------------------------
97 // maximum possible length for a string means "take all string" everywhere
98 // (as sizeof(StringData) is unknown here, we substract 100)
99 const unsigned int wxSTRING_MAXLEN
= UINT_MAX
- 100;
101 // ----------------------------------------------------------------------------
103 // ----------------------------------------------------------------------------
105 // global pointer to empty string
106 WXDLLEXPORT_DATA(extern const wxChar
*) wxEmptyString
;
108 // ---------------------------------------------------------------------------
109 // global functions complementing standard C string library replacements for
110 // strlen() and portable strcasecmp()
111 //---------------------------------------------------------------------------
113 // Use wxXXX() functions from wxchar.h instead! These functions are for
114 // backwards compatibility only.
116 // checks whether the passed in pointer is NULL and if the string is empty
117 inline bool WXDLLEXPORT
IsEmpty(const char *p
) { return (!p
|| !*p
); }
119 // safe version of strlen() (returns 0 if passed NULL pointer)
120 inline size_t WXDLLEXPORT
Strlen(const char *psz
)
121 { return psz
? strlen(psz
) : 0; }
123 // portable strcasecmp/_stricmp
124 inline int WXDLLEXPORT
Stricmp(const char *psz1
, const char *psz2
)
126 #if defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) )
127 return _stricmp(psz1
, psz2
);
128 #elif defined(__SC__)
129 return _stricmp(psz1
, psz2
);
130 #elif defined(__SALFORDC__)
131 return stricmp(psz1
, psz2
);
132 #elif defined(__BORLANDC__)
133 return stricmp(psz1
, psz2
);
134 #elif defined(__WATCOMC__)
135 return stricmp(psz1
, psz2
);
136 #elif defined(__EMX__)
137 return stricmp(psz1
, psz2
);
138 #elif defined(__WXPM__)
139 return stricmp(psz1
, psz2
);
140 #elif defined(__UNIX__) || defined(__GNUWIN32__)
141 return strcasecmp(psz1
, psz2
);
142 #elif defined(__MWERKS__) && !defined(__INTEL__)
143 register char c1
, c2
;
145 c1
= tolower(*psz1
++);
146 c2
= tolower(*psz2
++);
147 } while ( c1
&& (c1
== c2
) );
151 // almost all compilers/libraries provide this function (unfortunately under
152 // different names), that's why we don't implement our own which will surely
153 // be more efficient than this code (uncomment to use):
155 register char c1, c2;
157 c1 = tolower(*psz1++);
158 c2 = tolower(*psz2++);
159 } while ( c1 && (c1 == c2) );
164 #error "Please define string case-insensitive compare for your OS/compiler"
165 #endif // OS/compiler
168 // wxSnprintf() is like snprintf() if it's available and sprintf() (always
169 // available, but dangerous!) if not
170 extern int WXDLLEXPORT
wxSnprintf(wxChar
*buf
, size_t len
,
171 const wxChar
*format
, ...);
173 // and wxVsnprintf() is like vsnprintf() or vsprintf()
174 extern int WXDLLEXPORT
wxVsnprintf(wxChar
*buf
, size_t len
,
175 const wxChar
*format
, va_list argptr
);
177 // return an empty wxString
178 class WXDLLEXPORT wxString
; // not yet defined
179 inline const wxString
& wxGetEmptyString() { return *(wxString
*)&wxEmptyString
; }
181 // ---------------------------------------------------------------------------
182 // string data prepended with some housekeeping info (used by wxString class),
183 // is never used directly (but had to be put here to allow inlining)
184 // ---------------------------------------------------------------------------
186 struct WXDLLEXPORT wxStringData
188 int nRefs
; // reference count
189 size_t nDataLength
, // actual string length
190 nAllocLength
; // allocated memory size
192 // mimics declaration 'wxChar data[nAllocLength]'
193 wxChar
* data() const { return (wxChar
*)(this + 1); }
195 // empty string has a special ref count so it's never deleted
196 bool IsEmpty() const { return (nRefs
== -1); }
197 bool IsShared() const { return (nRefs
> 1); }
200 void Lock() { if ( !IsEmpty() ) nRefs
++; }
201 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) free(this); }
203 // if we had taken control over string memory (GetWriteBuf), it's
204 // intentionally put in invalid state
205 void Validate(bool b
) { nRefs
= (b
? 1 : 0); }
206 bool IsValid() const { return (nRefs
!= 0); }
209 // ---------------------------------------------------------------------------
210 // This is (yet another one) String class for C++ programmers. It doesn't use
211 // any of "advanced" C++ features (i.e. templates, exceptions, namespaces...)
212 // thus you should be able to compile it with practicaly any C++ compiler.
213 // This class uses copy-on-write technique, i.e. identical strings share the
214 // same memory as long as neither of them is changed.
216 // This class aims to be as compatible as possible with the new standard
217 // std::string class, but adds some additional functions and should be at
218 // least as efficient than the standard implementation.
220 // Performance note: it's more efficient to write functions which take "const
221 // String&" arguments than "const char *" if you assign the argument to
224 // It was compiled and tested under Win32, Linux (libc 5 & 6), Solaris 5.5.
227 // - ressource support (string tables in ressources)
228 // - more wide character (UNICODE) support
229 // - regular expressions support
230 // ---------------------------------------------------------------------------
232 #ifdef WXSTRING_IS_WXOBJECT
233 class WXDLLEXPORT wxString
: public wxObject
235 DECLARE_DYNAMIC_CLASS(wxString
)
236 #else //WXSTRING_IS_WXOBJECT
237 class WXDLLEXPORT wxString
239 #endif //WXSTRING_IS_WXOBJECT
241 friend class WXDLLEXPORT wxArrayString
;
243 // NB: special care was taken in arranging the member functions in such order
244 // that all inline functions can be effectively inlined, verify that all
245 // performace critical functions are still inlined if you change order!
247 // points to data preceded by wxStringData structure with ref count info
250 // accessor to string data
251 wxStringData
* GetStringData() const { return (wxStringData
*)m_pchData
- 1; }
253 // string (re)initialization functions
254 // initializes the string to the empty value (must be called only from
255 // ctors, use Reinit() otherwise)
256 void Init() { m_pchData
= (wxChar
*)wxEmptyString
; }
257 // initializaes the string with (a part of) C-string
258 void InitWith(const wxChar
*psz
, size_t nPos
= 0, size_t nLen
= wxSTRING_MAXLEN
);
259 // as Init, but also frees old data
260 void Reinit() { GetStringData()->Unlock(); Init(); }
263 // allocates memory for string of lenght nLen
264 void AllocBuffer(size_t nLen
);
265 // copies data to another string
266 void AllocCopy(wxString
&, int, int) const;
267 // effectively copies data to string
268 void AssignCopy(size_t, const wxChar
*);
270 // append a (sub)string
271 void ConcatSelf(int nLen
, const wxChar
*src
);
273 // functions called before writing to the string: they copy it if there
274 // are other references to our data (should be the only owner when writing)
275 void CopyBeforeWrite();
276 void AllocBeforeWrite(size_t);
278 // this method is not implemented - there is _no_ conversion from int to
279 // string, you're doing something wrong if the compiler wants to call it!
281 // try `s << i' or `s.Printf("%d", i)' instead
286 // constructors and destructor
287 // ctor for an empty string
288 wxString() { Init(); }
290 wxString(const wxString
& stringSrc
)
292 wxASSERT( stringSrc
.GetStringData()->IsValid() );
294 if ( stringSrc
.IsEmpty() ) {
295 // nothing to do for an empty string
299 m_pchData
= stringSrc
.m_pchData
; // share same data
300 GetStringData()->Lock(); // => one more copy
303 // string containing nRepeat copies of ch
304 wxString(wxChar ch
, size_t nRepeat
= 1);
305 // ctor takes first nLength characters from C string
306 // (default value of wxSTRING_MAXLEN means take all the string)
307 wxString(const wxChar
*psz
, size_t nLength
= wxSTRING_MAXLEN
)
308 { InitWith(psz
, 0, nLength
); }
311 // from multibyte string
312 // (NB: nLength is right now number of Unicode characters, not
313 // characters in psz! So try not to use it yet!)
314 wxString(const char *psz
, wxMBConv
& conv
= wxConvLibc
, size_t nLength
= wxSTRING_MAXLEN
);
315 // from wxWCharBuffer (i.e. return from wxGetString)
316 wxString(const wxWCharBuffer
& psz
)
317 { InitWith(psz
, 0, wxSTRING_MAXLEN
); }
319 // from C string (for compilers using unsigned char)
320 wxString(const unsigned char* psz
, size_t nLength
= wxSTRING_MAXLEN
)
321 { InitWith((const char*)psz
, 0, nLength
); }
322 // from multibyte string
323 wxString(const char *psz
, wxMBConv
& WXUNUSED(conv
) , size_t nLength
= wxSTRING_MAXLEN
)
324 { InitWith(psz
, 0, nLength
); }
327 // from wide (Unicode) string
328 wxString(const wchar_t *pwz
);
329 #endif // !wxUSE_WCHAR_T
332 wxString(const wxCharBuffer
& psz
)
333 { InitWith(psz
, 0, wxSTRING_MAXLEN
); }
334 #endif // Unicode/ANSI
336 // dtor is not virtual, this class must not be inherited from!
337 ~wxString() { GetStringData()->Unlock(); }
339 // generic attributes & operations
340 // as standard strlen()
341 size_t Len() const { return GetStringData()->nDataLength
; }
342 // string contains any characters?
343 bool IsEmpty() const { return Len() == 0; }
344 // empty string is "FALSE", so !str will return TRUE
345 bool operator!() const { return IsEmpty(); }
346 // empty string contents
353 wxASSERT( GetStringData()->nDataLength
== 0 );
355 // empty the string and free memory
358 if ( !GetStringData()->IsEmpty() )
361 wxASSERT( GetStringData()->nDataLength
== 0 ); // should be empty
362 wxASSERT( GetStringData()->nAllocLength
== 0 ); // and not own any memory
367 bool IsAscii() const;
369 bool IsNumber() const;
373 // data access (all indexes are 0 based)
375 wxChar
GetChar(size_t n
) const
376 { ASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
378 wxChar
& GetWritableChar(size_t n
)
379 { ASSERT_VALID_INDEX( n
); CopyBeforeWrite(); return m_pchData
[n
]; }
381 void SetChar(size_t n
, wxChar ch
)
382 { ASSERT_VALID_INDEX( n
); CopyBeforeWrite(); m_pchData
[n
] = ch
; }
384 // get last character
386 { wxASSERT( !IsEmpty() ); return m_pchData
[Len() - 1]; }
387 // get writable last character
389 { wxASSERT( !IsEmpty() ); CopyBeforeWrite(); return m_pchData
[Len()-1]; }
391 // operator version of GetChar
392 wxChar
operator[](size_t n
) const
393 { ASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
395 // operator version of GetChar
396 wxChar
operator[](int n
) const
397 { ASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
399 // operator version of GetChar
400 wxChar
operator[](unsigned int n
) const
401 { ASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
404 // operator version of GetWriteableChar
405 wxChar
& operator[](size_t n
)
406 { ASSERT_VALID_INDEX( n
); CopyBeforeWrite(); return m_pchData
[n
]; }
408 // operator version of GetWriteableChar
409 wxChar
& operator[](unsigned int n
)
410 { ASSERT_VALID_INDEX( n
); CopyBeforeWrite(); return m_pchData
[n
]; }
413 // implicit conversion to C string
414 operator const wxChar
*() const { return m_pchData
; }
415 // explicit conversion to C string (use this with printf()!)
416 const wxChar
* c_str() const { return m_pchData
; }
417 // (and this with [wx]Printf()!)
418 const wxChar
* wx_str() const { return m_pchData
; }
419 // identical to c_str()
420 const wxChar
* GetData() const { return m_pchData
; }
422 // conversions with (possible) format convertions: have to return a
423 // buffer with temporary data
425 const wxCharBuffer
mb_str(wxMBConv
& conv
= wxConvLibc
) const { return conv
.cWC2MB(m_pchData
); }
426 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
428 const wxChar
* wc_str(wxMBConv
& WXUNUSED(conv
) = wxConvLibc
) const { return m_pchData
; }
431 const wxCharBuffer
fn_str() const { return mb_str(wxConvFile
); }
433 const wxChar
* fn_str() const { return m_pchData
; }
434 #endif // wxMBFILES/!wxMBFILES
437 const wxChar
* mb_str(wxMBConv
& WXUNUSED(conv
) = wxConvLibc
) const
438 { return m_pchData
; }
439 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
441 const wxChar
* mb_str() const { return m_pchData
; }
442 const wxWX2MBbuf
mbc_str() const { return mb_str(); }
443 #endif // multibyte/!multibyte
445 const wxWCharBuffer
wc_str(wxMBConv
& conv
) const { return conv
.cMB2WC(m_pchData
); }
446 #endif // wxUSE_WCHAR_T
447 const wxChar
* fn_str() const { return m_pchData
; }
448 #endif // Unicode/ANSI
450 // overloaded assignment
451 // from another wxString
452 wxString
& operator=(const wxString
& stringSrc
);
454 wxString
& operator=(wxChar ch
);
456 wxString
& operator=(const wxChar
*psz
);
458 // from wxWCharBuffer
459 wxString
& operator=(const wxWCharBuffer
& psz
) { return operator=((const wchar_t *)psz
); }
461 // from another kind of C string
462 wxString
& operator=(const unsigned char* psz
);
464 // from a wide string
465 wxString
& operator=(const wchar_t *pwz
);
468 wxString
& operator=(const wxCharBuffer
& psz
) { return operator=((const char *)psz
); }
469 #endif // Unicode/ANSI
471 // string concatenation
472 // in place concatenation
474 Concatenate and return the result. Note that the left to right
475 associativity of << allows to write things like "str << str1 << str2
476 << ..." (unlike with +=)
479 wxString
& operator<<(const wxString
& s
)
481 wxASSERT( s
.GetStringData()->IsValid() );
483 ConcatSelf(s
.Len(), s
);
486 // string += C string
487 wxString
& operator<<(const wxChar
*psz
)
488 { ConcatSelf(wxStrlen(psz
), psz
); return *this; }
490 wxString
& operator<<(wxChar ch
) { ConcatSelf(1, &ch
); return *this; }
493 void operator+=(const wxString
& s
) { (void)operator<<(s
); }
494 // string += C string
495 void operator+=(const wxChar
*psz
) { (void)operator<<(psz
); }
497 void operator+=(wxChar ch
) { (void)operator<<(ch
); }
499 // string += buffer (i.e. from wxGetString)
501 wxString
& operator<<(const wxWCharBuffer
& s
) { (void)operator<<((const wchar_t *)s
); return *this; }
502 void operator+=(const wxWCharBuffer
& s
) { (void)operator<<((const wchar_t *)s
); }
504 wxString
& operator<<(const wxCharBuffer
& s
) { (void)operator<<((const char *)s
); return *this; }
505 void operator+=(const wxCharBuffer
& s
) { (void)operator<<((const char *)s
); }
508 // string += C string
509 wxString
& Append(const wxChar
* psz
)
510 { ConcatSelf(wxStrlen(psz
), psz
); return *this; }
511 // append count copies of given character
512 wxString
& Append(wxChar ch
, size_t count
= 1u)
513 { wxString
str(ch
, count
); return *this << str
; }
515 // prepend a string, return the string itself
516 wxString
& Prepend(const wxString
& str
)
517 { *this = str
+ *this; return *this; }
519 // non-destructive concatenation
521 friend wxString WXDLLEXPORT
operator+(const wxString
& string1
, const wxString
& string2
);
523 friend wxString WXDLLEXPORT
operator+(const wxString
& string
, wxChar ch
);
525 friend wxString WXDLLEXPORT
operator+(wxChar ch
, const wxString
& string
);
527 friend wxString WXDLLEXPORT
operator+(const wxString
& string
, const wxChar
*psz
);
529 friend wxString WXDLLEXPORT
operator+(const wxChar
*psz
, const wxString
& string
);
531 // stream-like functions
532 // insert an int into string
533 wxString
& operator<<(int i
);
534 // insert a float into string
535 wxString
& operator<<(float f
);
536 // insert a double into string
537 wxString
& operator<<(double d
);
540 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
541 int Cmp(const wxChar
*psz
) const { return wxStrcmp(c_str(), psz
); }
542 // same as Cmp() but not case-sensitive
543 int CmpNoCase(const wxChar
*psz
) const { return wxStricmp(c_str(), psz
); }
544 // test for the string equality, either considering case or not
545 // (if compareWithCase then the case matters)
546 bool IsSameAs(const wxChar
*psz
, bool compareWithCase
= TRUE
) const
547 { return (compareWithCase
? Cmp(psz
) : CmpNoCase(psz
)) == 0; }
548 // comparison with a signle character: returns TRUE if equal
549 bool IsSameAs(wxChar c
, bool compareWithCase
= TRUE
) const
551 return (Len() == 1) && (compareWithCase
? GetChar(0u) == c
552 : wxToupper(GetChar(0u)) == wxToupper(c
));
555 // simple sub-string extraction
556 // return substring starting at nFirst of length nCount (or till the end
557 // if nCount = default value)
558 wxString
Mid(size_t nFirst
, size_t nCount
= wxSTRING_MAXLEN
) const;
560 // operator version of Mid()
561 wxString
operator()(size_t start
, size_t len
) const
562 { return Mid(start
, len
); }
564 // get first nCount characters
565 wxString
Left(size_t nCount
) const;
566 // get last nCount characters
567 wxString
Right(size_t nCount
) const;
568 // get all characters before the first occurence of ch
569 // (returns the whole string if ch not found)
570 wxString
BeforeFirst(wxChar ch
) const;
571 // get all characters before the last occurence of ch
572 // (returns empty string if ch not found)
573 wxString
BeforeLast(wxChar ch
) const;
574 // get all characters after the first occurence of ch
575 // (returns empty string if ch not found)
576 wxString
AfterFirst(wxChar ch
) const;
577 // get all characters after the last occurence of ch
578 // (returns the whole string if ch not found)
579 wxString
AfterLast(wxChar ch
) const;
581 // for compatibility only, use more explicitly named functions above
582 wxString
Before(wxChar ch
) const { return BeforeLast(ch
); }
583 wxString
After(wxChar ch
) const { return AfterFirst(ch
); }
586 // convert to upper case in place, return the string itself
587 wxString
& MakeUpper();
588 // convert to upper case, return the copy of the string
589 // Here's something to remember: BC++ doesn't like returns in inlines.
590 wxString
Upper() const ;
591 // convert to lower case in place, return the string itself
592 wxString
& MakeLower();
593 // convert to lower case, return the copy of the string
594 wxString
Lower() const ;
596 // trimming/padding whitespace (either side) and truncating
597 // remove spaces from left or from right (default) side
598 wxString
& Trim(bool bFromRight
= TRUE
);
599 // add nCount copies chPad in the beginning or at the end (default)
600 wxString
& Pad(size_t nCount
, wxChar chPad
= wxT(' '), bool bFromRight
= TRUE
);
601 // truncate string to given length
602 wxString
& Truncate(size_t uiLen
);
604 // searching and replacing
605 // searching (return starting index, or -1 if not found)
606 int Find(wxChar ch
, bool bFromEnd
= FALSE
) const; // like strchr/strrchr
607 // searching (return starting index, or -1 if not found)
608 int Find(const wxChar
*pszSub
) const; // like strstr
609 // replace first (or all of bReplaceAll) occurences of substring with
610 // another string, returns the number of replacements made
611 size_t Replace(const wxChar
*szOld
,
613 bool bReplaceAll
= TRUE
);
615 // check if the string contents matches a mask containing '*' and '?'
616 bool Matches(const wxChar
*szMask
) const;
618 // conversion to numbers: all functions return TRUE only if the whole string
619 // is a number and put the value of this number into the pointer provided
620 // convert to a signed integer
621 bool ToLong(long *val
) const;
622 // convert to an unsigned integer
623 bool ToULong(unsigned long *val
) const;
624 // convert to a double
625 bool ToDouble(double *val
) const;
627 // formated input/output
628 // as sprintf(), returns the number of characters written or < 0 on error
629 int Printf(const wxChar
*pszFormat
, ...);
630 // as vprintf(), returns the number of characters written or < 0 on error
631 int PrintfV(const wxChar
* pszFormat
, va_list argptr
);
633 // raw access to string memory
634 // ensure that string has space for at least nLen characters
635 // only works if the data of this string is not shared
636 void Alloc(size_t nLen
);
637 // minimize the string's memory
638 // only works if the data of this string is not shared
640 // get writable buffer of at least nLen bytes. Unget() *must* be called
641 // a.s.a.p. to put string back in a reasonable state!
642 wxChar
*GetWriteBuf(size_t nLen
);
643 // call this immediately after GetWriteBuf() has been used
644 void UngetWriteBuf();
646 // wxWindows version 1 compatibility functions
649 wxString
SubString(size_t from
, size_t to
) const
650 { return Mid(from
, (to
- from
+ 1)); }
651 // values for second parameter of CompareTo function
652 enum caseCompare
{exact
, ignoreCase
};
653 // values for first parameter of Strip function
654 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
657 int sprintf(const wxChar
*pszFormat
, ...);
660 inline int CompareTo(const wxChar
* psz
, caseCompare cmp
= exact
) const
661 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
664 size_t Length() const { return Len(); }
665 // Count the number of characters
666 int Freq(wxChar ch
) const;
668 void LowerCase() { MakeLower(); }
670 void UpperCase() { MakeUpper(); }
671 // use Trim except that it doesn't change this string
672 wxString
Strip(stripType w
= trailing
) const;
674 // use Find (more general variants not yet supported)
675 size_t Index(const wxChar
* psz
) const { return Find(psz
); }
676 size_t Index(wxChar ch
) const { return Find(ch
); }
678 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
679 wxString
& RemoveLast() { return Truncate(Len() - 1); }
681 wxString
& Remove(size_t nStart
, size_t nLen
) { return erase( nStart
, nLen
); }
684 int First( const wxChar ch
) const { return Find(ch
); }
685 int First( const wxChar
* psz
) const { return Find(psz
); }
686 int First( const wxString
&str
) const { return Find(str
); }
687 int Last( const wxChar ch
) const { return Find(ch
, TRUE
); }
688 bool Contains(const wxString
& str
) const { return Find(str
) != -1; }
691 bool IsNull() const { return IsEmpty(); }
693 #ifdef wxSTD_STRING_COMPATIBILITY
694 // std::string compatibility functions
697 typedef wxChar value_type
;
698 typedef const value_type
*const_iterator
;
700 // an 'invalid' value for string index
701 static const size_t npos
;
704 // take nLen chars starting at nPos
705 wxString(const wxString
& str
, size_t nPos
, size_t nLen
)
707 wxASSERT( str
.GetStringData()->IsValid() );
708 InitWith(str
.c_str(), nPos
, nLen
== npos
? 0 : nLen
);
710 // take all characters from pStart to pEnd
711 wxString(const void *pStart
, const void *pEnd
);
713 // lib.string.capacity
714 // return the length of the string
715 size_t size() const { return Len(); }
716 // return the length of the string
717 size_t length() const { return Len(); }
718 // return the maximum size of the string
719 size_t max_size() const { return wxSTRING_MAXLEN
; }
720 // resize the string, filling the space with c if c != 0
721 void resize(size_t nSize
, wxChar ch
= wxT('\0'));
722 // delete the contents of the string
723 void clear() { Empty(); }
724 // returns true if the string is empty
725 bool empty() const { return IsEmpty(); }
726 // inform string about planned change in size
727 void reserve(size_t size
) { Alloc(size
); }
730 // return the character at position n
731 wxChar
at(size_t n
) const { return GetChar(n
); }
732 // returns the writable character at position n
733 wxChar
& at(size_t n
) { return GetWritableChar(n
); }
735 // first valid index position
736 const_iterator
begin() const { return wx_str(); }
737 // position one after the last valid one
738 const_iterator
end() const { return wx_str() + length(); }
740 // lib.string.modifiers
742 wxString
& append(const wxString
& str
)
743 { *this += str
; return *this; }
744 // append elements str[pos], ..., str[pos+n]
745 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
746 { ConcatSelf(n
, str
.c_str() + pos
); return *this; }
747 // append first n (or all if n == npos) characters of sz
748 wxString
& append(const wxChar
*sz
, size_t n
= npos
)
749 { ConcatSelf(n
== npos
? wxStrlen(sz
) : n
, sz
); return *this; }
751 // append n copies of ch
752 wxString
& append(size_t n
, wxChar ch
) { return Pad(n
, ch
); }
754 // same as `this_string = str'
755 wxString
& assign(const wxString
& str
) { return (*this) = str
; }
756 // same as ` = str[pos..pos + n]
757 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
758 { return *this = wxString((const wxChar
*)str
+ pos
, n
); }
759 // same as `= first n (or all if n == npos) characters of sz'
760 wxString
& assign(const wxChar
*sz
, size_t n
= npos
)
761 { return *this = wxString(sz
, n
); }
762 // same as `= n copies of ch'
763 wxString
& assign(size_t n
, wxChar ch
)
764 { return *this = wxString(ch
, n
); }
766 // insert another string
767 wxString
& insert(size_t nPos
, const wxString
& str
);
768 // insert n chars of str starting at nStart (in str)
769 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
770 { return insert(nPos
, wxString((const wxChar
*)str
+ nStart
, n
)); }
772 // insert first n (or all if n == npos) characters of sz
773 wxString
& insert(size_t nPos
, const wxChar
*sz
, size_t n
= npos
)
774 { return insert(nPos
, wxString(sz
, n
)); }
775 // insert n copies of ch
776 wxString
& insert(size_t nPos
, size_t n
, wxChar ch
)
777 { return insert(nPos
, wxString(ch
, n
)); }
779 // delete characters from nStart to nStart + nLen
780 wxString
& erase(size_t nStart
= 0, size_t nLen
= npos
);
782 // replaces the substring of length nLen starting at nStart
783 wxString
& replace(size_t nStart
, size_t nLen
, const wxChar
* sz
);
784 // replaces the substring with nCount copies of ch
785 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxChar ch
);
786 // replaces a substring with another substring
787 wxString
& replace(size_t nStart
, size_t nLen
,
788 const wxString
& str
, size_t nStart2
, size_t nLen2
);
789 // replaces the substring with first nCount chars of sz
790 wxString
& replace(size_t nStart
, size_t nLen
,
791 const wxChar
* sz
, size_t nCount
);
794 void swap(wxString
& str
);
796 // All find() functions take the nStart argument which specifies the
797 // position to start the search on, the default value is 0. All functions
798 // return npos if there were no match.
801 size_t find(const wxString
& str
, size_t nStart
= 0) const;
803 // VC++ 1.5 can't cope with this syntax.
804 #if !defined(__VISUALC__) || defined(__WIN32__)
805 // find first n characters of sz
806 size_t find(const wxChar
* sz
, size_t nStart
= 0, size_t n
= npos
) const;
809 // Gives a duplicate symbol (presumably a case-insensitivity problem)
810 #if !defined(__BORLANDC__)
811 // find the first occurence of character ch after nStart
812 size_t find(wxChar ch
, size_t nStart
= 0) const;
814 // rfind() family is exactly like find() but works right to left
816 // as find, but from the end
817 size_t rfind(const wxString
& str
, size_t nStart
= npos
) const;
819 // VC++ 1.5 can't cope with this syntax.
820 #if !defined(__VISUALC__) || defined(__WIN32__)
821 // as find, but from the end
822 size_t rfind(const wxChar
* sz
, size_t nStart
= npos
,
823 size_t n
= npos
) const;
824 // as find, but from the end
825 size_t rfind(wxChar ch
, size_t nStart
= npos
) const;
828 // find first/last occurence of any character in the set
830 // as strpbrk() but starts at nStart, returns npos if not found
831 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
832 { return find_first_of(str
.c_str(), nStart
); }
834 size_t find_first_of(const wxChar
* sz
, size_t nStart
= 0) const;
835 // same as find(char, size_t)
836 size_t find_first_of(wxChar c
, size_t nStart
= 0) const
837 { return find(c
, nStart
); }
838 // find the last (starting from nStart) char from str in this string
839 size_t find_last_of (const wxString
& str
, size_t nStart
= npos
) const
840 { return find_last_of(str
.c_str(), nStart
); }
842 size_t find_last_of (const wxChar
* sz
, size_t nStart
= npos
) const;
844 size_t find_last_of(wxChar c
, size_t nStart
= npos
) const
845 { return rfind(c
, nStart
); }
847 // find first/last occurence of any character not in the set
849 // as strspn() (starting from nStart), returns npos on failure
850 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
851 { return find_first_not_of(str
.c_str(), nStart
); }
853 size_t find_first_not_of(const wxChar
* sz
, size_t nStart
= 0) const;
855 size_t find_first_not_of(wxChar ch
, size_t nStart
= 0) const;
857 size_t find_last_not_of(const wxString
& str
, size_t nStart
=npos
) const;
859 size_t find_last_not_of(const wxChar
* sz
, size_t nStart
= npos
) const;
861 size_t find_last_not_of(wxChar ch
, size_t nStart
= npos
) const;
863 // All compare functions return -1, 0 or 1 if the [sub]string is less,
864 // equal or greater than the compare() argument.
866 // just like strcmp()
867 int compare(const wxString
& str
) const { return Cmp(str
); }
868 // comparison with a substring
869 int compare(size_t nStart
, size_t nLen
, const wxString
& str
) const;
870 // comparison of 2 substrings
871 int compare(size_t nStart
, size_t nLen
,
872 const wxString
& str
, size_t nStart2
, size_t nLen2
) const;
873 // just like strcmp()
874 int compare(const wxChar
* sz
) const { return Cmp(sz
); }
875 // substring comparison with first nCount characters of sz
876 int compare(size_t nStart
, size_t nLen
,
877 const wxChar
* sz
, size_t nCount
= npos
) const;
879 // substring extraction
880 wxString
substr(size_t nStart
= 0, size_t nLen
= npos
) const
881 { return Mid(nStart
, nLen
); }
882 #endif // wxSTD_STRING_COMPATIBILITY
885 // ----------------------------------------------------------------------------
886 // The string array uses it's knowledge of internal structure of the wxString
887 // class to optimize string storage. Normally, we would store pointers to
888 // string, but as wxString is, in fact, itself a pointer (sizeof(wxString) is
889 // sizeof(char *)) we store these pointers instead. The cast to "wxString *" is
890 // really all we need to turn such pointer into a string!
892 // Of course, it can be called a dirty hack, but we use twice less memory and
893 // this approach is also more speed efficient, so it's probably worth it.
895 // Usage notes: when a string is added/inserted, a new copy of it is created,
896 // so the original string may be safely deleted. When a string is retrieved
897 // from the array (operator[] or Item() method), a reference is returned.
898 // ----------------------------------------------------------------------------
900 class WXDLLEXPORT wxArrayString
903 // type of function used by wxArrayString::Sort()
904 typedef int (*CompareFunction
)(const wxString
& first
,
905 const wxString
& second
);
907 // constructors and destructor
908 // default ctor: if autoSort is TRUE, the array is always sorted (in
909 // alphabetical order)
910 wxArrayString(bool autoSort
= FALSE
);
912 wxArrayString(const wxArrayString
& array
);
913 // assignment operator
914 wxArrayString
& operator=(const wxArrayString
& src
);
915 // not virtual, this class should not be derived from
919 // empties the list, but doesn't release memory
921 // empties the list and releases memory
923 // preallocates memory for given number of items
924 void Alloc(size_t nCount
);
925 // minimzes the memory usage (by freeing all extra memory)
929 // number of elements in the array
930 size_t GetCount() const { return m_nCount
; }
932 bool IsEmpty() const { return m_nCount
== 0; }
933 // number of elements in the array (GetCount is preferred API)
934 size_t Count() const { return m_nCount
; }
936 // items access (range checking is done in debug version)
937 // get item at position uiIndex
938 wxString
& Item(size_t nIndex
) const
939 { wxASSERT( nIndex
< m_nCount
); return *(wxString
*)&(m_pItems
[nIndex
]); }
941 wxString
& operator[](size_t nIndex
) const { return Item(nIndex
); }
943 wxString
& Last() const { wxASSERT( !IsEmpty() ); return Item(Count() - 1); }
946 // Search the element in the array, starting from the beginning if
947 // bFromEnd is FALSE or from end otherwise. If bCase, comparison is case
948 // sensitive (default). Returns index of the first item matched or
950 int Index (const wxChar
*sz
, bool bCase
= TRUE
, bool bFromEnd
= FALSE
) const;
951 // add new element at the end (if the array is not sorted), return its
953 size_t Add(const wxString
& str
);
954 // add new element at given position
955 void Insert(const wxString
& str
, size_t uiIndex
);
956 // remove first item matching this value
957 void Remove(const wxChar
*sz
);
958 // remove item by index
959 void Remove(size_t nIndex
);
962 // sort array elements in alphabetical order (or reversed alphabetical
963 // order if reverseOrder parameter is TRUE)
964 void Sort(bool reverseOrder
= FALSE
);
965 // sort array elements using specified comparaison function
966 void Sort(CompareFunction compareFunction
);
969 void Copy(const wxArrayString
& src
); // copies the contents of another array
972 void Grow(); // makes array bigger if needed
973 void Free(); // free all the strings stored
975 void DoSort(); // common part of all Sort() variants
977 size_t m_nSize
, // current size of the array
978 m_nCount
; // current number of elements
980 wxChar
**m_pItems
; // pointer to data
982 bool m_autoSort
; // if TRUE, keep the array always sorted
985 class WXDLLEXPORT wxSortedArrayString
: public wxArrayString
988 wxSortedArrayString() : wxArrayString(TRUE
)
990 wxSortedArrayString(const wxArrayString
& array
) : wxArrayString(TRUE
)
994 // ---------------------------------------------------------------------------
995 // wxString comparison functions: operator versions are always case sensitive
996 // ---------------------------------------------------------------------------
999 inline bool operator==(const wxString
& s1
, const wxString
& s2
) { return (s1
.Cmp(s2
) == 0); }
1001 inline bool operator==(const wxString
& s1
, const wxChar
* s2
) { return (s1
.Cmp(s2
) == 0); }
1003 inline bool operator==(const wxChar
* s1
, const wxString
& s2
) { return (s2
.Cmp(s1
) == 0); }
1005 inline bool operator!=(const wxString
& s1
, const wxString
& s2
) { return (s1
.Cmp(s2
) != 0); }
1007 inline bool operator!=(const wxString
& s1
, const wxChar
* s2
) { return (s1
.Cmp(s2
) != 0); }
1009 inline bool operator!=(const wxChar
* s1
, const wxString
& s2
) { return (s2
.Cmp(s1
) != 0); }
1011 inline bool operator< (const wxString
& s1
, const wxString
& s2
) { return (s1
.Cmp(s2
) < 0); }
1013 inline bool operator< (const wxString
& s1
, const wxChar
* s2
) { return (s1
.Cmp(s2
) < 0); }
1015 inline bool operator< (const wxChar
* s1
, const wxString
& s2
) { return (s2
.Cmp(s1
) > 0); }
1017 inline bool operator> (const wxString
& s1
, const wxString
& s2
) { return (s1
.Cmp(s2
) > 0); }
1019 inline bool operator> (const wxString
& s1
, const wxChar
* s2
) { return (s1
.Cmp(s2
) > 0); }
1021 inline bool operator> (const wxChar
* s1
, const wxString
& s2
) { return (s2
.Cmp(s1
) < 0); }
1023 inline bool operator<=(const wxString
& s1
, const wxString
& s2
) { return (s1
.Cmp(s2
) <= 0); }
1025 inline bool operator<=(const wxString
& s1
, const wxChar
* s2
) { return (s1
.Cmp(s2
) <= 0); }
1027 inline bool operator<=(const wxChar
* s1
, const wxString
& s2
) { return (s2
.Cmp(s1
) >= 0); }
1029 inline bool operator>=(const wxString
& s1
, const wxString
& s2
) { return (s1
.Cmp(s2
) >= 0); }
1031 inline bool operator>=(const wxString
& s1
, const wxChar
* s2
) { return (s1
.Cmp(s2
) >= 0); }
1033 inline bool operator>=(const wxChar
* s1
, const wxString
& s2
) { return (s2
.Cmp(s1
) <= 0); }
1035 // comparison with char
1036 inline bool operator==(wxChar c
, const wxString
& s
) { return s
.IsSameAs(c
); }
1037 inline bool operator==(const wxString
& s
, wxChar c
) { return s
.IsSameAs(c
); }
1038 inline bool operator!=(wxChar c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
1039 inline bool operator!=(const wxString
& s
, wxChar c
) { return !s
.IsSameAs(c
); }
1042 inline bool operator==(const wxString
& s1
, const wxWCharBuffer
& s2
)
1043 { return (s1
.Cmp((const wchar_t *)s2
) == 0); }
1044 inline bool operator==(const wxWCharBuffer
& s1
, const wxString
& s2
)
1045 { return (s2
.Cmp((const wchar_t *)s1
) == 0); }
1047 inline bool operator==(const wxString
& s1
, const wxCharBuffer
& s2
)
1048 { return (s1
.Cmp((const char *)s2
) == 0); }
1049 inline bool operator==(const wxCharBuffer
& s1
, const wxString
& s2
)
1050 { return (s2
.Cmp((const char *)s1
) == 0); }
1053 wxString WXDLLEXPORT
operator+(const wxString
& string1
, const wxString
& string2
);
1054 wxString WXDLLEXPORT
operator+(const wxString
& string
, wxChar ch
);
1055 wxString WXDLLEXPORT
operator+(wxChar ch
, const wxString
& string
);
1056 wxString WXDLLEXPORT
operator+(const wxString
& string
, const wxChar
*psz
);
1057 wxString WXDLLEXPORT
operator+(const wxChar
*psz
, const wxString
& string
);
1059 inline wxString WXDLLEXPORT
operator+(const wxString
& string
, const wxWCharBuffer
& buf
)
1060 { return string
+ (const wchar_t *)buf
; }
1061 inline wxString WXDLLEXPORT
operator+(const wxWCharBuffer
& buf
, const wxString
& string
)
1062 { return (const wchar_t *)buf
+ string
; }
1064 inline wxString WXDLLEXPORT
operator+(const wxString
& string
, const wxCharBuffer
& buf
)
1065 { return string
+ (const char *)buf
; }
1066 inline wxString WXDLLEXPORT
operator+(const wxCharBuffer
& buf
, const wxString
& string
)
1067 { return (const char *)buf
+ string
; }
1070 // ---------------------------------------------------------------------------
1071 // Implementation only from here until the end of file
1072 // ---------------------------------------------------------------------------
1074 // don't pollute the library user's name space
1075 #undef ASSERT_VALID_INDEX
1077 #if defined(wxSTD_STRING_COMPATIBILITY) && wxUSE_STD_IOSTREAM
1079 #include "wx/ioswrap.h"
1081 WXDLLEXPORT istream
& operator>>(istream
&, wxString
&);
1082 WXDLLEXPORT ostream
& operator<<(ostream
&, const wxString
&);
1084 #endif // wxSTD_STRING_COMPATIBILITY
1086 #endif // _WX_WXSTRINGH__