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 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
22 #pragma interface "string.h"
25 // ----------------------------------------------------------------------------
27 // ----------------------------------------------------------------------------
29 #include "wx/defs.h" // everybody should include this
31 #if defined(__WXMAC__) || defined(__VISAGECPP__)
35 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
36 // problem in VACPP V4 with including stdlib.h multiple times
37 // strconv includes it anyway
50 #ifdef HAVE_STRCASECMP_IN_STRINGS_H
51 #include <strings.h> // for strcasecmp()
52 #endif // HAVE_STRCASECMP_IN_STRINGS_H
55 #include <StringMgr.h>
58 #include "wx/wxchar.h" // for wxChar
59 #include "wx/buffer.h" // for wxCharBuffer
60 #include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes
62 class WXDLLIMPEXP_BASE wxString
;
64 // ---------------------------------------------------------------------------
66 // ---------------------------------------------------------------------------
68 // casts [unfortunately!] needed to call some broken functions which require
69 // "char *" instead of "const char *"
70 #define WXSTRINGCAST (wxChar *)(const wxChar *)
71 #define wxCSTRINGCAST (wxChar *)(const wxChar *)
72 #define wxMBSTRINGCAST (char *)(const char *)
73 #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *)
75 // implementation only
76 #define wxASSERT_VALID_INDEX(i) \
77 wxASSERT_MSG( (size_t)(i) <= length(), _T("invalid index in wxString") )
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 // maximum possible length for a string means "take all string" everywhere
84 #define wxSTRING_MAXLEN wxStringBase::npos
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
90 // global pointer to empty string
91 extern WXDLLIMPEXP_DATA_BASE(const wxChar
*) wxEmptyString
;
93 // ---------------------------------------------------------------------------
94 // global functions complementing standard C string library replacements for
95 // strlen() and portable strcasecmp()
96 //---------------------------------------------------------------------------
98 // Use wxXXX() functions from wxchar.h instead! These functions are for
99 // backwards compatibility only.
101 // checks whether the passed in pointer is NULL and if the string is empty
102 inline bool IsEmpty(const char *p
) { return (!p
|| !*p
); }
104 // safe version of strlen() (returns 0 if passed NULL pointer)
105 inline size_t Strlen(const char *psz
)
106 { return psz
? strlen(psz
) : 0; }
108 // portable strcasecmp/_stricmp
109 inline int Stricmp(const char *psz1
, const char *psz2
)
111 #if defined(__VISUALC__) && defined(__WXWINCE__)
112 register char c1
, c2
;
114 c1
= tolower(*psz1
++);
115 c2
= tolower(*psz2
++);
116 } while ( c1
&& (c1
== c2
) );
119 #elif defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) )
120 return _stricmp(psz1
, psz2
);
121 #elif defined(__SC__)
122 return _stricmp(psz1
, psz2
);
123 #elif defined(__SALFORDC__)
124 return stricmp(psz1
, psz2
);
125 #elif defined(__BORLANDC__)
126 return stricmp(psz1
, psz2
);
127 #elif defined(__WATCOMC__)
128 return stricmp(psz1
, psz2
);
129 #elif defined(__DJGPP__)
130 return stricmp(psz1
, psz2
);
131 #elif defined(__EMX__)
132 return stricmp(psz1
, psz2
);
133 #elif defined(__WXPM__)
134 return stricmp(psz1
, psz2
);
135 #elif defined(__WXPALMOS__) || \
136 defined(HAVE_STRCASECMP_IN_STRING_H) || \
137 defined(HAVE_STRCASECMP_IN_STRINGS_H) || \
138 defined(__GNUWIN32__)
139 return strcasecmp(psz1
, psz2
);
140 #elif defined(__MWERKS__) && !defined(__INTEL__)
141 register char c1
, c2
;
143 c1
= tolower(*psz1
++);
144 c2
= tolower(*psz2
++);
145 } while ( c1
&& (c1
== c2
) );
149 // almost all compilers/libraries provide this function (unfortunately under
150 // different names), that's why we don't implement our own which will surely
151 // be more efficient than this code (uncomment to use):
153 register char c1, c2;
155 c1 = tolower(*psz1++);
156 c2 = tolower(*psz2++);
157 } while ( c1 && (c1 == c2) );
162 #error "Please define string case-insensitive compare for your OS/compiler"
163 #endif // OS/compiler
168 #include "wx/beforestd.h"
170 #include "wx/afterstd.h"
173 #ifdef HAVE_STD_WSTRING
174 typedef std::wstring wxStringBase
;
176 typedef std::basic_string
<wxChar
> wxStringBase
;
179 typedef std::string wxStringBase
;
182 #if (defined(__GNUG__) && (__GNUG__ < 3)) || \
183 (defined(_MSC_VER) && (_MSC_VER <= 1200))
184 #define wxSTRING_BASE_HASNT_CLEAR
187 #else // if !wxUSE_STL
189 #ifndef HAVE_STD_STRING_COMPARE
190 #define HAVE_STD_STRING_COMPARE
193 // ---------------------------------------------------------------------------
194 // string data prepended with some housekeeping info (used by wxString class),
195 // is never used directly (but had to be put here to allow inlining)
196 // ---------------------------------------------------------------------------
198 struct WXDLLIMPEXP_BASE wxStringData
200 int nRefs
; // reference count
201 size_t nDataLength
, // actual string length
202 nAllocLength
; // allocated memory size
204 // mimics declaration 'wxChar data[nAllocLength]'
205 wxChar
* data() const { return (wxChar
*)(this + 1); }
207 // empty string has a special ref count so it's never deleted
208 bool IsEmpty() const { return (nRefs
== -1); }
209 bool IsShared() const { return (nRefs
> 1); }
212 void Lock() { if ( !IsEmpty() ) nRefs
++; }
214 // VC++ will refuse to inline Unlock but profiling shows that it is wrong
215 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
218 // VC++ free must take place in same DLL as allocation when using non dll
219 // run-time library (e.g. Multithreaded instead of Multithreaded DLL)
220 #if defined(__VISUALC__) && defined(_MT) && !defined(_DLL)
221 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) Free(); }
222 // we must not inline deallocation since allocation is not inlined
225 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) free(this); }
228 // if we had taken control over string memory (GetWriteBuf), it's
229 // intentionally put in invalid state
230 void Validate(bool b
) { nRefs
= (b
? 1 : 0); }
231 bool IsValid() const { return (nRefs
!= 0); }
234 class WXDLLIMPEXP_BASE wxStringBase
237 friend class WXDLLIMPEXP_BASE wxArrayString
;
240 // an 'invalid' value for string index, moved to this place due to a CW bug
241 static const size_t npos
;
243 // points to data preceded by wxStringData structure with ref count info
246 // accessor to string data
247 wxStringData
* GetStringData() const { return (wxStringData
*)m_pchData
- 1; }
249 // string (re)initialization functions
250 // initializes the string to the empty value (must be called only from
251 // ctors, use Reinit() otherwise)
252 void Init() { m_pchData
= (wxChar
*)wxEmptyString
; }
253 // initializaes the string with (a part of) C-string
254 void InitWith(const wxChar
*psz
, size_t nPos
= 0, size_t nLen
= npos
);
255 // as Init, but also frees old data
256 void Reinit() { GetStringData()->Unlock(); Init(); }
259 // allocates memory for string of length nLen
260 bool AllocBuffer(size_t nLen
);
261 // copies data to another string
262 bool AllocCopy(wxString
&, int, int) const;
263 // effectively copies data to string
264 bool AssignCopy(size_t, const wxChar
*);
266 // append a (sub)string
267 bool ConcatSelf(size_t nLen
, const wxChar
*src
, size_t nMaxLen
);
268 bool ConcatSelf(size_t nLen
, const wxChar
*src
)
269 { return ConcatSelf(nLen
, src
, nLen
); }
271 // functions called before writing to the string: they copy it if there
272 // are other references to our data (should be the only owner when writing)
273 bool CopyBeforeWrite();
274 bool AllocBeforeWrite(size_t);
276 // compatibility with wxString
277 bool Alloc(size_t nLen
);
280 typedef wxChar value_type
;
281 typedef wxChar char_type
;
282 typedef size_t size_type
;
283 typedef value_type
& reference
;
284 typedef const value_type
& const_reference
;
285 typedef value_type
* pointer
;
286 typedef const value_type
* const_pointer
;
287 typedef value_type
*iterator
;
288 typedef const value_type
*const_iterator
;
290 // constructors and destructor
291 // ctor for an empty string
292 wxStringBase() { Init(); }
294 wxStringBase(const wxStringBase
& stringSrc
)
296 wxASSERT_MSG( stringSrc
.GetStringData()->IsValid(),
297 _T("did you forget to call UngetWriteBuf()?") );
299 if ( stringSrc
.empty() ) {
300 // nothing to do for an empty string
304 m_pchData
= stringSrc
.m_pchData
; // share same data
305 GetStringData()->Lock(); // => one more copy
308 // string containing nRepeat copies of ch
309 wxStringBase(size_type nRepeat
, wxChar ch
);
310 // ctor takes first nLength characters from C string
311 // (default value of npos means take all the string)
312 wxStringBase(const wxChar
*psz
)
313 { InitWith(psz
, 0, npos
); }
314 wxStringBase(const wxChar
*psz
, size_t nLength
)
315 { InitWith(psz
, 0, nLength
); }
316 wxStringBase(const wxChar
*psz
, wxMBConv
& WXUNUSED(conv
), size_t nLength
= npos
)
317 { InitWith(psz
, 0, nLength
); }
318 // take nLen chars starting at nPos
319 wxStringBase(const wxStringBase
& str
, size_t nPos
, size_t nLen
)
321 wxASSERT_MSG( str
.GetStringData()->IsValid(),
322 _T("did you forget to call UngetWriteBuf()?") );
324 size_t strLen
= str
.length() - nPos
; nLen
= strLen
< nLen
? strLen
: nLen
;
325 InitWith(str
.c_str(), nPos
, nLen
);
327 // take all characters from pStart to pEnd
328 wxStringBase(const void *pStart
, const void *pEnd
);
330 // dtor is not virtual, this class must not be inherited from!
333 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
334 //RN - according to the above VC++ does indeed inline this,
335 //even though it spits out two warnings
336 #pragma warning (disable:4714)
339 GetStringData()->Unlock();
342 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
343 //re-enable inlining warning
344 #pragma warning (default:4714)
346 // overloaded assignment
347 // from another wxString
348 wxStringBase
& operator=(const wxStringBase
& stringSrc
);
350 wxStringBase
& operator=(wxChar ch
);
352 wxStringBase
& operator=(const wxChar
*psz
);
354 // return the length of the string
355 size_type
size() const { return GetStringData()->nDataLength
; }
356 // return the length of the string
357 size_type
length() const { return size(); }
358 // return the maximum size of the string
359 size_type
max_size() const { return wxSTRING_MAXLEN
; }
360 // resize the string, filling the space with c if c != 0
361 void resize(size_t nSize
, wxChar ch
= wxT('\0'));
362 // delete the contents of the string
363 void clear() { erase(0, npos
); }
364 // returns true if the string is empty
365 bool empty() const { return size() == 0; }
366 // inform string about planned change in size
367 void reserve(size_t sz
) { Alloc(sz
); }
368 size_type
capacity() const { return GetStringData()->nAllocLength
; }
371 // return the character at position n
372 value_type
at(size_type n
) const
373 { wxASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
374 // returns the writable character at position n
375 reference
at(size_type n
)
376 { wxASSERT_VALID_INDEX( n
); CopyBeforeWrite(); return m_pchData
[n
]; }
378 // lib.string.modifiers
379 // append elements str[pos], ..., str[pos+n]
380 wxStringBase
& append(const wxStringBase
& str
, size_t pos
, size_t n
)
382 wxASSERT(pos
<= str
.length());
383 ConcatSelf(n
, str
.c_str() + pos
, str
.length() - pos
);
387 wxStringBase
& append(const wxStringBase
& str
)
388 { ConcatSelf(str
.length(), str
.c_str()); return *this; }
389 // append first n (or all if n == npos) characters of sz
390 wxStringBase
& append(const wxChar
*sz
)
391 { ConcatSelf(wxStrlen(sz
), sz
); return *this; }
392 wxStringBase
& append(const wxChar
*sz
, size_t n
)
393 { ConcatSelf(n
, sz
); return *this; }
394 // append n copies of ch
395 wxStringBase
& append(size_t n
, wxChar ch
);
396 // append from first to last
397 wxStringBase
& append(const_iterator first
, const_iterator last
)
398 { ConcatSelf(last
- first
, first
); return *this; }
400 // same as `this_string = str'
401 wxStringBase
& assign(const wxStringBase
& str
)
402 { return *this = str
; }
403 // same as ` = str[pos..pos + n]
404 wxStringBase
& assign(const wxStringBase
& str
, size_t pos
, size_t n
)
405 { clear(); return append(str
, pos
, n
); }
406 // same as `= first n (or all if n == npos) characters of sz'
407 wxStringBase
& assign(const wxChar
*sz
)
408 { clear(); return append(sz
, wxStrlen(sz
)); }
409 wxStringBase
& assign(const wxChar
*sz
, size_t n
)
410 { clear(); return append(sz
, n
); }
411 // same as `= n copies of ch'
412 wxStringBase
& assign(size_t n
, wxChar ch
)
413 { clear(); return append(n
, ch
); }
414 // assign from first to last
415 wxStringBase
& assign(const_iterator first
, const_iterator last
)
416 { clear(); return append(first
, last
); }
418 // first valid index position
419 const_iterator
begin() const { return m_pchData
; }
420 // position one after the last valid one
421 const_iterator
end() const { return m_pchData
+ length(); }
423 // first valid index position
425 // position one after the last valid one
428 // insert another string
429 wxStringBase
& insert(size_t nPos
, const wxStringBase
& str
)
431 wxASSERT( str
.GetStringData()->IsValid() );
432 return insert(nPos
, str
.c_str(), str
.length());
434 // insert n chars of str starting at nStart (in str)
435 wxStringBase
& insert(size_t nPos
, const wxStringBase
& str
, size_t nStart
, size_t n
)
437 wxASSERT( str
.GetStringData()->IsValid() );
438 wxASSERT( nStart
< str
.length() );
439 size_t strLen
= str
.length() - nStart
;
440 n
= strLen
< n
? strLen
: n
;
441 return insert(nPos
, str
.c_str() + nStart
, n
);
443 // insert first n (or all if n == npos) characters of sz
444 wxStringBase
& insert(size_t nPos
, const wxChar
*sz
, size_t n
= npos
);
445 // insert n copies of ch
446 wxStringBase
& insert(size_t nPos
, size_t n
, wxChar ch
)
447 { return insert(nPos
, wxStringBase(n
, ch
)); }
448 iterator
insert(iterator it
, wxChar ch
)
449 { size_t idx
= it
- begin(); insert(idx
, 1, ch
); return begin() + idx
; }
450 void insert(iterator it
, const_iterator first
, const_iterator last
)
451 { insert(it
- begin(), first
, last
- first
); }
452 void insert(iterator it
, size_type n
, wxChar ch
)
453 { insert(it
- begin(), n
, ch
); }
455 // delete characters from nStart to nStart + nLen
456 wxStringBase
& erase(size_type pos
= 0, size_type n
= npos
);
457 iterator
erase(iterator first
, iterator last
)
459 size_t idx
= first
- begin();
460 erase(idx
, last
- first
);
461 return begin() + idx
;
463 iterator
erase(iterator first
);
465 // explicit conversion to C string (use this with printf()!)
466 const wxChar
* c_str() const { return m_pchData
; }
467 const wxChar
* data() const { return m_pchData
; }
469 // replaces the substring of length nLen starting at nStart
470 wxStringBase
& replace(size_t nStart
, size_t nLen
, const wxChar
* sz
);
471 // replaces the substring of length nLen starting at nStart
472 wxStringBase
& replace(size_t nStart
, size_t nLen
, const wxStringBase
& str
)
473 { return replace(nStart
, nLen
, str
.c_str()); }
474 // replaces the substring with nCount copies of ch
475 wxStringBase
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxChar ch
);
476 // replaces a substring with another substring
477 wxStringBase
& replace(size_t nStart
, size_t nLen
,
478 const wxStringBase
& str
, size_t nStart2
, size_t nLen2
);
479 // replaces the substring with first nCount chars of sz
480 wxStringBase
& replace(size_t nStart
, size_t nLen
,
481 const wxChar
* sz
, size_t nCount
);
482 wxStringBase
& replace(iterator first
, iterator last
, const_pointer s
)
483 { return replace(first
- begin(), last
- first
, s
); }
484 wxStringBase
& replace(iterator first
, iterator last
, const_pointer s
,
486 { return replace(first
- begin(), last
- first
, s
, n
); }
487 wxStringBase
& replace(iterator first
, iterator last
, const wxStringBase
& s
)
488 { return replace(first
- begin(), last
- first
, s
); }
489 wxStringBase
& replace(iterator first
, iterator last
, size_type n
, wxChar c
)
490 { return replace(first
- begin(), last
- first
, n
, c
); }
491 wxStringBase
& replace(iterator first
, iterator last
,
492 const_iterator first1
, const_iterator last1
)
493 { return replace(first
- begin(), last
- first
, first1
, last1
- first1
); }
496 void swap(wxStringBase
& str
);
498 // All find() functions take the nStart argument which specifies the
499 // position to start the search on, the default value is 0. All functions
500 // return npos if there were no match.
503 size_t find(const wxStringBase
& str
, size_t nStart
= 0) const;
505 // VC++ 1.5 can't cope with this syntax.
506 #if !defined(__VISUALC__) || defined(__WIN32__)
507 // find first n characters of sz
508 size_t find(const wxChar
* sz
, size_t nStart
= 0, size_t n
= npos
) const;
511 // find the first occurence of character ch after nStart
512 size_t find(wxChar ch
, size_t nStart
= 0) const;
514 // rfind() family is exactly like find() but works right to left
516 // as find, but from the end
517 size_t rfind(const wxStringBase
& str
, size_t nStart
= npos
) const;
519 // VC++ 1.5 can't cope with this syntax.
520 // as find, but from the end
521 size_t rfind(const wxChar
* sz
, size_t nStart
= npos
,
522 size_t n
= npos
) const;
523 // as find, but from the end
524 size_t rfind(wxChar ch
, size_t nStart
= npos
) const;
526 // find first/last occurence of any character in the set
528 // as strpbrk() but starts at nStart, returns npos if not found
529 size_t find_first_of(const wxStringBase
& str
, size_t nStart
= 0) const
530 { return find_first_of(str
.c_str(), nStart
); }
532 size_t find_first_of(const wxChar
* sz
, size_t nStart
= 0) const;
533 size_t find_first_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
534 // same as find(char, size_t)
535 size_t find_first_of(wxChar c
, size_t nStart
= 0) const
536 { return find(c
, nStart
); }
537 // find the last (starting from nStart) char from str in this string
538 size_t find_last_of (const wxStringBase
& str
, size_t nStart
= npos
) const
539 { return find_last_of(str
.c_str(), nStart
); }
541 size_t find_last_of (const wxChar
* sz
, size_t nStart
= npos
) const;
542 size_t find_last_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
544 size_t find_last_of(wxChar c
, size_t nStart
= npos
) const
545 { return rfind(c
, nStart
); }
547 // find first/last occurence of any character not in the set
549 // as strspn() (starting from nStart), returns npos on failure
550 size_t find_first_not_of(const wxStringBase
& str
, size_t nStart
= 0) const
551 { return find_first_not_of(str
.c_str(), nStart
); }
553 size_t find_first_not_of(const wxChar
* sz
, size_t nStart
= 0) const;
554 size_t find_first_not_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
556 size_t find_first_not_of(wxChar ch
, size_t nStart
= 0) const;
558 size_t find_last_not_of(const wxStringBase
& str
, size_t nStart
= npos
) const
559 { return find_last_not_of(str
.c_str(), nStart
); }
561 size_t find_last_not_of(const wxChar
* sz
, size_t nStart
= npos
) const;
562 size_t find_last_not_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
564 size_t find_last_not_of(wxChar ch
, size_t nStart
= npos
) const;
566 // All compare functions return -1, 0 or 1 if the [sub]string is less,
567 // equal or greater than the compare() argument.
569 // comparison with another string
570 int compare(const wxStringBase
& str
) const;
571 // comparison with a substring
572 int compare(size_t nStart
, size_t nLen
, const wxStringBase
& str
) const;
573 // comparison of 2 substrings
574 int compare(size_t nStart
, size_t nLen
,
575 const wxStringBase
& str
, size_t nStart2
, size_t nLen2
) const;
576 // comparison with a c string
577 int compare(const wxChar
* sz
) const;
578 // substring comparison with first nCount characters of sz
579 int compare(size_t nStart
, size_t nLen
,
580 const wxChar
* sz
, size_t nCount
= npos
) const;
582 size_type
copy(wxChar
* s
, size_type n
, size_type pos
= 0);
584 // substring extraction
585 wxStringBase
substr(size_t nStart
= 0, size_t nLen
= npos
) const;
588 wxStringBase
& operator+=(const wxStringBase
& s
) { return append(s
); }
589 // string += C string
590 wxStringBase
& operator+=(const wxChar
*psz
) { return append(psz
); }
592 wxStringBase
& operator+=(wxChar ch
) { return append(1, ch
); }
597 // ----------------------------------------------------------------------------
598 // wxString: string class trying to be compatible with std::string, MFC
599 // CString and wxWindows 1.x wxString all at once
600 // ---------------------------------------------------------------------------
602 class WXDLLIMPEXP_BASE wxString
: public wxStringBase
605 friend class WXDLLIMPEXP_BASE wxArrayString
;
608 // NB: special care was taken in arranging the member functions in such order
609 // that all inline functions can be effectively inlined, verify that all
610 // performace critical functions are still inlined if you change order!
612 // if we hadn't made these operators private, it would be possible to
613 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
614 // converted to char in C and we do have operator=(char)
616 // NB: we don't need other versions (short/long and unsigned) as attempt
617 // to assign another numeric type to wxString will now result in
618 // ambiguity between operator=(char) and operator=(int)
619 wxString
& operator=(int);
621 // these methods are not implemented - there is _no_ conversion from int to
622 // string, you're doing something wrong if the compiler wants to call it!
624 // try `s << i' or `s.Printf("%d", i)' instead
628 // constructors and destructor
629 // ctor for an empty string
630 wxString() : wxStringBase() { }
632 wxString(const wxStringBase
& stringSrc
) : wxStringBase(stringSrc
) { }
633 wxString(const wxString
& stringSrc
) : wxStringBase(stringSrc
) { }
634 // string containing nRepeat copies of ch
635 wxString(wxChar ch
, size_t nRepeat
= 1)
636 : wxStringBase(nRepeat
, ch
) { }
637 wxString(size_t nRepeat
, wxChar ch
)
638 : wxStringBase(nRepeat
, ch
) { }
639 // ctor takes first nLength characters from C string
640 // (default value of npos means take all the string)
641 wxString(const wxChar
*psz
)
642 : wxStringBase(psz
? psz
: wxT("")) { }
643 wxString(const wxChar
*psz
, size_t nLength
)
644 : wxStringBase(psz
, nLength
) { }
645 wxString(const wxChar
*psz
, wxMBConv
& WXUNUSED(conv
), size_t nLength
= npos
)
646 : wxStringBase(psz
, nLength
== npos
? wxStrlen(psz
) : nLength
) { }
649 // from multibyte string
650 wxString(const char *psz
, wxMBConv
& conv
, size_t nLength
= npos
);
651 // from wxWCharBuffer (i.e. return from wxGetString)
652 wxString(const wxWCharBuffer
& psz
) : wxStringBase(psz
.data()) { }
654 // from C string (for compilers using unsigned char)
655 wxString(const unsigned char* psz
, size_t nLength
= npos
)
656 : wxStringBase((const char*)psz
, nLength
) { }
659 // from wide (Unicode) string
660 wxString(const wchar_t *pwz
, wxMBConv
& conv
= wxConvLibc
, size_t nLength
= npos
);
661 #endif // !wxUSE_WCHAR_T
664 wxString(const wxCharBuffer
& psz
)
665 : wxStringBase(psz
) { }
666 #endif // Unicode/ANSI
668 // generic attributes & operations
669 // as standard strlen()
670 size_t Len() const { return length(); }
671 // string contains any characters?
672 bool IsEmpty() const { return empty(); }
673 // empty string is "false", so !str will return true
674 bool operator!() const { return IsEmpty(); }
675 // truncate the string to given length
676 wxString
& Truncate(size_t uiLen
);
677 // empty string contents
682 wxASSERT_MSG( IsEmpty(), _T("string not empty after call to Empty()?") );
684 // empty the string and free memory
687 wxString
tmp(wxEmptyString
);
693 bool IsAscii() const;
695 bool IsNumber() const;
699 // data access (all indexes are 0 based)
701 wxChar
GetChar(size_t n
) const
704 wxChar
& GetWritableChar(size_t n
)
707 void SetChar(size_t n
, wxChar ch
)
710 // get last character
713 wxASSERT_MSG( !IsEmpty(), _T("wxString: index out of bounds") );
715 return at(length() - 1);
718 // get writable last character
721 wxASSERT_MSG( !IsEmpty(), _T("wxString: index out of bounds") );
722 return at(length() - 1);
726 Note that we we must define all of the overloads below to avoid
727 ambiguity when using str[0]. Also note that for a conforming compiler we
728 don't need const version of operatorp[] at all as indexed access to
729 const string is provided by implicit conversion to "const wxChar *"
730 below and defining them would only result in ambiguities, but some other
731 compilers refuse to compile "str[0]" without them.
734 #if defined(__BORLANDC__) || defined(__WATCOMC__) || defined(__MWERKS__)
735 wxChar
operator[](int n
) const
736 { return wxStringBase::at(n
); }
737 wxChar
operator[](size_type n
) const
738 { return wxStringBase::at(n
); }
739 #ifndef wxSIZE_T_IS_UINT
740 wxChar
operator[](unsigned int n
) const
741 { return wxStringBase::at(n
); }
742 #endif // size_t != unsigned int
743 #endif // broken compiler
746 // operator versions of GetWriteableChar()
747 wxChar
& operator[](int n
)
748 { return wxStringBase::at(n
); }
749 wxChar
& operator[](size_type n
)
750 { return wxStringBase::at(n
); }
751 #ifndef wxSIZE_T_IS_UINT
752 wxChar
& operator[](unsigned int n
)
753 { return wxStringBase::at(n
); }
754 #endif // size_t != unsigned int
756 // implicit conversion to C string
757 operator const wxChar
*() const { return c_str(); }
759 // identical to c_str(), for wxWin 1.6x compatibility
760 const wxChar
* wx_str() const { return c_str(); }
761 // identical to c_str(), for MFC compatibility
762 const wxChar
* GetData() const { return c_str(); }
764 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
765 // converting numbers or strings which are certain not to contain special
766 // chars (typically system functions, X atoms, environment variables etc.)
768 // the behaviour of these functions with the strings containing anything
769 // else than 7 bit ASCII characters is undefined, use at your own risk.
771 static wxString
FromAscii(const char *ascii
); // string
772 static wxString
FromAscii(const char ascii
); // char
773 const wxCharBuffer
ToAscii() const;
775 static wxString
FromAscii(const char *ascii
) { return wxString( ascii
); }
776 static wxString
FromAscii(const char ascii
) { return wxString( ascii
); }
777 const char *ToAscii() const { return c_str(); }
778 #endif // Unicode/!Unicode
780 // conversions with (possible) format conversions: have to return a
781 // buffer with temporary data
783 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
784 // return an ANSI (multibyte) string, wc_str() to return a wide string and
785 // fn_str() to return a string which should be used with the OS APIs
786 // accepting the file names. The return value is always the same, but the
787 // type differs because a function may either return pointer to the buffer
788 // directly or have to use intermediate buffer for translation.
790 const wxCharBuffer
mb_str(wxMBConv
& conv
= wxConvLibc
) const;
792 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
794 const wxChar
* wc_str() const { return c_str(); }
796 // for compatibility with !wxUSE_UNICODE version
797 const wxChar
* wc_str(wxMBConv
& WXUNUSED(conv
)) const { return c_str(); }
800 const wxCharBuffer
fn_str() const { return mb_str(wxConvFile
); }
802 const wxChar
* fn_str() const { return c_str(); }
803 #endif // wxMBFILES/!wxMBFILES
805 const wxChar
* mb_str() const { return c_str(); }
807 // for compatibility with wxUSE_UNICODE version
808 const wxChar
* mb_str(wxMBConv
& WXUNUSED(conv
)) const { return c_str(); }
810 const wxWX2MBbuf
mbc_str() const { return mb_str(); }
813 const wxWCharBuffer
wc_str(wxMBConv
& conv
) const;
814 #endif // wxUSE_WCHAR_T
816 const wxCharBuffer
fn_str() const { return wxConvFile
.cWC2WX( wc_str( wxConvLocal
) ); }
818 const wxChar
* fn_str() const { return c_str(); }
820 #endif // Unicode/ANSI
822 // overloaded assignment
823 // from another wxString
824 wxString
& operator=(const wxStringBase
& stringSrc
)
825 { return (wxString
&)wxStringBase::operator=(stringSrc
); }
827 wxString
& operator=(wxChar ch
)
828 { return (wxString
&)wxStringBase::operator=(ch
); }
830 wxString
& operator=(const wxChar
*psz
)
831 { return (wxString
&)wxStringBase::operator=(psz
); }
833 // from wxWCharBuffer
834 wxString
& operator=(const wxWCharBuffer
& psz
)
835 { (void) operator=((const wchar_t *)psz
); return *this; }
837 // from another kind of C string
838 wxString
& operator=(const unsigned char* psz
);
840 // from a wide string
841 wxString
& operator=(const wchar_t *pwz
);
844 wxString
& operator=(const wxCharBuffer
& psz
)
845 { (void) operator=((const char *)psz
); return *this; }
846 #endif // Unicode/ANSI
848 // string concatenation
849 // in place concatenation
851 Concatenate and return the result. Note that the left to right
852 associativity of << allows to write things like "str << str1 << str2
853 << ..." (unlike with +=)
856 wxString
& operator<<(const wxString
& s
)
859 wxASSERT_MSG( s
.GetStringData()->IsValid(),
860 _T("did you forget to call UngetWriteBuf()?") );
866 // string += C string
867 wxString
& operator<<(const wxChar
*psz
)
868 { append(psz
); return *this; }
870 wxString
& operator<<(wxChar ch
) { append(1, ch
); return *this; }
872 // string += buffer (i.e. from wxGetString)
874 wxString
& operator<<(const wxWCharBuffer
& s
)
875 { (void)operator<<((const wchar_t *)s
); return *this; }
876 void operator+=(const wxWCharBuffer
& s
)
877 { (void)operator<<((const wchar_t *)s
); }
878 #else // !wxUSE_UNICODE
879 wxString
& operator<<(const wxCharBuffer
& s
)
880 { (void)operator<<((const char *)s
); return *this; }
881 void operator+=(const wxCharBuffer
& s
)
882 { (void)operator<<((const char *)s
); }
883 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
885 // string += C string
886 wxString
& Append(const wxString
& s
)
888 // test for IsEmpty() to share the string if possible
895 wxString
& Append(const wxChar
* psz
)
896 { append(psz
); return *this; }
897 // append count copies of given character
898 wxString
& Append(wxChar ch
, size_t count
= 1u)
899 { append(count
, ch
); return *this; }
900 wxString
& Append(const wxChar
* psz
, size_t nLen
)
901 { append(psz
, nLen
); return *this; }
903 // prepend a string, return the string itself
904 wxString
& Prepend(const wxString
& str
)
905 { *this = str
+ *this; return *this; }
907 // non-destructive concatenation
909 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
, const wxString
& string2
);
911 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxChar ch
);
913 friend wxString WXDLLIMPEXP_BASE
operator+(wxChar ch
, const wxString
& string
);
915 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const wxChar
*psz
);
917 friend wxString WXDLLIMPEXP_BASE
operator+(const wxChar
*psz
, const wxString
& string
);
919 // stream-like functions
920 // insert an int into string
921 wxString
& operator<<(int i
)
922 { return (*this) << Format(_T("%d"), i
); }
923 // insert an unsigned int into string
924 wxString
& operator<<(unsigned int ui
)
925 { return (*this) << Format(_T("%u"), ui
); }
926 // insert a long into string
927 wxString
& operator<<(long l
)
928 { return (*this) << Format(_T("%ld"), l
); }
929 // insert an unsigned long into string
930 wxString
& operator<<(unsigned long ul
)
931 { return (*this) << Format(_T("%lu"), ul
); }
932 // insert a float into string
933 wxString
& operator<<(float f
)
934 { return (*this) << Format(_T("%f"), f
); }
935 // insert a double into string
936 wxString
& operator<<(double d
)
937 { return (*this) << Format(_T("%g"), d
); }
940 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
941 int Cmp(const wxChar
*psz
) const;
942 int Cmp(const wxString
& s
) const;
943 // same as Cmp() but not case-sensitive
944 int CmpNoCase(const wxChar
*psz
) const;
945 int CmpNoCase(const wxString
& s
) const;
946 // test for the string equality, either considering case or not
947 // (if compareWithCase then the case matters)
948 bool IsSameAs(const wxChar
*psz
, bool compareWithCase
= true) const
949 { return (compareWithCase
? Cmp(psz
) : CmpNoCase(psz
)) == 0; }
950 // comparison with a signle character: returns true if equal
951 bool IsSameAs(wxChar c
, bool compareWithCase
= true) const
953 return (length() == 1) && (compareWithCase
? GetChar(0u) == c
954 : wxToupper(GetChar(0u)) == wxToupper(c
));
957 // simple sub-string extraction
958 // return substring starting at nFirst of length nCount (or till the end
959 // if nCount = default value)
960 wxString
Mid(size_t nFirst
, size_t nCount
= npos
) const;
962 // operator version of Mid()
963 wxString
operator()(size_t start
, size_t len
) const
964 { return Mid(start
, len
); }
966 // check that the string starts with prefix and return the rest of the
967 // string in the provided pointer if it is not NULL, otherwise return
969 bool StartsWith(const wxChar
*prefix
, wxString
*rest
= NULL
) const;
971 // get first nCount characters
972 wxString
Left(size_t nCount
) const;
973 // get last nCount characters
974 wxString
Right(size_t nCount
) const;
975 // get all characters before the first occurance of ch
976 // (returns the whole string if ch not found)
977 wxString
BeforeFirst(wxChar ch
) const;
978 // get all characters before the last occurence of ch
979 // (returns empty string if ch not found)
980 wxString
BeforeLast(wxChar ch
) const;
981 // get all characters after the first occurence of ch
982 // (returns empty string if ch not found)
983 wxString
AfterFirst(wxChar ch
) const;
984 // get all characters after the last occurence of ch
985 // (returns the whole string if ch not found)
986 wxString
AfterLast(wxChar ch
) const;
988 // for compatibility only, use more explicitly named functions above
989 wxString
Before(wxChar ch
) const { return BeforeLast(ch
); }
990 wxString
After(wxChar ch
) const { return AfterFirst(ch
); }
993 // convert to upper case in place, return the string itself
994 wxString
& MakeUpper();
995 // convert to upper case, return the copy of the string
996 // Here's something to remember: BC++ doesn't like returns in inlines.
997 wxString
Upper() const ;
998 // convert to lower case in place, return the string itself
999 wxString
& MakeLower();
1000 // convert to lower case, return the copy of the string
1001 wxString
Lower() const ;
1003 // trimming/padding whitespace (either side) and truncating
1004 // remove spaces from left or from right (default) side
1005 wxString
& Trim(bool bFromRight
= true);
1006 // add nCount copies chPad in the beginning or at the end (default)
1007 wxString
& Pad(size_t nCount
, wxChar chPad
= wxT(' '), bool bFromRight
= true);
1009 // searching and replacing
1010 // searching (return starting index, or -1 if not found)
1011 int Find(wxChar ch
, bool bFromEnd
= false) const; // like strchr/strrchr
1012 // searching (return starting index, or -1 if not found)
1013 int Find(const wxChar
*pszSub
) const; // like strstr
1014 // replace first (or all of bReplaceAll) occurences of substring with
1015 // another string, returns the number of replacements made
1016 size_t Replace(const wxChar
*szOld
,
1017 const wxChar
*szNew
,
1018 bool bReplaceAll
= true);
1020 // check if the string contents matches a mask containing '*' and '?'
1021 bool Matches(const wxChar
*szMask
) const;
1023 // conversion to numbers: all functions return true only if the whole
1024 // string is a number and put the value of this number into the pointer
1025 // provided, the base is the numeric base in which the conversion should be
1026 // done and must be comprised between 2 and 36 or be 0 in which case the
1027 // standard C rules apply (leading '0' => octal, "0x" => hex)
1028 // convert to a signed integer
1029 bool ToLong(long *val
, int base
= 10) const;
1030 // convert to an unsigned integer
1031 bool ToULong(unsigned long *val
, int base
= 10) const;
1032 // convert to a double
1033 bool ToDouble(double *val
) const;
1035 // formated input/output
1036 // as sprintf(), returns the number of characters written or < 0 on error
1037 // (take 'this' into account in attribute parameter count)
1038 int Printf(const wxChar
*pszFormat
, ...) ATTRIBUTE_PRINTF_2
;
1039 // as vprintf(), returns the number of characters written or < 0 on error
1040 int PrintfV(const wxChar
* pszFormat
, va_list argptr
);
1042 // returns the string containing the result of Printf() to it
1043 static wxString
Format(const wxChar
*pszFormat
, ...) ATTRIBUTE_PRINTF_1
;
1044 // the same as above, but takes a va_list
1045 static wxString
FormatV(const wxChar
*pszFormat
, va_list argptr
);
1047 // raw access to string memory
1048 // ensure that string has space for at least nLen characters
1049 // only works if the data of this string is not shared
1050 bool Alloc(size_t nLen
) { reserve(nLen
); /*return capacity() >= nLen;*/ return true; }
1051 // minimize the string's memory
1052 // only works if the data of this string is not shared
1055 // get writable buffer of at least nLen bytes. Unget() *must* be called
1056 // a.s.a.p. to put string back in a reasonable state!
1057 wxChar
*GetWriteBuf(size_t nLen
);
1058 // call this immediately after GetWriteBuf() has been used
1059 void UngetWriteBuf();
1060 void UngetWriteBuf(size_t nLen
);
1063 // wxWidgets version 1 compatibility functions
1066 wxString
SubString(size_t from
, size_t to
) const
1067 { return Mid(from
, (to
- from
+ 1)); }
1068 // values for second parameter of CompareTo function
1069 enum caseCompare
{exact
, ignoreCase
};
1070 // values for first parameter of Strip function
1071 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
1074 // (take 'this' into account in attribute parameter count)
1075 int sprintf(const wxChar
*pszFormat
, ...) ATTRIBUTE_PRINTF_2
;
1078 inline int CompareTo(const wxChar
* psz
, caseCompare cmp
= exact
) const
1079 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
1082 size_t Length() const { return length(); }
1083 // Count the number of characters
1084 int Freq(wxChar ch
) const;
1086 void LowerCase() { MakeLower(); }
1088 void UpperCase() { MakeUpper(); }
1089 // use Trim except that it doesn't change this string
1090 wxString
Strip(stripType w
= trailing
) const;
1092 // use Find (more general variants not yet supported)
1093 size_t Index(const wxChar
* psz
) const { return Find(psz
); }
1094 size_t Index(wxChar ch
) const { return Find(ch
); }
1096 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
1097 wxString
& RemoveLast(size_t n
= 1) { return Truncate(length() - n
); }
1099 wxString
& Remove(size_t nStart
, size_t nLen
)
1100 { return (wxString
&)erase( nStart
, nLen
); }
1103 int First( const wxChar ch
) const { return Find(ch
); }
1104 int First( const wxChar
* psz
) const { return Find(psz
); }
1105 int First( const wxString
&str
) const { return Find(str
); }
1106 int Last( const wxChar ch
) const { return Find(ch
, true); }
1107 bool Contains(const wxString
& str
) const { return Find(str
) != wxNOT_FOUND
; }
1110 bool IsNull() const { return IsEmpty(); }
1112 // std::string compatibility functions
1114 // take nLen chars starting at nPos
1115 wxString(const wxString
& str
, size_t nPos
, size_t nLen
)
1116 : wxStringBase(str
, nPos
, nLen
) { }
1117 // take all characters from pStart to pEnd
1118 wxString(const void *pStart
, const void *pEnd
)
1119 : wxStringBase((const wxChar
*)pStart
, (const wxChar
*)pEnd
) { }
1121 wxString(const_iterator first
, const_iterator last
)
1122 : wxStringBase(first
, last
) { }
1125 // lib.string.modifiers
1126 // append elements str[pos], ..., str[pos+n]
1127 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
1128 { return (wxString
&)wxStringBase::append(str
, pos
, n
); }
1130 wxString
& append(const wxString
& str
)
1131 { return (wxString
&)wxStringBase::append(str
); }
1132 // append first n (or all if n == npos) characters of sz
1133 wxString
& append(const wxChar
*sz
)
1134 { return (wxString
&)wxStringBase::append(sz
); }
1135 wxString
& append(const wxChar
*sz
, size_t n
)
1136 { return (wxString
&)wxStringBase::append(sz
, n
); }
1137 // append n copies of ch
1138 wxString
& append(size_t n
, wxChar ch
)
1139 { return (wxString
&)wxStringBase::append(n
, ch
); }
1140 // append from first to last
1141 wxString
& append(const_iterator first
, const_iterator last
)
1142 { return (wxString
&)wxStringBase::append(first
, last
); }
1144 // same as `this_string = str'
1145 wxString
& assign(const wxString
& str
)
1146 { return (wxString
&)wxStringBase::assign(str
); }
1147 // same as ` = str[pos..pos + n]
1148 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
1149 { return (wxString
&)wxStringBase::assign(str
, pos
, n
); }
1150 // same as `= first n (or all if n == npos) characters of sz'
1151 wxString
& assign(const wxChar
*sz
)
1152 { return (wxString
&)wxStringBase::assign(sz
); }
1153 wxString
& assign(const wxChar
*sz
, size_t n
)
1154 { return (wxString
&)wxStringBase::assign(sz
, n
); }
1155 // same as `= n copies of ch'
1156 wxString
& assign(size_t n
, wxChar ch
)
1157 { return (wxString
&)wxStringBase::assign(n
, ch
); }
1158 // assign from first to last
1159 wxString
& assign(const_iterator first
, const_iterator last
)
1160 { return (wxString
&)wxStringBase::assign(first
, last
); }
1162 // string comparison
1163 #ifndef HAVE_STD_STRING_COMPARE
1164 int compare(const wxStringBase
& str
) const;
1165 // comparison with a substring
1166 int compare(size_t nStart
, size_t nLen
, const wxStringBase
& str
) const;
1167 // comparison of 2 substrings
1168 int compare(size_t nStart
, size_t nLen
,
1169 const wxStringBase
& str
, size_t nStart2
, size_t nLen2
) const;
1170 // just like strcmp()
1171 int compare(const wxChar
* sz
) const;
1172 // substring comparison with first nCount characters of sz
1173 int compare(size_t nStart
, size_t nLen
,
1174 const wxChar
* sz
, size_t nCount
= npos
) const;
1175 #endif // !defined HAVE_STD_STRING_COMPARE
1177 // insert another string
1178 wxString
& insert(size_t nPos
, const wxString
& str
)
1179 { return (wxString
&)wxStringBase::insert(nPos
, str
); }
1180 // insert n chars of str starting at nStart (in str)
1181 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
1182 { return (wxString
&)wxStringBase::insert(nPos
, str
, nStart
, n
); }
1183 // insert first n (or all if n == npos) characters of sz
1184 wxString
& insert(size_t nPos
, const wxChar
*sz
)
1185 { return (wxString
&)wxStringBase::insert(nPos
, sz
); }
1186 wxString
& insert(size_t nPos
, const wxChar
*sz
, size_t n
)
1187 { return (wxString
&)wxStringBase::insert(nPos
, sz
, n
); }
1188 // insert n copies of ch
1189 wxString
& insert(size_t nPos
, size_t n
, wxChar ch
)
1190 { return (wxString
&)wxStringBase::insert(nPos
, n
, ch
); }
1191 iterator
insert(iterator it
, wxChar ch
)
1192 { return wxStringBase::insert(it
, ch
); }
1193 void insert(iterator it
, const_iterator first
, const_iterator last
)
1194 { wxStringBase::insert(it
, first
, last
); }
1195 void insert(iterator it
, size_type n
, wxChar ch
)
1196 { wxStringBase::insert(it
, n
, ch
); }
1198 // delete characters from nStart to nStart + nLen
1199 wxString
& erase(size_type pos
= 0, size_type n
= npos
)
1200 { return (wxString
&)wxStringBase::erase(pos
, n
); }
1201 iterator
erase(iterator first
, iterator last
)
1202 { return wxStringBase::erase(first
, last
); }
1203 iterator
erase(iterator first
)
1204 { return wxStringBase::erase(first
); }
1206 #ifdef wxSTRING_BASE_HASNT_CLEAR
1207 void clear() { erase(); }
1210 // replaces the substring of length nLen starting at nStart
1211 wxString
& replace(size_t nStart
, size_t nLen
, const wxChar
* sz
)
1212 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, sz
); }
1213 // replaces the substring of length nLen starting at nStart
1214 wxString
& replace(size_t nStart
, size_t nLen
, const wxString
& str
)
1215 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, str
); }
1216 // replaces the substring with nCount copies of ch
1217 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxChar ch
)
1218 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, nCount
, ch
); }
1219 // replaces a substring with another substring
1220 wxString
& replace(size_t nStart
, size_t nLen
,
1221 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1222 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, str
,
1224 // replaces the substring with first nCount chars of sz
1225 wxString
& replace(size_t nStart
, size_t nLen
,
1226 const wxChar
* sz
, size_t nCount
)
1227 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, sz
, nCount
); }
1228 wxString
& replace(iterator first
, iterator last
, const_pointer s
)
1229 { return (wxString
&)wxStringBase::replace(first
, last
, s
); }
1230 wxString
& replace(iterator first
, iterator last
, const_pointer s
,
1232 { return (wxString
&)wxStringBase::replace(first
, last
, s
, n
); }
1233 wxString
& replace(iterator first
, iterator last
, const wxString
& s
)
1234 { return (wxString
&)wxStringBase::replace(first
, last
, s
); }
1235 wxString
& replace(iterator first
, iterator last
, size_type n
, wxChar c
)
1236 { return (wxString
&)wxStringBase::replace(first
, last
, n
, c
); }
1237 wxString
& replace(iterator first
, iterator last
,
1238 const_iterator first1
, const_iterator last1
)
1239 { return (wxString
&)wxStringBase::replace(first
, last
, first1
, last1
); }
1242 wxString
& operator+=(const wxString
& s
)
1243 { return (wxString
&)wxStringBase::operator+=(s
); }
1244 // string += C string
1245 wxString
& operator+=(const wxChar
*psz
)
1246 { return (wxString
&)wxStringBase::operator+=(psz
); }
1248 wxString
& operator+=(wxChar ch
)
1249 { return (wxString
&)wxStringBase::operator+=(ch
); }
1252 // define wxArrayString, for compatibility
1253 #if WXWIN_COMPATIBILITY_2_4 && !wxUSE_STL
1254 #include "wx/arrstr.h"
1258 // return an empty wxString (not very useful with wxUSE_STL == 1)
1259 inline const wxString
wxGetEmptyString() { return wxString(); }
1261 // return an empty wxString (more efficient than wxString() here)
1262 inline const wxString
& wxGetEmptyString()
1264 return *(wxString
*)&wxEmptyString
;
1266 #endif // wxUSE_STL/!wxUSE_STL
1268 // ----------------------------------------------------------------------------
1269 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
1270 // ----------------------------------------------------------------------------
1274 class WXDLLIMPEXP_BASE wxStringBuffer
1277 wxStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
1278 : m_str(str
), m_buf(lenWanted
)
1281 ~wxStringBuffer() { m_str
.assign(m_buf
.data(), wxStrlen(m_buf
.data())); }
1283 operator wxChar
*() { return m_buf
.data(); }
1288 wxWCharBuffer m_buf
;
1293 DECLARE_NO_COPY_CLASS(wxStringBuffer
)
1296 class WXDLLIMPEXP_BASE wxStringBufferLength
1299 wxStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
1300 : m_str(str
), m_buf(lenWanted
), m_len(0), m_lenSet(false)
1303 ~wxStringBufferLength()
1306 m_str
.assign(m_buf
.data(), m_len
);
1309 operator wxChar
*() { return m_buf
.data(); }
1310 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
1315 wxWCharBuffer m_buf
;
1322 DECLARE_NO_COPY_CLASS(wxStringBufferLength
)
1325 #else // if !wxUSE_STL
1327 class WXDLLIMPEXP_BASE wxStringBuffer
1330 wxStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
1331 : m_str(str
), m_buf(NULL
)
1332 { m_buf
= m_str
.GetWriteBuf(lenWanted
); }
1334 ~wxStringBuffer() { m_str
.UngetWriteBuf(); }
1336 operator wxChar
*() const { return m_buf
; }
1342 DECLARE_NO_COPY_CLASS(wxStringBuffer
)
1345 class WXDLLIMPEXP_BASE wxStringBufferLength
1348 wxStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
1349 : m_str(str
), m_buf(NULL
), m_len(0), m_lenSet(false)
1351 m_buf
= m_str
.GetWriteBuf(lenWanted
);
1352 wxASSERT(m_buf
!= NULL
);
1355 ~wxStringBufferLength()
1358 m_str
.UngetWriteBuf(m_len
);
1361 operator wxChar
*() const { return m_buf
; }
1362 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
1370 DECLARE_NO_COPY_CLASS(wxStringBufferLength
)
1373 #endif // !wxUSE_STL
1375 // ---------------------------------------------------------------------------
1376 // wxString comparison functions: operator versions are always case sensitive
1377 // ---------------------------------------------------------------------------
1379 // note that when wxUSE_STL == 1 the comparison operators taking std::string
1380 // are used and defining them also for wxString would only result in
1381 // compilation ambiguities when comparing std::string and wxString
1384 inline bool operator==(const wxString
& s1
, const wxString
& s2
)
1385 { return (s1
.Len() == s2
.Len()) && (s1
.Cmp(s2
) == 0); }
1386 inline bool operator==(const wxString
& s1
, const wxChar
* s2
)
1387 { return s1
.Cmp(s2
) == 0; }
1388 inline bool operator==(const wxChar
* s1
, const wxString
& s2
)
1389 { return s2
.Cmp(s1
) == 0; }
1390 inline bool operator!=(const wxString
& s1
, const wxString
& s2
)
1391 { return (s1
.Len() != s2
.Len()) || (s1
.Cmp(s2
) != 0); }
1392 inline bool operator!=(const wxString
& s1
, const wxChar
* s2
)
1393 { return s1
.Cmp(s2
) != 0; }
1394 inline bool operator!=(const wxChar
* s1
, const wxString
& s2
)
1395 { return s2
.Cmp(s1
) != 0; }
1396 inline bool operator< (const wxString
& s1
, const wxString
& s2
)
1397 { return s1
.Cmp(s2
) < 0; }
1398 inline bool operator< (const wxString
& s1
, const wxChar
* s2
)
1399 { return s1
.Cmp(s2
) < 0; }
1400 inline bool operator< (const wxChar
* s1
, const wxString
& s2
)
1401 { return s2
.Cmp(s1
) > 0; }
1402 inline bool operator> (const wxString
& s1
, const wxString
& s2
)
1403 { return s1
.Cmp(s2
) > 0; }
1404 inline bool operator> (const wxString
& s1
, const wxChar
* s2
)
1405 { return s1
.Cmp(s2
) > 0; }
1406 inline bool operator> (const wxChar
* s1
, const wxString
& s2
)
1407 { return s2
.Cmp(s1
) < 0; }
1408 inline bool operator<=(const wxString
& s1
, const wxString
& s2
)
1409 { return s1
.Cmp(s2
) <= 0; }
1410 inline bool operator<=(const wxString
& s1
, const wxChar
* s2
)
1411 { return s1
.Cmp(s2
) <= 0; }
1412 inline bool operator<=(const wxChar
* s1
, const wxString
& s2
)
1413 { return s2
.Cmp(s1
) >= 0; }
1414 inline bool operator>=(const wxString
& s1
, const wxString
& s2
)
1415 { return s1
.Cmp(s2
) >= 0; }
1416 inline bool operator>=(const wxString
& s1
, const wxChar
* s2
)
1417 { return s1
.Cmp(s2
) >= 0; }
1418 inline bool operator>=(const wxChar
* s1
, const wxString
& s2
)
1419 { return s2
.Cmp(s1
) <= 0; }
1422 inline bool operator==(const wxString
& s1
, const wxWCharBuffer
& s2
)
1423 { return (s1
.Cmp((const wchar_t *)s2
) == 0); }
1424 inline bool operator==(const wxWCharBuffer
& s1
, const wxString
& s2
)
1425 { return (s2
.Cmp((const wchar_t *)s1
) == 0); }
1426 inline bool operator!=(const wxString
& s1
, const wxWCharBuffer
& s2
)
1427 { return (s1
.Cmp((const wchar_t *)s2
) != 0); }
1428 inline bool operator!=(const wxWCharBuffer
& s1
, const wxString
& s2
)
1429 { return (s2
.Cmp((const wchar_t *)s1
) != 0); }
1430 #else // !wxUSE_UNICODE
1431 inline bool operator==(const wxString
& s1
, const wxCharBuffer
& s2
)
1432 { return (s1
.Cmp((const char *)s2
) == 0); }
1433 inline bool operator==(const wxCharBuffer
& s1
, const wxString
& s2
)
1434 { return (s2
.Cmp((const char *)s1
) == 0); }
1435 inline bool operator!=(const wxString
& s1
, const wxCharBuffer
& s2
)
1436 { return (s1
.Cmp((const char *)s2
) != 0); }
1437 inline bool operator!=(const wxCharBuffer
& s1
, const wxString
& s2
)
1438 { return (s2
.Cmp((const char *)s1
) != 0); }
1439 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1441 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
, const wxString
& string2
);
1442 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxChar ch
);
1443 wxString WXDLLIMPEXP_BASE
operator+(wxChar ch
, const wxString
& string
);
1444 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const wxChar
*psz
);
1445 wxString WXDLLIMPEXP_BASE
operator+(const wxChar
*psz
, const wxString
& string
);
1448 inline wxString
operator+(const wxString
& string
, const wxWCharBuffer
& buf
)
1449 { return string
+ (const wchar_t *)buf
; }
1450 inline wxString
operator+(const wxWCharBuffer
& buf
, const wxString
& string
)
1451 { return (const wchar_t *)buf
+ string
; }
1452 #else // !wxUSE_UNICODE
1453 inline wxString
operator+(const wxString
& string
, const wxCharBuffer
& buf
)
1454 { return string
+ (const char *)buf
; }
1455 inline wxString
operator+(const wxCharBuffer
& buf
, const wxString
& string
)
1456 { return (const char *)buf
+ string
; }
1457 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1459 #endif // !wxUSE_STL
1461 // comparison with char (those are not defined by std::[w]string and so should
1462 // be always available)
1463 inline bool operator==(wxChar c
, const wxString
& s
) { return s
.IsSameAs(c
); }
1464 inline bool operator==(const wxString
& s
, wxChar c
) { return s
.IsSameAs(c
); }
1465 inline bool operator!=(wxChar c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
1466 inline bool operator!=(const wxString
& s
, wxChar c
) { return !s
.IsSameAs(c
); }
1468 // ---------------------------------------------------------------------------
1469 // Implementation only from here until the end of file
1470 // ---------------------------------------------------------------------------
1472 // don't pollute the library user's name space
1473 #undef wxASSERT_VALID_INDEX
1475 #if wxUSE_STD_IOSTREAM
1477 #include "wx/iosfwrap.h"
1479 WXDLLIMPEXP_BASE wxSTD istream
& operator>>(wxSTD istream
&, wxString
&);
1480 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxString
&);
1482 #endif // wxSTD_STRING_COMPATIBILITY
1484 #endif // _WX_WXSTRINGH__