1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxString and wxArrayString classes
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
13 Efficient string class [more or less] compatible with MFC CString,
14 wxWidgets version 1 wxString and std::string and some handy functions
15 missing from string.h.
18 #ifndef _WX_WXSTRINGH__
19 #define _WX_WXSTRINGH__
21 // ----------------------------------------------------------------------------
23 // ----------------------------------------------------------------------------
25 #include "wx/defs.h" // everybody should include this
27 #if defined(__WXMAC__) || defined(__VISAGECPP__)
31 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
32 // problem in VACPP V4 with including stdlib.h multiple times
33 // strconv includes it anyway
46 #ifdef HAVE_STRCASECMP_IN_STRINGS_H
47 #include <strings.h> // for strcasecmp()
48 #endif // HAVE_STRCASECMP_IN_STRINGS_H
51 #include <StringMgr.h>
54 #include "wx/wxchar.h" // for wxChar
55 #include "wx/buffer.h" // for wxCharBuffer
56 #include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes
58 class WXDLLIMPEXP_BASE wxString
;
60 // ---------------------------------------------------------------------------
62 // ---------------------------------------------------------------------------
64 // casts [unfortunately!] needed to call some broken functions which require
65 // "char *" instead of "const char *"
66 #define WXSTRINGCAST (wxChar *)(const wxChar *)
67 #define wxCSTRINGCAST (wxChar *)(const wxChar *)
68 #define wxMBSTRINGCAST (char *)(const char *)
69 #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *)
71 // implementation only
72 #define wxASSERT_VALID_INDEX(i) \
73 wxASSERT_MSG( (size_t)(i) <= length(), _T("invalid index in wxString") )
75 // ----------------------------------------------------------------------------
77 // ----------------------------------------------------------------------------
79 // maximum possible length for a string means "take all string" everywhere
80 #define wxSTRING_MAXLEN wxStringBase::npos
82 // ----------------------------------------------------------------------------
84 // ----------------------------------------------------------------------------
86 // global pointer to empty string
87 extern WXDLLIMPEXP_DATA_BASE(const wxChar
*) wxEmptyString
;
89 // ---------------------------------------------------------------------------
90 // global functions complementing standard C string library replacements for
91 // strlen() and portable strcasecmp()
92 //---------------------------------------------------------------------------
94 // Use wxXXX() functions from wxchar.h instead! These functions are for
95 // backwards compatibility only.
97 // checks whether the passed in pointer is NULL and if the string is empty
98 inline bool IsEmpty(const char *p
) { return (!p
|| !*p
); }
100 // safe version of strlen() (returns 0 if passed NULL pointer)
101 inline size_t Strlen(const char *psz
)
102 { return psz
? strlen(psz
) : 0; }
104 // portable strcasecmp/_stricmp
105 inline int Stricmp(const char *psz1
, const char *psz2
)
107 #if defined(__VISUALC__) && defined(__WXWINCE__)
108 register char c1
, c2
;
110 c1
= tolower(*psz1
++);
111 c2
= tolower(*psz2
++);
112 } while ( c1
&& (c1
== c2
) );
115 #elif defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) )
116 return _stricmp(psz1
, psz2
);
117 #elif defined(__SC__)
118 return _stricmp(psz1
, psz2
);
119 #elif defined(__SALFORDC__)
120 return stricmp(psz1
, psz2
);
121 #elif defined(__BORLANDC__)
122 return stricmp(psz1
, psz2
);
123 #elif defined(__WATCOMC__)
124 return stricmp(psz1
, psz2
);
125 #elif defined(__DJGPP__)
126 return stricmp(psz1
, psz2
);
127 #elif defined(__EMX__)
128 return stricmp(psz1
, psz2
);
129 #elif defined(__WXPM__)
130 return stricmp(psz1
, psz2
);
131 #elif defined(__WXPALMOS__) || \
132 defined(HAVE_STRCASECMP_IN_STRING_H) || \
133 defined(HAVE_STRCASECMP_IN_STRINGS_H) || \
134 defined(__GNUWIN32__)
135 return strcasecmp(psz1
, psz2
);
136 #elif defined(__MWERKS__) && !defined(__INTEL__)
137 register char c1
, c2
;
139 c1
= tolower(*psz1
++);
140 c2
= tolower(*psz2
++);
141 } while ( c1
&& (c1
== c2
) );
145 // almost all compilers/libraries provide this function (unfortunately under
146 // different names), that's why we don't implement our own which will surely
147 // be more efficient than this code (uncomment to use):
149 register char c1, c2;
151 c1 = tolower(*psz1++);
152 c2 = tolower(*psz2++);
153 } while ( c1 && (c1 == c2) );
158 #error "Please define string case-insensitive compare for your OS/compiler"
159 #endif // OS/compiler
162 // ----------------------------------------------------------------------------
163 // deal with STL/non-STL/non-STL-but-wxUSE_STD_STRING
164 // ----------------------------------------------------------------------------
166 // in both cases we need to define wxStdString
167 #if wxUSE_STL || defined(wxUSE_STD_STRING)
169 #include "wx/beforestd.h"
171 #include "wx/afterstd.h"
174 #ifdef HAVE_STD_WSTRING
175 typedef std::wstring wxStdString
;
177 typedef std::basic_string
<wxChar
> wxStdString
;
180 typedef std::string wxStdString
;
183 #endif // need <string>
187 // we don't need an extra ctor from std::string when copy ctor already does
189 #undef wxUSE_STD_STRING
191 #if (defined(__GNUG__) && (__GNUG__ < 3)) || \
192 (defined(_MSC_VER) && (_MSC_VER <= 1200))
193 #define wxSTRING_BASE_HASNT_CLEAR
196 typedef wxStdString wxStringBase
;
197 #else // if !wxUSE_STL
199 #ifndef HAVE_STD_STRING_COMPARE
200 #define HAVE_STD_STRING_COMPARE
203 // ---------------------------------------------------------------------------
204 // string data prepended with some housekeeping info (used by wxString class),
205 // is never used directly (but had to be put here to allow inlining)
206 // ---------------------------------------------------------------------------
208 struct WXDLLIMPEXP_BASE wxStringData
210 int nRefs
; // reference count
211 size_t nDataLength
, // actual string length
212 nAllocLength
; // allocated memory size
214 // mimics declaration 'wxChar data[nAllocLength]'
215 wxChar
* data() const { return (wxChar
*)(this + 1); }
217 // empty string has a special ref count so it's never deleted
218 bool IsEmpty() const { return (nRefs
== -1); }
219 bool IsShared() const { return (nRefs
> 1); }
222 void Lock() { if ( !IsEmpty() ) nRefs
++; }
224 // VC++ will refuse to inline Unlock but profiling shows that it is wrong
225 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
228 // VC++ free must take place in same DLL as allocation when using non dll
229 // run-time library (e.g. Multithreaded instead of Multithreaded DLL)
230 #if defined(__VISUALC__) && defined(_MT) && !defined(_DLL)
231 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) Free(); }
232 // we must not inline deallocation since allocation is not inlined
235 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) free(this); }
238 // if we had taken control over string memory (GetWriteBuf), it's
239 // intentionally put in invalid state
240 void Validate(bool b
) { nRefs
= (b
? 1 : 0); }
241 bool IsValid() const { return (nRefs
!= 0); }
244 class WXDLLIMPEXP_BASE wxStringBase
247 friend class WXDLLIMPEXP_BASE wxArrayString
;
250 // an 'invalid' value for string index, moved to this place due to a CW bug
251 static const size_t npos
;
253 // points to data preceded by wxStringData structure with ref count info
256 // accessor to string data
257 wxStringData
* GetStringData() const { return (wxStringData
*)m_pchData
- 1; }
259 // string (re)initialization functions
260 // initializes the string to the empty value (must be called only from
261 // ctors, use Reinit() otherwise)
262 void Init() { m_pchData
= (wxChar
*)wxEmptyString
; }
263 // initializaes the string with (a part of) C-string
264 void InitWith(const wxChar
*psz
, size_t nPos
= 0, size_t nLen
= npos
);
265 // as Init, but also frees old data
266 void Reinit() { GetStringData()->Unlock(); Init(); }
269 // allocates memory for string of length nLen
270 bool AllocBuffer(size_t nLen
);
271 // copies data to another string
272 bool AllocCopy(wxString
&, int, int) const;
273 // effectively copies data to string
274 bool AssignCopy(size_t, const wxChar
*);
276 // append a (sub)string
277 bool ConcatSelf(size_t nLen
, const wxChar
*src
, size_t nMaxLen
);
278 bool ConcatSelf(size_t nLen
, const wxChar
*src
)
279 { return ConcatSelf(nLen
, src
, nLen
); }
281 // functions called before writing to the string: they copy it if there
282 // are other references to our data (should be the only owner when writing)
283 bool CopyBeforeWrite();
284 bool AllocBeforeWrite(size_t);
286 // compatibility with wxString
287 bool Alloc(size_t nLen
);
290 typedef wxChar value_type
;
291 typedef wxChar char_type
;
292 typedef size_t size_type
;
293 typedef value_type
& reference
;
294 typedef const value_type
& const_reference
;
295 typedef value_type
* pointer
;
296 typedef const value_type
* const_pointer
;
297 typedef value_type
*iterator
;
298 typedef const value_type
*const_iterator
;
300 // constructors and destructor
301 // ctor for an empty string
302 wxStringBase() { Init(); }
304 wxStringBase(const wxStringBase
& stringSrc
)
306 wxASSERT_MSG( stringSrc
.GetStringData()->IsValid(),
307 _T("did you forget to call UngetWriteBuf()?") );
309 if ( stringSrc
.empty() ) {
310 // nothing to do for an empty string
314 m_pchData
= stringSrc
.m_pchData
; // share same data
315 GetStringData()->Lock(); // => one more copy
318 // string containing nRepeat copies of ch
319 wxStringBase(size_type nRepeat
, wxChar ch
);
320 // ctor takes first nLength characters from C string
321 // (default value of npos means take all the string)
322 wxStringBase(const wxChar
*psz
)
323 { InitWith(psz
, 0, npos
); }
324 wxStringBase(const wxChar
*psz
, size_t nLength
)
325 { InitWith(psz
, 0, nLength
); }
326 wxStringBase(const wxChar
*psz
, wxMBConv
& WXUNUSED(conv
), size_t nLength
= npos
)
327 { InitWith(psz
, 0, nLength
); }
328 // take nLen chars starting at nPos
329 wxStringBase(const wxStringBase
& str
, size_t nPos
, size_t nLen
)
331 wxASSERT_MSG( str
.GetStringData()->IsValid(),
332 _T("did you forget to call UngetWriteBuf()?") );
334 size_t strLen
= str
.length() - nPos
; nLen
= strLen
< nLen
? strLen
: nLen
;
335 InitWith(str
.c_str(), nPos
, nLen
);
337 // take all characters from pStart to pEnd
338 wxStringBase(const void *pStart
, const void *pEnd
);
340 // dtor is not virtual, this class must not be inherited from!
343 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
344 //RN - according to the above VC++ does indeed inline this,
345 //even though it spits out two warnings
346 #pragma warning (disable:4714)
349 GetStringData()->Unlock();
352 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
353 //re-enable inlining warning
354 #pragma warning (default:4714)
356 // overloaded assignment
357 // from another wxString
358 wxStringBase
& operator=(const wxStringBase
& stringSrc
);
360 wxStringBase
& operator=(wxChar ch
);
362 wxStringBase
& operator=(const wxChar
*psz
);
364 // return the length of the string
365 size_type
size() const { return GetStringData()->nDataLength
; }
366 // return the length of the string
367 size_type
length() const { return size(); }
368 // return the maximum size of the string
369 size_type
max_size() const { return wxSTRING_MAXLEN
; }
370 // resize the string, filling the space with c if c != 0
371 void resize(size_t nSize
, wxChar ch
= wxT('\0'));
372 // delete the contents of the string
373 void clear() { erase(0, npos
); }
374 // returns true if the string is empty
375 bool empty() const { return size() == 0; }
376 // inform string about planned change in size
377 void reserve(size_t sz
) { Alloc(sz
); }
378 size_type
capacity() const { return GetStringData()->nAllocLength
; }
381 // return the character at position n
382 value_type
at(size_type n
) const
383 { wxASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
384 // returns the writable character at position n
385 reference
at(size_type n
)
386 { wxASSERT_VALID_INDEX( n
); CopyBeforeWrite(); return m_pchData
[n
]; }
388 // lib.string.modifiers
389 // append elements str[pos], ..., str[pos+n]
390 wxStringBase
& append(const wxStringBase
& str
, size_t pos
, size_t n
)
392 wxASSERT(pos
<= str
.length());
393 ConcatSelf(n
, str
.c_str() + pos
, str
.length() - pos
);
397 wxStringBase
& append(const wxStringBase
& str
)
398 { ConcatSelf(str
.length(), str
.c_str()); return *this; }
399 // append first n (or all if n == npos) characters of sz
400 wxStringBase
& append(const wxChar
*sz
)
401 { ConcatSelf(wxStrlen(sz
), sz
); return *this; }
402 wxStringBase
& append(const wxChar
*sz
, size_t n
)
403 { ConcatSelf(n
, sz
); return *this; }
404 // append n copies of ch
405 wxStringBase
& append(size_t n
, wxChar ch
);
406 // append from first to last
407 wxStringBase
& append(const_iterator first
, const_iterator last
)
408 { ConcatSelf(last
- first
, first
); return *this; }
410 // same as `this_string = str'
411 wxStringBase
& assign(const wxStringBase
& str
)
412 { return *this = str
; }
413 // same as ` = str[pos..pos + n]
414 wxStringBase
& assign(const wxStringBase
& str
, size_t pos
, size_t n
)
415 { clear(); return append(str
, pos
, n
); }
416 // same as `= first n (or all if n == npos) characters of sz'
417 wxStringBase
& assign(const wxChar
*sz
)
418 { clear(); return append(sz
, wxStrlen(sz
)); }
419 wxStringBase
& assign(const wxChar
*sz
, size_t n
)
420 { clear(); return append(sz
, n
); }
421 // same as `= n copies of ch'
422 wxStringBase
& assign(size_t n
, wxChar ch
)
423 { clear(); return append(n
, ch
); }
424 // assign from first to last
425 wxStringBase
& assign(const_iterator first
, const_iterator last
)
426 { clear(); return append(first
, last
); }
428 // first valid index position
429 const_iterator
begin() const { return m_pchData
; }
430 // position one after the last valid one
431 const_iterator
end() const { return m_pchData
+ length(); }
433 // first valid index position
435 // position one after the last valid one
438 // insert another string
439 wxStringBase
& insert(size_t nPos
, const wxStringBase
& str
)
441 wxASSERT( str
.GetStringData()->IsValid() );
442 return insert(nPos
, str
.c_str(), str
.length());
444 // insert n chars of str starting at nStart (in str)
445 wxStringBase
& insert(size_t nPos
, const wxStringBase
& str
, size_t nStart
, size_t n
)
447 wxASSERT( str
.GetStringData()->IsValid() );
448 wxASSERT( nStart
< str
.length() );
449 size_t strLen
= str
.length() - nStart
;
450 n
= strLen
< n
? strLen
: n
;
451 return insert(nPos
, str
.c_str() + nStart
, n
);
453 // insert first n (or all if n == npos) characters of sz
454 wxStringBase
& insert(size_t nPos
, const wxChar
*sz
, size_t n
= npos
);
455 // insert n copies of ch
456 wxStringBase
& insert(size_t nPos
, size_t n
, wxChar ch
)
457 { return insert(nPos
, wxStringBase(n
, ch
)); }
458 iterator
insert(iterator it
, wxChar ch
)
459 { size_t idx
= it
- begin(); insert(idx
, 1, ch
); return begin() + idx
; }
460 void insert(iterator it
, const_iterator first
, const_iterator last
)
461 { insert(it
- begin(), first
, last
- first
); }
462 void insert(iterator it
, size_type n
, wxChar ch
)
463 { insert(it
- begin(), n
, ch
); }
465 // delete characters from nStart to nStart + nLen
466 wxStringBase
& erase(size_type pos
= 0, size_type n
= npos
);
467 iterator
erase(iterator first
, iterator last
)
469 size_t idx
= first
- begin();
470 erase(idx
, last
- first
);
471 return begin() + idx
;
473 iterator
erase(iterator first
);
475 // explicit conversion to C string (use this with printf()!)
476 const wxChar
* c_str() const { return m_pchData
; }
477 const wxChar
* data() const { return m_pchData
; }
479 // replaces the substring of length nLen starting at nStart
480 wxStringBase
& replace(size_t nStart
, size_t nLen
, const wxChar
* sz
);
481 // replaces the substring of length nLen starting at nStart
482 wxStringBase
& replace(size_t nStart
, size_t nLen
, const wxStringBase
& str
)
483 { return replace(nStart
, nLen
, str
.c_str()); }
484 // replaces the substring with nCount copies of ch
485 wxStringBase
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxChar ch
);
486 // replaces a substring with another substring
487 wxStringBase
& replace(size_t nStart
, size_t nLen
,
488 const wxStringBase
& str
, size_t nStart2
, size_t nLen2
);
489 // replaces the substring with first nCount chars of sz
490 wxStringBase
& replace(size_t nStart
, size_t nLen
,
491 const wxChar
* sz
, size_t nCount
);
492 wxStringBase
& replace(iterator first
, iterator last
, const_pointer s
)
493 { return replace(first
- begin(), last
- first
, s
); }
494 wxStringBase
& replace(iterator first
, iterator last
, const_pointer s
,
496 { return replace(first
- begin(), last
- first
, s
, n
); }
497 wxStringBase
& replace(iterator first
, iterator last
, const wxStringBase
& s
)
498 { return replace(first
- begin(), last
- first
, s
); }
499 wxStringBase
& replace(iterator first
, iterator last
, size_type n
, wxChar c
)
500 { return replace(first
- begin(), last
- first
, n
, c
); }
501 wxStringBase
& replace(iterator first
, iterator last
,
502 const_iterator first1
, const_iterator last1
)
503 { return replace(first
- begin(), last
- first
, first1
, last1
- first1
); }
506 void swap(wxStringBase
& str
);
508 // All find() functions take the nStart argument which specifies the
509 // position to start the search on, the default value is 0. All functions
510 // return npos if there were no match.
513 size_t find(const wxStringBase
& str
, size_t nStart
= 0) const;
515 // find first n characters of sz
516 size_t find(const wxChar
* sz
, size_t nStart
= 0, size_t n
= npos
) const;
518 // find the first occurence of character ch after nStart
519 size_t find(wxChar ch
, size_t nStart
= 0) const;
521 // rfind() family is exactly like find() but works right to left
523 // as find, but from the end
524 size_t rfind(const wxStringBase
& str
, size_t nStart
= npos
) const;
526 // as find, but from the end
527 size_t rfind(const wxChar
* sz
, size_t nStart
= npos
,
528 size_t n
= npos
) const;
529 // as find, but from the end
530 size_t rfind(wxChar ch
, size_t nStart
= npos
) const;
532 // find first/last occurence of any character in the set
534 // as strpbrk() but starts at nStart, returns npos if not found
535 size_t find_first_of(const wxStringBase
& str
, size_t nStart
= 0) const
536 { return find_first_of(str
.c_str(), nStart
); }
538 size_t find_first_of(const wxChar
* sz
, size_t nStart
= 0) const;
539 size_t find_first_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
540 // same as find(char, size_t)
541 size_t find_first_of(wxChar c
, size_t nStart
= 0) const
542 { return find(c
, nStart
); }
543 // find the last (starting from nStart) char from str in this string
544 size_t find_last_of (const wxStringBase
& str
, size_t nStart
= npos
) const
545 { return find_last_of(str
.c_str(), nStart
); }
547 size_t find_last_of (const wxChar
* sz
, size_t nStart
= npos
) const;
548 size_t find_last_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
550 size_t find_last_of(wxChar c
, size_t nStart
= npos
) const
551 { return rfind(c
, nStart
); }
553 // find first/last occurence of any character not in the set
555 // as strspn() (starting from nStart), returns npos on failure
556 size_t find_first_not_of(const wxStringBase
& str
, size_t nStart
= 0) const
557 { return find_first_not_of(str
.c_str(), nStart
); }
559 size_t find_first_not_of(const wxChar
* sz
, size_t nStart
= 0) const;
560 size_t find_first_not_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
562 size_t find_first_not_of(wxChar ch
, size_t nStart
= 0) const;
564 size_t find_last_not_of(const wxStringBase
& str
, size_t nStart
= npos
) const
565 { return find_last_not_of(str
.c_str(), nStart
); }
567 size_t find_last_not_of(const wxChar
* sz
, size_t nStart
= npos
) const;
568 size_t find_last_not_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
570 size_t find_last_not_of(wxChar ch
, size_t nStart
= npos
) const;
572 // All compare functions return -1, 0 or 1 if the [sub]string is less,
573 // equal or greater than the compare() argument.
575 // comparison with another string
576 int compare(const wxStringBase
& str
) const;
577 // comparison with a substring
578 int compare(size_t nStart
, size_t nLen
, const wxStringBase
& str
) const;
579 // comparison of 2 substrings
580 int compare(size_t nStart
, size_t nLen
,
581 const wxStringBase
& str
, size_t nStart2
, size_t nLen2
) const;
582 // comparison with a c string
583 int compare(const wxChar
* sz
) const;
584 // substring comparison with first nCount characters of sz
585 int compare(size_t nStart
, size_t nLen
,
586 const wxChar
* sz
, size_t nCount
= npos
) const;
588 size_type
copy(wxChar
* s
, size_type n
, size_type pos
= 0);
590 // substring extraction
591 wxStringBase
substr(size_t nStart
= 0, size_t nLen
= npos
) const;
594 wxStringBase
& operator+=(const wxStringBase
& s
) { return append(s
); }
595 // string += C string
596 wxStringBase
& operator+=(const wxChar
*psz
) { return append(psz
); }
598 wxStringBase
& operator+=(wxChar ch
) { return append(1, ch
); }
603 // ----------------------------------------------------------------------------
604 // wxString: string class trying to be compatible with std::string, MFC
605 // CString and wxWindows 1.x wxString all at once
606 // ---------------------------------------------------------------------------
608 class WXDLLIMPEXP_BASE wxString
: public wxStringBase
611 friend class WXDLLIMPEXP_BASE wxArrayString
;
614 // NB: special care was taken in arranging the member functions in such order
615 // that all inline functions can be effectively inlined, verify that all
616 // performace critical functions are still inlined if you change order!
618 // if we hadn't made these operators private, it would be possible to
619 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
620 // converted to char in C and we do have operator=(char)
622 // NB: we don't need other versions (short/long and unsigned) as attempt
623 // to assign another numeric type to wxString will now result in
624 // ambiguity between operator=(char) and operator=(int)
625 wxString
& operator=(int);
627 // these methods are not implemented - there is _no_ conversion from int to
628 // string, you're doing something wrong if the compiler wants to call it!
630 // try `s << i' or `s.Printf("%d", i)' instead
634 // constructors and destructor
635 // ctor for an empty string
636 wxString() : wxStringBase() { }
638 wxString(const wxStringBase
& stringSrc
) : wxStringBase(stringSrc
) { }
639 wxString(const wxString
& stringSrc
) : wxStringBase(stringSrc
) { }
640 // string containing nRepeat copies of ch
641 wxString(wxChar ch
, size_t nRepeat
= 1)
642 : wxStringBase(nRepeat
, ch
) { }
643 wxString(size_t nRepeat
, wxChar ch
)
644 : wxStringBase(nRepeat
, ch
) { }
645 // ctor takes first nLength characters from C string
646 // (default value of npos means take all the string)
647 wxString(const wxChar
*psz
)
648 : wxStringBase(psz
? psz
: wxT("")) { }
649 wxString(const wxChar
*psz
, size_t nLength
)
650 : wxStringBase(psz
, nLength
) { }
651 wxString(const wxChar
*psz
, wxMBConv
& WXUNUSED(conv
), size_t nLength
= npos
)
652 : wxStringBase(psz
, nLength
== npos
? wxStrlen(psz
) : nLength
) { }
654 // even we're not build with wxUSE_STL == 1 it is very convenient to allow
655 // implicit conversions from std::string to wxString as this allows to use
656 // the same strings in non-GUI and GUI code, however we don't want to
657 // unconditionally add this ctor as it would make wx lib dependent on
658 // libstdc++ on some Linux versions which is bad, so instead we ask the
659 // client code to define this wxUSE_STD_STRING symbol if they need it
660 #ifdef wxUSE_STD_STRING
661 wxString(const wxStdString
& s
)
662 : wxStringBase(s
.c_str()) { }
663 #endif // wxUSE_STD_STRING
666 // from multibyte string
667 wxString(const char *psz
, wxMBConv
& conv
, size_t nLength
= npos
);
668 // from wxWCharBuffer (i.e. return from wxGetString)
669 wxString(const wxWCharBuffer
& psz
) : wxStringBase(psz
.data()) { }
671 // from C string (for compilers using unsigned char)
672 wxString(const unsigned char* psz
, size_t nLength
= npos
)
673 : wxStringBase((const char*)psz
, nLength
) { }
676 // from wide (Unicode) string
677 wxString(const wchar_t *pwz
, wxMBConv
& conv
= wxConvLibc
, size_t nLength
= npos
);
678 #endif // !wxUSE_WCHAR_T
681 wxString(const wxCharBuffer
& psz
)
682 : wxStringBase(psz
) { }
683 #endif // Unicode/ANSI
685 // generic attributes & operations
686 // as standard strlen()
687 size_t Len() const { return length(); }
688 // string contains any characters?
689 bool IsEmpty() const { return empty(); }
690 // empty string is "false", so !str will return true
691 bool operator!() const { return IsEmpty(); }
692 // truncate the string to given length
693 wxString
& Truncate(size_t uiLen
);
694 // empty string contents
699 wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") );
701 // empty the string and free memory
704 wxString
tmp(wxEmptyString
);
710 bool IsAscii() const;
712 bool IsNumber() const;
716 // data access (all indexes are 0 based)
718 wxChar
GetChar(size_t n
) const
721 wxChar
& GetWritableChar(size_t n
)
724 void SetChar(size_t n
, wxChar ch
)
727 // get last character
730 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
732 return at(length() - 1);
735 // get writable last character
738 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
739 return at(length() - 1);
743 Note that we we must define all of the overloads below to avoid
744 ambiguity when using str[0]. Also note that for a conforming compiler we
745 don't need const version of operatorp[] at all as indexed access to
746 const string is provided by implicit conversion to "const wxChar *"
747 below and defining them would only result in ambiguities, but some other
748 compilers refuse to compile "str[0]" without them.
751 #if defined(__BORLANDC__) || defined(__WATCOMC__) || defined(__MWERKS__)
752 wxChar
operator[](int n
) const
753 { return wxStringBase::at(n
); }
754 wxChar
operator[](size_type n
) const
755 { return wxStringBase::at(n
); }
756 #ifndef wxSIZE_T_IS_UINT
757 wxChar
operator[](unsigned int n
) const
758 { return wxStringBase::at(n
); }
759 #endif // size_t != unsigned int
760 #endif // broken compiler
763 // operator versions of GetWriteableChar()
764 wxChar
& operator[](int n
)
765 { return wxStringBase::at(n
); }
766 wxChar
& operator[](size_type n
)
767 { return wxStringBase::at(n
); }
768 #ifndef wxSIZE_T_IS_UINT
769 wxChar
& operator[](unsigned int n
)
770 { return wxStringBase::at(n
); }
771 #endif // size_t != unsigned int
773 // implicit conversion to C string
774 operator const wxChar
*() const { return c_str(); }
776 // identical to c_str(), for wxWin 1.6x compatibility
777 const wxChar
* wx_str() const { return c_str(); }
778 // identical to c_str(), for MFC compatibility
779 const wxChar
* GetData() const { return c_str(); }
781 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
782 // converting numbers or strings which are certain not to contain special
783 // chars (typically system functions, X atoms, environment variables etc.)
785 // the behaviour of these functions with the strings containing anything
786 // else than 7 bit ASCII characters is undefined, use at your own risk.
788 static wxString
FromAscii(const char *ascii
); // string
789 static wxString
FromAscii(const char ascii
); // char
790 const wxCharBuffer
ToAscii() const;
792 static wxString
FromAscii(const char *ascii
) { return wxString( ascii
); }
793 static wxString
FromAscii(const char ascii
) { return wxString( ascii
); }
794 const char *ToAscii() const { return c_str(); }
795 #endif // Unicode/!Unicode
797 // conversions with (possible) format conversions: have to return a
798 // buffer with temporary data
800 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
801 // return an ANSI (multibyte) string, wc_str() to return a wide string and
802 // fn_str() to return a string which should be used with the OS APIs
803 // accepting the file names. The return value is always the same, but the
804 // type differs because a function may either return pointer to the buffer
805 // directly or have to use intermediate buffer for translation.
807 const wxCharBuffer
mb_str(wxMBConv
& conv
= wxConvLibc
) const;
809 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
811 const wxChar
* wc_str() const { return c_str(); }
813 // for compatibility with !wxUSE_UNICODE version
814 const wxChar
* wc_str(wxMBConv
& WXUNUSED(conv
)) const { return c_str(); }
817 const wxCharBuffer
fn_str() const { return mb_str(wxConvFile
); }
819 const wxChar
* fn_str() const { return c_str(); }
820 #endif // wxMBFILES/!wxMBFILES
822 const wxChar
* mb_str() const { return c_str(); }
824 // for compatibility with wxUSE_UNICODE version
825 const wxChar
* mb_str(wxMBConv
& WXUNUSED(conv
)) const { return c_str(); }
827 const wxWX2MBbuf
mbc_str() const { return mb_str(); }
830 const wxWCharBuffer
wc_str(wxMBConv
& conv
) const;
831 #endif // wxUSE_WCHAR_T
833 const wxCharBuffer
fn_str() const { return wxConvFile
.cWC2WX( wc_str( wxConvLocal
) ); }
835 const wxChar
* fn_str() const { return c_str(); }
837 #endif // Unicode/ANSI
839 // overloaded assignment
840 // from another wxString
841 wxString
& operator=(const wxStringBase
& stringSrc
)
842 { return (wxString
&)wxStringBase::operator=(stringSrc
); }
844 wxString
& operator=(wxChar ch
)
845 { return (wxString
&)wxStringBase::operator=(ch
); }
847 wxString
& operator=(const wxChar
*psz
)
848 { return (wxString
&)wxStringBase::operator=(psz
); }
850 // from wxWCharBuffer
851 wxString
& operator=(const wxWCharBuffer
& psz
)
852 { (void) operator=((const wchar_t *)psz
); return *this; }
854 // from another kind of C string
855 wxString
& operator=(const unsigned char* psz
);
857 // from a wide string
858 wxString
& operator=(const wchar_t *pwz
);
861 wxString
& operator=(const wxCharBuffer
& psz
)
862 { (void) operator=((const char *)psz
); return *this; }
863 #endif // Unicode/ANSI
865 // string concatenation
866 // in place concatenation
868 Concatenate and return the result. Note that the left to right
869 associativity of << allows to write things like "str << str1 << str2
870 << ..." (unlike with +=)
873 wxString
& operator<<(const wxString
& s
)
876 wxASSERT_MSG( s
.GetStringData()->IsValid(),
877 _T("did you forget to call UngetWriteBuf()?") );
883 // string += C string
884 wxString
& operator<<(const wxChar
*psz
)
885 { append(psz
); return *this; }
887 wxString
& operator<<(wxChar ch
) { append(1, ch
); return *this; }
889 // string += buffer (i.e. from wxGetString)
891 wxString
& operator<<(const wxWCharBuffer
& s
)
892 { (void)operator<<((const wchar_t *)s
); return *this; }
893 void operator+=(const wxWCharBuffer
& s
)
894 { (void)operator<<((const wchar_t *)s
); }
895 #else // !wxUSE_UNICODE
896 wxString
& operator<<(const wxCharBuffer
& s
)
897 { (void)operator<<((const char *)s
); return *this; }
898 void operator+=(const wxCharBuffer
& s
)
899 { (void)operator<<((const char *)s
); }
900 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
902 // string += C string
903 wxString
& Append(const wxString
& s
)
905 // test for empty() to share the string if possible
912 wxString
& Append(const wxChar
* psz
)
913 { append(psz
); return *this; }
914 // append count copies of given character
915 wxString
& Append(wxChar ch
, size_t count
= 1u)
916 { append(count
, ch
); return *this; }
917 wxString
& Append(const wxChar
* psz
, size_t nLen
)
918 { append(psz
, nLen
); return *this; }
920 // prepend a string, return the string itself
921 wxString
& Prepend(const wxString
& str
)
922 { *this = str
+ *this; return *this; }
924 // non-destructive concatenation
926 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
, const wxString
& string2
);
928 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxChar ch
);
930 friend wxString WXDLLIMPEXP_BASE
operator+(wxChar ch
, const wxString
& string
);
932 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const wxChar
*psz
);
934 friend wxString WXDLLIMPEXP_BASE
operator+(const wxChar
*psz
, const wxString
& string
);
936 // stream-like functions
937 // insert an int into string
938 wxString
& operator<<(int i
)
939 { return (*this) << Format(_T("%d"), i
); }
940 // insert an unsigned int into string
941 wxString
& operator<<(unsigned int ui
)
942 { return (*this) << Format(_T("%u"), ui
); }
943 // insert a long into string
944 wxString
& operator<<(long l
)
945 { return (*this) << Format(_T("%ld"), l
); }
946 // insert an unsigned long into string
947 wxString
& operator<<(unsigned long ul
)
948 { return (*this) << Format(_T("%lu"), ul
); }
949 // insert a float into string
950 wxString
& operator<<(float f
)
951 { return (*this) << Format(_T("%f"), f
); }
952 // insert a double into string
953 wxString
& operator<<(double d
)
954 { return (*this) << Format(_T("%g"), d
); }
957 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
958 int Cmp(const wxChar
*psz
) const;
959 int Cmp(const wxString
& s
) const;
960 // same as Cmp() but not case-sensitive
961 int CmpNoCase(const wxChar
*psz
) const;
962 int CmpNoCase(const wxString
& s
) const;
963 // test for the string equality, either considering case or not
964 // (if compareWithCase then the case matters)
965 bool IsSameAs(const wxChar
*psz
, bool compareWithCase
= true) const
966 { return (compareWithCase
? Cmp(psz
) : CmpNoCase(psz
)) == 0; }
967 // comparison with a signle character: returns true if equal
968 bool IsSameAs(wxChar c
, bool compareWithCase
= true) const
970 return (length() == 1) && (compareWithCase
? GetChar(0u) == c
971 : wxToupper(GetChar(0u)) == wxToupper(c
));
974 // simple sub-string extraction
975 // return substring starting at nFirst of length nCount (or till the end
976 // if nCount = default value)
977 wxString
Mid(size_t nFirst
, size_t nCount
= npos
) const;
979 // operator version of Mid()
980 wxString
operator()(size_t start
, size_t len
) const
981 { return Mid(start
, len
); }
983 // check that the string starts with prefix and return the rest of the
984 // string in the provided pointer if it is not NULL, otherwise return
986 bool StartsWith(const wxChar
*prefix
, wxString
*rest
= NULL
) const;
988 // get first nCount characters
989 wxString
Left(size_t nCount
) const;
990 // get last nCount characters
991 wxString
Right(size_t nCount
) const;
992 // get all characters before the first occurance of ch
993 // (returns the whole string if ch not found)
994 wxString
BeforeFirst(wxChar ch
) const;
995 // get all characters before the last occurence of ch
996 // (returns empty string if ch not found)
997 wxString
BeforeLast(wxChar ch
) const;
998 // get all characters after the first occurence of ch
999 // (returns empty string if ch not found)
1000 wxString
AfterFirst(wxChar ch
) const;
1001 // get all characters after the last occurence of ch
1002 // (returns the whole string if ch not found)
1003 wxString
AfterLast(wxChar ch
) const;
1005 // for compatibility only, use more explicitly named functions above
1006 wxString
Before(wxChar ch
) const { return BeforeLast(ch
); }
1007 wxString
After(wxChar ch
) const { return AfterFirst(ch
); }
1010 // convert to upper case in place, return the string itself
1011 wxString
& MakeUpper();
1012 // convert to upper case, return the copy of the string
1013 // Here's something to remember: BC++ doesn't like returns in inlines.
1014 wxString
Upper() const ;
1015 // convert to lower case in place, return the string itself
1016 wxString
& MakeLower();
1017 // convert to lower case, return the copy of the string
1018 wxString
Lower() const ;
1020 // trimming/padding whitespace (either side) and truncating
1021 // remove spaces from left or from right (default) side
1022 wxString
& Trim(bool bFromRight
= true);
1023 // add nCount copies chPad in the beginning or at the end (default)
1024 wxString
& Pad(size_t nCount
, wxChar chPad
= wxT(' '), bool bFromRight
= true);
1026 // searching and replacing
1027 // searching (return starting index, or -1 if not found)
1028 int Find(wxChar ch
, bool bFromEnd
= false) const; // like strchr/strrchr
1029 // searching (return starting index, or -1 if not found)
1030 int Find(const wxChar
*pszSub
) const; // like strstr
1031 // replace first (or all of bReplaceAll) occurences of substring with
1032 // another string, returns the number of replacements made
1033 size_t Replace(const wxChar
*szOld
,
1034 const wxChar
*szNew
,
1035 bool bReplaceAll
= true);
1037 // check if the string contents matches a mask containing '*' and '?'
1038 bool Matches(const wxChar
*szMask
) const;
1040 // conversion to numbers: all functions return true only if the whole
1041 // string is a number and put the value of this number into the pointer
1042 // provided, the base is the numeric base in which the conversion should be
1043 // done and must be comprised between 2 and 36 or be 0 in which case the
1044 // standard C rules apply (leading '0' => octal, "0x" => hex)
1045 // convert to a signed integer
1046 bool ToLong(long *val
, int base
= 10) const;
1047 // convert to an unsigned integer
1048 bool ToULong(unsigned long *val
, int base
= 10) const;
1049 // convert to a double
1050 bool ToDouble(double *val
) const;
1052 // formated input/output
1053 // as sprintf(), returns the number of characters written or < 0 on error
1054 // (take 'this' into account in attribute parameter count)
1055 int Printf(const wxChar
*pszFormat
, ...) ATTRIBUTE_PRINTF_2
;
1056 // as vprintf(), returns the number of characters written or < 0 on error
1057 int PrintfV(const wxChar
* pszFormat
, va_list argptr
);
1059 // returns the string containing the result of Printf() to it
1060 static wxString
Format(const wxChar
*pszFormat
, ...) ATTRIBUTE_PRINTF_1
;
1061 // the same as above, but takes a va_list
1062 static wxString
FormatV(const wxChar
*pszFormat
, va_list argptr
);
1064 // raw access to string memory
1065 // ensure that string has space for at least nLen characters
1066 // only works if the data of this string is not shared
1067 bool Alloc(size_t nLen
) { reserve(nLen
); /*return capacity() >= nLen;*/ return true; }
1068 // minimize the string's memory
1069 // only works if the data of this string is not shared
1072 // get writable buffer of at least nLen bytes. Unget() *must* be called
1073 // a.s.a.p. to put string back in a reasonable state!
1074 wxChar
*GetWriteBuf(size_t nLen
);
1075 // call this immediately after GetWriteBuf() has been used
1076 void UngetWriteBuf();
1077 void UngetWriteBuf(size_t nLen
);
1080 // wxWidgets version 1 compatibility functions
1083 wxString
SubString(size_t from
, size_t to
) const
1084 { return Mid(from
, (to
- from
+ 1)); }
1085 // values for second parameter of CompareTo function
1086 enum caseCompare
{exact
, ignoreCase
};
1087 // values for first parameter of Strip function
1088 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
1091 // (take 'this' into account in attribute parameter count)
1092 int sprintf(const wxChar
*pszFormat
, ...) ATTRIBUTE_PRINTF_2
;
1095 inline int CompareTo(const wxChar
* psz
, caseCompare cmp
= exact
) const
1096 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
1099 size_t Length() const { return length(); }
1100 // Count the number of characters
1101 int Freq(wxChar ch
) const;
1103 void LowerCase() { MakeLower(); }
1105 void UpperCase() { MakeUpper(); }
1106 // use Trim except that it doesn't change this string
1107 wxString
Strip(stripType w
= trailing
) const;
1109 // use Find (more general variants not yet supported)
1110 size_t Index(const wxChar
* psz
) const { return Find(psz
); }
1111 size_t Index(wxChar ch
) const { return Find(ch
); }
1113 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
1114 wxString
& RemoveLast(size_t n
= 1) { return Truncate(length() - n
); }
1116 wxString
& Remove(size_t nStart
, size_t nLen
)
1117 { return (wxString
&)erase( nStart
, nLen
); }
1120 int First( const wxChar ch
) const { return Find(ch
); }
1121 int First( const wxChar
* psz
) const { return Find(psz
); }
1122 int First( const wxString
&str
) const { return Find(str
); }
1123 int Last( const wxChar ch
) const { return Find(ch
, true); }
1124 bool Contains(const wxString
& str
) const { return Find(str
) != wxNOT_FOUND
; }
1127 bool IsNull() const { return empty(); }
1129 // std::string compatibility functions
1131 // take nLen chars starting at nPos
1132 wxString(const wxString
& str
, size_t nPos
, size_t nLen
)
1133 : wxStringBase(str
, nPos
, nLen
) { }
1134 // take all characters from pStart to pEnd
1135 wxString(const void *pStart
, const void *pEnd
)
1136 : wxStringBase((const wxChar
*)pStart
, (const wxChar
*)pEnd
) { }
1138 wxString(const_iterator first
, const_iterator last
)
1139 : wxStringBase(first
, last
) { }
1142 // lib.string.modifiers
1143 // append elements str[pos], ..., str[pos+n]
1144 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
1145 { return (wxString
&)wxStringBase::append(str
, pos
, n
); }
1147 wxString
& append(const wxString
& str
)
1148 { return (wxString
&)wxStringBase::append(str
); }
1149 // append first n (or all if n == npos) characters of sz
1150 wxString
& append(const wxChar
*sz
)
1151 { return (wxString
&)wxStringBase::append(sz
); }
1152 wxString
& append(const wxChar
*sz
, size_t n
)
1153 { return (wxString
&)wxStringBase::append(sz
, n
); }
1154 // append n copies of ch
1155 wxString
& append(size_t n
, wxChar ch
)
1156 { return (wxString
&)wxStringBase::append(n
, ch
); }
1157 // append from first to last
1158 wxString
& append(const_iterator first
, const_iterator last
)
1159 { return (wxString
&)wxStringBase::append(first
, last
); }
1161 // same as `this_string = str'
1162 wxString
& assign(const wxString
& str
)
1163 { return (wxString
&)wxStringBase::assign(str
); }
1164 // same as ` = str[pos..pos + n]
1165 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
1166 { return (wxString
&)wxStringBase::assign(str
, pos
, n
); }
1167 // same as `= first n (or all if n == npos) characters of sz'
1168 wxString
& assign(const wxChar
*sz
)
1169 { return (wxString
&)wxStringBase::assign(sz
); }
1170 wxString
& assign(const wxChar
*sz
, size_t n
)
1171 { return (wxString
&)wxStringBase::assign(sz
, n
); }
1172 // same as `= n copies of ch'
1173 wxString
& assign(size_t n
, wxChar ch
)
1174 { return (wxString
&)wxStringBase::assign(n
, ch
); }
1175 // assign from first to last
1176 wxString
& assign(const_iterator first
, const_iterator last
)
1177 { return (wxString
&)wxStringBase::assign(first
, last
); }
1179 // string comparison
1180 #ifndef HAVE_STD_STRING_COMPARE
1181 int compare(const wxStringBase
& str
) const;
1182 // comparison with a substring
1183 int compare(size_t nStart
, size_t nLen
, const wxStringBase
& str
) const;
1184 // comparison of 2 substrings
1185 int compare(size_t nStart
, size_t nLen
,
1186 const wxStringBase
& str
, size_t nStart2
, size_t nLen2
) const;
1187 // just like strcmp()
1188 int compare(const wxChar
* sz
) const;
1189 // substring comparison with first nCount characters of sz
1190 int compare(size_t nStart
, size_t nLen
,
1191 const wxChar
* sz
, size_t nCount
= npos
) const;
1192 #endif // !defined HAVE_STD_STRING_COMPARE
1194 // insert another string
1195 wxString
& insert(size_t nPos
, const wxString
& str
)
1196 { return (wxString
&)wxStringBase::insert(nPos
, str
); }
1197 // insert n chars of str starting at nStart (in str)
1198 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
1199 { return (wxString
&)wxStringBase::insert(nPos
, str
, nStart
, n
); }
1200 // insert first n (or all if n == npos) characters of sz
1201 wxString
& insert(size_t nPos
, const wxChar
*sz
)
1202 { return (wxString
&)wxStringBase::insert(nPos
, sz
); }
1203 wxString
& insert(size_t nPos
, const wxChar
*sz
, size_t n
)
1204 { return (wxString
&)wxStringBase::insert(nPos
, sz
, n
); }
1205 // insert n copies of ch
1206 wxString
& insert(size_t nPos
, size_t n
, wxChar ch
)
1207 { return (wxString
&)wxStringBase::insert(nPos
, n
, ch
); }
1208 iterator
insert(iterator it
, wxChar ch
)
1209 { return wxStringBase::insert(it
, ch
); }
1210 void insert(iterator it
, const_iterator first
, const_iterator last
)
1211 { wxStringBase::insert(it
, first
, last
); }
1212 void insert(iterator it
, size_type n
, wxChar ch
)
1213 { wxStringBase::insert(it
, n
, ch
); }
1215 // delete characters from nStart to nStart + nLen
1216 wxString
& erase(size_type pos
= 0, size_type n
= npos
)
1217 { return (wxString
&)wxStringBase::erase(pos
, n
); }
1218 iterator
erase(iterator first
, iterator last
)
1219 { return wxStringBase::erase(first
, last
); }
1220 iterator
erase(iterator first
)
1221 { return wxStringBase::erase(first
); }
1223 #ifdef wxSTRING_BASE_HASNT_CLEAR
1224 void clear() { erase(); }
1227 // replaces the substring of length nLen starting at nStart
1228 wxString
& replace(size_t nStart
, size_t nLen
, const wxChar
* sz
)
1229 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, sz
); }
1230 // replaces the substring of length nLen starting at nStart
1231 wxString
& replace(size_t nStart
, size_t nLen
, const wxString
& str
)
1232 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, str
); }
1233 // replaces the substring with nCount copies of ch
1234 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxChar ch
)
1235 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, nCount
, ch
); }
1236 // replaces a substring with another substring
1237 wxString
& replace(size_t nStart
, size_t nLen
,
1238 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1239 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, str
,
1241 // replaces the substring with first nCount chars of sz
1242 wxString
& replace(size_t nStart
, size_t nLen
,
1243 const wxChar
* sz
, size_t nCount
)
1244 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, sz
, nCount
); }
1245 wxString
& replace(iterator first
, iterator last
, const_pointer s
)
1246 { return (wxString
&)wxStringBase::replace(first
, last
, s
); }
1247 wxString
& replace(iterator first
, iterator last
, const_pointer s
,
1249 { return (wxString
&)wxStringBase::replace(first
, last
, s
, n
); }
1250 wxString
& replace(iterator first
, iterator last
, const wxString
& s
)
1251 { return (wxString
&)wxStringBase::replace(first
, last
, s
); }
1252 wxString
& replace(iterator first
, iterator last
, size_type n
, wxChar c
)
1253 { return (wxString
&)wxStringBase::replace(first
, last
, n
, c
); }
1254 wxString
& replace(iterator first
, iterator last
,
1255 const_iterator first1
, const_iterator last1
)
1256 { return (wxString
&)wxStringBase::replace(first
, last
, first1
, last1
); }
1259 wxString
& operator+=(const wxString
& s
)
1260 { return (wxString
&)wxStringBase::operator+=(s
); }
1261 // string += C string
1262 wxString
& operator+=(const wxChar
*psz
)
1263 { return (wxString
&)wxStringBase::operator+=(psz
); }
1265 wxString
& operator+=(wxChar ch
)
1266 { return (wxString
&)wxStringBase::operator+=(ch
); }
1269 // define wxArrayString, for compatibility
1270 #if WXWIN_COMPATIBILITY_2_4 && !wxUSE_STL
1271 #include "wx/arrstr.h"
1275 // return an empty wxString (not very useful with wxUSE_STL == 1)
1276 inline const wxString
wxGetEmptyString() { return wxString(); }
1278 // return an empty wxString (more efficient than wxString() here)
1279 inline const wxString
& wxGetEmptyString()
1281 return *(wxString
*)&wxEmptyString
;
1283 #endif // wxUSE_STL/!wxUSE_STL
1285 // ----------------------------------------------------------------------------
1286 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
1287 // ----------------------------------------------------------------------------
1291 class WXDLLIMPEXP_BASE wxStringBuffer
1294 wxStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
1295 : m_str(str
), m_buf(lenWanted
)
1298 ~wxStringBuffer() { m_str
.assign(m_buf
.data(), wxStrlen(m_buf
.data())); }
1300 operator wxChar
*() { return m_buf
.data(); }
1305 wxWCharBuffer m_buf
;
1310 DECLARE_NO_COPY_CLASS(wxStringBuffer
)
1313 class WXDLLIMPEXP_BASE wxStringBufferLength
1316 wxStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
1317 : m_str(str
), m_buf(lenWanted
), m_len(0), m_lenSet(false)
1320 ~wxStringBufferLength()
1323 m_str
.assign(m_buf
.data(), m_len
);
1326 operator wxChar
*() { return m_buf
.data(); }
1327 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
1332 wxWCharBuffer m_buf
;
1339 DECLARE_NO_COPY_CLASS(wxStringBufferLength
)
1342 #else // if !wxUSE_STL
1344 class WXDLLIMPEXP_BASE wxStringBuffer
1347 wxStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
1348 : m_str(str
), m_buf(NULL
)
1349 { m_buf
= m_str
.GetWriteBuf(lenWanted
); }
1351 ~wxStringBuffer() { m_str
.UngetWriteBuf(); }
1353 operator wxChar
*() const { return m_buf
; }
1359 DECLARE_NO_COPY_CLASS(wxStringBuffer
)
1362 class WXDLLIMPEXP_BASE wxStringBufferLength
1365 wxStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
1366 : m_str(str
), m_buf(NULL
), m_len(0), m_lenSet(false)
1368 m_buf
= m_str
.GetWriteBuf(lenWanted
);
1369 wxASSERT(m_buf
!= NULL
);
1372 ~wxStringBufferLength()
1375 m_str
.UngetWriteBuf(m_len
);
1378 operator wxChar
*() const { return m_buf
; }
1379 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
1387 DECLARE_NO_COPY_CLASS(wxStringBufferLength
)
1390 #endif // !wxUSE_STL
1392 // ---------------------------------------------------------------------------
1393 // wxString comparison functions: operator versions are always case sensitive
1394 // ---------------------------------------------------------------------------
1396 // note that when wxUSE_STL == 1 the comparison operators taking std::string
1397 // are used and defining them also for wxString would only result in
1398 // compilation ambiguities when comparing std::string and wxString
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
.Len() != s2
.Len()) || (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; }
1431 inline bool operator>=(const wxString
& s1
, const wxString
& s2
)
1432 { return s1
.Cmp(s2
) >= 0; }
1433 inline bool operator>=(const wxString
& s1
, const wxChar
* s2
)
1434 { return s1
.Cmp(s2
) >= 0; }
1435 inline bool operator>=(const wxChar
* s1
, const wxString
& s2
)
1436 { return s2
.Cmp(s1
) <= 0; }
1439 inline bool operator==(const wxString
& s1
, const wxWCharBuffer
& s2
)
1440 { return (s1
.Cmp((const wchar_t *)s2
) == 0); }
1441 inline bool operator==(const wxWCharBuffer
& s1
, const wxString
& s2
)
1442 { return (s2
.Cmp((const wchar_t *)s1
) == 0); }
1443 inline bool operator!=(const wxString
& s1
, const wxWCharBuffer
& s2
)
1444 { return (s1
.Cmp((const wchar_t *)s2
) != 0); }
1445 inline bool operator!=(const wxWCharBuffer
& s1
, const wxString
& s2
)
1446 { return (s2
.Cmp((const wchar_t *)s1
) != 0); }
1447 #else // !wxUSE_UNICODE
1448 inline bool operator==(const wxString
& s1
, const wxCharBuffer
& s2
)
1449 { return (s1
.Cmp((const char *)s2
) == 0); }
1450 inline bool operator==(const wxCharBuffer
& s1
, const wxString
& s2
)
1451 { return (s2
.Cmp((const char *)s1
) == 0); }
1452 inline bool operator!=(const wxString
& s1
, const wxCharBuffer
& s2
)
1453 { return (s1
.Cmp((const char *)s2
) != 0); }
1454 inline bool operator!=(const wxCharBuffer
& s1
, const wxString
& s2
)
1455 { return (s2
.Cmp((const char *)s1
) != 0); }
1456 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1458 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
, const wxString
& string2
);
1459 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxChar ch
);
1460 wxString WXDLLIMPEXP_BASE
operator+(wxChar ch
, const wxString
& string
);
1461 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const wxChar
*psz
);
1462 wxString WXDLLIMPEXP_BASE
operator+(const wxChar
*psz
, const wxString
& string
);
1465 inline wxString
operator+(const wxString
& string
, const wxWCharBuffer
& buf
)
1466 { return string
+ (const wchar_t *)buf
; }
1467 inline wxString
operator+(const wxWCharBuffer
& buf
, const wxString
& string
)
1468 { return (const wchar_t *)buf
+ string
; }
1469 #else // !wxUSE_UNICODE
1470 inline wxString
operator+(const wxString
& string
, const wxCharBuffer
& buf
)
1471 { return string
+ (const char *)buf
; }
1472 inline wxString
operator+(const wxCharBuffer
& buf
, const wxString
& string
)
1473 { return (const char *)buf
+ string
; }
1474 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1476 #endif // !wxUSE_STL
1478 // comparison with char (those are not defined by std::[w]string and so should
1479 // be always available)
1480 inline bool operator==(wxChar c
, const wxString
& s
) { return s
.IsSameAs(c
); }
1481 inline bool operator==(const wxString
& s
, wxChar c
) { return s
.IsSameAs(c
); }
1482 inline bool operator!=(wxChar c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
1483 inline bool operator!=(const wxString
& s
, wxChar c
) { return !s
.IsSameAs(c
); }
1485 // ---------------------------------------------------------------------------
1486 // Implementation only from here until the end of file
1487 // ---------------------------------------------------------------------------
1489 // don't pollute the library user's name space
1490 #undef wxASSERT_VALID_INDEX
1492 #if wxUSE_STD_IOSTREAM
1494 #include "wx/iosfwrap.h"
1496 WXDLLIMPEXP_BASE wxSTD istream
& operator>>(wxSTD istream
&, wxString
&);
1497 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxString
&);
1499 #endif // wxSTD_STRING_COMPATIBILITY
1501 #endif // _WX_WXSTRINGH__