1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxString and wxArrayString classes
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
13 Efficient string class [more or less] compatible with MFC CString,
14 wxWidgets version 1 wxString and std::string and some handy functions
15 missing from string.h.
18 #ifndef _WX_WXSTRINGH__
19 #define _WX_WXSTRINGH__
21 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
22 #pragma interface "string.h"
25 // ----------------------------------------------------------------------------
27 // ----------------------------------------------------------------------------
29 #include "wx/defs.h" // everybody should include this
31 #if defined(__WXMAC__) || defined(__VISAGECPP__)
35 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
36 // problem in VACPP V4 with including stdlib.h multiple times
37 // strconv includes it anyway
50 #ifdef HAVE_STRCASECMP_IN_STRINGS_H
51 #include <strings.h> // for strcasecmp()
52 #endif // HAVE_STRCASECMP_IN_STRINGS_H
55 #include <StringMgr.h>
58 #include "wx/wxchar.h" // for wxChar
59 #include "wx/buffer.h" // for wxCharBuffer
60 #include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes
62 class WXDLLIMPEXP_BASE wxString
;
64 // ---------------------------------------------------------------------------
66 // ---------------------------------------------------------------------------
68 // casts [unfortunately!] needed to call some broken functions which require
69 // "char *" instead of "const char *"
70 #define WXSTRINGCAST (wxChar *)(const wxChar *)
71 #define wxCSTRINGCAST (wxChar *)(const wxChar *)
72 #define wxMBSTRINGCAST (char *)(const char *)
73 #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *)
75 // implementation only
76 #define wxASSERT_VALID_INDEX(i) \
77 wxASSERT_MSG( (size_t)(i) <= length(), _T("invalid index in wxString") )
79 // ----------------------------------------------------------------------------
81 // ----------------------------------------------------------------------------
83 // maximum possible length for a string means "take all string" everywhere
84 #define wxSTRING_MAXLEN wxStringBase::npos
86 // ----------------------------------------------------------------------------
88 // ----------------------------------------------------------------------------
90 // global pointer to empty string
91 extern WXDLLIMPEXP_DATA_BASE(const wxChar
*) wxEmptyString
;
93 // ---------------------------------------------------------------------------
94 // global functions complementing standard C string library replacements for
95 // strlen() and portable strcasecmp()
96 //---------------------------------------------------------------------------
98 // Use wxXXX() functions from wxchar.h instead! These functions are for
99 // backwards compatibility only.
101 // checks whether the passed in pointer is NULL and if the string is empty
102 inline bool IsEmpty(const char *p
) { return (!p
|| !*p
); }
104 // safe version of strlen() (returns 0 if passed NULL pointer)
105 inline size_t Strlen(const char *psz
)
106 { return psz
? strlen(psz
) : 0; }
108 // portable strcasecmp/_stricmp
109 inline int Stricmp(const char *psz1
, const char *psz2
)
111 #if defined(__VISUALC__) && defined(__WXWINCE__)
112 register char c1
, c2
;
114 c1
= tolower(*psz1
++);
115 c2
= tolower(*psz2
++);
116 } while ( c1
&& (c1
== c2
) );
119 #elif defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) )
120 return _stricmp(psz1
, psz2
);
121 #elif defined(__SC__)
122 return _stricmp(psz1
, psz2
);
123 #elif defined(__SALFORDC__)
124 return stricmp(psz1
, psz2
);
125 #elif defined(__BORLANDC__)
126 return stricmp(psz1
, psz2
);
127 #elif defined(__WATCOMC__)
128 return stricmp(psz1
, psz2
);
129 #elif defined(__DJGPP__)
130 return stricmp(psz1
, psz2
);
131 #elif defined(__EMX__)
132 return stricmp(psz1
, psz2
);
133 #elif defined(__WXPM__)
134 return stricmp(psz1
, psz2
);
135 #elif defined(__WXPALMOS__) || \
136 defined(HAVE_STRCASECMP_IN_STRING_H) || \
137 defined(HAVE_STRCASECMP_IN_STRINGS_H) || \
138 defined(__GNUWIN32__)
139 return strcasecmp(psz1
, psz2
);
140 #elif defined(__MWERKS__) && !defined(__INTEL__)
141 register char c1
, c2
;
143 c1
= tolower(*psz1
++);
144 c2
= tolower(*psz2
++);
145 } while ( c1
&& (c1
== c2
) );
149 // almost all compilers/libraries provide this function (unfortunately under
150 // different names), that's why we don't implement our own which will surely
151 // be more efficient than this code (uncomment to use):
153 register char c1, c2;
155 c1 = tolower(*psz1++);
156 c2 = tolower(*psz2++);
157 } while ( c1 && (c1 == c2) );
162 #error "Please define string case-insensitive compare for your OS/compiler"
163 #endif // OS/compiler
168 #include "wx/beforestd.h"
170 #include "wx/afterstd.h"
173 #ifdef HAVE_STD_WSTRING
174 typedef std::wstring wxStringBase
;
176 typedef std::basic_string
<wxChar
> wxStringBase
;
179 typedef std::string wxStringBase
;
182 #if (defined(__GNUG__) && (__GNUG__ < 3)) || \
183 (defined(_MSC_VER) && (_MSC_VER <= 1200))
184 #define wxSTRING_BASE_HASNT_CLEAR
187 #else // if !wxUSE_STL
189 #ifndef HAVE_STD_STRING_COMPARE
190 #define HAVE_STD_STRING_COMPARE
193 // ---------------------------------------------------------------------------
194 // string data prepended with some housekeeping info (used by wxString class),
195 // is never used directly (but had to be put here to allow inlining)
196 // ---------------------------------------------------------------------------
198 struct WXDLLIMPEXP_BASE wxStringData
200 int nRefs
; // reference count
201 size_t nDataLength
, // actual string length
202 nAllocLength
; // allocated memory size
204 // mimics declaration 'wxChar data[nAllocLength]'
205 wxChar
* data() const { return (wxChar
*)(this + 1); }
207 // empty string has a special ref count so it's never deleted
208 bool IsEmpty() const { return (nRefs
== -1); }
209 bool IsShared() const { return (nRefs
> 1); }
212 void Lock() { if ( !IsEmpty() ) nRefs
++; }
214 // VC++ will refuse to inline Unlock but profiling shows that it is wrong
215 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
218 // VC++ free must take place in same DLL as allocation when using non dll
219 // run-time library (e.g. Multithreaded instead of Multithreaded DLL)
220 #if defined(__VISUALC__) && defined(_MT) && !defined(_DLL)
221 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) Free(); }
222 // we must not inline deallocation since allocation is not inlined
225 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) free(this); }
228 // if we had taken control over string memory (GetWriteBuf), it's
229 // intentionally put in invalid state
230 void Validate(bool b
) { nRefs
= (b
? 1 : 0); }
231 bool IsValid() const { return (nRefs
!= 0); }
234 class WXDLLIMPEXP_BASE wxStringBase
237 friend class WXDLLIMPEXP_BASE wxArrayString
;
240 // an 'invalid' value for string index, moved to this place due to a CW bug
241 static const size_t npos
;
243 // points to data preceded by wxStringData structure with ref count info
246 // accessor to string data
247 wxStringData
* GetStringData() const { return (wxStringData
*)m_pchData
- 1; }
249 // string (re)initialization functions
250 // initializes the string to the empty value (must be called only from
251 // ctors, use Reinit() otherwise)
252 void Init() { m_pchData
= (wxChar
*)wxEmptyString
; }
253 // initializaes the string with (a part of) C-string
254 void InitWith(const wxChar
*psz
, size_t nPos
= 0, size_t nLen
= npos
);
255 // as Init, but also frees old data
256 void Reinit() { GetStringData()->Unlock(); Init(); }
259 // allocates memory for string of length nLen
260 bool AllocBuffer(size_t nLen
);
261 // copies data to another string
262 bool AllocCopy(wxString
&, int, int) const;
263 // effectively copies data to string
264 bool AssignCopy(size_t, const wxChar
*);
266 // append a (sub)string
267 bool ConcatSelf(size_t nLen
, const wxChar
*src
, size_t nMaxLen
);
268 bool ConcatSelf(size_t nLen
, const wxChar
*src
)
269 { return ConcatSelf(nLen
, src
, nLen
); }
271 // functions called before writing to the string: they copy it if there
272 // are other references to our data (should be the only owner when writing)
273 bool CopyBeforeWrite();
274 bool AllocBeforeWrite(size_t);
276 // compatibility with wxString
277 bool Alloc(size_t nLen
);
280 typedef wxChar value_type
;
281 typedef wxChar char_type
;
282 typedef size_t size_type
;
283 typedef value_type
& reference
;
284 typedef const value_type
& const_reference
;
285 typedef value_type
* pointer
;
286 typedef const value_type
* const_pointer
;
287 typedef value_type
*iterator
;
288 typedef const value_type
*const_iterator
;
290 // constructors and destructor
291 // ctor for an empty string
292 wxStringBase() { Init(); }
294 wxStringBase(const wxStringBase
& stringSrc
)
296 wxASSERT_MSG( stringSrc
.GetStringData()->IsValid(),
297 _T("did you forget to call UngetWriteBuf()?") );
299 if ( stringSrc
.empty() ) {
300 // nothing to do for an empty string
304 m_pchData
= stringSrc
.m_pchData
; // share same data
305 GetStringData()->Lock(); // => one more copy
308 // string containing nRepeat copies of ch
309 wxStringBase(size_type nRepeat
, wxChar ch
);
310 // ctor takes first nLength characters from C string
311 // (default value of npos means take all the string)
312 wxStringBase(const wxChar
*psz
)
313 { InitWith(psz
, 0, npos
); }
314 wxStringBase(const wxChar
*psz
, size_t nLength
)
315 { InitWith(psz
, 0, nLength
); }
316 wxStringBase(const wxChar
*psz
, wxMBConv
& WXUNUSED(conv
), size_t nLength
= npos
)
317 { InitWith(psz
, 0, nLength
); }
318 // take nLen chars starting at nPos
319 wxStringBase(const wxStringBase
& str
, size_t nPos
, size_t nLen
)
321 wxASSERT_MSG( str
.GetStringData()->IsValid(),
322 _T("did you forget to call UngetWriteBuf()?") );
324 size_t strLen
= str
.length() - nPos
; nLen
= strLen
< nLen
? strLen
: nLen
;
325 InitWith(str
.c_str(), nPos
, nLen
);
327 // take all characters from pStart to pEnd
328 wxStringBase(const void *pStart
, const void *pEnd
);
330 // dtor is not virtual, this class must not be inherited from!
333 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
334 //RN - according to the above VC++ does indeed inline this,
335 //even though it spits out two warnings
336 #pragma warning (disable:4714)
339 GetStringData()->Unlock();
342 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
343 //re-enable inlining warning
344 #pragma warning (default:4714)
346 // overloaded assignment
347 // from another wxString
348 wxStringBase
& operator=(const wxStringBase
& stringSrc
);
350 wxStringBase
& operator=(wxChar ch
);
352 wxStringBase
& operator=(const wxChar
*psz
);
354 // return the length of the string
355 size_type
size() const { return GetStringData()->nDataLength
; }
356 // return the length of the string
357 size_type
length() const { return size(); }
358 // return the maximum size of the string
359 size_type
max_size() const { return wxSTRING_MAXLEN
; }
360 // resize the string, filling the space with c if c != 0
361 void resize(size_t nSize
, wxChar ch
= wxT('\0'));
362 // delete the contents of the string
363 void clear() { erase(0, npos
); }
364 // returns true if the string is empty
365 bool empty() const { return size() == 0; }
366 // inform string about planned change in size
367 void reserve(size_t sz
) { Alloc(sz
); }
368 size_type
capacity() const { return GetStringData()->nAllocLength
; }
371 // return the character at position n
372 value_type
at(size_type n
) const
373 { wxASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
374 value_type
operator[](size_type n
) const { return at(n
); }
375 // returns the writable character at position n
376 reference
at(size_type n
)
377 { wxASSERT_VALID_INDEX( n
); CopyBeforeWrite(); return m_pchData
[n
]; }
378 reference
operator[](size_type n
)
379 { wxASSERT_VALID_INDEX( n
); CopyBeforeWrite(); return m_pchData
[n
]; }
381 // lib.string.modifiers
382 // append elements str[pos], ..., str[pos+n]
383 wxStringBase
& append(const wxStringBase
& str
, size_t pos
, size_t n
)
385 wxASSERT(pos
<= str
.length());
386 ConcatSelf(n
, str
.c_str() + pos
, str
.length() - pos
);
390 wxStringBase
& append(const wxStringBase
& str
)
391 { ConcatSelf(str
.length(), str
.c_str()); return *this; }
392 // append first n (or all if n == npos) characters of sz
393 wxStringBase
& append(const wxChar
*sz
)
394 { ConcatSelf(wxStrlen(sz
), sz
); return *this; }
395 wxStringBase
& append(const wxChar
*sz
, size_t n
)
396 { ConcatSelf(n
, sz
); return *this; }
397 // append n copies of ch
398 wxStringBase
& append(size_t n
, wxChar ch
);
399 // append from first to last
400 wxStringBase
& append(const_iterator first
, const_iterator last
)
401 { ConcatSelf(last
- first
, first
); return *this; }
403 // same as `this_string = str'
404 wxStringBase
& assign(const wxStringBase
& str
)
405 { return *this = str
; }
406 // same as ` = str[pos..pos + n]
407 wxStringBase
& assign(const wxStringBase
& str
, size_t pos
, size_t n
)
408 { clear(); return append(str
, pos
, n
); }
409 // same as `= first n (or all if n == npos) characters of sz'
410 wxStringBase
& assign(const wxChar
*sz
)
411 { clear(); return append(sz
, wxStrlen(sz
)); }
412 wxStringBase
& assign(const wxChar
*sz
, size_t n
)
413 { clear(); return append(sz
, n
); }
414 // same as `= n copies of ch'
415 wxStringBase
& assign(size_t n
, wxChar ch
)
416 { clear(); return append(n
, ch
); }
417 // assign from first to last
418 wxStringBase
& assign(const_iterator first
, const_iterator last
)
419 { clear(); return append(first
, last
); }
421 // first valid index position
422 const_iterator
begin() const { return m_pchData
; }
423 // position one after the last valid one
424 const_iterator
end() const { return m_pchData
+ length(); }
426 // first valid index position
428 // position one after the last valid one
431 // insert another string
432 wxStringBase
& insert(size_t nPos
, const wxStringBase
& str
)
434 wxASSERT( str
.GetStringData()->IsValid() );
435 return insert(nPos
, str
.c_str(), str
.length());
437 // insert n chars of str starting at nStart (in str)
438 wxStringBase
& insert(size_t nPos
, const wxStringBase
& str
, size_t nStart
, size_t n
)
440 wxASSERT( str
.GetStringData()->IsValid() );
441 wxASSERT( nStart
< str
.length() );
442 size_t strLen
= str
.length() - nStart
;
443 n
= strLen
< n
? strLen
: n
;
444 return insert(nPos
, str
.c_str() + nStart
, n
);
446 // insert first n (or all if n == npos) characters of sz
447 wxStringBase
& insert(size_t nPos
, const wxChar
*sz
, size_t n
= npos
);
448 // insert n copies of ch
449 wxStringBase
& insert(size_t nPos
, size_t n
, wxChar ch
)
450 { return insert(nPos
, wxStringBase(n
, ch
)); }
451 iterator
insert(iterator it
, wxChar ch
)
452 { size_t idx
= it
- begin(); insert(idx
, 1, ch
); return begin() + idx
; }
453 void insert(iterator it
, const_iterator first
, const_iterator last
)
454 { insert(it
- begin(), first
, last
- first
); }
455 void insert(iterator it
, size_type n
, wxChar ch
)
456 { insert(it
- begin(), n
, ch
); }
458 // delete characters from nStart to nStart + nLen
459 wxStringBase
& erase(size_type pos
= 0, size_type n
= npos
);
460 iterator
erase(iterator first
, iterator last
)
462 size_t idx
= first
- begin();
463 erase(idx
, last
- first
);
464 return begin() + idx
;
466 iterator
erase(iterator first
);
468 // explicit conversion to C string (use this with printf()!)
469 const wxChar
* c_str() const { return m_pchData
; }
470 const wxChar
* data() const { return m_pchData
; }
472 // replaces the substring of length nLen starting at nStart
473 wxStringBase
& replace(size_t nStart
, size_t nLen
, const wxChar
* sz
);
474 // replaces the substring of length nLen starting at nStart
475 wxStringBase
& replace(size_t nStart
, size_t nLen
, const wxStringBase
& str
)
476 { return replace(nStart
, nLen
, str
.c_str()); }
477 // replaces the substring with nCount copies of ch
478 wxStringBase
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxChar ch
);
479 // replaces a substring with another substring
480 wxStringBase
& replace(size_t nStart
, size_t nLen
,
481 const wxStringBase
& str
, size_t nStart2
, size_t nLen2
);
482 // replaces the substring with first nCount chars of sz
483 wxStringBase
& replace(size_t nStart
, size_t nLen
,
484 const wxChar
* sz
, size_t nCount
);
485 wxStringBase
& replace(iterator first
, iterator last
, const_pointer s
)
486 { return replace(first
- begin(), last
- first
, s
); }
487 wxStringBase
& replace(iterator first
, iterator last
, const_pointer s
,
489 { return replace(first
- begin(), last
- first
, s
, n
); }
490 wxStringBase
& replace(iterator first
, iterator last
, const wxStringBase
& s
)
491 { return replace(first
- begin(), last
- first
, s
); }
492 wxStringBase
& replace(iterator first
, iterator last
, size_type n
, wxChar c
)
493 { return replace(first
- begin(), last
- first
, n
, c
); }
494 wxStringBase
& replace(iterator first
, iterator last
,
495 const_iterator first1
, const_iterator last1
)
496 { return replace(first
- begin(), last
- first
, first1
, last1
- first1
); }
499 void swap(wxStringBase
& str
);
501 // All find() functions take the nStart argument which specifies the
502 // position to start the search on, the default value is 0. All functions
503 // return npos if there were no match.
506 size_t find(const wxStringBase
& str
, size_t nStart
= 0) const;
508 // VC++ 1.5 can't cope with this syntax.
509 #if !defined(__VISUALC__) || defined(__WIN32__)
510 // find first n characters of sz
511 size_t find(const wxChar
* sz
, size_t nStart
= 0, size_t n
= npos
) const;
514 // find the first occurence of character ch after nStart
515 size_t find(wxChar ch
, size_t nStart
= 0) const;
517 // rfind() family is exactly like find() but works right to left
519 // as find, but from the end
520 size_t rfind(const wxStringBase
& str
, size_t nStart
= npos
) const;
522 // VC++ 1.5 can't cope with this syntax.
523 // as find, but from the end
524 size_t rfind(const wxChar
* sz
, size_t nStart
= npos
,
525 size_t n
= npos
) const;
526 // as find, but from the end
527 size_t rfind(wxChar ch
, size_t nStart
= npos
) const;
529 // find first/last occurence of any character in the set
531 // as strpbrk() but starts at nStart, returns npos if not found
532 size_t find_first_of(const wxStringBase
& str
, size_t nStart
= 0) const
533 { return find_first_of(str
.c_str(), nStart
); }
535 size_t find_first_of(const wxChar
* sz
, size_t nStart
= 0) const;
536 size_t find_first_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
537 // same as find(char, size_t)
538 size_t find_first_of(wxChar c
, size_t nStart
= 0) const
539 { return find(c
, nStart
); }
540 // find the last (starting from nStart) char from str in this string
541 size_t find_last_of (const wxStringBase
& str
, size_t nStart
= npos
) const
542 { return find_last_of(str
.c_str(), nStart
); }
544 size_t find_last_of (const wxChar
* sz
, size_t nStart
= npos
) const;
545 size_t find_last_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
547 size_t find_last_of(wxChar c
, size_t nStart
= npos
) const
548 { return rfind(c
, nStart
); }
550 // find first/last occurence of any character not in the set
552 // as strspn() (starting from nStart), returns npos on failure
553 size_t find_first_not_of(const wxStringBase
& str
, size_t nStart
= 0) const
554 { return find_first_not_of(str
.c_str(), nStart
); }
556 size_t find_first_not_of(const wxChar
* sz
, size_t nStart
= 0) const;
557 size_t find_first_not_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
559 size_t find_first_not_of(wxChar ch
, size_t nStart
= 0) const;
561 size_t find_last_not_of(const wxStringBase
& str
, size_t nStart
= npos
) const
562 { return find_last_not_of(str
.c_str(), nStart
); }
564 size_t find_last_not_of(const wxChar
* sz
, size_t nStart
= npos
) const;
565 size_t find_last_not_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
567 size_t find_last_not_of(wxChar ch
, size_t nStart
= npos
) const;
569 // All compare functions return -1, 0 or 1 if the [sub]string is less,
570 // equal or greater than the compare() argument.
572 // comparison with another string
573 int compare(const wxStringBase
& str
) const;
574 // comparison with a substring
575 int compare(size_t nStart
, size_t nLen
, const wxStringBase
& str
) const;
576 // comparison of 2 substrings
577 int compare(size_t nStart
, size_t nLen
,
578 const wxStringBase
& str
, size_t nStart2
, size_t nLen2
) const;
579 // comparison with a c string
580 int compare(const wxChar
* sz
) const;
581 // substring comparison with first nCount characters of sz
582 int compare(size_t nStart
, size_t nLen
,
583 const wxChar
* sz
, size_t nCount
= npos
) const;
585 size_type
copy(wxChar
* s
, size_type n
, size_type pos
= 0);
587 // substring extraction
588 wxStringBase
substr(size_t nStart
= 0, size_t nLen
= npos
) const;
591 wxStringBase
& operator+=(const wxStringBase
& s
) { return append(s
); }
592 // string += C string
593 wxStringBase
& operator+=(const wxChar
*psz
) { return append(psz
); }
595 wxStringBase
& operator+=(wxChar ch
) { return append(1, ch
); }
600 // ----------------------------------------------------------------------------
601 // wxString: string class trying to be compatible with std::string, MFC
602 // CString and wxWindows 1.x wxString all at once
603 // ---------------------------------------------------------------------------
605 class WXDLLIMPEXP_BASE wxString
: public wxStringBase
608 friend class WXDLLIMPEXP_BASE wxArrayString
;
611 // NB: special care was taken in arranging the member functions in such order
612 // that all inline functions can be effectively inlined, verify that all
613 // performace critical functions are still inlined if you change order!
615 // if we hadn't made these operators private, it would be possible to
616 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
617 // converted to char in C and we do have operator=(char)
619 // NB: we don't need other versions (short/long and unsigned) as attempt
620 // to assign another numeric type to wxString will now result in
621 // ambiguity between operator=(char) and operator=(int)
622 wxString
& operator=(int);
624 // these methods are not implemented - there is _no_ conversion from int to
625 // string, you're doing something wrong if the compiler wants to call it!
627 // try `s << i' or `s.Printf("%d", i)' instead
631 // constructors and destructor
632 // ctor for an empty string
633 wxString() : wxStringBase() { }
635 wxString(const wxStringBase
& stringSrc
) : wxStringBase(stringSrc
) { }
636 wxString(const wxString
& stringSrc
) : wxStringBase(stringSrc
) { }
637 // string containing nRepeat copies of ch
638 wxString(wxChar ch
, size_t nRepeat
= 1)
639 : wxStringBase(nRepeat
, ch
) { }
640 wxString(size_t nRepeat
, wxChar ch
)
641 : wxStringBase(nRepeat
, ch
) { }
642 // ctor takes first nLength characters from C string
643 // (default value of npos means take all the string)
644 wxString(const wxChar
*psz
)
645 : wxStringBase(psz
? psz
: wxT("")) { }
646 wxString(const wxChar
*psz
, size_t nLength
)
647 : wxStringBase(psz
, nLength
) { }
648 wxString(const wxChar
*psz
, wxMBConv
& WXUNUSED(conv
), size_t nLength
= npos
)
649 : wxStringBase(psz
, nLength
== npos
? wxStrlen(psz
) : nLength
) { }
652 // from multibyte string
653 wxString(const char *psz
, wxMBConv
& conv
, size_t nLength
= npos
);
654 // from wxWCharBuffer (i.e. return from wxGetString)
655 wxString(const wxWCharBuffer
& psz
) : wxStringBase(psz
.data()) { }
657 // from C string (for compilers using unsigned char)
658 wxString(const unsigned char* psz
, size_t nLength
= npos
)
659 : wxStringBase((const char*)psz
, nLength
) { }
662 // from wide (Unicode) string
663 wxString(const wchar_t *pwz
, wxMBConv
& conv
= wxConvLibc
, size_t nLength
= npos
);
664 #endif // !wxUSE_WCHAR_T
667 wxString(const wxCharBuffer
& psz
)
668 : wxStringBase(psz
) { }
669 #endif // Unicode/ANSI
671 // generic attributes & operations
672 // as standard strlen()
673 size_t Len() const { return length(); }
674 // string contains any characters?
675 bool IsEmpty() const { return empty(); }
676 // empty string is "false", so !str will return true
677 bool operator!() const { return IsEmpty(); }
678 // truncate the string to given length
679 wxString
& Truncate(size_t uiLen
);
680 // empty string contents
685 wxASSERT_MSG( IsEmpty(), _T("string not empty after call to Empty()?") );
687 // empty the string and free memory
690 wxString
tmp(wxEmptyString
);
696 bool IsAscii() const;
698 bool IsNumber() const;
702 // data access (all indexes are 0 based)
704 wxChar
GetChar(size_t n
) const
705 { return operator[](n
); }
707 wxChar
& GetWritableChar(size_t n
)
708 { return operator[](n
); }
710 void SetChar(size_t n
, wxChar ch
)
711 { operator[](n
) = ch
; }
713 // get last character
716 wxASSERT_MSG( !IsEmpty(), _T("wxString: index out of bounds") );
718 return operator[](length() - 1);
721 // get writable last character
724 wxASSERT_MSG( !IsEmpty(), _T("wxString: index out of bounds") );
725 return operator[](length() - 1);
729 So why do we have all these overloaded operator[]s? A bit of history:
730 initially there was only one of them, taking size_t. Then people
731 started complaining because they wanted to use ints as indices (I
732 wonder why) and compilers were giving warnings about it, so we had to
733 add the operator[](int). Then it became apparent that you couldn't
734 write str[0] any longer because there was ambiguity between two
735 overloads and so you now had to write str[0u] (or, of course, use the
736 explicit casts to either int or size_t but nobody did this).
738 Finally, someone decided to compile wxWin on an Alpha machine and got
739 a surprize: str[0u] didn't compile there because it is of type
740 unsigned int and size_t is unsigned _long_ on Alpha and so there was
741 ambiguity between converting uint to int or ulong. To fix this one we
742 now add operator[](uint) for the machines where size_t is not already
743 the same as unsigned int - hopefully this fixes the problem (for some
746 The only real fix is, of course, to remove all versions but the one
750 // operator version of GetChar
751 wxChar
operator[](int n
) const
752 { return wxStringBase::operator[](n
); }
753 wxChar
& operator[](size_type n
)
754 { return wxStringBase::operator[](n
); }
755 wxChar
operator[](size_type n
) const
756 { return wxStringBase::operator[](n
); }
757 #ifndef wxSIZE_T_IS_UINT
758 // operator version of GetChar
759 wxChar
operator[](unsigned int n
) const
760 { return wxStringBase::operator[](n
); }
762 // operator version of GetWriteableChar
763 wxChar
& operator[](unsigned int n
)
764 { return wxStringBase::operator[](n
); }
765 #endif // size_t != unsigned int
767 // implicit conversion to C string
768 operator const wxChar
*() const { return c_str(); }
770 // identical to c_str(), for wxWin 1.6x compatibility
771 const wxChar
* wx_str() const { return c_str(); }
772 // identical to c_str(), for MFC compatibility
773 const wxChar
* GetData() const { return c_str(); }
775 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
776 // converting numbers or strings which are certain not to contain special
777 // chars (typically system functions, X atoms, environment variables etc.)
779 // the behaviour of these functions with the strings containing anything
780 // else than 7 bit ASCII characters is undefined, use at your own risk.
782 static wxString
FromAscii(const char *ascii
); // string
783 static wxString
FromAscii(const char ascii
); // char
784 const wxCharBuffer
ToAscii() const;
786 static wxString
FromAscii(const char *ascii
) { return wxString( ascii
); }
787 static wxString
FromAscii(const char ascii
) { return wxString( ascii
); }
788 const char *ToAscii() const { return c_str(); }
789 #endif // Unicode/!Unicode
791 // conversions with (possible) format conversions: have to return a
792 // buffer with temporary data
794 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
795 // return an ANSI (multibyte) string, wc_str() to return a wide string and
796 // fn_str() to return a string which should be used with the OS APIs
797 // accepting the file names. The return value is always the same, but the
798 // type differs because a function may either return pointer to the buffer
799 // directly or have to use intermediate buffer for translation.
801 const wxCharBuffer
mb_str(wxMBConv
& conv
= wxConvLibc
) const;
803 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
805 const wxChar
* wc_str() const { return c_str(); }
807 // for compatibility with !wxUSE_UNICODE version
808 const wxChar
* wc_str(wxMBConv
& WXUNUSED(conv
)) const { return c_str(); }
811 const wxCharBuffer
fn_str() const { return mb_str(wxConvFile
); }
813 const wxChar
* fn_str() const { return c_str(); }
814 #endif // wxMBFILES/!wxMBFILES
816 const wxChar
* mb_str() const { return c_str(); }
818 // for compatibility with wxUSE_UNICODE version
819 const wxChar
* mb_str(wxMBConv
& WXUNUSED(conv
)) const { return c_str(); }
821 const wxWX2MBbuf
mbc_str() const { return mb_str(); }
824 const wxWCharBuffer
wc_str(wxMBConv
& conv
) const;
825 #endif // wxUSE_WCHAR_T
827 const wxCharBuffer
fn_str() const { return wxConvFile
.cWC2WX( wc_str( wxConvLocal
) ); }
829 const wxChar
* fn_str() const { return c_str(); }
831 #endif // Unicode/ANSI
833 // overloaded assignment
834 // from another wxString
835 wxString
& operator=(const wxStringBase
& stringSrc
)
836 { return (wxString
&)wxStringBase::operator=(stringSrc
); }
838 wxString
& operator=(wxChar ch
)
839 { return (wxString
&)wxStringBase::operator=(ch
); }
841 wxString
& operator=(const wxChar
*psz
)
842 { return (wxString
&)wxStringBase::operator=(psz
); }
844 // from wxWCharBuffer
845 wxString
& operator=(const wxWCharBuffer
& psz
)
846 { (void) operator=((const wchar_t *)psz
); return *this; }
848 // from another kind of C string
849 wxString
& operator=(const unsigned char* psz
);
851 // from a wide string
852 wxString
& operator=(const wchar_t *pwz
);
855 wxString
& operator=(const wxCharBuffer
& psz
)
856 { (void) operator=((const char *)psz
); return *this; }
857 #endif // Unicode/ANSI
859 // string concatenation
860 // in place concatenation
862 Concatenate and return the result. Note that the left to right
863 associativity of << allows to write things like "str << str1 << str2
864 << ..." (unlike with +=)
867 wxString
& operator<<(const wxString
& s
)
870 wxASSERT_MSG( s
.GetStringData()->IsValid(),
871 _T("did you forget to call UngetWriteBuf()?") );
877 // string += C string
878 wxString
& operator<<(const wxChar
*psz
)
879 { append(psz
); return *this; }
881 wxString
& operator<<(wxChar ch
) { append(1, ch
); return *this; }
883 // string += buffer (i.e. from wxGetString)
885 wxString
& operator<<(const wxWCharBuffer
& s
)
886 { (void)operator<<((const wchar_t *)s
); return *this; }
887 void operator+=(const wxWCharBuffer
& s
)
888 { (void)operator<<((const wchar_t *)s
); }
889 #else // !wxUSE_UNICODE
890 wxString
& operator<<(const wxCharBuffer
& s
)
891 { (void)operator<<((const char *)s
); return *this; }
892 void operator+=(const wxCharBuffer
& s
)
893 { (void)operator<<((const char *)s
); }
894 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
896 // string += C string
897 wxString
& Append(const wxString
& s
)
899 // test for IsEmpty() to share the string if possible
906 wxString
& Append(const wxChar
* psz
)
907 { append(psz
); return *this; }
908 // append count copies of given character
909 wxString
& Append(wxChar ch
, size_t count
= 1u)
910 { append(count
, ch
); return *this; }
911 wxString
& Append(const wxChar
* psz
, size_t nLen
)
912 { append(psz
, nLen
); return *this; }
914 // prepend a string, return the string itself
915 wxString
& Prepend(const wxString
& str
)
916 { *this = str
+ *this; return *this; }
918 // non-destructive concatenation
920 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
, const wxString
& string2
);
922 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxChar ch
);
924 friend wxString WXDLLIMPEXP_BASE
operator+(wxChar ch
, const wxString
& string
);
926 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const wxChar
*psz
);
928 friend wxString WXDLLIMPEXP_BASE
operator+(const wxChar
*psz
, const wxString
& string
);
930 // stream-like functions
931 // insert an int into string
932 wxString
& operator<<(int i
)
933 { return (*this) << Format(_T("%d"), i
); }
934 // insert an unsigned int into string
935 wxString
& operator<<(unsigned int ui
)
936 { return (*this) << Format(_T("%u"), ui
); }
937 // insert a long into string
938 wxString
& operator<<(long l
)
939 { return (*this) << Format(_T("%ld"), l
); }
940 // insert an unsigned long into string
941 wxString
& operator<<(unsigned long ul
)
942 { return (*this) << Format(_T("%lu"), ul
); }
943 // insert a float into string
944 wxString
& operator<<(float f
)
945 { return (*this) << Format(_T("%f"), f
); }
946 // insert a double into string
947 wxString
& operator<<(double d
)
948 { return (*this) << Format(_T("%g"), d
); }
951 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
952 int Cmp(const wxChar
*psz
) const;
953 int Cmp(const wxString
& s
) const;
954 // same as Cmp() but not case-sensitive
955 int CmpNoCase(const wxChar
*psz
) const;
956 int CmpNoCase(const wxString
& s
) const;
957 // test for the string equality, either considering case or not
958 // (if compareWithCase then the case matters)
959 bool IsSameAs(const wxChar
*psz
, bool compareWithCase
= true) const
960 { return (compareWithCase
? Cmp(psz
) : CmpNoCase(psz
)) == 0; }
961 // comparison with a signle character: returns true if equal
962 bool IsSameAs(wxChar c
, bool compareWithCase
= true) const
964 return (length() == 1) && (compareWithCase
? GetChar(0u) == c
965 : wxToupper(GetChar(0u)) == wxToupper(c
));
968 // simple sub-string extraction
969 // return substring starting at nFirst of length nCount (or till the end
970 // if nCount = default value)
971 wxString
Mid(size_t nFirst
, size_t nCount
= npos
) const;
973 // operator version of Mid()
974 wxString
operator()(size_t start
, size_t len
) const
975 { return Mid(start
, len
); }
977 // check that the string starts with prefix and return the rest of the
978 // string in the provided pointer if it is not NULL, otherwise return
980 bool StartsWith(const wxChar
*prefix
, wxString
*rest
= NULL
) const;
982 // get first nCount characters
983 wxString
Left(size_t nCount
) const;
984 // get last nCount characters
985 wxString
Right(size_t nCount
) const;
986 // get all characters before the first occurance of ch
987 // (returns the whole string if ch not found)
988 wxString
BeforeFirst(wxChar ch
) const;
989 // get all characters before the last occurence of ch
990 // (returns empty string if ch not found)
991 wxString
BeforeLast(wxChar ch
) const;
992 // get all characters after the first occurence of ch
993 // (returns empty string if ch not found)
994 wxString
AfterFirst(wxChar ch
) const;
995 // get all characters after the last occurence of ch
996 // (returns the whole string if ch not found)
997 wxString
AfterLast(wxChar ch
) const;
999 // for compatibility only, use more explicitly named functions above
1000 wxString
Before(wxChar ch
) const { return BeforeLast(ch
); }
1001 wxString
After(wxChar ch
) const { return AfterFirst(ch
); }
1004 // convert to upper case in place, return the string itself
1005 wxString
& MakeUpper();
1006 // convert to upper case, return the copy of the string
1007 // Here's something to remember: BC++ doesn't like returns in inlines.
1008 wxString
Upper() const ;
1009 // convert to lower case in place, return the string itself
1010 wxString
& MakeLower();
1011 // convert to lower case, return the copy of the string
1012 wxString
Lower() const ;
1014 // trimming/padding whitespace (either side) and truncating
1015 // remove spaces from left or from right (default) side
1016 wxString
& Trim(bool bFromRight
= true);
1017 // add nCount copies chPad in the beginning or at the end (default)
1018 wxString
& Pad(size_t nCount
, wxChar chPad
= wxT(' '), bool bFromRight
= true);
1020 // searching and replacing
1021 // searching (return starting index, or -1 if not found)
1022 int Find(wxChar ch
, bool bFromEnd
= false) const; // like strchr/strrchr
1023 // searching (return starting index, or -1 if not found)
1024 int Find(const wxChar
*pszSub
) const; // like strstr
1025 // replace first (or all of bReplaceAll) occurences of substring with
1026 // another string, returns the number of replacements made
1027 size_t Replace(const wxChar
*szOld
,
1028 const wxChar
*szNew
,
1029 bool bReplaceAll
= true);
1031 // check if the string contents matches a mask containing '*' and '?'
1032 bool Matches(const wxChar
*szMask
) const;
1034 // conversion to numbers: all functions return true only if the whole
1035 // string is a number and put the value of this number into the pointer
1036 // provided, the base is the numeric base in which the conversion should be
1037 // done and must be comprised between 2 and 36 or be 0 in which case the
1038 // standard C rules apply (leading '0' => octal, "0x" => hex)
1039 // convert to a signed integer
1040 bool ToLong(long *val
, int base
= 10) const;
1041 // convert to an unsigned integer
1042 bool ToULong(unsigned long *val
, int base
= 10) const;
1043 // convert to a double
1044 bool ToDouble(double *val
) const;
1046 // formated input/output
1047 // as sprintf(), returns the number of characters written or < 0 on error
1048 // (take 'this' into account in attribute parameter count)
1049 int Printf(const wxChar
*pszFormat
, ...) ATTRIBUTE_PRINTF_2
;
1050 // as vprintf(), returns the number of characters written or < 0 on error
1051 int PrintfV(const wxChar
* pszFormat
, va_list argptr
);
1053 // returns the string containing the result of Printf() to it
1054 static wxString
Format(const wxChar
*pszFormat
, ...) ATTRIBUTE_PRINTF_1
;
1055 // the same as above, but takes a va_list
1056 static wxString
FormatV(const wxChar
*pszFormat
, va_list argptr
);
1058 // raw access to string memory
1059 // ensure that string has space for at least nLen characters
1060 // only works if the data of this string is not shared
1061 bool Alloc(size_t nLen
) { reserve(nLen
); /*return capacity() >= nLen;*/ return true; }
1062 // minimize the string's memory
1063 // only works if the data of this string is not shared
1066 // get writable buffer of at least nLen bytes. Unget() *must* be called
1067 // a.s.a.p. to put string back in a reasonable state!
1068 wxChar
*GetWriteBuf(size_t nLen
);
1069 // call this immediately after GetWriteBuf() has been used
1070 void UngetWriteBuf();
1071 void UngetWriteBuf(size_t nLen
);
1074 // wxWidgets version 1 compatibility functions
1077 wxString
SubString(size_t from
, size_t to
) const
1078 { return Mid(from
, (to
- from
+ 1)); }
1079 // values for second parameter of CompareTo function
1080 enum caseCompare
{exact
, ignoreCase
};
1081 // values for first parameter of Strip function
1082 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
1085 // (take 'this' into account in attribute parameter count)
1086 int sprintf(const wxChar
*pszFormat
, ...) ATTRIBUTE_PRINTF_2
;
1089 inline int CompareTo(const wxChar
* psz
, caseCompare cmp
= exact
) const
1090 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
1093 size_t Length() const { return length(); }
1094 // Count the number of characters
1095 int Freq(wxChar ch
) const;
1097 void LowerCase() { MakeLower(); }
1099 void UpperCase() { MakeUpper(); }
1100 // use Trim except that it doesn't change this string
1101 wxString
Strip(stripType w
= trailing
) const;
1103 // use Find (more general variants not yet supported)
1104 size_t Index(const wxChar
* psz
) const { return Find(psz
); }
1105 size_t Index(wxChar ch
) const { return Find(ch
); }
1107 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
1108 wxString
& RemoveLast(size_t n
= 1) { return Truncate(length() - n
); }
1110 wxString
& Remove(size_t nStart
, size_t nLen
)
1111 { return (wxString
&)erase( nStart
, nLen
); }
1114 int First( const wxChar ch
) const { return Find(ch
); }
1115 int First( const wxChar
* psz
) const { return Find(psz
); }
1116 int First( const wxString
&str
) const { return Find(str
); }
1117 int Last( const wxChar ch
) const { return Find(ch
, true); }
1118 bool Contains(const wxString
& str
) const { return Find(str
) != wxNOT_FOUND
; }
1121 bool IsNull() const { return IsEmpty(); }
1123 // std::string compatibility functions
1125 // take nLen chars starting at nPos
1126 wxString(const wxString
& str
, size_t nPos
, size_t nLen
)
1127 : wxStringBase(str
, nPos
, nLen
) { }
1128 // take all characters from pStart to pEnd
1129 wxString(const void *pStart
, const void *pEnd
)
1130 : wxStringBase((const wxChar
*)pStart
, (const wxChar
*)pEnd
) { }
1132 wxString(const_iterator first
, const_iterator last
)
1133 : wxStringBase(first
, last
) { }
1136 // lib.string.modifiers
1137 // append elements str[pos], ..., str[pos+n]
1138 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
1139 { return (wxString
&)wxStringBase::append(str
, pos
, n
); }
1141 wxString
& append(const wxString
& str
)
1142 { return (wxString
&)wxStringBase::append(str
); }
1143 // append first n (or all if n == npos) characters of sz
1144 wxString
& append(const wxChar
*sz
)
1145 { return (wxString
&)wxStringBase::append(sz
); }
1146 wxString
& append(const wxChar
*sz
, size_t n
)
1147 { return (wxString
&)wxStringBase::append(sz
, n
); }
1148 // append n copies of ch
1149 wxString
& append(size_t n
, wxChar ch
)
1150 { return (wxString
&)wxStringBase::append(n
, ch
); }
1151 // append from first to last
1152 wxString
& append(const_iterator first
, const_iterator last
)
1153 { return (wxString
&)wxStringBase::append(first
, last
); }
1155 // same as `this_string = str'
1156 wxString
& assign(const wxString
& str
)
1157 { return (wxString
&)wxStringBase::assign(str
); }
1158 // same as ` = str[pos..pos + n]
1159 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
1160 { return (wxString
&)wxStringBase::assign(str
, pos
, n
); }
1161 // same as `= first n (or all if n == npos) characters of sz'
1162 wxString
& assign(const wxChar
*sz
)
1163 { return (wxString
&)wxStringBase::assign(sz
); }
1164 wxString
& assign(const wxChar
*sz
, size_t n
)
1165 { return (wxString
&)wxStringBase::assign(sz
, n
); }
1166 // same as `= n copies of ch'
1167 wxString
& assign(size_t n
, wxChar ch
)
1168 { return (wxString
&)wxStringBase::assign(n
, ch
); }
1169 // assign from first to last
1170 wxString
& assign(const_iterator first
, const_iterator last
)
1171 { return (wxString
&)wxStringBase::assign(first
, last
); }
1173 // string comparison
1174 #ifndef HAVE_STD_STRING_COMPARE
1175 int compare(const wxStringBase
& str
) const;
1176 // comparison with a substring
1177 int compare(size_t nStart
, size_t nLen
, const wxStringBase
& str
) const;
1178 // comparison of 2 substrings
1179 int compare(size_t nStart
, size_t nLen
,
1180 const wxStringBase
& str
, size_t nStart2
, size_t nLen2
) const;
1181 // just like strcmp()
1182 int compare(const wxChar
* sz
) const;
1183 // substring comparison with first nCount characters of sz
1184 int compare(size_t nStart
, size_t nLen
,
1185 const wxChar
* sz
, size_t nCount
= npos
) const;
1186 #endif // !defined HAVE_STD_STRING_COMPARE
1188 // insert another string
1189 wxString
& insert(size_t nPos
, const wxString
& str
)
1190 { return (wxString
&)wxStringBase::insert(nPos
, str
); }
1191 // insert n chars of str starting at nStart (in str)
1192 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
1193 { return (wxString
&)wxStringBase::insert(nPos
, str
, nStart
, n
); }
1194 // insert first n (or all if n == npos) characters of sz
1195 wxString
& insert(size_t nPos
, const wxChar
*sz
)
1196 { return (wxString
&)wxStringBase::insert(nPos
, sz
); }
1197 wxString
& insert(size_t nPos
, const wxChar
*sz
, size_t n
)
1198 { return (wxString
&)wxStringBase::insert(nPos
, sz
, n
); }
1199 // insert n copies of ch
1200 wxString
& insert(size_t nPos
, size_t n
, wxChar ch
)
1201 { return (wxString
&)wxStringBase::insert(nPos
, n
, ch
); }
1202 iterator
insert(iterator it
, wxChar ch
)
1203 { return wxStringBase::insert(it
, ch
); }
1204 void insert(iterator it
, const_iterator first
, const_iterator last
)
1205 { wxStringBase::insert(it
, first
, last
); }
1206 void insert(iterator it
, size_type n
, wxChar ch
)
1207 { wxStringBase::insert(it
, n
, ch
); }
1209 // delete characters from nStart to nStart + nLen
1210 wxString
& erase(size_type pos
= 0, size_type n
= npos
)
1211 { return (wxString
&)wxStringBase::erase(pos
, n
); }
1212 iterator
erase(iterator first
, iterator last
)
1213 { return wxStringBase::erase(first
, last
); }
1214 iterator
erase(iterator first
)
1215 { return wxStringBase::erase(first
); }
1217 #ifdef wxSTRING_BASE_HASNT_CLEAR
1218 void clear() { erase(); }
1221 // replaces the substring of length nLen starting at nStart
1222 wxString
& replace(size_t nStart
, size_t nLen
, const wxChar
* sz
)
1223 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, sz
); }
1224 // replaces the substring of length nLen starting at nStart
1225 wxString
& replace(size_t nStart
, size_t nLen
, const wxString
& str
)
1226 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, str
); }
1227 // replaces the substring with nCount copies of ch
1228 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxChar ch
)
1229 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, nCount
, ch
); }
1230 // replaces a substring with another substring
1231 wxString
& replace(size_t nStart
, size_t nLen
,
1232 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1233 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, str
,
1235 // replaces the substring with first nCount chars of sz
1236 wxString
& replace(size_t nStart
, size_t nLen
,
1237 const wxChar
* sz
, size_t nCount
)
1238 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, sz
, nCount
); }
1239 wxString
& replace(iterator first
, iterator last
, const_pointer s
)
1240 { return (wxString
&)wxStringBase::replace(first
, last
, s
); }
1241 wxString
& replace(iterator first
, iterator last
, const_pointer s
,
1243 { return (wxString
&)wxStringBase::replace(first
, last
, s
, n
); }
1244 wxString
& replace(iterator first
, iterator last
, const wxString
& s
)
1245 { return (wxString
&)wxStringBase::replace(first
, last
, s
); }
1246 wxString
& replace(iterator first
, iterator last
, size_type n
, wxChar c
)
1247 { return (wxString
&)wxStringBase::replace(first
, last
, n
, c
); }
1248 wxString
& replace(iterator first
, iterator last
,
1249 const_iterator first1
, const_iterator last1
)
1250 { return (wxString
&)wxStringBase::replace(first
, last
, first1
, last1
); }
1253 wxString
& operator+=(const wxString
& s
)
1254 { return (wxString
&)wxStringBase::operator+=(s
); }
1255 // string += C string
1256 wxString
& operator+=(const wxChar
*psz
)
1257 { return (wxString
&)wxStringBase::operator+=(psz
); }
1259 wxString
& operator+=(wxChar ch
)
1260 { return (wxString
&)wxStringBase::operator+=(ch
); }
1263 // define wxArrayString, for compatibility
1264 #if WXWIN_COMPATIBILITY_2_4 && !wxUSE_STL
1265 #include "wx/arrstr.h"
1269 // return an empty wxString (not very useful with wxUSE_STL == 1)
1270 inline const wxString
wxGetEmptyString() { return wxString(); }
1272 // return an empty wxString (more efficient than wxString() here)
1273 inline const wxString
& wxGetEmptyString()
1275 return *(wxString
*)&wxEmptyString
;
1277 #endif // wxUSE_STL/!wxUSE_STL
1279 // ----------------------------------------------------------------------------
1280 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
1281 // ----------------------------------------------------------------------------
1285 class WXDLLIMPEXP_BASE wxStringBuffer
1288 wxStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
1289 : m_str(str
), m_buf(lenWanted
)
1292 ~wxStringBuffer() { m_str
.assign(m_buf
.data(), wxStrlen(m_buf
.data())); }
1294 operator wxChar
*() { return m_buf
.data(); }
1299 wxWCharBuffer m_buf
;
1304 DECLARE_NO_COPY_CLASS(wxStringBuffer
)
1307 class WXDLLIMPEXP_BASE wxStringBufferLength
1310 wxStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
1311 : m_str(str
), m_buf(lenWanted
), m_len(0), m_lenSet(false)
1314 ~wxStringBufferLength()
1317 m_str
.assign(m_buf
.data(), m_len
);
1320 operator wxChar
*() { return m_buf
.data(); }
1321 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
1326 wxWCharBuffer m_buf
;
1333 DECLARE_NO_COPY_CLASS(wxStringBufferLength
)
1336 #else // if !wxUSE_STL
1338 class WXDLLIMPEXP_BASE wxStringBuffer
1341 wxStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
1342 : m_str(str
), m_buf(NULL
)
1343 { m_buf
= m_str
.GetWriteBuf(lenWanted
); }
1345 ~wxStringBuffer() { m_str
.UngetWriteBuf(); }
1347 operator wxChar
*() const { return m_buf
; }
1353 DECLARE_NO_COPY_CLASS(wxStringBuffer
)
1356 class WXDLLIMPEXP_BASE wxStringBufferLength
1359 wxStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
1360 : m_str(str
), m_buf(NULL
), m_len(0), m_lenSet(false)
1362 m_buf
= m_str
.GetWriteBuf(lenWanted
);
1363 wxASSERT(m_buf
!= NULL
);
1366 ~wxStringBufferLength()
1369 m_str
.UngetWriteBuf(m_len
);
1372 operator wxChar
*() const { return m_buf
; }
1373 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
1381 DECLARE_NO_COPY_CLASS(wxStringBufferLength
)
1384 #endif // !wxUSE_STL
1386 // ---------------------------------------------------------------------------
1387 // wxString comparison functions: operator versions are always case sensitive
1388 // ---------------------------------------------------------------------------
1390 // note that when wxUSE_STL == 1 the comparison operators taking std::string
1391 // are used and defining them also for wxString would only result in
1392 // compilation ambiguities when comparing std::string and wxString
1395 inline bool operator==(const wxString
& s1
, const wxString
& s2
)
1396 { return (s1
.Len() == s2
.Len()) && (s1
.Cmp(s2
) == 0); }
1397 inline bool operator==(const wxString
& s1
, const wxChar
* s2
)
1398 { return s1
.Cmp(s2
) == 0; }
1399 inline bool operator==(const wxChar
* s1
, const wxString
& s2
)
1400 { return s2
.Cmp(s1
) == 0; }
1401 inline bool operator!=(const wxString
& s1
, const wxString
& s2
)
1402 { return (s1
.Len() != s2
.Len()) || (s1
.Cmp(s2
) != 0); }
1403 inline bool operator!=(const wxString
& s1
, const wxChar
* s2
)
1404 { return s1
.Cmp(s2
) != 0; }
1405 inline bool operator!=(const wxChar
* s1
, const wxString
& s2
)
1406 { return s2
.Cmp(s1
) != 0; }
1407 inline bool operator< (const wxString
& s1
, const wxString
& s2
)
1408 { return s1
.Cmp(s2
) < 0; }
1409 inline bool operator< (const wxString
& s1
, const wxChar
* s2
)
1410 { return s1
.Cmp(s2
) < 0; }
1411 inline bool operator< (const wxChar
* s1
, const wxString
& s2
)
1412 { return s2
.Cmp(s1
) > 0; }
1413 inline bool operator> (const wxString
& s1
, const wxString
& s2
)
1414 { return s1
.Cmp(s2
) > 0; }
1415 inline bool operator> (const wxString
& s1
, const wxChar
* s2
)
1416 { return s1
.Cmp(s2
) > 0; }
1417 inline bool operator> (const wxChar
* s1
, const wxString
& s2
)
1418 { return s2
.Cmp(s1
) < 0; }
1419 inline bool operator<=(const wxString
& s1
, const wxString
& s2
)
1420 { return s1
.Cmp(s2
) <= 0; }
1421 inline bool operator<=(const wxString
& s1
, const wxChar
* s2
)
1422 { return s1
.Cmp(s2
) <= 0; }
1423 inline bool operator<=(const wxChar
* s1
, const wxString
& s2
)
1424 { return s2
.Cmp(s1
) >= 0; }
1425 inline bool operator>=(const wxString
& s1
, const wxString
& s2
)
1426 { return s1
.Cmp(s2
) >= 0; }
1427 inline bool operator>=(const wxString
& s1
, const wxChar
* s2
)
1428 { return s1
.Cmp(s2
) >= 0; }
1429 inline bool operator>=(const wxChar
* s1
, const wxString
& s2
)
1430 { return s2
.Cmp(s1
) <= 0; }
1433 inline bool operator==(const wxString
& s1
, const wxWCharBuffer
& s2
)
1434 { return (s1
.Cmp((const wchar_t *)s2
) == 0); }
1435 inline bool operator==(const wxWCharBuffer
& s1
, const wxString
& s2
)
1436 { return (s2
.Cmp((const wchar_t *)s1
) == 0); }
1437 inline bool operator!=(const wxString
& s1
, const wxWCharBuffer
& s2
)
1438 { return (s1
.Cmp((const wchar_t *)s2
) != 0); }
1439 inline bool operator!=(const wxWCharBuffer
& s1
, const wxString
& s2
)
1440 { return (s2
.Cmp((const wchar_t *)s1
) != 0); }
1441 #else // !wxUSE_UNICODE
1442 inline bool operator==(const wxString
& s1
, const wxCharBuffer
& s2
)
1443 { return (s1
.Cmp((const char *)s2
) == 0); }
1444 inline bool operator==(const wxCharBuffer
& s1
, const wxString
& s2
)
1445 { return (s2
.Cmp((const char *)s1
) == 0); }
1446 inline bool operator!=(const wxString
& s1
, const wxCharBuffer
& s2
)
1447 { return (s1
.Cmp((const char *)s2
) != 0); }
1448 inline bool operator!=(const wxCharBuffer
& s1
, const wxString
& s2
)
1449 { return (s2
.Cmp((const char *)s1
) != 0); }
1450 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1452 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
, const wxString
& string2
);
1453 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxChar ch
);
1454 wxString WXDLLIMPEXP_BASE
operator+(wxChar ch
, const wxString
& string
);
1455 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const wxChar
*psz
);
1456 wxString WXDLLIMPEXP_BASE
operator+(const wxChar
*psz
, const wxString
& string
);
1459 inline wxString
operator+(const wxString
& string
, const wxWCharBuffer
& buf
)
1460 { return string
+ (const wchar_t *)buf
; }
1461 inline wxString
operator+(const wxWCharBuffer
& buf
, const wxString
& string
)
1462 { return (const wchar_t *)buf
+ string
; }
1463 #else // !wxUSE_UNICODE
1464 inline wxString
operator+(const wxString
& string
, const wxCharBuffer
& buf
)
1465 { return string
+ (const char *)buf
; }
1466 inline wxString
operator+(const wxCharBuffer
& buf
, const wxString
& string
)
1467 { return (const char *)buf
+ string
; }
1468 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1470 #endif // !wxUSE_STL
1472 // comparison with char (those are not defined by std::[w]string and so should
1473 // be always available)
1474 inline bool operator==(wxChar c
, const wxString
& s
) { return s
.IsSameAs(c
); }
1475 inline bool operator==(const wxString
& s
, wxChar c
) { return s
.IsSameAs(c
); }
1476 inline bool operator!=(wxChar c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
1477 inline bool operator!=(const wxString
& s
, wxChar c
) { return !s
.IsSameAs(c
); }
1479 // ---------------------------------------------------------------------------
1480 // Implementation only from here until the end of file
1481 // ---------------------------------------------------------------------------
1483 // don't pollute the library user's name space
1484 #undef wxASSERT_VALID_INDEX
1486 #if wxUSE_STD_IOSTREAM
1488 #include "wx/iosfwrap.h"
1490 WXDLLIMPEXP_BASE wxSTD istream
& operator>>(wxSTD istream
&, wxString
&);
1491 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxString
&);
1493 #endif // wxSTD_STRING_COMPATIBILITY
1495 #endif // _WX_WXSTRINGH__