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 ///////////////////////////////////////////////////////////////////////////////
12 #ifndef _WX_WXSTRINGH__
13 #define _WX_WXSTRINGH__
16 #pragma interface "string.h"
30 #include <strings.h> // for strcasecmp()
36 #ifdef WXSTRING_IS_WXOBJECT
37 #include "wx/object.h"
42 #include "wx/wxchar.h"
43 #include "wx/buffer.h"
46 Efficient string class [more or less] compatible with MFC CString,
47 wxWindows version 1 wxString and std::string and some handy functions
48 missing from string.h.
51 // ---------------------------------------------------------------------------
53 // ---------------------------------------------------------------------------
55 // compile the std::string compatibility functions if defined
56 #define wxSTD_STRING_COMPATIBILITY
58 // define to derive wxString from wxObject
59 #ifdef WXSTRING_IS_WXOBJECT
60 #undef WXSTRING_IS_WXOBJECT
63 // maximum possible length for a string means "take all string" everywhere
64 // (as sizeof(StringData) is unknown here we substract 100)
65 const unsigned int wxSTRING_MAXLEN
= UINT_MAX
- 100;
68 #define WXSTRINGCAST (wxChar *)(const wxChar *)
69 #define WXCSTRINGCAST (wxChar *)(const wxChar *)
70 #define MBSTRINGCAST (char *)(const char *)
71 #define WCSTRINGCAST (wchar_t *)(const wchar_t *)
73 // implementation only
74 #define ASSERT_VALID_INDEX(i) wxASSERT( (unsigned)(i) <= Len() )
76 // ---------------------------------------------------------------------------
77 // Global functions complementing standard C string library replacements for
78 // strlen() and portable strcasecmp()
79 //---------------------------------------------------------------------------
80 // USE wx* FUNCTIONS IN wx/wxchar.h INSTEAD - THIS IS ONLY FOR BINARY COMPATIBILITY
82 // checks whether the passed in pointer is NULL and if the string is empty
83 inline bool WXDLLEXPORT
IsEmpty(const char *p
) { return (!p
|| !*p
); }
85 // safe version of strlen() (returns 0 if passed NULL pointer)
86 inline size_t WXDLLEXPORT
Strlen(const char *psz
)
87 { return psz
? strlen(psz
) : 0; }
89 // portable strcasecmp/_stricmp
90 inline int WXDLLEXPORT
Stricmp(const char *psz1
, const char *psz2
)
92 #if defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) )
93 return _stricmp(psz1
, psz2
);
95 return _stricmp(psz1
, psz2
);
96 #elif defined(__SALFORDC__)
97 return stricmp(psz1
, psz2
);
98 #elif defined(__BORLANDC__)
99 return stricmp(psz1
, psz2
);
100 #elif defined(__WATCOMC__)
101 return stricmp(psz1
, psz2
);
102 #elif defined(__UNIX__) || defined(__GNUWIN32__)
103 return strcasecmp(psz1
, psz2
);
104 #elif defined(__MWERKS__) && !defined(__INTEL__)
105 register char c1
, c2
;
107 c1
= tolower(*psz1
++);
108 c2
= tolower(*psz2
++);
109 } while ( c1
&& (c1
== c2
) );
113 // almost all compilers/libraries provide this function (unfortunately under
114 // different names), that's why we don't implement our own which will surely
115 // be more efficient than this code (uncomment to use):
117 register char c1, c2;
119 c1 = tolower(*psz1++);
120 c2 = tolower(*psz2++);
121 } while ( c1 && (c1 == c2) );
126 #error "Please define string case-insensitive compare for your OS/compiler"
127 #endif // OS/compiler
130 // ----------------------------------------------------------------------------
132 // ----------------------------------------------------------------------------
134 WXDLLEXPORT_DATA(extern const wxChar
*) wxEmptyString
;
136 // global pointer to empty string
137 WXDLLEXPORT_DATA(extern const wxChar
*) g_szNul
;
139 // return an empty wxString
140 class WXDLLEXPORT wxString
; // not yet defined
141 inline const wxString
& wxGetEmptyString() { return *(wxString
*)&g_szNul
; }
143 // ---------------------------------------------------------------------------
144 // string data prepended with some housekeeping info (used by wxString class),
145 // is never used directly (but had to be put here to allow inlining)
146 // ---------------------------------------------------------------------------
147 struct WXDLLEXPORT wxStringData
149 int nRefs
; // reference count
150 size_t nDataLength
, // actual string length
151 nAllocLength
; // allocated memory size
153 // mimics declaration 'wxChar data[nAllocLength]'
154 wxChar
* data() const { return (wxChar
*)(this + 1); }
156 // empty string has a special ref count so it's never deleted
157 bool IsEmpty() const { return (nRefs
== -1); }
158 bool IsShared() const { return (nRefs
> 1); }
161 void Lock() { if ( !IsEmpty() ) nRefs
++; }
162 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) free(this); }
164 // if we had taken control over string memory (GetWriteBuf), it's
165 // intentionally put in invalid state
166 void Validate(bool b
) { nRefs
= (b
? 1 : 0); }
167 bool IsValid() const { return (nRefs
!= 0); }
170 // ---------------------------------------------------------------------------
171 // types of multibyte<->Unicode conversions
172 // ---------------------------------------------------------------------------
174 class WXDLLEXPORT wxMBConv
177 virtual size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const;
178 virtual size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const;
179 // No longer inline since BC++ complains.
180 const wxWCharBuffer
cMB2WC(const char *psz
) const;
181 const wxCharBuffer
cWC2MB(const wchar_t *psz
) const;
183 const wxWCharBuffer
cMB2WX(const char *psz
) const { return cMB2WC(psz
); }
184 const wxCharBuffer
cWX2MB(const wchar_t *psz
) const { return cWC2MB(psz
); }
185 const wchar_t* cWC2WX(const wchar_t *psz
) const { return psz
; }
186 const wchar_t* cMB2WC(const wchar_t *psz
) const { return psz
; }
188 const char* cMB2WX(const char *psz
) const { return psz
; }
189 const char* cWX2MB(const char *psz
) const { return psz
; }
190 const wxCharBuffer
cWC2WX(const wchar_t *psz
) const { return cWC2MB(psz
); }
191 const wxWCharBuffer
cWX2WC(const char *psz
) const { return cMB2WC(psz
); }
194 WXDLLEXPORT_DATA(extern wxMBConv
) wxConvLibc
;
195 #define wxConv_libc wxConvLibc
197 #define wxANOTHER_MBCONV(type) \
198 class type : public wxMBConv { \
200 virtual size_t MB2WC(wchar_t *buf, const char *psz, size_t n) const; \
201 virtual size_t WC2MB(char *buf, const wchar_t *psz, size_t n) const; \
204 WXDLLEXPORT_DATA(extern wxANOTHER_MBCONV(wxMBConvFile
)) wxConvFile
;
205 #define wxConv_file wxConvFile
206 WXDLLEXPORT_DATA(extern wxANOTHER_MBCONV(wxMBConvUTF7
)) wxConvUTF7
;
207 WXDLLEXPORT_DATA(extern wxANOTHER_MBCONV(wxMBConvUTF8
)) wxConvUTF8
;
208 #define wxConv_UTF8 wxConvUTF8
209 #if defined(__WXGTK12__)
210 WXDLLEXPORT_DATA(extern wxANOTHER_MBCONV(wxMBConvGdk
)) wxConvGdk
;
211 #define wxConv_gdk wxConvGdk
214 class wxCharacterSet
;
215 class WXDLLEXPORT wxCSConv
: public wxMBConv
219 wxCharacterSet
*m_cset
;
221 void SetName(const wxChar
*charset
);
223 wxCSConv(const wxChar
*charset
);
226 virtual size_t MB2WC(wchar_t *buf
, const char *psz
, size_t n
) const;
227 virtual size_t WC2MB(char *buf
, const wchar_t *psz
, size_t n
) const;
230 WXDLLEXPORT_DATA(extern wxCSConv
) wxConvLocal
;
231 #define wxConv_local wxConvLocal
233 WXDLLEXPORT_DATA(extern wxMBConv
*) wxConvCurrent
;
234 #define wxConv_current wxConvCurrent
236 // filenames are multibyte on Unix and probably widechar on Windows?
237 #if defined(__UNIX__) || defined(__BORLANDC__)
244 #define wxFNCONV(name) wxConvFile.cWX2MB(name)
245 #define FNSTRINGCAST MBSTRINGCAST
247 #define wxFNCONV(name) name
248 #define FNSTRINGCAST WXSTRINGCAST
250 #else//!wxUSE_WCHAR_T
251 class WXDLLEXPORT wxMBConv
{
253 const char* cMB2WX(const char *psz
) const { return psz
; }
254 const char* cWX2MB(const char *psz
) const { return psz
; }
256 WXDLLEXPORT_DATA(extern wxMBConv
) wxConvLibc
, wxConvFile
;
257 #define wxConv_libc wxConvLibc
258 #define wxConv_file wxConvFile
259 WXDLLEXPORT_DATA(extern wxMBConv
*) wxConvCurrent
;
260 #define wxConv_current wxConvCurrent
261 #define wxFNCONV(name) name
262 #define FNSTRINGCAST WXSTRINGCAST
263 #endif//wxUSE_WCHAR_T
265 // ---------------------------------------------------------------------------
266 // This is (yet another one) String class for C++ programmers. It doesn't use
267 // any of "advanced" C++ features (i.e. templates, exceptions, namespaces...)
268 // thus you should be able to compile it with practicaly any C++ compiler.
269 // This class uses copy-on-write technique, i.e. identical strings share the
270 // same memory as long as neither of them is changed.
272 // This class aims to be as compatible as possible with the new standard
273 // std::string class, but adds some additional functions and should be at
274 // least as efficient than the standard implementation.
276 // Performance note: it's more efficient to write functions which take "const
277 // String&" arguments than "const char *" if you assign the argument to
280 // It was compiled and tested under Win32, Linux (libc 5 & 6), Solaris 5.5.
283 // - ressource support (string tables in ressources)
284 // - more wide character (UNICODE) support
285 // - regular expressions support
286 // ---------------------------------------------------------------------------
288 #ifdef WXSTRING_IS_WXOBJECT
289 class WXDLLEXPORT wxString
: public wxObject
291 DECLARE_DYNAMIC_CLASS(wxString
)
292 #else //WXSTRING_IS_WXOBJECT
293 class WXDLLEXPORT wxString
295 #endif //WXSTRING_IS_WXOBJECT
297 friend class WXDLLEXPORT wxArrayString
;
299 // NB: special care was taken in arranging the member functions in such order
300 // that all inline functions can be effectively inlined, verify that all
301 // performace critical functions are still inlined if you change order!
303 // points to data preceded by wxStringData structure with ref count info
306 // accessor to string data
307 wxStringData
* GetStringData() const { return (wxStringData
*)m_pchData
- 1; }
309 // string (re)initialization functions
310 // initializes the string to the empty value (must be called only from
311 // ctors, use Reinit() otherwise)
312 void Init() { m_pchData
= (wxChar
*)g_szNul
; }
313 // initializaes the string with (a part of) C-string
314 void InitWith(const wxChar
*psz
, size_t nPos
= 0, size_t nLen
= wxSTRING_MAXLEN
);
315 // as Init, but also frees old data
316 void Reinit() { GetStringData()->Unlock(); Init(); }
319 // allocates memory for string of lenght nLen
320 void AllocBuffer(size_t nLen
);
321 // copies data to another string
322 void AllocCopy(wxString
&, int, int) const;
323 // effectively copies data to string
324 void AssignCopy(size_t, const wxChar
*);
326 // append a (sub)string
327 void ConcatSelf(int nLen
, const wxChar
*src
);
329 // functions called before writing to the string: they copy it if there
330 // are other references to our data (should be the only owner when writing)
331 void CopyBeforeWrite();
332 void AllocBeforeWrite(size_t);
334 // this method is not implemented - there is _no_ conversion from int to
335 // string, you're doing something wrong if the compiler wants to call it!
337 // try `s << i' or `s.Printf("%d", i)' instead
342 // constructors and destructor
343 // ctor for an empty string
344 wxString() { Init(); }
346 wxString(const wxString
& stringSrc
)
348 wxASSERT( stringSrc
.GetStringData()->IsValid() );
350 if ( stringSrc
.IsEmpty() ) {
351 // nothing to do for an empty string
355 m_pchData
= stringSrc
.m_pchData
; // share same data
356 GetStringData()->Lock(); // => one more copy
359 // string containing nRepeat copies of ch
360 wxString(wxChar ch
, size_t nRepeat
= 1);
361 // ctor takes first nLength characters from C string
362 // (default value of wxSTRING_MAXLEN means take all the string)
363 wxString(const wxChar
*psz
, size_t nLength
= wxSTRING_MAXLEN
)
364 { InitWith(psz
, 0, nLength
); }
366 // from multibyte string
367 // (NB: nLength is right now number of Unicode characters, not
368 // characters in psz! So try not to use it yet!)
369 wxString(const char *psz
, wxMBConv
& conv
= wxConvLibc
, size_t nLength
= wxSTRING_MAXLEN
);
370 // from wxWCharBuffer (i.e. return from wxGetString)
371 wxString(const wxWCharBuffer
& psz
)
372 { InitWith(psz
, 0, wxSTRING_MAXLEN
); }
374 // from C string (for compilers using unsigned char)
375 wxString(const unsigned char* psz
, size_t nLength
= wxSTRING_MAXLEN
)
376 { InitWith((const char*)psz
, 0, nLength
); }
377 // from multibyte string
378 wxString(const char *psz
, wxMBConv
& WXUNUSED(conv
), size_t nLength
= wxSTRING_MAXLEN
)
379 { InitWith(psz
, 0, nLength
); }
381 // from wide (Unicode) string
382 wxString(const wchar_t *pwz
);
385 wxString(const wxCharBuffer
& psz
)
386 { InitWith(psz
, 0, wxSTRING_MAXLEN
); }
388 // dtor is not virtual, this class must not be inherited from!
389 ~wxString() { GetStringData()->Unlock(); }
391 // generic attributes & operations
392 // as standard strlen()
393 size_t Len() const { return GetStringData()->nDataLength
; }
394 // string contains any characters?
395 bool IsEmpty() const { return Len() == 0; }
396 // empty string is "FALSE", so !str will return TRUE
397 bool operator!() const { return IsEmpty(); }
398 // empty string contents
405 wxASSERT( GetStringData()->nDataLength
== 0 );
407 // empty the string and free memory
410 if ( !GetStringData()->IsEmpty() )
413 wxASSERT( GetStringData()->nDataLength
== 0 ); // should be empty
414 wxASSERT( GetStringData()->nAllocLength
== 0 ); // and not own any memory
419 bool IsAscii() const;
421 bool IsNumber() const;
425 // data access (all indexes are 0 based)
427 wxChar
GetChar(size_t n
) const
428 { ASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
430 wxChar
& GetWritableChar(size_t n
)
431 { ASSERT_VALID_INDEX( n
); CopyBeforeWrite(); return m_pchData
[n
]; }
433 void SetChar(size_t n
, wxChar ch
)
434 { ASSERT_VALID_INDEX( n
); CopyBeforeWrite(); m_pchData
[n
] = ch
; }
436 // get last character
438 { wxASSERT( !IsEmpty() ); return m_pchData
[Len() - 1]; }
439 // get writable last character
441 { wxASSERT( !IsEmpty() ); CopyBeforeWrite(); return m_pchData
[Len()-1]; }
443 // under Unix it is tested with configure, assume it works on other
444 // platforms (there might be overloading problems if size_t and int are
446 #if !defined(__UNIX__) || wxUSE_SIZE_T_STRING_OPERATOR
447 // operator version of GetChar
448 wxChar
operator[](size_t n
) const
449 { ASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
452 // operator version of GetChar
453 wxChar
operator[](int n
) const
454 { ASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
455 // operator version of GetWritableChar
456 wxChar
& operator[](size_t n
)
457 { ASSERT_VALID_INDEX( n
); CopyBeforeWrite(); return m_pchData
[n
]; }
459 // implicit conversion to C string
460 operator const wxChar
*() const { return m_pchData
; }
461 // explicit conversion to C string (use this with printf()!)
462 const wxChar
* c_str() const { return m_pchData
; }
463 // (and this with [wx]Printf()!)
464 const wxChar
* wx_str() const { return m_pchData
; }
466 const wxChar
* GetData() const { return m_pchData
; }
468 const wxCharBuffer
mb_str(wxMBConv
& conv
= wxConvLibc
) const { return conv
.cWC2MB(m_pchData
); }
469 const wxChar
* wc_str(wxMBConv
& WXUNUSED(conv
) = wxConvLibc
) const { return m_pchData
; }
471 const wxCharBuffer
fn_str() const { return mb_str(wxConvFile
); }
473 const wxChar
* fn_str() const { return m_pchData
; }
476 const wxChar
* mb_str(wxMBConv
& WXUNUSED(conv
) = wxConvLibc
) const { return m_pchData
; }
478 const wxWCharBuffer
wc_str(wxMBConv
& conv
) const { return conv
.cMB2WC(m_pchData
); }
480 const wxChar
* fn_str() const { return m_pchData
; }
483 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
485 // overloaded assignment
486 // from another wxString
487 wxString
& operator=(const wxString
& stringSrc
);
489 wxString
& operator=(wxChar ch
);
491 wxString
& operator=(const wxChar
*psz
);
493 // from wxWCharBuffer
494 wxString
& operator=(const wxWCharBuffer
& psz
) { return operator=((const wchar_t *)psz
); }
496 // from another kind of C string
497 wxString
& operator=(const unsigned char* psz
);
499 // from a wide string
500 wxString
& operator=(const wchar_t *pwz
);
503 wxString
& operator=(const wxCharBuffer
& psz
) { return operator=((const char *)psz
); }
506 // string concatenation
507 // in place concatenation
509 Concatenate and return the result. Note that the left to right
510 associativity of << allows to write things like "str << str1 << str2
511 << ..." (unlike with +=)
514 wxString
& operator<<(const wxString
& s
)
516 wxASSERT( s
.GetStringData()->IsValid() );
518 ConcatSelf(s
.Len(), s
);
521 // string += C string
522 wxString
& operator<<(const wxChar
*psz
)
523 { ConcatSelf(wxStrlen(psz
), psz
); return *this; }
525 wxString
& operator<<(wxChar ch
) { ConcatSelf(1, &ch
); return *this; }
528 void operator+=(const wxString
& s
) { (void)operator<<(s
); }
529 // string += C string
530 void operator+=(const wxChar
*psz
) { (void)operator<<(psz
); }
532 void operator+=(wxChar ch
) { (void)operator<<(ch
); }
534 // string += buffer (i.e. from wxGetString)
536 wxString
& operator<<(const wxWCharBuffer
& s
) { (void)operator<<((const wchar_t *)s
); return *this; }
537 void operator+=(const wxWCharBuffer
& s
) { (void)operator<<((const wchar_t *)s
); }
539 wxString
& operator<<(const wxCharBuffer
& s
) { (void)operator<<((const char *)s
); return *this; }
540 void operator+=(const wxCharBuffer
& s
) { (void)operator<<((const char *)s
); }
543 // string += C string
544 wxString
& Append(const wxChar
* psz
)
545 { ConcatSelf(wxStrlen(psz
), psz
); return *this; }
546 // append count copies of given character
547 wxString
& Append(wxChar ch
, size_t count
= 1u)
548 { wxString
str(ch
, count
); return *this << str
; }
550 // prepend a string, return the string itself
551 wxString
& Prepend(const wxString
& str
)
552 { *this = str
+ *this; return *this; }
554 // non-destructive concatenation
556 friend wxString WXDLLEXPORT
operator+(const wxString
& string1
, const wxString
& string2
);
558 friend wxString WXDLLEXPORT
operator+(const wxString
& string
, wxChar ch
);
560 friend wxString WXDLLEXPORT
operator+(wxChar ch
, const wxString
& string
);
562 friend wxString WXDLLEXPORT
operator+(const wxString
& string
, const wxChar
*psz
);
564 friend wxString WXDLLEXPORT
operator+(const wxChar
*psz
, const wxString
& string
);
566 // stream-like functions
567 // insert an int into string
568 wxString
& operator<<(int i
);
569 // insert a float into string
570 wxString
& operator<<(float f
);
571 // insert a double into string
572 wxString
& operator<<(double d
);
575 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
576 int Cmp(const wxChar
*psz
) const { return wxStrcmp(c_str(), psz
); }
577 // same as Cmp() but not case-sensitive
578 int CmpNoCase(const wxChar
*psz
) const { return Stricmp(c_str(), psz
); }
579 // test for the string equality, either considering case or not
580 // (if compareWithCase then the case matters)
581 bool IsSameAs(const wxChar
*psz
, bool compareWithCase
= TRUE
) const
582 { return (compareWithCase
? Cmp(psz
) : CmpNoCase(psz
)) == 0; }
584 // simple sub-string extraction
585 // return substring starting at nFirst of length nCount (or till the end
586 // if nCount = default value)
587 wxString
Mid(size_t nFirst
, size_t nCount
= wxSTRING_MAXLEN
) const;
589 // operator version of Mid()
590 wxString
operator()(size_t start
, size_t len
) const
591 { return Mid(start
, len
); }
593 // get first nCount characters
594 wxString
Left(size_t nCount
) const;
595 // get last nCount characters
596 wxString
Right(size_t nCount
) const;
597 // get all characters before the first occurence of ch
598 // (returns the whole string if ch not found)
599 wxString
BeforeFirst(wxChar ch
) const;
600 // get all characters before the last occurence of ch
601 // (returns empty string if ch not found)
602 wxString
BeforeLast(wxChar ch
) const;
603 // get all characters after the first occurence of ch
604 // (returns empty string if ch not found)
605 wxString
AfterFirst(wxChar ch
) const;
606 // get all characters after the last occurence of ch
607 // (returns the whole string if ch not found)
608 wxString
AfterLast(wxChar ch
) const;
610 // for compatibility only, use more explicitly named functions above
611 wxString
Before(wxChar ch
) const { return BeforeLast(ch
); }
612 wxString
After(wxChar ch
) const { return AfterFirst(ch
); }
615 // convert to upper case in place, return the string itself
616 wxString
& MakeUpper();
617 // convert to upper case, return the copy of the string
618 // Here's something to remember: BC++ doesn't like returns in inlines.
619 wxString
Upper() const ;
620 // convert to lower case in place, return the string itself
621 wxString
& MakeLower();
622 // convert to lower case, return the copy of the string
623 wxString
Lower() const ;
625 // trimming/padding whitespace (either side) and truncating
626 // remove spaces from left or from right (default) side
627 wxString
& Trim(bool bFromRight
= TRUE
);
628 // add nCount copies chPad in the beginning or at the end (default)
629 wxString
& Pad(size_t nCount
, wxChar chPad
= _T(' '), bool bFromRight
= TRUE
);
630 // truncate string to given length
631 wxString
& Truncate(size_t uiLen
);
633 // searching and replacing
634 // searching (return starting index, or -1 if not found)
635 int Find(wxChar ch
, bool bFromEnd
= FALSE
) const; // like strchr/strrchr
636 // searching (return starting index, or -1 if not found)
637 int Find(const wxChar
*pszSub
) const; // like strstr
638 // replace first (or all of bReplaceAll) occurences of substring with
639 // another string, returns the number of replacements made
640 size_t Replace(const wxChar
*szOld
,
642 bool bReplaceAll
= TRUE
);
644 // check if the string contents matches a mask containing '*' and '?'
645 bool Matches(const wxChar
*szMask
) const;
647 // formated input/output
648 // as sprintf(), returns the number of characters written or < 0 on error
649 int Printf(const wxChar
*pszFormat
, ...);
650 // as vprintf(), returns the number of characters written or < 0 on error
651 int PrintfV(const wxChar
* pszFormat
, va_list argptr
);
653 // raw access to string memory
654 // ensure that string has space for at least nLen characters
655 // only works if the data of this string is not shared
656 void Alloc(size_t nLen
);
657 // minimize the string's memory
658 // only works if the data of this string is not shared
660 // get writable buffer of at least nLen bytes. Unget() *must* be called
661 // a.s.a.p. to put string back in a reasonable state!
662 wxChar
*GetWriteBuf(size_t nLen
);
663 // call this immediately after GetWriteBuf() has been used
664 void UngetWriteBuf();
666 // wxWindows version 1 compatibility functions
669 wxString
SubString(size_t from
, size_t to
) const
670 { return Mid(from
, (to
- from
+ 1)); }
671 // values for second parameter of CompareTo function
672 enum caseCompare
{exact
, ignoreCase
};
673 // values for first parameter of Strip function
674 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
677 int sprintf(const wxChar
*pszFormat
, ...);
680 inline int CompareTo(const wxChar
* psz
, caseCompare cmp
= exact
) const
681 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
684 size_t Length() const { return Len(); }
685 // Count the number of characters
686 int Freq(wxChar ch
) const;
688 void LowerCase() { MakeLower(); }
690 void UpperCase() { MakeUpper(); }
691 // use Trim except that it doesn't change this string
692 wxString
Strip(stripType w
= trailing
) const;
694 // use Find (more general variants not yet supported)
695 size_t Index(const wxChar
* psz
) const { return Find(psz
); }
696 size_t Index(wxChar ch
) const { return Find(ch
); }
698 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
699 wxString
& RemoveLast() { return Truncate(Len() - 1); }
701 wxString
& Remove(size_t nStart
, size_t nLen
) { return erase( nStart
, nLen
); }
704 int First( const wxChar ch
) const { return Find(ch
); }
705 int First( const wxChar
* psz
) const { return Find(psz
); }
706 int First( const wxString
&str
) const { return Find(str
); }
707 int Last( const wxChar ch
) const { return Find(ch
, TRUE
); }
708 bool Contains(const wxString
& str
) const { return Find(str
) != -1; }
711 bool IsNull() const { return IsEmpty(); }
713 #ifdef wxSTD_STRING_COMPATIBILITY
714 // std::string compatibility functions
717 typedef wxChar value_type
;
718 typedef const value_type
*const_iterator
;
720 // an 'invalid' value for string index
721 static const size_t npos
;
724 // take nLen chars starting at nPos
725 wxString(const wxString
& str
, size_t nPos
, size_t nLen
)
727 wxASSERT( str
.GetStringData()->IsValid() );
728 InitWith(str
.c_str(), nPos
, nLen
== npos
? 0 : nLen
);
730 // take all characters from pStart to pEnd
731 wxString(const void *pStart
, const void *pEnd
);
733 // lib.string.capacity
734 // return the length of the string
735 size_t size() const { return Len(); }
736 // return the length of the string
737 size_t length() const { return Len(); }
738 // return the maximum size of the string
739 size_t max_size() const { return wxSTRING_MAXLEN
; }
740 // resize the string, filling the space with c if c != 0
741 void resize(size_t nSize
, wxChar ch
= _T('\0'));
742 // delete the contents of the string
743 void clear() { Empty(); }
744 // returns true if the string is empty
745 bool empty() const { return IsEmpty(); }
748 // return the character at position n
749 wxChar
at(size_t n
) const { return GetChar(n
); }
750 // returns the writable character at position n
751 wxChar
& at(size_t n
) { return GetWritableChar(n
); }
753 // first valid index position
754 const_iterator
begin() const { return wx_str(); }
755 // position one after the last valid one
756 const_iterator
end() const { return wx_str() + length(); }
758 // lib.string.modifiers
760 wxString
& append(const wxString
& str
)
761 { *this += str
; return *this; }
762 // append elements str[pos], ..., str[pos+n]
763 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
764 { ConcatSelf(n
, str
.c_str() + pos
); return *this; }
765 // append first n (or all if n == npos) characters of sz
766 wxString
& append(const wxChar
*sz
, size_t n
= npos
)
767 { ConcatSelf(n
== npos
? wxStrlen(sz
) : n
, sz
); return *this; }
769 // append n copies of ch
770 wxString
& append(size_t n
, wxChar ch
) { return Pad(n
, ch
); }
772 // same as `this_string = str'
773 wxString
& assign(const wxString
& str
) { return (*this) = str
; }
774 // same as ` = str[pos..pos + n]
775 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
776 { return *this = wxString((const wxChar
*)str
+ pos
, n
); }
777 // same as `= first n (or all if n == npos) characters of sz'
778 wxString
& assign(const wxChar
*sz
, size_t n
= npos
)
779 { return *this = wxString(sz
, n
); }
780 // same as `= n copies of ch'
781 wxString
& assign(size_t n
, wxChar ch
)
782 { return *this = wxString(ch
, n
); }
784 // insert another string
785 wxString
& insert(size_t nPos
, const wxString
& str
);
786 // insert n chars of str starting at nStart (in str)
787 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
788 { return insert(nPos
, wxString((const wxChar
*)str
+ nStart
, n
)); }
790 // insert first n (or all if n == npos) characters of sz
791 wxString
& insert(size_t nPos
, const wxChar
*sz
, size_t n
= npos
)
792 { return insert(nPos
, wxString(sz
, n
)); }
793 // insert n copies of ch
794 wxString
& insert(size_t nPos
, size_t n
, wxChar ch
)
795 { return insert(nPos
, wxString(ch
, n
)); }
797 // delete characters from nStart to nStart + nLen
798 wxString
& erase(size_t nStart
= 0, size_t nLen
= npos
);
800 // replaces the substring of length nLen starting at nStart
801 wxString
& replace(size_t nStart
, size_t nLen
, const wxChar
* sz
);
802 // replaces the substring with nCount copies of ch
803 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxChar ch
);
804 // replaces a substring with another substring
805 wxString
& replace(size_t nStart
, size_t nLen
,
806 const wxString
& str
, size_t nStart2
, size_t nLen2
);
807 // replaces the substring with first nCount chars of sz
808 wxString
& replace(size_t nStart
, size_t nLen
,
809 const wxChar
* sz
, size_t nCount
);
812 void swap(wxString
& str
);
814 // All find() functions take the nStart argument which specifies the
815 // position to start the search on, the default value is 0. All functions
816 // return npos if there were no match.
819 size_t find(const wxString
& str
, size_t nStart
= 0) const;
821 // VC++ 1.5 can't cope with this syntax.
822 #if !defined(__VISUALC__) || defined(__WIN32__)
823 // find first n characters of sz
824 size_t find(const wxChar
* sz
, size_t nStart
= 0, size_t n
= npos
) const;
827 // Gives a duplicate symbol (presumably a case-insensitivity problem)
828 #if !defined(__BORLANDC__)
829 // find the first occurence of character ch after nStart
830 size_t find(wxChar ch
, size_t nStart
= 0) const;
832 // rfind() family is exactly like find() but works right to left
834 // as find, but from the end
835 size_t rfind(const wxString
& str
, size_t nStart
= npos
) const;
837 // VC++ 1.5 can't cope with this syntax.
838 #if !defined(__VISUALC__) || defined(__WIN32__)
839 // as find, but from the end
840 size_t rfind(const wxChar
* sz
, size_t nStart
= npos
,
841 size_t n
= npos
) const;
842 // as find, but from the end
843 size_t rfind(wxChar ch
, size_t nStart
= npos
) const;
846 // find first/last occurence of any character in the set
848 // as strpbrk() but starts at nStart, returns npos if not found
849 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
850 { return find_first_of(str
.c_str(), nStart
); }
852 size_t find_first_of(const wxChar
* sz
, size_t nStart
= 0) const;
853 // same as find(char, size_t)
854 size_t find_first_of(wxChar c
, size_t nStart
= 0) const
855 { return find(c
, nStart
); }
856 // find the last (starting from nStart) char from str in this string
857 size_t find_last_of (const wxString
& str
, size_t nStart
= npos
) const
858 { return find_last_of(str
.c_str(), nStart
); }
860 size_t find_last_of (const wxChar
* sz
, size_t nStart
= npos
) const;
862 size_t find_last_of(wxChar c
, size_t nStart
= npos
) const
863 { return rfind(c
, nStart
); }
865 // find first/last occurence of any character not in the set
867 // as strspn() (starting from nStart), returns npos on failure
868 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
869 { return find_first_not_of(str
.c_str(), nStart
); }
871 size_t find_first_not_of(const wxChar
* sz
, size_t nStart
= 0) const;
873 size_t find_first_not_of(wxChar ch
, size_t nStart
= 0) const;
875 size_t find_last_not_of(const wxString
& str
, size_t nStart
=npos
) const;
877 size_t find_last_not_of(const wxChar
* sz
, size_t nStart
= npos
) const;
879 size_t find_last_not_of(wxChar ch
, size_t nStart
= npos
) const;
881 // All compare functions return -1, 0 or 1 if the [sub]string is less,
882 // equal or greater than the compare() argument.
884 // just like strcmp()
885 int compare(const wxString
& str
) const { return Cmp(str
); }
886 // comparison with a substring
887 int compare(size_t nStart
, size_t nLen
, const wxString
& str
) const;
888 // comparison of 2 substrings
889 int compare(size_t nStart
, size_t nLen
,
890 const wxString
& str
, size_t nStart2
, size_t nLen2
) const;
891 // just like strcmp()
892 int compare(const wxChar
* sz
) const { return Cmp(sz
); }
893 // substring comparison with first nCount characters of sz
894 int compare(size_t nStart
, size_t nLen
,
895 const wxChar
* sz
, size_t nCount
= npos
) const;
897 // substring extraction
898 wxString
substr(size_t nStart
= 0, size_t nLen
= npos
) const;
899 #endif // wxSTD_STRING_COMPATIBILITY
902 // ----------------------------------------------------------------------------
903 // The string array uses it's knowledge of internal structure of the wxString
904 // class to optimize string storage. Normally, we would store pointers to
905 // string, but as wxString is, in fact, itself a pointer (sizeof(wxString) is
906 // sizeof(char *)) we store these pointers instead. The cast to "wxString *" is
907 // really all we need to turn such pointer into a string!
909 // Of course, it can be called a dirty hack, but we use twice less memory and
910 // this approach is also more speed efficient, so it's probably worth it.
912 // Usage notes: when a string is added/inserted, a new copy of it is created,
913 // so the original string may be safely deleted. When a string is retrieved
914 // from the array (operator[] or Item() method), a reference is returned.
915 // ----------------------------------------------------------------------------
916 class WXDLLEXPORT wxArrayString
919 // type of function used by wxArrayString::Sort()
920 typedef int (*CompareFunction
)(const wxString
& first
,
921 const wxString
& second
);
923 // constructors and destructor
927 wxArrayString(const wxArrayString
& array
);
928 // assignment operator
929 wxArrayString
& operator=(const wxArrayString
& src
);
930 // not virtual, this class should not be derived from
934 // empties the list, but doesn't release memory
936 // empties the list and releases memory
938 // preallocates memory for given number of items
939 void Alloc(size_t nCount
);
940 // minimzes the memory usage (by freeing all extra memory)
944 // number of elements in the array
945 size_t GetCount() const { return m_nCount
; }
947 bool IsEmpty() const { return m_nCount
== 0; }
948 // number of elements in the array (GetCount is preferred API)
949 size_t Count() const { return m_nCount
; }
951 // items access (range checking is done in debug version)
952 // get item at position uiIndex
953 wxString
& Item(size_t nIndex
) const
954 { wxASSERT( nIndex
< m_nCount
); return *(wxString
*)&(m_pItems
[nIndex
]); }
956 wxString
& operator[](size_t nIndex
) const { return Item(nIndex
); }
958 wxString
& Last() const { wxASSERT( !IsEmpty() ); return Item(Count() - 1); }
961 // Search the element in the array, starting from the beginning if
962 // bFromEnd is FALSE or from end otherwise. If bCase, comparison is case
963 // sensitive (default). Returns index of the first item matched or
965 int Index (const wxChar
*sz
, bool bCase
= TRUE
, bool bFromEnd
= FALSE
) const;
966 // add new element at the end
967 void Add(const wxString
& str
);
968 // add new element at given position
969 void Insert(const wxString
& str
, size_t uiIndex
);
970 // remove first item matching this value
971 void Remove(const wxChar
*sz
);
972 // remove item by index
973 void Remove(size_t nIndex
);
976 // sort array elements in alphabetical order (or reversed alphabetical
977 // order if reverseOrder parameter is TRUE)
978 void Sort(bool reverseOrder
= FALSE
);
979 // sort array elements using specified comparaison function
980 void Sort(CompareFunction compareFunction
);
983 void Grow(); // makes array bigger if needed
984 void Free(); // free the string stored
986 void DoSort(); // common part of all Sort() variants
988 size_t m_nSize
, // current size of the array
989 m_nCount
; // current number of elements
991 wxChar
**m_pItems
; // pointer to data
994 // ---------------------------------------------------------------------------
995 // wxString comparison functions: operator versions are always case sensitive
996 // ---------------------------------------------------------------------------
998 inline bool operator==(const wxString
& s1
, const wxString
& s2
) { return (s1
.Cmp(s2
) == 0); }
1000 inline bool operator==(const wxString
& s1
, const wxChar
* s2
) { return (s1
.Cmp(s2
) == 0); }
1002 inline bool operator==(const wxChar
* s1
, const wxString
& s2
) { return (s2
.Cmp(s1
) == 0); }
1004 inline bool operator!=(const wxString
& s1
, const wxString
& s2
) { return (s1
.Cmp(s2
) != 0); }
1006 inline bool operator!=(const wxString
& s1
, const wxChar
* s2
) { return (s1
.Cmp(s2
) != 0); }
1008 inline bool operator!=(const wxChar
* s1
, const wxString
& s2
) { return (s2
.Cmp(s1
) != 0); }
1010 inline bool operator< (const wxString
& s1
, const wxString
& s2
) { return (s1
.Cmp(s2
) < 0); }
1012 inline bool operator< (const wxString
& s1
, const wxChar
* s2
) { return (s1
.Cmp(s2
) < 0); }
1014 inline bool operator< (const wxChar
* s1
, const wxString
& s2
) { return (s2
.Cmp(s1
) > 0); }
1016 inline bool operator> (const wxString
& s1
, const wxString
& s2
) { return (s1
.Cmp(s2
) > 0); }
1018 inline bool operator> (const wxString
& s1
, const wxChar
* s2
) { return (s1
.Cmp(s2
) > 0); }
1020 inline bool operator> (const wxChar
* s1
, const wxString
& s2
) { return (s2
.Cmp(s1
) < 0); }
1022 inline bool operator<=(const wxString
& s1
, const wxString
& s2
) { return (s1
.Cmp(s2
) <= 0); }
1024 inline bool operator<=(const wxString
& s1
, const wxChar
* s2
) { return (s1
.Cmp(s2
) <= 0); }
1026 inline bool operator<=(const wxChar
* s1
, const wxString
& s2
) { return (s2
.Cmp(s1
) >= 0); }
1028 inline bool operator>=(const wxString
& s1
, const wxString
& s2
) { return (s1
.Cmp(s2
) >= 0); }
1030 inline bool operator>=(const wxString
& s1
, const wxChar
* s2
) { return (s1
.Cmp(s2
) >= 0); }
1032 inline bool operator>=(const wxChar
* s1
, const wxString
& s2
) { return (s2
.Cmp(s1
) <= 0); }
1034 wxString WXDLLEXPORT
operator+(const wxString
& string1
, const wxString
& string2
);
1035 wxString WXDLLEXPORT
operator+(const wxString
& string
, wxChar ch
);
1036 wxString WXDLLEXPORT
operator+(wxChar ch
, const wxString
& string
);
1037 wxString WXDLLEXPORT
operator+(const wxString
& string
, const wxChar
*psz
);
1038 wxString WXDLLEXPORT
operator+(const wxChar
*psz
, const wxString
& string
);
1040 inline wxString WXDLLEXPORT
operator+(const wxString
& string
, const wxWCharBuffer
& buf
)
1041 { return string
+ (const wchar_t *)buf
; }
1042 inline wxString WXDLLEXPORT
operator+(const wxWCharBuffer
& buf
, const wxString
& string
)
1043 { return (const wchar_t *)buf
+ string
; }
1045 inline wxString WXDLLEXPORT
operator+(const wxString
& string
, const wxCharBuffer
& buf
)
1046 { return string
+ (const char *)buf
; }
1047 inline wxString WXDLLEXPORT
operator+(const wxCharBuffer
& buf
, const wxString
& string
)
1048 { return (const char *)buf
+ string
; }
1051 // ---------------------------------------------------------------------------
1052 // Implementation only from here until the end of file
1053 // ---------------------------------------------------------------------------
1055 #ifdef wxSTD_STRING_COMPATIBILITY
1057 #include "wx/ioswrap.h"
1059 WXDLLEXPORT istream
& operator>>(istream
& is
, wxString
& str
);
1061 #endif // wxSTD_STRING_COMPATIBILITY
1063 #endif // _WX_WXSTRINGH__