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
28 #if defined(__WXMAC__) || defined(__VISAGECPP__)
32 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
33 // problem in VACPP V4 with including stdlib.h multiple times
34 // strconv includes it anyway
47 #ifdef HAVE_STRCASECMP_IN_STRINGS_H
48 #include <strings.h> // for strcasecmp()
49 #endif // HAVE_STRCASECMP_IN_STRINGS_H
50 #endif // ! __WXPALMOS5__
52 #include "wx/wxcrtbase.h" // for wxChar, wxStrlen() etc.
53 #include "wx/strvararg.h"
54 #include "wx/buffer.h" // for wxCharBuffer
55 #include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes
56 #include "wx/stringimpl.h"
57 #include "wx/stringops.h"
58 #include "wx/unichar.h"
60 class WXDLLIMPEXP_FWD_BASE wxString
;
62 // unless this symbol is predefined to disable the compatibility functions, do
64 #ifndef WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
65 #define WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER 1
70 template <typename T
> struct wxStringAsBufHelper
;
73 // ---------------------------------------------------------------------------
75 // ---------------------------------------------------------------------------
77 // casts [unfortunately!] needed to call some broken functions which require
78 // "char *" instead of "const char *"
79 #define WXSTRINGCAST (wxChar *)(const wxChar *)
80 #define wxCSTRINGCAST (wxChar *)(const wxChar *)
81 #define wxMBSTRINGCAST (char *)(const char *)
82 #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *)
84 // ----------------------------------------------------------------------------
86 // ----------------------------------------------------------------------------
88 #if WXWIN_COMPATIBILITY_2_6
90 // deprecated in favour of wxString::npos, don't use in new code
92 // maximum possible length for a string means "take all string" everywhere
93 #define wxSTRING_MAXLEN wxString::npos
95 #endif // WXWIN_COMPATIBILITY_2_6
97 // ---------------------------------------------------------------------------
98 // global functions complementing standard C string library replacements for
99 // strlen() and portable strcasecmp()
100 //---------------------------------------------------------------------------
102 #if WXWIN_COMPATIBILITY_2_8
103 // Use wxXXX() functions from wxcrt.h instead! These functions are for
104 // backwards compatibility only.
106 // checks whether the passed in pointer is NULL and if the string is empty
107 wxDEPRECATED( inline bool IsEmpty(const char *p
) );
108 inline bool IsEmpty(const char *p
) { return (!p
|| !*p
); }
110 // safe version of strlen() (returns 0 if passed NULL pointer)
111 wxDEPRECATED( inline size_t Strlen(const char *psz
) );
112 inline size_t Strlen(const char *psz
)
113 { return psz
? strlen(psz
) : 0; }
115 // portable strcasecmp/_stricmp
116 wxDEPRECATED( inline int Stricmp(const char *psz1
, const char *psz2
) );
117 inline int Stricmp(const char *psz1
, const char *psz2
)
119 #if defined(__VISUALC__) && defined(__WXWINCE__)
120 register char c1
, c2
;
122 c1
= tolower(*psz1
++);
123 c2
= tolower(*psz2
++);
124 } while ( c1
&& (c1
== c2
) );
127 #elif defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) )
128 return _stricmp(psz1
, psz2
);
129 #elif defined(__SC__)
130 return _stricmp(psz1
, psz2
);
131 #elif defined(__BORLANDC__)
132 return stricmp(psz1
, psz2
);
133 #elif defined(__WATCOMC__)
134 return stricmp(psz1
, psz2
);
135 #elif defined(__DJGPP__)
136 return stricmp(psz1
, psz2
);
137 #elif defined(__EMX__)
138 return stricmp(psz1
, psz2
);
139 #elif defined(__WXPM__)
140 return stricmp(psz1
, psz2
);
141 #elif defined(__WXPALMOS__) || \
142 defined(HAVE_STRCASECMP_IN_STRING_H) || \
143 defined(HAVE_STRCASECMP_IN_STRINGS_H) || \
144 defined(__GNUWIN32__)
145 return strcasecmp(psz1
, psz2
);
146 #elif defined(__MWERKS__) && !defined(__INTEL__)
147 register char c1
, c2
;
149 c1
= tolower(*psz1
++);
150 c2
= tolower(*psz2
++);
151 } while ( c1
&& (c1
== c2
) );
155 // almost all compilers/libraries provide this function (unfortunately under
156 // different names), that's why we don't implement our own which will surely
157 // be more efficient than this code (uncomment to use):
159 register char c1, c2;
161 c1 = tolower(*psz1++);
162 c2 = tolower(*psz2++);
163 } while ( c1 && (c1 == c2) );
168 #error "Please define string case-insensitive compare for your OS/compiler"
169 #endif // OS/compiler
172 #endif // WXWIN_COMPATIBILITY_2_8
174 // ----------------------------------------------------------------------------
176 // ----------------------------------------------------------------------------
178 // Lightweight object returned by wxString::c_str() and implicitly convertible
179 // to either const char* or const wchar_t*.
180 class WXDLLIMPEXP_BASE wxCStrData
183 // Ctors; for internal use by wxString and wxCStrData only
184 wxCStrData(const wxString
*str
, size_t offset
= 0, bool owned
= false)
185 : m_str(str
), m_offset(offset
), m_owned(owned
) {}
188 // Ctor constructs the object from char literal; they are needed to make
189 // operator?: compile and they intentionally take char*, not const char*
190 inline wxCStrData(char *buf
);
191 inline wxCStrData(wchar_t *buf
);
192 inline wxCStrData(const wxCStrData
& data
);
194 inline ~wxCStrData();
196 // methods defined inline below must be declared inline or mingw32 3.4.5
197 // warns about "<symbol> defined locally after being referenced with
198 // dllimport linkage"
199 #if wxUSE_UNICODE_WCHAR
202 const wchar_t* AsWChar() const;
203 operator const wchar_t*() const { return AsWChar(); }
205 #if !wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY
208 const char* AsChar() const;
209 const unsigned char* AsUnsignedChar() const
210 { return (const unsigned char *) AsChar(); }
211 operator const char*() const { return AsChar(); }
212 operator const unsigned char*() const { return AsUnsignedChar(); }
214 operator const void*() const { return AsChar(); }
216 inline const wxCharBuffer
AsCharBuf() const;
217 inline const wxWCharBuffer
AsWCharBuf() const;
219 inline wxString
AsString() const;
221 // returns the value as C string in internal representation (equivalent
222 // to AsString().wx_str(), but more efficient)
223 const wxStringCharType
*AsInternal() const;
225 // allow expressions like "c_str()[0]":
226 inline wxUniChar
operator[](size_t n
) const;
227 wxUniChar
operator[](int n
) const { return operator[](size_t(n
)); }
228 wxUniChar
operator[](long n
) const { return operator[](size_t(n
)); }
229 #ifndef wxSIZE_T_IS_UINT
230 wxUniChar
operator[](unsigned int n
) const { return operator[](size_t(n
)); }
231 #endif // size_t != unsigned int
233 // these operators are needed to emulate the pointer semantics of c_str():
234 // expressions like "wxChar *p = str.c_str() + 1;" should continue to work
235 // (we need both versions to resolve ambiguities):
236 wxCStrData
operator+(int n
) const
237 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
238 wxCStrData
operator+(long n
) const
239 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
240 wxCStrData
operator+(size_t n
) const
241 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
243 // and these for "str.c_str() + (p2 - p1)" (it also works for any integer
244 // expression but it must be ptrdiff_t and not e.g. int to work in this
246 wxCStrData
operator-(ptrdiff_t n
) const
248 wxASSERT_MSG( n
<= (ptrdiff_t)m_offset
,
249 _T("attempt to construct address before the beginning of the string") );
250 return wxCStrData(m_str
, m_offset
- n
, m_owned
);
253 // this operator is needed to make expressions like "*c_str()" or
254 // "*(c_str() + 2)" work
255 inline wxUniChar
operator*() const;
258 const wxString
*m_str
;
262 friend class WXDLLIMPEXP_FWD_BASE wxString
;
265 // ----------------------------------------------------------------------------
266 // wxStringPrintfMixin
267 // ---------------------------------------------------------------------------
269 // NB: VC6 has a bug that causes linker errors if you have template methods
270 // in a class using __declspec(dllimport). The solution is to split such
271 // class into two classes, one that contains the template methods and does
272 // *not* use WXDLLIMPEXP_BASE and another class that contains the rest
273 // (with DLL linkage).
275 // We only do this for VC6 here, because the code is less efficient
276 // (Printf() has to use dynamic_cast<>) and because OpenWatcom compiler
277 // cannot compile this code.
279 #if defined(__VISUALC__) && __VISUALC__ < 1300
280 #define wxNEEDS_WXSTRING_PRINTF_MIXIN
283 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
284 // this class contains implementation of wxString's vararg methods, it's
285 // exported from wxBase DLL
286 class WXDLLIMPEXP_BASE wxStringPrintfMixinBase
289 wxStringPrintfMixinBase() {}
291 #if !wxUSE_UTF8_LOCALE_ONLY
292 int DoPrintfWchar(const wxChar
*format
, ...);
293 static wxString
DoFormatWchar(const wxChar
*format
, ...);
295 #if wxUSE_UNICODE_UTF8
296 int DoPrintfUtf8(const char *format
, ...);
297 static wxString
DoFormatUtf8(const char *format
, ...);
301 // this class contains template wrappers for wxString's vararg methods, it's
302 // intentionally *not* exported from the DLL in order to fix the VC6 bug
304 class wxStringPrintfMixin
: public wxStringPrintfMixinBase
307 // to further complicate things, we can't return wxString from
308 // wxStringPrintfMixin::Format() because wxString is not yet declared at
309 // this point; the solution is to use this fake type trait template - this
310 // way the compiler won't know the return type until Format() is used
311 // (this doesn't compile with Watcom, but VC6 compiles it just fine):
312 template<typename T
> struct StringReturnType
314 typedef wxString type
;
318 // these are duplicated wxString methods, they're also declared below
319 // if !wxNEEDS_WXSTRING_PRINTF_MIXIN:
321 // static wxString Format(const wString& format, ...) ATTRIBUTE_PRINTF_1;
322 WX_DEFINE_VARARG_FUNC_SANS_N0(static typename StringReturnType
<T1
>::type
,
323 Format
, 1, (const wxFormatString
&),
324 DoFormatWchar
, DoFormatUtf8
)
325 // We have to implement the version without template arguments manually
326 // because of the StringReturnType<> hack, although WX_DEFINE_VARARG_FUNC
327 // normally does it itself. It has to be a template so that we can use
328 // the hack, even though there's no real template parameter. We can't move
329 // it to wxStrig, because it would shadow these versions of Format() then.
331 inline static typename StringReturnType
<T
>::type
334 // NB: this doesn't compile if T is not (some form of) a string;
335 // this makes Format's prototype equivalent to
336 // Format(const wxFormatString& fmt)
337 return DoFormatWchar(wxFormatString(fmt
));
340 // int Printf(const wxString& format, ...);
341 WX_DEFINE_VARARG_FUNC(int, Printf
, 1, (const wxFormatString
&),
342 DoPrintfWchar
, DoPrintfUtf8
)
343 // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2;
344 WX_DEFINE_VARARG_FUNC(int, sprintf
, 1, (const wxFormatString
&),
345 DoPrintfWchar
, DoPrintfUtf8
)
348 wxStringPrintfMixin() : wxStringPrintfMixinBase() {}
350 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
353 // ----------------------------------------------------------------------------
354 // wxString: string class trying to be compatible with std::string, MFC
355 // CString and wxWindows 1.x wxString all at once
356 // ---------------------------------------------------------------------------
358 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
359 // "non dll-interface class 'wxStringPrintfMixin' used as base interface
360 // for dll-interface class 'wxString'" -- this is OK in our case
361 #pragma warning (disable:4275)
364 #if wxUSE_UNICODE_UTF8
365 // see the comment near wxString::iterator for why we need this
366 class WXDLLIMPEXP_BASE wxStringIteratorNode
369 wxStringIteratorNode()
370 : m_str(NULL
), m_citer(NULL
), m_iter(NULL
), m_prev(NULL
), m_next(NULL
) {}
371 wxStringIteratorNode(const wxString
*str
,
372 wxStringImpl::const_iterator
*citer
)
373 { DoSet(str
, citer
, NULL
); }
374 wxStringIteratorNode(const wxString
*str
, wxStringImpl::iterator
*iter
)
375 { DoSet(str
, NULL
, iter
); }
376 ~wxStringIteratorNode()
379 inline void set(const wxString
*str
, wxStringImpl::const_iterator
*citer
)
380 { clear(); DoSet(str
, citer
, NULL
); }
381 inline void set(const wxString
*str
, wxStringImpl::iterator
*iter
)
382 { clear(); DoSet(str
, NULL
, iter
); }
384 const wxString
*m_str
;
385 wxStringImpl::const_iterator
*m_citer
;
386 wxStringImpl::iterator
*m_iter
;
387 wxStringIteratorNode
*m_prev
, *m_next
;
391 inline void DoSet(const wxString
*str
,
392 wxStringImpl::const_iterator
*citer
,
393 wxStringImpl::iterator
*iter
);
395 // the node belongs to a particular iterator instance, it's not copied
396 // when a copy of the iterator is made
397 DECLARE_NO_COPY_CLASS(wxStringIteratorNode
)
399 #endif // wxUSE_UNICODE_UTF8
401 class WXDLLIMPEXP_BASE wxString
402 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
403 : public wxStringPrintfMixin
406 // NB: special care was taken in arranging the member functions in such order
407 // that all inline functions can be effectively inlined, verify that all
408 // performance critical functions are still inlined if you change order!
410 // an 'invalid' value for string index, moved to this place due to a CW bug
411 static const size_t npos
;
414 // if we hadn't made these operators private, it would be possible to
415 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
416 // converted to char in C and we do have operator=(char)
418 // NB: we don't need other versions (short/long and unsigned) as attempt
419 // to assign another numeric type to wxString will now result in
420 // ambiguity between operator=(char) and operator=(int)
421 wxString
& operator=(int);
423 // these methods are not implemented - there is _no_ conversion from int to
424 // string, you're doing something wrong if the compiler wants to call it!
426 // try `s << i' or `s.Printf("%d", i)' instead
430 // buffer for holding temporary substring when using any of the methods
431 // that take (char*,size_t) or (wchar_t*,size_t) arguments:
433 struct SubstrBufFromType
438 SubstrBufFromType(const T
& data_
, size_t len_
)
439 : data(data_
), len(len_
)
441 wxASSERT_MSG( len
!= npos
, "must have real length" );
445 #if wxUSE_UNICODE_UTF8
446 // even char* -> char* needs conversion, from locale charset to UTF-8
447 typedef SubstrBufFromType
<wxCharBuffer
> SubstrBufFromWC
;
448 typedef SubstrBufFromType
<wxCharBuffer
> SubstrBufFromMB
;
449 #elif wxUSE_UNICODE_WCHAR
450 typedef SubstrBufFromType
<const wchar_t*> SubstrBufFromWC
;
451 typedef SubstrBufFromType
<wxWCharBuffer
> SubstrBufFromMB
;
453 typedef SubstrBufFromType
<const char*> SubstrBufFromMB
;
454 typedef SubstrBufFromType
<wxCharBuffer
> SubstrBufFromWC
;
458 // Functions implementing primitive operations on string data; wxString
459 // methods and iterators are implemented in terms of it. The differences
460 // between UTF-8 and wchar_t* representations of the string are mostly
463 #if wxUSE_UNICODE_UTF8
464 static SubstrBufFromMB
ConvertStr(const char *psz
, size_t nLength
,
465 const wxMBConv
& conv
);
466 static SubstrBufFromWC
ConvertStr(const wchar_t *pwz
, size_t nLength
,
467 const wxMBConv
& conv
);
468 #elif wxUSE_UNICODE_WCHAR
469 static SubstrBufFromMB
ConvertStr(const char *psz
, size_t nLength
,
470 const wxMBConv
& conv
);
472 static SubstrBufFromWC
ConvertStr(const wchar_t *pwz
, size_t nLength
,
473 const wxMBConv
& conv
);
476 #if !wxUSE_UNICODE_UTF8 // wxUSE_UNICODE_WCHAR or !wxUSE_UNICODE
477 // returns C string encoded as the implementation expects:
479 static const wchar_t* ImplStr(const wchar_t* str
)
480 { return str
? str
: wxT(""); }
481 static const SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
482 { return SubstrBufFromWC(str
, (str
&& n
== npos
) ? wxWcslen(str
) : n
); }
483 static wxWCharBuffer
ImplStr(const char* str
,
484 const wxMBConv
& conv
= wxConvLibc
)
485 { return ConvertStr(str
, npos
, conv
).data
; }
486 static SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
487 const wxMBConv
& conv
= wxConvLibc
)
488 { return ConvertStr(str
, n
, conv
); }
490 static const char* ImplStr(const char* str
,
491 const wxMBConv
& WXUNUSED(conv
) = wxConvLibc
)
492 { return str
? str
: ""; }
493 static const SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
494 const wxMBConv
& WXUNUSED(conv
) = wxConvLibc
)
495 { return SubstrBufFromMB(str
, (str
&& n
== npos
) ? wxStrlen(str
) : n
); }
496 static wxCharBuffer
ImplStr(const wchar_t* str
)
497 { return ConvertStr(str
, npos
, wxConvLibc
).data
; }
498 static SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
499 { return ConvertStr(str
, n
, wxConvLibc
); }
502 // translates position index in wxString to/from index in underlying
504 static size_t PosToImpl(size_t pos
) { return pos
; }
505 static void PosLenToImpl(size_t pos
, size_t len
,
506 size_t *implPos
, size_t *implLen
)
507 { *implPos
= pos
; *implLen
= len
; }
508 static size_t LenToImpl(size_t len
) { return len
; }
509 static size_t PosFromImpl(size_t pos
) { return pos
; }
511 #else // wxUSE_UNICODE_UTF8
513 static wxCharBuffer
ImplStr(const char* str
,
514 const wxMBConv
& conv
= wxConvLibc
)
515 { return ConvertStr(str
, npos
, conv
).data
; }
516 static SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
517 const wxMBConv
& conv
= wxConvLibc
)
518 { return ConvertStr(str
, n
, conv
); }
520 static wxCharBuffer
ImplStr(const wchar_t* str
)
521 { return ConvertStr(str
, npos
, wxMBConvUTF8()).data
; }
522 static SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
523 { return ConvertStr(str
, n
, wxMBConvUTF8()); }
525 size_t PosToImpl(size_t pos
) const
527 if ( pos
== 0 || pos
== npos
)
530 return (begin() + pos
).impl() - m_impl
.begin();
533 void PosLenToImpl(size_t pos
, size_t len
, size_t *implPos
, size_t *implLen
) const;
535 size_t LenToImpl(size_t len
) const
538 PosLenToImpl(0, len
, &pos
, &len2
);
542 size_t PosFromImpl(size_t pos
) const
544 if ( pos
== 0 || pos
== npos
)
547 return const_iterator(this, m_impl
.begin() + pos
) - begin();
549 #endif // !wxUSE_UNICODE_UTF8/wxUSE_UNICODE_UTF8
553 typedef wxUniChar value_type
;
554 typedef wxUniChar char_type
;
555 typedef wxUniCharRef reference
;
556 typedef wxChar
* pointer
;
557 typedef const wxChar
* const_pointer
;
559 typedef size_t size_type
;
560 typedef wxUniChar const_reference
;
563 #if wxUSE_UNICODE_UTF8
564 // random access is not O(1), as required by Random Access Iterator
565 #define WX_STR_ITERATOR_TAG std::bidirectional_iterator_tag
567 #define WX_STR_ITERATOR_TAG std::random_access_iterator_tag
570 #define WX_STR_ITERATOR_TAG void /* dummy type */
573 #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, reference_type) \
575 typedef wxStringImpl::iterator_name underlying_iterator; \
577 typedef WX_STR_ITERATOR_TAG iterator_category; \
578 typedef wxUniChar value_type; \
579 typedef int difference_type; \
580 typedef reference_type reference; \
581 typedef pointer_type pointer; \
583 reference operator[](size_t n) const { return *(*this + n); } \
585 iterator_name& operator++() \
586 { wxStringOperations::IncIter(m_cur); return *this; } \
587 iterator_name& operator--() \
588 { wxStringOperations::DecIter(m_cur); return *this; } \
589 iterator_name operator++(int) \
591 iterator_name tmp = *this; \
592 wxStringOperations::IncIter(m_cur); \
595 iterator_name operator--(int) \
597 iterator_name tmp = *this; \
598 wxStringOperations::DecIter(m_cur); \
602 iterator_name& operator+=(ptrdiff_t n) \
604 m_cur = wxStringOperations::AddToIter(m_cur, n); \
607 iterator_name& operator-=(ptrdiff_t n) \
609 m_cur = wxStringOperations::AddToIter(m_cur, -n); \
613 difference_type operator-(const iterator_name& i) const \
614 { return wxStringOperations::DiffIters(m_cur, i.m_cur); } \
616 bool operator==(const iterator_name& i) const \
617 { return m_cur == i.m_cur; } \
618 bool operator!=(const iterator_name& i) const \
619 { return m_cur != i.m_cur; } \
621 bool operator<(const iterator_name& i) const \
622 { return m_cur < i.m_cur; } \
623 bool operator>(const iterator_name& i) const \
624 { return m_cur > i.m_cur; } \
625 bool operator<=(const iterator_name& i) const \
626 { return m_cur <= i.m_cur; } \
627 bool operator>=(const iterator_name& i) const \
628 { return m_cur >= i.m_cur; } \
631 /* for internal wxString use only: */ \
632 underlying_iterator impl() const { return m_cur; } \
634 friend class wxString; \
635 friend class wxCStrData; \
638 underlying_iterator m_cur
640 class WXDLLIMPEXP_FWD_BASE const_iterator
;
642 #if wxUSE_UNICODE_UTF8
643 // NB: In UTF-8 build, (non-const) iterator needs to keep reference
644 // to the underlying wxStringImpl, because UTF-8 is variable-length
645 // encoding and changing the value pointer to by an iterator (using
646 // its operator*) requires calling wxStringImpl::replace() if the old
647 // and new values differ in their encoding's length.
649 // Furthermore, the replace() call may invalid all iterators for the
650 // string, so we have to keep track of outstanding iterators and update
651 // them if replace() happens.
653 // This is implemented by maintaining linked list of iterators for every
654 // string and traversing it in wxUniCharRef::operator=(). Head of the
655 // list is stored in wxString. (FIXME-UTF8)
657 class WXDLLIMPEXP_BASE iterator
659 WX_STR_ITERATOR_IMPL(iterator
, wxChar
*, wxUniCharRef
);
663 iterator(const iterator
& i
)
664 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
665 iterator
& operator=(const iterator
& i
)
670 m_node
.set(i
.str(), &m_cur
);
675 reference
operator*()
676 { return wxUniCharRef::CreateForString(m_node
, m_cur
); }
678 iterator
operator+(ptrdiff_t n
) const
679 { return iterator(str(), wxStringOperations::AddToIter(m_cur
, n
)); }
680 iterator
operator-(ptrdiff_t n
) const
681 { return iterator(str(), wxStringOperations::AddToIter(m_cur
, -n
)); }
684 iterator(wxString
*str
, underlying_iterator ptr
)
685 : m_cur(ptr
), m_node(str
, &m_cur
) {}
687 wxString
* str() const { return wx_const_cast(wxString
*, m_node
.m_str
); }
689 wxStringIteratorNode m_node
;
691 friend class const_iterator
;
694 class WXDLLIMPEXP_BASE const_iterator
696 // NB: reference_type is intentionally value, not reference, the character
697 // may be encoded differently in wxString data:
698 WX_STR_ITERATOR_IMPL(const_iterator
, const wxChar
*, wxUniChar
);
702 const_iterator(const const_iterator
& i
)
703 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
704 const_iterator(const iterator
& i
)
705 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
707 const_iterator
& operator=(const const_iterator
& i
)
712 m_node
.set(i
.str(), &m_cur
);
716 const_iterator
& operator=(const iterator
& i
)
717 { m_cur
= i
.m_cur
; m_node
.set(i
.str(), &m_cur
); return *this; }
719 reference
operator*() const
720 { return wxStringOperations::DecodeChar(m_cur
); }
722 const_iterator
operator+(ptrdiff_t n
) const
723 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur
, n
)); }
724 const_iterator
operator-(ptrdiff_t n
) const
725 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur
, -n
)); }
728 // for internal wxString use only:
729 const_iterator(const wxString
*str
, underlying_iterator ptr
)
730 : m_cur(ptr
), m_node(str
, &m_cur
) {}
732 const wxString
* str() const { return m_node
.m_str
; }
734 wxStringIteratorNode m_node
;
737 size_t IterToImplPos(wxString::iterator i
) const
738 { return wxStringImpl::const_iterator(i
.impl()) - m_impl
.begin(); }
740 #else // !wxUSE_UNICODE_UTF8
742 class WXDLLIMPEXP_BASE iterator
744 WX_STR_ITERATOR_IMPL(iterator
, wxChar
*, wxUniCharRef
);
748 iterator(const iterator
& i
) : m_cur(i
.m_cur
) {}
750 reference
operator*()
751 { return wxUniCharRef::CreateForString(m_cur
); }
753 iterator
operator+(ptrdiff_t n
) const
754 { return iterator(wxStringOperations::AddToIter(m_cur
, n
)); }
755 iterator
operator-(ptrdiff_t n
) const
756 { return iterator(wxStringOperations::AddToIter(m_cur
, -n
)); }
759 // for internal wxString use only:
760 iterator(underlying_iterator ptr
) : m_cur(ptr
) {}
761 iterator(wxString
*WXUNUSED(str
), underlying_iterator ptr
) : m_cur(ptr
) {}
763 friend class const_iterator
;
766 class WXDLLIMPEXP_BASE const_iterator
768 // NB: reference_type is intentionally value, not reference, the character
769 // may be encoded differently in wxString data:
770 WX_STR_ITERATOR_IMPL(const_iterator
, const wxChar
*, wxUniChar
);
774 const_iterator(const const_iterator
& i
) : m_cur(i
.m_cur
) {}
775 const_iterator(const iterator
& i
) : m_cur(i
.m_cur
) {}
777 reference
operator*() const
778 { return wxStringOperations::DecodeChar(m_cur
); }
780 const_iterator
operator+(ptrdiff_t n
) const
781 { return const_iterator(wxStringOperations::AddToIter(m_cur
, n
)); }
782 const_iterator
operator-(ptrdiff_t n
) const
783 { return const_iterator(wxStringOperations::AddToIter(m_cur
, -n
)); }
786 // for internal wxString use only:
787 const_iterator(underlying_iterator ptr
) : m_cur(ptr
) {}
788 const_iterator(const wxString
*WXUNUSED(str
), underlying_iterator ptr
)
791 #endif // wxUSE_UNICODE_UTF8/!wxUSE_UNICODE_UTF8
793 #undef WX_STR_ITERATOR_TAG
794 #undef WX_STR_ITERATOR_IMPL
796 friend class iterator
;
797 friend class const_iterator
;
799 template <typename T
>
800 class reverse_iterator_impl
803 typedef T iterator_type
;
805 typedef typename
T::iterator_category iterator_category
;
806 typedef typename
T::value_type value_type
;
807 typedef typename
T::difference_type difference_type
;
808 typedef typename
T::reference reference
;
809 typedef typename
T::pointer
*pointer
;
811 reverse_iterator_impl() {}
812 reverse_iterator_impl(iterator_type i
) : m_cur(i
) {}
813 reverse_iterator_impl(const reverse_iterator_impl
& ri
)
816 iterator_type
base() const { return m_cur
; }
818 reference
operator*() const { return *(m_cur
-1); }
819 reference
operator[](size_t n
) const { return *(*this + n
); }
821 reverse_iterator_impl
& operator++()
822 { --m_cur
; return *this; }
823 reverse_iterator_impl
operator++(int)
824 { reverse_iterator_impl tmp
= *this; --m_cur
; return tmp
; }
825 reverse_iterator_impl
& operator--()
826 { ++m_cur
; return *this; }
827 reverse_iterator_impl
operator--(int)
828 { reverse_iterator_impl tmp
= *this; ++m_cur
; return tmp
; }
830 // NB: explicit <T> in the functions below is to keep BCC 5.5 happy
831 reverse_iterator_impl
operator+(ptrdiff_t n
) const
832 { return reverse_iterator_impl
<T
>(m_cur
- n
); }
833 reverse_iterator_impl
operator-(ptrdiff_t n
) const
834 { return reverse_iterator_impl
<T
>(m_cur
+ n
); }
835 reverse_iterator_impl
operator+=(ptrdiff_t n
)
836 { m_cur
-= n
; return *this; }
837 reverse_iterator_impl
operator-=(ptrdiff_t n
)
838 { m_cur
+= n
; return *this; }
840 unsigned operator-(const reverse_iterator_impl
& i
) const
841 { return i
.m_cur
- m_cur
; }
843 bool operator==(const reverse_iterator_impl
& ri
) const
844 { return m_cur
== ri
.m_cur
; }
845 bool operator!=(const reverse_iterator_impl
& ri
) const
846 { return !(*this == ri
); }
848 bool operator<(const reverse_iterator_impl
& i
) const
849 { return m_cur
> i
.m_cur
; }
850 bool operator>(const reverse_iterator_impl
& i
) const
851 { return m_cur
< i
.m_cur
; }
852 bool operator<=(const reverse_iterator_impl
& i
) const
853 { return m_cur
>= i
.m_cur
; }
854 bool operator>=(const reverse_iterator_impl
& i
) const
855 { return m_cur
<= i
.m_cur
; }
861 typedef reverse_iterator_impl
<iterator
> reverse_iterator
;
862 typedef reverse_iterator_impl
<const_iterator
> const_reverse_iterator
;
865 // used to transform an expression built using c_str() (and hence of type
866 // wxCStrData) to an iterator into the string
867 static const_iterator
CreateConstIterator(const wxCStrData
& data
)
869 return const_iterator(data
.m_str
,
870 (data
.m_str
->begin() + data
.m_offset
).impl());
873 // in UTF-8 STL build, creation from std::string requires conversion under
874 // non-UTF8 locales, so we can't have and use wxString(wxStringImpl) ctor;
875 // instead we define dummy type that lets us have wxString ctor for creation
876 // from wxStringImpl that couldn't be used by user code (in all other builds,
877 // "standard" ctors can be used):
878 #if wxUSE_UNICODE_UTF8 && wxUSE_STL_BASED_WXSTRING
879 struct CtorFromStringImplTag
{};
881 wxString(CtorFromStringImplTag
* WXUNUSED(dummy
), const wxStringImpl
& src
)
884 static wxString
FromImpl(const wxStringImpl
& src
)
885 { return wxString((CtorFromStringImplTag
*)NULL
, src
); }
887 #if !wxUSE_STL_BASED_WXSTRING
888 wxString(const wxStringImpl
& src
) : m_impl(src
) { }
889 // else: already defined as wxString(wxStdString) below
891 static wxString
FromImpl(const wxStringImpl
& src
) { return wxString(src
); }
895 // constructors and destructor
896 // ctor for an empty string
900 wxString(const wxString
& stringSrc
) : m_impl(stringSrc
.m_impl
) { }
902 // string containing nRepeat copies of ch
903 wxString(wxUniChar ch
, size_t nRepeat
= 1 )
904 { assign(nRepeat
, ch
); }
905 wxString(size_t nRepeat
, wxUniChar ch
)
906 { assign(nRepeat
, ch
); }
907 wxString(wxUniCharRef ch
, size_t nRepeat
= 1)
908 { assign(nRepeat
, ch
); }
909 wxString(size_t nRepeat
, wxUniCharRef ch
)
910 { assign(nRepeat
, ch
); }
911 wxString(char ch
, size_t nRepeat
= 1)
912 { assign(nRepeat
, ch
); }
913 wxString(size_t nRepeat
, char ch
)
914 { assign(nRepeat
, ch
); }
915 wxString(wchar_t ch
, size_t nRepeat
= 1)
916 { assign(nRepeat
, ch
); }
917 wxString(size_t nRepeat
, wchar_t ch
)
918 { assign(nRepeat
, ch
); }
920 // ctors from char* strings:
921 wxString(const char *psz
)
922 : m_impl(ImplStr(psz
)) {}
923 wxString(const char *psz
, const wxMBConv
& conv
)
924 : m_impl(ImplStr(psz
, conv
)) {}
925 wxString(const char *psz
, size_t nLength
)
926 { assign(psz
, nLength
); }
927 wxString(const char *psz
, const wxMBConv
& conv
, size_t nLength
)
929 SubstrBufFromMB
str(ImplStr(psz
, nLength
, conv
));
930 m_impl
.assign(str
.data
, str
.len
);
933 // and unsigned char*:
934 wxString(const unsigned char *psz
)
935 : m_impl(ImplStr((const char*)psz
)) {}
936 wxString(const unsigned char *psz
, const wxMBConv
& conv
)
937 : m_impl(ImplStr((const char*)psz
, conv
)) {}
938 wxString(const unsigned char *psz
, size_t nLength
)
939 { assign((const char*)psz
, nLength
); }
940 wxString(const unsigned char *psz
, const wxMBConv
& conv
, size_t nLength
)
942 SubstrBufFromMB
str(ImplStr((const char*)psz
, nLength
, conv
));
943 m_impl
.assign(str
.data
, str
.len
);
946 // ctors from wchar_t* strings:
947 wxString(const wchar_t *pwz
)
948 : m_impl(ImplStr(pwz
)) {}
949 wxString(const wchar_t *pwz
, const wxMBConv
& WXUNUSED(conv
))
950 : m_impl(ImplStr(pwz
)) {}
951 wxString(const wchar_t *pwz
, size_t nLength
)
952 { assign(pwz
, nLength
); }
953 wxString(const wchar_t *pwz
, const wxMBConv
& WXUNUSED(conv
), size_t nLength
)
954 { assign(pwz
, nLength
); }
956 wxString(const wxCharBuffer
& buf
)
957 { assign(buf
.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
958 wxString(const wxWCharBuffer
& buf
)
959 { assign(buf
.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
961 // NB: this version uses m_impl.c_str() to force making a copy of the
962 // string, so that "wxString(str.c_str())" idiom for passing strings
963 // between threads works
964 wxString(const wxCStrData
& cstr
)
965 : m_impl(cstr
.AsString().m_impl
.c_str()) { }
967 // as we provide both ctors with this signature for both char and unsigned
968 // char string, we need to provide one for wxCStrData to resolve ambiguity
969 wxString(const wxCStrData
& cstr
, size_t nLength
)
970 : m_impl(cstr
.AsString().Mid(0, nLength
).m_impl
) {}
972 // and because wxString is convertible to wxCStrData and const wxChar *
973 // we also need to provide this one
974 wxString(const wxString
& str
, size_t nLength
)
975 { assign(str
, nLength
); }
977 // even if we're not built with wxUSE_STL == 1 it is very convenient to allow
978 // implicit conversions from std::string to wxString and vice verse as this
979 // allows to use the same strings in non-GUI and GUI code, however we don't
980 // want to unconditionally add this ctor as it would make wx lib dependent on
981 // libstdc++ on some Linux versions which is bad, so instead we ask the
982 // client code to define this wxUSE_STD_STRING symbol if they need it
984 #if wxUSE_UNICODE_WCHAR
985 wxString(const wxStdWideString
& str
) : m_impl(str
) {}
986 #else // UTF-8 or ANSI
987 wxString(const wxStdWideString
& str
)
988 { assign(str
.c_str(), str
.length()); }
991 #if !wxUSE_UNICODE // ANSI build
992 // FIXME-UTF8: do this in UTF8 build #if wxUSE_UTF8_LOCALE_ONLY, too
993 wxString(const std::string
& str
) : m_impl(str
) {}
995 wxString(const std::string
& str
)
996 { assign(str
.c_str(), str
.length()); }
998 #endif // wxUSE_STD_STRING
1000 // Unlike ctor from std::string, we provide conversion to std::string only
1001 // if wxUSE_STL and not merely wxUSE_STD_STRING (which is on by default),
1002 // because it conflicts with operator const char/wchar_t*:
1004 #if wxUSE_UNICODE_WCHAR && wxUSE_STL_BASED_WXSTRING
1005 // wxStringImpl is std::string in the encoding we want
1006 operator const wxStdWideString
&() const { return m_impl
; }
1008 // wxStringImpl is either not std::string or needs conversion
1009 operator wxStdWideString() const
1010 // FIXME-UTF8: broken for embedded NULs
1011 { return wxStdWideString(wc_str()); }
1014 #if (!wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY) && wxUSE_STL_BASED_WXSTRING
1015 // wxStringImpl is std::string in the encoding we want
1016 operator const std::string
&() const { return m_impl
; }
1018 // wxStringImpl is either not std::string or needs conversion
1019 operator std::string() const
1020 // FIXME-UTF8: broken for embedded NULs
1021 { return std::string(mb_str()); }
1025 wxString
Clone() const
1027 // make a deep copy of the string, i.e. the returned string will have
1028 // ref count = 1 with refcounted implementation
1029 return wxString::FromImpl(wxStringImpl(m_impl
.c_str(), m_impl
.length()));
1032 // first valid index position
1033 const_iterator
begin() const { return const_iterator(this, m_impl
.begin()); }
1034 iterator
begin() { return iterator(this, m_impl
.begin()); }
1035 // position one after the last valid one
1036 const_iterator
end() const { return const_iterator(this, m_impl
.end()); }
1037 iterator
end() { return iterator(this, m_impl
.end()); }
1039 // first element of the reversed string
1040 const_reverse_iterator
rbegin() const
1041 { return const_reverse_iterator(end()); }
1042 reverse_iterator
rbegin()
1043 { return reverse_iterator(end()); }
1044 // one beyond the end of the reversed string
1045 const_reverse_iterator
rend() const
1046 { return const_reverse_iterator(begin()); }
1047 reverse_iterator
rend()
1048 { return reverse_iterator(begin()); }
1050 // std::string methods:
1051 #if wxUSE_UNICODE_UTF8
1052 size_t length() const { return end() - begin(); } // FIXME-UTF8: optimize!
1054 size_t length() const { return m_impl
.length(); }
1057 size_type
size() const { return length(); }
1058 size_type
max_size() const { return npos
; }
1060 bool empty() const { return m_impl
.empty(); }
1062 size_type
capacity() const { return m_impl
.capacity(); } // FIXME-UTF8
1063 void reserve(size_t sz
) { m_impl
.reserve(sz
); } // FIXME-UTF8
1065 void resize(size_t nSize
, wxUniChar ch
= wxT('\0'))
1067 const size_t len
= length();
1071 #if wxUSE_UNICODE_UTF8
1074 // we can't use wxStringImpl::resize() for truncating the string as it
1075 // counts in bytes, not characters
1080 // we also can't use (presumably more efficient) resize() if we have to
1081 // append characters taking more than one byte
1082 if ( !ch
.IsAscii() )
1083 append(nSize
- len
, ch
);
1085 #endif // wxUSE_UNICODE_UTF8
1086 m_impl
.resize(nSize
, (wxStringCharType
)ch
);
1089 wxString
substr(size_t nStart
= 0, size_t nLen
= npos
) const
1092 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
1093 return FromImpl(m_impl
.substr(pos
, len
));
1096 // generic attributes & operations
1097 // as standard strlen()
1098 size_t Len() const { return length(); }
1099 // string contains any characters?
1100 bool IsEmpty() const { return empty(); }
1101 // empty string is "false", so !str will return true
1102 bool operator!() const { return empty(); }
1103 // truncate the string to given length
1104 wxString
& Truncate(size_t uiLen
);
1105 // empty string contents
1110 wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") );
1112 // empty the string and free memory
1115 wxString
tmp(wxEmptyString
);
1120 // Is an ascii value
1121 bool IsAscii() const;
1123 bool IsNumber() const;
1125 bool IsWord() const;
1127 // data access (all indexes are 0 based)
1129 wxUniChar
at(size_t n
) const
1130 { return *(begin() + n
); } // FIXME-UTF8: optimize?
1131 wxUniChar
GetChar(size_t n
) const
1133 // read/write access
1134 wxUniCharRef
at(size_t n
)
1135 { return *(begin() + n
); } // FIXME-UTF8: optimize?
1136 wxUniCharRef
GetWritableChar(size_t n
)
1139 void SetChar(size_t n
, wxUniChar ch
)
1142 // get last character
1143 wxUniChar
Last() const
1145 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1149 // get writable last character
1152 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1157 Note that we we must define all of the overloads below to avoid
1158 ambiguity when using str[0].
1160 wxUniChar
operator[](int n
) const
1162 wxUniChar
operator[](long n
) const
1164 wxUniChar
operator[](size_t n
) const
1166 #ifndef wxSIZE_T_IS_UINT
1167 wxUniChar
operator[](unsigned int n
) const
1169 #endif // size_t != unsigned int
1171 // operator versions of GetWriteableChar()
1172 wxUniCharRef
operator[](int n
)
1174 wxUniCharRef
operator[](long n
)
1176 wxUniCharRef
operator[](size_t n
)
1178 #ifndef wxSIZE_T_IS_UINT
1179 wxUniCharRef
operator[](unsigned int n
)
1181 #endif // size_t != unsigned int
1183 // explicit conversion to C string (use this with printf()!)
1184 wxCStrData
c_str() const { return wxCStrData(this); }
1185 wxCStrData
data() const { return c_str(); }
1187 // implicit conversion to C string
1188 operator wxCStrData() const { return c_str(); }
1190 // the first two operators conflict with operators for conversion to
1191 // std::string and they must be disabled in STL build; the next one only
1192 // makes sense if conversions to char* are also defined and not defining it
1193 // in STL build also helps us to get more clear error messages for the code
1194 // which relies on implicit conversion to char* in STL build
1196 operator const char*() const { return c_str(); }
1197 operator const wchar_t*() const { return c_str(); }
1199 // implicit conversion to untyped pointer for compatibility with previous
1200 // wxWidgets versions: this is the same as conversion to const char * so it
1202 operator const void*() const { return c_str(); }
1205 // identical to c_str(), for MFC compatibility
1206 const wxCStrData
GetData() const { return c_str(); }
1208 // explicit conversion to C string in internal representation (char*,
1209 // wchar_t*, UTF-8-encoded char*, depending on the build):
1210 const wxStringCharType
*wx_str() const { return m_impl
.c_str(); }
1212 // conversion to *non-const* multibyte or widestring buffer; modifying
1213 // returned buffer won't affect the string, these methods are only useful
1214 // for passing values to const-incorrect functions
1215 wxWritableCharBuffer
char_str(const wxMBConv
& conv
= wxConvLibc
) const
1216 { return mb_str(conv
); }
1217 wxWritableWCharBuffer
wchar_str() const { return wc_str(); }
1219 // conversion to the buffer of the given type T (= char or wchar_t) and
1220 // also optionally return the buffer length
1222 // this is mostly/only useful for the template functions
1224 // FIXME-VC6: the second argument only exists for VC6 which doesn't support
1225 // explicit template function selection, do not use it unless
1226 // you must support VC6!
1227 template <typename T
>
1228 wxCharTypeBuffer
<T
> tchar_str(size_t *len
= NULL
,
1229 T
* WXUNUSED(dummy
) = NULL
) const
1232 // we need a helper dispatcher depending on type
1233 return wxPrivate::wxStringAsBufHelper
<T
>::Get(*this, len
);
1235 // T can only be char in ANSI build
1239 return wxCharTypeBuffer
<T
>::CreateNonOwned(wx_str());
1240 #endif // Unicode build kind
1243 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
1244 // converting numbers or strings which are certain not to contain special
1245 // chars (typically system functions, X atoms, environment variables etc.)
1247 // the behaviour of these functions with the strings containing anything
1248 // else than 7 bit ASCII characters is undefined, use at your own risk.
1250 static wxString
FromAscii(const char *ascii
, size_t len
);
1251 static wxString
FromAscii(const char *ascii
);
1252 static wxString
FromAscii(char ascii
);
1253 const wxCharBuffer
ToAscii() const;
1255 static wxString
FromAscii(const char *ascii
) { return wxString( ascii
); }
1256 static wxString
FromAscii(const char *ascii
, size_t len
)
1257 { return wxString( ascii
, len
); }
1258 static wxString
FromAscii(char ascii
) { return wxString( ascii
); }
1259 const char *ToAscii() const { return c_str(); }
1260 #endif // Unicode/!Unicode
1262 // also provide unsigned char overloads as signed/unsigned doesn't matter
1263 // for 7 bit ASCII characters
1264 static wxString
FromAscii(const unsigned char *ascii
)
1265 { return FromAscii((const char *)ascii
); }
1266 static wxString
FromAscii(const unsigned char *ascii
, size_t len
)
1267 { return FromAscii((const char *)ascii
, len
); }
1269 // conversion to/from UTF-8:
1270 #if wxUSE_UNICODE_UTF8
1271 static wxString
FromUTF8Unchecked(const char *utf8
)
1274 return wxEmptyString
;
1276 wxASSERT( wxStringOperations::IsValidUtf8String(utf8
) );
1277 return FromImpl(wxStringImpl(utf8
));
1279 static wxString
FromUTF8Unchecked(const char *utf8
, size_t len
)
1282 return wxEmptyString
;
1284 return FromUTF8Unchecked(utf8
);
1286 wxASSERT( wxStringOperations::IsValidUtf8String(utf8
, len
) );
1287 return FromImpl(wxStringImpl(utf8
, len
));
1290 static wxString
FromUTF8(const char *utf8
)
1292 if ( !utf8
|| !wxStringOperations::IsValidUtf8String(utf8
) )
1295 return FromImpl(wxStringImpl(utf8
));
1297 static wxString
FromUTF8(const char *utf8
, size_t len
)
1300 return FromUTF8(utf8
);
1302 if ( !utf8
|| !wxStringOperations::IsValidUtf8String(utf8
, len
) )
1305 return FromImpl(wxStringImpl(utf8
, len
));
1308 const char* utf8_str() const { return wx_str(); }
1309 const char* ToUTF8() const { return wx_str(); }
1311 // this function exists in UTF-8 build only and returns the length of the
1312 // internal UTF-8 representation
1313 size_t utf8_length() const { return m_impl
.length(); }
1314 #elif wxUSE_UNICODE_WCHAR
1315 static wxString
FromUTF8(const char *utf8
, size_t len
= npos
)
1316 { return wxString(utf8
, wxMBConvUTF8(), len
); }
1317 static wxString
FromUTF8Unchecked(const char *utf8
, size_t len
= npos
)
1319 const wxString
s(utf8
, wxMBConvUTF8(), len
);
1320 wxASSERT_MSG( !utf8
|| !*utf8
|| !s
.empty(),
1321 "string must be valid UTF-8" );
1324 const wxCharBuffer
utf8_str() const { return mb_str(wxMBConvUTF8()); }
1325 const wxCharBuffer
ToUTF8() const { return utf8_str(); }
1327 static wxString
FromUTF8(const char *utf8
)
1328 { return wxString(wxMBConvUTF8().cMB2WC(utf8
)); }
1329 static wxString
FromUTF8(const char *utf8
, size_t len
)
1332 wxWCharBuffer
buf(wxMBConvUTF8().cMB2WC(utf8
, len
== npos
? wxNO_LEN
: len
, &wlen
));
1333 return wxString(buf
.data(), wlen
);
1335 const wxCharBuffer
utf8_str() const
1336 { return wxMBConvUTF8().cWC2MB(wc_str()); }
1337 const wxCharBuffer
ToUTF8() const { return utf8_str(); }
1340 // functions for storing binary data in wxString:
1342 static wxString
From8BitData(const char *data
, size_t len
)
1343 { return wxString(data
, wxConvISO8859_1
, len
); }
1344 // version for NUL-terminated data:
1345 static wxString
From8BitData(const char *data
)
1346 { return wxString(data
, wxConvISO8859_1
); }
1347 const wxCharBuffer
To8BitData() const { return mb_str(wxConvISO8859_1
); }
1349 static wxString
From8BitData(const char *data
, size_t len
)
1350 { return wxString(data
, len
); }
1351 // version for NUL-terminated data:
1352 static wxString
From8BitData(const char *data
)
1353 { return wxString(data
); }
1354 const char *To8BitData() const { return c_str(); }
1355 #endif // Unicode/ANSI
1357 // conversions with (possible) format conversions: have to return a
1358 // buffer with temporary data
1360 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
1361 // return an ANSI (multibyte) string, wc_str() to return a wide string and
1362 // fn_str() to return a string which should be used with the OS APIs
1363 // accepting the file names. The return value is always the same, but the
1364 // type differs because a function may either return pointer to the buffer
1365 // directly or have to use intermediate buffer for translation.
1368 #if wxUSE_UTF8_LOCALE_ONLY
1369 const char* mb_str() const { return wx_str(); }
1370 const wxCharBuffer
mb_str(const wxMBConv
& conv
) const;
1372 const wxCharBuffer
mb_str(const wxMBConv
& conv
= wxConvLibc
) const;
1375 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
1377 #if wxUSE_UNICODE_WCHAR
1378 const wchar_t* wc_str() const { return wx_str(); }
1379 #elif wxUSE_UNICODE_UTF8
1380 const wxWCharBuffer
wc_str() const;
1382 // for compatibility with !wxUSE_UNICODE version
1383 const wxWX2WCbuf
wc_str(const wxMBConv
& WXUNUSED(conv
)) const
1384 { return wc_str(); }
1387 const wxCharBuffer
fn_str() const { return mb_str(wxConvFile
); }
1389 const wxWX2WCbuf
fn_str() const { return wc_str(); }
1390 #endif // wxMBFILES/!wxMBFILES
1393 const wxChar
* mb_str() const { return wx_str(); }
1395 // for compatibility with wxUSE_UNICODE version
1396 const char* mb_str(const wxMBConv
& WXUNUSED(conv
)) const { return wx_str(); }
1398 const wxWX2MBbuf
mbc_str() const { return mb_str(); }
1401 const wxWCharBuffer
wc_str(const wxMBConv
& conv
= wxConvLibc
) const;
1402 #endif // wxUSE_WCHAR_T
1403 const wxCharBuffer
fn_str() const { return wxConvFile
.cWC2WX( wc_str( wxConvLibc
) ); }
1404 #endif // Unicode/ANSI
1406 #if wxUSE_UNICODE_UTF8
1407 const wxWCharBuffer
t_str() const { return wc_str(); }
1408 #elif wxUSE_UNICODE_WCHAR
1409 const wchar_t* t_str() const { return wx_str(); }
1411 const char* t_str() const { return wx_str(); }
1415 // overloaded assignment
1416 // from another wxString
1417 wxString
& operator=(const wxString
& stringSrc
)
1418 { if (&stringSrc
!= this) m_impl
= stringSrc
.m_impl
; return *this; }
1419 wxString
& operator=(const wxCStrData
& cstr
)
1420 { return *this = cstr
.AsString(); }
1422 wxString
& operator=(wxUniChar ch
)
1424 #if wxUSE_UNICODE_UTF8
1425 if ( !ch
.IsAscii() )
1426 m_impl
= wxStringOperations::EncodeChar(ch
);
1429 m_impl
= (wxStringCharType
)ch
;
1432 wxString
& operator=(wxUniCharRef ch
)
1433 { return operator=((wxUniChar
)ch
); }
1434 wxString
& operator=(char ch
)
1435 { return operator=(wxUniChar(ch
)); }
1436 wxString
& operator=(unsigned char ch
)
1437 { return operator=(wxUniChar(ch
)); }
1438 wxString
& operator=(wchar_t ch
)
1439 { return operator=(wxUniChar(ch
)); }
1440 // from a C string - STL probably will crash on NULL,
1441 // so we need to compensate in that case
1442 #if wxUSE_STL_BASED_WXSTRING
1443 wxString
& operator=(const char *psz
)
1444 { if (psz
) m_impl
= ImplStr(psz
); else Clear(); return *this; }
1445 wxString
& operator=(const wchar_t *pwz
)
1446 { if (pwz
) m_impl
= ImplStr(pwz
); else Clear(); return *this; }
1448 wxString
& operator=(const char *psz
)
1449 { m_impl
= ImplStr(psz
); return *this; }
1450 wxString
& operator=(const wchar_t *pwz
)
1451 { m_impl
= ImplStr(pwz
); return *this; }
1453 wxString
& operator=(const unsigned char *psz
)
1454 { return operator=((const char*)psz
); }
1456 // from wxWCharBuffer
1457 wxString
& operator=(const wxWCharBuffer
& s
)
1458 { return operator=(s
.data()); } // FIXME-UTF8: fix for embedded NULs
1459 // from wxCharBuffer
1460 wxString
& operator=(const wxCharBuffer
& s
)
1461 { return operator=(s
.data()); } // FIXME-UTF8: fix for embedded NULs
1463 // string concatenation
1464 // in place concatenation
1466 Concatenate and return the result. Note that the left to right
1467 associativity of << allows to write things like "str << str1 << str2
1468 << ..." (unlike with +=)
1471 wxString
& operator<<(const wxString
& s
)
1473 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1474 wxASSERT_MSG( s
.IsValid(),
1475 _T("did you forget to call UngetWriteBuf()?") );
1481 // string += C string
1482 wxString
& operator<<(const char *psz
)
1483 { append(psz
); return *this; }
1484 wxString
& operator<<(const wchar_t *pwz
)
1485 { append(pwz
); return *this; }
1486 wxString
& operator<<(const wxCStrData
& psz
)
1487 { append(psz
.AsString()); return *this; }
1489 wxString
& operator<<(wxUniChar ch
) { append(1, ch
); return *this; }
1490 wxString
& operator<<(wxUniCharRef ch
) { append(1, ch
); return *this; }
1491 wxString
& operator<<(char ch
) { append(1, ch
); return *this; }
1492 wxString
& operator<<(unsigned char ch
) { append(1, ch
); return *this; }
1493 wxString
& operator<<(wchar_t ch
) { append(1, ch
); return *this; }
1495 // string += buffer (i.e. from wxGetString)
1496 wxString
& operator<<(const wxWCharBuffer
& s
)
1497 { return operator<<((const wchar_t *)s
); }
1498 wxString
& operator<<(const wxCharBuffer
& s
)
1499 { return operator<<((const char *)s
); }
1501 // string += C string
1502 wxString
& Append(const wxString
& s
)
1504 // test for empty() to share the string if possible
1511 wxString
& Append(const char* psz
)
1512 { append(psz
); return *this; }
1513 wxString
& Append(const wchar_t* pwz
)
1514 { append(pwz
); return *this; }
1515 wxString
& Append(const wxCStrData
& psz
)
1516 { append(psz
); return *this; }
1517 wxString
& Append(const wxCharBuffer
& psz
)
1518 { append(psz
); return *this; }
1519 wxString
& Append(const wxWCharBuffer
& psz
)
1520 { append(psz
); return *this; }
1521 wxString
& Append(const char* psz
, size_t nLen
)
1522 { append(psz
, nLen
); return *this; }
1523 wxString
& Append(const wchar_t* pwz
, size_t nLen
)
1524 { append(pwz
, nLen
); return *this; }
1525 wxString
& Append(const wxCStrData
& psz
, size_t nLen
)
1526 { append(psz
, nLen
); return *this; }
1527 wxString
& Append(const wxCharBuffer
& psz
, size_t nLen
)
1528 { append(psz
, nLen
); return *this; }
1529 wxString
& Append(const wxWCharBuffer
& psz
, size_t nLen
)
1530 { append(psz
, nLen
); return *this; }
1531 // append count copies of given character
1532 wxString
& Append(wxUniChar ch
, size_t count
= 1u)
1533 { append(count
, ch
); return *this; }
1534 wxString
& Append(wxUniCharRef ch
, size_t count
= 1u)
1535 { append(count
, ch
); return *this; }
1536 wxString
& Append(char ch
, size_t count
= 1u)
1537 { append(count
, ch
); return *this; }
1538 wxString
& Append(unsigned char ch
, size_t count
= 1u)
1539 { append(count
, ch
); return *this; }
1540 wxString
& Append(wchar_t ch
, size_t count
= 1u)
1541 { append(count
, ch
); return *this; }
1543 // prepend a string, return the string itself
1544 wxString
& Prepend(const wxString
& str
)
1545 { *this = str
+ *this; return *this; }
1547 // non-destructive concatenation
1549 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
,
1550 const wxString
& string2
);
1551 // string with a single char
1552 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
1553 // char with a string
1554 friend wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
1555 // string with C string
1556 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
1558 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
1559 const wchar_t *pwz
);
1560 // C string with string
1561 friend wxString WXDLLIMPEXP_BASE
operator+(const char *psz
,
1562 const wxString
& string
);
1563 friend wxString WXDLLIMPEXP_BASE
operator+(const wchar_t *pwz
,
1564 const wxString
& string
);
1566 // stream-like functions
1567 // insert an int into string
1568 wxString
& operator<<(int i
)
1569 { return (*this) << Format(_T("%d"), i
); }
1570 // insert an unsigned int into string
1571 wxString
& operator<<(unsigned int ui
)
1572 { return (*this) << Format(_T("%u"), ui
); }
1573 // insert a long into string
1574 wxString
& operator<<(long l
)
1575 { return (*this) << Format(_T("%ld"), l
); }
1576 // insert an unsigned long into string
1577 wxString
& operator<<(unsigned long ul
)
1578 { return (*this) << Format(_T("%lu"), ul
); }
1579 #if defined wxLongLong_t && !defined wxLongLongIsLong
1580 // insert a long long if they exist and aren't longs
1581 wxString
& operator<<(wxLongLong_t ll
)
1583 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("d");
1584 return (*this) << Format(fmt
, ll
);
1586 // insert an unsigned long long
1587 wxString
& operator<<(wxULongLong_t ull
)
1589 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("u");
1590 return (*this) << Format(fmt
, ull
);
1593 // insert a float into string
1594 wxString
& operator<<(float f
)
1595 { return (*this) << Format(_T("%f"), f
); }
1596 // insert a double into string
1597 wxString
& operator<<(double d
)
1598 { return (*this) << Format(_T("%g"), d
); }
1600 // string comparison
1601 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
1602 int Cmp(const char *psz
) const
1603 { return compare(psz
); }
1604 int Cmp(const wchar_t *pwz
) const
1605 { return compare(pwz
); }
1606 int Cmp(const wxString
& s
) const
1607 { return compare(s
); }
1608 int Cmp(const wxCStrData
& s
) const
1609 { return compare(s
); }
1610 int Cmp(const wxCharBuffer
& s
) const
1611 { return compare(s
); }
1612 int Cmp(const wxWCharBuffer
& s
) const
1613 { return compare(s
); }
1614 // same as Cmp() but not case-sensitive
1615 int CmpNoCase(const wxString
& s
) const;
1617 // test for the string equality, either considering case or not
1618 // (if compareWithCase then the case matters)
1619 bool IsSameAs(const wxString
& str
, bool compareWithCase
= true) const
1621 #if !wxUSE_UNICODE_UTF8
1622 // in UTF-8 build, length() is O(n) and doing this would be _slower_
1623 if ( length() != str
.length() )
1626 return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0;
1628 bool IsSameAs(const char *str
, bool compareWithCase
= true) const
1629 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
1630 bool IsSameAs(const wchar_t *str
, bool compareWithCase
= true) const
1631 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
1633 bool IsSameAs(const wxCStrData
& str
, bool compareWithCase
= true) const
1634 { return IsSameAs(str
.AsString(), compareWithCase
); }
1635 bool IsSameAs(const wxCharBuffer
& str
, bool compareWithCase
= true) const
1636 { return IsSameAs(str
.data(), compareWithCase
); }
1637 bool IsSameAs(const wxWCharBuffer
& str
, bool compareWithCase
= true) const
1638 { return IsSameAs(str
.data(), compareWithCase
); }
1639 // comparison with a single character: returns true if equal
1640 bool IsSameAs(wxUniChar c
, bool compareWithCase
= true) const;
1641 // FIXME-UTF8: remove these overloads
1642 bool IsSameAs(wxUniCharRef c
, bool compareWithCase
= true) const
1643 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1644 bool IsSameAs(char c
, bool compareWithCase
= true) const
1645 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1646 bool IsSameAs(unsigned char c
, bool compareWithCase
= true) const
1647 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1648 bool IsSameAs(wchar_t c
, bool compareWithCase
= true) const
1649 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1650 bool IsSameAs(int c
, bool compareWithCase
= true) const
1651 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1653 // simple sub-string extraction
1654 // return substring starting at nFirst of length nCount (or till the end
1655 // if nCount = default value)
1656 wxString
Mid(size_t nFirst
, size_t nCount
= npos
) const;
1658 // operator version of Mid()
1659 wxString
operator()(size_t start
, size_t len
) const
1660 { return Mid(start
, len
); }
1662 // check if the string starts with the given prefix and return the rest
1663 // of the string in the provided pointer if it is not NULL; otherwise
1665 bool StartsWith(const wxString
& prefix
, wxString
*rest
= NULL
) const;
1666 // check if the string ends with the given suffix and return the
1667 // beginning of the string before the suffix in the provided pointer if
1668 // it is not NULL; otherwise return false
1669 bool EndsWith(const wxString
& suffix
, wxString
*rest
= NULL
) const;
1671 // get first nCount characters
1672 wxString
Left(size_t nCount
) const;
1673 // get last nCount characters
1674 wxString
Right(size_t nCount
) const;
1675 // get all characters before the first occurance of ch
1676 // (returns the whole string if ch not found)
1677 wxString
BeforeFirst(wxUniChar ch
) const;
1678 // get all characters before the last occurence of ch
1679 // (returns empty string if ch not found)
1680 wxString
BeforeLast(wxUniChar ch
) const;
1681 // get all characters after the first occurence of ch
1682 // (returns empty string if ch not found)
1683 wxString
AfterFirst(wxUniChar ch
) const;
1684 // get all characters after the last occurence of ch
1685 // (returns the whole string if ch not found)
1686 wxString
AfterLast(wxUniChar ch
) const;
1688 // for compatibility only, use more explicitly named functions above
1689 wxString
Before(wxUniChar ch
) const { return BeforeLast(ch
); }
1690 wxString
After(wxUniChar ch
) const { return AfterFirst(ch
); }
1693 // convert to upper case in place, return the string itself
1694 wxString
& MakeUpper();
1695 // convert to upper case, return the copy of the string
1696 // Here's something to remember: BC++ doesn't like returns in inlines.
1697 wxString
Upper() const ;
1698 // convert to lower case in place, return the string itself
1699 wxString
& MakeLower();
1700 // convert to lower case, return the copy of the string
1701 wxString
Lower() const ;
1703 // trimming/padding whitespace (either side) and truncating
1704 // remove spaces from left or from right (default) side
1705 wxString
& Trim(bool bFromRight
= true);
1706 // add nCount copies chPad in the beginning or at the end (default)
1707 wxString
& Pad(size_t nCount
, wxUniChar chPad
= wxT(' '), bool bFromRight
= true);
1709 // searching and replacing
1710 // searching (return starting index, or -1 if not found)
1711 int Find(wxUniChar ch
, bool bFromEnd
= false) const; // like strchr/strrchr
1712 int Find(wxUniCharRef ch
, bool bFromEnd
= false) const
1713 { return Find(wxUniChar(ch
), bFromEnd
); }
1714 int Find(char ch
, bool bFromEnd
= false) const
1715 { return Find(wxUniChar(ch
), bFromEnd
); }
1716 int Find(unsigned char ch
, bool bFromEnd
= false) const
1717 { return Find(wxUniChar(ch
), bFromEnd
); }
1718 int Find(wchar_t ch
, bool bFromEnd
= false) const
1719 { return Find(wxUniChar(ch
), bFromEnd
); }
1720 // searching (return starting index, or -1 if not found)
1721 int Find(const wxString
& sub
) const // like strstr
1723 size_type idx
= find(sub
);
1724 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
1726 int Find(const char *sub
) const // like strstr
1728 size_type idx
= find(sub
);
1729 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
1731 int Find(const wchar_t *sub
) const // like strstr
1733 size_type idx
= find(sub
);
1734 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
1737 int Find(const wxCStrData
& sub
) const
1738 { return Find(sub
.AsString()); }
1739 int Find(const wxCharBuffer
& sub
) const
1740 { return Find(sub
.data()); }
1741 int Find(const wxWCharBuffer
& sub
) const
1742 { return Find(sub
.data()); }
1744 // replace first (or all of bReplaceAll) occurences of substring with
1745 // another string, returns the number of replacements made
1746 size_t Replace(const wxString
& strOld
,
1747 const wxString
& strNew
,
1748 bool bReplaceAll
= true);
1750 // check if the string contents matches a mask containing '*' and '?'
1751 bool Matches(const wxString
& mask
) const;
1753 // conversion to numbers: all functions return true only if the whole
1754 // string is a number and put the value of this number into the pointer
1755 // provided, the base is the numeric base in which the conversion should be
1756 // done and must be comprised between 2 and 36 or be 0 in which case the
1757 // standard C rules apply (leading '0' => octal, "0x" => hex)
1758 // convert to a signed integer
1759 bool ToLong(long *val
, int base
= 10) const;
1760 // convert to an unsigned integer
1761 bool ToULong(unsigned long *val
, int base
= 10) const;
1762 // convert to wxLongLong
1763 #if defined(wxLongLong_t)
1764 bool ToLongLong(wxLongLong_t
*val
, int base
= 10) const;
1765 // convert to wxULongLong
1766 bool ToULongLong(wxULongLong_t
*val
, int base
= 10) const;
1767 #endif // wxLongLong_t
1768 // convert to a double
1769 bool ToDouble(double *val
) const;
1772 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1773 // formatted input/output
1774 // as sprintf(), returns the number of characters written or < 0 on error
1775 // (take 'this' into account in attribute parameter count)
1776 // int Printf(const wxString& format, ...);
1777 WX_DEFINE_VARARG_FUNC(int, Printf
, 1, (const wxFormatString
&),
1778 DoPrintfWchar
, DoPrintfUtf8
)
1780 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1781 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wxString
&),
1782 (wxFormatString(f1
)));
1783 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wxCStrData
&),
1784 (wxFormatString(f1
)));
1785 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const char*),
1786 (wxFormatString(f1
)));
1787 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wchar_t*),
1788 (wxFormatString(f1
)));
1790 #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN
1791 // as vprintf(), returns the number of characters written or < 0 on error
1792 int PrintfV(const wxString
& format
, va_list argptr
);
1794 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1795 // returns the string containing the result of Printf() to it
1796 // static wxString Format(const wxString& format, ...) ATTRIBUTE_PRINTF_1;
1797 WX_DEFINE_VARARG_FUNC(static wxString
, Format
, 1, (const wxFormatString
&),
1798 DoFormatWchar
, DoFormatUtf8
)
1800 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1801 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wxString
&),
1802 (wxFormatString(f1
)));
1803 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wxCStrData
&),
1804 (wxFormatString(f1
)));
1805 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const char*),
1806 (wxFormatString(f1
)));
1807 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wchar_t*),
1808 (wxFormatString(f1
)));
1811 // the same as above, but takes a va_list
1812 static wxString
FormatV(const wxString
& format
, va_list argptr
);
1814 // raw access to string memory
1815 // ensure that string has space for at least nLen characters
1816 // only works if the data of this string is not shared
1817 bool Alloc(size_t nLen
) { reserve(nLen
); return capacity() >= nLen
; }
1818 // minimize the string's memory
1819 // only works if the data of this string is not shared
1821 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1822 // These are deprecated, use wxStringBuffer or wxStringBufferLength instead
1824 // get writable buffer of at least nLen bytes. Unget() *must* be called
1825 // a.s.a.p. to put string back in a reasonable state!
1826 wxDEPRECATED( wxStringCharType
*GetWriteBuf(size_t nLen
) );
1827 // call this immediately after GetWriteBuf() has been used
1828 wxDEPRECATED( void UngetWriteBuf() );
1829 wxDEPRECATED( void UngetWriteBuf(size_t nLen
) );
1830 #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && wxUSE_UNICODE_UTF8
1832 // wxWidgets version 1 compatibility functions
1835 wxString
SubString(size_t from
, size_t to
) const
1836 { return Mid(from
, (to
- from
+ 1)); }
1837 // values for second parameter of CompareTo function
1838 enum caseCompare
{exact
, ignoreCase
};
1839 // values for first parameter of Strip function
1840 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
1842 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1844 // (take 'this' into account in attribute parameter count)
1845 // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2;
1846 WX_DEFINE_VARARG_FUNC(int, sprintf
, 1, (const wxFormatString
&),
1847 DoPrintfWchar
, DoPrintfUtf8
)
1849 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1850 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wxString
&),
1851 (wxFormatString(f1
)));
1852 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wxCStrData
&),
1853 (wxFormatString(f1
)));
1854 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const char*),
1855 (wxFormatString(f1
)));
1856 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wchar_t*),
1857 (wxFormatString(f1
)));
1859 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
1862 inline int CompareTo(const wxChar
* psz
, caseCompare cmp
= exact
) const
1863 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
1866 size_t Length() const { return length(); }
1867 // Count the number of characters
1868 int Freq(wxUniChar ch
) const;
1870 void LowerCase() { MakeLower(); }
1872 void UpperCase() { MakeUpper(); }
1873 // use Trim except that it doesn't change this string
1874 wxString
Strip(stripType w
= trailing
) const;
1876 // use Find (more general variants not yet supported)
1877 size_t Index(const wxChar
* psz
) const { return Find(psz
); }
1878 size_t Index(wxUniChar ch
) const { return Find(ch
); }
1880 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
1881 wxString
& RemoveLast(size_t n
= 1) { return Truncate(length() - n
); }
1883 wxString
& Remove(size_t nStart
, size_t nLen
)
1884 { return (wxString
&)erase( nStart
, nLen
); }
1887 int First( wxUniChar ch
) const { return Find(ch
); }
1888 int First( wxUniCharRef ch
) const { return Find(ch
); }
1889 int First( char ch
) const { return Find(ch
); }
1890 int First( unsigned char ch
) const { return Find(ch
); }
1891 int First( wchar_t ch
) const { return Find(ch
); }
1892 int First( const wxString
& str
) const { return Find(str
); }
1893 int Last( wxUniChar ch
) const { return Find(ch
, true); }
1894 bool Contains(const wxString
& str
) const { return Find(str
) != wxNOT_FOUND
; }
1897 bool IsNull() const { return empty(); }
1899 // std::string compatibility functions
1901 // take nLen chars starting at nPos
1902 wxString(const wxString
& str
, size_t nPos
, size_t nLen
)
1903 { assign(str
, nPos
, nLen
); }
1904 // take all characters from first to last
1905 wxString(const_iterator first
, const_iterator last
)
1906 : m_impl(first
.impl(), last
.impl()) { }
1907 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1908 // the 2 overloads below are for compatibility with the existing code using
1909 // pointers instead of iterators
1910 wxString(const char *first
, const char *last
)
1912 SubstrBufFromMB
str(ImplStr(first
, last
- first
));
1913 m_impl
.assign(str
.data
, str
.len
);
1915 wxString(const wchar_t *first
, const wchar_t *last
)
1917 SubstrBufFromWC
str(ImplStr(first
, last
- first
));
1918 m_impl
.assign(str
.data
, str
.len
);
1920 // and this one is needed to compile code adding offsets to c_str() result
1921 wxString(const wxCStrData
& first
, const wxCStrData
& last
)
1922 : m_impl(CreateConstIterator(first
).impl(),
1923 CreateConstIterator(last
).impl())
1925 wxASSERT_MSG( first
.m_str
== last
.m_str
,
1926 _T("pointers must be into the same string") );
1928 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1930 // lib.string.modifiers
1931 // append elements str[pos], ..., str[pos+n]
1932 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
1935 str
.PosLenToImpl(pos
, n
, &from
, &len
);
1936 m_impl
.append(str
.m_impl
, from
, len
);
1940 wxString
& append(const wxString
& str
)
1941 { m_impl
.append(str
.m_impl
); return *this; }
1942 // append first n (or all if n == npos) characters of sz
1943 wxString
& append(const char *sz
)
1944 { m_impl
.append(ImplStr(sz
)); return *this; }
1945 wxString
& append(const wchar_t *sz
)
1946 { m_impl
.append(ImplStr(sz
)); return *this; }
1947 wxString
& append(const char *sz
, size_t n
)
1949 SubstrBufFromMB
str(ImplStr(sz
, n
));
1950 m_impl
.append(str
.data
, str
.len
);
1953 wxString
& append(const wchar_t *sz
, size_t n
)
1955 SubstrBufFromWC
str(ImplStr(sz
, n
));
1956 m_impl
.append(str
.data
, str
.len
);
1960 wxString
& append(const wxCStrData
& str
)
1961 { return append(str
.AsString()); }
1962 wxString
& append(const wxCharBuffer
& str
)
1963 { return append(str
.data()); }
1964 wxString
& append(const wxWCharBuffer
& str
)
1965 { return append(str
.data()); }
1966 wxString
& append(const wxCStrData
& str
, size_t n
)
1967 { return append(str
.AsString(), 0, n
); }
1968 wxString
& append(const wxCharBuffer
& str
, size_t n
)
1969 { return append(str
.data(), n
); }
1970 wxString
& append(const wxWCharBuffer
& str
, size_t n
)
1971 { return append(str
.data(), n
); }
1973 // append n copies of ch
1974 wxString
& append(size_t n
, wxUniChar ch
)
1976 #if wxUSE_UNICODE_UTF8
1977 if ( !ch
.IsAscii() )
1978 m_impl
.append(wxStringOperations::EncodeNChars(n
, ch
));
1981 m_impl
.append(n
, (wxStringCharType
)ch
);
1984 wxString
& append(size_t n
, wxUniCharRef ch
)
1985 { return append(n
, wxUniChar(ch
)); }
1986 wxString
& append(size_t n
, char ch
)
1987 { return append(n
, wxUniChar(ch
)); }
1988 wxString
& append(size_t n
, unsigned char ch
)
1989 { return append(n
, wxUniChar(ch
)); }
1990 wxString
& append(size_t n
, wchar_t ch
)
1991 { return append(n
, wxUniChar(ch
)); }
1993 // append from first to last
1994 wxString
& append(const_iterator first
, const_iterator last
)
1995 { m_impl
.append(first
.impl(), last
.impl()); return *this; }
1996 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1997 wxString
& append(const char *first
, const char *last
)
1998 { return append(first
, last
- first
); }
1999 wxString
& append(const wchar_t *first
, const wchar_t *last
)
2000 { return append(first
, last
- first
); }
2001 wxString
& append(const wxCStrData
& first
, const wxCStrData
& last
)
2002 { return append(CreateConstIterator(first
), CreateConstIterator(last
)); }
2003 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2005 // same as `this_string = str'
2006 wxString
& assign(const wxString
& str
)
2007 { m_impl
= str
.m_impl
; return *this; }
2008 wxString
& assign(const wxString
& str
, size_t len
)
2010 m_impl
.assign(str
.m_impl
, 0, str
.LenToImpl(len
));
2013 // same as ` = str[pos..pos + n]
2014 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
2017 str
.PosLenToImpl(pos
, n
, &from
, &len
);
2018 m_impl
.assign(str
.m_impl
, from
, len
);
2021 // same as `= first n (or all if n == npos) characters of sz'
2022 wxString
& assign(const char *sz
)
2023 { m_impl
.assign(ImplStr(sz
)); return *this; }
2024 wxString
& assign(const wchar_t *sz
)
2025 { m_impl
.assign(ImplStr(sz
)); return *this; }
2026 wxString
& assign(const char *sz
, size_t n
)
2028 SubstrBufFromMB
str(ImplStr(sz
, n
));
2029 m_impl
.assign(str
.data
, str
.len
);
2032 wxString
& assign(const wchar_t *sz
, size_t n
)
2034 SubstrBufFromWC
str(ImplStr(sz
, n
));
2035 m_impl
.assign(str
.data
, str
.len
);
2039 wxString
& assign(const wxCStrData
& str
)
2040 { return assign(str
.AsString()); }
2041 wxString
& assign(const wxCharBuffer
& str
)
2042 { return assign(str
.data()); }
2043 wxString
& assign(const wxWCharBuffer
& str
)
2044 { return assign(str
.data()); }
2045 wxString
& assign(const wxCStrData
& str
, size_t len
)
2046 { return assign(str
.AsString(), len
); }
2047 wxString
& assign(const wxCharBuffer
& str
, size_t len
)
2048 { return assign(str
.data(), len
); }
2049 wxString
& assign(const wxWCharBuffer
& str
, size_t len
)
2050 { return assign(str
.data(), len
); }
2052 // same as `= n copies of ch'
2053 wxString
& assign(size_t n
, wxUniChar ch
)
2055 #if wxUSE_UNICODE_UTF8
2056 if ( !ch
.IsAscii() )
2057 m_impl
.assign(wxStringOperations::EncodeNChars(n
, ch
));
2060 m_impl
.assign(n
, (wxStringCharType
)ch
);
2064 wxString
& assign(size_t n
, wxUniCharRef ch
)
2065 { return assign(n
, wxUniChar(ch
)); }
2066 wxString
& assign(size_t n
, char ch
)
2067 { return assign(n
, wxUniChar(ch
)); }
2068 wxString
& assign(size_t n
, unsigned char ch
)
2069 { return assign(n
, wxUniChar(ch
)); }
2070 wxString
& assign(size_t n
, wchar_t ch
)
2071 { return assign(n
, wxUniChar(ch
)); }
2073 // assign from first to last
2074 wxString
& assign(const_iterator first
, const_iterator last
)
2075 { m_impl
.assign(first
.impl(), last
.impl()); return *this; }
2076 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2077 wxString
& assign(const char *first
, const char *last
)
2078 { return assign(first
, last
- first
); }
2079 wxString
& assign(const wchar_t *first
, const wchar_t *last
)
2080 { return assign(first
, last
- first
); }
2081 wxString
& assign(const wxCStrData
& first
, const wxCStrData
& last
)
2082 { return assign(CreateConstIterator(first
), CreateConstIterator(last
)); }
2083 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2085 // string comparison
2086 int compare(const wxString
& str
) const;
2087 int compare(const char* sz
) const;
2088 int compare(const wchar_t* sz
) const;
2089 int compare(const wxCStrData
& str
) const
2090 { return compare(str
.AsString()); }
2091 int compare(const wxCharBuffer
& str
) const
2092 { return compare(str
.data()); }
2093 int compare(const wxWCharBuffer
& str
) const
2094 { return compare(str
.data()); }
2095 // comparison with a substring
2096 int compare(size_t nStart
, size_t nLen
, const wxString
& str
) const;
2097 // comparison of 2 substrings
2098 int compare(size_t nStart
, size_t nLen
,
2099 const wxString
& str
, size_t nStart2
, size_t nLen2
) const;
2100 // substring comparison with first nCount characters of sz
2101 int compare(size_t nStart
, size_t nLen
,
2102 const char* sz
, size_t nCount
= npos
) const;
2103 int compare(size_t nStart
, size_t nLen
,
2104 const wchar_t* sz
, size_t nCount
= npos
) const;
2106 // insert another string
2107 wxString
& insert(size_t nPos
, const wxString
& str
)
2108 { insert(begin() + nPos
, str
.begin(), str
.end()); return *this; }
2109 // insert n chars of str starting at nStart (in str)
2110 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
2113 str
.PosLenToImpl(nStart
, n
, &from
, &len
);
2114 m_impl
.insert(PosToImpl(nPos
), str
.m_impl
, from
, len
);
2117 // insert first n (or all if n == npos) characters of sz
2118 wxString
& insert(size_t nPos
, const char *sz
)
2119 { m_impl
.insert(PosToImpl(nPos
), ImplStr(sz
)); return *this; }
2120 wxString
& insert(size_t nPos
, const wchar_t *sz
)
2121 { m_impl
.insert(PosToImpl(nPos
), ImplStr(sz
)); return *this; }
2122 wxString
& insert(size_t nPos
, const char *sz
, size_t n
)
2124 SubstrBufFromMB
str(ImplStr(sz
, n
));
2125 m_impl
.insert(PosToImpl(nPos
), str
.data
, str
.len
);
2128 wxString
& insert(size_t nPos
, const wchar_t *sz
, size_t n
)
2130 SubstrBufFromWC
str(ImplStr(sz
, n
));
2131 m_impl
.insert(PosToImpl(nPos
), str
.data
, str
.len
);
2134 // insert n copies of ch
2135 wxString
& insert(size_t nPos
, size_t n
, wxUniChar ch
)
2137 #if wxUSE_UNICODE_UTF8
2138 if ( !ch
.IsAscii() )
2139 m_impl
.insert(PosToImpl(nPos
), wxStringOperations::EncodeNChars(n
, ch
));
2142 m_impl
.insert(PosToImpl(nPos
), n
, (wxStringCharType
)ch
);
2145 iterator
insert(iterator it
, wxUniChar ch
)
2147 #if wxUSE_UNICODE_UTF8
2148 if ( !ch
.IsAscii() )
2150 size_t pos
= IterToImplPos(it
);
2151 m_impl
.insert(pos
, wxStringOperations::EncodeChar(ch
));
2152 return iterator(this, m_impl
.begin() + pos
);
2156 return iterator(this, m_impl
.insert(it
.impl(), (wxStringCharType
)ch
));
2158 void insert(iterator it
, const_iterator first
, const_iterator last
)
2159 { m_impl
.insert(it
.impl(), first
.impl(), last
.impl()); }
2160 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2161 void insert(iterator it
, const char *first
, const char *last
)
2162 { insert(it
- begin(), first
, last
- first
); }
2163 void insert(iterator it
, const wchar_t *first
, const wchar_t *last
)
2164 { insert(it
- begin(), first
, last
- first
); }
2165 void insert(iterator it
, const wxCStrData
& first
, const wxCStrData
& last
)
2166 { insert(it
, CreateConstIterator(first
), CreateConstIterator(last
)); }
2167 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2169 void insert(iterator it
, size_type n
, wxUniChar ch
)
2171 #if wxUSE_UNICODE_UTF8
2172 if ( !ch
.IsAscii() )
2173 m_impl
.insert(IterToImplPos(it
), wxStringOperations::EncodeNChars(n
, ch
));
2176 m_impl
.insert(it
.impl(), n
, (wxStringCharType
)ch
);
2179 // delete characters from nStart to nStart + nLen
2180 wxString
& erase(size_type pos
= 0, size_type n
= npos
)
2183 PosLenToImpl(pos
, n
, &from
, &len
);
2184 m_impl
.erase(from
, len
);
2187 // delete characters from first up to last
2188 iterator
erase(iterator first
, iterator last
)
2189 { return iterator(this, m_impl
.erase(first
.impl(), last
.impl())); }
2190 iterator
erase(iterator first
)
2191 { return iterator(this, m_impl
.erase(first
.impl())); }
2193 #ifdef wxSTRING_BASE_HASNT_CLEAR
2194 void clear() { erase(); }
2196 void clear() { m_impl
.clear(); }
2199 // replaces the substring of length nLen starting at nStart
2200 wxString
& replace(size_t nStart
, size_t nLen
, const char* sz
)
2203 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2204 m_impl
.replace(from
, len
, ImplStr(sz
));
2207 wxString
& replace(size_t nStart
, size_t nLen
, const wchar_t* sz
)
2210 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2211 m_impl
.replace(from
, len
, ImplStr(sz
));
2214 // replaces the substring of length nLen starting at nStart
2215 wxString
& replace(size_t nStart
, size_t nLen
, const wxString
& str
)
2218 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2219 m_impl
.replace(from
, len
, str
.m_impl
);
2222 // replaces the substring with nCount copies of ch
2223 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxUniChar ch
)
2226 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2227 #if wxUSE_UNICODE_UTF8
2228 if ( !ch
.IsAscii() )
2229 m_impl
.replace(from
, len
, wxStringOperations::EncodeNChars(nCount
, ch
));
2232 m_impl
.replace(from
, len
, nCount
, (wxStringCharType
)ch
);
2235 // replaces a substring with another substring
2236 wxString
& replace(size_t nStart
, size_t nLen
,
2237 const wxString
& str
, size_t nStart2
, size_t nLen2
)
2240 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2243 str
.PosLenToImpl(nStart2
, nLen2
, &from2
, &len2
);
2245 m_impl
.replace(from
, len
, str
.m_impl
, from2
, len2
);
2248 // replaces the substring with first nCount chars of sz
2249 wxString
& replace(size_t nStart
, size_t nLen
,
2250 const char* sz
, size_t nCount
)
2253 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2255 SubstrBufFromMB
str(ImplStr(sz
, nCount
));
2257 m_impl
.replace(from
, len
, str
.data
, str
.len
);
2260 wxString
& replace(size_t nStart
, size_t nLen
,
2261 const wchar_t* sz
, size_t nCount
)
2264 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2266 SubstrBufFromWC
str(ImplStr(sz
, nCount
));
2268 m_impl
.replace(from
, len
, str
.data
, str
.len
);
2271 wxString
& replace(size_t nStart
, size_t nLen
,
2272 const wxString
& s
, size_t nCount
)
2275 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2276 m_impl
.replace(from
, len
, s
.m_impl
.c_str(), s
.LenToImpl(nCount
));
2280 wxString
& replace(iterator first
, iterator last
, const char* s
)
2281 { m_impl
.replace(first
.impl(), last
.impl(), ImplStr(s
)); return *this; }
2282 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
)
2283 { m_impl
.replace(first
.impl(), last
.impl(), ImplStr(s
)); return *this; }
2284 wxString
& replace(iterator first
, iterator last
, const char* s
, size_type n
)
2286 SubstrBufFromMB
str(ImplStr(s
, n
));
2287 m_impl
.replace(first
.impl(), last
.impl(), str
.data
, str
.len
);
2290 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
, size_type n
)
2292 SubstrBufFromWC
str(ImplStr(s
, n
));
2293 m_impl
.replace(first
.impl(), last
.impl(), str
.data
, str
.len
);
2296 wxString
& replace(iterator first
, iterator last
, const wxString
& s
)
2297 { m_impl
.replace(first
.impl(), last
.impl(), s
.m_impl
); return *this; }
2298 wxString
& replace(iterator first
, iterator last
, size_type n
, wxUniChar ch
)
2300 #if wxUSE_UNICODE_UTF8
2301 if ( !ch
.IsAscii() )
2302 m_impl
.replace(first
.impl(), last
.impl(),
2303 wxStringOperations::EncodeNChars(n
, ch
));
2306 m_impl
.replace(first
.impl(), last
.impl(), n
, (wxStringCharType
)ch
);
2309 wxString
& replace(iterator first
, iterator last
,
2310 const_iterator first1
, const_iterator last1
)
2312 m_impl
.replace(first
.impl(), last
.impl(), first1
.impl(), last1
.impl());
2315 wxString
& replace(iterator first
, iterator last
,
2316 const char *first1
, const char *last1
)
2317 { replace(first
, last
, first1
, last1
- first1
); return *this; }
2318 wxString
& replace(iterator first
, iterator last
,
2319 const wchar_t *first1
, const wchar_t *last1
)
2320 { replace(first
, last
, first1
, last1
- first1
); return *this; }
2323 void swap(wxString
& str
)
2324 { m_impl
.swap(str
.m_impl
); }
2327 size_t find(const wxString
& str
, size_t nStart
= 0) const
2328 { return PosFromImpl(m_impl
.find(str
.m_impl
, PosToImpl(nStart
))); }
2330 // find first n characters of sz
2331 size_t find(const char* sz
, size_t nStart
= 0, size_t n
= npos
) const
2333 SubstrBufFromMB
str(ImplStr(sz
, n
));
2334 return PosFromImpl(m_impl
.find(str
.data
, PosToImpl(nStart
), str
.len
));
2336 size_t find(const wchar_t* sz
, size_t nStart
= 0, size_t n
= npos
) const
2338 SubstrBufFromWC
str(ImplStr(sz
, n
));
2339 return PosFromImpl(m_impl
.find(str
.data
, PosToImpl(nStart
), str
.len
));
2341 size_t find(const wxCharBuffer
& s
, size_t nStart
= 0, size_t n
= npos
) const
2342 { return find(s
.data(), nStart
, n
); }
2343 size_t find(const wxWCharBuffer
& s
, size_t nStart
= 0, size_t n
= npos
) const
2344 { return find(s
.data(), nStart
, n
); }
2345 size_t find(const wxCStrData
& s
, size_t nStart
= 0, size_t n
= npos
) const
2346 { return find(s
.AsWChar(), nStart
, n
); }
2348 // find the first occurence of character ch after nStart
2349 size_t find(wxUniChar ch
, size_t nStart
= 0) const
2351 #if wxUSE_UNICODE_UTF8
2352 if ( !ch
.IsAscii() )
2353 return PosFromImpl(m_impl
.find(wxStringOperations::EncodeChar(ch
),
2354 PosToImpl(nStart
)));
2357 return PosFromImpl(m_impl
.find((wxStringCharType
)ch
,
2358 PosToImpl(nStart
)));
2361 size_t find(wxUniCharRef ch
, size_t nStart
= 0) const
2362 { return find(wxUniChar(ch
), nStart
); }
2363 size_t find(char ch
, size_t nStart
= 0) const
2364 { return find(wxUniChar(ch
), nStart
); }
2365 size_t find(unsigned char ch
, size_t nStart
= 0) const
2366 { return find(wxUniChar(ch
), nStart
); }
2367 size_t find(wchar_t ch
, size_t nStart
= 0) const
2368 { return find(wxUniChar(ch
), nStart
); }
2370 // rfind() family is exactly like find() but works right to left
2372 // as find, but from the end
2373 size_t rfind(const wxString
& str
, size_t nStart
= npos
) const
2374 { return PosFromImpl(m_impl
.rfind(str
.m_impl
, PosToImpl(nStart
))); }
2376 // as find, but from the end
2377 size_t rfind(const char* sz
, size_t nStart
= npos
, size_t n
= npos
) const
2379 SubstrBufFromMB
str(ImplStr(sz
, n
));
2380 return PosFromImpl(m_impl
.rfind(str
.data
, PosToImpl(nStart
), str
.len
));
2382 size_t rfind(const wchar_t* sz
, size_t nStart
= npos
, size_t n
= npos
) const
2384 SubstrBufFromWC
str(ImplStr(sz
, n
));
2385 return PosFromImpl(m_impl
.rfind(str
.data
, PosToImpl(nStart
), str
.len
));
2387 size_t rfind(const wxCharBuffer
& s
, size_t nStart
= npos
, size_t n
= npos
) const
2388 { return rfind(s
.data(), nStart
, n
); }
2389 size_t rfind(const wxWCharBuffer
& s
, size_t nStart
= npos
, size_t n
= npos
) const
2390 { return rfind(s
.data(), nStart
, n
); }
2391 size_t rfind(const wxCStrData
& s
, size_t nStart
= npos
, size_t n
= npos
) const
2392 { return rfind(s
.AsWChar(), nStart
, n
); }
2393 // as find, but from the end
2394 size_t rfind(wxUniChar ch
, size_t nStart
= npos
) const
2396 #if wxUSE_UNICODE_UTF8
2397 if ( !ch
.IsAscii() )
2398 return PosFromImpl(m_impl
.rfind(wxStringOperations::EncodeChar(ch
),
2399 PosToImpl(nStart
)));
2402 return PosFromImpl(m_impl
.rfind((wxStringCharType
)ch
,
2403 PosToImpl(nStart
)));
2405 size_t rfind(wxUniCharRef ch
, size_t nStart
= npos
) const
2406 { return rfind(wxUniChar(ch
), nStart
); }
2407 size_t rfind(char ch
, size_t nStart
= npos
) const
2408 { return rfind(wxUniChar(ch
), nStart
); }
2409 size_t rfind(unsigned char ch
, size_t nStart
= npos
) const
2410 { return rfind(wxUniChar(ch
), nStart
); }
2411 size_t rfind(wchar_t ch
, size_t nStart
= npos
) const
2412 { return rfind(wxUniChar(ch
), nStart
); }
2414 // find first/last occurence of any character (not) in the set:
2415 #if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2416 // FIXME-UTF8: this is not entirely correct, because it doesn't work if
2417 // sizeof(wchar_t)==2 and surrogates are present in the string;
2418 // should we care? Probably not.
2419 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
2420 { return m_impl
.find_first_of(str
.m_impl
, nStart
); }
2421 size_t find_first_of(const char* sz
, size_t nStart
= 0) const
2422 { return m_impl
.find_first_of(ImplStr(sz
), nStart
); }
2423 size_t find_first_of(const wchar_t* sz
, size_t nStart
= 0) const
2424 { return m_impl
.find_first_of(ImplStr(sz
), nStart
); }
2425 size_t find_first_of(const char* sz
, size_t nStart
, size_t n
) const
2426 { return m_impl
.find_first_of(ImplStr(sz
), nStart
, n
); }
2427 size_t find_first_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
2428 { return m_impl
.find_first_of(ImplStr(sz
), nStart
, n
); }
2429 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const
2430 { return m_impl
.find_first_of((wxChar
)c
, nStart
); }
2432 size_t find_last_of(const wxString
& str
, size_t nStart
= npos
) const
2433 { return m_impl
.find_last_of(str
.m_impl
, nStart
); }
2434 size_t find_last_of(const char* sz
, size_t nStart
= npos
) const
2435 { return m_impl
.find_last_of(ImplStr(sz
), nStart
); }
2436 size_t find_last_of(const wchar_t* sz
, size_t nStart
= npos
) const
2437 { return m_impl
.find_last_of(ImplStr(sz
), nStart
); }
2438 size_t find_last_of(const char* sz
, size_t nStart
, size_t n
) const
2439 { return m_impl
.find_last_of(ImplStr(sz
), nStart
, n
); }
2440 size_t find_last_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
2441 { return m_impl
.find_last_of(ImplStr(sz
), nStart
, n
); }
2442 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const
2443 { return m_impl
.find_last_of((wxChar
)c
, nStart
); }
2445 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
2446 { return m_impl
.find_first_not_of(str
.m_impl
, nStart
); }
2447 size_t find_first_not_of(const char* sz
, size_t nStart
= 0) const
2448 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
); }
2449 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
= 0) const
2450 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
); }
2451 size_t find_first_not_of(const char* sz
, size_t nStart
, size_t n
) const
2452 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
, n
); }
2453 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
2454 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
, n
); }
2455 size_t find_first_not_of(wxUniChar c
, size_t nStart
= 0) const
2456 { return m_impl
.find_first_not_of((wxChar
)c
, nStart
); }
2458 size_t find_last_not_of(const wxString
& str
, size_t nStart
= npos
) const
2459 { return m_impl
.find_last_not_of(str
.m_impl
, nStart
); }
2460 size_t find_last_not_of(const char* sz
, size_t nStart
= npos
) const
2461 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
); }
2462 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
= npos
) const
2463 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
); }
2464 size_t find_last_not_of(const char* sz
, size_t nStart
, size_t n
) const
2465 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
, n
); }
2466 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
2467 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
, n
); }
2468 size_t find_last_not_of(wxUniChar c
, size_t nStart
= npos
) const
2469 { return m_impl
.find_last_not_of((wxChar
)c
, nStart
); }
2471 // we can't use std::string implementation in UTF-8 build, because the
2472 // character sets would be interpreted wrongly:
2474 // as strpbrk() but starts at nStart, returns npos if not found
2475 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
2476 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2477 { return find_first_of(str
.wc_str(), nStart
); }
2479 { return find_first_of(str
.mb_str(), nStart
); }
2482 size_t find_first_of(const char* sz
, size_t nStart
= 0) const;
2483 size_t find_first_of(const wchar_t* sz
, size_t nStart
= 0) const;
2484 size_t find_first_of(const char* sz
, size_t nStart
, size_t n
) const;
2485 size_t find_first_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2486 // same as find(char, size_t)
2487 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const
2488 { return find(c
, nStart
); }
2489 // find the last (starting from nStart) char from str in this string
2490 size_t find_last_of (const wxString
& str
, size_t nStart
= npos
) const
2491 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2492 { return find_last_of(str
.wc_str(), nStart
); }
2494 { return find_last_of(str
.mb_str(), nStart
); }
2497 size_t find_last_of (const char* sz
, size_t nStart
= npos
) const;
2498 size_t find_last_of (const wchar_t* sz
, size_t nStart
= npos
) const;
2499 size_t find_last_of(const char* sz
, size_t nStart
, size_t n
) const;
2500 size_t find_last_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2502 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const
2503 { return rfind(c
, nStart
); }
2505 // find first/last occurence of any character not in the set
2507 // as strspn() (starting from nStart), returns npos on failure
2508 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
2509 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2510 { return find_first_not_of(str
.wc_str(), nStart
); }
2512 { return find_first_not_of(str
.mb_str(), nStart
); }
2515 size_t find_first_not_of(const char* sz
, size_t nStart
= 0) const;
2516 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
= 0) const;
2517 size_t find_first_not_of(const char* sz
, size_t nStart
, size_t n
) const;
2518 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2520 size_t find_first_not_of(wxUniChar ch
, size_t nStart
= 0) const;
2522 size_t find_last_not_of(const wxString
& str
, size_t nStart
= npos
) const
2523 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2524 { return find_last_not_of(str
.wc_str(), nStart
); }
2526 { return find_last_not_of(str
.mb_str(), nStart
); }
2529 size_t find_last_not_of(const char* sz
, size_t nStart
= npos
) const;
2530 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
= npos
) const;
2531 size_t find_last_not_of(const char* sz
, size_t nStart
, size_t n
) const;
2532 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2534 size_t find_last_not_of(wxUniChar ch
, size_t nStart
= npos
) const;
2535 #endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not
2537 // provide char/wchar_t/wxUniCharRef overloads for char-finding functions
2538 // above to resolve ambiguities:
2539 size_t find_first_of(wxUniCharRef ch
, size_t nStart
= 0) const
2540 { return find_first_of(wxUniChar(ch
), nStart
); }
2541 size_t find_first_of(char ch
, size_t nStart
= 0) const
2542 { return find_first_of(wxUniChar(ch
), nStart
); }
2543 size_t find_first_of(unsigned char ch
, size_t nStart
= 0) const
2544 { return find_first_of(wxUniChar(ch
), nStart
); }
2545 size_t find_first_of(wchar_t ch
, size_t nStart
= 0) const
2546 { return find_first_of(wxUniChar(ch
), nStart
); }
2547 size_t find_last_of(wxUniCharRef ch
, size_t nStart
= npos
) const
2548 { return find_last_of(wxUniChar(ch
), nStart
); }
2549 size_t find_last_of(char ch
, size_t nStart
= npos
) const
2550 { return find_last_of(wxUniChar(ch
), nStart
); }
2551 size_t find_last_of(unsigned char ch
, size_t nStart
= npos
) const
2552 { return find_last_of(wxUniChar(ch
), nStart
); }
2553 size_t find_last_of(wchar_t ch
, size_t nStart
= npos
) const
2554 { return find_last_of(wxUniChar(ch
), nStart
); }
2555 size_t find_first_not_of(wxUniCharRef ch
, size_t nStart
= 0) const
2556 { return find_first_not_of(wxUniChar(ch
), nStart
); }
2557 size_t find_first_not_of(char ch
, size_t nStart
= 0) const
2558 { return find_first_not_of(wxUniChar(ch
), nStart
); }
2559 size_t find_first_not_of(unsigned char ch
, size_t nStart
= 0) const
2560 { return find_first_not_of(wxUniChar(ch
), nStart
); }
2561 size_t find_first_not_of(wchar_t ch
, size_t nStart
= 0) const
2562 { return find_first_not_of(wxUniChar(ch
), nStart
); }
2563 size_t find_last_not_of(wxUniCharRef ch
, size_t nStart
= npos
) const
2564 { return find_last_not_of(wxUniChar(ch
), nStart
); }
2565 size_t find_last_not_of(char ch
, size_t nStart
= npos
) const
2566 { return find_last_not_of(wxUniChar(ch
), nStart
); }
2567 size_t find_last_not_of(unsigned char ch
, size_t nStart
= npos
) const
2568 { return find_last_not_of(wxUniChar(ch
), nStart
); }
2569 size_t find_last_not_of(wchar_t ch
, size_t nStart
= npos
) const
2570 { return find_last_not_of(wxUniChar(ch
), nStart
); }
2572 // and additional overloads for the versions taking strings:
2573 size_t find_first_of(const wxCStrData
& sz
, size_t nStart
= 0) const
2574 { return find_first_of(sz
.AsString(), nStart
); }
2575 size_t find_first_of(const wxCharBuffer
& sz
, size_t nStart
= 0) const
2576 { return find_first_of(sz
.data(), nStart
); }
2577 size_t find_first_of(const wxWCharBuffer
& sz
, size_t nStart
= 0) const
2578 { return find_first_of(sz
.data(), nStart
); }
2579 size_t find_first_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
2580 { return find_first_of(sz
.AsWChar(), nStart
, n
); }
2581 size_t find_first_of(const wxCharBuffer
& sz
, size_t nStart
, size_t n
) const
2582 { return find_first_of(sz
.data(), nStart
, n
); }
2583 size_t find_first_of(const wxWCharBuffer
& sz
, size_t nStart
, size_t n
) const
2584 { return find_first_of(sz
.data(), nStart
, n
); }
2586 size_t find_last_of(const wxCStrData
& sz
, size_t nStart
= 0) const
2587 { return find_last_of(sz
.AsString(), nStart
); }
2588 size_t find_last_of(const wxCharBuffer
& sz
, size_t nStart
= 0) const
2589 { return find_last_of(sz
.data(), nStart
); }
2590 size_t find_last_of(const wxWCharBuffer
& sz
, size_t nStart
= 0) const
2591 { return find_last_of(sz
.data(), nStart
); }
2592 size_t find_last_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
2593 { return find_last_of(sz
.AsWChar(), nStart
, n
); }
2594 size_t find_last_of(const wxCharBuffer
& sz
, size_t nStart
, size_t n
) const
2595 { return find_last_of(sz
.data(), nStart
, n
); }
2596 size_t find_last_of(const wxWCharBuffer
& sz
, size_t nStart
, size_t n
) const
2597 { return find_last_of(sz
.data(), nStart
, n
); }
2599 size_t find_first_not_of(const wxCStrData
& sz
, size_t nStart
= 0) const
2600 { return find_first_not_of(sz
.AsString(), nStart
); }
2601 size_t find_first_not_of(const wxCharBuffer
& sz
, size_t nStart
= 0) const
2602 { return find_first_not_of(sz
.data(), nStart
); }
2603 size_t find_first_not_of(const wxWCharBuffer
& sz
, size_t nStart
= 0) const
2604 { return find_first_not_of(sz
.data(), nStart
); }
2605 size_t find_first_not_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
2606 { return find_first_not_of(sz
.AsWChar(), nStart
, n
); }
2607 size_t find_first_not_of(const wxCharBuffer
& sz
, size_t nStart
, size_t n
) const
2608 { return find_first_not_of(sz
.data(), nStart
, n
); }
2609 size_t find_first_not_of(const wxWCharBuffer
& sz
, size_t nStart
, size_t n
) const
2610 { return find_first_not_of(sz
.data(), nStart
, n
); }
2612 size_t find_last_not_of(const wxCStrData
& sz
, size_t nStart
= 0) const
2613 { return find_last_not_of(sz
.AsString(), nStart
); }
2614 size_t find_last_not_of(const wxCharBuffer
& sz
, size_t nStart
= 0) const
2615 { return find_last_not_of(sz
.data(), nStart
); }
2616 size_t find_last_not_of(const wxWCharBuffer
& sz
, size_t nStart
= 0) const
2617 { return find_last_not_of(sz
.data(), nStart
); }
2618 size_t find_last_not_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
2619 { return find_last_not_of(sz
.AsWChar(), nStart
, n
); }
2620 size_t find_last_not_of(const wxCharBuffer
& sz
, size_t nStart
, size_t n
) const
2621 { return find_last_not_of(sz
.data(), nStart
, n
); }
2622 size_t find_last_not_of(const wxWCharBuffer
& sz
, size_t nStart
, size_t n
) const
2623 { return find_last_not_of(sz
.data(), nStart
, n
); }
2626 wxString
& operator+=(const wxString
& s
)
2627 { m_impl
+= s
.m_impl
; return *this; }
2628 // string += C string
2629 wxString
& operator+=(const char *psz
)
2630 { m_impl
+= ImplStr(psz
); return *this; }
2631 wxString
& operator+=(const wchar_t *pwz
)
2632 { m_impl
+= ImplStr(pwz
); return *this; }
2633 wxString
& operator+=(const wxCStrData
& s
)
2634 { m_impl
+= s
.AsString().m_impl
; return *this; }
2635 wxString
& operator+=(const wxCharBuffer
& s
)
2636 { return operator+=(s
.data()); }
2637 wxString
& operator+=(const wxWCharBuffer
& s
)
2638 { return operator+=(s
.data()); }
2640 wxString
& operator+=(wxUniChar ch
)
2642 #if wxUSE_UNICODE_UTF8
2643 if ( !ch
.IsAscii() )
2644 m_impl
+= wxStringOperations::EncodeChar(ch
);
2647 m_impl
+= (wxStringCharType
)ch
;
2650 wxString
& operator+=(wxUniCharRef ch
) { return *this += wxUniChar(ch
); }
2651 wxString
& operator+=(int ch
) { return *this += wxUniChar(ch
); }
2652 wxString
& operator+=(char ch
) { return *this += wxUniChar(ch
); }
2653 wxString
& operator+=(unsigned char ch
) { return *this += wxUniChar(ch
); }
2654 wxString
& operator+=(wchar_t ch
) { return *this += wxUniChar(ch
); }
2657 #if !wxUSE_STL_BASED_WXSTRING
2658 // helpers for wxStringBuffer and wxStringBufferLength
2659 wxStringCharType
*DoGetWriteBuf(size_t nLen
)
2660 { return m_impl
.DoGetWriteBuf(nLen
); }
2661 void DoUngetWriteBuf()
2662 { m_impl
.DoUngetWriteBuf(); }
2663 void DoUngetWriteBuf(size_t nLen
)
2664 { m_impl
.DoUngetWriteBuf(nLen
); }
2665 #endif // !wxUSE_STL_BASED_WXSTRING
2667 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2668 #if !wxUSE_UTF8_LOCALE_ONLY
2669 int DoPrintfWchar(const wxChar
*format
, ...);
2670 static wxString
DoFormatWchar(const wxChar
*format
, ...);
2672 #if wxUSE_UNICODE_UTF8
2673 int DoPrintfUtf8(const char *format
, ...);
2674 static wxString
DoFormatUtf8(const char *format
, ...);
2678 #if !wxUSE_STL_BASED_WXSTRING
2679 // check string's data validity
2680 bool IsValid() const { return m_impl
.GetStringData()->IsValid(); }
2684 wxStringImpl m_impl
;
2686 // buffers for compatibility conversion from (char*)c_str() and
2687 // (wchar_t*)c_str():
2688 // FIXME-UTF8: bechmark various approaches to keeping compatibility buffers
2689 template<typename T
>
2690 struct ConvertedBuffer
2692 ConvertedBuffer() : m_buf(NULL
) {}
2696 operator T
*() const { return m_buf
; }
2698 ConvertedBuffer
& operator=(T
*str
)
2707 #if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
2708 ConvertedBuffer
<char> m_convertedToChar
;
2710 #if !wxUSE_UNICODE_WCHAR
2711 ConvertedBuffer
<wchar_t> m_convertedToWChar
;
2714 #if wxUSE_UNICODE_UTF8
2715 // FIXME-UTF8: (try to) move this elsewhere (TLS) or solve differently
2716 // assigning to character pointer to by wxString::interator may
2717 // change the underlying wxStringImpl iterator, so we have to
2718 // keep track of all iterators and update them as necessary:
2719 struct wxStringIteratorNodeHead
2721 wxStringIteratorNodeHead() : ptr(NULL
) {}
2722 wxStringIteratorNode
*ptr
;
2724 // copying is disallowed as it would result in more than one pointer into
2725 // the same linked list
2726 DECLARE_NO_COPY_CLASS(wxStringIteratorNodeHead
)
2729 wxStringIteratorNodeHead m_iterators
;
2731 friend class WXDLLIMPEXP_FWD_BASE wxStringIteratorNode
;
2732 friend class WXDLLIMPEXP_FWD_BASE wxUniCharRef
;
2733 #endif // wxUSE_UNICODE_UTF8
2735 friend class WXDLLIMPEXP_FWD_BASE wxCStrData
;
2736 friend class wxStringInternalBuffer
;
2737 friend class wxStringInternalBufferLength
;
2740 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
2741 #pragma warning (default:4275)
2744 // string iterator operators that satisfy STL Random Access Iterator
2746 inline wxString::iterator
operator+(ptrdiff_t n
, wxString::iterator i
)
2748 inline wxString::const_iterator
operator+(ptrdiff_t n
, wxString::const_iterator i
)
2750 inline wxString::reverse_iterator
operator+(ptrdiff_t n
, wxString::reverse_iterator i
)
2752 inline wxString::const_reverse_iterator
operator+(ptrdiff_t n
, wxString::const_reverse_iterator i
)
2755 // notice that even though for many compilers the friend declarations above are
2756 // enough, from the point of view of C++ standard we must have the declarations
2757 // here as friend ones are not injected in the enclosing namespace and without
2758 // them the code fails to compile with conforming compilers such as xlC or g++4
2759 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
, const wxString
& string2
);
2760 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const char *psz
);
2761 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const wchar_t *pwz
);
2762 wxString WXDLLIMPEXP_BASE
operator+(const char *psz
, const wxString
& string
);
2763 wxString WXDLLIMPEXP_BASE
operator+(const wchar_t *pwz
, const wxString
& string
);
2765 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
2766 wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
2768 inline wxString
operator+(const wxString
& string
, wxUniCharRef ch
)
2769 { return string
+ (wxUniChar
)ch
; }
2770 inline wxString
operator+(const wxString
& string
, char ch
)
2771 { return string
+ wxUniChar(ch
); }
2772 inline wxString
operator+(const wxString
& string
, wchar_t ch
)
2773 { return string
+ wxUniChar(ch
); }
2774 inline wxString
operator+(wxUniCharRef ch
, const wxString
& string
)
2775 { return (wxUniChar
)ch
+ string
; }
2776 inline wxString
operator+(char ch
, const wxString
& string
)
2777 { return wxUniChar(ch
) + string
; }
2778 inline wxString
operator+(wchar_t ch
, const wxString
& string
)
2779 { return wxUniChar(ch
) + string
; }
2782 #define wxGetEmptyString() wxString()
2784 // ----------------------------------------------------------------------------
2785 // helper functions which couldn't be defined inline
2786 // ----------------------------------------------------------------------------
2791 #if wxUSE_UNICODE_WCHAR
2794 struct wxStringAsBufHelper
<char>
2796 static wxCharBuffer
Get(const wxString
& s
, size_t *len
)
2798 wxCharBuffer
buf(s
.mb_str());
2800 *len
= buf
? strlen(buf
) : 0;
2806 struct wxStringAsBufHelper
<wchar_t>
2808 static wxWCharBuffer
Get(const wxString
& s
, size_t *len
)
2812 return wxWCharBuffer::CreateNonOwned(s
.wx_str());
2816 #elif wxUSE_UNICODE_UTF8
2819 struct wxStringAsBufHelper
<char>
2821 static wxCharBuffer
Get(const wxString
& s
, size_t *len
)
2824 *len
= s
.utf8_length();
2825 return wxCharBuffer::CreateNonOwned(s
.wx_str());
2830 struct wxStringAsBufHelper
<wchar_t>
2832 static wxWCharBuffer
Get(const wxString
& s
, size_t *len
)
2834 wxWCharBuffer
wbuf(s
.wc_str());
2836 *len
= wxWcslen(wbuf
);
2841 #endif // Unicode build kind
2843 } // namespace wxPrivate
2845 // ----------------------------------------------------------------------------
2846 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
2847 // ----------------------------------------------------------------------------
2849 #if !wxUSE_STL_BASED_WXSTRING
2850 // string buffer for direct access to string data in their native
2852 class wxStringInternalBuffer
2855 typedef wxStringCharType CharType
;
2857 wxStringInternalBuffer(wxString
& str
, size_t lenWanted
= 1024)
2858 : m_str(str
), m_buf(NULL
)
2859 { m_buf
= m_str
.DoGetWriteBuf(lenWanted
); }
2861 ~wxStringInternalBuffer() { m_str
.DoUngetWriteBuf(); }
2863 operator wxStringCharType
*() const { return m_buf
; }
2867 wxStringCharType
*m_buf
;
2869 DECLARE_NO_COPY_CLASS(wxStringInternalBuffer
)
2872 class wxStringInternalBufferLength
2875 typedef wxStringCharType CharType
;
2877 wxStringInternalBufferLength(wxString
& str
, size_t lenWanted
= 1024)
2878 : m_str(str
), m_buf(NULL
), m_len(0), m_lenSet(false)
2880 m_buf
= m_str
.DoGetWriteBuf(lenWanted
);
2881 wxASSERT(m_buf
!= NULL
);
2884 ~wxStringInternalBufferLength()
2887 m_str
.DoUngetWriteBuf(m_len
);
2890 operator wxStringCharType
*() const { return m_buf
; }
2891 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
2895 wxStringCharType
*m_buf
;
2899 DECLARE_NO_COPY_CLASS(wxStringInternalBufferLength
)
2902 #endif // !wxUSE_STL_BASED_WXSTRING
2904 template<typename T
>
2905 class WXDLLIMPEXP_BASE wxStringTypeBufferBase
2910 wxStringTypeBufferBase(wxString
& str
, size_t lenWanted
= 1024)
2911 : m_str(str
), m_buf(lenWanted
)
2913 // for compatibility with old wxStringBuffer which provided direct
2914 // access to wxString internal buffer, initialize ourselves with the
2915 // string initial contents
2917 // FIXME-VC6: remove the ugly (CharType *)NULL and use normal
2918 // tchar_str<CharType>
2920 const wxCharTypeBuffer
<CharType
> buf(str
.tchar_str(&len
, (CharType
*)NULL
));
2923 if ( len
> lenWanted
)
2925 // in this case there is not enough space for terminating NUL,
2926 // ensure that we still put it there
2927 m_buf
.data()[lenWanted
] = 0;
2928 len
= lenWanted
- 1;
2931 memcpy(m_buf
.data(), buf
, (len
+ 1)*sizeof(CharType
));
2933 //else: conversion failed, this can happen when trying to get Unicode
2934 // string contents into a char string
2937 operator CharType
*() { return m_buf
.data(); }
2941 wxCharTypeBuffer
<CharType
> m_buf
;
2944 template<typename T
>
2945 class WXDLLIMPEXP_BASE wxStringTypeBufferLengthBase
2946 : public wxStringTypeBufferBase
<T
>
2949 wxStringTypeBufferLengthBase(wxString
& str
, size_t lenWanted
= 1024)
2950 : wxStringTypeBufferBase
<T
>(str
, lenWanted
),
2955 ~wxStringTypeBufferLengthBase()
2957 wxASSERT_MSG( this->m_lenSet
, "forgot to call SetLength()" );
2960 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
2967 template<typename T
>
2968 class wxStringTypeBuffer
: public wxStringTypeBufferBase
<T
>
2971 wxStringTypeBuffer(wxString
& str
, size_t lenWanted
= 1024)
2972 : wxStringTypeBufferBase
<T
>(str
, lenWanted
)
2975 ~wxStringTypeBuffer()
2977 this->m_str
.assign(this->m_buf
.data());
2980 DECLARE_NO_COPY_CLASS(wxStringTypeBuffer
)
2983 template<typename T
>
2984 class wxStringTypeBufferLength
: public wxStringTypeBufferLengthBase
<T
>
2987 wxStringTypeBufferLength(wxString
& str
, size_t lenWanted
= 1024)
2988 : wxStringTypeBufferLengthBase
<T
>(str
, lenWanted
)
2991 ~wxStringTypeBufferLength()
2993 this->m_str
.assign(this->m_buf
.data(), this->m_len
);
2996 DECLARE_NO_COPY_CLASS(wxStringTypeBufferLength
)
2999 #if wxUSE_STL_BASED_WXSTRING
3001 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase
<wxStringCharType
> )
3003 class wxStringInternalBuffer
: public wxStringTypeBufferBase
<wxStringCharType
>
3006 wxStringInternalBuffer(wxString
& str
, size_t lenWanted
= 1024)
3007 : wxStringTypeBufferBase
<wxStringCharType
>(str
, lenWanted
) {}
3008 ~wxStringInternalBuffer()
3009 { m_str
.m_impl
.assign(m_buf
.data()); }
3011 DECLARE_NO_COPY_CLASS(wxStringInternalBuffer
)
3014 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE(
3015 wxStringTypeBufferLengthBase
<wxStringCharType
> )
3017 class wxStringInternalBufferLength
3018 : public wxStringTypeBufferLengthBase
<wxStringCharType
>
3021 wxStringInternalBufferLength(wxString
& str
, size_t lenWanted
= 1024)
3022 : wxStringTypeBufferLengthBase
<wxStringCharType
>(str
, lenWanted
) {}
3024 ~wxStringInternalBufferLength()
3026 m_str
.m_impl
.assign(m_buf
.data(), m_len
);
3029 DECLARE_NO_COPY_CLASS(wxStringInternalBufferLength
)
3032 #endif // wxUSE_STL_BASED_WXSTRING
3035 #if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
3036 typedef wxStringTypeBuffer
<wxChar
> wxStringBuffer
;
3037 typedef wxStringTypeBufferLength
<wxChar
> wxStringBufferLength
;
3038 #else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
3039 typedef wxStringInternalBuffer wxStringBuffer
;
3040 typedef wxStringInternalBufferLength wxStringBufferLength
;
3041 #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
3043 #if wxUSE_UNICODE_UTF8
3044 typedef wxStringInternalBuffer wxUTF8StringBuffer
;
3045 typedef wxStringInternalBufferLength wxUTF8StringBufferLength
;
3046 #elif wxUSE_UNICODE_WCHAR
3048 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase
<char> )
3050 class WXDLLIMPEXP_BASE wxUTF8StringBuffer
: public wxStringTypeBufferBase
<char>
3053 wxUTF8StringBuffer(wxString
& str
, size_t lenWanted
= 1024)
3054 : wxStringTypeBufferBase
<char>(str
, lenWanted
) {}
3055 ~wxUTF8StringBuffer();
3057 DECLARE_NO_COPY_CLASS(wxUTF8StringBuffer
)
3060 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferLengthBase
<char> )
3062 class WXDLLIMPEXP_BASE wxUTF8StringBufferLength
3063 : public wxStringTypeBufferLengthBase
<char>
3066 wxUTF8StringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
3067 : wxStringTypeBufferLengthBase
<char>(str
, lenWanted
) {}
3068 ~wxUTF8StringBufferLength();
3070 DECLARE_NO_COPY_CLASS(wxUTF8StringBufferLength
)
3072 #endif // wxUSE_UNICODE_UTF8/wxUSE_UNICODE_WCHAR
3075 // ---------------------------------------------------------------------------
3076 // wxString comparison functions: operator versions are always case sensitive
3077 // ---------------------------------------------------------------------------
3079 #define wxCMP_WXCHAR_STRING(p, s, op) 0 op s.Cmp(p)
3081 wxDEFINE_ALL_COMPARISONS(const wxChar
*, const wxString
&, wxCMP_WXCHAR_STRING
)
3083 #undef wxCMP_WXCHAR_STRING
3085 inline bool operator==(const wxString
& s1
, const wxString
& s2
)
3086 { return s1
.IsSameAs(s2
); }
3087 inline bool operator!=(const wxString
& s1
, const wxString
& s2
)
3088 { return !s1
.IsSameAs(s2
); }
3089 inline bool operator< (const wxString
& s1
, const wxString
& s2
)
3090 { return s1
.Cmp(s2
) < 0; }
3091 inline bool operator> (const wxString
& s1
, const wxString
& s2
)
3092 { return s1
.Cmp(s2
) > 0; }
3093 inline bool operator<=(const wxString
& s1
, const wxString
& s2
)
3094 { return s1
.Cmp(s2
) <= 0; }
3095 inline bool operator>=(const wxString
& s1
, const wxString
& s2
)
3096 { return s1
.Cmp(s2
) >= 0; }
3098 inline bool operator==(const wxString
& s1
, const wxCStrData
& s2
)
3099 { return s1
== s2
.AsString(); }
3100 inline bool operator==(const wxCStrData
& s1
, const wxString
& s2
)
3101 { return s1
.AsString() == s2
; }
3102 inline bool operator!=(const wxString
& s1
, const wxCStrData
& s2
)
3103 { return s1
!= s2
.AsString(); }
3104 inline bool operator!=(const wxCStrData
& s1
, const wxString
& s2
)
3105 { return s1
.AsString() != s2
; }
3107 inline bool operator==(const wxString
& s1
, const wxWCharBuffer
& s2
)
3108 { return (s1
.Cmp((const wchar_t *)s2
) == 0); }
3109 inline bool operator==(const wxWCharBuffer
& s1
, const wxString
& s2
)
3110 { return (s2
.Cmp((const wchar_t *)s1
) == 0); }
3111 inline bool operator!=(const wxString
& s1
, const wxWCharBuffer
& s2
)
3112 { return (s1
.Cmp((const wchar_t *)s2
) != 0); }
3113 inline bool operator!=(const wxWCharBuffer
& s1
, const wxString
& s2
)
3114 { return (s2
.Cmp((const wchar_t *)s1
) != 0); }
3116 inline bool operator==(const wxString
& s1
, const wxCharBuffer
& s2
)
3117 { return (s1
.Cmp((const char *)s2
) == 0); }
3118 inline bool operator==(const wxCharBuffer
& s1
, const wxString
& s2
)
3119 { return (s2
.Cmp((const char *)s1
) == 0); }
3120 inline bool operator!=(const wxString
& s1
, const wxCharBuffer
& s2
)
3121 { return (s1
.Cmp((const char *)s2
) != 0); }
3122 inline bool operator!=(const wxCharBuffer
& s1
, const wxString
& s2
)
3123 { return (s2
.Cmp((const char *)s1
) != 0); }
3125 inline wxString
operator+(const wxString
& string
, const wxWCharBuffer
& buf
)
3126 { return string
+ (const wchar_t *)buf
; }
3127 inline wxString
operator+(const wxWCharBuffer
& buf
, const wxString
& string
)
3128 { return (const wchar_t *)buf
+ string
; }
3130 inline wxString
operator+(const wxString
& string
, const wxCharBuffer
& buf
)
3131 { return string
+ (const char *)buf
; }
3132 inline wxString
operator+(const wxCharBuffer
& buf
, const wxString
& string
)
3133 { return (const char *)buf
+ string
; }
3135 // comparison with char
3136 inline bool operator==(const wxUniChar
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
3137 inline bool operator==(const wxUniCharRef
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
3138 inline bool operator==(char c
, const wxString
& s
) { return s
.IsSameAs(c
); }
3139 inline bool operator==(wchar_t c
, const wxString
& s
) { return s
.IsSameAs(c
); }
3140 inline bool operator==(int c
, const wxString
& s
) { return s
.IsSameAs(c
); }
3141 inline bool operator==(const wxString
& s
, const wxUniChar
& c
) { return s
.IsSameAs(c
); }
3142 inline bool operator==(const wxString
& s
, const wxUniCharRef
& c
) { return s
.IsSameAs(c
); }
3143 inline bool operator==(const wxString
& s
, char c
) { return s
.IsSameAs(c
); }
3144 inline bool operator==(const wxString
& s
, wchar_t c
) { return s
.IsSameAs(c
); }
3145 inline bool operator!=(const wxUniChar
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
3146 inline bool operator!=(const wxUniCharRef
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
3147 inline bool operator!=(char c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
3148 inline bool operator!=(wchar_t c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
3149 inline bool operator!=(int c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
3150 inline bool operator!=(const wxString
& s
, const wxUniChar
& c
) { return !s
.IsSameAs(c
); }
3151 inline bool operator!=(const wxString
& s
, const wxUniCharRef
& c
) { return !s
.IsSameAs(c
); }
3152 inline bool operator!=(const wxString
& s
, char c
) { return !s
.IsSameAs(c
); }
3153 inline bool operator!=(const wxString
& s
, wchar_t c
) { return !s
.IsSameAs(c
); }
3155 // comparison with C string in Unicode build
3158 #define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s
3160 wxDEFINE_ALL_COMPARISONS(const char *, const wxString
&, wxCMP_CHAR_STRING
)
3162 #undef wxCMP_CHAR_STRING
3164 #endif // wxUSE_UNICODE
3166 // we also need to provide the operators for comparison with wxCStrData to
3167 // resolve ambiguity between operator(const wxChar *,const wxString &) and
3168 // operator(const wxChar *, const wxChar *) for "p == s.c_str()"
3170 // notice that these are (shallow) pointer comparisons, not (deep) string ones
3171 #define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar()
3172 #define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar()
3174 wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData
&, wxCMP_WCHAR_CSTRDATA
)
3175 wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData
&, wxCMP_CHAR_CSTRDATA
)
3177 #undef wxCMP_CHAR_CSTRDATA
3178 #undef wxCMP_WCHAR_CSTRDATA
3180 // ---------------------------------------------------------------------------
3181 // Implementation only from here until the end of file
3182 // ---------------------------------------------------------------------------
3184 #if wxUSE_STD_IOSTREAM
3186 #include "wx/iosfwrap.h"
3188 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxString
&);
3189 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxCStrData
&);
3190 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxCharBuffer
&);
3191 #ifndef __BORLANDC__
3192 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxWCharBuffer
&);
3195 #if wxUSE_UNICODE && defined(HAVE_WOSTREAM)
3197 WXDLLIMPEXP_BASE wxSTD wostream
& operator<<(wxSTD wostream
&, const wxString
&);
3198 WXDLLIMPEXP_BASE wxSTD wostream
& operator<<(wxSTD wostream
&, const wxCStrData
&);
3199 WXDLLIMPEXP_BASE wxSTD wostream
& operator<<(wxSTD wostream
&, const wxWCharBuffer
&);
3201 #endif // wxUSE_UNICODE && defined(HAVE_WOSTREAM)
3203 #endif // wxUSE_STD_IOSTREAM
3205 // ---------------------------------------------------------------------------
3206 // wxCStrData implementation
3207 // ---------------------------------------------------------------------------
3209 inline wxCStrData::wxCStrData(char *buf
)
3210 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
3211 inline wxCStrData::wxCStrData(wchar_t *buf
)
3212 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
3214 inline wxCStrData::wxCStrData(const wxCStrData
& data
)
3215 : m_str(data
.m_owned
? new wxString(*data
.m_str
) : data
.m_str
),
3216 m_offset(data
.m_offset
),
3217 m_owned(data
.m_owned
)
3221 inline wxCStrData::~wxCStrData()
3224 delete wx_const_cast(wxString
*, m_str
); // cast to silence warnings
3227 // simple cases for AsChar() and AsWChar(), the complicated ones are
3229 #if wxUSE_UNICODE_WCHAR
3230 inline const wchar_t* wxCStrData::AsWChar() const
3232 return m_str
->wx_str() + m_offset
;
3234 #endif // wxUSE_UNICODE_WCHAR
3237 inline const char* wxCStrData::AsChar() const
3239 return m_str
->wx_str() + m_offset
;
3241 #endif // !wxUSE_UNICODE
3243 #if wxUSE_UTF8_LOCALE_ONLY
3244 inline const char* wxCStrData::AsChar() const
3246 return wxStringOperations::AddToIter(m_str
->wx_str(), m_offset
);
3248 #endif // wxUSE_UTF8_LOCALE_ONLY
3250 inline const wxCharBuffer
wxCStrData::AsCharBuf() const
3253 return wxCharBuffer::CreateNonOwned(AsChar());
3255 return AsString().mb_str();
3259 inline const wxWCharBuffer
wxCStrData::AsWCharBuf() const
3261 #if wxUSE_UNICODE_WCHAR
3262 return wxWCharBuffer::CreateNonOwned(AsWChar());
3264 return AsString().wc_str();
3268 inline wxString
wxCStrData::AsString() const
3270 if ( m_offset
== 0 )
3273 return m_str
->Mid(m_offset
);
3276 inline const wxStringCharType
*wxCStrData::AsInternal() const
3278 #if wxUSE_UNICODE_UTF8
3279 return wxStringOperations::AddToIter(m_str
->wx_str(), m_offset
);
3281 return m_str
->wx_str() + m_offset
;
3285 inline wxUniChar
wxCStrData::operator*() const
3287 if ( m_str
->empty() )
3288 return wxUniChar(_T('\0'));
3290 return (*m_str
)[m_offset
];
3293 inline wxUniChar
wxCStrData::operator[](size_t n
) const
3295 // NB: we intentionally use operator[] and not at() here because the former
3296 // works for the terminating NUL while the latter does not
3297 return (*m_str
)[m_offset
+ n
];
3300 // ----------------------------------------------------------------------------
3301 // more wxCStrData operators
3302 // ----------------------------------------------------------------------------
3304 // we need to define those to allow "size_t pos = p - s.c_str()" where p is
3305 // some pointer into the string
3306 inline size_t operator-(const char *p
, const wxCStrData
& cs
)
3308 return p
- cs
.AsChar();
3311 inline size_t operator-(const wchar_t *p
, const wxCStrData
& cs
)
3313 return p
- cs
.AsWChar();
3316 // ----------------------------------------------------------------------------
3317 // implementation of wx[W]CharBuffer inline methods using wxCStrData
3318 // ----------------------------------------------------------------------------
3320 // FIXME-UTF8: move this to buffer.h
3321 inline wxCharBuffer::wxCharBuffer(const wxCStrData
& cstr
)
3322 : wxCharTypeBufferBase(cstr
.AsCharBuf())
3326 inline wxWCharBuffer::wxWCharBuffer(const wxCStrData
& cstr
)
3327 : wxCharTypeBufferBase(cstr
.AsWCharBuf())
3331 #if wxUSE_UNICODE_UTF8
3332 // ----------------------------------------------------------------------------
3333 // implementation of wxStringIteratorNode inline methods
3334 // ----------------------------------------------------------------------------
3336 void wxStringIteratorNode::DoSet(const wxString
*str
,
3337 wxStringImpl::const_iterator
*citer
,
3338 wxStringImpl::iterator
*iter
)
3346 m_next
= str
->m_iterators
.ptr
;
3347 wx_const_cast(wxString
*, m_str
)->m_iterators
.ptr
= this;
3349 m_next
->m_prev
= this;
3357 void wxStringIteratorNode::clear()
3360 m_next
->m_prev
= m_prev
;
3362 m_prev
->m_next
= m_next
;
3363 else if ( m_str
) // first in the list
3364 wx_const_cast(wxString
*, m_str
)->m_iterators
.ptr
= m_next
;
3366 m_next
= m_prev
= NULL
;
3371 #endif // wxUSE_UNICODE_UTF8
3373 #if WXWIN_COMPATIBILITY_2_8
3374 // lot of code out there doesn't explicitly include wx/crt.h, but uses
3375 // CRT wrappers that are now declared in wx/wxcrt.h and wx/wxcrtvararg.h,
3376 // so let's include this header now that wxString is defined and it's safe
3381 #endif // _WX_WXSTRING_H_