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 // ----------------------------------------------------------------------------
56 #include <strings.h> // for strcasecmp()
59 #include "wx/defs.h" // everybody should include this
60 #include "wx/debug.h" // for wxASSERT()
61 #include "wx/wxchar.h" // for wxChar
62 #include "wx/buffer.h" // for wxCharBuffer
63 #include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes
66 #ifdef WXSTRING_IS_WXOBJECT
67 #include "wx/object.h" // base class
71 // ---------------------------------------------------------------------------
73 // ---------------------------------------------------------------------------
76 #define WXSTRINGCAST (wxChar *)(const wxChar *)
77 #define wxCSTRINGCAST (wxChar *)(const wxChar *)
78 #define wxMBSTRINGCAST (char *)(const char *)
79 #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *)
81 // implementation only
82 #define ASSERT_VALID_INDEX(i) wxASSERT( (unsigned)(i) <= Len() )
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 // maximum possible length for a string means "take all string" everywhere
89 // (as sizeof(StringData) is unknown here, we substract 100)
90 const unsigned int wxSTRING_MAXLEN
= UINT_MAX
- 100;
92 // ----------------------------------------------------------------------------
94 // ----------------------------------------------------------------------------
96 // global pointer to empty string
97 WXDLLEXPORT_DATA(extern const wxChar
*) wxEmptyString
;
99 // ---------------------------------------------------------------------------
100 // global functions complementing standard C string library replacements for
101 // strlen() and portable strcasecmp()
102 //---------------------------------------------------------------------------
104 // Use wxXXX() functions from wxchar.h instead! These functions are for
105 // backwards compatibility only.
107 // checks whether the passed in pointer is NULL and if the string is empty
108 inline bool WXDLLEXPORT
IsEmpty(const char *p
) { return (!p
|| !*p
); }
110 // safe version of strlen() (returns 0 if passed NULL pointer)
111 inline size_t WXDLLEXPORT
Strlen(const char *psz
)
112 { return psz
? strlen(psz
) : 0; }
114 // portable strcasecmp/_stricmp
115 inline int WXDLLEXPORT
Stricmp(const char *psz1
, const char *psz2
)
117 #if defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) )
118 return _stricmp(psz1
, psz2
);
119 #elif defined(__SC__)
120 return _stricmp(psz1
, psz2
);
121 #elif defined(__SALFORDC__)
122 return stricmp(psz1
, psz2
);
123 #elif defined(__BORLANDC__)
124 return stricmp(psz1
, psz2
);
125 #elif defined(__WATCOMC__)
126 return stricmp(psz1
, psz2
);
127 #elif defined(__EMX__)
128 return stricmp(psz1
, psz2
);
129 #elif defined(__WXPM__)
130 return stricmp(psz1
, psz2
);
131 #elif defined(__UNIX__) || defined(__GNUWIN32__)
132 return strcasecmp(psz1
, psz2
);
133 #elif defined(__MWERKS__) && !defined(__INTEL__)
134 register char c1
, c2
;
136 c1
= tolower(*psz1
++);
137 c2
= tolower(*psz2
++);
138 } while ( c1
&& (c1
== c2
) );
142 // almost all compilers/libraries provide this function (unfortunately under
143 // different names), that's why we don't implement our own which will surely
144 // be more efficient than this code (uncomment to use):
146 register char c1, c2;
148 c1 = tolower(*psz1++);
149 c2 = tolower(*psz2++);
150 } while ( c1 && (c1 == c2) );
155 #error "Please define string case-insensitive compare for your OS/compiler"
156 #endif // OS/compiler
159 // wxSnprintf() is like snprintf() if it's available and sprintf() (always
160 // available, but dangerous!) if not
161 extern int WXDLLEXPORT
wxSnprintf(wxChar
*buf
, size_t len
,
162 const wxChar
*format
, ...);
164 // and wxVsnprintf() is like vsnprintf() or vsprintf()
165 extern int WXDLLEXPORT
wxVsnprintf(wxChar
*buf
, size_t len
,
166 const wxChar
*format
, va_list argptr
);
168 // return an empty wxString
169 class WXDLLEXPORT wxString
; // not yet defined
170 inline const wxString
& wxGetEmptyString() { return *(wxString
*)&wxEmptyString
; }
172 // ---------------------------------------------------------------------------
173 // string data prepended with some housekeeping info (used by wxString class),
174 // is never used directly (but had to be put here to allow inlining)
175 // ---------------------------------------------------------------------------
177 struct WXDLLEXPORT wxStringData
179 int nRefs
; // reference count
180 size_t nDataLength
, // actual string length
181 nAllocLength
; // allocated memory size
183 // mimics declaration 'wxChar data[nAllocLength]'
184 wxChar
* data() const { return (wxChar
*)(this + 1); }
186 // empty string has a special ref count so it's never deleted
187 bool IsEmpty() const { return (nRefs
== -1); }
188 bool IsShared() const { return (nRefs
> 1); }
191 void Lock() { if ( !IsEmpty() ) nRefs
++; }
192 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) free(this); }
194 // if we had taken control over string memory (GetWriteBuf), it's
195 // intentionally put in invalid state
196 void Validate(bool b
) { nRefs
= (b
? 1 : 0); }
197 bool IsValid() const { return (nRefs
!= 0); }
200 // ---------------------------------------------------------------------------
201 // This is (yet another one) String class for C++ programmers. It doesn't use
202 // any of "advanced" C++ features (i.e. templates, exceptions, namespaces...)
203 // thus you should be able to compile it with practicaly any C++ compiler.
204 // This class uses copy-on-write technique, i.e. identical strings share the
205 // same memory as long as neither of them is changed.
207 // This class aims to be as compatible as possible with the new standard
208 // std::string class, but adds some additional functions and should be at
209 // least as efficient than the standard implementation.
211 // Performance note: it's more efficient to write functions which take "const
212 // String&" arguments than "const char *" if you assign the argument to
215 // It was compiled and tested under Win32, Linux (libc 5 & 6), Solaris 5.5.
218 // - ressource support (string tables in ressources)
219 // - more wide character (UNICODE) support
220 // - regular expressions support
221 // ---------------------------------------------------------------------------
223 #ifdef WXSTRING_IS_WXOBJECT
224 class WXDLLEXPORT wxString
: public wxObject
226 DECLARE_DYNAMIC_CLASS(wxString
)
227 #else //WXSTRING_IS_WXOBJECT
228 class WXDLLEXPORT wxString
230 #endif //WXSTRING_IS_WXOBJECT
232 friend class WXDLLEXPORT wxArrayString
;
234 // NB: special care was taken in arranging the member functions in such order
235 // that all inline functions can be effectively inlined, verify that all
236 // performace critical functions are still inlined if you change order!
238 // points to data preceded by wxStringData structure with ref count info
241 // accessor to string data
242 wxStringData
* GetStringData() const { return (wxStringData
*)m_pchData
- 1; }
244 // string (re)initialization functions
245 // initializes the string to the empty value (must be called only from
246 // ctors, use Reinit() otherwise)
247 void Init() { m_pchData
= (wxChar
*)wxEmptyString
; }
248 // initializaes the string with (a part of) C-string
249 void InitWith(const wxChar
*psz
, size_t nPos
= 0, size_t nLen
= wxSTRING_MAXLEN
);
250 // as Init, but also frees old data
251 void Reinit() { GetStringData()->Unlock(); Init(); }
254 // allocates memory for string of lenght nLen
255 void AllocBuffer(size_t nLen
);
256 // copies data to another string
257 void AllocCopy(wxString
&, int, int) const;
258 // effectively copies data to string
259 void AssignCopy(size_t, const wxChar
*);
261 // append a (sub)string
262 void ConcatSelf(int nLen
, const wxChar
*src
);
264 // functions called before writing to the string: they copy it if there
265 // are other references to our data (should be the only owner when writing)
266 void CopyBeforeWrite();
267 void AllocBeforeWrite(size_t);
269 // this method is not implemented - there is _no_ conversion from int to
270 // string, you're doing something wrong if the compiler wants to call it!
272 // try `s << i' or `s.Printf("%d", i)' instead
277 // constructors and destructor
278 // ctor for an empty string
279 wxString() { Init(); }
281 wxString(const wxString
& stringSrc
)
283 wxASSERT( stringSrc
.GetStringData()->IsValid() );
285 if ( stringSrc
.IsEmpty() ) {
286 // nothing to do for an empty string
290 m_pchData
= stringSrc
.m_pchData
; // share same data
291 GetStringData()->Lock(); // => one more copy
294 // string containing nRepeat copies of ch
295 wxString(wxChar ch
, size_t nRepeat
= 1);
296 // ctor takes first nLength characters from C string
297 // (default value of wxSTRING_MAXLEN means take all the string)
298 wxString(const wxChar
*psz
, size_t nLength
= wxSTRING_MAXLEN
)
299 { InitWith(psz
, 0, nLength
); }
302 // from multibyte string
303 // (NB: nLength is right now number of Unicode characters, not
304 // characters in psz! So try not to use it yet!)
305 wxString(const char *psz
, wxMBConv
& conv
= wxConvLibc
, size_t nLength
= wxSTRING_MAXLEN
);
306 // from wxWCharBuffer (i.e. return from wxGetString)
307 wxString(const wxWCharBuffer
& psz
)
308 { InitWith(psz
, 0, wxSTRING_MAXLEN
); }
310 // from C string (for compilers using unsigned char)
311 wxString(const unsigned char* psz
, size_t nLength
= wxSTRING_MAXLEN
)
312 { InitWith((const char*)psz
, 0, nLength
); }
313 // from multibyte string
314 wxString(const char *psz
, wxMBConv
& WXUNUSED(conv
) , size_t nLength
= wxSTRING_MAXLEN
)
315 { InitWith(psz
, 0, nLength
); }
318 // from wide (Unicode) string
319 wxString(const wchar_t *pwz
);
320 #endif // !wxUSE_WCHAR_T
323 wxString(const wxCharBuffer
& psz
)
324 { InitWith(psz
, 0, wxSTRING_MAXLEN
); }
325 #endif // Unicode/ANSI
327 // dtor is not virtual, this class must not be inherited from!
328 ~wxString() { GetStringData()->Unlock(); }
330 // generic attributes & operations
331 // as standard strlen()
332 size_t Len() const { return GetStringData()->nDataLength
; }
333 // string contains any characters?
334 bool IsEmpty() const { return Len() == 0; }
335 // empty string is "FALSE", so !str will return TRUE
336 bool operator!() const { return IsEmpty(); }
337 // empty string contents
344 wxASSERT( GetStringData()->nDataLength
== 0 );
346 // empty the string and free memory
349 if ( !GetStringData()->IsEmpty() )
352 wxASSERT( GetStringData()->nDataLength
== 0 ); // should be empty
353 wxASSERT( GetStringData()->nAllocLength
== 0 ); // and not own any memory
358 bool IsAscii() const;
360 bool IsNumber() const;
364 // data access (all indexes are 0 based)
366 wxChar
GetChar(size_t n
) const
367 { ASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
369 wxChar
& GetWritableChar(size_t n
)
370 { ASSERT_VALID_INDEX( n
); CopyBeforeWrite(); return m_pchData
[n
]; }
372 void SetChar(size_t n
, wxChar ch
)
373 { ASSERT_VALID_INDEX( n
); CopyBeforeWrite(); m_pchData
[n
] = ch
; }
375 // get last character
377 { wxASSERT( !IsEmpty() ); return m_pchData
[Len() - 1]; }
378 // get writable last character
380 { wxASSERT( !IsEmpty() ); CopyBeforeWrite(); return m_pchData
[Len()-1]; }
382 // operator version of GetChar
383 wxChar
operator[](size_t n
) const
384 { ASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
386 // operator version of GetChar
387 wxChar
operator[](int n
) const
388 { ASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
390 // operator version of GetChar
391 wxChar
operator[](unsigned int n
) const
392 { ASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
395 // operator version of GetWriteableChar
396 wxChar
& operator[](size_t n
)
397 { ASSERT_VALID_INDEX( n
); CopyBeforeWrite(); return m_pchData
[n
]; }
399 // operator version of GetWriteableChar
400 wxChar
& operator[](unsigned int n
)
401 { ASSERT_VALID_INDEX( n
); CopyBeforeWrite(); return m_pchData
[n
]; }
404 // implicit conversion to C string
405 operator const wxChar
*() const { return m_pchData
; }
406 // explicit conversion to C string (use this with printf()!)
407 const wxChar
* c_str() const { return m_pchData
; }
408 // (and this with [wx]Printf()!)
409 const wxChar
* wx_str() const { return m_pchData
; }
410 // identical to c_str()
411 const wxChar
* GetData() const { return m_pchData
; }
413 // conversions with (possible) format convertions: have to return a
414 // buffer with temporary data
416 const wxCharBuffer
mb_str(wxMBConv
& conv
= wxConvLibc
) const { return conv
.cWC2MB(m_pchData
); }
417 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
419 const wxChar
* wc_str(wxMBConv
& WXUNUSED(conv
) = wxConvLibc
) const { return m_pchData
; }
422 const wxCharBuffer
fn_str() const { return mb_str(wxConvFile
); }
424 const wxChar
* fn_str() const { return m_pchData
; }
425 #endif // wxMBFILES/!wxMBFILES
428 const wxChar
* mb_str(wxMBConv
& WXUNUSED(conv
) = wxConvLibc
) const
429 { return m_pchData
; }
430 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
432 const wxChar
* mb_str() const { return m_pchData
; }
433 const wxWX2MBbuf
mbc_str() const { return mb_str(); }
434 #endif // multibyte/!multibyte
436 const wxWCharBuffer
wc_str(wxMBConv
& conv
) const { return conv
.cMB2WC(m_pchData
); }
437 #endif // wxUSE_WCHAR_T
438 const wxChar
* fn_str() const { return m_pchData
; }
439 #endif // Unicode/ANSI
441 // overloaded assignment
442 // from another wxString
443 wxString
& operator=(const wxString
& stringSrc
);
445 wxString
& operator=(wxChar ch
);
447 wxString
& operator=(const wxChar
*psz
);
449 // from wxWCharBuffer
450 wxString
& operator=(const wxWCharBuffer
& psz
) { return operator=((const wchar_t *)psz
); }
452 // from another kind of C string
453 wxString
& operator=(const unsigned char* psz
);
455 // from a wide string
456 wxString
& operator=(const wchar_t *pwz
);
459 wxString
& operator=(const wxCharBuffer
& psz
) { return operator=((const char *)psz
); }
460 #endif // Unicode/ANSI
462 // string concatenation
463 // in place concatenation
465 Concatenate and return the result. Note that the left to right
466 associativity of << allows to write things like "str << str1 << str2
467 << ..." (unlike with +=)
470 wxString
& operator<<(const wxString
& s
)
472 wxASSERT( s
.GetStringData()->IsValid() );
474 ConcatSelf(s
.Len(), s
);
477 // string += C string
478 wxString
& operator<<(const wxChar
*psz
)
479 { ConcatSelf(wxStrlen(psz
), psz
); return *this; }
481 wxString
& operator<<(wxChar ch
) { ConcatSelf(1, &ch
); return *this; }
484 void operator+=(const wxString
& s
) { (void)operator<<(s
); }
485 // string += C string
486 void operator+=(const wxChar
*psz
) { (void)operator<<(psz
); }
488 void operator+=(wxChar ch
) { (void)operator<<(ch
); }
490 // string += buffer (i.e. from wxGetString)
492 wxString
& operator<<(const wxWCharBuffer
& s
) { (void)operator<<((const wchar_t *)s
); return *this; }
493 void operator+=(const wxWCharBuffer
& s
) { (void)operator<<((const wchar_t *)s
); }
495 wxString
& operator<<(const wxCharBuffer
& s
) { (void)operator<<((const char *)s
); return *this; }
496 void operator+=(const wxCharBuffer
& s
) { (void)operator<<((const char *)s
); }
499 // string += C string
500 wxString
& Append(const wxChar
* psz
)
501 { ConcatSelf(wxStrlen(psz
), psz
); return *this; }
502 // append count copies of given character
503 wxString
& Append(wxChar ch
, size_t count
= 1u)
504 { wxString
str(ch
, count
); return *this << str
; }
506 // prepend a string, return the string itself
507 wxString
& Prepend(const wxString
& str
)
508 { *this = str
+ *this; return *this; }
510 // non-destructive concatenation
512 friend wxString WXDLLEXPORT
operator+(const wxString
& string1
, const wxString
& string2
);
514 friend wxString WXDLLEXPORT
operator+(const wxString
& string
, wxChar ch
);
516 friend wxString WXDLLEXPORT
operator+(wxChar ch
, const wxString
& string
);
518 friend wxString WXDLLEXPORT
operator+(const wxString
& string
, const wxChar
*psz
);
520 friend wxString WXDLLEXPORT
operator+(const wxChar
*psz
, const wxString
& string
);
522 // stream-like functions
523 // insert an int into string
524 wxString
& operator<<(int i
);
525 // insert a float into string
526 wxString
& operator<<(float f
);
527 // insert a double into string
528 wxString
& operator<<(double d
);
531 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
532 int Cmp(const wxChar
*psz
) const { return wxStrcmp(c_str(), psz
); }
533 // same as Cmp() but not case-sensitive
534 int CmpNoCase(const wxChar
*psz
) const { return wxStricmp(c_str(), psz
); }
535 // test for the string equality, either considering case or not
536 // (if compareWithCase then the case matters)
537 bool IsSameAs(const wxChar
*psz
, bool compareWithCase
= TRUE
) const
538 { return (compareWithCase
? Cmp(psz
) : CmpNoCase(psz
)) == 0; }
539 // comparison with a signle character: returns TRUE if equal
540 bool IsSameAs(wxChar c
, bool compareWithCase
= TRUE
) const
542 return (Len() == 1) && (compareWithCase
? GetChar(0u) == c
543 : wxToupper(GetChar(0u)) == wxToupper(c
));
546 // simple sub-string extraction
547 // return substring starting at nFirst of length nCount (or till the end
548 // if nCount = default value)
549 wxString
Mid(size_t nFirst
, size_t nCount
= wxSTRING_MAXLEN
) const;
551 // operator version of Mid()
552 wxString
operator()(size_t start
, size_t len
) const
553 { return Mid(start
, len
); }
555 // get first nCount characters
556 wxString
Left(size_t nCount
) const;
557 // get last nCount characters
558 wxString
Right(size_t nCount
) const;
559 // get all characters before the first occurence of ch
560 // (returns the whole string if ch not found)
561 wxString
BeforeFirst(wxChar ch
) const;
562 // get all characters before the last occurence of ch
563 // (returns empty string if ch not found)
564 wxString
BeforeLast(wxChar ch
) const;
565 // get all characters after the first occurence of ch
566 // (returns empty string if ch not found)
567 wxString
AfterFirst(wxChar ch
) const;
568 // get all characters after the last occurence of ch
569 // (returns the whole string if ch not found)
570 wxString
AfterLast(wxChar ch
) const;
572 // for compatibility only, use more explicitly named functions above
573 wxString
Before(wxChar ch
) const { return BeforeLast(ch
); }
574 wxString
After(wxChar ch
) const { return AfterFirst(ch
); }
577 // convert to upper case in place, return the string itself
578 wxString
& MakeUpper();
579 // convert to upper case, return the copy of the string
580 // Here's something to remember: BC++ doesn't like returns in inlines.
581 wxString
Upper() const ;
582 // convert to lower case in place, return the string itself
583 wxString
& MakeLower();
584 // convert to lower case, return the copy of the string
585 wxString
Lower() const ;
587 // trimming/padding whitespace (either side) and truncating
588 // remove spaces from left or from right (default) side
589 wxString
& Trim(bool bFromRight
= TRUE
);
590 // add nCount copies chPad in the beginning or at the end (default)
591 wxString
& Pad(size_t nCount
, wxChar chPad
= wxT(' '), bool bFromRight
= TRUE
);
592 // truncate string to given length
593 wxString
& Truncate(size_t uiLen
);
595 // searching and replacing
596 // searching (return starting index, or -1 if not found)
597 int Find(wxChar ch
, bool bFromEnd
= FALSE
) const; // like strchr/strrchr
598 // searching (return starting index, or -1 if not found)
599 int Find(const wxChar
*pszSub
) const; // like strstr
600 // replace first (or all of bReplaceAll) occurences of substring with
601 // another string, returns the number of replacements made
602 size_t Replace(const wxChar
*szOld
,
604 bool bReplaceAll
= TRUE
);
606 // check if the string contents matches a mask containing '*' and '?'
607 bool Matches(const wxChar
*szMask
) const;
609 // formated input/output
610 // as sprintf(), returns the number of characters written or < 0 on error
611 int Printf(const wxChar
*pszFormat
, ...);
612 // as vprintf(), returns the number of characters written or < 0 on error
613 int PrintfV(const wxChar
* pszFormat
, va_list argptr
);
615 // raw access to string memory
616 // ensure that string has space for at least nLen characters
617 // only works if the data of this string is not shared
618 void Alloc(size_t nLen
);
619 // minimize the string's memory
620 // only works if the data of this string is not shared
622 // get writable buffer of at least nLen bytes. Unget() *must* be called
623 // a.s.a.p. to put string back in a reasonable state!
624 wxChar
*GetWriteBuf(size_t nLen
);
625 // call this immediately after GetWriteBuf() has been used
626 void UngetWriteBuf();
628 // wxWindows version 1 compatibility functions
631 wxString
SubString(size_t from
, size_t to
) const
632 { return Mid(from
, (to
- from
+ 1)); }
633 // values for second parameter of CompareTo function
634 enum caseCompare
{exact
, ignoreCase
};
635 // values for first parameter of Strip function
636 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
639 int sprintf(const wxChar
*pszFormat
, ...);
642 inline int CompareTo(const wxChar
* psz
, caseCompare cmp
= exact
) const
643 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
646 size_t Length() const { return Len(); }
647 // Count the number of characters
648 int Freq(wxChar ch
) const;
650 void LowerCase() { MakeLower(); }
652 void UpperCase() { MakeUpper(); }
653 // use Trim except that it doesn't change this string
654 wxString
Strip(stripType w
= trailing
) const;
656 // use Find (more general variants not yet supported)
657 size_t Index(const wxChar
* psz
) const { return Find(psz
); }
658 size_t Index(wxChar ch
) const { return Find(ch
); }
660 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
661 wxString
& RemoveLast() { return Truncate(Len() - 1); }
663 wxString
& Remove(size_t nStart
, size_t nLen
) { return erase( nStart
, nLen
); }
666 int First( const wxChar ch
) const { return Find(ch
); }
667 int First( const wxChar
* psz
) const { return Find(psz
); }
668 int First( const wxString
&str
) const { return Find(str
); }
669 int Last( const wxChar ch
) const { return Find(ch
, TRUE
); }
670 bool Contains(const wxString
& str
) const { return Find(str
) != -1; }
673 bool IsNull() const { return IsEmpty(); }
675 #ifdef wxSTD_STRING_COMPATIBILITY
676 // std::string compatibility functions
679 typedef wxChar value_type
;
680 typedef const value_type
*const_iterator
;
682 // an 'invalid' value for string index
683 static const size_t npos
;
686 // take nLen chars starting at nPos
687 wxString(const wxString
& str
, size_t nPos
, size_t nLen
)
689 wxASSERT( str
.GetStringData()->IsValid() );
690 InitWith(str
.c_str(), nPos
, nLen
== npos
? 0 : nLen
);
692 // take all characters from pStart to pEnd
693 wxString(const void *pStart
, const void *pEnd
);
695 // lib.string.capacity
696 // return the length of the string
697 size_t size() const { return Len(); }
698 // return the length of the string
699 size_t length() const { return Len(); }
700 // return the maximum size of the string
701 size_t max_size() const { return wxSTRING_MAXLEN
; }
702 // resize the string, filling the space with c if c != 0
703 void resize(size_t nSize
, wxChar ch
= wxT('\0'));
704 // delete the contents of the string
705 void clear() { Empty(); }
706 // returns true if the string is empty
707 bool empty() const { return IsEmpty(); }
708 // inform string about planned change in size
709 void reserve(size_t size
) { Alloc(size
); }
712 // return the character at position n
713 wxChar
at(size_t n
) const { return GetChar(n
); }
714 // returns the writable character at position n
715 wxChar
& at(size_t n
) { return GetWritableChar(n
); }
717 // first valid index position
718 const_iterator
begin() const { return wx_str(); }
719 // position one after the last valid one
720 const_iterator
end() const { return wx_str() + length(); }
722 // lib.string.modifiers
724 wxString
& append(const wxString
& str
)
725 { *this += str
; return *this; }
726 // append elements str[pos], ..., str[pos+n]
727 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
728 { ConcatSelf(n
, str
.c_str() + pos
); return *this; }
729 // append first n (or all if n == npos) characters of sz
730 wxString
& append(const wxChar
*sz
, size_t n
= npos
)
731 { ConcatSelf(n
== npos
? wxStrlen(sz
) : n
, sz
); return *this; }
733 // append n copies of ch
734 wxString
& append(size_t n
, wxChar ch
) { return Pad(n
, ch
); }
736 // same as `this_string = str'
737 wxString
& assign(const wxString
& str
) { return (*this) = str
; }
738 // same as ` = str[pos..pos + n]
739 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
740 { return *this = wxString((const wxChar
*)str
+ pos
, n
); }
741 // same as `= first n (or all if n == npos) characters of sz'
742 wxString
& assign(const wxChar
*sz
, size_t n
= npos
)
743 { return *this = wxString(sz
, n
); }
744 // same as `= n copies of ch'
745 wxString
& assign(size_t n
, wxChar ch
)
746 { return *this = wxString(ch
, n
); }
748 // insert another string
749 wxString
& insert(size_t nPos
, const wxString
& str
);
750 // insert n chars of str starting at nStart (in str)
751 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
752 { return insert(nPos
, wxString((const wxChar
*)str
+ nStart
, n
)); }
754 // insert first n (or all if n == npos) characters of sz
755 wxString
& insert(size_t nPos
, const wxChar
*sz
, size_t n
= npos
)
756 { return insert(nPos
, wxString(sz
, n
)); }
757 // insert n copies of ch
758 wxString
& insert(size_t nPos
, size_t n
, wxChar ch
)
759 { return insert(nPos
, wxString(ch
, n
)); }
761 // delete characters from nStart to nStart + nLen
762 wxString
& erase(size_t nStart
= 0, size_t nLen
= npos
);
764 // replaces the substring of length nLen starting at nStart
765 wxString
& replace(size_t nStart
, size_t nLen
, const wxChar
* sz
);
766 // replaces the substring with nCount copies of ch
767 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxChar ch
);
768 // replaces a substring with another substring
769 wxString
& replace(size_t nStart
, size_t nLen
,
770 const wxString
& str
, size_t nStart2
, size_t nLen2
);
771 // replaces the substring with first nCount chars of sz
772 wxString
& replace(size_t nStart
, size_t nLen
,
773 const wxChar
* sz
, size_t nCount
);
776 void swap(wxString
& str
);
778 // All find() functions take the nStart argument which specifies the
779 // position to start the search on, the default value is 0. All functions
780 // return npos if there were no match.
783 size_t find(const wxString
& str
, size_t nStart
= 0) const;
785 // VC++ 1.5 can't cope with this syntax.
786 #if !defined(__VISUALC__) || defined(__WIN32__)
787 // find first n characters of sz
788 size_t find(const wxChar
* sz
, size_t nStart
= 0, size_t n
= npos
) const;
791 // Gives a duplicate symbol (presumably a case-insensitivity problem)
792 #if !defined(__BORLANDC__)
793 // find the first occurence of character ch after nStart
794 size_t find(wxChar ch
, size_t nStart
= 0) const;
796 // rfind() family is exactly like find() but works right to left
798 // as find, but from the end
799 size_t rfind(const wxString
& str
, size_t nStart
= npos
) const;
801 // VC++ 1.5 can't cope with this syntax.
802 #if !defined(__VISUALC__) || defined(__WIN32__)
803 // as find, but from the end
804 size_t rfind(const wxChar
* sz
, size_t nStart
= npos
,
805 size_t n
= npos
) const;
806 // as find, but from the end
807 size_t rfind(wxChar ch
, size_t nStart
= npos
) const;
810 // find first/last occurence of any character in the set
812 // as strpbrk() but starts at nStart, returns npos if not found
813 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
814 { return find_first_of(str
.c_str(), nStart
); }
816 size_t find_first_of(const wxChar
* sz
, size_t nStart
= 0) const;
817 // same as find(char, size_t)
818 size_t find_first_of(wxChar c
, size_t nStart
= 0) const
819 { return find(c
, nStart
); }
820 // find the last (starting from nStart) char from str in this string
821 size_t find_last_of (const wxString
& str
, size_t nStart
= npos
) const
822 { return find_last_of(str
.c_str(), nStart
); }
824 size_t find_last_of (const wxChar
* sz
, size_t nStart
= npos
) const;
826 size_t find_last_of(wxChar c
, size_t nStart
= npos
) const
827 { return rfind(c
, nStart
); }
829 // find first/last occurence of any character not in the set
831 // as strspn() (starting from nStart), returns npos on failure
832 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
833 { return find_first_not_of(str
.c_str(), nStart
); }
835 size_t find_first_not_of(const wxChar
* sz
, size_t nStart
= 0) const;
837 size_t find_first_not_of(wxChar ch
, size_t nStart
= 0) const;
839 size_t find_last_not_of(const wxString
& str
, size_t nStart
=npos
) const;
841 size_t find_last_not_of(const wxChar
* sz
, size_t nStart
= npos
) const;
843 size_t find_last_not_of(wxChar ch
, size_t nStart
= npos
) const;
845 // All compare functions return -1, 0 or 1 if the [sub]string is less,
846 // equal or greater than the compare() argument.
848 // just like strcmp()
849 int compare(const wxString
& str
) const { return Cmp(str
); }
850 // comparison with a substring
851 int compare(size_t nStart
, size_t nLen
, const wxString
& str
) const;
852 // comparison of 2 substrings
853 int compare(size_t nStart
, size_t nLen
,
854 const wxString
& str
, size_t nStart2
, size_t nLen2
) const;
855 // just like strcmp()
856 int compare(const wxChar
* sz
) const { return Cmp(sz
); }
857 // substring comparison with first nCount characters of sz
858 int compare(size_t nStart
, size_t nLen
,
859 const wxChar
* sz
, size_t nCount
= npos
) const;
861 // substring extraction
862 wxString
substr(size_t nStart
= 0, size_t nLen
= npos
) const
863 { return Mid(nStart
, nLen
); }
864 #endif // wxSTD_STRING_COMPATIBILITY
867 // ----------------------------------------------------------------------------
868 // The string array uses it's knowledge of internal structure of the wxString
869 // class to optimize string storage. Normally, we would store pointers to
870 // string, but as wxString is, in fact, itself a pointer (sizeof(wxString) is
871 // sizeof(char *)) we store these pointers instead. The cast to "wxString *" is
872 // really all we need to turn such pointer into a string!
874 // Of course, it can be called a dirty hack, but we use twice less memory and
875 // this approach is also more speed efficient, so it's probably worth it.
877 // Usage notes: when a string is added/inserted, a new copy of it is created,
878 // so the original string may be safely deleted. When a string is retrieved
879 // from the array (operator[] or Item() method), a reference is returned.
880 // ----------------------------------------------------------------------------
882 class WXDLLEXPORT wxArrayString
885 // type of function used by wxArrayString::Sort()
886 typedef int (*CompareFunction
)(const wxString
& first
,
887 const wxString
& second
);
889 // constructors and destructor
890 // default ctor: if autoSort is TRUE, the array is always sorted (in
891 // alphabetical order)
892 wxArrayString(bool autoSort
= FALSE
);
894 wxArrayString(const wxArrayString
& array
);
895 // assignment operator
896 wxArrayString
& operator=(const wxArrayString
& src
);
897 // not virtual, this class should not be derived from
901 // empties the list, but doesn't release memory
903 // empties the list and releases memory
905 // preallocates memory for given number of items
906 void Alloc(size_t nCount
);
907 // minimzes the memory usage (by freeing all extra memory)
911 // number of elements in the array
912 size_t GetCount() const { return m_nCount
; }
914 bool IsEmpty() const { return m_nCount
== 0; }
915 // number of elements in the array (GetCount is preferred API)
916 size_t Count() const { return m_nCount
; }
918 // items access (range checking is done in debug version)
919 // get item at position uiIndex
920 wxString
& Item(size_t nIndex
) const
921 { wxASSERT( nIndex
< m_nCount
); return *(wxString
*)&(m_pItems
[nIndex
]); }
923 wxString
& operator[](size_t nIndex
) const { return Item(nIndex
); }
925 wxString
& Last() const { wxASSERT( !IsEmpty() ); return Item(Count() - 1); }
928 // Search the element in the array, starting from the beginning if
929 // bFromEnd is FALSE or from end otherwise. If bCase, comparison is case
930 // sensitive (default). Returns index of the first item matched or
932 int Index (const wxChar
*sz
, bool bCase
= TRUE
, bool bFromEnd
= FALSE
) const;
933 // add new element at the end (if the array is not sorted), return its
935 size_t Add(const wxString
& str
);
936 // add new element at given position
937 void Insert(const wxString
& str
, size_t uiIndex
);
938 // remove first item matching this value
939 void Remove(const wxChar
*sz
);
940 // remove item by index
941 void Remove(size_t nIndex
);
944 // sort array elements in alphabetical order (or reversed alphabetical
945 // order if reverseOrder parameter is TRUE)
946 void Sort(bool reverseOrder
= FALSE
);
947 // sort array elements using specified comparaison function
948 void Sort(CompareFunction compareFunction
);
951 void Copy(const wxArrayString
& src
); // copies the contents of another array
954 void Grow(); // makes array bigger if needed
955 void Free(); // free all the strings stored
957 void DoSort(); // common part of all Sort() variants
959 size_t m_nSize
, // current size of the array
960 m_nCount
; // current number of elements
962 wxChar
**m_pItems
; // pointer to data
964 bool m_autoSort
; // if TRUE, keep the array always sorted
967 class WXDLLEXPORT wxSortedArrayString
: public wxArrayString
970 wxSortedArrayString() : wxArrayString(TRUE
)
972 wxSortedArrayString(const wxArrayString
& array
) : wxArrayString(TRUE
)
976 // ---------------------------------------------------------------------------
977 // wxString comparison functions: operator versions are always case sensitive
978 // ---------------------------------------------------------------------------
981 inline bool operator==(const wxString
& s1
, const wxString
& s2
) { return (s1
.Cmp(s2
) == 0); }
983 inline bool operator==(const wxString
& s1
, const wxChar
* s2
) { return (s1
.Cmp(s2
) == 0); }
985 inline bool operator==(const wxChar
* s1
, const wxString
& s2
) { return (s2
.Cmp(s1
) == 0); }
987 inline bool operator!=(const wxString
& s1
, const wxString
& s2
) { return (s1
.Cmp(s2
) != 0); }
989 inline bool operator!=(const wxString
& s1
, const wxChar
* s2
) { return (s1
.Cmp(s2
) != 0); }
991 inline bool operator!=(const wxChar
* s1
, const wxString
& s2
) { return (s2
.Cmp(s1
) != 0); }
993 inline bool operator< (const wxString
& s1
, const wxString
& s2
) { return (s1
.Cmp(s2
) < 0); }
995 inline bool operator< (const wxString
& s1
, const wxChar
* s2
) { return (s1
.Cmp(s2
) < 0); }
997 inline bool operator< (const wxChar
* s1
, const wxString
& s2
) { return (s2
.Cmp(s1
) > 0); }
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 // comparison with char
1018 inline bool operator==(wxChar c
, const wxString
& s
) { return s
.IsSameAs(c
); }
1019 inline bool operator==(const wxString
& s
, wxChar c
) { return s
.IsSameAs(c
); }
1020 inline bool operator!=(wxChar c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
1021 inline bool operator!=(const wxString
& s
, wxChar c
) { return !s
.IsSameAs(c
); }
1024 inline bool operator==(const wxString
& s1
, const wxWCharBuffer
& s2
)
1025 { return (s1
.Cmp((const wchar_t *)s2
) == 0); }
1026 inline bool operator==(const wxWCharBuffer
& s1
, const wxString
& s2
)
1027 { return (s2
.Cmp((const wchar_t *)s1
) == 0); }
1029 inline bool operator==(const wxString
& s1
, const wxCharBuffer
& s2
)
1030 { return (s1
.Cmp((const char *)s2
) == 0); }
1031 inline bool operator==(const wxCharBuffer
& s1
, const wxString
& s2
)
1032 { return (s2
.Cmp((const char *)s1
) == 0); }
1035 wxString WXDLLEXPORT
operator+(const wxString
& string1
, const wxString
& string2
);
1036 wxString WXDLLEXPORT
operator+(const wxString
& string
, wxChar ch
);
1037 wxString WXDLLEXPORT
operator+(wxChar ch
, const wxString
& string
);
1038 wxString WXDLLEXPORT
operator+(const wxString
& string
, const wxChar
*psz
);
1039 wxString WXDLLEXPORT
operator+(const wxChar
*psz
, const wxString
& string
);
1041 inline wxString WXDLLEXPORT
operator+(const wxString
& string
, const wxWCharBuffer
& buf
)
1042 { return string
+ (const wchar_t *)buf
; }
1043 inline wxString WXDLLEXPORT
operator+(const wxWCharBuffer
& buf
, const wxString
& string
)
1044 { return (const wchar_t *)buf
+ string
; }
1046 inline wxString WXDLLEXPORT
operator+(const wxString
& string
, const wxCharBuffer
& buf
)
1047 { return string
+ (const char *)buf
; }
1048 inline wxString WXDLLEXPORT
operator+(const wxCharBuffer
& buf
, const wxString
& string
)
1049 { return (const char *)buf
+ string
; }
1052 // ---------------------------------------------------------------------------
1053 // Implementation only from here until the end of file
1054 // ---------------------------------------------------------------------------
1056 // don't pollute the library user's name space
1057 #undef ASSERT_VALID_INDEX
1059 #if defined(wxSTD_STRING_COMPATIBILITY) && wxUSE_STD_IOSTREAM
1061 #include "wx/ioswrap.h"
1063 WXDLLEXPORT istream
& operator>>(istream
&, wxString
&);
1064 WXDLLEXPORT ostream
& operator<<(ostream
&, const wxString
&);
1066 #endif // wxSTD_STRING_COMPATIBILITY
1068 #endif // _WX_WXSTRINGH__