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 // maximum possible length for a string means "take all string" everywhere
80 #define wxSTRING_MAXLEN wxStringBase::npos
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 // global pointer to empty string
87 extern WXDLLIMPEXP_DATA_BASE(const wxChar
*) wxEmptyString
;
89 // ---------------------------------------------------------------------------
90 // global functions complementing standard C string library replacements for
91 // strlen() and portable strcasecmp()
92 //---------------------------------------------------------------------------
94 // Use wxXXX() functions from wxchar.h instead! These functions are for
95 // backwards compatibility only.
97 // checks whether the passed in pointer is NULL and if the string is empty
98 inline bool IsEmpty(const char *p
) { return (!p
|| !*p
); }
100 // safe version of strlen() (returns 0 if passed NULL pointer)
101 inline size_t Strlen(const char *psz
)
102 { return psz
? strlen(psz
) : 0; }
104 // portable strcasecmp/_stricmp
105 inline int Stricmp(const char *psz1
, const char *psz2
)
107 #if defined(__VISUALC__) && defined(__WXWINCE__)
108 register char c1
, c2
;
110 c1
= tolower(*psz1
++);
111 c2
= tolower(*psz2
++);
112 } while ( c1
&& (c1
== c2
) );
115 #elif defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) )
116 return _stricmp(psz1
, psz2
);
117 #elif defined(__SC__)
118 return _stricmp(psz1
, psz2
);
119 #elif defined(__SALFORDC__)
120 return stricmp(psz1
, psz2
);
121 #elif defined(__BORLANDC__)
122 return stricmp(psz1
, psz2
);
123 #elif defined(__WATCOMC__)
124 return stricmp(psz1
, psz2
);
125 #elif defined(__DJGPP__)
126 return stricmp(psz1
, psz2
);
127 #elif defined(__EMX__)
128 return stricmp(psz1
, psz2
);
129 #elif defined(__WXPM__)
130 return stricmp(psz1
, psz2
);
131 #elif defined(__WXPALMOS__) || \
132 defined(HAVE_STRCASECMP_IN_STRING_H) || \
133 defined(HAVE_STRCASECMP_IN_STRINGS_H) || \
134 defined(__GNUWIN32__)
135 return strcasecmp(psz1
, psz2
);
136 #elif defined(__MWERKS__) && !defined(__INTEL__)
137 register char c1
, c2
;
139 c1
= tolower(*psz1
++);
140 c2
= tolower(*psz2
++);
141 } while ( c1
&& (c1
== c2
) );
145 // almost all compilers/libraries provide this function (unfortunately under
146 // different names), that's why we don't implement our own which will surely
147 // be more efficient than this code (uncomment to use):
149 register char c1, c2;
151 c1 = tolower(*psz1++);
152 c2 = tolower(*psz2++);
153 } while ( c1 && (c1 == c2) );
158 #error "Please define string case-insensitive compare for your OS/compiler"
159 #endif // OS/compiler
162 // ----------------------------------------------------------------------------
163 // deal with STL/non-STL/non-STL-but-wxUSE_STD_STRING
164 // ----------------------------------------------------------------------------
166 // in both cases we need to define wxStdString
167 #if wxUSE_STL || wxUSE_STD_STRING
169 #include "wx/beforestd.h"
171 #include "wx/afterstd.h"
174 #ifdef HAVE_STD_WSTRING
175 typedef std::wstring wxStdString
;
177 typedef std::basic_string
<wxChar
> wxStdString
;
180 typedef std::string wxStdString
;
183 #endif // need <string>
187 // we don't need an extra ctor from std::string when copy ctor already does
189 #undef wxUSE_STD_STRING
190 #define wxUSE_STD_STRING 0
192 #if (defined(__GNUG__) && (__GNUG__ < 3)) || \
193 (defined(_MSC_VER) && (_MSC_VER <= 1200))
194 #define wxSTRING_BASE_HASNT_CLEAR
197 typedef wxStdString wxStringBase
;
198 #else // if !wxUSE_STL
200 #if !defined(HAVE_STD_STRING_COMPARE) && \
201 (!defined(__WX_SETUP_H__) || wxUSE_STL == 0)
202 #define HAVE_STD_STRING_COMPARE
205 // ---------------------------------------------------------------------------
206 // string data prepended with some housekeeping info (used by wxString class),
207 // is never used directly (but had to be put here to allow inlining)
208 // ---------------------------------------------------------------------------
210 struct WXDLLIMPEXP_BASE wxStringData
212 int nRefs
; // reference count
213 size_t nDataLength
, // actual string length
214 nAllocLength
; // allocated memory size
216 // mimics declaration 'wxChar data[nAllocLength]'
217 wxChar
* data() const { return (wxChar
*)(this + 1); }
219 // empty string has a special ref count so it's never deleted
220 bool IsEmpty() const { return (nRefs
== -1); }
221 bool IsShared() const { return (nRefs
> 1); }
224 void Lock() { if ( !IsEmpty() ) nRefs
++; }
226 // VC++ will refuse to inline Unlock but profiling shows that it is wrong
227 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
230 // VC++ free must take place in same DLL as allocation when using non dll
231 // run-time library (e.g. Multithreaded instead of Multithreaded DLL)
232 #if defined(__VISUALC__) && defined(_MT) && !defined(_DLL)
233 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) Free(); }
234 // we must not inline deallocation since allocation is not inlined
237 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) free(this); }
240 // if we had taken control over string memory (GetWriteBuf), it's
241 // intentionally put in invalid state
242 void Validate(bool b
) { nRefs
= (b
? 1 : 0); }
243 bool IsValid() const { return (nRefs
!= 0); }
246 class WXDLLIMPEXP_BASE wxStringBase
249 friend class WXDLLIMPEXP_BASE wxArrayString
;
252 // an 'invalid' value for string index, moved to this place due to a CW bug
253 static const size_t npos
;
255 // points to data preceded by wxStringData structure with ref count info
258 // accessor to string data
259 wxStringData
* GetStringData() const { return (wxStringData
*)m_pchData
- 1; }
261 // string (re)initialization functions
262 // initializes the string to the empty value (must be called only from
263 // ctors, use Reinit() otherwise)
264 void Init() { m_pchData
= (wxChar
*)wxEmptyString
; }
265 // initializes the string with (a part of) C-string
266 void InitWith(const wxChar
*psz
, size_t nPos
= 0, size_t nLen
= npos
);
267 // as Init, but also frees old data
268 void Reinit() { GetStringData()->Unlock(); Init(); }
271 // allocates memory for string of length nLen
272 bool AllocBuffer(size_t nLen
);
273 // copies data to another string
274 bool AllocCopy(wxString
&, int, int) const;
275 // effectively copies data to string
276 bool AssignCopy(size_t, const wxChar
*);
278 // append a (sub)string
279 bool ConcatSelf(size_t nLen
, const wxChar
*src
, size_t nMaxLen
);
280 bool ConcatSelf(size_t nLen
, const wxChar
*src
)
281 { return ConcatSelf(nLen
, src
, nLen
); }
283 // functions called before writing to the string: they copy it if there
284 // are other references to our data (should be the only owner when writing)
285 bool CopyBeforeWrite();
286 bool AllocBeforeWrite(size_t);
288 // compatibility with wxString
289 bool Alloc(size_t nLen
);
292 typedef wxChar value_type
;
293 typedef wxChar char_type
;
294 typedef size_t size_type
;
295 typedef value_type
& reference
;
296 typedef const value_type
& const_reference
;
297 typedef value_type
* pointer
;
298 typedef const value_type
* const_pointer
;
299 typedef value_type
*iterator
;
300 typedef const value_type
*const_iterator
;
302 #define wxSTRING_REVERSE_ITERATOR(name, const_or_not) \
306 typedef wxChar value_type; \
307 typedef const_or_not value_type& reference; \
308 typedef const_or_not value_type *pointer; \
309 typedef const_or_not value_type *iterator_type; \
311 name(iterator_type i) : m_cur(i) { } \
312 name(const name& ri) : m_cur(ri.m_cur) { } \
314 iterator_type base() const { return m_cur; } \
316 reference operator*() const { return *(m_cur - 1); } \
317 pointer operator->() const { return m_cur - 1; } \
319 name& operator++() { --m_cur; return *this; } \
320 name operator++(int) { name tmp = *this; --m_cur; return tmp; } \
321 name& operator--() { ++m_cur; return *this; } \
322 name operator--(int) { name tmp = *this; ++m_cur; return tmp; } \
324 bool operator==(name ri) const { return m_cur == ri.m_cur; } \
325 bool operator!=(name ri) const { return !(*this == ri); } \
328 iterator_type m_cur; \
331 wxSTRING_REVERSE_ITERATOR(const_reverse_iterator
, const);
333 #define wxSTRING_CONST
334 wxSTRING_REVERSE_ITERATOR(reverse_iterator
, wxSTRING_CONST
);
335 #undef wxSTRING_CONST
337 #undef wxSTRING_REVERSE_ITERATOR
340 // constructors and destructor
341 // ctor for an empty string
342 wxStringBase() { Init(); }
344 wxStringBase(const wxStringBase
& stringSrc
)
346 wxASSERT_MSG( stringSrc
.GetStringData()->IsValid(),
347 _T("did you forget to call UngetWriteBuf()?") );
349 if ( stringSrc
.empty() ) {
350 // nothing to do for an empty string
354 m_pchData
= stringSrc
.m_pchData
; // share same data
355 GetStringData()->Lock(); // => one more copy
358 // string containing nRepeat copies of ch
359 wxStringBase(size_type nRepeat
, wxChar ch
);
360 // ctor takes first nLength characters from C string
361 // (default value of npos means take all the string)
362 wxStringBase(const wxChar
*psz
)
363 { InitWith(psz
, 0, npos
); }
364 wxStringBase(const wxChar
*psz
, size_t nLength
)
365 { InitWith(psz
, 0, nLength
); }
366 wxStringBase(const wxChar
*psz
,
367 const wxMBConv
& WXUNUSED(conv
),
368 size_t nLength
= npos
)
369 { InitWith(psz
, 0, nLength
); }
370 // take nLen chars starting at nPos
371 wxStringBase(const wxStringBase
& str
, size_t nPos
, size_t nLen
)
373 wxASSERT_MSG( str
.GetStringData()->IsValid(),
374 _T("did you forget to call UngetWriteBuf()?") );
376 size_t strLen
= str
.length() - nPos
; nLen
= strLen
< nLen
? strLen
: nLen
;
377 InitWith(str
.c_str(), nPos
, nLen
);
379 // take all characters from pStart to pEnd
380 wxStringBase(const void *pStart
, const void *pEnd
);
382 // dtor is not virtual, this class must not be inherited from!
385 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
386 //RN - according to the above VC++ does indeed inline this,
387 //even though it spits out two warnings
388 #pragma warning (disable:4714)
391 GetStringData()->Unlock();
394 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
395 //re-enable inlining warning
396 #pragma warning (default:4714)
398 // overloaded assignment
399 // from another wxString
400 wxStringBase
& operator=(const wxStringBase
& stringSrc
);
402 wxStringBase
& operator=(wxChar ch
);
404 wxStringBase
& operator=(const wxChar
*psz
);
406 // return the length of the string
407 size_type
length() const { return GetStringData()->nDataLength
; }
408 // return the length of the string
409 size_type
size() const { return length(); }
410 // return the maximum size of the string
411 size_type
max_size() const { return wxSTRING_MAXLEN
; }
412 // resize the string, filling the space with c if c != 0
413 void resize(size_t nSize
, wxChar ch
= wxT('\0'));
414 // delete the contents of the string
415 void clear() { erase(0, npos
); }
416 // returns true if the string is empty
417 bool empty() const { return length() == 0; }
418 // inform string about planned change in size
419 void reserve(size_t sz
) { Alloc(sz
); }
420 size_type
capacity() const { return GetStringData()->nAllocLength
; }
423 // return the character at position n
424 value_type
at(size_type n
) const
425 { wxASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
426 // returns the writable character at position n
427 reference
at(size_type n
)
428 { wxASSERT_VALID_INDEX( n
); CopyBeforeWrite(); return m_pchData
[n
]; }
430 // lib.string.modifiers
431 // append elements str[pos], ..., str[pos+n]
432 wxStringBase
& append(const wxStringBase
& str
, size_t pos
, size_t n
)
434 wxASSERT(pos
<= str
.length());
435 ConcatSelf(n
, str
.c_str() + pos
, str
.length() - pos
);
439 wxStringBase
& append(const wxStringBase
& str
)
440 { ConcatSelf(str
.length(), str
.c_str()); return *this; }
441 // append first n (or all if n == npos) characters of sz
442 wxStringBase
& append(const wxChar
*sz
)
443 { ConcatSelf(wxStrlen(sz
), sz
); return *this; }
444 wxStringBase
& append(const wxChar
*sz
, size_t n
)
445 { ConcatSelf(n
, sz
); return *this; }
446 // append n copies of ch
447 wxStringBase
& append(size_t n
, wxChar ch
);
448 // append from first to last
449 wxStringBase
& append(const_iterator first
, const_iterator last
)
450 { ConcatSelf(last
- first
, first
); return *this; }
452 // same as `this_string = str'
453 wxStringBase
& assign(const wxStringBase
& str
)
454 { return *this = str
; }
455 // same as ` = str[pos..pos + n]
456 wxStringBase
& assign(const wxStringBase
& str
, size_t pos
, size_t n
)
457 { clear(); return append(str
, pos
, n
); }
458 // same as `= first n (or all if n == npos) characters of sz'
459 wxStringBase
& assign(const wxChar
*sz
)
460 { clear(); return append(sz
, wxStrlen(sz
)); }
461 wxStringBase
& assign(const wxChar
*sz
, size_t n
)
462 { clear(); return append(sz
, n
); }
463 // same as `= n copies of ch'
464 wxStringBase
& assign(size_t n
, wxChar ch
)
465 { clear(); return append(n
, ch
); }
466 // assign from first to last
467 wxStringBase
& assign(const_iterator first
, const_iterator last
)
468 { clear(); return append(first
, last
); }
470 // first valid index position
471 const_iterator
begin() const { return m_pchData
; }
473 // position one after the last valid one
474 const_iterator
end() const { return m_pchData
+ length(); }
477 // first element of the reversed string
478 const_reverse_iterator
rbegin() const { return const_reverse_iterator(end()); }
479 reverse_iterator
rbegin() { return reverse_iterator(end()); }
480 // one beyond the end of the reversed string
481 const_reverse_iterator
rend() const { return const_reverse_iterator(begin()); }
482 reverse_iterator
rend() { return reverse_iterator(begin()); }
484 // insert another string
485 wxStringBase
& insert(size_t nPos
, const wxStringBase
& str
)
487 wxASSERT( str
.GetStringData()->IsValid() );
488 return insert(nPos
, str
.c_str(), str
.length());
490 // insert n chars of str starting at nStart (in str)
491 wxStringBase
& insert(size_t nPos
, const wxStringBase
& str
, size_t nStart
, size_t n
)
493 wxASSERT( str
.GetStringData()->IsValid() );
494 wxASSERT( nStart
< str
.length() );
495 size_t strLen
= str
.length() - nStart
;
496 n
= strLen
< n
? strLen
: n
;
497 return insert(nPos
, str
.c_str() + nStart
, n
);
499 // insert first n (or all if n == npos) characters of sz
500 wxStringBase
& insert(size_t nPos
, const wxChar
*sz
, size_t n
= npos
);
501 // insert n copies of ch
502 wxStringBase
& insert(size_t nPos
, size_t n
, wxChar ch
)
503 { return insert(nPos
, wxStringBase(n
, ch
)); }
504 iterator
insert(iterator it
, wxChar ch
)
505 { size_t idx
= it
- begin(); insert(idx
, 1, ch
); return begin() + idx
; }
506 void insert(iterator it
, const_iterator first
, const_iterator last
)
507 { insert(it
- begin(), first
, last
- first
); }
508 void insert(iterator it
, size_type n
, wxChar ch
)
509 { insert(it
- begin(), n
, ch
); }
511 // delete characters from nStart to nStart + nLen
512 wxStringBase
& erase(size_type pos
= 0, size_type n
= npos
);
513 iterator
erase(iterator first
, iterator last
)
515 size_t idx
= first
- begin();
516 erase(idx
, last
- first
);
517 return begin() + idx
;
519 iterator
erase(iterator first
);
521 // explicit conversion to C string (use this with printf()!)
522 const wxChar
* c_str() const { return m_pchData
; }
523 const wxChar
* data() const { return m_pchData
; }
525 // replaces the substring of length nLen starting at nStart
526 wxStringBase
& replace(size_t nStart
, size_t nLen
, const wxChar
* sz
);
527 // replaces the substring of length nLen starting at nStart
528 wxStringBase
& replace(size_t nStart
, size_t nLen
, const wxStringBase
& str
)
529 { return replace(nStart
, nLen
, str
.c_str()); }
530 // replaces the substring with nCount copies of ch
531 wxStringBase
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxChar ch
);
532 // replaces a substring with another substring
533 wxStringBase
& replace(size_t nStart
, size_t nLen
,
534 const wxStringBase
& str
, size_t nStart2
, size_t nLen2
);
535 // replaces the substring with first nCount chars of sz
536 wxStringBase
& replace(size_t nStart
, size_t nLen
,
537 const wxChar
* sz
, size_t nCount
);
538 wxStringBase
& replace(iterator first
, iterator last
, const_pointer s
)
539 { return replace(first
- begin(), last
- first
, s
); }
540 wxStringBase
& replace(iterator first
, iterator last
, const_pointer s
,
542 { return replace(first
- begin(), last
- first
, s
, n
); }
543 wxStringBase
& replace(iterator first
, iterator last
, const wxStringBase
& s
)
544 { return replace(first
- begin(), last
- first
, s
); }
545 wxStringBase
& replace(iterator first
, iterator last
, size_type n
, wxChar c
)
546 { return replace(first
- begin(), last
- first
, n
, c
); }
547 wxStringBase
& replace(iterator first
, iterator last
,
548 const_iterator first1
, const_iterator last1
)
549 { return replace(first
- begin(), last
- first
, first1
, last1
- first1
); }
552 void swap(wxStringBase
& str
);
554 // All find() functions take the nStart argument which specifies the
555 // position to start the search on, the default value is 0. All functions
556 // return npos if there were no match.
559 size_t find(const wxStringBase
& str
, size_t nStart
= 0) const;
561 // find first n characters of sz
562 size_t find(const wxChar
* sz
, size_t nStart
= 0, size_t n
= npos
) const;
564 // find the first occurence of character ch after nStart
565 size_t find(wxChar ch
, size_t nStart
= 0) const;
567 // rfind() family is exactly like find() but works right to left
569 // as find, but from the end
570 size_t rfind(const wxStringBase
& str
, size_t nStart
= npos
) const;
572 // as find, but from the end
573 size_t rfind(const wxChar
* sz
, size_t nStart
= npos
,
574 size_t n
= npos
) const;
575 // as find, but from the end
576 size_t rfind(wxChar ch
, size_t nStart
= npos
) const;
578 // find first/last occurence of any character in the set
580 // as strpbrk() but starts at nStart, returns npos if not found
581 size_t find_first_of(const wxStringBase
& str
, size_t nStart
= 0) const
582 { return find_first_of(str
.c_str(), nStart
); }
584 size_t find_first_of(const wxChar
* sz
, size_t nStart
= 0) const;
585 size_t find_first_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
586 // same as find(char, size_t)
587 size_t find_first_of(wxChar c
, size_t nStart
= 0) const
588 { return find(c
, nStart
); }
589 // find the last (starting from nStart) char from str in this string
590 size_t find_last_of (const wxStringBase
& str
, size_t nStart
= npos
) const
591 { return find_last_of(str
.c_str(), nStart
); }
593 size_t find_last_of (const wxChar
* sz
, size_t nStart
= npos
) const;
594 size_t find_last_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
596 size_t find_last_of(wxChar c
, size_t nStart
= npos
) const
597 { return rfind(c
, nStart
); }
599 // find first/last occurence of any character not in the set
601 // as strspn() (starting from nStart), returns npos on failure
602 size_t find_first_not_of(const wxStringBase
& str
, size_t nStart
= 0) const
603 { return find_first_not_of(str
.c_str(), nStart
); }
605 size_t find_first_not_of(const wxChar
* sz
, size_t nStart
= 0) const;
606 size_t find_first_not_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
608 size_t find_first_not_of(wxChar ch
, size_t nStart
= 0) const;
610 size_t find_last_not_of(const wxStringBase
& str
, size_t nStart
= npos
) const
611 { return find_last_not_of(str
.c_str(), nStart
); }
613 size_t find_last_not_of(const wxChar
* sz
, size_t nStart
= npos
) const;
614 size_t find_last_not_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
616 size_t find_last_not_of(wxChar ch
, size_t nStart
= npos
) const;
618 // All compare functions return -1, 0 or 1 if the [sub]string is less,
619 // equal or greater than the compare() argument.
621 // comparison with another string
622 int compare(const wxStringBase
& str
) const;
623 // comparison with a substring
624 int compare(size_t nStart
, size_t nLen
, const wxStringBase
& str
) const;
625 // comparison of 2 substrings
626 int compare(size_t nStart
, size_t nLen
,
627 const wxStringBase
& str
, size_t nStart2
, size_t nLen2
) const;
628 // comparison with a c string
629 int compare(const wxChar
* sz
) const;
630 // substring comparison with first nCount characters of sz
631 int compare(size_t nStart
, size_t nLen
,
632 const wxChar
* sz
, size_t nCount
= npos
) const;
634 size_type
copy(wxChar
* s
, size_type n
, size_type pos
= 0);
636 // substring extraction
637 wxStringBase
substr(size_t nStart
= 0, size_t nLen
= npos
) const;
640 wxStringBase
& operator+=(const wxStringBase
& s
) { return append(s
); }
641 // string += C string
642 wxStringBase
& operator+=(const wxChar
*psz
) { return append(psz
); }
644 wxStringBase
& operator+=(wxChar ch
) { return append(1, ch
); }
649 // ----------------------------------------------------------------------------
650 // wxString: string class trying to be compatible with std::string, MFC
651 // CString and wxWindows 1.x wxString all at once
652 // ---------------------------------------------------------------------------
654 class WXDLLIMPEXP_BASE wxString
: public wxStringBase
657 friend class WXDLLIMPEXP_BASE wxArrayString
;
660 // NB: special care was taken in arranging the member functions in such order
661 // that all inline functions can be effectively inlined, verify that all
662 // performance critical functions are still inlined if you change order!
664 // if we hadn't made these operators private, it would be possible to
665 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
666 // converted to char in C and we do have operator=(char)
668 // NB: we don't need other versions (short/long and unsigned) as attempt
669 // to assign another numeric type to wxString will now result in
670 // ambiguity between operator=(char) and operator=(int)
671 wxString
& operator=(int);
673 // these methods are not implemented - there is _no_ conversion from int to
674 // string, you're doing something wrong if the compiler wants to call it!
676 // try `s << i' or `s.Printf("%d", i)' instead
680 // constructors and destructor
681 // ctor for an empty string
682 wxString() : wxStringBase() { }
684 wxString(const wxStringBase
& stringSrc
) : wxStringBase(stringSrc
) { }
685 wxString(const wxString
& stringSrc
) : wxStringBase(stringSrc
) { }
686 // string containing nRepeat copies of ch
687 wxString(wxChar ch
, size_t nRepeat
= 1)
688 : wxStringBase(nRepeat
, ch
) { }
689 wxString(size_t nRepeat
, wxChar ch
)
690 : wxStringBase(nRepeat
, ch
) { }
691 // ctor takes first nLength characters from C string
692 // (default value of npos means take all the string)
693 wxString(const wxChar
*psz
)
694 : wxStringBase(psz
? psz
: wxT("")) { }
695 wxString(const wxChar
*psz
, size_t nLength
)
696 : wxStringBase(psz
, nLength
) { }
697 wxString(const wxChar
*psz
,
698 const wxMBConv
& WXUNUSED(conv
),
699 size_t nLength
= npos
)
700 : wxStringBase(psz
, nLength
== npos
? wxStrlen(psz
) : nLength
) { }
702 // even if we're not built with wxUSE_STL == 1 it is very convenient to allow
703 // implicit conversions from std::string to wxString as this allows to use
704 // the same strings in non-GUI and GUI code, however we don't want to
705 // unconditionally add this ctor as it would make wx lib dependent on
706 // libstdc++ on some Linux versions which is bad, so instead we ask the
707 // client code to define this wxUSE_STD_STRING symbol if they need it
709 wxString(const wxStdString
& s
)
710 : wxStringBase(s
.c_str()) { }
711 #endif // wxUSE_STD_STRING
714 // from multibyte string
715 wxString(const char *psz
, const wxMBConv
& conv
, size_t nLength
= npos
);
716 // from wxWCharBuffer (i.e. return from wxGetString)
717 wxString(const wxWCharBuffer
& psz
) : wxStringBase(psz
.data()) { }
719 // from C string (for compilers using unsigned char)
720 wxString(const unsigned char* psz
)
721 : wxStringBase((const char*)psz
) { }
722 // from part of C string (for compilers using unsigned char)
723 wxString(const unsigned char* psz
, size_t nLength
)
724 : wxStringBase((const char*)psz
, nLength
) { }
727 // from wide (Unicode) string
728 wxString(const wchar_t *pwz
,
729 const wxMBConv
& conv
= wxConvLibc
,
730 size_t nLength
= npos
);
731 #endif // !wxUSE_WCHAR_T
734 wxString(const wxCharBuffer
& psz
)
735 : wxStringBase(psz
) { }
736 #endif // Unicode/ANSI
738 // generic attributes & operations
739 // as standard strlen()
740 size_t Len() const { return length(); }
741 // string contains any characters?
742 bool IsEmpty() const { return empty(); }
743 // empty string is "false", so !str will return true
744 bool operator!() const { return empty(); }
745 // truncate the string to given length
746 wxString
& Truncate(size_t uiLen
);
747 // empty string contents
752 wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") );
754 // empty the string and free memory
757 wxString
tmp(wxEmptyString
);
763 bool IsAscii() const;
765 bool IsNumber() const;
769 // data access (all indexes are 0 based)
771 wxChar
GetChar(size_t n
) const
774 wxChar
& GetWritableChar(size_t n
)
777 void SetChar(size_t n
, wxChar ch
)
780 // get last character
783 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
785 return at(length() - 1);
788 // get writable last character
791 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
792 return at(length() - 1);
796 Note that we we must define all of the overloads below to avoid
797 ambiguity when using str[0]. Also note that for a conforming compiler we
798 don't need const version of operatorp[] at all as indexed access to
799 const string is provided by implicit conversion to "const wxChar *"
800 below and defining them would only result in ambiguities, but some other
801 compilers refuse to compile "str[0]" without them.
804 #if defined(__BORLANDC__) || defined(__WATCOMC__) || defined(__MWERKS__)
805 wxChar
operator[](int n
) const
806 { return wxStringBase::at(n
); }
807 wxChar
operator[](size_type n
) const
808 { return wxStringBase::at(n
); }
809 #ifndef wxSIZE_T_IS_UINT
810 wxChar
operator[](unsigned int n
) const
811 { return wxStringBase::at(n
); }
812 #endif // size_t != unsigned int
813 #endif // broken compiler
816 // operator versions of GetWriteableChar()
817 wxChar
& operator[](int n
)
818 { return wxStringBase::at(n
); }
819 wxChar
& operator[](size_type n
)
820 { return wxStringBase::at(n
); }
821 #ifndef wxSIZE_T_IS_UINT
822 wxChar
& operator[](unsigned int n
)
823 { return wxStringBase::at(n
); }
824 #endif // size_t != unsigned int
826 // implicit conversion to C string
827 operator const wxChar
*() const { return c_str(); }
829 // identical to c_str(), for wxWin 1.6x compatibility
830 const wxChar
* wx_str() const { return c_str(); }
831 // identical to c_str(), for MFC compatibility
832 const wxChar
* GetData() const { return c_str(); }
834 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
835 // converting numbers or strings which are certain not to contain special
836 // chars (typically system functions, X atoms, environment variables etc.)
838 // the behaviour of these functions with the strings containing anything
839 // else than 7 bit ASCII characters is undefined, use at your own risk.
841 static wxString
FromAscii(const char *ascii
); // string
842 static wxString
FromAscii(const char ascii
); // char
843 const wxCharBuffer
ToAscii() const;
845 static wxString
FromAscii(const char *ascii
) { return wxString( ascii
); }
846 static wxString
FromAscii(const char ascii
) { return wxString( ascii
); }
847 const char *ToAscii() const { return c_str(); }
848 #endif // Unicode/!Unicode
850 // conversions with (possible) format conversions: have to return a
851 // buffer with temporary data
853 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
854 // return an ANSI (multibyte) string, wc_str() to return a wide string and
855 // fn_str() to return a string which should be used with the OS APIs
856 // accepting the file names. The return value is always the same, but the
857 // type differs because a function may either return pointer to the buffer
858 // directly or have to use intermediate buffer for translation.
860 const wxCharBuffer
mb_str(const wxMBConv
& conv
= wxConvLibc
) const;
862 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
864 const wxChar
* wc_str() const { return c_str(); }
866 // for compatibility with !wxUSE_UNICODE version
867 const wxChar
* wc_str(const wxMBConv
& WXUNUSED(conv
)) const { return c_str(); }
870 const wxCharBuffer
fn_str() const { return mb_str(wxConvFile
); }
872 const wxChar
* fn_str() const { return c_str(); }
873 #endif // wxMBFILES/!wxMBFILES
875 const wxChar
* mb_str() const { return c_str(); }
877 // for compatibility with wxUSE_UNICODE version
878 const wxChar
* mb_str(const wxMBConv
& WXUNUSED(conv
)) const { return c_str(); }
880 const wxWX2MBbuf
mbc_str() const { return mb_str(); }
883 const wxWCharBuffer
wc_str(const wxMBConv
& conv
) const;
884 #endif // wxUSE_WCHAR_T
886 const wxCharBuffer
fn_str() const { return wxConvFile
.cWC2WX( wc_str( wxConvLocal
) ); }
888 const wxChar
* fn_str() const { return c_str(); }
890 #endif // Unicode/ANSI
892 // overloaded assignment
893 // from another wxString
894 wxString
& operator=(const wxStringBase
& stringSrc
)
895 { return (wxString
&)wxStringBase::operator=(stringSrc
); }
897 wxString
& operator=(wxChar ch
)
898 { return (wxString
&)wxStringBase::operator=(ch
); }
899 // from a C string - STL probably will crash on NULL,
900 // so we need to compensate in that case
902 wxString
& operator=(const wxChar
*psz
)
903 { if(psz
) wxStringBase::operator=(psz
); else Clear(); return *this; }
905 wxString
& operator=(const wxChar
*psz
)
906 { return (wxString
&)wxStringBase::operator=(psz
); }
910 // from wxWCharBuffer
911 wxString
& operator=(const wxWCharBuffer
& psz
)
912 { (void) operator=((const wchar_t *)psz
); return *this; }
914 // from another kind of C string
915 wxString
& operator=(const unsigned char* psz
);
917 // from a wide string
918 wxString
& operator=(const wchar_t *pwz
);
921 wxString
& operator=(const wxCharBuffer
& psz
)
922 { (void) operator=((const char *)psz
); return *this; }
923 #endif // Unicode/ANSI
925 // string concatenation
926 // in place concatenation
928 Concatenate and return the result. Note that the left to right
929 associativity of << allows to write things like "str << str1 << str2
930 << ..." (unlike with +=)
933 wxString
& operator<<(const wxString
& s
)
936 wxASSERT_MSG( s
.GetStringData()->IsValid(),
937 _T("did you forget to call UngetWriteBuf()?") );
943 // string += C string
944 wxString
& operator<<(const wxChar
*psz
)
945 { append(psz
); return *this; }
947 wxString
& operator<<(wxChar ch
) { append(1, ch
); return *this; }
949 // string += buffer (i.e. from wxGetString)
951 wxString
& operator<<(const wxWCharBuffer
& s
)
952 { (void)operator<<((const wchar_t *)s
); return *this; }
953 void operator+=(const wxWCharBuffer
& s
)
954 { (void)operator<<((const wchar_t *)s
); }
955 #else // !wxUSE_UNICODE
956 wxString
& operator<<(const wxCharBuffer
& s
)
957 { (void)operator<<((const char *)s
); return *this; }
958 void operator+=(const wxCharBuffer
& s
)
959 { (void)operator<<((const char *)s
); }
960 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
962 // string += C string
963 wxString
& Append(const wxString
& s
)
965 // test for empty() to share the string if possible
972 wxString
& Append(const wxChar
* psz
)
973 { append(psz
); return *this; }
974 // append count copies of given character
975 wxString
& Append(wxChar ch
, size_t count
= 1u)
976 { append(count
, ch
); return *this; }
977 wxString
& Append(const wxChar
* psz
, size_t nLen
)
978 { append(psz
, nLen
); return *this; }
980 // prepend a string, return the string itself
981 wxString
& Prepend(const wxString
& str
)
982 { *this = str
+ *this; return *this; }
984 // non-destructive concatenation
986 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
,
987 const wxString
& string2
);
988 // string with a single char
989 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxChar ch
);
990 // char with a string
991 friend wxString WXDLLIMPEXP_BASE
operator+(wxChar ch
, const wxString
& string
);
992 // string with C string
993 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
995 // C string with string
996 friend wxString WXDLLIMPEXP_BASE
operator+(const wxChar
*psz
,
997 const wxString
& string
);
999 // stream-like functions
1000 // insert an int into string
1001 wxString
& operator<<(int i
)
1002 { return (*this) << Format(_T("%d"), i
); }
1003 // insert an unsigned int into string
1004 wxString
& operator<<(unsigned int ui
)
1005 { return (*this) << Format(_T("%u"), ui
); }
1006 // insert a long into string
1007 wxString
& operator<<(long l
)
1008 { return (*this) << Format(_T("%ld"), l
); }
1009 // insert an unsigned long into string
1010 wxString
& operator<<(unsigned long ul
)
1011 { return (*this) << Format(_T("%lu"), ul
); }
1012 #if defined wxLongLong_t && !defined wxLongLongIsLong
1013 // insert a long long if they exist and aren't longs
1014 wxString
& operator<<(wxLongLong_t ll
)
1016 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("d");
1017 return (*this) << Format(fmt
, ll
);
1019 // insert an unsigned long long
1020 wxString
& operator<<(wxULongLong_t ull
)
1022 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("u");
1023 return (*this) << Format(fmt
, ull
);
1026 // insert a float into string
1027 wxString
& operator<<(float f
)
1028 { return (*this) << Format(_T("%f"), f
); }
1029 // insert a double into string
1030 wxString
& operator<<(double d
)
1031 { return (*this) << Format(_T("%g"), d
); }
1033 // string comparison
1034 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
1035 int Cmp(const wxChar
*psz
) const;
1036 int Cmp(const wxString
& s
) const;
1037 // same as Cmp() but not case-sensitive
1038 int CmpNoCase(const wxChar
*psz
) const;
1039 int CmpNoCase(const wxString
& s
) const;
1040 // test for the string equality, either considering case or not
1041 // (if compareWithCase then the case matters)
1042 bool IsSameAs(const wxChar
*psz
, bool compareWithCase
= true) const
1043 { return (compareWithCase
? Cmp(psz
) : CmpNoCase(psz
)) == 0; }
1044 // comparison with a single character: returns true if equal
1045 bool IsSameAs(wxChar c
, bool compareWithCase
= true) const
1047 return (length() == 1) && (compareWithCase
? GetChar(0u) == c
1048 : wxToupper(GetChar(0u)) == wxToupper(c
));
1051 // simple sub-string extraction
1052 // return substring starting at nFirst of length nCount (or till the end
1053 // if nCount = default value)
1054 wxString
Mid(size_t nFirst
, size_t nCount
= npos
) const;
1056 // operator version of Mid()
1057 wxString
operator()(size_t start
, size_t len
) const
1058 { return Mid(start
, len
); }
1060 // check that the string starts with prefix and return the rest of the
1061 // string in the provided pointer if it is not NULL, otherwise return
1063 bool StartsWith(const wxChar
*prefix
, wxString
*rest
= NULL
) const;
1065 // get first nCount characters
1066 wxString
Left(size_t nCount
) const;
1067 // get last nCount characters
1068 wxString
Right(size_t nCount
) const;
1069 // get all characters before the first occurance of ch
1070 // (returns the whole string if ch not found)
1071 wxString
BeforeFirst(wxChar ch
) const;
1072 // get all characters before the last occurence of ch
1073 // (returns empty string if ch not found)
1074 wxString
BeforeLast(wxChar ch
) const;
1075 // get all characters after the first occurence of ch
1076 // (returns empty string if ch not found)
1077 wxString
AfterFirst(wxChar ch
) const;
1078 // get all characters after the last occurence of ch
1079 // (returns the whole string if ch not found)
1080 wxString
AfterLast(wxChar ch
) const;
1082 // for compatibility only, use more explicitly named functions above
1083 wxString
Before(wxChar ch
) const { return BeforeLast(ch
); }
1084 wxString
After(wxChar ch
) const { return AfterFirst(ch
); }
1087 // convert to upper case in place, return the string itself
1088 wxString
& MakeUpper();
1089 // convert to upper case, return the copy of the string
1090 // Here's something to remember: BC++ doesn't like returns in inlines.
1091 wxString
Upper() const ;
1092 // convert to lower case in place, return the string itself
1093 wxString
& MakeLower();
1094 // convert to lower case, return the copy of the string
1095 wxString
Lower() const ;
1097 // trimming/padding whitespace (either side) and truncating
1098 // remove spaces from left or from right (default) side
1099 wxString
& Trim(bool bFromRight
= true);
1100 // add nCount copies chPad in the beginning or at the end (default)
1101 wxString
& Pad(size_t nCount
, wxChar chPad
= wxT(' '), bool bFromRight
= true);
1103 // searching and replacing
1104 // searching (return starting index, or -1 if not found)
1105 int Find(wxChar ch
, bool bFromEnd
= false) const; // like strchr/strrchr
1106 // searching (return starting index, or -1 if not found)
1107 int Find(const wxChar
*pszSub
) const; // like strstr
1108 // replace first (or all of bReplaceAll) occurences of substring with
1109 // another string, returns the number of replacements made
1110 size_t Replace(const wxChar
*szOld
,
1111 const wxChar
*szNew
,
1112 bool bReplaceAll
= true);
1114 // check if the string contents matches a mask containing '*' and '?'
1115 bool Matches(const wxChar
*szMask
) const;
1117 // conversion to numbers: all functions return true only if the whole
1118 // string is a number and put the value of this number into the pointer
1119 // provided, the base is the numeric base in which the conversion should be
1120 // done and must be comprised between 2 and 36 or be 0 in which case the
1121 // standard C rules apply (leading '0' => octal, "0x" => hex)
1122 // convert to a signed integer
1123 bool ToLong(long *val
, int base
= 10) const;
1124 // convert to an unsigned integer
1125 bool ToULong(unsigned long *val
, int base
= 10) const;
1126 // convert to a double
1127 bool ToDouble(double *val
) const;
1129 // formatted input/output
1130 // as sprintf(), returns the number of characters written or < 0 on error
1131 // (take 'this' into account in attribute parameter count)
1132 int Printf(const wxChar
*pszFormat
, ...) ATTRIBUTE_PRINTF_2
;
1133 // as vprintf(), returns the number of characters written or < 0 on error
1134 int PrintfV(const wxChar
* pszFormat
, va_list argptr
);
1136 // returns the string containing the result of Printf() to it
1137 static wxString
Format(const wxChar
*pszFormat
, ...) ATTRIBUTE_PRINTF_1
;
1138 // the same as above, but takes a va_list
1139 static wxString
FormatV(const wxChar
*pszFormat
, va_list argptr
);
1141 // raw access to string memory
1142 // ensure that string has space for at least nLen characters
1143 // only works if the data of this string is not shared
1144 bool Alloc(size_t nLen
) { reserve(nLen
); /*return capacity() >= nLen;*/ return true; }
1145 // minimize the string's memory
1146 // only works if the data of this string is not shared
1149 // get writable buffer of at least nLen bytes. Unget() *must* be called
1150 // a.s.a.p. to put string back in a reasonable state!
1151 wxChar
*GetWriteBuf(size_t nLen
);
1152 // call this immediately after GetWriteBuf() has been used
1153 void UngetWriteBuf();
1154 void UngetWriteBuf(size_t nLen
);
1157 // wxWidgets version 1 compatibility functions
1160 wxString
SubString(size_t from
, size_t to
) const
1161 { return Mid(from
, (to
- from
+ 1)); }
1162 // values for second parameter of CompareTo function
1163 enum caseCompare
{exact
, ignoreCase
};
1164 // values for first parameter of Strip function
1165 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
1168 // (take 'this' into account in attribute parameter count)
1169 int sprintf(const wxChar
*pszFormat
, ...) ATTRIBUTE_PRINTF_2
;
1172 inline int CompareTo(const wxChar
* psz
, caseCompare cmp
= exact
) const
1173 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
1176 size_t Length() const { return length(); }
1177 // Count the number of characters
1178 int Freq(wxChar ch
) const;
1180 void LowerCase() { MakeLower(); }
1182 void UpperCase() { MakeUpper(); }
1183 // use Trim except that it doesn't change this string
1184 wxString
Strip(stripType w
= trailing
) const;
1186 // use Find (more general variants not yet supported)
1187 size_t Index(const wxChar
* psz
) const { return Find(psz
); }
1188 size_t Index(wxChar ch
) const { return Find(ch
); }
1190 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
1191 wxString
& RemoveLast(size_t n
= 1) { return Truncate(length() - n
); }
1193 wxString
& Remove(size_t nStart
, size_t nLen
)
1194 { return (wxString
&)erase( nStart
, nLen
); }
1197 int First( const wxChar ch
) const { return Find(ch
); }
1198 int First( const wxChar
* psz
) const { return Find(psz
); }
1199 int First( const wxString
&str
) const { return Find(str
); }
1200 int Last( const wxChar ch
) const { return Find(ch
, true); }
1201 bool Contains(const wxString
& str
) const { return Find(str
) != wxNOT_FOUND
; }
1204 bool IsNull() const { return empty(); }
1206 // std::string compatibility functions
1208 // take nLen chars starting at nPos
1209 wxString(const wxString
& str
, size_t nPos
, size_t nLen
)
1210 : wxStringBase(str
, nPos
, nLen
) { }
1211 // take all characters from pStart to pEnd
1212 wxString(const void *pStart
, const void *pEnd
)
1213 : wxStringBase((const wxChar
*)pStart
, (const wxChar
*)pEnd
) { }
1215 wxString(const_iterator first
, const_iterator last
)
1216 : wxStringBase(first
, last
) { }
1219 // lib.string.modifiers
1220 // append elements str[pos], ..., str[pos+n]
1221 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
1222 { return (wxString
&)wxStringBase::append(str
, pos
, n
); }
1224 wxString
& append(const wxString
& str
)
1225 { return (wxString
&)wxStringBase::append(str
); }
1226 // append first n (or all if n == npos) characters of sz
1227 wxString
& append(const wxChar
*sz
)
1228 { return (wxString
&)wxStringBase::append(sz
); }
1229 wxString
& append(const wxChar
*sz
, size_t n
)
1230 { return (wxString
&)wxStringBase::append(sz
, n
); }
1231 // append n copies of ch
1232 wxString
& append(size_t n
, wxChar ch
)
1233 { return (wxString
&)wxStringBase::append(n
, ch
); }
1234 // append from first to last
1235 wxString
& append(const_iterator first
, const_iterator last
)
1236 { return (wxString
&)wxStringBase::append(first
, last
); }
1238 // same as `this_string = str'
1239 wxString
& assign(const wxString
& str
)
1240 { return (wxString
&)wxStringBase::assign(str
); }
1241 // same as ` = str[pos..pos + n]
1242 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
1243 { return (wxString
&)wxStringBase::assign(str
, pos
, n
); }
1244 // same as `= first n (or all if n == npos) characters of sz'
1245 wxString
& assign(const wxChar
*sz
)
1246 { return (wxString
&)wxStringBase::assign(sz
); }
1247 wxString
& assign(const wxChar
*sz
, size_t n
)
1248 { return (wxString
&)wxStringBase::assign(sz
, n
); }
1249 // same as `= n copies of ch'
1250 wxString
& assign(size_t n
, wxChar ch
)
1251 { return (wxString
&)wxStringBase::assign(n
, ch
); }
1252 // assign from first to last
1253 wxString
& assign(const_iterator first
, const_iterator last
)
1254 { return (wxString
&)wxStringBase::assign(first
, last
); }
1256 // string comparison
1257 #if !defined(HAVE_STD_STRING_COMPARE)
1258 int compare(const wxStringBase
& str
) const;
1259 // comparison with a substring
1260 int compare(size_t nStart
, size_t nLen
, const wxStringBase
& str
) const;
1261 // comparison of 2 substrings
1262 int compare(size_t nStart
, size_t nLen
,
1263 const wxStringBase
& str
, size_t nStart2
, size_t nLen2
) const;
1264 // just like strcmp()
1265 int compare(const wxChar
* sz
) const;
1266 // substring comparison with first nCount characters of sz
1267 int compare(size_t nStart
, size_t nLen
,
1268 const wxChar
* sz
, size_t nCount
= npos
) const;
1269 #endif // !defined HAVE_STD_STRING_COMPARE
1271 // insert another string
1272 wxString
& insert(size_t nPos
, const wxString
& str
)
1273 { return (wxString
&)wxStringBase::insert(nPos
, str
); }
1274 // insert n chars of str starting at nStart (in str)
1275 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
1276 { return (wxString
&)wxStringBase::insert(nPos
, str
, nStart
, n
); }
1277 // insert first n (or all if n == npos) characters of sz
1278 wxString
& insert(size_t nPos
, const wxChar
*sz
)
1279 { return (wxString
&)wxStringBase::insert(nPos
, sz
); }
1280 wxString
& insert(size_t nPos
, const wxChar
*sz
, size_t n
)
1281 { return (wxString
&)wxStringBase::insert(nPos
, sz
, n
); }
1282 // insert n copies of ch
1283 wxString
& insert(size_t nPos
, size_t n
, wxChar ch
)
1284 { return (wxString
&)wxStringBase::insert(nPos
, n
, ch
); }
1285 iterator
insert(iterator it
, wxChar ch
)
1286 { return wxStringBase::insert(it
, ch
); }
1287 void insert(iterator it
, const_iterator first
, const_iterator last
)
1288 { wxStringBase::insert(it
, first
, last
); }
1289 void insert(iterator it
, size_type n
, wxChar ch
)
1290 { wxStringBase::insert(it
, n
, ch
); }
1292 // delete characters from nStart to nStart + nLen
1293 wxString
& erase(size_type pos
= 0, size_type n
= npos
)
1294 { return (wxString
&)wxStringBase::erase(pos
, n
); }
1295 iterator
erase(iterator first
, iterator last
)
1296 { return wxStringBase::erase(first
, last
); }
1297 iterator
erase(iterator first
)
1298 { return wxStringBase::erase(first
); }
1300 #ifdef wxSTRING_BASE_HASNT_CLEAR
1301 void clear() { erase(); }
1304 // replaces the substring of length nLen starting at nStart
1305 wxString
& replace(size_t nStart
, size_t nLen
, const wxChar
* sz
)
1306 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, sz
); }
1307 // replaces the substring of length nLen starting at nStart
1308 wxString
& replace(size_t nStart
, size_t nLen
, const wxString
& str
)
1309 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, str
); }
1310 // replaces the substring with nCount copies of ch
1311 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxChar ch
)
1312 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, nCount
, ch
); }
1313 // replaces a substring with another substring
1314 wxString
& replace(size_t nStart
, size_t nLen
,
1315 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1316 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, str
,
1318 // replaces the substring with first nCount chars of sz
1319 wxString
& replace(size_t nStart
, size_t nLen
,
1320 const wxChar
* sz
, size_t nCount
)
1321 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, sz
, nCount
); }
1322 wxString
& replace(iterator first
, iterator last
, const_pointer s
)
1323 { return (wxString
&)wxStringBase::replace(first
, last
, s
); }
1324 wxString
& replace(iterator first
, iterator last
, const_pointer s
,
1326 { return (wxString
&)wxStringBase::replace(first
, last
, s
, n
); }
1327 wxString
& replace(iterator first
, iterator last
, const wxString
& s
)
1328 { return (wxString
&)wxStringBase::replace(first
, last
, s
); }
1329 wxString
& replace(iterator first
, iterator last
, size_type n
, wxChar c
)
1330 { return (wxString
&)wxStringBase::replace(first
, last
, n
, c
); }
1331 wxString
& replace(iterator first
, iterator last
,
1332 const_iterator first1
, const_iterator last1
)
1333 { return (wxString
&)wxStringBase::replace(first
, last
, first1
, last1
); }
1336 wxString
& operator+=(const wxString
& s
)
1337 { return (wxString
&)wxStringBase::operator+=(s
); }
1338 // string += C string
1339 wxString
& operator+=(const wxChar
*psz
)
1340 { return (wxString
&)wxStringBase::operator+=(psz
); }
1342 wxString
& operator+=(wxChar ch
)
1343 { return (wxString
&)wxStringBase::operator+=(ch
); }
1346 // notice that even though for many compilers the friend declarations above are
1347 // enough, from the point of view of C++ standard we must have the declarations
1348 // here as friend ones are not injected in the enclosing namespace and without
1349 // them the code fails to compile with conforming compilers such as xlC or g++4
1350 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
, const wxString
& string2
);
1351 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxChar ch
);
1352 wxString WXDLLIMPEXP_BASE
operator+(wxChar ch
, const wxString
& string
);
1353 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const wxChar
*psz
);
1354 wxString WXDLLIMPEXP_BASE
operator+(const wxChar
*psz
, const wxString
& string
);
1357 // define wxArrayString, for compatibility
1358 #if WXWIN_COMPATIBILITY_2_4 && !wxUSE_STL
1359 #include "wx/arrstr.h"
1363 // return an empty wxString (not very useful with wxUSE_STL == 1)
1364 inline const wxString
wxGetEmptyString() { return wxString(); }
1366 // return an empty wxString (more efficient than wxString() here)
1367 inline const wxString
& wxGetEmptyString()
1369 return *(wxString
*)&wxEmptyString
;
1371 #endif // wxUSE_STL/!wxUSE_STL
1373 // ----------------------------------------------------------------------------
1374 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
1375 // ----------------------------------------------------------------------------
1379 class WXDLLIMPEXP_BASE wxStringBuffer
1382 wxStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
1383 : m_str(str
), m_buf(lenWanted
)
1386 ~wxStringBuffer() { m_str
.assign(m_buf
.data(), wxStrlen(m_buf
.data())); }
1388 operator wxChar
*() { return m_buf
.data(); }
1393 wxWCharBuffer m_buf
;
1398 DECLARE_NO_COPY_CLASS(wxStringBuffer
)
1401 class WXDLLIMPEXP_BASE wxStringBufferLength
1404 wxStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
1405 : m_str(str
), m_buf(lenWanted
), m_len(0), m_lenSet(false)
1408 ~wxStringBufferLength()
1411 m_str
.assign(m_buf
.data(), m_len
);
1414 operator wxChar
*() { return m_buf
.data(); }
1415 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
1420 wxWCharBuffer m_buf
;
1427 DECLARE_NO_COPY_CLASS(wxStringBufferLength
)
1430 #else // if !wxUSE_STL
1432 class WXDLLIMPEXP_BASE wxStringBuffer
1435 wxStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
1436 : m_str(str
), m_buf(NULL
)
1437 { m_buf
= m_str
.GetWriteBuf(lenWanted
); }
1439 ~wxStringBuffer() { m_str
.UngetWriteBuf(); }
1441 operator wxChar
*() const { return m_buf
; }
1447 DECLARE_NO_COPY_CLASS(wxStringBuffer
)
1450 class WXDLLIMPEXP_BASE wxStringBufferLength
1453 wxStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
1454 : m_str(str
), m_buf(NULL
), m_len(0), m_lenSet(false)
1456 m_buf
= m_str
.GetWriteBuf(lenWanted
);
1457 wxASSERT(m_buf
!= NULL
);
1460 ~wxStringBufferLength()
1463 m_str
.UngetWriteBuf(m_len
);
1466 operator wxChar
*() const { return m_buf
; }
1467 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
1475 DECLARE_NO_COPY_CLASS(wxStringBufferLength
)
1478 #endif // !wxUSE_STL
1480 // ---------------------------------------------------------------------------
1481 // wxString comparison functions: operator versions are always case sensitive
1482 // ---------------------------------------------------------------------------
1484 // note that when wxUSE_STL == 1 the comparison operators taking std::string
1485 // are used and defining them also for wxString would only result in
1486 // compilation ambiguities when comparing std::string and wxString
1489 inline bool operator==(const wxString
& s1
, const wxString
& s2
)
1490 { return (s1
.Len() == s2
.Len()) && (s1
.Cmp(s2
) == 0); }
1491 inline bool operator==(const wxString
& s1
, const wxChar
* s2
)
1492 { return s1
.Cmp(s2
) == 0; }
1493 inline bool operator==(const wxChar
* s1
, const wxString
& s2
)
1494 { return s2
.Cmp(s1
) == 0; }
1495 inline bool operator!=(const wxString
& s1
, const wxString
& s2
)
1496 { return (s1
.Len() != s2
.Len()) || (s1
.Cmp(s2
) != 0); }
1497 inline bool operator!=(const wxString
& s1
, const wxChar
* s2
)
1498 { return s1
.Cmp(s2
) != 0; }
1499 inline bool operator!=(const wxChar
* s1
, const wxString
& s2
)
1500 { return s2
.Cmp(s1
) != 0; }
1501 inline bool operator< (const wxString
& s1
, const wxString
& s2
)
1502 { return s1
.Cmp(s2
) < 0; }
1503 inline bool operator< (const wxString
& s1
, const wxChar
* s2
)
1504 { return s1
.Cmp(s2
) < 0; }
1505 inline bool operator< (const wxChar
* s1
, const wxString
& s2
)
1506 { return s2
.Cmp(s1
) > 0; }
1507 inline bool operator> (const wxString
& s1
, const wxString
& s2
)
1508 { return s1
.Cmp(s2
) > 0; }
1509 inline bool operator> (const wxString
& s1
, const wxChar
* s2
)
1510 { return s1
.Cmp(s2
) > 0; }
1511 inline bool operator> (const wxChar
* s1
, const wxString
& s2
)
1512 { return s2
.Cmp(s1
) < 0; }
1513 inline bool operator<=(const wxString
& s1
, const wxString
& s2
)
1514 { return s1
.Cmp(s2
) <= 0; }
1515 inline bool operator<=(const wxString
& s1
, const wxChar
* s2
)
1516 { return s1
.Cmp(s2
) <= 0; }
1517 inline bool operator<=(const wxChar
* s1
, const wxString
& s2
)
1518 { return s2
.Cmp(s1
) >= 0; }
1519 inline bool operator>=(const wxString
& s1
, const wxString
& s2
)
1520 { return s1
.Cmp(s2
) >= 0; }
1521 inline bool operator>=(const wxString
& s1
, const wxChar
* s2
)
1522 { return s1
.Cmp(s2
) >= 0; }
1523 inline bool operator>=(const wxChar
* s1
, const wxString
& s2
)
1524 { return s2
.Cmp(s1
) <= 0; }
1527 inline bool operator==(const wxString
& s1
, const wxWCharBuffer
& s2
)
1528 { return (s1
.Cmp((const wchar_t *)s2
) == 0); }
1529 inline bool operator==(const wxWCharBuffer
& s1
, const wxString
& s2
)
1530 { return (s2
.Cmp((const wchar_t *)s1
) == 0); }
1531 inline bool operator!=(const wxString
& s1
, const wxWCharBuffer
& s2
)
1532 { return (s1
.Cmp((const wchar_t *)s2
) != 0); }
1533 inline bool operator!=(const wxWCharBuffer
& s1
, const wxString
& s2
)
1534 { return (s2
.Cmp((const wchar_t *)s1
) != 0); }
1535 #else // !wxUSE_UNICODE
1536 inline bool operator==(const wxString
& s1
, const wxCharBuffer
& s2
)
1537 { return (s1
.Cmp((const char *)s2
) == 0); }
1538 inline bool operator==(const wxCharBuffer
& s1
, const wxString
& s2
)
1539 { return (s2
.Cmp((const char *)s1
) == 0); }
1540 inline bool operator!=(const wxString
& s1
, const wxCharBuffer
& s2
)
1541 { return (s1
.Cmp((const char *)s2
) != 0); }
1542 inline bool operator!=(const wxCharBuffer
& s1
, const wxString
& s2
)
1543 { return (s2
.Cmp((const char *)s1
) != 0); }
1544 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1547 inline wxString
operator+(const wxString
& string
, const wxWCharBuffer
& buf
)
1548 { return string
+ (const wchar_t *)buf
; }
1549 inline wxString
operator+(const wxWCharBuffer
& buf
, const wxString
& string
)
1550 { return (const wchar_t *)buf
+ string
; }
1551 #else // !wxUSE_UNICODE
1552 inline wxString
operator+(const wxString
& string
, const wxCharBuffer
& buf
)
1553 { return string
+ (const char *)buf
; }
1554 inline wxString
operator+(const wxCharBuffer
& buf
, const wxString
& string
)
1555 { return (const char *)buf
+ string
; }
1556 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1558 #endif // !wxUSE_STL
1560 // comparison with char (those are not defined by std::[w]string and so should
1561 // be always available)
1562 inline bool operator==(wxChar c
, const wxString
& s
) { return s
.IsSameAs(c
); }
1563 inline bool operator==(const wxString
& s
, wxChar c
) { return s
.IsSameAs(c
); }
1564 inline bool operator!=(wxChar c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
1565 inline bool operator!=(const wxString
& s
, wxChar c
) { return !s
.IsSameAs(c
); }
1567 // ---------------------------------------------------------------------------
1568 // Implementation only from here until the end of file
1569 // ---------------------------------------------------------------------------
1571 // don't pollute the library user's name space
1572 #undef wxASSERT_VALID_INDEX
1574 #if wxUSE_STD_IOSTREAM
1576 #include "wx/iosfwrap.h"
1578 WXDLLIMPEXP_BASE wxSTD istream
& operator>>(wxSTD istream
&, wxString
&);
1579 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxString
&);
1581 #endif // wxSTD_STRING_COMPATIBILITY
1583 #endif // _WX_WXSTRINGH__