1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxString class
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
13 Efficient string class [more or less] compatible with MFC CString,
14 wxWidgets version 1 wxString and std::string and some handy functions
15 missing from string.h.
18 #ifndef _WX_WXSTRING_H__
19 #define _WX_WXSTRING_H__
21 // ----------------------------------------------------------------------------
23 // ----------------------------------------------------------------------------
25 #include "wx/defs.h" // everybody should include this
27 #if defined(__WXMAC__) || defined(__VISAGECPP__)
31 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
32 // problem in VACPP V4 with including stdlib.h multiple times
33 // strconv includes it anyway
46 #ifdef HAVE_STRCASECMP_IN_STRINGS_H
47 #include <strings.h> // for strcasecmp()
48 #endif // HAVE_STRCASECMP_IN_STRINGS_H
51 #include <StringMgr.h>
54 #include "wx/wxcrt.h" // for wxChar, wxStrlen() etc.
55 #include "wx/strvararg.h"
56 #include "wx/buffer.h" // for wxCharBuffer
57 #include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes
58 #include "wx/stringimpl.h"
59 #include "wx/stringops.h"
60 #include "wx/unichar.h"
62 class WXDLLIMPEXP_BASE wxString
;
64 // unless this symbol is predefined to disable the compatibility functions, do
66 #ifndef WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
67 #define WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER 1
70 // ---------------------------------------------------------------------------
72 // ---------------------------------------------------------------------------
74 // casts [unfortunately!] needed to call some broken functions which require
75 // "char *" instead of "const char *"
76 #define WXSTRINGCAST (wxChar *)(const wxChar *)
77 #define wxCSTRINGCAST (wxChar *)(const wxChar *)
78 #define wxMBSTRINGCAST (char *)(const char *)
79 #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *)
81 // like _T(), but for literals in wxString's internal representation, i.e.
82 // char* in UTF-8 build and wxChar* otherwise:
83 #if wxUSE_UNICODE_UTF8
84 #define wxSTRING_TEXT(str) str
86 #define wxSTRING_TEXT(str) _T(str)
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
93 #if WXWIN_COMPATIBILITY_2_6
95 // deprecated in favour of wxString::npos, don't use in new code
97 // maximum possible length for a string means "take all string" everywhere
98 #define wxSTRING_MAXLEN wxString::npos
100 #endif // WXWIN_COMPATIBILITY_2_6
102 // ---------------------------------------------------------------------------
103 // global functions complementing standard C string library replacements for
104 // strlen() and portable strcasecmp()
105 //---------------------------------------------------------------------------
107 #if WXWIN_COMPATIBILITY_2_8
108 // Use wxXXX() functions from wxcrt.h instead! These functions are for
109 // backwards compatibility only.
111 // checks whether the passed in pointer is NULL and if the string is empty
112 wxDEPRECATED( inline bool IsEmpty(const char *p
) );
113 inline bool IsEmpty(const char *p
) { return (!p
|| !*p
); }
115 // safe version of strlen() (returns 0 if passed NULL pointer)
116 wxDEPRECATED( inline size_t Strlen(const char *psz
) );
117 inline size_t Strlen(const char *psz
)
118 { return psz
? strlen(psz
) : 0; }
120 // portable strcasecmp/_stricmp
121 wxDEPRECATED( inline int Stricmp(const char *psz1
, const char *psz2
) );
122 inline int Stricmp(const char *psz1
, const char *psz2
)
124 #if defined(__VISUALC__) && defined(__WXWINCE__)
125 register char c1
, c2
;
127 c1
= tolower(*psz1
++);
128 c2
= tolower(*psz2
++);
129 } while ( c1
&& (c1
== c2
) );
132 #elif defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) )
133 return _stricmp(psz1
, psz2
);
134 #elif defined(__SC__)
135 return _stricmp(psz1
, psz2
);
136 #elif defined(__SALFORDC__)
137 return stricmp(psz1
, psz2
);
138 #elif defined(__BORLANDC__)
139 return stricmp(psz1
, psz2
);
140 #elif defined(__WATCOMC__)
141 return stricmp(psz1
, psz2
);
142 #elif defined(__DJGPP__)
143 return stricmp(psz1
, psz2
);
144 #elif defined(__EMX__)
145 return stricmp(psz1
, psz2
);
146 #elif defined(__WXPM__)
147 return stricmp(psz1
, psz2
);
148 #elif defined(__WXPALMOS__) || \
149 defined(HAVE_STRCASECMP_IN_STRING_H) || \
150 defined(HAVE_STRCASECMP_IN_STRINGS_H) || \
151 defined(__GNUWIN32__)
152 return strcasecmp(psz1
, psz2
);
153 #elif defined(__MWERKS__) && !defined(__INTEL__)
154 register char c1
, c2
;
156 c1
= tolower(*psz1
++);
157 c2
= tolower(*psz2
++);
158 } while ( c1
&& (c1
== c2
) );
162 // almost all compilers/libraries provide this function (unfortunately under
163 // different names), that's why we don't implement our own which will surely
164 // be more efficient than this code (uncomment to use):
166 register char c1, c2;
168 c1 = tolower(*psz1++);
169 c2 = tolower(*psz2++);
170 } while ( c1 && (c1 == c2) );
175 #error "Please define string case-insensitive compare for your OS/compiler"
176 #endif // OS/compiler
179 #endif // WXWIN_COMPATIBILITY_2_8
181 // ----------------------------------------------------------------------------
183 // ----------------------------------------------------------------------------
185 // Lightweight object returned by wxString::c_str() and implicitly convertible
186 // to either const char* or const wchar_t*.
187 class WXDLLIMPEXP_BASE wxCStrData
190 // Ctors; for internal use by wxString and wxCStrData only
191 wxCStrData(const wxString
*str
, size_t offset
= 0, bool owned
= false)
192 : m_str(str
), m_offset(offset
), m_owned(owned
) {}
195 // Ctor constructs the object from char literal; they are needed to make
196 // operator?: compile and they intentionally take char*, not const char*
197 inline wxCStrData(char *buf
);
198 inline wxCStrData(wchar_t *buf
);
199 inline wxCStrData(const wxCStrData
& data
);
201 inline ~wxCStrData();
203 // methods defined inline below must be declared inline or mingw32 3.4.5
204 // warns about "<symbol> defined locally after being referenced with
205 // dllimport linkage"
206 #if wxUSE_UNICODE_WCHAR
209 const wchar_t* AsWChar() const;
210 operator const wchar_t*() const { return AsWChar(); }
212 #if !wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY
215 const char* AsChar() const;
216 const unsigned char* AsUnsignedChar() const
217 { return (const unsigned char *) AsChar(); }
218 operator const char*() const { return AsChar(); }
219 operator const unsigned char*() const { return AsUnsignedChar(); }
221 operator const void*() const { return AsChar(); }
223 inline const wxCharBuffer
AsCharBuf() const;
224 inline const wxWCharBuffer
AsWCharBuf() const;
226 inline wxString
AsString() const;
228 // returns the value as C string in internal representation (equivalent
229 // to AsString().wx_str(), but more efficient)
230 const wxStringCharType
*AsInternal() const;
232 // allow expressions like "c_str()[0]":
233 inline wxUniChar
operator[](size_t n
) const;
234 wxUniChar
operator[](int n
) const { return operator[](size_t(n
)); }
235 wxUniChar
operator[](long n
) const { return operator[](size_t(n
)); }
236 #ifndef wxSIZE_T_IS_UINT
237 wxUniChar
operator[](unsigned int n
) const { return operator[](size_t(n
)); }
238 #endif // size_t != unsigned int
240 // these operators are needed to emulate the pointer semantics of c_str():
241 // expressions like "wxChar *p = str.c_str() + 1;" should continue to work
242 // (we need both versions to resolve ambiguities):
243 wxCStrData
operator+(int n
) const
244 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
245 wxCStrData
operator+(long n
) const
246 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
247 wxCStrData
operator+(size_t n
) const
248 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
250 // and these for "str.c_str() + n - 2":
251 wxCStrData
operator-(int n
) const
253 wxASSERT_MSG( n
<= (int)m_offset
,
254 _T("attempt to construct address before the beginning of the string") );
255 return wxCStrData(m_str
, m_offset
- n
, m_owned
);
257 wxCStrData
operator-(long n
) const
259 wxASSERT_MSG( n
<= (int)m_offset
,
260 _T("attempt to construct address before the beginning of the string") );
261 return wxCStrData(m_str
, m_offset
- n
, m_owned
);
263 wxCStrData
operator-(size_t n
) const
265 wxASSERT_MSG( n
<= m_offset
,
266 _T("attempt to construct address before the beginning of the string") );
267 return wxCStrData(m_str
, m_offset
- n
, m_owned
);
270 // this operator is needed to make expressions like "*c_str()" or
271 // "*(c_str() + 2)" work
272 inline wxUniChar
operator*() const;
275 const wxString
*m_str
;
279 friend class WXDLLIMPEXP_BASE wxString
;
282 // ----------------------------------------------------------------------------
283 // wxStringPrintfMixin
284 // ---------------------------------------------------------------------------
286 // NB: VC6 has a bug that causes linker errors if you have template methods
287 // in a class using __declspec(dllimport). The solution is to split such
288 // class into two classes, one that contains the template methods and does
289 // *not* use WXDLLIMPEXP_BASE and another class that contains the rest
290 // (with DLL linkage).
292 // We only do this for VC6 here, because the code is less efficient
293 // (Printf() has to use dynamic_cast<>) and because OpenWatcom compiler
294 // cannot compile this code.
296 #if defined(__VISUALC__) && __VISUALC__ < 1300
297 #define wxNEEDS_WXSTRING_PRINTF_MIXIN
300 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
301 // this class contains implementation of wxString's vararg methods, it's
302 // exported from wxBase DLL
303 class WXDLLIMPEXP_BASE wxStringPrintfMixinBase
306 wxStringPrintfMixinBase() {}
308 #if !wxUSE_UTF8_LOCALE_ONLY
309 int DoPrintfWchar(const wxChar
*format
, ...);
310 static wxString
DoFormatWchar(const wxChar
*format
, ...);
312 #if wxUSE_UNICODE_UTF8
313 int DoPrintfUtf8(const char *format
, ...);
314 static wxString
DoFormatUtf8(const char *format
, ...);
318 // this class contains template wrappers for wxString's vararg methods, it's
319 // intentionally *not* exported from the DLL in order to fix the VC6 bug
321 class wxStringPrintfMixin
: public wxStringPrintfMixinBase
324 // to further complicate things, we can't return wxString from
325 // wxStringPrintfMixin::Format() because wxString is not yet declared at
326 // this point; the solution is to use this fake type trait template - this
327 // way the compiler won't know the return type until Format() is used
328 // (this doesn't compile with Watcom, but VC6 compiles it just fine):
329 template<typename T
> struct StringReturnType
331 typedef wxString type
;
335 // these are duplicated wxString methods, they're also declared below
336 // if !wxNEEDS_WXSTRING_PRINTF_MIXIN:
338 // static wxString Format(const wString& format, ...) ATTRIBUTE_PRINTF_1;
339 WX_DEFINE_VARARG_FUNC_SANS_N0(static typename StringReturnType
<T1
>::type
,
340 Format
, 1, (const wxFormatString
&),
341 DoFormatWchar
, DoFormatUtf8
)
342 // We have to implement the version without template arguments manually
343 // because of the StringReturnType<> hack, although WX_DEFINE_VARARG_FUNC
344 // normally does it itself. It has to be a template so that we can use
345 // the hack, even though there's no real template parameter:
346 struct FormatDummyArg
{} ;
349 inline static typename StringReturnType
<T
>::type
350 Format(const wxFormatString
& fmt
, FormatDummyArg dummy
= FormatDummyArg())
352 return DoFormatWchar(fmt
);
355 // int Printf(const wxString& format, ...);
356 WX_DEFINE_VARARG_FUNC(int, Printf
, 1, (const wxFormatString
&),
357 DoPrintfWchar
, DoPrintfUtf8
)
358 // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2;
359 WX_DEFINE_VARARG_FUNC(int, sprintf
, 1, (const wxFormatString
&),
360 DoPrintfWchar
, DoPrintfUtf8
)
363 wxStringPrintfMixin() : wxStringPrintfMixinBase() {}
365 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
368 // ----------------------------------------------------------------------------
369 // wxString: string class trying to be compatible with std::string, MFC
370 // CString and wxWindows 1.x wxString all at once
371 // ---------------------------------------------------------------------------
373 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
374 // "non dll-interface class 'wxStringPrintfMixin' used as base interface
375 // for dll-interface class 'wxString'" -- this is OK in our case
376 #pragma warning (disable:4275)
379 #if wxUSE_UNICODE_UTF8
380 // see the comment near wxString::iterator for why we need this
381 struct WXDLLIMPEXP_BASE wxStringIteratorNode
383 inline wxStringIteratorNode(const wxString
*str
,
384 wxStringImpl::const_iterator
*citer
);
385 inline wxStringIteratorNode(const wxString
*str
,
386 wxStringImpl::iterator
*iter
);
387 inline ~wxStringIteratorNode();
389 const wxString
*m_str
;
390 wxStringImpl::const_iterator
*m_citer
;
391 wxStringImpl::iterator
*m_iter
;
392 wxStringIteratorNode
*m_prev
, *m_next
;
394 #endif // wxUSE_UNICODE_UTF8
396 class WXDLLIMPEXP_BASE wxString
397 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
398 : public wxStringPrintfMixin
401 // NB: special care was taken in arranging the member functions in such order
402 // that all inline functions can be effectively inlined, verify that all
403 // performance critical functions are still inlined if you change order!
405 // an 'invalid' value for string index, moved to this place due to a CW bug
406 static const size_t npos
;
409 // if we hadn't made these operators private, it would be possible to
410 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
411 // converted to char in C and we do have operator=(char)
413 // NB: we don't need other versions (short/long and unsigned) as attempt
414 // to assign another numeric type to wxString will now result in
415 // ambiguity between operator=(char) and operator=(int)
416 wxString
& operator=(int);
418 // these methods are not implemented - there is _no_ conversion from int to
419 // string, you're doing something wrong if the compiler wants to call it!
421 // try `s << i' or `s.Printf("%d", i)' instead
425 // buffer for holding temporary substring when using any of the methods
426 // that take (char*,size_t) or (wchar_t*,size_t) arguments:
428 struct SubstrBufFromType
433 SubstrBufFromType(const T
& data_
, size_t len_
)
434 : data(data_
), len(len_
) {}
437 #if wxUSE_UNICODE_UTF8
438 // even char* -> char* needs conversion, from locale charset to UTF-8
439 typedef SubstrBufFromType
<wxCharBuffer
> SubstrBufFromWC
;
440 typedef SubstrBufFromType
<wxCharBuffer
> SubstrBufFromMB
;
441 #elif wxUSE_UNICODE_WCHAR
442 typedef SubstrBufFromType
<const wchar_t*> SubstrBufFromWC
;
443 typedef SubstrBufFromType
<wxWCharBuffer
> SubstrBufFromMB
;
445 typedef SubstrBufFromType
<const char*> SubstrBufFromMB
;
446 typedef SubstrBufFromType
<wxCharBuffer
> SubstrBufFromWC
;
450 // Functions implementing primitive operations on string data; wxString
451 // methods and iterators are implemented in terms of it. The differences
452 // between UTF-8 and wchar_t* representations of the string are mostly
455 #if wxUSE_UNICODE_UTF8
456 static SubstrBufFromMB
ConvertStr(const char *psz
, size_t nLength
,
457 const wxMBConv
& conv
);
458 static SubstrBufFromWC
ConvertStr(const wchar_t *pwz
, size_t nLength
,
459 const wxMBConv
& conv
);
460 #elif wxUSE_UNICODE_WCHAR
461 static SubstrBufFromMB
ConvertStr(const char *psz
, size_t nLength
,
462 const wxMBConv
& conv
);
464 static SubstrBufFromWC
ConvertStr(const wchar_t *pwz
, size_t nLength
,
465 const wxMBConv
& conv
);
468 #if !wxUSE_UNICODE_UTF8 // wxUSE_UNICODE_WCHAR or !wxUSE_UNICODE
469 // returns C string encoded as the implementation expects:
471 static const wchar_t* ImplStr(const wchar_t* str
)
472 { return str
? str
: wxT(""); }
473 static const SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
474 { return SubstrBufFromWC(str
, (str
&& n
== npos
) ? wxWcslen(str
) : n
); }
475 static wxWCharBuffer
ImplStr(const char* str
,
476 const wxMBConv
& conv
= wxConvLibc
)
477 { return ConvertStr(str
, npos
, conv
).data
; }
478 static SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
479 const wxMBConv
& conv
= wxConvLibc
)
480 { return ConvertStr(str
, n
, conv
); }
482 static const char* ImplStr(const char* str
,
483 const wxMBConv
& WXUNUSED(conv
) = wxConvLibc
)
484 { return str
? str
: ""; }
485 static const SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
486 const wxMBConv
& WXUNUSED(conv
) = wxConvLibc
)
487 { return SubstrBufFromMB(str
, (str
&& n
== npos
) ? wxStrlen(str
) : n
); }
488 static wxCharBuffer
ImplStr(const wchar_t* str
)
489 { return ConvertStr(str
, npos
, wxConvLibc
).data
; }
490 static SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
491 { return ConvertStr(str
, n
, wxConvLibc
); }
494 // translates position index in wxString to/from index in underlying
496 static size_t PosToImpl(size_t pos
) { return pos
; }
497 static void PosLenToImpl(size_t pos
, size_t len
,
498 size_t *implPos
, size_t *implLen
)
499 { *implPos
= pos
; *implLen
= len
; }
500 static size_t LenToImpl(size_t len
) { return len
; }
501 static size_t PosFromImpl(size_t pos
) { return pos
; }
503 #else // wxUSE_UNICODE_UTF8
505 static wxCharBuffer
ImplStr(const char* str
,
506 const wxMBConv
& conv
= wxConvLibc
)
507 { return ConvertStr(str
, npos
, conv
).data
; }
508 static SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
509 const wxMBConv
& conv
= wxConvLibc
)
510 { return ConvertStr(str
, n
, conv
); }
512 static wxCharBuffer
ImplStr(const wchar_t* str
)
513 { return ConvertStr(str
, npos
, wxMBConvUTF8()).data
; }
514 static SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
515 { return ConvertStr(str
, n
, wxMBConvUTF8()); }
517 size_t PosToImpl(size_t pos
) const
519 if ( pos
== 0 || pos
== npos
)
522 return (begin() + pos
).impl() - m_impl
.begin();
525 void PosLenToImpl(size_t pos
, size_t len
, size_t *implPos
, size_t *implLen
) const;
527 size_t LenToImpl(size_t len
) const
530 PosLenToImpl(0, len
, &pos
, &len2
);
534 size_t PosFromImpl(size_t pos
) const
536 if ( pos
== 0 || pos
== npos
)
539 return const_iterator(this, m_impl
.begin() + pos
) - begin();
541 #endif // !wxUSE_UNICODE_UTF8/wxUSE_UNICODE_UTF8
545 typedef wxUniChar value_type
;
546 typedef wxUniChar char_type
;
547 typedef wxUniCharRef reference
;
548 typedef wxChar
* pointer
;
549 typedef const wxChar
* const_pointer
;
551 typedef size_t size_type
;
552 typedef wxUniChar const_reference
;
555 #if wxUSE_UNICODE_UTF8
556 // random access is not O(1), as required by Random Access Iterator
557 #define WX_STR_ITERATOR_TAG std::bidirectional_iterator_tag
559 #define WX_STR_ITERATOR_TAG std::random_access_iterator_tag
562 #define WX_STR_ITERATOR_TAG void /* dummy type */
565 #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, reference_type) \
567 typedef wxStringImpl::iterator_name underlying_iterator; \
569 typedef WX_STR_ITERATOR_TAG iterator_category; \
570 typedef wxUniChar value_type; \
571 typedef int difference_type; \
572 typedef reference_type reference; \
573 typedef pointer_type pointer; \
575 reference operator[](size_t n) const { return *(*this + n); } \
577 iterator_name& operator++() \
578 { wxStringOperations::IncIter(m_cur); return *this; } \
579 iterator_name& operator--() \
580 { wxStringOperations::DecIter(m_cur); return *this; } \
581 iterator_name operator++(int) \
583 iterator_name tmp = *this; \
584 wxStringOperations::IncIter(m_cur); \
587 iterator_name operator--(int) \
589 iterator_name tmp = *this; \
590 wxStringOperations::DecIter(m_cur); \
594 iterator_name& operator+=(int n) \
596 m_cur = wxStringOperations::AddToIter(m_cur, n); \
599 iterator_name& operator+=(size_t n) \
601 m_cur = wxStringOperations::AddToIter(m_cur, (int)n); \
604 iterator_name& operator-=(int n) \
606 m_cur = wxStringOperations::AddToIter(m_cur, -n); \
609 iterator_name& operator-=(size_t n) \
611 m_cur = wxStringOperations::AddToIter(m_cur, -(int)n); \
615 difference_type operator-(const iterator_name& i) const \
616 { return wxStringOperations::DiffIters(m_cur, i.m_cur); } \
618 bool operator==(const iterator_name& i) const \
619 { return m_cur == i.m_cur; } \
620 bool operator!=(const iterator_name& i) const \
621 { 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; } \
629 bool operator>=(const iterator_name& i) const \
630 { return m_cur >= i.m_cur; } \
633 /* for internal wxString use only: */ \
634 underlying_iterator impl() const { return m_cur; } \
636 friend class WXDLLIMPEXP_BASE wxString; \
637 friend class WXDLLIMPEXP_BASE wxCStrData; \
640 underlying_iterator m_cur
642 class const_iterator
;
644 #if wxUSE_UNICODE_UTF8
645 // NB: In UTF-8 build, (non-const) iterator needs to keep reference
646 // to the underlying wxStringImpl, because UTF-8 is variable-length
647 // encoding and changing the value pointer to by an iterator (using
648 // its operator*) requires calling wxStringImpl::replace() if the old
649 // and new values differ in their encoding's length.
651 // Furthermore, the replace() call may invalid all iterators for the
652 // string, so we have to keep track of outstanding iterators and update
653 // them if replace() happens.
655 // This is implemented by maintaining linked list of iterators for every
656 // string and traversing it in wxUniCharRef::operator=(). Head of the
657 // list is stored in wxString. (FIXME-UTF8)
661 WX_STR_ITERATOR_IMPL(iterator
, wxChar
*, wxUniCharRef
);
664 iterator(const iterator
& i
)
665 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
667 reference
operator*()
668 { return wxUniCharRef::CreateForString(m_node
, m_cur
); }
670 iterator
operator+(int n
) const
671 { return iterator(str(), wxStringOperations::AddToIter(m_cur
, n
)); }
672 iterator
operator+(size_t n
) const
673 { return iterator(str(), wxStringOperations::AddToIter(m_cur
, (int)n
)); }
674 iterator
operator-(int n
) const
675 { return iterator(str(), wxStringOperations::AddToIter(m_cur
, -n
)); }
676 iterator
operator-(size_t n
) const
677 { return iterator(str(), wxStringOperations::AddToIter(m_cur
, -(int)n
)); }
680 iterator(wxString
*str
, underlying_iterator ptr
)
681 : m_cur(ptr
), m_node(str
, &m_cur
) {}
682 iterator(wxString
& str
, underlying_iterator ptr
)
683 : m_cur(ptr
), m_node(&str
, &m_cur
) {}
685 wxString
* str() const { return wx_const_cast(wxString
*, m_node
.m_str
); }
687 wxStringIteratorNode m_node
;
689 friend class const_iterator
;
694 // NB: reference_type is intentionally value, not reference, the character
695 // may be encoded differently in wxString data:
696 WX_STR_ITERATOR_IMPL(const_iterator
, const wxChar
*, wxUniChar
);
699 const_iterator(const const_iterator
& i
)
700 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
701 const_iterator(const iterator
& i
)
702 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
704 reference
operator*() const
705 { return wxStringOperations::DecodeChar(m_cur
); }
707 const_iterator
operator+(int n
) const
708 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur
, n
)); }
709 const_iterator
operator+(size_t n
) const
710 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur
, (int)n
)); }
711 const_iterator
operator-(int n
) const
712 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur
, -n
)); }
713 const_iterator
operator-(size_t n
) const
714 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur
, -(int)n
)); }
717 // for internal wxString use only:
718 const_iterator(const wxString
*str
, underlying_iterator ptr
)
719 : m_cur(ptr
), m_node(str
, &m_cur
) {}
720 const_iterator(const wxString
& str
, underlying_iterator ptr
)
721 : m_cur(ptr
), m_node(&str
, &m_cur
) {}
723 const wxString
* str() const { return m_node
.m_str
; }
725 wxStringIteratorNode m_node
;
728 size_t IterToImplPos(wxString::iterator i
) const
729 { return wxStringImpl::const_iterator(i
.impl()) - m_impl
.begin(); }
731 #else // !wxUSE_UNICODE_UTF8
735 WX_STR_ITERATOR_IMPL(iterator
, wxChar
*, wxUniCharRef
);
738 iterator(const iterator
& i
) : m_cur(i
.m_cur
) {}
740 reference
operator*()
741 { return wxUniCharRef::CreateForString(m_cur
); }
743 iterator
operator+(int n
) const
744 { return iterator(wxStringOperations::AddToIter(m_cur
, n
)); }
745 iterator
operator+(size_t n
) const
746 { return iterator(wxStringOperations::AddToIter(m_cur
, (int)n
)); }
747 iterator
operator-(int n
) const
748 { return iterator(wxStringOperations::AddToIter(m_cur
, -n
)); }
749 iterator
operator-(size_t n
) const
750 { return iterator(wxStringOperations::AddToIter(m_cur
, -(int)n
)); }
753 // for internal wxString use only:
754 iterator(underlying_iterator ptr
) : m_cur(ptr
) {}
755 iterator(wxString
*WXUNUSED(str
), underlying_iterator ptr
) : m_cur(ptr
) {}
757 friend class const_iterator
;
762 // NB: reference_type is intentionally value, not reference, the character
763 // may be encoded differently in wxString data:
764 WX_STR_ITERATOR_IMPL(const_iterator
, const wxChar
*, wxUniChar
);
767 const_iterator(const const_iterator
& i
) : m_cur(i
.m_cur
) {}
768 const_iterator(const iterator
& i
) : m_cur(i
.m_cur
) {}
770 reference
operator*() const
771 { return wxStringOperations::DecodeChar(m_cur
); }
773 const_iterator
operator+(int n
) const
774 { return const_iterator(wxStringOperations::AddToIter(m_cur
, n
)); }
775 const_iterator
operator+(size_t n
) const
776 { return const_iterator(wxStringOperations::AddToIter(m_cur
, (int)n
)); }
777 const_iterator
operator-(int n
) const
778 { return const_iterator(wxStringOperations::AddToIter(m_cur
, -n
)); }
779 const_iterator
operator-(size_t n
) const
780 { return const_iterator(wxStringOperations::AddToIter(m_cur
, -(int)n
)); }
783 // for internal wxString use only:
784 const_iterator(underlying_iterator ptr
) : m_cur(ptr
) {}
785 const_iterator(const wxString
*WXUNUSED(str
), underlying_iterator ptr
)
788 #endif // wxUSE_UNICODE_UTF8/!wxUSE_UNICODE_UTF8
790 #undef WX_STR_ITERATOR_TAG
791 #undef WX_STR_ITERATOR_IMPL
793 friend class iterator
;
794 friend class const_iterator
;
796 template <typename T
>
797 class reverse_iterator_impl
800 typedef T iterator_type
;
802 typedef typename
T::iterator_category iterator_category
;
803 typedef typename
T::value_type value_type
;
804 typedef typename
T::difference_type difference_type
;
805 typedef typename
T::reference reference
;
806 typedef typename
T::pointer
*pointer
;
808 reverse_iterator_impl(iterator_type i
) : m_cur(i
) {}
809 reverse_iterator_impl(const reverse_iterator_impl
& ri
)
812 iterator_type
base() const { return m_cur
; }
814 reference
operator*() const { return *(m_cur
-1); }
815 reference
operator[](size_t n
) const { return *(*this + n
); }
817 reverse_iterator_impl
& operator++()
818 { --m_cur
; return *this; }
819 reverse_iterator_impl
operator++(int)
820 { reverse_iterator_impl tmp
= *this; --m_cur
; return tmp
; }
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
; }
826 // NB: explicit <T> in the functions below is to keep BCC 5.5 happy
827 reverse_iterator_impl
operator+(int n
) const
828 { return reverse_iterator_impl
<T
>(m_cur
- n
); }
829 reverse_iterator_impl
operator+(size_t n
) const
830 { return reverse_iterator_impl
<T
>(m_cur
- n
); }
831 reverse_iterator_impl
operator-(int n
) const
832 { return reverse_iterator_impl
<T
>(m_cur
+ n
); }
833 reverse_iterator_impl
operator-(size_t n
) const
834 { return reverse_iterator_impl
<T
>(m_cur
+ n
); }
835 reverse_iterator_impl
operator+=(int n
)
836 { m_cur
-= n
; return *this; }
837 reverse_iterator_impl
operator+=(size_t n
)
838 { m_cur
-= n
; return *this; }
839 reverse_iterator_impl
operator-=(int n
)
840 { m_cur
+= n
; return *this; }
841 reverse_iterator_impl
operator-=(size_t n
)
842 { m_cur
+= n
; return *this; }
844 unsigned operator-(const reverse_iterator_impl
& i
) const
845 { return i
.m_cur
- m_cur
; }
847 bool operator==(const reverse_iterator_impl
& ri
) const
848 { return m_cur
== ri
.m_cur
; }
849 bool operator!=(const reverse_iterator_impl
& ri
) const
850 { return !(*this == ri
); }
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
; }
856 bool operator<=(const reverse_iterator_impl
& i
) const
857 { return m_cur
>= i
.m_cur
; }
858 bool operator>=(const reverse_iterator_impl
& i
) const
859 { return m_cur
<= i
.m_cur
; }
865 typedef reverse_iterator_impl
<iterator
> reverse_iterator
;
866 typedef reverse_iterator_impl
<const_iterator
> const_reverse_iterator
;
869 // used to transform an expression built using c_str() (and hence of type
870 // wxCStrData) to an iterator into the string
871 static const_iterator
CreateConstIterator(const wxCStrData
& data
)
873 return const_iterator(data
.m_str
,
874 (data
.m_str
->begin() + data
.m_offset
).impl());
877 // in UTF-8 STL build, creation from std::string requires conversion under
878 // non-UTF8 locales, so we can't have and use wxString(wxStringImpl) ctor;
879 // instead we define dummy type that lets us have wxString ctor for creation
880 // from wxStringImpl that couldn't be used by user code (in all other builds,
881 // "standard" ctors can be used):
882 #if wxUSE_UNICODE_UTF8 && wxUSE_STL_BASED_WXSTRING
883 struct CtorFromStringImplTag
{};
885 wxString(CtorFromStringImplTag
* WXUNUSED(dummy
), const wxStringImpl
& src
)
888 static wxString
FromImpl(const wxStringImpl
& src
)
889 { return wxString((CtorFromStringImplTag
*)NULL
, src
); }
891 #if !wxUSE_STL_BASED_WXSTRING
892 wxString(const wxStringImpl
& src
) : m_impl(src
) { }
893 // else: already defined as wxString(wxStdString) below
895 static wxString
FromImpl(const wxStringImpl
& src
) { return wxString(src
); }
899 // constructors and destructor
900 // ctor for an empty string
904 wxString(const wxString
& stringSrc
) : m_impl(stringSrc
.m_impl
) { }
906 // string containing nRepeat copies of ch
907 wxString(wxUniChar ch
, size_t nRepeat
= 1)
908 { assign(nRepeat
, ch
); }
909 wxString(size_t nRepeat
, wxUniChar ch
)
910 { assign(nRepeat
, ch
); }
911 wxString(wxUniCharRef ch
, size_t nRepeat
= 1)
912 { assign(nRepeat
, ch
); }
913 wxString(size_t nRepeat
, wxUniCharRef ch
)
914 { assign(nRepeat
, ch
); }
915 wxString(char ch
, size_t nRepeat
= 1)
916 { assign(nRepeat
, ch
); }
917 wxString(size_t nRepeat
, char ch
)
918 { assign(nRepeat
, ch
); }
919 wxString(wchar_t ch
, size_t nRepeat
= 1)
920 { assign(nRepeat
, ch
); }
921 wxString(size_t nRepeat
, wchar_t ch
)
922 { assign(nRepeat
, ch
); }
924 // ctors from char* strings:
925 wxString(const char *psz
)
926 : m_impl(ImplStr(psz
)) {}
927 wxString(const char *psz
, const wxMBConv
& conv
)
928 : m_impl(ImplStr(psz
, conv
)) {}
929 wxString(const char *psz
, size_t nLength
)
930 { assign(psz
, nLength
); }
931 wxString(const char *psz
, const wxMBConv
& conv
, size_t nLength
)
933 SubstrBufFromMB
str(ImplStr(psz
, nLength
, conv
));
934 m_impl
.assign(str
.data
, str
.len
);
937 // and unsigned char*:
938 wxString(const unsigned char *psz
)
939 : m_impl(ImplStr((const char*)psz
)) {}
940 wxString(const unsigned char *psz
, const wxMBConv
& conv
)
941 : m_impl(ImplStr((const char*)psz
, conv
)) {}
942 wxString(const unsigned char *psz
, size_t nLength
)
943 { assign((const char*)psz
, nLength
); }
944 wxString(const unsigned char *psz
, const wxMBConv
& conv
, size_t nLength
)
946 SubstrBufFromMB
str(ImplStr((const char*)psz
, nLength
, conv
));
947 m_impl
.assign(str
.data
, str
.len
);
950 // ctors from wchar_t* strings:
951 wxString(const wchar_t *pwz
)
952 : m_impl(ImplStr(pwz
)) {}
953 wxString(const wchar_t *pwz
, const wxMBConv
& WXUNUSED(conv
))
954 : m_impl(ImplStr(pwz
)) {}
955 wxString(const wchar_t *pwz
, size_t nLength
)
956 { assign(pwz
, nLength
); }
957 wxString(const wchar_t *pwz
, const wxMBConv
& WXUNUSED(conv
), size_t nLength
)
958 { assign(pwz
, nLength
); }
960 wxString(const wxCharBuffer
& buf
)
961 { assign(buf
.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
962 wxString(const wxWCharBuffer
& buf
)
963 { assign(buf
.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
965 wxString(const wxCStrData
& cstr
)
966 : m_impl(cstr
.AsString().m_impl
) { }
968 // as we provide both ctors with this signature for both char and unsigned
969 // char string, we need to provide one for wxCStrData to resolve ambiguity
970 wxString(const wxCStrData
& cstr
, size_t nLength
)
971 : m_impl(cstr
.AsString().Mid(0, nLength
).m_impl
) {}
973 // and because wxString is convertible to wxCStrData and const wxChar *
974 // we also need to provide this one
975 wxString(const wxString
& str
, size_t nLength
)
976 { assign(str
, nLength
); }
978 // even if we're not built with wxUSE_STL == 1 it is very convenient to allow
979 // implicit conversions from std::string to wxString and vice verse as this
980 // allows to use the same strings in non-GUI and GUI code, however we don't
981 // want to unconditionally add this ctor as it would make wx lib dependent on
982 // libstdc++ on some Linux versions which is bad, so instead we ask the
983 // client code to define this wxUSE_STD_STRING symbol if they need it
985 #if wxUSE_UNICODE_WCHAR
986 wxString(const wxStdWideString
& str
) : m_impl(str
) {}
987 #else // UTF-8 or ANSI
988 wxString(const wxStdWideString
& str
)
989 { assign(str
.c_str(), str
.length()); }
992 #if !wxUSE_UNICODE // ANSI build
993 // FIXME-UTF8: do this in UTF8 build #if wxUSE_UTF8_LOCALE_ONLY, too
994 wxString(const std::string
& str
) : m_impl(str
) {}
996 wxString(const std::string
& str
)
997 { assign(str
.c_str(), str
.length()); }
1000 #if wxUSE_UNICODE_WCHAR && wxUSE_STL_BASED_WXSTRING
1001 // wxStringImpl is std::string in the encoding we want
1002 operator const wxStdWideString
&() const { return m_impl
; }
1004 // wxStringImpl is either not std::string or needs conversion
1005 operator wxStdWideString() const
1006 // FIXME-UTF8: broken for embedded NULs
1007 { return wxStdWideString(wc_str()); }
1010 #if (!wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY) && wxUSE_STL_BASED_WXSTRING
1011 // wxStringImpl is std::string in the encoding we want
1012 operator const std::string
&() const { return m_impl
; }
1014 // wxStringImpl is either not std::string or needs conversion
1015 operator std::string() const
1016 // FIXME-UTF8: broken for embedded NULs
1017 { return std::string(mb_str()); }
1021 // first valid index position
1022 const_iterator
begin() const { return const_iterator(this, m_impl
.begin()); }
1023 iterator
begin() { return iterator(this, m_impl
.begin()); }
1024 // position one after the last valid one
1025 const_iterator
end() const { return const_iterator(this, m_impl
.end()); }
1026 iterator
end() { return iterator(this, m_impl
.end()); }
1028 // first element of the reversed string
1029 const_reverse_iterator
rbegin() const
1030 { return const_reverse_iterator(end()); }
1031 reverse_iterator
rbegin()
1032 { return reverse_iterator(end()); }
1033 // one beyond the end of the reversed string
1034 const_reverse_iterator
rend() const
1035 { return const_reverse_iterator(begin()); }
1036 reverse_iterator
rend()
1037 { return reverse_iterator(begin()); }
1039 // std::string methods:
1040 #if wxUSE_UNICODE_UTF8
1041 size_t length() const { return end() - begin(); } // FIXME-UTF8: optimize!
1043 size_t length() const { return m_impl
.length(); }
1046 size_type
size() const { return length(); }
1047 size_type
max_size() const { return npos
; }
1049 bool empty() const { return m_impl
.empty(); }
1051 size_type
capacity() const { return m_impl
.capacity(); } // FIXME-UTF8
1052 void reserve(size_t sz
) { m_impl
.reserve(sz
); } // FIXME-UTF8
1054 void resize(size_t nSize
, wxUniChar ch
= wxT('\0'))
1056 #if wxUSE_UNICODE_UTF8
1057 if ( !ch
.IsAscii() )
1059 size_t len
= length();
1062 else if ( nSize
< len
)
1065 append(nSize
- len
, ch
);
1069 m_impl
.resize(nSize
, (wxStringCharType
)ch
);
1072 wxString
substr(size_t nStart
= 0, size_t nLen
= npos
) const
1075 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
1076 return FromImpl(m_impl
.substr(pos
, len
));
1079 // generic attributes & operations
1080 // as standard strlen()
1081 size_t Len() const { return length(); }
1082 // string contains any characters?
1083 bool IsEmpty() const { return empty(); }
1084 // empty string is "false", so !str will return true
1085 bool operator!() const { return empty(); }
1086 // truncate the string to given length
1087 wxString
& Truncate(size_t uiLen
);
1088 // empty string contents
1093 wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") );
1095 // empty the string and free memory
1098 wxString
tmp(wxEmptyString
);
1103 // Is an ascii value
1104 bool IsAscii() const;
1106 bool IsNumber() const;
1108 bool IsWord() const;
1110 // data access (all indexes are 0 based)
1112 wxUniChar
at(size_t n
) const
1113 { return *(begin() + n
); } // FIXME-UTF8: optimize?
1114 wxUniChar
GetChar(size_t n
) const
1116 // read/write access
1117 wxUniCharRef
at(size_t n
)
1118 { return *(begin() + n
); } // FIXME-UTF8: optimize?
1119 wxUniCharRef
GetWritableChar(size_t n
)
1122 void SetChar(size_t n
, wxUniChar ch
)
1125 // get last character
1126 wxUniChar
Last() const
1128 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1132 // get writable last character
1135 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1140 Note that we we must define all of the overloads below to avoid
1141 ambiguity when using str[0].
1143 wxUniChar
operator[](int n
) const
1145 wxUniChar
operator[](long n
) const
1147 wxUniChar
operator[](size_t n
) const
1149 #ifndef wxSIZE_T_IS_UINT
1150 wxUniChar
operator[](unsigned int n
) const
1152 #endif // size_t != unsigned int
1154 // operator versions of GetWriteableChar()
1155 wxUniCharRef
operator[](int n
)
1157 wxUniCharRef
operator[](long n
)
1159 wxUniCharRef
operator[](size_t n
)
1161 #ifndef wxSIZE_T_IS_UINT
1162 wxUniCharRef
operator[](unsigned int n
)
1164 #endif // size_t != unsigned int
1166 // explicit conversion to C string (use this with printf()!)
1167 wxCStrData
c_str() const { return wxCStrData(this); }
1168 wxCStrData
data() const { return c_str(); }
1170 // implicit conversion to C string
1171 operator wxCStrData() const { return c_str(); }
1172 operator const char*() const { return c_str(); }
1173 operator const wchar_t*() const { return c_str(); }
1175 // implicit conversion to untyped pointer for compatibility with previous
1176 // wxWidgets versions: this is the same as conversion to const char * so it
1178 operator const void*() const { return c_str(); }
1180 // identical to c_str(), for MFC compatibility
1181 const wxCStrData
GetData() const { return c_str(); }
1183 // explicit conversion to C string in internal representation (char*,
1184 // wchar_t*, UTF-8-encoded char*, depending on the build):
1185 const wxStringCharType
*wx_str() const { return m_impl
.c_str(); }
1187 // conversion to *non-const* multibyte or widestring buffer; modifying
1188 // returned buffer won't affect the string, these methods are only useful
1189 // for passing values to const-incorrect functions
1190 wxWritableCharBuffer
char_str(const wxMBConv
& conv
= wxConvLibc
) const
1191 { return mb_str(conv
); }
1192 wxWritableWCharBuffer
wchar_str() const { return wc_str(); }
1194 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
1195 // converting numbers or strings which are certain not to contain special
1196 // chars (typically system functions, X atoms, environment variables etc.)
1198 // the behaviour of these functions with the strings containing anything
1199 // else than 7 bit ASCII characters is undefined, use at your own risk.
1201 static wxString
FromAscii(const char *ascii
); // string
1202 static wxString
FromAscii(const char ascii
); // char
1203 const wxCharBuffer
ToAscii() const;
1205 static wxString
FromAscii(const char *ascii
) { return wxString( ascii
); }
1206 static wxString
FromAscii(const char ascii
) { return wxString( ascii
); }
1207 const char *ToAscii() const { return c_str(); }
1208 #endif // Unicode/!Unicode
1210 // conversion to/from UTF-8:
1211 #if wxUSE_UNICODE_UTF8
1212 static wxString
FromUTF8(const char *utf8
)
1214 wxASSERT( wxStringOperations::IsValidUtf8String(utf8
) );
1215 return FromImpl(wxStringImpl(utf8
));
1217 static wxString
FromUTF8(const char *utf8
, size_t len
)
1219 wxASSERT( wxStringOperations::IsValidUtf8String(utf8
, len
) );
1220 return FromImpl(wxStringImpl(utf8
, len
));
1222 const char* utf8_str() const { return wx_str(); }
1223 const char* ToUTF8() const { return wx_str(); }
1224 #elif wxUSE_UNICODE_WCHAR
1225 static wxString
FromUTF8(const char *utf8
)
1226 { return wxString(utf8
, wxMBConvUTF8()); }
1227 static wxString
FromUTF8(const char *utf8
, size_t len
)
1228 { return wxString(utf8
, wxMBConvUTF8(), len
); }
1229 const wxCharBuffer
utf8_str() const { return mb_str(wxMBConvUTF8()); }
1230 const wxCharBuffer
ToUTF8() const { return utf8_str(); }
1232 static wxString
FromUTF8(const char *utf8
)
1233 { return wxString(wxMBConvUTF8().cMB2WC(utf8
)); }
1234 static wxString
FromUTF8(const char *utf8
, size_t len
)
1237 wxWCharBuffer
buf(wxMBConvUTF8().cMB2WC(utf8
, len
== npos
? wxNO_LEN
: len
, &wlen
));
1238 return wxString(buf
.data(), wlen
);
1240 const wxCharBuffer
utf8_str() const
1241 { return wxMBConvUTF8().cWC2MB(wc_str()); }
1242 const wxCharBuffer
ToUTF8() const { return utf8_str(); }
1245 // functions for storing binary data in wxString:
1247 static wxString
From8BitData(const char *data
, size_t len
)
1248 { return wxString(data
, wxConvISO8859_1
, len
); }
1249 // version for NUL-terminated data:
1250 static wxString
From8BitData(const char *data
)
1251 { return wxString(data
, wxConvISO8859_1
); }
1252 const wxCharBuffer
To8BitData() const { return mb_str(wxConvISO8859_1
); }
1254 static wxString
From8BitData(const char *data
, size_t len
)
1255 { return wxString(data
, len
); }
1256 // version for NUL-terminated data:
1257 static wxString
From8BitData(const char *data
)
1258 { return wxString(data
); }
1259 const char *To8BitData() const { return c_str(); }
1260 #endif // Unicode/ANSI
1262 // conversions with (possible) format conversions: have to return a
1263 // buffer with temporary data
1265 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
1266 // return an ANSI (multibyte) string, wc_str() to return a wide string and
1267 // fn_str() to return a string which should be used with the OS APIs
1268 // accepting the file names. The return value is always the same, but the
1269 // type differs because a function may either return pointer to the buffer
1270 // directly or have to use intermediate buffer for translation.
1273 #if wxUSE_UTF8_LOCALE_ONLY
1274 const char* mb_str() const { return wx_str(); }
1275 const wxCharBuffer
mb_str(const wxMBConv
& conv
) const;
1277 const wxCharBuffer
mb_str(const wxMBConv
& conv
= wxConvLibc
) const;
1280 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
1282 #if wxUSE_UNICODE_WCHAR
1283 const wxChar
* wc_str() const { return wx_str(); }
1284 #elif wxUSE_UNICODE_UTF8
1285 const wxWCharBuffer
wc_str() const;
1287 // for compatibility with !wxUSE_UNICODE version
1288 const wxWX2WCbuf
wc_str(const wxMBConv
& WXUNUSED(conv
)) const
1289 { return wc_str(); }
1292 const wxCharBuffer
fn_str() const { return mb_str(wxConvFile
); }
1294 const wxWX2WCbuf
fn_str() const { return wc_str(); }
1295 #endif // wxMBFILES/!wxMBFILES
1298 const wxChar
* mb_str() const { return wx_str(); }
1300 // for compatibility with wxUSE_UNICODE version
1301 const wxChar
* mb_str(const wxMBConv
& WXUNUSED(conv
)) const { return wx_str(); }
1303 const wxWX2MBbuf
mbc_str() const { return mb_str(); }
1306 const wxWCharBuffer
wc_str(const wxMBConv
& conv
= wxConvLibc
) const;
1307 #endif // wxUSE_WCHAR_T
1309 const wxCharBuffer
fn_str() const { return wxConvFile
.cWC2WX( wc_str( wxConvLocal
) ); }
1311 const wxChar
* fn_str() const { return c_str(); }
1313 #endif // Unicode/ANSI
1315 // overloaded assignment
1316 // from another wxString
1317 wxString
& operator=(const wxString
& stringSrc
)
1318 { m_impl
= stringSrc
.m_impl
; return *this; }
1319 wxString
& operator=(const wxCStrData
& cstr
)
1320 { return *this = cstr
.AsString(); }
1322 wxString
& operator=(wxUniChar ch
)
1323 { m_impl
= wxStringOperations::EncodeChar(ch
); return *this; }
1324 wxString
& operator=(wxUniCharRef ch
)
1325 { return operator=((wxUniChar
)ch
); }
1326 wxString
& operator=(char ch
)
1327 { return operator=(wxUniChar(ch
)); }
1328 wxString
& operator=(unsigned char ch
)
1329 { return operator=(wxUniChar(ch
)); }
1330 wxString
& operator=(wchar_t ch
)
1331 { return operator=(wxUniChar(ch
)); }
1332 // from a C string - STL probably will crash on NULL,
1333 // so we need to compensate in that case
1334 #if wxUSE_STL_BASED_WXSTRING
1335 wxString
& operator=(const char *psz
)
1336 { if (psz
) m_impl
= ImplStr(psz
); else Clear(); return *this; }
1337 wxString
& operator=(const wchar_t *pwz
)
1338 { if (pwz
) m_impl
= ImplStr(pwz
); else Clear(); return *this; }
1340 wxString
& operator=(const char *psz
)
1341 { m_impl
= ImplStr(psz
); return *this; }
1342 wxString
& operator=(const wchar_t *pwz
)
1343 { m_impl
= ImplStr(pwz
); return *this; }
1345 wxString
& operator=(const unsigned char *psz
)
1346 { return operator=((const char*)psz
); }
1348 // from wxWCharBuffer
1349 wxString
& operator=(const wxWCharBuffer
& s
)
1350 { return operator=(s
.data()); } // FIXME-UTF8: fix for embedded NULs
1351 // from wxCharBuffer
1352 wxString
& operator=(const wxCharBuffer
& s
)
1353 { return operator=(s
.data()); } // FIXME-UTF8: fix for embedded NULs
1355 // string concatenation
1356 // in place concatenation
1358 Concatenate and return the result. Note that the left to right
1359 associativity of << allows to write things like "str << str1 << str2
1360 << ..." (unlike with +=)
1363 wxString
& operator<<(const wxString
& s
)
1365 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1366 wxASSERT_MSG( s
.IsValid(),
1367 _T("did you forget to call UngetWriteBuf()?") );
1373 // string += C string
1374 wxString
& operator<<(const char *psz
)
1375 { append(psz
); return *this; }
1376 wxString
& operator<<(const wchar_t *pwz
)
1377 { append(pwz
); return *this; }
1378 wxString
& operator<<(const wxCStrData
& psz
)
1379 { append(psz
.AsString()); return *this; }
1381 wxString
& operator<<(wxUniChar ch
) { append(1, ch
); return *this; }
1382 wxString
& operator<<(wxUniCharRef ch
) { append(1, ch
); return *this; }
1383 wxString
& operator<<(char ch
) { append(1, ch
); return *this; }
1384 wxString
& operator<<(unsigned char ch
) { append(1, ch
); return *this; }
1385 wxString
& operator<<(wchar_t ch
) { append(1, ch
); return *this; }
1387 // string += buffer (i.e. from wxGetString)
1388 wxString
& operator<<(const wxWCharBuffer
& s
)
1389 { return operator<<((const wchar_t *)s
); }
1390 wxString
& operator<<(const wxCharBuffer
& s
)
1391 { return operator<<((const char *)s
); }
1393 // string += C string
1394 wxString
& Append(const wxString
& s
)
1396 // test for empty() to share the string if possible
1403 wxString
& Append(const char* psz
)
1404 { append(psz
); return *this; }
1405 wxString
& Append(const wchar_t* pwz
)
1406 { append(pwz
); return *this; }
1407 wxString
& Append(const wxCStrData
& psz
)
1408 { append(psz
); return *this; }
1409 wxString
& Append(const wxCharBuffer
& psz
)
1410 { append(psz
); return *this; }
1411 wxString
& Append(const wxWCharBuffer
& psz
)
1412 { append(psz
); return *this; }
1413 // append count copies of given character
1414 wxString
& Append(wxUniChar ch
, size_t count
= 1u)
1415 { append(count
, ch
); return *this; }
1416 wxString
& Append(wxUniCharRef ch
, size_t count
= 1u)
1417 { append(count
, ch
); return *this; }
1418 wxString
& Append(char ch
, size_t count
= 1u)
1419 { append(count
, ch
); return *this; }
1420 wxString
& Append(unsigned char ch
, size_t count
= 1u)
1421 { append(count
, ch
); return *this; }
1422 wxString
& Append(wchar_t ch
, size_t count
= 1u)
1423 { append(count
, ch
); return *this; }
1424 wxString
& Append(const char* psz
, size_t nLen
)
1425 { append(psz
, nLen
); return *this; }
1426 wxString
& Append(const wchar_t* pwz
, size_t nLen
)
1427 { append(pwz
, nLen
); return *this; }
1429 // prepend a string, return the string itself
1430 wxString
& Prepend(const wxString
& str
)
1431 { *this = str
+ *this; return *this; }
1433 // non-destructive concatenation
1435 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
,
1436 const wxString
& string2
);
1437 // string with a single char
1438 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
1439 // char with a string
1440 friend wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
1441 // string with C string
1442 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
1444 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
1445 const wchar_t *pwz
);
1446 // C string with string
1447 friend wxString WXDLLIMPEXP_BASE
operator+(const char *psz
,
1448 const wxString
& string
);
1449 friend wxString WXDLLIMPEXP_BASE
operator+(const wchar_t *pwz
,
1450 const wxString
& string
);
1452 // stream-like functions
1453 // insert an int into string
1454 wxString
& operator<<(int i
)
1455 { return (*this) << Format(_T("%d"), i
); }
1456 // insert an unsigned int into string
1457 wxString
& operator<<(unsigned int ui
)
1458 { return (*this) << Format(_T("%u"), ui
); }
1459 // insert a long into string
1460 wxString
& operator<<(long l
)
1461 { return (*this) << Format(_T("%ld"), l
); }
1462 // insert an unsigned long into string
1463 wxString
& operator<<(unsigned long ul
)
1464 { return (*this) << Format(_T("%lu"), ul
); }
1465 #if defined wxLongLong_t && !defined wxLongLongIsLong
1466 // insert a long long if they exist and aren't longs
1467 wxString
& operator<<(wxLongLong_t ll
)
1469 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("d");
1470 return (*this) << Format(fmt
, ll
);
1472 // insert an unsigned long long
1473 wxString
& operator<<(wxULongLong_t ull
)
1475 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("u");
1476 return (*this) << Format(fmt
, ull
);
1479 // insert a float into string
1480 wxString
& operator<<(float f
)
1481 { return (*this) << Format(_T("%f"), f
); }
1482 // insert a double into string
1483 wxString
& operator<<(double d
)
1484 { return (*this) << Format(_T("%g"), d
); }
1486 // string comparison
1487 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
1488 int Cmp(const char *psz
) const
1489 { return compare(psz
); }
1490 int Cmp(const wchar_t *pwz
) const
1491 { return compare(pwz
); }
1492 int Cmp(const wxString
& s
) const
1493 { return compare(s
); }
1494 int Cmp(const wxCStrData
& s
) const
1495 { return compare(s
); }
1496 int Cmp(const wxCharBuffer
& s
) const
1497 { return compare(s
); }
1498 int Cmp(const wxWCharBuffer
& s
) const
1499 { return compare(s
); }
1500 // same as Cmp() but not case-sensitive
1501 int CmpNoCase(const wxString
& s
) const;
1502 // test for the string equality, either considering case or not
1503 // (if compareWithCase then the case matters)
1504 bool IsSameAs(const wxString
& str
, bool compareWithCase
= true) const
1505 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
1506 bool IsSameAs(const char *str
, bool compareWithCase
= true) const
1507 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
1508 bool IsSameAs(const wchar_t *str
, bool compareWithCase
= true) const
1509 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
1510 bool IsSameAs(const wxCStrData
& str
, bool compareWithCase
= true) const
1511 { return IsSameAs(str
.AsString(), compareWithCase
); }
1512 bool IsSameAs(const wxCharBuffer
& str
, bool compareWithCase
= true) const
1513 { return IsSameAs(str
.data(), compareWithCase
); }
1514 bool IsSameAs(const wxWCharBuffer
& str
, bool compareWithCase
= true) const
1515 { return IsSameAs(str
.data(), compareWithCase
); }
1516 // comparison with a single character: returns true if equal
1517 bool IsSameAs(wxUniChar c
, bool compareWithCase
= true) const
1519 return (length() == 1) && (compareWithCase
? GetChar(0u) == c
1520 : wxToupper(GetChar(0u)) == wxToupper(c
));
1522 // FIXME-UTF8: remove these overloads
1523 bool IsSameAs(wxUniCharRef c
, bool compareWithCase
= true) const
1524 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1525 bool IsSameAs(char c
, bool compareWithCase
= true) const
1526 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1527 bool IsSameAs(unsigned char c
, bool compareWithCase
= true) const
1528 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1529 bool IsSameAs(wchar_t c
, bool compareWithCase
= true) const
1530 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1531 bool IsSameAs(int c
, bool compareWithCase
= true) const
1532 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1534 // simple sub-string extraction
1535 // return substring starting at nFirst of length nCount (or till the end
1536 // if nCount = default value)
1537 wxString
Mid(size_t nFirst
, size_t nCount
= npos
) const;
1539 // operator version of Mid()
1540 wxString
operator()(size_t start
, size_t len
) const
1541 { return Mid(start
, len
); }
1543 // check if the string starts with the given prefix and return the rest
1544 // of the string in the provided pointer if it is not NULL; otherwise
1546 bool StartsWith(const wxString
& prefix
, wxString
*rest
= NULL
) const;
1547 // check if the string ends with the given suffix and return the
1548 // beginning of the string before the suffix in the provided pointer if
1549 // it is not NULL; otherwise return false
1550 bool EndsWith(const wxString
& suffix
, wxString
*rest
= NULL
) const;
1552 // get first nCount characters
1553 wxString
Left(size_t nCount
) const;
1554 // get last nCount characters
1555 wxString
Right(size_t nCount
) const;
1556 // get all characters before the first occurance of ch
1557 // (returns the whole string if ch not found)
1558 wxString
BeforeFirst(wxUniChar ch
) const;
1559 // get all characters before the last occurence of ch
1560 // (returns empty string if ch not found)
1561 wxString
BeforeLast(wxUniChar ch
) const;
1562 // get all characters after the first occurence of ch
1563 // (returns empty string if ch not found)
1564 wxString
AfterFirst(wxUniChar ch
) const;
1565 // get all characters after the last occurence of ch
1566 // (returns the whole string if ch not found)
1567 wxString
AfterLast(wxUniChar ch
) const;
1569 // for compatibility only, use more explicitly named functions above
1570 wxString
Before(wxUniChar ch
) const { return BeforeLast(ch
); }
1571 wxString
After(wxUniChar ch
) const { return AfterFirst(ch
); }
1574 // convert to upper case in place, return the string itself
1575 wxString
& MakeUpper();
1576 // convert to upper case, return the copy of the string
1577 // Here's something to remember: BC++ doesn't like returns in inlines.
1578 wxString
Upper() const ;
1579 // convert to lower case in place, return the string itself
1580 wxString
& MakeLower();
1581 // convert to lower case, return the copy of the string
1582 wxString
Lower() const ;
1584 // trimming/padding whitespace (either side) and truncating
1585 // remove spaces from left or from right (default) side
1586 wxString
& Trim(bool bFromRight
= true);
1587 // add nCount copies chPad in the beginning or at the end (default)
1588 wxString
& Pad(size_t nCount
, wxUniChar chPad
= wxT(' '), bool bFromRight
= true);
1590 // searching and replacing
1591 // searching (return starting index, or -1 if not found)
1592 int Find(wxUniChar ch
, bool bFromEnd
= false) const; // like strchr/strrchr
1593 int Find(wxUniCharRef ch
, bool bFromEnd
= false) const
1594 { return Find(wxUniChar(ch
), bFromEnd
); }
1595 int Find(char ch
, bool bFromEnd
= false) const
1596 { return Find(wxUniChar(ch
), bFromEnd
); }
1597 int Find(unsigned char ch
, bool bFromEnd
= false) const
1598 { return Find(wxUniChar(ch
), bFromEnd
); }
1599 int Find(wchar_t ch
, bool bFromEnd
= false) const
1600 { return Find(wxUniChar(ch
), bFromEnd
); }
1601 // searching (return starting index, or -1 if not found)
1602 int Find(const wxString
& sub
) const // like strstr
1604 size_type idx
= find(sub
);
1605 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
1607 int Find(const char *sub
) const // like strstr
1609 size_type idx
= find(sub
);
1610 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
1612 int Find(const wchar_t *sub
) const // like strstr
1614 size_type idx
= find(sub
);
1615 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
1618 int Find(const wxCStrData
& sub
) const
1619 { return Find(sub
.AsString()); }
1620 int Find(const wxCharBuffer
& sub
) const
1621 { return Find(sub
.data()); }
1622 int Find(const wxWCharBuffer
& sub
) const
1623 { return Find(sub
.data()); }
1625 // replace first (or all of bReplaceAll) occurences of substring with
1626 // another string, returns the number of replacements made
1627 size_t Replace(const wxString
& strOld
,
1628 const wxString
& strNew
,
1629 bool bReplaceAll
= true);
1631 // check if the string contents matches a mask containing '*' and '?'
1632 bool Matches(const wxString
& mask
) const;
1634 // conversion to numbers: all functions return true only if the whole
1635 // string is a number and put the value of this number into the pointer
1636 // provided, the base is the numeric base in which the conversion should be
1637 // done and must be comprised between 2 and 36 or be 0 in which case the
1638 // standard C rules apply (leading '0' => octal, "0x" => hex)
1639 // convert to a signed integer
1640 bool ToLong(long *val
, int base
= 10) const;
1641 // convert to an unsigned integer
1642 bool ToULong(unsigned long *val
, int base
= 10) const;
1643 // convert to wxLongLong
1644 #if defined(wxLongLong_t)
1645 bool ToLongLong(wxLongLong_t
*val
, int base
= 10) const;
1646 // convert to wxULongLong
1647 bool ToULongLong(wxULongLong_t
*val
, int base
= 10) const;
1648 #endif // wxLongLong_t
1649 // convert to a double
1650 bool ToDouble(double *val
) const;
1653 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1654 // formatted input/output
1655 // as sprintf(), returns the number of characters written or < 0 on error
1656 // (take 'this' into account in attribute parameter count)
1657 // int Printf(const wxString& format, ...);
1658 WX_DEFINE_VARARG_FUNC(int, Printf
, 1, (const wxFormatString
&),
1659 DoPrintfWchar
, DoPrintfUtf8
)
1661 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1662 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wxString
&),
1663 (wxFormatString(f1
)));
1664 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wxCStrData
&),
1665 (wxFormatString(f1
)));
1666 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const char*),
1667 (wxFormatString(f1
)));
1668 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wchar_t*),
1669 (wxFormatString(f1
)));
1671 #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN
1672 // as vprintf(), returns the number of characters written or < 0 on error
1673 int PrintfV(const wxString
& format
, va_list argptr
);
1675 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1676 // returns the string containing the result of Printf() to it
1677 // static wxString Format(const wxString& format, ...) ATTRIBUTE_PRINTF_1;
1678 WX_DEFINE_VARARG_FUNC(static wxString
, Format
, 1, (const wxFormatString
&),
1679 DoFormatWchar
, DoFormatUtf8
)
1681 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1682 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wxString
&),
1683 (wxFormatString(f1
)));
1684 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wxCStrData
&),
1685 (wxFormatString(f1
)));
1686 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const char*),
1687 (wxFormatString(f1
)));
1688 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wchar_t*),
1689 (wxFormatString(f1
)));
1692 // the same as above, but takes a va_list
1693 static wxString
FormatV(const wxString
& format
, va_list argptr
);
1695 // raw access to string memory
1696 // ensure that string has space for at least nLen characters
1697 // only works if the data of this string is not shared
1698 bool Alloc(size_t nLen
) { reserve(nLen
); /*return capacity() >= nLen;*/ return true; }
1699 // minimize the string's memory
1700 // only works if the data of this string is not shared
1702 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1703 // These are deprecated, use wxStringBuffer or wxStringBufferLength instead
1705 // get writable buffer of at least nLen bytes. Unget() *must* be called
1706 // a.s.a.p. to put string back in a reasonable state!
1707 wxDEPRECATED( wxStringCharType
*GetWriteBuf(size_t nLen
) );
1708 // call this immediately after GetWriteBuf() has been used
1709 wxDEPRECATED( void UngetWriteBuf() );
1710 wxDEPRECATED( void UngetWriteBuf(size_t nLen
) );
1711 #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && wxUSE_UNICODE_UTF8
1713 // wxWidgets version 1 compatibility functions
1716 wxString
SubString(size_t from
, size_t to
) const
1717 { return Mid(from
, (to
- from
+ 1)); }
1718 // values for second parameter of CompareTo function
1719 enum caseCompare
{exact
, ignoreCase
};
1720 // values for first parameter of Strip function
1721 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
1723 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1725 // (take 'this' into account in attribute parameter count)
1726 // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2;
1727 WX_DEFINE_VARARG_FUNC(int, sprintf
, 1, (const wxFormatString
&),
1728 DoPrintfWchar
, DoPrintfUtf8
)
1730 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1731 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wxString
&),
1732 (wxFormatString(f1
)));
1733 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wxCStrData
&),
1734 (wxFormatString(f1
)));
1735 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const char*),
1736 (wxFormatString(f1
)));
1737 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wchar_t*),
1738 (wxFormatString(f1
)));
1740 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
1743 inline int CompareTo(const wxChar
* psz
, caseCompare cmp
= exact
) const
1744 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
1747 size_t Length() const { return length(); }
1748 // Count the number of characters
1749 int Freq(wxUniChar ch
) const;
1751 void LowerCase() { MakeLower(); }
1753 void UpperCase() { MakeUpper(); }
1754 // use Trim except that it doesn't change this string
1755 wxString
Strip(stripType w
= trailing
) const;
1757 // use Find (more general variants not yet supported)
1758 size_t Index(const wxChar
* psz
) const { return Find(psz
); }
1759 size_t Index(wxUniChar ch
) const { return Find(ch
); }
1761 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
1762 wxString
& RemoveLast(size_t n
= 1) { return Truncate(length() - n
); }
1764 wxString
& Remove(size_t nStart
, size_t nLen
)
1765 { return (wxString
&)erase( nStart
, nLen
); }
1768 int First( wxUniChar ch
) const { return Find(ch
); }
1769 int First( wxUniCharRef ch
) const { return Find(ch
); }
1770 int First( char ch
) const { return Find(ch
); }
1771 int First( unsigned char ch
) const { return Find(ch
); }
1772 int First( wchar_t ch
) const { return Find(ch
); }
1773 int First( const wxString
& str
) const { return Find(str
); }
1774 int Last( wxUniChar ch
) const { return Find(ch
, true); }
1775 bool Contains(const wxString
& str
) const { return Find(str
) != wxNOT_FOUND
; }
1778 bool IsNull() const { return empty(); }
1780 // std::string compatibility functions
1782 // take nLen chars starting at nPos
1783 wxString(const wxString
& str
, size_t nPos
, size_t nLen
)
1784 { assign(str
, nPos
, nLen
); }
1785 // take all characters from first to last
1786 wxString(const_iterator first
, const_iterator last
)
1787 : m_impl(first
.impl(), last
.impl()) { }
1788 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1789 // the 2 overloads below are for compatibility with the existing code using
1790 // pointers instead of iterators
1791 wxString(const char *first
, const char *last
)
1793 SubstrBufFromMB
str(ImplStr(first
, last
- first
));
1794 m_impl
.assign(str
.data
, str
.len
);
1796 wxString(const wchar_t *first
, const wchar_t *last
)
1798 SubstrBufFromWC
str(ImplStr(first
, last
- first
));
1799 m_impl
.assign(str
.data
, str
.len
);
1801 // and this one is needed to compile code adding offsets to c_str() result
1802 wxString(const wxCStrData
& first
, const wxCStrData
& last
)
1803 : m_impl(CreateConstIterator(first
).impl(),
1804 CreateConstIterator(last
).impl())
1806 wxASSERT_MSG( first
.m_str
== last
.m_str
,
1807 _T("pointers must be into the same string") );
1809 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1811 // lib.string.modifiers
1812 // append elements str[pos], ..., str[pos+n]
1813 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
1816 str
.PosLenToImpl(pos
, n
, &from
, &len
);
1817 m_impl
.append(str
.m_impl
, from
, len
);
1821 wxString
& append(const wxString
& str
)
1822 { m_impl
.append(str
.m_impl
); return *this; }
1823 // append first n (or all if n == npos) characters of sz
1824 wxString
& append(const char *sz
)
1825 { m_impl
.append(ImplStr(sz
)); return *this; }
1826 wxString
& append(const wchar_t *sz
)
1827 { m_impl
.append(ImplStr(sz
)); return *this; }
1828 wxString
& append(const char *sz
, size_t n
)
1830 SubstrBufFromMB
str(ImplStr(sz
, n
));
1831 m_impl
.append(str
.data
, str
.len
);
1834 wxString
& append(const wchar_t *sz
, size_t n
)
1836 SubstrBufFromWC
str(ImplStr(sz
, n
));
1837 m_impl
.append(str
.data
, str
.len
);
1841 wxString
& append(const wxCStrData
& str
)
1842 { return append(str
.AsString()); }
1843 wxString
& append(const wxCharBuffer
& str
)
1844 { return append(str
.data()); }
1845 wxString
& append(const wxWCharBuffer
& str
)
1846 { return append(str
.data()); }
1848 // append n copies of ch
1849 wxString
& append(size_t n
, wxUniChar ch
)
1851 #if wxUSE_UNICODE_UTF8
1852 if ( !ch
.IsAscii() )
1853 m_impl
.append(wxStringOperations::EncodeNChars(n
, ch
));
1856 m_impl
.append(n
, (wxStringCharType
)ch
);
1859 // append from first to last
1860 wxString
& append(const_iterator first
, const_iterator last
)
1861 { m_impl
.append(first
.impl(), last
.impl()); return *this; }
1862 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1863 wxString
& append(const char *first
, const char *last
)
1864 { return append(first
, last
- first
); }
1865 wxString
& append(const wchar_t *first
, const wchar_t *last
)
1866 { return append(first
, last
- first
); }
1867 wxString
& append(const wxCStrData
& first
, const wxCStrData
& last
)
1868 { return append(CreateConstIterator(first
), CreateConstIterator(last
)); }
1869 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1871 // same as `this_string = str'
1872 wxString
& assign(const wxString
& str
)
1873 { m_impl
= str
.m_impl
; return *this; }
1874 wxString
& assign(const wxString
& str
, size_t len
)
1876 m_impl
.assign(str
.m_impl
, 0, str
.LenToImpl(len
));
1879 // same as ` = str[pos..pos + n]
1880 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
1883 str
.PosLenToImpl(pos
, n
, &from
, &len
);
1884 m_impl
.assign(str
.m_impl
, from
, len
);
1887 // same as `= first n (or all if n == npos) characters of sz'
1888 wxString
& assign(const char *sz
)
1889 { m_impl
.assign(ImplStr(sz
)); return *this; }
1890 wxString
& assign(const wchar_t *sz
)
1891 { m_impl
.assign(ImplStr(sz
)); return *this; }
1892 wxString
& assign(const char *sz
, size_t n
)
1894 SubstrBufFromMB
str(ImplStr(sz
, n
));
1895 m_impl
.assign(str
.data
, str
.len
);
1898 wxString
& assign(const wchar_t *sz
, size_t n
)
1900 SubstrBufFromWC
str(ImplStr(sz
, n
));
1901 m_impl
.assign(str
.data
, str
.len
);
1905 wxString
& assign(const wxCStrData
& str
)
1906 { return assign(str
.AsString()); }
1907 wxString
& assign(const wxCharBuffer
& str
)
1908 { return assign(str
.data()); }
1909 wxString
& assign(const wxWCharBuffer
& str
)
1910 { return assign(str
.data()); }
1911 wxString
& assign(const wxCStrData
& str
, size_t len
)
1912 { return assign(str
.AsString(), len
); }
1913 wxString
& assign(const wxCharBuffer
& str
, size_t len
)
1914 { return assign(str
.data(), len
); }
1915 wxString
& assign(const wxWCharBuffer
& str
, size_t len
)
1916 { return assign(str
.data(), len
); }
1918 // same as `= n copies of ch'
1919 wxString
& assign(size_t n
, wxUniChar ch
)
1921 #if wxUSE_UNICODE_UTF8
1922 if ( !ch
.IsAscii() )
1923 m_impl
.assign(wxStringOperations::EncodeNChars(n
, ch
));
1926 m_impl
.assign(n
, (wxStringCharType
)ch
);
1930 wxString
& assign(size_t n
, wxUniCharRef ch
)
1931 { return assign(n
, wxUniChar(ch
)); }
1932 wxString
& assign(size_t n
, char ch
)
1933 { return assign(n
, wxUniChar(ch
)); }
1934 wxString
& assign(size_t n
, unsigned char ch
)
1935 { return assign(n
, wxUniChar(ch
)); }
1936 wxString
& assign(size_t n
, wchar_t ch
)
1937 { return assign(n
, wxUniChar(ch
)); }
1939 // assign from first to last
1940 wxString
& assign(const_iterator first
, const_iterator last
)
1941 { m_impl
.assign(first
.impl(), last
.impl()); return *this; }
1942 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1943 wxString
& assign(const char *first
, const char *last
)
1944 { return assign(first
, last
- first
); }
1945 wxString
& assign(const wchar_t *first
, const wchar_t *last
)
1946 { return assign(first
, last
- first
); }
1947 wxString
& assign(const wxCStrData
& first
, const wxCStrData
& last
)
1948 { return assign(CreateConstIterator(first
), CreateConstIterator(last
)); }
1949 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1951 // string comparison
1952 int compare(const wxString
& str
) const;
1953 int compare(const char* sz
) const;
1954 int compare(const wchar_t* sz
) const;
1955 int compare(const wxCStrData
& str
) const
1956 { return compare(str
.AsString()); }
1957 int compare(const wxCharBuffer
& str
) const
1958 { return compare(str
.data()); }
1959 int compare(const wxWCharBuffer
& str
) const
1960 { return compare(str
.data()); }
1961 // comparison with a substring
1962 int compare(size_t nStart
, size_t nLen
, const wxString
& str
) const;
1963 // comparison of 2 substrings
1964 int compare(size_t nStart
, size_t nLen
,
1965 const wxString
& str
, size_t nStart2
, size_t nLen2
) const;
1966 // substring comparison with first nCount characters of sz
1967 int compare(size_t nStart
, size_t nLen
,
1968 const char* sz
, size_t nCount
= npos
) const;
1969 int compare(size_t nStart
, size_t nLen
,
1970 const wchar_t* sz
, size_t nCount
= npos
) const;
1972 // insert another string
1973 wxString
& insert(size_t nPos
, const wxString
& str
)
1974 { insert(begin() + nPos
, str
.begin(), str
.end()); return *this; }
1975 // insert n chars of str starting at nStart (in str)
1976 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
1979 str
.PosLenToImpl(nStart
, n
, &from
, &len
);
1980 m_impl
.insert(PosToImpl(nPos
), str
.m_impl
, from
, len
);
1983 // insert first n (or all if n == npos) characters of sz
1984 wxString
& insert(size_t nPos
, const char *sz
)
1985 { m_impl
.insert(PosToImpl(nPos
), ImplStr(sz
)); return *this; }
1986 wxString
& insert(size_t nPos
, const wchar_t *sz
)
1987 { m_impl
.insert(PosToImpl(nPos
), ImplStr(sz
)); return *this; }
1988 wxString
& insert(size_t nPos
, const char *sz
, size_t n
)
1990 SubstrBufFromMB
str(ImplStr(sz
, n
));
1991 m_impl
.insert(PosToImpl(nPos
), str
.data
, str
.len
);
1994 wxString
& insert(size_t nPos
, const wchar_t *sz
, size_t n
)
1996 SubstrBufFromWC
str(ImplStr(sz
, n
));
1997 m_impl
.insert(PosToImpl(nPos
), str
.data
, str
.len
);
2000 // insert n copies of ch
2001 wxString
& insert(size_t nPos
, size_t n
, wxUniChar ch
)
2003 #if wxUSE_UNICODE_UTF8
2004 if ( !ch
.IsAscii() )
2005 m_impl
.insert(PosToImpl(nPos
), wxStringOperations::EncodeNChars(n
, ch
));
2008 m_impl
.insert(PosToImpl(nPos
), n
, (wxStringCharType
)ch
);
2011 iterator
insert(iterator it
, wxUniChar ch
)
2013 #if wxUSE_UNICODE_UTF8
2014 if ( !ch
.IsAscii() )
2016 size_t pos
= IterToImplPos(it
);
2017 m_impl
.insert(pos
, wxStringOperations::EncodeChar(ch
));
2018 return iterator(this, m_impl
.begin() + pos
);
2022 return iterator(this, m_impl
.insert(it
.impl(), (wxStringCharType
)ch
));
2024 void insert(iterator it
, const_iterator first
, const_iterator last
)
2025 { m_impl
.insert(it
.impl(), first
.impl(), last
.impl()); }
2026 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2027 void insert(iterator it
, const char *first
, const char *last
)
2028 { insert(it
- begin(), first
, last
- first
); }
2029 void insert(iterator it
, const wchar_t *first
, const wchar_t *last
)
2030 { insert(it
- begin(), first
, last
- first
); }
2031 void insert(iterator it
, const wxCStrData
& first
, const wxCStrData
& last
)
2032 { insert(it
, CreateConstIterator(first
), CreateConstIterator(last
)); }
2033 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2035 void insert(iterator it
, size_type n
, wxUniChar ch
)
2037 #if wxUSE_UNICODE_UTF8
2038 if ( !ch
.IsAscii() )
2039 m_impl
.insert(IterToImplPos(it
), wxStringOperations::EncodeNChars(n
, ch
));
2042 m_impl
.insert(it
.impl(), n
, (wxStringCharType
)ch
);
2045 // delete characters from nStart to nStart + nLen
2046 wxString
& erase(size_type pos
= 0, size_type n
= npos
)
2049 PosLenToImpl(pos
, n
, &from
, &len
);
2050 m_impl
.erase(from
, len
);
2053 // delete characters from first up to last
2054 iterator
erase(iterator first
, iterator last
)
2055 { return iterator(this, m_impl
.erase(first
.impl(), last
.impl())); }
2056 iterator
erase(iterator first
)
2057 { return iterator(this, m_impl
.erase(first
.impl())); }
2059 #ifdef wxSTRING_BASE_HASNT_CLEAR
2060 void clear() { erase(); }
2062 void clear() { m_impl
.clear(); }
2065 // replaces the substring of length nLen starting at nStart
2066 wxString
& replace(size_t nStart
, size_t nLen
, const char* sz
)
2069 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2070 m_impl
.replace(from
, len
, ImplStr(sz
));
2073 wxString
& replace(size_t nStart
, size_t nLen
, const wchar_t* sz
)
2076 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2077 m_impl
.replace(from
, len
, ImplStr(sz
));
2080 // replaces the substring of length nLen starting at nStart
2081 wxString
& replace(size_t nStart
, size_t nLen
, const wxString
& str
)
2084 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2085 m_impl
.replace(from
, len
, str
.m_impl
);
2088 // replaces the substring with nCount copies of ch
2089 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxUniChar ch
)
2092 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2093 #if wxUSE_UNICODE_UTF8
2094 if ( !ch
.IsAscii() )
2095 m_impl
.replace(from
, len
, wxStringOperations::EncodeNChars(nCount
, ch
));
2098 m_impl
.replace(from
, len
, nCount
, (wxStringCharType
)ch
);
2101 // replaces a substring with another substring
2102 wxString
& replace(size_t nStart
, size_t nLen
,
2103 const wxString
& str
, size_t nStart2
, size_t nLen2
)
2106 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2109 str
.PosLenToImpl(nStart2
, nLen2
, &from2
, &len2
);
2111 m_impl
.replace(from
, len
, str
.m_impl
, from2
, len2
);
2114 // replaces the substring with first nCount chars of sz
2115 wxString
& replace(size_t nStart
, size_t nLen
,
2116 const char* sz
, size_t nCount
)
2119 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2121 SubstrBufFromMB
str(ImplStr(sz
, nCount
));
2123 m_impl
.replace(from
, len
, str
.data
, str
.len
);
2126 wxString
& replace(size_t nStart
, size_t nLen
,
2127 const wchar_t* sz
, size_t nCount
)
2130 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2132 SubstrBufFromWC
str(ImplStr(sz
, nCount
));
2134 m_impl
.replace(from
, len
, str
.data
, str
.len
);
2137 wxString
& replace(size_t nStart
, size_t nLen
,
2138 const wxString
& s
, size_t nCount
)
2141 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2142 m_impl
.replace(from
, len
, s
.m_impl
.c_str(), s
.LenToImpl(nCount
));
2146 wxString
& replace(iterator first
, iterator last
, const char* s
)
2147 { m_impl
.replace(first
.impl(), last
.impl(), ImplStr(s
)); return *this; }
2148 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
)
2149 { m_impl
.replace(first
.impl(), last
.impl(), ImplStr(s
)); return *this; }
2150 wxString
& replace(iterator first
, iterator last
, const char* s
, size_type n
)
2152 SubstrBufFromMB
str(ImplStr(s
, n
));
2153 m_impl
.replace(first
.impl(), last
.impl(), str
.data
, str
.len
);
2156 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
, size_type n
)
2158 SubstrBufFromWC
str(ImplStr(s
, n
));
2159 m_impl
.replace(first
.impl(), last
.impl(), str
.data
, str
.len
);
2162 wxString
& replace(iterator first
, iterator last
, const wxString
& s
)
2163 { m_impl
.replace(first
.impl(), last
.impl(), s
.m_impl
); return *this; }
2164 wxString
& replace(iterator first
, iterator last
, size_type n
, wxUniChar ch
)
2166 #if wxUSE_UNICODE_UTF8
2167 if ( !ch
.IsAscii() )
2168 m_impl
.replace(first
.impl(), last
.impl(),
2169 wxStringOperations::EncodeNChars(n
, ch
));
2172 m_impl
.replace(first
.impl(), last
.impl(), n
, (wxStringCharType
)ch
);
2175 wxString
& replace(iterator first
, iterator last
,
2176 const_iterator first1
, const_iterator last1
)
2178 m_impl
.replace(first
.impl(), last
.impl(), first1
.impl(), last1
.impl());
2181 wxString
& replace(iterator first
, iterator last
,
2182 const char *first1
, const char *last1
)
2183 { replace(first
, last
, first1
, last1
- first1
); return *this; }
2184 wxString
& replace(iterator first
, iterator last
,
2185 const wchar_t *first1
, const wchar_t *last1
)
2186 { replace(first
, last
, first1
, last1
- first1
); return *this; }
2189 void swap(wxString
& str
)
2190 { m_impl
.swap(str
.m_impl
); }
2193 size_t find(const wxString
& str
, size_t nStart
= 0) const
2194 { return PosFromImpl(m_impl
.find(str
.m_impl
, PosToImpl(nStart
))); }
2196 // find first n characters of sz
2197 size_t find(const char* sz
, size_t nStart
= 0, size_t n
= npos
) const
2199 SubstrBufFromMB
str(ImplStr(sz
, n
));
2200 return PosFromImpl(m_impl
.find(str
.data
, PosToImpl(nStart
), str
.len
));
2202 size_t find(const wchar_t* sz
, size_t nStart
= 0, size_t n
= npos
) const
2204 SubstrBufFromWC
str(ImplStr(sz
, n
));
2205 return PosFromImpl(m_impl
.find(str
.data
, PosToImpl(nStart
), str
.len
));
2207 size_t find(const wxCharBuffer
& s
, size_t nStart
= 0, size_t n
= npos
) const
2208 { return find(s
.data(), nStart
, n
); }
2209 size_t find(const wxWCharBuffer
& s
, size_t nStart
= 0, size_t n
= npos
) const
2210 { return find(s
.data(), nStart
, n
); }
2211 size_t find(const wxCStrData
& s
, size_t nStart
= 0, size_t n
= npos
) const
2212 { return find(s
.AsWChar(), nStart
, n
); }
2214 // find the first occurence of character ch after nStart
2215 size_t find(wxUniChar ch
, size_t nStart
= 0) const
2217 return PosFromImpl(m_impl
.find(wxStringOperations::EncodeChar(ch
),
2218 PosToImpl(nStart
)));
2220 size_t find(wxUniCharRef ch
, size_t nStart
= 0) const
2221 { return find(wxUniChar(ch
), nStart
); }
2222 size_t find(char ch
, size_t nStart
= 0) const
2223 { return find(wxUniChar(ch
), nStart
); }
2224 size_t find(unsigned char ch
, size_t nStart
= 0) const
2225 { return find(wxUniChar(ch
), nStart
); }
2226 size_t find(wchar_t ch
, size_t nStart
= 0) const
2227 { return find(wxUniChar(ch
), nStart
); }
2229 // rfind() family is exactly like find() but works right to left
2231 // as find, but from the end
2232 size_t rfind(const wxString
& str
, size_t nStart
= npos
) const
2233 { return PosFromImpl(m_impl
.rfind(str
.m_impl
, PosToImpl(nStart
))); }
2235 // as find, but from the end
2236 size_t rfind(const char* sz
, size_t nStart
= npos
, size_t n
= npos
) const
2238 SubstrBufFromMB
str(ImplStr(sz
, n
));
2239 return PosFromImpl(m_impl
.rfind(str
.data
, PosToImpl(nStart
), str
.len
));
2241 size_t rfind(const wchar_t* sz
, size_t nStart
= npos
, size_t n
= npos
) const
2243 SubstrBufFromWC
str(ImplStr(sz
, n
));
2244 return PosFromImpl(m_impl
.rfind(str
.data
, PosToImpl(nStart
), str
.len
));
2246 size_t rfind(const wxCharBuffer
& s
, size_t nStart
= npos
, size_t n
= npos
) const
2247 { return rfind(s
.data(), nStart
, n
); }
2248 size_t rfind(const wxWCharBuffer
& s
, size_t nStart
= npos
, size_t n
= npos
) const
2249 { return rfind(s
.data(), nStart
, n
); }
2250 size_t rfind(const wxCStrData
& s
, size_t nStart
= npos
, size_t n
= npos
) const
2251 { return rfind(s
.AsWChar(), nStart
, n
); }
2252 // as find, but from the end
2253 size_t rfind(wxUniChar ch
, size_t nStart
= npos
) const
2255 return PosFromImpl(m_impl
.rfind(wxStringOperations::EncodeChar(ch
),
2256 PosToImpl(nStart
)));
2258 size_t rfind(wxUniCharRef ch
, size_t nStart
= npos
) const
2259 { return rfind(wxUniChar(ch
), nStart
); }
2260 size_t rfind(char ch
, size_t nStart
= npos
) const
2261 { return rfind(wxUniChar(ch
), nStart
); }
2262 size_t rfind(unsigned char ch
, size_t nStart
= npos
) const
2263 { return rfind(wxUniChar(ch
), nStart
); }
2264 size_t rfind(wchar_t ch
, size_t nStart
= npos
) const
2265 { return rfind(wxUniChar(ch
), nStart
); }
2267 // find first/last occurence of any character (not) in the set:
2268 #if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2269 // FIXME-UTF8: this is not entirely correct, because it doesn't work if
2270 // sizeof(wchar_t)==2 and surrogates are present in the string;
2271 // should we care? Probably not.
2272 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
2273 { return m_impl
.find_first_of(str
.m_impl
, nStart
); }
2274 size_t find_first_of(const char* sz
, size_t nStart
= 0) const
2275 { return m_impl
.find_first_of(ImplStr(sz
), nStart
); }
2276 size_t find_first_of(const wchar_t* sz
, size_t nStart
= 0) const
2277 { return m_impl
.find_first_of(ImplStr(sz
), nStart
); }
2278 size_t find_first_of(const char* sz
, size_t nStart
, size_t n
) const
2279 { return m_impl
.find_first_of(ImplStr(sz
), nStart
, n
); }
2280 size_t find_first_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
2281 { return m_impl
.find_first_of(ImplStr(sz
), nStart
, n
); }
2282 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const
2283 { return m_impl
.find_first_of((wxChar
)c
, nStart
); }
2285 size_t find_last_of(const wxString
& str
, size_t nStart
= npos
) const
2286 { return m_impl
.find_last_of(str
.m_impl
, nStart
); }
2287 size_t find_last_of(const char* sz
, size_t nStart
= npos
) const
2288 { return m_impl
.find_last_of(ImplStr(sz
), nStart
); }
2289 size_t find_last_of(const wchar_t* sz
, size_t nStart
= npos
) const
2290 { return m_impl
.find_last_of(ImplStr(sz
), nStart
); }
2291 size_t find_last_of(const char* sz
, size_t nStart
, size_t n
) const
2292 { return m_impl
.find_last_of(ImplStr(sz
), nStart
, n
); }
2293 size_t find_last_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
2294 { return m_impl
.find_last_of(ImplStr(sz
), nStart
, n
); }
2295 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const
2296 { return m_impl
.find_last_of((wxChar
)c
, nStart
); }
2298 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
2299 { return m_impl
.find_first_not_of(str
.m_impl
, nStart
); }
2300 size_t find_first_not_of(const char* sz
, size_t nStart
= 0) const
2301 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
); }
2302 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
= 0) const
2303 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
); }
2304 size_t find_first_not_of(const char* sz
, size_t nStart
, size_t n
) const
2305 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
, n
); }
2306 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
2307 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
, n
); }
2308 size_t find_first_not_of(wxUniChar c
, size_t nStart
= 0) const
2309 { return m_impl
.find_first_not_of((wxChar
)c
, nStart
); }
2311 size_t find_last_not_of(const wxString
& str
, size_t nStart
= npos
) const
2312 { return m_impl
.find_last_not_of(str
.m_impl
, nStart
); }
2313 size_t find_last_not_of(const char* sz
, size_t nStart
= npos
) const
2314 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
); }
2315 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
= npos
) const
2316 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
); }
2317 size_t find_last_not_of(const char* sz
, size_t nStart
, size_t n
) const
2318 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
, n
); }
2319 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
2320 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
, n
); }
2321 size_t find_last_not_of(wxUniChar c
, size_t nStart
= npos
) const
2322 { return m_impl
.find_last_not_of((wxChar
)c
, nStart
); }
2324 // we can't use std::string implementation in UTF-8 build, because the
2325 // character sets would be interpreted wrongly:
2327 // as strpbrk() but starts at nStart, returns npos if not found
2328 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
2329 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2330 { return find_first_of(str
.wc_str(), nStart
); }
2332 { return find_first_of(str
.mb_str(), nStart
); }
2335 size_t find_first_of(const char* sz
, size_t nStart
= 0) const;
2336 size_t find_first_of(const wchar_t* sz
, size_t nStart
= 0) const;
2337 size_t find_first_of(const char* sz
, size_t nStart
, size_t n
) const;
2338 size_t find_first_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2339 // same as find(char, size_t)
2340 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const
2341 { return find(c
, nStart
); }
2342 // find the last (starting from nStart) char from str in this string
2343 size_t find_last_of (const wxString
& str
, size_t nStart
= npos
) const
2344 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2345 { return find_last_of(str
.wc_str(), nStart
); }
2347 { return find_last_of(str
.mb_str(), nStart
); }
2350 size_t find_last_of (const char* sz
, size_t nStart
= npos
) const;
2351 size_t find_last_of (const wchar_t* sz
, size_t nStart
= npos
) const;
2352 size_t find_last_of(const char* sz
, size_t nStart
, size_t n
) const;
2353 size_t find_last_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2355 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const
2356 { return rfind(c
, nStart
); }
2358 // find first/last occurence of any character not in the set
2360 // as strspn() (starting from nStart), returns npos on failure
2361 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
2362 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2363 { return find_first_not_of(str
.wc_str(), nStart
); }
2365 { return find_first_not_of(str
.mb_str(), nStart
); }
2368 size_t find_first_not_of(const char* sz
, size_t nStart
= 0) const;
2369 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
= 0) const;
2370 size_t find_first_not_of(const char* sz
, size_t nStart
, size_t n
) const;
2371 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2373 size_t find_first_not_of(wxUniChar ch
, size_t nStart
= 0) const;
2375 size_t find_last_not_of(const wxString
& str
, size_t nStart
= npos
) const
2376 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2377 { return find_last_not_of(str
.wc_str(), nStart
); }
2379 { return find_last_not_of(str
.mb_str(), nStart
); }
2382 size_t find_last_not_of(const char* sz
, size_t nStart
= npos
) const;
2383 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
= npos
) const;
2384 size_t find_last_not_of(const char* sz
, size_t nStart
, size_t n
) const;
2385 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2387 size_t find_last_not_of(wxUniChar ch
, size_t nStart
= npos
) const;
2388 #endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not
2390 // provide char/wchar_t/wxUniCharRef overloads for char-finding functions
2391 // above to resolve ambiguities:
2392 size_t find_first_of(wxUniCharRef ch
, size_t nStart
= 0) const
2393 { return find_first_of(wxUniChar(ch
), nStart
); }
2394 size_t find_first_of(char ch
, size_t nStart
= 0) const
2395 { return find_first_of(wxUniChar(ch
), nStart
); }
2396 size_t find_first_of(unsigned char ch
, size_t nStart
= 0) const
2397 { return find_first_of(wxUniChar(ch
), nStart
); }
2398 size_t find_first_of(wchar_t ch
, size_t nStart
= 0) const
2399 { return find_first_of(wxUniChar(ch
), nStart
); }
2400 size_t find_last_of(wxUniCharRef ch
, size_t nStart
= npos
) const
2401 { return find_last_of(wxUniChar(ch
), nStart
); }
2402 size_t find_last_of(char ch
, size_t nStart
= npos
) const
2403 { return find_last_of(wxUniChar(ch
), nStart
); }
2404 size_t find_last_of(unsigned char ch
, size_t nStart
= npos
) const
2405 { return find_last_of(wxUniChar(ch
), nStart
); }
2406 size_t find_last_of(wchar_t ch
, size_t nStart
= npos
) const
2407 { return find_last_of(wxUniChar(ch
), nStart
); }
2408 size_t find_first_not_of(wxUniCharRef ch
, size_t nStart
= 0) const
2409 { return find_first_not_of(wxUniChar(ch
), nStart
); }
2410 size_t find_first_not_of(char ch
, size_t nStart
= 0) const
2411 { return find_first_not_of(wxUniChar(ch
), nStart
); }
2412 size_t find_first_not_of(unsigned char ch
, size_t nStart
= 0) const
2413 { return find_first_not_of(wxUniChar(ch
), nStart
); }
2414 size_t find_first_not_of(wchar_t ch
, size_t nStart
= 0) const
2415 { return find_first_not_of(wxUniChar(ch
), nStart
); }
2416 size_t find_last_not_of(wxUniCharRef ch
, size_t nStart
= npos
) const
2417 { return find_last_not_of(wxUniChar(ch
), nStart
); }
2418 size_t find_last_not_of(char ch
, size_t nStart
= npos
) const
2419 { return find_last_not_of(wxUniChar(ch
), nStart
); }
2420 size_t find_last_not_of(unsigned char ch
, size_t nStart
= npos
) const
2421 { return find_last_not_of(wxUniChar(ch
), nStart
); }
2422 size_t find_last_not_of(wchar_t ch
, size_t nStart
= npos
) const
2423 { return find_last_not_of(wxUniChar(ch
), nStart
); }
2425 // and additional overloads for the versions taking strings:
2426 size_t find_first_of(const wxCStrData
& sz
, size_t nStart
= 0) const
2427 { return find_first_of(sz
.AsString(), nStart
); }
2428 size_t find_first_of(const wxCharBuffer
& sz
, size_t nStart
= 0) const
2429 { return find_first_of(sz
.data(), nStart
); }
2430 size_t find_first_of(const wxWCharBuffer
& sz
, size_t nStart
= 0) const
2431 { return find_first_of(sz
.data(), nStart
); }
2432 size_t find_first_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
2433 { return find_first_of(sz
.AsWChar(), nStart
, n
); }
2434 size_t find_first_of(const wxCharBuffer
& sz
, size_t nStart
, size_t n
) const
2435 { return find_first_of(sz
.data(), nStart
, n
); }
2436 size_t find_first_of(const wxWCharBuffer
& sz
, size_t nStart
, size_t n
) const
2437 { return find_first_of(sz
.data(), nStart
, n
); }
2439 size_t find_last_of(const wxCStrData
& sz
, size_t nStart
= 0) const
2440 { return find_last_of(sz
.AsString(), nStart
); }
2441 size_t find_last_of(const wxCharBuffer
& sz
, size_t nStart
= 0) const
2442 { return find_last_of(sz
.data(), nStart
); }
2443 size_t find_last_of(const wxWCharBuffer
& sz
, size_t nStart
= 0) const
2444 { return find_last_of(sz
.data(), nStart
); }
2445 size_t find_last_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
2446 { return find_last_of(sz
.AsWChar(), nStart
, n
); }
2447 size_t find_last_of(const wxCharBuffer
& sz
, size_t nStart
, size_t n
) const
2448 { return find_last_of(sz
.data(), nStart
, n
); }
2449 size_t find_last_of(const wxWCharBuffer
& sz
, size_t nStart
, size_t n
) const
2450 { return find_last_of(sz
.data(), nStart
, n
); }
2452 size_t find_first_not_of(const wxCStrData
& sz
, size_t nStart
= 0) const
2453 { return find_first_not_of(sz
.AsString(), nStart
); }
2454 size_t find_first_not_of(const wxCharBuffer
& sz
, size_t nStart
= 0) const
2455 { return find_first_not_of(sz
.data(), nStart
); }
2456 size_t find_first_not_of(const wxWCharBuffer
& sz
, size_t nStart
= 0) const
2457 { return find_first_not_of(sz
.data(), nStart
); }
2458 size_t find_first_not_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
2459 { return find_first_not_of(sz
.AsWChar(), nStart
, n
); }
2460 size_t find_first_not_of(const wxCharBuffer
& sz
, size_t nStart
, size_t n
) const
2461 { return find_first_not_of(sz
.data(), nStart
, n
); }
2462 size_t find_first_not_of(const wxWCharBuffer
& sz
, size_t nStart
, size_t n
) const
2463 { return find_first_not_of(sz
.data(), nStart
, n
); }
2465 size_t find_last_not_of(const wxCStrData
& sz
, size_t nStart
= 0) const
2466 { return find_last_not_of(sz
.AsString(), nStart
); }
2467 size_t find_last_not_of(const wxCharBuffer
& sz
, size_t nStart
= 0) const
2468 { return find_last_not_of(sz
.data(), nStart
); }
2469 size_t find_last_not_of(const wxWCharBuffer
& sz
, size_t nStart
= 0) const
2470 { return find_last_not_of(sz
.data(), nStart
); }
2471 size_t find_last_not_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
2472 { return find_last_not_of(sz
.AsWChar(), nStart
, n
); }
2473 size_t find_last_not_of(const wxCharBuffer
& sz
, size_t nStart
, size_t n
) const
2474 { return find_last_not_of(sz
.data(), nStart
, n
); }
2475 size_t find_last_not_of(const wxWCharBuffer
& sz
, size_t nStart
, size_t n
) const
2476 { return find_last_not_of(sz
.data(), nStart
, n
); }
2479 wxString
& operator+=(const wxString
& s
)
2480 { m_impl
+= s
.m_impl
; return *this; }
2481 // string += C string
2482 wxString
& operator+=(const char *psz
)
2483 { m_impl
+= ImplStr(psz
); return *this; }
2484 wxString
& operator+=(const wchar_t *pwz
)
2485 { m_impl
+= ImplStr(pwz
); return *this; }
2486 wxString
& operator+=(const wxCStrData
& s
)
2487 { m_impl
+= s
.AsString().m_impl
; return *this; }
2488 wxString
& operator+=(const wxCharBuffer
& s
)
2489 { return operator+=(s
.data()); }
2490 wxString
& operator+=(const wxWCharBuffer
& s
)
2491 { return operator+=(s
.data()); }
2493 wxString
& operator+=(wxUniChar ch
)
2494 { m_impl
+= wxStringOperations::EncodeChar(ch
); return *this; }
2495 wxString
& operator+=(wxUniCharRef ch
) { return *this += wxUniChar(ch
); }
2496 wxString
& operator+=(int ch
) { return *this += wxUniChar(ch
); }
2497 wxString
& operator+=(char ch
) { return *this += wxUniChar(ch
); }
2498 wxString
& operator+=(unsigned char ch
) { return *this += wxUniChar(ch
); }
2499 wxString
& operator+=(wchar_t ch
) { return *this += wxUniChar(ch
); }
2502 #if !wxUSE_STL_BASED_WXSTRING
2503 // helpers for wxStringBuffer and wxStringBufferLength
2504 wxStringCharType
*DoGetWriteBuf(size_t nLen
)
2505 { return m_impl
.DoGetWriteBuf(nLen
); }
2506 void DoUngetWriteBuf()
2507 { m_impl
.DoUngetWriteBuf(); }
2508 void DoUngetWriteBuf(size_t nLen
)
2509 { m_impl
.DoUngetWriteBuf(nLen
); }
2510 #endif // !wxUSE_STL_BASED_WXSTRING
2512 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2513 #if !wxUSE_UTF8_LOCALE_ONLY
2514 int DoPrintfWchar(const wxChar
*format
, ...);
2515 static wxString
DoFormatWchar(const wxChar
*format
, ...);
2517 #if wxUSE_UNICODE_UTF8
2518 int DoPrintfUtf8(const char *format
, ...);
2519 static wxString
DoFormatUtf8(const char *format
, ...);
2523 #if !wxUSE_STL_BASED_WXSTRING
2524 // check string's data validity
2525 bool IsValid() const { return m_impl
.GetStringData()->IsValid(); }
2529 wxStringImpl m_impl
;
2531 // buffers for compatibility conversion from (char*)c_str() and
2532 // (wchar_t*)c_str():
2533 // FIXME-UTF8: bechmark various approaches to keeping compatibility buffers
2534 template<typename T
>
2535 struct ConvertedBuffer
2537 ConvertedBuffer() : m_buf(NULL
) {}
2541 operator T
*() const { return m_buf
; }
2543 ConvertedBuffer
& operator=(T
*str
)
2552 #if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
2553 ConvertedBuffer
<char> m_convertedToChar
;
2555 #if !wxUSE_UNICODE_WCHAR
2556 ConvertedBuffer
<wchar_t> m_convertedToWChar
;
2559 #if wxUSE_UNICODE_UTF8
2560 // FIXME-UTF8: (try to) move this elsewhere (TLS) or solve differently
2561 // assigning to character pointer to by wxString::interator may
2562 // change the underlying wxStringImpl iterator, so we have to
2563 // keep track of all iterators and update them as necessary:
2564 struct wxStringIteratorNodeHead
2566 wxStringIteratorNodeHead() : ptr(NULL
) {}
2567 wxStringIteratorNode
*ptr
;
2570 wxStringIteratorNodeHead m_iterators
;
2572 friend class WXDLLIMPEXP_BASE wxStringIteratorNode
;
2573 friend class WXDLLIMPEXP_BASE wxUniCharRef
;
2574 #endif // wxUSE_UNICODE_UTF8
2576 friend class WXDLLIMPEXP_BASE wxCStrData
;
2577 friend class wxImplStringBuffer
;
2578 friend class wxImplStringBufferLength
;
2581 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
2582 #pragma warning (default:4275)
2585 // string iterator operators that satisfy STL Random Access Iterator
2587 inline wxString::iterator
operator+(int n
, wxString::iterator i
)
2589 inline wxString::iterator
operator+(size_t n
, wxString::iterator i
)
2591 inline wxString::const_iterator
operator+(int n
, wxString::const_iterator i
)
2593 inline wxString::const_iterator
operator+(size_t n
, wxString::const_iterator i
)
2595 inline wxString::reverse_iterator
operator+(int n
, wxString::reverse_iterator i
)
2597 inline wxString::reverse_iterator
operator+(size_t n
, wxString::reverse_iterator i
)
2599 inline wxString::const_reverse_iterator
operator+(int n
, wxString::const_reverse_iterator i
)
2601 inline wxString::const_reverse_iterator
operator+(size_t n
, wxString::const_reverse_iterator i
)
2604 // notice that even though for many compilers the friend declarations above are
2605 // enough, from the point of view of C++ standard we must have the declarations
2606 // here as friend ones are not injected in the enclosing namespace and without
2607 // them the code fails to compile with conforming compilers such as xlC or g++4
2608 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
, const wxString
& string2
);
2609 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const char *psz
);
2610 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const wchar_t *pwz
);
2611 wxString WXDLLIMPEXP_BASE
operator+(const char *psz
, const wxString
& string
);
2612 wxString WXDLLIMPEXP_BASE
operator+(const wchar_t *pwz
, const wxString
& string
);
2614 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
2615 wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
2617 inline wxString
operator+(const wxString
& string
, wxUniCharRef ch
)
2618 { return string
+ (wxUniChar
)ch
; }
2619 inline wxString
operator+(const wxString
& string
, char ch
)
2620 { return string
+ wxUniChar(ch
); }
2621 inline wxString
operator+(const wxString
& string
, wchar_t ch
)
2622 { return string
+ wxUniChar(ch
); }
2623 inline wxString
operator+(wxUniCharRef ch
, const wxString
& string
)
2624 { return (wxUniChar
)ch
+ string
; }
2625 inline wxString
operator+(char ch
, const wxString
& string
)
2626 { return wxUniChar(ch
) + string
; }
2627 inline wxString
operator+(wchar_t ch
, const wxString
& string
)
2628 { return wxUniChar(ch
) + string
; }
2631 #if wxUSE_STL_BASED_WXSTRING
2632 // return an empty wxString (not very useful with wxUSE_STL == 1)
2633 inline const wxString
wxGetEmptyString() { return wxString(); }
2634 #else // !wxUSE_STL_BASED_WXSTRING
2635 // return an empty wxString (more efficient than wxString() here)
2636 inline const wxString
& wxGetEmptyString()
2638 return *(wxString
*)&wxEmptyString
;
2640 #endif // wxUSE_STL_BASED_WXSTRING/!wxUSE_STL_BASED_WXSTRING
2642 // ----------------------------------------------------------------------------
2643 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
2644 // ----------------------------------------------------------------------------
2646 #if !wxUSE_STL_BASED_WXSTRING
2647 // string buffer for direct access to string data in their native
2649 class wxImplStringBuffer
2652 typedef wxStringCharType CharType
;
2654 wxImplStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
2655 : m_str(str
), m_buf(NULL
)
2656 { m_buf
= m_str
.DoGetWriteBuf(lenWanted
); }
2658 ~wxImplStringBuffer() { m_str
.DoUngetWriteBuf(); }
2660 operator wxStringCharType
*() const { return m_buf
; }
2664 wxStringCharType
*m_buf
;
2666 DECLARE_NO_COPY_CLASS(wxImplStringBuffer
)
2669 class wxImplStringBufferLength
2672 typedef wxStringCharType CharType
;
2674 wxImplStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
2675 : m_str(str
), m_buf(NULL
), m_len(0), m_lenSet(false)
2677 m_buf
= m_str
.DoGetWriteBuf(lenWanted
);
2678 wxASSERT(m_buf
!= NULL
);
2681 ~wxImplStringBufferLength()
2684 m_str
.DoUngetWriteBuf(m_len
);
2687 operator wxStringCharType
*() const { return m_buf
; }
2688 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
2692 wxStringCharType
*m_buf
;
2696 DECLARE_NO_COPY_CLASS(wxImplStringBufferLength
)
2699 #endif // !wxUSE_STL_BASED_WXSTRING
2701 template<typename T
>
2702 class wxStringTypeBufferBase
2707 wxStringTypeBufferBase(wxString
& str
, size_t lenWanted
= 1024)
2708 : m_str(str
), m_buf(lenWanted
)
2712 operator CharType
*() { return m_buf
.data(); }
2716 wxCharTypeBuffer
<CharType
> m_buf
;
2719 template<typename T
>
2720 class wxStringTypeBufferLengthBase
2725 wxStringTypeBufferLengthBase(wxString
& str
, size_t lenWanted
= 1024)
2726 : m_str(str
), m_buf(lenWanted
), m_len(0), m_lenSet(false)
2729 ~wxStringTypeBufferLengthBase()
2732 m_str
.assign(m_buf
.data(), m_len
);
2735 operator CharType
*() { return m_buf
.data(); }
2736 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
2740 wxCharTypeBuffer
<CharType
> m_buf
;
2745 template<typename T
>
2746 class wxStringTypeBuffer
: public wxStringTypeBufferBase
<T
>
2749 wxStringTypeBuffer(wxString
& str
, size_t lenWanted
= 1024)
2750 : wxStringTypeBufferBase
<T
>(str
, lenWanted
) {}
2751 ~wxStringTypeBuffer()
2753 this->m_str
.assign(this->m_buf
.data());
2756 DECLARE_NO_COPY_CLASS(wxStringTypeBuffer
)
2759 template<typename T
>
2760 class wxStringTypeBufferLength
: public wxStringTypeBufferLengthBase
<T
>
2763 wxStringTypeBufferLength(wxString
& str
, size_t lenWanted
= 1024)
2764 : wxStringTypeBufferLengthBase
<T
>(str
, lenWanted
) {}
2766 ~wxStringTypeBufferLength()
2768 wxASSERT(this->m_lenSet
);
2769 this->m_str
.assign(this->m_buf
.data(), this->m_len
);
2772 DECLARE_NO_COPY_CLASS(wxStringTypeBufferLength
)
2775 #if wxUSE_STL_BASED_WXSTRING
2776 class wxImplStringBuffer
: public wxStringTypeBufferBase
<wxStringCharType
>
2779 wxImplStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
2780 : wxStringTypeBufferBase
<wxStringCharType
>(str
, lenWanted
) {}
2781 ~wxImplStringBuffer()
2782 { m_str
.m_impl
.assign(m_buf
.data()); }
2784 DECLARE_NO_COPY_CLASS(wxImplStringBuffer
)
2787 class wxImplStringBufferLength
: public wxStringTypeBufferLengthBase
<wxStringCharType
>
2790 wxImplStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
2791 : wxStringTypeBufferLengthBase
<wxStringCharType
>(str
, lenWanted
) {}
2793 ~wxImplStringBufferLength()
2796 m_str
.m_impl
.assign(m_buf
.data(), m_len
);
2799 DECLARE_NO_COPY_CLASS(wxImplStringBufferLength
)
2801 #endif // wxUSE_STL_BASED_WXSTRING
2804 #if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
2805 typedef wxStringTypeBuffer
<wxChar
> wxStringBuffer
;
2806 typedef wxStringTypeBufferLength
<wxChar
> wxStringBufferLength
;
2807 #else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2808 typedef wxImplStringBuffer wxStringBuffer
;
2809 typedef wxImplStringBufferLength wxStringBufferLength
;
2810 #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2812 // ---------------------------------------------------------------------------
2813 // wxString comparison functions: operator versions are always case sensitive
2814 // ---------------------------------------------------------------------------
2816 #define wxCMP_WXCHAR_STRING(p, s, op) 0 op s.Cmp(p)
2818 wxDEFINE_ALL_COMPARISONS(const wxChar
*, const wxString
&, wxCMP_WXCHAR_STRING
)
2820 #undef wxCMP_WXCHAR_STRING
2822 // note that there is an optimization in operator==() and !=(): we (quickly)
2823 // checks the strings length first, before comparing their data
2824 inline bool operator==(const wxString
& s1
, const wxString
& s2
)
2825 { return (s1
.Len() == s2
.Len()) && (s1
.Cmp(s2
) == 0); }
2826 inline bool operator!=(const wxString
& s1
, const wxString
& s2
)
2827 { return (s1
.Len() != s2
.Len()) || (s1
.Cmp(s2
) != 0); }
2828 inline bool operator< (const wxString
& s1
, const wxString
& s2
)
2829 { return s1
.Cmp(s2
) < 0; }
2830 inline bool operator> (const wxString
& s1
, const wxString
& s2
)
2831 { return s1
.Cmp(s2
) > 0; }
2832 inline bool operator<=(const wxString
& s1
, const wxString
& s2
)
2833 { return s1
.Cmp(s2
) <= 0; }
2834 inline bool operator>=(const wxString
& s1
, const wxString
& s2
)
2835 { return s1
.Cmp(s2
) >= 0; }
2837 inline bool operator==(const wxString
& s1
, const wxCStrData
& s2
)
2838 { return s1
== s2
.AsString(); }
2839 inline bool operator==(const wxCStrData
& s1
, const wxString
& s2
)
2840 { return s1
.AsString() == s2
; }
2841 inline bool operator!=(const wxString
& s1
, const wxCStrData
& s2
)
2842 { return s1
!= s2
.AsString(); }
2843 inline bool operator!=(const wxCStrData
& s1
, const wxString
& s2
)
2844 { return s1
.AsString() != s2
; }
2846 inline bool operator==(const wxString
& s1
, const wxWCharBuffer
& s2
)
2847 { return (s1
.Cmp((const wchar_t *)s2
) == 0); }
2848 inline bool operator==(const wxWCharBuffer
& s1
, const wxString
& s2
)
2849 { return (s2
.Cmp((const wchar_t *)s1
) == 0); }
2850 inline bool operator!=(const wxString
& s1
, const wxWCharBuffer
& s2
)
2851 { return (s1
.Cmp((const wchar_t *)s2
) != 0); }
2852 inline bool operator!=(const wxWCharBuffer
& s1
, const wxString
& s2
)
2853 { return (s2
.Cmp((const wchar_t *)s1
) != 0); }
2855 inline bool operator==(const wxString
& s1
, const wxCharBuffer
& s2
)
2856 { return (s1
.Cmp((const char *)s2
) == 0); }
2857 inline bool operator==(const wxCharBuffer
& s1
, const wxString
& s2
)
2858 { return (s2
.Cmp((const char *)s1
) == 0); }
2859 inline bool operator!=(const wxString
& s1
, const wxCharBuffer
& s2
)
2860 { return (s1
.Cmp((const char *)s2
) != 0); }
2861 inline bool operator!=(const wxCharBuffer
& s1
, const wxString
& s2
)
2862 { return (s2
.Cmp((const char *)s1
) != 0); }
2864 inline wxString
operator+(const wxString
& string
, const wxWCharBuffer
& buf
)
2865 { return string
+ (const wchar_t *)buf
; }
2866 inline wxString
operator+(const wxWCharBuffer
& buf
, const wxString
& string
)
2867 { return (const wchar_t *)buf
+ string
; }
2869 inline wxString
operator+(const wxString
& string
, const wxCharBuffer
& buf
)
2870 { return string
+ (const char *)buf
; }
2871 inline wxString
operator+(const wxCharBuffer
& buf
, const wxString
& string
)
2872 { return (const char *)buf
+ string
; }
2874 // comparison with char
2875 inline bool operator==(const wxUniChar
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2876 inline bool operator==(const wxUniCharRef
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2877 inline bool operator==(char c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2878 inline bool operator==(wchar_t c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2879 inline bool operator==(int c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2880 inline bool operator==(const wxString
& s
, const wxUniChar
& c
) { return s
.IsSameAs(c
); }
2881 inline bool operator==(const wxString
& s
, const wxUniCharRef
& c
) { return s
.IsSameAs(c
); }
2882 inline bool operator==(const wxString
& s
, char c
) { return s
.IsSameAs(c
); }
2883 inline bool operator==(const wxString
& s
, wchar_t c
) { return s
.IsSameAs(c
); }
2884 inline bool operator!=(const wxUniChar
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2885 inline bool operator!=(const wxUniCharRef
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2886 inline bool operator!=(char c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2887 inline bool operator!=(wchar_t c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2888 inline bool operator!=(int c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2889 inline bool operator!=(const wxString
& s
, const wxUniChar
& c
) { return !s
.IsSameAs(c
); }
2890 inline bool operator!=(const wxString
& s
, const wxUniCharRef
& c
) { return !s
.IsSameAs(c
); }
2891 inline bool operator!=(const wxString
& s
, char c
) { return !s
.IsSameAs(c
); }
2892 inline bool operator!=(const wxString
& s
, wchar_t c
) { return !s
.IsSameAs(c
); }
2894 // comparison with C string in Unicode build
2897 #define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s
2899 wxDEFINE_ALL_COMPARISONS(const char *, const wxString
&, wxCMP_CHAR_STRING
)
2901 #undef wxCMP_CHAR_STRING
2903 #endif // wxUSE_UNICODE
2905 // we also need to provide the operators for comparison with wxCStrData to
2906 // resolve ambiguity between operator(const wxChar *,const wxString &) and
2907 // operator(const wxChar *, const wxChar *) for "p == s.c_str()"
2909 // notice that these are (shallow) pointer comparisons, not (deep) string ones
2910 #define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar()
2911 #define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar()
2913 wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData
&, wxCMP_WCHAR_CSTRDATA
)
2914 wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData
&, wxCMP_CHAR_CSTRDATA
)
2916 #undef wxCMP_CHAR_CSTRDATA
2917 #undef wxCMP_WCHAR_CSTRDATA
2919 // ---------------------------------------------------------------------------
2920 // Implementation only from here until the end of file
2921 // ---------------------------------------------------------------------------
2923 #if wxUSE_STD_IOSTREAM
2925 #include "wx/iosfwrap.h"
2927 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxString
&);
2928 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxCStrData
&);
2929 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxCharBuffer
&);
2930 #ifndef __BORLANDC__
2931 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxWCharBuffer
&);
2934 #endif // wxSTD_STRING_COMPATIBILITY
2936 // ---------------------------------------------------------------------------
2937 // wxCStrData implementation
2938 // ---------------------------------------------------------------------------
2940 inline wxCStrData::wxCStrData(char *buf
)
2941 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
2942 inline wxCStrData::wxCStrData(wchar_t *buf
)
2943 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
2945 inline wxCStrData::wxCStrData(const wxCStrData
& data
)
2946 : m_str(data
.m_owned
? new wxString(*data
.m_str
) : data
.m_str
),
2947 m_offset(data
.m_offset
),
2948 m_owned(data
.m_owned
)
2952 inline wxCStrData::~wxCStrData()
2958 // simple cases for AsChar() and AsWChar(), the complicated ones are
2960 #if wxUSE_UNICODE_WCHAR
2961 inline const wchar_t* wxCStrData::AsWChar() const
2963 return m_str
->wx_str() + m_offset
;
2965 #endif // wxUSE_UNICODE_WCHAR
2968 inline const char* wxCStrData::AsChar() const
2970 return m_str
->wx_str() + m_offset
;
2972 #endif // !wxUSE_UNICODE
2974 #if wxUSE_UTF8_LOCALE_ONLY
2975 inline const char* wxCStrData::AsChar() const
2977 return wxStringOperations::AddToIter(m_str
->wx_str(), m_offset
);
2979 #endif // wxUSE_UTF8_LOCALE_ONLY
2981 inline const wxCharBuffer
wxCStrData::AsCharBuf() const
2984 return wxCharBuffer::CreateNonOwned(AsChar());
2986 return AsString().mb_str();
2990 inline const wxWCharBuffer
wxCStrData::AsWCharBuf() const
2992 #if wxUSE_UNICODE_WCHAR
2993 return wxWCharBuffer::CreateNonOwned(AsWChar());
2995 return AsString().wc_str();
2999 inline wxString
wxCStrData::AsString() const
3001 if ( m_offset
== 0 )
3004 return m_str
->Mid(m_offset
);
3007 inline const wxStringCharType
*wxCStrData::AsInternal() const
3009 #if wxUSE_UNICODE_UTF8
3010 return wxStringOperations::AddToIter(m_str
->wx_str(), m_offset
);
3012 return m_str
->wx_str() + m_offset
;
3016 inline wxUniChar
wxCStrData::operator*() const
3018 if ( m_str
->empty() )
3019 return wxUniChar(_T('\0'));
3021 return (*m_str
)[m_offset
];
3024 inline wxUniChar
wxCStrData::operator[](size_t n
) const
3026 // NB: we intentionally use operator[] and not at() here because the former
3027 // works for the terminating NUL while the latter does not
3028 return (*m_str
)[m_offset
+ n
];
3031 // ----------------------------------------------------------------------------
3032 // more wxCStrData operators
3033 // ----------------------------------------------------------------------------
3035 // we need to define those to allow "size_t pos = p - s.c_str()" where p is
3036 // some pointer into the string
3037 inline size_t operator-(const char *p
, const wxCStrData
& cs
)
3039 return p
- cs
.AsChar();
3042 inline size_t operator-(const wchar_t *p
, const wxCStrData
& cs
)
3044 return p
- cs
.AsWChar();
3047 // ----------------------------------------------------------------------------
3048 // implementation of wx[W]CharBuffer inline methods using wxCStrData
3049 // ----------------------------------------------------------------------------
3051 // FIXME-UTF8: move this to buffer.h
3052 inline wxCharBuffer::wxCharBuffer(const wxCStrData
& cstr
)
3053 : wxCharTypeBufferBase(cstr
.AsCharBuf())
3057 inline wxWCharBuffer::wxWCharBuffer(const wxCStrData
& cstr
)
3058 : wxCharTypeBufferBase(cstr
.AsWCharBuf())
3062 #if wxUSE_UNICODE_UTF8
3063 // ----------------------------------------------------------------------------
3064 // implementation of wxStringIteratorNode inline methods
3065 // ----------------------------------------------------------------------------
3067 wxStringIteratorNode::wxStringIteratorNode(const wxString
*str
,
3068 wxStringImpl::const_iterator
*citer
)
3073 m_next(str
->m_iterators
.ptr
)
3075 wx_const_cast(wxString
*, m_str
)->m_iterators
.ptr
= this;
3077 m_next
->m_prev
= this;
3080 wxStringIteratorNode::wxStringIteratorNode(const wxString
*str
,
3081 wxStringImpl::iterator
*iter
)
3086 m_next(str
->m_iterators
.ptr
)
3088 wx_const_cast(wxString
*, m_str
)->m_iterators
.ptr
= this;
3090 m_next
->m_prev
= this;
3093 wxStringIteratorNode::~wxStringIteratorNode()
3096 m_next
->m_prev
= m_prev
;
3098 m_prev
->m_next
= m_next
;
3099 else // first in the list
3100 wx_const_cast(wxString
*, m_str
)->m_iterators
.ptr
= m_next
;
3102 #endif // wxUSE_UNICODE_UTF8
3104 #if WXWIN_COMPATIBILITY_2_8
3105 // lot of code out there doesn't explicitly include wx/wxchar.h, but uses
3106 // CRT wrappers that are now declared in wx/wxcrt.h and wx/wxcrtvararg.h,
3107 // so let's include this header now that wxString is defined and it's safe
3109 #include "wx/wxchar.h"
3112 #endif // _WX_WXSTRING_H_