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(HAVE_STRCASECMP_IN_STRING_H) || \
136 defined(HAVE_STRCASECMP_IN_STRINGS_H) || \
137 defined(__GNUWIN32__)
138 return strcasecmp(psz1
, psz2
);
139 #elif defined(__MWERKS__) && !defined(__INTEL__)
140 register char c1
, c2
;
142 c1
= tolower(*psz1
++);
143 c2
= tolower(*psz2
++);
144 } while ( c1
&& (c1
== c2
) );
148 // almost all compilers/libraries provide this function (unfortunately under
149 // different names), that's why we don't implement our own which will surely
150 // be more efficient than this code (uncomment to use):
152 register char c1, c2;
154 c1 = tolower(*psz1++);
155 c2 = tolower(*psz2++);
156 } while ( c1 && (c1 == c2) );
161 #error "Please define string case-insensitive compare for your OS/compiler"
162 #endif // OS/compiler
167 #include "wx/beforestd.h"
169 #include "wx/afterstd.h"
172 #ifdef HAVE_STD_WSTRING
173 typedef std::wstring wxStringBase
;
175 typedef std::basic_string
<wxChar
> wxStringBase
;
178 typedef std::string wxStringBase
;
181 #if (defined(__GNUG__) && (__GNUG__ < 3)) || \
182 (defined(_MSC_VER) && (_MSC_VER <= 1200))
183 #define wxSTRING_BASE_HASNT_CLEAR
186 #else // if !wxUSE_STL
188 #ifndef HAVE_STD_STRING_COMPARE
189 #define HAVE_STD_STRING_COMPARE
192 // ---------------------------------------------------------------------------
193 // string data prepended with some housekeeping info (used by wxString class),
194 // is never used directly (but had to be put here to allow inlining)
195 // ---------------------------------------------------------------------------
197 struct WXDLLIMPEXP_BASE wxStringData
199 int nRefs
; // reference count
200 size_t nDataLength
, // actual string length
201 nAllocLength
; // allocated memory size
203 // mimics declaration 'wxChar data[nAllocLength]'
204 wxChar
* data() const { return (wxChar
*)(this + 1); }
206 // empty string has a special ref count so it's never deleted
207 bool IsEmpty() const { return (nRefs
== -1); }
208 bool IsShared() const { return (nRefs
> 1); }
211 void Lock() { if ( !IsEmpty() ) nRefs
++; }
213 // VC++ will refuse to inline Unlock but profiling shows that it is wrong
214 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
217 // VC++ free must take place in same DLL as allocation when using non dll
218 // run-time library (e.g. Multithreaded instead of Multithreaded DLL)
219 #if defined(__VISUALC__) && defined(_MT) && !defined(_DLL)
220 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) Free(); }
221 // we must not inline deallocation since allocation is not inlined
224 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) free(this); }
227 // if we had taken control over string memory (GetWriteBuf), it's
228 // intentionally put in invalid state
229 void Validate(bool b
) { nRefs
= (b
? 1 : 0); }
230 bool IsValid() const { return (nRefs
!= 0); }
233 class WXDLLIMPEXP_BASE wxStringBase
236 friend class WXDLLIMPEXP_BASE wxArrayString
;
239 // an 'invalid' value for string index, moved to this place due to a CW bug
240 static const size_t npos
;
242 // points to data preceded by wxStringData structure with ref count info
245 // accessor to string data
246 wxStringData
* GetStringData() const { return (wxStringData
*)m_pchData
- 1; }
248 // string (re)initialization functions
249 // initializes the string to the empty value (must be called only from
250 // ctors, use Reinit() otherwise)
251 void Init() { m_pchData
= (wxChar
*)wxEmptyString
; }
252 // initializaes the string with (a part of) C-string
253 void InitWith(const wxChar
*psz
, size_t nPos
= 0, size_t nLen
= npos
);
254 // as Init, but also frees old data
255 void Reinit() { GetStringData()->Unlock(); Init(); }
258 // allocates memory for string of length nLen
259 bool AllocBuffer(size_t nLen
);
260 // copies data to another string
261 bool AllocCopy(wxString
&, int, int) const;
262 // effectively copies data to string
263 bool AssignCopy(size_t, const wxChar
*);
265 // append a (sub)string
266 bool ConcatSelf(size_t nLen
, const wxChar
*src
, size_t nMaxLen
);
267 bool ConcatSelf(size_t nLen
, const wxChar
*src
)
268 { return ConcatSelf(nLen
, src
, nLen
); }
270 // functions called before writing to the string: they copy it if there
271 // are other references to our data (should be the only owner when writing)
272 bool CopyBeforeWrite();
273 bool AllocBeforeWrite(size_t);
275 // compatibility with wxString
276 bool Alloc(size_t nLen
);
279 typedef wxChar value_type
;
280 typedef wxChar char_type
;
281 typedef size_t size_type
;
282 typedef value_type
& reference
;
283 typedef const value_type
& const_reference
;
284 typedef value_type
* pointer
;
285 typedef const value_type
* const_pointer
;
286 typedef value_type
*iterator
;
287 typedef const value_type
*const_iterator
;
289 // constructors and destructor
290 // ctor for an empty string
291 wxStringBase() { Init(); }
293 wxStringBase(const wxStringBase
& stringSrc
)
295 wxASSERT_MSG( stringSrc
.GetStringData()->IsValid(),
296 _T("did you forget to call UngetWriteBuf()?") );
298 if ( stringSrc
.empty() ) {
299 // nothing to do for an empty string
303 m_pchData
= stringSrc
.m_pchData
; // share same data
304 GetStringData()->Lock(); // => one more copy
307 // string containing nRepeat copies of ch
308 wxStringBase(size_type nRepeat
, wxChar ch
);
309 // ctor takes first nLength characters from C string
310 // (default value of npos means take all the string)
311 wxStringBase(const wxChar
*psz
)
312 { InitWith(psz
, 0, npos
); }
313 wxStringBase(const wxChar
*psz
, size_t nLength
)
314 { InitWith(psz
, 0, nLength
); }
315 wxStringBase(const wxChar
*psz
, wxMBConv
& WXUNUSED(conv
), size_t nLength
= npos
)
316 { InitWith(psz
, 0, nLength
); }
317 // take nLen chars starting at nPos
318 wxStringBase(const wxStringBase
& str
, size_t nPos
, size_t nLen
)
320 wxASSERT_MSG( str
.GetStringData()->IsValid(),
321 _T("did you forget to call UngetWriteBuf()?") );
323 size_t strLen
= str
.length() - nPos
; nLen
= strLen
< nLen
? strLen
: nLen
;
324 InitWith(str
.c_str(), nPos
, nLen
);
326 // take all characters from pStart to pEnd
327 wxStringBase(const void *pStart
, const void *pEnd
);
329 // dtor is not virtual, this class must not be inherited from!
332 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
333 //RN - according to the above VC++ does indeed inline this,
334 //even though it spits out two warnings
335 #pragma warning (disable:4714)
338 GetStringData()->Unlock();
341 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
342 //re-enable inlining warning
343 #pragma warning (default:4714)
345 // overloaded assignment
346 // from another wxString
347 wxStringBase
& operator=(const wxStringBase
& stringSrc
);
349 wxStringBase
& operator=(wxChar ch
);
351 wxStringBase
& operator=(const wxChar
*psz
);
353 // return the length of the string
354 size_type
size() const { return GetStringData()->nDataLength
; }
355 // return the length of the string
356 size_type
length() const { return size(); }
357 // return the maximum size of the string
358 size_type
max_size() const { return wxSTRING_MAXLEN
; }
359 // resize the string, filling the space with c if c != 0
360 void resize(size_t nSize
, wxChar ch
= wxT('\0'));
361 // delete the contents of the string
362 void clear() { erase(0, npos
); }
363 // returns true if the string is empty
364 bool empty() const { return size() == 0; }
365 // inform string about planned change in size
366 void reserve(size_t sz
) { Alloc(sz
); }
367 size_type
capacity() const { return GetStringData()->nAllocLength
; }
370 // return the character at position n
371 value_type
at(size_type n
) const
372 { wxASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
373 value_type
operator[](size_type n
) const { return at(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
]; }
377 reference
operator[](size_type n
)
378 { wxASSERT_VALID_INDEX( n
); CopyBeforeWrite(); return m_pchData
[n
]; }
380 // lib.string.modifiers
381 // append elements str[pos], ..., str[pos+n]
382 wxStringBase
& append(const wxStringBase
& str
, size_t pos
, size_t n
)
384 wxASSERT(pos
<= str
.length());
385 ConcatSelf(n
, str
.c_str() + pos
, str
.length() - pos
);
389 wxStringBase
& append(const wxStringBase
& str
)
390 { ConcatSelf(str
.length(), str
.c_str()); return *this; }
391 // append first n (or all if n == npos) characters of sz
392 wxStringBase
& append(const wxChar
*sz
)
393 { ConcatSelf(wxStrlen(sz
), sz
); return *this; }
394 wxStringBase
& append(const wxChar
*sz
, size_t n
)
395 { ConcatSelf(n
, sz
); return *this; }
396 // append n copies of ch
397 wxStringBase
& append(size_t n
, wxChar ch
);
398 // append from first to last
399 wxStringBase
& append(const_iterator first
, const_iterator last
)
400 { ConcatSelf(last
- first
, first
); return *this; }
402 // same as `this_string = str'
403 wxStringBase
& assign(const wxStringBase
& str
)
404 { return *this = str
; }
405 // same as ` = str[pos..pos + n]
406 wxStringBase
& assign(const wxStringBase
& str
, size_t pos
, size_t n
)
407 { clear(); return append(str
, pos
, n
); }
408 // same as `= first n (or all if n == npos) characters of sz'
409 wxStringBase
& assign(const wxChar
*sz
)
410 { clear(); return append(sz
, wxStrlen(sz
)); }
411 wxStringBase
& assign(const wxChar
*sz
, size_t n
)
412 { clear(); return append(sz
, n
); }
413 // same as `= n copies of ch'
414 wxStringBase
& assign(size_t n
, wxChar ch
)
415 { clear(); return append(n
, ch
); }
416 // assign from first to last
417 wxStringBase
& assign(const_iterator first
, const_iterator last
)
418 { clear(); return append(first
, last
); }
420 // first valid index position
421 const_iterator
begin() const { return m_pchData
; }
422 // position one after the last valid one
423 const_iterator
end() const { return m_pchData
+ length(); }
425 // first valid index position
427 // position one after the last valid one
430 // insert another string
431 wxStringBase
& insert(size_t nPos
, const wxStringBase
& str
)
433 wxASSERT( str
.GetStringData()->IsValid() );
434 return insert(nPos
, str
.c_str(), str
.length());
436 // insert n chars of str starting at nStart (in str)
437 wxStringBase
& insert(size_t nPos
, const wxStringBase
& str
, size_t nStart
, size_t n
)
439 wxASSERT( str
.GetStringData()->IsValid() );
440 wxASSERT( nStart
< str
.length() );
441 size_t strLen
= str
.length() - nStart
;
442 n
= strLen
< n
? strLen
: n
;
443 return insert(nPos
, str
.c_str() + nStart
, n
);
445 // insert first n (or all if n == npos) characters of sz
446 wxStringBase
& insert(size_t nPos
, const wxChar
*sz
, size_t n
= npos
);
447 // insert n copies of ch
448 wxStringBase
& insert(size_t nPos
, size_t n
, wxChar ch
)
449 { return insert(nPos
, wxStringBase(n
, ch
)); }
450 iterator
insert(iterator it
, wxChar ch
)
451 { size_t idx
= it
- begin(); insert(idx
, 1, ch
); return begin() + idx
; }
452 void insert(iterator it
, const_iterator first
, const_iterator last
)
453 { insert(it
- begin(), first
, last
- first
); }
454 void insert(iterator it
, size_type n
, wxChar ch
)
455 { insert(it
- begin(), n
, ch
); }
457 // delete characters from nStart to nStart + nLen
458 wxStringBase
& erase(size_type pos
= 0, size_type n
= npos
);
459 iterator
erase(iterator first
, iterator last
)
461 size_t idx
= first
- begin();
462 erase(idx
, last
- first
);
463 return begin() + idx
;
465 iterator
erase(iterator first
);
467 // explicit conversion to C string (use this with printf()!)
468 const wxChar
* c_str() const { return m_pchData
; }
469 const wxChar
* data() const { return m_pchData
; }
471 // replaces the substring of length nLen starting at nStart
472 wxStringBase
& replace(size_t nStart
, size_t nLen
, const wxChar
* sz
);
473 // replaces the substring of length nLen starting at nStart
474 wxStringBase
& replace(size_t nStart
, size_t nLen
, const wxStringBase
& str
)
475 { return replace(nStart
, nLen
, str
.c_str()); }
476 // replaces the substring with nCount copies of ch
477 wxStringBase
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxChar ch
);
478 // replaces a substring with another substring
479 wxStringBase
& replace(size_t nStart
, size_t nLen
,
480 const wxStringBase
& str
, size_t nStart2
, size_t nLen2
);
481 // replaces the substring with first nCount chars of sz
482 wxStringBase
& replace(size_t nStart
, size_t nLen
,
483 const wxChar
* sz
, size_t nCount
);
484 wxStringBase
& replace(iterator first
, iterator last
, const_pointer s
)
485 { return replace(first
- begin(), last
- first
, s
); }
486 wxStringBase
& replace(iterator first
, iterator last
, const_pointer s
,
488 { return replace(first
- begin(), last
- first
, s
, n
); }
489 wxStringBase
& replace(iterator first
, iterator last
, const wxStringBase
& s
)
490 { return replace(first
- begin(), last
- first
, s
); }
491 wxStringBase
& replace(iterator first
, iterator last
, size_type n
, wxChar c
)
492 { return replace(first
- begin(), last
- first
, n
, c
); }
493 wxStringBase
& replace(iterator first
, iterator last
,
494 const_iterator first1
, const_iterator last1
)
495 { return replace(first
- begin(), last
- first
, first1
, last1
- first1
); }
498 void swap(wxStringBase
& str
);
500 // All find() functions take the nStart argument which specifies the
501 // position to start the search on, the default value is 0. All functions
502 // return npos if there were no match.
505 size_t find(const wxStringBase
& str
, size_t nStart
= 0) const;
507 // VC++ 1.5 can't cope with this syntax.
508 #if !defined(__VISUALC__) || defined(__WIN32__)
509 // find first n characters of sz
510 size_t find(const wxChar
* sz
, size_t nStart
= 0, size_t n
= npos
) const;
513 // find the first occurence of character ch after nStart
514 size_t find(wxChar ch
, size_t nStart
= 0) const;
516 // rfind() family is exactly like find() but works right to left
518 // as find, but from the end
519 size_t rfind(const wxStringBase
& str
, size_t nStart
= npos
) const;
521 // VC++ 1.5 can't cope with this syntax.
522 // as find, but from the end
523 size_t rfind(const wxChar
* sz
, size_t nStart
= npos
,
524 size_t n
= npos
) const;
525 // as find, but from the end
526 size_t rfind(wxChar ch
, size_t nStart
= npos
) const;
528 // find first/last occurence of any character in the set
530 // as strpbrk() but starts at nStart, returns npos if not found
531 size_t find_first_of(const wxStringBase
& str
, size_t nStart
= 0) const
532 { return find_first_of(str
.c_str(), nStart
); }
534 size_t find_first_of(const wxChar
* sz
, size_t nStart
= 0) const;
535 size_t find_first_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
536 // same as find(char, size_t)
537 size_t find_first_of(wxChar c
, size_t nStart
= 0) const
538 { return find(c
, nStart
); }
539 // find the last (starting from nStart) char from str in this string
540 size_t find_last_of (const wxStringBase
& str
, size_t nStart
= npos
) const
541 { return find_last_of(str
.c_str(), nStart
); }
543 size_t find_last_of (const wxChar
* sz
, size_t nStart
= npos
) const;
544 size_t find_last_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
546 size_t find_last_of(wxChar c
, size_t nStart
= npos
) const
547 { return rfind(c
, nStart
); }
549 // find first/last occurence of any character not in the set
551 // as strspn() (starting from nStart), returns npos on failure
552 size_t find_first_not_of(const wxStringBase
& str
, size_t nStart
= 0) const
553 { return find_first_not_of(str
.c_str(), nStart
); }
555 size_t find_first_not_of(const wxChar
* sz
, size_t nStart
= 0) const;
556 size_t find_first_not_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
558 size_t find_first_not_of(wxChar ch
, size_t nStart
= 0) const;
560 size_t find_last_not_of(const wxStringBase
& str
, size_t nStart
= npos
) const
561 { return find_last_not_of(str
.c_str(), nStart
); }
563 size_t find_last_not_of(const wxChar
* sz
, size_t nStart
= npos
) const;
564 size_t find_last_not_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
566 size_t find_last_not_of(wxChar ch
, size_t nStart
= npos
) const;
568 // All compare functions return -1, 0 or 1 if the [sub]string is less,
569 // equal or greater than the compare() argument.
571 // comparison with another string
572 int compare(const wxStringBase
& str
) const;
573 // comparison with a substring
574 int compare(size_t nStart
, size_t nLen
, const wxStringBase
& str
) const;
575 // comparison of 2 substrings
576 int compare(size_t nStart
, size_t nLen
,
577 const wxStringBase
& str
, size_t nStart2
, size_t nLen2
) const;
578 // comparison with a c string
579 int compare(const wxChar
* sz
) const;
580 // substring comparison with first nCount characters of sz
581 int compare(size_t nStart
, size_t nLen
,
582 const wxChar
* sz
, size_t nCount
= npos
) const;
584 size_type
copy(wxChar
* s
, size_type n
, size_type pos
= 0);
586 // substring extraction
587 wxStringBase
substr(size_t nStart
= 0, size_t nLen
= npos
) const;
590 wxStringBase
& operator+=(const wxStringBase
& s
) { return append(s
); }
591 // string += C string
592 wxStringBase
& operator+=(const wxChar
*psz
) { return append(psz
); }
594 wxStringBase
& operator+=(wxChar ch
) { return append(1, ch
); }
599 // ----------------------------------------------------------------------------
600 // wxString: string class trying to be compatible with std::string, MFC
601 // CString and wxWindows 1.x wxString all at once
602 // ---------------------------------------------------------------------------
604 class WXDLLIMPEXP_BASE wxString
: public wxStringBase
607 friend class WXDLLIMPEXP_BASE wxArrayString
;
610 // NB: special care was taken in arranging the member functions in such order
611 // that all inline functions can be effectively inlined, verify that all
612 // performace critical functions are still inlined if you change order!
614 // if we hadn't made these operators private, it would be possible to
615 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
616 // converted to char in C and we do have operator=(char)
618 // NB: we don't need other versions (short/long and unsigned) as attempt
619 // to assign another numeric type to wxString will now result in
620 // ambiguity between operator=(char) and operator=(int)
621 wxString
& operator=(int);
623 // these methods are not implemented - there is _no_ conversion from int to
624 // string, you're doing something wrong if the compiler wants to call it!
626 // try `s << i' or `s.Printf("%d", i)' instead
630 // constructors and destructor
631 // ctor for an empty string
632 wxString() : wxStringBase() { }
634 wxString(const wxStringBase
& stringSrc
) : wxStringBase(stringSrc
) { }
635 wxString(const wxString
& stringSrc
) : wxStringBase(stringSrc
) { }
636 // string containing nRepeat copies of ch
637 wxString(wxChar ch
, size_t nRepeat
= 1)
638 : wxStringBase(nRepeat
, ch
) { }
639 wxString(size_t nRepeat
, wxChar ch
)
640 : wxStringBase(nRepeat
, ch
) { }
641 // ctor takes first nLength characters from C string
642 // (default value of npos means take all the string)
643 wxString(const wxChar
*psz
)
644 : wxStringBase(psz
? psz
: wxT("")) { }
645 wxString(const wxChar
*psz
, size_t nLength
)
646 : wxStringBase(psz
, nLength
) { }
647 wxString(const wxChar
*psz
, wxMBConv
& WXUNUSED(conv
), size_t nLength
= npos
)
648 : wxStringBase(psz
, nLength
== npos
? wxStrlen(psz
) : nLength
) { }
651 // from multibyte string
652 wxString(const char *psz
, wxMBConv
& conv
, size_t nLength
= npos
);
653 // from wxWCharBuffer (i.e. return from wxGetString)
654 wxString(const wxWCharBuffer
& psz
) : wxStringBase(psz
.data()) { }
656 // from C string (for compilers using unsigned char)
657 wxString(const unsigned char* psz
, size_t nLength
= npos
)
658 : wxStringBase((const char*)psz
, nLength
) { }
661 // from wide (Unicode) string
662 wxString(const wchar_t *pwz
, wxMBConv
& conv
= wxConvLibc
, size_t nLength
= npos
);
663 #endif // !wxUSE_WCHAR_T
666 wxString(const wxCharBuffer
& psz
)
667 : wxStringBase(psz
) { }
668 #endif // Unicode/ANSI
670 // generic attributes & operations
671 // as standard strlen()
672 size_t Len() const { return length(); }
673 // string contains any characters?
674 bool IsEmpty() const { return empty(); }
675 // empty string is "false", so !str will return true
676 bool operator!() const { return IsEmpty(); }
677 // truncate the string to given length
678 wxString
& Truncate(size_t uiLen
);
679 // empty string contents
684 wxASSERT_MSG( IsEmpty(), _T("string not empty after call to Empty()?") );
686 // empty the string and free memory
689 wxString
tmp(wxEmptyString
);
695 bool IsAscii() const;
697 bool IsNumber() const;
701 // data access (all indexes are 0 based)
703 wxChar
GetChar(size_t n
) const
704 { return operator[](n
); }
706 wxChar
& GetWritableChar(size_t n
)
707 { return operator[](n
); }
709 void SetChar(size_t n
, wxChar ch
)
710 { operator[](n
) = ch
; }
712 // get last character
715 wxASSERT_MSG( !IsEmpty(), _T("wxString: index out of bounds") );
717 return operator[](length() - 1);
720 // get writable last character
723 wxASSERT_MSG( !IsEmpty(), _T("wxString: index out of bounds") );
724 return operator[](length() - 1);
728 So why do we have all these overloaded operator[]s? A bit of history:
729 initially there was only one of them, taking size_t. Then people
730 started complaining because they wanted to use ints as indices (I
731 wonder why) and compilers were giving warnings about it, so we had to
732 add the operator[](int). Then it became apparent that you couldn't
733 write str[0] any longer because there was ambiguity between two
734 overloads and so you now had to write str[0u] (or, of course, use the
735 explicit casts to either int or size_t but nobody did this).
737 Finally, someone decided to compile wxWin on an Alpha machine and got
738 a surprize: str[0u] didn't compile there because it is of type
739 unsigned int and size_t is unsigned _long_ on Alpha and so there was
740 ambiguity between converting uint to int or ulong. To fix this one we
741 now add operator[](uint) for the machines where size_t is not already
742 the same as unsigned int - hopefully this fixes the problem (for some
745 The only real fix is, of course, to remove all versions but the one
749 // operator version of GetChar
750 wxChar
operator[](int n
) const
751 { return wxStringBase::operator[](n
); }
752 wxChar
& operator[](size_type n
)
753 { return wxStringBase::operator[](n
); }
754 wxChar
operator[](size_type n
) const
755 { return wxStringBase::operator[](n
); }
756 #ifndef wxSIZE_T_IS_UINT
757 // operator version of GetChar
758 wxChar
operator[](unsigned int n
) const
759 { return wxStringBase::operator[](n
); }
761 // operator version of GetWriteableChar
762 wxChar
& operator[](unsigned int n
)
763 { return wxStringBase::operator[](n
); }
764 #endif // size_t != unsigned int
766 // implicit conversion to C string
767 operator const wxChar
*() const { return c_str(); }
769 // identical to c_str(), for wxWin 1.6x compatibility
770 const wxChar
* wx_str() const { return c_str(); }
771 // identical to c_str(), for MFC compatibility
772 const wxChar
* GetData() const { return c_str(); }
774 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
775 // converting numbers or strings which are certain not to contain special
776 // chars (typically system functions, X atoms, environment variables etc.)
778 // the behaviour of these functions with the strings containing anything
779 // else than 7 bit ASCII characters is undefined, use at your own risk.
781 static wxString
FromAscii(const char *ascii
); // string
782 static wxString
FromAscii(const char ascii
); // char
783 const wxCharBuffer
ToAscii() const;
785 static wxString
FromAscii(const char *ascii
) { return wxString( ascii
); }
786 static wxString
FromAscii(const char ascii
) { return wxString( ascii
); }
787 const char *ToAscii() const { return c_str(); }
788 #endif // Unicode/!Unicode
790 // conversions with (possible) format conversions: have to return a
791 // buffer with temporary data
793 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
794 // return an ANSI (multibyte) string, wc_str() to return a wide string and
795 // fn_str() to return a string which should be used with the OS APIs
796 // accepting the file names. The return value is always the same, but the
797 // type differs because a function may either return pointer to the buffer
798 // directly or have to use intermediate buffer for translation.
800 const wxCharBuffer
mb_str(wxMBConv
& conv
= wxConvLibc
) const;
802 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
804 const wxChar
* wc_str() const { return c_str(); }
806 // for compatibility with !wxUSE_UNICODE version
807 const wxChar
* wc_str(wxMBConv
& WXUNUSED(conv
)) const { return c_str(); }
810 const wxCharBuffer
fn_str() const { return mb_str(wxConvFile
); }
812 const wxChar
* fn_str() const { return c_str(); }
813 #endif // wxMBFILES/!wxMBFILES
815 const wxChar
* mb_str() const { return c_str(); }
817 // for compatibility with wxUSE_UNICODE version
818 const wxChar
* mb_str(wxMBConv
& WXUNUSED(conv
)) const { return c_str(); }
820 const wxWX2MBbuf
mbc_str() const { return mb_str(); }
823 const wxWCharBuffer
wc_str(wxMBConv
& conv
) const;
824 #endif // wxUSE_WCHAR_T
826 const wxCharBuffer
fn_str() const { return wxConvFile
.cWC2WX( wc_str( wxConvLocal
) ); }
828 const wxChar
* fn_str() const { return c_str(); }
830 #endif // Unicode/ANSI
832 // overloaded assignment
833 // from another wxString
834 wxString
& operator=(const wxStringBase
& stringSrc
)
835 { return (wxString
&)wxStringBase::operator=(stringSrc
); }
837 wxString
& operator=(wxChar ch
)
838 { return (wxString
&)wxStringBase::operator=(ch
); }
840 wxString
& operator=(const wxChar
*psz
)
841 { return (wxString
&)wxStringBase::operator=(psz
); }
843 // from wxWCharBuffer
844 wxString
& operator=(const wxWCharBuffer
& psz
)
845 { (void) operator=((const wchar_t *)psz
); return *this; }
847 // from another kind of C string
848 wxString
& operator=(const unsigned char* psz
);
850 // from a wide string
851 wxString
& operator=(const wchar_t *pwz
);
854 wxString
& operator=(const wxCharBuffer
& psz
)
855 { (void) operator=((const char *)psz
); return *this; }
856 #endif // Unicode/ANSI
858 // string concatenation
859 // in place concatenation
861 Concatenate and return the result. Note that the left to right
862 associativity of << allows to write things like "str << str1 << str2
863 << ..." (unlike with +=)
866 wxString
& operator<<(const wxString
& s
)
869 wxASSERT_MSG( s
.GetStringData()->IsValid(),
870 _T("did you forget to call UngetWriteBuf()?") );
876 // string += C string
877 wxString
& operator<<(const wxChar
*psz
)
878 { append(psz
); return *this; }
880 wxString
& operator<<(wxChar ch
) { append(1, ch
); return *this; }
882 // string += buffer (i.e. from wxGetString)
884 wxString
& operator<<(const wxWCharBuffer
& s
)
885 { (void)operator<<((const wchar_t *)s
); return *this; }
886 void operator+=(const wxWCharBuffer
& s
)
887 { (void)operator<<((const wchar_t *)s
); }
888 #else // !wxUSE_UNICODE
889 wxString
& operator<<(const wxCharBuffer
& s
)
890 { (void)operator<<((const char *)s
); return *this; }
891 void operator+=(const wxCharBuffer
& s
)
892 { (void)operator<<((const char *)s
); }
893 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
895 // string += C string
896 wxString
& Append(const wxString
& s
)
898 // test for IsEmpty() to share the string if possible
905 wxString
& Append(const wxChar
* psz
)
906 { append(psz
); return *this; }
907 // append count copies of given character
908 wxString
& Append(wxChar ch
, size_t count
= 1u)
909 { append(count
, ch
); return *this; }
910 wxString
& Append(const wxChar
* psz
, size_t nLen
)
911 { append(psz
, nLen
); return *this; }
913 // prepend a string, return the string itself
914 wxString
& Prepend(const wxString
& str
)
915 { *this = str
+ *this; return *this; }
917 // non-destructive concatenation
919 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
, const wxString
& string2
);
921 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxChar ch
);
923 friend wxString WXDLLIMPEXP_BASE
operator+(wxChar ch
, const wxString
& string
);
925 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const wxChar
*psz
);
927 friend wxString WXDLLIMPEXP_BASE
operator+(const wxChar
*psz
, const wxString
& string
);
929 // stream-like functions
930 // insert an int into string
931 wxString
& operator<<(int i
)
932 { return (*this) << Format(_T("%d"), i
); }
933 // insert an unsigned int into string
934 wxString
& operator<<(unsigned int ui
)
935 { return (*this) << Format(_T("%u"), ui
); }
936 // insert a long into string
937 wxString
& operator<<(long l
)
938 { return (*this) << Format(_T("%ld"), l
); }
939 // insert an unsigned long into string
940 wxString
& operator<<(unsigned long ul
)
941 { return (*this) << Format(_T("%lu"), ul
); }
942 // insert a float into string
943 wxString
& operator<<(float f
)
944 { return (*this) << Format(_T("%f"), f
); }
945 // insert a double into string
946 wxString
& operator<<(double d
)
947 { return (*this) << Format(_T("%g"), d
); }
950 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
951 int Cmp(const wxChar
*psz
) const;
952 int Cmp(const wxString
& s
) const;
953 // same as Cmp() but not case-sensitive
954 int CmpNoCase(const wxChar
*psz
) const;
955 int CmpNoCase(const wxString
& s
) const;
956 // test for the string equality, either considering case or not
957 // (if compareWithCase then the case matters)
958 bool IsSameAs(const wxChar
*psz
, bool compareWithCase
= true) const
959 { return (compareWithCase
? Cmp(psz
) : CmpNoCase(psz
)) == 0; }
960 // comparison with a signle character: returns true if equal
961 bool IsSameAs(wxChar c
, bool compareWithCase
= true) const
963 return (length() == 1) && (compareWithCase
? GetChar(0u) == c
964 : wxToupper(GetChar(0u)) == wxToupper(c
));
967 // simple sub-string extraction
968 // return substring starting at nFirst of length nCount (or till the end
969 // if nCount = default value)
970 wxString
Mid(size_t nFirst
, size_t nCount
= npos
) const;
972 // operator version of Mid()
973 wxString
operator()(size_t start
, size_t len
) const
974 { return Mid(start
, len
); }
976 // check that the string starts with prefix and return the rest of the
977 // string in the provided pointer if it is not NULL, otherwise return
979 bool StartsWith(const wxChar
*prefix
, wxString
*rest
= NULL
) const;
981 // get first nCount characters
982 wxString
Left(size_t nCount
) const;
983 // get last nCount characters
984 wxString
Right(size_t nCount
) const;
985 // get all characters before the first occurance of ch
986 // (returns the whole string if ch not found)
987 wxString
BeforeFirst(wxChar ch
) const;
988 // get all characters before the last occurence of ch
989 // (returns empty string if ch not found)
990 wxString
BeforeLast(wxChar ch
) const;
991 // get all characters after the first occurence of ch
992 // (returns empty string if ch not found)
993 wxString
AfterFirst(wxChar ch
) const;
994 // get all characters after the last occurence of ch
995 // (returns the whole string if ch not found)
996 wxString
AfterLast(wxChar ch
) const;
998 // for compatibility only, use more explicitly named functions above
999 wxString
Before(wxChar ch
) const { return BeforeLast(ch
); }
1000 wxString
After(wxChar ch
) const { return AfterFirst(ch
); }
1003 // convert to upper case in place, return the string itself
1004 wxString
& MakeUpper();
1005 // convert to upper case, return the copy of the string
1006 // Here's something to remember: BC++ doesn't like returns in inlines.
1007 wxString
Upper() const ;
1008 // convert to lower case in place, return the string itself
1009 wxString
& MakeLower();
1010 // convert to lower case, return the copy of the string
1011 wxString
Lower() const ;
1013 // trimming/padding whitespace (either side) and truncating
1014 // remove spaces from left or from right (default) side
1015 wxString
& Trim(bool bFromRight
= true);
1016 // add nCount copies chPad in the beginning or at the end (default)
1017 wxString
& Pad(size_t nCount
, wxChar chPad
= wxT(' '), bool bFromRight
= true);
1019 // searching and replacing
1020 // searching (return starting index, or -1 if not found)
1021 int Find(wxChar ch
, bool bFromEnd
= false) const; // like strchr/strrchr
1022 // searching (return starting index, or -1 if not found)
1023 int Find(const wxChar
*pszSub
) const; // like strstr
1024 // replace first (or all of bReplaceAll) occurences of substring with
1025 // another string, returns the number of replacements made
1026 size_t Replace(const wxChar
*szOld
,
1027 const wxChar
*szNew
,
1028 bool bReplaceAll
= true);
1030 // check if the string contents matches a mask containing '*' and '?'
1031 bool Matches(const wxChar
*szMask
) const;
1033 // conversion to numbers: all functions return true only if the whole
1034 // string is a number and put the value of this number into the pointer
1035 // provided, the base is the numeric base in which the conversion should be
1036 // done and must be comprised between 2 and 36 or be 0 in which case the
1037 // standard C rules apply (leading '0' => octal, "0x" => hex)
1038 // convert to a signed integer
1039 bool ToLong(long *val
, int base
= 10) const;
1040 // convert to an unsigned integer
1041 bool ToULong(unsigned long *val
, int base
= 10) const;
1042 // convert to a double
1043 bool ToDouble(double *val
) const;
1045 // formated input/output
1046 // as sprintf(), returns the number of characters written or < 0 on error
1047 // (take 'this' into account in attribute parameter count)
1048 int Printf(const wxChar
*pszFormat
, ...) ATTRIBUTE_PRINTF_2
;
1049 // as vprintf(), returns the number of characters written or < 0 on error
1050 int PrintfV(const wxChar
* pszFormat
, va_list argptr
);
1052 // returns the string containing the result of Printf() to it
1053 static wxString
Format(const wxChar
*pszFormat
, ...) ATTRIBUTE_PRINTF_1
;
1054 // the same as above, but takes a va_list
1055 static wxString
FormatV(const wxChar
*pszFormat
, va_list argptr
);
1057 // raw access to string memory
1058 // ensure that string has space for at least nLen characters
1059 // only works if the data of this string is not shared
1060 bool Alloc(size_t nLen
) { reserve(nLen
); /*return capacity() >= nLen;*/ return true; }
1061 // minimize the string's memory
1062 // only works if the data of this string is not shared
1065 // get writable buffer of at least nLen bytes. Unget() *must* be called
1066 // a.s.a.p. to put string back in a reasonable state!
1067 wxChar
*GetWriteBuf(size_t nLen
);
1068 // call this immediately after GetWriteBuf() has been used
1069 void UngetWriteBuf();
1070 void UngetWriteBuf(size_t nLen
);
1073 // wxWidgets version 1 compatibility functions
1076 wxString
SubString(size_t from
, size_t to
) const
1077 { return Mid(from
, (to
- from
+ 1)); }
1078 // values for second parameter of CompareTo function
1079 enum caseCompare
{exact
, ignoreCase
};
1080 // values for first parameter of Strip function
1081 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
1084 // (take 'this' into account in attribute parameter count)
1085 int sprintf(const wxChar
*pszFormat
, ...) ATTRIBUTE_PRINTF_2
;
1088 inline int CompareTo(const wxChar
* psz
, caseCompare cmp
= exact
) const
1089 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
1092 size_t Length() const { return length(); }
1093 // Count the number of characters
1094 int Freq(wxChar ch
) const;
1096 void LowerCase() { MakeLower(); }
1098 void UpperCase() { MakeUpper(); }
1099 // use Trim except that it doesn't change this string
1100 wxString
Strip(stripType w
= trailing
) const;
1102 // use Find (more general variants not yet supported)
1103 size_t Index(const wxChar
* psz
) const { return Find(psz
); }
1104 size_t Index(wxChar ch
) const { return Find(ch
); }
1106 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
1107 wxString
& RemoveLast(size_t n
= 1) { return Truncate(length() - n
); }
1109 wxString
& Remove(size_t nStart
, size_t nLen
)
1110 { return (wxString
&)erase( nStart
, nLen
); }
1113 int First( const wxChar ch
) const { return Find(ch
); }
1114 int First( const wxChar
* psz
) const { return Find(psz
); }
1115 int First( const wxString
&str
) const { return Find(str
); }
1116 int Last( const wxChar ch
) const { return Find(ch
, true); }
1117 bool Contains(const wxString
& str
) const { return Find(str
) != wxNOT_FOUND
; }
1120 bool IsNull() const { return IsEmpty(); }
1122 // std::string compatibility functions
1124 // take nLen chars starting at nPos
1125 wxString(const wxString
& str
, size_t nPos
, size_t nLen
)
1126 : wxStringBase(str
, nPos
, nLen
) { }
1127 // take all characters from pStart to pEnd
1128 wxString(const void *pStart
, const void *pEnd
)
1129 : wxStringBase((const wxChar
*)pStart
, (const wxChar
*)pEnd
) { }
1131 wxString(const_iterator first
, const_iterator last
)
1132 : wxStringBase(first
, last
) { }
1135 // lib.string.modifiers
1136 // append elements str[pos], ..., str[pos+n]
1137 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
1138 { return (wxString
&)wxStringBase::append(str
, pos
, n
); }
1140 wxString
& append(const wxString
& str
)
1141 { return (wxString
&)wxStringBase::append(str
); }
1142 // append first n (or all if n == npos) characters of sz
1143 wxString
& append(const wxChar
*sz
)
1144 { return (wxString
&)wxStringBase::append(sz
); }
1145 wxString
& append(const wxChar
*sz
, size_t n
)
1146 { return (wxString
&)wxStringBase::append(sz
, n
); }
1147 // append n copies of ch
1148 wxString
& append(size_t n
, wxChar ch
)
1149 { return (wxString
&)wxStringBase::append(n
, ch
); }
1150 // append from first to last
1151 wxString
& append(const_iterator first
, const_iterator last
)
1152 { return (wxString
&)wxStringBase::append(first
, last
); }
1154 // same as `this_string = str'
1155 wxString
& assign(const wxString
& str
)
1156 { return (wxString
&)wxStringBase::assign(str
); }
1157 // same as ` = str[pos..pos + n]
1158 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
1159 { return (wxString
&)wxStringBase::assign(str
, pos
, n
); }
1160 // same as `= first n (or all if n == npos) characters of sz'
1161 wxString
& assign(const wxChar
*sz
)
1162 { return (wxString
&)wxStringBase::assign(sz
); }
1163 wxString
& assign(const wxChar
*sz
, size_t n
)
1164 { return (wxString
&)wxStringBase::assign(sz
, n
); }
1165 // same as `= n copies of ch'
1166 wxString
& assign(size_t n
, wxChar ch
)
1167 { return (wxString
&)wxStringBase::assign(n
, ch
); }
1168 // assign from first to last
1169 wxString
& assign(const_iterator first
, const_iterator last
)
1170 { return (wxString
&)wxStringBase::assign(first
, last
); }
1172 // string comparison
1173 #ifndef HAVE_STD_STRING_COMPARE
1174 int compare(const wxStringBase
& str
) const;
1175 // comparison with a substring
1176 int compare(size_t nStart
, size_t nLen
, const wxStringBase
& str
) const;
1177 // comparison of 2 substrings
1178 int compare(size_t nStart
, size_t nLen
,
1179 const wxStringBase
& str
, size_t nStart2
, size_t nLen2
) const;
1180 // just like strcmp()
1181 int compare(const wxChar
* sz
) const;
1182 // substring comparison with first nCount characters of sz
1183 int compare(size_t nStart
, size_t nLen
,
1184 const wxChar
* sz
, size_t nCount
= npos
) const;
1185 #endif // !defined HAVE_STD_STRING_COMPARE
1187 // insert another string
1188 wxString
& insert(size_t nPos
, const wxString
& str
)
1189 { return (wxString
&)wxStringBase::insert(nPos
, str
); }
1190 // insert n chars of str starting at nStart (in str)
1191 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
1192 { return (wxString
&)wxStringBase::insert(nPos
, str
, nStart
, n
); }
1193 // insert first n (or all if n == npos) characters of sz
1194 wxString
& insert(size_t nPos
, const wxChar
*sz
)
1195 { return (wxString
&)wxStringBase::insert(nPos
, sz
); }
1196 wxString
& insert(size_t nPos
, const wxChar
*sz
, size_t n
)
1197 { return (wxString
&)wxStringBase::insert(nPos
, sz
, n
); }
1198 // insert n copies of ch
1199 wxString
& insert(size_t nPos
, size_t n
, wxChar ch
)
1200 { return (wxString
&)wxStringBase::insert(nPos
, n
, ch
); }
1201 iterator
insert(iterator it
, wxChar ch
)
1202 { return wxStringBase::insert(it
, ch
); }
1203 void insert(iterator it
, const_iterator first
, const_iterator last
)
1204 { wxStringBase::insert(it
, first
, last
); }
1205 void insert(iterator it
, size_type n
, wxChar ch
)
1206 { wxStringBase::insert(it
, n
, ch
); }
1208 // delete characters from nStart to nStart + nLen
1209 wxString
& erase(size_type pos
= 0, size_type n
= npos
)
1210 { return (wxString
&)wxStringBase::erase(pos
, n
); }
1211 iterator
erase(iterator first
, iterator last
)
1212 { return wxStringBase::erase(first
, last
); }
1213 iterator
erase(iterator first
)
1214 { return wxStringBase::erase(first
); }
1216 #ifdef wxSTRING_BASE_HASNT_CLEAR
1217 void clear() { erase(); }
1220 // replaces the substring of length nLen starting at nStart
1221 wxString
& replace(size_t nStart
, size_t nLen
, const wxChar
* sz
)
1222 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, sz
); }
1223 // replaces the substring of length nLen starting at nStart
1224 wxString
& replace(size_t nStart
, size_t nLen
, const wxString
& str
)
1225 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, str
); }
1226 // replaces the substring with nCount copies of ch
1227 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxChar ch
)
1228 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, nCount
, ch
); }
1229 // replaces a substring with another substring
1230 wxString
& replace(size_t nStart
, size_t nLen
,
1231 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1232 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, str
,
1234 // replaces the substring with first nCount chars of sz
1235 wxString
& replace(size_t nStart
, size_t nLen
,
1236 const wxChar
* sz
, size_t nCount
)
1237 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, sz
, nCount
); }
1238 wxString
& replace(iterator first
, iterator last
, const_pointer s
)
1239 { return (wxString
&)wxStringBase::replace(first
, last
, s
); }
1240 wxString
& replace(iterator first
, iterator last
, const_pointer s
,
1242 { return (wxString
&)wxStringBase::replace(first
, last
, s
, n
); }
1243 wxString
& replace(iterator first
, iterator last
, const wxString
& s
)
1244 { return (wxString
&)wxStringBase::replace(first
, last
, s
); }
1245 wxString
& replace(iterator first
, iterator last
, size_type n
, wxChar c
)
1246 { return (wxString
&)wxStringBase::replace(first
, last
, n
, c
); }
1247 wxString
& replace(iterator first
, iterator last
,
1248 const_iterator first1
, const_iterator last1
)
1249 { return (wxString
&)wxStringBase::replace(first
, last
, first1
, last1
); }
1252 wxString
& operator+=(const wxString
& s
)
1253 { return (wxString
&)wxStringBase::operator+=(s
); }
1254 // string += C string
1255 wxString
& operator+=(const wxChar
*psz
)
1256 { return (wxString
&)wxStringBase::operator+=(psz
); }
1258 wxString
& operator+=(wxChar ch
)
1259 { return (wxString
&)wxStringBase::operator+=(ch
); }
1262 // define wxArrayString, for compatibility
1263 #if WXWIN_COMPATIBILITY_2_4 && !wxUSE_STL
1264 #include "wx/arrstr.h"
1268 // return an empty wxString (not very useful with wxUSE_STL == 1)
1269 inline const wxString
wxGetEmptyString() { return wxString(); }
1271 // return an empty wxString (more efficient than wxString() here)
1272 inline const wxString
& wxGetEmptyString()
1274 return *(wxString
*)&wxEmptyString
;
1276 #endif // wxUSE_STL/!wxUSE_STL
1278 // ----------------------------------------------------------------------------
1279 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
1280 // ----------------------------------------------------------------------------
1284 class WXDLLIMPEXP_BASE wxStringBuffer
1287 wxStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
1288 : m_str(str
), m_buf(lenWanted
)
1291 ~wxStringBuffer() { m_str
.assign(m_buf
.data(), wxStrlen(m_buf
.data())); }
1293 operator wxChar
*() { return m_buf
.data(); }
1298 wxWCharBuffer m_buf
;
1303 DECLARE_NO_COPY_CLASS(wxStringBuffer
)
1306 class WXDLLIMPEXP_BASE wxStringBufferLength
1309 wxStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
1310 : m_str(str
), m_buf(lenWanted
), m_len(0), m_lenSet(false)
1313 ~wxStringBufferLength()
1316 m_str
.assign(m_buf
.data(), m_len
);
1319 operator wxChar
*() { return m_buf
.data(); }
1320 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
1325 wxWCharBuffer m_buf
;
1332 DECLARE_NO_COPY_CLASS(wxStringBufferLength
)
1335 #else // if !wxUSE_STL
1337 class WXDLLIMPEXP_BASE wxStringBuffer
1340 wxStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
1341 : m_str(str
), m_buf(NULL
)
1342 { m_buf
= m_str
.GetWriteBuf(lenWanted
); }
1344 ~wxStringBuffer() { m_str
.UngetWriteBuf(); }
1346 operator wxChar
*() const { return m_buf
; }
1352 DECLARE_NO_COPY_CLASS(wxStringBuffer
)
1355 class WXDLLIMPEXP_BASE wxStringBufferLength
1358 wxStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
1359 : m_str(str
), m_buf(NULL
), m_len(0), m_lenSet(false)
1361 m_buf
= m_str
.GetWriteBuf(lenWanted
);
1362 wxASSERT(m_buf
!= NULL
);
1365 ~wxStringBufferLength()
1368 m_str
.UngetWriteBuf(m_len
);
1371 operator wxChar
*() const { return m_buf
; }
1372 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
1380 DECLARE_NO_COPY_CLASS(wxStringBufferLength
)
1383 #endif // !wxUSE_STL
1385 // ---------------------------------------------------------------------------
1386 // wxString comparison functions: operator versions are always case sensitive
1387 // ---------------------------------------------------------------------------
1389 // note that when wxUSE_STL == 1 the comparison operators taking std::string
1390 // are used and defining them also for wxString would only result in
1391 // compilation ambiguities when comparing std::string and wxString
1394 inline bool operator==(const wxString
& s1
, const wxString
& s2
)
1395 { return (s1
.Len() == s2
.Len()) && (s1
.Cmp(s2
) == 0); }
1396 inline bool operator==(const wxString
& s1
, const wxChar
* s2
)
1397 { return s1
.Cmp(s2
) == 0; }
1398 inline bool operator==(const wxChar
* s1
, const wxString
& s2
)
1399 { return s2
.Cmp(s1
) == 0; }
1400 inline bool operator!=(const wxString
& s1
, const wxString
& s2
)
1401 { return (s1
.Len() != s2
.Len()) || (s1
.Cmp(s2
) != 0); }
1402 inline bool operator!=(const wxString
& s1
, const wxChar
* s2
)
1403 { return s1
.Cmp(s2
) != 0; }
1404 inline bool operator!=(const wxChar
* s1
, const wxString
& s2
)
1405 { return s2
.Cmp(s1
) != 0; }
1406 inline bool operator< (const wxString
& s1
, const wxString
& s2
)
1407 { return s1
.Cmp(s2
) < 0; }
1408 inline bool operator< (const wxString
& s1
, const wxChar
* s2
)
1409 { return s1
.Cmp(s2
) < 0; }
1410 inline bool operator< (const wxChar
* s1
, const wxString
& s2
)
1411 { return s2
.Cmp(s1
) > 0; }
1412 inline bool operator> (const wxString
& s1
, const wxString
& s2
)
1413 { return s1
.Cmp(s2
) > 0; }
1414 inline bool operator> (const wxString
& s1
, const wxChar
* s2
)
1415 { return s1
.Cmp(s2
) > 0; }
1416 inline bool operator> (const wxChar
* s1
, const wxString
& s2
)
1417 { return s2
.Cmp(s1
) < 0; }
1418 inline bool operator<=(const wxString
& s1
, const wxString
& s2
)
1419 { return s1
.Cmp(s2
) <= 0; }
1420 inline bool operator<=(const wxString
& s1
, const wxChar
* s2
)
1421 { return s1
.Cmp(s2
) <= 0; }
1422 inline bool operator<=(const wxChar
* s1
, const wxString
& s2
)
1423 { return s2
.Cmp(s1
) >= 0; }
1424 inline bool operator>=(const wxString
& s1
, const wxString
& s2
)
1425 { return s1
.Cmp(s2
) >= 0; }
1426 inline bool operator>=(const wxString
& s1
, const wxChar
* s2
)
1427 { return s1
.Cmp(s2
) >= 0; }
1428 inline bool operator>=(const wxChar
* s1
, const wxString
& s2
)
1429 { return s2
.Cmp(s1
) <= 0; }
1432 inline bool operator==(const wxString
& s1
, const wxWCharBuffer
& s2
)
1433 { return (s1
.Cmp((const wchar_t *)s2
) == 0); }
1434 inline bool operator==(const wxWCharBuffer
& s1
, const wxString
& s2
)
1435 { return (s2
.Cmp((const wchar_t *)s1
) == 0); }
1436 inline bool operator!=(const wxString
& s1
, const wxWCharBuffer
& s2
)
1437 { return (s1
.Cmp((const wchar_t *)s2
) != 0); }
1438 inline bool operator!=(const wxWCharBuffer
& s1
, const wxString
& s2
)
1439 { return (s2
.Cmp((const wchar_t *)s1
) != 0); }
1440 #else // !wxUSE_UNICODE
1441 inline bool operator==(const wxString
& s1
, const wxCharBuffer
& s2
)
1442 { return (s1
.Cmp((const char *)s2
) == 0); }
1443 inline bool operator==(const wxCharBuffer
& s1
, const wxString
& s2
)
1444 { return (s2
.Cmp((const char *)s1
) == 0); }
1445 inline bool operator!=(const wxString
& s1
, const wxCharBuffer
& s2
)
1446 { return (s1
.Cmp((const char *)s2
) != 0); }
1447 inline bool operator!=(const wxCharBuffer
& s1
, const wxString
& s2
)
1448 { return (s2
.Cmp((const char *)s1
) != 0); }
1449 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1451 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
, const wxString
& string2
);
1452 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxChar ch
);
1453 wxString WXDLLIMPEXP_BASE
operator+(wxChar ch
, const wxString
& string
);
1454 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const wxChar
*psz
);
1455 wxString WXDLLIMPEXP_BASE
operator+(const wxChar
*psz
, const wxString
& string
);
1458 inline wxString
operator+(const wxString
& string
, const wxWCharBuffer
& buf
)
1459 { return string
+ (const wchar_t *)buf
; }
1460 inline wxString
operator+(const wxWCharBuffer
& buf
, const wxString
& string
)
1461 { return (const wchar_t *)buf
+ string
; }
1462 #else // !wxUSE_UNICODE
1463 inline wxString
operator+(const wxString
& string
, const wxCharBuffer
& buf
)
1464 { return string
+ (const char *)buf
; }
1465 inline wxString
operator+(const wxCharBuffer
& buf
, const wxString
& string
)
1466 { return (const char *)buf
+ string
; }
1467 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1469 #endif // !wxUSE_STL
1471 // comparison with char (those are not defined by std::[w]string and so should
1472 // be always available)
1473 inline bool operator==(wxChar c
, const wxString
& s
) { return s
.IsSameAs(c
); }
1474 inline bool operator==(const wxString
& s
, wxChar c
) { return s
.IsSameAs(c
); }
1475 inline bool operator!=(wxChar c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
1476 inline bool operator!=(const wxString
& s
, wxChar c
) { return !s
.IsSameAs(c
); }
1478 // ---------------------------------------------------------------------------
1479 // Implementation only from here until the end of file
1480 // ---------------------------------------------------------------------------
1482 // don't pollute the library user's name space
1483 #undef wxASSERT_VALID_INDEX
1485 #if wxUSE_STD_IOSTREAM
1487 #include "wx/iosfwrap.h"
1489 WXDLLIMPEXP_BASE wxSTD istream
& operator>>(wxSTD istream
&, wxString
&);
1490 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxString
&);
1492 #endif // wxSTD_STRING_COMPATIBILITY
1494 #endif // _WX_WXSTRINGH__