1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxString class
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_WXSTRING_H__
19 #define _WX_WXSTRING_H__
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/wxcrt.h" // for wxChar, wxStrlen() etc.
55 #include "wx/strvararg.h"
56 #include "wx/buffer.h" // for wxCharBuffer
57 #include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes
58 #include "wx/stringimpl.h"
59 #include "wx/stringops.h"
60 #include "wx/unichar.h"
62 class WXDLLIMPEXP_BASE wxString
;
64 // unless this symbol is predefined to disable the compatibility functions, do
66 #ifndef WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
67 #define WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER 1
70 // ---------------------------------------------------------------------------
72 // ---------------------------------------------------------------------------
74 // casts [unfortunately!] needed to call some broken functions which require
75 // "char *" instead of "const char *"
76 #define WXSTRINGCAST (wxChar *)(const wxChar *)
77 #define wxCSTRINGCAST (wxChar *)(const wxChar *)
78 #define wxMBSTRINGCAST (char *)(const char *)
79 #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *)
81 // ----------------------------------------------------------------------------
83 // ----------------------------------------------------------------------------
85 #if WXWIN_COMPATIBILITY_2_6
87 // deprecated in favour of wxString::npos, don't use in new code
89 // maximum possible length for a string means "take all string" everywhere
90 #define wxSTRING_MAXLEN wxString::npos
92 #endif // WXWIN_COMPATIBILITY_2_6
94 // ---------------------------------------------------------------------------
95 // global functions complementing standard C string library replacements for
96 // strlen() and portable strcasecmp()
97 //---------------------------------------------------------------------------
99 #if WXWIN_COMPATIBILITY_2_8
100 // Use wxXXX() functions from wxcrt.h instead! These functions are for
101 // backwards compatibility only.
103 // checks whether the passed in pointer is NULL and if the string is empty
104 wxDEPRECATED( inline bool IsEmpty(const char *p
) );
105 inline bool IsEmpty(const char *p
) { return (!p
|| !*p
); }
107 // safe version of strlen() (returns 0 if passed NULL pointer)
108 wxDEPRECATED( inline size_t Strlen(const char *psz
) );
109 inline size_t Strlen(const char *psz
)
110 { return psz
? strlen(psz
) : 0; }
112 // portable strcasecmp/_stricmp
113 wxDEPRECATED( inline int Stricmp(const char *psz1
, const char *psz2
) );
114 inline int Stricmp(const char *psz1
, const char *psz2
)
116 #if defined(__VISUALC__) && defined(__WXWINCE__)
117 register char c1
, c2
;
119 c1
= tolower(*psz1
++);
120 c2
= tolower(*psz2
++);
121 } while ( c1
&& (c1
== c2
) );
124 #elif defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) )
125 return _stricmp(psz1
, psz2
);
126 #elif defined(__SC__)
127 return _stricmp(psz1
, psz2
);
128 #elif defined(__SALFORDC__)
129 return stricmp(psz1
, psz2
);
130 #elif defined(__BORLANDC__)
131 return stricmp(psz1
, psz2
);
132 #elif defined(__WATCOMC__)
133 return stricmp(psz1
, psz2
);
134 #elif defined(__DJGPP__)
135 return stricmp(psz1
, psz2
);
136 #elif defined(__EMX__)
137 return stricmp(psz1
, psz2
);
138 #elif defined(__WXPM__)
139 return stricmp(psz1
, psz2
);
140 #elif defined(__WXPALMOS__) || \
141 defined(HAVE_STRCASECMP_IN_STRING_H) || \
142 defined(HAVE_STRCASECMP_IN_STRINGS_H) || \
143 defined(__GNUWIN32__)
144 return strcasecmp(psz1
, psz2
);
145 #elif defined(__MWERKS__) && !defined(__INTEL__)
146 register char c1
, c2
;
148 c1
= tolower(*psz1
++);
149 c2
= tolower(*psz2
++);
150 } while ( c1
&& (c1
== c2
) );
154 // almost all compilers/libraries provide this function (unfortunately under
155 // different names), that's why we don't implement our own which will surely
156 // be more efficient than this code (uncomment to use):
158 register char c1, c2;
160 c1 = tolower(*psz1++);
161 c2 = tolower(*psz2++);
162 } while ( c1 && (c1 == c2) );
167 #error "Please define string case-insensitive compare for your OS/compiler"
168 #endif // OS/compiler
171 #endif // WXWIN_COMPATIBILITY_2_8
173 // ----------------------------------------------------------------------------
175 // ----------------------------------------------------------------------------
177 // Lightweight object returned by wxString::c_str() and implicitly convertible
178 // to either const char* or const wchar_t*.
179 class WXDLLIMPEXP_BASE wxCStrData
182 // Ctors; for internal use by wxString and wxCStrData only
183 wxCStrData(const wxString
*str
, size_t offset
= 0, bool owned
= false)
184 : m_str(str
), m_offset(offset
), m_owned(owned
) {}
187 // Ctor constructs the object from char literal; they are needed to make
188 // operator?: compile and they intentionally take char*, not const char*
189 inline wxCStrData(char *buf
);
190 inline wxCStrData(wchar_t *buf
);
191 inline wxCStrData(const wxCStrData
& data
);
193 inline ~wxCStrData();
195 // methods defined inline below must be declared inline or mingw32 3.4.5
196 // warns about "<symbol> defined locally after being referenced with
197 // dllimport linkage"
198 #if wxUSE_UNICODE_WCHAR
201 const wchar_t* AsWChar() const;
202 operator const wchar_t*() const { return AsWChar(); }
207 const char* AsChar() const;
208 const unsigned char* AsUnsignedChar() const
209 { return (const unsigned char *) AsChar(); }
210 operator const char*() const { return AsChar(); }
211 operator const unsigned char*() const { return AsUnsignedChar(); }
213 operator const void*() const { return AsChar(); }
215 inline const wxCharBuffer
AsCharBuf() const;
216 inline const wxWCharBuffer
AsWCharBuf() const;
218 inline wxString
AsString() const;
220 // returns the value as C string in internal representation (equivalent
221 // to AsString().wx_str(), but more efficient)
222 const wxStringCharType
*AsInternal() const;
224 // allow expressions like "c_str()[0]":
225 inline wxUniChar
operator[](size_t n
) const;
226 wxUniChar
operator[](int n
) const { return operator[](size_t(n
)); }
227 wxUniChar
operator[](long n
) const { return operator[](size_t(n
)); }
228 #ifndef wxSIZE_T_IS_UINT
229 wxUniChar
operator[](unsigned int n
) const { return operator[](size_t(n
)); }
230 #endif // size_t != unsigned int
232 // these operators are needed to emulate the pointer semantics of c_str():
233 // expressions like "wxChar *p = str.c_str() + 1;" should continue to work
234 // (we need both versions to resolve ambiguities):
235 wxCStrData
operator+(int n
) const
236 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
237 wxCStrData
operator+(long n
) const
238 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
239 wxCStrData
operator+(size_t n
) const
240 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
242 // and these for "str.c_str() + n - 2":
243 wxCStrData
operator-(int n
) const
245 wxASSERT_MSG( n
<= (int)m_offset
,
246 _T("attempt to construct address before the beginning of the string") );
247 return wxCStrData(m_str
, m_offset
- n
, m_owned
);
249 wxCStrData
operator-(long n
) const
251 wxASSERT_MSG( n
<= (int)m_offset
,
252 _T("attempt to construct address before the beginning of the string") );
253 return wxCStrData(m_str
, m_offset
- n
, m_owned
);
255 wxCStrData
operator-(size_t n
) const
257 wxASSERT_MSG( n
<= m_offset
,
258 _T("attempt to construct address before the beginning of the string") );
259 return wxCStrData(m_str
, m_offset
- n
, m_owned
);
262 // this operator is needed to make expressions like "*c_str()" or
263 // "*(c_str() + 2)" work
264 inline wxUniChar
operator*() const;
267 const wxString
*m_str
;
271 friend class WXDLLIMPEXP_BASE wxString
;
274 // ----------------------------------------------------------------------------
275 // wxStringPrintfMixin
276 // ---------------------------------------------------------------------------
278 // NB: VC6 has a bug that causes linker errors if you have template methods
279 // in a class using __declspec(dllimport). The solution is to split such
280 // class into two classes, one that contains the template methods and does
281 // *not* use WXDLLIMPEXP_BASE and another class that contains the rest
282 // (with DLL linkage).
284 // We only do this for VC6 here, because the code is less efficient
285 // (Printf() has to use dynamic_cast<>) and because OpenWatcom compiler
286 // cannot compile this code.
288 #if defined(__VISUALC__) && __VISUALC__ < 1300
289 #define wxNEEDS_WXSTRING_PRINTF_MIXIN
292 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
293 // this class contains implementation of wxString's vararg methods, it's
294 // exported from wxBase DLL
295 class WXDLLIMPEXP_BASE wxStringPrintfMixinBase
298 wxStringPrintfMixinBase() {}
300 int DoPrintf(const wxString
& format
, ...);
301 static wxString
DoFormat(const wxString
& format
, ...);
304 // this class contains template wrappers for wxString's vararg methods, it's
305 // intentionally *not* exported from the DLL in order to fix the VC6 bug
307 class wxStringPrintfMixin
: public wxStringPrintfMixinBase
310 // to further complicate things, we can't return wxString from
311 // wxStringPrintfMixin::Format() because wxString is not yet declared at
312 // this point; the solution is to use this fake type trait template - this
313 // way the compiler won't know the return type until Format() is used
314 // (this doesn't compile with Watcom, but VC6 compiles it just fine):
315 template<typename T
> struct StringReturnType
317 typedef wxString type
;
321 // these are duplicated wxString methods, they're also declared below
322 // if !wxNEEDS_WXSTRING_PRINTF_MIXIN:
324 // static wxString Format(const wString& format, ...) ATTRIBUTE_PRINTF_1;
325 WX_DEFINE_VARARG_FUNC2_SANS_N0(static typename StringReturnType
<T1
>::type
,
326 Format
, 1, (const wxString
&),
328 // We have to implement the version without template arguments manually
329 // because of the StringReturnType<> hack, although WX_DEFINE_VARARG_FUNC
330 // normally does it itself. It has to be a template so that we can use
331 // the hack, even though there's no real template parameter:
332 struct FormatDummyArg
{} ;
335 inline static typename StringReturnType
<T
>::type
336 Format(const wxString
& fmt
, FormatDummyArg dummy
= FormatDummyArg())
338 return DoFormat(fmt
);
341 // int Printf(const wxString& format, ...);
342 WX_DEFINE_VARARG_FUNC(int, Printf
, 1, (const wxString
&), DoPrintf
)
343 // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2;
344 WX_DEFINE_VARARG_FUNC(int, sprintf
, 1, (const wxString
&), DoPrintf
)
347 wxStringPrintfMixin() : wxStringPrintfMixinBase() {}
349 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
352 // ----------------------------------------------------------------------------
353 // wxString: string class trying to be compatible with std::string, MFC
354 // CString and wxWindows 1.x wxString all at once
355 // ---------------------------------------------------------------------------
357 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
358 // "non dll-interface class 'wxStringPrintfMixin' used as base interface
359 // for dll-interface class 'wxString'" -- this is OK in our case
360 #pragma warning (disable:4275)
363 class WXDLLIMPEXP_BASE wxString
364 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
365 : public wxStringPrintfMixin
368 // NB: special care was taken in arranging the member functions in such order
369 // that all inline functions can be effectively inlined, verify that all
370 // performance critical functions are still inlined if you change order!
372 // an 'invalid' value for string index, moved to this place due to a CW bug
373 static const size_t npos
;
376 // if we hadn't made these operators private, it would be possible to
377 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
378 // converted to char in C and we do have operator=(char)
380 // NB: we don't need other versions (short/long and unsigned) as attempt
381 // to assign another numeric type to wxString will now result in
382 // ambiguity between operator=(char) and operator=(int)
383 wxString
& operator=(int);
385 // these methods are not implemented - there is _no_ conversion from int to
386 // string, you're doing something wrong if the compiler wants to call it!
388 // try `s << i' or `s.Printf("%d", i)' instead
392 // buffer for holding temporary substring when using any of the methods
393 // that take (char*,size_t) or (wchar_t*,size_t) arguments:
395 struct SubstrBufFromType
400 SubstrBufFromType(const T
& data_
, size_t len_
)
401 : data(data_
), len(len_
) {}
404 #if wxUSE_UNICODE_UTF8
405 // even char* -> char* needs conversion, from locale charset to UTF-8
406 typedef SubstrBufFromType
<wxCharBuffer
> SubstrBufFromWC
;
407 typedef SubstrBufFromType
<wxCharBuffer
> SubstrBufFromMB
;
408 #elif wxUSE_UNICODE_WCHAR
409 typedef SubstrBufFromType
<const wchar_t*> SubstrBufFromWC
;
410 typedef SubstrBufFromType
<wxWCharBuffer
> SubstrBufFromMB
;
412 typedef SubstrBufFromType
<const char*> SubstrBufFromMB
;
413 typedef SubstrBufFromType
<wxCharBuffer
> SubstrBufFromWC
;
417 // Functions implementing primitive operations on string data; wxString
418 // methods and iterators are implemented in terms of it. The differences
419 // between UTF-8 and wchar_t* representations of the string are mostly
422 #if wxUSE_UNICODE_UTF8
423 static SubstrBufFromMB
ConvertStr(const char *psz
, size_t nLength
,
424 const wxMBConv
& conv
);
425 static SubstrBufFromWC
ConvertStr(const wchar_t *pwz
, size_t nLength
,
426 const wxMBConv
& conv
);
427 #elif wxUSE_UNICODE_WCHAR
428 static SubstrBufFromMB
ConvertStr(const char *psz
, size_t nLength
,
429 const wxMBConv
& conv
);
431 static SubstrBufFromWC
ConvertStr(const wchar_t *pwz
, size_t nLength
,
432 const wxMBConv
& conv
);
435 #if !wxUSE_UNICODE_UTF8 // wxUSE_UNICODE_WCHAR or !wxUSE_UNICODE
436 // returns C string encoded as the implementation expects:
438 static const wchar_t* ImplStr(const wchar_t* str
)
439 { return str
? str
: wxT(""); }
440 static const SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
441 { return SubstrBufFromWC(str
, (str
&& n
== npos
) ? wxWcslen(str
) : n
); }
442 static wxWCharBuffer
ImplStr(const char* str
,
443 const wxMBConv
& conv
= wxConvLibc
)
444 { return ConvertStr(str
, npos
, conv
).data
; }
445 static SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
446 const wxMBConv
& conv
= wxConvLibc
)
447 { return ConvertStr(str
, n
, conv
); }
449 static const char* ImplStr(const char* str
,
450 const wxMBConv
& WXUNUSED(conv
) = wxConvLibc
)
451 { return str
? str
: ""; }
452 static const SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
453 const wxMBConv
& WXUNUSED(conv
) = wxConvLibc
)
454 { return SubstrBufFromMB(str
, (str
&& n
== npos
) ? wxStrlen(str
) : n
); }
455 static wxCharBuffer
ImplStr(const wchar_t* str
)
456 { return ConvertStr(str
, npos
, wxConvLibc
).data
; }
457 static SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
458 { return ConvertStr(str
, n
, wxConvLibc
); }
461 // translates position index in wxString to/from index in underlying
463 static size_t PosToImpl(size_t pos
) { return pos
; }
464 static void PosLenToImpl(size_t pos
, size_t len
,
465 size_t *implPos
, size_t *implLen
)
466 { *implPos
= pos
; *implLen
= len
; }
467 static size_t LenToImpl(size_t len
) { return len
; }
468 static size_t PosFromImpl(size_t pos
) { return pos
; }
470 #else // wxUSE_UNICODE_UTF8
472 // FIXME-UTF8: return as-is without copying under UTF8 locale, return
473 // converted string under other locales - needs wxCharBuffer
475 static wxCharBuffer
ImplStr(const char* str
,
476 const wxMBConv
& conv
= wxConvLibc
)
477 { return ConvertStr(str
, npos
, conv
).data
; }
478 static SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
479 const wxMBConv
& conv
= wxConvLibc
)
480 { return ConvertStr(str
, n
, conv
); }
482 static wxCharBuffer
ImplStr(const wchar_t* str
)
483 { return ConvertStr(str
, npos
, wxConvUTF8
).data
; }
484 static SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
485 { return ConvertStr(str
, n
, wxConvUTF8
); }
487 size_t PosToImpl(size_t pos
) const
489 if ( pos
== 0 || pos
== npos
)
492 return (begin() + pos
).impl() - m_impl
.begin();
495 void PosLenToImpl(size_t pos
, size_t len
, size_t *implPos
, size_t *implLen
) const;
497 size_t LenToImpl(size_t len
) const
500 PosLenToImpl(0, len
, &pos
, &len2
);
504 size_t PosFromImpl(size_t pos
) const
506 if ( pos
== 0 || pos
== npos
)
509 return const_iterator(m_impl
.begin() + pos
) - begin();
511 #endif // !wxUSE_UNICODE_UTF8/wxUSE_UNICODE_UTF8
515 typedef wxUniChar value_type
;
516 typedef wxUniChar char_type
;
517 typedef wxUniCharRef reference
;
518 typedef wxChar
* pointer
;
519 typedef const wxChar
* const_pointer
;
521 typedef size_t size_type
;
522 typedef wxUniChar const_reference
;
525 #if wxUSE_UNICODE_UTF8
526 // random access is not O(1), as required by Random Access Iterator
527 #define WX_STR_ITERATOR_TAG std::bidirectional_iterator_tag
529 #define WX_STR_ITERATOR_TAG std::random_access_iterator_tag
532 #define WX_STR_ITERATOR_TAG void /* dummy type */
535 #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, \
536 reference_type, reference_ctor) \
538 typedef wxStringImpl::iterator_name underlying_iterator; \
540 typedef WX_STR_ITERATOR_TAG iterator_category; \
541 typedef wxUniChar value_type; \
542 typedef int difference_type; \
543 typedef reference_type reference; \
544 typedef pointer_type pointer; \
546 reference operator*() const { return reference_ctor; } \
547 reference operator[](size_t n) const { return *(*this + n); } \
549 iterator_name& operator++() \
550 { wxStringOperations::IncIter(m_cur); return *this; } \
551 iterator_name& operator--() \
552 { wxStringOperations::DecIter(m_cur); return *this; } \
553 iterator_name operator++(int) \
555 iterator_name tmp = *this; \
556 wxStringOperations::IncIter(m_cur); \
559 iterator_name operator--(int) \
561 iterator_name tmp = *this; \
562 wxStringOperations::DecIter(m_cur); \
566 iterator_name& operator+=(int n) \
568 m_cur = wxStringOperations::AddToIter(m_cur, n); \
571 iterator_name& operator+=(size_t n) \
573 m_cur = wxStringOperations::AddToIter(m_cur, (int)n); \
576 iterator_name& operator-=(int n) \
578 m_cur = wxStringOperations::AddToIter(m_cur, -n); \
581 iterator_name& operator-=(size_t n) \
583 m_cur = wxStringOperations::AddToIter(m_cur, -(int)n); \
587 difference_type operator-(const iterator_name& i) const \
588 { return wxStringOperations::DiffIters(m_cur, i.m_cur); } \
590 bool operator==(const iterator_name& i) const \
591 { return m_cur == i.m_cur; } \
592 bool operator!=(const iterator_name& i) const \
593 { return m_cur != i.m_cur; } \
595 bool operator<(const iterator_name& i) const \
596 { return m_cur < i.m_cur; } \
597 bool operator>(const iterator_name& i) const \
598 { return m_cur > i.m_cur; } \
599 bool operator<=(const iterator_name& i) const \
600 { return m_cur <= i.m_cur; } \
601 bool operator>=(const iterator_name& i) const \
602 { return m_cur >= i.m_cur; } \
605 /* for internal wxString use only: */ \
606 underlying_iterator impl() const { return m_cur; } \
608 friend class WXDLLIMPEXP_BASE wxString; \
609 friend class WXDLLIMPEXP_BASE wxCStrData; \
612 underlying_iterator m_cur
614 class const_iterator
;
616 #if wxUSE_UNICODE_UTF8
619 // NB: In UTF-8 build, (non-const) iterator needs to keep reference
620 // to the underlying wxStringImpl, because UTF-8 is variable-length
621 // encoding and changing the value pointer to by an iterator using
622 // its operator* requires calling wxStringImpl::replace() if the old
623 // and new values differ in their encoding's length.
625 WX_STR_ITERATOR_IMPL(iterator
, wxChar
*, wxUniCharRef
,
626 wxUniCharRef::CreateForString(m_str
, m_cur
));
629 iterator(const iterator
& i
) : m_cur(i
.m_cur
), m_str(i
.m_str
) {}
631 iterator
operator+(int n
) const
632 { return iterator(m_str
, wxStringOperations::AddToIter(m_cur
, n
)); }
633 iterator
operator+(size_t n
) const
634 { return iterator(m_str
, wxStringOperations::AddToIter(m_cur
, (int)n
)); }
635 iterator
operator-(int n
) const
636 { return iterator(m_str
, wxStringOperations::AddToIter(m_cur
, -n
)); }
637 iterator
operator-(size_t n
) const
638 { return iterator(m_str
, wxStringOperations::AddToIter(m_cur
, -(int)n
)); }
641 iterator(wxString
*str
, underlying_iterator ptr
)
642 : m_cur(ptr
), m_str(str
->m_impl
) {}
643 iterator(wxStringImpl
& str
, underlying_iterator ptr
)
644 : m_cur(ptr
), m_str(str
) {}
648 friend class const_iterator
;
651 size_t IterToImplPos(wxString::iterator i
) const
652 { return wxStringImpl::const_iterator(i
.impl()) - m_impl
.begin(); }
654 #else // !wxUSE_UNICODE_UTF8
658 WX_STR_ITERATOR_IMPL(iterator
, wxChar
*, wxUniCharRef
,
659 wxUniCharRef::CreateForString(m_cur
));
662 iterator(const iterator
& i
) : m_cur(i
.m_cur
) {}
664 iterator
operator+(int n
) const
665 { return iterator(wxStringOperations::AddToIter(m_cur
, n
)); }
666 iterator
operator+(size_t n
) const
667 { return iterator(wxStringOperations::AddToIter(m_cur
, (int)n
)); }
668 iterator
operator-(int n
) const
669 { return iterator(wxStringOperations::AddToIter(m_cur
, -n
)); }
670 iterator
operator-(size_t n
) const
671 { return iterator(wxStringOperations::AddToIter(m_cur
, -(int)n
)); }
674 // for internal wxString use only:
675 iterator(underlying_iterator ptr
) : m_cur(ptr
) {}
676 iterator(wxString
*WXUNUSED(str
), underlying_iterator ptr
) : m_cur(ptr
) {}
678 friend class const_iterator
;
680 #endif // wxUSE_UNICODE_UTF8/!wxUSE_UNICODE_UTF8
684 // NB: reference_type is intentionally value, not reference, the character
685 // may be encoded differently in wxString data:
686 WX_STR_ITERATOR_IMPL(const_iterator
, const wxChar
*, wxUniChar
,
687 wxStringOperations::DecodeChar(m_cur
));
690 const_iterator(const const_iterator
& i
) : m_cur(i
.m_cur
) {}
691 const_iterator(const iterator
& i
) : m_cur(i
.m_cur
) {}
693 const_iterator
operator+(int n
) const
694 { return const_iterator(wxStringOperations::AddToIter(m_cur
, n
)); }
695 const_iterator
operator+(size_t n
) const
696 { return const_iterator(wxStringOperations::AddToIter(m_cur
, (int)n
)); }
697 const_iterator
operator-(int n
) const
698 { return const_iterator(wxStringOperations::AddToIter(m_cur
, -n
)); }
699 const_iterator
operator-(size_t n
) const
700 { return const_iterator(wxStringOperations::AddToIter(m_cur
, -(int)n
)); }
703 // for internal wxString use only:
704 const_iterator(underlying_iterator ptr
) : m_cur(ptr
) {}
707 #undef WX_STR_ITERATOR_TAG
708 #undef WX_STR_ITERATOR_IMPL
710 friend class iterator
;
711 friend class const_iterator
;
713 template <typename T
>
714 class reverse_iterator_impl
717 typedef T iterator_type
;
719 typedef typename
T::iterator_category iterator_category
;
720 typedef typename
T::value_type value_type
;
721 typedef typename
T::difference_type difference_type
;
722 typedef typename
T::reference reference
;
723 typedef typename
T::pointer
*pointer
;
725 reverse_iterator_impl(iterator_type i
) : m_cur(i
) {}
726 reverse_iterator_impl(const reverse_iterator_impl
& ri
)
729 iterator_type
base() const { return m_cur
; }
731 reference
operator*() const { return *(m_cur
-1); }
732 reference
operator[](size_t n
) const { return *(*this + n
); }
734 reverse_iterator_impl
& operator++()
735 { --m_cur
; return *this; }
736 reverse_iterator_impl
operator++(int)
737 { reverse_iterator_impl tmp
= *this; --m_cur
; return tmp
; }
738 reverse_iterator_impl
& operator--()
739 { ++m_cur
; return *this; }
740 reverse_iterator_impl
operator--(int)
741 { reverse_iterator_impl tmp
= *this; ++m_cur
; return tmp
; }
743 // NB: explicit <T> in the functions below is to keep BCC 5.5 happy
744 reverse_iterator_impl
operator+(int n
) const
745 { return reverse_iterator_impl
<T
>(m_cur
- n
); }
746 reverse_iterator_impl
operator+(size_t n
) const
747 { return reverse_iterator_impl
<T
>(m_cur
- n
); }
748 reverse_iterator_impl
operator-(int n
) const
749 { return reverse_iterator_impl
<T
>(m_cur
+ n
); }
750 reverse_iterator_impl
operator-(size_t n
) const
751 { return reverse_iterator_impl
<T
>(m_cur
+ n
); }
752 reverse_iterator_impl
operator+=(int n
)
753 { m_cur
-= n
; return *this; }
754 reverse_iterator_impl
operator+=(size_t n
)
755 { m_cur
-= n
; return *this; }
756 reverse_iterator_impl
operator-=(int n
)
757 { m_cur
+= n
; return *this; }
758 reverse_iterator_impl
operator-=(size_t n
)
759 { m_cur
+= n
; return *this; }
761 unsigned operator-(const reverse_iterator_impl
& i
) const
762 { return i
.m_cur
- m_cur
; }
764 bool operator==(const reverse_iterator_impl
& ri
) const
765 { return m_cur
== ri
.m_cur
; }
766 bool operator!=(const reverse_iterator_impl
& ri
) const
767 { return !(*this == ri
); }
769 bool operator<(const reverse_iterator_impl
& i
) const
770 { return m_cur
> i
.m_cur
; }
771 bool operator>(const reverse_iterator_impl
& i
) const
772 { return m_cur
< i
.m_cur
; }
773 bool operator<=(const reverse_iterator_impl
& i
) const
774 { return m_cur
>= i
.m_cur
; }
775 bool operator>=(const reverse_iterator_impl
& i
) const
776 { return m_cur
<= i
.m_cur
; }
782 typedef reverse_iterator_impl
<iterator
> reverse_iterator
;
783 typedef reverse_iterator_impl
<const_iterator
> const_reverse_iterator
;
786 // used to transform an expression built using c_str() (and hence of type
787 // wxCStrData) to an iterator into the string
788 static const_iterator
CreateConstIterator(const wxCStrData
& data
)
790 return const_iterator(data
.m_str
->begin() + data
.m_offset
);
793 // in UTF-8 STL build, creation from std::string requires conversion under
794 // non-UTF8 locales, so we can't have and use wxString(wxStringImpl) ctor;
795 // instead we define dummy type that lets us have wxString ctor for creation
796 // from wxStringImpl that couldn't be used by user code (in all other builds,
797 // "standard" ctors can be used):
798 #if wxUSE_UNICODE_UTF8 && wxUSE_STL_BASED_WXSTRING
799 struct CtorFromStringImplTag
{};
801 wxString(CtorFromStringImplTag
* WXUNUSED(dummy
), const wxStringImpl
& src
)
804 static wxString
FromImpl(const wxStringImpl
& src
)
805 { return wxString((CtorFromStringImplTag
*)NULL
, src
); }
807 #if !wxUSE_STL_BASED_WXSTRING
808 wxString(const wxStringImpl
& src
) : m_impl(src
) { }
809 // else: already defined as wxString(wxStdString) below
811 static wxString
FromImpl(const wxStringImpl
& src
) { return wxString(src
); }
815 // constructors and destructor
816 // ctor for an empty string
820 wxString(const wxString
& stringSrc
) : m_impl(stringSrc
.m_impl
) { }
822 // string containing nRepeat copies of ch
823 wxString(wxUniChar ch
, size_t nRepeat
= 1)
824 { assign(nRepeat
, ch
); }
825 wxString(size_t nRepeat
, wxUniChar ch
)
826 { assign(nRepeat
, ch
); }
827 wxString(wxUniCharRef ch
, size_t nRepeat
= 1)
828 { assign(nRepeat
, ch
); }
829 wxString(size_t nRepeat
, wxUniCharRef ch
)
830 { assign(nRepeat
, ch
); }
831 wxString(char ch
, size_t nRepeat
= 1)
832 { assign(nRepeat
, ch
); }
833 wxString(size_t nRepeat
, char ch
)
834 { assign(nRepeat
, ch
); }
835 wxString(wchar_t ch
, size_t nRepeat
= 1)
836 { assign(nRepeat
, ch
); }
837 wxString(size_t nRepeat
, wchar_t ch
)
838 { assign(nRepeat
, ch
); }
840 // ctors from char* strings:
841 wxString(const char *psz
)
842 : m_impl(ImplStr(psz
)) {}
843 wxString(const char *psz
, const wxMBConv
& conv
)
844 : m_impl(ImplStr(psz
, conv
)) {}
845 wxString(const char *psz
, size_t nLength
)
846 { assign(psz
, nLength
); }
847 wxString(const char *psz
, const wxMBConv
& conv
, size_t nLength
)
849 SubstrBufFromMB
str(ImplStr(psz
, nLength
, conv
));
850 m_impl
.assign(str
.data
, str
.len
);
853 // and unsigned char*:
854 wxString(const unsigned char *psz
)
855 : m_impl(ImplStr((const char*)psz
)) {}
856 wxString(const unsigned char *psz
, const wxMBConv
& conv
)
857 : m_impl(ImplStr((const char*)psz
, conv
)) {}
858 wxString(const unsigned char *psz
, size_t nLength
)
859 { assign((const char*)psz
, nLength
); }
860 wxString(const unsigned char *psz
, const wxMBConv
& conv
, size_t nLength
)
862 SubstrBufFromMB
str(ImplStr((const char*)psz
, nLength
, conv
));
863 m_impl
.assign(str
.data
, str
.len
);
866 // ctors from wchar_t* strings:
867 wxString(const wchar_t *pwz
)
868 : m_impl(ImplStr(pwz
)) {}
869 wxString(const wchar_t *pwz
, const wxMBConv
& WXUNUSED(conv
))
870 : m_impl(ImplStr(pwz
)) {}
871 wxString(const wchar_t *pwz
, size_t nLength
)
872 { assign(pwz
, nLength
); }
873 wxString(const wchar_t *pwz
, const wxMBConv
& WXUNUSED(conv
), size_t nLength
)
874 { assign(pwz
, nLength
); }
876 wxString(const wxCharBuffer
& buf
)
877 { assign(buf
.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
878 wxString(const wxWCharBuffer
& buf
)
879 { assign(buf
.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
881 wxString(const wxCStrData
& cstr
)
882 : m_impl(cstr
.AsString().m_impl
) { }
884 // as we provide both ctors with this signature for both char and unsigned
885 // char string, we need to provide one for wxCStrData to resolve ambiguity
886 wxString(const wxCStrData
& cstr
, size_t nLength
)
887 : m_impl(cstr
.AsString().Mid(0, nLength
).m_impl
) {}
889 // and because wxString is convertible to wxCStrData and const wxChar *
890 // we also need to provide this one
891 wxString(const wxString
& str
, size_t nLength
)
892 : m_impl(str
.Mid(0, nLength
).m_impl
) {}
894 // even if we're not built with wxUSE_STL == 1 it is very convenient to allow
895 // implicit conversions from std::string to wxString and vice verse as this
896 // allows to use the same strings in non-GUI and GUI code, however we don't
897 // want to unconditionally add this ctor as it would make wx lib dependent on
898 // libstdc++ on some Linux versions which is bad, so instead we ask the
899 // client code to define this wxUSE_STD_STRING symbol if they need it
901 #if wxUSE_UNICODE_WCHAR
902 wxString(const wxStdWideString
& str
) : m_impl(str
) {}
903 #else // UTF-8 or ANSI
904 wxString(const wxStdWideString
& str
)
905 { assign(str
.c_str(), str
.length()); }
908 #if !wxUSE_UNICODE // ANSI build
909 // FIXME-UTF8: do this in UTF8 build #if wxUSE_UTF8_LOCALE_ONLY, too
910 wxString(const std::string
& str
) : m_impl(str
) {}
912 wxString(const std::string
& str
)
913 { assign(str
.c_str(), str
.length()); }
916 #if wxUSE_UNICODE_WCHAR && wxUSE_STL_BASED_WXSTRING
917 // wxStringImpl is std::string in the encoding we want
918 operator const wxStdWideString
&() const { return m_impl
; }
920 // wxStringImpl is either not std::string or needs conversion
921 operator wxStdWideString() const
922 // FIXME-UTF8: broken for embedded NULs
923 { return wxStdWideString(wc_str()); }
926 #if !wxUSE_UNICODE && wxUSE_STL_BASED_WXSTRING
927 // FIXME-UTF8: do this in UTF8 build #if wxUSE_UTF8_LOCALE_ONLY, too
928 // wxStringImpl is std::string in the encoding we want
929 operator const std::string
&() const { return m_impl
; }
931 // wxStringImpl is either not std::string or needs conversion
932 operator std::string() const
933 // FIXME-UTF8: broken for embedded NULs
934 { return std::string(mb_str()); }
937 #endif // wxUSE_STD_STRING
939 // first valid index position
940 const_iterator
begin() const { return const_iterator(m_impl
.begin()); }
941 iterator
begin() { return iterator(this, m_impl
.begin()); }
942 // position one after the last valid one
943 const_iterator
end() const { return const_iterator(m_impl
.end()); }
944 iterator
end() { return iterator(this, m_impl
.end()); }
946 // first element of the reversed string
947 const_reverse_iterator
rbegin() const
948 { return const_reverse_iterator(end()); }
949 reverse_iterator
rbegin()
950 { return reverse_iterator(end()); }
951 // one beyond the end of the reversed string
952 const_reverse_iterator
rend() const
953 { return const_reverse_iterator(begin()); }
954 reverse_iterator
rend()
955 { return reverse_iterator(begin()); }
957 // std::string methods:
958 #if wxUSE_UNICODE_UTF8
959 size_t length() const { return end() - begin(); } // FIXME-UTF8: optimize!
961 size_t length() const { return m_impl
.length(); }
964 size_type
size() const { return length(); }
965 size_type
max_size() const { return npos
; }
967 bool empty() const { return m_impl
.empty(); }
969 size_type
capacity() const { return m_impl
.capacity(); } // FIXME-UTF8
970 void reserve(size_t sz
) { m_impl
.reserve(sz
); } // FIXME-UTF8
972 void resize(size_t nSize
, wxUniChar ch
= wxT('\0'))
974 #if wxUSE_UNICODE_UTF8
977 size_t len
= length();
980 else if ( nSize
< len
)
983 append(nSize
- len
, ch
);
987 m_impl
.resize(nSize
, (wxStringCharType
)ch
);
990 wxString
substr(size_t nStart
= 0, size_t nLen
= npos
) const
993 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
994 return FromImpl(m_impl
.substr(pos
, len
));
997 // generic attributes & operations
998 // as standard strlen()
999 size_t Len() const { return length(); }
1000 // string contains any characters?
1001 bool IsEmpty() const { return empty(); }
1002 // empty string is "false", so !str will return true
1003 bool operator!() const { return empty(); }
1004 // truncate the string to given length
1005 wxString
& Truncate(size_t uiLen
);
1006 // empty string contents
1011 wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") );
1013 // empty the string and free memory
1016 wxString
tmp(wxEmptyString
);
1021 // Is an ascii value
1022 bool IsAscii() const;
1024 bool IsNumber() const;
1026 bool IsWord() const;
1028 // data access (all indexes are 0 based)
1030 wxUniChar
at(size_t n
) const
1031 { return *(begin() + n
); } // FIXME-UTF8: optimize?
1032 wxUniChar
GetChar(size_t n
) const
1034 // read/write access
1035 wxUniCharRef
at(size_t n
)
1036 { return *(begin() + n
); } // FIXME-UTF8: optimize?
1037 wxUniCharRef
GetWritableChar(size_t n
)
1040 void SetChar(size_t n
, wxUniChar ch
)
1043 // get last character
1044 wxUniChar
Last() const
1046 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1048 return at(length() - 1);
1051 // get writable last character
1054 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1055 return at(length() - 1);
1059 Note that we we must define all of the overloads below to avoid
1060 ambiguity when using str[0].
1062 wxUniChar
operator[](int n
) const
1064 wxUniChar
operator[](long n
) const
1066 wxUniChar
operator[](size_t n
) const
1068 #ifndef wxSIZE_T_IS_UINT
1069 wxUniChar
operator[](unsigned int n
) const
1071 #endif // size_t != unsigned int
1073 // operator versions of GetWriteableChar()
1074 wxUniCharRef
operator[](int n
)
1076 wxUniCharRef
operator[](long n
)
1078 wxUniCharRef
operator[](size_t n
)
1080 #ifndef wxSIZE_T_IS_UINT
1081 wxUniCharRef
operator[](unsigned int n
)
1083 #endif // size_t != unsigned int
1085 // explicit conversion to C string (use this with printf()!)
1086 wxCStrData
c_str() const { return wxCStrData(this); }
1087 wxCStrData
data() const { return c_str(); }
1089 // implicit conversion to C string
1090 operator wxCStrData() const { return c_str(); }
1091 operator const char*() const { return c_str(); }
1092 operator const wchar_t*() const { return c_str(); }
1094 // implicit conversion to untyped pointer for compatibility with previous
1095 // wxWidgets versions: this is the same as conversion to const char * so it
1097 operator const void*() const { return c_str(); }
1099 // identical to c_str(), for MFC compatibility
1100 const wxCStrData
GetData() const { return c_str(); }
1102 // explicit conversion to C string in internal representation (char*,
1103 // wchar_t*, UTF-8-encoded char*, depending on the build):
1104 const wxStringCharType
*wx_str() const { return m_impl
.c_str(); }
1106 // conversion to *non-const* multibyte or widestring buffer; modifying
1107 // returned buffer won't affect the string, these methods are only useful
1108 // for passing values to const-incorrect functions
1109 wxWritableCharBuffer
char_str(const wxMBConv
& conv
= wxConvLibc
) const
1110 { return mb_str(conv
); }
1111 wxWritableWCharBuffer
wchar_str() const { return wc_str(); }
1113 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
1114 // converting numbers or strings which are certain not to contain special
1115 // chars (typically system functions, X atoms, environment variables etc.)
1117 // the behaviour of these functions with the strings containing anything
1118 // else than 7 bit ASCII characters is undefined, use at your own risk.
1120 static wxString
FromAscii(const char *ascii
); // string
1121 static wxString
FromAscii(const char ascii
); // char
1122 const wxCharBuffer
ToAscii() const;
1124 static wxString
FromAscii(const char *ascii
) { return wxString( ascii
); }
1125 static wxString
FromAscii(const char ascii
) { return wxString( ascii
); }
1126 const char *ToAscii() const { return c_str(); }
1127 #endif // Unicode/!Unicode
1129 // conversions with (possible) format conversions: have to return a
1130 // buffer with temporary data
1132 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
1133 // return an ANSI (multibyte) string, wc_str() to return a wide string and
1134 // fn_str() to return a string which should be used with the OS APIs
1135 // accepting the file names. The return value is always the same, but the
1136 // type differs because a function may either return pointer to the buffer
1137 // directly or have to use intermediate buffer for translation.
1139 const wxCharBuffer
mb_str(const wxMBConv
& conv
= wxConvLibc
) const;
1141 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
1143 #if wxUSE_UNICODE_WCHAR
1144 const wxChar
* wc_str() const { return wx_str(); }
1145 #elif wxUSE_UNICODE_UTF8
1146 const wxWCharBuffer
wc_str() const;
1148 // for compatibility with !wxUSE_UNICODE version
1149 const wxWX2WCbuf
wc_str(const wxMBConv
& WXUNUSED(conv
)) const
1150 { return wc_str(); }
1153 const wxCharBuffer
fn_str() const { return mb_str(wxConvFile
); }
1155 const wxWX2WCbuf
fn_str() const { return wc_str(); }
1156 #endif // wxMBFILES/!wxMBFILES
1159 const wxChar
* mb_str() const { return wx_str(); }
1161 // for compatibility with wxUSE_UNICODE version
1162 const wxChar
* mb_str(const wxMBConv
& WXUNUSED(conv
)) const { return wx_str(); }
1164 const wxWX2MBbuf
mbc_str() const { return mb_str(); }
1167 const wxWCharBuffer
wc_str(const wxMBConv
& conv
= wxConvLibc
) const;
1168 #endif // wxUSE_WCHAR_T
1170 const wxCharBuffer
fn_str() const { return wxConvFile
.cWC2WX( wc_str( wxConvLocal
) ); }
1172 const wxChar
* fn_str() const { return c_str(); }
1174 #endif // Unicode/ANSI
1176 // overloaded assignment
1177 // from another wxString
1178 wxString
& operator=(const wxString
& stringSrc
)
1179 { m_impl
= stringSrc
.m_impl
; return *this; }
1180 wxString
& operator=(const wxCStrData
& cstr
)
1181 { return *this = cstr
.AsString(); }
1183 wxString
& operator=(wxUniChar ch
)
1184 { m_impl
= wxStringOperations::EncodeChar(ch
); return *this; }
1185 wxString
& operator=(wxUniCharRef ch
)
1186 { return operator=((wxUniChar
)ch
); }
1187 wxString
& operator=(char ch
)
1188 { return operator=(wxUniChar(ch
)); }
1189 wxString
& operator=(unsigned char ch
)
1190 { return operator=(wxUniChar(ch
)); }
1191 wxString
& operator=(wchar_t ch
)
1192 { return operator=(wxUniChar(ch
)); }
1193 // from a C string - STL probably will crash on NULL,
1194 // so we need to compensate in that case
1195 #if wxUSE_STL_BASED_WXSTRING
1196 wxString
& operator=(const char *psz
)
1197 { if (psz
) m_impl
= ImplStr(psz
); else Clear(); return *this; }
1198 wxString
& operator=(const wchar_t *pwz
)
1199 { if (pwz
) m_impl
= ImplStr(pwz
); else Clear(); return *this; }
1201 wxString
& operator=(const char *psz
)
1202 { m_impl
= ImplStr(psz
); return *this; }
1203 wxString
& operator=(const wchar_t *pwz
)
1204 { m_impl
= ImplStr(pwz
); return *this; }
1206 wxString
& operator=(const unsigned char *psz
)
1207 { return operator=((const char*)psz
); }
1209 // from wxWCharBuffer
1210 wxString
& operator=(const wxWCharBuffer
& s
)
1211 { return operator=(s
.data()); } // FIXME-UTF8: fix for embedded NULs
1212 // from wxCharBuffer
1213 wxString
& operator=(const wxCharBuffer
& s
)
1214 { return operator=(s
.data()); } // FIXME-UTF8: fix for embedded NULs
1216 // string concatenation
1217 // in place concatenation
1219 Concatenate and return the result. Note that the left to right
1220 associativity of << allows to write things like "str << str1 << str2
1221 << ..." (unlike with +=)
1224 wxString
& operator<<(const wxString
& s
)
1226 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1227 wxASSERT_MSG( s
.IsValid(),
1228 _T("did you forget to call UngetWriteBuf()?") );
1234 // string += C string
1235 wxString
& operator<<(const char *psz
)
1236 { append(psz
); return *this; }
1237 wxString
& operator<<(const wchar_t *pwz
)
1238 { append(pwz
); return *this; }
1239 wxString
& operator<<(const wxCStrData
& psz
)
1240 { append(psz
.AsString()); return *this; }
1242 wxString
& operator<<(wxUniChar ch
) { append(1, ch
); return *this; }
1243 wxString
& operator<<(wxUniCharRef ch
) { append(1, ch
); return *this; }
1244 wxString
& operator<<(char ch
) { append(1, ch
); return *this; }
1245 wxString
& operator<<(unsigned char ch
) { append(1, ch
); return *this; }
1246 wxString
& operator<<(wchar_t ch
) { append(1, ch
); return *this; }
1248 // string += buffer (i.e. from wxGetString)
1249 wxString
& operator<<(const wxWCharBuffer
& s
)
1250 { return operator<<((const wchar_t *)s
); }
1251 wxString
& operator+=(const wxWCharBuffer
& s
)
1252 { return operator<<((const wchar_t *)s
); }
1254 wxString
& operator<<(const wxCharBuffer
& s
)
1255 { return operator<<((const char *)s
); }
1256 wxString
& operator+=(const wxCharBuffer
& s
)
1257 { return operator<<((const char *)s
); }
1259 // string += C string
1260 wxString
& Append(const wxString
& s
)
1262 // test for empty() to share the string if possible
1269 wxString
& Append(const wxCStrData
& psz
)
1270 { append(psz
); return *this; }
1271 wxString
& Append(const char* psz
)
1272 { append(psz
); return *this; }
1273 wxString
& Append(const wchar_t* pwz
)
1274 { append(pwz
); return *this; }
1275 // append count copies of given character
1276 wxString
& Append(wxUniChar ch
, size_t count
= 1u)
1277 { append(count
, ch
); return *this; }
1278 wxString
& Append(wxUniCharRef ch
, size_t count
= 1u)
1279 { append(count
, ch
); return *this; }
1280 wxString
& Append(char ch
, size_t count
= 1u)
1281 { append(count
, ch
); return *this; }
1282 wxString
& Append(unsigned char ch
, size_t count
= 1u)
1283 { append(count
, ch
); return *this; }
1284 wxString
& Append(wchar_t ch
, size_t count
= 1u)
1285 { append(count
, ch
); return *this; }
1286 wxString
& Append(const char* psz
, size_t nLen
)
1287 { append(psz
, nLen
); return *this; }
1288 wxString
& Append(const wchar_t* pwz
, size_t nLen
)
1289 { append(pwz
, nLen
); return *this; }
1291 // prepend a string, return the string itself
1292 wxString
& Prepend(const wxString
& str
)
1293 { *this = str
+ *this; return *this; }
1295 // non-destructive concatenation
1297 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
,
1298 const wxString
& string2
);
1299 // string with a single char
1300 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
1301 // char with a string
1302 friend wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
1303 // string with C string
1304 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
1306 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
1307 const wchar_t *pwz
);
1308 // C string with string
1309 friend wxString WXDLLIMPEXP_BASE
operator+(const char *psz
,
1310 const wxString
& string
);
1311 friend wxString WXDLLIMPEXP_BASE
operator+(const wchar_t *pwz
,
1312 const wxString
& string
);
1314 // stream-like functions
1315 // insert an int into string
1316 wxString
& operator<<(int i
)
1317 { return (*this) << Format(_T("%d"), i
); }
1318 // insert an unsigned int into string
1319 wxString
& operator<<(unsigned int ui
)
1320 { return (*this) << Format(_T("%u"), ui
); }
1321 // insert a long into string
1322 wxString
& operator<<(long l
)
1323 { return (*this) << Format(_T("%ld"), l
); }
1324 // insert an unsigned long into string
1325 wxString
& operator<<(unsigned long ul
)
1326 { return (*this) << Format(_T("%lu"), ul
); }
1327 #if defined wxLongLong_t && !defined wxLongLongIsLong
1328 // insert a long long if they exist and aren't longs
1329 wxString
& operator<<(wxLongLong_t ll
)
1331 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("d");
1332 return (*this) << Format(fmt
, ll
);
1334 // insert an unsigned long long
1335 wxString
& operator<<(wxULongLong_t ull
)
1337 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("u");
1338 return (*this) << Format(fmt
, ull
);
1341 // insert a float into string
1342 wxString
& operator<<(float f
)
1343 { return (*this) << Format(_T("%f"), f
); }
1344 // insert a double into string
1345 wxString
& operator<<(double d
)
1346 { return (*this) << Format(_T("%g"), d
); }
1348 // string comparison
1349 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
1350 int Cmp(const char *psz
) const
1351 { return compare(psz
); }
1352 int Cmp(const wchar_t *pwz
) const
1353 { return compare(pwz
); }
1354 int Cmp(const wxString
& s
) const
1355 { return compare(s
); }
1356 // same as Cmp() but not case-sensitive
1357 int CmpNoCase(const wxString
& s
) const;
1358 int CmpNoCase(const char *psz
) const
1359 { return CmpNoCase(wxString(psz
)); }
1360 int CmpNoCase(const wchar_t *pwz
) const
1361 { return CmpNoCase(wxString(pwz
)); }
1362 // test for the string equality, either considering case or not
1363 // (if compareWithCase then the case matters)
1364 bool IsSameAs(const wxString
& str
, bool compareWithCase
= true) const
1365 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
1366 bool IsSameAs(const char *str
, bool compareWithCase
= true) const
1367 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
1368 bool IsSameAs(const wchar_t *str
, bool compareWithCase
= true) const
1369 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
1370 // comparison with a single character: returns true if equal
1371 bool IsSameAs(wxUniChar c
, bool compareWithCase
= true) const
1373 return (length() == 1) && (compareWithCase
? GetChar(0u) == c
1374 : wxToupper(GetChar(0u)) == wxToupper(c
));
1376 // FIXME-UTF8: remove these overloads
1377 bool IsSameAs(wxUniCharRef c
, bool compareWithCase
= true) const
1378 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1379 bool IsSameAs(char c
, bool compareWithCase
= true) const
1380 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1381 bool IsSameAs(unsigned char c
, bool compareWithCase
= true) const
1382 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1383 bool IsSameAs(wchar_t c
, bool compareWithCase
= true) const
1384 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1385 bool IsSameAs(int c
, bool compareWithCase
= true) const
1386 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1388 // simple sub-string extraction
1389 // return substring starting at nFirst of length nCount (or till the end
1390 // if nCount = default value)
1391 wxString
Mid(size_t nFirst
, size_t nCount
= npos
) const;
1393 // operator version of Mid()
1394 wxString
operator()(size_t start
, size_t len
) const
1395 { return Mid(start
, len
); }
1397 // check if the string starts with the given prefix and return the rest
1398 // of the string in the provided pointer if it is not NULL; otherwise
1400 bool StartsWith(const wxChar
*prefix
, wxString
*rest
= NULL
) const;
1401 // check if the string ends with the given suffix and return the
1402 // beginning of the string before the suffix in the provided pointer if
1403 // it is not NULL; otherwise return false
1404 bool EndsWith(const wxChar
*suffix
, wxString
*rest
= NULL
) const;
1406 // get first nCount characters
1407 wxString
Left(size_t nCount
) const;
1408 // get last nCount characters
1409 wxString
Right(size_t nCount
) const;
1410 // get all characters before the first occurance of ch
1411 // (returns the whole string if ch not found)
1412 wxString
BeforeFirst(wxUniChar ch
) const;
1413 // get all characters before the last occurence of ch
1414 // (returns empty string if ch not found)
1415 wxString
BeforeLast(wxUniChar ch
) const;
1416 // get all characters after the first occurence of ch
1417 // (returns empty string if ch not found)
1418 wxString
AfterFirst(wxUniChar ch
) const;
1419 // get all characters after the last occurence of ch
1420 // (returns the whole string if ch not found)
1421 wxString
AfterLast(wxUniChar ch
) const;
1423 // for compatibility only, use more explicitly named functions above
1424 wxString
Before(wxUniChar ch
) const { return BeforeLast(ch
); }
1425 wxString
After(wxUniChar ch
) const { return AfterFirst(ch
); }
1428 // convert to upper case in place, return the string itself
1429 wxString
& MakeUpper();
1430 // convert to upper case, return the copy of the string
1431 // Here's something to remember: BC++ doesn't like returns in inlines.
1432 wxString
Upper() const ;
1433 // convert to lower case in place, return the string itself
1434 wxString
& MakeLower();
1435 // convert to lower case, return the copy of the string
1436 wxString
Lower() const ;
1438 // trimming/padding whitespace (either side) and truncating
1439 // remove spaces from left or from right (default) side
1440 wxString
& Trim(bool bFromRight
= true);
1441 // add nCount copies chPad in the beginning or at the end (default)
1442 wxString
& Pad(size_t nCount
, wxUniChar chPad
= wxT(' '), bool bFromRight
= true);
1444 // searching and replacing
1445 // searching (return starting index, or -1 if not found)
1446 int Find(wxUniChar ch
, bool bFromEnd
= false) const; // like strchr/strrchr
1447 int Find(wxUniCharRef ch
, bool bFromEnd
= false) const
1448 { return Find(wxUniChar(ch
), bFromEnd
); }
1449 int Find(char ch
, bool bFromEnd
= false) const
1450 { return Find(wxUniChar(ch
), bFromEnd
); }
1451 int Find(unsigned char ch
, bool bFromEnd
= false) const
1452 { return Find(wxUniChar(ch
), bFromEnd
); }
1453 int Find(wchar_t ch
, bool bFromEnd
= false) const
1454 { return Find(wxUniChar(ch
), bFromEnd
); }
1455 // searching (return starting index, or -1 if not found)
1456 // FIXME-UTF8: keep wxString overload only
1457 int Find(const wxString
& sub
) const // like strstr
1459 size_type idx
= find(sub
);
1460 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
1462 int Find(const char *sub
) const // like strstr
1464 size_type idx
= find(sub
);
1465 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
1467 int Find(const wchar_t *sub
) const // like strstr
1469 size_type idx
= find(sub
);
1470 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
1473 // replace first (or all of bReplaceAll) occurences of substring with
1474 // another string, returns the number of replacements made
1475 size_t Replace(const wxString
& strOld
,
1476 const wxString
& strNew
,
1477 bool bReplaceAll
= true);
1479 // check if the string contents matches a mask containing '*' and '?'
1480 bool Matches(const wxString
& mask
) const;
1482 // conversion to numbers: all functions return true only if the whole
1483 // string is a number and put the value of this number into the pointer
1484 // provided, the base is the numeric base in which the conversion should be
1485 // done and must be comprised between 2 and 36 or be 0 in which case the
1486 // standard C rules apply (leading '0' => octal, "0x" => hex)
1487 // convert to a signed integer
1488 bool ToLong(long *val
, int base
= 10) const;
1489 // convert to an unsigned integer
1490 bool ToULong(unsigned long *val
, int base
= 10) const;
1491 // convert to wxLongLong
1492 #if defined(wxLongLong_t)
1493 bool ToLongLong(wxLongLong_t
*val
, int base
= 10) const;
1494 // convert to wxULongLong
1495 bool ToULongLong(wxULongLong_t
*val
, int base
= 10) const;
1496 #endif // wxLongLong_t
1497 // convert to a double
1498 bool ToDouble(double *val
) const;
1501 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1502 // formatted input/output
1503 // as sprintf(), returns the number of characters written or < 0 on error
1504 // (take 'this' into account in attribute parameter count)
1505 // int Printf(const wxString& format, ...);
1506 WX_DEFINE_VARARG_FUNC(int, Printf
, 1, (const wxString
&), DoPrintf
)
1508 WX_DEFINE_VARARG_FUNC(int, Printf
, 1, (const char*), DoPrintf
)
1509 WX_DEFINE_VARARG_FUNC(int, Printf
, 1, (const wchar_t*), DoPrintf
)
1510 WX_DEFINE_VARARG_FUNC(int, Printf
, 1, (const wxCStrData
&), DoPrintf
)
1512 #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN
1513 // as vprintf(), returns the number of characters written or < 0 on error
1514 int PrintfV(const wxString
& format
, va_list argptr
);
1516 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1517 // returns the string containing the result of Printf() to it
1518 // static wxString Format(const wxString& format, ...) ATTRIBUTE_PRINTF_1;
1519 WX_DEFINE_VARARG_FUNC(static wxString
, Format
, 1, (const wxString
&), DoFormat
)
1521 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1522 WX_DEFINE_VARARG_FUNC(static wxString
, Format
, 1, (const char*), DoFormat
)
1523 WX_DEFINE_VARARG_FUNC(static wxString
, Format
, 1, (const wchar_t*), DoFormat
)
1524 WX_DEFINE_VARARG_FUNC(static wxString
, Format
, 1, (const wxCStrData
&), DoFormat
)
1527 // the same as above, but takes a va_list
1528 static wxString
FormatV(const wxString
& format
, va_list argptr
);
1530 // raw access to string memory
1531 // ensure that string has space for at least nLen characters
1532 // only works if the data of this string is not shared
1533 bool Alloc(size_t nLen
) { reserve(nLen
); /*return capacity() >= nLen;*/ return true; }
1534 // minimize the string's memory
1535 // only works if the data of this string is not shared
1537 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1538 // These are deprecated, use wxStringBuffer or wxStringBufferLength instead
1540 // get writable buffer of at least nLen bytes. Unget() *must* be called
1541 // a.s.a.p. to put string back in a reasonable state!
1542 wxDEPRECATED( wxStringCharType
*GetWriteBuf(size_t nLen
) );
1543 // call this immediately after GetWriteBuf() has been used
1544 wxDEPRECATED( void UngetWriteBuf() );
1545 wxDEPRECATED( void UngetWriteBuf(size_t nLen
) );
1546 #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && wxUSE_UNICODE_UTF8
1548 // wxWidgets version 1 compatibility functions
1551 wxString
SubString(size_t from
, size_t to
) const
1552 { return Mid(from
, (to
- from
+ 1)); }
1553 // values for second parameter of CompareTo function
1554 enum caseCompare
{exact
, ignoreCase
};
1555 // values for first parameter of Strip function
1556 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
1558 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1560 // (take 'this' into account in attribute parameter count)
1561 // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2;
1562 WX_DEFINE_VARARG_FUNC(int, sprintf
, 1, (const wxString
&), DoPrintf
)
1564 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1565 WX_DEFINE_VARARG_FUNC(int, sprintf
, 1, (const char*), DoPrintf
)
1566 WX_DEFINE_VARARG_FUNC(int, sprintf
, 1, (const wchar_t*), DoPrintf
)
1567 WX_DEFINE_VARARG_FUNC(int, sprintf
, 1, (const wxCStrData
&), DoPrintf
)
1569 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
1572 inline int CompareTo(const wxChar
* psz
, caseCompare cmp
= exact
) const
1573 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
1576 size_t Length() const { return length(); }
1577 // Count the number of characters
1578 int Freq(wxUniChar ch
) const;
1580 void LowerCase() { MakeLower(); }
1582 void UpperCase() { MakeUpper(); }
1583 // use Trim except that it doesn't change this string
1584 wxString
Strip(stripType w
= trailing
) const;
1586 // use Find (more general variants not yet supported)
1587 size_t Index(const wxChar
* psz
) const { return Find(psz
); }
1588 size_t Index(wxUniChar ch
) const { return Find(ch
); }
1590 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
1591 wxString
& RemoveLast(size_t n
= 1) { return Truncate(length() - n
); }
1593 wxString
& Remove(size_t nStart
, size_t nLen
)
1594 { return (wxString
&)erase( nStart
, nLen
); }
1597 int First( wxUniChar ch
) const { return Find(ch
); }
1598 int First( wxUniCharRef ch
) const { return Find(ch
); }
1599 int First( char ch
) const { return Find(ch
); }
1600 int First( unsigned char ch
) const { return Find(ch
); }
1601 int First( wchar_t ch
) const { return Find(ch
); }
1602 int First( const wxChar
* psz
) const { return Find(psz
); }
1603 int First( const wxString
& str
) const { return Find(str
); }
1604 int Last( wxUniChar ch
) const { return Find(ch
, true); }
1605 bool Contains(const wxString
& str
) const { return Find(str
) != wxNOT_FOUND
; }
1608 bool IsNull() const { return empty(); }
1610 // std::string compatibility functions
1612 // take nLen chars starting at nPos
1613 wxString(const wxString
& str
, size_t nPos
, size_t nLen
)
1614 : m_impl(str
.m_impl
, nPos
, nLen
) { }
1615 // take all characters from first to last
1616 wxString(const_iterator first
, const_iterator last
)
1617 : m_impl(first
.impl(), last
.impl()) { }
1618 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1619 // the 2 overloads below are for compatibility with the existing code using
1620 // pointers instead of iterators
1621 wxString(const char *first
, const char *last
)
1623 SubstrBufFromMB
str(ImplStr(first
, last
- first
));
1624 m_impl
.assign(str
.data
, str
.len
);
1626 wxString(const wchar_t *first
, const wchar_t *last
)
1628 SubstrBufFromWC
str(ImplStr(first
, last
- first
));
1629 m_impl
.assign(str
.data
, str
.len
);
1631 // and this one is needed to compile code adding offsets to c_str() result
1632 wxString(const wxCStrData
& first
, const wxCStrData
& last
)
1633 : m_impl(CreateConstIterator(first
).impl(),
1634 CreateConstIterator(last
).impl())
1636 wxASSERT_MSG( first
.m_str
== last
.m_str
,
1637 _T("pointers must be into the same string") );
1639 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1641 // lib.string.modifiers
1642 // append elements str[pos], ..., str[pos+n]
1643 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
1646 str
.PosLenToImpl(pos
, n
, &from
, &len
);
1647 m_impl
.append(str
.m_impl
, from
, len
);
1651 wxString
& append(const wxString
& str
)
1652 { m_impl
.append(str
.m_impl
); return *this; }
1653 wxString
& append(const wxCStrData
& str
)
1654 { m_impl
.append(str
.AsString().m_impl
); return *this; }
1655 // append first n (or all if n == npos) characters of sz
1656 wxString
& append(const char *sz
)
1657 { m_impl
.append(ImplStr(sz
)); return *this; }
1658 wxString
& append(const wchar_t *sz
)
1659 { m_impl
.append(ImplStr(sz
)); return *this; }
1660 wxString
& append(const char *sz
, size_t n
)
1662 SubstrBufFromMB
str(ImplStr(sz
, n
));
1663 m_impl
.append(str
.data
, str
.len
);
1666 wxString
& append(const wchar_t *sz
, size_t n
)
1668 SubstrBufFromWC
str(ImplStr(sz
, n
));
1669 m_impl
.append(str
.data
, str
.len
);
1672 // append n copies of ch
1673 wxString
& append(size_t n
, wxUniChar ch
)
1675 #if wxUSE_UNICODE_UTF8
1676 if ( !ch
.IsAscii() )
1677 m_impl
.append(wxStringOperations::EncodeNChars(n
, ch
));
1680 m_impl
.append(n
, (wxStringCharType
)ch
);
1683 // append from first to last
1684 wxString
& append(const_iterator first
, const_iterator last
)
1685 { m_impl
.append(first
.impl(), last
.impl()); return *this; }
1686 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1687 wxString
& append(const char *first
, const char *last
)
1688 { return append(first
, last
- first
); }
1689 wxString
& append(const wchar_t *first
, const wchar_t *last
)
1690 { return append(first
, last
- first
); }
1691 wxString
& append(const wxCStrData
& first
, const wxCStrData
& last
)
1692 { return append(CreateConstIterator(first
), CreateConstIterator(last
)); }
1693 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1695 // same as `this_string = str'
1696 wxString
& assign(const wxString
& str
)
1697 { m_impl
= str
.m_impl
; return *this; }
1698 wxString
& assign(const wxString
& str
, size_t len
)
1700 m_impl
.assign(str
.m_impl
, 0, str
.LenToImpl(len
));
1703 // same as ` = str[pos..pos + n]
1704 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
1707 str
.PosLenToImpl(pos
, n
, &from
, &len
);
1708 m_impl
.assign(str
.m_impl
, from
, len
);
1711 // same as `= first n (or all if n == npos) characters of sz'
1712 wxString
& assign(const char *sz
)
1713 { m_impl
.assign(ImplStr(sz
)); return *this; }
1714 wxString
& assign(const wchar_t *sz
)
1715 { m_impl
.assign(ImplStr(sz
)); return *this; }
1716 wxString
& assign(const char *sz
, size_t n
)
1718 SubstrBufFromMB
str(ImplStr(sz
, n
));
1719 m_impl
.assign(str
.data
, str
.len
);
1722 wxString
& assign(const wchar_t *sz
, size_t n
)
1724 SubstrBufFromWC
str(ImplStr(sz
, n
));
1725 m_impl
.assign(str
.data
, str
.len
);
1728 // same as `= n copies of ch'
1729 wxString
& assign(size_t n
, wxUniChar ch
)
1731 #if wxUSE_UNICODE_UTF8
1732 if ( !ch
.IsAscii() )
1733 m_impl
.assign(wxStringOperations::EncodeNChars(n
, ch
));
1736 m_impl
.assign(n
, (wxStringCharType
)ch
);
1740 wxString
& assign(size_t n
, wxUniCharRef ch
)
1741 { return assign(n
, wxUniChar(ch
)); }
1742 wxString
& assign(size_t n
, char ch
)
1743 { return assign(n
, wxUniChar(ch
)); }
1744 wxString
& assign(size_t n
, unsigned char ch
)
1745 { return assign(n
, wxUniChar(ch
)); }
1746 wxString
& assign(size_t n
, wchar_t ch
)
1747 { return assign(n
, wxUniChar(ch
)); }
1749 // assign from first to last
1750 wxString
& assign(const_iterator first
, const_iterator last
)
1751 { m_impl
.assign(first
.impl(), last
.impl()); return *this; }
1752 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1753 wxString
& assign(const char *first
, const char *last
)
1754 { return assign(first
, last
- first
); }
1755 wxString
& assign(const wchar_t *first
, const wchar_t *last
)
1756 { return assign(first
, last
- first
); }
1757 wxString
& assign(const wxCStrData
& first
, const wxCStrData
& last
)
1758 { return assign(CreateConstIterator(first
), CreateConstIterator(last
)); }
1759 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1761 // string comparison
1762 int compare(const wxString
& str
) const;
1763 // comparison with a substring
1764 int compare(size_t nStart
, size_t nLen
, const wxString
& str
) const;
1765 // comparison of 2 substrings
1766 int compare(size_t nStart
, size_t nLen
,
1767 const wxString
& str
, size_t nStart2
, size_t nLen2
) const;
1768 // just like strcmp()
1769 int compare(const char* sz
) const;
1770 int compare(const wchar_t* sz
) const;
1771 // substring comparison with first nCount characters of sz
1772 int compare(size_t nStart
, size_t nLen
,
1773 const char* sz
, size_t nCount
= npos
) const;
1774 int compare(size_t nStart
, size_t nLen
,
1775 const wchar_t* sz
, size_t nCount
= npos
) const;
1777 // insert another string
1778 wxString
& insert(size_t nPos
, const wxString
& str
)
1779 { insert(begin() + nPos
, str
.begin(), str
.end()); return *this; }
1780 // insert n chars of str starting at nStart (in str)
1781 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
1784 str
.PosLenToImpl(nStart
, n
, &from
, &len
);
1785 m_impl
.insert(PosToImpl(nPos
), str
.m_impl
, from
, len
);
1788 // insert first n (or all if n == npos) characters of sz
1789 wxString
& insert(size_t nPos
, const char *sz
)
1790 { m_impl
.insert(PosToImpl(nPos
), ImplStr(sz
)); return *this; }
1791 wxString
& insert(size_t nPos
, const wchar_t *sz
)
1792 { m_impl
.insert(PosToImpl(nPos
), ImplStr(sz
)); return *this; }
1793 wxString
& insert(size_t nPos
, const char *sz
, size_t n
)
1795 SubstrBufFromMB
str(ImplStr(sz
, n
));
1796 m_impl
.insert(PosToImpl(nPos
), str
.data
, str
.len
);
1799 wxString
& insert(size_t nPos
, const wchar_t *sz
, size_t n
)
1801 SubstrBufFromWC
str(ImplStr(sz
, n
));
1802 m_impl
.insert(PosToImpl(nPos
), str
.data
, str
.len
);
1805 // insert n copies of ch
1806 wxString
& insert(size_t nPos
, size_t n
, wxUniChar ch
)
1808 #if wxUSE_UNICODE_UTF8
1809 if ( !ch
.IsAscii() )
1810 m_impl
.insert(PosToImpl(nPos
), wxStringOperations::EncodeNChars(n
, ch
));
1813 m_impl
.insert(PosToImpl(nPos
), n
, (wxStringCharType
)ch
);
1816 iterator
insert(iterator it
, wxUniChar ch
)
1818 #if wxUSE_UNICODE_UTF8
1819 if ( !ch
.IsAscii() )
1821 size_t pos
= IterToImplPos(it
);
1822 m_impl
.insert(pos
, wxStringOperations::EncodeChar(ch
));
1823 return iterator(this, m_impl
.begin() + pos
);
1827 return iterator(this, m_impl
.insert(it
.impl(), (wxStringCharType
)ch
));
1829 void insert(iterator it
, const_iterator first
, const_iterator last
)
1830 { m_impl
.insert(it
.impl(), first
.impl(), last
.impl()); }
1831 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1832 void insert(iterator it
, const char *first
, const char *last
)
1833 { insert(it
- begin(), first
, last
- first
); }
1834 void insert(iterator it
, const wchar_t *first
, const wchar_t *last
)
1835 { insert(it
- begin(), first
, last
- first
); }
1836 void insert(iterator it
, const wxCStrData
& first
, const wxCStrData
& last
)
1837 { insert(it
, CreateConstIterator(first
), CreateConstIterator(last
)); }
1838 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1840 void insert(iterator it
, size_type n
, wxUniChar ch
)
1842 #if wxUSE_UNICODE_UTF8
1843 if ( !ch
.IsAscii() )
1844 m_impl
.insert(IterToImplPos(it
), wxStringOperations::EncodeNChars(n
, ch
));
1847 m_impl
.insert(it
.impl(), n
, (wxStringCharType
)ch
);
1850 // delete characters from nStart to nStart + nLen
1851 wxString
& erase(size_type pos
= 0, size_type n
= npos
)
1854 PosLenToImpl(pos
, n
, &from
, &len
);
1855 m_impl
.erase(from
, len
);
1858 // delete characters from first up to last
1859 iterator
erase(iterator first
, iterator last
)
1860 { return iterator(this, m_impl
.erase(first
.impl(), last
.impl())); }
1861 iterator
erase(iterator first
)
1862 { return iterator(this, m_impl
.erase(first
.impl())); }
1864 #ifdef wxSTRING_BASE_HASNT_CLEAR
1865 void clear() { erase(); }
1867 void clear() { m_impl
.clear(); }
1870 // replaces the substring of length nLen starting at nStart
1871 wxString
& replace(size_t nStart
, size_t nLen
, const char* sz
)
1874 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1875 m_impl
.replace(from
, len
, ImplStr(sz
));
1878 wxString
& replace(size_t nStart
, size_t nLen
, const wchar_t* sz
)
1881 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1882 m_impl
.replace(from
, len
, ImplStr(sz
));
1885 // replaces the substring of length nLen starting at nStart
1886 wxString
& replace(size_t nStart
, size_t nLen
, const wxString
& str
)
1889 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1890 m_impl
.replace(from
, len
, str
.m_impl
);
1893 // replaces the substring with nCount copies of ch
1894 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxUniChar ch
)
1897 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1898 #if wxUSE_UNICODE_UTF8
1899 if ( !ch
.IsAscii() )
1900 m_impl
.replace(from
, len
, wxStringOperations::EncodeNChars(nCount
, ch
));
1903 m_impl
.replace(from
, len
, nCount
, (wxStringCharType
)ch
);
1906 // replaces a substring with another substring
1907 wxString
& replace(size_t nStart
, size_t nLen
,
1908 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1911 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1914 str
.PosLenToImpl(nStart2
, nLen2
, &from2
, &len2
);
1916 m_impl
.replace(from
, len
, str
.m_impl
, from2
, len2
);
1919 // replaces the substring with first nCount chars of sz
1920 wxString
& replace(size_t nStart
, size_t nLen
,
1921 const char* sz
, size_t nCount
)
1924 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1926 SubstrBufFromMB
str(ImplStr(sz
, nCount
));
1928 m_impl
.replace(from
, len
, str
.data
, str
.len
);
1931 wxString
& replace(size_t nStart
, size_t nLen
,
1932 const wchar_t* sz
, size_t nCount
)
1935 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1937 SubstrBufFromWC
str(ImplStr(sz
, nCount
));
1939 m_impl
.replace(from
, len
, str
.data
, str
.len
);
1942 wxString
& replace(size_t nStart
, size_t nLen
,
1943 const wxString
& s
, size_t nCount
)
1946 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1947 m_impl
.replace(from
, len
, s
.m_impl
.c_str(), s
.LenToImpl(nCount
));
1951 wxString
& replace(iterator first
, iterator last
, const char* s
)
1952 { m_impl
.replace(first
.impl(), last
.impl(), ImplStr(s
)); return *this; }
1953 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
)
1954 { m_impl
.replace(first
.impl(), last
.impl(), ImplStr(s
)); return *this; }
1955 wxString
& replace(iterator first
, iterator last
, const char* s
, size_type n
)
1957 SubstrBufFromMB
str(ImplStr(s
, n
));
1958 m_impl
.replace(first
.impl(), last
.impl(), str
.data
, str
.len
);
1961 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
, size_type n
)
1963 SubstrBufFromWC
str(ImplStr(s
, n
));
1964 m_impl
.replace(first
.impl(), last
.impl(), str
.data
, str
.len
);
1967 wxString
& replace(iterator first
, iterator last
, const wxString
& s
)
1968 { m_impl
.replace(first
.impl(), last
.impl(), s
.m_impl
); return *this; }
1969 wxString
& replace(iterator first
, iterator last
, size_type n
, wxUniChar ch
)
1971 #if wxUSE_UNICODE_UTF8
1972 if ( !ch
.IsAscii() )
1973 m_impl
.replace(first
.impl(), last
.impl(),
1974 wxStringOperations::EncodeNChars(n
, ch
));
1977 m_impl
.replace(first
.impl(), last
.impl(), n
, (wxStringCharType
)ch
);
1980 wxString
& replace(iterator first
, iterator last
,
1981 const_iterator first1
, const_iterator last1
)
1983 m_impl
.replace(first
.impl(), last
.impl(), first1
.impl(), last1
.impl());
1986 wxString
& replace(iterator first
, iterator last
,
1987 const char *first1
, const char *last1
)
1988 { replace(first
, last
, first1
, last1
- first1
); return *this; }
1989 wxString
& replace(iterator first
, iterator last
,
1990 const wchar_t *first1
, const wchar_t *last1
)
1991 { replace(first
, last
, first1
, last1
- first1
); return *this; }
1994 void swap(wxString
& str
)
1995 { m_impl
.swap(str
.m_impl
); }
1998 size_t find(const wxString
& str
, size_t nStart
= 0) const
1999 { return PosFromImpl(m_impl
.find(str
.m_impl
, PosToImpl(nStart
))); }
2001 // find first n characters of sz
2002 size_t find(const char* sz
, size_t nStart
= 0, size_t n
= npos
) const
2004 SubstrBufFromMB
str(ImplStr(sz
, n
));
2005 return PosFromImpl(m_impl
.find(str
.data
, PosToImpl(nStart
), str
.len
));
2007 size_t find(const wchar_t* sz
, size_t nStart
= 0, size_t n
= npos
) const
2009 SubstrBufFromWC
str(ImplStr(sz
, n
));
2010 return PosFromImpl(m_impl
.find(str
.data
, PosToImpl(nStart
), str
.len
));
2013 // find the first occurence of character ch after nStart
2014 size_t find(wxUniChar ch
, size_t nStart
= 0) const
2016 return PosFromImpl(m_impl
.find(wxStringOperations::EncodeChar(ch
),
2017 PosToImpl(nStart
)));
2019 size_t find(wxUniCharRef ch
, size_t nStart
= 0) const
2020 { return find(wxUniChar(ch
), nStart
); }
2021 size_t find(char ch
, size_t nStart
= 0) const
2022 { return find(wxUniChar(ch
), nStart
); }
2023 size_t find(unsigned char ch
, size_t nStart
= 0) const
2024 { return find(wxUniChar(ch
), nStart
); }
2025 size_t find(wchar_t ch
, size_t nStart
= 0) const
2026 { return find(wxUniChar(ch
), nStart
); }
2028 // rfind() family is exactly like find() but works right to left
2030 // as find, but from the end
2031 size_t rfind(const wxString
& str
, size_t nStart
= npos
) const
2032 { return PosFromImpl(m_impl
.rfind(str
.m_impl
, PosToImpl(nStart
))); }
2034 // as find, but from the end
2035 size_t rfind(const char* sz
, size_t nStart
= npos
, size_t n
= npos
) const
2037 SubstrBufFromMB
str(ImplStr(sz
, n
));
2038 return PosFromImpl(m_impl
.rfind(str
.data
, PosToImpl(nStart
), str
.len
));
2040 size_t rfind(const wchar_t* sz
, size_t nStart
= npos
, size_t n
= npos
) const
2042 SubstrBufFromWC
str(ImplStr(sz
, n
));
2043 return PosFromImpl(m_impl
.rfind(str
.data
, PosToImpl(nStart
), str
.len
));
2045 // as find, but from the end
2046 size_t rfind(wxUniChar ch
, size_t nStart
= npos
) const
2048 return PosFromImpl(m_impl
.rfind(wxStringOperations::EncodeChar(ch
),
2049 PosToImpl(nStart
)));
2051 size_t rfind(wxUniCharRef ch
, size_t nStart
= npos
) const
2052 { return rfind(wxUniChar(ch
), nStart
); }
2053 size_t rfind(char ch
, size_t nStart
= npos
) const
2054 { return rfind(wxUniChar(ch
), nStart
); }
2055 size_t rfind(unsigned char ch
, size_t nStart
= npos
) const
2056 { return rfind(wxUniChar(ch
), nStart
); }
2057 size_t rfind(wchar_t ch
, size_t nStart
= npos
) const
2058 { return rfind(wxUniChar(ch
), nStart
); }
2060 // find first/last occurence of any character (not) in the set:
2061 #if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2062 // FIXME-UTF8: this is not entirely correct, because it doesn't work if
2063 // sizeof(wchar_t)==2 and surrogates are present in the string;
2064 // should we care? Probably not.
2065 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
2066 { return m_impl
.find_first_of(str
.m_impl
, nStart
); }
2067 size_t find_first_of(const char* sz
, size_t nStart
= 0) const
2068 { return m_impl
.find_first_of(ImplStr(sz
), nStart
); }
2069 size_t find_first_of(const wchar_t* sz
, size_t nStart
= 0) const
2070 { return m_impl
.find_first_of(ImplStr(sz
), nStart
); }
2071 size_t find_first_of(const char* sz
, size_t nStart
, size_t n
) const
2072 { return m_impl
.find_first_of(ImplStr(sz
), nStart
, n
); }
2073 size_t find_first_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
2074 { return m_impl
.find_first_of(ImplStr(sz
), nStart
, n
); }
2075 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const
2076 { return m_impl
.find_first_of((wxChar
)c
, nStart
); }
2078 size_t find_last_of(const wxString
& str
, size_t nStart
= npos
) const
2079 { return m_impl
.find_last_of(str
.m_impl
, nStart
); }
2080 size_t find_last_of(const char* sz
, size_t nStart
= npos
) const
2081 { return m_impl
.find_last_of(ImplStr(sz
), nStart
); }
2082 size_t find_last_of(const wchar_t* sz
, size_t nStart
= npos
) const
2083 { return m_impl
.find_last_of(ImplStr(sz
), nStart
); }
2084 size_t find_last_of(const char* sz
, size_t nStart
, size_t n
) const
2085 { return m_impl
.find_last_of(ImplStr(sz
), nStart
, n
); }
2086 size_t find_last_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
2087 { return m_impl
.find_last_of(ImplStr(sz
), nStart
, n
); }
2088 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const
2089 { return m_impl
.find_last_of((wxChar
)c
, nStart
); }
2091 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
2092 { return m_impl
.find_first_not_of(str
.m_impl
, nStart
); }
2093 size_t find_first_not_of(const char* sz
, size_t nStart
= 0) const
2094 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
); }
2095 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
= 0) const
2096 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
); }
2097 size_t find_first_not_of(const char* sz
, size_t nStart
, size_t n
) const
2098 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
, n
); }
2099 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
2100 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
, n
); }
2101 size_t find_first_not_of(wxUniChar c
, size_t nStart
= 0) const
2102 { return m_impl
.find_first_not_of((wxChar
)c
, nStart
); }
2104 size_t find_last_not_of(const wxString
& str
, size_t nStart
= npos
) const
2105 { return m_impl
.find_last_not_of(str
.m_impl
, nStart
); }
2106 size_t find_last_not_of(const char* sz
, size_t nStart
= npos
) const
2107 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
); }
2108 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
= npos
) const
2109 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
); }
2110 size_t find_last_not_of(const char* sz
, size_t nStart
, size_t n
) const
2111 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
, n
); }
2112 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
2113 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
, n
); }
2114 size_t find_last_not_of(wxUniChar c
, size_t nStart
= npos
) const
2115 { return m_impl
.find_last_not_of((wxChar
)c
, nStart
); }
2117 // we can't use std::string implementation in UTF-8 build, because the
2118 // character sets would be interpreted wrongly:
2120 // as strpbrk() but starts at nStart, returns npos if not found
2121 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
2122 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2123 { return find_first_of(str
.mb_str().data(), nStart
); }
2125 { return find_first_of((const wxChar
*)str
.c_str(), nStart
); }
2128 size_t find_first_of(const char* sz
, size_t nStart
= 0) const;
2129 size_t find_first_of(const wchar_t* sz
, size_t nStart
= 0) const;
2130 size_t find_first_of(const char* sz
, size_t nStart
, size_t n
) const;
2131 size_t find_first_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2132 // same as find(char, size_t)
2133 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const
2134 { return find(c
, nStart
); }
2135 // find the last (starting from nStart) char from str in this string
2136 size_t find_last_of (const wxString
& str
, size_t nStart
= npos
) const
2137 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2138 { return find_last_of(str
.mb_str().data(), nStart
); }
2140 { return find_last_of((const wxChar
*)str
.c_str(), nStart
); }
2143 size_t find_last_of (const char* sz
, size_t nStart
= npos
) const;
2144 size_t find_last_of (const wchar_t* sz
, size_t nStart
= npos
) const;
2145 size_t find_last_of(const char* sz
, size_t nStart
, size_t n
) const;
2146 size_t find_last_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2148 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const
2149 { return rfind(c
, nStart
); }
2151 // find first/last occurence of any character not in the set
2153 // as strspn() (starting from nStart), returns npos on failure
2154 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
2155 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2156 { return find_first_not_of(str
.mb_str().data(), nStart
); }
2158 { return find_first_not_of((const wxChar
*)str
.c_str(), nStart
); }
2161 size_t find_first_not_of(const char* sz
, size_t nStart
= 0) const;
2162 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
= 0) const;
2163 size_t find_first_not_of(const char* sz
, size_t nStart
, size_t n
) const;
2164 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2166 size_t find_first_not_of(wxUniChar ch
, size_t nStart
= 0) const;
2168 size_t find_last_not_of(const wxString
& str
, size_t nStart
= npos
) const
2169 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2170 { return find_last_not_of(str
.mb_str().data(), nStart
); }
2172 { return find_last_not_of((const wxChar
*)str
.c_str(), nStart
); }
2175 size_t find_last_not_of(const char* sz
, size_t nStart
= npos
) const;
2176 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
= npos
) const;
2177 size_t find_last_not_of(const char* sz
, size_t nStart
, size_t n
) const;
2178 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2180 size_t find_last_not_of(wxUniChar ch
, size_t nStart
= npos
) const;
2181 #endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not
2183 // provide char/wchar_t/wxUniCharRef overloads for char-finding functions
2184 // above to resolve ambiguities:
2185 size_t find_first_of(wxUniCharRef ch
, size_t nStart
= 0) const
2186 { return find_first_of(wxUniChar(ch
), nStart
); }
2187 size_t find_first_of(char ch
, size_t nStart
= 0) const
2188 { return find_first_of(wxUniChar(ch
), nStart
); }
2189 size_t find_first_of(unsigned char ch
, size_t nStart
= 0) const
2190 { return find_first_of(wxUniChar(ch
), nStart
); }
2191 size_t find_first_of(wchar_t ch
, size_t nStart
= 0) const
2192 { return find_first_of(wxUniChar(ch
), nStart
); }
2193 size_t find_last_of(wxUniCharRef ch
, size_t nStart
= npos
) const
2194 { return find_last_of(wxUniChar(ch
), nStart
); }
2195 size_t find_last_of(char ch
, size_t nStart
= npos
) const
2196 { return find_last_of(wxUniChar(ch
), nStart
); }
2197 size_t find_last_of(unsigned char ch
, size_t nStart
= npos
) const
2198 { return find_last_of(wxUniChar(ch
), nStart
); }
2199 size_t find_last_of(wchar_t ch
, size_t nStart
= npos
) const
2200 { return find_last_of(wxUniChar(ch
), nStart
); }
2201 size_t find_first_not_of(wxUniCharRef ch
, size_t nStart
= 0) const
2202 { return find_first_not_of(wxUniChar(ch
), nStart
); }
2203 size_t find_first_not_of(char ch
, size_t nStart
= 0) const
2204 { return find_first_not_of(wxUniChar(ch
), nStart
); }
2205 size_t find_first_not_of(unsigned char ch
, size_t nStart
= 0) const
2206 { return find_first_not_of(wxUniChar(ch
), nStart
); }
2207 size_t find_first_not_of(wchar_t ch
, size_t nStart
= 0) const
2208 { return find_first_not_of(wxUniChar(ch
), nStart
); }
2209 size_t find_last_not_of(wxUniCharRef ch
, size_t nStart
= npos
) const
2210 { return find_last_not_of(wxUniChar(ch
), nStart
); }
2211 size_t find_last_not_of(char ch
, size_t nStart
= npos
) const
2212 { return find_last_not_of(wxUniChar(ch
), nStart
); }
2213 size_t find_last_not_of(unsigned char ch
, size_t nStart
= npos
) const
2214 { return find_last_not_of(wxUniChar(ch
), nStart
); }
2215 size_t find_last_not_of(wchar_t ch
, size_t nStart
= npos
) const
2216 { return find_last_not_of(wxUniChar(ch
), nStart
); }
2219 wxString
& operator+=(const wxString
& s
)
2220 { m_impl
+= s
.m_impl
; return *this; }
2221 // string += C string
2222 wxString
& operator+=(const char *psz
)
2223 { m_impl
+= ImplStr(psz
); return *this; }
2224 wxString
& operator+=(const wchar_t *pwz
)
2225 { m_impl
+= ImplStr(pwz
); return *this; }
2226 wxString
& operator+=(const wxCStrData
& s
)
2227 { m_impl
+= s
.AsString().m_impl
; return *this; }
2229 wxString
& operator+=(wxUniChar ch
)
2230 { m_impl
+= wxStringOperations::EncodeChar(ch
); return *this; }
2231 wxString
& operator+=(wxUniCharRef ch
) { return *this += wxUniChar(ch
); }
2232 wxString
& operator+=(int ch
) { return *this += wxUniChar(ch
); }
2233 wxString
& operator+=(char ch
) { return *this += wxUniChar(ch
); }
2234 wxString
& operator+=(unsigned char ch
) { return *this += wxUniChar(ch
); }
2235 wxString
& operator+=(wchar_t ch
) { return *this += wxUniChar(ch
); }
2238 #if !wxUSE_STL_BASED_WXSTRING
2239 // helpers for wxStringBuffer and wxStringBufferLength
2240 wxStringCharType
*DoGetWriteBuf(size_t nLen
)
2241 { return m_impl
.DoGetWriteBuf(nLen
); }
2242 void DoUngetWriteBuf()
2243 { m_impl
.DoUngetWriteBuf(); }
2244 void DoUngetWriteBuf(size_t nLen
)
2245 { m_impl
.DoUngetWriteBuf(nLen
); }
2246 #endif // !wxUSE_STL_BASED_WXSTRING
2248 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2249 int DoPrintf(const wxString
& format
, ...);
2250 static wxString
DoFormat(const wxString
& format
, ...);
2253 #if !wxUSE_STL_BASED_WXSTRING
2254 // check string's data validity
2255 bool IsValid() const { return m_impl
.GetStringData()->IsValid(); }
2259 wxStringImpl m_impl
;
2261 // buffers for compatibility conversion from (char*)c_str() and
2262 // (wchar_t*)c_str():
2263 // FIXME-UTF8: bechmark various approaches to keeping compatibility buffers
2264 template<typename T
>
2265 struct ConvertedBuffer
2267 ConvertedBuffer() : m_buf(NULL
) {}
2271 operator T
*() const { return m_buf
; }
2273 ConvertedBuffer
& operator=(T
*str
)
2283 ConvertedBuffer
<char> m_convertedToChar
;
2285 #if !wxUSE_UNICODE_WCHAR
2286 ConvertedBuffer
<wchar_t> m_convertedToWChar
;
2289 friend class WXDLLIMPEXP_BASE wxCStrData
;
2290 friend class wxImplStringBuffer
;
2291 friend class wxImplStringBufferLength
;
2294 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
2295 #pragma warning (default:4275)
2298 // string iterator operators that satisfy STL Random Access Iterator
2300 inline wxString::iterator
operator+(int n
, wxString::iterator i
)
2302 inline wxString::iterator
operator+(size_t n
, wxString::iterator i
)
2304 inline wxString::const_iterator
operator+(int n
, wxString::const_iterator i
)
2306 inline wxString::const_iterator
operator+(size_t n
, wxString::const_iterator i
)
2308 inline wxString::reverse_iterator
operator+(int n
, wxString::reverse_iterator i
)
2310 inline wxString::reverse_iterator
operator+(size_t n
, wxString::reverse_iterator i
)
2312 inline wxString::const_reverse_iterator
operator+(int n
, wxString::const_reverse_iterator i
)
2314 inline wxString::const_reverse_iterator
operator+(size_t n
, wxString::const_reverse_iterator i
)
2317 // notice that even though for many compilers the friend declarations above are
2318 // enough, from the point of view of C++ standard we must have the declarations
2319 // here as friend ones are not injected in the enclosing namespace and without
2320 // them the code fails to compile with conforming compilers such as xlC or g++4
2321 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
, const wxString
& string2
);
2322 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const char *psz
);
2323 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const wchar_t *pwz
);
2324 wxString WXDLLIMPEXP_BASE
operator+(const char *psz
, const wxString
& string
);
2325 wxString WXDLLIMPEXP_BASE
operator+(const wchar_t *pwz
, const wxString
& string
);
2327 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
2328 wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
2330 inline wxString
operator+(const wxString
& string
, wxUniCharRef ch
)
2331 { return string
+ (wxUniChar
)ch
; }
2332 inline wxString
operator+(const wxString
& string
, char ch
)
2333 { return string
+ wxUniChar(ch
); }
2334 inline wxString
operator+(const wxString
& string
, wchar_t ch
)
2335 { return string
+ wxUniChar(ch
); }
2336 inline wxString
operator+(wxUniCharRef ch
, const wxString
& string
)
2337 { return (wxUniChar
)ch
+ string
; }
2338 inline wxString
operator+(char ch
, const wxString
& string
)
2339 { return wxUniChar(ch
) + string
; }
2340 inline wxString
operator+(wchar_t ch
, const wxString
& string
)
2341 { return wxUniChar(ch
) + string
; }
2344 #if wxUSE_STL_BASED_WXSTRING
2345 // return an empty wxString (not very useful with wxUSE_STL == 1)
2346 inline const wxString
wxGetEmptyString() { return wxString(); }
2347 #else // !wxUSE_STL_BASED_WXSTRING
2348 // return an empty wxString (more efficient than wxString() here)
2349 inline const wxString
& wxGetEmptyString()
2351 return *(wxString
*)&wxEmptyString
;
2353 #endif // wxUSE_STL_BASED_WXSTRING/!wxUSE_STL_BASED_WXSTRING
2355 // ----------------------------------------------------------------------------
2356 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
2357 // ----------------------------------------------------------------------------
2359 #if !wxUSE_STL_BASED_WXSTRING
2360 // string buffer for direct access to string data in their native
2362 class wxImplStringBuffer
2365 typedef wxStringCharType CharType
;
2367 wxImplStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
2368 : m_str(str
), m_buf(NULL
)
2369 { m_buf
= m_str
.DoGetWriteBuf(lenWanted
); }
2371 ~wxImplStringBuffer() { m_str
.DoUngetWriteBuf(); }
2373 operator wxStringCharType
*() const { return m_buf
; }
2377 wxStringCharType
*m_buf
;
2379 DECLARE_NO_COPY_CLASS(wxImplStringBuffer
)
2382 class wxImplStringBufferLength
2385 typedef wxStringCharType CharType
;
2387 wxImplStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
2388 : m_str(str
), m_buf(NULL
), m_len(0), m_lenSet(false)
2390 m_buf
= m_str
.DoGetWriteBuf(lenWanted
);
2391 wxASSERT(m_buf
!= NULL
);
2394 ~wxImplStringBufferLength()
2397 m_str
.DoUngetWriteBuf(m_len
);
2400 operator wxStringCharType
*() const { return m_buf
; }
2401 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
2405 wxStringCharType
*m_buf
;
2409 DECLARE_NO_COPY_CLASS(wxImplStringBufferLength
)
2412 #endif // !wxUSE_STL_BASED_WXSTRING
2414 template<typename T
>
2415 class wxStringTypeBufferBase
2420 wxStringTypeBufferBase(wxString
& str
, size_t lenWanted
= 1024)
2421 : m_str(str
), m_buf(lenWanted
)
2425 operator CharType
*() { return m_buf
.data(); }
2429 wxCharTypeBuffer
<CharType
> m_buf
;
2432 template<typename T
>
2433 class wxStringTypeBufferLengthBase
2438 wxStringTypeBufferLengthBase(wxString
& str
, size_t lenWanted
= 1024)
2439 : m_str(str
), m_buf(lenWanted
), m_len(0), m_lenSet(false)
2442 ~wxStringTypeBufferLengthBase()
2445 m_str
.assign(m_buf
.data(), m_len
);
2448 operator CharType
*() { return m_buf
.data(); }
2449 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
2453 wxCharTypeBuffer
<CharType
> m_buf
;
2458 template<typename T
>
2459 class wxStringTypeBuffer
: public wxStringTypeBufferBase
<T
>
2462 wxStringTypeBuffer(wxString
& str
, size_t lenWanted
= 1024)
2463 : wxStringTypeBufferBase
<T
>(str
, lenWanted
) {}
2464 ~wxStringTypeBuffer()
2466 this->m_str
.assign(this->m_buf
.data());
2469 DECLARE_NO_COPY_CLASS(wxStringTypeBuffer
)
2472 template<typename T
>
2473 class wxStringTypeBufferLength
: public wxStringTypeBufferLengthBase
<T
>
2476 wxStringTypeBufferLength(wxString
& str
, size_t lenWanted
= 1024)
2477 : wxStringTypeBufferLengthBase
<T
>(str
, lenWanted
) {}
2479 ~wxStringTypeBufferLength()
2481 wxASSERT(this->m_lenSet
);
2482 this->m_str
.assign(this->m_buf
.data(), this->m_len
);
2485 DECLARE_NO_COPY_CLASS(wxStringTypeBufferLength
)
2488 #if wxUSE_STL_BASED_WXSTRING
2489 class wxImplStringBuffer
: public wxStringTypeBufferBase
<wxStringCharType
>
2492 wxImplStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
2493 : wxStringTypeBufferBase
<wxStringCharType
>(str
, lenWanted
) {}
2494 ~wxImplStringBuffer()
2495 { m_str
.m_impl
.assign(m_buf
.data()); }
2497 DECLARE_NO_COPY_CLASS(wxImplStringBuffer
)
2500 class wxImplStringBufferLength
: public wxStringTypeBufferLengthBase
<wxStringCharType
>
2503 wxImplStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
2504 : wxStringTypeBufferLengthBase
<wxStringCharType
>(str
, lenWanted
) {}
2506 ~wxImplStringBufferLength()
2509 m_str
.m_impl
.assign(m_buf
.data(), m_len
);
2512 DECLARE_NO_COPY_CLASS(wxImplStringBufferLength
)
2514 #endif // wxUSE_STL_BASED_WXSTRING
2517 #if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
2518 typedef wxStringTypeBuffer
<wxChar
> wxStringBuffer
;
2519 typedef wxStringTypeBufferLength
<wxChar
> wxStringBufferLength
;
2520 #else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2521 typedef wxImplStringBuffer wxStringBuffer
;
2522 typedef wxImplStringBufferLength wxStringBufferLength
;
2523 #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2525 // ---------------------------------------------------------------------------
2526 // wxString comparison functions: operator versions are always case sensitive
2527 // ---------------------------------------------------------------------------
2529 #define wxCMP_WXCHAR_STRING(p, s, op) 0 op s.Cmp(p)
2531 wxDEFINE_ALL_COMPARISONS(const wxChar
*, const wxString
&, wxCMP_WXCHAR_STRING
)
2533 #undef wxCMP_WXCHAR_STRING
2535 // note that there is an optimization in operator==() and !=(): we (quickly)
2536 // checks the strings length first, before comparing their data
2537 inline bool operator==(const wxString
& s1
, const wxString
& s2
)
2538 { return (s1
.Len() == s2
.Len()) && (s1
.Cmp(s2
) == 0); }
2539 inline bool operator!=(const wxString
& s1
, const wxString
& s2
)
2540 { return (s1
.Len() != s2
.Len()) || (s1
.Cmp(s2
) != 0); }
2541 inline bool operator< (const wxString
& s1
, const wxString
& s2
)
2542 { return s1
.Cmp(s2
) < 0; }
2543 inline bool operator> (const wxString
& s1
, const wxString
& s2
)
2544 { return s1
.Cmp(s2
) > 0; }
2545 inline bool operator<=(const wxString
& s1
, const wxString
& s2
)
2546 { return s1
.Cmp(s2
) <= 0; }
2547 inline bool operator>=(const wxString
& s1
, const wxString
& s2
)
2548 { return s1
.Cmp(s2
) >= 0; }
2551 inline bool operator==(const wxString
& s1
, const wxWCharBuffer
& s2
)
2552 { return (s1
.Cmp((const wchar_t *)s2
) == 0); }
2553 inline bool operator==(const wxWCharBuffer
& s1
, const wxString
& s2
)
2554 { return (s2
.Cmp((const wchar_t *)s1
) == 0); }
2555 inline bool operator!=(const wxString
& s1
, const wxWCharBuffer
& s2
)
2556 { return (s1
.Cmp((const wchar_t *)s2
) != 0); }
2557 inline bool operator!=(const wxWCharBuffer
& s1
, const wxString
& s2
)
2558 { return (s2
.Cmp((const wchar_t *)s1
) != 0); }
2559 #else // !wxUSE_UNICODE
2560 inline bool operator==(const wxString
& s1
, const wxCharBuffer
& s2
)
2561 { return (s1
.Cmp((const char *)s2
) == 0); }
2562 inline bool operator==(const wxCharBuffer
& s1
, const wxString
& s2
)
2563 { return (s2
.Cmp((const char *)s1
) == 0); }
2564 inline bool operator!=(const wxString
& s1
, const wxCharBuffer
& s2
)
2565 { return (s1
.Cmp((const char *)s2
) != 0); }
2566 inline bool operator!=(const wxCharBuffer
& s1
, const wxString
& s2
)
2567 { return (s2
.Cmp((const char *)s1
) != 0); }
2568 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
2571 inline wxString
operator+(const wxString
& string
, const wxWCharBuffer
& buf
)
2572 { return string
+ (const wchar_t *)buf
; }
2573 inline wxString
operator+(const wxWCharBuffer
& buf
, const wxString
& string
)
2574 { return (const wchar_t *)buf
+ string
; }
2575 #else // !wxUSE_UNICODE
2576 inline wxString
operator+(const wxString
& string
, const wxCharBuffer
& buf
)
2577 { return string
+ (const char *)buf
; }
2578 inline wxString
operator+(const wxCharBuffer
& buf
, const wxString
& string
)
2579 { return (const char *)buf
+ string
; }
2580 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
2582 // comparison with char
2583 inline bool operator==(const wxUniChar
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2584 inline bool operator==(const wxUniCharRef
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2585 inline bool operator==(char c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2586 inline bool operator==(wchar_t c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2587 inline bool operator==(int c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2588 inline bool operator==(const wxString
& s
, const wxUniChar
& c
) { return s
.IsSameAs(c
); }
2589 inline bool operator==(const wxString
& s
, const wxUniCharRef
& c
) { return s
.IsSameAs(c
); }
2590 inline bool operator==(const wxString
& s
, char c
) { return s
.IsSameAs(c
); }
2591 inline bool operator==(const wxString
& s
, wchar_t c
) { return s
.IsSameAs(c
); }
2592 inline bool operator!=(const wxUniChar
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2593 inline bool operator!=(const wxUniCharRef
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2594 inline bool operator!=(char c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2595 inline bool operator!=(wchar_t c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2596 inline bool operator!=(int c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2597 inline bool operator!=(const wxString
& s
, const wxUniChar
& c
) { return !s
.IsSameAs(c
); }
2598 inline bool operator!=(const wxString
& s
, const wxUniCharRef
& c
) { return !s
.IsSameAs(c
); }
2599 inline bool operator!=(const wxString
& s
, char c
) { return !s
.IsSameAs(c
); }
2600 inline bool operator!=(const wxString
& s
, wchar_t c
) { return !s
.IsSameAs(c
); }
2602 // comparison with C string in Unicode build
2605 #define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s
2607 wxDEFINE_ALL_COMPARISONS(const char *, const wxString
&, wxCMP_CHAR_STRING
)
2609 #undef wxCMP_CHAR_STRING
2611 #endif // wxUSE_UNICODE
2613 // we also need to provide the operators for comparison with wxCStrData to
2614 // resolve ambiguity between operator(const wxChar *,const wxString &) and
2615 // operator(const wxChar *, const wxChar *) for "p == s.c_str()"
2617 // notice that these are (shallow) pointer comparisons, not (deep) string ones
2618 #define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar()
2619 #define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar()
2621 wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData
&, wxCMP_WCHAR_CSTRDATA
)
2622 wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData
&, wxCMP_CHAR_CSTRDATA
)
2624 #undef wxCMP_CHAR_CSTRDATA
2625 #undef wxCMP_WCHAR_CSTRDATA
2627 // ---------------------------------------------------------------------------
2628 // Implementation only from here until the end of file
2629 // ---------------------------------------------------------------------------
2631 #if wxUSE_STD_IOSTREAM
2633 #include "wx/iosfwrap.h"
2635 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxString
&);
2636 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxCStrData
&);
2637 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxCharBuffer
&);
2638 #ifndef __BORLANDC__
2639 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxWCharBuffer
&);
2642 #endif // wxSTD_STRING_COMPATIBILITY
2644 // ---------------------------------------------------------------------------
2645 // wxCStrData implementation
2646 // ---------------------------------------------------------------------------
2648 inline wxCStrData::wxCStrData(char *buf
)
2649 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
2650 inline wxCStrData::wxCStrData(wchar_t *buf
)
2651 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
2653 inline wxCStrData::wxCStrData(const wxCStrData
& data
)
2654 : m_str(data
.m_owned
? new wxString(*data
.m_str
) : data
.m_str
),
2655 m_offset(data
.m_offset
),
2656 m_owned(data
.m_owned
)
2660 inline wxCStrData::~wxCStrData()
2666 // simple cases for AsChar() and AsWChar(), the complicated ones are
2668 #if wxUSE_UNICODE_WCHAR
2669 inline const wchar_t* wxCStrData::AsWChar() const
2671 return m_str
->wx_str() + m_offset
;
2673 #endif // wxUSE_UNICODE_WCHAR
2676 inline const char* wxCStrData::AsChar() const
2678 return m_str
->wx_str() + m_offset
;
2680 #endif // !wxUSE_UNICODE
2682 inline const wxCharBuffer
wxCStrData::AsCharBuf() const
2685 return wxCharBuffer::CreateNonOwned(AsChar());
2687 return AsString().mb_str();
2691 inline const wxWCharBuffer
wxCStrData::AsWCharBuf() const
2693 #if wxUSE_UNICODE_WCHAR
2694 return wxWCharBuffer::CreateNonOwned(AsWChar());
2696 return AsString().wc_str();
2700 inline wxString
wxCStrData::AsString() const
2702 if ( m_offset
== 0 )
2705 return m_str
->Mid(m_offset
);
2708 inline const wxStringCharType
*wxCStrData::AsInternal() const
2710 return wxStringOperations::AddToIter(m_str
->wx_str(), m_offset
);
2713 inline wxUniChar
wxCStrData::operator*() const
2715 if ( m_str
->empty() )
2716 return wxUniChar(_T('\0'));
2718 return (*m_str
)[m_offset
];
2721 inline wxUniChar
wxCStrData::operator[](size_t n
) const
2723 // NB: we intentionally use operator[] and not at() here because the former
2724 // works for the terminating NUL while the latter does not
2725 return (*m_str
)[m_offset
+ n
];
2728 // ----------------------------------------------------------------------------
2729 // more wxCStrData operators
2730 // ----------------------------------------------------------------------------
2732 // we need to define those to allow "size_t pos = p - s.c_str()" where p is
2733 // some pointer into the string
2734 inline size_t operator-(const char *p
, const wxCStrData
& cs
)
2736 return p
- cs
.AsChar();
2739 inline size_t operator-(const wchar_t *p
, const wxCStrData
& cs
)
2741 return p
- cs
.AsWChar();
2744 // ----------------------------------------------------------------------------
2745 // implementation of wx[W]CharBuffer inline methods using wxCStrData
2746 // ----------------------------------------------------------------------------
2748 // FIXME-UTF8: move this to buffer.h
2749 inline wxCharBuffer::wxCharBuffer(const wxCStrData
& cstr
)
2750 : wxCharTypeBufferBase(cstr
.AsCharBuf())
2754 inline wxWCharBuffer::wxWCharBuffer(const wxCStrData
& cstr
)
2755 : wxCharTypeBufferBase(cstr
.AsWCharBuf())
2759 #if WXWIN_COMPATIBILITY_2_8
2760 // lot of code out there doesn't explicitly include wx/wxchar.h, but uses
2761 // CRT wrappers that are now declared in wx/wxcrt.h and wx/wxcrtvararg.h,
2762 // so let's include this header now that wxString is defined and it's safe
2764 #include "wx/wxchar.h"
2767 #endif // _WX_WXSTRING_H_