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 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
84 // must define this static for VA or else you get multiply defined symbols everywhere
85 extern const unsigned int wxSTRING_MAXLEN
;
88 // maximum possible length for a string means "take all string" everywhere
89 // (as sizeof(StringData) is unknown here, we subtract 100)
90 const unsigned int wxSTRING_MAXLEN
= UINT_MAX
- 100;
94 // ----------------------------------------------------------------------------
96 // ----------------------------------------------------------------------------
98 // global pointer to empty string
99 extern WXDLLIMPEXP_DATA_BASE(const wxChar
*) wxEmptyString
;
101 // ---------------------------------------------------------------------------
102 // global functions complementing standard C string library replacements for
103 // strlen() and portable strcasecmp()
104 //---------------------------------------------------------------------------
106 // Use wxXXX() functions from wxchar.h instead! These functions are for
107 // backwards compatibility only.
109 // checks whether the passed in pointer is NULL and if the string is empty
110 inline bool IsEmpty(const char *p
) { return (!p
|| !*p
); }
112 // safe version of strlen() (returns 0 if passed NULL pointer)
113 inline size_t Strlen(const char *psz
)
114 { return psz
? strlen(psz
) : 0; }
116 // portable strcasecmp/_stricmp
117 inline int Stricmp(const char *psz1
, const char *psz2
)
119 #if defined(__VISUALC__) && defined(__WXWINCE__)
120 register char c1
, c2
;
122 c1
= tolower(*psz1
++);
123 c2
= tolower(*psz2
++);
124 } while ( c1
&& (c1
== c2
) );
127 #elif defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) )
128 return _stricmp(psz1
, psz2
);
129 #elif defined(__SC__)
130 return _stricmp(psz1
, psz2
);
131 #elif defined(__SALFORDC__)
132 return stricmp(psz1
, psz2
);
133 #elif defined(__BORLANDC__)
134 return stricmp(psz1
, psz2
);
135 #elif defined(__WATCOMC__)
136 return stricmp(psz1
, psz2
);
137 #elif defined(__DJGPP__)
138 return stricmp(psz1
, psz2
);
139 #elif defined(__EMX__)
140 return stricmp(psz1
, psz2
);
141 #elif defined(__WXPM__)
142 return stricmp(psz1
, psz2
);
143 #elif defined(HAVE_STRCASECMP_IN_STRING_H) || \
144 defined(HAVE_STRCASECMP_IN_STRINGS_H) || \
145 defined(__GNUWIN32__)
146 return strcasecmp(psz1
, psz2
);
147 #elif defined(__MWERKS__) && !defined(__INTEL__)
148 register char c1
, c2
;
150 c1
= tolower(*psz1
++);
151 c2
= tolower(*psz2
++);
152 } while ( c1
&& (c1
== c2
) );
156 // almost all compilers/libraries provide this function (unfortunately under
157 // different names), that's why we don't implement our own which will surely
158 // be more efficient than this code (uncomment to use):
160 register char c1, c2;
162 c1 = tolower(*psz1++);
163 c2 = tolower(*psz2++);
164 } while ( c1 && (c1 == c2) );
169 #error "Please define string case-insensitive compare for your OS/compiler"
170 #endif // OS/compiler
175 #include "wx/beforestd.h"
177 #include "wx/afterstd.h"
180 #ifdef HAVE_STD_WSTRING
181 typedef std::wstring wxStringBase
;
183 typedef std::basic_string
<wxChar
> wxStringBase
;
186 typedef std::string wxStringBase
;
189 #if (defined(__GNUG__) && (__GNUG__ < 3)) || \
190 (defined(_MSC_VER) && (_MSC_VER <= 1200))
191 #define wxSTRING_BASE_HASNT_CLEAR
194 #else // if !wxUSE_STL
196 #ifndef HAVE_STD_STRING_COMPARE
197 #define HAVE_STD_STRING_COMPARE
200 // ---------------------------------------------------------------------------
201 // string data prepended with some housekeeping info (used by wxString class),
202 // is never used directly (but had to be put here to allow inlining)
203 // ---------------------------------------------------------------------------
205 struct WXDLLIMPEXP_BASE wxStringData
207 int nRefs
; // reference count
208 size_t nDataLength
, // actual string length
209 nAllocLength
; // allocated memory size
211 // mimics declaration 'wxChar data[nAllocLength]'
212 wxChar
* data() const { return (wxChar
*)(this + 1); }
214 // empty string has a special ref count so it's never deleted
215 bool IsEmpty() const { return (nRefs
== -1); }
216 bool IsShared() const { return (nRefs
> 1); }
219 void Lock() { if ( !IsEmpty() ) nRefs
++; }
221 // VC++ will refuse to inline Unlock but profiling shows that it is wrong
222 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
225 // VC++ free must take place in same DLL as allocation when using non dll
226 // run-time library (e.g. Multithreaded instead of Multithreaded DLL)
227 #if defined(__VISUALC__) && defined(_MT) && !defined(_DLL)
228 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) Free(); }
229 // we must not inline deallocation since allocation is not inlined
232 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) free(this); }
235 // if we had taken control over string memory (GetWriteBuf), it's
236 // intentionally put in invalid state
237 void Validate(bool b
) { nRefs
= (b
? 1 : 0); }
238 bool IsValid() const { return (nRefs
!= 0); }
241 class WXDLLIMPEXP_BASE wxStringBase
244 friend class WXDLLIMPEXP_BASE wxArrayString
;
247 // an 'invalid' value for string index, moved to this place due to a CW bug
248 static const size_t npos
;
250 // points to data preceded by wxStringData structure with ref count info
253 // accessor to string data
254 wxStringData
* GetStringData() const { return (wxStringData
*)m_pchData
- 1; }
256 // string (re)initialization functions
257 // initializes the string to the empty value (must be called only from
258 // ctors, use Reinit() otherwise)
259 void Init() { m_pchData
= (wxChar
*)wxEmptyString
; }
260 // initializaes the string with (a part of) C-string
261 void InitWith(const wxChar
*psz
, size_t nPos
= 0, size_t nLen
= npos
);
262 // as Init, but also frees old data
263 void Reinit() { GetStringData()->Unlock(); Init(); }
266 // allocates memory for string of length nLen
267 bool AllocBuffer(size_t nLen
);
268 // copies data to another string
269 bool AllocCopy(wxString
&, int, int) const;
270 // effectively copies data to string
271 bool AssignCopy(size_t, const wxChar
*);
273 // append a (sub)string
274 bool ConcatSelf(size_t nLen
, const wxChar
*src
, size_t nMaxLen
);
275 bool ConcatSelf(size_t nLen
, const wxChar
*src
)
276 { return ConcatSelf(nLen
, src
, nLen
); }
278 // functions called before writing to the string: they copy it if there
279 // are other references to our data (should be the only owner when writing)
280 bool CopyBeforeWrite();
281 bool AllocBeforeWrite(size_t);
283 // compatibility with wxString
284 bool Alloc(size_t nLen
);
287 typedef wxChar value_type
;
288 typedef wxChar char_type
;
289 typedef size_t size_type
;
290 typedef value_type
& reference
;
291 typedef const value_type
& const_reference
;
292 typedef value_type
* pointer
;
293 typedef const value_type
* const_pointer
;
294 typedef value_type
*iterator
;
295 typedef const value_type
*const_iterator
;
297 // constructors and destructor
298 // ctor for an empty string
299 wxStringBase() { Init(); }
301 wxStringBase(const wxStringBase
& stringSrc
)
303 wxASSERT_MSG( stringSrc
.GetStringData()->IsValid(),
304 _T("did you forget to call UngetWriteBuf()?") );
306 if ( stringSrc
.empty() ) {
307 // nothing to do for an empty string
311 m_pchData
= stringSrc
.m_pchData
; // share same data
312 GetStringData()->Lock(); // => one more copy
315 // string containing nRepeat copies of ch
316 wxStringBase(size_type nRepeat
, wxChar ch
);
317 // ctor takes first nLength characters from C string
318 // (default value of npos means take all the string)
319 wxStringBase(const wxChar
*psz
)
320 { InitWith(psz
, 0, npos
); }
321 wxStringBase(const wxChar
*psz
, size_t nLength
)
322 { InitWith(psz
, 0, nLength
); }
323 wxStringBase(const wxChar
*psz
, wxMBConv
& WXUNUSED(conv
), size_t nLength
= npos
)
324 { InitWith(psz
, 0, nLength
); }
325 // take nLen chars starting at nPos
326 wxStringBase(const wxStringBase
& str
, size_t nPos
, size_t nLen
)
328 wxASSERT_MSG( str
.GetStringData()->IsValid(),
329 _T("did you forget to call UngetWriteBuf()?") );
331 size_t strLen
= str
.length() - nPos
; nLen
= strLen
< nLen
? strLen
: nLen
;
332 InitWith(str
.c_str(), nPos
, nLen
);
334 // take all characters from pStart to pEnd
335 wxStringBase(const void *pStart
, const void *pEnd
);
337 // dtor is not virtual, this class must not be inherited from!
340 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
341 //RN - according to the above VC++ does indeed inline this,
342 //even though it spits out two warnings
343 #pragma warning (disable:4714)
346 GetStringData()->Unlock();
349 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
350 //re-enable inlining warning
351 #pragma warning (default:4714)
353 // overloaded assignment
354 // from another wxString
355 wxStringBase
& operator=(const wxStringBase
& stringSrc
);
357 wxStringBase
& operator=(wxChar ch
);
359 wxStringBase
& operator=(const wxChar
*psz
);
361 // return the length of the string
362 size_type
size() const { return GetStringData()->nDataLength
; }
363 // return the length of the string
364 size_type
length() const { return size(); }
365 // return the maximum size of the string
366 size_type
max_size() const { return wxSTRING_MAXLEN
; }
367 // resize the string, filling the space with c if c != 0
368 void resize(size_t nSize
, wxChar ch
= wxT('\0'));
369 // delete the contents of the string
370 void clear() { erase(0, npos
); }
371 // returns true if the string is empty
372 bool empty() const { return size() == 0; }
373 // inform string about planned change in size
374 void reserve(size_t sz
) { Alloc(sz
); }
375 size_type
capacity() const { return GetStringData()->nAllocLength
; }
378 // return the character at position n
379 value_type
at(size_type n
) const
380 { wxASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
381 value_type
operator[](size_type n
) const { return at(n
); }
382 // returns the writable character at position n
383 reference
at(size_type n
)
384 { wxASSERT_VALID_INDEX( n
); CopyBeforeWrite(); return m_pchData
[n
]; }
385 reference
operator[](size_type n
)
386 { wxASSERT_VALID_INDEX( n
); CopyBeforeWrite(); return m_pchData
[n
]; }
388 // lib.string.modifiers
389 // append elements str[pos], ..., str[pos+n]
390 wxStringBase
& append(const wxStringBase
& str
, size_t pos
, size_t n
)
392 wxASSERT(pos
<= str
.length());
393 ConcatSelf(n
, str
.c_str() + pos
, str
.length() - pos
);
397 wxStringBase
& append(const wxStringBase
& str
)
398 { ConcatSelf(str
.length(), str
.c_str()); return *this; }
399 // append first n (or all if n == npos) characters of sz
400 wxStringBase
& append(const wxChar
*sz
)
401 { ConcatSelf(wxStrlen(sz
), sz
); return *this; }
402 wxStringBase
& append(const wxChar
*sz
, size_t n
)
403 { ConcatSelf(n
, sz
); return *this; }
404 // append n copies of ch
405 wxStringBase
& append(size_t n
, wxChar ch
);
406 // append from first to last
407 wxStringBase
& append(const_iterator first
, const_iterator last
)
408 { ConcatSelf(last
- first
, first
); return *this; }
410 // same as `this_string = str'
411 wxStringBase
& assign(const wxStringBase
& str
)
412 { return *this = str
; }
413 // same as ` = str[pos..pos + n]
414 wxStringBase
& assign(const wxStringBase
& str
, size_t pos
, size_t n
)
415 { clear(); return append(str
, pos
, n
); }
416 // same as `= first n (or all if n == npos) characters of sz'
417 wxStringBase
& assign(const wxChar
*sz
)
418 { clear(); return append(sz
, wxStrlen(sz
)); }
419 wxStringBase
& assign(const wxChar
*sz
, size_t n
)
420 { clear(); return append(sz
, n
); }
421 // same as `= n copies of ch'
422 wxStringBase
& assign(size_t n
, wxChar ch
)
423 { clear(); return append(n
, ch
); }
424 // assign from first to last
425 wxStringBase
& assign(const_iterator first
, const_iterator last
)
426 { clear(); return append(first
, last
); }
428 // first valid index position
429 const_iterator
begin() const { return m_pchData
; }
430 // position one after the last valid one
431 const_iterator
end() const { return m_pchData
+ length(); }
433 // first valid index position
435 // position one after the last valid one
438 // insert another string
439 wxStringBase
& insert(size_t nPos
, const wxStringBase
& str
)
441 wxASSERT( str
.GetStringData()->IsValid() );
442 return insert(nPos
, str
.c_str(), str
.length());
444 // insert n chars of str starting at nStart (in str)
445 wxStringBase
& insert(size_t nPos
, const wxStringBase
& str
, size_t nStart
, size_t n
)
447 wxASSERT( str
.GetStringData()->IsValid() );
448 wxASSERT( nStart
< str
.length() );
449 size_t strLen
= str
.length() - nStart
;
450 n
= strLen
< n
? strLen
: n
;
451 return insert(nPos
, str
.c_str() + nStart
, n
);
453 // insert first n (or all if n == npos) characters of sz
454 wxStringBase
& insert(size_t nPos
, const wxChar
*sz
, size_t n
= npos
);
455 // insert n copies of ch
456 wxStringBase
& insert(size_t nPos
, size_t n
, wxChar ch
)
457 { return insert(nPos
, wxStringBase(n
, ch
)); }
458 iterator
insert(iterator it
, wxChar ch
)
459 { size_t idx
= it
- begin(); insert(idx
, 1, ch
); return begin() + idx
; }
460 void insert(iterator it
, const_iterator first
, const_iterator last
)
461 { insert(it
- begin(), first
, last
- first
); }
462 void insert(iterator it
, size_type n
, wxChar ch
)
463 { insert(it
- begin(), n
, ch
); }
465 // delete characters from nStart to nStart + nLen
466 wxStringBase
& erase(size_type pos
= 0, size_type n
= npos
);
467 iterator
erase(iterator first
, iterator last
)
469 size_t idx
= first
- begin();
470 erase(idx
, last
- first
);
471 return begin() + idx
;
473 iterator
erase(iterator first
);
475 // explicit conversion to C string (use this with printf()!)
476 const wxChar
* c_str() const { return m_pchData
; }
477 const wxChar
* data() const { return m_pchData
; }
479 // replaces the substring of length nLen starting at nStart
480 wxStringBase
& replace(size_t nStart
, size_t nLen
, const wxChar
* sz
);
481 // replaces the substring of length nLen starting at nStart
482 wxStringBase
& replace(size_t nStart
, size_t nLen
, const wxStringBase
& str
)
483 { return replace(nStart
, nLen
, str
.c_str()); }
484 // replaces the substring with nCount copies of ch
485 wxStringBase
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxChar ch
);
486 // replaces a substring with another substring
487 wxStringBase
& replace(size_t nStart
, size_t nLen
,
488 const wxStringBase
& str
, size_t nStart2
, size_t nLen2
);
489 // replaces the substring with first nCount chars of sz
490 wxStringBase
& replace(size_t nStart
, size_t nLen
,
491 const wxChar
* sz
, size_t nCount
);
492 wxStringBase
& replace(iterator first
, iterator last
, const_pointer s
)
493 { return replace(first
- begin(), last
- first
, s
); }
494 wxStringBase
& replace(iterator first
, iterator last
, const_pointer s
,
496 { return replace(first
- begin(), last
- first
, s
, n
); }
497 wxStringBase
& replace(iterator first
, iterator last
, const wxStringBase
& s
)
498 { return replace(first
- begin(), last
- first
, s
); }
499 wxStringBase
& replace(iterator first
, iterator last
, size_type n
, wxChar c
)
500 { return replace(first
- begin(), last
- first
, n
, c
); }
501 wxStringBase
& replace(iterator first
, iterator last
,
502 const_iterator first1
, const_iterator last1
)
503 { return replace(first
- begin(), last
- first
, first1
, last1
- first1
); }
506 void swap(wxStringBase
& str
);
508 // All find() functions take the nStart argument which specifies the
509 // position to start the search on, the default value is 0. All functions
510 // return npos if there were no match.
513 size_t find(const wxStringBase
& str
, size_t nStart
= 0) const;
515 // VC++ 1.5 can't cope with this syntax.
516 #if !defined(__VISUALC__) || defined(__WIN32__)
517 // find first n characters of sz
518 size_t find(const wxChar
* sz
, size_t nStart
= 0, size_t n
= npos
) const;
521 // find the first occurence of character ch after nStart
522 size_t find(wxChar ch
, size_t nStart
= 0) const;
524 // rfind() family is exactly like find() but works right to left
526 // as find, but from the end
527 size_t rfind(const wxStringBase
& str
, size_t nStart
= npos
) const;
529 // VC++ 1.5 can't cope with this syntax.
530 // as find, but from the end
531 size_t rfind(const wxChar
* sz
, size_t nStart
= npos
,
532 size_t n
= npos
) const;
533 // as find, but from the end
534 size_t rfind(wxChar ch
, size_t nStart
= npos
) const;
536 // find first/last occurence of any character in the set
538 // as strpbrk() but starts at nStart, returns npos if not found
539 size_t find_first_of(const wxStringBase
& str
, size_t nStart
= 0) const
540 { return find_first_of(str
.c_str(), nStart
); }
542 size_t find_first_of(const wxChar
* sz
, size_t nStart
= 0) const;
543 size_t find_first_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
544 // same as find(char, size_t)
545 size_t find_first_of(wxChar c
, size_t nStart
= 0) const
546 { return find(c
, nStart
); }
547 // find the last (starting from nStart) char from str in this string
548 size_t find_last_of (const wxStringBase
& str
, size_t nStart
= npos
) const
549 { return find_last_of(str
.c_str(), nStart
); }
551 size_t find_last_of (const wxChar
* sz
, size_t nStart
= npos
) const;
552 size_t find_last_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
554 size_t find_last_of(wxChar c
, size_t nStart
= npos
) const
555 { return rfind(c
, nStart
); }
557 // find first/last occurence of any character not in the set
559 // as strspn() (starting from nStart), returns npos on failure
560 size_t find_first_not_of(const wxStringBase
& str
, size_t nStart
= 0) const
561 { return find_first_not_of(str
.c_str(), nStart
); }
563 size_t find_first_not_of(const wxChar
* sz
, size_t nStart
= 0) const;
564 size_t find_first_not_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
566 size_t find_first_not_of(wxChar ch
, size_t nStart
= 0) const;
568 size_t find_last_not_of(const wxStringBase
& str
, size_t nStart
= npos
) const
569 { return find_last_not_of(str
.c_str(), nStart
); }
571 size_t find_last_not_of(const wxChar
* sz
, size_t nStart
= npos
) const;
572 size_t find_last_not_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
574 size_t find_last_not_of(wxChar ch
, size_t nStart
= npos
) const;
576 // All compare functions return -1, 0 or 1 if the [sub]string is less,
577 // equal or greater than the compare() argument.
579 // comparison with another string
580 int compare(const wxStringBase
& str
) const;
581 // comparison with a substring
582 int compare(size_t nStart
, size_t nLen
, const wxStringBase
& str
) const;
583 // comparison of 2 substrings
584 int compare(size_t nStart
, size_t nLen
,
585 const wxStringBase
& str
, size_t nStart2
, size_t nLen2
) const;
586 // comparison with a c string
587 int compare(const wxChar
* sz
) const;
588 // substring comparison with first nCount characters of sz
589 int compare(size_t nStart
, size_t nLen
,
590 const wxChar
* sz
, size_t nCount
= npos
) const;
592 size_type
copy(wxChar
* s
, size_type n
, size_type pos
= 0);
594 // substring extraction
595 wxStringBase
substr(size_t nStart
= 0, size_t nLen
= npos
) const;
598 wxStringBase
& operator+=(const wxStringBase
& s
) { return append(s
); }
599 // string += C string
600 wxStringBase
& operator+=(const wxChar
*psz
) { return append(psz
); }
602 wxStringBase
& operator+=(wxChar ch
) { return append(1, ch
); }
607 // ----------------------------------------------------------------------------
608 // wxString: string class trying to be compatible with std::string, MFC
609 // CString and wxWindows 1.x wxString all at once
610 // ---------------------------------------------------------------------------
612 class WXDLLIMPEXP_BASE wxString
: public wxStringBase
615 friend class WXDLLIMPEXP_BASE wxArrayString
;
618 // NB: special care was taken in arranging the member functions in such order
619 // that all inline functions can be effectively inlined, verify that all
620 // performace critical functions are still inlined if you change order!
622 // if we hadn't made these operators private, it would be possible to
623 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
624 // converted to char in C and we do have operator=(char)
626 // NB: we don't need other versions (short/long and unsigned) as attempt
627 // to assign another numeric type to wxString will now result in
628 // ambiguity between operator=(char) and operator=(int)
629 wxString
& operator=(int);
631 // these methods are not implemented - there is _no_ conversion from int to
632 // string, you're doing something wrong if the compiler wants to call it!
634 // try `s << i' or `s.Printf("%d", i)' instead
638 // constructors and destructor
639 // ctor for an empty string
640 wxString() : wxStringBase() { }
642 wxString(const wxStringBase
& stringSrc
) : wxStringBase(stringSrc
) { }
643 wxString(const wxString
& stringSrc
) : wxStringBase(stringSrc
) { }
644 // string containing nRepeat copies of ch
645 wxString(wxChar ch
, size_t nRepeat
= 1)
646 : wxStringBase(nRepeat
, ch
) { }
647 wxString(size_t nRepeat
, wxChar ch
)
648 : wxStringBase(nRepeat
, ch
) { }
649 // ctor takes first nLength characters from C string
650 // (default value of npos means take all the string)
651 wxString(const wxChar
*psz
)
652 : wxStringBase(psz
? psz
: wxT("")) { }
653 wxString(const wxChar
*psz
, size_t nLength
)
654 : wxStringBase(psz
, nLength
) { }
655 wxString(const wxChar
*psz
, wxMBConv
& WXUNUSED(conv
), size_t nLength
= npos
)
656 : wxStringBase(psz
, nLength
== npos
? wxStrlen(psz
) : nLength
) { }
659 // from multibyte string
660 wxString(const char *psz
, wxMBConv
& conv
, size_t nLength
= npos
);
661 // from wxWCharBuffer (i.e. return from wxGetString)
662 wxString(const wxWCharBuffer
& psz
) : wxStringBase(psz
.data()) { }
664 // from C string (for compilers using unsigned char)
665 wxString(const unsigned char* psz
, size_t nLength
= npos
)
666 : wxStringBase((const char*)psz
, nLength
) { }
669 // from wide (Unicode) string
670 wxString(const wchar_t *pwz
, wxMBConv
& conv
= wxConvLibc
, size_t nLength
= npos
);
671 #endif // !wxUSE_WCHAR_T
674 wxString(const wxCharBuffer
& psz
)
675 : wxStringBase(psz
) { }
676 #endif // Unicode/ANSI
678 // generic attributes & operations
679 // as standard strlen()
680 size_t Len() const { return length(); }
681 // string contains any characters?
682 bool IsEmpty() const { return empty(); }
683 // empty string is "false", so !str will return true
684 bool operator!() const { return IsEmpty(); }
685 // truncate the string to given length
686 wxString
& Truncate(size_t uiLen
);
687 // empty string contents
692 wxASSERT_MSG( IsEmpty(), _T("string not empty after call to Empty()?") );
694 // empty the string and free memory
697 wxString
tmp(wxEmptyString
);
703 bool IsAscii() const;
705 bool IsNumber() const;
709 // data access (all indexes are 0 based)
711 wxChar
GetChar(size_t n
) const
712 { return operator[](n
); }
714 wxChar
& GetWritableChar(size_t n
)
715 { return operator[](n
); }
717 void SetChar(size_t n
, wxChar ch
)
718 { operator[](n
) = ch
; }
720 // get last character
723 wxASSERT_MSG( !IsEmpty(), _T("wxString: index out of bounds") );
725 return operator[](length() - 1);
728 // get writable last character
731 wxASSERT_MSG( !IsEmpty(), _T("wxString: index out of bounds") );
732 return operator[](length() - 1);
736 So why do we have all these overloaded operator[]s? A bit of history:
737 initially there was only one of them, taking size_t. Then people
738 started complaining because they wanted to use ints as indices (I
739 wonder why) and compilers were giving warnings about it, so we had to
740 add the operator[](int). Then it became apparent that you couldn't
741 write str[0] any longer because there was ambiguity between two
742 overloads and so you now had to write str[0u] (or, of course, use the
743 explicit casts to either int or size_t but nobody did this).
745 Finally, someone decided to compile wxWin on an Alpha machine and got
746 a surprize: str[0u] didn't compile there because it is of type
747 unsigned int and size_t is unsigned _long_ on Alpha and so there was
748 ambiguity between converting uint to int or ulong. To fix this one we
749 now add operator[](uint) for the machines where size_t is not already
750 the same as unsigned int - hopefully this fixes the problem (for some
753 The only real fix is, of course, to remove all versions but the one
757 // operator version of GetChar
758 wxChar
operator[](int n
) const
759 { return wxStringBase::operator[](n
); }
760 wxChar
& operator[](size_type n
)
761 { return wxStringBase::operator[](n
); }
762 wxChar
operator[](size_type n
) const
763 { return wxStringBase::operator[](n
); }
764 #ifndef wxSIZE_T_IS_UINT
765 // operator version of GetChar
766 wxChar
operator[](unsigned int n
) const
767 { return wxStringBase::operator[](n
); }
769 // operator version of GetWriteableChar
770 wxChar
& operator[](unsigned int n
)
771 { return wxStringBase::operator[](n
); }
772 #endif // size_t != unsigned int
774 // implicit conversion to C string
775 operator const wxChar
*() const { return c_str(); }
777 // identical to c_str(), for wxWin 1.6x compatibility
778 const wxChar
* wx_str() const { return c_str(); }
779 // identical to c_str(), for MFC compatibility
780 const wxChar
* GetData() const { return c_str(); }
782 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
783 // converting numbers or strings which are certain not to contain special
784 // chars (typically system functions, X atoms, environment variables etc.)
786 // the behaviour of these functions with the strings containing anything
787 // else than 7 bit ASCII characters is undefined, use at your own risk.
789 static wxString
FromAscii(const char *ascii
); // string
790 static wxString
FromAscii(const char ascii
); // char
791 const wxCharBuffer
ToAscii() const;
793 static wxString
FromAscii(const char *ascii
) { return wxString( ascii
); }
794 static wxString
FromAscii(const char ascii
) { return wxString( ascii
); }
795 const char *ToAscii() const { return c_str(); }
796 #endif // Unicode/!Unicode
798 // conversions with (possible) format conversions: have to return a
799 // buffer with temporary data
801 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
802 // return an ANSI (multibyte) string, wc_str() to return a wide string and
803 // fn_str() to return a string which should be used with the OS APIs
804 // accepting the file names. The return value is always the same, but the
805 // type differs because a function may either return pointer to the buffer
806 // directly or have to use intermediate buffer for translation.
808 const wxCharBuffer
mb_str(wxMBConv
& conv
= wxConvLibc
) const;
810 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
812 const wxChar
* wc_str() const { return c_str(); }
814 // for compatibility with !wxUSE_UNICODE version
815 const wxChar
* wc_str(wxMBConv
& WXUNUSED(conv
)) const { return c_str(); }
818 const wxCharBuffer
fn_str() const { return mb_str(wxConvFile
); }
820 const wxChar
* fn_str() const { return c_str(); }
821 #endif // wxMBFILES/!wxMBFILES
823 const wxChar
* mb_str() const { return c_str(); }
825 // for compatibility with wxUSE_UNICODE version
826 const wxChar
* mb_str(wxMBConv
& WXUNUSED(conv
)) const { return c_str(); }
828 const wxWX2MBbuf
mbc_str() const { return mb_str(); }
831 const wxWCharBuffer
wc_str(wxMBConv
& conv
) const;
832 #endif // wxUSE_WCHAR_T
834 const wxChar
* fn_str() const { return c_str(); }
835 #endif // Unicode/ANSI
837 // overloaded assignment
838 // from another wxString
839 wxString
& operator=(const wxStringBase
& stringSrc
)
840 { return (wxString
&)wxStringBase::operator=(stringSrc
); }
842 wxString
& operator=(wxChar ch
)
843 { return (wxString
&)wxStringBase::operator=(ch
); }
845 wxString
& operator=(const wxChar
*psz
)
846 { return (wxString
&)wxStringBase::operator=(psz
); }
848 // from wxWCharBuffer
849 wxString
& operator=(const wxWCharBuffer
& psz
)
850 { (void) operator=((const wchar_t *)psz
); return *this; }
852 // from another kind of C string
853 wxString
& operator=(const unsigned char* psz
);
855 // from a wide string
856 wxString
& operator=(const wchar_t *pwz
);
859 wxString
& operator=(const wxCharBuffer
& psz
)
860 { (void) operator=((const char *)psz
); return *this; }
861 #endif // Unicode/ANSI
863 // string concatenation
864 // in place concatenation
866 Concatenate and return the result. Note that the left to right
867 associativity of << allows to write things like "str << str1 << str2
868 << ..." (unlike with +=)
871 wxString
& operator<<(const wxString
& s
)
874 wxASSERT_MSG( s
.GetStringData()->IsValid(),
875 _T("did you forget to call UngetWriteBuf()?") );
881 // string += C string
882 wxString
& operator<<(const wxChar
*psz
)
883 { append(psz
); return *this; }
885 wxString
& operator<<(wxChar ch
) { append(1, ch
); return *this; }
887 // string += buffer (i.e. from wxGetString)
889 wxString
& operator<<(const wxWCharBuffer
& s
)
890 { (void)operator<<((const wchar_t *)s
); return *this; }
891 void operator+=(const wxWCharBuffer
& s
)
892 { (void)operator<<((const wchar_t *)s
); }
893 #else // !wxUSE_UNICODE
894 wxString
& operator<<(const wxCharBuffer
& s
)
895 { (void)operator<<((const char *)s
); return *this; }
896 void operator+=(const wxCharBuffer
& s
)
897 { (void)operator<<((const char *)s
); }
898 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
900 // string += C string
901 wxString
& Append(const wxString
& s
)
903 // test for IsEmpty() to share the string if possible
910 wxString
& Append(const wxChar
* psz
)
911 { append(psz
); return *this; }
912 // append count copies of given character
913 wxString
& Append(wxChar ch
, size_t count
= 1u)
914 { append(count
, ch
); return *this; }
915 wxString
& Append(const wxChar
* psz
, size_t nLen
)
916 { append(psz
, nLen
); return *this; }
918 // prepend a string, return the string itself
919 wxString
& Prepend(const wxString
& str
)
920 { *this = str
+ *this; return *this; }
922 // non-destructive concatenation
924 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
, const wxString
& string2
);
926 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxChar ch
);
928 friend wxString WXDLLIMPEXP_BASE
operator+(wxChar ch
, const wxString
& string
);
930 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const wxChar
*psz
);
932 friend wxString WXDLLIMPEXP_BASE
operator+(const wxChar
*psz
, const wxString
& string
);
934 // stream-like functions
935 // insert an int into string
936 wxString
& operator<<(int i
)
937 { return (*this) << Format(_T("%d"), i
); }
938 // insert an unsigned int into string
939 wxString
& operator<<(unsigned int ui
)
940 { return (*this) << Format(_T("%u"), ui
); }
941 // insert a long into string
942 wxString
& operator<<(long l
)
943 { return (*this) << Format(_T("%ld"), l
); }
944 // insert an unsigned long into string
945 wxString
& operator<<(unsigned long ul
)
946 { return (*this) << Format(_T("%lu"), ul
); }
947 // insert a float into string
948 wxString
& operator<<(float f
)
949 { return (*this) << Format(_T("%f"), f
); }
950 // insert a double into string
951 wxString
& operator<<(double d
)
952 { return (*this) << Format(_T("%g"), d
); }
955 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
956 int Cmp(const wxChar
*psz
) const;
957 int Cmp(const wxString
& s
) const;
958 // same as Cmp() but not case-sensitive
959 int CmpNoCase(const wxChar
*psz
) const;
960 int CmpNoCase(const wxString
& s
) const;
961 // test for the string equality, either considering case or not
962 // (if compareWithCase then the case matters)
963 bool IsSameAs(const wxChar
*psz
, bool compareWithCase
= true) const
964 { return (compareWithCase
? Cmp(psz
) : CmpNoCase(psz
)) == 0; }
965 // comparison with a signle character: returns true if equal
966 bool IsSameAs(wxChar c
, bool compareWithCase
= true) const
968 return (length() == 1) && (compareWithCase
? GetChar(0u) == c
969 : wxToupper(GetChar(0u)) == wxToupper(c
));
972 // simple sub-string extraction
973 // return substring starting at nFirst of length nCount (or till the end
974 // if nCount = default value)
975 wxString
Mid(size_t nFirst
, size_t nCount
= npos
) const;
977 // operator version of Mid()
978 wxString
operator()(size_t start
, size_t len
) const
979 { return Mid(start
, len
); }
981 // check that the string starts with prefix and return the rest of the
982 // string in the provided pointer if it is not NULL, otherwise return
984 bool StartsWith(const wxChar
*prefix
, wxString
*rest
= NULL
) const;
986 // get first nCount characters
987 wxString
Left(size_t nCount
) const;
988 // get last nCount characters
989 wxString
Right(size_t nCount
) const;
990 // get all characters before the first occurance of ch
991 // (returns the whole string if ch not found)
992 wxString
BeforeFirst(wxChar ch
) const;
993 // get all characters before the last occurence of ch
994 // (returns empty string if ch not found)
995 wxString
BeforeLast(wxChar ch
) const;
996 // get all characters after the first occurence of ch
997 // (returns empty string if ch not found)
998 wxString
AfterFirst(wxChar ch
) const;
999 // get all characters after the last occurence of ch
1000 // (returns the whole string if ch not found)
1001 wxString
AfterLast(wxChar ch
) const;
1003 // for compatibility only, use more explicitly named functions above
1004 wxString
Before(wxChar ch
) const { return BeforeLast(ch
); }
1005 wxString
After(wxChar ch
) const { return AfterFirst(ch
); }
1008 // convert to upper case in place, return the string itself
1009 wxString
& MakeUpper();
1010 // convert to upper case, return the copy of the string
1011 // Here's something to remember: BC++ doesn't like returns in inlines.
1012 wxString
Upper() const ;
1013 // convert to lower case in place, return the string itself
1014 wxString
& MakeLower();
1015 // convert to lower case, return the copy of the string
1016 wxString
Lower() const ;
1018 // trimming/padding whitespace (either side) and truncating
1019 // remove spaces from left or from right (default) side
1020 wxString
& Trim(bool bFromRight
= true);
1021 // add nCount copies chPad in the beginning or at the end (default)
1022 wxString
& Pad(size_t nCount
, wxChar chPad
= wxT(' '), bool bFromRight
= true);
1024 // searching and replacing
1025 // searching (return starting index, or -1 if not found)
1026 int Find(wxChar ch
, bool bFromEnd
= false) const; // like strchr/strrchr
1027 // searching (return starting index, or -1 if not found)
1028 int Find(const wxChar
*pszSub
) const; // like strstr
1029 // replace first (or all of bReplaceAll) occurences of substring with
1030 // another string, returns the number of replacements made
1031 size_t Replace(const wxChar
*szOld
,
1032 const wxChar
*szNew
,
1033 bool bReplaceAll
= true);
1035 // check if the string contents matches a mask containing '*' and '?'
1036 bool Matches(const wxChar
*szMask
) const;
1038 // conversion to numbers: all functions return true only if the whole
1039 // string is a number and put the value of this number into the pointer
1040 // provided, the base is the numeric base in which the conversion should be
1041 // done and must be comprised between 2 and 36 or be 0 in which case the
1042 // standard C rules apply (leading '0' => octal, "0x" => hex)
1043 // convert to a signed integer
1044 bool ToLong(long *val
, int base
= 10) const;
1045 // convert to an unsigned integer
1046 bool ToULong(unsigned long *val
, int base
= 10) const;
1047 // convert to a double
1048 bool ToDouble(double *val
) const;
1050 // formated input/output
1051 // as sprintf(), returns the number of characters written or < 0 on error
1052 // (take 'this' into account in attribute parameter count)
1053 int Printf(const wxChar
*pszFormat
, ...) ATTRIBUTE_PRINTF_2
;
1054 // as vprintf(), returns the number of characters written or < 0 on error
1055 int PrintfV(const wxChar
* pszFormat
, va_list argptr
);
1057 // returns the string containing the result of Printf() to it
1058 static wxString
Format(const wxChar
*pszFormat
, ...) ATTRIBUTE_PRINTF_1
;
1059 // the same as above, but takes a va_list
1060 static wxString
FormatV(const wxChar
*pszFormat
, va_list argptr
);
1062 // raw access to string memory
1063 // ensure that string has space for at least nLen characters
1064 // only works if the data of this string is not shared
1065 bool Alloc(size_t nLen
) { reserve(nLen
); /*return capacity() >= nLen;*/ return true; }
1066 // minimize the string's memory
1067 // only works if the data of this string is not shared
1070 // get writable buffer of at least nLen bytes. Unget() *must* be called
1071 // a.s.a.p. to put string back in a reasonable state!
1072 wxChar
*GetWriteBuf(size_t nLen
);
1073 // call this immediately after GetWriteBuf() has been used
1074 void UngetWriteBuf();
1075 void UngetWriteBuf(size_t nLen
);
1078 // wxWidgets version 1 compatibility functions
1081 wxString
SubString(size_t from
, size_t to
) const
1082 { return Mid(from
, (to
- from
+ 1)); }
1083 // values for second parameter of CompareTo function
1084 enum caseCompare
{exact
, ignoreCase
};
1085 // values for first parameter of Strip function
1086 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
1089 // (take 'this' into account in attribute parameter count)
1090 int sprintf(const wxChar
*pszFormat
, ...) ATTRIBUTE_PRINTF_2
;
1093 inline int CompareTo(const wxChar
* psz
, caseCompare cmp
= exact
) const
1094 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
1097 size_t Length() const { return length(); }
1098 // Count the number of characters
1099 int Freq(wxChar ch
) const;
1101 void LowerCase() { MakeLower(); }
1103 void UpperCase() { MakeUpper(); }
1104 // use Trim except that it doesn't change this string
1105 wxString
Strip(stripType w
= trailing
) const;
1107 // use Find (more general variants not yet supported)
1108 size_t Index(const wxChar
* psz
) const { return Find(psz
); }
1109 size_t Index(wxChar ch
) const { return Find(ch
); }
1111 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
1112 wxString
& RemoveLast(size_t n
= 1) { return Truncate(length() - n
); }
1114 wxString
& Remove(size_t nStart
, size_t nLen
)
1115 { return (wxString
&)erase( nStart
, nLen
); }
1118 int First( const wxChar ch
) const { return Find(ch
); }
1119 int First( const wxChar
* psz
) const { return Find(psz
); }
1120 int First( const wxString
&str
) const { return Find(str
); }
1121 int Last( const wxChar ch
) const { return Find(ch
, true); }
1122 bool Contains(const wxString
& str
) const { return Find(str
) != wxNOT_FOUND
; }
1125 bool IsNull() const { return IsEmpty(); }
1127 // std::string compatibility functions
1129 // take nLen chars starting at nPos
1130 wxString(const wxString
& str
, size_t nPos
, size_t nLen
)
1131 : wxStringBase(str
, nPos
, nLen
) { }
1132 // take all characters from pStart to pEnd
1133 wxString(const void *pStart
, const void *pEnd
)
1134 : wxStringBase((const wxChar
*)pStart
, (const wxChar
*)pEnd
) { }
1136 wxString(const_iterator first
, const_iterator last
)
1137 : wxStringBase(first
, last
) { }
1140 // lib.string.modifiers
1141 // append elements str[pos], ..., str[pos+n]
1142 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
1143 { return (wxString
&)wxStringBase::append(str
, pos
, n
); }
1145 wxString
& append(const wxString
& str
)
1146 { return (wxString
&)wxStringBase::append(str
); }
1147 // append first n (or all if n == npos) characters of sz
1148 wxString
& append(const wxChar
*sz
)
1149 { return (wxString
&)wxStringBase::append(sz
); }
1150 wxString
& append(const wxChar
*sz
, size_t n
)
1151 { return (wxString
&)wxStringBase::append(sz
, n
); }
1152 // append n copies of ch
1153 wxString
& append(size_t n
, wxChar ch
)
1154 { return (wxString
&)wxStringBase::append(n
, ch
); }
1155 // append from first to last
1156 wxString
& append(const_iterator first
, const_iterator last
)
1157 { return (wxString
&)wxStringBase::append(first
, last
); }
1159 // same as `this_string = str'
1160 wxString
& assign(const wxString
& str
)
1161 { return (wxString
&)wxStringBase::assign(str
); }
1162 // same as ` = str[pos..pos + n]
1163 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
1164 { return (wxString
&)wxStringBase::assign(str
, pos
, n
); }
1165 // same as `= first n (or all if n == npos) characters of sz'
1166 wxString
& assign(const wxChar
*sz
)
1167 { return (wxString
&)wxStringBase::assign(sz
); }
1168 wxString
& assign(const wxChar
*sz
, size_t n
)
1169 { return (wxString
&)wxStringBase::assign(sz
, n
); }
1170 // same as `= n copies of ch'
1171 wxString
& assign(size_t n
, wxChar ch
)
1172 { return (wxString
&)wxStringBase::assign(n
, ch
); }
1173 // assign from first to last
1174 wxString
& assign(const_iterator first
, const_iterator last
)
1175 { return (wxString
&)wxStringBase::assign(first
, last
); }
1177 // string comparison
1178 #ifndef HAVE_STD_STRING_COMPARE
1179 int compare(const wxStringBase
& str
) const;
1180 // comparison with a substring
1181 int compare(size_t nStart
, size_t nLen
, const wxStringBase
& str
) const;
1182 // comparison of 2 substrings
1183 int compare(size_t nStart
, size_t nLen
,
1184 const wxStringBase
& str
, size_t nStart2
, size_t nLen2
) const;
1185 // just like strcmp()
1186 int compare(const wxChar
* sz
) const;
1187 // substring comparison with first nCount characters of sz
1188 int compare(size_t nStart
, size_t nLen
,
1189 const wxChar
* sz
, size_t nCount
= npos
) const;
1190 #endif // !defined HAVE_STD_STRING_COMPARE
1192 // insert another string
1193 wxString
& insert(size_t nPos
, const wxString
& str
)
1194 { return (wxString
&)wxStringBase::insert(nPos
, str
); }
1195 // insert n chars of str starting at nStart (in str)
1196 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
1197 { return (wxString
&)wxStringBase::insert(nPos
, str
, nStart
, n
); }
1198 // insert first n (or all if n == npos) characters of sz
1199 wxString
& insert(size_t nPos
, const wxChar
*sz
)
1200 { return (wxString
&)wxStringBase::insert(nPos
, sz
); }
1201 wxString
& insert(size_t nPos
, const wxChar
*sz
, size_t n
)
1202 { return (wxString
&)wxStringBase::insert(nPos
, sz
, n
); }
1203 // insert n copies of ch
1204 wxString
& insert(size_t nPos
, size_t n
, wxChar ch
)
1205 { return (wxString
&)wxStringBase::insert(nPos
, n
, ch
); }
1206 iterator
insert(iterator it
, wxChar ch
)
1207 { return wxStringBase::insert(it
, ch
); }
1208 void insert(iterator it
, const_iterator first
, const_iterator last
)
1209 { wxStringBase::insert(it
, first
, last
); }
1210 void insert(iterator it
, size_type n
, wxChar ch
)
1211 { wxStringBase::insert(it
, n
, ch
); }
1213 // delete characters from nStart to nStart + nLen
1214 wxString
& erase(size_type pos
= 0, size_type n
= npos
)
1215 { return (wxString
&)wxStringBase::erase(pos
, n
); }
1216 iterator
erase(iterator first
, iterator last
)
1217 { return wxStringBase::erase(first
, last
); }
1218 iterator
erase(iterator first
)
1219 { return wxStringBase::erase(first
); }
1221 #ifdef wxSTRING_BASE_HASNT_CLEAR
1222 void clear() { erase(); }
1225 // replaces the substring of length nLen starting at nStart
1226 wxString
& replace(size_t nStart
, size_t nLen
, const wxChar
* sz
)
1227 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, sz
); }
1228 // replaces the substring of length nLen starting at nStart
1229 wxString
& replace(size_t nStart
, size_t nLen
, const wxString
& str
)
1230 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, str
); }
1231 // replaces the substring with nCount copies of ch
1232 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxChar ch
)
1233 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, nCount
, ch
); }
1234 // replaces a substring with another substring
1235 wxString
& replace(size_t nStart
, size_t nLen
,
1236 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1237 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, str
,
1239 // replaces the substring with first nCount chars of sz
1240 wxString
& replace(size_t nStart
, size_t nLen
,
1241 const wxChar
* sz
, size_t nCount
)
1242 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, sz
, nCount
); }
1243 wxString
& replace(iterator first
, iterator last
, const_pointer s
)
1244 { return (wxString
&)wxStringBase::replace(first
, last
, s
); }
1245 wxString
& replace(iterator first
, iterator last
, const_pointer s
,
1247 { return (wxString
&)wxStringBase::replace(first
, last
, s
, n
); }
1248 wxString
& replace(iterator first
, iterator last
, const wxString
& s
)
1249 { return (wxString
&)wxStringBase::replace(first
, last
, s
); }
1250 wxString
& replace(iterator first
, iterator last
, size_type n
, wxChar c
)
1251 { return (wxString
&)wxStringBase::replace(first
, last
, n
, c
); }
1252 wxString
& replace(iterator first
, iterator last
,
1253 const_iterator first1
, const_iterator last1
)
1254 { return (wxString
&)wxStringBase::replace(first
, last
, first1
, last1
); }
1257 wxString
& operator+=(const wxString
& s
)
1258 { return (wxString
&)wxStringBase::operator+=(s
); }
1259 // string += C string
1260 wxString
& operator+=(const wxChar
*psz
)
1261 { return (wxString
&)wxStringBase::operator+=(psz
); }
1263 wxString
& operator+=(wxChar ch
)
1264 { return (wxString
&)wxStringBase::operator+=(ch
); }
1267 // define wxArrayString, for compatibility
1268 #if WXWIN_COMPATIBILITY_2_4 && !wxUSE_STL
1269 #include "wx/arrstr.h"
1273 // return an empty wxString (not very useful with wxUSE_STL == 1)
1274 inline const wxString
wxGetEmptyString() { return wxString(); }
1276 // return an empty wxString (more efficient than wxString() here)
1277 inline const wxString
& wxGetEmptyString()
1279 return *(wxString
*)&wxEmptyString
;
1281 #endif // wxUSE_STL/!wxUSE_STL
1283 // ----------------------------------------------------------------------------
1284 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
1285 // ----------------------------------------------------------------------------
1289 class WXDLLIMPEXP_BASE wxStringBuffer
1292 wxStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
1293 : m_str(str
), m_buf(lenWanted
)
1296 ~wxStringBuffer() { m_str
.assign(m_buf
.data(), wxStrlen(m_buf
.data())); }
1298 operator wxChar
*() { return m_buf
.data(); }
1303 wxWCharBuffer m_buf
;
1308 DECLARE_NO_COPY_CLASS(wxStringBuffer
)
1311 class WXDLLIMPEXP_BASE wxStringBufferLength
1314 wxStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
1315 : m_str(str
), m_buf(lenWanted
), m_len(0), m_lenSet(false)
1318 ~wxStringBufferLength()
1321 m_str
.assign(m_buf
.data(), m_len
);
1324 operator wxChar
*() { return m_buf
.data(); }
1325 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
1330 wxWCharBuffer m_buf
;
1337 DECLARE_NO_COPY_CLASS(wxStringBufferLength
)
1340 #else // if !wxUSE_STL
1342 class WXDLLIMPEXP_BASE wxStringBuffer
1345 wxStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
1346 : m_str(str
), m_buf(NULL
)
1347 { m_buf
= m_str
.GetWriteBuf(lenWanted
); }
1349 ~wxStringBuffer() { m_str
.UngetWriteBuf(); }
1351 operator wxChar
*() const { return m_buf
; }
1357 DECLARE_NO_COPY_CLASS(wxStringBuffer
)
1360 class WXDLLIMPEXP_BASE wxStringBufferLength
1363 wxStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
1364 : m_str(str
), m_buf(NULL
), m_len(0), m_lenSet(false)
1366 m_buf
= m_str
.GetWriteBuf(lenWanted
);
1367 wxASSERT(m_buf
!= NULL
);
1370 ~wxStringBufferLength()
1373 m_str
.UngetWriteBuf(m_len
);
1376 operator wxChar
*() const { return m_buf
; }
1377 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
1385 DECLARE_NO_COPY_CLASS(wxStringBufferLength
)
1388 #endif // !wxUSE_STL
1390 // ---------------------------------------------------------------------------
1391 // wxString comparison functions: operator versions are always case sensitive
1392 // ---------------------------------------------------------------------------
1394 // note that when wxUSE_STL == 1 the comparison operators taking std::string
1395 // are used and defining them also for wxString would only result in
1396 // compilation ambiguities when comparing std::string and wxString
1399 inline bool operator==(const wxString
& s1
, const wxString
& s2
)
1400 { return (s1
.Len() == s2
.Len()) && (s1
.Cmp(s2
) == 0); }
1401 inline bool operator==(const wxString
& s1
, const wxChar
* s2
)
1402 { return s1
.Cmp(s2
) == 0; }
1403 inline bool operator==(const wxChar
* s1
, const wxString
& s2
)
1404 { return s2
.Cmp(s1
) == 0; }
1405 inline bool operator!=(const wxString
& s1
, const wxString
& s2
)
1406 { return (s1
.Len() != s2
.Len()) || (s1
.Cmp(s2
) != 0); }
1407 inline bool operator!=(const wxString
& s1
, const wxChar
* s2
)
1408 { return s1
.Cmp(s2
) != 0; }
1409 inline bool operator!=(const wxChar
* s1
, const wxString
& s2
)
1410 { return s2
.Cmp(s1
) != 0; }
1411 inline bool operator< (const wxString
& s1
, const wxString
& s2
)
1412 { return s1
.Cmp(s2
) < 0; }
1413 inline bool operator< (const wxString
& s1
, const wxChar
* s2
)
1414 { return s1
.Cmp(s2
) < 0; }
1415 inline bool operator< (const wxChar
* s1
, const wxString
& s2
)
1416 { return s2
.Cmp(s1
) > 0; }
1417 inline bool operator> (const wxString
& s1
, const wxString
& s2
)
1418 { return s1
.Cmp(s2
) > 0; }
1419 inline bool operator> (const wxString
& s1
, const wxChar
* s2
)
1420 { return s1
.Cmp(s2
) > 0; }
1421 inline bool operator> (const wxChar
* s1
, const wxString
& s2
)
1422 { return s2
.Cmp(s1
) < 0; }
1423 inline bool operator<=(const wxString
& s1
, const wxString
& s2
)
1424 { return s1
.Cmp(s2
) <= 0; }
1425 inline bool operator<=(const wxString
& s1
, const wxChar
* s2
)
1426 { return s1
.Cmp(s2
) <= 0; }
1427 inline bool operator<=(const wxChar
* s1
, const wxString
& s2
)
1428 { return s2
.Cmp(s1
) >= 0; }
1429 inline bool operator>=(const wxString
& s1
, const wxString
& s2
)
1430 { return s1
.Cmp(s2
) >= 0; }
1431 inline bool operator>=(const wxString
& s1
, const wxChar
* s2
)
1432 { return s1
.Cmp(s2
) >= 0; }
1433 inline bool operator>=(const wxChar
* s1
, const wxString
& s2
)
1434 { return s2
.Cmp(s1
) <= 0; }
1436 #endif // !wxUSE_STL
1438 // comparison with char
1439 inline bool operator==(wxChar c
, const wxString
& s
) { return s
.IsSameAs(c
); }
1440 inline bool operator==(const wxString
& s
, wxChar c
) { return s
.IsSameAs(c
); }
1441 inline bool operator!=(wxChar c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
1442 inline bool operator!=(const wxString
& s
, wxChar c
) { return !s
.IsSameAs(c
); }
1445 inline bool operator==(const wxString
& s1
, const wxWCharBuffer
& s2
)
1446 { return (s1
.Cmp((const wchar_t *)s2
) == 0); }
1447 inline bool operator==(const wxWCharBuffer
& s1
, const wxString
& s2
)
1448 { return (s2
.Cmp((const wchar_t *)s1
) == 0); }
1449 inline bool operator!=(const wxString
& s1
, const wxWCharBuffer
& s2
)
1450 { return (s1
.Cmp((const wchar_t *)s2
) != 0); }
1451 inline bool operator!=(const wxWCharBuffer
& s1
, const wxString
& s2
)
1452 { return (s2
.Cmp((const wchar_t *)s1
) != 0); }
1453 #else // !wxUSE_UNICODE
1454 inline bool operator==(const wxString
& s1
, const wxCharBuffer
& s2
)
1455 { return (s1
.Cmp((const char *)s2
) == 0); }
1456 inline bool operator==(const wxCharBuffer
& s1
, const wxString
& s2
)
1457 { return (s2
.Cmp((const char *)s1
) == 0); }
1458 inline bool operator!=(const wxString
& s1
, const wxCharBuffer
& s2
)
1459 { return (s1
.Cmp((const char *)s2
) != 0); }
1460 inline bool operator!=(const wxCharBuffer
& s1
, const wxString
& s2
)
1461 { return (s2
.Cmp((const char *)s1
) != 0); }
1462 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1466 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
, const wxString
& string2
);
1467 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxChar ch
);
1468 wxString WXDLLIMPEXP_BASE
operator+(wxChar ch
, const wxString
& string
);
1469 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const wxChar
*psz
);
1470 wxString WXDLLIMPEXP_BASE
operator+(const wxChar
*psz
, const wxString
& string
);
1472 #endif // !wxUSE_STL
1475 inline wxString
operator+(const wxString
& string
, const wxWCharBuffer
& buf
)
1476 { return string
+ (const wchar_t *)buf
; }
1477 inline wxString
operator+(const wxWCharBuffer
& buf
, const wxString
& string
)
1478 { return (const wchar_t *)buf
+ string
; }
1479 #else // !wxUSE_UNICODE
1480 inline wxString
operator+(const wxString
& string
, const wxCharBuffer
& buf
)
1481 { return string
+ (const char *)buf
; }
1482 inline wxString
operator+(const wxCharBuffer
& buf
, const wxString
& string
)
1483 { return (const char *)buf
+ string
; }
1484 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1486 // ---------------------------------------------------------------------------
1487 // Implementation only from here until the end of file
1488 // ---------------------------------------------------------------------------
1490 // don't pollute the library user's name space
1491 #undef wxASSERT_VALID_INDEX
1493 #if wxUSE_STD_IOSTREAM
1495 #include "wx/iosfwrap.h"
1497 WXDLLIMPEXP_BASE wxSTD istream
& operator>>(wxSTD istream
&, wxString
&);
1498 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxString
&);
1500 #endif // wxSTD_STRING_COMPATIBILITY
1502 #endif // _WX_WXSTRINGH__