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/wxchar.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/unichar.h"
61 class WXDLLIMPEXP_BASE wxString
;
63 // unless this symbol is predefined to disable the compatibility functions, do
65 #ifndef WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
66 #define WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER 1
69 // ---------------------------------------------------------------------------
71 // ---------------------------------------------------------------------------
73 // casts [unfortunately!] needed to call some broken functions which require
74 // "char *" instead of "const char *"
75 #define WXSTRINGCAST (wxChar *)(const wxChar *)
76 #define wxCSTRINGCAST (wxChar *)(const wxChar *)
77 #define wxMBSTRINGCAST (char *)(const char *)
78 #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *)
80 // ----------------------------------------------------------------------------
82 // ----------------------------------------------------------------------------
84 #if WXWIN_COMPATIBILITY_2_6
86 // deprecated in favour of wxString::npos, don't use in new code
88 // maximum possible length for a string means "take all string" everywhere
89 #define wxSTRING_MAXLEN wxString::npos
91 #endif // WXWIN_COMPATIBILITY_2_6
93 // ---------------------------------------------------------------------------
94 // global functions complementing standard C string library replacements for
95 // strlen() and portable strcasecmp()
96 //---------------------------------------------------------------------------
98 #if WXWIN_COMPATIBILITY_2_8
99 // Use wxXXX() functions from wxcrt.h instead! These functions are for
100 // backwards compatibility only.
102 // checks whether the passed in pointer is NULL and if the string is empty
103 wxDEPRECATED( inline bool IsEmpty(const char *p
) );
104 inline bool IsEmpty(const char *p
) { return (!p
|| !*p
); }
106 // safe version of strlen() (returns 0 if passed NULL pointer)
107 wxDEPRECATED( inline size_t Strlen(const char *psz
) );
108 inline size_t Strlen(const char *psz
)
109 { return psz
? strlen(psz
) : 0; }
111 // portable strcasecmp/_stricmp
112 wxDEPRECATED( inline int Stricmp(const char *psz1
, const char *psz2
) );
113 inline int Stricmp(const char *psz1
, const char *psz2
)
115 #if defined(__VISUALC__) && defined(__WXWINCE__)
116 register char c1
, c2
;
118 c1
= tolower(*psz1
++);
119 c2
= tolower(*psz2
++);
120 } while ( c1
&& (c1
== c2
) );
123 #elif defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) )
124 return _stricmp(psz1
, psz2
);
125 #elif defined(__SC__)
126 return _stricmp(psz1
, psz2
);
127 #elif defined(__SALFORDC__)
128 return stricmp(psz1
, psz2
);
129 #elif defined(__BORLANDC__)
130 return stricmp(psz1
, psz2
);
131 #elif defined(__WATCOMC__)
132 return stricmp(psz1
, psz2
);
133 #elif defined(__DJGPP__)
134 return stricmp(psz1
, psz2
);
135 #elif defined(__EMX__)
136 return stricmp(psz1
, psz2
);
137 #elif defined(__WXPM__)
138 return stricmp(psz1
, psz2
);
139 #elif defined(__WXPALMOS__) || \
140 defined(HAVE_STRCASECMP_IN_STRING_H) || \
141 defined(HAVE_STRCASECMP_IN_STRINGS_H) || \
142 defined(__GNUWIN32__)
143 return strcasecmp(psz1
, psz2
);
144 #elif defined(__MWERKS__) && !defined(__INTEL__)
145 register char c1
, c2
;
147 c1
= tolower(*psz1
++);
148 c2
= tolower(*psz2
++);
149 } while ( c1
&& (c1
== c2
) );
153 // almost all compilers/libraries provide this function (unfortunately under
154 // different names), that's why we don't implement our own which will surely
155 // be more efficient than this code (uncomment to use):
157 register char c1, c2;
159 c1 = tolower(*psz1++);
160 c2 = tolower(*psz2++);
161 } while ( c1 && (c1 == c2) );
166 #error "Please define string case-insensitive compare for your OS/compiler"
167 #endif // OS/compiler
170 #endif // WXWIN_COMPATIBILITY_2_8
172 // ----------------------------------------------------------------------------
174 // ----------------------------------------------------------------------------
176 // Lightweight object returned by wxString::c_str() and implicitly convertible
177 // to either const char* or const wchar_t*.
178 class WXDLLIMPEXP_BASE wxCStrData
181 // Ctors; for internal use by wxString and wxCStrData only
182 wxCStrData(const wxString
*str
, size_t offset
= 0, bool owned
= false)
183 : m_str(str
), m_offset(offset
), m_owned(owned
) {}
186 // Ctor constructs the object from char literal; they are needed to make
187 // operator?: compile and they intentionally take char*, not const char*
188 wxCStrData(char *buf
);
189 wxCStrData(wchar_t *buf
);
191 inline ~wxCStrData();
193 // methods defined inline below must be declared inline or mingw32 3.4.5
194 // warns about "<symbol> defined locally after being referenced with
195 // dllimport linkage"
196 #if wxUSE_UNICODE_WCHAR
199 const wchar_t* AsWChar() const;
200 operator const wchar_t*() const { return AsWChar(); }
202 inline operator bool() const;
207 const char* AsChar() const;
208 const unsigned char* AsUnsignedChar() const
209 { return (const unsigned char *) AsChar(); }
210 operator const char*() const { return AsChar(); }
211 operator const unsigned char*() const { return AsUnsignedChar(); }
213 operator const void*() const { return AsChar(); }
215 inline const wxCharBuffer
AsCharBuf() const;
216 inline const wxWCharBuffer
AsWCharBuf() const;
218 inline wxString
AsString() const;
220 // allow expressions like "c_str()[0]":
221 inline wxUniChar
operator[](size_t n
) const;
222 wxUniChar
operator[](int n
) const { return operator[](size_t(n
)); }
223 wxUniChar
operator[](long n
) const { return operator[](size_t(n
)); }
224 #ifndef wxSIZE_T_IS_UINT
225 wxUniChar
operator[](unsigned int n
) const { return operator[](size_t(n
)); }
226 #endif // size_t != unsigned int
228 // these operators are needed to emulate the pointer semantics of c_str():
229 // expressions like "wxChar *p = str.c_str() + 1;" should continue to work
230 // (we need both versions to resolve ambiguities):
231 wxCStrData
operator+(int n
) const
232 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
233 wxCStrData
operator+(long n
) const
234 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
235 wxCStrData
operator+(size_t n
) const
236 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
238 // and these for "str.c_str() + n - 2":
239 wxCStrData
operator-(int n
) const
241 wxASSERT_MSG( n
<= (int)m_offset
,
242 _T("attempt to construct address before the beginning of the string") );
243 return wxCStrData(m_str
, m_offset
- n
, m_owned
);
245 wxCStrData
operator-(long n
) const
247 wxASSERT_MSG( n
<= (int)m_offset
,
248 _T("attempt to construct address before the beginning of the string") );
249 return wxCStrData(m_str
, m_offset
- n
, m_owned
);
251 wxCStrData
operator-(size_t n
) const
253 wxASSERT_MSG( n
<= m_offset
,
254 _T("attempt to construct address before the beginning of the string") );
255 return wxCStrData(m_str
, m_offset
- n
, m_owned
);
258 // this operator is needed to make expressions like "*c_str()" or
259 // "*(c_str() + 2)" work
260 inline wxUniChar
operator*() const;
263 const wxString
*m_str
;
267 friend class WXDLLIMPEXP_BASE wxString
;
270 // ----------------------------------------------------------------------------
271 // wxStringPrintfMixin
272 // ---------------------------------------------------------------------------
274 // NB: VC6 has a bug that causes linker errors if you have template methods
275 // in a class using __declspec(dllimport). The solution is to split such
276 // class into two classes, one that contains the template methods and does
277 // *not* use WXDLLIMPEXP_BASE and another class that contains the rest
278 // (with DLL linkage).
280 // We only do this for VC6 here, because the code is less efficient
281 // (Printf() has to use dynamic_cast<>) and because OpenWatcom compiler
282 // cannot compile this code.
284 #if defined(__VISUALC__) && __VISUALC__ < 1300
285 #define wxNEEDS_WXSTRING_PRINTF_MIXIN
288 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
289 // this class contains implementation of wxString's vararg methods, it's
290 // exported from wxBase DLL
291 class WXDLLIMPEXP_BASE wxStringPrintfMixinBase
294 wxStringPrintfMixinBase() {}
296 int DoPrintf(const wxChar
*format
, ...) ATTRIBUTE_PRINTF_2
;
297 static wxString
DoFormat(const wxChar
*format
, ...) ATTRIBUTE_PRINTF_1
;
300 // this class contains template wrappers for wxString's vararg methods, it's
301 // intentionally *not* exported from the DLL in order to fix the VC6 bug
303 class wxStringPrintfMixin
: public wxStringPrintfMixinBase
306 // to further complicate things, we can't return wxString from
307 // wxStringPrintfMixin::Format() because wxString is not yet declared at
308 // this point; the solution is to use this fake type trait template - this
309 // way the compiler won't know the return type until Format() is used
310 // (this doesn't compile with Watcom, but VC6 compiles it just fine):
311 template<typename T
> struct StringReturnType
313 typedef wxString type
;
317 // these are duplicated wxString methods, they're also declared below
318 // if !wxNEEDS_WXSTRING_PRINTF_MIXIN:
320 // int Printf(const wxChar *pszFormat, ...);
321 WX_DEFINE_VARARG_FUNC(int, Printf
, DoPrintf
)
322 // static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1;
323 WX_DEFINE_VARARG_FUNC(static typename StringReturnType
<T1
>::type
,
325 // int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2;
326 WX_DEFINE_VARARG_FUNC(int, sprintf
, DoPrintf
)
329 wxStringPrintfMixin() : wxStringPrintfMixinBase() {}
331 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
334 // ----------------------------------------------------------------------------
335 // wxString: string class trying to be compatible with std::string, MFC
336 // CString and wxWindows 1.x wxString all at once
337 // ---------------------------------------------------------------------------
339 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
340 // "non dll-interface class 'wxStringPrintfMixin' used as base interface
341 // for dll-interface class 'wxString'" -- this is OK in our case
342 #pragma warning (disable:4275)
345 class WXDLLIMPEXP_BASE wxString
346 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
347 : public wxStringPrintfMixin
350 // NB: special care was taken in arranging the member functions in such order
351 // that all inline functions can be effectively inlined, verify that all
352 // performance critical functions are still inlined if you change order!
354 // an 'invalid' value for string index, moved to this place due to a CW bug
355 static const size_t npos
;
358 // if we hadn't made these operators private, it would be possible to
359 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
360 // converted to char in C and we do have operator=(char)
362 // NB: we don't need other versions (short/long and unsigned) as attempt
363 // to assign another numeric type to wxString will now result in
364 // ambiguity between operator=(char) and operator=(int)
365 wxString
& operator=(int);
367 // these methods are not implemented - there is _no_ conversion from int to
368 // string, you're doing something wrong if the compiler wants to call it!
370 // try `s << i' or `s.Printf("%d", i)' instead
374 // buffer for holding temporary substring when using any of the methods
375 // that take (char*,size_t) or (wchar_t*,size_t) arguments:
376 // FIXME-UTF8: This will need changes when UTF8 build is introduced
378 struct SubstrBufFromType
383 SubstrBufFromType(const T
& data_
, size_t len_
)
384 : data(data_
), len(len_
) {}
387 #if wxUSE_UNICODE_UTF8
388 // even char* -> char* needs conversion, from locale charset to UTF-8
389 typedef SubstrBufFromType
<wxCharBuffer
> SubstrBufFromWC
;
390 typedef SubstrBufFromType
<wxCharBuffer
> SubstrBufFromMB
;
391 #elif wxUSE_UNICODE_WCHAR
392 typedef SubstrBufFromType
<const wchar_t*> SubstrBufFromWC
;
393 typedef SubstrBufFromType
<wxWCharBuffer
> SubstrBufFromMB
;
395 typedef SubstrBufFromType
<const char*> SubstrBufFromMB
;
396 typedef SubstrBufFromType
<wxCharBuffer
> SubstrBufFromWC
;
400 // Functions implementing primitive operations on string data; wxString
401 // methods and iterators are implemented in terms of it. The differences
402 // between UTF-8 and wchar_t* representations of the string are mostly
405 #if wxUSE_UNICODE_UTF8
406 static SubstrBufFromMB
ConvertStr(const char *psz
, size_t nLength
,
407 const wxMBConv
& conv
);
408 static SubstrBufFromWC
ConvertStr(const wchar_t *pwz
, size_t nLength
,
409 const wxMBConv
& conv
);
410 #elif wxUSE_UNICODE_WCHAR
411 static SubstrBufFromMB
ConvertStr(const char *psz
, size_t nLength
,
412 const wxMBConv
& conv
);
414 static SubstrBufFromWC
ConvertStr(const wchar_t *pwz
, size_t nLength
,
415 const wxMBConv
& conv
);
418 #if !wxUSE_UNICODE_UTF8 // wxUSE_UNICODE_WCHAR or !wxUSE_UNICODE
419 // returns C string encoded as the implementation expects:
421 static const wchar_t* ImplStr(const wchar_t* str
)
422 { return str
? str
: wxT(""); }
423 static const SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
424 { return SubstrBufFromWC(str
, (str
&& n
== npos
) ? wxWcslen(str
) : n
); }
425 static wxWCharBuffer
ImplStr(const char* str
,
426 const wxMBConv
& conv
= wxConvLibc
)
427 { return ConvertStr(str
, npos
, conv
).data
; }
428 static SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
429 const wxMBConv
& conv
= wxConvLibc
)
430 { return ConvertStr(str
, n
, conv
); }
432 static const char* ImplStr(const char* str
,
433 const wxMBConv
& WXUNUSED(conv
) = wxConvLibc
)
434 { return str
? str
: ""; }
435 static const SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
436 const wxMBConv
& WXUNUSED(conv
) = wxConvLibc
)
437 { return SubstrBufFromMB(str
, (str
&& n
== npos
) ? wxStrlen(str
) : n
); }
438 static wxCharBuffer
ImplStr(const wchar_t* str
)
439 { return ConvertStr(str
, npos
, wxConvLibc
).data
; }
440 static SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
441 { return ConvertStr(str
, n
, wxConvLibc
); }
444 // moves the iterator to the next Unicode character
445 static void IncIter(wxStringImpl::iterator
& i
) { ++i
; }
446 static void IncIter(wxStringImpl::const_iterator
& i
) { ++i
; }
447 // moves the iterator to the previous Unicode character
448 static void DecIter(wxStringImpl::iterator
& i
) { --i
; }
449 static void DecIter(wxStringImpl::const_iterator
& i
) { --i
; }
450 // moves the iterator by n Unicode characters
451 static wxStringImpl::iterator
AddToIter(wxStringImpl::iterator i
, int n
)
453 static wxStringImpl::const_iterator
AddToIter(wxStringImpl::const_iterator i
, int n
)
455 // returns distance of the two iterators in Unicode characters
456 static int DiffIters(wxStringImpl::iterator i1
, wxStringImpl::iterator i2
)
458 static int DiffIters(wxStringImpl::const_iterator i1
, wxStringImpl::const_iterator i2
)
461 // encodes the character to a form used to represent it in internal
462 // representation (returns a string in UTF8 version)
463 static wxChar
EncodeChar(wxUniChar ch
) { return (wxChar
)ch
; }
464 static wxUniChar
DecodeChar(wxStringImpl::const_iterator i
) { return *i
; }
466 // translates position index in wxString to/from index in underlying
468 static size_t PosToImpl(size_t pos
) { return pos
; }
469 static void PosLenToImpl(size_t pos
, size_t len
,
470 size_t *implPos
, size_t *implLen
)
471 { *implPos
= pos
; *implLen
= len
; }
472 static size_t LenToImpl(size_t len
) { return len
; }
473 static size_t PosFromImpl(size_t pos
) { return pos
; }
475 #else // wxUSE_UNICODE_UTF8
477 // checks correctness of UTF-8 sequence
478 static bool IsValidUtf8String(const char *c
);
480 static bool IsValidUtf8LeadByte(unsigned char c
);
483 // table of offsets to skip forward when iterating
484 static unsigned char ms_utf8IterTable
[256];
486 static void IncIter(wxStringImpl::iterator
& i
)
488 wxASSERT( IsValidUtf8LeadByte(*i
) );
489 i
+= ms_utf8IterTable
[(unsigned char)*i
];
491 static void IncIter(wxStringImpl::const_iterator
& i
)
493 wxASSERT( IsValidUtf8LeadByte(*i
) );
494 i
+= ms_utf8IterTable
[(unsigned char)*i
];
497 static void DecIter(wxStringImpl::iterator
& i
);
498 static void DecIter(wxStringImpl::const_iterator
& i
);
499 static wxStringImpl::iterator
AddToIter(wxStringImpl::iterator i
, int n
);
500 static wxStringImpl::const_iterator
AddToIter(wxStringImpl::const_iterator i
, int n
);
501 static int DiffIters(wxStringImpl::const_iterator i1
, wxStringImpl::const_iterator i2
);
502 static int DiffIters(wxStringImpl::iterator i1
, wxStringImpl::iterator i2
);
504 struct Utf8CharBuffer
507 operator const char*() const { return data
; }
509 static Utf8CharBuffer
EncodeChar(wxUniChar ch
);
510 // returns n copies of ch encoded in UTF-8 string
511 static wxCharBuffer
EncodeNChars(size_t n
, wxUniChar ch
);
513 // returns the length of UTF-8 encoding of the character with lead byte 'c'
514 static size_t GetUtf8CharLength(char c
)
516 wxASSERT( IsValidUtf8LeadByte(c
) );
517 return ms_utf8IterTable
[(unsigned char)c
];
520 // decodes single UTF-8 character from UTF-8 string
521 // FIXME-UTF8: move EncodeChar/DecodeChar and other operations to separate
523 static wxUniChar
DecodeChar(wxStringImpl::const_iterator i
)
524 { return wxUniCharRef::DecodeChar(i
); }
525 friend class WXDLLIMPEXP_BASE wxUniCharRef
;
527 size_t PosToImpl(size_t pos
) const
529 if ( pos
== 0 || pos
== npos
)
532 return (begin() + pos
).impl() - m_impl
.begin();
535 void PosLenToImpl(size_t pos
, size_t len
, size_t *implPos
, size_t *implLen
) const;
537 size_t LenToImpl(size_t len
) const
540 PosLenToImpl(0, len
, &pos
, &len2
);
544 size_t PosFromImpl(size_t pos
) const
546 if ( pos
== 0 || pos
== npos
)
549 return const_iterator(m_impl
.begin() + pos
) - begin();
552 // FIXME-UTF8: return as-is without copying under UTF8 locale, return
553 // converted string under other locales - needs wxCharBuffer
555 static wxCharBuffer
ImplStr(const char* str
,
556 const wxMBConv
& conv
= wxConvLibc
)
557 { return ConvertStr(str
, npos
, conv
).data
; }
558 static SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
559 const wxMBConv
& conv
= wxConvLibc
)
560 { return ConvertStr(str
, n
, conv
); }
562 static wxCharBuffer
ImplStr(const wchar_t* str
)
563 { return ConvertStr(str
, npos
, wxConvUTF8
).data
; }
564 static SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
565 { return ConvertStr(str
, n
, wxConvUTF8
); }
566 #endif // !wxUSE_UNICODE_UTF8/wxUSE_UNICODE_UTF8
570 typedef wxUniChar value_type
;
571 typedef wxUniChar char_type
;
572 typedef wxUniCharRef reference
;
573 typedef wxChar
* pointer
;
574 typedef const wxChar
* const_pointer
;
576 typedef size_t size_type
;
577 typedef wxUniChar const_reference
;
580 #if wxUSE_UNICODE_UTF8
581 // random access is not O(1), as required by Random Access Iterator
582 #define WX_STR_ITERATOR_TAG std::bidirectional_iterator_tag
584 #define WX_STR_ITERATOR_TAG std::random_access_iterator_tag
587 #define WX_STR_ITERATOR_TAG void /* dummy type */
590 #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, \
591 reference_type, reference_ctor) \
593 typedef wxStringImpl::iterator_name underlying_iterator; \
595 typedef WX_STR_ITERATOR_TAG iterator_category; \
596 typedef wxUniChar value_type; \
597 typedef int difference_type; \
598 typedef reference_type reference; \
599 typedef pointer_type pointer; \
601 reference operator*() const { return reference_ctor; } \
602 reference operator[](size_t n) const { return *(*this + n); } \
604 iterator_name& operator++() \
605 { wxString::IncIter(m_cur); return *this; } \
606 iterator_name& operator--() \
607 { wxString::DecIter(m_cur); return *this; } \
608 iterator_name operator++(int) \
610 iterator_name tmp = *this; \
611 wxString::IncIter(m_cur); \
614 iterator_name operator--(int) \
616 iterator_name tmp = *this; \
617 wxString::DecIter(m_cur); \
621 iterator_name& operator+=(int n) \
622 { m_cur = wxString::AddToIter(m_cur, n); return *this; } \
623 iterator_name& operator+=(size_t n) \
624 { m_cur = wxString::AddToIter(m_cur, (int)n); return *this; } \
625 iterator_name& operator-=(int n) \
626 { m_cur = wxString::AddToIter(m_cur, -n); return *this; } \
627 iterator_name& operator-=(size_t n) \
628 { m_cur = wxString::AddToIter(m_cur, -(int)n); return *this; } \
630 difference_type operator-(const iterator_name& i) const \
631 { return wxString::DiffIters(m_cur, i.m_cur); } \
633 bool operator==(const iterator_name& i) const \
634 { return m_cur == i.m_cur; } \
635 bool operator!=(const iterator_name& i) const \
636 { return m_cur != i.m_cur; } \
638 bool operator<(const iterator_name& i) const \
639 { return m_cur < i.m_cur; } \
640 bool operator>(const iterator_name& i) const \
641 { return m_cur > i.m_cur; } \
642 bool operator<=(const iterator_name& i) const \
643 { return m_cur <= i.m_cur; } \
644 bool operator>=(const iterator_name& i) const \
645 { return m_cur >= i.m_cur; } \
648 /* for internal wxString use only: */ \
649 underlying_iterator impl() const { return m_cur; } \
651 friend class WXDLLIMPEXP_BASE wxString; \
652 friend class WXDLLIMPEXP_BASE wxCStrData; \
655 underlying_iterator m_cur
657 class const_iterator
;
659 #if wxUSE_UNICODE_UTF8
662 // NB: In UTF-8 build, (non-const) iterator needs to keep reference
663 // to the underlying wxStringImpl, because UTF-8 is variable-length
664 // encoding and changing the value pointer to by an iterator using
665 // its operator* requires calling wxStringImpl::replace() if the old
666 // and new values differ in their encoding's length.
668 WX_STR_ITERATOR_IMPL(iterator
, wxChar
*, wxUniCharRef
,
669 wxUniCharRef::CreateForString(m_str
, m_cur
));
672 iterator(const iterator
& i
) : m_cur(i
.m_cur
), m_str(i
.m_str
) {}
674 iterator
operator+(int n
) const
675 { return iterator(m_str
, wxString::AddToIter(m_cur
, n
)); }
676 iterator
operator+(size_t n
) const
677 { return iterator(m_str
, wxString::AddToIter(m_cur
, (int)n
)); }
678 iterator
operator-(int n
) const
679 { return iterator(m_str
, wxString::AddToIter(m_cur
, -n
)); }
680 iterator
operator-(size_t n
) const
681 { return iterator(m_str
, wxString::AddToIter(m_cur
, -(int)n
)); }
684 iterator(wxString
*str
, underlying_iterator ptr
)
685 : m_cur(ptr
), m_str(str
->m_impl
) {}
686 iterator(wxStringImpl
& str
, underlying_iterator ptr
)
687 : m_cur(ptr
), m_str(str
) {}
691 friend class const_iterator
;
694 size_t IterToImplPos(wxString::iterator i
) const
695 { return wxStringImpl::const_iterator(i
.impl()) - m_impl
.begin(); }
697 #else // !wxUSE_UNICODE_UTF8
701 WX_STR_ITERATOR_IMPL(iterator
, wxChar
*, wxUniCharRef
,
702 wxUniCharRef::CreateForString(m_cur
));
705 iterator(const iterator
& i
) : m_cur(i
.m_cur
) {}
707 iterator
operator+(int n
) const
708 { return iterator(wxString::AddToIter(m_cur
, n
)); }
709 iterator
operator+(size_t n
) const
710 { return iterator(wxString::AddToIter(m_cur
, (int)n
)); }
711 iterator
operator-(int n
) const
712 { return iterator(wxString::AddToIter(m_cur
, -n
)); }
713 iterator
operator-(size_t n
) const
714 { return iterator(wxString::AddToIter(m_cur
, -(int)n
)); }
717 // for internal wxString use only:
718 iterator(underlying_iterator ptr
) : m_cur(ptr
) {}
719 iterator(wxString
*WXUNUSED(str
), underlying_iterator ptr
) : m_cur(ptr
) {}
721 friend class const_iterator
;
723 #endif // wxUSE_UNICODE_UTF8/!wxUSE_UNICODE_UTF8
727 // NB: reference_type is intentionally value, not reference, the character
728 // may be encoded differently in wxString data:
729 WX_STR_ITERATOR_IMPL(const_iterator
, const wxChar
*, wxUniChar
,
730 wxString::DecodeChar(m_cur
));
733 const_iterator(const const_iterator
& i
) : m_cur(i
.m_cur
) {}
734 const_iterator(const iterator
& i
) : m_cur(i
.m_cur
) {}
736 const_iterator
operator+(int n
) const
737 { return const_iterator(wxString::AddToIter(m_cur
, n
)); }
738 const_iterator
operator+(size_t n
) const
739 { return const_iterator(wxString::AddToIter(m_cur
, (int)n
)); }
740 const_iterator
operator-(int n
) const
741 { return const_iterator(wxString::AddToIter(m_cur
, -n
)); }
742 const_iterator
operator-(size_t n
) const
743 { return const_iterator(wxString::AddToIter(m_cur
, -(int)n
)); }
746 // for internal wxString use only:
747 const_iterator(underlying_iterator ptr
) : m_cur(ptr
) {}
750 #undef WX_STR_ITERATOR_TAG
751 #undef WX_STR_ITERATOR_IMPL
753 friend class iterator
;
754 friend class const_iterator
;
756 template <typename T
>
757 class reverse_iterator_impl
760 typedef T iterator_type
;
762 typedef typename
T::iterator_category iterator_category
;
763 typedef typename
T::value_type value_type
;
764 typedef typename
T::difference_type difference_type
;
765 typedef typename
T::reference reference
;
766 typedef typename
T::pointer
*pointer
;
768 reverse_iterator_impl(iterator_type i
) : m_cur(i
) {}
769 reverse_iterator_impl(const reverse_iterator_impl
& ri
)
772 iterator_type
base() const { return m_cur
; }
774 reference
operator*() const { return *(m_cur
-1); }
775 reference
operator[](size_t n
) const { return *(*this + n
); }
777 reverse_iterator_impl
& operator++()
778 { --m_cur
; return *this; }
779 reverse_iterator_impl
operator++(int)
780 { reverse_iterator_impl tmp
= *this; --m_cur
; return tmp
; }
781 reverse_iterator_impl
& operator--()
782 { ++m_cur
; return *this; }
783 reverse_iterator_impl
operator--(int)
784 { reverse_iterator_impl tmp
= *this; ++m_cur
; return tmp
; }
786 // NB: explicit <T> in the functions below is to keep BCC 5.5 happy
787 reverse_iterator_impl
operator+(int n
) const
788 { return reverse_iterator_impl
<T
>(m_cur
- n
); }
789 reverse_iterator_impl
operator+(size_t n
) const
790 { return reverse_iterator_impl
<T
>(m_cur
- n
); }
791 reverse_iterator_impl
operator-(int n
) const
792 { return reverse_iterator_impl
<T
>(m_cur
+ n
); }
793 reverse_iterator_impl
operator-(size_t n
) const
794 { return reverse_iterator_impl
<T
>(m_cur
+ n
); }
795 reverse_iterator_impl
operator+=(int n
)
796 { m_cur
-= n
; return *this; }
797 reverse_iterator_impl
operator+=(size_t n
)
798 { m_cur
-= n
; return *this; }
799 reverse_iterator_impl
operator-=(int n
)
800 { m_cur
+= n
; return *this; }
801 reverse_iterator_impl
operator-=(size_t n
)
802 { m_cur
+= n
; return *this; }
804 unsigned operator-(const reverse_iterator_impl
& i
) const
805 { return i
.m_cur
- m_cur
; }
807 bool operator==(const reverse_iterator_impl
& ri
) const
808 { return m_cur
== ri
.m_cur
; }
809 bool operator!=(const reverse_iterator_impl
& ri
) const
810 { return !(*this == ri
); }
812 bool operator<(const reverse_iterator_impl
& i
) const
813 { return m_cur
> i
.m_cur
; }
814 bool operator>(const reverse_iterator_impl
& i
) const
815 { return m_cur
< i
.m_cur
; }
816 bool operator<=(const reverse_iterator_impl
& i
) const
817 { return m_cur
>= i
.m_cur
; }
818 bool operator>=(const reverse_iterator_impl
& i
) const
819 { return m_cur
<= i
.m_cur
; }
825 typedef reverse_iterator_impl
<iterator
> reverse_iterator
;
826 typedef reverse_iterator_impl
<const_iterator
> const_reverse_iterator
;
829 // used to transform an expression built using c_str() (and hence of type
830 // wxCStrData) to an iterator into the string
831 static const_iterator
CreateConstIterator(const wxCStrData
& data
)
833 return const_iterator(data
.m_str
->begin() + data
.m_offset
);
837 // constructors and destructor
838 // ctor for an empty string
842 // FIXME-UTF8: this one needs to do UTF-8 conversion in UTF-8 build!
843 wxString(const wxStringImpl
& stringSrc
) : m_impl(stringSrc
) { }
845 wxString(const wxString
& stringSrc
) : m_impl(stringSrc
.m_impl
) { }
847 // string containing nRepeat copies of ch
848 wxString(wxUniChar ch
, size_t nRepeat
= 1)
849 { assign(nRepeat
, ch
); }
850 wxString(size_t nRepeat
, wxUniChar ch
)
851 { assign(nRepeat
, ch
); }
852 wxString(wxUniCharRef ch
, size_t nRepeat
= 1)
853 { assign(nRepeat
, ch
); }
854 wxString(size_t nRepeat
, wxUniCharRef ch
)
855 { assign(nRepeat
, ch
); }
856 wxString(char ch
, size_t nRepeat
= 1)
857 { assign(nRepeat
, ch
); }
858 wxString(size_t nRepeat
, char ch
)
859 { assign(nRepeat
, ch
); }
860 wxString(wchar_t ch
, size_t nRepeat
= 1)
861 { assign(nRepeat
, ch
); }
862 wxString(size_t nRepeat
, wchar_t ch
)
863 { assign(nRepeat
, ch
); }
865 // ctors from char* strings:
866 wxString(const char *psz
)
867 : m_impl(ImplStr(psz
)) {}
868 wxString(const char *psz
, const wxMBConv
& conv
)
869 : m_impl(ImplStr(psz
, conv
)) {}
870 wxString(const char *psz
, size_t nLength
)
871 { assign(psz
, nLength
); }
872 wxString(const char *psz
, const wxMBConv
& conv
, size_t nLength
)
874 SubstrBufFromMB
str(ImplStr(psz
, nLength
, conv
));
875 m_impl
.assign(str
.data
, str
.len
);
878 // and unsigned char*:
879 wxString(const unsigned char *psz
)
880 : m_impl(ImplStr((const char*)psz
)) {}
881 wxString(const unsigned char *psz
, const wxMBConv
& conv
)
882 : m_impl(ImplStr((const char*)psz
, conv
)) {}
883 wxString(const unsigned char *psz
, size_t nLength
)
884 { assign((const char*)psz
, nLength
); }
885 wxString(const unsigned char *psz
, const wxMBConv
& conv
, size_t nLength
)
887 SubstrBufFromMB
str(ImplStr((const char*)psz
, nLength
, conv
));
888 m_impl
.assign(str
.data
, str
.len
);
891 // ctors from wchar_t* strings:
892 wxString(const wchar_t *pwz
)
893 : m_impl(ImplStr(pwz
)) {}
894 wxString(const wchar_t *pwz
, const wxMBConv
& WXUNUSED(conv
))
895 : m_impl(ImplStr(pwz
)) {}
896 wxString(const wchar_t *pwz
, size_t nLength
)
897 { assign(pwz
, nLength
); }
898 wxString(const wchar_t *pwz
, const wxMBConv
& WXUNUSED(conv
), size_t nLength
)
899 { assign(pwz
, nLength
); }
901 wxString(const wxCharBuffer
& buf
)
902 { assign(buf
.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
903 wxString(const wxWCharBuffer
& buf
)
904 { assign(buf
.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
906 wxString(const wxCStrData
& cstr
)
907 : m_impl(cstr
.AsString().m_impl
) { }
909 // as we provide both ctors with this signature for both char and unsigned
910 // char string, we need to provide one for wxCStrData to resolve ambiguity
911 wxString(const wxCStrData
& cstr
, size_t nLength
)
912 : m_impl(cstr
.AsString().Mid(0, nLength
).m_impl
) {}
914 // and because wxString is convertible to wxCStrData and const wxChar *
915 // we also need to provide this one
916 wxString(const wxString
& str
, size_t nLength
)
917 : m_impl(str
.Mid(0, nLength
).m_impl
) {}
919 // even if we're not built with wxUSE_STL == 1 it is very convenient to allow
920 // implicit conversions from std::string to wxString as this allows to use
921 // the same strings in non-GUI and GUI code, however we don't want to
922 // unconditionally add this ctor as it would make wx lib dependent on
923 // libstdc++ on some Linux versions which is bad, so instead we ask the
924 // client code to define this wxUSE_STD_STRING symbol if they need it
925 #if wxUSE_STD_STRING && !wxUSE_STL_BASED_WXSTRING
926 wxString(const wxStdString
& s
)
927 // FIXME-UTF8: this one needs to do UTF-8 conversion in UTF-8 build!
928 : m_impl(s
.c_str()) { } // FIXME-UTF8: this is broken for embedded 0s
929 #endif // wxUSE_STD_STRING && !wxUSE_STL_BASED_WXSTRING
932 // first valid index position
933 const_iterator
begin() const { return const_iterator(m_impl
.begin()); }
934 iterator
begin() { return iterator(this, m_impl
.begin()); }
935 // position one after the last valid one
936 const_iterator
end() const { return const_iterator(m_impl
.end()); }
937 iterator
end() { return iterator(this, m_impl
.end()); }
939 // first element of the reversed string
940 const_reverse_iterator
rbegin() const
941 { return const_reverse_iterator(end()); }
942 reverse_iterator
rbegin()
943 { return reverse_iterator(end()); }
944 // one beyond the end of the reversed string
945 const_reverse_iterator
rend() const
946 { return const_reverse_iterator(begin()); }
947 reverse_iterator
rend()
948 { return reverse_iterator(begin()); }
950 // std::string methods:
951 #if wxUSE_UNICODE_UTF8
952 size_t length() const { return end() - begin(); } // FIXME-UTF8: optimize!
954 size_t length() const { return m_impl
.length(); }
957 size_type
size() const { return length(); }
958 size_type
max_size() const { return npos
; }
960 bool empty() const { return m_impl
.empty(); }
962 size_type
capacity() const { return m_impl
.capacity(); } // FIXME-UTF8
963 void reserve(size_t sz
) { m_impl
.reserve(sz
); } // FIXME-UTF8
965 void resize(size_t nSize
, wxUniChar ch
= wxT('\0'))
967 #if wxUSE_UNICODE_UTF8
970 size_t len
= length();
973 else if ( nSize
< len
)
976 append(nSize
- len
, ch
);
980 m_impl
.resize(nSize
, (wxStringCharType
)ch
);
983 wxString
substr(size_t nStart
= 0, size_t nLen
= npos
) const
986 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
987 return m_impl
.substr(pos
, len
);
990 // generic attributes & operations
991 // as standard strlen()
992 size_t Len() const { return length(); }
993 // string contains any characters?
994 bool IsEmpty() const { return empty(); }
995 // empty string is "false", so !str will return true
996 bool operator!() const { return empty(); }
997 // truncate the string to given length
998 wxString
& Truncate(size_t uiLen
);
999 // empty string contents
1004 wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") );
1006 // empty the string and free memory
1009 wxString
tmp(wxEmptyString
);
1014 // Is an ascii value
1015 bool IsAscii() const;
1017 bool IsNumber() const;
1019 bool IsWord() const;
1021 // data access (all indexes are 0 based)
1023 wxUniChar
at(size_t n
) const
1024 { return *(begin() + n
); } // FIXME-UTF8: optimize?
1025 wxUniChar
GetChar(size_t n
) const
1027 // read/write access
1028 wxUniCharRef
at(size_t n
)
1029 { return *(begin() + n
); } // FIXME-UTF8: optimize?
1030 wxUniCharRef
GetWritableChar(size_t n
)
1033 void SetChar(size_t n
, wxUniChar ch
)
1036 // get last character
1037 wxUniChar
Last() const
1039 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1041 return at(length() - 1);
1044 // get writable last character
1047 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1048 return at(length() - 1);
1052 Note that we we must define all of the overloads below to avoid
1053 ambiguity when using str[0].
1055 wxUniChar
operator[](int n
) const
1057 wxUniChar
operator[](long n
) const
1059 wxUniChar
operator[](size_t n
) const
1061 #ifndef wxSIZE_T_IS_UINT
1062 wxUniChar
operator[](unsigned int n
) const
1064 #endif // size_t != unsigned int
1066 // operator versions of GetWriteableChar()
1067 wxUniCharRef
operator[](int n
)
1069 wxUniCharRef
operator[](long n
)
1071 wxUniCharRef
operator[](size_t n
)
1073 #ifndef wxSIZE_T_IS_UINT
1074 wxUniCharRef
operator[](unsigned int n
)
1076 #endif // size_t != unsigned int
1078 // explicit conversion to C string (use this with printf()!)
1079 wxCStrData
c_str() const { return wxCStrData(this); }
1080 wxCStrData
data() const { return c_str(); }
1082 // implicit conversion to C string
1083 operator wxCStrData() const { return c_str(); }
1084 operator const char*() const { return c_str(); }
1085 operator const wchar_t*() const { return c_str(); }
1087 // identical to c_str(), for MFC compatibility
1088 const wxCStrData
GetData() const { return c_str(); }
1090 // explicit conversion to C string in internal representation (char*,
1091 // wchar_t*, UTF-8-encoded char*, depending on the build):
1092 const wxStringCharType
*wx_str() const { return m_impl
.c_str(); }
1094 // conversion to *non-const* multibyte or widestring buffer; modifying
1095 // returned buffer won't affect the string, these methods are only useful
1096 // for passing values to const-incorrect functions
1097 wxWritableCharBuffer
char_str(const wxMBConv
& conv
= wxConvLibc
) const
1098 { return mb_str(conv
); }
1099 wxWritableWCharBuffer
wchar_str() const { return wc_str(); }
1101 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
1102 // converting numbers or strings which are certain not to contain special
1103 // chars (typically system functions, X atoms, environment variables etc.)
1105 // the behaviour of these functions with the strings containing anything
1106 // else than 7 bit ASCII characters is undefined, use at your own risk.
1108 static wxString
FromAscii(const char *ascii
); // string
1109 static wxString
FromAscii(const char ascii
); // char
1110 const wxCharBuffer
ToAscii() const;
1112 static wxString
FromAscii(const char *ascii
) { return wxString( ascii
); }
1113 static wxString
FromAscii(const char ascii
) { return wxString( ascii
); }
1114 const char *ToAscii() const { return c_str(); }
1115 #endif // Unicode/!Unicode
1117 // conversions with (possible) format conversions: have to return a
1118 // buffer with temporary data
1120 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
1121 // return an ANSI (multibyte) string, wc_str() to return a wide string and
1122 // fn_str() to return a string which should be used with the OS APIs
1123 // accepting the file names. The return value is always the same, but the
1124 // type differs because a function may either return pointer to the buffer
1125 // directly or have to use intermediate buffer for translation.
1127 const wxCharBuffer
mb_str(const wxMBConv
& conv
= wxConvLibc
) const;
1129 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
1131 #if wxUSE_UNICODE_WCHAR
1132 const wxChar
* wc_str() const { return wx_str(); }
1133 #elif wxUSE_UNICODE_UTF8
1134 const wxWCharBuffer
wc_str() const;
1136 // for compatibility with !wxUSE_UNICODE version
1137 const wxWX2WCbuf
wc_str(const wxMBConv
& WXUNUSED(conv
)) const
1138 { return wc_str(); }
1141 const wxCharBuffer
fn_str() const { return mb_str(wxConvFile
); }
1143 const wxWX2WCbuf
fn_str() const { return wc_str(); }
1144 #endif // wxMBFILES/!wxMBFILES
1147 const wxChar
* mb_str() const { return wx_str(); }
1149 // for compatibility with wxUSE_UNICODE version
1150 const wxChar
* mb_str(const wxMBConv
& WXUNUSED(conv
)) const { return wx_str(); }
1152 const wxWX2MBbuf
mbc_str() const { return mb_str(); }
1155 const wxWCharBuffer
wc_str(const wxMBConv
& conv
= wxConvLibc
) const;
1156 #endif // wxUSE_WCHAR_T
1158 const wxCharBuffer
fn_str() const { return wxConvFile
.cWC2WX( wc_str( wxConvLocal
) ); }
1160 const wxChar
* fn_str() const { return c_str(); }
1162 #endif // Unicode/ANSI
1164 // overloaded assignment
1165 // from another wxString
1166 wxString
& operator=(const wxString
& stringSrc
)
1167 { m_impl
= stringSrc
.m_impl
; return *this; }
1168 wxString
& operator=(const wxCStrData
& cstr
)
1169 { return *this = cstr
.AsString(); }
1171 wxString
& operator=(wxUniChar ch
)
1172 { m_impl
= EncodeChar(ch
); return *this; }
1173 wxString
& operator=(wxUniCharRef ch
)
1174 { return operator=((wxUniChar
)ch
); }
1175 wxString
& operator=(char ch
)
1176 { return operator=(wxUniChar(ch
)); }
1177 wxString
& operator=(unsigned char ch
)
1178 { return operator=(wxUniChar(ch
)); }
1179 wxString
& operator=(wchar_t ch
)
1180 { return operator=(wxUniChar(ch
)); }
1181 // from a C string - STL probably will crash on NULL,
1182 // so we need to compensate in that case
1183 #if wxUSE_STL_BASED_WXSTRING
1184 wxString
& operator=(const char *psz
)
1185 { if (psz
) m_impl
= ImplStr(psz
); else Clear(); return *this; }
1186 wxString
& operator=(const wchar_t *pwz
)
1187 { if (pwz
) m_impl
= ImplStr(pwz
); else Clear(); return *this; }
1189 wxString
& operator=(const char *psz
)
1190 { m_impl
= ImplStr(psz
); return *this; }
1191 wxString
& operator=(const wchar_t *pwz
)
1192 { m_impl
= ImplStr(pwz
); return *this; }
1194 wxString
& operator=(const unsigned char *psz
)
1195 { return operator=((const char*)psz
); }
1197 // from wxWCharBuffer
1198 wxString
& operator=(const wxWCharBuffer
& s
)
1199 { return operator=(s
.data()); } // FIXME-UTF8: fix for embedded NULs
1200 // from wxCharBuffer
1201 wxString
& operator=(const wxCharBuffer
& s
)
1202 { return operator=(s
.data()); } // FIXME-UTF8: fix for embedded NULs
1204 // string concatenation
1205 // in place concatenation
1207 Concatenate and return the result. Note that the left to right
1208 associativity of << allows to write things like "str << str1 << str2
1209 << ..." (unlike with +=)
1212 wxString
& operator<<(const wxString
& s
)
1214 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1215 wxASSERT_MSG( s
.IsValid(),
1216 _T("did you forget to call UngetWriteBuf()?") );
1222 // string += C string
1223 wxString
& operator<<(const char *psz
)
1224 { append(psz
); return *this; }
1225 wxString
& operator<<(const wchar_t *pwz
)
1226 { append(pwz
); return *this; }
1227 wxString
& operator<<(const wxCStrData
& psz
)
1228 { append(psz
.AsString()); return *this; }
1230 wxString
& operator<<(wxUniChar ch
) { append(1, ch
); return *this; }
1231 wxString
& operator<<(wxUniCharRef ch
) { append(1, ch
); return *this; }
1232 wxString
& operator<<(char ch
) { append(1, ch
); return *this; }
1233 wxString
& operator<<(unsigned char ch
) { append(1, ch
); return *this; }
1234 wxString
& operator<<(wchar_t ch
) { append(1, ch
); return *this; }
1236 // string += buffer (i.e. from wxGetString)
1237 wxString
& operator<<(const wxWCharBuffer
& s
)
1238 { return operator<<((const wchar_t *)s
); }
1239 wxString
& operator+=(const wxWCharBuffer
& s
)
1240 { return operator<<((const wchar_t *)s
); }
1242 wxString
& operator<<(const wxCharBuffer
& s
)
1243 { return operator<<((const char *)s
); }
1244 wxString
& operator+=(const wxCharBuffer
& s
)
1245 { return operator<<((const char *)s
); }
1247 // string += C string
1248 wxString
& Append(const wxString
& s
)
1250 // test for empty() to share the string if possible
1257 wxString
& Append(const wxCStrData
& psz
)
1258 { append(psz
); return *this; }
1259 wxString
& Append(const char* psz
)
1260 { append(psz
); return *this; }
1261 wxString
& Append(const wchar_t* pwz
)
1262 { append(pwz
); return *this; }
1263 // append count copies of given character
1264 wxString
& Append(wxUniChar ch
, size_t count
= 1u)
1265 { append(count
, ch
); return *this; }
1266 wxString
& Append(wxUniCharRef ch
, size_t count
= 1u)
1267 { append(count
, ch
); return *this; }
1268 wxString
& Append(char ch
, size_t count
= 1u)
1269 { append(count
, ch
); return *this; }
1270 wxString
& Append(unsigned char ch
, size_t count
= 1u)
1271 { append(count
, ch
); return *this; }
1272 wxString
& Append(wchar_t ch
, size_t count
= 1u)
1273 { append(count
, ch
); return *this; }
1274 wxString
& Append(const char* psz
, size_t nLen
)
1275 { append(psz
, nLen
); return *this; }
1276 wxString
& Append(const wchar_t* pwz
, size_t nLen
)
1277 { append(pwz
, nLen
); return *this; }
1279 // prepend a string, return the string itself
1280 wxString
& Prepend(const wxString
& str
)
1281 { *this = str
+ *this; return *this; }
1283 // non-destructive concatenation
1285 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
,
1286 const wxString
& string2
);
1287 // string with a single char
1288 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
1289 // char with a string
1290 friend wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
1291 // string with C string
1292 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
1294 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
1295 const wchar_t *pwz
);
1296 // C string with string
1297 friend wxString WXDLLIMPEXP_BASE
operator+(const char *psz
,
1298 const wxString
& string
);
1299 friend wxString WXDLLIMPEXP_BASE
operator+(const wchar_t *pwz
,
1300 const wxString
& string
);
1302 // stream-like functions
1303 // insert an int into string
1304 wxString
& operator<<(int i
)
1305 { return (*this) << Format(_T("%d"), i
); }
1306 // insert an unsigned int into string
1307 wxString
& operator<<(unsigned int ui
)
1308 { return (*this) << Format(_T("%u"), ui
); }
1309 // insert a long into string
1310 wxString
& operator<<(long l
)
1311 { return (*this) << Format(_T("%ld"), l
); }
1312 // insert an unsigned long into string
1313 wxString
& operator<<(unsigned long ul
)
1314 { return (*this) << Format(_T("%lu"), ul
); }
1315 #if defined wxLongLong_t && !defined wxLongLongIsLong
1316 // insert a long long if they exist and aren't longs
1317 wxString
& operator<<(wxLongLong_t ll
)
1319 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("d");
1320 return (*this) << Format(fmt
, ll
);
1322 // insert an unsigned long long
1323 wxString
& operator<<(wxULongLong_t ull
)
1325 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("u");
1326 return (*this) << Format(fmt
, ull
);
1329 // insert a float into string
1330 wxString
& operator<<(float f
)
1331 { return (*this) << Format(_T("%f"), f
); }
1332 // insert a double into string
1333 wxString
& operator<<(double d
)
1334 { return (*this) << Format(_T("%g"), d
); }
1336 // string comparison
1337 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
1338 int Cmp(const char *psz
) const
1339 { return compare(psz
); }
1340 int Cmp(const wchar_t *pwz
) const
1341 { return compare(pwz
); }
1342 int Cmp(const wxString
& s
) const
1343 { return compare(s
); }
1344 // same as Cmp() but not case-sensitive
1345 int CmpNoCase(const wxString
& s
) const;
1346 int CmpNoCase(const char *psz
) const
1347 { return CmpNoCase(wxString(psz
)); }
1348 int CmpNoCase(const wchar_t *pwz
) const
1349 { return CmpNoCase(wxString(pwz
)); }
1350 // test for the string equality, either considering case or not
1351 // (if compareWithCase then the case matters)
1352 bool IsSameAs(const wxString
& str
, bool compareWithCase
= true) const
1353 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
1354 bool IsSameAs(const char *str
, bool compareWithCase
= true) const
1355 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
1356 bool IsSameAs(const wchar_t *str
, bool compareWithCase
= true) const
1357 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
1358 // comparison with a single character: returns true if equal
1359 bool IsSameAs(wxUniChar c
, bool compareWithCase
= true) const
1361 return (length() == 1) && (compareWithCase
? GetChar(0u) == c
1362 : wxToupper(GetChar(0u)) == wxToupper(c
));
1364 // FIXME-UTF8: remove these overloads
1365 bool IsSameAs(wxUniCharRef c
, bool compareWithCase
= true) const
1366 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1367 bool IsSameAs(char c
, bool compareWithCase
= true) const
1368 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1369 bool IsSameAs(unsigned char c
, bool compareWithCase
= true) const
1370 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1371 bool IsSameAs(wchar_t c
, bool compareWithCase
= true) const
1372 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1373 bool IsSameAs(int c
, bool compareWithCase
= true) const
1374 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1376 // simple sub-string extraction
1377 // return substring starting at nFirst of length nCount (or till the end
1378 // if nCount = default value)
1379 wxString
Mid(size_t nFirst
, size_t nCount
= npos
) const;
1381 // operator version of Mid()
1382 wxString
operator()(size_t start
, size_t len
) const
1383 { return Mid(start
, len
); }
1385 // check if the string starts with the given prefix and return the rest
1386 // of the string in the provided pointer if it is not NULL; otherwise
1388 bool StartsWith(const wxChar
*prefix
, wxString
*rest
= NULL
) const;
1389 // check if the string ends with the given suffix and return the
1390 // beginning of the string before the suffix in the provided pointer if
1391 // it is not NULL; otherwise return false
1392 bool EndsWith(const wxChar
*suffix
, wxString
*rest
= NULL
) const;
1394 // get first nCount characters
1395 wxString
Left(size_t nCount
) const;
1396 // get last nCount characters
1397 wxString
Right(size_t nCount
) const;
1398 // get all characters before the first occurance of ch
1399 // (returns the whole string if ch not found)
1400 wxString
BeforeFirst(wxUniChar ch
) const;
1401 // get all characters before the last occurence of ch
1402 // (returns empty string if ch not found)
1403 wxString
BeforeLast(wxUniChar ch
) const;
1404 // get all characters after the first occurence of ch
1405 // (returns empty string if ch not found)
1406 wxString
AfterFirst(wxUniChar ch
) const;
1407 // get all characters after the last occurence of ch
1408 // (returns the whole string if ch not found)
1409 wxString
AfterLast(wxUniChar ch
) const;
1411 // for compatibility only, use more explicitly named functions above
1412 wxString
Before(wxUniChar ch
) const { return BeforeLast(ch
); }
1413 wxString
After(wxUniChar ch
) const { return AfterFirst(ch
); }
1416 // convert to upper case in place, return the string itself
1417 wxString
& MakeUpper();
1418 // convert to upper case, return the copy of the string
1419 // Here's something to remember: BC++ doesn't like returns in inlines.
1420 wxString
Upper() const ;
1421 // convert to lower case in place, return the string itself
1422 wxString
& MakeLower();
1423 // convert to lower case, return the copy of the string
1424 wxString
Lower() const ;
1426 // trimming/padding whitespace (either side) and truncating
1427 // remove spaces from left or from right (default) side
1428 wxString
& Trim(bool bFromRight
= true);
1429 // add nCount copies chPad in the beginning or at the end (default)
1430 wxString
& Pad(size_t nCount
, wxUniChar chPad
= wxT(' '), bool bFromRight
= true);
1432 // searching and replacing
1433 // searching (return starting index, or -1 if not found)
1434 int Find(wxUniChar ch
, bool bFromEnd
= false) const; // like strchr/strrchr
1435 int Find(wxUniCharRef ch
, bool bFromEnd
= false) const
1436 { return Find(wxUniChar(ch
), bFromEnd
); }
1437 int Find(char ch
, bool bFromEnd
= false) const
1438 { return Find(wxUniChar(ch
), bFromEnd
); }
1439 int Find(unsigned char ch
, bool bFromEnd
= false) const
1440 { return Find(wxUniChar(ch
), bFromEnd
); }
1441 int Find(wchar_t ch
, bool bFromEnd
= false) const
1442 { return Find(wxUniChar(ch
), bFromEnd
); }
1443 // searching (return starting index, or -1 if not found)
1444 // FIXME-UTF8: keep wxString overload only
1445 int Find(const wxString
& sub
) const // like strstr
1447 size_type idx
= find(sub
);
1448 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
1450 int Find(const char *sub
) const // like strstr
1452 size_type idx
= find(sub
);
1453 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
1455 int Find(const wchar_t *sub
) const // like strstr
1457 size_type idx
= find(sub
);
1458 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
1461 // replace first (or all of bReplaceAll) occurences of substring with
1462 // another string, returns the number of replacements made
1463 size_t Replace(const wxString
& strOld
,
1464 const wxString
& strNew
,
1465 bool bReplaceAll
= true);
1467 // check if the string contents matches a mask containing '*' and '?'
1468 bool Matches(const wxString
& mask
) const;
1470 // conversion to numbers: all functions return true only if the whole
1471 // string is a number and put the value of this number into the pointer
1472 // provided, the base is the numeric base in which the conversion should be
1473 // done and must be comprised between 2 and 36 or be 0 in which case the
1474 // standard C rules apply (leading '0' => octal, "0x" => hex)
1475 // convert to a signed integer
1476 bool ToLong(long *val
, int base
= 10) const;
1477 // convert to an unsigned integer
1478 bool ToULong(unsigned long *val
, int base
= 10) const;
1479 // convert to wxLongLong
1480 #if defined(wxLongLong_t)
1481 bool ToLongLong(wxLongLong_t
*val
, int base
= 10) const;
1482 // convert to wxULongLong
1483 bool ToULongLong(wxULongLong_t
*val
, int base
= 10) const;
1484 #endif // wxLongLong_t
1485 // convert to a double
1486 bool ToDouble(double *val
) const;
1489 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1490 // formatted input/output
1491 // as sprintf(), returns the number of characters written or < 0 on error
1492 // (take 'this' into account in attribute parameter count)
1493 // int Printf(const wxChar *pszFormat, ...);
1494 WX_DEFINE_VARARG_FUNC(int, Printf
, DoPrintf
)
1495 #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN
1496 // as vprintf(), returns the number of characters written or < 0 on error
1497 int PrintfV(const wxString
& format
, va_list argptr
);
1499 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1500 // returns the string containing the result of Printf() to it
1501 // static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1;
1502 WX_DEFINE_VARARG_FUNC(static wxString
, Format
, DoFormat
)
1504 // the same as above, but takes a va_list
1505 static wxString
FormatV(const wxString
& format
, va_list argptr
);
1507 // raw access to string memory
1508 // ensure that string has space for at least nLen characters
1509 // only works if the data of this string is not shared
1510 bool Alloc(size_t nLen
) { reserve(nLen
); /*return capacity() >= nLen;*/ return true; }
1511 // minimize the string's memory
1512 // only works if the data of this string is not shared
1514 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1515 // These are deprecated, use wxStringBuffer or wxStringBufferLength instead
1517 // get writable buffer of at least nLen bytes. Unget() *must* be called
1518 // a.s.a.p. to put string back in a reasonable state!
1519 wxDEPRECATED( wxChar
*GetWriteBuf(size_t nLen
) );
1520 // call this immediately after GetWriteBuf() has been used
1521 wxDEPRECATED( void UngetWriteBuf() );
1522 wxDEPRECATED( void UngetWriteBuf(size_t nLen
) );
1523 #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && wxUSE_UNICODE_UTF8
1525 // wxWidgets version 1 compatibility functions
1528 wxString
SubString(size_t from
, size_t to
) const
1529 { return Mid(from
, (to
- from
+ 1)); }
1530 // values for second parameter of CompareTo function
1531 enum caseCompare
{exact
, ignoreCase
};
1532 // values for first parameter of Strip function
1533 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
1535 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1537 // (take 'this' into account in attribute parameter count)
1538 // int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2;
1539 WX_DEFINE_VARARG_FUNC(int, sprintf
, DoPrintf
)
1540 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
1543 inline int CompareTo(const wxChar
* psz
, caseCompare cmp
= exact
) const
1544 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
1547 size_t Length() const { return length(); }
1548 // Count the number of characters
1549 int Freq(wxUniChar ch
) const;
1551 void LowerCase() { MakeLower(); }
1553 void UpperCase() { MakeUpper(); }
1554 // use Trim except that it doesn't change this string
1555 wxString
Strip(stripType w
= trailing
) const;
1557 // use Find (more general variants not yet supported)
1558 size_t Index(const wxChar
* psz
) const { return Find(psz
); }
1559 size_t Index(wxUniChar ch
) const { return Find(ch
); }
1561 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
1562 wxString
& RemoveLast(size_t n
= 1) { return Truncate(length() - n
); }
1564 wxString
& Remove(size_t nStart
, size_t nLen
)
1565 { return (wxString
&)erase( nStart
, nLen
); }
1568 int First( wxUniChar ch
) const { return Find(ch
); }
1569 int First( wxUniCharRef ch
) const { return Find(ch
); }
1570 int First( char ch
) const { return Find(ch
); }
1571 int First( unsigned char ch
) const { return Find(ch
); }
1572 int First( wchar_t ch
) const { return Find(ch
); }
1573 int First( const wxChar
* psz
) const { return Find(psz
); }
1574 int First( const wxString
& str
) const { return Find(str
); }
1575 int Last( wxUniChar ch
) const { return Find(ch
, true); }
1576 bool Contains(const wxString
& str
) const { return Find(str
) != wxNOT_FOUND
; }
1579 bool IsNull() const { return empty(); }
1581 // std::string compatibility functions
1583 // take nLen chars starting at nPos
1584 wxString(const wxString
& str
, size_t nPos
, size_t nLen
)
1585 : m_impl(str
.m_impl
, nPos
, nLen
) { }
1586 // take all characters from first to last
1587 wxString(const_iterator first
, const_iterator last
)
1588 : m_impl(first
.impl(), last
.impl()) { }
1589 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1590 // the 2 overloads below are for compatibility with the existing code using
1591 // pointers instead of iterators
1592 wxString(const char *first
, const char *last
)
1594 SubstrBufFromMB
str(ImplStr(first
, last
- first
));
1595 m_impl
.assign(str
.data
, str
.len
);
1597 wxString(const wchar_t *first
, const wchar_t *last
)
1599 SubstrBufFromWC
str(ImplStr(first
, last
- first
));
1600 m_impl
.assign(str
.data
, str
.len
);
1602 // and this one is needed to compile code adding offsets to c_str() result
1603 wxString(const wxCStrData
& first
, const wxCStrData
& last
)
1604 : m_impl(CreateConstIterator(first
).impl(),
1605 CreateConstIterator(last
).impl())
1607 wxASSERT_MSG( first
.m_str
== last
.m_str
,
1608 _T("pointers must be into the same string") );
1610 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1612 // lib.string.modifiers
1613 // append elements str[pos], ..., str[pos+n]
1614 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
1617 str
.PosLenToImpl(pos
, n
, &from
, &len
);
1618 m_impl
.append(str
.m_impl
, from
, len
);
1622 wxString
& append(const wxString
& str
)
1623 { m_impl
.append(str
.m_impl
); return *this; }
1624 wxString
& append(const wxCStrData
& str
)
1625 { m_impl
.append(str
.AsString().m_impl
); return *this; }
1626 // append first n (or all if n == npos) characters of sz
1627 wxString
& append(const char *sz
)
1628 { m_impl
.append(ImplStr(sz
)); return *this; }
1629 wxString
& append(const wchar_t *sz
)
1630 { m_impl
.append(ImplStr(sz
)); return *this; }
1631 wxString
& append(const char *sz
, size_t n
)
1633 SubstrBufFromMB
str(ImplStr(sz
, n
));
1634 m_impl
.append(str
.data
, str
.len
);
1637 wxString
& append(const wchar_t *sz
, size_t n
)
1639 SubstrBufFromWC
str(ImplStr(sz
, n
));
1640 m_impl
.append(str
.data
, str
.len
);
1643 // append n copies of ch
1644 wxString
& append(size_t n
, wxUniChar ch
)
1646 #if wxUSE_UNICODE_UTF8
1647 if ( !ch
.IsAscii() )
1648 m_impl
.append(EncodeNChars(n
, ch
));
1651 m_impl
.append(n
, (wxStringCharType
)ch
);
1654 // append from first to last
1655 wxString
& append(const_iterator first
, const_iterator last
)
1656 { m_impl
.append(first
.impl(), last
.impl()); return *this; }
1657 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1658 wxString
& append(const char *first
, const char *last
)
1659 { return append(first
, last
- first
); }
1660 wxString
& append(const wchar_t *first
, const wchar_t *last
)
1661 { return append(first
, last
- first
); }
1662 wxString
& append(const wxCStrData
& first
, const wxCStrData
& last
)
1663 { return append(CreateConstIterator(first
), CreateConstIterator(last
)); }
1664 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1666 // same as `this_string = str'
1667 wxString
& assign(const wxString
& str
)
1668 { m_impl
= str
.m_impl
; return *this; }
1669 wxString
& assign(const wxString
& str
, size_t len
)
1671 m_impl
.assign(str
.m_impl
, 0, str
.LenToImpl(len
));
1674 // same as ` = str[pos..pos + n]
1675 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
1678 str
.PosLenToImpl(pos
, n
, &from
, &len
);
1679 m_impl
.assign(str
.m_impl
, from
, len
);
1682 // same as `= first n (or all if n == npos) characters of sz'
1683 wxString
& assign(const char *sz
)
1684 { m_impl
.assign(ImplStr(sz
)); return *this; }
1685 wxString
& assign(const wchar_t *sz
)
1686 { m_impl
.assign(ImplStr(sz
)); return *this; }
1687 wxString
& assign(const char *sz
, size_t n
)
1689 SubstrBufFromMB
str(ImplStr(sz
, n
));
1690 m_impl
.assign(str
.data
, str
.len
);
1693 wxString
& assign(const wchar_t *sz
, size_t n
)
1695 SubstrBufFromWC
str(ImplStr(sz
, n
));
1696 m_impl
.assign(str
.data
, str
.len
);
1699 // same as `= n copies of ch'
1700 wxString
& assign(size_t n
, wxUniChar ch
)
1702 #if wxUSE_UNICODE_UTF8
1703 if ( !ch
.IsAscii() )
1704 m_impl
.assign(EncodeNChars(n
, ch
));
1707 m_impl
.assign(n
, (wxStringCharType
)ch
);
1711 wxString
& assign(size_t n
, wxUniCharRef ch
)
1712 { return assign(n
, wxUniChar(ch
)); }
1713 wxString
& assign(size_t n
, char ch
)
1714 { return assign(n
, wxUniChar(ch
)); }
1715 wxString
& assign(size_t n
, unsigned char ch
)
1716 { return assign(n
, wxUniChar(ch
)); }
1717 wxString
& assign(size_t n
, wchar_t ch
)
1718 { return assign(n
, wxUniChar(ch
)); }
1720 // assign from first to last
1721 wxString
& assign(const_iterator first
, const_iterator last
)
1722 { m_impl
.assign(first
.impl(), last
.impl()); return *this; }
1723 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1724 wxString
& assign(const char *first
, const char *last
)
1725 { return assign(first
, last
- first
); }
1726 wxString
& assign(const wchar_t *first
, const wchar_t *last
)
1727 { return assign(first
, last
- first
); }
1728 wxString
& assign(const wxCStrData
& first
, const wxCStrData
& last
)
1729 { return assign(CreateConstIterator(first
), CreateConstIterator(last
)); }
1730 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1732 // string comparison
1733 int compare(const wxString
& str
) const;
1734 // comparison with a substring
1735 int compare(size_t nStart
, size_t nLen
, const wxString
& str
) const;
1736 // comparison of 2 substrings
1737 int compare(size_t nStart
, size_t nLen
,
1738 const wxString
& str
, size_t nStart2
, size_t nLen2
) const;
1739 // just like strcmp()
1740 int compare(const char* sz
) const;
1741 int compare(const wchar_t* sz
) const;
1742 // substring comparison with first nCount characters of sz
1743 int compare(size_t nStart
, size_t nLen
,
1744 const char* sz
, size_t nCount
= npos
) const;
1745 int compare(size_t nStart
, size_t nLen
,
1746 const wchar_t* sz
, size_t nCount
= npos
) const;
1748 // insert another string
1749 wxString
& insert(size_t nPos
, const wxString
& str
)
1750 { insert(begin() + nPos
, str
.begin(), str
.end()); return *this; }
1751 // insert n chars of str starting at nStart (in str)
1752 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
1755 str
.PosLenToImpl(nStart
, n
, &from
, &len
);
1756 m_impl
.insert(PosToImpl(nPos
), str
.m_impl
, from
, len
);
1759 // insert first n (or all if n == npos) characters of sz
1760 wxString
& insert(size_t nPos
, const char *sz
)
1761 { m_impl
.insert(PosToImpl(nPos
), ImplStr(sz
)); return *this; }
1762 wxString
& insert(size_t nPos
, const wchar_t *sz
)
1763 { m_impl
.insert(PosToImpl(nPos
), ImplStr(sz
)); return *this; }
1764 wxString
& insert(size_t nPos
, const char *sz
, size_t n
)
1766 SubstrBufFromMB
str(ImplStr(sz
, n
));
1767 m_impl
.insert(PosToImpl(nPos
), str
.data
, str
.len
);
1770 wxString
& insert(size_t nPos
, const wchar_t *sz
, size_t n
)
1772 SubstrBufFromWC
str(ImplStr(sz
, n
));
1773 m_impl
.insert(PosToImpl(nPos
), str
.data
, str
.len
);
1776 // insert n copies of ch
1777 wxString
& insert(size_t nPos
, size_t n
, wxUniChar ch
)
1779 #if wxUSE_UNICODE_UTF8
1780 if ( !ch
.IsAscii() )
1781 m_impl
.insert(PosToImpl(nPos
), EncodeNChars(n
, ch
));
1784 m_impl
.insert(PosToImpl(nPos
), n
, (wxStringCharType
)ch
);
1787 iterator
insert(iterator it
, wxUniChar ch
)
1789 #if wxUSE_UNICODE_UTF8
1790 if ( !ch
.IsAscii() )
1792 size_t pos
= IterToImplPos(it
);
1793 m_impl
.insert(pos
, EncodeChar(ch
));
1794 return iterator(this, m_impl
.begin() + pos
);
1798 return iterator(this, m_impl
.insert(it
.impl(), (wxStringCharType
)ch
));
1800 void insert(iterator it
, const_iterator first
, const_iterator last
)
1801 { m_impl
.insert(it
.impl(), first
.impl(), last
.impl()); }
1802 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1803 void insert(iterator it
, const char *first
, const char *last
)
1804 { insert(it
- begin(), first
, last
- first
); }
1805 void insert(iterator it
, const wchar_t *first
, const wchar_t *last
)
1806 { insert(it
- begin(), first
, last
- first
); }
1807 void insert(iterator it
, const wxCStrData
& first
, const wxCStrData
& last
)
1808 { insert(it
, CreateConstIterator(first
), CreateConstIterator(last
)); }
1809 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1811 void insert(iterator it
, size_type n
, wxUniChar ch
)
1813 #if wxUSE_UNICODE_UTF8
1814 if ( !ch
.IsAscii() )
1815 m_impl
.insert(IterToImplPos(it
), EncodeNChars(n
, ch
));
1818 m_impl
.insert(it
.impl(), n
, (wxStringCharType
)ch
);
1821 // delete characters from nStart to nStart + nLen
1822 wxString
& erase(size_type pos
= 0, size_type n
= npos
)
1825 PosLenToImpl(pos
, n
, &from
, &len
);
1826 m_impl
.erase(from
, len
);
1829 // delete characters from first up to last
1830 iterator
erase(iterator first
, iterator last
)
1831 { return iterator(this, m_impl
.erase(first
.impl(), last
.impl())); }
1832 iterator
erase(iterator first
)
1833 { return iterator(this, m_impl
.erase(first
.impl())); }
1835 #ifdef wxSTRING_BASE_HASNT_CLEAR
1836 void clear() { erase(); }
1838 void clear() { m_impl
.clear(); }
1841 // replaces the substring of length nLen starting at nStart
1842 wxString
& replace(size_t nStart
, size_t nLen
, const char* sz
)
1845 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1846 m_impl
.replace(from
, len
, ImplStr(sz
));
1849 wxString
& replace(size_t nStart
, size_t nLen
, const wchar_t* sz
)
1852 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1853 m_impl
.replace(from
, len
, ImplStr(sz
));
1856 // replaces the substring of length nLen starting at nStart
1857 wxString
& replace(size_t nStart
, size_t nLen
, const wxString
& str
)
1860 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1861 m_impl
.replace(from
, len
, str
.m_impl
);
1864 // replaces the substring with nCount copies of ch
1865 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxUniChar ch
)
1868 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1869 #if wxUSE_UNICODE_UTF8
1870 if ( !ch
.IsAscii() )
1871 m_impl
.replace(from
, len
, EncodeNChars(nCount
, ch
));
1874 m_impl
.replace(from
, len
, nCount
, (wxStringCharType
)ch
);
1877 // replaces a substring with another substring
1878 wxString
& replace(size_t nStart
, size_t nLen
,
1879 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1882 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1885 str
.PosLenToImpl(nStart2
, nLen2
, &from2
, &len2
);
1887 m_impl
.replace(from
, len
, str
.m_impl
, from2
, len2
);
1890 // replaces the substring with first nCount chars of sz
1891 wxString
& replace(size_t nStart
, size_t nLen
,
1892 const char* sz
, size_t nCount
)
1895 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1897 SubstrBufFromMB
str(ImplStr(sz
, nCount
));
1899 m_impl
.replace(from
, len
, str
.data
, str
.len
);
1902 wxString
& replace(size_t nStart
, size_t nLen
,
1903 const wchar_t* sz
, size_t nCount
)
1906 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1908 SubstrBufFromWC
str(ImplStr(sz
, nCount
));
1910 m_impl
.replace(from
, len
, str
.data
, str
.len
);
1913 wxString
& replace(size_t nStart
, size_t nLen
,
1914 const wxString
& s
, size_t nCount
)
1917 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1918 m_impl
.replace(from
, len
, s
.m_impl
.c_str(), s
.LenToImpl(nCount
));
1922 wxString
& replace(iterator first
, iterator last
, const char* s
)
1923 { m_impl
.replace(first
.impl(), last
.impl(), ImplStr(s
)); return *this; }
1924 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
)
1925 { m_impl
.replace(first
.impl(), last
.impl(), ImplStr(s
)); return *this; }
1926 wxString
& replace(iterator first
, iterator last
, const char* s
, size_type n
)
1928 SubstrBufFromMB
str(ImplStr(s
, n
));
1929 m_impl
.replace(first
.impl(), last
.impl(), str
.data
, str
.len
);
1932 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
, size_type n
)
1934 SubstrBufFromWC
str(ImplStr(s
, n
));
1935 m_impl
.replace(first
.impl(), last
.impl(), str
.data
, str
.len
);
1938 wxString
& replace(iterator first
, iterator last
, const wxString
& s
)
1939 { m_impl
.replace(first
.impl(), last
.impl(), s
.m_impl
); return *this; }
1940 wxString
& replace(iterator first
, iterator last
, size_type n
, wxUniChar ch
)
1942 #if wxUSE_UNICODE_UTF8
1943 if ( !ch
.IsAscii() )
1944 m_impl
.replace(first
.impl(), last
.impl(), EncodeNChars(n
, ch
));
1947 m_impl
.replace(first
.impl(), last
.impl(), n
, (wxStringCharType
)ch
);
1950 wxString
& replace(iterator first
, iterator last
,
1951 const_iterator first1
, const_iterator last1
)
1953 m_impl
.replace(first
.impl(), last
.impl(), first1
.impl(), last1
.impl());
1956 wxString
& replace(iterator first
, iterator last
,
1957 const char *first1
, const char *last1
)
1958 { replace(first
, last
, first1
, last1
- first1
); return *this; }
1959 wxString
& replace(iterator first
, iterator last
,
1960 const wchar_t *first1
, const wchar_t *last1
)
1961 { replace(first
, last
, first1
, last1
- first1
); return *this; }
1964 void swap(wxString
& str
)
1965 { m_impl
.swap(str
.m_impl
); }
1968 size_t find(const wxString
& str
, size_t nStart
= 0) const
1969 { return PosFromImpl(m_impl
.find(str
.m_impl
, PosToImpl(nStart
))); }
1971 // find first n characters of sz
1972 size_t find(const char* sz
, size_t nStart
= 0, size_t n
= npos
) const
1974 SubstrBufFromMB
str(ImplStr(sz
, n
));
1975 return PosFromImpl(m_impl
.find(str
.data
, PosToImpl(nStart
), str
.len
));
1977 size_t find(const wchar_t* sz
, size_t nStart
= 0, size_t n
= npos
) const
1979 SubstrBufFromWC
str(ImplStr(sz
, n
));
1980 return PosFromImpl(m_impl
.find(str
.data
, PosToImpl(nStart
), str
.len
));
1983 // find the first occurence of character ch after nStart
1984 size_t find(wxUniChar ch
, size_t nStart
= 0) const
1985 { return PosFromImpl(m_impl
.find(EncodeChar(ch
), PosToImpl(nStart
))); }
1986 size_t find(wxUniCharRef ch
, size_t nStart
= 0) const
1987 { return find(wxUniChar(ch
), nStart
); }
1988 size_t find(char ch
, size_t nStart
= 0) const
1989 { return find(wxUniChar(ch
), nStart
); }
1990 size_t find(unsigned char ch
, size_t nStart
= 0) const
1991 { return find(wxUniChar(ch
), nStart
); }
1992 size_t find(wchar_t ch
, size_t nStart
= 0) const
1993 { return find(wxUniChar(ch
), nStart
); }
1995 // rfind() family is exactly like find() but works right to left
1997 // as find, but from the end
1998 size_t rfind(const wxString
& str
, size_t nStart
= npos
) const
1999 { return PosFromImpl(m_impl
.rfind(str
.m_impl
, PosToImpl(nStart
))); }
2001 // as find, but from the end
2002 size_t rfind(const char* sz
, size_t nStart
= npos
, size_t n
= npos
) const
2004 SubstrBufFromMB
str(ImplStr(sz
, n
));
2005 return PosFromImpl(m_impl
.rfind(str
.data
, PosToImpl(nStart
), str
.len
));
2007 size_t rfind(const wchar_t* sz
, size_t nStart
= npos
, size_t n
= npos
) const
2009 SubstrBufFromWC
str(ImplStr(sz
, n
));
2010 return PosFromImpl(m_impl
.rfind(str
.data
, PosToImpl(nStart
), str
.len
));
2012 // as find, but from the end
2013 size_t rfind(wxUniChar ch
, size_t nStart
= npos
) const
2014 { return PosFromImpl(m_impl
.rfind(EncodeChar(ch
), PosToImpl(nStart
))); }
2015 size_t rfind(wxUniCharRef ch
, size_t nStart
= npos
) const
2016 { return rfind(wxUniChar(ch
), nStart
); }
2017 size_t rfind(char ch
, size_t nStart
= npos
) const
2018 { return rfind(wxUniChar(ch
), nStart
); }
2019 size_t rfind(unsigned char ch
, size_t nStart
= npos
) const
2020 { return rfind(wxUniChar(ch
), nStart
); }
2021 size_t rfind(wchar_t ch
, size_t nStart
= npos
) const
2022 { return rfind(wxUniChar(ch
), nStart
); }
2024 // find first/last occurence of any character (not) in the set:
2025 #if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2026 // FIXME-UTF8: this is not entirely correct, because it doesn't work if
2027 // sizeof(wchar_t)==2 and surrogates are present in the string;
2028 // should we care? Probably not.
2029 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
2030 { return m_impl
.find_first_of(str
.m_impl
, nStart
); }
2031 size_t find_first_of(const char* sz
, size_t nStart
= 0) const
2032 { return m_impl
.find_first_of(ImplStr(sz
), nStart
); }
2033 size_t find_first_of(const wchar_t* sz
, size_t nStart
= 0) const
2034 { return m_impl
.find_first_of(ImplStr(sz
), nStart
); }
2035 size_t find_first_of(const char* sz
, size_t nStart
, size_t n
) const
2036 { return m_impl
.find_first_of(ImplStr(sz
), nStart
, n
); }
2037 size_t find_first_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
2038 { return m_impl
.find_first_of(ImplStr(sz
), nStart
, n
); }
2039 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const
2040 { return m_impl
.find_first_of((wxChar
)c
, nStart
); }
2042 size_t find_last_of(const wxString
& str
, size_t nStart
= npos
) const
2043 { return m_impl
.find_last_of(str
.m_impl
, nStart
); }
2044 size_t find_last_of(const char* sz
, size_t nStart
= npos
) const
2045 { return m_impl
.find_last_of(ImplStr(sz
), nStart
); }
2046 size_t find_last_of(const wchar_t* sz
, size_t nStart
= npos
) const
2047 { return m_impl
.find_last_of(ImplStr(sz
), nStart
); }
2048 size_t find_last_of(const char* sz
, size_t nStart
, size_t n
) const
2049 { return m_impl
.find_last_of(ImplStr(sz
), nStart
, n
); }
2050 size_t find_last_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
2051 { return m_impl
.find_last_of(ImplStr(sz
), nStart
, n
); }
2052 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const
2053 { return m_impl
.find_last_of((wxChar
)c
, nStart
); }
2055 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
2056 { return m_impl
.find_first_not_of(str
.m_impl
, nStart
); }
2057 size_t find_first_not_of(const char* sz
, size_t nStart
= 0) const
2058 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
); }
2059 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
= 0) const
2060 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
); }
2061 size_t find_first_not_of(const char* sz
, size_t nStart
, size_t n
) const
2062 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
, n
); }
2063 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
2064 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
, n
); }
2065 size_t find_first_not_of(wxUniChar c
, size_t nStart
= 0) const
2066 { return m_impl
.find_first_not_of((wxChar
)c
, nStart
); }
2068 size_t find_last_not_of(const wxString
& str
, size_t nStart
= npos
) const
2069 { return m_impl
.find_last_not_of(str
.m_impl
, nStart
); }
2070 size_t find_last_not_of(const char* sz
, size_t nStart
= npos
) const
2071 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
); }
2072 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
= npos
) const
2073 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
); }
2074 size_t find_last_not_of(const char* sz
, size_t nStart
, size_t n
) const
2075 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
, n
); }
2076 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
2077 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
, n
); }
2078 size_t find_last_not_of(wxUniChar c
, size_t nStart
= npos
) const
2079 { return m_impl
.find_last_not_of((wxChar
)c
, nStart
); }
2081 // we can't use std::string implementation in UTF-8 build, because the
2082 // character sets would be interpreted wrongly:
2084 // as strpbrk() but starts at nStart, returns npos if not found
2085 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
2086 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2087 { return find_first_of(str
.mb_str().data(), nStart
); }
2089 { return find_first_of((const wxChar
*)str
.c_str(), nStart
); }
2092 size_t find_first_of(const char* sz
, size_t nStart
= 0) const;
2093 size_t find_first_of(const wchar_t* sz
, size_t nStart
= 0) const;
2094 size_t find_first_of(const char* sz
, size_t nStart
, size_t n
) const;
2095 size_t find_first_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2096 // same as find(char, size_t)
2097 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const
2098 { return find(c
, nStart
); }
2099 // find the last (starting from nStart) char from str in this string
2100 size_t find_last_of (const wxString
& str
, size_t nStart
= npos
) const
2101 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2102 { return find_last_of(str
.mb_str().data(), nStart
); }
2104 { return find_last_of((const wxChar
*)str
.c_str(), nStart
); }
2107 size_t find_last_of (const char* sz
, size_t nStart
= npos
) const;
2108 size_t find_last_of (const wchar_t* sz
, size_t nStart
= npos
) const;
2109 size_t find_last_of(const char* sz
, size_t nStart
, size_t n
) const;
2110 size_t find_last_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2112 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const
2113 { return rfind(c
, nStart
); }
2115 // find first/last occurence of any character not in the set
2117 // as strspn() (starting from nStart), returns npos on failure
2118 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
2119 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2120 { return find_first_not_of(str
.mb_str().data(), nStart
); }
2122 { return find_first_not_of((const wxChar
*)str
.c_str(), nStart
); }
2125 size_t find_first_not_of(const char* sz
, size_t nStart
= 0) const;
2126 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
= 0) const;
2127 size_t find_first_not_of(const char* sz
, size_t nStart
, size_t n
) const;
2128 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2130 size_t find_first_not_of(wxUniChar ch
, size_t nStart
= 0) const;
2132 size_t find_last_not_of(const wxString
& str
, size_t nStart
= npos
) const
2133 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2134 { return find_last_not_of(str
.mb_str().data(), nStart
); }
2136 { return find_last_not_of((const wxChar
*)str
.c_str(), nStart
); }
2139 size_t find_last_not_of(const char* sz
, size_t nStart
= npos
) const;
2140 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
= npos
) const;
2141 size_t find_last_not_of(const char* sz
, size_t nStart
, size_t n
) const;
2142 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2144 size_t find_last_not_of(wxUniChar ch
, size_t nStart
= npos
) const;
2145 #endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not
2147 // provide char/wchar_t/wxUniCharRef overloads for char-finding functions
2148 // above to resolve ambiguities:
2149 size_t find_first_of(wxUniCharRef ch
, size_t nStart
= 0) const
2150 { return find_first_of(wxUniChar(ch
), nStart
); }
2151 size_t find_first_of(char ch
, size_t nStart
= 0) const
2152 { return find_first_of(wxUniChar(ch
), nStart
); }
2153 size_t find_first_of(unsigned char ch
, size_t nStart
= 0) const
2154 { return find_first_of(wxUniChar(ch
), nStart
); }
2155 size_t find_first_of(wchar_t ch
, size_t nStart
= 0) const
2156 { return find_first_of(wxUniChar(ch
), nStart
); }
2157 size_t find_last_of(wxUniCharRef ch
, size_t nStart
= npos
) const
2158 { return find_last_of(wxUniChar(ch
), nStart
); }
2159 size_t find_last_of(char ch
, size_t nStart
= npos
) const
2160 { return find_last_of(wxUniChar(ch
), nStart
); }
2161 size_t find_last_of(unsigned char ch
, size_t nStart
= npos
) const
2162 { return find_last_of(wxUniChar(ch
), nStart
); }
2163 size_t find_last_of(wchar_t ch
, size_t nStart
= npos
) const
2164 { return find_last_of(wxUniChar(ch
), nStart
); }
2165 size_t find_first_not_of(wxUniCharRef ch
, size_t nStart
= 0) const
2166 { return find_first_not_of(wxUniChar(ch
), nStart
); }
2167 size_t find_first_not_of(char ch
, size_t nStart
= 0) const
2168 { return find_first_not_of(wxUniChar(ch
), nStart
); }
2169 size_t find_first_not_of(unsigned char ch
, size_t nStart
= 0) const
2170 { return find_first_not_of(wxUniChar(ch
), nStart
); }
2171 size_t find_first_not_of(wchar_t ch
, size_t nStart
= 0) const
2172 { return find_first_not_of(wxUniChar(ch
), nStart
); }
2173 size_t find_last_not_of(wxUniCharRef ch
, size_t nStart
= npos
) const
2174 { return find_last_not_of(wxUniChar(ch
), nStart
); }
2175 size_t find_last_not_of(char ch
, size_t nStart
= npos
) const
2176 { return find_last_not_of(wxUniChar(ch
), nStart
); }
2177 size_t find_last_not_of(unsigned char ch
, size_t nStart
= npos
) const
2178 { return find_last_not_of(wxUniChar(ch
), nStart
); }
2179 size_t find_last_not_of(wchar_t ch
, size_t nStart
= npos
) const
2180 { return find_last_not_of(wxUniChar(ch
), nStart
); }
2183 wxString
& operator+=(const wxString
& s
)
2184 { m_impl
+= s
.m_impl
; return *this; }
2185 // string += C string
2186 wxString
& operator+=(const char *psz
)
2187 { m_impl
+= ImplStr(psz
); return *this; }
2188 wxString
& operator+=(const wchar_t *pwz
)
2189 { m_impl
+= ImplStr(pwz
); return *this; }
2190 wxString
& operator+=(const wxCStrData
& s
)
2191 { m_impl
+= s
.AsString().m_impl
; return *this; }
2193 wxString
& operator+=(wxUniChar ch
)
2194 { m_impl
+= EncodeChar(ch
); return *this; }
2195 wxString
& operator+=(wxUniCharRef ch
) { return *this += wxUniChar(ch
); }
2196 wxString
& operator+=(int ch
) { return *this += wxUniChar(ch
); }
2197 wxString
& operator+=(char ch
) { return *this += wxUniChar(ch
); }
2198 wxString
& operator+=(unsigned char ch
) { return *this += wxUniChar(ch
); }
2199 wxString
& operator+=(wchar_t ch
) { return *this += wxUniChar(ch
); }
2202 #if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2203 // helpers for wxStringBuffer and wxStringBufferLength
2204 wxStringCharType
*DoGetWriteBuf(size_t nLen
)
2205 { return m_impl
.DoGetWriteBuf(nLen
); }
2206 void DoUngetWriteBuf()
2207 { m_impl
.DoUngetWriteBuf(); }
2208 void DoUngetWriteBuf(size_t nLen
)
2209 { m_impl
.DoUngetWriteBuf(nLen
); }
2211 friend class WXDLLIMPEXP_BASE wxStringBuffer
;
2212 friend class WXDLLIMPEXP_BASE wxStringBufferLength
;
2213 #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2215 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2216 int DoPrintf(const wxChar
*format
, ...) ATTRIBUTE_PRINTF_2
;
2217 static wxString
DoFormat(const wxChar
*format
, ...) ATTRIBUTE_PRINTF_1
;
2220 #if !wxUSE_STL_BASED_WXSTRING
2221 // check string's data validity
2222 bool IsValid() const { return m_impl
.GetStringData()->IsValid(); }
2226 wxStringImpl m_impl
;
2228 // buffers for compatibility conversion from (char*)c_str() and
2229 // (wchar_t*)c_str():
2230 // FIXME-UTF8: bechmark various approaches to keeping compatibility buffers
2231 template<typename T
>
2232 struct ConvertedBuffer
2234 ConvertedBuffer() : m_buf(NULL
) {}
2238 operator T
*() const { return m_buf
; }
2240 ConvertedBuffer
& operator=(T
*str
)
2250 ConvertedBuffer
<char> m_convertedToChar
;
2252 #if !wxUSE_UNICODE_WCHAR
2253 ConvertedBuffer
<wchar_t> m_convertedToWChar
;
2255 friend class WXDLLIMPEXP_BASE wxCStrData
;
2258 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
2259 #pragma warning (default:4275)
2262 // string iterator operators that satisfy STL Random Access Iterator
2264 inline wxString::iterator
operator+(int n
, wxString::iterator i
)
2266 inline wxString::iterator
operator+(size_t n
, wxString::iterator i
)
2268 inline wxString::const_iterator
operator+(int n
, wxString::const_iterator i
)
2270 inline wxString::const_iterator
operator+(size_t n
, wxString::const_iterator i
)
2272 inline wxString::reverse_iterator
operator+(int n
, wxString::reverse_iterator i
)
2274 inline wxString::reverse_iterator
operator+(size_t n
, wxString::reverse_iterator i
)
2276 inline wxString::const_reverse_iterator
operator+(int n
, wxString::const_reverse_iterator i
)
2278 inline wxString::const_reverse_iterator
operator+(size_t n
, wxString::const_reverse_iterator i
)
2281 // notice that even though for many compilers the friend declarations above are
2282 // enough, from the point of view of C++ standard we must have the declarations
2283 // here as friend ones are not injected in the enclosing namespace and without
2284 // them the code fails to compile with conforming compilers such as xlC or g++4
2285 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
, const wxString
& string2
);
2286 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const char *psz
);
2287 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const wchar_t *pwz
);
2288 wxString WXDLLIMPEXP_BASE
operator+(const char *psz
, const wxString
& string
);
2289 wxString WXDLLIMPEXP_BASE
operator+(const wchar_t *pwz
, const wxString
& string
);
2291 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
2292 wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
2294 inline wxString
operator+(const wxString
& string
, wxUniCharRef ch
)
2295 { return string
+ (wxUniChar
)ch
; }
2296 inline wxString
operator+(const wxString
& string
, char ch
)
2297 { return string
+ wxUniChar(ch
); }
2298 inline wxString
operator+(const wxString
& string
, wchar_t ch
)
2299 { return string
+ wxUniChar(ch
); }
2300 inline wxString
operator+(wxUniCharRef ch
, const wxString
& string
)
2301 { return (wxUniChar
)ch
+ string
; }
2302 inline wxString
operator+(char ch
, const wxString
& string
)
2303 { return wxUniChar(ch
) + string
; }
2304 inline wxString
operator+(wchar_t ch
, const wxString
& string
)
2305 { return wxUniChar(ch
) + string
; }
2308 #if wxUSE_STL_BASED_WXSTRING
2309 // return an empty wxString (not very useful with wxUSE_STL == 1)
2310 inline const wxString
wxGetEmptyString() { return wxString(); }
2311 #else // !wxUSE_STL_BASED_WXSTRING
2312 // return an empty wxString (more efficient than wxString() here)
2313 inline const wxString
& wxGetEmptyString()
2315 return *(wxString
*)&wxEmptyString
;
2317 #endif // wxUSE_STL_BASED_WXSTRING/!wxUSE_STL_BASED_WXSTRING
2319 // ----------------------------------------------------------------------------
2320 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
2321 // ----------------------------------------------------------------------------
2323 #if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
2325 class WXDLLIMPEXP_BASE wxStringBuffer
2328 wxStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
2329 : m_str(str
), m_buf(lenWanted
)
2332 ~wxStringBuffer() { m_str
.assign(m_buf
.data(), wxStrlen(m_buf
.data())); }
2334 operator wxChar
*() { return m_buf
.data(); }
2339 wxWCharBuffer m_buf
;
2344 DECLARE_NO_COPY_CLASS(wxStringBuffer
)
2347 class WXDLLIMPEXP_BASE wxStringBufferLength
2350 wxStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
2351 : m_str(str
), m_buf(lenWanted
), m_len(0), m_lenSet(false)
2354 ~wxStringBufferLength()
2357 m_str
.assign(m_buf
.data(), m_len
);
2360 operator wxChar
*() { return m_buf
.data(); }
2361 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
2366 wxWCharBuffer m_buf
;
2373 DECLARE_NO_COPY_CLASS(wxStringBufferLength
)
2376 #else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2378 class WXDLLIMPEXP_BASE wxStringBuffer
2381 wxStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
2382 : m_str(str
), m_buf(NULL
)
2383 { m_buf
= m_str
.DoGetWriteBuf(lenWanted
); }
2385 ~wxStringBuffer() { m_str
.DoUngetWriteBuf(); }
2387 operator wxChar
*() const { return m_buf
; }
2393 DECLARE_NO_COPY_CLASS(wxStringBuffer
)
2396 class WXDLLIMPEXP_BASE wxStringBufferLength
2399 wxStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
2400 : m_str(str
), m_buf(NULL
), m_len(0), m_lenSet(false)
2402 m_buf
= m_str
.DoGetWriteBuf(lenWanted
);
2403 wxASSERT(m_buf
!= NULL
);
2406 ~wxStringBufferLength()
2409 m_str
.DoUngetWriteBuf(m_len
);
2412 operator wxChar
*() const { return m_buf
; }
2413 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
2421 DECLARE_NO_COPY_CLASS(wxStringBufferLength
)
2424 #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2426 // ---------------------------------------------------------------------------
2427 // wxString comparison functions: operator versions are always case sensitive
2428 // ---------------------------------------------------------------------------
2430 #define wxCMP_WXCHAR_STRING(p, s, op) s.Cmp(p) op 0
2432 wxDEFINE_ALL_COMPARISONS(const wxChar
*, const wxString
&, wxCMP_WXCHAR_STRING
)
2434 #undef wxCMP_WXCHAR_STRING
2436 // note that there is an optimization in operator==() and !=(): we (quickly)
2437 // checks the strings length first, before comparing their data
2438 inline bool operator==(const wxString
& s1
, const wxString
& s2
)
2439 { return (s1
.Len() == s2
.Len()) && (s1
.Cmp(s2
) == 0); }
2440 inline bool operator!=(const wxString
& s1
, const wxString
& s2
)
2441 { return (s1
.Len() != s2
.Len()) || (s1
.Cmp(s2
) != 0); }
2442 inline bool operator< (const wxString
& s1
, const wxString
& s2
)
2443 { return s1
.Cmp(s2
) < 0; }
2444 inline bool operator> (const wxString
& s1
, const wxString
& s2
)
2445 { return s1
.Cmp(s2
) > 0; }
2446 inline bool operator<=(const wxString
& s1
, const wxString
& s2
)
2447 { return s1
.Cmp(s2
) <= 0; }
2448 inline bool operator>=(const wxString
& s1
, const wxString
& s2
)
2449 { return s1
.Cmp(s2
) >= 0; }
2452 inline bool operator==(const wxString
& s1
, const wxWCharBuffer
& s2
)
2453 { return (s1
.Cmp((const wchar_t *)s2
) == 0); }
2454 inline bool operator==(const wxWCharBuffer
& s1
, const wxString
& s2
)
2455 { return (s2
.Cmp((const wchar_t *)s1
) == 0); }
2456 inline bool operator!=(const wxString
& s1
, const wxWCharBuffer
& s2
)
2457 { return (s1
.Cmp((const wchar_t *)s2
) != 0); }
2458 inline bool operator!=(const wxWCharBuffer
& s1
, const wxString
& s2
)
2459 { return (s2
.Cmp((const wchar_t *)s1
) != 0); }
2460 #else // !wxUSE_UNICODE
2461 inline bool operator==(const wxString
& s1
, const wxCharBuffer
& s2
)
2462 { return (s1
.Cmp((const char *)s2
) == 0); }
2463 inline bool operator==(const wxCharBuffer
& s1
, const wxString
& s2
)
2464 { return (s2
.Cmp((const char *)s1
) == 0); }
2465 inline bool operator!=(const wxString
& s1
, const wxCharBuffer
& s2
)
2466 { return (s1
.Cmp((const char *)s2
) != 0); }
2467 inline bool operator!=(const wxCharBuffer
& s1
, const wxString
& s2
)
2468 { return (s2
.Cmp((const char *)s1
) != 0); }
2469 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
2472 inline wxString
operator+(const wxString
& string
, const wxWCharBuffer
& buf
)
2473 { return string
+ (const wchar_t *)buf
; }
2474 inline wxString
operator+(const wxWCharBuffer
& buf
, const wxString
& string
)
2475 { return (const wchar_t *)buf
+ string
; }
2476 #else // !wxUSE_UNICODE
2477 inline wxString
operator+(const wxString
& string
, const wxCharBuffer
& buf
)
2478 { return string
+ (const char *)buf
; }
2479 inline wxString
operator+(const wxCharBuffer
& buf
, const wxString
& string
)
2480 { return (const char *)buf
+ string
; }
2481 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
2483 // comparison with char
2484 inline bool operator==(const wxUniChar
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2485 inline bool operator==(const wxUniCharRef
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2486 inline bool operator==(char c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2487 inline bool operator==(wchar_t c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2488 inline bool operator==(int c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2489 inline bool operator==(const wxString
& s
, const wxUniChar
& c
) { return s
.IsSameAs(c
); }
2490 inline bool operator==(const wxString
& s
, const wxUniCharRef
& c
) { return s
.IsSameAs(c
); }
2491 inline bool operator==(const wxString
& s
, char c
) { return s
.IsSameAs(c
); }
2492 inline bool operator==(const wxString
& s
, wchar_t c
) { return s
.IsSameAs(c
); }
2493 inline bool operator!=(const wxUniChar
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2494 inline bool operator!=(const wxUniCharRef
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2495 inline bool operator!=(char c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2496 inline bool operator!=(wchar_t c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2497 inline bool operator!=(int c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2498 inline bool operator!=(const wxString
& s
, const wxUniChar
& c
) { return !s
.IsSameAs(c
); }
2499 inline bool operator!=(const wxString
& s
, const wxUniCharRef
& c
) { return !s
.IsSameAs(c
); }
2500 inline bool operator!=(const wxString
& s
, char c
) { return !s
.IsSameAs(c
); }
2501 inline bool operator!=(const wxString
& s
, wchar_t c
) { return !s
.IsSameAs(c
); }
2503 // comparison with C string in Unicode build
2506 #define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s
2508 wxDEFINE_ALL_COMPARISONS(const char *, const wxString
&, wxCMP_CHAR_STRING
)
2510 #undef wxCMP_CHAR_STRING
2512 #endif // wxUSE_UNICODE
2514 // we also need to provide the operators for comparison with wxCStrData to
2515 // resolve ambiguity between operator(const wxChar *,const wxString &) and
2516 // operator(const wxChar *, const wxChar *) for "p == s.c_str()"
2518 // notice that these are (shallow) pointer comparisons, not (deep) string ones
2519 #define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar()
2520 #define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar()
2522 wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData
&, wxCMP_WCHAR_CSTRDATA
)
2523 wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData
&, wxCMP_CHAR_CSTRDATA
)
2525 #undef wxCMP_CHAR_CSTRDATA
2526 #undef wxCMP_WCHAR_CSTRDATA
2528 // ---------------------------------------------------------------------------
2529 // Implementation only from here until the end of file
2530 // ---------------------------------------------------------------------------
2532 #if wxUSE_STD_IOSTREAM
2534 #include "wx/iosfwrap.h"
2536 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxString
&);
2537 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxCStrData
&);
2538 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxCharBuffer
&);
2539 #ifndef __BORLANDC__
2540 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxWCharBuffer
&);
2543 #endif // wxSTD_STRING_COMPATIBILITY
2545 // ---------------------------------------------------------------------------
2546 // wxCStrData implementation
2547 // ---------------------------------------------------------------------------
2549 inline wxCStrData::wxCStrData(char *buf
)
2550 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
2551 inline wxCStrData::wxCStrData(wchar_t *buf
)
2552 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
2554 inline wxCStrData::~wxCStrData()
2560 inline wxCStrData::operator bool() const
2562 return !m_str
->empty();
2565 // simple cases for AsChar() and AsWChar(), the complicated ones are
2567 #if wxUSE_UNICODE_WCHAR
2568 inline const wchar_t* wxCStrData::AsWChar() const
2570 return m_str
->wx_str() + m_offset
;
2572 #endif // wxUSE_UNICODE_WCHAR
2575 inline const char* wxCStrData::AsChar() const
2577 return m_str
->wx_str() + m_offset
;
2579 #endif // !wxUSE_UNICODE
2581 inline const wxCharBuffer
wxCStrData::AsCharBuf() const
2584 return wxCharBuffer::CreateNonOwned(AsChar());
2586 return AsString().mb_str();
2590 inline const wxWCharBuffer
wxCStrData::AsWCharBuf() const
2592 #if wxUSE_UNICODE_WCHAR
2593 return wxWCharBuffer::CreateNonOwned(AsWChar());
2595 return AsString().wc_str();
2599 inline wxString
wxCStrData::AsString() const
2601 if ( m_offset
== 0 )
2604 return m_str
->Mid(m_offset
);
2607 inline wxUniChar
wxCStrData::operator*() const
2609 if ( m_str
->empty() )
2610 return wxUniChar(_T('\0'));
2612 return (*m_str
)[m_offset
];
2615 inline wxUniChar
wxCStrData::operator[](size_t n
) const
2617 return m_str
->at(m_offset
+ n
);
2620 // ----------------------------------------------------------------------------
2621 // more wxCStrData operators
2622 // ----------------------------------------------------------------------------
2624 // we need to define those to allow "size_t pos = p - s.c_str()" where p is
2625 // some pointer into the string
2626 inline size_t operator-(const char *p
, const wxCStrData
& cs
)
2628 return p
- cs
.AsChar();
2631 inline size_t operator-(const wchar_t *p
, const wxCStrData
& cs
)
2633 return p
- cs
.AsWChar();
2636 // ----------------------------------------------------------------------------
2637 // implementation of wx[W]CharBuffer inline methods using wxCStrData
2638 // ----------------------------------------------------------------------------
2640 // FIXME-UTF8: move this to buffer.h
2641 inline wxCharBuffer::wxCharBuffer(const wxCStrData
& cstr
)
2642 : wxCharTypeBufferBase(cstr
.AsCharBuf())
2646 inline wxWCharBuffer::wxWCharBuffer(const wxCStrData
& cstr
)
2647 : wxCharTypeBufferBase(cstr
.AsWCharBuf())
2651 #endif // _WX_WXSTRING_H_