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 || 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 #if !defined(HAVE_STD_STRING_COMPARE) && \
200 (!defined(__WX_SETUP_H__) || wxUSE_STL == 0)
201 #define HAVE_STD_STRING_COMPARE
204 // ---------------------------------------------------------------------------
205 // string data prepended with some housekeeping info (used by wxString class),
206 // is never used directly (but had to be put here to allow inlining)
207 // ---------------------------------------------------------------------------
209 struct WXDLLIMPEXP_BASE wxStringData
211 int nRefs
; // reference count
212 size_t nDataLength
, // actual string length
213 nAllocLength
; // allocated memory size
215 // mimics declaration 'wxChar data[nAllocLength]'
216 wxChar
* data() const { return (wxChar
*)(this + 1); }
218 // empty string has a special ref count so it's never deleted
219 bool IsEmpty() const { return (nRefs
== -1); }
220 bool IsShared() const { return (nRefs
> 1); }
223 void Lock() { if ( !IsEmpty() ) nRefs
++; }
225 // VC++ will refuse to inline Unlock but profiling shows that it is wrong
226 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
229 // VC++ free must take place in same DLL as allocation when using non dll
230 // run-time library (e.g. Multithreaded instead of Multithreaded DLL)
231 #if defined(__VISUALC__) && defined(_MT) && !defined(_DLL)
232 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) Free(); }
233 // we must not inline deallocation since allocation is not inlined
236 void Unlock() { if ( !IsEmpty() && --nRefs
== 0) free(this); }
239 // if we had taken control over string memory (GetWriteBuf), it's
240 // intentionally put in invalid state
241 void Validate(bool b
) { nRefs
= (b
? 1 : 0); }
242 bool IsValid() const { return (nRefs
!= 0); }
245 class WXDLLIMPEXP_BASE wxStringBase
248 friend class WXDLLIMPEXP_BASE wxArrayString
;
251 // an 'invalid' value for string index, moved to this place due to a CW bug
252 static const size_t npos
;
254 // points to data preceded by wxStringData structure with ref count info
257 // accessor to string data
258 wxStringData
* GetStringData() const { return (wxStringData
*)m_pchData
- 1; }
260 // string (re)initialization functions
261 // initializes the string to the empty value (must be called only from
262 // ctors, use Reinit() otherwise)
263 void Init() { m_pchData
= (wxChar
*)wxEmptyString
; }
264 // initializaes the string with (a part of) C-string
265 void InitWith(const wxChar
*psz
, size_t nPos
= 0, size_t nLen
= npos
);
266 // as Init, but also frees old data
267 void Reinit() { GetStringData()->Unlock(); Init(); }
270 // allocates memory for string of length nLen
271 bool AllocBuffer(size_t nLen
);
272 // copies data to another string
273 bool AllocCopy(wxString
&, int, int) const;
274 // effectively copies data to string
275 bool AssignCopy(size_t, const wxChar
*);
277 // append a (sub)string
278 bool ConcatSelf(size_t nLen
, const wxChar
*src
, size_t nMaxLen
);
279 bool ConcatSelf(size_t nLen
, const wxChar
*src
)
280 { return ConcatSelf(nLen
, src
, nLen
); }
282 // functions called before writing to the string: they copy it if there
283 // are other references to our data (should be the only owner when writing)
284 bool CopyBeforeWrite();
285 bool AllocBeforeWrite(size_t);
287 // compatibility with wxString
288 bool Alloc(size_t nLen
);
291 typedef wxChar value_type
;
292 typedef wxChar char_type
;
293 typedef size_t size_type
;
294 typedef value_type
& reference
;
295 typedef const value_type
& const_reference
;
296 typedef value_type
* pointer
;
297 typedef const value_type
* const_pointer
;
298 typedef value_type
*iterator
;
299 typedef const value_type
*const_iterator
;
301 // constructors and destructor
302 // ctor for an empty string
303 wxStringBase() { Init(); }
305 wxStringBase(const wxStringBase
& stringSrc
)
307 wxASSERT_MSG( stringSrc
.GetStringData()->IsValid(),
308 _T("did you forget to call UngetWriteBuf()?") );
310 if ( stringSrc
.empty() ) {
311 // nothing to do for an empty string
315 m_pchData
= stringSrc
.m_pchData
; // share same data
316 GetStringData()->Lock(); // => one more copy
319 // string containing nRepeat copies of ch
320 wxStringBase(size_type nRepeat
, wxChar ch
);
321 // ctor takes first nLength characters from C string
322 // (default value of npos means take all the string)
323 wxStringBase(const wxChar
*psz
)
324 { InitWith(psz
, 0, npos
); }
325 wxStringBase(const wxChar
*psz
, size_t nLength
)
326 { InitWith(psz
, 0, nLength
); }
327 wxStringBase(const wxChar
*psz
, wxMBConv
& WXUNUSED(conv
), size_t nLength
= npos
)
328 { InitWith(psz
, 0, nLength
); }
329 // take nLen chars starting at nPos
330 wxStringBase(const wxStringBase
& str
, size_t nPos
, size_t nLen
)
332 wxASSERT_MSG( str
.GetStringData()->IsValid(),
333 _T("did you forget to call UngetWriteBuf()?") );
335 size_t strLen
= str
.length() - nPos
; nLen
= strLen
< nLen
? strLen
: nLen
;
336 InitWith(str
.c_str(), nPos
, nLen
);
338 // take all characters from pStart to pEnd
339 wxStringBase(const void *pStart
, const void *pEnd
);
341 // dtor is not virtual, this class must not be inherited from!
344 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
345 //RN - according to the above VC++ does indeed inline this,
346 //even though it spits out two warnings
347 #pragma warning (disable:4714)
350 GetStringData()->Unlock();
353 #if defined(__VISUALC__) && (__VISUALC__ >= 1200)
354 //re-enable inlining warning
355 #pragma warning (default:4714)
357 // overloaded assignment
358 // from another wxString
359 wxStringBase
& operator=(const wxStringBase
& stringSrc
);
361 wxStringBase
& operator=(wxChar ch
);
363 wxStringBase
& operator=(const wxChar
*psz
);
365 // return the length of the string
366 size_type
size() const { return GetStringData()->nDataLength
; }
367 // return the length of the string
368 size_type
length() const { return size(); }
369 // return the maximum size of the string
370 size_type
max_size() const { return wxSTRING_MAXLEN
; }
371 // resize the string, filling the space with c if c != 0
372 void resize(size_t nSize
, wxChar ch
= wxT('\0'));
373 // delete the contents of the string
374 void clear() { erase(0, npos
); }
375 // returns true if the string is empty
376 bool empty() const { return size() == 0; }
377 // inform string about planned change in size
378 void reserve(size_t sz
) { Alloc(sz
); }
379 size_type
capacity() const { return GetStringData()->nAllocLength
; }
382 // return the character at position n
383 value_type
at(size_type n
) const
384 { wxASSERT_VALID_INDEX( n
); return m_pchData
[n
]; }
385 // returns the writable character at position n
386 reference
at(size_type n
)
387 { wxASSERT_VALID_INDEX( n
); CopyBeforeWrite(); return m_pchData
[n
]; }
389 // lib.string.modifiers
390 // append elements str[pos], ..., str[pos+n]
391 wxStringBase
& append(const wxStringBase
& str
, size_t pos
, size_t n
)
393 wxASSERT(pos
<= str
.length());
394 ConcatSelf(n
, str
.c_str() + pos
, str
.length() - pos
);
398 wxStringBase
& append(const wxStringBase
& str
)
399 { ConcatSelf(str
.length(), str
.c_str()); return *this; }
400 // append first n (or all if n == npos) characters of sz
401 wxStringBase
& append(const wxChar
*sz
)
402 { ConcatSelf(wxStrlen(sz
), sz
); return *this; }
403 wxStringBase
& append(const wxChar
*sz
, size_t n
)
404 { ConcatSelf(n
, sz
); return *this; }
405 // append n copies of ch
406 wxStringBase
& append(size_t n
, wxChar ch
);
407 // append from first to last
408 wxStringBase
& append(const_iterator first
, const_iterator last
)
409 { ConcatSelf(last
- first
, first
); return *this; }
411 // same as `this_string = str'
412 wxStringBase
& assign(const wxStringBase
& str
)
413 { return *this = str
; }
414 // same as ` = str[pos..pos + n]
415 wxStringBase
& assign(const wxStringBase
& str
, size_t pos
, size_t n
)
416 { clear(); return append(str
, pos
, n
); }
417 // same as `= first n (or all if n == npos) characters of sz'
418 wxStringBase
& assign(const wxChar
*sz
)
419 { clear(); return append(sz
, wxStrlen(sz
)); }
420 wxStringBase
& assign(const wxChar
*sz
, size_t n
)
421 { clear(); return append(sz
, n
); }
422 // same as `= n copies of ch'
423 wxStringBase
& assign(size_t n
, wxChar ch
)
424 { clear(); return append(n
, ch
); }
425 // assign from first to last
426 wxStringBase
& assign(const_iterator first
, const_iterator last
)
427 { clear(); return append(first
, last
); }
429 // first valid index position
430 const_iterator
begin() const { return m_pchData
; }
431 // position one after the last valid one
432 const_iterator
end() const { return m_pchData
+ length(); }
434 // first valid index position
436 // position one after the last valid one
439 // insert another string
440 wxStringBase
& insert(size_t nPos
, const wxStringBase
& str
)
442 wxASSERT( str
.GetStringData()->IsValid() );
443 return insert(nPos
, str
.c_str(), str
.length());
445 // insert n chars of str starting at nStart (in str)
446 wxStringBase
& insert(size_t nPos
, const wxStringBase
& str
, size_t nStart
, size_t n
)
448 wxASSERT( str
.GetStringData()->IsValid() );
449 wxASSERT( nStart
< str
.length() );
450 size_t strLen
= str
.length() - nStart
;
451 n
= strLen
< n
? strLen
: n
;
452 return insert(nPos
, str
.c_str() + nStart
, n
);
454 // insert first n (or all if n == npos) characters of sz
455 wxStringBase
& insert(size_t nPos
, const wxChar
*sz
, size_t n
= npos
);
456 // insert n copies of ch
457 wxStringBase
& insert(size_t nPos
, size_t n
, wxChar ch
)
458 { return insert(nPos
, wxStringBase(n
, ch
)); }
459 iterator
insert(iterator it
, wxChar ch
)
460 { size_t idx
= it
- begin(); insert(idx
, 1, ch
); return begin() + idx
; }
461 void insert(iterator it
, const_iterator first
, const_iterator last
)
462 { insert(it
- begin(), first
, last
- first
); }
463 void insert(iterator it
, size_type n
, wxChar ch
)
464 { insert(it
- begin(), n
, ch
); }
466 // delete characters from nStart to nStart + nLen
467 wxStringBase
& erase(size_type pos
= 0, size_type n
= npos
);
468 iterator
erase(iterator first
, iterator last
)
470 size_t idx
= first
- begin();
471 erase(idx
, last
- first
);
472 return begin() + idx
;
474 iterator
erase(iterator first
);
476 // explicit conversion to C string (use this with printf()!)
477 const wxChar
* c_str() const { return m_pchData
; }
478 const wxChar
* data() const { return m_pchData
; }
480 // replaces the substring of length nLen starting at nStart
481 wxStringBase
& replace(size_t nStart
, size_t nLen
, const wxChar
* sz
);
482 // replaces the substring of length nLen starting at nStart
483 wxStringBase
& replace(size_t nStart
, size_t nLen
, const wxStringBase
& str
)
484 { return replace(nStart
, nLen
, str
.c_str()); }
485 // replaces the substring with nCount copies of ch
486 wxStringBase
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxChar ch
);
487 // replaces a substring with another substring
488 wxStringBase
& replace(size_t nStart
, size_t nLen
,
489 const wxStringBase
& str
, size_t nStart2
, size_t nLen2
);
490 // replaces the substring with first nCount chars of sz
491 wxStringBase
& replace(size_t nStart
, size_t nLen
,
492 const wxChar
* sz
, size_t nCount
);
493 wxStringBase
& replace(iterator first
, iterator last
, const_pointer s
)
494 { return replace(first
- begin(), last
- first
, s
); }
495 wxStringBase
& replace(iterator first
, iterator last
, const_pointer s
,
497 { return replace(first
- begin(), last
- first
, s
, n
); }
498 wxStringBase
& replace(iterator first
, iterator last
, const wxStringBase
& s
)
499 { return replace(first
- begin(), last
- first
, s
); }
500 wxStringBase
& replace(iterator first
, iterator last
, size_type n
, wxChar c
)
501 { return replace(first
- begin(), last
- first
, n
, c
); }
502 wxStringBase
& replace(iterator first
, iterator last
,
503 const_iterator first1
, const_iterator last1
)
504 { return replace(first
- begin(), last
- first
, first1
, last1
- first1
); }
507 void swap(wxStringBase
& str
);
509 // All find() functions take the nStart argument which specifies the
510 // position to start the search on, the default value is 0. All functions
511 // return npos if there were no match.
514 size_t find(const wxStringBase
& str
, size_t nStart
= 0) const;
516 // find first n characters of sz
517 size_t find(const wxChar
* sz
, size_t nStart
= 0, size_t n
= npos
) const;
519 // find the first occurence of character ch after nStart
520 size_t find(wxChar ch
, size_t nStart
= 0) const;
522 // rfind() family is exactly like find() but works right to left
524 // as find, but from the end
525 size_t rfind(const wxStringBase
& str
, size_t nStart
= npos
) const;
527 // as find, but from the end
528 size_t rfind(const wxChar
* sz
, size_t nStart
= npos
,
529 size_t n
= npos
) const;
530 // as find, but from the end
531 size_t rfind(wxChar ch
, size_t nStart
= npos
) const;
533 // find first/last occurence of any character in the set
535 // as strpbrk() but starts at nStart, returns npos if not found
536 size_t find_first_of(const wxStringBase
& str
, size_t nStart
= 0) const
537 { return find_first_of(str
.c_str(), nStart
); }
539 size_t find_first_of(const wxChar
* sz
, size_t nStart
= 0) const;
540 size_t find_first_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
541 // same as find(char, size_t)
542 size_t find_first_of(wxChar c
, size_t nStart
= 0) const
543 { return find(c
, nStart
); }
544 // find the last (starting from nStart) char from str in this string
545 size_t find_last_of (const wxStringBase
& str
, size_t nStart
= npos
) const
546 { return find_last_of(str
.c_str(), nStart
); }
548 size_t find_last_of (const wxChar
* sz
, size_t nStart
= npos
) const;
549 size_t find_last_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
551 size_t find_last_of(wxChar c
, size_t nStart
= npos
) const
552 { return rfind(c
, nStart
); }
554 // find first/last occurence of any character not in the set
556 // as strspn() (starting from nStart), returns npos on failure
557 size_t find_first_not_of(const wxStringBase
& str
, size_t nStart
= 0) const
558 { return find_first_not_of(str
.c_str(), nStart
); }
560 size_t find_first_not_of(const wxChar
* sz
, size_t nStart
= 0) const;
561 size_t find_first_not_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
563 size_t find_first_not_of(wxChar ch
, size_t nStart
= 0) const;
565 size_t find_last_not_of(const wxStringBase
& str
, size_t nStart
= npos
) const
566 { return find_last_not_of(str
.c_str(), nStart
); }
568 size_t find_last_not_of(const wxChar
* sz
, size_t nStart
= npos
) const;
569 size_t find_last_not_of(const wxChar
* sz
, size_t nStart
, size_t n
) const;
571 size_t find_last_not_of(wxChar ch
, size_t nStart
= npos
) const;
573 // All compare functions return -1, 0 or 1 if the [sub]string is less,
574 // equal or greater than the compare() argument.
576 // comparison with another string
577 int compare(const wxStringBase
& str
) const;
578 // comparison with a substring
579 int compare(size_t nStart
, size_t nLen
, const wxStringBase
& str
) const;
580 // comparison of 2 substrings
581 int compare(size_t nStart
, size_t nLen
,
582 const wxStringBase
& str
, size_t nStart2
, size_t nLen2
) const;
583 // comparison with a c string
584 int compare(const wxChar
* sz
) const;
585 // substring comparison with first nCount characters of sz
586 int compare(size_t nStart
, size_t nLen
,
587 const wxChar
* sz
, size_t nCount
= npos
) const;
589 size_type
copy(wxChar
* s
, size_type n
, size_type pos
= 0);
591 // substring extraction
592 wxStringBase
substr(size_t nStart
= 0, size_t nLen
= npos
) const;
595 wxStringBase
& operator+=(const wxStringBase
& s
) { return append(s
); }
596 // string += C string
597 wxStringBase
& operator+=(const wxChar
*psz
) { return append(psz
); }
599 wxStringBase
& operator+=(wxChar ch
) { return append(1, ch
); }
604 // ----------------------------------------------------------------------------
605 // wxString: string class trying to be compatible with std::string, MFC
606 // CString and wxWindows 1.x wxString all at once
607 // ---------------------------------------------------------------------------
609 class WXDLLIMPEXP_BASE wxString
: public wxStringBase
612 friend class WXDLLIMPEXP_BASE wxArrayString
;
615 // NB: special care was taken in arranging the member functions in such order
616 // that all inline functions can be effectively inlined, verify that all
617 // performace critical functions are still inlined if you change order!
619 // if we hadn't made these operators private, it would be possible to
620 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
621 // converted to char in C and we do have operator=(char)
623 // NB: we don't need other versions (short/long and unsigned) as attempt
624 // to assign another numeric type to wxString will now result in
625 // ambiguity between operator=(char) and operator=(int)
626 wxString
& operator=(int);
628 // these methods are not implemented - there is _no_ conversion from int to
629 // string, you're doing something wrong if the compiler wants to call it!
631 // try `s << i' or `s.Printf("%d", i)' instead
635 // constructors and destructor
636 // ctor for an empty string
637 wxString() : wxStringBase() { }
639 wxString(const wxStringBase
& stringSrc
) : wxStringBase(stringSrc
) { }
640 wxString(const wxString
& stringSrc
) : wxStringBase(stringSrc
) { }
641 // string containing nRepeat copies of ch
642 wxString(wxChar ch
, size_t nRepeat
= 1)
643 : wxStringBase(nRepeat
, ch
) { }
644 wxString(size_t nRepeat
, wxChar ch
)
645 : wxStringBase(nRepeat
, ch
) { }
646 // ctor takes first nLength characters from C string
647 // (default value of npos means take all the string)
648 wxString(const wxChar
*psz
)
649 : wxStringBase(psz
? psz
: wxT("")) { }
650 wxString(const wxChar
*psz
, size_t nLength
)
651 : wxStringBase(psz
, nLength
) { }
652 wxString(const wxChar
*psz
, wxMBConv
& WXUNUSED(conv
), size_t nLength
= npos
)
653 : wxStringBase(psz
, nLength
== npos
? wxStrlen(psz
) : nLength
) { }
655 // even we're not build with wxUSE_STL == 1 it is very convenient to allow
656 // implicit conversions from std::string to wxString as this allows to use
657 // the same strings in non-GUI and GUI code, however we don't want to
658 // unconditionally add this ctor as it would make wx lib dependent on
659 // libstdc++ on some Linux versions which is bad, so instead we ask the
660 // client code to define this wxUSE_STD_STRING symbol if they need it
662 wxString(const wxStdString
& s
)
663 : wxStringBase(s
.c_str()) { }
664 #endif // wxUSE_STD_STRING
667 // from multibyte string
668 wxString(const char *psz
, wxMBConv
& conv
, size_t nLength
= npos
);
669 // from wxWCharBuffer (i.e. return from wxGetString)
670 wxString(const wxWCharBuffer
& psz
) : wxStringBase(psz
.data()) { }
672 // from C string (for compilers using unsigned char)
673 wxString(const unsigned char* psz
, size_t nLength
= npos
)
674 : wxStringBase((const char*)psz
, nLength
) { }
677 // from wide (Unicode) string
678 wxString(const wchar_t *pwz
, wxMBConv
& conv
= wxConvLibc
, size_t nLength
= npos
);
679 #endif // !wxUSE_WCHAR_T
682 wxString(const wxCharBuffer
& psz
)
683 : wxStringBase(psz
) { }
684 #endif // Unicode/ANSI
686 // generic attributes & operations
687 // as standard strlen()
688 size_t Len() const { return length(); }
689 // string contains any characters?
690 bool IsEmpty() const { return empty(); }
691 // empty string is "false", so !str will return true
692 bool operator!() const { return IsEmpty(); }
693 // truncate the string to given length
694 wxString
& Truncate(size_t uiLen
);
695 // empty string contents
700 wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") );
702 // empty the string and free memory
705 wxString
tmp(wxEmptyString
);
711 bool IsAscii() const;
713 bool IsNumber() const;
717 // data access (all indexes are 0 based)
719 wxChar
GetChar(size_t n
) const
722 wxChar
& GetWritableChar(size_t n
)
725 void SetChar(size_t n
, wxChar ch
)
728 // get last character
731 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
733 return at(length() - 1);
736 // get writable last character
739 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
740 return at(length() - 1);
744 Note that we we must define all of the overloads below to avoid
745 ambiguity when using str[0]. Also note that for a conforming compiler we
746 don't need const version of operatorp[] at all as indexed access to
747 const string is provided by implicit conversion to "const wxChar *"
748 below and defining them would only result in ambiguities, but some other
749 compilers refuse to compile "str[0]" without them.
752 #if defined(__BORLANDC__) || defined(__WATCOMC__) || defined(__MWERKS__)
753 wxChar
operator[](int n
) const
754 { return wxStringBase::at(n
); }
755 wxChar
operator[](size_type n
) const
756 { return wxStringBase::at(n
); }
757 #ifndef wxSIZE_T_IS_UINT
758 wxChar
operator[](unsigned int n
) const
759 { return wxStringBase::at(n
); }
760 #endif // size_t != unsigned int
761 #endif // broken compiler
764 // operator versions of GetWriteableChar()
765 wxChar
& operator[](int n
)
766 { return wxStringBase::at(n
); }
767 wxChar
& operator[](size_type n
)
768 { return wxStringBase::at(n
); }
769 #ifndef wxSIZE_T_IS_UINT
770 wxChar
& operator[](unsigned int n
)
771 { return wxStringBase::at(n
); }
772 #endif // size_t != unsigned int
774 // implicit conversion to C string
775 operator const wxChar
*() const { return c_str(); }
777 // identical to c_str(), for wxWin 1.6x compatibility
778 const wxChar
* wx_str() const { return c_str(); }
779 // identical to c_str(), for MFC compatibility
780 const wxChar
* GetData() const { return c_str(); }
782 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
783 // converting numbers or strings which are certain not to contain special
784 // chars (typically system functions, X atoms, environment variables etc.)
786 // the behaviour of these functions with the strings containing anything
787 // else than 7 bit ASCII characters is undefined, use at your own risk.
789 static wxString
FromAscii(const char *ascii
); // string
790 static wxString
FromAscii(const char ascii
); // char
791 const wxCharBuffer
ToAscii() const;
793 static wxString
FromAscii(const char *ascii
) { return wxString( ascii
); }
794 static wxString
FromAscii(const char ascii
) { return wxString( ascii
); }
795 const char *ToAscii() const { return c_str(); }
796 #endif // Unicode/!Unicode
798 // conversions with (possible) format conversions: have to return a
799 // buffer with temporary data
801 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
802 // return an ANSI (multibyte) string, wc_str() to return a wide string and
803 // fn_str() to return a string which should be used with the OS APIs
804 // accepting the file names. The return value is always the same, but the
805 // type differs because a function may either return pointer to the buffer
806 // directly or have to use intermediate buffer for translation.
808 const wxCharBuffer
mb_str(wxMBConv
& conv
= wxConvLibc
) const;
810 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
812 const wxChar
* wc_str() const { return c_str(); }
814 // for compatibility with !wxUSE_UNICODE version
815 const wxChar
* wc_str(wxMBConv
& WXUNUSED(conv
)) const { return c_str(); }
818 const wxCharBuffer
fn_str() const { return mb_str(wxConvFile
); }
820 const wxChar
* fn_str() const { return c_str(); }
821 #endif // wxMBFILES/!wxMBFILES
823 const wxChar
* mb_str() const { return c_str(); }
825 // for compatibility with wxUSE_UNICODE version
826 const wxChar
* mb_str(wxMBConv
& WXUNUSED(conv
)) const { return c_str(); }
828 const wxWX2MBbuf
mbc_str() const { return mb_str(); }
831 const wxWCharBuffer
wc_str(wxMBConv
& conv
) const;
832 #endif // wxUSE_WCHAR_T
834 const wxCharBuffer
fn_str() const { return wxConvFile
.cWC2WX( wc_str( wxConvLocal
) ); }
836 const wxChar
* fn_str() const { return c_str(); }
838 #endif // Unicode/ANSI
840 // overloaded assignment
841 // from another wxString
842 wxString
& operator=(const wxStringBase
& stringSrc
)
843 { return (wxString
&)wxStringBase::operator=(stringSrc
); }
845 wxString
& operator=(wxChar ch
)
846 { return (wxString
&)wxStringBase::operator=(ch
); }
847 // from a C string - STL probably will crash on NULL,
848 // so we need to compensate in that case
850 wxString
& operator=(const wxChar
*psz
)
851 { if(psz
) wxStringBase::operator=(psz
); else Clear(); return *this; }
853 wxString
& operator=(const wxChar
*psz
)
854 { return (wxString
&)wxStringBase::operator=(psz
); }
858 // from wxWCharBuffer
859 wxString
& operator=(const wxWCharBuffer
& psz
)
860 { (void) operator=((const wchar_t *)psz
); return *this; }
862 // from another kind of C string
863 wxString
& operator=(const unsigned char* psz
);
865 // from a wide string
866 wxString
& operator=(const wchar_t *pwz
);
869 wxString
& operator=(const wxCharBuffer
& psz
)
870 { (void) operator=((const char *)psz
); return *this; }
871 #endif // Unicode/ANSI
873 // string concatenation
874 // in place concatenation
876 Concatenate and return the result. Note that the left to right
877 associativity of << allows to write things like "str << str1 << str2
878 << ..." (unlike with +=)
881 wxString
& operator<<(const wxString
& s
)
884 wxASSERT_MSG( s
.GetStringData()->IsValid(),
885 _T("did you forget to call UngetWriteBuf()?") );
891 // string += C string
892 wxString
& operator<<(const wxChar
*psz
)
893 { append(psz
); return *this; }
895 wxString
& operator<<(wxChar ch
) { append(1, ch
); return *this; }
897 // string += buffer (i.e. from wxGetString)
899 wxString
& operator<<(const wxWCharBuffer
& s
)
900 { (void)operator<<((const wchar_t *)s
); return *this; }
901 void operator+=(const wxWCharBuffer
& s
)
902 { (void)operator<<((const wchar_t *)s
); }
903 #else // !wxUSE_UNICODE
904 wxString
& operator<<(const wxCharBuffer
& s
)
905 { (void)operator<<((const char *)s
); return *this; }
906 void operator+=(const wxCharBuffer
& s
)
907 { (void)operator<<((const char *)s
); }
908 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
910 // string += C string
911 wxString
& Append(const wxString
& s
)
913 // test for empty() to share the string if possible
920 wxString
& Append(const wxChar
* psz
)
921 { append(psz
); return *this; }
922 // append count copies of given character
923 wxString
& Append(wxChar ch
, size_t count
= 1u)
924 { append(count
, ch
); return *this; }
925 wxString
& Append(const wxChar
* psz
, size_t nLen
)
926 { append(psz
, nLen
); return *this; }
928 // prepend a string, return the string itself
929 wxString
& Prepend(const wxString
& str
)
930 { *this = str
+ *this; return *this; }
932 // non-destructive concatenation
934 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
, const wxString
& string2
);
936 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxChar ch
);
938 friend wxString WXDLLIMPEXP_BASE
operator+(wxChar ch
, const wxString
& string
);
940 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const wxChar
*psz
);
942 friend wxString WXDLLIMPEXP_BASE
operator+(const wxChar
*psz
, const wxString
& string
);
944 // stream-like functions
945 // insert an int into string
946 wxString
& operator<<(int i
)
947 { return (*this) << Format(_T("%d"), i
); }
948 // insert an unsigned int into string
949 wxString
& operator<<(unsigned int ui
)
950 { return (*this) << Format(_T("%u"), ui
); }
951 // insert a long into string
952 wxString
& operator<<(long l
)
953 { return (*this) << Format(_T("%ld"), l
); }
954 // insert an unsigned long into string
955 wxString
& operator<<(unsigned long ul
)
956 { return (*this) << Format(_T("%lu"), ul
); }
957 // insert a float into string
958 wxString
& operator<<(float f
)
959 { return (*this) << Format(_T("%f"), f
); }
960 // insert a double into string
961 wxString
& operator<<(double d
)
962 { return (*this) << Format(_T("%g"), d
); }
965 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
966 int Cmp(const wxChar
*psz
) const;
967 int Cmp(const wxString
& s
) const;
968 // same as Cmp() but not case-sensitive
969 int CmpNoCase(const wxChar
*psz
) const;
970 int CmpNoCase(const wxString
& s
) const;
971 // test for the string equality, either considering case or not
972 // (if compareWithCase then the case matters)
973 bool IsSameAs(const wxChar
*psz
, bool compareWithCase
= true) const
974 { return (compareWithCase
? Cmp(psz
) : CmpNoCase(psz
)) == 0; }
975 // comparison with a signle character: returns true if equal
976 bool IsSameAs(wxChar c
, bool compareWithCase
= true) const
978 return (length() == 1) && (compareWithCase
? GetChar(0u) == c
979 : wxToupper(GetChar(0u)) == wxToupper(c
));
982 // simple sub-string extraction
983 // return substring starting at nFirst of length nCount (or till the end
984 // if nCount = default value)
985 wxString
Mid(size_t nFirst
, size_t nCount
= npos
) const;
987 // operator version of Mid()
988 wxString
operator()(size_t start
, size_t len
) const
989 { return Mid(start
, len
); }
991 // check that the string starts with prefix and return the rest of the
992 // string in the provided pointer if it is not NULL, otherwise return
994 bool StartsWith(const wxChar
*prefix
, wxString
*rest
= NULL
) const;
996 // get first nCount characters
997 wxString
Left(size_t nCount
) const;
998 // get last nCount characters
999 wxString
Right(size_t nCount
) const;
1000 // get all characters before the first occurance of ch
1001 // (returns the whole string if ch not found)
1002 wxString
BeforeFirst(wxChar ch
) const;
1003 // get all characters before the last occurence of ch
1004 // (returns empty string if ch not found)
1005 wxString
BeforeLast(wxChar ch
) const;
1006 // get all characters after the first occurence of ch
1007 // (returns empty string if ch not found)
1008 wxString
AfterFirst(wxChar ch
) const;
1009 // get all characters after the last occurence of ch
1010 // (returns the whole string if ch not found)
1011 wxString
AfterLast(wxChar ch
) const;
1013 // for compatibility only, use more explicitly named functions above
1014 wxString
Before(wxChar ch
) const { return BeforeLast(ch
); }
1015 wxString
After(wxChar ch
) const { return AfterFirst(ch
); }
1018 // convert to upper case in place, return the string itself
1019 wxString
& MakeUpper();
1020 // convert to upper case, return the copy of the string
1021 // Here's something to remember: BC++ doesn't like returns in inlines.
1022 wxString
Upper() const ;
1023 // convert to lower case in place, return the string itself
1024 wxString
& MakeLower();
1025 // convert to lower case, return the copy of the string
1026 wxString
Lower() const ;
1028 // trimming/padding whitespace (either side) and truncating
1029 // remove spaces from left or from right (default) side
1030 wxString
& Trim(bool bFromRight
= true);
1031 // add nCount copies chPad in the beginning or at the end (default)
1032 wxString
& Pad(size_t nCount
, wxChar chPad
= wxT(' '), bool bFromRight
= true);
1034 // searching and replacing
1035 // searching (return starting index, or -1 if not found)
1036 int Find(wxChar ch
, bool bFromEnd
= false) const; // like strchr/strrchr
1037 // searching (return starting index, or -1 if not found)
1038 int Find(const wxChar
*pszSub
) const; // like strstr
1039 // replace first (or all of bReplaceAll) occurences of substring with
1040 // another string, returns the number of replacements made
1041 size_t Replace(const wxChar
*szOld
,
1042 const wxChar
*szNew
,
1043 bool bReplaceAll
= true);
1045 // check if the string contents matches a mask containing '*' and '?'
1046 bool Matches(const wxChar
*szMask
) const;
1048 // conversion to numbers: all functions return true only if the whole
1049 // string is a number and put the value of this number into the pointer
1050 // provided, the base is the numeric base in which the conversion should be
1051 // done and must be comprised between 2 and 36 or be 0 in which case the
1052 // standard C rules apply (leading '0' => octal, "0x" => hex)
1053 // convert to a signed integer
1054 bool ToLong(long *val
, int base
= 10) const;
1055 // convert to an unsigned integer
1056 bool ToULong(unsigned long *val
, int base
= 10) const;
1057 // convert to a double
1058 bool ToDouble(double *val
) const;
1060 // formated input/output
1061 // as sprintf(), returns the number of characters written or < 0 on error
1062 // (take 'this' into account in attribute parameter count)
1063 int Printf(const wxChar
*pszFormat
, ...) ATTRIBUTE_PRINTF_2
;
1064 // as vprintf(), returns the number of characters written or < 0 on error
1065 int PrintfV(const wxChar
* pszFormat
, va_list argptr
);
1067 // returns the string containing the result of Printf() to it
1068 static wxString
Format(const wxChar
*pszFormat
, ...) ATTRIBUTE_PRINTF_1
;
1069 // the same as above, but takes a va_list
1070 static wxString
FormatV(const wxChar
*pszFormat
, va_list argptr
);
1072 // raw access to string memory
1073 // ensure that string has space for at least nLen characters
1074 // only works if the data of this string is not shared
1075 bool Alloc(size_t nLen
) { reserve(nLen
); /*return capacity() >= nLen;*/ return true; }
1076 // minimize the string's memory
1077 // only works if the data of this string is not shared
1080 // get writable buffer of at least nLen bytes. Unget() *must* be called
1081 // a.s.a.p. to put string back in a reasonable state!
1082 wxChar
*GetWriteBuf(size_t nLen
);
1083 // call this immediately after GetWriteBuf() has been used
1084 void UngetWriteBuf();
1085 void UngetWriteBuf(size_t nLen
);
1088 // wxWidgets version 1 compatibility functions
1091 wxString
SubString(size_t from
, size_t to
) const
1092 { return Mid(from
, (to
- from
+ 1)); }
1093 // values for second parameter of CompareTo function
1094 enum caseCompare
{exact
, ignoreCase
};
1095 // values for first parameter of Strip function
1096 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
1099 // (take 'this' into account in attribute parameter count)
1100 int sprintf(const wxChar
*pszFormat
, ...) ATTRIBUTE_PRINTF_2
;
1103 inline int CompareTo(const wxChar
* psz
, caseCompare cmp
= exact
) const
1104 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
1107 size_t Length() const { return length(); }
1108 // Count the number of characters
1109 int Freq(wxChar ch
) const;
1111 void LowerCase() { MakeLower(); }
1113 void UpperCase() { MakeUpper(); }
1114 // use Trim except that it doesn't change this string
1115 wxString
Strip(stripType w
= trailing
) const;
1117 // use Find (more general variants not yet supported)
1118 size_t Index(const wxChar
* psz
) const { return Find(psz
); }
1119 size_t Index(wxChar ch
) const { return Find(ch
); }
1121 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
1122 wxString
& RemoveLast(size_t n
= 1) { return Truncate(length() - n
); }
1124 wxString
& Remove(size_t nStart
, size_t nLen
)
1125 { return (wxString
&)erase( nStart
, nLen
); }
1128 int First( const wxChar ch
) const { return Find(ch
); }
1129 int First( const wxChar
* psz
) const { return Find(psz
); }
1130 int First( const wxString
&str
) const { return Find(str
); }
1131 int Last( const wxChar ch
) const { return Find(ch
, true); }
1132 bool Contains(const wxString
& str
) const { return Find(str
) != wxNOT_FOUND
; }
1135 bool IsNull() const { return empty(); }
1137 // std::string compatibility functions
1139 // take nLen chars starting at nPos
1140 wxString(const wxString
& str
, size_t nPos
, size_t nLen
)
1141 : wxStringBase(str
, nPos
, nLen
) { }
1142 // take all characters from pStart to pEnd
1143 wxString(const void *pStart
, const void *pEnd
)
1144 : wxStringBase((const wxChar
*)pStart
, (const wxChar
*)pEnd
) { }
1146 wxString(const_iterator first
, const_iterator last
)
1147 : wxStringBase(first
, last
) { }
1150 // lib.string.modifiers
1151 // append elements str[pos], ..., str[pos+n]
1152 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
1153 { return (wxString
&)wxStringBase::append(str
, pos
, n
); }
1155 wxString
& append(const wxString
& str
)
1156 { return (wxString
&)wxStringBase::append(str
); }
1157 // append first n (or all if n == npos) characters of sz
1158 wxString
& append(const wxChar
*sz
)
1159 { return (wxString
&)wxStringBase::append(sz
); }
1160 wxString
& append(const wxChar
*sz
, size_t n
)
1161 { return (wxString
&)wxStringBase::append(sz
, n
); }
1162 // append n copies of ch
1163 wxString
& append(size_t n
, wxChar ch
)
1164 { return (wxString
&)wxStringBase::append(n
, ch
); }
1165 // append from first to last
1166 wxString
& append(const_iterator first
, const_iterator last
)
1167 { return (wxString
&)wxStringBase::append(first
, last
); }
1169 // same as `this_string = str'
1170 wxString
& assign(const wxString
& str
)
1171 { return (wxString
&)wxStringBase::assign(str
); }
1172 // same as ` = str[pos..pos + n]
1173 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
1174 { return (wxString
&)wxStringBase::assign(str
, pos
, n
); }
1175 // same as `= first n (or all if n == npos) characters of sz'
1176 wxString
& assign(const wxChar
*sz
)
1177 { return (wxString
&)wxStringBase::assign(sz
); }
1178 wxString
& assign(const wxChar
*sz
, size_t n
)
1179 { return (wxString
&)wxStringBase::assign(sz
, n
); }
1180 // same as `= n copies of ch'
1181 wxString
& assign(size_t n
, wxChar ch
)
1182 { return (wxString
&)wxStringBase::assign(n
, ch
); }
1183 // assign from first to last
1184 wxString
& assign(const_iterator first
, const_iterator last
)
1185 { return (wxString
&)wxStringBase::assign(first
, last
); }
1187 // string comparison
1188 #if !defined(HAVE_STD_STRING_COMPARE)
1189 int compare(const wxStringBase
& str
) const;
1190 // comparison with a substring
1191 int compare(size_t nStart
, size_t nLen
, const wxStringBase
& str
) const;
1192 // comparison of 2 substrings
1193 int compare(size_t nStart
, size_t nLen
,
1194 const wxStringBase
& str
, size_t nStart2
, size_t nLen2
) const;
1195 // just like strcmp()
1196 int compare(const wxChar
* sz
) const;
1197 // substring comparison with first nCount characters of sz
1198 int compare(size_t nStart
, size_t nLen
,
1199 const wxChar
* sz
, size_t nCount
= npos
) const;
1200 #endif // !defined HAVE_STD_STRING_COMPARE
1202 // insert another string
1203 wxString
& insert(size_t nPos
, const wxString
& str
)
1204 { return (wxString
&)wxStringBase::insert(nPos
, str
); }
1205 // insert n chars of str starting at nStart (in str)
1206 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
1207 { return (wxString
&)wxStringBase::insert(nPos
, str
, nStart
, n
); }
1208 // insert first n (or all if n == npos) characters of sz
1209 wxString
& insert(size_t nPos
, const wxChar
*sz
)
1210 { return (wxString
&)wxStringBase::insert(nPos
, sz
); }
1211 wxString
& insert(size_t nPos
, const wxChar
*sz
, size_t n
)
1212 { return (wxString
&)wxStringBase::insert(nPos
, sz
, n
); }
1213 // insert n copies of ch
1214 wxString
& insert(size_t nPos
, size_t n
, wxChar ch
)
1215 { return (wxString
&)wxStringBase::insert(nPos
, n
, ch
); }
1216 iterator
insert(iterator it
, wxChar ch
)
1217 { return wxStringBase::insert(it
, ch
); }
1218 void insert(iterator it
, const_iterator first
, const_iterator last
)
1219 { wxStringBase::insert(it
, first
, last
); }
1220 void insert(iterator it
, size_type n
, wxChar ch
)
1221 { wxStringBase::insert(it
, n
, ch
); }
1223 // delete characters from nStart to nStart + nLen
1224 wxString
& erase(size_type pos
= 0, size_type n
= npos
)
1225 { return (wxString
&)wxStringBase::erase(pos
, n
); }
1226 iterator
erase(iterator first
, iterator last
)
1227 { return wxStringBase::erase(first
, last
); }
1228 iterator
erase(iterator first
)
1229 { return wxStringBase::erase(first
); }
1231 #ifdef wxSTRING_BASE_HASNT_CLEAR
1232 void clear() { erase(); }
1235 // replaces the substring of length nLen starting at nStart
1236 wxString
& replace(size_t nStart
, size_t nLen
, const wxChar
* sz
)
1237 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, sz
); }
1238 // replaces the substring of length nLen starting at nStart
1239 wxString
& replace(size_t nStart
, size_t nLen
, const wxString
& str
)
1240 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, str
); }
1241 // replaces the substring with nCount copies of ch
1242 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxChar ch
)
1243 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, nCount
, ch
); }
1244 // replaces a substring with another substring
1245 wxString
& replace(size_t nStart
, size_t nLen
,
1246 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1247 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, str
,
1249 // replaces the substring with first nCount chars of sz
1250 wxString
& replace(size_t nStart
, size_t nLen
,
1251 const wxChar
* sz
, size_t nCount
)
1252 { return (wxString
&)wxStringBase::replace(nStart
, nLen
, sz
, nCount
); }
1253 wxString
& replace(iterator first
, iterator last
, const_pointer s
)
1254 { return (wxString
&)wxStringBase::replace(first
, last
, s
); }
1255 wxString
& replace(iterator first
, iterator last
, const_pointer s
,
1257 { return (wxString
&)wxStringBase::replace(first
, last
, s
, n
); }
1258 wxString
& replace(iterator first
, iterator last
, const wxString
& s
)
1259 { return (wxString
&)wxStringBase::replace(first
, last
, s
); }
1260 wxString
& replace(iterator first
, iterator last
, size_type n
, wxChar c
)
1261 { return (wxString
&)wxStringBase::replace(first
, last
, n
, c
); }
1262 wxString
& replace(iterator first
, iterator last
,
1263 const_iterator first1
, const_iterator last1
)
1264 { return (wxString
&)wxStringBase::replace(first
, last
, first1
, last1
); }
1267 wxString
& operator+=(const wxString
& s
)
1268 { return (wxString
&)wxStringBase::operator+=(s
); }
1269 // string += C string
1270 wxString
& operator+=(const wxChar
*psz
)
1271 { return (wxString
&)wxStringBase::operator+=(psz
); }
1273 wxString
& operator+=(wxChar ch
)
1274 { return (wxString
&)wxStringBase::operator+=(ch
); }
1277 // define wxArrayString, for compatibility
1278 #if WXWIN_COMPATIBILITY_2_4 && !wxUSE_STL
1279 #include "wx/arrstr.h"
1283 // return an empty wxString (not very useful with wxUSE_STL == 1)
1284 inline const wxString
wxGetEmptyString() { return wxString(); }
1286 // return an empty wxString (more efficient than wxString() here)
1287 inline const wxString
& wxGetEmptyString()
1289 return *(wxString
*)&wxEmptyString
;
1291 #endif // wxUSE_STL/!wxUSE_STL
1293 // ----------------------------------------------------------------------------
1294 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
1295 // ----------------------------------------------------------------------------
1299 class WXDLLIMPEXP_BASE wxStringBuffer
1302 wxStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
1303 : m_str(str
), m_buf(lenWanted
)
1306 ~wxStringBuffer() { m_str
.assign(m_buf
.data(), wxStrlen(m_buf
.data())); }
1308 operator wxChar
*() { return m_buf
.data(); }
1313 wxWCharBuffer m_buf
;
1318 DECLARE_NO_COPY_CLASS(wxStringBuffer
)
1321 class WXDLLIMPEXP_BASE wxStringBufferLength
1324 wxStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
1325 : m_str(str
), m_buf(lenWanted
), m_len(0), m_lenSet(false)
1328 ~wxStringBufferLength()
1331 m_str
.assign(m_buf
.data(), m_len
);
1334 operator wxChar
*() { return m_buf
.data(); }
1335 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
1340 wxWCharBuffer m_buf
;
1347 DECLARE_NO_COPY_CLASS(wxStringBufferLength
)
1350 #else // if !wxUSE_STL
1352 class WXDLLIMPEXP_BASE wxStringBuffer
1355 wxStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
1356 : m_str(str
), m_buf(NULL
)
1357 { m_buf
= m_str
.GetWriteBuf(lenWanted
); }
1359 ~wxStringBuffer() { m_str
.UngetWriteBuf(); }
1361 operator wxChar
*() const { return m_buf
; }
1367 DECLARE_NO_COPY_CLASS(wxStringBuffer
)
1370 class WXDLLIMPEXP_BASE wxStringBufferLength
1373 wxStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
1374 : m_str(str
), m_buf(NULL
), m_len(0), m_lenSet(false)
1376 m_buf
= m_str
.GetWriteBuf(lenWanted
);
1377 wxASSERT(m_buf
!= NULL
);
1380 ~wxStringBufferLength()
1383 m_str
.UngetWriteBuf(m_len
);
1386 operator wxChar
*() const { return m_buf
; }
1387 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
1395 DECLARE_NO_COPY_CLASS(wxStringBufferLength
)
1398 #endif // !wxUSE_STL
1400 // ---------------------------------------------------------------------------
1401 // wxString comparison functions: operator versions are always case sensitive
1402 // ---------------------------------------------------------------------------
1404 // note that when wxUSE_STL == 1 the comparison operators taking std::string
1405 // are used and defining them also for wxString would only result in
1406 // compilation ambiguities when comparing std::string and wxString
1409 inline bool operator==(const wxString
& s1
, const wxString
& s2
)
1410 { return (s1
.Len() == s2
.Len()) && (s1
.Cmp(s2
) == 0); }
1411 inline bool operator==(const wxString
& s1
, const wxChar
* s2
)
1412 { return s1
.Cmp(s2
) == 0; }
1413 inline bool operator==(const wxChar
* s1
, const wxString
& s2
)
1414 { return s2
.Cmp(s1
) == 0; }
1415 inline bool operator!=(const wxString
& s1
, const wxString
& s2
)
1416 { return (s1
.Len() != s2
.Len()) || (s1
.Cmp(s2
) != 0); }
1417 inline bool operator!=(const wxString
& s1
, const wxChar
* s2
)
1418 { return s1
.Cmp(s2
) != 0; }
1419 inline bool operator!=(const wxChar
* s1
, const wxString
& s2
)
1420 { return s2
.Cmp(s1
) != 0; }
1421 inline bool operator< (const wxString
& s1
, const wxString
& s2
)
1422 { return s1
.Cmp(s2
) < 0; }
1423 inline bool operator< (const wxString
& s1
, const wxChar
* s2
)
1424 { return s1
.Cmp(s2
) < 0; }
1425 inline bool operator< (const wxChar
* s1
, const wxString
& s2
)
1426 { return s2
.Cmp(s1
) > 0; }
1427 inline bool operator> (const wxString
& s1
, const wxString
& s2
)
1428 { return s1
.Cmp(s2
) > 0; }
1429 inline bool operator> (const wxString
& s1
, const wxChar
* s2
)
1430 { return s1
.Cmp(s2
) > 0; }
1431 inline bool operator> (const wxChar
* s1
, const wxString
& s2
)
1432 { return s2
.Cmp(s1
) < 0; }
1433 inline bool operator<=(const wxString
& s1
, const wxString
& s2
)
1434 { return s1
.Cmp(s2
) <= 0; }
1435 inline bool operator<=(const wxString
& s1
, const wxChar
* s2
)
1436 { return s1
.Cmp(s2
) <= 0; }
1437 inline bool operator<=(const wxChar
* s1
, const wxString
& s2
)
1438 { return s2
.Cmp(s1
) >= 0; }
1439 inline bool operator>=(const wxString
& s1
, const wxString
& s2
)
1440 { return s1
.Cmp(s2
) >= 0; }
1441 inline bool operator>=(const wxString
& s1
, const wxChar
* s2
)
1442 { return s1
.Cmp(s2
) >= 0; }
1443 inline bool operator>=(const wxChar
* s1
, const wxString
& s2
)
1444 { return s2
.Cmp(s1
) <= 0; }
1447 inline bool operator==(const wxString
& s1
, const wxWCharBuffer
& s2
)
1448 { return (s1
.Cmp((const wchar_t *)s2
) == 0); }
1449 inline bool operator==(const wxWCharBuffer
& s1
, const wxString
& s2
)
1450 { return (s2
.Cmp((const wchar_t *)s1
) == 0); }
1451 inline bool operator!=(const wxString
& s1
, const wxWCharBuffer
& s2
)
1452 { return (s1
.Cmp((const wchar_t *)s2
) != 0); }
1453 inline bool operator!=(const wxWCharBuffer
& s1
, const wxString
& s2
)
1454 { return (s2
.Cmp((const wchar_t *)s1
) != 0); }
1455 #else // !wxUSE_UNICODE
1456 inline bool operator==(const wxString
& s1
, const wxCharBuffer
& s2
)
1457 { return (s1
.Cmp((const char *)s2
) == 0); }
1458 inline bool operator==(const wxCharBuffer
& s1
, const wxString
& s2
)
1459 { return (s2
.Cmp((const char *)s1
) == 0); }
1460 inline bool operator!=(const wxString
& s1
, const wxCharBuffer
& s2
)
1461 { return (s1
.Cmp((const char *)s2
) != 0); }
1462 inline bool operator!=(const wxCharBuffer
& s1
, const wxString
& s2
)
1463 { return (s2
.Cmp((const char *)s1
) != 0); }
1464 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1467 inline wxString
operator+(const wxString
& string
, const wxWCharBuffer
& buf
)
1468 { return string
+ (const wchar_t *)buf
; }
1469 inline wxString
operator+(const wxWCharBuffer
& buf
, const wxString
& string
)
1470 { return (const wchar_t *)buf
+ string
; }
1471 #else // !wxUSE_UNICODE
1472 inline wxString
operator+(const wxString
& string
, const wxCharBuffer
& buf
)
1473 { return string
+ (const char *)buf
; }
1474 inline wxString
operator+(const wxCharBuffer
& buf
, const wxString
& string
)
1475 { return (const char *)buf
+ string
; }
1476 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
1478 #endif // !wxUSE_STL
1480 // comparison with char (those are not defined by std::[w]string and so should
1481 // be always available)
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
); }
1484 inline bool operator!=(wxChar c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
1485 inline bool operator!=(const wxString
& s
, wxChar c
) { return !s
.IsSameAs(c
); }
1487 // ---------------------------------------------------------------------------
1488 // Implementation only from here until the end of file
1489 // ---------------------------------------------------------------------------
1491 // don't pollute the library user's name space
1492 #undef wxASSERT_VALID_INDEX
1494 #if wxUSE_STD_IOSTREAM
1496 #include "wx/iosfwrap.h"
1498 WXDLLIMPEXP_BASE wxSTD istream
& operator>>(wxSTD istream
&, wxString
&);
1499 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxString
&);
1501 #endif // wxSTD_STRING_COMPATIBILITY
1503 #endif // _WX_WXSTRINGH__