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 wxWidgets version 1 wxString and std::string and some handy functions
15 missing from string.h.
18 #ifndef _WX_WXSTRINGH__
19 #define _WX_WXSTRINGH__
21 // ----------------------------------------------------------------------------
23 // ----------------------------------------------------------------------------
25 #include "wx/defs.h" // everybody should include this
27 #if defined(__WXMAC__) || defined(__VISAGECPP__)
31 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
32 // problem in VACPP V4 with including stdlib.h multiple times
33 // strconv includes it anyway
46 #ifdef HAVE_STRCASECMP_IN_STRINGS_H
47 #include <strings.h> // for strcasecmp()
48 #endif // HAVE_STRCASECMP_IN_STRINGS_H
51 #include <StringMgr.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 class WXDLLIMPEXP_BASE wxString
;
60 // ---------------------------------------------------------------------------
62 // ---------------------------------------------------------------------------
64 // casts [unfortunately!] needed to call some broken functions which require
65 // "char *" instead of "const char *"
66 #define WXSTRINGCAST (wxChar *)(const wxChar *)
67 #define wxCSTRINGCAST (wxChar *)(const wxChar *)
68 #define wxMBSTRINGCAST (char *)(const char *)
69 #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *)
71 // implementation only
72 #define wxASSERT_VALID_INDEX(i) \
73 wxASSERT_MSG( (size_t)(i) <= length(), _T("invalid index in wxString") )
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 #if WXWIN_COMPATIBILITY_2_6
81 // deprecated in favour of wxString::npos, don't use in new code
83 // maximum possible length for a string means "take all string" everywhere
84 #define wxSTRING_MAXLEN wxStringBase::npos
86 #endif // WXWIN_COMPATIBILITY_2_6
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(__WXPALMOS__) || \
138 defined(HAVE_STRCASECMP_IN_STRING_H) || \
139 defined(HAVE_STRCASECMP_IN_STRINGS_H) || \
140 defined(__GNUWIN32__)
141 return strcasecmp(psz1
, psz2
);
142 #elif defined(__MWERKS__) && !defined(__INTEL__)
143 register char c1
, c2
;
145 c1
= tolower(*psz1
++);
146 c2
= tolower(*psz2
++);
147 } while ( c1
&& (c1
== c2
) );
151 // almost all compilers/libraries provide this function (unfortunately under
152 // different names), that's why we don't implement our own which will surely
153 // be more efficient than this code (uncomment to use):
155 register char c1, c2;
157 c1 = tolower(*psz1++);
158 c2 = tolower(*psz2++);
159 } while ( c1 && (c1 == c2) );
164 #error "Please define string case-insensitive compare for your OS/compiler"
165 #endif // OS/compiler
168 // ----------------------------------------------------------------------------
169 // deal with STL/non-STL/non-STL-but-wxUSE_STD_STRING
170 // ----------------------------------------------------------------------------
172 #if wxUSE_STL || wxUSE_STD_STRING
173 // these compilers come without standard C++ library headers by default,
174 // remove the tests here if you do have them (e.g. from STLPort)
175 #if defined(__DMC__) || defined(__WATCOMC__)
177 #undef wxUSE_STD_STRING
179 #define wxUSE_STD_STRING 0
181 #endif // wxUSE_STL || wxUSE_STD_STRING
183 // in both cases we need to define wxStdString
184 #if wxUSE_STL || wxUSE_STD_STRING
186 #include "wx/beforestd.h"
188 #include "wx/afterstd.h"
191 #ifdef HAVE_STD_WSTRING
192 typedef std::wstring wxStdString
;
194 typedef std::basic_string
<wxChar
> wxStdString
;
197 typedef std::string wxStdString
;
200 #endif // need <string>
204 // we don't need an extra ctor from std::string when copy ctor already does
206 #undef wxUSE_STD_STRING
207 #define wxUSE_STD_STRING 0
209 #if (defined(__GNUG__) && (__GNUG__ < 3)) || \
210 (defined(_MSC_VER) && (_MSC_VER <= 1200))
211 #define wxSTRING_BASE_HASNT_CLEAR
214 typedef wxStdString wxStringBase
;
215 #else // if !wxUSE_STL
217 #if !defined(HAVE_STD_STRING_COMPARE) && \
218 (!defined(__WX_SETUP_H__) || wxUSE_STL == 0)
219 #define HAVE_STD_STRING_COMPARE
222 // ---------------------------------------------------------------------------
223 // string data prepended with some housekeeping info (used by wxString class),
224 // is never used directly (but had to be put here to allow inlining)
225 // ---------------------------------------------------------------------------
227 struct WXDLLIMPEXP_BASE wxStringData
229 int nRefs
; // reference count
230 size_t nDataLength
, // actual string length
231 nAllocLength
; // allocated memory size
233 // mimics declaration 'wxChar data[nAllocLength]'
234 wxChar
* data() const { return (wxChar
*)(this + 1); }
236 // empty string has a special ref count so it's never deleted
237 bool IsEmpty() const { return (nRefs
== -1); }
238 bool IsShared() const { return (nRefs
> 1); }
241 void Lock() { if ( !IsEmpty() ) nRefs
++; }
243 // VC++ will refuse to inline Unlock but profiling shows that it is wrong
244 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
247 // VC++ free must take place in same DLL as allocation when using non dll
248 // run-time library (e.g. Multithreaded instead of Multithreaded DLL)
249 #if defined(__VISUALC__) && defined(_MT) && !defined(_DLL)
250 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) Free(); }
251 // we must not inline deallocation since allocation is not inlined
254 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) free(this); }
257 // if we had taken control over string memory (GetWriteBuf), it's
258 // intentionally put in invalid state
259 void Validate(bool b
) { nRefs
= (b
? 1 : 0); }
260 bool IsValid() const { return (nRefs
!= 0); }
263 class WXDLLIMPEXP_BASE wxStringBase
266 friend class WXDLLIMPEXP_BASE wxArrayString
;
269 // an 'invalid' value for string index, moved to this place due to a CW bug
270 static const size_t npos
;
272 // points to data preceded by wxStringData structure with ref count info
275 // accessor to string data
276 wxStringData
* GetStringData() const { return (wxStringData
*)m_pchData
- 1; }
278 // string (re)initialization functions
279 // initializes the string to the empty value (must be called only from
280 // ctors, use Reinit() otherwise)
281 void Init() { m_pchData
= (wxChar
*)wxEmptyString
; }
282 // initializes the string with (a part of) C-string
283 void InitWith(const wxChar
*psz
, size_t nPos
= 0, size_t nLen
= npos
);
284 // as Init, but also frees old data
285 void Reinit() { GetStringData()->Unlock(); Init(); }
288 // allocates memory for string of length nLen
289 bool AllocBuffer(size_t nLen
);
290 // copies data to another string
291 bool AllocCopy(wxString
&, int, int) const;
292 // effectively copies data to string
293 bool AssignCopy(size_t, const wxChar
*);
295 // append a (sub)string
296 bool ConcatSelf(size_t nLen
, const wxChar
*src
, size_t nMaxLen
);
297 bool ConcatSelf(size_t nLen
, const wxChar
*src
)
298 { return ConcatSelf(nLen
, src
, nLen
); }
300 // functions called before writing to the string: they copy it if there
301 // are other references to our data (should be the only owner when writing)
302 bool CopyBeforeWrite();
303 bool AllocBeforeWrite(size_t);
305 // compatibility with wxString
306 bool Alloc(size_t nLen
);
309 typedef wxChar value_type
;
310 typedef wxChar char_type
;
311 typedef size_t size_type
;
312 typedef value_type
& reference
;
313 typedef const value_type
& const_reference
;
314 typedef value_type
* pointer
;
315 typedef const value_type
* const_pointer
;
316 typedef value_type
*iterator
;
317 typedef const value_type
*const_iterator
;
319 #define wxSTRING_REVERSE_ITERATOR(name, const_or_not) \
323 typedef wxChar value_type; \
324 typedef const_or_not value_type& reference; \
325 typedef const_or_not value_type *pointer; \
326 typedef const_or_not value_type *iterator_type; \
328 name(iterator_type i) : m_cur(i) { } \
329 name(const name& ri) : m_cur(ri.m_cur) { } \
331 iterator_type base() const { return m_cur; } \
333 reference operator*() const { return *(m_cur - 1); } \
335 name& operator++() { --m_cur; return *this; } \
336 name operator++(int) { name tmp = *this; --m_cur; return tmp; } \
337 name& operator--() { ++m_cur; return *this; } \
338 name operator--(int) { name tmp = *this; ++m_cur; return tmp; } \
340 bool operator==(name ri) const { return m_cur == ri.m_cur; } \
341 bool operator!=(name ri) const { return !(*this == ri); } \
344 iterator_type m_cur; \
347 wxSTRING_REVERSE_ITERATOR(const_reverse_iterator
, const);
349 #define wxSTRING_CONST
350 wxSTRING_REVERSE_ITERATOR(reverse_iterator
, wxSTRING_CONST
);
351 #undef wxSTRING_CONST
353 #undef wxSTRING_REVERSE_ITERATOR
356 // constructors and destructor
357 // ctor for an empty string
358 wxStringBase() { Init(); }
360 wxStringBase(const wxStringBase
& stringSrc
)
362 wxASSERT_MSG( stringSrc
.GetStringData()->IsValid(),
363 _T("did you forget to call UngetWriteBuf()?") );
365 if ( stringSrc
.empty() ) {
366 // nothing to do for an empty string
370 m_pchData
= stringSrc
.m_pchData
; // share same data
371 GetStringData()->Lock(); // => one more copy
374 // string containing nRepeat copies of ch
375 wxStringBase(size_type nRepeat
, wxChar ch
);
376 // ctor takes first nLength characters from C string
377 // (default value of npos means take all the string)
378 wxStringBase(const wxChar
*psz
)
379 { InitWith(psz
, 0, npos
); }
380 wxStringBase(const wxChar
*psz
, size_t nLength
)
381 { InitWith(psz
, 0, nLength
); }
382 wxStringBase(const wxChar
*psz
,
383 const wxMBConv
& WXUNUSED(conv
),
384 size_t nLength
= npos
)
385 { InitWith(psz
, 0, nLength
); }
386 // take nLen chars starting at nPos
387 wxStringBase(const wxStringBase
& str
, size_t nPos
, size_t nLen
)
389 wxASSERT_MSG( str
.GetStringData()->IsValid(),
390 _T("did you forget to call UngetWriteBuf()?") );
392 size_t strLen
= str
.length() - nPos
; nLen
= strLen
< nLen
? strLen
: nLen
;
393 InitWith(str
.c_str(), nPos
, nLen
);
395 // take all characters from pStart to pEnd
396 wxStringBase(const void *pStart
, const void *pEnd
);
398 // dtor is not virtual, this class must not be inherited from!
401 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
402 //RN - according to the above VC++ does indeed inline this,
403 //even though it spits out two warnings
404 #pragma warning (disable:4714)
407 GetStringData()->Unlock();
410 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
411 //re-enable inlining warning
412 #pragma warning (default:4714)
414 // overloaded assignment
415 // from another wxString
416 wxStringBase
& operator=(const wxStringBase
& stringSrc
);
418 wxStringBase
& operator=(wxChar ch
);
420 wxStringBase
& operator=(const wxChar
*psz
);
422 // return the length of the string
423 size_type
length() const { return GetStringData()->nDataLength
; }
424 // return the length of the string
425 size_type
size() const { return length(); }
426 // return the maximum size of the string
427 size_type
max_size() const { return npos
; }
428 // resize the string, filling the space with c if c != 0
429 void resize(size_t nSize
, wxChar ch
= wxT('\0'));
430 // delete the contents of the string
431 void clear() { erase(0, npos
); }
432 // returns true if the string is empty
433 bool empty() const { return length() == 0; }
434 // inform string about planned change in size
435 void reserve(size_t sz
) { Alloc(sz
); }
436 size_type
capacity() const { return GetStringData()->nAllocLength
; }
439 // return the character at position n
440 value_type
at(size_type n
) const
441 { wxASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
442 // returns the writable character at position n
443 reference
at(size_type n
)
444 { wxASSERT_VALID_INDEX( n
); CopyBeforeWrite(); return m_pchData
[n
]; }
446 // lib.string.modifiers
447 // append elements str[pos], ..., str[pos+n]
448 wxStringBase
& append(const wxStringBase
& str
, size_t pos
, size_t n
)
450 wxASSERT(pos
<= str
.length());
451 ConcatSelf(n
, str
.c_str() + pos
, str
.length() - pos
);
455 wxStringBase
& append(const wxStringBase
& str
)
456 { ConcatSelf(str
.length(), str
.c_str()); return *this; }
457 // append first n (or all if n == npos) characters of sz
458 wxStringBase
& append(const wxChar
*sz
)
459 { ConcatSelf(wxStrlen(sz
), sz
); return *this; }
460 wxStringBase
& append(const wxChar
*sz
, size_t n
)
461 { ConcatSelf(n
, sz
); return *this; }
462 // append n copies of ch
463 wxStringBase
& append(size_t n
, wxChar ch
);
464 // append from first to last
465 wxStringBase
& append(const_iterator first
, const_iterator last
)
466 { ConcatSelf(last
- first
, first
); return *this; }
468 // same as `this_string = str'
469 wxStringBase
& assign(const wxStringBase
& str
)
470 { return *this = str
; }
471 // same as ` = str[pos..pos + n]
472 wxStringBase
& assign(const wxStringBase
& str
, size_t pos
, size_t n
)
473 { clear(); return append(str
, pos
, n
); }
474 // same as `= first n (or all if n == npos) characters of sz'
475 wxStringBase
& assign(const wxChar
*sz
)
476 { clear(); return append(sz
, wxStrlen(sz
)); }
477 wxStringBase
& assign(const wxChar
*sz
, size_t n
)
478 { clear(); return append(sz
, n
); }
479 // same as `= n copies of ch'
480 wxStringBase
& assign(size_t n
, wxChar ch
)
481 { clear(); return append(n
, ch
); }
482 // assign from first to last
483 wxStringBase
& assign(const_iterator first
, const_iterator last
)
484 { clear(); return append(first
, last
); }
486 // first valid index position
487 const_iterator
begin() const { return m_pchData
; }
489 // position one after the last valid one
490 const_iterator
end() const { return m_pchData
+ length(); }
493 // first element of the reversed string
494 const_reverse_iterator
rbegin() const { return const_reverse_iterator(end()); }
495 reverse_iterator
rbegin() { return reverse_iterator(end()); }
496 // one beyond the end of the reversed string
497 const_reverse_iterator
rend() const { return const_reverse_iterator(begin()); }
498 reverse_iterator
rend() { return reverse_iterator(begin()); }
500 // insert another string
501 wxStringBase
& insert(size_t nPos
, const wxStringBase
& str
)
503 wxASSERT( str
.GetStringData()->IsValid() );
504 return insert(nPos
, str
.c_str(), str
.length());
506 // insert n chars of str starting at nStart (in str)
507 wxStringBase
& insert(size_t nPos
, const wxStringBase
& str
, size_t nStart
, size_t n
)
509 wxASSERT( str
.GetStringData()->IsValid() );
510 wxASSERT( nStart
< str
.length() );
511 size_t strLen
= str
.length() - nStart
;
512 n
= strLen
< n
? strLen
: n
;
513 return insert(nPos
, str
.c_str() + nStart
, n
);
515 // insert first n (or all if n == npos) characters of sz
516 wxStringBase
& insert(size_t nPos
, const wxChar
*sz
, size_t n
= npos
);
517 // insert n copies of ch
518 wxStringBase
& insert(size_t nPos
, size_t n
, wxChar ch
)
519 { return insert(nPos
, wxStringBase(n
, ch
)); }
520 iterator
insert(iterator it
, wxChar ch
)
521 { size_t idx
= it
- begin(); insert(idx
, 1, ch
); return begin() + idx
; }
522 void insert(iterator it
, const_iterator first
, const_iterator last
)
523 { insert(it
- begin(), first
, last
- first
); }
524 void insert(iterator it
, size_type n
, wxChar ch
)
525 { insert(it
- begin(), n
, ch
); }
527 // delete characters from nStart to nStart + nLen
528 wxStringBase
& erase(size_type pos
= 0, size_type n
= npos
);
529 iterator
erase(iterator first
, iterator last
)
531 size_t idx
= first
- begin();
532 erase(idx
, last
- first
);
533 return begin() + idx
;
535 iterator
erase(iterator first
);
537 // explicit conversion to C string (use this with printf()!)
538 const wxChar
* c_str() const { return m_pchData
; }
539 const wxChar
* data() const { return m_pchData
; }
541 // replaces the substring of length nLen starting at nStart
542 wxStringBase
& replace(size_t nStart
, size_t nLen
, const wxChar
* sz
);
543 // replaces the substring of length nLen starting at nStart
544 wxStringBase
& replace(size_t nStart
, size_t nLen
, const wxStringBase
& str
)
545 { return replace(nStart
, nLen
, str
.c_str()); }
546 // replaces the substring with nCount copies of ch
547 wxStringBase
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxChar ch
);
548 // replaces a substring with another substring
549 wxStringBase
& replace(size_t nStart
, size_t nLen
,
550 const wxStringBase
& str
, size_t nStart2
, size_t nLen2
);
551 // replaces the substring with first nCount chars of sz
552 wxStringBase
& replace(size_t nStart
, size_t nLen
,
553 const wxChar
* sz
, size_t nCount
);
554 wxStringBase
& replace(iterator first
, iterator last
, const_pointer s
)
555 { return replace(first
- begin(), last
- first
, s
); }
556 wxStringBase
& replace(iterator first
, iterator last
, const_pointer s
,
558 { return replace(first
- begin(), last
- first
, s
, n
); }
559 wxStringBase
& replace(iterator first
, iterator last
, const wxStringBase
& s
)
560 { return replace(first
- begin(), last
- first
, s
); }
561 wxStringBase
& replace(iterator first
, iterator last
, size_type n
, wxChar c
)
562 { return replace(first
- begin(), last
- first
, n
, c
); }
563 wxStringBase
& replace(iterator first
, iterator last
,
564 const_iterator first1
, const_iterator last1
)
565 { return replace(first
- begin(), last
- first
, first1
, last1
- first1
); }
568 void swap(wxStringBase
& str
);
570 // All find() functions take the nStart argument which specifies the
571 // position to start the search on, the default value is 0. All functions
572 // return npos if there were no match.
575 size_t find(const wxStringBase
& str
, size_t nStart
= 0) const;
577 // find first n characters of sz
578 size_t find(const wxChar
* sz
, size_t nStart
= 0, size_t n
= npos
) const;
580 // find the first occurence of character ch after nStart
581 size_t find(wxChar ch
, size_t nStart
= 0) const;
583 // rfind() family is exactly like find() but works right to left
585 // as find, but from the end
586 size_t rfind(const wxStringBase
& str
, size_t nStart
= npos
) const;
588 // as find, but from the end
589 size_t rfind(const wxChar
* sz
, size_t nStart
= npos
,
590 size_t n
= npos
) const;
591 // as find, but from the end
592 size_t rfind(wxChar ch
, size_t nStart
= npos
) const;
594 // find first/last occurence of any character in the set
596 // as strpbrk() but starts at nStart, returns npos if not found
597 size_t find_first_of(const wxStringBase
& str
, size_t nStart
= 0) const
598 { return find_first_of(str
.c_str(), nStart
); }
600 size_t find_first_of(const wxChar
* sz
, size_t nStart
= 0) const;
601 size_t find_first_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
602 // same as find(char, size_t)
603 size_t find_first_of(wxChar c
, size_t nStart
= 0) const
604 { return find(c
, nStart
); }
605 // find the last (starting from nStart) char from str in this string
606 size_t find_last_of (const wxStringBase
& str
, size_t nStart
= npos
) const
607 { return find_last_of(str
.c_str(), nStart
); }
609 size_t find_last_of (const wxChar
* sz
, size_t nStart
= npos
) const;
610 size_t find_last_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
612 size_t find_last_of(wxChar c
, size_t nStart
= npos
) const
613 { return rfind(c
, nStart
); }
615 // find first/last occurence of any character not in the set
617 // as strspn() (starting from nStart), returns npos on failure
618 size_t find_first_not_of(const wxStringBase
& str
, size_t nStart
= 0) const
619 { return find_first_not_of(str
.c_str(), nStart
); }
621 size_t find_first_not_of(const wxChar
* sz
, size_t nStart
= 0) const;
622 size_t find_first_not_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
624 size_t find_first_not_of(wxChar ch
, size_t nStart
= 0) const;
626 size_t find_last_not_of(const wxStringBase
& str
, size_t nStart
= npos
) const
627 { return find_last_not_of(str
.c_str(), nStart
); }
629 size_t find_last_not_of(const wxChar
* sz
, size_t nStart
= npos
) const;
630 size_t find_last_not_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
632 size_t find_last_not_of(wxChar ch
, size_t nStart
= npos
) const;
634 // All compare functions return -1, 0 or 1 if the [sub]string is less,
635 // equal or greater than the compare() argument.
637 // comparison with another string
638 int compare(const wxStringBase
& str
) const;
639 // comparison with a substring
640 int compare(size_t nStart
, size_t nLen
, const wxStringBase
& str
) const;
641 // comparison of 2 substrings
642 int compare(size_t nStart
, size_t nLen
,
643 const wxStringBase
& str
, size_t nStart2
, size_t nLen2
) const;
644 // comparison with a c string
645 int compare(const wxChar
* sz
) const;
646 // substring comparison with first nCount characters of sz
647 int compare(size_t nStart
, size_t nLen
,
648 const wxChar
* sz
, size_t nCount
= npos
) const;
650 size_type
copy(wxChar
* s
, size_type n
, size_type pos
= 0);
652 // substring extraction
653 wxStringBase
substr(size_t nStart
= 0, size_t nLen
= npos
) const;
656 wxStringBase
& operator+=(const wxStringBase
& s
) { return append(s
); }
657 // string += C string
658 wxStringBase
& operator+=(const wxChar
*psz
) { return append(psz
); }
660 wxStringBase
& operator+=(wxChar ch
) { return append(1, ch
); }
665 // ----------------------------------------------------------------------------
666 // wxString: string class trying to be compatible with std::string, MFC
667 // CString and wxWindows 1.x wxString all at once
668 // ---------------------------------------------------------------------------
670 class WXDLLIMPEXP_BASE wxString
: public wxStringBase
673 friend class WXDLLIMPEXP_BASE wxArrayString
;
676 // NB: special care was taken in arranging the member functions in such order
677 // that all inline functions can be effectively inlined, verify that all
678 // performance critical functions are still inlined if you change order!
680 // if we hadn't made these operators private, it would be possible to
681 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
682 // converted to char in C and we do have operator=(char)
684 // NB: we don't need other versions (short/long and unsigned) as attempt
685 // to assign another numeric type to wxString will now result in
686 // ambiguity between operator=(char) and operator=(int)
687 wxString
& operator=(int);
689 // these methods are not implemented - there is _no_ conversion from int to
690 // string, you're doing something wrong if the compiler wants to call it!
692 // try `s << i' or `s.Printf("%d", i)' instead
696 // constructors and destructor
697 // ctor for an empty string
698 wxString() : wxStringBase() { }
700 wxString(const wxStringBase
& stringSrc
) : wxStringBase(stringSrc
) { }
701 wxString(const wxString
& stringSrc
) : wxStringBase(stringSrc
) { }
702 // string containing nRepeat copies of ch
703 wxString(wxChar ch
, size_t nRepeat
= 1)
704 : wxStringBase(nRepeat
, ch
) { }
705 wxString(size_t nRepeat
, wxChar ch
)
706 : wxStringBase(nRepeat
, ch
) { }
707 // ctor takes first nLength characters from C string
708 // (default value of npos means take all the string)
709 wxString(const wxChar
*psz
)
710 : wxStringBase(psz
? psz
: wxT("")) { }
711 wxString(const wxChar
*psz
, size_t nLength
)
712 : wxStringBase(psz
, nLength
) { }
713 wxString(const wxChar
*psz
,
714 const wxMBConv
& WXUNUSED(conv
),
715 size_t nLength
= npos
)
716 : wxStringBase(psz
, nLength
== npos
? wxStrlen(psz
) : nLength
) { }
718 // even if we're not built with wxUSE_STL == 1 it is very convenient to allow
719 // implicit conversions from std::string to wxString as this allows to use
720 // the same strings in non-GUI and GUI code, however we don't want to
721 // unconditionally add this ctor as it would make wx lib dependent on
722 // libstdc++ on some Linux versions which is bad, so instead we ask the
723 // client code to define this wxUSE_STD_STRING symbol if they need it
725 wxString(const wxStdString
& s
)
726 : wxStringBase(s
.c_str()) { }
727 #endif // wxUSE_STD_STRING
730 // from multibyte string
731 wxString(const char *psz
, const wxMBConv
& conv
, size_t nLength
= npos
);
732 // from wxWCharBuffer (i.e. return from wxGetString)
733 wxString(const wxWCharBuffer
& psz
) : wxStringBase(psz
.data()) { }
735 // from C string (for compilers using unsigned char)
736 wxString(const unsigned char* psz
)
737 : wxStringBase((const char*)psz
) { }
738 // from part of C string (for compilers using unsigned char)
739 wxString(const unsigned char* psz
, size_t nLength
)
740 : wxStringBase((const char*)psz
, nLength
) { }
743 // from wide (Unicode) string
744 wxString(const wchar_t *pwz
,
745 const wxMBConv
& conv
= wxConvLibc
,
746 size_t nLength
= npos
);
747 #endif // !wxUSE_WCHAR_T
750 wxString(const wxCharBuffer
& psz
)
751 : wxStringBase(psz
) { }
752 #endif // Unicode/ANSI
754 // generic attributes & operations
755 // as standard strlen()
756 size_t Len() const { return length(); }
757 // string contains any characters?
758 bool IsEmpty() const { return empty(); }
759 // empty string is "false", so !str will return true
760 bool operator!() const { return empty(); }
761 // truncate the string to given length
762 wxString
& Truncate(size_t uiLen
);
763 // empty string contents
768 wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") );
770 // empty the string and free memory
773 wxString
tmp(wxEmptyString
);
779 bool IsAscii() const;
781 bool IsNumber() const;
785 // data access (all indexes are 0 based)
787 wxChar
GetChar(size_t n
) const
790 wxChar
& GetWritableChar(size_t n
)
793 void SetChar(size_t n
, wxChar ch
)
796 // get last character
799 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
801 return at(length() - 1);
804 // get writable last character
807 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
808 return at(length() - 1);
812 Note that we we must define all of the overloads below to avoid
813 ambiguity when using str[0]. Also note that for a conforming compiler we
814 don't need const version of operatorp[] at all as indexed access to
815 const string is provided by implicit conversion to "const wxChar *"
816 below and defining them would only result in ambiguities, but some other
817 compilers refuse to compile "str[0]" without them.
820 #if defined(__BORLANDC__) || defined(__WATCOMC__) || defined(__MWERKS__)
821 wxChar
operator[](int n
) const
822 { return wxStringBase::at(n
); }
823 wxChar
operator[](size_type n
) const
824 { return wxStringBase::at(n
); }
825 #ifndef wxSIZE_T_IS_UINT
826 wxChar
operator[](unsigned int n
) const
827 { return wxStringBase::at(n
); }
828 #endif // size_t != unsigned int
829 #endif // broken compiler
832 // operator versions of GetWriteableChar()
833 wxChar
& operator[](int n
)
834 { return wxStringBase::at(n
); }
835 wxChar
& operator[](size_type n
)
836 { return wxStringBase::at(n
); }
837 #ifndef wxSIZE_T_IS_UINT
838 wxChar
& operator[](unsigned int n
)
839 { return wxStringBase::at(n
); }
840 #endif // size_t != unsigned int
842 // implicit conversion to C string
843 operator const wxChar
*() const { return c_str(); }
845 // identical to c_str(), for wxWin 1.6x compatibility
846 const wxChar
* wx_str() const { return c_str(); }
847 // identical to c_str(), for MFC compatibility
848 const wxChar
* GetData() const { return c_str(); }
850 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
851 // converting numbers or strings which are certain not to contain special
852 // chars (typically system functions, X atoms, environment variables etc.)
854 // the behaviour of these functions with the strings containing anything
855 // else than 7 bit ASCII characters is undefined, use at your own risk.
857 static wxString
FromAscii(const char *ascii
); // string
858 static wxString
FromAscii(const char ascii
); // char
859 const wxCharBuffer
ToAscii() const;
861 static wxString
FromAscii(const char *ascii
) { return wxString( ascii
); }
862 static wxString
FromAscii(const char ascii
) { return wxString( ascii
); }
863 const char *ToAscii() const { return c_str(); }
864 #endif // Unicode/!Unicode
866 // conversions with (possible) format conversions: have to return a
867 // buffer with temporary data
869 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
870 // return an ANSI (multibyte) string, wc_str() to return a wide string and
871 // fn_str() to return a string which should be used with the OS APIs
872 // accepting the file names. The return value is always the same, but the
873 // type differs because a function may either return pointer to the buffer
874 // directly or have to use intermediate buffer for translation.
876 const wxCharBuffer
mb_str(const wxMBConv
& conv
= wxConvLibc
) const;
878 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
880 const wxChar
* wc_str() const { return c_str(); }
882 // for compatibility with !wxUSE_UNICODE version
883 const wxChar
* wc_str(const wxMBConv
& WXUNUSED(conv
)) const { return c_str(); }
886 const wxCharBuffer
fn_str() const { return mb_str(wxConvFile
); }
888 const wxChar
* fn_str() const { return c_str(); }
889 #endif // wxMBFILES/!wxMBFILES
891 const wxChar
* mb_str() const { return c_str(); }
893 // for compatibility with wxUSE_UNICODE version
894 const wxChar
* mb_str(const wxMBConv
& WXUNUSED(conv
)) const { return c_str(); }
896 const wxWX2MBbuf
mbc_str() const { return mb_str(); }
899 const wxWCharBuffer
wc_str(const wxMBConv
& conv
) const;
900 #endif // wxUSE_WCHAR_T
902 const wxCharBuffer
fn_str() const { return wxConvFile
.cWC2WX( wc_str( wxConvLocal
) ); }
904 const wxChar
* fn_str() const { return c_str(); }
906 #endif // Unicode/ANSI
908 // overloaded assignment
909 // from another wxString
910 wxString
& operator=(const wxStringBase
& stringSrc
)
911 { return (wxString
&)wxStringBase::operator=(stringSrc
); }
913 wxString
& operator=(wxChar ch
)
914 { return (wxString
&)wxStringBase::operator=(ch
); }
915 // from a C string - STL probably will crash on NULL,
916 // so we need to compensate in that case
918 wxString
& operator=(const wxChar
*psz
)
919 { if(psz
) wxStringBase::operator=(psz
); else Clear(); return *this; }
921 wxString
& operator=(const wxChar
*psz
)
922 { return (wxString
&)wxStringBase::operator=(psz
); }
926 // from wxWCharBuffer
927 wxString
& operator=(const wxWCharBuffer
& psz
)
928 { (void) operator=((const wchar_t *)psz
); return *this; }
930 // from another kind of C string
931 wxString
& operator=(const unsigned char* psz
);
933 // from a wide string
934 wxString
& operator=(const wchar_t *pwz
);
937 wxString
& operator=(const wxCharBuffer
& psz
)
938 { (void) operator=((const char *)psz
); return *this; }
939 #endif // Unicode/ANSI
941 // string concatenation
942 // in place concatenation
944 Concatenate and return the result. Note that the left to right
945 associativity of << allows to write things like "str << str1 << str2
946 << ..." (unlike with +=)
949 wxString
& operator<<(const wxString
& s
)
952 wxASSERT_MSG( s
.GetStringData()->IsValid(),
953 _T("did you forget to call UngetWriteBuf()?") );
959 // string += C string
960 wxString
& operator<<(const wxChar
*psz
)
961 { append(psz
); return *this; }
963 wxString
& operator<<(wxChar ch
) { append(1, ch
); return *this; }
965 // string += buffer (i.e. from wxGetString)
967 wxString
& operator<<(const wxWCharBuffer
& s
)
968 { (void)operator<<((const wchar_t *)s
); return *this; }
969 void operator+=(const wxWCharBuffer
& s
)
970 { (void)operator<<((const wchar_t *)s
); }
971 #else // !wxUSE_UNICODE
972 wxString
& operator<<(const wxCharBuffer
& s
)
973 { (void)operator<<((const char *)s
); return *this; }
974 void operator+=(const wxCharBuffer
& s
)
975 { (void)operator<<((const char *)s
); }
976 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
978 // string += C string
979 wxString
& Append(const wxString
& s
)
981 // test for empty() to share the string if possible
988 wxString
& Append(const wxChar
* psz
)
989 { append(psz
); return *this; }
990 // append count copies of given character
991 wxString
& Append(wxChar ch
, size_t count
= 1u)
992 { append(count
, ch
); return *this; }
993 wxString
& Append(const wxChar
* psz
, size_t nLen
)
994 { append(psz
, nLen
); return *this; }
996 // prepend a string, return the string itself
997 wxString
& Prepend(const wxString
& str
)
998 { *this = str
+ *this; return *this; }
1000 // non-destructive concatenation
1002 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
,
1003 const wxString
& string2
);
1004 // string with a single char
1005 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxChar ch
);
1006 // char with a string
1007 friend wxString WXDLLIMPEXP_BASE
operator+(wxChar ch
, const wxString
& string
);
1008 // string with C string
1009 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
1011 // C string with string
1012 friend wxString WXDLLIMPEXP_BASE
operator+(const wxChar
*psz
,
1013 const wxString
& string
);
1015 // stream-like functions
1016 // insert an int into string
1017 wxString
& operator<<(int i
)
1018 { return (*this) << Format(_T("%d"), i
); }
1019 // insert an unsigned int into string
1020 wxString
& operator<<(unsigned int ui
)
1021 { return (*this) << Format(_T("%u"), ui
); }
1022 // insert a long into string
1023 wxString
& operator<<(long l
)
1024 { return (*this) << Format(_T("%ld"), l
); }
1025 // insert an unsigned long into string
1026 wxString
& operator<<(unsigned long ul
)
1027 { return (*this) << Format(_T("%lu"), ul
); }
1028 #if defined wxLongLong_t && !defined wxLongLongIsLong
1029 // insert a long long if they exist and aren't longs
1030 wxString
& operator<<(wxLongLong_t ll
)
1032 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("d");
1033 return (*this) << Format(fmt
, ll
);
1035 // insert an unsigned long long
1036 wxString
& operator<<(wxULongLong_t ull
)
1038 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("u");
1039 return (*this) << Format(fmt
, ull
);
1042 // insert a float into string
1043 wxString
& operator<<(float f
)
1044 { return (*this) << Format(_T("%f"), f
); }
1045 // insert a double into string
1046 wxString
& operator<<(double d
)
1047 { return (*this) << Format(_T("%g"), d
); }
1049 // string comparison
1050 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
1051 int Cmp(const wxChar
*psz
) const;
1052 int Cmp(const wxString
& s
) const;
1053 // same as Cmp() but not case-sensitive
1054 int CmpNoCase(const wxChar
*psz
) const;
1055 int CmpNoCase(const wxString
& s
) const;
1056 // test for the string equality, either considering case or not
1057 // (if compareWithCase then the case matters)
1058 bool IsSameAs(const wxChar
*psz
, bool compareWithCase
= true) const
1059 { return (compareWithCase
? Cmp(psz
) : CmpNoCase(psz
)) == 0; }
1060 // comparison with a single character: returns true if equal
1061 bool IsSameAs(wxChar c
, bool compareWithCase
= true) const
1063 return (length() == 1) && (compareWithCase
? GetChar(0u) == c
1064 : wxToupper(GetChar(0u)) == wxToupper(c
));
1067 // simple sub-string extraction
1068 // return substring starting at nFirst of length nCount (or till the end
1069 // if nCount = default value)
1070 wxString
Mid(size_t nFirst
, size_t nCount
= npos
) const;
1072 // operator version of Mid()
1073 wxString
operator()(size_t start
, size_t len
) const
1074 { return Mid(start
, len
); }
1076 // check if the string starts with the given prefix and return the rest
1077 // of the string in the provided pointer if it is not NULL; otherwise
1079 bool StartsWith(const wxChar
*prefix
, wxString
*rest
= NULL
) const;
1080 // check if the string ends with the given suffix and return the
1081 // beginning of the string before the suffix in the provided pointer if
1082 // it is not NULL; otherwise return false
1083 bool EndsWith(const wxChar
*suffix
, wxString
*rest
= NULL
) const;
1085 // get first nCount characters
1086 wxString
Left(size_t nCount
) const;
1087 // get last nCount characters
1088 wxString
Right(size_t nCount
) const;
1089 // get all characters before the first occurance of ch
1090 // (returns the whole string if ch not found)
1091 wxString
BeforeFirst(wxChar ch
) const;
1092 // get all characters before the last occurence of ch
1093 // (returns empty string if ch not found)
1094 wxString
BeforeLast(wxChar ch
) const;
1095 // get all characters after the first occurence of ch
1096 // (returns empty string if ch not found)
1097 wxString
AfterFirst(wxChar ch
) const;
1098 // get all characters after the last occurence of ch
1099 // (returns the whole string if ch not found)
1100 wxString
AfterLast(wxChar ch
) const;
1102 // for compatibility only, use more explicitly named functions above
1103 wxString
Before(wxChar ch
) const { return BeforeLast(ch
); }
1104 wxString
After(wxChar ch
) const { return AfterFirst(ch
); }
1107 // convert to upper case in place, return the string itself
1108 wxString
& MakeUpper();
1109 // convert to upper case, return the copy of the string
1110 // Here's something to remember: BC++ doesn't like returns in inlines.
1111 wxString
Upper() const ;
1112 // convert to lower case in place, return the string itself
1113 wxString
& MakeLower();
1114 // convert to lower case, return the copy of the string
1115 wxString
Lower() const ;
1117 // trimming/padding whitespace (either side) and truncating
1118 // remove spaces from left or from right (default) side
1119 wxString
& Trim(bool bFromRight
= true);
1120 // add nCount copies chPad in the beginning or at the end (default)
1121 wxString
& Pad(size_t nCount
, wxChar chPad
= wxT(' '), bool bFromRight
= true);
1123 // searching and replacing
1124 // searching (return starting index, or -1 if not found)
1125 int Find(wxChar ch
, bool bFromEnd
= false) const; // like strchr/strrchr
1126 // searching (return starting index, or -1 if not found)
1127 int Find(const wxChar
*pszSub
) const; // like strstr
1128 // replace first (or all of bReplaceAll) occurences of substring with
1129 // another string, returns the number of replacements made
1130 size_t Replace(const wxChar
*szOld
,
1131 const wxChar
*szNew
,
1132 bool bReplaceAll
= true);
1134 // check if the string contents matches a mask containing '*' and '?'
1135 bool Matches(const wxChar
*szMask
) const;
1137 // conversion to numbers: all functions return true only if the whole
1138 // string is a number and put the value of this number into the pointer
1139 // provided, the base is the numeric base in which the conversion should be
1140 // done and must be comprised between 2 and 36 or be 0 in which case the
1141 // standard C rules apply (leading '0' => octal, "0x" => hex)
1142 // convert to a signed integer
1143 bool ToLong(long *val
, int base
= 10) const;
1144 // convert to an unsigned integer
1145 bool ToULong(unsigned long *val
, int base
= 10) const;
1146 // convert to wxLongLong
1147 #if defined(wxLongLong_t)
1148 bool ToLongLong(wxLongLong_t
*val
, int base
= 10) const;
1149 // convert to wxULongLong
1150 bool ToULongLong(wxULongLong_t
*val
, int base
= 10) const;
1151 #endif // wxLongLong_t
1152 // convert to a double
1153 bool ToDouble(double *val
) const;
1157 // formatted input/output
1158 // as sprintf(), returns the number of characters written or < 0 on error
1159 // (take 'this' into account in attribute parameter count)
1160 int Printf(const wxChar
*pszFormat
, ...) ATTRIBUTE_PRINTF_2
;
1161 // as vprintf(), returns the number of characters written or < 0 on error
1162 int PrintfV(const wxChar
* pszFormat
, va_list argptr
);
1164 // returns the string containing the result of Printf() to it
1165 static wxString
Format(const wxChar
*pszFormat
, ...) ATTRIBUTE_PRINTF_1
;
1166 // the same as above, but takes a va_list
1167 static wxString
FormatV(const wxChar
*pszFormat
, va_list argptr
);
1169 // raw access to string memory
1170 // ensure that string has space for at least nLen characters
1171 // only works if the data of this string is not shared
1172 bool Alloc(size_t nLen
) { reserve(nLen
); /*return capacity() >= nLen;*/ return true; }
1173 // minimize the string's memory
1174 // only works if the data of this string is not shared
1177 // get writable buffer of at least nLen bytes. Unget() *must* be called
1178 // a.s.a.p. to put string back in a reasonable state!
1179 wxChar
*GetWriteBuf(size_t nLen
);
1180 // call this immediately after GetWriteBuf() has been used
1181 void UngetWriteBuf();
1182 void UngetWriteBuf(size_t nLen
);
1185 // wxWidgets version 1 compatibility functions
1188 wxString
SubString(size_t from
, size_t to
) const
1189 { return Mid(from
, (to
- from
+ 1)); }
1190 // values for second parameter of CompareTo function
1191 enum caseCompare
{exact
, ignoreCase
};
1192 // values for first parameter of Strip function
1193 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
1196 // (take 'this' into account in attribute parameter count)
1197 int sprintf(const wxChar
*pszFormat
, ...) ATTRIBUTE_PRINTF_2
;
1200 inline int CompareTo(const wxChar
* psz
, caseCompare cmp
= exact
) const
1201 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
1204 size_t Length() const { return length(); }
1205 // Count the number of characters
1206 int Freq(wxChar ch
) const;
1208 void LowerCase() { MakeLower(); }
1210 void UpperCase() { MakeUpper(); }
1211 // use Trim except that it doesn't change this string
1212 wxString
Strip(stripType w
= trailing
) const;
1214 // use Find (more general variants not yet supported)
1215 size_t Index(const wxChar
* psz
) const { return Find(psz
); }
1216 size_t Index(wxChar ch
) const { return Find(ch
); }
1218 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
1219 wxString
& RemoveLast(size_t n
= 1) { return Truncate(length() - n
); }
1221 wxString
& Remove(size_t nStart
, size_t nLen
)
1222 { return (wxString
&)erase( nStart
, nLen
); }
1225 int First( const wxChar ch
) const { return Find(ch
); }
1226 int First( const wxChar
* psz
) const { return Find(psz
); }
1227 int First( const wxString
&str
) const { return Find(str
); }
1228 int Last( const wxChar ch
) const { return Find(ch
, true); }
1229 bool Contains(const wxString
& str
) const { return Find(str
) != wxNOT_FOUND
; }
1232 bool IsNull() const { return empty(); }
1234 // std::string compatibility functions
1236 // take nLen chars starting at nPos
1237 wxString(const wxString
& str
, size_t nPos
, size_t nLen
)
1238 : wxStringBase(str
, nPos
, nLen
) { }
1239 // take all characters from pStart to pEnd
1240 wxString(const void *pStart
, const void *pEnd
)
1241 : wxStringBase((const wxChar
*)pStart
, (const wxChar
*)pEnd
) { }
1243 wxString(const_iterator first
, const_iterator last
)
1244 : wxStringBase(first
, last
) { }
1247 // lib.string.modifiers
1248 // append elements str[pos], ..., str[pos+n]
1249 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
1250 { return (wxString
&)wxStringBase::append(str
, pos
, n
); }
1252 wxString
& append(const wxString
& str
)
1253 { return (wxString
&)wxStringBase::append(str
); }
1254 // append first n (or all if n == npos) characters of sz
1255 wxString
& append(const wxChar
*sz
)
1256 { return (wxString
&)wxStringBase::append(sz
); }
1257 wxString
& append(const wxChar
*sz
, size_t n
)
1258 { return (wxString
&)wxStringBase::append(sz
, n
); }
1259 // append n copies of ch
1260 wxString
& append(size_t n
, wxChar ch
)
1261 { return (wxString
&)wxStringBase::append(n
, ch
); }
1262 // append from first to last
1263 wxString
& append(const_iterator first
, const_iterator last
)
1264 { return (wxString
&)wxStringBase::append(first
, last
); }
1266 // same as `this_string = str'
1267 wxString
& assign(const wxString
& str
)
1268 { return (wxString
&)wxStringBase::assign(str
); }
1269 // same as ` = str[pos..pos + n]
1270 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
1271 { return (wxString
&)wxStringBase::assign(str
, pos
, n
); }
1272 // same as `= first n (or all if n == npos) characters of sz'
1273 wxString
& assign(const wxChar
*sz
)
1274 { return (wxString
&)wxStringBase::assign(sz
); }
1275 wxString
& assign(const wxChar
*sz
, size_t n
)
1276 { return (wxString
&)wxStringBase::assign(sz
, n
); }
1277 // same as `= n copies of ch'
1278 wxString
& assign(size_t n
, wxChar ch
)
1279 { return (wxString
&)wxStringBase::assign(n
, ch
); }
1280 // assign from first to last
1281 wxString
& assign(const_iterator first
, const_iterator last
)
1282 { return (wxString
&)wxStringBase::assign(first
, last
); }
1284 // string comparison
1285 #if !defined(HAVE_STD_STRING_COMPARE)
1286 int compare(const wxStringBase
& str
) const;
1287 // comparison with a substring
1288 int compare(size_t nStart
, size_t nLen
, const wxStringBase
& str
) const;
1289 // comparison of 2 substrings
1290 int compare(size_t nStart
, size_t nLen
,
1291 const wxStringBase
& str
, size_t nStart2
, size_t nLen2
) const;
1292 // just like strcmp()
1293 int compare(const wxChar
* sz
) const;
1294 // substring comparison with first nCount characters of sz
1295 int compare(size_t nStart
, size_t nLen
,
1296 const wxChar
* sz
, size_t nCount
= npos
) const;
1297 #endif // !defined HAVE_STD_STRING_COMPARE
1299 // insert another string
1300 wxString
& insert(size_t nPos
, const wxString
& str
)
1301 { return (wxString
&)wxStringBase::insert(nPos
, str
); }
1302 // insert n chars of str starting at nStart (in str)
1303 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
1304 { return (wxString
&)wxStringBase::insert(nPos
, str
, nStart
, n
); }
1305 // insert first n (or all if n == npos) characters of sz
1306 wxString
& insert(size_t nPos
, const wxChar
*sz
)
1307 { return (wxString
&)wxStringBase::insert(nPos
, sz
); }
1308 wxString
& insert(size_t nPos
, const wxChar
*sz
, size_t n
)
1309 { return (wxString
&)wxStringBase::insert(nPos
, sz
, n
); }
1310 // insert n copies of ch
1311 wxString
& insert(size_t nPos
, size_t n
, wxChar ch
)
1312 { return (wxString
&)wxStringBase::insert(nPos
, n
, ch
); }
1313 iterator
insert(iterator it
, wxChar ch
)
1314 { return wxStringBase::insert(it
, ch
); }
1315 void insert(iterator it
, const_iterator first
, const_iterator last
)
1316 { wxStringBase::insert(it
, first
, last
); }
1317 void insert(iterator it
, size_type n
, wxChar ch
)
1318 { wxStringBase::insert(it
, n
, ch
); }
1320 // delete characters from nStart to nStart + nLen
1321 wxString
& erase(size_type pos
= 0, size_type n
= npos
)
1322 { return (wxString
&)wxStringBase::erase(pos
, n
); }
1323 iterator
erase(iterator first
, iterator last
)
1324 { return wxStringBase::erase(first
, last
); }
1325 iterator
erase(iterator first
)
1326 { return wxStringBase::erase(first
); }
1328 #ifdef wxSTRING_BASE_HASNT_CLEAR
1329 void clear() { erase(); }
1332 // replaces the substring of length nLen starting at nStart
1333 wxString
& replace(size_t nStart
, size_t nLen
, const wxChar
* sz
)
1334 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, sz
); }
1335 // replaces the substring of length nLen starting at nStart
1336 wxString
& replace(size_t nStart
, size_t nLen
, const wxString
& str
)
1337 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, str
); }
1338 // replaces the substring with nCount copies of ch
1339 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxChar ch
)
1340 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, nCount
, ch
); }
1341 // replaces a substring with another substring
1342 wxString
& replace(size_t nStart
, size_t nLen
,
1343 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1344 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, str
,
1346 // replaces the substring with first nCount chars of sz
1347 wxString
& replace(size_t nStart
, size_t nLen
,
1348 const wxChar
* sz
, size_t nCount
)
1349 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, sz
, nCount
); }
1350 wxString
& replace(iterator first
, iterator last
, const_pointer s
)
1351 { return (wxString
&)wxStringBase::replace(first
, last
, s
); }
1352 wxString
& replace(iterator first
, iterator last
, const_pointer s
,
1354 { return (wxString
&)wxStringBase::replace(first
, last
, s
, n
); }
1355 wxString
& replace(iterator first
, iterator last
, const wxString
& s
)
1356 { return (wxString
&)wxStringBase::replace(first
, last
, s
); }
1357 wxString
& replace(iterator first
, iterator last
, size_type n
, wxChar c
)
1358 { return (wxString
&)wxStringBase::replace(first
, last
, n
, c
); }
1359 wxString
& replace(iterator first
, iterator last
,
1360 const_iterator first1
, const_iterator last1
)
1361 { return (wxString
&)wxStringBase::replace(first
, last
, first1
, last1
); }
1364 wxString
& operator+=(const wxString
& s
)
1365 { return (wxString
&)wxStringBase::operator+=(s
); }
1366 // string += C string
1367 wxString
& operator+=(const wxChar
*psz
)
1368 { return (wxString
&)wxStringBase::operator+=(psz
); }
1370 wxString
& operator+=(wxChar ch
)
1371 { return (wxString
&)wxStringBase::operator+=(ch
); }
1374 // notice that even though for many compilers the friend declarations above are
1375 // enough, from the point of view of C++ standard we must have the declarations
1376 // here as friend ones are not injected in the enclosing namespace and without
1377 // them the code fails to compile with conforming compilers such as xlC or g++4
1378 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
, const wxString
& string2
);
1379 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxChar ch
);
1380 wxString WXDLLIMPEXP_BASE
operator+(wxChar ch
, const wxString
& string
);
1381 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const wxChar
*psz
);
1382 wxString WXDLLIMPEXP_BASE
operator+(const wxChar
*psz
, const wxString
& string
);
1385 // define wxArrayString, for compatibility
1386 #if WXWIN_COMPATIBILITY_2_4 && !wxUSE_STL
1387 #include "wx/arrstr.h"
1391 // return an empty wxString (not very useful with wxUSE_STL == 1)
1392 inline const wxString
wxGetEmptyString() { return wxString(); }
1394 // return an empty wxString (more efficient than wxString() here)
1395 inline const wxString
& wxGetEmptyString()
1397 return *(wxString
*)&wxEmptyString
;
1399 #endif // wxUSE_STL/!wxUSE_STL
1401 // ----------------------------------------------------------------------------
1402 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
1403 // ----------------------------------------------------------------------------
1407 class WXDLLIMPEXP_BASE wxStringBuffer
1410 wxStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
1411 : m_str(str
), m_buf(lenWanted
)
1414 ~wxStringBuffer() { m_str
.assign(m_buf
.data(), wxStrlen(m_buf
.data())); }
1416 operator wxChar
*() { return m_buf
.data(); }
1421 wxWCharBuffer m_buf
;
1426 DECLARE_NO_COPY_CLASS(wxStringBuffer
)
1429 class WXDLLIMPEXP_BASE wxStringBufferLength
1432 wxStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
1433 : m_str(str
), m_buf(lenWanted
), m_len(0), m_lenSet(false)
1436 ~wxStringBufferLength()
1439 m_str
.assign(m_buf
.data(), m_len
);
1442 operator wxChar
*() { return m_buf
.data(); }
1443 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
1448 wxWCharBuffer m_buf
;
1455 DECLARE_NO_COPY_CLASS(wxStringBufferLength
)
1458 #else // if !wxUSE_STL
1460 class WXDLLIMPEXP_BASE wxStringBuffer
1463 wxStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
1464 : m_str(str
), m_buf(NULL
)
1465 { m_buf
= m_str
.GetWriteBuf(lenWanted
); }
1467 ~wxStringBuffer() { m_str
.UngetWriteBuf(); }
1469 operator wxChar
*() const { return m_buf
; }
1475 DECLARE_NO_COPY_CLASS(wxStringBuffer
)
1478 class WXDLLIMPEXP_BASE wxStringBufferLength
1481 wxStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
1482 : m_str(str
), m_buf(NULL
), m_len(0), m_lenSet(false)
1484 m_buf
= m_str
.GetWriteBuf(lenWanted
);
1485 wxASSERT(m_buf
!= NULL
);
1488 ~wxStringBufferLength()
1491 m_str
.UngetWriteBuf(m_len
);
1494 operator wxChar
*() const { return m_buf
; }
1495 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
1503 DECLARE_NO_COPY_CLASS(wxStringBufferLength
)
1506 #endif // !wxUSE_STL
1508 // ---------------------------------------------------------------------------
1509 // wxString comparison functions: operator versions are always case sensitive
1510 // ---------------------------------------------------------------------------
1512 // note that when wxUSE_STL == 1 the comparison operators taking std::string
1513 // are used and defining them also for wxString would only result in
1514 // compilation ambiguities when comparing std::string and wxString
1517 inline bool operator==(const wxString
& s1
, const wxString
& s2
)
1518 { return (s1
.Len() == s2
.Len()) && (s1
.Cmp(s2
) == 0); }
1519 inline bool operator==(const wxString
& s1
, const wxChar
* s2
)
1520 { return s1
.Cmp(s2
) == 0; }
1521 inline bool operator==(const wxChar
* s1
, const wxString
& s2
)
1522 { return s2
.Cmp(s1
) == 0; }
1523 inline bool operator!=(const wxString
& s1
, const wxString
& s2
)
1524 { return (s1
.Len() != s2
.Len()) || (s1
.Cmp(s2
) != 0); }
1525 inline bool operator!=(const wxString
& s1
, const wxChar
* s2
)
1526 { return s1
.Cmp(s2
) != 0; }
1527 inline bool operator!=(const wxChar
* s1
, const wxString
& s2
)
1528 { return s2
.Cmp(s1
) != 0; }
1529 inline bool operator< (const wxString
& s1
, const wxString
& s2
)
1530 { return s1
.Cmp(s2
) < 0; }
1531 inline bool operator< (const wxString
& s1
, const wxChar
* s2
)
1532 { return s1
.Cmp(s2
) < 0; }
1533 inline bool operator< (const wxChar
* s1
, const wxString
& s2
)
1534 { return s2
.Cmp(s1
) > 0; }
1535 inline bool operator> (const wxString
& s1
, const wxString
& s2
)
1536 { return s1
.Cmp(s2
) > 0; }
1537 inline bool operator> (const wxString
& s1
, const wxChar
* s2
)
1538 { return s1
.Cmp(s2
) > 0; }
1539 inline bool operator> (const wxChar
* s1
, const wxString
& s2
)
1540 { return s2
.Cmp(s1
) < 0; }
1541 inline bool operator<=(const wxString
& s1
, const wxString
& s2
)
1542 { return s1
.Cmp(s2
) <= 0; }
1543 inline bool operator<=(const wxString
& s1
, const wxChar
* s2
)
1544 { return s1
.Cmp(s2
) <= 0; }
1545 inline bool operator<=(const wxChar
* s1
, const wxString
& s2
)
1546 { return s2
.Cmp(s1
) >= 0; }
1547 inline bool operator>=(const wxString
& s1
, const wxString
& s2
)
1548 { return s1
.Cmp(s2
) >= 0; }
1549 inline bool operator>=(const wxString
& s1
, const wxChar
* s2
)
1550 { return s1
.Cmp(s2
) >= 0; }
1551 inline bool operator>=(const wxChar
* s1
, const wxString
& s2
)
1552 { return s2
.Cmp(s1
) <= 0; }
1555 inline bool operator==(const wxString
& s1
, const wxWCharBuffer
& s2
)
1556 { return (s1
.Cmp((const wchar_t *)s2
) == 0); }
1557 inline bool operator==(const wxWCharBuffer
& s1
, const wxString
& s2
)
1558 { return (s2
.Cmp((const wchar_t *)s1
) == 0); }
1559 inline bool operator!=(const wxString
& s1
, const wxWCharBuffer
& s2
)
1560 { return (s1
.Cmp((const wchar_t *)s2
) != 0); }
1561 inline bool operator!=(const wxWCharBuffer
& s1
, const wxString
& s2
)
1562 { return (s2
.Cmp((const wchar_t *)s1
) != 0); }
1563 #else // !wxUSE_UNICODE
1564 inline bool operator==(const wxString
& s1
, const wxCharBuffer
& s2
)
1565 { return (s1
.Cmp((const char *)s2
) == 0); }
1566 inline bool operator==(const wxCharBuffer
& s1
, const wxString
& s2
)
1567 { return (s2
.Cmp((const char *)s1
) == 0); }
1568 inline bool operator!=(const wxString
& s1
, const wxCharBuffer
& s2
)
1569 { return (s1
.Cmp((const char *)s2
) != 0); }
1570 inline bool operator!=(const wxCharBuffer
& s1
, const wxString
& s2
)
1571 { return (s2
.Cmp((const char *)s1
) != 0); }
1572 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1575 inline wxString
operator+(const wxString
& string
, const wxWCharBuffer
& buf
)
1576 { return string
+ (const wchar_t *)buf
; }
1577 inline wxString
operator+(const wxWCharBuffer
& buf
, const wxString
& string
)
1578 { return (const wchar_t *)buf
+ string
; }
1579 #else // !wxUSE_UNICODE
1580 inline wxString
operator+(const wxString
& string
, const wxCharBuffer
& buf
)
1581 { return string
+ (const char *)buf
; }
1582 inline wxString
operator+(const wxCharBuffer
& buf
, const wxString
& string
)
1583 { return (const char *)buf
+ string
; }
1584 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1586 #endif // !wxUSE_STL
1588 // comparison with char (those are not defined by std::[w]string and so should
1589 // be always available)
1590 inline bool operator==(wxChar c
, const wxString
& s
) { return s
.IsSameAs(c
); }
1591 inline bool operator==(const wxString
& s
, wxChar c
) { return s
.IsSameAs(c
); }
1592 inline bool operator!=(wxChar c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
1593 inline bool operator!=(const wxString
& s
, wxChar c
) { return !s
.IsSameAs(c
); }
1595 // ---------------------------------------------------------------------------
1596 // Implementation only from here until the end of file
1597 // ---------------------------------------------------------------------------
1599 // don't pollute the library user's name space
1600 #undef wxASSERT_VALID_INDEX
1602 #if wxUSE_STD_IOSTREAM
1604 #include "wx/iosfwrap.h"
1606 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxString
&);
1608 #endif // wxSTD_STRING_COMPATIBILITY
1610 #endif // _WX_WXSTRINGH__