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 wxCStrData(char *buf
);
190 wxCStrData(wchar_t *buf
);
192 inline ~wxCStrData();
194 // methods defined inline below must be declared inline or mingw32 3.4.5
195 // warns about "<symbol> defined locally after being referenced with
196 // dllimport linkage"
197 #if wxUSE_UNICODE_WCHAR
200 const wchar_t* AsWChar() const;
201 operator const wchar_t*() const { return AsWChar(); }
206 const char* AsChar() const;
207 const unsigned char* AsUnsignedChar() const
208 { return (const unsigned char *) AsChar(); }
209 operator const char*() const { return AsChar(); }
210 operator const unsigned char*() const { return AsUnsignedChar(); }
212 operator const void*() const { return AsChar(); }
214 inline const wxCharBuffer
AsCharBuf() const;
215 inline const wxWCharBuffer
AsWCharBuf() const;
217 inline wxString
AsString() const;
219 // returns the value as C string in internal representation (equivalent
220 // to AsString().wx_str(), but more efficient)
221 const wxStringCharType
*AsInternal() const;
223 // allow expressions like "c_str()[0]":
224 inline wxUniChar
operator[](size_t n
) const;
225 wxUniChar
operator[](int n
) const { return operator[](size_t(n
)); }
226 wxUniChar
operator[](long n
) const { return operator[](size_t(n
)); }
227 #ifndef wxSIZE_T_IS_UINT
228 wxUniChar
operator[](unsigned int n
) const { return operator[](size_t(n
)); }
229 #endif // size_t != unsigned int
231 // these operators are needed to emulate the pointer semantics of c_str():
232 // expressions like "wxChar *p = str.c_str() + 1;" should continue to work
233 // (we need both versions to resolve ambiguities):
234 wxCStrData
operator+(int n
) const
235 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
236 wxCStrData
operator+(long n
) const
237 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
238 wxCStrData
operator+(size_t n
) const
239 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
241 // and these for "str.c_str() + n - 2":
242 wxCStrData
operator-(int n
) const
244 wxASSERT_MSG( n
<= (int)m_offset
,
245 _T("attempt to construct address before the beginning of the string") );
246 return wxCStrData(m_str
, m_offset
- n
, m_owned
);
248 wxCStrData
operator-(long n
) const
250 wxASSERT_MSG( n
<= (int)m_offset
,
251 _T("attempt to construct address before the beginning of the string") );
252 return wxCStrData(m_str
, m_offset
- n
, m_owned
);
254 wxCStrData
operator-(size_t n
) const
256 wxASSERT_MSG( n
<= m_offset
,
257 _T("attempt to construct address before the beginning of the string") );
258 return wxCStrData(m_str
, m_offset
- n
, m_owned
);
261 // this operator is needed to make expressions like "*c_str()" or
262 // "*(c_str() + 2)" work
263 inline wxUniChar
operator*() const;
266 const wxString
*m_str
;
270 friend class WXDLLIMPEXP_BASE wxString
;
273 // ----------------------------------------------------------------------------
274 // wxStringPrintfMixin
275 // ---------------------------------------------------------------------------
277 // NB: VC6 has a bug that causes linker errors if you have template methods
278 // in a class using __declspec(dllimport). The solution is to split such
279 // class into two classes, one that contains the template methods and does
280 // *not* use WXDLLIMPEXP_BASE and another class that contains the rest
281 // (with DLL linkage).
283 // We only do this for VC6 here, because the code is less efficient
284 // (Printf() has to use dynamic_cast<>) and because OpenWatcom compiler
285 // cannot compile this code.
287 #if defined(__VISUALC__) && __VISUALC__ < 1300
288 #define wxNEEDS_WXSTRING_PRINTF_MIXIN
291 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
292 // this class contains implementation of wxString's vararg methods, it's
293 // exported from wxBase DLL
294 class WXDLLIMPEXP_BASE wxStringPrintfMixinBase
297 wxStringPrintfMixinBase() {}
299 int DoPrintf(const wxString
& format
, ...);
300 static wxString
DoFormat(const wxString
& format
, ...);
303 // this class contains template wrappers for wxString's vararg methods, it's
304 // intentionally *not* exported from the DLL in order to fix the VC6 bug
306 class wxStringPrintfMixin
: public wxStringPrintfMixinBase
309 // to further complicate things, we can't return wxString from
310 // wxStringPrintfMixin::Format() because wxString is not yet declared at
311 // this point; the solution is to use this fake type trait template - this
312 // way the compiler won't know the return type until Format() is used
313 // (this doesn't compile with Watcom, but VC6 compiles it just fine):
314 template<typename T
> struct StringReturnType
316 typedef wxString type
;
320 // these are duplicated wxString methods, they're also declared below
321 // if !wxNEEDS_WXSTRING_PRINTF_MIXIN:
323 // static wxString Format(const wString& format, ...) ATTRIBUTE_PRINTF_1;
324 WX_DEFINE_VARARG_FUNC2_SANS_N0(static typename StringReturnType
<T1
>::type
,
325 Format
, 1, (const wxString
&),
327 // We have to implement the version without template arguments manually
328 // because of the StringReturnType<> hack, although WX_DEFINE_VARARG_FUNC
329 // normally does it itself. It has to be a template so that we can use
330 // the hack, even though there's no real template parameter:
331 struct FormatDummyArg
{} ;
334 inline static typename StringReturnType
<T
>::type
335 Format(const wxString
& fmt
, FormatDummyArg dummy
= FormatDummyArg())
337 return DoFormat(fmt
);
340 // int Printf(const wxString& format, ...);
341 WX_DEFINE_VARARG_FUNC(int, Printf
, 1, (const wxString
&), DoPrintf
)
342 // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2;
343 WX_DEFINE_VARARG_FUNC(int, sprintf
, 1, (const wxString
&), DoPrintf
)
346 wxStringPrintfMixin() : wxStringPrintfMixinBase() {}
348 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
351 // ----------------------------------------------------------------------------
352 // wxString: string class trying to be compatible with std::string, MFC
353 // CString and wxWindows 1.x wxString all at once
354 // ---------------------------------------------------------------------------
356 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
357 // "non dll-interface class 'wxStringPrintfMixin' used as base interface
358 // for dll-interface class 'wxString'" -- this is OK in our case
359 #pragma warning (disable:4275)
362 class WXDLLIMPEXP_BASE wxString
363 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
364 : public wxStringPrintfMixin
367 // NB: special care was taken in arranging the member functions in such order
368 // that all inline functions can be effectively inlined, verify that all
369 // performance critical functions are still inlined if you change order!
371 // an 'invalid' value for string index, moved to this place due to a CW bug
372 static const size_t npos
;
375 // if we hadn't made these operators private, it would be possible to
376 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
377 // converted to char in C and we do have operator=(char)
379 // NB: we don't need other versions (short/long and unsigned) as attempt
380 // to assign another numeric type to wxString will now result in
381 // ambiguity between operator=(char) and operator=(int)
382 wxString
& operator=(int);
384 // these methods are not implemented - there is _no_ conversion from int to
385 // string, you're doing something wrong if the compiler wants to call it!
387 // try `s << i' or `s.Printf("%d", i)' instead
391 // buffer for holding temporary substring when using any of the methods
392 // that take (char*,size_t) or (wchar_t*,size_t) arguments:
394 struct SubstrBufFromType
399 SubstrBufFromType(const T
& data_
, size_t len_
)
400 : data(data_
), len(len_
) {}
403 #if wxUSE_UNICODE_UTF8
404 // even char* -> char* needs conversion, from locale charset to UTF-8
405 typedef SubstrBufFromType
<wxCharBuffer
> SubstrBufFromWC
;
406 typedef SubstrBufFromType
<wxCharBuffer
> SubstrBufFromMB
;
407 #elif wxUSE_UNICODE_WCHAR
408 typedef SubstrBufFromType
<const wchar_t*> SubstrBufFromWC
;
409 typedef SubstrBufFromType
<wxWCharBuffer
> SubstrBufFromMB
;
411 typedef SubstrBufFromType
<const char*> SubstrBufFromMB
;
412 typedef SubstrBufFromType
<wxCharBuffer
> SubstrBufFromWC
;
416 // Functions implementing primitive operations on string data; wxString
417 // methods and iterators are implemented in terms of it. The differences
418 // between UTF-8 and wchar_t* representations of the string are mostly
421 #if wxUSE_UNICODE_UTF8
422 static SubstrBufFromMB
ConvertStr(const char *psz
, size_t nLength
,
423 const wxMBConv
& conv
);
424 static SubstrBufFromWC
ConvertStr(const wchar_t *pwz
, size_t nLength
,
425 const wxMBConv
& conv
);
426 #elif wxUSE_UNICODE_WCHAR
427 static SubstrBufFromMB
ConvertStr(const char *psz
, size_t nLength
,
428 const wxMBConv
& conv
);
430 static SubstrBufFromWC
ConvertStr(const wchar_t *pwz
, size_t nLength
,
431 const wxMBConv
& conv
);
434 #if !wxUSE_UNICODE_UTF8 // wxUSE_UNICODE_WCHAR or !wxUSE_UNICODE
435 // returns C string encoded as the implementation expects:
437 static const wchar_t* ImplStr(const wchar_t* str
)
438 { return str
? str
: wxT(""); }
439 static const SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
440 { return SubstrBufFromWC(str
, (str
&& n
== npos
) ? wxWcslen(str
) : n
); }
441 static wxWCharBuffer
ImplStr(const char* str
,
442 const wxMBConv
& conv
= wxConvLibc
)
443 { return ConvertStr(str
, npos
, conv
).data
; }
444 static SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
445 const wxMBConv
& conv
= wxConvLibc
)
446 { return ConvertStr(str
, n
, conv
); }
448 static const char* ImplStr(const char* str
,
449 const wxMBConv
& WXUNUSED(conv
) = wxConvLibc
)
450 { return str
? str
: ""; }
451 static const SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
452 const wxMBConv
& WXUNUSED(conv
) = wxConvLibc
)
453 { return SubstrBufFromMB(str
, (str
&& n
== npos
) ? wxStrlen(str
) : n
); }
454 static wxCharBuffer
ImplStr(const wchar_t* str
)
455 { return ConvertStr(str
, npos
, wxConvLibc
).data
; }
456 static SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
457 { return ConvertStr(str
, n
, wxConvLibc
); }
460 // translates position index in wxString to/from index in underlying
462 static size_t PosToImpl(size_t pos
) { return pos
; }
463 static void PosLenToImpl(size_t pos
, size_t len
,
464 size_t *implPos
, size_t *implLen
)
465 { *implPos
= pos
; *implLen
= len
; }
466 static size_t LenToImpl(size_t len
) { return len
; }
467 static size_t PosFromImpl(size_t pos
) { return pos
; }
469 #else // wxUSE_UNICODE_UTF8
471 // FIXME-UTF8: return as-is without copying under UTF8 locale, return
472 // converted string under other locales - needs wxCharBuffer
474 static wxCharBuffer
ImplStr(const char* str
,
475 const wxMBConv
& conv
= wxConvLibc
)
476 { return ConvertStr(str
, npos
, conv
).data
; }
477 static SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
478 const wxMBConv
& conv
= wxConvLibc
)
479 { return ConvertStr(str
, n
, conv
); }
481 static wxCharBuffer
ImplStr(const wchar_t* str
)
482 { return ConvertStr(str
, npos
, wxConvUTF8
).data
; }
483 static SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
484 { return ConvertStr(str
, n
, wxConvUTF8
); }
486 size_t PosToImpl(size_t pos
) const
488 if ( pos
== 0 || pos
== npos
)
491 return (begin() + pos
).impl() - m_impl
.begin();
494 void PosLenToImpl(size_t pos
, size_t len
, size_t *implPos
, size_t *implLen
) const;
496 size_t LenToImpl(size_t len
) const
499 PosLenToImpl(0, len
, &pos
, &len2
);
503 size_t PosFromImpl(size_t pos
) const
505 if ( pos
== 0 || pos
== npos
)
508 return const_iterator(m_impl
.begin() + pos
) - begin();
510 #endif // !wxUSE_UNICODE_UTF8/wxUSE_UNICODE_UTF8
514 typedef wxUniChar value_type
;
515 typedef wxUniChar char_type
;
516 typedef wxUniCharRef reference
;
517 typedef wxChar
* pointer
;
518 typedef const wxChar
* const_pointer
;
520 typedef size_t size_type
;
521 typedef wxUniChar const_reference
;
524 #if wxUSE_UNICODE_UTF8
525 // random access is not O(1), as required by Random Access Iterator
526 #define WX_STR_ITERATOR_TAG std::bidirectional_iterator_tag
528 #define WX_STR_ITERATOR_TAG std::random_access_iterator_tag
531 #define WX_STR_ITERATOR_TAG void /* dummy type */
534 #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, \
535 reference_type, reference_ctor) \
537 typedef wxStringImpl::iterator_name underlying_iterator; \
539 typedef WX_STR_ITERATOR_TAG iterator_category; \
540 typedef wxUniChar value_type; \
541 typedef int difference_type; \
542 typedef reference_type reference; \
543 typedef pointer_type pointer; \
545 reference operator*() const { return reference_ctor; } \
546 reference operator[](size_t n) const { return *(*this + n); } \
548 iterator_name& operator++() \
549 { wxStringOperations::IncIter(m_cur); return *this; } \
550 iterator_name& operator--() \
551 { wxStringOperations::DecIter(m_cur); return *this; } \
552 iterator_name operator++(int) \
554 iterator_name tmp = *this; \
555 wxStringOperations::IncIter(m_cur); \
558 iterator_name operator--(int) \
560 iterator_name tmp = *this; \
561 wxStringOperations::DecIter(m_cur); \
565 iterator_name& operator+=(int n) \
567 m_cur = wxStringOperations::AddToIter(m_cur, n); \
570 iterator_name& operator+=(size_t n) \
572 m_cur = wxStringOperations::AddToIter(m_cur, (int)n); \
575 iterator_name& operator-=(int n) \
577 m_cur = wxStringOperations::AddToIter(m_cur, -n); \
580 iterator_name& operator-=(size_t n) \
582 m_cur = wxStringOperations::AddToIter(m_cur, -(int)n); \
586 difference_type operator-(const iterator_name& i) const \
587 { return wxStringOperations::DiffIters(m_cur, i.m_cur); } \
589 bool operator==(const iterator_name& i) const \
590 { return m_cur == i.m_cur; } \
591 bool operator!=(const iterator_name& i) const \
592 { return m_cur != i.m_cur; } \
594 bool operator<(const iterator_name& i) const \
595 { return m_cur < i.m_cur; } \
596 bool operator>(const iterator_name& i) const \
597 { return m_cur > i.m_cur; } \
598 bool operator<=(const iterator_name& i) const \
599 { return m_cur <= i.m_cur; } \
600 bool operator>=(const iterator_name& i) const \
601 { return m_cur >= i.m_cur; } \
604 /* for internal wxString use only: */ \
605 underlying_iterator impl() const { return m_cur; } \
607 friend class WXDLLIMPEXP_BASE wxString; \
608 friend class WXDLLIMPEXP_BASE wxCStrData; \
611 underlying_iterator m_cur
613 class const_iterator
;
615 #if wxUSE_UNICODE_UTF8
618 // NB: In UTF-8 build, (non-const) iterator needs to keep reference
619 // to the underlying wxStringImpl, because UTF-8 is variable-length
620 // encoding and changing the value pointer to by an iterator using
621 // its operator* requires calling wxStringImpl::replace() if the old
622 // and new values differ in their encoding's length.
624 WX_STR_ITERATOR_IMPL(iterator
, wxChar
*, wxUniCharRef
,
625 wxUniCharRef::CreateForString(m_str
, m_cur
));
628 iterator(const iterator
& i
) : m_cur(i
.m_cur
), m_str(i
.m_str
) {}
630 iterator
operator+(int n
) const
631 { return iterator(m_str
, wxStringOperations::AddToIter(m_cur
, n
)); }
632 iterator
operator+(size_t n
) const
633 { return iterator(m_str
, wxStringOperations::AddToIter(m_cur
, (int)n
)); }
634 iterator
operator-(int n
) const
635 { return iterator(m_str
, wxStringOperations::AddToIter(m_cur
, -n
)); }
636 iterator
operator-(size_t n
) const
637 { return iterator(m_str
, wxStringOperations::AddToIter(m_cur
, -(int)n
)); }
640 iterator(wxString
*str
, underlying_iterator ptr
)
641 : m_cur(ptr
), m_str(str
->m_impl
) {}
642 iterator(wxStringImpl
& str
, underlying_iterator ptr
)
643 : m_cur(ptr
), m_str(str
) {}
647 friend class const_iterator
;
650 size_t IterToImplPos(wxString::iterator i
) const
651 { return wxStringImpl::const_iterator(i
.impl()) - m_impl
.begin(); }
653 #else // !wxUSE_UNICODE_UTF8
657 WX_STR_ITERATOR_IMPL(iterator
, wxChar
*, wxUniCharRef
,
658 wxUniCharRef::CreateForString(m_cur
));
661 iterator(const iterator
& i
) : m_cur(i
.m_cur
) {}
663 iterator
operator+(int n
) const
664 { return iterator(wxStringOperations::AddToIter(m_cur
, n
)); }
665 iterator
operator+(size_t n
) const
666 { return iterator(wxStringOperations::AddToIter(m_cur
, (int)n
)); }
667 iterator
operator-(int n
) const
668 { return iterator(wxStringOperations::AddToIter(m_cur
, -n
)); }
669 iterator
operator-(size_t n
) const
670 { return iterator(wxStringOperations::AddToIter(m_cur
, -(int)n
)); }
673 // for internal wxString use only:
674 iterator(underlying_iterator ptr
) : m_cur(ptr
) {}
675 iterator(wxString
*WXUNUSED(str
), underlying_iterator ptr
) : m_cur(ptr
) {}
677 friend class const_iterator
;
679 #endif // wxUSE_UNICODE_UTF8/!wxUSE_UNICODE_UTF8
683 // NB: reference_type is intentionally value, not reference, the character
684 // may be encoded differently in wxString data:
685 WX_STR_ITERATOR_IMPL(const_iterator
, const wxChar
*, wxUniChar
,
686 wxStringOperations::DecodeChar(m_cur
));
689 const_iterator(const const_iterator
& i
) : m_cur(i
.m_cur
) {}
690 const_iterator(const iterator
& i
) : m_cur(i
.m_cur
) {}
692 const_iterator
operator+(int n
) const
693 { return const_iterator(wxStringOperations::AddToIter(m_cur
, n
)); }
694 const_iterator
operator+(size_t n
) const
695 { return const_iterator(wxStringOperations::AddToIter(m_cur
, (int)n
)); }
696 const_iterator
operator-(int n
) const
697 { return const_iterator(wxStringOperations::AddToIter(m_cur
, -n
)); }
698 const_iterator
operator-(size_t n
) const
699 { return const_iterator(wxStringOperations::AddToIter(m_cur
, -(int)n
)); }
702 // for internal wxString use only:
703 const_iterator(underlying_iterator ptr
) : m_cur(ptr
) {}
706 #undef WX_STR_ITERATOR_TAG
707 #undef WX_STR_ITERATOR_IMPL
709 friend class iterator
;
710 friend class const_iterator
;
712 template <typename T
>
713 class reverse_iterator_impl
716 typedef T iterator_type
;
718 typedef typename
T::iterator_category iterator_category
;
719 typedef typename
T::value_type value_type
;
720 typedef typename
T::difference_type difference_type
;
721 typedef typename
T::reference reference
;
722 typedef typename
T::pointer
*pointer
;
724 reverse_iterator_impl(iterator_type i
) : m_cur(i
) {}
725 reverse_iterator_impl(const reverse_iterator_impl
& ri
)
728 iterator_type
base() const { return m_cur
; }
730 reference
operator*() const { return *(m_cur
-1); }
731 reference
operator[](size_t n
) const { return *(*this + n
); }
733 reverse_iterator_impl
& operator++()
734 { --m_cur
; return *this; }
735 reverse_iterator_impl
operator++(int)
736 { reverse_iterator_impl tmp
= *this; --m_cur
; return tmp
; }
737 reverse_iterator_impl
& operator--()
738 { ++m_cur
; return *this; }
739 reverse_iterator_impl
operator--(int)
740 { reverse_iterator_impl tmp
= *this; ++m_cur
; return tmp
; }
742 // NB: explicit <T> in the functions below is to keep BCC 5.5 happy
743 reverse_iterator_impl
operator+(int n
) const
744 { return reverse_iterator_impl
<T
>(m_cur
- n
); }
745 reverse_iterator_impl
operator+(size_t n
) const
746 { return reverse_iterator_impl
<T
>(m_cur
- n
); }
747 reverse_iterator_impl
operator-(int n
) const
748 { return reverse_iterator_impl
<T
>(m_cur
+ n
); }
749 reverse_iterator_impl
operator-(size_t n
) const
750 { return reverse_iterator_impl
<T
>(m_cur
+ n
); }
751 reverse_iterator_impl
operator+=(int n
)
752 { m_cur
-= n
; return *this; }
753 reverse_iterator_impl
operator+=(size_t n
)
754 { m_cur
-= n
; return *this; }
755 reverse_iterator_impl
operator-=(int n
)
756 { m_cur
+= n
; return *this; }
757 reverse_iterator_impl
operator-=(size_t n
)
758 { m_cur
+= n
; return *this; }
760 unsigned operator-(const reverse_iterator_impl
& i
) const
761 { return i
.m_cur
- m_cur
; }
763 bool operator==(const reverse_iterator_impl
& ri
) const
764 { return m_cur
== ri
.m_cur
; }
765 bool operator!=(const reverse_iterator_impl
& ri
) const
766 { return !(*this == ri
); }
768 bool operator<(const reverse_iterator_impl
& i
) const
769 { return m_cur
> i
.m_cur
; }
770 bool operator>(const reverse_iterator_impl
& i
) const
771 { return m_cur
< i
.m_cur
; }
772 bool operator<=(const reverse_iterator_impl
& i
) const
773 { return m_cur
>= i
.m_cur
; }
774 bool operator>=(const reverse_iterator_impl
& i
) const
775 { return m_cur
<= i
.m_cur
; }
781 typedef reverse_iterator_impl
<iterator
> reverse_iterator
;
782 typedef reverse_iterator_impl
<const_iterator
> const_reverse_iterator
;
785 // used to transform an expression built using c_str() (and hence of type
786 // wxCStrData) to an iterator into the string
787 static const_iterator
CreateConstIterator(const wxCStrData
& data
)
789 return const_iterator(data
.m_str
->begin() + data
.m_offset
);
792 // in UTF-8 STL build, creation from std::string requires conversion under
793 // non-UTF8 locales, so we can't have and use wxString(wxStringImpl) ctor;
794 // instead we define dummy type that lets us have wxString ctor for creation
795 // from wxStringImpl that couldn't be used by user code (in all other builds,
796 // "standard" ctors can be used):
797 #if wxUSE_UNICODE_UTF8 && wxUSE_STL_BASED_WXSTRING
798 struct CtorFromStringImplTag
{};
800 wxString(CtorFromStringImplTag
* WXUNUSED(dummy
), const wxStringImpl
& src
)
803 static wxString
FromImpl(const wxStringImpl
& src
)
804 { return wxString((CtorFromStringImplTag
*)NULL
, src
); }
806 #if !wxUSE_STL_BASED_WXSTRING
807 wxString(const wxStringImpl
& src
) : m_impl(src
) { }
808 // else: already defined as wxString(wxStdString) below
810 static wxString
FromImpl(const wxStringImpl
& src
) { return wxString(src
); }
814 // constructors and destructor
815 // ctor for an empty string
819 wxString(const wxString
& stringSrc
) : m_impl(stringSrc
.m_impl
) { }
821 // string containing nRepeat copies of ch
822 wxString(wxUniChar ch
, size_t nRepeat
= 1)
823 { assign(nRepeat
, ch
); }
824 wxString(size_t nRepeat
, wxUniChar ch
)
825 { assign(nRepeat
, ch
); }
826 wxString(wxUniCharRef ch
, size_t nRepeat
= 1)
827 { assign(nRepeat
, ch
); }
828 wxString(size_t nRepeat
, wxUniCharRef ch
)
829 { assign(nRepeat
, ch
); }
830 wxString(char ch
, size_t nRepeat
= 1)
831 { assign(nRepeat
, ch
); }
832 wxString(size_t nRepeat
, char ch
)
833 { assign(nRepeat
, ch
); }
834 wxString(wchar_t ch
, size_t nRepeat
= 1)
835 { assign(nRepeat
, ch
); }
836 wxString(size_t nRepeat
, wchar_t ch
)
837 { assign(nRepeat
, ch
); }
839 // ctors from char* strings:
840 wxString(const char *psz
)
841 : m_impl(ImplStr(psz
)) {}
842 wxString(const char *psz
, const wxMBConv
& conv
)
843 : m_impl(ImplStr(psz
, conv
)) {}
844 wxString(const char *psz
, size_t nLength
)
845 { assign(psz
, nLength
); }
846 wxString(const char *psz
, const wxMBConv
& conv
, size_t nLength
)
848 SubstrBufFromMB
str(ImplStr(psz
, nLength
, conv
));
849 m_impl
.assign(str
.data
, str
.len
);
852 // and unsigned char*:
853 wxString(const unsigned char *psz
)
854 : m_impl(ImplStr((const char*)psz
)) {}
855 wxString(const unsigned char *psz
, const wxMBConv
& conv
)
856 : m_impl(ImplStr((const char*)psz
, conv
)) {}
857 wxString(const unsigned char *psz
, size_t nLength
)
858 { assign((const char*)psz
, nLength
); }
859 wxString(const unsigned char *psz
, const wxMBConv
& conv
, size_t nLength
)
861 SubstrBufFromMB
str(ImplStr((const char*)psz
, nLength
, conv
));
862 m_impl
.assign(str
.data
, str
.len
);
865 // ctors from wchar_t* strings:
866 wxString(const wchar_t *pwz
)
867 : m_impl(ImplStr(pwz
)) {}
868 wxString(const wchar_t *pwz
, const wxMBConv
& WXUNUSED(conv
))
869 : m_impl(ImplStr(pwz
)) {}
870 wxString(const wchar_t *pwz
, size_t nLength
)
871 { assign(pwz
, nLength
); }
872 wxString(const wchar_t *pwz
, const wxMBConv
& WXUNUSED(conv
), size_t nLength
)
873 { assign(pwz
, nLength
); }
875 wxString(const wxCharBuffer
& buf
)
876 { assign(buf
.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
877 wxString(const wxWCharBuffer
& buf
)
878 { assign(buf
.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
880 wxString(const wxCStrData
& cstr
)
881 : m_impl(cstr
.AsString().m_impl
) { }
883 // as we provide both ctors with this signature for both char and unsigned
884 // char string, we need to provide one for wxCStrData to resolve ambiguity
885 wxString(const wxCStrData
& cstr
, size_t nLength
)
886 : m_impl(cstr
.AsString().Mid(0, nLength
).m_impl
) {}
888 // and because wxString is convertible to wxCStrData and const wxChar *
889 // we also need to provide this one
890 wxString(const wxString
& str
, size_t nLength
)
891 : m_impl(str
.Mid(0, nLength
).m_impl
) {}
893 // even if we're not built with wxUSE_STL == 1 it is very convenient to allow
894 // implicit conversions from std::string to wxString and vice verse as this
895 // allows to use the same strings in non-GUI and GUI code, however we don't
896 // want to unconditionally add this ctor as it would make wx lib dependent on
897 // libstdc++ on some Linux versions which is bad, so instead we ask the
898 // client code to define this wxUSE_STD_STRING symbol if they need it
900 #if wxUSE_UNICODE_WCHAR
901 wxString(const wxStdWideString
& str
) : m_impl(str
) {}
902 #else // UTF-8 or ANSI
903 wxString(const wxStdWideString
& str
)
904 { assign(str
.c_str(), str
.length()); }
907 #if !wxUSE_UNICODE // ANSI build
908 // FIXME-UTF8: do this in UTF8 build #if wxUSE_UTF8_LOCALE_ONLY, too
909 wxString(const std::string
& str
) : m_impl(str
) {}
911 wxString(const std::string
& str
)
912 { assign(str
.c_str(), str
.length()); }
915 #if wxUSE_UNICODE_WCHAR && wxUSE_STL_BASED_WXSTRING
916 // wxStringImpl is std::string in the encoding we want
917 operator const wxStdWideString
&() const { return m_impl
; }
919 // wxStringImpl is either not std::string or needs conversion
920 operator wxStdWideString() const
921 // FIXME-UTF8: broken for embedded NULs
922 { return wxStdWideString(wc_str()); }
925 #if !wxUSE_UNICODE && wxUSE_STL_BASED_WXSTRING
926 // FIXME-UTF8: do this in UTF8 build #if wxUSE_UTF8_LOCALE_ONLY, too
927 // wxStringImpl is std::string in the encoding we want
928 operator const std::string
&() const { return m_impl
; }
930 // wxStringImpl is either not std::string or needs conversion
931 operator std::string() const
932 // FIXME-UTF8: broken for embedded NULs
933 { return std::string(mb_str()); }
936 #endif // wxUSE_STD_STRING
938 // first valid index position
939 const_iterator
begin() const { return const_iterator(m_impl
.begin()); }
940 iterator
begin() { return iterator(this, m_impl
.begin()); }
941 // position one after the last valid one
942 const_iterator
end() const { return const_iterator(m_impl
.end()); }
943 iterator
end() { return iterator(this, m_impl
.end()); }
945 // first element of the reversed string
946 const_reverse_iterator
rbegin() const
947 { return const_reverse_iterator(end()); }
948 reverse_iterator
rbegin()
949 { return reverse_iterator(end()); }
950 // one beyond the end of the reversed string
951 const_reverse_iterator
rend() const
952 { return const_reverse_iterator(begin()); }
953 reverse_iterator
rend()
954 { return reverse_iterator(begin()); }
956 // std::string methods:
957 #if wxUSE_UNICODE_UTF8
958 size_t length() const { return end() - begin(); } // FIXME-UTF8: optimize!
960 size_t length() const { return m_impl
.length(); }
963 size_type
size() const { return length(); }
964 size_type
max_size() const { return npos
; }
966 bool empty() const { return m_impl
.empty(); }
968 size_type
capacity() const { return m_impl
.capacity(); } // FIXME-UTF8
969 void reserve(size_t sz
) { m_impl
.reserve(sz
); } // FIXME-UTF8
971 void resize(size_t nSize
, wxUniChar ch
= wxT('\0'))
973 #if wxUSE_UNICODE_UTF8
976 size_t len
= length();
979 else if ( nSize
< len
)
982 append(nSize
- len
, ch
);
986 m_impl
.resize(nSize
, (wxStringCharType
)ch
);
989 wxString
substr(size_t nStart
= 0, size_t nLen
= npos
) const
992 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
993 return FromImpl(m_impl
.substr(pos
, len
));
996 // generic attributes & operations
997 // as standard strlen()
998 size_t Len() const { return length(); }
999 // string contains any characters?
1000 bool IsEmpty() const { return empty(); }
1001 // empty string is "false", so !str will return true
1002 bool operator!() const { return empty(); }
1003 // truncate the string to given length
1004 wxString
& Truncate(size_t uiLen
);
1005 // empty string contents
1010 wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") );
1012 // empty the string and free memory
1015 wxString
tmp(wxEmptyString
);
1020 // Is an ascii value
1021 bool IsAscii() const;
1023 bool IsNumber() const;
1025 bool IsWord() const;
1027 // data access (all indexes are 0 based)
1029 wxUniChar
at(size_t n
) const
1030 { return *(begin() + n
); } // FIXME-UTF8: optimize?
1031 wxUniChar
GetChar(size_t n
) const
1033 // read/write access
1034 wxUniCharRef
at(size_t n
)
1035 { return *(begin() + n
); } // FIXME-UTF8: optimize?
1036 wxUniCharRef
GetWritableChar(size_t n
)
1039 void SetChar(size_t n
, wxUniChar ch
)
1042 // get last character
1043 wxUniChar
Last() const
1045 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1047 return at(length() - 1);
1050 // get writable last character
1053 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1054 return at(length() - 1);
1058 Note that we we must define all of the overloads below to avoid
1059 ambiguity when using str[0].
1061 wxUniChar
operator[](int n
) const
1063 wxUniChar
operator[](long n
) const
1065 wxUniChar
operator[](size_t n
) const
1067 #ifndef wxSIZE_T_IS_UINT
1068 wxUniChar
operator[](unsigned int n
) const
1070 #endif // size_t != unsigned int
1072 // operator versions of GetWriteableChar()
1073 wxUniCharRef
operator[](int n
)
1075 wxUniCharRef
operator[](long n
)
1077 wxUniCharRef
operator[](size_t n
)
1079 #ifndef wxSIZE_T_IS_UINT
1080 wxUniCharRef
operator[](unsigned int n
)
1082 #endif // size_t != unsigned int
1084 // explicit conversion to C string (use this with printf()!)
1085 wxCStrData
c_str() const { return wxCStrData(this); }
1086 wxCStrData
data() const { return c_str(); }
1088 // implicit conversion to C string
1089 operator wxCStrData() const { return c_str(); }
1090 operator const char*() const { return c_str(); }
1091 operator const wchar_t*() const { return c_str(); }
1093 // implicit conversion to untyped pointer for compatibility with previous
1094 // wxWidgets versions: this is the same as conversion to const char * so it
1096 operator const void*() const { return c_str(); }
1098 // identical to c_str(), for MFC compatibility
1099 const wxCStrData
GetData() const { return c_str(); }
1101 // explicit conversion to C string in internal representation (char*,
1102 // wchar_t*, UTF-8-encoded char*, depending on the build):
1103 const wxStringCharType
*wx_str() const { return m_impl
.c_str(); }
1105 // conversion to *non-const* multibyte or widestring buffer; modifying
1106 // returned buffer won't affect the string, these methods are only useful
1107 // for passing values to const-incorrect functions
1108 wxWritableCharBuffer
char_str(const wxMBConv
& conv
= wxConvLibc
) const
1109 { return mb_str(conv
); }
1110 wxWritableWCharBuffer
wchar_str() const { return wc_str(); }
1112 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
1113 // converting numbers or strings which are certain not to contain special
1114 // chars (typically system functions, X atoms, environment variables etc.)
1116 // the behaviour of these functions with the strings containing anything
1117 // else than 7 bit ASCII characters is undefined, use at your own risk.
1119 static wxString
FromAscii(const char *ascii
); // string
1120 static wxString
FromAscii(const char ascii
); // char
1121 const wxCharBuffer
ToAscii() const;
1123 static wxString
FromAscii(const char *ascii
) { return wxString( ascii
); }
1124 static wxString
FromAscii(const char ascii
) { return wxString( ascii
); }
1125 const char *ToAscii() const { return c_str(); }
1126 #endif // Unicode/!Unicode
1128 // conversions with (possible) format conversions: have to return a
1129 // buffer with temporary data
1131 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
1132 // return an ANSI (multibyte) string, wc_str() to return a wide string and
1133 // fn_str() to return a string which should be used with the OS APIs
1134 // accepting the file names. The return value is always the same, but the
1135 // type differs because a function may either return pointer to the buffer
1136 // directly or have to use intermediate buffer for translation.
1138 const wxCharBuffer
mb_str(const wxMBConv
& conv
= wxConvLibc
) const;
1140 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
1142 #if wxUSE_UNICODE_WCHAR
1143 const wxChar
* wc_str() const { return wx_str(); }
1144 #elif wxUSE_UNICODE_UTF8
1145 const wxWCharBuffer
wc_str() const;
1147 // for compatibility with !wxUSE_UNICODE version
1148 const wxWX2WCbuf
wc_str(const wxMBConv
& WXUNUSED(conv
)) const
1149 { return wc_str(); }
1152 const wxCharBuffer
fn_str() const { return mb_str(wxConvFile
); }
1154 const wxWX2WCbuf
fn_str() const { return wc_str(); }
1155 #endif // wxMBFILES/!wxMBFILES
1158 const wxChar
* mb_str() const { return wx_str(); }
1160 // for compatibility with wxUSE_UNICODE version
1161 const wxChar
* mb_str(const wxMBConv
& WXUNUSED(conv
)) const { return wx_str(); }
1163 const wxWX2MBbuf
mbc_str() const { return mb_str(); }
1166 const wxWCharBuffer
wc_str(const wxMBConv
& conv
= wxConvLibc
) const;
1167 #endif // wxUSE_WCHAR_T
1169 const wxCharBuffer
fn_str() const { return wxConvFile
.cWC2WX( wc_str( wxConvLocal
) ); }
1171 const wxChar
* fn_str() const { return c_str(); }
1173 #endif // Unicode/ANSI
1175 // overloaded assignment
1176 // from another wxString
1177 wxString
& operator=(const wxString
& stringSrc
)
1178 { m_impl
= stringSrc
.m_impl
; return *this; }
1179 wxString
& operator=(const wxCStrData
& cstr
)
1180 { return *this = cstr
.AsString(); }
1182 wxString
& operator=(wxUniChar ch
)
1183 { m_impl
= wxStringOperations::EncodeChar(ch
); return *this; }
1184 wxString
& operator=(wxUniCharRef ch
)
1185 { return operator=((wxUniChar
)ch
); }
1186 wxString
& operator=(char ch
)
1187 { return operator=(wxUniChar(ch
)); }
1188 wxString
& operator=(unsigned char ch
)
1189 { return operator=(wxUniChar(ch
)); }
1190 wxString
& operator=(wchar_t ch
)
1191 { return operator=(wxUniChar(ch
)); }
1192 // from a C string - STL probably will crash on NULL,
1193 // so we need to compensate in that case
1194 #if wxUSE_STL_BASED_WXSTRING
1195 wxString
& operator=(const char *psz
)
1196 { if (psz
) m_impl
= ImplStr(psz
); else Clear(); return *this; }
1197 wxString
& operator=(const wchar_t *pwz
)
1198 { if (pwz
) m_impl
= ImplStr(pwz
); else Clear(); return *this; }
1200 wxString
& operator=(const char *psz
)
1201 { m_impl
= ImplStr(psz
); return *this; }
1202 wxString
& operator=(const wchar_t *pwz
)
1203 { m_impl
= ImplStr(pwz
); return *this; }
1205 wxString
& operator=(const unsigned char *psz
)
1206 { return operator=((const char*)psz
); }
1208 // from wxWCharBuffer
1209 wxString
& operator=(const wxWCharBuffer
& s
)
1210 { return operator=(s
.data()); } // FIXME-UTF8: fix for embedded NULs
1211 // from wxCharBuffer
1212 wxString
& operator=(const wxCharBuffer
& s
)
1213 { return operator=(s
.data()); } // FIXME-UTF8: fix for embedded NULs
1215 // string concatenation
1216 // in place concatenation
1218 Concatenate and return the result. Note that the left to right
1219 associativity of << allows to write things like "str << str1 << str2
1220 << ..." (unlike with +=)
1223 wxString
& operator<<(const wxString
& s
)
1225 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1226 wxASSERT_MSG( s
.IsValid(),
1227 _T("did you forget to call UngetWriteBuf()?") );
1233 // string += C string
1234 wxString
& operator<<(const char *psz
)
1235 { append(psz
); return *this; }
1236 wxString
& operator<<(const wchar_t *pwz
)
1237 { append(pwz
); return *this; }
1238 wxString
& operator<<(const wxCStrData
& psz
)
1239 { append(psz
.AsString()); return *this; }
1241 wxString
& operator<<(wxUniChar ch
) { append(1, ch
); return *this; }
1242 wxString
& operator<<(wxUniCharRef ch
) { append(1, ch
); return *this; }
1243 wxString
& operator<<(char ch
) { append(1, ch
); return *this; }
1244 wxString
& operator<<(unsigned char ch
) { append(1, ch
); return *this; }
1245 wxString
& operator<<(wchar_t ch
) { append(1, ch
); return *this; }
1247 // string += buffer (i.e. from wxGetString)
1248 wxString
& operator<<(const wxWCharBuffer
& s
)
1249 { return operator<<((const wchar_t *)s
); }
1250 wxString
& operator+=(const wxWCharBuffer
& s
)
1251 { return operator<<((const wchar_t *)s
); }
1253 wxString
& operator<<(const wxCharBuffer
& s
)
1254 { return operator<<((const char *)s
); }
1255 wxString
& operator+=(const wxCharBuffer
& s
)
1256 { return operator<<((const char *)s
); }
1258 // string += C string
1259 wxString
& Append(const wxString
& s
)
1261 // test for empty() to share the string if possible
1268 wxString
& Append(const wxCStrData
& psz
)
1269 { append(psz
); return *this; }
1270 wxString
& Append(const char* psz
)
1271 { append(psz
); return *this; }
1272 wxString
& Append(const wchar_t* pwz
)
1273 { append(pwz
); return *this; }
1274 // append count copies of given character
1275 wxString
& Append(wxUniChar ch
, size_t count
= 1u)
1276 { append(count
, ch
); return *this; }
1277 wxString
& Append(wxUniCharRef ch
, size_t count
= 1u)
1278 { append(count
, ch
); return *this; }
1279 wxString
& Append(char ch
, size_t count
= 1u)
1280 { append(count
, ch
); return *this; }
1281 wxString
& Append(unsigned char ch
, size_t count
= 1u)
1282 { append(count
, ch
); return *this; }
1283 wxString
& Append(wchar_t ch
, size_t count
= 1u)
1284 { append(count
, ch
); return *this; }
1285 wxString
& Append(const char* psz
, size_t nLen
)
1286 { append(psz
, nLen
); return *this; }
1287 wxString
& Append(const wchar_t* pwz
, size_t nLen
)
1288 { append(pwz
, nLen
); return *this; }
1290 // prepend a string, return the string itself
1291 wxString
& Prepend(const wxString
& str
)
1292 { *this = str
+ *this; return *this; }
1294 // non-destructive concatenation
1296 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
,
1297 const wxString
& string2
);
1298 // string with a single char
1299 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
1300 // char with a string
1301 friend wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
1302 // string with C string
1303 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
1305 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
1306 const wchar_t *pwz
);
1307 // C string with string
1308 friend wxString WXDLLIMPEXP_BASE
operator+(const char *psz
,
1309 const wxString
& string
);
1310 friend wxString WXDLLIMPEXP_BASE
operator+(const wchar_t *pwz
,
1311 const wxString
& string
);
1313 // stream-like functions
1314 // insert an int into string
1315 wxString
& operator<<(int i
)
1316 { return (*this) << Format(_T("%d"), i
); }
1317 // insert an unsigned int into string
1318 wxString
& operator<<(unsigned int ui
)
1319 { return (*this) << Format(_T("%u"), ui
); }
1320 // insert a long into string
1321 wxString
& operator<<(long l
)
1322 { return (*this) << Format(_T("%ld"), l
); }
1323 // insert an unsigned long into string
1324 wxString
& operator<<(unsigned long ul
)
1325 { return (*this) << Format(_T("%lu"), ul
); }
1326 #if defined wxLongLong_t && !defined wxLongLongIsLong
1327 // insert a long long if they exist and aren't longs
1328 wxString
& operator<<(wxLongLong_t ll
)
1330 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("d");
1331 return (*this) << Format(fmt
, ll
);
1333 // insert an unsigned long long
1334 wxString
& operator<<(wxULongLong_t ull
)
1336 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("u");
1337 return (*this) << Format(fmt
, ull
);
1340 // insert a float into string
1341 wxString
& operator<<(float f
)
1342 { return (*this) << Format(_T("%f"), f
); }
1343 // insert a double into string
1344 wxString
& operator<<(double d
)
1345 { return (*this) << Format(_T("%g"), d
); }
1347 // string comparison
1348 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
1349 int Cmp(const char *psz
) const
1350 { return compare(psz
); }
1351 int Cmp(const wchar_t *pwz
) const
1352 { return compare(pwz
); }
1353 int Cmp(const wxString
& s
) const
1354 { return compare(s
); }
1355 // same as Cmp() but not case-sensitive
1356 int CmpNoCase(const wxString
& s
) const;
1357 int CmpNoCase(const char *psz
) const
1358 { return CmpNoCase(wxString(psz
)); }
1359 int CmpNoCase(const wchar_t *pwz
) const
1360 { return CmpNoCase(wxString(pwz
)); }
1361 // test for the string equality, either considering case or not
1362 // (if compareWithCase then the case matters)
1363 bool IsSameAs(const wxString
& str
, bool compareWithCase
= true) const
1364 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
1365 bool IsSameAs(const char *str
, bool compareWithCase
= true) const
1366 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
1367 bool IsSameAs(const wchar_t *str
, bool compareWithCase
= true) const
1368 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
1369 // comparison with a single character: returns true if equal
1370 bool IsSameAs(wxUniChar c
, bool compareWithCase
= true) const
1372 return (length() == 1) && (compareWithCase
? GetChar(0u) == c
1373 : wxToupper(GetChar(0u)) == wxToupper(c
));
1375 // FIXME-UTF8: remove these overloads
1376 bool IsSameAs(wxUniCharRef c
, bool compareWithCase
= true) const
1377 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1378 bool IsSameAs(char c
, bool compareWithCase
= true) const
1379 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1380 bool IsSameAs(unsigned char c
, bool compareWithCase
= true) const
1381 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1382 bool IsSameAs(wchar_t c
, bool compareWithCase
= true) const
1383 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1384 bool IsSameAs(int c
, bool compareWithCase
= true) const
1385 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1387 // simple sub-string extraction
1388 // return substring starting at nFirst of length nCount (or till the end
1389 // if nCount = default value)
1390 wxString
Mid(size_t nFirst
, size_t nCount
= npos
) const;
1392 // operator version of Mid()
1393 wxString
operator()(size_t start
, size_t len
) const
1394 { return Mid(start
, len
); }
1396 // check if the string starts with the given prefix and return the rest
1397 // of the string in the provided pointer if it is not NULL; otherwise
1399 bool StartsWith(const wxChar
*prefix
, wxString
*rest
= NULL
) const;
1400 // check if the string ends with the given suffix and return the
1401 // beginning of the string before the suffix in the provided pointer if
1402 // it is not NULL; otherwise return false
1403 bool EndsWith(const wxChar
*suffix
, wxString
*rest
= NULL
) const;
1405 // get first nCount characters
1406 wxString
Left(size_t nCount
) const;
1407 // get last nCount characters
1408 wxString
Right(size_t nCount
) const;
1409 // get all characters before the first occurance of ch
1410 // (returns the whole string if ch not found)
1411 wxString
BeforeFirst(wxUniChar ch
) const;
1412 // get all characters before the last occurence of ch
1413 // (returns empty string if ch not found)
1414 wxString
BeforeLast(wxUniChar ch
) const;
1415 // get all characters after the first occurence of ch
1416 // (returns empty string if ch not found)
1417 wxString
AfterFirst(wxUniChar ch
) const;
1418 // get all characters after the last occurence of ch
1419 // (returns the whole string if ch not found)
1420 wxString
AfterLast(wxUniChar ch
) const;
1422 // for compatibility only, use more explicitly named functions above
1423 wxString
Before(wxUniChar ch
) const { return BeforeLast(ch
); }
1424 wxString
After(wxUniChar ch
) const { return AfterFirst(ch
); }
1427 // convert to upper case in place, return the string itself
1428 wxString
& MakeUpper();
1429 // convert to upper case, return the copy of the string
1430 // Here's something to remember: BC++ doesn't like returns in inlines.
1431 wxString
Upper() const ;
1432 // convert to lower case in place, return the string itself
1433 wxString
& MakeLower();
1434 // convert to lower case, return the copy of the string
1435 wxString
Lower() const ;
1437 // trimming/padding whitespace (either side) and truncating
1438 // remove spaces from left or from right (default) side
1439 wxString
& Trim(bool bFromRight
= true);
1440 // add nCount copies chPad in the beginning or at the end (default)
1441 wxString
& Pad(size_t nCount
, wxUniChar chPad
= wxT(' '), bool bFromRight
= true);
1443 // searching and replacing
1444 // searching (return starting index, or -1 if not found)
1445 int Find(wxUniChar ch
, bool bFromEnd
= false) const; // like strchr/strrchr
1446 int Find(wxUniCharRef ch
, bool bFromEnd
= false) const
1447 { return Find(wxUniChar(ch
), bFromEnd
); }
1448 int Find(char ch
, bool bFromEnd
= false) const
1449 { return Find(wxUniChar(ch
), bFromEnd
); }
1450 int Find(unsigned char ch
, bool bFromEnd
= false) const
1451 { return Find(wxUniChar(ch
), bFromEnd
); }
1452 int Find(wchar_t ch
, bool bFromEnd
= false) const
1453 { return Find(wxUniChar(ch
), bFromEnd
); }
1454 // searching (return starting index, or -1 if not found)
1455 // FIXME-UTF8: keep wxString overload only
1456 int Find(const wxString
& sub
) const // like strstr
1458 size_type idx
= find(sub
);
1459 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
1461 int Find(const char *sub
) const // like strstr
1463 size_type idx
= find(sub
);
1464 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
1466 int Find(const wchar_t *sub
) const // like strstr
1468 size_type idx
= find(sub
);
1469 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
1472 // replace first (or all of bReplaceAll) occurences of substring with
1473 // another string, returns the number of replacements made
1474 size_t Replace(const wxString
& strOld
,
1475 const wxString
& strNew
,
1476 bool bReplaceAll
= true);
1478 // check if the string contents matches a mask containing '*' and '?'
1479 bool Matches(const wxString
& mask
) const;
1481 // conversion to numbers: all functions return true only if the whole
1482 // string is a number and put the value of this number into the pointer
1483 // provided, the base is the numeric base in which the conversion should be
1484 // done and must be comprised between 2 and 36 or be 0 in which case the
1485 // standard C rules apply (leading '0' => octal, "0x" => hex)
1486 // convert to a signed integer
1487 bool ToLong(long *val
, int base
= 10) const;
1488 // convert to an unsigned integer
1489 bool ToULong(unsigned long *val
, int base
= 10) const;
1490 // convert to wxLongLong
1491 #if defined(wxLongLong_t)
1492 bool ToLongLong(wxLongLong_t
*val
, int base
= 10) const;
1493 // convert to wxULongLong
1494 bool ToULongLong(wxULongLong_t
*val
, int base
= 10) const;
1495 #endif // wxLongLong_t
1496 // convert to a double
1497 bool ToDouble(double *val
) const;
1500 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1501 // formatted input/output
1502 // as sprintf(), returns the number of characters written or < 0 on error
1503 // (take 'this' into account in attribute parameter count)
1504 // int Printf(const wxString& format, ...);
1505 WX_DEFINE_VARARG_FUNC(int, Printf
, 1, (const wxString
&), DoPrintf
)
1507 WX_DEFINE_VARARG_FUNC(int, Printf
, 1, (const char*), DoPrintf
)
1508 WX_DEFINE_VARARG_FUNC(int, Printf
, 1, (const wchar_t*), DoPrintf
)
1509 WX_DEFINE_VARARG_FUNC(int, Printf
, 1, (const wxCStrData
&), DoPrintf
)
1511 #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN
1512 // as vprintf(), returns the number of characters written or < 0 on error
1513 int PrintfV(const wxString
& format
, va_list argptr
);
1515 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1516 // returns the string containing the result of Printf() to it
1517 // static wxString Format(const wxString& format, ...) ATTRIBUTE_PRINTF_1;
1518 WX_DEFINE_VARARG_FUNC(static wxString
, Format
, 1, (const wxString
&), DoFormat
)
1520 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1521 WX_DEFINE_VARARG_FUNC(static wxString
, Format
, 1, (const char*), DoFormat
)
1522 WX_DEFINE_VARARG_FUNC(static wxString
, Format
, 1, (const wchar_t*), DoFormat
)
1523 WX_DEFINE_VARARG_FUNC(static wxString
, Format
, 1, (const wxCStrData
&), DoFormat
)
1526 // the same as above, but takes a va_list
1527 static wxString
FormatV(const wxString
& format
, va_list argptr
);
1529 // raw access to string memory
1530 // ensure that string has space for at least nLen characters
1531 // only works if the data of this string is not shared
1532 bool Alloc(size_t nLen
) { reserve(nLen
); /*return capacity() >= nLen;*/ return true; }
1533 // minimize the string's memory
1534 // only works if the data of this string is not shared
1536 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1537 // These are deprecated, use wxStringBuffer or wxStringBufferLength instead
1539 // get writable buffer of at least nLen bytes. Unget() *must* be called
1540 // a.s.a.p. to put string back in a reasonable state!
1541 wxDEPRECATED( wxStringCharType
*GetWriteBuf(size_t nLen
) );
1542 // call this immediately after GetWriteBuf() has been used
1543 wxDEPRECATED( void UngetWriteBuf() );
1544 wxDEPRECATED( void UngetWriteBuf(size_t nLen
) );
1545 #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && wxUSE_UNICODE_UTF8
1547 // wxWidgets version 1 compatibility functions
1550 wxString
SubString(size_t from
, size_t to
) const
1551 { return Mid(from
, (to
- from
+ 1)); }
1552 // values for second parameter of CompareTo function
1553 enum caseCompare
{exact
, ignoreCase
};
1554 // values for first parameter of Strip function
1555 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
1557 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1559 // (take 'this' into account in attribute parameter count)
1560 // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2;
1561 WX_DEFINE_VARARG_FUNC(int, sprintf
, 1, (const wxString
&), DoPrintf
)
1563 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1564 WX_DEFINE_VARARG_FUNC(int, sprintf
, 1, (const char*), DoPrintf
)
1565 WX_DEFINE_VARARG_FUNC(int, sprintf
, 1, (const wchar_t*), DoPrintf
)
1566 WX_DEFINE_VARARG_FUNC(int, sprintf
, 1, (const wxCStrData
&), DoPrintf
)
1568 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
1571 inline int CompareTo(const wxChar
* psz
, caseCompare cmp
= exact
) const
1572 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
1575 size_t Length() const { return length(); }
1576 // Count the number of characters
1577 int Freq(wxUniChar ch
) const;
1579 void LowerCase() { MakeLower(); }
1581 void UpperCase() { MakeUpper(); }
1582 // use Trim except that it doesn't change this string
1583 wxString
Strip(stripType w
= trailing
) const;
1585 // use Find (more general variants not yet supported)
1586 size_t Index(const wxChar
* psz
) const { return Find(psz
); }
1587 size_t Index(wxUniChar ch
) const { return Find(ch
); }
1589 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
1590 wxString
& RemoveLast(size_t n
= 1) { return Truncate(length() - n
); }
1592 wxString
& Remove(size_t nStart
, size_t nLen
)
1593 { return (wxString
&)erase( nStart
, nLen
); }
1596 int First( wxUniChar ch
) const { return Find(ch
); }
1597 int First( wxUniCharRef ch
) const { return Find(ch
); }
1598 int First( char ch
) const { return Find(ch
); }
1599 int First( unsigned char ch
) const { return Find(ch
); }
1600 int First( wchar_t ch
) const { return Find(ch
); }
1601 int First( const wxChar
* psz
) const { return Find(psz
); }
1602 int First( const wxString
& str
) const { return Find(str
); }
1603 int Last( wxUniChar ch
) const { return Find(ch
, true); }
1604 bool Contains(const wxString
& str
) const { return Find(str
) != wxNOT_FOUND
; }
1607 bool IsNull() const { return empty(); }
1609 // std::string compatibility functions
1611 // take nLen chars starting at nPos
1612 wxString(const wxString
& str
, size_t nPos
, size_t nLen
)
1613 : m_impl(str
.m_impl
, nPos
, nLen
) { }
1614 // take all characters from first to last
1615 wxString(const_iterator first
, const_iterator last
)
1616 : m_impl(first
.impl(), last
.impl()) { }
1617 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1618 // the 2 overloads below are for compatibility with the existing code using
1619 // pointers instead of iterators
1620 wxString(const char *first
, const char *last
)
1622 SubstrBufFromMB
str(ImplStr(first
, last
- first
));
1623 m_impl
.assign(str
.data
, str
.len
);
1625 wxString(const wchar_t *first
, const wchar_t *last
)
1627 SubstrBufFromWC
str(ImplStr(first
, last
- first
));
1628 m_impl
.assign(str
.data
, str
.len
);
1630 // and this one is needed to compile code adding offsets to c_str() result
1631 wxString(const wxCStrData
& first
, const wxCStrData
& last
)
1632 : m_impl(CreateConstIterator(first
).impl(),
1633 CreateConstIterator(last
).impl())
1635 wxASSERT_MSG( first
.m_str
== last
.m_str
,
1636 _T("pointers must be into the same string") );
1638 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1640 // lib.string.modifiers
1641 // append elements str[pos], ..., str[pos+n]
1642 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
1645 str
.PosLenToImpl(pos
, n
, &from
, &len
);
1646 m_impl
.append(str
.m_impl
, from
, len
);
1650 wxString
& append(const wxString
& str
)
1651 { m_impl
.append(str
.m_impl
); return *this; }
1652 wxString
& append(const wxCStrData
& str
)
1653 { m_impl
.append(str
.AsString().m_impl
); return *this; }
1654 // append first n (or all if n == npos) characters of sz
1655 wxString
& append(const char *sz
)
1656 { m_impl
.append(ImplStr(sz
)); return *this; }
1657 wxString
& append(const wchar_t *sz
)
1658 { m_impl
.append(ImplStr(sz
)); return *this; }
1659 wxString
& append(const char *sz
, size_t n
)
1661 SubstrBufFromMB
str(ImplStr(sz
, n
));
1662 m_impl
.append(str
.data
, str
.len
);
1665 wxString
& append(const wchar_t *sz
, size_t n
)
1667 SubstrBufFromWC
str(ImplStr(sz
, n
));
1668 m_impl
.append(str
.data
, str
.len
);
1671 // append n copies of ch
1672 wxString
& append(size_t n
, wxUniChar ch
)
1674 #if wxUSE_UNICODE_UTF8
1675 if ( !ch
.IsAscii() )
1676 m_impl
.append(wxStringOperations::EncodeNChars(n
, ch
));
1679 m_impl
.append(n
, (wxStringCharType
)ch
);
1682 // append from first to last
1683 wxString
& append(const_iterator first
, const_iterator last
)
1684 { m_impl
.append(first
.impl(), last
.impl()); return *this; }
1685 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1686 wxString
& append(const char *first
, const char *last
)
1687 { return append(first
, last
- first
); }
1688 wxString
& append(const wchar_t *first
, const wchar_t *last
)
1689 { return append(first
, last
- first
); }
1690 wxString
& append(const wxCStrData
& first
, const wxCStrData
& last
)
1691 { return append(CreateConstIterator(first
), CreateConstIterator(last
)); }
1692 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1694 // same as `this_string = str'
1695 wxString
& assign(const wxString
& str
)
1696 { m_impl
= str
.m_impl
; return *this; }
1697 wxString
& assign(const wxString
& str
, size_t len
)
1699 m_impl
.assign(str
.m_impl
, 0, str
.LenToImpl(len
));
1702 // same as ` = str[pos..pos + n]
1703 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
1706 str
.PosLenToImpl(pos
, n
, &from
, &len
);
1707 m_impl
.assign(str
.m_impl
, from
, len
);
1710 // same as `= first n (or all if n == npos) characters of sz'
1711 wxString
& assign(const char *sz
)
1712 { m_impl
.assign(ImplStr(sz
)); return *this; }
1713 wxString
& assign(const wchar_t *sz
)
1714 { m_impl
.assign(ImplStr(sz
)); return *this; }
1715 wxString
& assign(const char *sz
, size_t n
)
1717 SubstrBufFromMB
str(ImplStr(sz
, n
));
1718 m_impl
.assign(str
.data
, str
.len
);
1721 wxString
& assign(const wchar_t *sz
, size_t n
)
1723 SubstrBufFromWC
str(ImplStr(sz
, n
));
1724 m_impl
.assign(str
.data
, str
.len
);
1727 // same as `= n copies of ch'
1728 wxString
& assign(size_t n
, wxUniChar ch
)
1730 #if wxUSE_UNICODE_UTF8
1731 if ( !ch
.IsAscii() )
1732 m_impl
.assign(wxStringOperations::EncodeNChars(n
, ch
));
1735 m_impl
.assign(n
, (wxStringCharType
)ch
);
1739 wxString
& assign(size_t n
, wxUniCharRef ch
)
1740 { return assign(n
, wxUniChar(ch
)); }
1741 wxString
& assign(size_t n
, char ch
)
1742 { return assign(n
, wxUniChar(ch
)); }
1743 wxString
& assign(size_t n
, unsigned char ch
)
1744 { return assign(n
, wxUniChar(ch
)); }
1745 wxString
& assign(size_t n
, wchar_t ch
)
1746 { return assign(n
, wxUniChar(ch
)); }
1748 // assign from first to last
1749 wxString
& assign(const_iterator first
, const_iterator last
)
1750 { m_impl
.assign(first
.impl(), last
.impl()); return *this; }
1751 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1752 wxString
& assign(const char *first
, const char *last
)
1753 { return assign(first
, last
- first
); }
1754 wxString
& assign(const wchar_t *first
, const wchar_t *last
)
1755 { return assign(first
, last
- first
); }
1756 wxString
& assign(const wxCStrData
& first
, const wxCStrData
& last
)
1757 { return assign(CreateConstIterator(first
), CreateConstIterator(last
)); }
1758 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1760 // string comparison
1761 int compare(const wxString
& str
) const;
1762 // comparison with a substring
1763 int compare(size_t nStart
, size_t nLen
, const wxString
& str
) const;
1764 // comparison of 2 substrings
1765 int compare(size_t nStart
, size_t nLen
,
1766 const wxString
& str
, size_t nStart2
, size_t nLen2
) const;
1767 // just like strcmp()
1768 int compare(const char* sz
) const;
1769 int compare(const wchar_t* sz
) const;
1770 // substring comparison with first nCount characters of sz
1771 int compare(size_t nStart
, size_t nLen
,
1772 const char* sz
, size_t nCount
= npos
) const;
1773 int compare(size_t nStart
, size_t nLen
,
1774 const wchar_t* sz
, size_t nCount
= npos
) const;
1776 // insert another string
1777 wxString
& insert(size_t nPos
, const wxString
& str
)
1778 { insert(begin() + nPos
, str
.begin(), str
.end()); return *this; }
1779 // insert n chars of str starting at nStart (in str)
1780 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
1783 str
.PosLenToImpl(nStart
, n
, &from
, &len
);
1784 m_impl
.insert(PosToImpl(nPos
), str
.m_impl
, from
, len
);
1787 // insert first n (or all if n == npos) characters of sz
1788 wxString
& insert(size_t nPos
, const char *sz
)
1789 { m_impl
.insert(PosToImpl(nPos
), ImplStr(sz
)); return *this; }
1790 wxString
& insert(size_t nPos
, const wchar_t *sz
)
1791 { m_impl
.insert(PosToImpl(nPos
), ImplStr(sz
)); return *this; }
1792 wxString
& insert(size_t nPos
, const char *sz
, size_t n
)
1794 SubstrBufFromMB
str(ImplStr(sz
, n
));
1795 m_impl
.insert(PosToImpl(nPos
), str
.data
, str
.len
);
1798 wxString
& insert(size_t nPos
, const wchar_t *sz
, size_t n
)
1800 SubstrBufFromWC
str(ImplStr(sz
, n
));
1801 m_impl
.insert(PosToImpl(nPos
), str
.data
, str
.len
);
1804 // insert n copies of ch
1805 wxString
& insert(size_t nPos
, size_t n
, wxUniChar ch
)
1807 #if wxUSE_UNICODE_UTF8
1808 if ( !ch
.IsAscii() )
1809 m_impl
.insert(PosToImpl(nPos
), wxStringOperations::EncodeNChars(n
, ch
));
1812 m_impl
.insert(PosToImpl(nPos
), n
, (wxStringCharType
)ch
);
1815 iterator
insert(iterator it
, wxUniChar ch
)
1817 #if wxUSE_UNICODE_UTF8
1818 if ( !ch
.IsAscii() )
1820 size_t pos
= IterToImplPos(it
);
1821 m_impl
.insert(pos
, wxStringOperations::EncodeChar(ch
));
1822 return iterator(this, m_impl
.begin() + pos
);
1826 return iterator(this, m_impl
.insert(it
.impl(), (wxStringCharType
)ch
));
1828 void insert(iterator it
, const_iterator first
, const_iterator last
)
1829 { m_impl
.insert(it
.impl(), first
.impl(), last
.impl()); }
1830 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1831 void insert(iterator it
, const char *first
, const char *last
)
1832 { insert(it
- begin(), first
, last
- first
); }
1833 void insert(iterator it
, const wchar_t *first
, const wchar_t *last
)
1834 { insert(it
- begin(), first
, last
- first
); }
1835 void insert(iterator it
, const wxCStrData
& first
, const wxCStrData
& last
)
1836 { insert(it
, CreateConstIterator(first
), CreateConstIterator(last
)); }
1837 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1839 void insert(iterator it
, size_type n
, wxUniChar ch
)
1841 #if wxUSE_UNICODE_UTF8
1842 if ( !ch
.IsAscii() )
1843 m_impl
.insert(IterToImplPos(it
), wxStringOperations::EncodeNChars(n
, ch
));
1846 m_impl
.insert(it
.impl(), n
, (wxStringCharType
)ch
);
1849 // delete characters from nStart to nStart + nLen
1850 wxString
& erase(size_type pos
= 0, size_type n
= npos
)
1853 PosLenToImpl(pos
, n
, &from
, &len
);
1854 m_impl
.erase(from
, len
);
1857 // delete characters from first up to last
1858 iterator
erase(iterator first
, iterator last
)
1859 { return iterator(this, m_impl
.erase(first
.impl(), last
.impl())); }
1860 iterator
erase(iterator first
)
1861 { return iterator(this, m_impl
.erase(first
.impl())); }
1863 #ifdef wxSTRING_BASE_HASNT_CLEAR
1864 void clear() { erase(); }
1866 void clear() { m_impl
.clear(); }
1869 // replaces the substring of length nLen starting at nStart
1870 wxString
& replace(size_t nStart
, size_t nLen
, const char* sz
)
1873 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1874 m_impl
.replace(from
, len
, ImplStr(sz
));
1877 wxString
& replace(size_t nStart
, size_t nLen
, const wchar_t* sz
)
1880 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1881 m_impl
.replace(from
, len
, ImplStr(sz
));
1884 // replaces the substring of length nLen starting at nStart
1885 wxString
& replace(size_t nStart
, size_t nLen
, const wxString
& str
)
1888 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1889 m_impl
.replace(from
, len
, str
.m_impl
);
1892 // replaces the substring with nCount copies of ch
1893 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxUniChar ch
)
1896 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1897 #if wxUSE_UNICODE_UTF8
1898 if ( !ch
.IsAscii() )
1899 m_impl
.replace(from
, len
, wxStringOperations::EncodeNChars(nCount
, ch
));
1902 m_impl
.replace(from
, len
, nCount
, (wxStringCharType
)ch
);
1905 // replaces a substring with another substring
1906 wxString
& replace(size_t nStart
, size_t nLen
,
1907 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1910 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1913 str
.PosLenToImpl(nStart2
, nLen2
, &from2
, &len2
);
1915 m_impl
.replace(from
, len
, str
.m_impl
, from2
, len2
);
1918 // replaces the substring with first nCount chars of sz
1919 wxString
& replace(size_t nStart
, size_t nLen
,
1920 const char* sz
, size_t nCount
)
1923 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1925 SubstrBufFromMB
str(ImplStr(sz
, nCount
));
1927 m_impl
.replace(from
, len
, str
.data
, str
.len
);
1930 wxString
& replace(size_t nStart
, size_t nLen
,
1931 const wchar_t* sz
, size_t nCount
)
1934 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1936 SubstrBufFromWC
str(ImplStr(sz
, nCount
));
1938 m_impl
.replace(from
, len
, str
.data
, str
.len
);
1941 wxString
& replace(size_t nStart
, size_t nLen
,
1942 const wxString
& s
, size_t nCount
)
1945 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1946 m_impl
.replace(from
, len
, s
.m_impl
.c_str(), s
.LenToImpl(nCount
));
1950 wxString
& replace(iterator first
, iterator last
, const char* s
)
1951 { m_impl
.replace(first
.impl(), last
.impl(), ImplStr(s
)); return *this; }
1952 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
)
1953 { m_impl
.replace(first
.impl(), last
.impl(), ImplStr(s
)); return *this; }
1954 wxString
& replace(iterator first
, iterator last
, const char* s
, size_type n
)
1956 SubstrBufFromMB
str(ImplStr(s
, n
));
1957 m_impl
.replace(first
.impl(), last
.impl(), str
.data
, str
.len
);
1960 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
, size_type n
)
1962 SubstrBufFromWC
str(ImplStr(s
, n
));
1963 m_impl
.replace(first
.impl(), last
.impl(), str
.data
, str
.len
);
1966 wxString
& replace(iterator first
, iterator last
, const wxString
& s
)
1967 { m_impl
.replace(first
.impl(), last
.impl(), s
.m_impl
); return *this; }
1968 wxString
& replace(iterator first
, iterator last
, size_type n
, wxUniChar ch
)
1970 #if wxUSE_UNICODE_UTF8
1971 if ( !ch
.IsAscii() )
1972 m_impl
.replace(first
.impl(), last
.impl(),
1973 wxStringOperations::EncodeNChars(n
, ch
));
1976 m_impl
.replace(first
.impl(), last
.impl(), n
, (wxStringCharType
)ch
);
1979 wxString
& replace(iterator first
, iterator last
,
1980 const_iterator first1
, const_iterator last1
)
1982 m_impl
.replace(first
.impl(), last
.impl(), first1
.impl(), last1
.impl());
1985 wxString
& replace(iterator first
, iterator last
,
1986 const char *first1
, const char *last1
)
1987 { replace(first
, last
, first1
, last1
- first1
); return *this; }
1988 wxString
& replace(iterator first
, iterator last
,
1989 const wchar_t *first1
, const wchar_t *last1
)
1990 { replace(first
, last
, first1
, last1
- first1
); return *this; }
1993 void swap(wxString
& str
)
1994 { m_impl
.swap(str
.m_impl
); }
1997 size_t find(const wxString
& str
, size_t nStart
= 0) const
1998 { return PosFromImpl(m_impl
.find(str
.m_impl
, PosToImpl(nStart
))); }
2000 // find first n characters of sz
2001 size_t find(const char* sz
, size_t nStart
= 0, size_t n
= npos
) const
2003 SubstrBufFromMB
str(ImplStr(sz
, n
));
2004 return PosFromImpl(m_impl
.find(str
.data
, PosToImpl(nStart
), str
.len
));
2006 size_t find(const wchar_t* sz
, size_t nStart
= 0, size_t n
= npos
) const
2008 SubstrBufFromWC
str(ImplStr(sz
, n
));
2009 return PosFromImpl(m_impl
.find(str
.data
, PosToImpl(nStart
), str
.len
));
2012 // find the first occurence of character ch after nStart
2013 size_t find(wxUniChar ch
, size_t nStart
= 0) const
2015 return PosFromImpl(m_impl
.find(wxStringOperations::EncodeChar(ch
),
2016 PosToImpl(nStart
)));
2018 size_t find(wxUniCharRef ch
, size_t nStart
= 0) const
2019 { return find(wxUniChar(ch
), nStart
); }
2020 size_t find(char ch
, size_t nStart
= 0) const
2021 { return find(wxUniChar(ch
), nStart
); }
2022 size_t find(unsigned char ch
, size_t nStart
= 0) const
2023 { return find(wxUniChar(ch
), nStart
); }
2024 size_t find(wchar_t ch
, size_t nStart
= 0) const
2025 { return find(wxUniChar(ch
), nStart
); }
2027 // rfind() family is exactly like find() but works right to left
2029 // as find, but from the end
2030 size_t rfind(const wxString
& str
, size_t nStart
= npos
) const
2031 { return PosFromImpl(m_impl
.rfind(str
.m_impl
, PosToImpl(nStart
))); }
2033 // as find, but from the end
2034 size_t rfind(const char* sz
, size_t nStart
= npos
, size_t n
= npos
) const
2036 SubstrBufFromMB
str(ImplStr(sz
, n
));
2037 return PosFromImpl(m_impl
.rfind(str
.data
, PosToImpl(nStart
), str
.len
));
2039 size_t rfind(const wchar_t* sz
, size_t nStart
= npos
, size_t n
= npos
) const
2041 SubstrBufFromWC
str(ImplStr(sz
, n
));
2042 return PosFromImpl(m_impl
.rfind(str
.data
, PosToImpl(nStart
), str
.len
));
2044 // as find, but from the end
2045 size_t rfind(wxUniChar ch
, size_t nStart
= npos
) const
2047 return PosFromImpl(m_impl
.rfind(wxStringOperations::EncodeChar(ch
),
2048 PosToImpl(nStart
)));
2050 size_t rfind(wxUniCharRef ch
, size_t nStart
= npos
) const
2051 { return rfind(wxUniChar(ch
), nStart
); }
2052 size_t rfind(char ch
, size_t nStart
= npos
) const
2053 { return rfind(wxUniChar(ch
), nStart
); }
2054 size_t rfind(unsigned char ch
, size_t nStart
= npos
) const
2055 { return rfind(wxUniChar(ch
), nStart
); }
2056 size_t rfind(wchar_t ch
, size_t nStart
= npos
) const
2057 { return rfind(wxUniChar(ch
), nStart
); }
2059 // find first/last occurence of any character (not) in the set:
2060 #if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2061 // FIXME-UTF8: this is not entirely correct, because it doesn't work if
2062 // sizeof(wchar_t)==2 and surrogates are present in the string;
2063 // should we care? Probably not.
2064 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
2065 { return m_impl
.find_first_of(str
.m_impl
, nStart
); }
2066 size_t find_first_of(const char* sz
, size_t nStart
= 0) const
2067 { return m_impl
.find_first_of(ImplStr(sz
), nStart
); }
2068 size_t find_first_of(const wchar_t* sz
, size_t nStart
= 0) const
2069 { return m_impl
.find_first_of(ImplStr(sz
), nStart
); }
2070 size_t find_first_of(const char* sz
, size_t nStart
, size_t n
) const
2071 { return m_impl
.find_first_of(ImplStr(sz
), nStart
, n
); }
2072 size_t find_first_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
2073 { return m_impl
.find_first_of(ImplStr(sz
), nStart
, n
); }
2074 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const
2075 { return m_impl
.find_first_of((wxChar
)c
, nStart
); }
2077 size_t find_last_of(const wxString
& str
, size_t nStart
= npos
) const
2078 { return m_impl
.find_last_of(str
.m_impl
, nStart
); }
2079 size_t find_last_of(const char* sz
, size_t nStart
= npos
) const
2080 { return m_impl
.find_last_of(ImplStr(sz
), nStart
); }
2081 size_t find_last_of(const wchar_t* sz
, size_t nStart
= npos
) const
2082 { return m_impl
.find_last_of(ImplStr(sz
), nStart
); }
2083 size_t find_last_of(const char* sz
, size_t nStart
, size_t n
) const
2084 { return m_impl
.find_last_of(ImplStr(sz
), nStart
, n
); }
2085 size_t find_last_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
2086 { return m_impl
.find_last_of(ImplStr(sz
), nStart
, n
); }
2087 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const
2088 { return m_impl
.find_last_of((wxChar
)c
, nStart
); }
2090 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
2091 { return m_impl
.find_first_not_of(str
.m_impl
, nStart
); }
2092 size_t find_first_not_of(const char* sz
, size_t nStart
= 0) const
2093 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
); }
2094 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
= 0) const
2095 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
); }
2096 size_t find_first_not_of(const char* sz
, size_t nStart
, size_t n
) const
2097 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
, n
); }
2098 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
2099 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
, n
); }
2100 size_t find_first_not_of(wxUniChar c
, size_t nStart
= 0) const
2101 { return m_impl
.find_first_not_of((wxChar
)c
, nStart
); }
2103 size_t find_last_not_of(const wxString
& str
, size_t nStart
= npos
) const
2104 { return m_impl
.find_last_not_of(str
.m_impl
, nStart
); }
2105 size_t find_last_not_of(const char* sz
, size_t nStart
= npos
) const
2106 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
); }
2107 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
= npos
) const
2108 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
); }
2109 size_t find_last_not_of(const char* sz
, size_t nStart
, size_t n
) const
2110 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
, n
); }
2111 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
2112 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
, n
); }
2113 size_t find_last_not_of(wxUniChar c
, size_t nStart
= npos
) const
2114 { return m_impl
.find_last_not_of((wxChar
)c
, nStart
); }
2116 // we can't use std::string implementation in UTF-8 build, because the
2117 // character sets would be interpreted wrongly:
2119 // as strpbrk() but starts at nStart, returns npos if not found
2120 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
2121 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2122 { return find_first_of(str
.mb_str().data(), nStart
); }
2124 { return find_first_of((const wxChar
*)str
.c_str(), nStart
); }
2127 size_t find_first_of(const char* sz
, size_t nStart
= 0) const;
2128 size_t find_first_of(const wchar_t* sz
, size_t nStart
= 0) const;
2129 size_t find_first_of(const char* sz
, size_t nStart
, size_t n
) const;
2130 size_t find_first_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2131 // same as find(char, size_t)
2132 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const
2133 { return find(c
, nStart
); }
2134 // find the last (starting from nStart) char from str in this string
2135 size_t find_last_of (const wxString
& str
, size_t nStart
= npos
) const
2136 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2137 { return find_last_of(str
.mb_str().data(), nStart
); }
2139 { return find_last_of((const wxChar
*)str
.c_str(), nStart
); }
2142 size_t find_last_of (const char* sz
, size_t nStart
= npos
) const;
2143 size_t find_last_of (const wchar_t* sz
, size_t nStart
= npos
) const;
2144 size_t find_last_of(const char* sz
, size_t nStart
, size_t n
) const;
2145 size_t find_last_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2147 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const
2148 { return rfind(c
, nStart
); }
2150 // find first/last occurence of any character not in the set
2152 // as strspn() (starting from nStart), returns npos on failure
2153 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
2154 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2155 { return find_first_not_of(str
.mb_str().data(), nStart
); }
2157 { return find_first_not_of((const wxChar
*)str
.c_str(), nStart
); }
2160 size_t find_first_not_of(const char* sz
, size_t nStart
= 0) const;
2161 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
= 0) const;
2162 size_t find_first_not_of(const char* sz
, size_t nStart
, size_t n
) const;
2163 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2165 size_t find_first_not_of(wxUniChar ch
, size_t nStart
= 0) const;
2167 size_t find_last_not_of(const wxString
& str
, size_t nStart
= npos
) const
2168 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2169 { return find_last_not_of(str
.mb_str().data(), nStart
); }
2171 { return find_last_not_of((const wxChar
*)str
.c_str(), nStart
); }
2174 size_t find_last_not_of(const char* sz
, size_t nStart
= npos
) const;
2175 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
= npos
) const;
2176 size_t find_last_not_of(const char* sz
, size_t nStart
, size_t n
) const;
2177 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2179 size_t find_last_not_of(wxUniChar ch
, size_t nStart
= npos
) const;
2180 #endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not
2182 // provide char/wchar_t/wxUniCharRef overloads for char-finding functions
2183 // above to resolve ambiguities:
2184 size_t find_first_of(wxUniCharRef ch
, size_t nStart
= 0) const
2185 { return find_first_of(wxUniChar(ch
), nStart
); }
2186 size_t find_first_of(char ch
, size_t nStart
= 0) const
2187 { return find_first_of(wxUniChar(ch
), nStart
); }
2188 size_t find_first_of(unsigned char ch
, size_t nStart
= 0) const
2189 { return find_first_of(wxUniChar(ch
), nStart
); }
2190 size_t find_first_of(wchar_t ch
, size_t nStart
= 0) const
2191 { return find_first_of(wxUniChar(ch
), nStart
); }
2192 size_t find_last_of(wxUniCharRef ch
, size_t nStart
= npos
) const
2193 { return find_last_of(wxUniChar(ch
), nStart
); }
2194 size_t find_last_of(char ch
, size_t nStart
= npos
) const
2195 { return find_last_of(wxUniChar(ch
), nStart
); }
2196 size_t find_last_of(unsigned char ch
, size_t nStart
= npos
) const
2197 { return find_last_of(wxUniChar(ch
), nStart
); }
2198 size_t find_last_of(wchar_t ch
, size_t nStart
= npos
) const
2199 { return find_last_of(wxUniChar(ch
), nStart
); }
2200 size_t find_first_not_of(wxUniCharRef ch
, size_t nStart
= 0) const
2201 { return find_first_not_of(wxUniChar(ch
), nStart
); }
2202 size_t find_first_not_of(char ch
, size_t nStart
= 0) const
2203 { return find_first_not_of(wxUniChar(ch
), nStart
); }
2204 size_t find_first_not_of(unsigned char ch
, size_t nStart
= 0) const
2205 { return find_first_not_of(wxUniChar(ch
), nStart
); }
2206 size_t find_first_not_of(wchar_t ch
, size_t nStart
= 0) const
2207 { return find_first_not_of(wxUniChar(ch
), nStart
); }
2208 size_t find_last_not_of(wxUniCharRef ch
, size_t nStart
= npos
) const
2209 { return find_last_not_of(wxUniChar(ch
), nStart
); }
2210 size_t find_last_not_of(char ch
, size_t nStart
= npos
) const
2211 { return find_last_not_of(wxUniChar(ch
), nStart
); }
2212 size_t find_last_not_of(unsigned char ch
, size_t nStart
= npos
) const
2213 { return find_last_not_of(wxUniChar(ch
), nStart
); }
2214 size_t find_last_not_of(wchar_t ch
, size_t nStart
= npos
) const
2215 { return find_last_not_of(wxUniChar(ch
), nStart
); }
2218 wxString
& operator+=(const wxString
& s
)
2219 { m_impl
+= s
.m_impl
; return *this; }
2220 // string += C string
2221 wxString
& operator+=(const char *psz
)
2222 { m_impl
+= ImplStr(psz
); return *this; }
2223 wxString
& operator+=(const wchar_t *pwz
)
2224 { m_impl
+= ImplStr(pwz
); return *this; }
2225 wxString
& operator+=(const wxCStrData
& s
)
2226 { m_impl
+= s
.AsString().m_impl
; return *this; }
2228 wxString
& operator+=(wxUniChar ch
)
2229 { m_impl
+= wxStringOperations::EncodeChar(ch
); return *this; }
2230 wxString
& operator+=(wxUniCharRef ch
) { return *this += wxUniChar(ch
); }
2231 wxString
& operator+=(int ch
) { return *this += wxUniChar(ch
); }
2232 wxString
& operator+=(char ch
) { return *this += wxUniChar(ch
); }
2233 wxString
& operator+=(unsigned char ch
) { return *this += wxUniChar(ch
); }
2234 wxString
& operator+=(wchar_t ch
) { return *this += wxUniChar(ch
); }
2237 #if !wxUSE_STL_BASED_WXSTRING
2238 // helpers for wxStringBuffer and wxStringBufferLength
2239 wxStringCharType
*DoGetWriteBuf(size_t nLen
)
2240 { return m_impl
.DoGetWriteBuf(nLen
); }
2241 void DoUngetWriteBuf()
2242 { m_impl
.DoUngetWriteBuf(); }
2243 void DoUngetWriteBuf(size_t nLen
)
2244 { m_impl
.DoUngetWriteBuf(nLen
); }
2245 #endif // !wxUSE_STL_BASED_WXSTRING
2247 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2248 int DoPrintf(const wxString
& format
, ...);
2249 static wxString
DoFormat(const wxString
& format
, ...);
2252 #if !wxUSE_STL_BASED_WXSTRING
2253 // check string's data validity
2254 bool IsValid() const { return m_impl
.GetStringData()->IsValid(); }
2258 wxStringImpl m_impl
;
2260 // buffers for compatibility conversion from (char*)c_str() and
2261 // (wchar_t*)c_str():
2262 // FIXME-UTF8: bechmark various approaches to keeping compatibility buffers
2263 template<typename T
>
2264 struct ConvertedBuffer
2266 ConvertedBuffer() : m_buf(NULL
) {}
2270 operator T
*() const { return m_buf
; }
2272 ConvertedBuffer
& operator=(T
*str
)
2282 ConvertedBuffer
<char> m_convertedToChar
;
2284 #if !wxUSE_UNICODE_WCHAR
2285 ConvertedBuffer
<wchar_t> m_convertedToWChar
;
2288 friend class WXDLLIMPEXP_BASE wxCStrData
;
2289 friend class wxImplStringBuffer
;
2290 friend class wxImplStringBufferLength
;
2293 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
2294 #pragma warning (default:4275)
2297 // string iterator operators that satisfy STL Random Access Iterator
2299 inline wxString::iterator
operator+(int n
, wxString::iterator i
)
2301 inline wxString::iterator
operator+(size_t n
, wxString::iterator i
)
2303 inline wxString::const_iterator
operator+(int n
, wxString::const_iterator i
)
2305 inline wxString::const_iterator
operator+(size_t n
, wxString::const_iterator i
)
2307 inline wxString::reverse_iterator
operator+(int n
, wxString::reverse_iterator i
)
2309 inline wxString::reverse_iterator
operator+(size_t n
, wxString::reverse_iterator i
)
2311 inline wxString::const_reverse_iterator
operator+(int n
, wxString::const_reverse_iterator i
)
2313 inline wxString::const_reverse_iterator
operator+(size_t n
, wxString::const_reverse_iterator i
)
2316 // notice that even though for many compilers the friend declarations above are
2317 // enough, from the point of view of C++ standard we must have the declarations
2318 // here as friend ones are not injected in the enclosing namespace and without
2319 // them the code fails to compile with conforming compilers such as xlC or g++4
2320 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
, const wxString
& string2
);
2321 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const char *psz
);
2322 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const wchar_t *pwz
);
2323 wxString WXDLLIMPEXP_BASE
operator+(const char *psz
, const wxString
& string
);
2324 wxString WXDLLIMPEXP_BASE
operator+(const wchar_t *pwz
, const wxString
& string
);
2326 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
2327 wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
2329 inline wxString
operator+(const wxString
& string
, wxUniCharRef ch
)
2330 { return string
+ (wxUniChar
)ch
; }
2331 inline wxString
operator+(const wxString
& string
, char ch
)
2332 { return string
+ wxUniChar(ch
); }
2333 inline wxString
operator+(const wxString
& string
, wchar_t ch
)
2334 { return string
+ wxUniChar(ch
); }
2335 inline wxString
operator+(wxUniCharRef ch
, const wxString
& string
)
2336 { return (wxUniChar
)ch
+ string
; }
2337 inline wxString
operator+(char ch
, const wxString
& string
)
2338 { return wxUniChar(ch
) + string
; }
2339 inline wxString
operator+(wchar_t ch
, const wxString
& string
)
2340 { return wxUniChar(ch
) + string
; }
2343 #if wxUSE_STL_BASED_WXSTRING
2344 // return an empty wxString (not very useful with wxUSE_STL == 1)
2345 inline const wxString
wxGetEmptyString() { return wxString(); }
2346 #else // !wxUSE_STL_BASED_WXSTRING
2347 // return an empty wxString (more efficient than wxString() here)
2348 inline const wxString
& wxGetEmptyString()
2350 return *(wxString
*)&wxEmptyString
;
2352 #endif // wxUSE_STL_BASED_WXSTRING/!wxUSE_STL_BASED_WXSTRING
2354 // ----------------------------------------------------------------------------
2355 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
2356 // ----------------------------------------------------------------------------
2358 #if !wxUSE_STL_BASED_WXSTRING
2359 // string buffer for direct access to string data in their native
2361 class wxImplStringBuffer
2364 typedef wxStringCharType CharType
;
2366 wxImplStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
2367 : m_str(str
), m_buf(NULL
)
2368 { m_buf
= m_str
.DoGetWriteBuf(lenWanted
); }
2370 ~wxImplStringBuffer() { m_str
.DoUngetWriteBuf(); }
2372 operator wxStringCharType
*() const { return m_buf
; }
2376 wxStringCharType
*m_buf
;
2378 DECLARE_NO_COPY_CLASS(wxImplStringBuffer
)
2381 class wxImplStringBufferLength
2384 typedef wxStringCharType CharType
;
2386 wxImplStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
2387 : m_str(str
), m_buf(NULL
), m_len(0), m_lenSet(false)
2389 m_buf
= m_str
.DoGetWriteBuf(lenWanted
);
2390 wxASSERT(m_buf
!= NULL
);
2393 ~wxImplStringBufferLength()
2396 m_str
.DoUngetWriteBuf(m_len
);
2399 operator wxStringCharType
*() const { return m_buf
; }
2400 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
2404 wxStringCharType
*m_buf
;
2408 DECLARE_NO_COPY_CLASS(wxImplStringBufferLength
)
2411 #endif // !wxUSE_STL_BASED_WXSTRING
2413 template<typename T
>
2414 class wxStringTypeBufferBase
2419 wxStringTypeBufferBase(wxString
& str
, size_t lenWanted
= 1024)
2420 : m_str(str
), m_buf(lenWanted
)
2424 operator CharType
*() { return m_buf
.data(); }
2428 wxCharTypeBuffer
<CharType
> m_buf
;
2431 template<typename T
>
2432 class wxStringTypeBufferLengthBase
2437 wxStringTypeBufferLengthBase(wxString
& str
, size_t lenWanted
= 1024)
2438 : m_str(str
), m_buf(lenWanted
), m_len(0), m_lenSet(false)
2441 ~wxStringTypeBufferLengthBase()
2444 m_str
.assign(m_buf
.data(), m_len
);
2447 operator CharType
*() { return m_buf
.data(); }
2448 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
2452 wxCharTypeBuffer
<CharType
> m_buf
;
2457 template<typename T
>
2458 class wxStringTypeBuffer
: public wxStringTypeBufferBase
<T
>
2461 wxStringTypeBuffer(wxString
& str
, size_t lenWanted
= 1024)
2462 : wxStringTypeBufferBase
<T
>(str
, lenWanted
) {}
2463 ~wxStringTypeBuffer()
2465 this->m_str
.assign(this->m_buf
.data());
2468 DECLARE_NO_COPY_CLASS(wxStringTypeBuffer
)
2471 template<typename T
>
2472 class wxStringTypeBufferLength
: public wxStringTypeBufferLengthBase
<T
>
2475 wxStringTypeBufferLength(wxString
& str
, size_t lenWanted
= 1024)
2476 : wxStringTypeBufferLengthBase
<T
>(str
, lenWanted
) {}
2478 ~wxStringTypeBufferLength()
2480 wxASSERT(this->m_lenSet
);
2481 this->m_str
.assign(this->m_buf
.data(), this->m_len
);
2484 DECLARE_NO_COPY_CLASS(wxStringTypeBufferLength
)
2487 #if wxUSE_STL_BASED_WXSTRING
2488 class wxImplStringBuffer
: public wxStringTypeBufferBase
<wxStringCharType
>
2491 wxImplStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
2492 : wxStringTypeBufferBase
<wxStringCharType
>(str
, lenWanted
) {}
2493 ~wxImplStringBuffer()
2494 { m_str
.m_impl
.assign(m_buf
.data()); }
2496 DECLARE_NO_COPY_CLASS(wxImplStringBuffer
)
2499 class wxImplStringBufferLength
: public wxStringTypeBufferLengthBase
<wxStringCharType
>
2502 wxImplStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
2503 : wxStringTypeBufferLengthBase
<wxStringCharType
>(str
, lenWanted
) {}
2505 ~wxImplStringBufferLength()
2508 m_str
.m_impl
.assign(m_buf
.data(), m_len
);
2511 DECLARE_NO_COPY_CLASS(wxImplStringBufferLength
)
2513 #endif // wxUSE_STL_BASED_WXSTRING
2516 #if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
2517 typedef wxStringTypeBuffer
<wxChar
> wxStringBuffer
;
2518 typedef wxStringTypeBufferLength
<wxChar
> wxStringBufferLength
;
2519 #else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2520 typedef wxImplStringBuffer wxStringBuffer
;
2521 typedef wxImplStringBufferLength wxStringBufferLength
;
2522 #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2524 // ---------------------------------------------------------------------------
2525 // wxString comparison functions: operator versions are always case sensitive
2526 // ---------------------------------------------------------------------------
2528 #define wxCMP_WXCHAR_STRING(p, s, op) 0 op s.Cmp(p)
2530 wxDEFINE_ALL_COMPARISONS(const wxChar
*, const wxString
&, wxCMP_WXCHAR_STRING
)
2532 #undef wxCMP_WXCHAR_STRING
2534 // note that there is an optimization in operator==() and !=(): we (quickly)
2535 // checks the strings length first, before comparing their data
2536 inline bool operator==(const wxString
& s1
, const wxString
& s2
)
2537 { return (s1
.Len() == s2
.Len()) && (s1
.Cmp(s2
) == 0); }
2538 inline bool operator!=(const wxString
& s1
, const wxString
& s2
)
2539 { return (s1
.Len() != s2
.Len()) || (s1
.Cmp(s2
) != 0); }
2540 inline bool operator< (const wxString
& s1
, const wxString
& s2
)
2541 { return s1
.Cmp(s2
) < 0; }
2542 inline bool operator> (const wxString
& s1
, const wxString
& s2
)
2543 { return s1
.Cmp(s2
) > 0; }
2544 inline bool operator<=(const wxString
& s1
, const wxString
& s2
)
2545 { return s1
.Cmp(s2
) <= 0; }
2546 inline bool operator>=(const wxString
& s1
, const wxString
& s2
)
2547 { return s1
.Cmp(s2
) >= 0; }
2550 inline bool operator==(const wxString
& s1
, const wxWCharBuffer
& s2
)
2551 { return (s1
.Cmp((const wchar_t *)s2
) == 0); }
2552 inline bool operator==(const wxWCharBuffer
& s1
, const wxString
& s2
)
2553 { return (s2
.Cmp((const wchar_t *)s1
) == 0); }
2554 inline bool operator!=(const wxString
& s1
, const wxWCharBuffer
& s2
)
2555 { return (s1
.Cmp((const wchar_t *)s2
) != 0); }
2556 inline bool operator!=(const wxWCharBuffer
& s1
, const wxString
& s2
)
2557 { return (s2
.Cmp((const wchar_t *)s1
) != 0); }
2558 #else // !wxUSE_UNICODE
2559 inline bool operator==(const wxString
& s1
, const wxCharBuffer
& s2
)
2560 { return (s1
.Cmp((const char *)s2
) == 0); }
2561 inline bool operator==(const wxCharBuffer
& s1
, const wxString
& s2
)
2562 { return (s2
.Cmp((const char *)s1
) == 0); }
2563 inline bool operator!=(const wxString
& s1
, const wxCharBuffer
& s2
)
2564 { return (s1
.Cmp((const char *)s2
) != 0); }
2565 inline bool operator!=(const wxCharBuffer
& s1
, const wxString
& s2
)
2566 { return (s2
.Cmp((const char *)s1
) != 0); }
2567 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
2570 inline wxString
operator+(const wxString
& string
, const wxWCharBuffer
& buf
)
2571 { return string
+ (const wchar_t *)buf
; }
2572 inline wxString
operator+(const wxWCharBuffer
& buf
, const wxString
& string
)
2573 { return (const wchar_t *)buf
+ string
; }
2574 #else // !wxUSE_UNICODE
2575 inline wxString
operator+(const wxString
& string
, const wxCharBuffer
& buf
)
2576 { return string
+ (const char *)buf
; }
2577 inline wxString
operator+(const wxCharBuffer
& buf
, const wxString
& string
)
2578 { return (const char *)buf
+ string
; }
2579 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
2581 // comparison with char
2582 inline bool operator==(const wxUniChar
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2583 inline bool operator==(const wxUniCharRef
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2584 inline bool operator==(char c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2585 inline bool operator==(wchar_t c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2586 inline bool operator==(int c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2587 inline bool operator==(const wxString
& s
, const wxUniChar
& c
) { return s
.IsSameAs(c
); }
2588 inline bool operator==(const wxString
& s
, const wxUniCharRef
& c
) { return s
.IsSameAs(c
); }
2589 inline bool operator==(const wxString
& s
, char c
) { return s
.IsSameAs(c
); }
2590 inline bool operator==(const wxString
& s
, wchar_t c
) { return s
.IsSameAs(c
); }
2591 inline bool operator!=(const wxUniChar
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2592 inline bool operator!=(const wxUniCharRef
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2593 inline bool operator!=(char c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2594 inline bool operator!=(wchar_t c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2595 inline bool operator!=(int c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2596 inline bool operator!=(const wxString
& s
, const wxUniChar
& c
) { return !s
.IsSameAs(c
); }
2597 inline bool operator!=(const wxString
& s
, const wxUniCharRef
& c
) { return !s
.IsSameAs(c
); }
2598 inline bool operator!=(const wxString
& s
, char c
) { return !s
.IsSameAs(c
); }
2599 inline bool operator!=(const wxString
& s
, wchar_t c
) { return !s
.IsSameAs(c
); }
2601 // comparison with C string in Unicode build
2604 #define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s
2606 wxDEFINE_ALL_COMPARISONS(const char *, const wxString
&, wxCMP_CHAR_STRING
)
2608 #undef wxCMP_CHAR_STRING
2610 #endif // wxUSE_UNICODE
2612 // we also need to provide the operators for comparison with wxCStrData to
2613 // resolve ambiguity between operator(const wxChar *,const wxString &) and
2614 // operator(const wxChar *, const wxChar *) for "p == s.c_str()"
2616 // notice that these are (shallow) pointer comparisons, not (deep) string ones
2617 #define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar()
2618 #define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar()
2620 wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData
&, wxCMP_WCHAR_CSTRDATA
)
2621 wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData
&, wxCMP_CHAR_CSTRDATA
)
2623 #undef wxCMP_CHAR_CSTRDATA
2624 #undef wxCMP_WCHAR_CSTRDATA
2626 // ---------------------------------------------------------------------------
2627 // Implementation only from here until the end of file
2628 // ---------------------------------------------------------------------------
2630 #if wxUSE_STD_IOSTREAM
2632 #include "wx/iosfwrap.h"
2634 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxString
&);
2635 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxCStrData
&);
2636 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxCharBuffer
&);
2637 #ifndef __BORLANDC__
2638 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxWCharBuffer
&);
2641 #endif // wxSTD_STRING_COMPATIBILITY
2643 // ---------------------------------------------------------------------------
2644 // wxCStrData implementation
2645 // ---------------------------------------------------------------------------
2647 inline wxCStrData::wxCStrData(char *buf
)
2648 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
2649 inline wxCStrData::wxCStrData(wchar_t *buf
)
2650 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
2652 inline wxCStrData::~wxCStrData()
2658 // simple cases for AsChar() and AsWChar(), the complicated ones are
2660 #if wxUSE_UNICODE_WCHAR
2661 inline const wchar_t* wxCStrData::AsWChar() const
2663 return m_str
->wx_str() + m_offset
;
2665 #endif // wxUSE_UNICODE_WCHAR
2668 inline const char* wxCStrData::AsChar() const
2670 return m_str
->wx_str() + m_offset
;
2672 #endif // !wxUSE_UNICODE
2674 inline const wxCharBuffer
wxCStrData::AsCharBuf() const
2677 return wxCharBuffer::CreateNonOwned(AsChar());
2679 return AsString().mb_str();
2683 inline const wxWCharBuffer
wxCStrData::AsWCharBuf() const
2685 #if wxUSE_UNICODE_WCHAR
2686 return wxWCharBuffer::CreateNonOwned(AsWChar());
2688 return AsString().wc_str();
2692 inline wxString
wxCStrData::AsString() const
2694 if ( m_offset
== 0 )
2697 return m_str
->Mid(m_offset
);
2700 inline const wxStringCharType
*wxCStrData::AsInternal() const
2702 return wxStringOperations::AddToIter(m_str
->wx_str(), m_offset
);
2705 inline wxUniChar
wxCStrData::operator*() const
2707 if ( m_str
->empty() )
2708 return wxUniChar(_T('\0'));
2710 return (*m_str
)[m_offset
];
2713 inline wxUniChar
wxCStrData::operator[](size_t n
) const
2715 // NB: we intentionally use operator[] and not at() here because the former
2716 // works for the terminating NUL while the latter does not
2717 return (*m_str
)[m_offset
+ n
];
2720 // ----------------------------------------------------------------------------
2721 // more wxCStrData operators
2722 // ----------------------------------------------------------------------------
2724 // we need to define those to allow "size_t pos = p - s.c_str()" where p is
2725 // some pointer into the string
2726 inline size_t operator-(const char *p
, const wxCStrData
& cs
)
2728 return p
- cs
.AsChar();
2731 inline size_t operator-(const wchar_t *p
, const wxCStrData
& cs
)
2733 return p
- cs
.AsWChar();
2736 // ----------------------------------------------------------------------------
2737 // implementation of wx[W]CharBuffer inline methods using wxCStrData
2738 // ----------------------------------------------------------------------------
2740 // FIXME-UTF8: move this to buffer.h
2741 inline wxCharBuffer::wxCharBuffer(const wxCStrData
& cstr
)
2742 : wxCharTypeBufferBase(cstr
.AsCharBuf())
2746 inline wxWCharBuffer::wxWCharBuffer(const wxCStrData
& cstr
)
2747 : wxCharTypeBufferBase(cstr
.AsWCharBuf())
2751 #endif // _WX_WXSTRING_H_