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 licence
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__
21 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
22 #pragma interface "string.h"
25 // ----------------------------------------------------------------------------
27 // ----------------------------------------------------------------------------
29 #include "wx/defs.h" // everybody should include this
31 #if defined(__WXMAC__) || defined(__VISAGECPP__)
35 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
36 // problem in VACPP V4 with including stdlib.h multiple times
37 // strconv includes it anyway
51 #include <strings.h> // for strcasecmp()
52 #endif // HAVE_STRINGS_H
54 #include "wx/wxchar.h" // for wxChar
55 #include "wx/buffer.h" // for wxCharBuffer
56 #include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes
58 // ---------------------------------------------------------------------------
60 // ---------------------------------------------------------------------------
62 // casts [unfortunately!] needed to call some broken functions which require
63 // "char *" instead of "const char *"
64 #define WXSTRINGCAST (wxChar *)(const wxChar *)
65 #define wxCSTRINGCAST (wxChar *)(const wxChar *)
66 #define wxMBSTRINGCAST (char *)(const char *)
67 #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *)
69 // implementation only
70 #define wxASSERT_VALID_INDEX(i) \
71 wxASSERT_MSG( (size_t)(i) <= length(), _T("invalid index in wxString") )
73 // ----------------------------------------------------------------------------
75 // ----------------------------------------------------------------------------
77 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
78 // must define this static for VA or else you get multiply defined symbols everywhere
79 extern const unsigned int wxSTRING_MAXLEN
;
82 // maximum possible length for a string means "take all string" everywhere
83 // (as sizeof(StringData) is unknown here, we substract 100)
84 const unsigned int wxSTRING_MAXLEN
= UINT_MAX
- 100;
88 // ----------------------------------------------------------------------------
90 // ----------------------------------------------------------------------------
92 // global pointer to empty string
93 extern WXDLLIMPEXP_DATA_BASE(const wxChar
*) wxEmptyString
;
95 // ---------------------------------------------------------------------------
96 // global functions complementing standard C string library replacements for
97 // strlen() and portable strcasecmp()
98 //---------------------------------------------------------------------------
100 // Use wxXXX() functions from wxchar.h instead! These functions are for
101 // backwards compatibility only.
103 // checks whether the passed in pointer is NULL and if the string is empty
104 inline bool IsEmpty(const char *p
) { return (!p
|| !*p
); }
106 // safe version of strlen() (returns 0 if passed NULL pointer)
107 inline size_t Strlen(const char *psz
)
108 { return psz
? strlen(psz
) : 0; }
110 // portable strcasecmp/_stricmp
111 inline int Stricmp(const char *psz1
, const char *psz2
)
113 #if defined(__VISUALC__) && defined(__WXWINCE__)
114 register char c1
, c2
;
116 c1
= tolower(*psz1
++);
117 c2
= tolower(*psz2
++);
118 } while ( c1
&& (c1
== c2
) );
121 #elif defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) )
122 return _stricmp(psz1
, psz2
);
123 #elif defined(__SC__)
124 return _stricmp(psz1
, psz2
);
125 #elif defined(__SALFORDC__)
126 return stricmp(psz1
, psz2
);
127 #elif defined(__BORLANDC__)
128 return stricmp(psz1
, psz2
);
129 #elif defined(__WATCOMC__)
130 return stricmp(psz1
, psz2
);
131 #elif defined(__DJGPP__)
132 return stricmp(psz1
, psz2
);
133 #elif defined(__EMX__)
134 return stricmp(psz1
, psz2
);
135 #elif defined(__WXPM__)
136 return stricmp(psz1
, psz2
);
137 #elif defined(__UNIX__) || defined(__GNUWIN32__)
138 return strcasecmp(psz1
, psz2
);
139 #elif defined(__MWERKS__) && !defined(__INTEL__)
140 register char c1
, c2
;
142 c1
= tolower(*psz1
++);
143 c2
= tolower(*psz2
++);
144 } while ( c1
&& (c1
== c2
) );
148 // almost all compilers/libraries provide this function (unfortunately under
149 // different names), that's why we don't implement our own which will surely
150 // be more efficient than this code (uncomment to use):
152 register char c1, c2;
154 c1 = tolower(*psz1++);
155 c2 = tolower(*psz2++);
156 } while ( c1 && (c1 == c2) );
161 #error "Please define string case-insensitive compare for your OS/compiler"
162 #endif // OS/compiler
165 // return an empty wxString
166 class WXDLLIMPEXP_BASE wxString
; // not yet defined
167 inline const wxString
& wxGetEmptyString() { return *(wxString
*)&wxEmptyString
; }
171 #include "wx/beforestd.h"
173 #include "wx/afterstd.h"
176 #ifdef HAVE_STD_WSTRING
177 typedef std::wstring wxStringBase
;
179 typedef std::basic_string
<wxChar
> wxStringBase
;
182 typedef std::string wxStringBase
;
185 #if (defined(__GNUG__) && (__GNUG__ < 3)) || \
186 (defined(_MSC_VER) && (_MSC_VER <= 1100))
187 #define wxSTRING_BASE_HASNT_CLEAR
190 #else // if !wxUSE_STL
192 #ifndef HAVE_STD_STRING_COMPARE
193 #define HAVE_STD_STRING_COMPARE
196 // ---------------------------------------------------------------------------
197 // string data prepended with some housekeeping info (used by wxString class),
198 // is never used directly (but had to be put here to allow inlining)
199 // ---------------------------------------------------------------------------
201 struct WXDLLIMPEXP_BASE wxStringData
203 int nRefs
; // reference count
204 size_t nDataLength
, // actual string length
205 nAllocLength
; // allocated memory size
207 // mimics declaration 'wxChar data[nAllocLength]'
208 wxChar
* data() const { return (wxChar
*)(this + 1); }
210 // empty string has a special ref count so it's never deleted
211 bool IsEmpty() const { return (nRefs
== -1); }
212 bool IsShared() const { return (nRefs
> 1); }
215 void Lock() { if ( !IsEmpty() ) nRefs
++; }
217 // VC++ will refuse to inline Unlock but profiling shows that it is wrong
218 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
219 // RN - VC6 Refuses to inline this anyway and spits out 2 warnings...
222 // VC++ free must take place in same DLL as allocation when using non dll
223 // run-time library (e.g. Multithreaded instead of Multithreaded DLL)
224 #if defined(__VISUALC__) && defined(_MT) && !defined(_DLL)
225 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) Free(); }
226 // we must not inline deallocation since allocation is not inlined
229 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) free(this); }
232 // if we had taken control over string memory (GetWriteBuf), it's
233 // intentionally put in invalid state
234 void Validate(bool b
) { nRefs
= (b
? 1 : 0); }
235 bool IsValid() const { return (nRefs
!= 0); }
238 class WXDLLIMPEXP_BASE wxStringBase
241 friend class WXDLLIMPEXP_BASE wxArrayString
;
244 // an 'invalid' value for string index, moved to this place due to a CW bug
245 static const size_t npos
;
247 // points to data preceded by wxStringData structure with ref count info
250 // accessor to string data
251 wxStringData
* GetStringData() const { return (wxStringData
*)m_pchData
- 1; }
253 // string (re)initialization functions
254 // initializes the string to the empty value (must be called only from
255 // ctors, use Reinit() otherwise)
256 void Init() { m_pchData
= (wxChar
*)wxEmptyString
; }
257 // initializaes the string with (a part of) C-string
258 void InitWith(const wxChar
*psz
, size_t nPos
= 0, size_t nLen
= npos
);
259 // as Init, but also frees old data
260 void Reinit() { GetStringData()->Unlock(); Init(); }
263 // allocates memory for string of length nLen
264 bool AllocBuffer(size_t nLen
);
265 // copies data to another string
266 bool AllocCopy(wxString
&, int, int) const;
267 // effectively copies data to string
268 bool AssignCopy(size_t, const wxChar
*);
270 // append a (sub)string
271 bool ConcatSelf(size_t nLen
, const wxChar
*src
, size_t nMaxLen
);
272 bool ConcatSelf(size_t nLen
, const wxChar
*src
)
273 { return ConcatSelf(nLen
, src
, nLen
); }
275 // functions called before writing to the string: they copy it if there
276 // are other references to our data (should be the only owner when writing)
277 bool CopyBeforeWrite();
278 bool AllocBeforeWrite(size_t);
280 // compatibility with wxString
281 bool Alloc(size_t nLen
);
284 typedef wxChar value_type
;
285 typedef wxChar char_type
;
286 typedef size_t size_type
;
287 typedef value_type
& reference
;
288 typedef const value_type
& const_reference
;
289 typedef value_type
* pointer
;
290 typedef const value_type
* const_pointer
;
291 typedef value_type
*iterator
;
292 typedef const value_type
*const_iterator
;
294 // constructors and destructor
295 // ctor for an empty string
296 wxStringBase() { Init(); }
298 wxStringBase(const wxStringBase
& stringSrc
)
300 wxASSERT_MSG( stringSrc
.GetStringData()->IsValid(),
301 _T("did you forget to call UngetWriteBuf()?") );
303 if ( stringSrc
.empty() ) {
304 // nothing to do for an empty string
308 m_pchData
= stringSrc
.m_pchData
; // share same data
309 GetStringData()->Lock(); // => one more copy
312 // string containing nRepeat copies of ch
313 wxStringBase(size_type nRepeat
, wxChar ch
);
314 // ctor takes first nLength characters from C string
315 // (default value of npos means take all the string)
316 wxStringBase(const wxChar
*psz
)
317 { InitWith(psz
, 0, npos
); }
318 wxStringBase(const wxChar
*psz
, size_t nLength
)
319 { InitWith(psz
, 0, nLength
); }
320 wxStringBase(const wxChar
*psz
, wxMBConv
& WXUNUSED(conv
), size_t nLength
= npos
)
321 { InitWith(psz
, 0, nLength
); }
322 // take nLen chars starting at nPos
323 wxStringBase(const wxStringBase
& str
, size_t nPos
, size_t nLen
)
325 wxASSERT_MSG( str
.GetStringData()->IsValid(),
326 _T("did you forget to call UngetWriteBuf()?") );
328 size_t strLen
= str
.length() - nPos
; nLen
= strLen
< nLen
? strLen
: nLen
;
329 InitWith(str
.c_str(), nPos
, nLen
);
331 // take all characters from pStart to pEnd
332 wxStringBase(const void *pStart
, const void *pEnd
);
334 // dtor is not virtual, this class must not be inherited from!
335 ~wxStringBase() { GetStringData()->Unlock(); }
337 // overloaded assignment
338 // from another wxString
339 wxStringBase
& operator=(const wxStringBase
& stringSrc
);
341 wxStringBase
& operator=(wxChar ch
);
343 wxStringBase
& operator=(const wxChar
*psz
);
345 // return the length of the string
346 size_type
size() const { return GetStringData()->nDataLength
; }
347 // return the length of the string
348 size_type
length() const { return size(); }
349 // return the maximum size of the string
350 size_type
max_size() const { return wxSTRING_MAXLEN
; }
351 // resize the string, filling the space with c if c != 0
352 void resize(size_t nSize
, wxChar ch
= wxT('\0'));
353 // delete the contents of the string
354 void clear() { erase(0, npos
); }
355 // returns true if the string is empty
356 bool empty() const { return size() == 0; }
357 // inform string about planned change in size
358 void reserve(size_t sz
) { Alloc(sz
); }
359 size_type
capacity() const { return GetStringData()->nAllocLength
; }
362 // return the character at position n
363 value_type
at(size_type n
) const
364 { wxASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
365 value_type
operator[](size_type n
) const { return at(n
); }
366 // returns the writable character at position n
367 reference
at(size_type n
)
368 { wxASSERT_VALID_INDEX( n
); CopyBeforeWrite(); return m_pchData
[n
]; }
369 reference
operator[](size_type n
)
370 { wxASSERT_VALID_INDEX( n
); CopyBeforeWrite(); return m_pchData
[n
]; }
372 // lib.string.modifiers
373 // append elements str[pos], ..., str[pos+n]
374 wxStringBase
& append(const wxStringBase
& str
, size_t pos
, size_t n
)
376 wxASSERT(pos
<= str
.length());
377 ConcatSelf(n
, str
.c_str() + pos
, str
.length() - pos
);
381 wxStringBase
& append(const wxStringBase
& str
)
382 { ConcatSelf(str
.length(), str
.c_str()); return *this; }
383 // append first n (or all if n == npos) characters of sz
384 wxStringBase
& append(const wxChar
*sz
)
385 { ConcatSelf(wxStrlen(sz
), sz
); return *this; }
386 wxStringBase
& append(const wxChar
*sz
, size_t n
)
387 { ConcatSelf(n
, sz
); return *this; }
388 // append n copies of ch
389 wxStringBase
& append(size_t n
, wxChar ch
);
390 // append from first to last
391 wxStringBase
& append(const_iterator first
, const_iterator last
)
392 { ConcatSelf(last
- first
, first
); return *this; }
394 // same as `this_string = str'
395 wxStringBase
& assign(const wxStringBase
& str
)
396 { return *this = str
; }
397 // same as ` = str[pos..pos + n]
398 wxStringBase
& assign(const wxStringBase
& str
, size_t pos
, size_t n
)
399 { clear(); return append(str
, pos
, n
); }
400 // same as `= first n (or all if n == npos) characters of sz'
401 wxStringBase
& assign(const wxChar
*sz
)
402 { clear(); return append(sz
, wxStrlen(sz
)); }
403 wxStringBase
& assign(const wxChar
*sz
, size_t n
)
404 { clear(); return append(sz
, n
); }
405 // same as `= n copies of ch'
406 wxStringBase
& assign(size_t n
, wxChar ch
)
407 { clear(); return append(n
, ch
); }
408 // assign from first to last
409 wxStringBase
& assign(const_iterator first
, const_iterator last
)
410 { clear(); return append(first
, last
); }
412 // first valid index position
413 const_iterator
begin() const { return m_pchData
; }
414 // position one after the last valid one
415 const_iterator
end() const { return m_pchData
+ length(); }
417 // first valid index position
418 iterator
begin() { CopyBeforeWrite(); return m_pchData
; }
419 // position one after the last valid one
420 iterator
end() { CopyBeforeWrite(); return m_pchData
+ length(); }
422 // insert another string
423 wxStringBase
& insert(size_t nPos
, const wxStringBase
& str
)
425 wxASSERT( str
.GetStringData()->IsValid() );
426 return insert(nPos
, str
.c_str(), str
.length());
428 // insert n chars of str starting at nStart (in str)
429 wxStringBase
& insert(size_t nPos
, const wxStringBase
& str
, size_t nStart
, size_t n
)
431 wxASSERT( str
.GetStringData()->IsValid() );
432 wxASSERT( nStart
< str
.length() );
433 size_t strLen
= str
.length() - nStart
;
434 n
= strLen
< n
? strLen
: n
;
435 return insert(nPos
, str
.c_str() + nStart
, n
);
437 // insert first n (or all if n == npos) characters of sz
438 wxStringBase
& insert(size_t nPos
, const wxChar
*sz
, size_t n
= npos
);
439 // insert n copies of ch
440 wxStringBase
& insert(size_t nPos
, size_t n
, wxChar ch
)
441 { return insert(nPos
, wxStringBase(n
, ch
)); }
442 iterator
insert(iterator it
, wxChar ch
)
443 { size_t idx
= it
- begin(); insert(idx
, 1, ch
); return begin() + idx
; }
444 void insert(iterator it
, const_iterator first
, const_iterator last
)
445 { insert(it
- begin(), first
, last
- first
); }
446 void insert(iterator it
, size_type n
, wxChar ch
)
447 { insert(it
- begin(), n
, ch
); }
449 // delete characters from nStart to nStart + nLen
450 wxStringBase
& erase(size_type pos
= 0, size_type n
= npos
);
451 iterator
erase(iterator first
, iterator last
)
453 size_t idx
= first
- begin();
454 erase(idx
, last
- first
);
455 return begin() + idx
;
457 iterator
erase(iterator first
);
459 // explicit conversion to C string (use this with printf()!)
460 const wxChar
* c_str() const { return m_pchData
; }
461 const wxChar
* data() const { return m_pchData
; }
463 // replaces the substring of length nLen starting at nStart
464 wxStringBase
& replace(size_t nStart
, size_t nLen
, const wxChar
* sz
);
465 // replaces the substring of length nLen starting at nStart
466 wxStringBase
& replace(size_t nStart
, size_t nLen
, const wxStringBase
& str
)
467 { return replace(nStart
, nLen
, str
.c_str()); }
468 // replaces the substring with nCount copies of ch
469 wxStringBase
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxChar ch
);
470 // replaces a substring with another substring
471 wxStringBase
& replace(size_t nStart
, size_t nLen
,
472 const wxStringBase
& str
, size_t nStart2
, size_t nLen2
);
473 // replaces the substring with first nCount chars of sz
474 wxStringBase
& replace(size_t nStart
, size_t nLen
,
475 const wxChar
* sz
, size_t nCount
);
476 wxStringBase
& replace(iterator first
, iterator last
, const_pointer s
)
477 { return replace(first
- begin(), last
- first
, s
); }
478 wxStringBase
& replace(iterator first
, iterator last
, const_pointer s
,
480 { return replace(first
- begin(), last
- first
, s
, n
); }
481 wxStringBase
& replace(iterator first
, iterator last
, const wxStringBase
& s
)
482 { return replace(first
- begin(), last
- first
, s
); }
483 wxStringBase
& replace(iterator first
, iterator last
, size_type n
, wxChar c
)
484 { return replace(first
- begin(), last
- first
, n
, c
); }
485 wxStringBase
& replace(iterator first
, iterator last
,
486 const_iterator first1
, const_iterator last1
)
487 { return replace(first
- begin(), last
- first
, first1
, last1
- first1
); }
490 void swap(wxStringBase
& str
);
492 // All find() functions take the nStart argument which specifies the
493 // position to start the search on, the default value is 0. All functions
494 // return npos if there were no match.
497 size_t find(const wxStringBase
& str
, size_t nStart
= 0) const;
499 // VC++ 1.5 can't cope with this syntax.
500 #if !defined(__VISUALC__) || defined(__WIN32__)
501 // find first n characters of sz
502 size_t find(const wxChar
* sz
, size_t nStart
= 0, size_t n
= npos
) const;
505 // find the first occurence of character ch after nStart
506 size_t find(wxChar ch
, size_t nStart
= 0) const;
508 // rfind() family is exactly like find() but works right to left
510 // as find, but from the end
511 size_t rfind(const wxStringBase
& str
, size_t nStart
= npos
) const;
513 // VC++ 1.5 can't cope with this syntax.
514 // as find, but from the end
515 size_t rfind(const wxChar
* sz
, size_t nStart
= npos
,
516 size_t n
= npos
) const;
517 // as find, but from the end
518 size_t rfind(wxChar ch
, size_t nStart
= npos
) const;
520 // find first/last occurence of any character in the set
522 // as strpbrk() but starts at nStart, returns npos if not found
523 size_t find_first_of(const wxStringBase
& str
, size_t nStart
= 0) const
524 { return find_first_of(str
.c_str(), nStart
); }
526 size_t find_first_of(const wxChar
* sz
, size_t nStart
= 0) const;
527 size_t find_first_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
528 // same as find(char, size_t)
529 size_t find_first_of(wxChar c
, size_t nStart
= 0) const
530 { return find(c
, nStart
); }
531 // find the last (starting from nStart) char from str in this string
532 size_t find_last_of (const wxStringBase
& str
, size_t nStart
= npos
) const
533 { return find_last_of(str
.c_str(), nStart
); }
535 size_t find_last_of (const wxChar
* sz
, size_t nStart
= npos
) const;
536 size_t find_last_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
538 size_t find_last_of(wxChar c
, size_t nStart
= npos
) const
539 { return rfind(c
, nStart
); }
541 // find first/last occurence of any character not in the set
543 // as strspn() (starting from nStart), returns npos on failure
544 size_t find_first_not_of(const wxStringBase
& str
, size_t nStart
= 0) const
545 { return find_first_not_of(str
.c_str(), nStart
); }
547 size_t find_first_not_of(const wxChar
* sz
, size_t nStart
= 0) const;
548 size_t find_first_not_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
550 size_t find_first_not_of(wxChar ch
, size_t nStart
= 0) const;
552 size_t find_last_not_of(const wxStringBase
& str
, size_t nStart
= npos
) const
553 { return find_last_not_of(str
.c_str(), nStart
); }
555 size_t find_last_not_of(const wxChar
* sz
, size_t nStart
= npos
) const;
556 size_t find_last_not_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
558 size_t find_last_not_of(wxChar ch
, size_t nStart
= npos
) const;
560 // All compare functions return -1, 0 or 1 if the [sub]string is less,
561 // equal or greater than the compare() argument.
563 // just like strcmp()
564 int compare(const wxStringBase
& str
) const
565 { return wxStrcmp(c_str(), str
.c_str()); }
566 // comparison with a substring
567 int compare(size_t nStart
, size_t nLen
, const wxStringBase
& str
) const;
568 // comparison of 2 substrings
569 int compare(size_t nStart
, size_t nLen
,
570 const wxStringBase
& str
, size_t nStart2
, size_t nLen2
) const;
571 // just like strcmp()
572 int compare(const wxChar
* sz
) const
573 { return wxStrcmp(c_str(), sz
); }
574 // substring comparison with first nCount characters of sz
575 int compare(size_t nStart
, size_t nLen
,
576 const wxChar
* sz
, size_t nCount
= npos
) const;
578 size_type
copy(wxChar
* s
, size_type n
, size_type pos
= 0);
580 // substring extraction
581 wxStringBase
substr(size_t nStart
= 0, size_t nLen
= npos
) const;
584 wxStringBase
& operator+=(const wxStringBase
& s
) { return append(s
); }
585 // string += C string
586 wxStringBase
& operator+=(const wxChar
*psz
) { return append(psz
); }
588 wxStringBase
& operator+=(wxChar ch
) { return append(1, ch
); }
593 // ---------------------------------------------------------------------------
594 // This is (yet another one) String class for C++ programmers. It doesn't use
595 // any of "advanced" C++ features (i.e. templates, exceptions, namespaces...)
596 // thus you should be able to compile it with practicaly any C++ compiler.
597 // This class uses copy-on-write technique, i.e. identical strings share the
598 // same memory as long as neither of them is changed.
600 // This class aims to be as compatible as possible with the new standard
601 // std::string class, but adds some additional functions and should be at
602 // least as efficient than the standard implementation.
604 // Performance note: it's more efficient to write functions which take "const
605 // String&" arguments than "const char *" if you assign the argument to
608 // It was compiled and tested under Win32, Linux (libc 5 & 6), Solaris 5.5.
611 // - ressource support (string tables in ressources)
612 // - more wide character (UNICODE) support
613 // - regular expressions support
614 // ---------------------------------------------------------------------------
616 class WXDLLIMPEXP_BASE wxString
: public wxStringBase
619 friend class WXDLLIMPEXP_BASE wxArrayString
;
622 // NB: special care was taken in arranging the member functions in such order
623 // that all inline functions can be effectively inlined, verify that all
624 // performace critical functions are still inlined if you change order!
626 // if we hadn't made these operators private, it would be possible to
627 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
628 // converted to char in C and we do have operator=(char)
630 // NB: we don't need other versions (short/long and unsigned) as attempt
631 // to assign another numeric type to wxString will now result in
632 // ambiguity between operator=(char) and operator=(int)
633 wxString
& operator=(int);
635 // these methods are not implemented - there is _no_ conversion from int to
636 // string, you're doing something wrong if the compiler wants to call it!
638 // try `s << i' or `s.Printf("%d", i)' instead
642 // constructors and destructor
643 // ctor for an empty string
644 wxString() : wxStringBase() { }
646 wxString(const wxStringBase
& stringSrc
) : wxStringBase(stringSrc
) { }
647 wxString(const wxString
& stringSrc
) : wxStringBase(stringSrc
) { }
648 // string containing nRepeat copies of ch
649 wxString(wxChar ch
, size_t nRepeat
= 1)
650 : wxStringBase(nRepeat
, ch
) { }
651 wxString(size_t nRepeat
, wxChar ch
)
652 : wxStringBase(nRepeat
, ch
) { }
653 // ctor takes first nLength characters from C string
654 // (default value of npos means take all the string)
655 wxString(const wxChar
*psz
)
656 : wxStringBase(psz
? psz
: wxT("")) { }
657 wxString(const wxChar
*psz
, size_t nLength
)
658 : wxStringBase(psz
, nLength
) { }
659 wxString(const wxChar
*psz
, wxMBConv
& WXUNUSED(conv
), size_t nLength
= npos
)
660 : wxStringBase(psz
, nLength
== npos
? wxStrlen(psz
) : nLength
) { }
663 // from multibyte string
664 // (NB: nLength is right now number of Unicode characters, not
665 // characters in psz! So try not to use it yet!)
666 wxString(const char *psz
, wxMBConv
& conv
, size_t nLength
= npos
);
667 // from wxWCharBuffer (i.e. return from wxGetString)
668 wxString(const wxWCharBuffer
& psz
) : wxStringBase(psz
.data()) { }
670 // from C string (for compilers using unsigned char)
671 wxString(const unsigned char* psz
, size_t nLength
= npos
)
672 : wxStringBase((const char*)psz
, nLength
) { }
675 // from wide (Unicode) string
676 wxString(const wchar_t *pwz
, wxMBConv
& conv
= wxConvLibc
, size_t nLength
= npos
);
677 #endif // !wxUSE_WCHAR_T
680 wxString(const wxCharBuffer
& psz
)
681 : wxStringBase(psz
, npos
) { }
682 #endif // Unicode/ANSI
684 // generic attributes & operations
685 // as standard strlen()
686 size_t Len() const { return length(); }
687 // string contains any characters?
688 bool IsEmpty() const { return empty(); }
689 // empty string is "FALSE", so !str will return TRUE
690 bool operator!() const { return IsEmpty(); }
691 // truncate the string to given length
692 wxString
& Truncate(size_t uiLen
);
693 // empty string contents
698 wxASSERT_MSG( IsEmpty(), _T("string not empty after call to Empty()?") );
700 // empty the string and free memory
703 wxString
tmp(wxEmptyString
);
709 bool IsAscii() const;
711 bool IsNumber() const;
715 // data access (all indexes are 0 based)
717 wxChar
GetChar(size_t n
) const
718 { return operator[](n
); }
720 wxChar
& GetWritableChar(size_t n
)
721 { return operator[](n
); }
723 void SetChar(size_t n
, wxChar ch
)
724 { operator[](n
) = ch
; }
726 // get last character
729 wxASSERT_MSG( !IsEmpty(), _T("wxString: index out of bounds") );
731 return operator[](length() - 1);
734 // get writable last character
737 wxASSERT_MSG( !IsEmpty(), _T("wxString: index out of bounds") );
738 return operator[](length() - 1);
742 So why do we have all these overloaded operator[]s? A bit of history:
743 initially there was only one of them, taking size_t. Then people
744 started complaining because they wanted to use ints as indices (I
745 wonder why) and compilers were giving warnings about it, so we had to
746 add the operator[](int). Then it became apparent that you couldn't
747 write str[0] any longer because there was ambiguity between two
748 overloads and so you now had to write str[0u] (or, of course, use the
749 explicit casts to either int or size_t but nobody did this).
751 Finally, someone decided to compile wxWin on an Alpha machine and got
752 a surprize: str[0u] didn't compile there because it is of type
753 unsigned int and size_t is unsigned _long_ on Alpha and so there was
754 ambiguity between converting uint to int or ulong. To fix this one we
755 now add operator[](uint) for the machines where size_t is not already
756 the same as unsigned int - hopefully this fixes the problem (for some
759 The only real fix is, of course, to remove all versions but the one
763 // operator version of GetChar
764 wxChar
operator[](int n
) const
765 { return wxStringBase::operator[](n
); }
766 wxChar
& operator[](size_type n
)
767 { return wxStringBase::operator[](n
); }
768 wxChar
operator[](size_type n
) const
769 { return wxStringBase::operator[](n
); }
770 #ifndef wxSIZE_T_IS_UINT
771 // operator version of GetChar
772 wxChar
operator[](unsigned int n
) const
773 { return wxStringBase::operator[](n
); }
775 // operator version of GetWriteableChar
776 wxChar
& operator[](unsigned int n
)
777 { return wxStringBase::operator[](n
); }
778 #endif // size_t != unsigned int
780 // implicit conversion to C string
781 operator const wxChar
*() const { return c_str(); }
783 // identical to c_str(), for wxWin 1.6x compatibility
784 const wxChar
* wx_str() const { return c_str(); }
785 // identical to c_str(), for MFC compatibility
786 const wxChar
* GetData() const { return c_str(); }
788 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
789 // converting numbers or strings which are certain not to contain special
790 // chars (typically system functions, X atoms, environment variables etc.)
792 // the behaviour of these functions with the strings containing anything
793 // else than 7 bit ASCII characters is undefined, use at your own risk.
795 static wxString
FromAscii(const char *ascii
); // string
796 static wxString
FromAscii(const char ascii
); // char
797 const wxCharBuffer
ToAscii() const;
799 static wxString
FromAscii(const char *ascii
) { return wxString( ascii
); }
800 static wxString
FromAscii(const char ascii
) { return wxString( ascii
); }
801 const char *ToAscii() const { return c_str(); }
802 #endif // Unicode/!Unicode
804 // conversions with (possible) format conversions: have to return a
805 // buffer with temporary data
807 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
808 // return an ANSI (multibyte) string, wc_str() to return a wide string and
809 // fn_str() to return a string which should be used with the OS APIs
810 // accepting the file names. The return value is always the same, but the
811 // type differs because a function may either return pointer to the buffer
812 // directly or have to use intermediate buffer for translation.
814 const wxCharBuffer
mb_str(wxMBConv
& conv
= wxConvLibc
) const
815 { return conv
.cWC2MB(c_str()); }
817 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
819 const wxChar
* wc_str() const { return c_str(); }
821 // for compatibility with !wxUSE_UNICODE version
822 const wxChar
* wc_str(wxMBConv
& WXUNUSED(conv
)) const { return c_str(); }
825 const wxCharBuffer
fn_str() const { return mb_str(wxConvFile
); }
827 const wxChar
* fn_str() const { return c_str(); }
828 #endif // wxMBFILES/!wxMBFILES
830 const wxChar
* mb_str() const { return c_str(); }
832 // for compatibility with wxUSE_UNICODE version
833 const wxChar
* mb_str(wxMBConv
& WXUNUSED(conv
)) const { return c_str(); }
835 const wxWX2MBbuf
mbc_str() const { return mb_str(); }
838 const wxWCharBuffer
wc_str(wxMBConv
& conv
) const
839 { return conv
.cMB2WC(c_str()); }
840 #endif // wxUSE_WCHAR_T
842 const wxChar
* fn_str() const { return c_str(); }
843 #endif // Unicode/ANSI
845 // overloaded assignment
846 // from another wxString
847 wxString
& operator=(const wxStringBase
& stringSrc
)
848 { return (wxString
&)wxStringBase::operator=(stringSrc
); }
850 wxString
& operator=(wxChar ch
)
851 { return (wxString
&)wxStringBase::operator=(ch
); }
853 wxString
& operator=(const wxChar
*psz
)
854 { return (wxString
&)wxStringBase::operator=(psz
); }
856 // from wxWCharBuffer
857 wxString
& operator=(const wxWCharBuffer
& psz
)
858 { (void) operator=((const wchar_t *)psz
); return *this; }
860 // from another kind of C string
861 wxString
& operator=(const unsigned char* psz
);
863 // from a wide string
864 wxString
& operator=(const wchar_t *pwz
);
867 wxString
& operator=(const wxCharBuffer
& psz
)
868 { (void) operator=((const char *)psz
); return *this; }
869 #endif // Unicode/ANSI
871 // string concatenation
872 // in place concatenation
874 Concatenate and return the result. Note that the left to right
875 associativity of << allows to write things like "str << str1 << str2
876 << ..." (unlike with +=)
879 wxString
& operator<<(const wxString
& s
)
882 wxASSERT_MSG( s
.GetStringData()->IsValid(),
883 _T("did you forget to call UngetWriteBuf()?") );
889 // string += C string
890 wxString
& operator<<(const wxChar
*psz
)
891 { append(psz
); return *this; }
893 wxString
& operator<<(wxChar ch
) { append(1, ch
); return *this; }
895 // string += buffer (i.e. from wxGetString)
897 wxString
& operator<<(const wxWCharBuffer
& s
)
898 { (void)operator<<((const wchar_t *)s
); return *this; }
899 void operator+=(const wxWCharBuffer
& s
)
900 { (void)operator<<((const wchar_t *)s
); }
901 #else // !wxUSE_UNICODE
902 wxString
& operator<<(const wxCharBuffer
& s
)
903 { (void)operator<<((const char *)s
); return *this; }
904 void operator+=(const wxCharBuffer
& s
)
905 { (void)operator<<((const char *)s
); }
906 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
908 // string += C string
909 wxString
& Append(const wxString
& s
)
911 // test for IsEmpty() to share the string if possible
918 wxString
& Append(const wxChar
* psz
)
919 { append(psz
); return *this; }
920 // append count copies of given character
921 wxString
& Append(wxChar ch
, size_t count
= 1u)
922 { append(count
, ch
); return *this; }
923 wxString
& Append(const wxChar
* psz
, size_t nLen
)
924 { append(psz
, nLen
); return *this; }
926 // prepend a string, return the string itself
927 wxString
& Prepend(const wxString
& str
)
928 { *this = str
+ *this; return *this; }
930 // non-destructive concatenation
932 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
, const wxString
& string2
);
934 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxChar ch
);
936 friend wxString WXDLLIMPEXP_BASE
operator+(wxChar ch
, const wxString
& string
);
938 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const wxChar
*psz
);
940 friend wxString WXDLLIMPEXP_BASE
operator+(const wxChar
*psz
, const wxString
& string
);
942 // stream-like functions
943 // insert an int into string
944 wxString
& operator<<(int i
)
945 { return (*this) << Format(_T("%d"), i
); }
946 // insert an unsigned int into string
947 wxString
& operator<<(unsigned int ui
)
948 { return (*this) << Format(_T("%u"), ui
); }
949 // insert a long into string
950 wxString
& operator<<(long l
)
951 { return (*this) << Format(_T("%ld"), l
); }
952 // insert an unsigned long into string
953 wxString
& operator<<(unsigned long ul
)
954 { return (*this) << Format(_T("%lu"), ul
); }
955 // insert a float into string
956 wxString
& operator<<(float f
)
957 { return (*this) << Format(_T("%f"), f
); }
958 // insert a double into string
959 wxString
& operator<<(double d
)
960 { return (*this) << Format(_T("%g"), d
); }
963 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
964 int Cmp(const wxChar
*psz
) const { return wxStrcmp(c_str(), psz
); }
965 // same as Cmp() but not case-sensitive
966 int CmpNoCase(const wxChar
*psz
) const { return wxStricmp(c_str(), psz
); }
967 // test for the string equality, either considering case or not
968 // (if compareWithCase then the case matters)
969 bool IsSameAs(const wxChar
*psz
, bool compareWithCase
= TRUE
) const
970 { return (compareWithCase
? Cmp(psz
) : CmpNoCase(psz
)) == 0; }
971 // comparison with a signle character: returns TRUE if equal
972 bool IsSameAs(wxChar c
, bool compareWithCase
= TRUE
) const
974 return (length() == 1) && (compareWithCase
? GetChar(0u) == c
975 : wxToupper(GetChar(0u)) == wxToupper(c
));
978 // simple sub-string extraction
979 // return substring starting at nFirst of length nCount (or till the end
980 // if nCount = default value)
981 wxString
Mid(size_t nFirst
, size_t nCount
= npos
) const;
983 // operator version of Mid()
984 wxString
operator()(size_t start
, size_t len
) const
985 { return Mid(start
, len
); }
987 // check that the string starts with prefix and return the rest of the
988 // string in the provided pointer if it is not NULL, otherwise return
990 bool StartsWith(const wxChar
*prefix
, wxString
*rest
= NULL
) const;
992 // get first nCount characters
993 wxString
Left(size_t nCount
) const;
994 // get last nCount characters
995 wxString
Right(size_t nCount
) const;
996 // get all characters before the first occurance of ch
997 // (returns the whole string if ch not found)
998 wxString
BeforeFirst(wxChar ch
) const;
999 // get all characters before the last occurence of ch
1000 // (returns empty string if ch not found)
1001 wxString
BeforeLast(wxChar ch
) const;
1002 // get all characters after the first occurence of ch
1003 // (returns empty string if ch not found)
1004 wxString
AfterFirst(wxChar ch
) const;
1005 // get all characters after the last occurence of ch
1006 // (returns the whole string if ch not found)
1007 wxString
AfterLast(wxChar ch
) const;
1009 // for compatibility only, use more explicitly named functions above
1010 wxString
Before(wxChar ch
) const { return BeforeLast(ch
); }
1011 wxString
After(wxChar ch
) const { return AfterFirst(ch
); }
1014 // convert to upper case in place, return the string itself
1015 wxString
& MakeUpper();
1016 // convert to upper case, return the copy of the string
1017 // Here's something to remember: BC++ doesn't like returns in inlines.
1018 wxString
Upper() const ;
1019 // convert to lower case in place, return the string itself
1020 wxString
& MakeLower();
1021 // convert to lower case, return the copy of the string
1022 wxString
Lower() const ;
1024 // trimming/padding whitespace (either side) and truncating
1025 // remove spaces from left or from right (default) side
1026 wxString
& Trim(bool bFromRight
= TRUE
);
1027 // add nCount copies chPad in the beginning or at the end (default)
1028 wxString
& Pad(size_t nCount
, wxChar chPad
= wxT(' '), bool bFromRight
= TRUE
);
1030 // searching and replacing
1031 // searching (return starting index, or -1 if not found)
1032 int Find(wxChar ch
, bool bFromEnd
= FALSE
) const; // like strchr/strrchr
1033 // searching (return starting index, or -1 if not found)
1034 int Find(const wxChar
*pszSub
) const; // like strstr
1035 // replace first (or all of bReplaceAll) occurences of substring with
1036 // another string, returns the number of replacements made
1037 size_t Replace(const wxChar
*szOld
,
1038 const wxChar
*szNew
,
1039 bool bReplaceAll
= TRUE
);
1041 // check if the string contents matches a mask containing '*' and '?'
1042 bool Matches(const wxChar
*szMask
) const;
1044 // conversion to numbers: all functions return TRUE only if the whole
1045 // string is a number and put the value of this number into the pointer
1046 // provided, the base is the numeric base in which the conversion should be
1047 // done and must be comprised between 2 and 36 or be 0 in which case the
1048 // standard C rules apply (leading '0' => octal, "0x" => hex)
1049 // convert to a signed integer
1050 bool ToLong(long *val
, int base
= 10) const;
1051 // convert to an unsigned integer
1052 bool ToULong(unsigned long *val
, int base
= 10) const;
1053 // convert to a double
1054 bool ToDouble(double *val
) const;
1056 // formated input/output
1057 // as sprintf(), returns the number of characters written or < 0 on error
1058 // (take 'this' into account in attribute parameter count)
1059 int Printf(const wxChar
*pszFormat
, ...) ATTRIBUTE_PRINTF_2
;
1060 // as vprintf(), returns the number of characters written or < 0 on error
1061 int PrintfV(const wxChar
* pszFormat
, va_list argptr
);
1063 // returns the string containing the result of Printf() to it
1064 static wxString
Format(const wxChar
*pszFormat
, ...) ATTRIBUTE_PRINTF_1
;
1065 // the same as above, but takes a va_list
1066 static wxString
FormatV(const wxChar
*pszFormat
, va_list argptr
);
1068 // raw access to string memory
1069 // ensure that string has space for at least nLen characters
1070 // only works if the data of this string is not shared
1071 bool Alloc(size_t nLen
) { reserve(nLen
); /*return capacity() >= nLen;*/ return true; }
1072 // minimize the string's memory
1073 // only works if the data of this string is not shared
1076 // get writable buffer of at least nLen bytes. Unget() *must* be called
1077 // a.s.a.p. to put string back in a reasonable state!
1078 wxChar
*GetWriteBuf(size_t nLen
);
1079 // call this immediately after GetWriteBuf() has been used
1080 void UngetWriteBuf();
1081 void UngetWriteBuf(size_t nLen
);
1084 // wxWindows version 1 compatibility functions
1087 wxString
SubString(size_t from
, size_t to
) const
1088 { return Mid(from
, (to
- from
+ 1)); }
1089 // values for second parameter of CompareTo function
1090 enum caseCompare
{exact
, ignoreCase
};
1091 // values for first parameter of Strip function
1092 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
1095 // (take 'this' into account in attribute parameter count)
1096 int sprintf(const wxChar
*pszFormat
, ...) ATTRIBUTE_PRINTF_2
;
1099 inline int CompareTo(const wxChar
* psz
, caseCompare cmp
= exact
) const
1100 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
1103 size_t Length() const { return length(); }
1104 // Count the number of characters
1105 int Freq(wxChar ch
) const;
1107 void LowerCase() { MakeLower(); }
1109 void UpperCase() { MakeUpper(); }
1110 // use Trim except that it doesn't change this string
1111 wxString
Strip(stripType w
= trailing
) const;
1113 // use Find (more general variants not yet supported)
1114 size_t Index(const wxChar
* psz
) const { return Find(psz
); }
1115 size_t Index(wxChar ch
) const { return Find(ch
); }
1117 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
1118 wxString
& RemoveLast(size_t n
= 1) { return Truncate(length() - n
); }
1120 wxString
& Remove(size_t nStart
, size_t nLen
)
1121 { return (wxString
&)erase( nStart
, nLen
); }
1124 int First( const wxChar ch
) const { return Find(ch
); }
1125 int First( const wxChar
* psz
) const { return Find(psz
); }
1126 int First( const wxString
&str
) const { return Find(str
); }
1127 int Last( const wxChar ch
) const { return Find(ch
, TRUE
); }
1128 bool Contains(const wxString
& str
) const { return Find(str
) != -1; }
1131 bool IsNull() const { return IsEmpty(); }
1133 // std::string compatibility functions
1135 // take nLen chars starting at nPos
1136 wxString(const wxString
& str
, size_t nPos
, size_t nLen
)
1137 : wxStringBase(str
, nPos
, nLen
) { }
1138 // take all characters from pStart to pEnd
1139 wxString(const void *pStart
, const void *pEnd
)
1140 : wxStringBase((const char*)pStart
, (const char*)pEnd
) { }
1142 wxString(const_iterator first
, const_iterator last
)
1143 : wxStringBase(first
, last
) { }
1146 // lib.string.modifiers
1147 // append elements str[pos], ..., str[pos+n]
1148 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
1149 { return (wxString
&)wxStringBase::append(str
, pos
, n
); }
1151 wxString
& append(const wxString
& str
)
1152 { return (wxString
&)wxStringBase::append(str
); }
1153 // append first n (or all if n == npos) characters of sz
1154 wxString
& append(const wxChar
*sz
)
1155 { return (wxString
&)wxStringBase::append(sz
); }
1156 wxString
& append(const wxChar
*sz
, size_t n
)
1157 { return (wxString
&)wxStringBase::append(sz
, n
); }
1158 // append n copies of ch
1159 wxString
& append(size_t n
, wxChar ch
)
1160 { return (wxString
&)wxStringBase::append(n
, ch
); }
1161 // append from first to last
1162 wxString
& append(const_iterator first
, const_iterator last
)
1163 { return (wxString
&)wxStringBase::append(first
, last
); }
1165 // same as `this_string = str'
1166 wxString
& assign(const wxString
& str
)
1167 { return (wxString
&)wxStringBase::assign(str
); }
1168 // same as ` = str[pos..pos + n]
1169 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
1170 { return (wxString
&)wxStringBase::assign(str
, pos
, n
); }
1171 // same as `= first n (or all if n == npos) characters of sz'
1172 wxString
& assign(const wxChar
*sz
)
1173 { return (wxString
&)wxStringBase::assign(sz
); }
1174 wxString
& assign(const wxChar
*sz
, size_t n
)
1175 { return (wxString
&)wxStringBase::assign(sz
, n
); }
1176 // same as `= n copies of ch'
1177 wxString
& assign(size_t n
, wxChar ch
)
1178 { return (wxString
&)wxStringBase::assign(n
, ch
); }
1179 // assign from first to last
1180 wxString
& assign(const_iterator first
, const_iterator last
)
1181 { return (wxString
&)wxStringBase::assign(first
, last
); }
1183 // string comparison
1184 #ifndef HAVE_STD_STRING_COMPARE
1185 int compare(const wxStringBase
& str
) const;
1186 // comparison with a substring
1187 int compare(size_t nStart
, size_t nLen
, const wxStringBase
& str
) const;
1188 // comparison of 2 substrings
1189 int compare(size_t nStart
, size_t nLen
,
1190 const wxStringBase
& str
, size_t nStart2
, size_t nLen2
) const;
1191 // just like strcmp()
1192 int compare(const wxChar
* sz
) const;
1193 // substring comparison with first nCount characters of sz
1194 int compare(size_t nStart
, size_t nLen
,
1195 const wxChar
* sz
, size_t nCount
= npos
) const;
1196 #endif // !defined HAVE_STD_STRING_COMPARE
1198 // insert another string
1199 wxString
& insert(size_t nPos
, const wxString
& str
)
1200 { return (wxString
&)wxStringBase::insert(nPos
, str
); }
1201 // insert n chars of str starting at nStart (in str)
1202 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
1203 { return (wxString
&)wxStringBase::insert(nPos
, str
, nStart
, n
); }
1204 // insert first n (or all if n == npos) characters of sz
1205 wxString
& insert(size_t nPos
, const wxChar
*sz
)
1206 { return (wxString
&)wxStringBase::insert(nPos
, sz
); }
1207 wxString
& insert(size_t nPos
, const wxChar
*sz
, size_t n
)
1208 { return (wxString
&)wxStringBase::insert(nPos
, sz
, n
); }
1209 // insert n copies of ch
1210 wxString
& insert(size_t nPos
, size_t n
, wxChar ch
)
1211 { return (wxString
&)wxStringBase::insert(nPos
, n
, ch
); }
1212 iterator
insert(iterator it
, wxChar ch
)
1213 { return wxStringBase::insert(it
, ch
); }
1214 void insert(iterator it
, const_iterator first
, const_iterator last
)
1215 { wxStringBase::insert(it
, first
, last
); }
1216 void insert(iterator it
, size_type n
, wxChar ch
)
1217 { wxStringBase::insert(it
, n
, ch
); }
1219 // delete characters from nStart to nStart + nLen
1220 wxString
& erase(size_type pos
= 0, size_type n
= npos
)
1221 { return (wxString
&)wxStringBase::erase(pos
, n
); }
1222 iterator
erase(iterator first
, iterator last
)
1223 { return wxStringBase::erase(first
, last
); }
1224 iterator
erase(iterator first
)
1225 { return wxStringBase::erase(first
); }
1227 #ifdef wxSTRING_BASE_HASNT_CLEAR
1228 void clear() { erase(); }
1231 // replaces the substring of length nLen starting at nStart
1232 wxString
& replace(size_t nStart
, size_t nLen
, const wxChar
* sz
)
1233 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, sz
); }
1234 // replaces the substring of length nLen starting at nStart
1235 wxString
& replace(size_t nStart
, size_t nLen
, const wxString
& str
)
1236 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, str
); }
1237 // replaces the substring with nCount copies of ch
1238 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxChar ch
)
1239 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, nCount
, ch
); }
1240 // replaces a substring with another substring
1241 wxString
& replace(size_t nStart
, size_t nLen
,
1242 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1243 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, str
,
1245 // replaces the substring with first nCount chars of sz
1246 wxString
& replace(size_t nStart
, size_t nLen
,
1247 const wxChar
* sz
, size_t nCount
)
1248 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, sz
, nCount
); }
1249 wxString
& replace(iterator first
, iterator last
, const_pointer s
)
1250 { return (wxString
&)wxStringBase::replace(first
, last
, s
); }
1251 wxString
& replace(iterator first
, iterator last
, const_pointer s
,
1253 { return (wxString
&)wxStringBase::replace(first
, last
, s
, n
); }
1254 wxString
& replace(iterator first
, iterator last
, const wxString
& s
)
1255 { return (wxString
&)wxStringBase::replace(first
, last
, s
); }
1256 wxString
& replace(iterator first
, iterator last
, size_type n
, wxChar c
)
1257 { return (wxString
&)wxStringBase::replace(first
, last
, n
, c
); }
1258 wxString
& replace(iterator first
, iterator last
,
1259 const_iterator first1
, const_iterator last1
)
1260 { return (wxString
&)wxStringBase::replace(first
, last
, first1
, last1
); }
1263 wxString
& operator+=(const wxString
& s
)
1264 { return (wxString
&)wxStringBase::operator+=(s
); }
1265 // string += C string
1266 wxString
& operator+=(const wxChar
*psz
)
1267 { return (wxString
&)wxStringBase::operator+=(psz
); }
1269 wxString
& operator+=(wxChar ch
)
1270 { return (wxString
&)wxStringBase::operator+=(ch
); }
1273 // define wxArrayString, for compatibility
1274 #if WXWIN_COMPATIBILITY_2_4 && !wxUSE_STL
1275 #include "wx/arrstr.h"
1278 // ----------------------------------------------------------------------------
1279 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
1280 // ----------------------------------------------------------------------------
1284 class WXDLLIMPEXP_BASE wxStringBuffer
1287 wxStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
1288 : m_str(str
), m_buf(lenWanted
)
1291 ~wxStringBuffer() { m_str
.assign(m_buf
.data(), wxStrlen(m_buf
.data())); }
1293 operator wxChar
*() { return m_buf
.data(); }
1298 wxWCharBuffer m_buf
;
1303 DECLARE_NO_COPY_CLASS(wxStringBuffer
)
1306 class WXDLLIMPEXP_BASE wxStringBufferLength
1309 wxStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
1310 : m_str(str
), m_buf(lenWanted
), m_len(0), m_lenSet(false)
1313 ~wxStringBufferLength()
1316 m_str
.assign(m_buf
.data(), m_len
);
1319 operator wxChar
*() { return m_buf
.data(); }
1320 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
1325 wxWCharBuffer m_buf
;
1332 DECLARE_NO_COPY_CLASS(wxStringBufferLength
)
1335 #else // if !wxUSE_STL
1337 class WXDLLIMPEXP_BASE wxStringBuffer
1340 wxStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
1341 : m_str(str
), m_buf(NULL
)
1342 { m_buf
= m_str
.GetWriteBuf(lenWanted
); }
1344 ~wxStringBuffer() { m_str
.UngetWriteBuf(); }
1346 operator wxChar
*() const { return m_buf
; }
1352 DECLARE_NO_COPY_CLASS(wxStringBuffer
)
1355 class WXDLLIMPEXP_BASE wxStringBufferLength
1358 wxStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
1359 : m_str(str
), m_buf(NULL
), m_len(0), m_lenSet(false)
1360 { m_buf
= m_str
.GetWriteBuf(lenWanted
); }
1362 ~wxStringBufferLength()
1365 m_str
.UngetWriteBuf(m_len
);
1368 operator wxChar
*() const { return m_buf
; }
1369 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
1377 DECLARE_NO_COPY_CLASS(wxStringBufferLength
)
1380 #endif // !wxUSE_STL
1382 // ---------------------------------------------------------------------------
1383 // wxString comparison functions: operator versions are always case sensitive
1384 // ---------------------------------------------------------------------------
1388 inline bool operator==(const wxString
& s1
, const wxString
& s2
)
1389 { return s1
.compare(s2
) == 0; }
1390 inline bool operator==(const wxString
& s1
, const wxChar
* s2
)
1391 { return s1
.compare(s2
) == 0; }
1392 inline bool operator==(const wxChar
* s1
, const wxString
& s2
)
1393 { return s2
.compare(s1
) == 0; }
1394 inline bool operator!=(const wxString
& s1
, const wxString
& s2
)
1395 { return s1
.compare(s2
) != 0; }
1396 inline bool operator!=(const wxString
& s1
, const wxChar
* s2
)
1397 { return s1
.compare(s2
) != 0; }
1398 inline bool operator!=(const wxChar
* s1
, const wxString
& s2
)
1399 { return s2
.compare(s1
) != 0; }
1400 inline bool operator< (const wxString
& s1
, const wxString
& s2
)
1401 { return s1
.compare(s2
) < 0; }
1402 inline bool operator< (const wxString
& s1
, const wxChar
* s2
)
1403 { return s1
.compare(s2
) < 0; }
1404 inline bool operator< (const wxChar
* s1
, const wxString
& s2
)
1405 { return s2
.compare(s1
) > 0; }
1406 inline bool operator> (const wxString
& s1
, const wxString
& s2
)
1407 { return s1
.compare(s2
) > 0; }
1408 inline bool operator> (const wxString
& s1
, const wxChar
* s2
)
1409 { return s1
.compare(s2
) > 0; }
1410 inline bool operator> (const wxChar
* s1
, const wxString
& s2
)
1411 { return s2
.compare(s1
) < 0; }
1412 inline bool operator<=(const wxString
& s1
, const wxString
& s2
)
1413 { return s1
.compare(s2
) <= 0; }
1414 inline bool operator<=(const wxString
& s1
, const wxChar
* s2
)
1415 { return s1
.compare(s2
) <= 0; }
1416 inline bool operator<=(const wxChar
* s1
, const wxString
& s2
)
1417 { return s2
.compare(s1
) >= 0; }
1418 inline bool operator>=(const wxString
& s1
, const wxString
& s2
)
1419 { return s1
.compare(s2
) >= 0; }
1420 inline bool operator>=(const wxString
& s1
, const wxChar
* s2
)
1421 { return s1
.compare(s2
) >= 0; }
1422 inline bool operator>=(const wxChar
* s1
, const wxString
& s2
)
1423 { return s2
.compare(s1
) <= 0; }
1425 #else // if !wxUSE_STL
1427 inline bool operator==(const wxString
& s1
, const wxString
& s2
)
1428 { return (s1
.Len() == s2
.Len()) && (s1
.Cmp(s2
) == 0); }
1429 inline bool operator==(const wxString
& s1
, const wxChar
* s2
)
1430 { return s1
.Cmp(s2
) == 0; }
1431 inline bool operator==(const wxChar
* s1
, const wxString
& s2
)
1432 { return s2
.Cmp(s1
) == 0; }
1433 inline bool operator!=(const wxString
& s1
, const wxString
& s2
)
1434 { return (s1
.Len() != s2
.Len()) || (s1
.Cmp(s2
) != 0); }
1435 inline bool operator!=(const wxString
& s1
, const wxChar
* s2
)
1436 { return s1
.Cmp(s2
) != 0; }
1437 inline bool operator!=(const wxChar
* s1
, const wxString
& s2
)
1438 { return s2
.Cmp(s1
) != 0; }
1439 inline bool operator< (const wxString
& s1
, const wxString
& s2
)
1440 { return s1
.Cmp(s2
) < 0; }
1441 inline bool operator< (const wxString
& s1
, const wxChar
* s2
)
1442 { return s1
.Cmp(s2
) < 0; }
1443 inline bool operator< (const wxChar
* s1
, const wxString
& s2
)
1444 { return s2
.Cmp(s1
) > 0; }
1445 inline bool operator> (const wxString
& s1
, const wxString
& s2
)
1446 { return s1
.Cmp(s2
) > 0; }
1447 inline bool operator> (const wxString
& s1
, const wxChar
* s2
)
1448 { return s1
.Cmp(s2
) > 0; }
1449 inline bool operator> (const wxChar
* s1
, const wxString
& s2
)
1450 { return s2
.Cmp(s1
) < 0; }
1451 inline bool operator<=(const wxString
& s1
, const wxString
& s2
)
1452 { return s1
.Cmp(s2
) <= 0; }
1453 inline bool operator<=(const wxString
& s1
, const wxChar
* s2
)
1454 { return s1
.Cmp(s2
) <= 0; }
1455 inline bool operator<=(const wxChar
* s1
, const wxString
& s2
)
1456 { return s2
.Cmp(s1
) >= 0; }
1457 inline bool operator>=(const wxString
& s1
, const wxString
& s2
)
1458 { return s1
.Cmp(s2
) >= 0; }
1459 inline bool operator>=(const wxString
& s1
, const wxChar
* s2
)
1460 { return s1
.Cmp(s2
) >= 0; }
1461 inline bool operator>=(const wxChar
* s1
, const wxString
& s2
)
1462 { return s2
.Cmp(s1
) <= 0; }
1464 #endif // !wxUSE_STL
1466 // comparison with char
1467 inline bool operator==(wxChar c
, const wxString
& s
) { return s
.IsSameAs(c
); }
1468 inline bool operator==(const wxString
& s
, wxChar c
) { return s
.IsSameAs(c
); }
1469 inline bool operator!=(wxChar c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
1470 inline bool operator!=(const wxString
& s
, wxChar c
) { return !s
.IsSameAs(c
); }
1473 inline bool operator==(const wxString
& s1
, const wxWCharBuffer
& s2
)
1474 { return (s1
.Cmp((const wchar_t *)s2
) == 0); }
1475 inline bool operator==(const wxWCharBuffer
& s1
, const wxString
& s2
)
1476 { return (s2
.Cmp((const wchar_t *)s1
) == 0); }
1477 inline bool operator!=(const wxString
& s1
, const wxWCharBuffer
& s2
)
1478 { return (s1
.Cmp((const wchar_t *)s2
) != 0); }
1479 inline bool operator!=(const wxWCharBuffer
& s1
, const wxString
& s2
)
1480 { return (s2
.Cmp((const wchar_t *)s1
) != 0); }
1481 #else // !wxUSE_UNICODE
1482 inline bool operator==(const wxString
& s1
, const wxCharBuffer
& s2
)
1483 { return (s1
.Cmp((const char *)s2
) == 0); }
1484 inline bool operator==(const wxCharBuffer
& s1
, const wxString
& s2
)
1485 { return (s2
.Cmp((const char *)s1
) == 0); }
1486 inline bool operator!=(const wxString
& s1
, const wxCharBuffer
& s2
)
1487 { return (s1
.Cmp((const char *)s2
) != 0); }
1488 inline bool operator!=(const wxCharBuffer
& s1
, const wxString
& s2
)
1489 { return (s2
.Cmp((const char *)s1
) != 0); }
1490 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1494 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
, const wxString
& string2
);
1495 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxChar ch
);
1496 wxString WXDLLIMPEXP_BASE
operator+(wxChar ch
, const wxString
& string
);
1497 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const wxChar
*psz
);
1498 wxString WXDLLIMPEXP_BASE
operator+(const wxChar
*psz
, const wxString
& string
);
1500 #endif // !wxUSE_STL
1503 inline wxString
operator+(const wxString
& string
, const wxWCharBuffer
& buf
)
1504 { return string
+ (const wchar_t *)buf
; }
1505 inline wxString
operator+(const wxWCharBuffer
& buf
, const wxString
& string
)
1506 { return (const wchar_t *)buf
+ string
; }
1507 #else // !wxUSE_UNICODE
1508 inline wxString
operator+(const wxString
& string
, const wxCharBuffer
& buf
)
1509 { return string
+ (const char *)buf
; }
1510 inline wxString
operator+(const wxCharBuffer
& buf
, const wxString
& string
)
1511 { return (const char *)buf
+ string
; }
1512 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1514 // ---------------------------------------------------------------------------
1515 // Implementation only from here until the end of file
1516 // ---------------------------------------------------------------------------
1518 // don't pollute the library user's name space
1519 #undef wxASSERT_VALID_INDEX
1521 #if wxUSE_STD_IOSTREAM
1523 #include "wx/iosfwrap.h"
1525 WXDLLIMPEXP_BASE wxSTD istream
& operator>>(wxSTD istream
&, wxString
&);
1526 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxString
&);
1528 #endif // wxSTD_STRING_COMPATIBILITY
1530 #endif // _WX_WXSTRINGH__