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 // return an empty wxString
160 class WXDLLEXPORT wxString
; // not yet defined
161 inline const wxString
& wxGetEmptyString() { return *(wxString
*)&wxEmptyString
; }
163 // ---------------------------------------------------------------------------
164 // string data prepended with some housekeeping info (used by wxString class),
165 // is never used directly (but had to be put here to allow inlining)
166 // ---------------------------------------------------------------------------
168 struct WXDLLEXPORT wxStringData
170 int nRefs
; // reference count
171 size_t nDataLength
, // actual string length
172 nAllocLength
; // allocated memory size
174 // mimics declaration 'wxChar data[nAllocLength]'
175 wxChar
* data() const { return (wxChar
*)(this + 1); }
177 // empty string has a special ref count so it's never deleted
178 bool IsEmpty() const { return (nRefs
== -1); }
179 bool IsShared() const { return (nRefs
> 1); }
182 void Lock() { if ( !IsEmpty() ) nRefs
++; }
183 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) free(this); }
185 // if we had taken control over string memory (GetWriteBuf), it's
186 // intentionally put in invalid state
187 void Validate(bool b
) { nRefs
= (b
? 1 : 0); }
188 bool IsValid() const { return (nRefs
!= 0); }
191 // ---------------------------------------------------------------------------
192 // This is (yet another one) String class for C++ programmers. It doesn't use
193 // any of "advanced" C++ features (i.e. templates, exceptions, namespaces...)
194 // thus you should be able to compile it with practicaly any C++ compiler.
195 // This class uses copy-on-write technique, i.e. identical strings share the
196 // same memory as long as neither of them is changed.
198 // This class aims to be as compatible as possible with the new standard
199 // std::string class, but adds some additional functions and should be at
200 // least as efficient than the standard implementation.
202 // Performance note: it's more efficient to write functions which take "const
203 // String&" arguments than "const char *" if you assign the argument to
206 // It was compiled and tested under Win32, Linux (libc 5 & 6), Solaris 5.5.
209 // - ressource support (string tables in ressources)
210 // - more wide character (UNICODE) support
211 // - regular expressions support
212 // ---------------------------------------------------------------------------
214 #ifdef WXSTRING_IS_WXOBJECT
215 class WXDLLEXPORT wxString
: public wxObject
217 DECLARE_DYNAMIC_CLASS(wxString
)
218 #else //WXSTRING_IS_WXOBJECT
219 class WXDLLEXPORT wxString
221 #endif //WXSTRING_IS_WXOBJECT
223 friend class WXDLLEXPORT wxArrayString
;
225 // NB: special care was taken in arranging the member functions in such order
226 // that all inline functions can be effectively inlined, verify that all
227 // performace critical functions are still inlined if you change order!
229 // points to data preceded by wxStringData structure with ref count info
232 // accessor to string data
233 wxStringData
* GetStringData() const { return (wxStringData
*)m_pchData
- 1; }
235 // string (re)initialization functions
236 // initializes the string to the empty value (must be called only from
237 // ctors, use Reinit() otherwise)
238 void Init() { m_pchData
= (wxChar
*)wxEmptyString
; }
239 // initializaes the string with (a part of) C-string
240 void InitWith(const wxChar
*psz
, size_t nPos
= 0, size_t nLen
= wxSTRING_MAXLEN
);
241 // as Init, but also frees old data
242 void Reinit() { GetStringData()->Unlock(); Init(); }
245 // allocates memory for string of lenght nLen
246 void AllocBuffer(size_t nLen
);
247 // copies data to another string
248 void AllocCopy(wxString
&, int, int) const;
249 // effectively copies data to string
250 void AssignCopy(size_t, const wxChar
*);
252 // append a (sub)string
253 void ConcatSelf(int nLen
, const wxChar
*src
);
255 // functions called before writing to the string: they copy it if there
256 // are other references to our data (should be the only owner when writing)
257 void CopyBeforeWrite();
258 void AllocBeforeWrite(size_t);
260 // this method is not implemented - there is _no_ conversion from int to
261 // string, you're doing something wrong if the compiler wants to call it!
263 // try `s << i' or `s.Printf("%d", i)' instead
268 // constructors and destructor
269 // ctor for an empty string
270 wxString() { Init(); }
272 wxString(const wxString
& stringSrc
)
274 wxASSERT( stringSrc
.GetStringData()->IsValid() );
276 if ( stringSrc
.IsEmpty() ) {
277 // nothing to do for an empty string
281 m_pchData
= stringSrc
.m_pchData
; // share same data
282 GetStringData()->Lock(); // => one more copy
285 // string containing nRepeat copies of ch
286 wxString(wxChar ch
, size_t nRepeat
= 1);
287 // ctor takes first nLength characters from C string
288 // (default value of wxSTRING_MAXLEN means take all the string)
289 wxString(const wxChar
*psz
, size_t nLength
= wxSTRING_MAXLEN
)
290 { InitWith(psz
, 0, nLength
); }
293 // from multibyte string
294 // (NB: nLength is right now number of Unicode characters, not
295 // characters in psz! So try not to use it yet!)
296 wxString(const char *psz
, wxMBConv
& conv
= wxConvLibc
, size_t nLength
= wxSTRING_MAXLEN
);
297 // from wxWCharBuffer (i.e. return from wxGetString)
298 wxString(const wxWCharBuffer
& psz
)
299 { InitWith(psz
, 0, wxSTRING_MAXLEN
); }
301 // from C string (for compilers using unsigned char)
302 wxString(const unsigned char* psz
, size_t nLength
= wxSTRING_MAXLEN
)
303 { InitWith((const char*)psz
, 0, nLength
); }
304 // from multibyte string
305 wxString(const char *psz
, wxMBConv
& WXUNUSED(conv
), size_t nLength
= wxSTRING_MAXLEN
)
306 { InitWith(psz
, 0, nLength
); }
309 // from wide (Unicode) string
310 wxString(const wchar_t *pwz
);
311 #endif // !wxUSE_WCHAR_T
314 wxString(const wxCharBuffer
& psz
)
315 { InitWith(psz
, 0, wxSTRING_MAXLEN
); }
316 #endif // Unicode/ANSI
318 // dtor is not virtual, this class must not be inherited from!
319 ~wxString() { GetStringData()->Unlock(); }
321 // generic attributes & operations
322 // as standard strlen()
323 size_t Len() const { return GetStringData()->nDataLength
; }
324 // string contains any characters?
325 bool IsEmpty() const { return Len() == 0; }
326 // empty string is "FALSE", so !str will return TRUE
327 bool operator!() const { return IsEmpty(); }
328 // empty string contents
335 wxASSERT( GetStringData()->nDataLength
== 0 );
337 // empty the string and free memory
340 if ( !GetStringData()->IsEmpty() )
343 wxASSERT( GetStringData()->nDataLength
== 0 ); // should be empty
344 wxASSERT( GetStringData()->nAllocLength
== 0 ); // and not own any memory
349 bool IsAscii() const;
351 bool IsNumber() const;
355 // data access (all indexes are 0 based)
357 wxChar
GetChar(size_t n
) const
358 { ASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
360 wxChar
& GetWritableChar(size_t n
)
361 { ASSERT_VALID_INDEX( n
); CopyBeforeWrite(); return m_pchData
[n
]; }
363 void SetChar(size_t n
, wxChar ch
)
364 { ASSERT_VALID_INDEX( n
); CopyBeforeWrite(); m_pchData
[n
] = ch
; }
366 // get last character
368 { wxASSERT( !IsEmpty() ); return m_pchData
[Len() - 1]; }
369 // get writable last character
371 { wxASSERT( !IsEmpty() ); CopyBeforeWrite(); return m_pchData
[Len()-1]; }
373 // operator version of GetChar
374 wxChar
operator[](size_t n
) const
375 { ASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
377 // operator version of GetChar
378 wxChar
operator[](int n
) const
379 { ASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
380 // operator version of GetChar
381 wxChar
operator[](unsigned int n
) const
382 { ASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
383 // operator version of GetWritableChar
384 wxChar
& operator[](size_t n
)
385 { ASSERT_VALID_INDEX( n
); CopyBeforeWrite(); return m_pchData
[n
]; }
387 // implicit conversion to C string
388 operator const wxChar
*() const { return m_pchData
; }
389 // explicit conversion to C string (use this with printf()!)
390 const wxChar
* c_str() const { return m_pchData
; }
391 // (and this with [wx]Printf()!)
392 const wxChar
* wx_str() const { return m_pchData
; }
393 // identical to c_str()
394 const wxChar
* GetData() const { return m_pchData
; }
396 // conversions with (possible) format convertions: have to return a
397 // buffer with temporary data
399 const wxCharBuffer
mb_str(wxMBConv
& conv
= wxConvLibc
) const { return conv
.cWC2MB(m_pchData
); }
400 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
402 const wxChar
* wc_str(wxMBConv
& WXUNUSED(conv
) = wxConvLibc
) const { return m_pchData
; }
405 const wxCharBuffer
fn_str() const { return mb_str(wxConvFile
); }
407 const wxChar
* fn_str() const { return m_pchData
; }
408 #endif // wxMBFILES/!wxMBFILES
411 const wxChar
* mb_str(wxMBConv
& WXUNUSED(conv
) = wxConvLibc
) const
412 { return m_pchData
; }
413 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
415 const wxChar
* mb_str() const { return m_pchData
; }
416 const wxWX2MBbuf
mbc_str() const { return mb_str(); }
417 #endif // multibyte/!multibyte
419 const wxWCharBuffer
wc_str(wxMBConv
& conv
) const { return conv
.cMB2WC(m_pchData
); }
420 #endif // wxUSE_WCHAR_T
421 const wxChar
* fn_str() const { return m_pchData
; }
422 #endif // Unicode/ANSI
424 // overloaded assignment
425 // from another wxString
426 wxString
& operator=(const wxString
& stringSrc
);
428 wxString
& operator=(wxChar ch
);
430 wxString
& operator=(const wxChar
*psz
);
432 // from wxWCharBuffer
433 wxString
& operator=(const wxWCharBuffer
& psz
) { return operator=((const wchar_t *)psz
); }
435 // from another kind of C string
436 wxString
& operator=(const unsigned char* psz
);
438 // from a wide string
439 wxString
& operator=(const wchar_t *pwz
);
442 wxString
& operator=(const wxCharBuffer
& psz
) { return operator=((const char *)psz
); }
443 #endif // Unicode/ANSI
445 // string concatenation
446 // in place concatenation
448 Concatenate and return the result. Note that the left to right
449 associativity of << allows to write things like "str << str1 << str2
450 << ..." (unlike with +=)
453 wxString
& operator<<(const wxString
& s
)
455 wxASSERT( s
.GetStringData()->IsValid() );
457 ConcatSelf(s
.Len(), s
);
460 // string += C string
461 wxString
& operator<<(const wxChar
*psz
)
462 { ConcatSelf(wxStrlen(psz
), psz
); return *this; }
464 wxString
& operator<<(wxChar ch
) { ConcatSelf(1, &ch
); return *this; }
467 void operator+=(const wxString
& s
) { (void)operator<<(s
); }
468 // string += C string
469 void operator+=(const wxChar
*psz
) { (void)operator<<(psz
); }
471 void operator+=(wxChar ch
) { (void)operator<<(ch
); }
473 // string += buffer (i.e. from wxGetString)
475 wxString
& operator<<(const wxWCharBuffer
& s
) { (void)operator<<((const wchar_t *)s
); return *this; }
476 void operator+=(const wxWCharBuffer
& s
) { (void)operator<<((const wchar_t *)s
); }
478 wxString
& operator<<(const wxCharBuffer
& s
) { (void)operator<<((const char *)s
); return *this; }
479 void operator+=(const wxCharBuffer
& s
) { (void)operator<<((const char *)s
); }
482 // string += C string
483 wxString
& Append(const wxChar
* psz
)
484 { ConcatSelf(wxStrlen(psz
), psz
); return *this; }
485 // append count copies of given character
486 wxString
& Append(wxChar ch
, size_t count
= 1u)
487 { wxString
str(ch
, count
); return *this << str
; }
489 // prepend a string, return the string itself
490 wxString
& Prepend(const wxString
& str
)
491 { *this = str
+ *this; return *this; }
493 // non-destructive concatenation
495 friend wxString WXDLLEXPORT
operator+(const wxString
& string1
, const wxString
& string2
);
497 friend wxString WXDLLEXPORT
operator+(const wxString
& string
, wxChar ch
);
499 friend wxString WXDLLEXPORT
operator+(wxChar ch
, const wxString
& string
);
501 friend wxString WXDLLEXPORT
operator+(const wxString
& string
, const wxChar
*psz
);
503 friend wxString WXDLLEXPORT
operator+(const wxChar
*psz
, const wxString
& string
);
505 // stream-like functions
506 // insert an int into string
507 wxString
& operator<<(int i
);
508 // insert a float into string
509 wxString
& operator<<(float f
);
510 // insert a double into string
511 wxString
& operator<<(double d
);
514 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
515 int Cmp(const wxChar
*psz
) const { return wxStrcmp(c_str(), psz
); }
516 // same as Cmp() but not case-sensitive
517 int CmpNoCase(const wxChar
*psz
) const { return wxStricmp(c_str(), psz
); }
518 // test for the string equality, either considering case or not
519 // (if compareWithCase then the case matters)
520 bool IsSameAs(const wxChar
*psz
, bool compareWithCase
= TRUE
) const
521 { return (compareWithCase
? Cmp(psz
) : CmpNoCase(psz
)) == 0; }
522 // comparison with a signle character: returns TRUE if equal
523 bool IsSameAs(wxChar c
, bool compareWithCase
= TRUE
) const
525 return (Len() == 1) && (compareWithCase
? GetChar(0u) == c
526 : wxToupper(GetChar(0u)) == wxToupper(c
));
529 // simple sub-string extraction
530 // return substring starting at nFirst of length nCount (or till the end
531 // if nCount = default value)
532 wxString
Mid(size_t nFirst
, size_t nCount
= wxSTRING_MAXLEN
) const;
534 // operator version of Mid()
535 wxString
operator()(size_t start
, size_t len
) const
536 { return Mid(start
, len
); }
538 // get first nCount characters
539 wxString
Left(size_t nCount
) const;
540 // get last nCount characters
541 wxString
Right(size_t nCount
) const;
542 // get all characters before the first occurence of ch
543 // (returns the whole string if ch not found)
544 wxString
BeforeFirst(wxChar ch
) const;
545 // get all characters before the last occurence of ch
546 // (returns empty string if ch not found)
547 wxString
BeforeLast(wxChar ch
) const;
548 // get all characters after the first occurence of ch
549 // (returns empty string if ch not found)
550 wxString
AfterFirst(wxChar ch
) const;
551 // get all characters after the last occurence of ch
552 // (returns the whole string if ch not found)
553 wxString
AfterLast(wxChar ch
) const;
555 // for compatibility only, use more explicitly named functions above
556 wxString
Before(wxChar ch
) const { return BeforeLast(ch
); }
557 wxString
After(wxChar ch
) const { return AfterFirst(ch
); }
560 // convert to upper case in place, return the string itself
561 wxString
& MakeUpper();
562 // convert to upper case, return the copy of the string
563 // Here's something to remember: BC++ doesn't like returns in inlines.
564 wxString
Upper() const ;
565 // convert to lower case in place, return the string itself
566 wxString
& MakeLower();
567 // convert to lower case, return the copy of the string
568 wxString
Lower() const ;
570 // trimming/padding whitespace (either side) and truncating
571 // remove spaces from left or from right (default) side
572 wxString
& Trim(bool bFromRight
= TRUE
);
573 // add nCount copies chPad in the beginning or at the end (default)
574 wxString
& Pad(size_t nCount
, wxChar chPad
= wxT(' '), bool bFromRight
= TRUE
);
575 // truncate string to given length
576 wxString
& Truncate(size_t uiLen
);
578 // searching and replacing
579 // searching (return starting index, or -1 if not found)
580 int Find(wxChar ch
, bool bFromEnd
= FALSE
) const; // like strchr/strrchr
581 // searching (return starting index, or -1 if not found)
582 int Find(const wxChar
*pszSub
) const; // like strstr
583 // replace first (or all of bReplaceAll) occurences of substring with
584 // another string, returns the number of replacements made
585 size_t Replace(const wxChar
*szOld
,
587 bool bReplaceAll
= TRUE
);
589 // check if the string contents matches a mask containing '*' and '?'
590 bool Matches(const wxChar
*szMask
) const;
592 // formated input/output
593 // as sprintf(), returns the number of characters written or < 0 on error
594 int Printf(const wxChar
*pszFormat
, ...);
595 // as vprintf(), returns the number of characters written or < 0 on error
596 int PrintfV(const wxChar
* pszFormat
, va_list argptr
);
598 // raw access to string memory
599 // ensure that string has space for at least nLen characters
600 // only works if the data of this string is not shared
601 void Alloc(size_t nLen
);
602 // minimize the string's memory
603 // only works if the data of this string is not shared
605 // get writable buffer of at least nLen bytes. Unget() *must* be called
606 // a.s.a.p. to put string back in a reasonable state!
607 wxChar
*GetWriteBuf(size_t nLen
);
608 // call this immediately after GetWriteBuf() has been used
609 void UngetWriteBuf();
611 // wxWindows version 1 compatibility functions
614 wxString
SubString(size_t from
, size_t to
) const
615 { return Mid(from
, (to
- from
+ 1)); }
616 // values for second parameter of CompareTo function
617 enum caseCompare
{exact
, ignoreCase
};
618 // values for first parameter of Strip function
619 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
622 int sprintf(const wxChar
*pszFormat
, ...);
625 inline int CompareTo(const wxChar
* psz
, caseCompare cmp
= exact
) const
626 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
629 size_t Length() const { return Len(); }
630 // Count the number of characters
631 int Freq(wxChar ch
) const;
633 void LowerCase() { MakeLower(); }
635 void UpperCase() { MakeUpper(); }
636 // use Trim except that it doesn't change this string
637 wxString
Strip(stripType w
= trailing
) const;
639 // use Find (more general variants not yet supported)
640 size_t Index(const wxChar
* psz
) const { return Find(psz
); }
641 size_t Index(wxChar ch
) const { return Find(ch
); }
643 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
644 wxString
& RemoveLast() { return Truncate(Len() - 1); }
646 wxString
& Remove(size_t nStart
, size_t nLen
) { return erase( nStart
, nLen
); }
649 int First( const wxChar ch
) const { return Find(ch
); }
650 int First( const wxChar
* psz
) const { return Find(psz
); }
651 int First( const wxString
&str
) const { return Find(str
); }
652 int Last( const wxChar ch
) const { return Find(ch
, TRUE
); }
653 bool Contains(const wxString
& str
) const { return Find(str
) != -1; }
656 bool IsNull() const { return IsEmpty(); }
658 #ifdef wxSTD_STRING_COMPATIBILITY
659 // std::string compatibility functions
662 typedef wxChar value_type
;
663 typedef const value_type
*const_iterator
;
665 // an 'invalid' value for string index
666 static const size_t npos
;
669 // take nLen chars starting at nPos
670 wxString(const wxString
& str
, size_t nPos
, size_t nLen
)
672 wxASSERT( str
.GetStringData()->IsValid() );
673 InitWith(str
.c_str(), nPos
, nLen
== npos
? 0 : nLen
);
675 // take all characters from pStart to pEnd
676 wxString(const void *pStart
, const void *pEnd
);
678 // lib.string.capacity
679 // return the length of the string
680 size_t size() const { return Len(); }
681 // return the length of the string
682 size_t length() const { return Len(); }
683 // return the maximum size of the string
684 size_t max_size() const { return wxSTRING_MAXLEN
; }
685 // resize the string, filling the space with c if c != 0
686 void resize(size_t nSize
, wxChar ch
= wxT('\0'));
687 // delete the contents of the string
688 void clear() { Empty(); }
689 // returns true if the string is empty
690 bool empty() const { return IsEmpty(); }
693 // return the character at position n
694 wxChar
at(size_t n
) const { return GetChar(n
); }
695 // returns the writable character at position n
696 wxChar
& at(size_t n
) { return GetWritableChar(n
); }
698 // first valid index position
699 const_iterator
begin() const { return wx_str(); }
700 // position one after the last valid one
701 const_iterator
end() const { return wx_str() + length(); }
703 // lib.string.modifiers
705 wxString
& append(const wxString
& str
)
706 { *this += str
; return *this; }
707 // append elements str[pos], ..., str[pos+n]
708 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
709 { ConcatSelf(n
, str
.c_str() + pos
); return *this; }
710 // append first n (or all if n == npos) characters of sz
711 wxString
& append(const wxChar
*sz
, size_t n
= npos
)
712 { ConcatSelf(n
== npos
? wxStrlen(sz
) : n
, sz
); return *this; }
714 // append n copies of ch
715 wxString
& append(size_t n
, wxChar ch
) { return Pad(n
, ch
); }
717 // same as `this_string = str'
718 wxString
& assign(const wxString
& str
) { return (*this) = str
; }
719 // same as ` = str[pos..pos + n]
720 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
721 { return *this = wxString((const wxChar
*)str
+ pos
, n
); }
722 // same as `= first n (or all if n == npos) characters of sz'
723 wxString
& assign(const wxChar
*sz
, size_t n
= npos
)
724 { return *this = wxString(sz
, n
); }
725 // same as `= n copies of ch'
726 wxString
& assign(size_t n
, wxChar ch
)
727 { return *this = wxString(ch
, n
); }
729 // insert another string
730 wxString
& insert(size_t nPos
, const wxString
& str
);
731 // insert n chars of str starting at nStart (in str)
732 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
733 { return insert(nPos
, wxString((const wxChar
*)str
+ nStart
, n
)); }
735 // insert first n (or all if n == npos) characters of sz
736 wxString
& insert(size_t nPos
, const wxChar
*sz
, size_t n
= npos
)
737 { return insert(nPos
, wxString(sz
, n
)); }
738 // insert n copies of ch
739 wxString
& insert(size_t nPos
, size_t n
, wxChar ch
)
740 { return insert(nPos
, wxString(ch
, n
)); }
742 // delete characters from nStart to nStart + nLen
743 wxString
& erase(size_t nStart
= 0, size_t nLen
= npos
);
745 // replaces the substring of length nLen starting at nStart
746 wxString
& replace(size_t nStart
, size_t nLen
, const wxChar
* sz
);
747 // replaces the substring with nCount copies of ch
748 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxChar ch
);
749 // replaces a substring with another substring
750 wxString
& replace(size_t nStart
, size_t nLen
,
751 const wxString
& str
, size_t nStart2
, size_t nLen2
);
752 // replaces the substring with first nCount chars of sz
753 wxString
& replace(size_t nStart
, size_t nLen
,
754 const wxChar
* sz
, size_t nCount
);
757 void swap(wxString
& str
);
759 // All find() functions take the nStart argument which specifies the
760 // position to start the search on, the default value is 0. All functions
761 // return npos if there were no match.
764 size_t find(const wxString
& str
, size_t nStart
= 0) const;
766 // VC++ 1.5 can't cope with this syntax.
767 #if !defined(__VISUALC__) || defined(__WIN32__)
768 // find first n characters of sz
769 size_t find(const wxChar
* sz
, size_t nStart
= 0, size_t n
= npos
) const;
772 // Gives a duplicate symbol (presumably a case-insensitivity problem)
773 #if !defined(__BORLANDC__)
774 // find the first occurence of character ch after nStart
775 size_t find(wxChar ch
, size_t nStart
= 0) const;
777 // rfind() family is exactly like find() but works right to left
779 // as find, but from the end
780 size_t rfind(const wxString
& str
, size_t nStart
= npos
) const;
782 // VC++ 1.5 can't cope with this syntax.
783 #if !defined(__VISUALC__) || defined(__WIN32__)
784 // as find, but from the end
785 size_t rfind(const wxChar
* sz
, size_t nStart
= npos
,
786 size_t n
= npos
) const;
787 // as find, but from the end
788 size_t rfind(wxChar ch
, size_t nStart
= npos
) const;
791 // find first/last occurence of any character in the set
793 // as strpbrk() but starts at nStart, returns npos if not found
794 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
795 { return find_first_of(str
.c_str(), nStart
); }
797 size_t find_first_of(const wxChar
* sz
, size_t nStart
= 0) const;
798 // same as find(char, size_t)
799 size_t find_first_of(wxChar c
, size_t nStart
= 0) const
800 { return find(c
, nStart
); }
801 // find the last (starting from nStart) char from str in this string
802 size_t find_last_of (const wxString
& str
, size_t nStart
= npos
) const
803 { return find_last_of(str
.c_str(), nStart
); }
805 size_t find_last_of (const wxChar
* sz
, size_t nStart
= npos
) const;
807 size_t find_last_of(wxChar c
, size_t nStart
= npos
) const
808 { return rfind(c
, nStart
); }
810 // find first/last occurence of any character not in the set
812 // as strspn() (starting from nStart), returns npos on failure
813 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
814 { return find_first_not_of(str
.c_str(), nStart
); }
816 size_t find_first_not_of(const wxChar
* sz
, size_t nStart
= 0) const;
818 size_t find_first_not_of(wxChar ch
, size_t nStart
= 0) const;
820 size_t find_last_not_of(const wxString
& str
, size_t nStart
=npos
) const;
822 size_t find_last_not_of(const wxChar
* sz
, size_t nStart
= npos
) const;
824 size_t find_last_not_of(wxChar ch
, size_t nStart
= npos
) const;
826 // All compare functions return -1, 0 or 1 if the [sub]string is less,
827 // equal or greater than the compare() argument.
829 // just like strcmp()
830 int compare(const wxString
& str
) const { return Cmp(str
); }
831 // comparison with a substring
832 int compare(size_t nStart
, size_t nLen
, const wxString
& str
) const;
833 // comparison of 2 substrings
834 int compare(size_t nStart
, size_t nLen
,
835 const wxString
& str
, size_t nStart2
, size_t nLen2
) const;
836 // just like strcmp()
837 int compare(const wxChar
* sz
) const { return Cmp(sz
); }
838 // substring comparison with first nCount characters of sz
839 int compare(size_t nStart
, size_t nLen
,
840 const wxChar
* sz
, size_t nCount
= npos
) const;
842 // substring extraction
843 wxString
substr(size_t nStart
= 0, size_t nLen
= npos
) const;
844 #endif // wxSTD_STRING_COMPATIBILITY
847 // ----------------------------------------------------------------------------
848 // The string array uses it's knowledge of internal structure of the wxString
849 // class to optimize string storage. Normally, we would store pointers to
850 // string, but as wxString is, in fact, itself a pointer (sizeof(wxString) is
851 // sizeof(char *)) we store these pointers instead. The cast to "wxString *" is
852 // really all we need to turn such pointer into a string!
854 // Of course, it can be called a dirty hack, but we use twice less memory and
855 // this approach is also more speed efficient, so it's probably worth it.
857 // Usage notes: when a string is added/inserted, a new copy of it is created,
858 // so the original string may be safely deleted. When a string is retrieved
859 // from the array (operator[] or Item() method), a reference is returned.
860 // ----------------------------------------------------------------------------
862 class WXDLLEXPORT wxArrayString
865 // type of function used by wxArrayString::Sort()
866 typedef int (*CompareFunction
)(const wxString
& first
,
867 const wxString
& second
);
869 // constructors and destructor
870 // default ctor: if autoSort is TRUE, the array is always sorted (in
871 // alphabetical order)
872 wxArrayString(bool autoSort
= FALSE
);
874 wxArrayString(const wxArrayString
& array
);
875 // assignment operator
876 wxArrayString
& operator=(const wxArrayString
& src
);
877 // not virtual, this class should not be derived from
881 // empties the list, but doesn't release memory
883 // empties the list and releases memory
885 // preallocates memory for given number of items
886 void Alloc(size_t nCount
);
887 // minimzes the memory usage (by freeing all extra memory)
891 // number of elements in the array
892 size_t GetCount() const { return m_nCount
; }
894 bool IsEmpty() const { return m_nCount
== 0; }
895 // number of elements in the array (GetCount is preferred API)
896 size_t Count() const { return m_nCount
; }
898 // items access (range checking is done in debug version)
899 // get item at position uiIndex
900 wxString
& Item(size_t nIndex
) const
901 { wxASSERT( nIndex
< m_nCount
); return *(wxString
*)&(m_pItems
[nIndex
]); }
903 wxString
& operator[](size_t nIndex
) const { return Item(nIndex
); }
905 wxString
& Last() const { wxASSERT( !IsEmpty() ); return Item(Count() - 1); }
908 // Search the element in the array, starting from the beginning if
909 // bFromEnd is FALSE or from end otherwise. If bCase, comparison is case
910 // sensitive (default). Returns index of the first item matched or
912 int Index (const wxChar
*sz
, bool bCase
= TRUE
, bool bFromEnd
= FALSE
) const;
913 // add new element at the end (if the array is not sorted), return its
915 size_t Add(const wxString
& str
);
916 // add new element at given position
917 void Insert(const wxString
& str
, size_t uiIndex
);
918 // remove first item matching this value
919 void Remove(const wxChar
*sz
);
920 // remove item by index
921 void Remove(size_t nIndex
);
924 // sort array elements in alphabetical order (or reversed alphabetical
925 // order if reverseOrder parameter is TRUE)
926 void Sort(bool reverseOrder
= FALSE
);
927 // sort array elements using specified comparaison function
928 void Sort(CompareFunction compareFunction
);
931 void Copy(const wxArrayString
& src
); // copies the contents of another array
934 void Grow(); // makes array bigger if needed
935 void Free(); // free all the strings stored
937 void DoSort(); // common part of all Sort() variants
939 size_t m_nSize
, // current size of the array
940 m_nCount
; // current number of elements
942 wxChar
**m_pItems
; // pointer to data
944 bool m_autoSort
; // if TRUE, keep the array always sorted
947 class WXDLLEXPORT wxSortedArrayString
: public wxArrayString
950 wxSortedArrayString() : wxArrayString(TRUE
)
952 wxSortedArrayString(const wxArrayString
& array
) : wxArrayString(TRUE
)
956 // ---------------------------------------------------------------------------
957 // wxString comparison functions: operator versions are always case sensitive
958 // ---------------------------------------------------------------------------
961 inline bool operator==(const wxString
& s1
, const wxString
& s2
) { return (s1
.Cmp(s2
) == 0); }
963 inline bool operator==(const wxString
& s1
, const wxChar
* s2
) { return (s1
.Cmp(s2
) == 0); }
965 inline bool operator==(const wxChar
* s1
, const wxString
& s2
) { return (s2
.Cmp(s1
) == 0); }
967 inline bool operator!=(const wxString
& s1
, const wxString
& s2
) { return (s1
.Cmp(s2
) != 0); }
969 inline bool operator!=(const wxString
& s1
, const wxChar
* s2
) { return (s1
.Cmp(s2
) != 0); }
971 inline bool operator!=(const wxChar
* s1
, const wxString
& s2
) { return (s2
.Cmp(s1
) != 0); }
973 inline bool operator< (const wxString
& s1
, const wxString
& s2
) { return (s1
.Cmp(s2
) < 0); }
975 inline bool operator< (const wxString
& s1
, const wxChar
* s2
) { return (s1
.Cmp(s2
) < 0); }
977 inline bool operator< (const wxChar
* s1
, const wxString
& s2
) { return (s2
.Cmp(s1
) > 0); }
979 inline bool operator> (const wxString
& s1
, const wxString
& s2
) { return (s1
.Cmp(s2
) > 0); }
981 inline bool operator> (const wxString
& s1
, const wxChar
* s2
) { return (s1
.Cmp(s2
) > 0); }
983 inline bool operator> (const wxChar
* s1
, const wxString
& s2
) { return (s2
.Cmp(s1
) < 0); }
985 inline bool operator<=(const wxString
& s1
, const wxString
& s2
) { return (s1
.Cmp(s2
) <= 0); }
987 inline bool operator<=(const wxString
& s1
, const wxChar
* s2
) { return (s1
.Cmp(s2
) <= 0); }
989 inline bool operator<=(const wxChar
* s1
, const wxString
& s2
) { return (s2
.Cmp(s1
) >= 0); }
991 inline bool operator>=(const wxString
& s1
, const wxString
& s2
) { return (s1
.Cmp(s2
) >= 0); }
993 inline bool operator>=(const wxString
& s1
, const wxChar
* s2
) { return (s1
.Cmp(s2
) >= 0); }
995 inline bool operator>=(const wxChar
* s1
, const wxString
& s2
) { return (s2
.Cmp(s1
) <= 0); }
997 // comparison with char
998 inline bool operator==(wxChar c
, const wxString
& s
) { return s
.IsSameAs(c
); }
999 inline bool operator==(const wxString
& s
, wxChar c
) { return s
.IsSameAs(c
); }
1000 inline bool operator!=(wxChar c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
1001 inline bool operator!=(const wxString
& s
, wxChar c
) { return !s
.IsSameAs(c
); }
1004 inline bool operator==(const wxString
& s1
, const wxWCharBuffer
& s2
)
1005 { return (s1
.Cmp((const wchar_t *)s2
) == 0); }
1006 inline bool operator==(const wxWCharBuffer
& s1
, const wxString
& s2
)
1007 { return (s2
.Cmp((const wchar_t *)s1
) == 0); }
1009 inline bool operator==(const wxString
& s1
, const wxCharBuffer
& s2
)
1010 { return (s1
.Cmp((const char *)s2
) == 0); }
1011 inline bool operator==(const wxCharBuffer
& s1
, const wxString
& s2
)
1012 { return (s2
.Cmp((const char *)s1
) == 0); }
1015 wxString WXDLLEXPORT
operator+(const wxString
& string1
, const wxString
& string2
);
1016 wxString WXDLLEXPORT
operator+(const wxString
& string
, wxChar ch
);
1017 wxString WXDLLEXPORT
operator+(wxChar ch
, const wxString
& string
);
1018 wxString WXDLLEXPORT
operator+(const wxString
& string
, const wxChar
*psz
);
1019 wxString WXDLLEXPORT
operator+(const wxChar
*psz
, const wxString
& string
);
1021 inline wxString WXDLLEXPORT
operator+(const wxString
& string
, const wxWCharBuffer
& buf
)
1022 { return string
+ (const wchar_t *)buf
; }
1023 inline wxString WXDLLEXPORT
operator+(const wxWCharBuffer
& buf
, const wxString
& string
)
1024 { return (const wchar_t *)buf
+ string
; }
1026 inline wxString WXDLLEXPORT
operator+(const wxString
& string
, const wxCharBuffer
& buf
)
1027 { return string
+ (const char *)buf
; }
1028 inline wxString WXDLLEXPORT
operator+(const wxCharBuffer
& buf
, const wxString
& string
)
1029 { return (const char *)buf
+ string
; }
1032 // ---------------------------------------------------------------------------
1033 // Implementation only from here until the end of file
1034 // ---------------------------------------------------------------------------
1036 // don't pollute the library user's name space
1037 #undef ASSERT_VALID_INDEX
1039 #if defined(wxSTD_STRING_COMPATIBILITY) && wxUSE_STD_IOSTREAM
1041 #include "wx/ioswrap.h"
1043 WXDLLEXPORT istream
& operator>>(istream
&, wxString
&);
1044 WXDLLEXPORT ostream
& operator<<(ostream
&, const wxString
&);
1046 #endif // wxSTD_STRING_COMPATIBILITY
1048 #endif // _WX_WXSTRINGH__