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/wxcrtbase.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 // the node belongs to a particular iterator instance, it's not copied
395 // when a copy of the iterator is made
396 DECLARE_NO_COPY_CLASS(wxStringIteratorNode
)
398 #endif // wxUSE_UNICODE_UTF8
400 class WXDLLIMPEXP_BASE wxString
401 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
402 : public wxStringPrintfMixin
405 // NB: special care was taken in arranging the member functions in such order
406 // that all inline functions can be effectively inlined, verify that all
407 // performance critical functions are still inlined if you change order!
409 // an 'invalid' value for string index, moved to this place due to a CW bug
410 static const size_t npos
;
413 // if we hadn't made these operators private, it would be possible to
414 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
415 // converted to char in C and we do have operator=(char)
417 // NB: we don't need other versions (short/long and unsigned) as attempt
418 // to assign another numeric type to wxString will now result in
419 // ambiguity between operator=(char) and operator=(int)
420 wxString
& operator=(int);
422 // these methods are not implemented - there is _no_ conversion from int to
423 // string, you're doing something wrong if the compiler wants to call it!
425 // try `s << i' or `s.Printf("%d", i)' instead
429 // buffer for holding temporary substring when using any of the methods
430 // that take (char*,size_t) or (wchar_t*,size_t) arguments:
432 struct SubstrBufFromType
437 SubstrBufFromType(const T
& data_
, size_t len_
)
438 : data(data_
), len(len_
) {}
441 #if wxUSE_UNICODE_UTF8
442 // even char* -> char* needs conversion, from locale charset to UTF-8
443 typedef SubstrBufFromType
<wxCharBuffer
> SubstrBufFromWC
;
444 typedef SubstrBufFromType
<wxCharBuffer
> SubstrBufFromMB
;
445 #elif wxUSE_UNICODE_WCHAR
446 typedef SubstrBufFromType
<const wchar_t*> SubstrBufFromWC
;
447 typedef SubstrBufFromType
<wxWCharBuffer
> SubstrBufFromMB
;
449 typedef SubstrBufFromType
<const char*> SubstrBufFromMB
;
450 typedef SubstrBufFromType
<wxCharBuffer
> SubstrBufFromWC
;
454 // Functions implementing primitive operations on string data; wxString
455 // methods and iterators are implemented in terms of it. The differences
456 // between UTF-8 and wchar_t* representations of the string are mostly
459 #if wxUSE_UNICODE_UTF8
460 static SubstrBufFromMB
ConvertStr(const char *psz
, size_t nLength
,
461 const wxMBConv
& conv
);
462 static SubstrBufFromWC
ConvertStr(const wchar_t *pwz
, size_t nLength
,
463 const wxMBConv
& conv
);
464 #elif wxUSE_UNICODE_WCHAR
465 static SubstrBufFromMB
ConvertStr(const char *psz
, size_t nLength
,
466 const wxMBConv
& conv
);
468 static SubstrBufFromWC
ConvertStr(const wchar_t *pwz
, size_t nLength
,
469 const wxMBConv
& conv
);
472 #if !wxUSE_UNICODE_UTF8 // wxUSE_UNICODE_WCHAR or !wxUSE_UNICODE
473 // returns C string encoded as the implementation expects:
475 static const wchar_t* ImplStr(const wchar_t* str
)
476 { return str
? str
: wxT(""); }
477 static const SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
478 { return SubstrBufFromWC(str
, (str
&& n
== npos
) ? wxWcslen(str
) : n
); }
479 static wxWCharBuffer
ImplStr(const char* str
,
480 const wxMBConv
& conv
= wxConvLibc
)
481 { return ConvertStr(str
, npos
, conv
).data
; }
482 static SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
483 const wxMBConv
& conv
= wxConvLibc
)
484 { return ConvertStr(str
, n
, conv
); }
486 static const char* ImplStr(const char* str
,
487 const wxMBConv
& WXUNUSED(conv
) = wxConvLibc
)
488 { return str
? str
: ""; }
489 static const SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
490 const wxMBConv
& WXUNUSED(conv
) = wxConvLibc
)
491 { return SubstrBufFromMB(str
, (str
&& n
== npos
) ? wxStrlen(str
) : n
); }
492 static wxCharBuffer
ImplStr(const wchar_t* str
)
493 { return ConvertStr(str
, npos
, wxConvLibc
).data
; }
494 static SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
495 { return ConvertStr(str
, n
, wxConvLibc
); }
498 // translates position index in wxString to/from index in underlying
500 static size_t PosToImpl(size_t pos
) { return pos
; }
501 static void PosLenToImpl(size_t pos
, size_t len
,
502 size_t *implPos
, size_t *implLen
)
503 { *implPos
= pos
; *implLen
= len
; }
504 static size_t LenToImpl(size_t len
) { return len
; }
505 static size_t PosFromImpl(size_t pos
) { return pos
; }
507 #else // wxUSE_UNICODE_UTF8
509 static wxCharBuffer
ImplStr(const char* str
,
510 const wxMBConv
& conv
= wxConvLibc
)
511 { return ConvertStr(str
, npos
, conv
).data
; }
512 static SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
513 const wxMBConv
& conv
= wxConvLibc
)
514 { return ConvertStr(str
, n
, conv
); }
516 static wxCharBuffer
ImplStr(const wchar_t* str
)
517 { return ConvertStr(str
, npos
, wxMBConvUTF8()).data
; }
518 static SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
519 { return ConvertStr(str
, n
, wxMBConvUTF8()); }
521 size_t PosToImpl(size_t pos
) const
523 if ( pos
== 0 || pos
== npos
)
526 return (begin() + pos
).impl() - m_impl
.begin();
529 void PosLenToImpl(size_t pos
, size_t len
, size_t *implPos
, size_t *implLen
) const;
531 size_t LenToImpl(size_t len
) const
534 PosLenToImpl(0, len
, &pos
, &len2
);
538 size_t PosFromImpl(size_t pos
) const
540 if ( pos
== 0 || pos
== npos
)
543 return const_iterator(this, m_impl
.begin() + pos
) - begin();
545 #endif // !wxUSE_UNICODE_UTF8/wxUSE_UNICODE_UTF8
549 typedef wxUniChar value_type
;
550 typedef wxUniChar char_type
;
551 typedef wxUniCharRef reference
;
552 typedef wxChar
* pointer
;
553 typedef const wxChar
* const_pointer
;
555 typedef size_t size_type
;
556 typedef wxUniChar const_reference
;
559 #if wxUSE_UNICODE_UTF8
560 // random access is not O(1), as required by Random Access Iterator
561 #define WX_STR_ITERATOR_TAG std::bidirectional_iterator_tag
563 #define WX_STR_ITERATOR_TAG std::random_access_iterator_tag
566 #define WX_STR_ITERATOR_TAG void /* dummy type */
569 #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, reference_type) \
571 typedef wxStringImpl::iterator_name underlying_iterator; \
573 typedef WX_STR_ITERATOR_TAG iterator_category; \
574 typedef wxUniChar value_type; \
575 typedef int difference_type; \
576 typedef reference_type reference; \
577 typedef pointer_type pointer; \
579 reference operator[](size_t n) const { return *(*this + n); } \
581 iterator_name& operator++() \
582 { wxStringOperations::IncIter(m_cur); return *this; } \
583 iterator_name& operator--() \
584 { wxStringOperations::DecIter(m_cur); return *this; } \
585 iterator_name operator++(int) \
587 iterator_name tmp = *this; \
588 wxStringOperations::IncIter(m_cur); \
591 iterator_name operator--(int) \
593 iterator_name tmp = *this; \
594 wxStringOperations::DecIter(m_cur); \
598 iterator_name& operator+=(int n) \
600 m_cur = wxStringOperations::AddToIter(m_cur, n); \
603 iterator_name& operator+=(size_t n) \
605 m_cur = wxStringOperations::AddToIter(m_cur, (int)n); \
608 iterator_name& operator-=(int n) \
610 m_cur = wxStringOperations::AddToIter(m_cur, -n); \
613 iterator_name& operator-=(size_t n) \
615 m_cur = wxStringOperations::AddToIter(m_cur, -(int)n); \
619 difference_type operator-(const iterator_name& i) const \
620 { return wxStringOperations::DiffIters(m_cur, i.m_cur); } \
622 bool operator==(const iterator_name& i) const \
623 { return m_cur == i.m_cur; } \
624 bool operator!=(const iterator_name& i) const \
625 { 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; } \
631 bool operator<=(const iterator_name& i) const \
632 { return m_cur <= i.m_cur; } \
633 bool operator>=(const iterator_name& i) const \
634 { return m_cur >= i.m_cur; } \
637 /* for internal wxString use only: */ \
638 underlying_iterator impl() const { return m_cur; } \
640 friend class WXDLLIMPEXP_BASE wxString; \
641 friend class WXDLLIMPEXP_BASE wxCStrData; \
644 underlying_iterator m_cur
646 class WXDLLIMPEXP_BASE const_iterator
;
648 #if wxUSE_UNICODE_UTF8
649 // NB: In UTF-8 build, (non-const) iterator needs to keep reference
650 // to the underlying wxStringImpl, because UTF-8 is variable-length
651 // encoding and changing the value pointer to by an iterator (using
652 // its operator*) requires calling wxStringImpl::replace() if the old
653 // and new values differ in their encoding's length.
655 // Furthermore, the replace() call may invalid all iterators for the
656 // string, so we have to keep track of outstanding iterators and update
657 // them if replace() happens.
659 // This is implemented by maintaining linked list of iterators for every
660 // string and traversing it in wxUniCharRef::operator=(). Head of the
661 // list is stored in wxString. (FIXME-UTF8)
663 class WXDLLIMPEXP_BASE iterator
665 WX_STR_ITERATOR_IMPL(iterator
, wxChar
*, wxUniCharRef
);
668 iterator(const iterator
& i
)
669 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
670 iterator
& operator=(const iterator
& i
)
671 { m_cur
= i
.m_cur
; return *this; }
673 reference
operator*()
674 { return wxUniCharRef::CreateForString(m_node
, m_cur
); }
676 iterator
operator+(int n
) const
677 { return iterator(str(), wxStringOperations::AddToIter(m_cur
, n
)); }
678 iterator
operator+(size_t n
) const
679 { return iterator(str(), wxStringOperations::AddToIter(m_cur
, (int)n
)); }
680 iterator
operator-(int n
) const
681 { return iterator(str(), wxStringOperations::AddToIter(m_cur
, -n
)); }
682 iterator
operator-(size_t n
) const
683 { return iterator(str(), wxStringOperations::AddToIter(m_cur
, -(int)n
)); }
686 iterator(wxString
*str
, underlying_iterator ptr
)
687 : m_cur(ptr
), m_node(str
, &m_cur
) {}
689 wxString
* str() const { return wx_const_cast(wxString
*, m_node
.m_str
); }
691 wxStringIteratorNode m_node
;
693 friend class const_iterator
;
696 class WXDLLIMPEXP_BASE const_iterator
698 // NB: reference_type is intentionally value, not reference, the character
699 // may be encoded differently in wxString data:
700 WX_STR_ITERATOR_IMPL(const_iterator
, const wxChar
*, wxUniChar
);
703 const_iterator(const const_iterator
& i
)
704 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
705 const_iterator(const iterator
& i
)
706 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
708 const_iterator
& operator=(const const_iterator
& i
)
709 { m_cur
= i
.m_cur
; return *this; }
710 const_iterator
& operator=(const iterator
& i
)
711 { m_cur
= i
.m_cur
; return *this; }
713 reference
operator*() const
714 { return wxStringOperations::DecodeChar(m_cur
); }
716 const_iterator
operator+(int n
) const
717 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur
, n
)); }
718 const_iterator
operator+(size_t n
) const
719 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur
, (int)n
)); }
720 const_iterator
operator-(int n
) const
721 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur
, -n
)); }
722 const_iterator
operator-(size_t n
) const
723 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur
, -(int)n
)); }
726 // for internal wxString use only:
727 const_iterator(const wxString
*str
, underlying_iterator ptr
)
728 : m_cur(ptr
), m_node(str
, &m_cur
) {}
730 const wxString
* str() const { return m_node
.m_str
; }
732 wxStringIteratorNode m_node
;
735 size_t IterToImplPos(wxString::iterator i
) const
736 { return wxStringImpl::const_iterator(i
.impl()) - m_impl
.begin(); }
738 #else // !wxUSE_UNICODE_UTF8
740 class WXDLLIMPEXP_BASE iterator
742 WX_STR_ITERATOR_IMPL(iterator
, wxChar
*, wxUniCharRef
);
745 iterator(const iterator
& i
) : m_cur(i
.m_cur
) {}
747 reference
operator*()
748 { return wxUniCharRef::CreateForString(m_cur
); }
750 iterator
operator+(int n
) const
751 { return iterator(wxStringOperations::AddToIter(m_cur
, n
)); }
752 iterator
operator+(size_t n
) const
753 { return iterator(wxStringOperations::AddToIter(m_cur
, (int)n
)); }
754 iterator
operator-(int n
) const
755 { return iterator(wxStringOperations::AddToIter(m_cur
, -n
)); }
756 iterator
operator-(size_t n
) const
757 { return iterator(wxStringOperations::AddToIter(m_cur
, -(int)n
)); }
760 // for internal wxString use only:
761 iterator(underlying_iterator ptr
) : m_cur(ptr
) {}
762 iterator(wxString
*WXUNUSED(str
), underlying_iterator ptr
) : m_cur(ptr
) {}
764 friend class const_iterator
;
767 class WXDLLIMPEXP_BASE const_iterator
769 // NB: reference_type is intentionally value, not reference, the character
770 // may be encoded differently in wxString data:
771 WX_STR_ITERATOR_IMPL(const_iterator
, const wxChar
*, wxUniChar
);
774 const_iterator(const const_iterator
& i
) : m_cur(i
.m_cur
) {}
775 const_iterator(const iterator
& i
) : m_cur(i
.m_cur
) {}
777 reference
operator*() const
778 { return wxStringOperations::DecodeChar(m_cur
); }
780 const_iterator
operator+(int n
) const
781 { return const_iterator(wxStringOperations::AddToIter(m_cur
, n
)); }
782 const_iterator
operator+(size_t n
) const
783 { return const_iterator(wxStringOperations::AddToIter(m_cur
, (int)n
)); }
784 const_iterator
operator-(int n
) const
785 { return const_iterator(wxStringOperations::AddToIter(m_cur
, -n
)); }
786 const_iterator
operator-(size_t n
) const
787 { return const_iterator(wxStringOperations::AddToIter(m_cur
, -(int)n
)); }
790 // for internal wxString use only:
791 const_iterator(underlying_iterator ptr
) : m_cur(ptr
) {}
792 const_iterator(const wxString
*WXUNUSED(str
), underlying_iterator ptr
)
795 #endif // wxUSE_UNICODE_UTF8/!wxUSE_UNICODE_UTF8
797 #undef WX_STR_ITERATOR_TAG
798 #undef WX_STR_ITERATOR_IMPL
800 friend class iterator
;
801 friend class const_iterator
;
803 template <typename T
>
804 class reverse_iterator_impl
807 typedef T iterator_type
;
809 typedef typename
T::iterator_category iterator_category
;
810 typedef typename
T::value_type value_type
;
811 typedef typename
T::difference_type difference_type
;
812 typedef typename
T::reference reference
;
813 typedef typename
T::pointer
*pointer
;
815 reverse_iterator_impl(iterator_type i
) : m_cur(i
) {}
816 reverse_iterator_impl(const reverse_iterator_impl
& ri
)
819 iterator_type
base() const { return m_cur
; }
821 reference
operator*() const { return *(m_cur
-1); }
822 reference
operator[](size_t n
) const { return *(*this + n
); }
824 reverse_iterator_impl
& operator++()
825 { --m_cur
; return *this; }
826 reverse_iterator_impl
operator++(int)
827 { reverse_iterator_impl tmp
= *this; --m_cur
; return tmp
; }
828 reverse_iterator_impl
& operator--()
829 { ++m_cur
; return *this; }
830 reverse_iterator_impl
operator--(int)
831 { reverse_iterator_impl tmp
= *this; ++m_cur
; return tmp
; }
833 // NB: explicit <T> in the functions below is to keep BCC 5.5 happy
834 reverse_iterator_impl
operator+(int n
) const
835 { return reverse_iterator_impl
<T
>(m_cur
- n
); }
836 reverse_iterator_impl
operator+(size_t n
) const
837 { return reverse_iterator_impl
<T
>(m_cur
- n
); }
838 reverse_iterator_impl
operator-(int n
) const
839 { return reverse_iterator_impl
<T
>(m_cur
+ n
); }
840 reverse_iterator_impl
operator-(size_t n
) const
841 { return reverse_iterator_impl
<T
>(m_cur
+ n
); }
842 reverse_iterator_impl
operator+=(int n
)
843 { m_cur
-= n
; return *this; }
844 reverse_iterator_impl
operator+=(size_t n
)
845 { m_cur
-= n
; return *this; }
846 reverse_iterator_impl
operator-=(int n
)
847 { m_cur
+= n
; return *this; }
848 reverse_iterator_impl
operator-=(size_t n
)
849 { m_cur
+= n
; return *this; }
851 unsigned operator-(const reverse_iterator_impl
& i
) const
852 { return i
.m_cur
- m_cur
; }
854 bool operator==(const reverse_iterator_impl
& ri
) const
855 { return m_cur
== ri
.m_cur
; }
856 bool operator!=(const reverse_iterator_impl
& ri
) const
857 { return !(*this == ri
); }
859 bool operator<(const reverse_iterator_impl
& i
) const
860 { return m_cur
> i
.m_cur
; }
861 bool operator>(const reverse_iterator_impl
& i
) const
862 { return m_cur
< i
.m_cur
; }
863 bool operator<=(const reverse_iterator_impl
& i
) const
864 { return m_cur
>= i
.m_cur
; }
865 bool operator>=(const reverse_iterator_impl
& i
) const
866 { return m_cur
<= i
.m_cur
; }
872 typedef reverse_iterator_impl
<iterator
> reverse_iterator
;
873 typedef reverse_iterator_impl
<const_iterator
> const_reverse_iterator
;
876 // used to transform an expression built using c_str() (and hence of type
877 // wxCStrData) to an iterator into the string
878 static const_iterator
CreateConstIterator(const wxCStrData
& data
)
880 return const_iterator(data
.m_str
,
881 (data
.m_str
->begin() + data
.m_offset
).impl());
884 // in UTF-8 STL build, creation from std::string requires conversion under
885 // non-UTF8 locales, so we can't have and use wxString(wxStringImpl) ctor;
886 // instead we define dummy type that lets us have wxString ctor for creation
887 // from wxStringImpl that couldn't be used by user code (in all other builds,
888 // "standard" ctors can be used):
889 #if wxUSE_UNICODE_UTF8 && wxUSE_STL_BASED_WXSTRING
890 struct CtorFromStringImplTag
{};
892 wxString(CtorFromStringImplTag
* WXUNUSED(dummy
), const wxStringImpl
& src
)
895 static wxString
FromImpl(const wxStringImpl
& src
)
896 { return wxString((CtorFromStringImplTag
*)NULL
, src
); }
898 #if !wxUSE_STL_BASED_WXSTRING
899 wxString(const wxStringImpl
& src
) : m_impl(src
) { }
900 // else: already defined as wxString(wxStdString) below
902 static wxString
FromImpl(const wxStringImpl
& src
) { return wxString(src
); }
906 // constructors and destructor
907 // ctor for an empty string
911 wxString(const wxString
& stringSrc
) : m_impl(stringSrc
.m_impl
) { }
913 // string containing nRepeat copies of ch
914 wxString(wxUniChar ch
, size_t nRepeat
= 1)
915 { assign(nRepeat
, ch
); }
916 wxString(size_t nRepeat
, wxUniChar ch
)
917 { assign(nRepeat
, ch
); }
918 wxString(wxUniCharRef ch
, size_t nRepeat
= 1)
919 { assign(nRepeat
, ch
); }
920 wxString(size_t nRepeat
, wxUniCharRef ch
)
921 { assign(nRepeat
, ch
); }
922 wxString(char ch
, size_t nRepeat
= 1)
923 { assign(nRepeat
, ch
); }
924 wxString(size_t nRepeat
, char ch
)
925 { assign(nRepeat
, ch
); }
926 wxString(wchar_t ch
, size_t nRepeat
= 1)
927 { assign(nRepeat
, ch
); }
928 wxString(size_t nRepeat
, wchar_t ch
)
929 { assign(nRepeat
, ch
); }
931 // ctors from char* strings:
932 wxString(const char *psz
)
933 : m_impl(ImplStr(psz
)) {}
934 wxString(const char *psz
, const wxMBConv
& conv
)
935 : m_impl(ImplStr(psz
, conv
)) {}
936 wxString(const char *psz
, size_t nLength
)
937 { assign(psz
, nLength
); }
938 wxString(const char *psz
, const wxMBConv
& conv
, size_t nLength
)
940 SubstrBufFromMB
str(ImplStr(psz
, nLength
, conv
));
941 m_impl
.assign(str
.data
, str
.len
);
944 // and unsigned char*:
945 wxString(const unsigned char *psz
)
946 : m_impl(ImplStr((const char*)psz
)) {}
947 wxString(const unsigned char *psz
, const wxMBConv
& conv
)
948 : m_impl(ImplStr((const char*)psz
, conv
)) {}
949 wxString(const unsigned char *psz
, size_t nLength
)
950 { assign((const char*)psz
, nLength
); }
951 wxString(const unsigned char *psz
, const wxMBConv
& conv
, size_t nLength
)
953 SubstrBufFromMB
str(ImplStr((const char*)psz
, nLength
, conv
));
954 m_impl
.assign(str
.data
, str
.len
);
957 // ctors from wchar_t* strings:
958 wxString(const wchar_t *pwz
)
959 : m_impl(ImplStr(pwz
)) {}
960 wxString(const wchar_t *pwz
, const wxMBConv
& WXUNUSED(conv
))
961 : m_impl(ImplStr(pwz
)) {}
962 wxString(const wchar_t *pwz
, size_t nLength
)
963 { assign(pwz
, nLength
); }
964 wxString(const wchar_t *pwz
, const wxMBConv
& WXUNUSED(conv
), size_t nLength
)
965 { assign(pwz
, nLength
); }
967 wxString(const wxCharBuffer
& buf
)
968 { assign(buf
.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
969 wxString(const wxWCharBuffer
& buf
)
970 { assign(buf
.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
972 wxString(const wxCStrData
& cstr
)
973 : m_impl(cstr
.AsString().m_impl
) { }
975 // as we provide both ctors with this signature for both char and unsigned
976 // char string, we need to provide one for wxCStrData to resolve ambiguity
977 wxString(const wxCStrData
& cstr
, size_t nLength
)
978 : m_impl(cstr
.AsString().Mid(0, nLength
).m_impl
) {}
980 // and because wxString is convertible to wxCStrData and const wxChar *
981 // we also need to provide this one
982 wxString(const wxString
& str
, size_t nLength
)
983 { assign(str
, nLength
); }
985 // even if we're not built with wxUSE_STL == 1 it is very convenient to allow
986 // implicit conversions from std::string to wxString and vice verse as this
987 // allows to use the same strings in non-GUI and GUI code, however we don't
988 // want to unconditionally add this ctor as it would make wx lib dependent on
989 // libstdc++ on some Linux versions which is bad, so instead we ask the
990 // client code to define this wxUSE_STD_STRING symbol if they need it
992 #if wxUSE_UNICODE_WCHAR
993 wxString(const wxStdWideString
& str
) : m_impl(str
) {}
994 #else // UTF-8 or ANSI
995 wxString(const wxStdWideString
& str
)
996 { assign(str
.c_str(), str
.length()); }
999 #if !wxUSE_UNICODE // ANSI build
1000 // FIXME-UTF8: do this in UTF8 build #if wxUSE_UTF8_LOCALE_ONLY, too
1001 wxString(const std::string
& str
) : m_impl(str
) {}
1003 wxString(const std::string
& str
)
1004 { assign(str
.c_str(), str
.length()); }
1007 #if wxUSE_UNICODE_WCHAR && wxUSE_STL_BASED_WXSTRING
1008 // wxStringImpl is std::string in the encoding we want
1009 operator const wxStdWideString
&() const { return m_impl
; }
1011 // wxStringImpl is either not std::string or needs conversion
1012 operator wxStdWideString() const
1013 // FIXME-UTF8: broken for embedded NULs
1014 { return wxStdWideString(wc_str()); }
1017 #if (!wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY) && wxUSE_STL_BASED_WXSTRING
1018 // wxStringImpl is std::string in the encoding we want
1019 operator const std::string
&() const { return m_impl
; }
1021 // wxStringImpl is either not std::string or needs conversion
1022 operator std::string() const
1023 // FIXME-UTF8: broken for embedded NULs
1024 { return std::string(mb_str()); }
1028 // first valid index position
1029 const_iterator
begin() const { return const_iterator(this, m_impl
.begin()); }
1030 iterator
begin() { return iterator(this, m_impl
.begin()); }
1031 // position one after the last valid one
1032 const_iterator
end() const { return const_iterator(this, m_impl
.end()); }
1033 iterator
end() { return iterator(this, m_impl
.end()); }
1035 // first element of the reversed string
1036 const_reverse_iterator
rbegin() const
1037 { return const_reverse_iterator(end()); }
1038 reverse_iterator
rbegin()
1039 { return reverse_iterator(end()); }
1040 // one beyond the end of the reversed string
1041 const_reverse_iterator
rend() const
1042 { return const_reverse_iterator(begin()); }
1043 reverse_iterator
rend()
1044 { return reverse_iterator(begin()); }
1046 // std::string methods:
1047 #if wxUSE_UNICODE_UTF8
1048 size_t length() const { return end() - begin(); } // FIXME-UTF8: optimize!
1050 size_t length() const { return m_impl
.length(); }
1053 size_type
size() const { return length(); }
1054 size_type
max_size() const { return npos
; }
1056 bool empty() const { return m_impl
.empty(); }
1058 size_type
capacity() const { return m_impl
.capacity(); } // FIXME-UTF8
1059 void reserve(size_t sz
) { m_impl
.reserve(sz
); } // FIXME-UTF8
1061 void resize(size_t nSize
, wxUniChar ch
= wxT('\0'))
1063 #if wxUSE_UNICODE_UTF8
1064 if ( !ch
.IsAscii() )
1066 size_t len
= length();
1069 else if ( nSize
< len
)
1072 append(nSize
- len
, ch
);
1076 m_impl
.resize(nSize
, (wxStringCharType
)ch
);
1079 wxString
substr(size_t nStart
= 0, size_t nLen
= npos
) const
1082 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
1083 return FromImpl(m_impl
.substr(pos
, len
));
1086 // generic attributes & operations
1087 // as standard strlen()
1088 size_t Len() const { return length(); }
1089 // string contains any characters?
1090 bool IsEmpty() const { return empty(); }
1091 // empty string is "false", so !str will return true
1092 bool operator!() const { return empty(); }
1093 // truncate the string to given length
1094 wxString
& Truncate(size_t uiLen
);
1095 // empty string contents
1100 wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") );
1102 // empty the string and free memory
1105 wxString
tmp(wxEmptyString
);
1110 // Is an ascii value
1111 bool IsAscii() const;
1113 bool IsNumber() const;
1115 bool IsWord() const;
1117 // data access (all indexes are 0 based)
1119 wxUniChar
at(size_t n
) const
1120 { return *(begin() + n
); } // FIXME-UTF8: optimize?
1121 wxUniChar
GetChar(size_t n
) const
1123 // read/write access
1124 wxUniCharRef
at(size_t n
)
1125 { return *(begin() + n
); } // FIXME-UTF8: optimize?
1126 wxUniCharRef
GetWritableChar(size_t n
)
1129 void SetChar(size_t n
, wxUniChar ch
)
1132 // get last character
1133 wxUniChar
Last() const
1135 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1139 // get writable last character
1142 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1147 Note that we we must define all of the overloads below to avoid
1148 ambiguity when using str[0].
1150 wxUniChar
operator[](int n
) const
1152 wxUniChar
operator[](long n
) const
1154 wxUniChar
operator[](size_t n
) const
1156 #ifndef wxSIZE_T_IS_UINT
1157 wxUniChar
operator[](unsigned int n
) const
1159 #endif // size_t != unsigned int
1161 // operator versions of GetWriteableChar()
1162 wxUniCharRef
operator[](int n
)
1164 wxUniCharRef
operator[](long n
)
1166 wxUniCharRef
operator[](size_t n
)
1168 #ifndef wxSIZE_T_IS_UINT
1169 wxUniCharRef
operator[](unsigned int n
)
1171 #endif // size_t != unsigned int
1173 // explicit conversion to C string (use this with printf()!)
1174 wxCStrData
c_str() const { return wxCStrData(this); }
1175 wxCStrData
data() const { return c_str(); }
1177 // implicit conversion to C string
1178 operator wxCStrData() const { return c_str(); }
1179 operator const char*() const { return c_str(); }
1180 operator const wchar_t*() const { return c_str(); }
1182 // implicit conversion to untyped pointer for compatibility with previous
1183 // wxWidgets versions: this is the same as conversion to const char * so it
1185 operator const void*() const { return c_str(); }
1187 // identical to c_str(), for MFC compatibility
1188 const wxCStrData
GetData() const { return c_str(); }
1190 // explicit conversion to C string in internal representation (char*,
1191 // wchar_t*, UTF-8-encoded char*, depending on the build):
1192 const wxStringCharType
*wx_str() const { return m_impl
.c_str(); }
1194 // conversion to *non-const* multibyte or widestring buffer; modifying
1195 // returned buffer won't affect the string, these methods are only useful
1196 // for passing values to const-incorrect functions
1197 wxWritableCharBuffer
char_str(const wxMBConv
& conv
= wxConvLibc
) const
1198 { return mb_str(conv
); }
1199 wxWritableWCharBuffer
wchar_str() const { return wc_str(); }
1201 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
1202 // converting numbers or strings which are certain not to contain special
1203 // chars (typically system functions, X atoms, environment variables etc.)
1205 // the behaviour of these functions with the strings containing anything
1206 // else than 7 bit ASCII characters is undefined, use at your own risk.
1208 static wxString
FromAscii(const char *ascii
); // string
1209 static wxString
FromAscii(const char ascii
); // char
1210 const wxCharBuffer
ToAscii() const;
1212 static wxString
FromAscii(const char *ascii
) { return wxString( ascii
); }
1213 static wxString
FromAscii(const char ascii
) { return wxString( ascii
); }
1214 const char *ToAscii() const { return c_str(); }
1215 #endif // Unicode/!Unicode
1217 // conversion to/from UTF-8:
1218 #if wxUSE_UNICODE_UTF8
1219 static wxString
FromUTF8(const char *utf8
)
1222 return wxEmptyString
;
1224 wxASSERT( wxStringOperations::IsValidUtf8String(utf8
) );
1225 return FromImpl(wxStringImpl(utf8
));
1227 static wxString
FromUTF8(const char *utf8
, size_t len
)
1230 return wxEmptyString
;
1232 return FromUTF8(utf8
);
1234 wxASSERT( wxStringOperations::IsValidUtf8String(utf8
, len
) );
1235 return FromImpl(wxStringImpl(utf8
, len
));
1237 const char* utf8_str() const { return wx_str(); }
1238 const char* ToUTF8() const { return wx_str(); }
1239 #elif wxUSE_UNICODE_WCHAR
1240 static wxString
FromUTF8(const char *utf8
)
1241 { return wxString(utf8
, wxMBConvUTF8()); }
1242 static wxString
FromUTF8(const char *utf8
, size_t len
)
1243 { return wxString(utf8
, wxMBConvUTF8(), len
); }
1244 const wxCharBuffer
utf8_str() const { return mb_str(wxMBConvUTF8()); }
1245 const wxCharBuffer
ToUTF8() const { return utf8_str(); }
1247 static wxString
FromUTF8(const char *utf8
)
1248 { return wxString(wxMBConvUTF8().cMB2WC(utf8
)); }
1249 static wxString
FromUTF8(const char *utf8
, size_t len
)
1252 wxWCharBuffer
buf(wxMBConvUTF8().cMB2WC(utf8
, len
== npos
? wxNO_LEN
: len
, &wlen
));
1253 return wxString(buf
.data(), wlen
);
1255 const wxCharBuffer
utf8_str() const
1256 { return wxMBConvUTF8().cWC2MB(wc_str()); }
1257 const wxCharBuffer
ToUTF8() const { return utf8_str(); }
1260 // functions for storing binary data in wxString:
1262 static wxString
From8BitData(const char *data
, size_t len
)
1263 { return wxString(data
, wxConvISO8859_1
, len
); }
1264 // version for NUL-terminated data:
1265 static wxString
From8BitData(const char *data
)
1266 { return wxString(data
, wxConvISO8859_1
); }
1267 const wxCharBuffer
To8BitData() const { return mb_str(wxConvISO8859_1
); }
1269 static wxString
From8BitData(const char *data
, size_t len
)
1270 { return wxString(data
, len
); }
1271 // version for NUL-terminated data:
1272 static wxString
From8BitData(const char *data
)
1273 { return wxString(data
); }
1274 const char *To8BitData() const { return c_str(); }
1275 #endif // Unicode/ANSI
1277 // conversions with (possible) format conversions: have to return a
1278 // buffer with temporary data
1280 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
1281 // return an ANSI (multibyte) string, wc_str() to return a wide string and
1282 // fn_str() to return a string which should be used with the OS APIs
1283 // accepting the file names. The return value is always the same, but the
1284 // type differs because a function may either return pointer to the buffer
1285 // directly or have to use intermediate buffer for translation.
1288 #if wxUSE_UTF8_LOCALE_ONLY
1289 const char* mb_str() const { return wx_str(); }
1290 const wxCharBuffer
mb_str(const wxMBConv
& conv
) const;
1292 const wxCharBuffer
mb_str(const wxMBConv
& conv
= wxConvLibc
) const;
1295 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
1297 #if wxUSE_UNICODE_WCHAR
1298 const wxChar
* wc_str() const { return wx_str(); }
1299 #elif wxUSE_UNICODE_UTF8
1300 const wxWCharBuffer
wc_str() const;
1302 // for compatibility with !wxUSE_UNICODE version
1303 const wxWX2WCbuf
wc_str(const wxMBConv
& WXUNUSED(conv
)) const
1304 { return wc_str(); }
1307 const wxCharBuffer
fn_str() const { return mb_str(wxConvFile
); }
1309 const wxWX2WCbuf
fn_str() const { return wc_str(); }
1310 #endif // wxMBFILES/!wxMBFILES
1313 const wxChar
* mb_str() const { return wx_str(); }
1315 // for compatibility with wxUSE_UNICODE version
1316 const wxChar
* mb_str(const wxMBConv
& WXUNUSED(conv
)) const { return wx_str(); }
1318 const wxWX2MBbuf
mbc_str() const { return mb_str(); }
1321 const wxWCharBuffer
wc_str(const wxMBConv
& conv
= wxConvLibc
) const;
1322 #endif // wxUSE_WCHAR_T
1324 const wxCharBuffer
fn_str() const { return wxConvFile
.cWC2WX( wc_str( wxConvLocal
) ); }
1326 const wxChar
* fn_str() const { return c_str(); }
1328 #endif // Unicode/ANSI
1330 // overloaded assignment
1331 // from another wxString
1332 wxString
& operator=(const wxString
& stringSrc
)
1333 { m_impl
= stringSrc
.m_impl
; return *this; }
1334 wxString
& operator=(const wxCStrData
& cstr
)
1335 { return *this = cstr
.AsString(); }
1337 wxString
& operator=(wxUniChar ch
)
1338 { m_impl
= wxStringOperations::EncodeChar(ch
); return *this; }
1339 wxString
& operator=(wxUniCharRef ch
)
1340 { return operator=((wxUniChar
)ch
); }
1341 wxString
& operator=(char ch
)
1342 { return operator=(wxUniChar(ch
)); }
1343 wxString
& operator=(unsigned char ch
)
1344 { return operator=(wxUniChar(ch
)); }
1345 wxString
& operator=(wchar_t ch
)
1346 { return operator=(wxUniChar(ch
)); }
1347 // from a C string - STL probably will crash on NULL,
1348 // so we need to compensate in that case
1349 #if wxUSE_STL_BASED_WXSTRING
1350 wxString
& operator=(const char *psz
)
1351 { if (psz
) m_impl
= ImplStr(psz
); else Clear(); return *this; }
1352 wxString
& operator=(const wchar_t *pwz
)
1353 { if (pwz
) m_impl
= ImplStr(pwz
); else Clear(); return *this; }
1355 wxString
& operator=(const char *psz
)
1356 { m_impl
= ImplStr(psz
); return *this; }
1357 wxString
& operator=(const wchar_t *pwz
)
1358 { m_impl
= ImplStr(pwz
); return *this; }
1360 wxString
& operator=(const unsigned char *psz
)
1361 { return operator=((const char*)psz
); }
1363 // from wxWCharBuffer
1364 wxString
& operator=(const wxWCharBuffer
& s
)
1365 { return operator=(s
.data()); } // FIXME-UTF8: fix for embedded NULs
1366 // from wxCharBuffer
1367 wxString
& operator=(const wxCharBuffer
& s
)
1368 { return operator=(s
.data()); } // FIXME-UTF8: fix for embedded NULs
1370 // string concatenation
1371 // in place concatenation
1373 Concatenate and return the result. Note that the left to right
1374 associativity of << allows to write things like "str << str1 << str2
1375 << ..." (unlike with +=)
1378 wxString
& operator<<(const wxString
& s
)
1380 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1381 wxASSERT_MSG( s
.IsValid(),
1382 _T("did you forget to call UngetWriteBuf()?") );
1388 // string += C string
1389 wxString
& operator<<(const char *psz
)
1390 { append(psz
); return *this; }
1391 wxString
& operator<<(const wchar_t *pwz
)
1392 { append(pwz
); return *this; }
1393 wxString
& operator<<(const wxCStrData
& psz
)
1394 { append(psz
.AsString()); return *this; }
1396 wxString
& operator<<(wxUniChar ch
) { append(1, ch
); return *this; }
1397 wxString
& operator<<(wxUniCharRef ch
) { append(1, ch
); return *this; }
1398 wxString
& operator<<(char ch
) { append(1, ch
); return *this; }
1399 wxString
& operator<<(unsigned char ch
) { append(1, ch
); return *this; }
1400 wxString
& operator<<(wchar_t ch
) { append(1, ch
); return *this; }
1402 // string += buffer (i.e. from wxGetString)
1403 wxString
& operator<<(const wxWCharBuffer
& s
)
1404 { return operator<<((const wchar_t *)s
); }
1405 wxString
& operator<<(const wxCharBuffer
& s
)
1406 { return operator<<((const char *)s
); }
1408 // string += C string
1409 wxString
& Append(const wxString
& s
)
1411 // test for empty() to share the string if possible
1418 wxString
& Append(const char* psz
)
1419 { append(psz
); return *this; }
1420 wxString
& Append(const wchar_t* pwz
)
1421 { append(pwz
); return *this; }
1422 wxString
& Append(const wxCStrData
& psz
)
1423 { append(psz
); return *this; }
1424 wxString
& Append(const wxCharBuffer
& psz
)
1425 { append(psz
); return *this; }
1426 wxString
& Append(const wxWCharBuffer
& psz
)
1427 { append(psz
); return *this; }
1428 // append count copies of given character
1429 wxString
& Append(wxUniChar ch
, size_t count
= 1u)
1430 { append(count
, ch
); return *this; }
1431 wxString
& Append(wxUniCharRef ch
, size_t count
= 1u)
1432 { append(count
, ch
); return *this; }
1433 wxString
& Append(char ch
, size_t count
= 1u)
1434 { append(count
, ch
); return *this; }
1435 wxString
& Append(unsigned char ch
, size_t count
= 1u)
1436 { append(count
, ch
); return *this; }
1437 wxString
& Append(wchar_t ch
, size_t count
= 1u)
1438 { append(count
, ch
); return *this; }
1439 wxString
& Append(const char* psz
, size_t nLen
)
1440 { append(psz
, nLen
); return *this; }
1441 wxString
& Append(const wchar_t* pwz
, size_t nLen
)
1442 { append(pwz
, nLen
); return *this; }
1444 // prepend a string, return the string itself
1445 wxString
& Prepend(const wxString
& str
)
1446 { *this = str
+ *this; return *this; }
1448 // non-destructive concatenation
1450 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
,
1451 const wxString
& string2
);
1452 // string with a single char
1453 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
1454 // char with a string
1455 friend wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
1456 // string with C string
1457 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
1459 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
1460 const wchar_t *pwz
);
1461 // C string with string
1462 friend wxString WXDLLIMPEXP_BASE
operator+(const char *psz
,
1463 const wxString
& string
);
1464 friend wxString WXDLLIMPEXP_BASE
operator+(const wchar_t *pwz
,
1465 const wxString
& string
);
1467 // stream-like functions
1468 // insert an int into string
1469 wxString
& operator<<(int i
)
1470 { return (*this) << Format(_T("%d"), i
); }
1471 // insert an unsigned int into string
1472 wxString
& operator<<(unsigned int ui
)
1473 { return (*this) << Format(_T("%u"), ui
); }
1474 // insert a long into string
1475 wxString
& operator<<(long l
)
1476 { return (*this) << Format(_T("%ld"), l
); }
1477 // insert an unsigned long into string
1478 wxString
& operator<<(unsigned long ul
)
1479 { return (*this) << Format(_T("%lu"), ul
); }
1480 #if defined wxLongLong_t && !defined wxLongLongIsLong
1481 // insert a long long if they exist and aren't longs
1482 wxString
& operator<<(wxLongLong_t ll
)
1484 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("d");
1485 return (*this) << Format(fmt
, ll
);
1487 // insert an unsigned long long
1488 wxString
& operator<<(wxULongLong_t ull
)
1490 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("u");
1491 return (*this) << Format(fmt
, ull
);
1494 // insert a float into string
1495 wxString
& operator<<(float f
)
1496 { return (*this) << Format(_T("%f"), f
); }
1497 // insert a double into string
1498 wxString
& operator<<(double d
)
1499 { return (*this) << Format(_T("%g"), d
); }
1501 // string comparison
1502 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
1503 int Cmp(const char *psz
) const
1504 { return compare(psz
); }
1505 int Cmp(const wchar_t *pwz
) const
1506 { return compare(pwz
); }
1507 int Cmp(const wxString
& s
) const
1508 { return compare(s
); }
1509 int Cmp(const wxCStrData
& s
) const
1510 { return compare(s
); }
1511 int Cmp(const wxCharBuffer
& s
) const
1512 { return compare(s
); }
1513 int Cmp(const wxWCharBuffer
& s
) const
1514 { return compare(s
); }
1515 // same as Cmp() but not case-sensitive
1516 int CmpNoCase(const wxString
& s
) const;
1517 // test for the string equality, either considering case or not
1518 // (if compareWithCase then the case matters)
1519 bool IsSameAs(const wxString
& str
, bool compareWithCase
= true) const
1520 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
1521 bool IsSameAs(const char *str
, bool compareWithCase
= true) const
1522 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
1523 bool IsSameAs(const wchar_t *str
, bool compareWithCase
= true) const
1524 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
1525 bool IsSameAs(const wxCStrData
& str
, bool compareWithCase
= true) const
1526 { return IsSameAs(str
.AsString(), compareWithCase
); }
1527 bool IsSameAs(const wxCharBuffer
& str
, bool compareWithCase
= true) const
1528 { return IsSameAs(str
.data(), compareWithCase
); }
1529 bool IsSameAs(const wxWCharBuffer
& str
, bool compareWithCase
= true) const
1530 { return IsSameAs(str
.data(), compareWithCase
); }
1531 // comparison with a single character: returns true if equal
1532 bool IsSameAs(wxUniChar c
, bool compareWithCase
= true) const;
1533 // FIXME-UTF8: remove these overloads
1534 bool IsSameAs(wxUniCharRef c
, bool compareWithCase
= true) const
1535 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1536 bool IsSameAs(char c
, bool compareWithCase
= true) const
1537 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1538 bool IsSameAs(unsigned char c
, bool compareWithCase
= true) const
1539 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1540 bool IsSameAs(wchar_t c
, bool compareWithCase
= true) const
1541 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1542 bool IsSameAs(int c
, bool compareWithCase
= true) const
1543 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1545 // simple sub-string extraction
1546 // return substring starting at nFirst of length nCount (or till the end
1547 // if nCount = default value)
1548 wxString
Mid(size_t nFirst
, size_t nCount
= npos
) const;
1550 // operator version of Mid()
1551 wxString
operator()(size_t start
, size_t len
) const
1552 { return Mid(start
, len
); }
1554 // check if the string starts with the given prefix and return the rest
1555 // of the string in the provided pointer if it is not NULL; otherwise
1557 bool StartsWith(const wxString
& prefix
, wxString
*rest
= NULL
) const;
1558 // check if the string ends with the given suffix and return the
1559 // beginning of the string before the suffix in the provided pointer if
1560 // it is not NULL; otherwise return false
1561 bool EndsWith(const wxString
& suffix
, wxString
*rest
= NULL
) const;
1563 // get first nCount characters
1564 wxString
Left(size_t nCount
) const;
1565 // get last nCount characters
1566 wxString
Right(size_t nCount
) const;
1567 // get all characters before the first occurance of ch
1568 // (returns the whole string if ch not found)
1569 wxString
BeforeFirst(wxUniChar ch
) const;
1570 // get all characters before the last occurence of ch
1571 // (returns empty string if ch not found)
1572 wxString
BeforeLast(wxUniChar ch
) const;
1573 // get all characters after the first occurence of ch
1574 // (returns empty string if ch not found)
1575 wxString
AfterFirst(wxUniChar ch
) const;
1576 // get all characters after the last occurence of ch
1577 // (returns the whole string if ch not found)
1578 wxString
AfterLast(wxUniChar ch
) const;
1580 // for compatibility only, use more explicitly named functions above
1581 wxString
Before(wxUniChar ch
) const { return BeforeLast(ch
); }
1582 wxString
After(wxUniChar ch
) const { return AfterFirst(ch
); }
1585 // convert to upper case in place, return the string itself
1586 wxString
& MakeUpper();
1587 // convert to upper case, return the copy of the string
1588 // Here's something to remember: BC++ doesn't like returns in inlines.
1589 wxString
Upper() const ;
1590 // convert to lower case in place, return the string itself
1591 wxString
& MakeLower();
1592 // convert to lower case, return the copy of the string
1593 wxString
Lower() const ;
1595 // trimming/padding whitespace (either side) and truncating
1596 // remove spaces from left or from right (default) side
1597 wxString
& Trim(bool bFromRight
= true);
1598 // add nCount copies chPad in the beginning or at the end (default)
1599 wxString
& Pad(size_t nCount
, wxUniChar chPad
= wxT(' '), bool bFromRight
= true);
1601 // searching and replacing
1602 // searching (return starting index, or -1 if not found)
1603 int Find(wxUniChar ch
, bool bFromEnd
= false) const; // like strchr/strrchr
1604 int Find(wxUniCharRef ch
, bool bFromEnd
= false) const
1605 { return Find(wxUniChar(ch
), bFromEnd
); }
1606 int Find(char ch
, bool bFromEnd
= false) const
1607 { return Find(wxUniChar(ch
), bFromEnd
); }
1608 int Find(unsigned char ch
, bool bFromEnd
= false) const
1609 { return Find(wxUniChar(ch
), bFromEnd
); }
1610 int Find(wchar_t ch
, bool bFromEnd
= false) const
1611 { return Find(wxUniChar(ch
), bFromEnd
); }
1612 // searching (return starting index, or -1 if not found)
1613 int Find(const wxString
& sub
) const // like strstr
1615 size_type idx
= find(sub
);
1616 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
1618 int Find(const char *sub
) const // like strstr
1620 size_type idx
= find(sub
);
1621 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
1623 int Find(const wchar_t *sub
) const // like strstr
1625 size_type idx
= find(sub
);
1626 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
1629 int Find(const wxCStrData
& sub
) const
1630 { return Find(sub
.AsString()); }
1631 int Find(const wxCharBuffer
& sub
) const
1632 { return Find(sub
.data()); }
1633 int Find(const wxWCharBuffer
& sub
) const
1634 { return Find(sub
.data()); }
1636 // replace first (or all of bReplaceAll) occurences of substring with
1637 // another string, returns the number of replacements made
1638 size_t Replace(const wxString
& strOld
,
1639 const wxString
& strNew
,
1640 bool bReplaceAll
= true);
1642 // check if the string contents matches a mask containing '*' and '?'
1643 bool Matches(const wxString
& mask
) const;
1645 // conversion to numbers: all functions return true only if the whole
1646 // string is a number and put the value of this number into the pointer
1647 // provided, the base is the numeric base in which the conversion should be
1648 // done and must be comprised between 2 and 36 or be 0 in which case the
1649 // standard C rules apply (leading '0' => octal, "0x" => hex)
1650 // convert to a signed integer
1651 bool ToLong(long *val
, int base
= 10) const;
1652 // convert to an unsigned integer
1653 bool ToULong(unsigned long *val
, int base
= 10) const;
1654 // convert to wxLongLong
1655 #if defined(wxLongLong_t)
1656 bool ToLongLong(wxLongLong_t
*val
, int base
= 10) const;
1657 // convert to wxULongLong
1658 bool ToULongLong(wxULongLong_t
*val
, int base
= 10) const;
1659 #endif // wxLongLong_t
1660 // convert to a double
1661 bool ToDouble(double *val
) const;
1664 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1665 // formatted input/output
1666 // as sprintf(), returns the number of characters written or < 0 on error
1667 // (take 'this' into account in attribute parameter count)
1668 // int Printf(const wxString& format, ...);
1669 WX_DEFINE_VARARG_FUNC(int, Printf
, 1, (const wxFormatString
&),
1670 DoPrintfWchar
, DoPrintfUtf8
)
1672 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1673 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wxString
&),
1674 (wxFormatString(f1
)));
1675 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wxCStrData
&),
1676 (wxFormatString(f1
)));
1677 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const char*),
1678 (wxFormatString(f1
)));
1679 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wchar_t*),
1680 (wxFormatString(f1
)));
1682 #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN
1683 // as vprintf(), returns the number of characters written or < 0 on error
1684 int PrintfV(const wxString
& format
, va_list argptr
);
1686 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1687 // returns the string containing the result of Printf() to it
1688 // static wxString Format(const wxString& format, ...) ATTRIBUTE_PRINTF_1;
1689 WX_DEFINE_VARARG_FUNC(static wxString
, Format
, 1, (const wxFormatString
&),
1690 DoFormatWchar
, DoFormatUtf8
)
1692 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1693 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wxString
&),
1694 (wxFormatString(f1
)));
1695 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wxCStrData
&),
1696 (wxFormatString(f1
)));
1697 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const char*),
1698 (wxFormatString(f1
)));
1699 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wchar_t*),
1700 (wxFormatString(f1
)));
1703 // the same as above, but takes a va_list
1704 static wxString
FormatV(const wxString
& format
, va_list argptr
);
1706 // raw access to string memory
1707 // ensure that string has space for at least nLen characters
1708 // only works if the data of this string is not shared
1709 bool Alloc(size_t nLen
) { reserve(nLen
); /*return capacity() >= nLen;*/ return true; }
1710 // minimize the string's memory
1711 // only works if the data of this string is not shared
1713 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1714 // These are deprecated, use wxStringBuffer or wxStringBufferLength instead
1716 // get writable buffer of at least nLen bytes. Unget() *must* be called
1717 // a.s.a.p. to put string back in a reasonable state!
1718 wxDEPRECATED( wxStringCharType
*GetWriteBuf(size_t nLen
) );
1719 // call this immediately after GetWriteBuf() has been used
1720 wxDEPRECATED( void UngetWriteBuf() );
1721 wxDEPRECATED( void UngetWriteBuf(size_t nLen
) );
1722 #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && wxUSE_UNICODE_UTF8
1724 // wxWidgets version 1 compatibility functions
1727 wxString
SubString(size_t from
, size_t to
) const
1728 { return Mid(from
, (to
- from
+ 1)); }
1729 // values for second parameter of CompareTo function
1730 enum caseCompare
{exact
, ignoreCase
};
1731 // values for first parameter of Strip function
1732 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
1734 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1736 // (take 'this' into account in attribute parameter count)
1737 // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2;
1738 WX_DEFINE_VARARG_FUNC(int, sprintf
, 1, (const wxFormatString
&),
1739 DoPrintfWchar
, DoPrintfUtf8
)
1741 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1742 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wxString
&),
1743 (wxFormatString(f1
)));
1744 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wxCStrData
&),
1745 (wxFormatString(f1
)));
1746 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const char*),
1747 (wxFormatString(f1
)));
1748 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wchar_t*),
1749 (wxFormatString(f1
)));
1751 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
1754 inline int CompareTo(const wxChar
* psz
, caseCompare cmp
= exact
) const
1755 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
1758 size_t Length() const { return length(); }
1759 // Count the number of characters
1760 int Freq(wxUniChar ch
) const;
1762 void LowerCase() { MakeLower(); }
1764 void UpperCase() { MakeUpper(); }
1765 // use Trim except that it doesn't change this string
1766 wxString
Strip(stripType w
= trailing
) const;
1768 // use Find (more general variants not yet supported)
1769 size_t Index(const wxChar
* psz
) const { return Find(psz
); }
1770 size_t Index(wxUniChar ch
) const { return Find(ch
); }
1772 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
1773 wxString
& RemoveLast(size_t n
= 1) { return Truncate(length() - n
); }
1775 wxString
& Remove(size_t nStart
, size_t nLen
)
1776 { return (wxString
&)erase( nStart
, nLen
); }
1779 int First( wxUniChar ch
) const { return Find(ch
); }
1780 int First( wxUniCharRef ch
) const { return Find(ch
); }
1781 int First( char ch
) const { return Find(ch
); }
1782 int First( unsigned char ch
) const { return Find(ch
); }
1783 int First( wchar_t ch
) const { return Find(ch
); }
1784 int First( const wxString
& str
) const { return Find(str
); }
1785 int Last( wxUniChar ch
) const { return Find(ch
, true); }
1786 bool Contains(const wxString
& str
) const { return Find(str
) != wxNOT_FOUND
; }
1789 bool IsNull() const { return empty(); }
1791 // std::string compatibility functions
1793 // take nLen chars starting at nPos
1794 wxString(const wxString
& str
, size_t nPos
, size_t nLen
)
1795 { assign(str
, nPos
, nLen
); }
1796 // take all characters from first to last
1797 wxString(const_iterator first
, const_iterator last
)
1798 : m_impl(first
.impl(), last
.impl()) { }
1799 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1800 // the 2 overloads below are for compatibility with the existing code using
1801 // pointers instead of iterators
1802 wxString(const char *first
, const char *last
)
1804 SubstrBufFromMB
str(ImplStr(first
, last
- first
));
1805 m_impl
.assign(str
.data
, str
.len
);
1807 wxString(const wchar_t *first
, const wchar_t *last
)
1809 SubstrBufFromWC
str(ImplStr(first
, last
- first
));
1810 m_impl
.assign(str
.data
, str
.len
);
1812 // and this one is needed to compile code adding offsets to c_str() result
1813 wxString(const wxCStrData
& first
, const wxCStrData
& last
)
1814 : m_impl(CreateConstIterator(first
).impl(),
1815 CreateConstIterator(last
).impl())
1817 wxASSERT_MSG( first
.m_str
== last
.m_str
,
1818 _T("pointers must be into the same string") );
1820 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1822 // lib.string.modifiers
1823 // append elements str[pos], ..., str[pos+n]
1824 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
1827 str
.PosLenToImpl(pos
, n
, &from
, &len
);
1828 m_impl
.append(str
.m_impl
, from
, len
);
1832 wxString
& append(const wxString
& str
)
1833 { m_impl
.append(str
.m_impl
); return *this; }
1834 // append first n (or all if n == npos) characters of sz
1835 wxString
& append(const char *sz
)
1836 { m_impl
.append(ImplStr(sz
)); return *this; }
1837 wxString
& append(const wchar_t *sz
)
1838 { m_impl
.append(ImplStr(sz
)); return *this; }
1839 wxString
& append(const char *sz
, size_t n
)
1841 SubstrBufFromMB
str(ImplStr(sz
, n
));
1842 m_impl
.append(str
.data
, str
.len
);
1845 wxString
& append(const wchar_t *sz
, size_t n
)
1847 SubstrBufFromWC
str(ImplStr(sz
, n
));
1848 m_impl
.append(str
.data
, str
.len
);
1852 wxString
& append(const wxCStrData
& str
)
1853 { return append(str
.AsString()); }
1854 wxString
& append(const wxCharBuffer
& str
)
1855 { return append(str
.data()); }
1856 wxString
& append(const wxWCharBuffer
& str
)
1857 { return append(str
.data()); }
1859 // append n copies of ch
1860 wxString
& append(size_t n
, wxUniChar ch
)
1862 #if wxUSE_UNICODE_UTF8
1863 if ( !ch
.IsAscii() )
1864 m_impl
.append(wxStringOperations::EncodeNChars(n
, ch
));
1867 m_impl
.append(n
, (wxStringCharType
)ch
);
1870 // append from first to last
1871 wxString
& append(const_iterator first
, const_iterator last
)
1872 { m_impl
.append(first
.impl(), last
.impl()); return *this; }
1873 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1874 wxString
& append(const char *first
, const char *last
)
1875 { return append(first
, last
- first
); }
1876 wxString
& append(const wchar_t *first
, const wchar_t *last
)
1877 { return append(first
, last
- first
); }
1878 wxString
& append(const wxCStrData
& first
, const wxCStrData
& last
)
1879 { return append(CreateConstIterator(first
), CreateConstIterator(last
)); }
1880 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1882 // same as `this_string = str'
1883 wxString
& assign(const wxString
& str
)
1884 { m_impl
= str
.m_impl
; return *this; }
1885 wxString
& assign(const wxString
& str
, size_t len
)
1887 m_impl
.assign(str
.m_impl
, 0, str
.LenToImpl(len
));
1890 // same as ` = str[pos..pos + n]
1891 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
1894 str
.PosLenToImpl(pos
, n
, &from
, &len
);
1895 m_impl
.assign(str
.m_impl
, from
, len
);
1898 // same as `= first n (or all if n == npos) characters of sz'
1899 wxString
& assign(const char *sz
)
1900 { m_impl
.assign(ImplStr(sz
)); return *this; }
1901 wxString
& assign(const wchar_t *sz
)
1902 { m_impl
.assign(ImplStr(sz
)); return *this; }
1903 wxString
& assign(const char *sz
, size_t n
)
1905 SubstrBufFromMB
str(ImplStr(sz
, n
));
1906 m_impl
.assign(str
.data
, str
.len
);
1909 wxString
& assign(const wchar_t *sz
, size_t n
)
1911 SubstrBufFromWC
str(ImplStr(sz
, n
));
1912 m_impl
.assign(str
.data
, str
.len
);
1916 wxString
& assign(const wxCStrData
& str
)
1917 { return assign(str
.AsString()); }
1918 wxString
& assign(const wxCharBuffer
& str
)
1919 { return assign(str
.data()); }
1920 wxString
& assign(const wxWCharBuffer
& str
)
1921 { return assign(str
.data()); }
1922 wxString
& assign(const wxCStrData
& str
, size_t len
)
1923 { return assign(str
.AsString(), len
); }
1924 wxString
& assign(const wxCharBuffer
& str
, size_t len
)
1925 { return assign(str
.data(), len
); }
1926 wxString
& assign(const wxWCharBuffer
& str
, size_t len
)
1927 { return assign(str
.data(), len
); }
1929 // same as `= n copies of ch'
1930 wxString
& assign(size_t n
, wxUniChar ch
)
1932 #if wxUSE_UNICODE_UTF8
1933 if ( !ch
.IsAscii() )
1934 m_impl
.assign(wxStringOperations::EncodeNChars(n
, ch
));
1937 m_impl
.assign(n
, (wxStringCharType
)ch
);
1941 wxString
& assign(size_t n
, wxUniCharRef ch
)
1942 { return assign(n
, wxUniChar(ch
)); }
1943 wxString
& assign(size_t n
, char ch
)
1944 { return assign(n
, wxUniChar(ch
)); }
1945 wxString
& assign(size_t n
, unsigned char ch
)
1946 { return assign(n
, wxUniChar(ch
)); }
1947 wxString
& assign(size_t n
, wchar_t ch
)
1948 { return assign(n
, wxUniChar(ch
)); }
1950 // assign from first to last
1951 wxString
& assign(const_iterator first
, const_iterator last
)
1952 { m_impl
.assign(first
.impl(), last
.impl()); return *this; }
1953 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1954 wxString
& assign(const char *first
, const char *last
)
1955 { return assign(first
, last
- first
); }
1956 wxString
& assign(const wchar_t *first
, const wchar_t *last
)
1957 { return assign(first
, last
- first
); }
1958 wxString
& assign(const wxCStrData
& first
, const wxCStrData
& last
)
1959 { return assign(CreateConstIterator(first
), CreateConstIterator(last
)); }
1960 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1962 // string comparison
1963 int compare(const wxString
& str
) const;
1964 int compare(const char* sz
) const;
1965 int compare(const wchar_t* sz
) const;
1966 int compare(const wxCStrData
& str
) const
1967 { return compare(str
.AsString()); }
1968 int compare(const wxCharBuffer
& str
) const
1969 { return compare(str
.data()); }
1970 int compare(const wxWCharBuffer
& str
) const
1971 { return compare(str
.data()); }
1972 // comparison with a substring
1973 int compare(size_t nStart
, size_t nLen
, const wxString
& str
) const;
1974 // comparison of 2 substrings
1975 int compare(size_t nStart
, size_t nLen
,
1976 const wxString
& str
, size_t nStart2
, size_t nLen2
) const;
1977 // substring comparison with first nCount characters of sz
1978 int compare(size_t nStart
, size_t nLen
,
1979 const char* sz
, size_t nCount
= npos
) const;
1980 int compare(size_t nStart
, size_t nLen
,
1981 const wchar_t* sz
, size_t nCount
= npos
) const;
1983 // insert another string
1984 wxString
& insert(size_t nPos
, const wxString
& str
)
1985 { insert(begin() + nPos
, str
.begin(), str
.end()); return *this; }
1986 // insert n chars of str starting at nStart (in str)
1987 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
1990 str
.PosLenToImpl(nStart
, n
, &from
, &len
);
1991 m_impl
.insert(PosToImpl(nPos
), str
.m_impl
, from
, len
);
1994 // insert first n (or all if n == npos) characters of sz
1995 wxString
& insert(size_t nPos
, const char *sz
)
1996 { m_impl
.insert(PosToImpl(nPos
), ImplStr(sz
)); return *this; }
1997 wxString
& insert(size_t nPos
, const wchar_t *sz
)
1998 { m_impl
.insert(PosToImpl(nPos
), ImplStr(sz
)); return *this; }
1999 wxString
& insert(size_t nPos
, const char *sz
, size_t n
)
2001 SubstrBufFromMB
str(ImplStr(sz
, n
));
2002 m_impl
.insert(PosToImpl(nPos
), str
.data
, str
.len
);
2005 wxString
& insert(size_t nPos
, const wchar_t *sz
, size_t n
)
2007 SubstrBufFromWC
str(ImplStr(sz
, n
));
2008 m_impl
.insert(PosToImpl(nPos
), str
.data
, str
.len
);
2011 // insert n copies of ch
2012 wxString
& insert(size_t nPos
, size_t n
, wxUniChar ch
)
2014 #if wxUSE_UNICODE_UTF8
2015 if ( !ch
.IsAscii() )
2016 m_impl
.insert(PosToImpl(nPos
), wxStringOperations::EncodeNChars(n
, ch
));
2019 m_impl
.insert(PosToImpl(nPos
), n
, (wxStringCharType
)ch
);
2022 iterator
insert(iterator it
, wxUniChar ch
)
2024 #if wxUSE_UNICODE_UTF8
2025 if ( !ch
.IsAscii() )
2027 size_t pos
= IterToImplPos(it
);
2028 m_impl
.insert(pos
, wxStringOperations::EncodeChar(ch
));
2029 return iterator(this, m_impl
.begin() + pos
);
2033 return iterator(this, m_impl
.insert(it
.impl(), (wxStringCharType
)ch
));
2035 void insert(iterator it
, const_iterator first
, const_iterator last
)
2036 { m_impl
.insert(it
.impl(), first
.impl(), last
.impl()); }
2037 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2038 void insert(iterator it
, const char *first
, const char *last
)
2039 { insert(it
- begin(), first
, last
- first
); }
2040 void insert(iterator it
, const wchar_t *first
, const wchar_t *last
)
2041 { insert(it
- begin(), first
, last
- first
); }
2042 void insert(iterator it
, const wxCStrData
& first
, const wxCStrData
& last
)
2043 { insert(it
, CreateConstIterator(first
), CreateConstIterator(last
)); }
2044 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2046 void insert(iterator it
, size_type n
, wxUniChar ch
)
2048 #if wxUSE_UNICODE_UTF8
2049 if ( !ch
.IsAscii() )
2050 m_impl
.insert(IterToImplPos(it
), wxStringOperations::EncodeNChars(n
, ch
));
2053 m_impl
.insert(it
.impl(), n
, (wxStringCharType
)ch
);
2056 // delete characters from nStart to nStart + nLen
2057 wxString
& erase(size_type pos
= 0, size_type n
= npos
)
2060 PosLenToImpl(pos
, n
, &from
, &len
);
2061 m_impl
.erase(from
, len
);
2064 // delete characters from first up to last
2065 iterator
erase(iterator first
, iterator last
)
2066 { return iterator(this, m_impl
.erase(first
.impl(), last
.impl())); }
2067 iterator
erase(iterator first
)
2068 { return iterator(this, m_impl
.erase(first
.impl())); }
2070 #ifdef wxSTRING_BASE_HASNT_CLEAR
2071 void clear() { erase(); }
2073 void clear() { m_impl
.clear(); }
2076 // replaces the substring of length nLen starting at nStart
2077 wxString
& replace(size_t nStart
, size_t nLen
, const char* sz
)
2080 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2081 m_impl
.replace(from
, len
, ImplStr(sz
));
2084 wxString
& replace(size_t nStart
, size_t nLen
, const wchar_t* sz
)
2087 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2088 m_impl
.replace(from
, len
, ImplStr(sz
));
2091 // replaces the substring of length nLen starting at nStart
2092 wxString
& replace(size_t nStart
, size_t nLen
, const wxString
& str
)
2095 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2096 m_impl
.replace(from
, len
, str
.m_impl
);
2099 // replaces the substring with nCount copies of ch
2100 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxUniChar ch
)
2103 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2104 #if wxUSE_UNICODE_UTF8
2105 if ( !ch
.IsAscii() )
2106 m_impl
.replace(from
, len
, wxStringOperations::EncodeNChars(nCount
, ch
));
2109 m_impl
.replace(from
, len
, nCount
, (wxStringCharType
)ch
);
2112 // replaces a substring with another substring
2113 wxString
& replace(size_t nStart
, size_t nLen
,
2114 const wxString
& str
, size_t nStart2
, size_t nLen2
)
2117 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2120 str
.PosLenToImpl(nStart2
, nLen2
, &from2
, &len2
);
2122 m_impl
.replace(from
, len
, str
.m_impl
, from2
, len2
);
2125 // replaces the substring with first nCount chars of sz
2126 wxString
& replace(size_t nStart
, size_t nLen
,
2127 const char* sz
, size_t nCount
)
2130 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2132 SubstrBufFromMB
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 wchar_t* sz
, size_t nCount
)
2141 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2143 SubstrBufFromWC
str(ImplStr(sz
, nCount
));
2145 m_impl
.replace(from
, len
, str
.data
, str
.len
);
2148 wxString
& replace(size_t nStart
, size_t nLen
,
2149 const wxString
& s
, size_t nCount
)
2152 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2153 m_impl
.replace(from
, len
, s
.m_impl
.c_str(), s
.LenToImpl(nCount
));
2157 wxString
& replace(iterator first
, iterator last
, const char* s
)
2158 { m_impl
.replace(first
.impl(), last
.impl(), ImplStr(s
)); return *this; }
2159 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
)
2160 { m_impl
.replace(first
.impl(), last
.impl(), ImplStr(s
)); return *this; }
2161 wxString
& replace(iterator first
, iterator last
, const char* s
, size_type n
)
2163 SubstrBufFromMB
str(ImplStr(s
, n
));
2164 m_impl
.replace(first
.impl(), last
.impl(), str
.data
, str
.len
);
2167 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
, size_type n
)
2169 SubstrBufFromWC
str(ImplStr(s
, n
));
2170 m_impl
.replace(first
.impl(), last
.impl(), str
.data
, str
.len
);
2173 wxString
& replace(iterator first
, iterator last
, const wxString
& s
)
2174 { m_impl
.replace(first
.impl(), last
.impl(), s
.m_impl
); return *this; }
2175 wxString
& replace(iterator first
, iterator last
, size_type n
, wxUniChar ch
)
2177 #if wxUSE_UNICODE_UTF8
2178 if ( !ch
.IsAscii() )
2179 m_impl
.replace(first
.impl(), last
.impl(),
2180 wxStringOperations::EncodeNChars(n
, ch
));
2183 m_impl
.replace(first
.impl(), last
.impl(), n
, (wxStringCharType
)ch
);
2186 wxString
& replace(iterator first
, iterator last
,
2187 const_iterator first1
, const_iterator last1
)
2189 m_impl
.replace(first
.impl(), last
.impl(), first1
.impl(), last1
.impl());
2192 wxString
& replace(iterator first
, iterator last
,
2193 const char *first1
, const char *last1
)
2194 { replace(first
, last
, first1
, last1
- first1
); return *this; }
2195 wxString
& replace(iterator first
, iterator last
,
2196 const wchar_t *first1
, const wchar_t *last1
)
2197 { replace(first
, last
, first1
, last1
- first1
); return *this; }
2200 void swap(wxString
& str
)
2201 { m_impl
.swap(str
.m_impl
); }
2204 size_t find(const wxString
& str
, size_t nStart
= 0) const
2205 { return PosFromImpl(m_impl
.find(str
.m_impl
, PosToImpl(nStart
))); }
2207 // find first n characters of sz
2208 size_t find(const char* sz
, size_t nStart
= 0, size_t n
= npos
) const
2210 SubstrBufFromMB
str(ImplStr(sz
, n
));
2211 return PosFromImpl(m_impl
.find(str
.data
, PosToImpl(nStart
), str
.len
));
2213 size_t find(const wchar_t* sz
, size_t nStart
= 0, size_t n
= npos
) const
2215 SubstrBufFromWC
str(ImplStr(sz
, n
));
2216 return PosFromImpl(m_impl
.find(str
.data
, PosToImpl(nStart
), str
.len
));
2218 size_t find(const wxCharBuffer
& s
, size_t nStart
= 0, size_t n
= npos
) const
2219 { return find(s
.data(), nStart
, n
); }
2220 size_t find(const wxWCharBuffer
& s
, size_t nStart
= 0, size_t n
= npos
) const
2221 { return find(s
.data(), nStart
, n
); }
2222 size_t find(const wxCStrData
& s
, size_t nStart
= 0, size_t n
= npos
) const
2223 { return find(s
.AsWChar(), nStart
, n
); }
2225 // find the first occurence of character ch after nStart
2226 size_t find(wxUniChar ch
, size_t nStart
= 0) const
2228 return PosFromImpl(m_impl
.find(wxStringOperations::EncodeChar(ch
),
2229 PosToImpl(nStart
)));
2231 size_t find(wxUniCharRef ch
, size_t nStart
= 0) const
2232 { return find(wxUniChar(ch
), nStart
); }
2233 size_t find(char ch
, size_t nStart
= 0) const
2234 { return find(wxUniChar(ch
), nStart
); }
2235 size_t find(unsigned char ch
, size_t nStart
= 0) const
2236 { return find(wxUniChar(ch
), nStart
); }
2237 size_t find(wchar_t ch
, size_t nStart
= 0) const
2238 { return find(wxUniChar(ch
), nStart
); }
2240 // rfind() family is exactly like find() but works right to left
2242 // as find, but from the end
2243 size_t rfind(const wxString
& str
, size_t nStart
= npos
) const
2244 { return PosFromImpl(m_impl
.rfind(str
.m_impl
, PosToImpl(nStart
))); }
2246 // as find, but from the end
2247 size_t rfind(const char* sz
, size_t nStart
= npos
, size_t n
= npos
) const
2249 SubstrBufFromMB
str(ImplStr(sz
, n
));
2250 return PosFromImpl(m_impl
.rfind(str
.data
, PosToImpl(nStart
), str
.len
));
2252 size_t rfind(const wchar_t* sz
, size_t nStart
= npos
, size_t n
= npos
) const
2254 SubstrBufFromWC
str(ImplStr(sz
, n
));
2255 return PosFromImpl(m_impl
.rfind(str
.data
, PosToImpl(nStart
), str
.len
));
2257 size_t rfind(const wxCharBuffer
& s
, size_t nStart
= npos
, size_t n
= npos
) const
2258 { return rfind(s
.data(), nStart
, n
); }
2259 size_t rfind(const wxWCharBuffer
& s
, size_t nStart
= npos
, size_t n
= npos
) const
2260 { return rfind(s
.data(), nStart
, n
); }
2261 size_t rfind(const wxCStrData
& s
, size_t nStart
= npos
, size_t n
= npos
) const
2262 { return rfind(s
.AsWChar(), nStart
, n
); }
2263 // as find, but from the end
2264 size_t rfind(wxUniChar ch
, size_t nStart
= npos
) const
2266 return PosFromImpl(m_impl
.rfind(wxStringOperations::EncodeChar(ch
),
2267 PosToImpl(nStart
)));
2269 size_t rfind(wxUniCharRef ch
, size_t nStart
= npos
) const
2270 { return rfind(wxUniChar(ch
), nStart
); }
2271 size_t rfind(char ch
, size_t nStart
= npos
) const
2272 { return rfind(wxUniChar(ch
), nStart
); }
2273 size_t rfind(unsigned char ch
, size_t nStart
= npos
) const
2274 { return rfind(wxUniChar(ch
), nStart
); }
2275 size_t rfind(wchar_t ch
, size_t nStart
= npos
) const
2276 { return rfind(wxUniChar(ch
), nStart
); }
2278 // find first/last occurence of any character (not) in the set:
2279 #if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2280 // FIXME-UTF8: this is not entirely correct, because it doesn't work if
2281 // sizeof(wchar_t)==2 and surrogates are present in the string;
2282 // should we care? Probably not.
2283 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
2284 { return m_impl
.find_first_of(str
.m_impl
, nStart
); }
2285 size_t find_first_of(const char* sz
, size_t nStart
= 0) const
2286 { return m_impl
.find_first_of(ImplStr(sz
), nStart
); }
2287 size_t find_first_of(const wchar_t* sz
, size_t nStart
= 0) const
2288 { return m_impl
.find_first_of(ImplStr(sz
), nStart
); }
2289 size_t find_first_of(const char* sz
, size_t nStart
, size_t n
) const
2290 { return m_impl
.find_first_of(ImplStr(sz
), nStart
, n
); }
2291 size_t find_first_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
2292 { return m_impl
.find_first_of(ImplStr(sz
), nStart
, n
); }
2293 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const
2294 { return m_impl
.find_first_of((wxChar
)c
, nStart
); }
2296 size_t find_last_of(const wxString
& str
, size_t nStart
= npos
) const
2297 { return m_impl
.find_last_of(str
.m_impl
, nStart
); }
2298 size_t find_last_of(const char* sz
, size_t nStart
= npos
) const
2299 { return m_impl
.find_last_of(ImplStr(sz
), nStart
); }
2300 size_t find_last_of(const wchar_t* sz
, size_t nStart
= npos
) const
2301 { return m_impl
.find_last_of(ImplStr(sz
), nStart
); }
2302 size_t find_last_of(const char* sz
, size_t nStart
, size_t n
) const
2303 { return m_impl
.find_last_of(ImplStr(sz
), nStart
, n
); }
2304 size_t find_last_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
2305 { return m_impl
.find_last_of(ImplStr(sz
), nStart
, n
); }
2306 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const
2307 { return m_impl
.find_last_of((wxChar
)c
, nStart
); }
2309 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
2310 { return m_impl
.find_first_not_of(str
.m_impl
, nStart
); }
2311 size_t find_first_not_of(const char* sz
, size_t nStart
= 0) const
2312 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
); }
2313 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
= 0) const
2314 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
); }
2315 size_t find_first_not_of(const char* sz
, size_t nStart
, size_t n
) const
2316 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
, n
); }
2317 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
2318 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
, n
); }
2319 size_t find_first_not_of(wxUniChar c
, size_t nStart
= 0) const
2320 { return m_impl
.find_first_not_of((wxChar
)c
, nStart
); }
2322 size_t find_last_not_of(const wxString
& str
, size_t nStart
= npos
) const
2323 { return m_impl
.find_last_not_of(str
.m_impl
, nStart
); }
2324 size_t find_last_not_of(const char* sz
, size_t nStart
= npos
) const
2325 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
); }
2326 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
= npos
) const
2327 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
); }
2328 size_t find_last_not_of(const char* sz
, size_t nStart
, size_t n
) const
2329 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
, n
); }
2330 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
2331 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
, n
); }
2332 size_t find_last_not_of(wxUniChar c
, size_t nStart
= npos
) const
2333 { return m_impl
.find_last_not_of((wxChar
)c
, nStart
); }
2335 // we can't use std::string implementation in UTF-8 build, because the
2336 // character sets would be interpreted wrongly:
2338 // as strpbrk() but starts at nStart, returns npos if not found
2339 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
2340 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2341 { return find_first_of(str
.wc_str(), nStart
); }
2343 { return find_first_of(str
.mb_str(), nStart
); }
2346 size_t find_first_of(const char* sz
, size_t nStart
= 0) const;
2347 size_t find_first_of(const wchar_t* sz
, size_t nStart
= 0) const;
2348 size_t find_first_of(const char* sz
, size_t nStart
, size_t n
) const;
2349 size_t find_first_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2350 // same as find(char, size_t)
2351 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const
2352 { return find(c
, nStart
); }
2353 // find the last (starting from nStart) char from str in this string
2354 size_t find_last_of (const wxString
& str
, size_t nStart
= npos
) const
2355 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2356 { return find_last_of(str
.wc_str(), nStart
); }
2358 { return find_last_of(str
.mb_str(), nStart
); }
2361 size_t find_last_of (const char* sz
, size_t nStart
= npos
) const;
2362 size_t find_last_of (const wchar_t* sz
, size_t nStart
= npos
) const;
2363 size_t find_last_of(const char* sz
, size_t nStart
, size_t n
) const;
2364 size_t find_last_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2366 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const
2367 { return rfind(c
, nStart
); }
2369 // find first/last occurence of any character not in the set
2371 // as strspn() (starting from nStart), returns npos on failure
2372 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
2373 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2374 { return find_first_not_of(str
.wc_str(), nStart
); }
2376 { return find_first_not_of(str
.mb_str(), nStart
); }
2379 size_t find_first_not_of(const char* sz
, size_t nStart
= 0) const;
2380 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
= 0) const;
2381 size_t find_first_not_of(const char* sz
, size_t nStart
, size_t n
) const;
2382 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2384 size_t find_first_not_of(wxUniChar ch
, size_t nStart
= 0) const;
2386 size_t find_last_not_of(const wxString
& str
, size_t nStart
= npos
) const
2387 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2388 { return find_last_not_of(str
.wc_str(), nStart
); }
2390 { return find_last_not_of(str
.mb_str(), nStart
); }
2393 size_t find_last_not_of(const char* sz
, size_t nStart
= npos
) const;
2394 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
= npos
) const;
2395 size_t find_last_not_of(const char* sz
, size_t nStart
, size_t n
) const;
2396 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2398 size_t find_last_not_of(wxUniChar ch
, size_t nStart
= npos
) const;
2399 #endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not
2401 // provide char/wchar_t/wxUniCharRef overloads for char-finding functions
2402 // above to resolve ambiguities:
2403 size_t find_first_of(wxUniCharRef ch
, size_t nStart
= 0) const
2404 { return find_first_of(wxUniChar(ch
), nStart
); }
2405 size_t find_first_of(char ch
, size_t nStart
= 0) const
2406 { return find_first_of(wxUniChar(ch
), nStart
); }
2407 size_t find_first_of(unsigned char ch
, size_t nStart
= 0) const
2408 { return find_first_of(wxUniChar(ch
), nStart
); }
2409 size_t find_first_of(wchar_t ch
, size_t nStart
= 0) const
2410 { return find_first_of(wxUniChar(ch
), nStart
); }
2411 size_t find_last_of(wxUniCharRef ch
, size_t nStart
= npos
) const
2412 { return find_last_of(wxUniChar(ch
), nStart
); }
2413 size_t find_last_of(char ch
, size_t nStart
= npos
) const
2414 { return find_last_of(wxUniChar(ch
), nStart
); }
2415 size_t find_last_of(unsigned char ch
, size_t nStart
= npos
) const
2416 { return find_last_of(wxUniChar(ch
), nStart
); }
2417 size_t find_last_of(wchar_t ch
, size_t nStart
= npos
) const
2418 { return find_last_of(wxUniChar(ch
), nStart
); }
2419 size_t find_first_not_of(wxUniCharRef ch
, size_t nStart
= 0) const
2420 { return find_first_not_of(wxUniChar(ch
), nStart
); }
2421 size_t find_first_not_of(char ch
, size_t nStart
= 0) const
2422 { return find_first_not_of(wxUniChar(ch
), nStart
); }
2423 size_t find_first_not_of(unsigned char ch
, size_t nStart
= 0) const
2424 { return find_first_not_of(wxUniChar(ch
), nStart
); }
2425 size_t find_first_not_of(wchar_t ch
, size_t nStart
= 0) const
2426 { return find_first_not_of(wxUniChar(ch
), nStart
); }
2427 size_t find_last_not_of(wxUniCharRef ch
, size_t nStart
= npos
) const
2428 { return find_last_not_of(wxUniChar(ch
), nStart
); }
2429 size_t find_last_not_of(char ch
, size_t nStart
= npos
) const
2430 { return find_last_not_of(wxUniChar(ch
), nStart
); }
2431 size_t find_last_not_of(unsigned char ch
, size_t nStart
= npos
) const
2432 { return find_last_not_of(wxUniChar(ch
), nStart
); }
2433 size_t find_last_not_of(wchar_t ch
, size_t nStart
= npos
) const
2434 { return find_last_not_of(wxUniChar(ch
), nStart
); }
2436 // and additional overloads for the versions taking strings:
2437 size_t find_first_of(const wxCStrData
& sz
, size_t nStart
= 0) const
2438 { return find_first_of(sz
.AsString(), nStart
); }
2439 size_t find_first_of(const wxCharBuffer
& sz
, size_t nStart
= 0) const
2440 { return find_first_of(sz
.data(), nStart
); }
2441 size_t find_first_of(const wxWCharBuffer
& sz
, size_t nStart
= 0) const
2442 { return find_first_of(sz
.data(), nStart
); }
2443 size_t find_first_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
2444 { return find_first_of(sz
.AsWChar(), nStart
, n
); }
2445 size_t find_first_of(const wxCharBuffer
& sz
, size_t nStart
, size_t n
) const
2446 { return find_first_of(sz
.data(), nStart
, n
); }
2447 size_t find_first_of(const wxWCharBuffer
& sz
, size_t nStart
, size_t n
) const
2448 { return find_first_of(sz
.data(), nStart
, n
); }
2450 size_t find_last_of(const wxCStrData
& sz
, size_t nStart
= 0) const
2451 { return find_last_of(sz
.AsString(), nStart
); }
2452 size_t find_last_of(const wxCharBuffer
& sz
, size_t nStart
= 0) const
2453 { return find_last_of(sz
.data(), nStart
); }
2454 size_t find_last_of(const wxWCharBuffer
& sz
, size_t nStart
= 0) const
2455 { return find_last_of(sz
.data(), nStart
); }
2456 size_t find_last_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
2457 { return find_last_of(sz
.AsWChar(), nStart
, n
); }
2458 size_t find_last_of(const wxCharBuffer
& sz
, size_t nStart
, size_t n
) const
2459 { return find_last_of(sz
.data(), nStart
, n
); }
2460 size_t find_last_of(const wxWCharBuffer
& sz
, size_t nStart
, size_t n
) const
2461 { return find_last_of(sz
.data(), nStart
, n
); }
2463 size_t find_first_not_of(const wxCStrData
& sz
, size_t nStart
= 0) const
2464 { return find_first_not_of(sz
.AsString(), nStart
); }
2465 size_t find_first_not_of(const wxCharBuffer
& sz
, size_t nStart
= 0) const
2466 { return find_first_not_of(sz
.data(), nStart
); }
2467 size_t find_first_not_of(const wxWCharBuffer
& sz
, size_t nStart
= 0) const
2468 { return find_first_not_of(sz
.data(), nStart
); }
2469 size_t find_first_not_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
2470 { return find_first_not_of(sz
.AsWChar(), nStart
, n
); }
2471 size_t find_first_not_of(const wxCharBuffer
& sz
, size_t nStart
, size_t n
) const
2472 { return find_first_not_of(sz
.data(), nStart
, n
); }
2473 size_t find_first_not_of(const wxWCharBuffer
& sz
, size_t nStart
, size_t n
) const
2474 { return find_first_not_of(sz
.data(), nStart
, n
); }
2476 size_t find_last_not_of(const wxCStrData
& sz
, size_t nStart
= 0) const
2477 { return find_last_not_of(sz
.AsString(), nStart
); }
2478 size_t find_last_not_of(const wxCharBuffer
& sz
, size_t nStart
= 0) const
2479 { return find_last_not_of(sz
.data(), nStart
); }
2480 size_t find_last_not_of(const wxWCharBuffer
& sz
, size_t nStart
= 0) const
2481 { return find_last_not_of(sz
.data(), nStart
); }
2482 size_t find_last_not_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
2483 { return find_last_not_of(sz
.AsWChar(), nStart
, n
); }
2484 size_t find_last_not_of(const wxCharBuffer
& sz
, size_t nStart
, size_t n
) const
2485 { return find_last_not_of(sz
.data(), nStart
, n
); }
2486 size_t find_last_not_of(const wxWCharBuffer
& sz
, size_t nStart
, size_t n
) const
2487 { return find_last_not_of(sz
.data(), nStart
, n
); }
2490 wxString
& operator+=(const wxString
& s
)
2491 { m_impl
+= s
.m_impl
; return *this; }
2492 // string += C string
2493 wxString
& operator+=(const char *psz
)
2494 { m_impl
+= ImplStr(psz
); return *this; }
2495 wxString
& operator+=(const wchar_t *pwz
)
2496 { m_impl
+= ImplStr(pwz
); return *this; }
2497 wxString
& operator+=(const wxCStrData
& s
)
2498 { m_impl
+= s
.AsString().m_impl
; return *this; }
2499 wxString
& operator+=(const wxCharBuffer
& s
)
2500 { return operator+=(s
.data()); }
2501 wxString
& operator+=(const wxWCharBuffer
& s
)
2502 { return operator+=(s
.data()); }
2504 wxString
& operator+=(wxUniChar ch
)
2505 { m_impl
+= wxStringOperations::EncodeChar(ch
); return *this; }
2506 wxString
& operator+=(wxUniCharRef ch
) { return *this += wxUniChar(ch
); }
2507 wxString
& operator+=(int ch
) { return *this += wxUniChar(ch
); }
2508 wxString
& operator+=(char ch
) { return *this += wxUniChar(ch
); }
2509 wxString
& operator+=(unsigned char ch
) { return *this += wxUniChar(ch
); }
2510 wxString
& operator+=(wchar_t ch
) { return *this += wxUniChar(ch
); }
2513 #if !wxUSE_STL_BASED_WXSTRING
2514 // helpers for wxStringBuffer and wxStringBufferLength
2515 wxStringCharType
*DoGetWriteBuf(size_t nLen
)
2516 { return m_impl
.DoGetWriteBuf(nLen
); }
2517 void DoUngetWriteBuf()
2518 { m_impl
.DoUngetWriteBuf(); }
2519 void DoUngetWriteBuf(size_t nLen
)
2520 { m_impl
.DoUngetWriteBuf(nLen
); }
2521 #endif // !wxUSE_STL_BASED_WXSTRING
2523 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2524 #if !wxUSE_UTF8_LOCALE_ONLY
2525 int DoPrintfWchar(const wxChar
*format
, ...);
2526 static wxString
DoFormatWchar(const wxChar
*format
, ...);
2528 #if wxUSE_UNICODE_UTF8
2529 int DoPrintfUtf8(const char *format
, ...);
2530 static wxString
DoFormatUtf8(const char *format
, ...);
2534 #if !wxUSE_STL_BASED_WXSTRING
2535 // check string's data validity
2536 bool IsValid() const { return m_impl
.GetStringData()->IsValid(); }
2540 wxStringImpl m_impl
;
2543 // "struct 'ConvertedBuffer<T>' needs to have dll-interface to be used by
2544 // clients of class 'wxString'" - this is private, we don't care
2545 #pragma warning (disable:4251)
2548 // buffers for compatibility conversion from (char*)c_str() and
2549 // (wchar_t*)c_str():
2550 // FIXME-UTF8: bechmark various approaches to keeping compatibility buffers
2551 template<typename T
>
2552 struct ConvertedBuffer
2554 ConvertedBuffer() : m_buf(NULL
) {}
2558 operator T
*() const { return m_buf
; }
2560 ConvertedBuffer
& operator=(T
*str
)
2569 #if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
2570 ConvertedBuffer
<char> m_convertedToChar
;
2572 #if !wxUSE_UNICODE_WCHAR
2573 ConvertedBuffer
<wchar_t> m_convertedToWChar
;
2577 #pragma warning (default:4251)
2580 #if wxUSE_UNICODE_UTF8
2581 // FIXME-UTF8: (try to) move this elsewhere (TLS) or solve differently
2582 // assigning to character pointer to by wxString::interator may
2583 // change the underlying wxStringImpl iterator, so we have to
2584 // keep track of all iterators and update them as necessary:
2585 struct wxStringIteratorNodeHead
2587 wxStringIteratorNodeHead() : ptr(NULL
) {}
2588 wxStringIteratorNode
*ptr
;
2590 // copying is disallowed as it would result in more than one pointer into
2591 // the same linked list
2592 DECLARE_NO_COPY_CLASS(wxStringIteratorNodeHead
)
2595 wxStringIteratorNodeHead m_iterators
;
2597 friend class WXDLLIMPEXP_BASE wxStringIteratorNode
;
2598 friend class WXDLLIMPEXP_BASE wxUniCharRef
;
2599 #endif // wxUSE_UNICODE_UTF8
2601 friend class WXDLLIMPEXP_BASE wxCStrData
;
2602 friend class wxImplStringBuffer
;
2603 friend class wxImplStringBufferLength
;
2606 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
2607 #pragma warning (default:4275)
2610 // string iterator operators that satisfy STL Random Access Iterator
2612 inline wxString::iterator
operator+(int n
, wxString::iterator i
)
2614 inline wxString::iterator
operator+(size_t n
, wxString::iterator i
)
2616 inline wxString::const_iterator
operator+(int n
, wxString::const_iterator i
)
2618 inline wxString::const_iterator
operator+(size_t n
, wxString::const_iterator i
)
2620 inline wxString::reverse_iterator
operator+(int n
, wxString::reverse_iterator i
)
2622 inline wxString::reverse_iterator
operator+(size_t n
, wxString::reverse_iterator i
)
2624 inline wxString::const_reverse_iterator
operator+(int n
, wxString::const_reverse_iterator i
)
2626 inline wxString::const_reverse_iterator
operator+(size_t n
, wxString::const_reverse_iterator i
)
2629 // notice that even though for many compilers the friend declarations above are
2630 // enough, from the point of view of C++ standard we must have the declarations
2631 // here as friend ones are not injected in the enclosing namespace and without
2632 // them the code fails to compile with conforming compilers such as xlC or g++4
2633 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
, const wxString
& string2
);
2634 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const char *psz
);
2635 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const wchar_t *pwz
);
2636 wxString WXDLLIMPEXP_BASE
operator+(const char *psz
, const wxString
& string
);
2637 wxString WXDLLIMPEXP_BASE
operator+(const wchar_t *pwz
, const wxString
& string
);
2639 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
2640 wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
2642 inline wxString
operator+(const wxString
& string
, wxUniCharRef ch
)
2643 { return string
+ (wxUniChar
)ch
; }
2644 inline wxString
operator+(const wxString
& string
, char ch
)
2645 { return string
+ wxUniChar(ch
); }
2646 inline wxString
operator+(const wxString
& string
, wchar_t ch
)
2647 { return string
+ wxUniChar(ch
); }
2648 inline wxString
operator+(wxUniCharRef ch
, const wxString
& string
)
2649 { return (wxUniChar
)ch
+ string
; }
2650 inline wxString
operator+(char ch
, const wxString
& string
)
2651 { return wxUniChar(ch
) + string
; }
2652 inline wxString
operator+(wchar_t ch
, const wxString
& string
)
2653 { return wxUniChar(ch
) + string
; }
2656 #if wxUSE_STL_BASED_WXSTRING
2657 // return an empty wxString (not very useful with wxUSE_STL == 1)
2658 inline const wxString
wxGetEmptyString() { return wxString(); }
2659 #else // !wxUSE_STL_BASED_WXSTRING
2660 // return an empty wxString (more efficient than wxString() here)
2661 inline const wxString
& wxGetEmptyString()
2663 return *(wxString
*)&wxEmptyString
;
2665 #endif // wxUSE_STL_BASED_WXSTRING/!wxUSE_STL_BASED_WXSTRING
2667 // ----------------------------------------------------------------------------
2668 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
2669 // ----------------------------------------------------------------------------
2671 #if !wxUSE_STL_BASED_WXSTRING
2672 // string buffer for direct access to string data in their native
2674 class wxImplStringBuffer
2677 typedef wxStringCharType CharType
;
2679 wxImplStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
2680 : m_str(str
), m_buf(NULL
)
2681 { m_buf
= m_str
.DoGetWriteBuf(lenWanted
); }
2683 ~wxImplStringBuffer() { m_str
.DoUngetWriteBuf(); }
2685 operator wxStringCharType
*() const { return m_buf
; }
2689 wxStringCharType
*m_buf
;
2691 DECLARE_NO_COPY_CLASS(wxImplStringBuffer
)
2694 class wxImplStringBufferLength
2697 typedef wxStringCharType CharType
;
2699 wxImplStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
2700 : m_str(str
), m_buf(NULL
), m_len(0), m_lenSet(false)
2702 m_buf
= m_str
.DoGetWriteBuf(lenWanted
);
2703 wxASSERT(m_buf
!= NULL
);
2706 ~wxImplStringBufferLength()
2709 m_str
.DoUngetWriteBuf(m_len
);
2712 operator wxStringCharType
*() const { return m_buf
; }
2713 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
2717 wxStringCharType
*m_buf
;
2721 DECLARE_NO_COPY_CLASS(wxImplStringBufferLength
)
2724 #endif // !wxUSE_STL_BASED_WXSTRING
2726 template<typename T
>
2727 class wxStringTypeBufferBase
2732 wxStringTypeBufferBase(wxString
& str
, size_t lenWanted
= 1024)
2733 : m_str(str
), m_buf(lenWanted
)
2737 operator CharType
*() { return m_buf
.data(); }
2741 wxCharTypeBuffer
<CharType
> m_buf
;
2744 template<typename T
>
2745 class wxStringTypeBufferLengthBase
2750 wxStringTypeBufferLengthBase(wxString
& str
, size_t lenWanted
= 1024)
2751 : m_str(str
), m_buf(lenWanted
), m_len(0), m_lenSet(false)
2754 ~wxStringTypeBufferLengthBase()
2757 m_str
.assign(m_buf
.data(), m_len
);
2760 operator CharType
*() { return m_buf
.data(); }
2761 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
2765 wxCharTypeBuffer
<CharType
> m_buf
;
2770 template<typename T
>
2771 class wxStringTypeBuffer
: public wxStringTypeBufferBase
<T
>
2774 wxStringTypeBuffer(wxString
& str
, size_t lenWanted
= 1024)
2775 : wxStringTypeBufferBase
<T
>(str
, lenWanted
) {}
2776 ~wxStringTypeBuffer()
2778 this->m_str
.assign(this->m_buf
.data());
2781 DECLARE_NO_COPY_CLASS(wxStringTypeBuffer
)
2784 template<typename T
>
2785 class wxStringTypeBufferLength
: public wxStringTypeBufferLengthBase
<T
>
2788 wxStringTypeBufferLength(wxString
& str
, size_t lenWanted
= 1024)
2789 : wxStringTypeBufferLengthBase
<T
>(str
, lenWanted
) {}
2791 ~wxStringTypeBufferLength()
2793 wxASSERT(this->m_lenSet
);
2794 this->m_str
.assign(this->m_buf
.data(), this->m_len
);
2797 DECLARE_NO_COPY_CLASS(wxStringTypeBufferLength
)
2800 #if wxUSE_STL_BASED_WXSTRING
2801 class wxImplStringBuffer
: public wxStringTypeBufferBase
<wxStringCharType
>
2804 wxImplStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
2805 : wxStringTypeBufferBase
<wxStringCharType
>(str
, lenWanted
) {}
2806 ~wxImplStringBuffer()
2807 { m_str
.m_impl
.assign(m_buf
.data()); }
2809 DECLARE_NO_COPY_CLASS(wxImplStringBuffer
)
2812 class wxImplStringBufferLength
: public wxStringTypeBufferLengthBase
<wxStringCharType
>
2815 wxImplStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
2816 : wxStringTypeBufferLengthBase
<wxStringCharType
>(str
, lenWanted
) {}
2818 ~wxImplStringBufferLength()
2821 m_str
.m_impl
.assign(m_buf
.data(), m_len
);
2824 DECLARE_NO_COPY_CLASS(wxImplStringBufferLength
)
2826 #endif // wxUSE_STL_BASED_WXSTRING
2829 #if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
2830 typedef wxStringTypeBuffer
<wxChar
> wxStringBuffer
;
2831 typedef wxStringTypeBufferLength
<wxChar
> wxStringBufferLength
;
2832 #else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2833 typedef wxImplStringBuffer wxStringBuffer
;
2834 typedef wxImplStringBufferLength wxStringBufferLength
;
2835 #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2837 // ---------------------------------------------------------------------------
2838 // wxString comparison functions: operator versions are always case sensitive
2839 // ---------------------------------------------------------------------------
2841 #define wxCMP_WXCHAR_STRING(p, s, op) 0 op s.Cmp(p)
2843 wxDEFINE_ALL_COMPARISONS(const wxChar
*, const wxString
&, wxCMP_WXCHAR_STRING
)
2845 #undef wxCMP_WXCHAR_STRING
2847 // note that there is an optimization in operator==() and !=(): we (quickly)
2848 // checks the strings length first, before comparing their data
2849 inline bool operator==(const wxString
& s1
, const wxString
& s2
)
2850 { return (s1
.Len() == s2
.Len()) && (s1
.Cmp(s2
) == 0); }
2851 inline bool operator!=(const wxString
& s1
, const wxString
& s2
)
2852 { return (s1
.Len() != s2
.Len()) || (s1
.Cmp(s2
) != 0); }
2853 inline bool operator< (const wxString
& s1
, const wxString
& s2
)
2854 { return s1
.Cmp(s2
) < 0; }
2855 inline bool operator> (const wxString
& s1
, const wxString
& s2
)
2856 { return s1
.Cmp(s2
) > 0; }
2857 inline bool operator<=(const wxString
& s1
, const wxString
& s2
)
2858 { return s1
.Cmp(s2
) <= 0; }
2859 inline bool operator>=(const wxString
& s1
, const wxString
& s2
)
2860 { return s1
.Cmp(s2
) >= 0; }
2862 inline bool operator==(const wxString
& s1
, const wxCStrData
& s2
)
2863 { return s1
== s2
.AsString(); }
2864 inline bool operator==(const wxCStrData
& s1
, const wxString
& s2
)
2865 { return s1
.AsString() == s2
; }
2866 inline bool operator!=(const wxString
& s1
, const wxCStrData
& s2
)
2867 { return s1
!= s2
.AsString(); }
2868 inline bool operator!=(const wxCStrData
& s1
, const wxString
& s2
)
2869 { return s1
.AsString() != s2
; }
2871 inline bool operator==(const wxString
& s1
, const wxWCharBuffer
& s2
)
2872 { return (s1
.Cmp((const wchar_t *)s2
) == 0); }
2873 inline bool operator==(const wxWCharBuffer
& s1
, const wxString
& s2
)
2874 { return (s2
.Cmp((const wchar_t *)s1
) == 0); }
2875 inline bool operator!=(const wxString
& s1
, const wxWCharBuffer
& s2
)
2876 { return (s1
.Cmp((const wchar_t *)s2
) != 0); }
2877 inline bool operator!=(const wxWCharBuffer
& s1
, const wxString
& s2
)
2878 { return (s2
.Cmp((const wchar_t *)s1
) != 0); }
2880 inline bool operator==(const wxString
& s1
, const wxCharBuffer
& s2
)
2881 { return (s1
.Cmp((const char *)s2
) == 0); }
2882 inline bool operator==(const wxCharBuffer
& s1
, const wxString
& s2
)
2883 { return (s2
.Cmp((const char *)s1
) == 0); }
2884 inline bool operator!=(const wxString
& s1
, const wxCharBuffer
& s2
)
2885 { return (s1
.Cmp((const char *)s2
) != 0); }
2886 inline bool operator!=(const wxCharBuffer
& s1
, const wxString
& s2
)
2887 { return (s2
.Cmp((const char *)s1
) != 0); }
2889 inline wxString
operator+(const wxString
& string
, const wxWCharBuffer
& buf
)
2890 { return string
+ (const wchar_t *)buf
; }
2891 inline wxString
operator+(const wxWCharBuffer
& buf
, const wxString
& string
)
2892 { return (const wchar_t *)buf
+ string
; }
2894 inline wxString
operator+(const wxString
& string
, const wxCharBuffer
& buf
)
2895 { return string
+ (const char *)buf
; }
2896 inline wxString
operator+(const wxCharBuffer
& buf
, const wxString
& string
)
2897 { return (const char *)buf
+ string
; }
2899 // comparison with char
2900 inline bool operator==(const wxUniChar
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2901 inline bool operator==(const wxUniCharRef
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2902 inline bool operator==(char c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2903 inline bool operator==(wchar_t c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2904 inline bool operator==(int c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2905 inline bool operator==(const wxString
& s
, const wxUniChar
& c
) { return s
.IsSameAs(c
); }
2906 inline bool operator==(const wxString
& s
, const wxUniCharRef
& c
) { return s
.IsSameAs(c
); }
2907 inline bool operator==(const wxString
& s
, char c
) { return s
.IsSameAs(c
); }
2908 inline bool operator==(const wxString
& s
, wchar_t c
) { return s
.IsSameAs(c
); }
2909 inline bool operator!=(const wxUniChar
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2910 inline bool operator!=(const wxUniCharRef
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2911 inline bool operator!=(char c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2912 inline bool operator!=(wchar_t c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2913 inline bool operator!=(int c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2914 inline bool operator!=(const wxString
& s
, const wxUniChar
& c
) { return !s
.IsSameAs(c
); }
2915 inline bool operator!=(const wxString
& s
, const wxUniCharRef
& c
) { return !s
.IsSameAs(c
); }
2916 inline bool operator!=(const wxString
& s
, char c
) { return !s
.IsSameAs(c
); }
2917 inline bool operator!=(const wxString
& s
, wchar_t c
) { return !s
.IsSameAs(c
); }
2919 // comparison with C string in Unicode build
2922 #define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s
2924 wxDEFINE_ALL_COMPARISONS(const char *, const wxString
&, wxCMP_CHAR_STRING
)
2926 #undef wxCMP_CHAR_STRING
2928 #endif // wxUSE_UNICODE
2930 // we also need to provide the operators for comparison with wxCStrData to
2931 // resolve ambiguity between operator(const wxChar *,const wxString &) and
2932 // operator(const wxChar *, const wxChar *) for "p == s.c_str()"
2934 // notice that these are (shallow) pointer comparisons, not (deep) string ones
2935 #define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar()
2936 #define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar()
2938 wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData
&, wxCMP_WCHAR_CSTRDATA
)
2939 wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData
&, wxCMP_CHAR_CSTRDATA
)
2941 #undef wxCMP_CHAR_CSTRDATA
2942 #undef wxCMP_WCHAR_CSTRDATA
2944 // ---------------------------------------------------------------------------
2945 // Implementation only from here until the end of file
2946 // ---------------------------------------------------------------------------
2948 #if wxUSE_STD_IOSTREAM
2950 #include "wx/iosfwrap.h"
2952 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxString
&);
2953 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxCStrData
&);
2954 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxCharBuffer
&);
2955 #ifndef __BORLANDC__
2956 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxWCharBuffer
&);
2959 #endif // wxSTD_STRING_COMPATIBILITY
2961 // ---------------------------------------------------------------------------
2962 // wxCStrData implementation
2963 // ---------------------------------------------------------------------------
2965 inline wxCStrData::wxCStrData(char *buf
)
2966 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
2967 inline wxCStrData::wxCStrData(wchar_t *buf
)
2968 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
2970 inline wxCStrData::wxCStrData(const wxCStrData
& data
)
2971 : m_str(data
.m_owned
? new wxString(*data
.m_str
) : data
.m_str
),
2972 m_offset(data
.m_offset
),
2973 m_owned(data
.m_owned
)
2977 inline wxCStrData::~wxCStrData()
2983 // simple cases for AsChar() and AsWChar(), the complicated ones are
2985 #if wxUSE_UNICODE_WCHAR
2986 inline const wchar_t* wxCStrData::AsWChar() const
2988 return m_str
->wx_str() + m_offset
;
2990 #endif // wxUSE_UNICODE_WCHAR
2993 inline const char* wxCStrData::AsChar() const
2995 return m_str
->wx_str() + m_offset
;
2997 #endif // !wxUSE_UNICODE
2999 #if wxUSE_UTF8_LOCALE_ONLY
3000 inline const char* wxCStrData::AsChar() const
3002 return wxStringOperations::AddToIter(m_str
->wx_str(), m_offset
);
3004 #endif // wxUSE_UTF8_LOCALE_ONLY
3006 inline const wxCharBuffer
wxCStrData::AsCharBuf() const
3009 return wxCharBuffer::CreateNonOwned(AsChar());
3011 return AsString().mb_str();
3015 inline const wxWCharBuffer
wxCStrData::AsWCharBuf() const
3017 #if wxUSE_UNICODE_WCHAR
3018 return wxWCharBuffer::CreateNonOwned(AsWChar());
3020 return AsString().wc_str();
3024 inline wxString
wxCStrData::AsString() const
3026 if ( m_offset
== 0 )
3029 return m_str
->Mid(m_offset
);
3032 inline const wxStringCharType
*wxCStrData::AsInternal() const
3034 #if wxUSE_UNICODE_UTF8
3035 return wxStringOperations::AddToIter(m_str
->wx_str(), m_offset
);
3037 return m_str
->wx_str() + m_offset
;
3041 inline wxUniChar
wxCStrData::operator*() const
3043 if ( m_str
->empty() )
3044 return wxUniChar(_T('\0'));
3046 return (*m_str
)[m_offset
];
3049 inline wxUniChar
wxCStrData::operator[](size_t n
) const
3051 // NB: we intentionally use operator[] and not at() here because the former
3052 // works for the terminating NUL while the latter does not
3053 return (*m_str
)[m_offset
+ n
];
3056 // ----------------------------------------------------------------------------
3057 // more wxCStrData operators
3058 // ----------------------------------------------------------------------------
3060 // we need to define those to allow "size_t pos = p - s.c_str()" where p is
3061 // some pointer into the string
3062 inline size_t operator-(const char *p
, const wxCStrData
& cs
)
3064 return p
- cs
.AsChar();
3067 inline size_t operator-(const wchar_t *p
, const wxCStrData
& cs
)
3069 return p
- cs
.AsWChar();
3072 // ----------------------------------------------------------------------------
3073 // implementation of wx[W]CharBuffer inline methods using wxCStrData
3074 // ----------------------------------------------------------------------------
3076 // FIXME-UTF8: move this to buffer.h
3077 inline wxCharBuffer::wxCharBuffer(const wxCStrData
& cstr
)
3078 : wxCharTypeBufferBase(cstr
.AsCharBuf())
3082 inline wxWCharBuffer::wxWCharBuffer(const wxCStrData
& cstr
)
3083 : wxCharTypeBufferBase(cstr
.AsWCharBuf())
3087 #if wxUSE_UNICODE_UTF8
3088 // ----------------------------------------------------------------------------
3089 // implementation of wxStringIteratorNode inline methods
3090 // ----------------------------------------------------------------------------
3092 wxStringIteratorNode::wxStringIteratorNode(const wxString
*str
,
3093 wxStringImpl::const_iterator
*citer
)
3098 m_next(str
->m_iterators
.ptr
)
3100 wx_const_cast(wxString
*, m_str
)->m_iterators
.ptr
= this;
3102 m_next
->m_prev
= this;
3105 wxStringIteratorNode::wxStringIteratorNode(const wxString
*str
,
3106 wxStringImpl::iterator
*iter
)
3111 m_next(str
->m_iterators
.ptr
)
3113 wx_const_cast(wxString
*, m_str
)->m_iterators
.ptr
= this;
3115 m_next
->m_prev
= this;
3118 wxStringIteratorNode::~wxStringIteratorNode()
3121 m_next
->m_prev
= m_prev
;
3123 m_prev
->m_next
= m_next
;
3124 else // first in the list
3125 wx_const_cast(wxString
*, m_str
)->m_iterators
.ptr
= m_next
;
3127 #endif // wxUSE_UNICODE_UTF8
3129 #if WXWIN_COMPATIBILITY_2_8
3130 // lot of code out there doesn't explicitly include wx/crt.h, but uses
3131 // CRT wrappers that are now declared in wx/wxcrt.h and wx/wxcrtvararg.h,
3132 // so let's include this header now that wxString is defined and it's safe
3137 #endif // _WX_WXSTRING_H_