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 // ----------------------------------------------------------------------------
34 // ----------------------------------------------------------------------------
36 #include "wx/defs.h" // everybody should include this
38 #if defined(__WXMAC__) || defined(__VISAGECPP__)
46 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
47 // problem in VACPP V4 with including stdlib.h multiple times
48 // strconv includes it anyway
62 #include <strings.h> // for strcasecmp()
63 #endif // HAVE_STRINGS_H
65 #include "wx/wxchar.h" // for wxChar
66 #include "wx/buffer.h" // for wxCharBuffer
67 #include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes
69 // ---------------------------------------------------------------------------
71 // ---------------------------------------------------------------------------
73 // casts [unfortunately!] needed to call some broken functions which require
74 // "char *" instead of "const char *"
75 #define WXSTRINGCAST (wxChar *)(const wxChar *)
76 #define wxCSTRINGCAST (wxChar *)(const wxChar *)
77 #define wxMBSTRINGCAST (char *)(const char *)
78 #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *)
80 // implementation only
81 #define wxASSERT_VALID_INDEX(i) \
82 wxASSERT_MSG( (size_t)(i) <= Len(), _T("invaid index in wxString") )
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
89 // must define this static for VA or else you get multiply defined symbols everywhere
90 extern const unsigned int wxSTRING_MAXLEN
;
93 // maximum possible length for a string means "take all string" everywhere
94 // (as sizeof(StringData) is unknown here, we substract 100)
95 const unsigned int wxSTRING_MAXLEN
= UINT_MAX
- 100;
99 // ----------------------------------------------------------------------------
101 // ----------------------------------------------------------------------------
103 // global pointer to empty string
104 WXDLLEXPORT_DATA(extern const wxChar
*) wxEmptyString
;
106 // ---------------------------------------------------------------------------
107 // global functions complementing standard C string library replacements for
108 // strlen() and portable strcasecmp()
109 //---------------------------------------------------------------------------
111 // Use wxXXX() functions from wxchar.h instead! These functions are for
112 // backwards compatibility only.
114 // checks whether the passed in pointer is NULL and if the string is empty
115 inline bool IsEmpty(const char *p
) { return (!p
|| !*p
); }
117 // safe version of strlen() (returns 0 if passed NULL pointer)
118 inline size_t Strlen(const char *psz
)
119 { return psz
? strlen(psz
) : 0; }
121 // portable strcasecmp/_stricmp
122 inline int Stricmp(const char *psz1
, const char *psz2
)
124 #if defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) )
125 return _stricmp(psz1
, psz2
);
126 #elif defined(__SC__)
127 return _stricmp(psz1
, psz2
);
128 #elif defined(__SALFORDC__)
129 return stricmp(psz1
, psz2
);
130 #elif defined(__BORLANDC__)
131 return stricmp(psz1
, psz2
);
132 #elif defined(__WATCOMC__)
133 return stricmp(psz1
, psz2
);
134 #elif defined(__DJGPP__)
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
++; }
202 // VC++ will refuse to inline this function but profiling shows that it
204 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
207 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) free(this); }
209 // if we had taken control over string memory (GetWriteBuf), it's
210 // intentionally put in invalid state
211 void Validate(bool b
) { nRefs
= (b
? 1 : 0); }
212 bool IsValid() const { return (nRefs
!= 0); }
215 // ---------------------------------------------------------------------------
216 // This is (yet another one) String class for C++ programmers. It doesn't use
217 // any of "advanced" C++ features (i.e. templates, exceptions, namespaces...)
218 // thus you should be able to compile it with practicaly any C++ compiler.
219 // This class uses copy-on-write technique, i.e. identical strings share the
220 // same memory as long as neither of them is changed.
222 // This class aims to be as compatible as possible with the new standard
223 // std::string class, but adds some additional functions and should be at
224 // least as efficient than the standard implementation.
226 // Performance note: it's more efficient to write functions which take "const
227 // String&" arguments than "const char *" if you assign the argument to
230 // It was compiled and tested under Win32, Linux (libc 5 & 6), Solaris 5.5.
233 // - ressource support (string tables in ressources)
234 // - more wide character (UNICODE) support
235 // - regular expressions support
236 // ---------------------------------------------------------------------------
238 class WXDLLEXPORT wxString
240 friend class WXDLLEXPORT wxArrayString
;
242 // NB: special care was taken in arranging the member functions in such order
243 // that all inline functions can be effectively inlined, verify that all
244 // performace critical functions are still inlined if you change order!
246 // points to data preceded by wxStringData structure with ref count info
249 // accessor to string data
250 wxStringData
* GetStringData() const { return (wxStringData
*)m_pchData
- 1; }
252 // string (re)initialization functions
253 // initializes the string to the empty value (must be called only from
254 // ctors, use Reinit() otherwise)
255 void Init() { m_pchData
= (wxChar
*)wxEmptyString
; }
256 // initializaes the string with (a part of) C-string
257 void InitWith(const wxChar
*psz
, size_t nPos
= 0, size_t nLen
= wxSTRING_MAXLEN
);
258 // as Init, but also frees old data
259 void Reinit() { GetStringData()->Unlock(); Init(); }
262 // allocates memory for string of lenght nLen
263 void AllocBuffer(size_t nLen
);
264 // copies data to another string
265 void AllocCopy(wxString
&, int, int) const;
266 // effectively copies data to string
267 void AssignCopy(size_t, const wxChar
*);
269 // append a (sub)string
270 void ConcatSelf(int nLen
, const wxChar
*src
);
272 // functions called before writing to the string: they copy it if there
273 // are other references to our data (should be the only owner when writing)
274 void CopyBeforeWrite();
275 void AllocBeforeWrite(size_t);
277 // if we hadn't made these operators private, it would be possible to
278 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
279 // converted to char in C and we do have operator=(char)
281 // NB: we don't need other versions (short/long and unsigned) as attempt
282 // to assign another numeric type to wxString will now result in
283 // ambiguity between operator=(char) and operator=(int)
284 wxString
& operator=(int);
286 // these methods are not implemented - there is _no_ conversion from int to
287 // string, you're doing something wrong if the compiler wants to call it!
289 // try `s << i' or `s.Printf("%d", i)' instead
293 // constructors and destructor
294 // ctor for an empty string
295 wxString() { Init(); }
297 wxString(const wxString
& stringSrc
)
299 wxASSERT( stringSrc
.GetStringData()->IsValid() );
301 if ( stringSrc
.IsEmpty() ) {
302 // nothing to do for an empty string
306 m_pchData
= stringSrc
.m_pchData
; // share same data
307 GetStringData()->Lock(); // => one more copy
310 // string containing nRepeat copies of ch
311 wxString(wxChar ch
, size_t nRepeat
= 1);
312 // ctor takes first nLength characters from C string
313 // (default value of wxSTRING_MAXLEN means take all the string)
314 wxString(const wxChar
*psz
, size_t nLength
= wxSTRING_MAXLEN
)
315 { InitWith(psz
, 0, nLength
); }
316 wxString(const wxChar
*psz
, wxMBConv
& WXUNUSED(conv
), size_t nLength
= wxSTRING_MAXLEN
)
317 { InitWith(psz
, 0, nLength
); }
320 // from multibyte string
321 // (NB: nLength is right now number of Unicode characters, not
322 // characters in psz! So try not to use it yet!)
323 wxString(const char *psz
, wxMBConv
& conv
= wxConvLibc
, size_t nLength
= wxSTRING_MAXLEN
);
324 // from wxWCharBuffer (i.e. return from wxGetString)
325 wxString(const wxWCharBuffer
& psz
)
326 { InitWith(psz
, 0, wxSTRING_MAXLEN
); }
328 // from C string (for compilers using unsigned char)
329 wxString(const unsigned char* psz
, size_t nLength
= wxSTRING_MAXLEN
)
330 { InitWith((const char*)psz
, 0, nLength
); }
333 // from wide (Unicode) string
334 wxString(const wchar_t *pwz
, wxMBConv
& conv
= wxConvLibc
, size_t nLength
= wxSTRING_MAXLEN
);
335 #endif // !wxUSE_WCHAR_T
338 wxString(const wxCharBuffer
& psz
)
339 { InitWith(psz
, 0, wxSTRING_MAXLEN
); }
340 #endif // Unicode/ANSI
342 // dtor is not virtual, this class must not be inherited from!
343 ~wxString() { GetStringData()->Unlock(); }
345 // generic attributes & operations
346 // as standard strlen()
347 size_t Len() const { return GetStringData()->nDataLength
; }
348 // string contains any characters?
349 bool IsEmpty() const { return Len() == 0; }
350 // empty string is "FALSE", so !str will return TRUE
351 bool operator!() const { return IsEmpty(); }
352 // truncate the string to given length
353 wxString
& Truncate(size_t uiLen
);
354 // empty string contents
359 wxASSERT_MSG( IsEmpty(), _T("string not empty after call to Empty()?") );
361 // empty the string and free memory
364 if ( !GetStringData()->IsEmpty() )
367 wxASSERT( GetStringData()->nDataLength
== 0 ); // should be empty
368 wxASSERT( GetStringData()->nAllocLength
== 0 ); // and not own any memory
373 bool IsAscii() const;
375 bool IsNumber() const;
379 // data access (all indexes are 0 based)
381 wxChar
GetChar(size_t n
) const
382 { wxASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
384 wxChar
& GetWritableChar(size_t n
)
385 { wxASSERT_VALID_INDEX( n
); CopyBeforeWrite(); return m_pchData
[n
]; }
387 void SetChar(size_t n
, wxChar ch
)
388 { wxASSERT_VALID_INDEX( n
); CopyBeforeWrite(); m_pchData
[n
] = ch
; }
390 // get last character
392 { wxASSERT( !IsEmpty() ); return m_pchData
[Len() - 1]; }
393 // get writable last character
395 { wxASSERT( !IsEmpty() ); CopyBeforeWrite(); return m_pchData
[Len()-1]; }
398 So why do we have all these overloaded operator[]s? A bit of history:
399 initially there was only one of them, taking size_t. Then people
400 started complaining because they wanted to use ints as indices (I
401 wonder why) and compilers were giving warnings about it, so we had to
402 add the operator[](int). Then it became apparent that you couldn't
403 write str[0] any longer because there was ambiguity between two
404 overloads and so you now had to write str[0u] (or, of course, use the
405 explicit casts to either int or size_t but nobody did this).
407 Finally, someone decided to compile wxWin on an Alpha machine and got
408 a surprize: str[0u] didn't compile there because it is of type
409 unsigned int and size_t is unsigned _long_ on Alpha and so there was
410 ambiguity between converting uint to int or ulong. To fix this one we
411 now add operator[](uint) for the machines where size_t is not already
412 the same as unsigned int - hopefully this fixes the problem (for some
415 The only real fix is, of course, to remove all versions but the one
419 // operator version of GetChar
420 wxChar
operator[](size_t n
) const
421 { wxASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
423 // operator version of GetChar
424 wxChar
operator[](int n
) const
425 { wxASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
427 // operator version of GetWriteableChar
428 wxChar
& operator[](size_t n
)
429 { wxASSERT_VALID_INDEX( n
); CopyBeforeWrite(); return m_pchData
[n
]; }
431 #ifndef wxSIZE_T_IS_UINT
432 // operator version of GetChar
433 wxChar
operator[](unsigned int n
) const
434 { wxASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
436 // operator version of GetWriteableChar
437 wxChar
& operator[](unsigned int n
)
438 { wxASSERT_VALID_INDEX( n
); CopyBeforeWrite(); return m_pchData
[n
]; }
439 #endif // size_t != unsigned int
441 // implicit conversion to C string
442 operator const wxChar
*() const { return m_pchData
; }
443 // explicit conversion to C string (use this with printf()!)
444 const wxChar
* c_str() const { return m_pchData
; }
445 // identical to c_str()
446 const wxChar
* wx_str() const { return m_pchData
; }
447 // identical to c_str()
448 const wxChar
* GetData() const { return m_pchData
; }
450 // conversions with (possible) format convertions: have to return a
451 // buffer with temporary data
453 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
454 // return an ANSI (multibyte) string, wc_str() to return a wide string and
455 // fn_str() to return a string which should be used with the OS APIs
456 // accepting the file names. The return value is always the same, but the
457 // type differs because a function may either return pointer to the buffer
458 // directly or have to use intermediate buffer for translation.
460 const wxCharBuffer
mb_str(wxMBConv
& conv
= wxConvLibc
) const
461 { return conv
.cWC2MB(m_pchData
); }
463 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
465 const wxChar
* wc_str() const { return m_pchData
; }
467 // for compatibility with !wxUSE_UNICODE version
468 const wxChar
* wc_str(wxMBConv
& WXUNUSED(conv
)) const { return m_pchData
; }
471 const wxCharBuffer
fn_str() const { return mb_str(wxConvFile
); }
473 const wxChar
* fn_str() const { return m_pchData
; }
474 #endif // wxMBFILES/!wxMBFILES
476 const wxChar
* mb_str() const { return m_pchData
; }
478 // for compatibility with wxUSE_UNICODE version
479 const wxChar
* mb_str(wxMBConv
& WXUNUSED(conv
)) const { return m_pchData
; }
481 const wxWX2MBbuf
mbc_str() const { return mb_str(); }
484 const wxWCharBuffer
wc_str(wxMBConv
& conv
) const
485 { return conv
.cMB2WC(m_pchData
); }
486 #endif // wxUSE_WCHAR_T
488 const wxChar
* fn_str() const { return m_pchData
; }
489 #endif // Unicode/ANSI
491 // overloaded assignment
492 // from another wxString
493 wxString
& operator=(const wxString
& stringSrc
);
495 wxString
& operator=(wxChar ch
);
497 wxString
& operator=(const wxChar
*psz
);
499 // from wxWCharBuffer
500 wxString
& operator=(const wxWCharBuffer
& psz
) { return operator=((const wchar_t *)psz
); }
502 // from another kind of C string
503 wxString
& operator=(const unsigned char* psz
);
505 // from a wide string
506 wxString
& operator=(const wchar_t *pwz
);
509 wxString
& operator=(const wxCharBuffer
& psz
) { return operator=((const char *)psz
); }
510 #endif // Unicode/ANSI
512 // string concatenation
513 // in place concatenation
515 Concatenate and return the result. Note that the left to right
516 associativity of << allows to write things like "str << str1 << str2
517 << ..." (unlike with +=)
520 wxString
& operator<<(const wxString
& s
)
522 wxASSERT( s
.GetStringData()->IsValid() );
524 ConcatSelf(s
.Len(), s
);
527 // string += C string
528 wxString
& operator<<(const wxChar
*psz
)
529 { ConcatSelf(wxStrlen(psz
), psz
); return *this; }
531 wxString
& operator<<(wxChar ch
) { ConcatSelf(1, &ch
); return *this; }
534 void operator+=(const wxString
& s
) { (void)operator<<(s
); }
535 // string += C string
536 void operator+=(const wxChar
*psz
) { (void)operator<<(psz
); }
538 void operator+=(wxChar ch
) { (void)operator<<(ch
); }
540 // string += buffer (i.e. from wxGetString)
542 wxString
& operator<<(const wxWCharBuffer
& s
) { (void)operator<<((const wchar_t *)s
); return *this; }
543 void operator+=(const wxWCharBuffer
& s
) { (void)operator<<((const wchar_t *)s
); }
545 wxString
& operator<<(const wxCharBuffer
& s
) { (void)operator<<((const char *)s
); return *this; }
546 void operator+=(const wxCharBuffer
& s
) { (void)operator<<((const char *)s
); }
549 // string += C string
550 wxString
& Append(const wxChar
* psz
)
551 { ConcatSelf(wxStrlen(psz
), psz
); return *this; }
552 // append count copies of given character
553 wxString
& Append(wxChar ch
, size_t count
= 1u)
554 { wxString
str(ch
, count
); return *this << str
; }
555 wxString
& Append(const wxChar
* psz
, size_t nLen
)
556 { ConcatSelf(nLen
, psz
); return *this; }
558 // prepend a string, return the string itself
559 wxString
& Prepend(const wxString
& str
)
560 { *this = str
+ *this; return *this; }
562 // non-destructive concatenation
564 friend wxString WXDLLEXPORT
operator+(const wxString
& string1
, const wxString
& string2
);
566 friend wxString WXDLLEXPORT
operator+(const wxString
& string
, wxChar ch
);
568 friend wxString WXDLLEXPORT
operator+(wxChar ch
, const wxString
& string
);
570 friend wxString WXDLLEXPORT
operator+(const wxString
& string
, const wxChar
*psz
);
572 friend wxString WXDLLEXPORT
operator+(const wxChar
*psz
, const wxString
& string
);
574 // stream-like functions
575 // insert an int into string
576 wxString
& operator<<(int i
)
577 { return (*this) << Format(_T("%d"), i
); }
578 // insert an unsigned int into string
579 wxString
& operator<<(unsigned int ui
)
580 { return (*this) << Format(_T("%u"), ui
); }
581 // insert a long into string
582 wxString
& operator<<(long l
)
583 { return (*this) << Format(_T("%ld"), l
); }
584 // insert an unsigned long into string
585 wxString
& operator<<(unsigned long ul
)
586 { return (*this) << Format(_T("%lu"), ul
); }
587 // insert a float into string
588 wxString
& operator<<(float f
)
589 { return (*this) << Format(_T("%f"), f
); }
590 // insert a double into string
591 wxString
& operator<<(double d
)
592 { return (*this) << Format(_T("%g"), d
); }
595 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
596 int Cmp(const wxChar
*psz
) const { return wxStrcmp(c_str(), psz
); }
597 // same as Cmp() but not case-sensitive
598 int CmpNoCase(const wxChar
*psz
) const { return wxStricmp(c_str(), psz
); }
599 // test for the string equality, either considering case or not
600 // (if compareWithCase then the case matters)
601 bool IsSameAs(const wxChar
*psz
, bool compareWithCase
= TRUE
) const
602 { return (compareWithCase
? Cmp(psz
) : CmpNoCase(psz
)) == 0; }
603 // comparison with a signle character: returns TRUE if equal
604 bool IsSameAs(wxChar c
, bool compareWithCase
= TRUE
) const
606 return (Len() == 1) && (compareWithCase
? GetChar(0u) == c
607 : wxToupper(GetChar(0u)) == wxToupper(c
));
610 // simple sub-string extraction
611 // return substring starting at nFirst of length nCount (or till the end
612 // if nCount = default value)
613 wxString
Mid(size_t nFirst
, size_t nCount
= wxSTRING_MAXLEN
) const;
615 // operator version of Mid()
616 wxString
operator()(size_t start
, size_t len
) const
617 { return Mid(start
, len
); }
619 // check that the string starts with prefix and return the rest of the
620 // string in the provided pointer if it is not NULL, otherwise return
622 bool StartsWith(const wxChar
*prefix
, wxString
*rest
= NULL
) const;
624 // get first nCount characters
625 wxString
Left(size_t nCount
) const;
626 // get last nCount characters
627 wxString
Right(size_t nCount
) const;
628 // get all characters before the first occurance of ch
629 // (returns the whole string if ch not found)
630 wxString
BeforeFirst(wxChar ch
) const;
631 // get all characters before the last occurence of ch
632 // (returns empty string if ch not found)
633 wxString
BeforeLast(wxChar ch
) const;
634 // get all characters after the first occurence of ch
635 // (returns empty string if ch not found)
636 wxString
AfterFirst(wxChar ch
) const;
637 // get all characters after the last occurence of ch
638 // (returns the whole string if ch not found)
639 wxString
AfterLast(wxChar ch
) const;
641 // for compatibility only, use more explicitly named functions above
642 wxString
Before(wxChar ch
) const { return BeforeLast(ch
); }
643 wxString
After(wxChar ch
) const { return AfterFirst(ch
); }
646 // convert to upper case in place, return the string itself
647 wxString
& MakeUpper();
648 // convert to upper case, return the copy of the string
649 // Here's something to remember: BC++ doesn't like returns in inlines.
650 wxString
Upper() const ;
651 // convert to lower case in place, return the string itself
652 wxString
& MakeLower();
653 // convert to lower case, return the copy of the string
654 wxString
Lower() const ;
656 // trimming/padding whitespace (either side) and truncating
657 // remove spaces from left or from right (default) side
658 wxString
& Trim(bool bFromRight
= TRUE
);
659 // add nCount copies chPad in the beginning or at the end (default)
660 wxString
& Pad(size_t nCount
, wxChar chPad
= wxT(' '), bool bFromRight
= TRUE
);
662 // searching and replacing
663 // searching (return starting index, or -1 if not found)
664 int Find(wxChar ch
, bool bFromEnd
= FALSE
) const; // like strchr/strrchr
665 // searching (return starting index, or -1 if not found)
666 int Find(const wxChar
*pszSub
) const; // like strstr
667 // replace first (or all of bReplaceAll) occurences of substring with
668 // another string, returns the number of replacements made
669 size_t Replace(const wxChar
*szOld
,
671 bool bReplaceAll
= TRUE
);
673 // check if the string contents matches a mask containing '*' and '?'
674 bool Matches(const wxChar
*szMask
) const;
676 // conversion to numbers: all functions return TRUE only if the whole
677 // string is a number and put the value of this number into the pointer
678 // provided, the base is the numeric base in which the conversion should be
679 // done and must be comprised between 2 and 36 or be 0 in which case the
680 // standard C rules apply (leading '0' => octal, "0x" => hex)
681 // convert to a signed integer
682 bool ToLong(long *val
, int base
= 10) const;
683 // convert to an unsigned integer
684 bool ToULong(unsigned long *val
, int base
= 10) const;
685 // convert to a double
686 bool ToDouble(double *val
) const;
688 // formated input/output
689 // as sprintf(), returns the number of characters written or < 0 on error
690 int Printf(const wxChar
*pszFormat
, ...);
691 // as vprintf(), returns the number of characters written or < 0 on error
692 int PrintfV(const wxChar
* pszFormat
, va_list argptr
);
694 // returns the string containing the result of Printf() to it
695 static wxString
Format(const wxChar
*pszFormat
, ...);
696 // the same as above, but takes a va_list
697 static wxString
FormatV(const wxChar
*pszFormat
, va_list argptr
);
699 // raw access to string memory
700 // ensure that string has space for at least nLen characters
701 // only works if the data of this string is not shared
702 void Alloc(size_t nLen
);
703 // minimize the string's memory
704 // only works if the data of this string is not shared
706 // get writable buffer of at least nLen bytes. Unget() *must* be called
707 // a.s.a.p. to put string back in a reasonable state!
708 wxChar
*GetWriteBuf(size_t nLen
);
709 // call this immediately after GetWriteBuf() has been used
710 void UngetWriteBuf();
711 void UngetWriteBuf(size_t nLen
);
713 // wxWindows version 1 compatibility functions
716 wxString
SubString(size_t from
, size_t to
) const
717 { return Mid(from
, (to
- from
+ 1)); }
718 // values for second parameter of CompareTo function
719 enum caseCompare
{exact
, ignoreCase
};
720 // values for first parameter of Strip function
721 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
724 int sprintf(const wxChar
*pszFormat
, ...);
727 inline int CompareTo(const wxChar
* psz
, caseCompare cmp
= exact
) const
728 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
731 size_t Length() const { return Len(); }
732 // Count the number of characters
733 int Freq(wxChar ch
) const;
735 void LowerCase() { MakeLower(); }
737 void UpperCase() { MakeUpper(); }
738 // use Trim except that it doesn't change this string
739 wxString
Strip(stripType w
= trailing
) const;
741 // use Find (more general variants not yet supported)
742 size_t Index(const wxChar
* psz
) const { return Find(psz
); }
743 size_t Index(wxChar ch
) const { return Find(ch
); }
745 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
746 wxString
& RemoveLast(size_t n
= 1) { return Truncate(Len() - n
); }
748 wxString
& Remove(size_t nStart
, size_t nLen
) { return erase( nStart
, nLen
); }
751 int First( const wxChar ch
) const { return Find(ch
); }
752 int First( const wxChar
* psz
) const { return Find(psz
); }
753 int First( const wxString
&str
) const { return Find(str
); }
754 int Last( const wxChar ch
) const { return Find(ch
, TRUE
); }
755 bool Contains(const wxString
& str
) const { return Find(str
) != -1; }
758 bool IsNull() const { return IsEmpty(); }
760 #ifdef wxSTD_STRING_COMPATIBILITY
761 // std::string compatibility functions
764 typedef wxChar value_type
;
765 typedef const value_type
*const_iterator
;
767 // an 'invalid' value for string index
768 static const size_t npos
;
771 // take nLen chars starting at nPos
772 wxString(const wxString
& str
, size_t nPos
, size_t nLen
)
774 wxASSERT( str
.GetStringData()->IsValid() );
775 InitWith(str
.c_str(), nPos
, nLen
== npos
? 0 : nLen
);
777 // take all characters from pStart to pEnd
778 wxString(const void *pStart
, const void *pEnd
);
780 // lib.string.capacity
781 // return the length of the string
782 size_t size() const { return Len(); }
783 // return the length of the string
784 size_t length() const { return Len(); }
785 // return the maximum size of the string
786 size_t max_size() const { return wxSTRING_MAXLEN
; }
787 // resize the string, filling the space with c if c != 0
788 void resize(size_t nSize
, wxChar ch
= wxT('\0'));
789 // delete the contents of the string
790 void clear() { Empty(); }
791 // returns true if the string is empty
792 bool empty() const { return IsEmpty(); }
793 // inform string about planned change in size
794 void reserve(size_t size
) { Alloc(size
); }
797 // return the character at position n
798 wxChar
at(size_t n
) const { return GetChar(n
); }
799 // returns the writable character at position n
800 wxChar
& at(size_t n
) { return GetWritableChar(n
); }
802 // first valid index position
803 const_iterator
begin() const { return wx_str(); }
804 // position one after the last valid one
805 const_iterator
end() const { return wx_str() + length(); }
807 // lib.string.modifiers
809 wxString
& append(const wxString
& str
)
810 { *this += str
; return *this; }
811 // append elements str[pos], ..., str[pos+n]
812 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
813 { ConcatSelf(n
, str
.c_str() + pos
); return *this; }
814 // append first n (or all if n == npos) characters of sz
815 wxString
& append(const wxChar
*sz
, size_t n
= npos
)
816 { ConcatSelf(n
== npos
? wxStrlen(sz
) : n
, sz
); return *this; }
818 // append n copies of ch
819 wxString
& append(size_t n
, wxChar ch
) { return Pad(n
, ch
); }
821 // same as `this_string = str'
822 wxString
& assign(const wxString
& str
)
823 { return *this = str
; }
824 // same as ` = str[pos..pos + n]
825 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
826 { Empty(); return Append(str
.c_str() + pos
, n
); }
827 // same as `= first n (or all if n == npos) characters of sz'
828 wxString
& assign(const wxChar
*sz
, size_t n
= npos
)
829 { Empty(); return Append(sz
, n
== npos
? wxStrlen(sz
) : n
); }
830 // same as `= n copies of ch'
831 wxString
& assign(size_t n
, wxChar ch
)
832 { Empty(); return Append(ch
, n
); }
834 // insert another string
835 wxString
& insert(size_t nPos
, const wxString
& str
);
836 // insert n chars of str starting at nStart (in str)
837 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
838 { return insert(nPos
, wxString((const wxChar
*)str
+ nStart
, n
)); }
840 // insert first n (or all if n == npos) characters of sz
841 wxString
& insert(size_t nPos
, const wxChar
*sz
, size_t n
= npos
)
842 { return insert(nPos
, wxString(sz
, n
)); }
843 // insert n copies of ch
844 wxString
& insert(size_t nPos
, size_t n
, wxChar ch
)
845 { return insert(nPos
, wxString(ch
, n
)); }
847 // delete characters from nStart to nStart + nLen
848 wxString
& erase(size_t nStart
= 0, size_t nLen
= npos
);
850 // replaces the substring of length nLen starting at nStart
851 wxString
& replace(size_t nStart
, size_t nLen
, const wxChar
* sz
);
852 // replaces the substring with nCount copies of ch
853 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxChar ch
);
854 // replaces a substring with another substring
855 wxString
& replace(size_t nStart
, size_t nLen
,
856 const wxString
& str
, size_t nStart2
, size_t nLen2
);
857 // replaces the substring with first nCount chars of sz
858 wxString
& replace(size_t nStart
, size_t nLen
,
859 const wxChar
* sz
, size_t nCount
);
862 void swap(wxString
& str
);
864 // All find() functions take the nStart argument which specifies the
865 // position to start the search on, the default value is 0. All functions
866 // return npos if there were no match.
869 size_t find(const wxString
& str
, size_t nStart
= 0) const;
871 // VC++ 1.5 can't cope with this syntax.
872 #if !defined(__VISUALC__) || defined(__WIN32__)
873 // find first n characters of sz
874 size_t find(const wxChar
* sz
, size_t nStart
= 0, size_t n
= npos
) const;
877 // Gives a duplicate symbol (presumably a case-insensitivity problem)
878 #if !defined(__BORLANDC__)
879 // find the first occurence of character ch after nStart
880 size_t find(wxChar ch
, size_t nStart
= 0) const;
882 // rfind() family is exactly like find() but works right to left
884 // as find, but from the end
885 size_t rfind(const wxString
& str
, size_t nStart
= npos
) const;
887 // VC++ 1.5 can't cope with this syntax.
888 #if !defined(__VISUALC__) || defined(__WIN32__)
889 // as find, but from the end
890 size_t rfind(const wxChar
* sz
, size_t nStart
= npos
,
891 size_t n
= npos
) const;
892 // as find, but from the end
893 size_t rfind(wxChar ch
, size_t nStart
= npos
) const;
896 // find first/last occurence of any character in the set
898 // as strpbrk() but starts at nStart, returns npos if not found
899 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
900 { return find_first_of(str
.c_str(), nStart
); }
902 size_t find_first_of(const wxChar
* sz
, size_t nStart
= 0) const;
903 // same as find(char, size_t)
904 size_t find_first_of(wxChar c
, size_t nStart
= 0) const
905 { return find(c
, nStart
); }
906 // find the last (starting from nStart) char from str in this string
907 size_t find_last_of (const wxString
& str
, size_t nStart
= npos
) const
908 { return find_last_of(str
.c_str(), nStart
); }
910 size_t find_last_of (const wxChar
* sz
, size_t nStart
= npos
) const;
912 size_t find_last_of(wxChar c
, size_t nStart
= npos
) const
913 { return rfind(c
, nStart
); }
915 // find first/last occurence of any character not in the set
917 // as strspn() (starting from nStart), returns npos on failure
918 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
919 { return find_first_not_of(str
.c_str(), nStart
); }
921 size_t find_first_not_of(const wxChar
* sz
, size_t nStart
= 0) const;
923 size_t find_first_not_of(wxChar ch
, size_t nStart
= 0) const;
925 size_t find_last_not_of(const wxString
& str
, size_t nStart
= npos
) const
926 { return find_first_not_of(str
.c_str(), nStart
); }
928 size_t find_last_not_of(const wxChar
* sz
, size_t nStart
= npos
) const;
930 size_t find_last_not_of(wxChar ch
, size_t nStart
= npos
) const;
932 // All compare functions return -1, 0 or 1 if the [sub]string is less,
933 // equal or greater than the compare() argument.
935 // just like strcmp()
936 int compare(const wxString
& str
) const { return Cmp(str
); }
937 // comparison with a substring
938 int compare(size_t nStart
, size_t nLen
, const wxString
& str
) const
939 { return Mid(nStart
, nLen
).Cmp(str
); }
940 // comparison of 2 substrings
941 int compare(size_t nStart
, size_t nLen
,
942 const wxString
& str
, size_t nStart2
, size_t nLen2
) const
943 { return Mid(nStart
, nLen
).Cmp(str
.Mid(nStart2
, nLen2
)); }
944 // just like strcmp()
945 int compare(const wxChar
* sz
) const { return Cmp(sz
); }
946 // substring comparison with first nCount characters of sz
947 int compare(size_t nStart
, size_t nLen
,
948 const wxChar
* sz
, size_t nCount
= npos
) const
949 { return Mid(nStart
, nLen
).Cmp(wxString(sz
, nCount
)); }
951 // substring extraction
952 wxString
substr(size_t nStart
= 0, size_t nLen
= npos
) const
953 { return Mid(nStart
, nLen
); }
954 #endif // wxSTD_STRING_COMPATIBILITY
957 // ----------------------------------------------------------------------------
958 // The string array uses it's knowledge of internal structure of the wxString
959 // class to optimize string storage. Normally, we would store pointers to
960 // string, but as wxString is, in fact, itself a pointer (sizeof(wxString) is
961 // sizeof(char *)) we store these pointers instead. The cast to "wxString *" is
962 // really all we need to turn such pointer into a string!
964 // Of course, it can be called a dirty hack, but we use twice less memory and
965 // this approach is also more speed efficient, so it's probably worth it.
967 // Usage notes: when a string is added/inserted, a new copy of it is created,
968 // so the original string may be safely deleted. When a string is retrieved
969 // from the array (operator[] or Item() method), a reference is returned.
970 // ----------------------------------------------------------------------------
972 class WXDLLEXPORT wxArrayString
975 // type of function used by wxArrayString::Sort()
976 typedef int (*CompareFunction
)(const wxString
& first
,
977 const wxString
& second
);
979 // constructors and destructor
981 wxArrayString() { Init(FALSE
); }
982 // if autoSort is TRUE, the array is always sorted (in alphabetical order)
984 // NB: the reason for using int and not bool is that like this we can avoid
985 // using this ctor for implicit conversions from "const char *" (which
986 // we'd like to be implicitly converted to wxString instead!)
988 // of course, using explicit would be even better - if all compilers
990 wxArrayString(int autoSort
) { Init(autoSort
!= 0); }
992 wxArrayString(const wxArrayString
& array
);
993 // assignment operator
994 wxArrayString
& operator=(const wxArrayString
& src
);
995 // not virtual, this class should not be derived from
999 // empties the list, but doesn't release memory
1001 // empties the list and releases memory
1003 // preallocates memory for given number of items
1004 void Alloc(size_t nCount
);
1005 // minimzes the memory usage (by freeing all extra memory)
1009 // number of elements in the array
1010 size_t GetCount() const { return m_nCount
; }
1012 bool IsEmpty() const { return m_nCount
== 0; }
1013 // number of elements in the array (GetCount is preferred API)
1014 size_t Count() const { return m_nCount
; }
1016 // items access (range checking is done in debug version)
1017 // get item at position uiIndex
1018 wxString
& Item(size_t nIndex
) const
1019 { wxASSERT( nIndex
< m_nCount
); return *(wxString
*)&(m_pItems
[nIndex
]); }
1021 wxString
& operator[](size_t nIndex
) const { return Item(nIndex
); }
1023 wxString
& Last() const { wxASSERT( !IsEmpty() ); return Item(Count() - 1); }
1025 // return a wxString[], useful for the controls which
1026 // take one in their ctor. You must delete[] it yourself
1027 // once you are done with it. Will return NULL if the
1028 // ArrayString was empty.
1029 wxString
* GetStringArray() const;
1032 // Search the element in the array, starting from the beginning if
1033 // bFromEnd is FALSE or from end otherwise. If bCase, comparison is case
1034 // sensitive (default). Returns index of the first item matched or
1036 int Index (const wxChar
*sz
, bool bCase
= TRUE
, bool bFromEnd
= FALSE
) const;
1037 // add new element at the end (if the array is not sorted), return its
1039 size_t Add(const wxString
& str
);
1040 // add new element at given position
1041 void Insert(const wxString
& str
, size_t uiIndex
);
1042 // expand the array to have count elements
1043 void SetCount(size_t count
);
1044 // remove first item matching this value
1045 void Remove(const wxChar
*sz
);
1046 // remove item by index
1047 void Remove(size_t nIndex
);
1048 void RemoveAt(size_t nIndex
) { Remove(nIndex
); }
1051 // sort array elements in alphabetical order (or reversed alphabetical
1052 // order if reverseOrder parameter is TRUE)
1053 void Sort(bool reverseOrder
= FALSE
);
1054 // sort array elements using specified comparaison function
1055 void Sort(CompareFunction compareFunction
);
1058 // compare two arrays case sensitively
1059 bool operator==(const wxArrayString
& a
) const;
1060 // compare two arrays case sensitively
1061 bool operator!=(const wxArrayString
& a
) const { return !(*this == a
); }
1064 void Init(bool autoSort
); // common part of all ctors
1065 void Copy(const wxArrayString
& src
); // copies the contents of another array
1068 void Grow(); // makes array bigger if needed
1069 void Free(); // free all the strings stored
1071 void DoSort(); // common part of all Sort() variants
1073 size_t m_nSize
, // current size of the array
1074 m_nCount
; // current number of elements
1076 wxChar
**m_pItems
; // pointer to data
1078 bool m_autoSort
; // if TRUE, keep the array always sorted
1081 class WXDLLEXPORT wxSortedArrayString
: public wxArrayString
1084 wxSortedArrayString() : wxArrayString(TRUE
)
1086 wxSortedArrayString(const wxArrayString
& array
) : wxArrayString(TRUE
)
1090 // ----------------------------------------------------------------------------
1091 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
1092 // ----------------------------------------------------------------------------
1094 class WXDLLEXPORT wxStringBuffer
1097 wxStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
1098 : m_str(str
) { m_buf
= m_str
.GetWriteBuf(lenWanted
); }
1100 ~wxStringBuffer() { m_str
.UngetWriteBuf(); }
1102 operator wxChar
*() const { return m_buf
; }
1109 // ---------------------------------------------------------------------------
1110 // wxString comparison functions: operator versions are always case sensitive
1111 // ---------------------------------------------------------------------------
1113 inline bool operator==(const wxString
& s1
, const wxString
& s2
)
1114 { return (s1
.Len() == s2
.Len()) && (s1
.Cmp(s2
) == 0); }
1115 inline bool operator==(const wxString
& s1
, const wxChar
* s2
)
1116 { return s1
.Cmp(s2
) == 0; }
1117 inline bool operator==(const wxChar
* s1
, const wxString
& s2
)
1118 { return s2
.Cmp(s1
) == 0; }
1119 inline bool operator!=(const wxString
& s1
, const wxString
& s2
)
1120 { return (s1
.Len() != s2
.Len()) || (s1
.Cmp(s2
) != 0); }
1121 inline bool operator!=(const wxString
& s1
, const wxChar
* s2
)
1122 { return s1
.Cmp(s2
) != 0; }
1123 inline bool operator!=(const wxChar
* s1
, const wxString
& s2
)
1124 { return s2
.Cmp(s1
) != 0; }
1125 inline bool operator< (const wxString
& s1
, const wxString
& s2
)
1126 { return s1
.Cmp(s2
) < 0; }
1127 inline bool operator< (const wxString
& s1
, const wxChar
* s2
)
1128 { return s1
.Cmp(s2
) < 0; }
1129 inline bool operator< (const wxChar
* s1
, const wxString
& s2
)
1130 { return s2
.Cmp(s1
) > 0; }
1131 inline bool operator> (const wxString
& s1
, const wxString
& s2
)
1132 { return s1
.Cmp(s2
) > 0; }
1133 inline bool operator> (const wxString
& s1
, const wxChar
* s2
)
1134 { return s1
.Cmp(s2
) > 0; }
1135 inline bool operator> (const wxChar
* s1
, const wxString
& s2
)
1136 { return s2
.Cmp(s1
) < 0; }
1137 inline bool operator<=(const wxString
& s1
, const wxString
& s2
)
1138 { return s1
.Cmp(s2
) <= 0; }
1139 inline bool operator<=(const wxString
& s1
, const wxChar
* s2
)
1140 { return s1
.Cmp(s2
) <= 0; }
1141 inline bool operator<=(const wxChar
* s1
, const wxString
& s2
)
1142 { return s2
.Cmp(s1
) >= 0; }
1143 inline bool operator>=(const wxString
& s1
, const wxString
& s2
)
1144 { return s1
.Cmp(s2
) >= 0; }
1145 inline bool operator>=(const wxString
& s1
, const wxChar
* s2
)
1146 { return s1
.Cmp(s2
) >= 0; }
1147 inline bool operator>=(const wxChar
* s1
, const wxString
& s2
)
1148 { return s2
.Cmp(s1
) <= 0; }
1150 // comparison with char
1151 inline bool operator==(wxChar c
, const wxString
& s
) { return s
.IsSameAs(c
); }
1152 inline bool operator==(const wxString
& s
, wxChar c
) { return s
.IsSameAs(c
); }
1153 inline bool operator!=(wxChar c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
1154 inline bool operator!=(const wxString
& s
, wxChar c
) { return !s
.IsSameAs(c
); }
1157 inline bool operator==(const wxString
& s1
, const wxWCharBuffer
& s2
)
1158 { return (s1
.Cmp((const wchar_t *)s2
) == 0); }
1159 inline bool operator==(const wxWCharBuffer
& s1
, const wxString
& s2
)
1160 { return (s2
.Cmp((const wchar_t *)s1
) == 0); }
1161 inline bool operator!=(const wxString
& s1
, const wxWCharBuffer
& s2
)
1162 { return (s1
.Cmp((const wchar_t *)s2
) != 0); }
1163 inline bool operator!=(const wxWCharBuffer
& s1
, const wxString
& s2
)
1164 { return (s2
.Cmp((const wchar_t *)s1
) != 0); }
1165 #else // !wxUSE_UNICODE
1166 inline bool operator==(const wxString
& s1
, const wxCharBuffer
& s2
)
1167 { return (s1
.Cmp((const char *)s2
) == 0); }
1168 inline bool operator==(const wxCharBuffer
& s1
, const wxString
& s2
)
1169 { return (s2
.Cmp((const char *)s1
) == 0); }
1170 inline bool operator!=(const wxString
& s1
, const wxCharBuffer
& s2
)
1171 { return (s1
.Cmp((const char *)s2
) != 0); }
1172 inline bool operator!=(const wxCharBuffer
& s1
, const wxString
& s2
)
1173 { return (s2
.Cmp((const char *)s1
) != 0); }
1174 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1176 wxString WXDLLEXPORT
operator+(const wxString
& string1
, const wxString
& string2
);
1177 wxString WXDLLEXPORT
operator+(const wxString
& string
, wxChar ch
);
1178 wxString WXDLLEXPORT
operator+(wxChar ch
, const wxString
& string
);
1179 wxString WXDLLEXPORT
operator+(const wxString
& string
, const wxChar
*psz
);
1180 wxString WXDLLEXPORT
operator+(const wxChar
*psz
, const wxString
& string
);
1182 inline wxString
operator+(const wxString
& string
, const wxWCharBuffer
& buf
)
1183 { return string
+ (const wchar_t *)buf
; }
1184 inline wxString
operator+(const wxWCharBuffer
& buf
, const wxString
& string
)
1185 { return (const wchar_t *)buf
+ string
; }
1186 #else // !wxUSE_UNICODE
1187 inline wxString
operator+(const wxString
& string
, const wxCharBuffer
& buf
)
1188 { return string
+ (const char *)buf
; }
1189 inline wxString
operator+(const wxCharBuffer
& buf
, const wxString
& string
)
1190 { return (const char *)buf
+ string
; }
1191 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1193 // ---------------------------------------------------------------------------
1194 // Implementation only from here until the end of file
1195 // ---------------------------------------------------------------------------
1197 // don't pollute the library user's name space
1198 #undef wxASSERT_VALID_INDEX
1200 #if defined(wxSTD_STRING_COMPATIBILITY) && wxUSE_STD_IOSTREAM
1202 #include "wx/ioswrap.h"
1204 WXDLLEXPORT wxSTD istream
& operator>>(wxSTD istream
&, wxString
&);
1205 WXDLLEXPORT wxSTD ostream
& operator<<(wxSTD ostream
&, const wxString
&);
1207 #endif // wxSTD_STRING_COMPATIBILITY
1209 #endif // _WX_WXSTRINGH__