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 // ---------------------------------------------------------------------------
65 // ---------------------------------------------------------------------------
67 // casts [unfortunately!] needed to call some broken functions which require
68 // "char *" instead of "const char *"
69 #define WXSTRINGCAST (wxChar *)(const wxChar *)
70 #define wxCSTRINGCAST (wxChar *)(const wxChar *)
71 #define wxMBSTRINGCAST (char *)(const char *)
72 #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *)
74 // ----------------------------------------------------------------------------
76 // ----------------------------------------------------------------------------
78 #if WXWIN_COMPATIBILITY_2_6
80 // deprecated in favour of wxString::npos, don't use in new code
82 // maximum possible length for a string means "take all string" everywhere
83 #define wxSTRING_MAXLEN wxString::npos
85 #endif // WXWIN_COMPATIBILITY_2_6
87 // ---------------------------------------------------------------------------
88 // global functions complementing standard C string library replacements for
89 // strlen() and portable strcasecmp()
90 //---------------------------------------------------------------------------
92 #if WXWIN_COMPATIBILITY_2_8
93 // Use wxXXX() functions from wxcrt.h instead! These functions are for
94 // backwards compatibility only.
96 // checks whether the passed in pointer is NULL and if the string is empty
97 wxDEPRECATED( inline bool IsEmpty(const char *p
) );
98 inline bool IsEmpty(const char *p
) { return (!p
|| !*p
); }
100 // safe version of strlen() (returns 0 if passed NULL pointer)
101 wxDEPRECATED( inline size_t Strlen(const char *psz
) );
102 inline size_t Strlen(const char *psz
)
103 { return psz
? strlen(psz
) : 0; }
105 // portable strcasecmp/_stricmp
106 wxDEPRECATED( inline int Stricmp(const char *psz1
, const char *psz2
) );
107 inline int Stricmp(const char *psz1
, const char *psz2
)
109 #if defined(__VISUALC__) && defined(__WXWINCE__)
110 register char c1
, c2
;
112 c1
= tolower(*psz1
++);
113 c2
= tolower(*psz2
++);
114 } while ( c1
&& (c1
== c2
) );
117 #elif defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) )
118 return _stricmp(psz1
, psz2
);
119 #elif defined(__SC__)
120 return _stricmp(psz1
, psz2
);
121 #elif defined(__SALFORDC__)
122 return stricmp(psz1
, psz2
);
123 #elif defined(__BORLANDC__)
124 return stricmp(psz1
, psz2
);
125 #elif defined(__WATCOMC__)
126 return stricmp(psz1
, psz2
);
127 #elif defined(__DJGPP__)
128 return stricmp(psz1
, psz2
);
129 #elif defined(__EMX__)
130 return stricmp(psz1
, psz2
);
131 #elif defined(__WXPM__)
132 return stricmp(psz1
, psz2
);
133 #elif defined(__WXPALMOS__) || \
134 defined(HAVE_STRCASECMP_IN_STRING_H) || \
135 defined(HAVE_STRCASECMP_IN_STRINGS_H) || \
136 defined(__GNUWIN32__)
137 return strcasecmp(psz1
, psz2
);
138 #elif defined(__MWERKS__) && !defined(__INTEL__)
139 register char c1
, c2
;
141 c1
= tolower(*psz1
++);
142 c2
= tolower(*psz2
++);
143 } while ( c1
&& (c1
== c2
) );
147 // almost all compilers/libraries provide this function (unfortunately under
148 // different names), that's why we don't implement our own which will surely
149 // be more efficient than this code (uncomment to use):
151 register char c1, c2;
153 c1 = tolower(*psz1++);
154 c2 = tolower(*psz2++);
155 } while ( c1 && (c1 == c2) );
160 #error "Please define string case-insensitive compare for your OS/compiler"
161 #endif // OS/compiler
164 #endif // WXWIN_COMPATIBILITY_2_8
166 // ----------------------------------------------------------------------------
168 // ----------------------------------------------------------------------------
170 // Lightweight object returned by wxString::c_str() and implicitly convertible
171 // to either const char* or const wchar_t*.
175 // Ctors; for internal use by wxString and wxCStrData only
176 wxCStrData(const wxString
*str
, size_t offset
= 0, bool owned
= false)
177 : m_str(str
), m_offset(offset
), m_owned(owned
) {}
180 // Ctor constructs the object from char literal; they are needed to make
181 // operator?: compile and they intentionally take char*, not const char*
182 wxCStrData(char *buf
);
183 wxCStrData(wchar_t *buf
);
187 // FIXME: we'll need convertors for both char* and wchar_t* and NONE
188 // for wxChar*, but that's after completing the transition to
189 // "smart" wxUniChar class. For now, just have conversion to
190 // char* in ANSI build and wchar_t in Unicode build.
192 const wchar_t* AsWChar() const;
193 operator const wchar_t*() const { return AsWChar(); }
195 const char* AsChar() const;
196 const unsigned char* AsUnsignedChar() const
197 { return (const unsigned char *) AsChar(); }
198 operator const void*() const { return AsChar(); }
199 operator const char*() const { return AsChar(); }
200 operator const unsigned char*() const { return AsUnsignedChar(); }
203 wxString
AsString() const;
205 // allow expressions like "c_str()[0]":
206 wxUniChar
operator[](int n
) const { return operator[](size_t(n
)); }
207 wxUniChar
operator[](size_t n
) const;
208 wxUniChar
operator[](long n
) const { return operator[](size_t(n
)); }
209 #ifndef wxSIZE_T_IS_UINT
210 wxUniChar
operator[](unsigned int n
) const { return operator[](size_t(n
)); }
211 #endif // size_t != unsigned int
213 // these operators are needed to emulate the pointer semantics of c_str():
214 // expressions like "wxChar *p = str.c_str() + 1;" should continue to work
215 // (we need both versions to resolve ambiguities):
216 wxCStrData
operator+(int n
) const
217 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
218 wxCStrData
operator+(long n
) const
219 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
220 wxCStrData
operator+(size_t n
) const
221 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
223 // this operator is needed to make expressions like "*c_str()" or
224 // "*(c_str() + 2)" work
225 wxUniChar
operator*() const;
228 const wxString
*m_str
;
232 friend class WXDLLIMPEXP_BASE wxString
;
235 // ----------------------------------------------------------------------------
236 // wxStringPrintfMixin
237 // ---------------------------------------------------------------------------
239 // NB: VC6 has a bug that causes linker errors if you have template methods
240 // in a class using __declspec(dllimport). The solution is to split such
241 // class into two classes, one that contains the template methods and does
242 // *not* use WXDLLIMPEXP_BASE and another class that contains the rest
243 // (with DLL linkage).
245 // We only do this for VC6 here, because the code is less efficient
246 // (Printf() has to use dynamic_cast<>) and because OpenWatcom compiler
247 // cannot compile this code.
249 #if defined(__VISUALC__) && __VISUALC__ < 1300
250 #define wxNEEDS_WXSTRING_PRINTF_MIXIN
253 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
254 // this class contains implementation of wxString's vararg methods, it's
255 // exported from wxBase DLL
256 class WXDLLIMPEXP_BASE wxStringPrintfMixinBase
259 wxStringPrintfMixinBase() {}
261 int DoPrintf(const wxChar
*format
, ...) ATTRIBUTE_PRINTF_2
;
262 static wxString
DoFormat(const wxChar
*format
, ...) ATTRIBUTE_PRINTF_1
;
265 // this class contains template wrappers for wxString's vararg methods, it's
266 // intentionally *not* exported from the DLL in order to fix the VC6 bug
268 class wxStringPrintfMixin
: public wxStringPrintfMixinBase
271 // to further complicate things, we can't return wxString from
272 // wxStringPrintfMixin::Format() because wxString is not yet declared at
273 // this point; the solution is to use this fake type trait template - this
274 // way the compiler won't know the return type until Format() is used
275 // (this doesn't compile with Watcom, but VC6 compiles it just fine):
276 template<typename T
> struct StringReturnType
278 typedef wxString type
;
282 // these are duplicated wxString methods, they're also declared below
283 // if !wxNEEDS_WXSTRING_PRINTF_MIXIN:
285 // int Printf(const wxChar *pszFormat, ...);
286 WX_DEFINE_VARARG_FUNC(int, Printf
, DoPrintf
)
287 // static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1;
288 WX_DEFINE_VARARG_FUNC(static typename StringReturnType
<T1
>::type
,
290 // int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2;
291 WX_DEFINE_VARARG_FUNC(int, sprintf
, DoPrintf
)
294 wxStringPrintfMixin() : wxStringPrintfMixinBase() {}
296 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
299 // ----------------------------------------------------------------------------
300 // wxString: string class trying to be compatible with std::string, MFC
301 // CString and wxWindows 1.x wxString all at once
302 // ---------------------------------------------------------------------------
304 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
305 // "non dll-interface class 'wxStringPrintfMixin' used as base interface
306 // for dll-interface class 'wxString'" -- this is OK in our case
307 #pragma warning (disable:4275)
310 class WXDLLIMPEXP_BASE wxString
311 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
312 : public wxStringPrintfMixin
315 // NB: special care was taken in arranging the member functions in such order
316 // that all inline functions can be effectively inlined, verify that all
317 // performance critical functions are still inlined if you change order!
319 // an 'invalid' value for string index, moved to this place due to a CW bug
320 static const size_t npos
;
323 // if we hadn't made these operators private, it would be possible to
324 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
325 // converted to char in C and we do have operator=(char)
327 // NB: we don't need other versions (short/long and unsigned) as attempt
328 // to assign another numeric type to wxString will now result in
329 // ambiguity between operator=(char) and operator=(int)
330 wxString
& operator=(int);
332 // these methods are not implemented - there is _no_ conversion from int to
333 // string, you're doing something wrong if the compiler wants to call it!
335 // try `s << i' or `s.Printf("%d", i)' instead
339 // buffer for holding temporary substring when using any of the methods
340 // that take (char*,size_t) or (wchar_t*,size_t) arguments:
341 // FIXME-UTF8: This will need changes when UTF8 build is introduced
343 struct SubstrBufFromType
348 SubstrBufFromType() {}
349 SubstrBufFromType(const T
& data_
, size_t len_
)
350 : data(data_
), len(len_
) {}
353 #if wxUSE_UNICODE_UTF8
354 // FIXME-UTF8: this will have to use slightly different type
355 #elif wxUSE_UNICODE_WCHAR
356 typedef SubstrBufFromType
<const wchar_t*> SubstrBufFromWC
;
357 typedef SubstrBufFromType
<wxWCharBuffer
> SubstrBufFromMB
;
359 typedef SubstrBufFromType
<const char*> SubstrBufFromMB
;
360 typedef SubstrBufFromType
<wxCharBuffer
> SubstrBufFromWC
;
364 // Functions implementing primitive operations on string data; wxString
365 // methods and iterators are implemented in terms of it. The differences
366 // between UTF-8 and wchar_t* representations of the string are mostly
370 // FIXME-UTF8: This will need changes when UTF8 build is introduced
371 static SubstrBufFromMB
ConvertStr(const char *psz
, size_t nLength
,
372 const wxMBConv
& conv
);
374 static SubstrBufFromWC
ConvertStr(const wchar_t *pwz
, size_t nLength
,
375 const wxMBConv
& conv
);
378 #if !wxUSE_UNICODE_UTF8 // wxUSE_UNICODE_WCHAR or !wxUSE_UNICODE
379 // returns C string encoded as the implementation expects:
381 static const wchar_t* ImplStr(const wchar_t* str
)
383 static const SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
384 { return SubstrBufFromWC(str
, n
== npos
? wxWcslen(str
) : n
); }
385 static wxWCharBuffer
ImplStr(const char* str
)
386 { return ConvertStr(str
, npos
, wxConvLibc
).data
; }
387 static SubstrBufFromMB
ImplStr(const char* str
, size_t n
)
388 { return ConvertStr(str
, n
, wxConvLibc
); }
390 static const char* ImplStr(const char* str
)
392 static const SubstrBufFromMB
ImplStr(const char* str
, size_t n
)
393 { return SubstrBufFromMB(str
, n
== npos
? wxStrlen(str
) : n
); }
394 static wxCharBuffer
ImplStr(const wchar_t* str
)
395 { return ConvertStr(str
, npos
, wxConvLibc
).data
; }
396 static SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
397 { return ConvertStr(str
, n
, wxConvLibc
); }
400 // moves the iterator to the next Unicode character
401 static void IncIter(wxStringImpl::iterator
& i
) { ++i
; }
402 static void IncIter(wxStringImpl::const_iterator
& i
) { ++i
; }
403 // moves the iterator to the previous Unicode character
404 static void DecIter(wxStringImpl::iterator
& i
) { --i
; }
405 static void DecIter(wxStringImpl::const_iterator
& i
) { --i
; }
406 // moves the iterator by n Unicode characters
407 static wxStringImpl::iterator
AddToIter(wxStringImpl::iterator i
, int n
)
409 static wxStringImpl::const_iterator
AddToIter(wxStringImpl::const_iterator i
, int n
)
411 // returns distance of the two iterators in Unicode characters
412 static int DiffIters(wxStringImpl::iterator i1
, wxStringImpl::iterator i2
)
414 static int DiffIters(wxStringImpl::const_iterator i1
, wxStringImpl::const_iterator i2
)
417 // encodes the character to a form used to represent it in internal
418 // representation (returns a string in UTF8 version)
419 static wxChar
EncodeChar(wxUniChar ch
) { return (wxChar
)ch
; }
421 // translates position index in wxString to/from index in underlying
423 static size_t PosToImpl(size_t pos
) { return pos
; }
424 static void PosLenToImpl(size_t pos
, size_t len
,
425 size_t *implPos
, size_t *implLen
)
426 { *implPos
= pos
; *implLen
= len
; }
427 static size_t PosFromImpl(size_t pos
) { return pos
; }
429 #else // wxUSE_UNICODE_UTF8
431 typedef char Utf8CharBuffer
[5];
432 static Utf8CharBuffer
EncodeChar(wxUniChar ch
);
433 // returns n copies of ch encoded in UTF-8 string
434 static wxCharBuffer
EncodeNChars(size_t n
, wxUniChar ch
);
436 size_t PosToImpl(size_t pos
) const
438 if ( pos
== 0 || pos
== npos
)
441 return wxStringImpl::const_iterator(begin() + pos
) - m_impl
.begin();
444 size_t PosFromImpl(size_t pos
) const
446 if ( pos
== 0 || pos
== npos
)
449 return const_iterator(m_impl
.begin() + pos
) - begin();
452 // FIXME: return as-is without copying under UTF8 locale, return
453 // converted string under other locales - needs wxCharBuffer
455 static wxCharBuffer
ImplStr(const char* str
);
457 static wxCharBuffer
ImplStr(const wchar_t* str
)
458 { return wxConvUTF8
.cWC2MB(str
); }
459 #endif // !wxUSE_UNICODE_UTF8/wxUSE_UNICODE_UTF8
463 // constructors and destructor
464 // ctor for an empty string
467 wxString(const wxStringImpl
& stringSrc
) : m_impl(stringSrc
) { }
468 wxString(const wxString
& stringSrc
) : m_impl(stringSrc
) { }
469 // string containing nRepeat copies of ch
470 wxString(wxUniChar ch
, size_t nRepeat
= 1)
471 : m_impl(nRepeat
, ch
) { }
472 wxString(size_t nRepeat
, wxUniChar ch
)
473 : m_impl(nRepeat
, ch
) { }
474 wxString(wxUniCharRef ch
, size_t nRepeat
= 1)
475 : m_impl(nRepeat
, ch
) { }
476 wxString(size_t nRepeat
, wxUniCharRef ch
)
477 : m_impl(nRepeat
, ch
) { }
478 wxString(char ch
, size_t nRepeat
= 1)
479 : m_impl(nRepeat
, ch
) { }
480 wxString(size_t nRepeat
, char ch
)
481 : m_impl(nRepeat
, ch
) { }
482 wxString(wchar_t ch
, size_t nRepeat
= 1)
483 : m_impl(nRepeat
, ch
) { }
484 wxString(size_t nRepeat
, wchar_t ch
)
485 : m_impl(nRepeat
, ch
) { }
486 // ctor takes first nLength characters from C string
487 // (default value of npos means take all the string)
488 wxString(const wxChar
*psz
)
489 : m_impl(psz
? psz
: wxT("")) { }
490 wxString(const wxChar
*psz
, size_t nLength
)
491 : m_impl(psz
, nLength
) { }
492 wxString(const wxChar
*psz
,
493 const wxMBConv
& WXUNUSED(conv
),
494 size_t nLength
= npos
)
495 : m_impl(psz
, nLength
== npos
? wxStrlen(psz
) : nLength
) { }
497 // even if we're not built with wxUSE_STL == 1 it is very convenient to allow
498 // implicit conversions from std::string to wxString as this allows to use
499 // the same strings in non-GUI and GUI code, however we don't want to
500 // unconditionally add this ctor as it would make wx lib dependent on
501 // libstdc++ on some Linux versions which is bad, so instead we ask the
502 // client code to define this wxUSE_STD_STRING symbol if they need it
503 #if wxUSE_STD_STRING && !wxUSE_STL_BASED_WXSTRING
504 wxString(const wxStdString
& s
)
505 : m_impl(s
.c_str()) { } // FIXME-UTF8: this is broken for embedded 0s
506 #endif // wxUSE_STD_STRING && !wxUSE_STL_BASED_WXSTRING
509 // from multibyte string
510 wxString(const char *psz
,
511 const wxMBConv
& conv
= wxConvLibc
,
512 size_t nLength
= npos
);
513 // from multibyte string for ANSI compatibility, with wxConvLibc
514 wxString(const char *psz
, size_t nLength
);
515 // from wxWCharBuffer (i.e. return from wxGetString)
516 wxString(const wxWCharBuffer
& psz
) : m_impl(psz
.data()) { }
518 // from C string (for compilers using unsigned char)
519 wxString(const unsigned char* psz
)
520 : m_impl((const char*)psz
) { }
521 // from part of C string (for compilers using unsigned char)
522 wxString(const unsigned char* psz
, size_t nLength
)
523 : m_impl((const char*)psz
, nLength
) { }
526 // from wide (Unicode) string
527 wxString(const wchar_t *pwz
,
528 const wxMBConv
& conv
= wxConvLibc
,
529 size_t nLength
= npos
);
530 // from wide string for Unicode compatibility, with wxConvLibc
531 wxString(const wchar_t *pwz
, size_t nLength
);
532 #endif // !wxUSE_WCHAR_T
535 wxString(const wxCharBuffer
& psz
)
537 #endif // Unicode/ANSI
539 wxString(const wxCStrData
& cstr
)
540 : m_impl(cstr
.AsString().m_impl
) { }
542 // as we provide both ctors with this signature for both char and unsigned
543 // char string, we need to provide one for wxCStrData to resolve ambiguity
544 wxString(const wxCStrData
& cstr
, size_t nLength
)
545 { assign(cstr
.AsString(), nLength
); }
547 // and because wxString is convertible to wxCStrData and const wxChar *
548 // we also need to provide this one
549 wxString(const wxString
& str
, size_t nLength
)
550 { assign(str
, nLength
); }
554 typedef wxUniChar value_type
;
555 typedef wxUniChar char_type
;
556 typedef wxUniCharRef reference
;
557 typedef wxChar
* pointer
;
558 typedef const wxChar
* const_pointer
;
560 typedef size_t size_type
;
561 typedef wxUniChar const_reference
;
564 #define WX_STR_ITERATOR_TAG std::random_access_iterator_tag
566 #define WX_STR_ITERATOR_TAG void /* dummy type */
569 #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, \
570 reference_type, reference_ctor) \
572 typedef wxStringImpl::iterator_name underlying_iterator; \
574 typedef WX_STR_ITERATOR_TAG iterator_category; \
575 typedef wxUniChar value_type; \
576 typedef int difference_type; \
577 typedef reference_type reference; \
578 typedef pointer_type pointer; \
580 iterator_name(const iterator_name& i) : m_cur(i.m_cur) {} \
582 reference operator*() const { return reference_ctor; } \
583 reference operator[](size_t n) const { return *(*this + n); } \
585 iterator_name& operator++() \
586 { wxString::IncIter(m_cur); return *this; } \
587 iterator_name& operator--() \
588 { wxString::DecIter(m_cur); return *this; } \
589 iterator_name operator++(int) \
591 iterator_name tmp = *this; \
592 wxString::IncIter(m_cur); \
595 iterator_name operator--(int) \
597 iterator_name tmp = *this; \
598 wxString::DecIter(m_cur); \
602 iterator_name operator+(int n) const \
603 { return iterator_name(wxString::AddToIter(m_cur, n)); } \
604 iterator_name operator+(size_t n) const \
605 { return iterator_name(wxString::AddToIter(m_cur, (int)n)); } \
606 iterator_name operator-(int n) const \
607 { return iterator_name(wxString::AddToIter(m_cur, -n)); } \
608 iterator_name operator-(size_t n) const \
609 { return iterator_name(wxString::AddToIter(m_cur, -(int)n)); } \
610 iterator_name operator+=(int n) \
611 { m_cur = wxString::AddToIter(m_cur, n); return *this; } \
612 iterator_name operator+=(size_t n) \
613 { m_cur = wxString::AddToIter(m_cur, (int)n); return *this; } \
614 iterator_name operator-=(int n) \
615 { m_cur = wxString::AddToIter(m_cur, -n); return *this; } \
616 iterator_name operator-=(size_t n) \
617 { m_cur = wxString::AddToIter(m_cur, -(int)n); return *this; } \
619 unsigned operator-(const iterator_name& i) const \
620 { return wxString::DiffIters(m_cur, i.m_cur); } \
622 bool operator==(const iterator_name&i) const \
623 { return m_cur == i.m_cur; } \
624 bool operator!=(const iterator_name& i) const \
625 { return m_cur != i.m_cur; } \
627 bool operator<(const iterator_name& i) const \
628 { return m_cur < i.m_cur; } \
629 bool operator>(const iterator_name& i) const \
630 { return m_cur > i.m_cur; } \
631 bool operator<=(const iterator_name& i) const \
632 { return m_cur <= i.m_cur; } \
633 bool operator>=(const iterator_name& i) const \
634 { return m_cur >= i.m_cur; } \
637 /* for internal wxString use only: */ \
638 iterator_name(underlying_iterator ptr) : m_cur(ptr) {} \
639 operator underlying_iterator() const { return m_cur; } \
641 friend class WXDLLIMPEXP_BASE wxString; \
642 friend class WXDLLIMPEXP_BASE wxCStrData; \
645 underlying_iterator m_cur;
647 class const_iterator
;
651 WX_STR_ITERATOR_IMPL(iterator
, wxChar
*, wxUniCharRef
,
652 wxUniCharRef::CreateForString(m_cur
))
654 friend class const_iterator
;
659 // NB: reference_type is intentionally value, not reference, the character
660 // may be encoded differently in wxString data:
661 WX_STR_ITERATOR_IMPL(const_iterator
, const wxChar
*, wxUniChar
,
665 const_iterator(const iterator
& i
) : m_cur(i
.m_cur
) {}
668 #undef WX_STR_ITERATOR_TAG
669 #undef WX_STR_ITERATOR_IMPL
671 friend class iterator
;
672 friend class const_iterator
;
674 template <typename T
>
675 class reverse_iterator_impl
678 typedef T iterator_type
;
680 typedef typename
T::iterator_category iterator_category
;
681 typedef typename
T::value_type value_type
;
682 typedef typename
T::difference_type difference_type
;
683 typedef typename
T::reference reference
;
684 typedef typename
T::pointer
*pointer
;
686 reverse_iterator_impl(iterator_type i
) : m_cur(i
) {}
687 reverse_iterator_impl(const reverse_iterator_impl
& ri
)
690 iterator_type
base() const { return m_cur
; }
692 reference
operator*() const { return *(m_cur
-1); }
693 reference
operator[](size_t n
) const { return *(*this + n
); }
695 reverse_iterator_impl
& operator++()
696 { --m_cur
; return *this; }
697 reverse_iterator_impl
operator++(int)
698 { reverse_iterator_impl tmp
= *this; --m_cur
; return tmp
; }
699 reverse_iterator_impl
& operator--()
700 { ++m_cur
; return *this; }
701 reverse_iterator_impl
operator--(int)
702 { reverse_iterator_impl tmp
= *this; ++m_cur
; return tmp
; }
704 reverse_iterator_impl
operator+(int n
) const
705 { return reverse_iterator_impl(m_cur
- n
); }
706 reverse_iterator_impl
operator+(size_t n
) const
707 { return reverse_iterator_impl(m_cur
- n
); }
708 reverse_iterator_impl
operator-(int n
) const
709 { return reverse_iterator_impl(m_cur
+ n
); }
710 reverse_iterator_impl
operator-(size_t n
) const
711 { return reverse_iterator_impl(m_cur
+ n
); }
712 reverse_iterator_impl
operator+=(int n
)
713 { m_cur
-= n
; return *this; }
714 reverse_iterator_impl
operator+=(size_t n
)
715 { m_cur
-= n
; return *this; }
716 reverse_iterator_impl
operator-=(int n
)
717 { m_cur
+= n
; return *this; }
718 reverse_iterator_impl
operator-=(size_t n
)
719 { m_cur
+= n
; return *this; }
721 unsigned operator-(const reverse_iterator_impl
& i
) const
722 { return i
.m_cur
- m_cur
; }
724 bool operator==(const reverse_iterator_impl
& ri
) const
725 { return m_cur
== ri
.m_cur
; }
726 bool operator!=(const reverse_iterator_impl
& ri
) const
727 { return !(*this == ri
); }
729 bool operator<(const reverse_iterator_impl
& i
) const
730 { return m_cur
> i
.m_cur
; }
731 bool operator>(const reverse_iterator_impl
& i
) const
732 { return m_cur
< i
.m_cur
; }
733 bool operator<=(const reverse_iterator_impl
& i
) const
734 { return m_cur
>= i
.m_cur
; }
735 bool operator>=(const reverse_iterator_impl
& i
) const
736 { return m_cur
<= i
.m_cur
; }
742 typedef reverse_iterator_impl
<iterator
> reverse_iterator
;
743 typedef reverse_iterator_impl
<const_iterator
> const_reverse_iterator
;
745 // first valid index position
746 const_iterator
begin() const { return const_iterator(m_impl
.begin()); }
747 iterator
begin() { return iterator(m_impl
.begin()); }
748 // position one after the last valid one
749 const_iterator
end() const { return const_iterator(m_impl
.end()); }
750 iterator
end() { return iterator(m_impl
.end()); }
752 // first element of the reversed string
753 const_reverse_iterator
rbegin() const
754 { return const_reverse_iterator(end()); }
755 reverse_iterator
rbegin()
756 { return reverse_iterator(end()); }
757 // one beyond the end of the reversed string
758 const_reverse_iterator
rend() const
759 { return const_reverse_iterator(begin()); }
760 reverse_iterator
rend()
761 { return reverse_iterator(begin()); }
763 // std::string methods:
764 #if wxUSE_UNICODE_UTF8
765 size_t length() const { return end() - begin(); } // FIXME-UTF8: optimize!
767 size_t length() const { return m_impl
.length(); }
770 size_type
size() const { return length(); }
771 size_type
max_size() const { return npos
; }
773 bool empty() const { return m_impl
.empty(); }
775 size_type
capacity() const { return m_impl
.capacity(); } // FIXME-UTF8
776 void reserve(size_t sz
) { m_impl
.reserve(sz
); } // FIXME-UTF8
778 void resize(size_t nSize
, wxUniChar ch
= wxT('\0'))
780 #if wxUSE_UNICODE_UTF8
783 size_t len
= length();
786 else if ( nSize
< len
)
789 append(nSize
- len
, ch
);
793 m_impl
.resize(nSize
, (wxStringCharType
)ch
);
796 wxString
substr(size_t nStart
= 0, size_t nLen
= npos
) const
799 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
800 return m_impl
.substr(pos
, len
);
803 // generic attributes & operations
804 // as standard strlen()
805 size_t Len() const { return length(); }
806 // string contains any characters?
807 bool IsEmpty() const { return empty(); }
808 // empty string is "false", so !str will return true
809 bool operator!() const { return empty(); }
810 // truncate the string to given length
811 wxString
& Truncate(size_t uiLen
);
812 // empty string contents
817 wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") );
819 // empty the string and free memory
822 wxString
tmp(wxEmptyString
);
828 bool IsAscii() const;
830 bool IsNumber() const;
834 // data access (all indexes are 0 based)
836 wxUniChar
at(size_t n
) const
837 { return *(begin() + n
); } // FIXME-UTF8: optimize?
838 wxUniChar
GetChar(size_t n
) const
841 wxUniCharRef
at(size_t n
)
842 { return *(begin() + n
); } // FIXME-UTF8: optimize?
843 wxUniCharRef
GetWritableChar(size_t n
)
846 void SetChar(size_t n
, wxUniChar ch
)
849 // get last character
850 wxUniChar
Last() const
852 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
854 return at(length() - 1);
857 // get writable last character
860 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
861 return at(length() - 1);
865 Note that we we must define all of the overloads below to avoid
866 ambiguity when using str[0].
868 wxUniChar
operator[](int n
) const
870 wxUniChar
operator[](long n
) const
872 wxUniChar
operator[](size_t n
) const
874 #ifndef wxSIZE_T_IS_UINT
875 wxUniChar
operator[](unsigned int n
) const
877 #endif // size_t != unsigned int
879 // operator versions of GetWriteableChar()
880 wxUniCharRef
operator[](int n
)
882 wxUniCharRef
operator[](long n
)
884 wxUniCharRef
operator[](size_t n
)
886 #ifndef wxSIZE_T_IS_UINT
887 wxUniCharRef
operator[](unsigned int n
)
889 #endif // size_t != unsigned int
891 // explicit conversion to C string (use this with printf()!)
892 wxCStrData
c_str() const { return wxCStrData(this); }
893 wxCStrData
data() const { return c_str(); }
895 // implicit conversion to C string
896 operator wxCStrData() const { return c_str(); }
897 operator const wxChar
*() const { return c_str(); }
899 // identical to c_str(), for MFC compatibility
900 const wxCStrData
GetData() const { return c_str(); }
902 // explicit conversion to C string in internal representation (char*,
903 // wchar_t*, UTF-8-encoded char*, depending on the build):
904 const_pointer
wx_str() const { return m_impl
.c_str(); }
906 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
907 // converting numbers or strings which are certain not to contain special
908 // chars (typically system functions, X atoms, environment variables etc.)
910 // the behaviour of these functions with the strings containing anything
911 // else than 7 bit ASCII characters is undefined, use at your own risk.
913 static wxString
FromAscii(const char *ascii
); // string
914 static wxString
FromAscii(const char ascii
); // char
915 const wxCharBuffer
ToAscii() const;
917 static wxString
FromAscii(const char *ascii
) { return wxString( ascii
); }
918 static wxString
FromAscii(const char ascii
) { return wxString( ascii
); }
919 const char *ToAscii() const { return c_str(); }
920 #endif // Unicode/!Unicode
922 // conversions with (possible) format conversions: have to return a
923 // buffer with temporary data
925 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
926 // return an ANSI (multibyte) string, wc_str() to return a wide string and
927 // fn_str() to return a string which should be used with the OS APIs
928 // accepting the file names. The return value is always the same, but the
929 // type differs because a function may either return pointer to the buffer
930 // directly or have to use intermediate buffer for translation.
932 const wxCharBuffer
mb_str(const wxMBConv
& conv
= wxConvLibc
) const;
934 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
936 const wxChar
* wc_str() const { return c_str(); }
938 // for compatibility with !wxUSE_UNICODE version
939 const wxChar
* wc_str(const wxMBConv
& WXUNUSED(conv
)) const { return c_str(); }
942 const wxCharBuffer
fn_str() const { return mb_str(wxConvFile
); }
944 const wxChar
* fn_str() const { return c_str(); }
945 #endif // wxMBFILES/!wxMBFILES
947 const wxChar
* mb_str() const { return c_str(); }
949 // for compatibility with wxUSE_UNICODE version
950 const wxChar
* mb_str(const wxMBConv
& WXUNUSED(conv
)) const { return c_str(); }
952 const wxWX2MBbuf
mbc_str() const { return mb_str(); }
955 const wxWCharBuffer
wc_str(const wxMBConv
& conv
) const;
956 #endif // wxUSE_WCHAR_T
958 const wxCharBuffer
fn_str() const { return wxConvFile
.cWC2WX( wc_str( wxConvLocal
) ); }
960 const wxChar
* fn_str() const { return c_str(); }
962 #endif // Unicode/ANSI
964 // overloaded assignment
965 // from another wxString
966 wxString
& operator=(const wxStringImpl
& stringSrc
)
967 { m_impl
= stringSrc
; return *this; }
968 wxString
& operator=(const wxCStrData
& cstr
)
969 { return *this = cstr
.AsString(); }
971 wxString
& operator=(wxUniChar ch
)
972 { m_impl
= EncodeChar(ch
); return *this; }
973 wxString
& operator=(wxUniCharRef ch
)
974 { return operator=((wxUniChar
)ch
); }
975 wxString
& operator=(char ch
)
976 { return operator=(wxUniChar(ch
)); }
977 wxString
& operator=(wchar_t ch
)
978 { return operator=(wxUniChar(ch
)); }
979 // from a C string - STL probably will crash on NULL,
980 // so we need to compensate in that case
981 #if wxUSE_STL_BASED_WXSTRING
982 wxString
& operator=(const wxChar
*psz
)
983 { if(psz
) m_impl
= psz
; else Clear(); return *this; }
985 wxString
& operator=(const wxChar
*psz
)
986 { m_impl
= psz
; return *this; }
990 // from wxWCharBuffer
991 wxString
& operator=(const wxWCharBuffer
& s
)
992 { (void) operator=((const wchar_t *)s
); return *this; }
994 wxString
& operator=(const char* psz
)
995 { return operator=(wxString(psz
)); }
997 // from another kind of C string
998 wxString
& operator=(const unsigned char* psz
);
1000 // from a wide string
1001 wxString
& operator=(const wchar_t *pwz
);
1003 // from wxCharBuffer
1004 wxString
& operator=(const wxCharBuffer
& psz
)
1005 { (void) operator=((const char *)psz
); return *this; }
1006 #endif // Unicode/ANSI
1008 // string concatenation
1009 // in place concatenation
1011 Concatenate and return the result. Note that the left to right
1012 associativity of << allows to write things like "str << str1 << str2
1013 << ..." (unlike with +=)
1016 wxString
& operator<<(const wxString
& s
)
1018 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1019 wxASSERT_MSG( s
.IsValid(),
1020 _T("did you forget to call UngetWriteBuf()?") );
1026 // string += C string
1027 wxString
& operator<<(const char *psz
)
1028 { append(psz
); return *this; }
1029 wxString
& operator<<(const wchar_t *pwz
)
1030 { append(pwz
); return *this; }
1031 wxString
& operator<<(const wxCStrData
& psz
)
1032 { append(psz
.AsString()); return *this; }
1034 wxString
& operator<<(wxUniChar ch
) { append(1, ch
); return *this; }
1035 wxString
& operator<<(wxUniCharRef ch
) { append(1, ch
); return *this; }
1036 wxString
& operator<<(char ch
) { append(1, ch
); return *this; }
1037 wxString
& operator<<(wchar_t ch
) { append(1, ch
); return *this; }
1039 // string += buffer (i.e. from wxGetString)
1040 wxString
& operator<<(const wxWCharBuffer
& s
)
1041 { return operator<<((const wchar_t *)s
); }
1042 wxString
& operator+=(const wxWCharBuffer
& s
)
1043 { return operator<<((const wchar_t *)s
); }
1045 wxString
& operator<<(const wxCharBuffer
& s
)
1046 { return operator<<((const char *)s
); }
1047 wxString
& operator+=(const wxCharBuffer
& s
)
1048 { return operator<<((const char *)s
); }
1050 // string += C string
1051 wxString
& Append(const wxString
& s
)
1053 // test for empty() to share the string if possible
1060 wxString
& Append(const wxCStrData
& psz
)
1061 { append(psz
); return *this; }
1062 wxString
& Append(const char* psz
)
1063 { append(psz
); return *this; }
1064 wxString
& Append(const wchar_t* pwz
)
1065 { append(pwz
); return *this; }
1066 // append count copies of given character
1067 wxString
& Append(wxUniChar ch
, size_t count
= 1u)
1068 { append(count
, ch
); return *this; }
1069 wxString
& Append(wxUniCharRef ch
, size_t count
= 1u)
1070 { append(count
, ch
); return *this; }
1071 wxString
& Append(char ch
, size_t count
= 1u)
1072 { append(count
, ch
); return *this; }
1073 wxString
& Append(wchar_t ch
, size_t count
= 1u)
1074 { append(count
, ch
); return *this; }
1075 wxString
& Append(const char* psz
, size_t nLen
)
1076 { append(psz
, nLen
); return *this; }
1077 wxString
& Append(const wchar_t* pwz
, size_t nLen
)
1078 { append(pwz
, nLen
); return *this; }
1080 // prepend a string, return the string itself
1081 wxString
& Prepend(const wxString
& str
)
1082 { *this = str
+ *this; return *this; }
1084 // non-destructive concatenation
1086 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
,
1087 const wxString
& string2
);
1088 // string with a single char
1089 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
1090 // char with a string
1091 friend wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
1092 // string with C string
1093 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
1095 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
1096 const wchar_t *pwz
);
1097 // C string with string
1098 friend wxString WXDLLIMPEXP_BASE
operator+(const char *psz
,
1099 const wxString
& string
);
1100 friend wxString WXDLLIMPEXP_BASE
operator+(const wchar_t *pwz
,
1101 const wxString
& string
);
1103 // stream-like functions
1104 // insert an int into string
1105 wxString
& operator<<(int i
)
1106 { return (*this) << Format(_T("%d"), i
); }
1107 // insert an unsigned int into string
1108 wxString
& operator<<(unsigned int ui
)
1109 { return (*this) << Format(_T("%u"), ui
); }
1110 // insert a long into string
1111 wxString
& operator<<(long l
)
1112 { return (*this) << Format(_T("%ld"), l
); }
1113 // insert an unsigned long into string
1114 wxString
& operator<<(unsigned long ul
)
1115 { return (*this) << Format(_T("%lu"), ul
); }
1116 #if defined wxLongLong_t && !defined wxLongLongIsLong
1117 // insert a long long if they exist and aren't longs
1118 wxString
& operator<<(wxLongLong_t ll
)
1120 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("d");
1121 return (*this) << Format(fmt
, ll
);
1123 // insert an unsigned long long
1124 wxString
& operator<<(wxULongLong_t ull
)
1126 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("u");
1127 return (*this) << Format(fmt
, ull
);
1130 // insert a float into string
1131 wxString
& operator<<(float f
)
1132 { return (*this) << Format(_T("%f"), f
); }
1133 // insert a double into string
1134 wxString
& operator<<(double d
)
1135 { return (*this) << Format(_T("%g"), d
); }
1137 // string comparison
1138 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
1139 int Cmp(const char *psz
) const
1140 { return compare(psz
); }
1141 int Cmp(const wchar_t *pwz
) const
1142 { return compare(pwz
); }
1143 int Cmp(const wxString
& s
) const
1144 { return compare(s
); }
1145 // same as Cmp() but not case-sensitive
1146 int CmpNoCase(const wxString
& s
) const;
1147 int CmpNoCase(const char *psz
) const
1148 { return CmpNoCase(wxString(psz
)); }
1149 int CmpNoCase(const wchar_t *pwz
) const
1150 { return CmpNoCase(wxString(pwz
)); }
1151 // test for the string equality, either considering case or not
1152 // (if compareWithCase then the case matters)
1153 bool IsSameAs(const char *psz
, bool compareWithCase
= true) const
1154 { return (compareWithCase
? Cmp(psz
) : CmpNoCase(psz
)) == 0; }
1155 bool IsSameAs(const wchar_t *pwz
, bool compareWithCase
= true) const
1156 { return (compareWithCase
? Cmp(pwz
) : CmpNoCase(pwz
)) == 0; }
1157 // comparison with a single character: returns true if equal
1158 bool IsSameAs(wxUniChar c
, bool compareWithCase
= true) const
1160 return (length() == 1) && (compareWithCase
? GetChar(0u) == c
1161 : wxToupper(GetChar(0u)) == wxToupper(c
));
1164 // simple sub-string extraction
1165 // return substring starting at nFirst of length nCount (or till the end
1166 // if nCount = default value)
1167 wxString
Mid(size_t nFirst
, size_t nCount
= npos
) const;
1169 // operator version of Mid()
1170 wxString
operator()(size_t start
, size_t len
) const
1171 { return Mid(start
, len
); }
1173 // check if the string starts with the given prefix and return the rest
1174 // of the string in the provided pointer if it is not NULL; otherwise
1176 bool StartsWith(const wxChar
*prefix
, wxString
*rest
= NULL
) const;
1177 // check if the string ends with the given suffix and return the
1178 // beginning of the string before the suffix in the provided pointer if
1179 // it is not NULL; otherwise return false
1180 bool EndsWith(const wxChar
*suffix
, wxString
*rest
= NULL
) const;
1182 // get first nCount characters
1183 wxString
Left(size_t nCount
) const;
1184 // get last nCount characters
1185 wxString
Right(size_t nCount
) const;
1186 // get all characters before the first occurance of ch
1187 // (returns the whole string if ch not found)
1188 wxString
BeforeFirst(wxUniChar ch
) const;
1189 // get all characters before the last occurence of ch
1190 // (returns empty string if ch not found)
1191 wxString
BeforeLast(wxUniChar ch
) const;
1192 // get all characters after the first occurence of ch
1193 // (returns empty string if ch not found)
1194 wxString
AfterFirst(wxUniChar ch
) const;
1195 // get all characters after the last occurence of ch
1196 // (returns the whole string if ch not found)
1197 wxString
AfterLast(wxUniChar ch
) const;
1199 // for compatibility only, use more explicitly named functions above
1200 wxString
Before(wxUniChar ch
) const { return BeforeLast(ch
); }
1201 wxString
After(wxUniChar ch
) const { return AfterFirst(ch
); }
1204 // convert to upper case in place, return the string itself
1205 wxString
& MakeUpper();
1206 // convert to upper case, return the copy of the string
1207 // Here's something to remember: BC++ doesn't like returns in inlines.
1208 wxString
Upper() const ;
1209 // convert to lower case in place, return the string itself
1210 wxString
& MakeLower();
1211 // convert to lower case, return the copy of the string
1212 wxString
Lower() const ;
1214 // trimming/padding whitespace (either side) and truncating
1215 // remove spaces from left or from right (default) side
1216 wxString
& Trim(bool bFromRight
= true);
1217 // add nCount copies chPad in the beginning or at the end (default)
1218 wxString
& Pad(size_t nCount
, wxUniChar chPad
= wxT(' '), bool bFromRight
= true);
1220 // searching and replacing
1221 // searching (return starting index, or -1 if not found)
1222 int Find(wxUniChar ch
, bool bFromEnd
= false) const; // like strchr/strrchr
1223 // searching (return starting index, or -1 if not found)
1224 int Find(const wxChar
*pszSub
) const; // like strstr
1225 // replace first (or all of bReplaceAll) occurences of substring with
1226 // another string, returns the number of replacements made
1227 size_t Replace(const wxChar
*szOld
,
1228 const wxChar
*szNew
,
1229 bool bReplaceAll
= true);
1231 // check if the string contents matches a mask containing '*' and '?'
1232 bool Matches(const wxChar
*szMask
) const;
1234 // conversion to numbers: all functions return true only if the whole
1235 // string is a number and put the value of this number into the pointer
1236 // provided, the base is the numeric base in which the conversion should be
1237 // done and must be comprised between 2 and 36 or be 0 in which case the
1238 // standard C rules apply (leading '0' => octal, "0x" => hex)
1239 // convert to a signed integer
1240 bool ToLong(long *val
, int base
= 10) const;
1241 // convert to an unsigned integer
1242 bool ToULong(unsigned long *val
, int base
= 10) const;
1243 // convert to wxLongLong
1244 #if defined(wxLongLong_t)
1245 bool ToLongLong(wxLongLong_t
*val
, int base
= 10) const;
1246 // convert to wxULongLong
1247 bool ToULongLong(wxULongLong_t
*val
, int base
= 10) const;
1248 #endif // wxLongLong_t
1249 // convert to a double
1250 bool ToDouble(double *val
) const;
1253 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1254 // formatted input/output
1255 // as sprintf(), returns the number of characters written or < 0 on error
1256 // (take 'this' into account in attribute parameter count)
1257 // int Printf(const wxChar *pszFormat, ...);
1258 WX_DEFINE_VARARG_FUNC(int, Printf
, DoPrintf
)
1259 #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN
1260 // as vprintf(), returns the number of characters written or < 0 on error
1261 int PrintfV(const wxString
& format
, va_list argptr
);
1263 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1264 // returns the string containing the result of Printf() to it
1265 // static wxString Format(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_1;
1266 WX_DEFINE_VARARG_FUNC(static wxString
, Format
, DoFormat
)
1268 // the same as above, but takes a va_list
1269 static wxString
FormatV(const wxString
& format
, va_list argptr
);
1271 // raw access to string memory
1272 // ensure that string has space for at least nLen characters
1273 // only works if the data of this string is not shared
1274 bool Alloc(size_t nLen
) { reserve(nLen
); /*return capacity() >= nLen;*/ return true; }
1275 // minimize the string's memory
1276 // only works if the data of this string is not shared
1278 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1279 // These are deprecated, use wxStringBuffer or wxStringBufferLength instead
1281 // get writable buffer of at least nLen bytes. Unget() *must* be called
1282 // a.s.a.p. to put string back in a reasonable state!
1283 wxDEPRECATED( wxChar
*GetWriteBuf(size_t nLen
) );
1284 // call this immediately after GetWriteBuf() has been used
1285 wxDEPRECATED( void UngetWriteBuf() );
1286 wxDEPRECATED( void UngetWriteBuf(size_t nLen
) );
1287 #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && wxUSE_UNICODE_UTF8
1289 // wxWidgets version 1 compatibility functions
1292 wxString
SubString(size_t from
, size_t to
) const
1293 { return Mid(from
, (to
- from
+ 1)); }
1294 // values for second parameter of CompareTo function
1295 enum caseCompare
{exact
, ignoreCase
};
1296 // values for first parameter of Strip function
1297 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
1299 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1301 // (take 'this' into account in attribute parameter count)
1302 // int sprintf(const wxChar *pszFormat, ...) ATTRIBUTE_PRINTF_2;
1303 WX_DEFINE_VARARG_FUNC(int, sprintf
, DoPrintf
)
1304 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
1307 inline int CompareTo(const wxChar
* psz
, caseCompare cmp
= exact
) const
1308 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
1311 size_t Length() const { return length(); }
1312 // Count the number of characters
1313 int Freq(wxUniChar ch
) const;
1315 void LowerCase() { MakeLower(); }
1317 void UpperCase() { MakeUpper(); }
1318 // use Trim except that it doesn't change this string
1319 wxString
Strip(stripType w
= trailing
) const;
1321 // use Find (more general variants not yet supported)
1322 size_t Index(const wxChar
* psz
) const { return Find(psz
); }
1323 size_t Index(wxUniChar ch
) const { return Find(ch
); }
1325 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
1326 wxString
& RemoveLast(size_t n
= 1) { return Truncate(length() - n
); }
1328 wxString
& Remove(size_t nStart
, size_t nLen
)
1329 { return (wxString
&)erase( nStart
, nLen
); }
1332 int First( wxUniChar ch
) const { return Find(ch
); }
1333 int First( char ch
) const { return Find(ch
); }
1334 int First( wchar_t ch
) const { return Find(ch
); }
1335 int First( const wxChar
* psz
) const { return Find(psz
); }
1336 int First( const wxString
&str
) const { return Find(str
); }
1337 int Last( wxUniChar ch
) const { return Find(ch
, true); }
1338 bool Contains(const wxString
& str
) const { return Find(str
) != wxNOT_FOUND
; }
1341 bool IsNull() const { return empty(); }
1343 // std::string compatibility functions
1345 // take nLen chars starting at nPos
1346 wxString(const wxString
& str
, size_t nPos
, size_t nLen
)
1347 : m_impl(str
.m_impl
, nPos
, nLen
) { }
1348 // take all characters from pStart to pEnd
1349 wxString(const void *pStart
, const void *pEnd
)
1350 : m_impl((const wxChar
*)pStart
, (const wxChar
*)pEnd
) { }
1351 wxString(const_iterator first
, const_iterator last
)
1352 : m_impl(first
, last
) { }
1353 wxString(iterator first
, iterator last
)
1354 : m_impl(first
, last
) { }
1356 // lib.string.modifiers
1357 // append elements str[pos], ..., str[pos+n]
1358 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
1361 str
.PosLenToImpl(pos
, n
, &from
, &len
);
1362 m_impl
.append(str
.m_impl
, from
, len
);
1366 wxString
& append(const wxString
& str
)
1367 { m_impl
.append(str
.m_impl
); return *this; }
1368 wxString
& append(const wxCStrData
& str
)
1369 { m_impl
.append(str
.AsString().m_impl
); return *this; }
1370 // append first n (or all if n == npos) characters of sz
1371 wxString
& append(const char *sz
)
1372 { m_impl
.append(ImplStr(sz
)); return *this; }
1373 wxString
& append(const wchar_t *sz
)
1374 { m_impl
.append(ImplStr(sz
)); return *this; }
1375 wxString
& append(const char *sz
, size_t n
)
1377 SubstrBufFromMB
str(ImplStr(sz
, n
));
1378 m_impl
.append(str
.data
, str
.len
);
1381 wxString
& append(const wchar_t *sz
, size_t n
)
1383 SubstrBufFromWC
str(ImplStr(sz
, n
));
1384 m_impl
.append(str
.data
, str
.len
);
1387 // append n copies of ch
1388 wxString
& append(size_t n
, wxUniChar ch
)
1390 #if wxUSE_UNICODE_UTF8
1391 if ( !ch
.IsAscii() )
1392 m_impl
.append(EncodeNChars(n
, ch
));
1395 m_impl
.append(n
, (wxStringCharType
)ch
);
1398 // append from first to last
1399 wxString
& append(const_iterator first
, const_iterator last
)
1400 { m_impl
.append(first
, last
); return *this; }
1402 // same as `this_string = str'
1403 wxString
& assign(const wxString
& str
)
1404 { m_impl
= str
.m_impl
; return *this; }
1405 // same as ` = str[pos..pos + n]
1406 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
1409 str
.PosLenToImpl(pos
, n
, &from
, &len
);
1410 m_impl
.assign(str
.m_impl
, from
, len
);
1413 // same as `= first n (or all if n == npos) characters of sz'
1414 wxString
& assign(const char *sz
)
1415 { m_impl
.assign(ImplStr(sz
)); return *this; }
1416 wxString
& assign(const wchar_t *sz
)
1417 { m_impl
.assign(ImplStr(sz
)); return *this; }
1418 wxString
& assign(const char *sz
, size_t n
)
1420 SubstrBufFromMB
str(ImplStr(sz
, n
));
1421 m_impl
.assign(str
.data
, str
.len
);
1424 wxString
& assign(const wchar_t *sz
, size_t n
)
1426 SubstrBufFromWC
str(ImplStr(sz
, n
));
1427 m_impl
.assign(str
.data
, str
.len
);
1430 // same as `= n copies of ch'
1431 wxString
& assign(size_t n
, wxUniChar ch
)
1433 #if wxUSE_UNICODE_UTF8
1434 if ( !ch
.IsAscii() )
1435 m_impl
.assign(EncodeNChars(n
, ch
));
1438 m_impl
.assign(n
, (wxStringCharType
)ch
);
1441 // assign from first to last
1442 wxString
& assign(const_iterator first
, const_iterator last
)
1443 { m_impl
.assign(first
, last
); return *this; }
1445 // string comparison
1446 int compare(const wxString
& str
) const;
1447 // comparison with a substring
1448 int compare(size_t nStart
, size_t nLen
, const wxString
& str
) const;
1449 // comparison of 2 substrings
1450 int compare(size_t nStart
, size_t nLen
,
1451 const wxString
& str
, size_t nStart2
, size_t nLen2
) const;
1452 // just like strcmp()
1453 int compare(const char* sz
) const;
1454 int compare(const wchar_t* sz
) const;
1455 // substring comparison with first nCount characters of sz
1456 int compare(size_t nStart
, size_t nLen
,
1457 const char* sz
, size_t nCount
= npos
) const;
1458 int compare(size_t nStart
, size_t nLen
,
1459 const wchar_t* sz
, size_t nCount
= npos
) const;
1461 // insert another string
1462 wxString
& insert(size_t nPos
, const wxString
& str
)
1463 { insert(begin() + nPos
, str
.begin(), str
.end()); return *this; }
1464 // insert n chars of str starting at nStart (in str)
1465 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
1468 str
.PosLenToImpl(nStart
, n
, &from
, &len
);
1469 m_impl
.insert(PosToImpl(nPos
), str
.m_impl
, from
, len
);
1472 // insert first n (or all if n == npos) characters of sz
1473 wxString
& insert(size_t nPos
, const char *sz
)
1474 { m_impl
.insert(PosToImpl(nPos
), ImplStr(sz
)); return *this; }
1475 wxString
& insert(size_t nPos
, const wchar_t *sz
)
1476 { m_impl
.insert(PosToImpl(nPos
), ImplStr(sz
)); return *this; }
1477 wxString
& insert(size_t nPos
, const char *sz
, size_t n
)
1479 SubstrBufFromMB
str(ImplStr(sz
, n
));
1480 m_impl
.insert(PosToImpl(nPos
), str
.data
, str
.len
);
1483 wxString
& insert(size_t nPos
, const wchar_t *sz
, size_t n
)
1485 SubstrBufFromWC
str(ImplStr(sz
, n
));
1486 m_impl
.insert(PosToImpl(nPos
), str
.data
, str
.len
);
1489 // insert n copies of ch
1490 wxString
& insert(size_t nPos
, size_t n
, wxUniChar ch
)
1492 #if wxUSE_UNICODE_UTF8
1493 if ( !ch
.IsAscii() )
1494 m_impl
.insert(begin() + nPos
, EncodeNChars(n
, ch
));
1497 m_impl
.insert(begin() + nPos
, n
, (wxStringCharType
)ch
);
1500 iterator
insert(iterator it
, wxUniChar ch
)
1501 { return iterator(m_impl
.insert(it
, EncodeChar(ch
))); }
1502 void insert(iterator it
, const_iterator first
, const_iterator last
)
1503 { m_impl
.insert(it
, first
, last
); }
1504 void insert(iterator it
, size_type n
, wxUniChar ch
)
1506 #if wxUSE_UNICODE_UTF8
1507 if ( !ch
.IsAscii() )
1508 m_impl
.insert(it
, EncodeNChars(n
, ch
));
1511 m_impl
.insert(it
, n
, (wxStringCharType
)ch
);
1514 // delete characters from nStart to nStart + nLen
1515 wxString
& erase(size_type pos
= 0, size_type n
= npos
)
1518 PosLenToImpl(pos
, n
, &from
, &len
);
1519 m_impl
.erase(from
, len
);
1522 iterator
erase(iterator first
, iterator last
)
1523 { return iterator(m_impl
.erase(first
, last
)); }
1524 iterator
erase(iterator first
)
1525 { return iterator(m_impl
.erase(first
)); }
1527 #ifdef wxSTRING_BASE_HASNT_CLEAR
1528 void clear() { erase(); }
1530 void clear() { m_impl
.clear(); }
1533 // replaces the substring of length nLen starting at nStart
1534 wxString
& replace(size_t nStart
, size_t nLen
, const char* sz
)
1537 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1538 m_impl
.replace(from
, len
, ImplStr(sz
));
1541 wxString
& replace(size_t nStart
, size_t nLen
, const wchar_t* sz
)
1544 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1545 m_impl
.replace(from
, len
, ImplStr(sz
));
1548 // replaces the substring of length nLen starting at nStart
1549 wxString
& replace(size_t nStart
, size_t nLen
, const wxString
& str
)
1552 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1553 m_impl
.replace(from
, len
, str
.m_impl
);
1556 // replaces the substring with nCount copies of ch
1557 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxUniChar ch
)
1560 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1561 #if wxUSE_UNICODE_UTF8
1562 if ( !ch
.IsAscii() )
1563 m_impl
.replace(from
, len
, EncodeNChars(nCount
, ch
));
1566 m_impl
.replace(from
, len
, nCount
, (wxStringCharType
)ch
);
1569 // replaces a substring with another substring
1570 wxString
& replace(size_t nStart
, size_t nLen
,
1571 const wxString
& str
, size_t nStart2
, size_t nLen2
)
1574 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1577 str
.PosLenToImpl(nStart2
, nLen2
, &from2
, &len2
);
1579 m_impl
.replace(from
, len
, str
.m_impl
, from2
, len2
);
1582 // replaces the substring with first nCount chars of sz
1583 wxString
& replace(size_t nStart
, size_t nLen
,
1584 const char* sz
, size_t nCount
)
1587 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1589 SubstrBufFromMB
str(ImplStr(sz
, nCount
));
1591 m_impl
.replace(from
, len
, str
.data
, str
.len
);
1594 wxString
& replace(size_t nStart
, size_t nLen
,
1595 const wchar_t* sz
, size_t nCount
)
1598 PosLenToImpl(nStart
, nLen
, &from
, &len
);
1600 SubstrBufFromWC
str(ImplStr(sz
, nCount
));
1602 m_impl
.replace(from
, len
, str
.data
, str
.len
);
1605 wxString
& replace(iterator first
, iterator last
, const char* s
)
1606 { m_impl
.replace(first
, last
, ImplStr(s
)); return *this; }
1607 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
)
1608 { m_impl
.replace(first
, last
, ImplStr(s
)); return *this; }
1609 wxString
& replace(iterator first
, iterator last
, const char* s
, size_type n
)
1611 SubstrBufFromMB
str(ImplStr(s
, n
));
1612 m_impl
.replace(first
, last
, str
.data
, str
.len
);
1615 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
, size_type n
)
1617 SubstrBufFromWC
str(ImplStr(s
, n
));
1618 m_impl
.replace(first
, last
, str
.data
, str
.len
);
1621 wxString
& replace(iterator first
, iterator last
, const wxString
& s
)
1622 { m_impl
.replace(first
, last
, s
.m_impl
); return *this; }
1623 wxString
& replace(iterator first
, iterator last
, size_type n
, wxUniChar ch
)
1625 #if wxUSE_UNICODE_UTF8
1626 if ( !ch
.IsAscii() )
1627 m_impl
.replace(first
, last
, EncodeNChars(n
, ch
));
1630 m_impl
.replace(first
, last
, n
, (wxStringCharType
)ch
);
1633 wxString
& replace(iterator first
, iterator last
,
1634 const_iterator first1
, const_iterator last1
)
1635 { m_impl
.replace(first
, last
, first1
, last1
); return *this; }
1638 void swap(wxString
& str
)
1639 { m_impl
.swap(str
.m_impl
); }
1642 size_t find(const wxString
& str
, size_t nStart
= 0) const
1643 { return PosFromImpl(m_impl
.find(str
.m_impl
, PosToImpl(nStart
))); }
1645 // find first n characters of sz
1646 size_t find(const char* sz
, size_t nStart
= 0, size_t n
= npos
) const
1648 SubstrBufFromMB
str(ImplStr(sz
, n
));
1649 return PosFromImpl(m_impl
.find(str
.data
, PosToImpl(nStart
), str
.len
));
1651 size_t find(const wchar_t* sz
, size_t nStart
= 0, size_t n
= npos
) const
1653 SubstrBufFromWC
str(ImplStr(sz
, n
));
1654 return PosFromImpl(m_impl
.find(str
.data
, PosToImpl(nStart
), str
.len
));
1657 // find the first occurence of character ch after nStart
1658 size_t find(wxUniChar ch
, size_t nStart
= 0) const
1659 { return PosFromImpl(m_impl
.find(EncodeChar(ch
), PosToImpl(nStart
))); }
1660 size_t find(wxUniCharRef ch
, size_t nStart
= 0) const
1661 { return find(wxUniChar(ch
), nStart
); }
1662 size_t find(char ch
, size_t nStart
= 0) const
1663 { return find(wxUniChar(ch
), nStart
); }
1664 size_t find(wchar_t ch
, size_t nStart
= 0) const
1665 { return find(wxUniChar(ch
), nStart
); }
1667 // rfind() family is exactly like find() but works right to left
1669 // as find, but from the end
1670 size_t rfind(const wxString
& str
, size_t nStart
= npos
) const
1671 { return PosFromImpl(m_impl
.rfind(str
.m_impl
, PosToImpl(nStart
))); }
1673 // as find, but from the end
1674 size_t rfind(const char* sz
, size_t nStart
= npos
, size_t n
= npos
) const
1676 SubstrBufFromMB
str(ImplStr(sz
, n
));
1677 return PosFromImpl(m_impl
.rfind(str
.data
, PosToImpl(nStart
), str
.len
));
1679 size_t rfind(const wchar_t* sz
, size_t nStart
= npos
, size_t n
= npos
) const
1681 SubstrBufFromWC
str(ImplStr(sz
, n
));
1682 return PosFromImpl(m_impl
.rfind(str
.data
, PosToImpl(nStart
), str
.len
));
1684 // as find, but from the end
1685 size_t rfind(wxUniChar ch
, size_t nStart
= npos
) const
1686 { return PosFromImpl(m_impl
.rfind(EncodeChar(ch
), PosToImpl(nStart
))); }
1687 size_t rfind(wxUniCharRef ch
, size_t nStart
= npos
) const
1688 { return rfind(wxUniChar(ch
), nStart
); }
1689 size_t rfind(char ch
, size_t nStart
= npos
) const
1690 { return rfind(wxUniChar(ch
), nStart
); }
1691 size_t rfind(wchar_t ch
, size_t nStart
= npos
) const
1692 { return rfind(wxUniChar(ch
), nStart
); }
1694 // find first/last occurence of any character (not) in the set:
1695 #if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1696 // FIXME-UTF8: this is not entirely correct, because it doesn't work if
1697 // sizeof(wchar_t)==2 and surrogates are present in the string;
1698 // should we care? Probably not.
1699 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
1700 { return m_impl
.find_first_of(str
.m_impl
, nStart
); }
1701 size_t find_first_of(const char* sz
, size_t nStart
= 0) const
1702 { return m_impl
.find_first_of(ImplStr(sz
), nStart
); }
1703 size_t find_first_of(const wchar_t* sz
, size_t nStart
= 0) const
1704 { return m_impl
.find_first_of(ImplStr(sz
), nStart
); }
1705 size_t find_first_of(const char* sz
, size_t nStart
, size_t n
) const
1706 { return m_impl
.find_first_of(ImplStr(sz
), nStart
, n
); }
1707 size_t find_first_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
1708 { return m_impl
.find_first_of(ImplStr(sz
), nStart
, n
); }
1709 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const
1710 { return m_impl
.find_first_of((wxChar
)c
, nStart
); }
1712 size_t find_last_of(const wxString
& str
, size_t nStart
= npos
) const
1713 { return m_impl
.find_last_of(str
.m_impl
, nStart
); }
1714 size_t find_last_of(const char* sz
, size_t nStart
= npos
) const
1715 { return m_impl
.find_last_of(ImplStr(sz
), nStart
); }
1716 size_t find_last_of(const wchar_t* sz
, size_t nStart
= npos
) const
1717 { return m_impl
.find_last_of(ImplStr(sz
), nStart
); }
1718 size_t find_last_of(const char* sz
, size_t nStart
, size_t n
) const
1719 { return m_impl
.find_last_of(ImplStr(sz
), nStart
, n
); }
1720 size_t find_last_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
1721 { return m_impl
.find_last_of(ImplStr(sz
), nStart
, n
); }
1722 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const
1723 { return m_impl
.find_last_of((wxChar
)c
, nStart
); }
1725 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
1726 { return m_impl
.find_first_not_of(str
.m_impl
, nStart
); }
1727 size_t find_first_not_of(const char* sz
, size_t nStart
= 0) const
1728 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
); }
1729 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
= 0) const
1730 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
); }
1731 size_t find_first_not_of(const char* sz
, size_t nStart
, size_t n
) const
1732 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
, n
); }
1733 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
1734 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
, n
); }
1735 size_t find_first_not_of(wxUniChar c
, size_t nStart
= 0) const
1736 { return m_impl
.find_first_not_of((wxChar
)c
, nStart
); }
1738 size_t find_last_not_of(const wxString
& str
, size_t nStart
= npos
) const
1739 { return m_impl
.find_last_not_of(str
.m_impl
, nStart
); }
1740 size_t find_last_not_of(const char* sz
, size_t nStart
= npos
) const
1741 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
); }
1742 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
= npos
) const
1743 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
); }
1744 size_t find_last_not_of(const char* sz
, size_t nStart
, size_t n
) const
1745 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
, n
); }
1746 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
1747 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
, n
); }
1748 size_t find_last_not_of(wxUniChar c
, size_t nStart
= npos
) const
1749 { return m_impl
.find_last_not_of((wxChar
)c
, nStart
); }
1751 // we can't use std::string implementation in UTF-8 build, because the
1752 // character sets would be interpreted wrongly:
1754 // as strpbrk() but starts at nStart, returns npos if not found
1755 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
1756 { return find_first_of((const wxChar
*)str
.c_str(), nStart
); }
1758 size_t find_first_of(const char* sz
, size_t nStart
= 0) const;
1759 size_t find_first_of(const wchar_t* sz
, size_t nStart
= 0) const;
1760 size_t find_first_of(const char* sz
, size_t nStart
, size_t n
) const;
1761 size_t find_first_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
1762 // same as find(char, size_t)
1763 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const
1764 { return find(c
, nStart
); }
1765 // find the last (starting from nStart) char from str in this string
1766 size_t find_last_of (const wxString
& str
, size_t nStart
= npos
) const
1767 { return find_last_of((const wxChar
*)str
.c_str(), nStart
); }
1769 size_t find_last_of (const char* sz
, size_t nStart
= npos
) const;
1770 size_t find_last_of (const wchar_t* sz
, size_t nStart
= npos
) const;
1771 size_t find_last_of(const char* sz
, size_t nStart
, size_t n
) const;
1772 size_t find_last_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
1774 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const
1775 { return rfind(c
, nStart
); }
1777 // find first/last occurence of any character not in the set
1779 // as strspn() (starting from nStart), returns npos on failure
1780 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
1781 { return find_first_not_of((const wxChar
*)str
.c_str(), nStart
); }
1783 size_t find_first_not_of(const char* sz
, size_t nStart
= 0) const;
1784 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
= 0) const;
1785 size_t find_first_not_of(const char* sz
, size_t nStart
, size_t n
) const;
1786 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
1788 size_t find_first_not_of(wxUniChar ch
, size_t nStart
= 0) const;
1790 size_t find_last_not_of(const wxString
& str
, size_t nStart
= npos
) const
1791 { return find_last_not_of((const wxChar
*)str
.c_str(), nStart
); }
1793 size_t find_last_not_of(const char* sz
, size_t nStart
= npos
) const;
1794 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
= npos
) const;
1795 size_t find_last_not_of(const char* sz
, size_t nStart
, size_t n
) const;
1796 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
1798 size_t find_last_not_of(wxUniChar ch
, size_t nStart
= npos
) const;
1799 #endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not
1801 // provide char/wchar_t/wxUniCharRef overloads for char-finding functions
1802 // above to resolve ambiguities:
1803 size_t find_first_of(wxUniCharRef ch
, size_t nStart
= 0) const
1804 { return find_first_of(wxUniChar(ch
), nStart
); }
1805 size_t find_first_of(char ch
, size_t nStart
= 0) const
1806 { return find_first_of(wxUniChar(ch
), nStart
); }
1807 size_t find_first_of(wchar_t ch
, size_t nStart
= 0) const
1808 { return find_first_of(wxUniChar(ch
), nStart
); }
1809 size_t find_last_of(wxUniCharRef ch
, size_t nStart
= npos
) const
1810 { return find_last_of(wxUniChar(ch
), nStart
); }
1811 size_t find_last_of(char ch
, size_t nStart
= npos
) const
1812 { return find_last_of(wxUniChar(ch
), nStart
); }
1813 size_t find_last_of(wchar_t ch
, size_t nStart
= npos
) const
1814 { return find_last_of(wxUniChar(ch
), nStart
); }
1815 size_t find_first_not_of(wxUniCharRef ch
, size_t nStart
= 0) const
1816 { return find_first_not_of(wxUniChar(ch
), nStart
); }
1817 size_t find_first_not_of(char ch
, size_t nStart
= 0) const
1818 { return find_first_not_of(wxUniChar(ch
), nStart
); }
1819 size_t find_first_not_of(wchar_t ch
, size_t nStart
= 0) const
1820 { return find_first_not_of(wxUniChar(ch
), nStart
); }
1821 size_t find_last_not_of(wxUniCharRef ch
, size_t nStart
= npos
) const
1822 { return find_last_not_of(wxUniChar(ch
), nStart
); }
1823 size_t find_last_not_of(char ch
, size_t nStart
= npos
) const
1824 { return find_last_not_of(wxUniChar(ch
), nStart
); }
1825 size_t find_last_not_of(wchar_t ch
, size_t nStart
= npos
) const
1826 { return find_last_not_of(wxUniChar(ch
), nStart
); }
1829 wxString
& operator+=(const wxString
& s
)
1830 { m_impl
+= s
.m_impl
; return *this; }
1831 // string += C string
1832 wxString
& operator+=(const char *psz
)
1833 { m_impl
+= ImplStr(psz
); return *this; }
1834 wxString
& operator+=(const wchar_t *pwz
)
1835 { m_impl
+= ImplStr(pwz
); return *this; }
1836 wxString
& operator+=(const wxCStrData
& s
)
1837 { m_impl
+= s
.AsString().m_impl
; return *this; }
1839 wxString
& operator+=(wxUniChar ch
)
1840 { m_impl
+= EncodeChar(ch
); return *this; }
1841 wxString
& operator+=(wxUniCharRef ch
) { return *this += wxUniChar(ch
); }
1842 wxString
& operator+=(char ch
) { return *this += wxUniChar(ch
); }
1843 wxString
& operator+=(unsigned char ch
) { return *this += wxUniChar(ch
); }
1844 wxString
& operator+=(wchar_t ch
) { return *this += wxUniChar(ch
); }
1847 #if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1848 // helpers for wxStringBuffer and wxStringBufferLength
1849 wxStringCharType
*DoGetWriteBuf(size_t nLen
)
1850 { return m_impl
.DoGetWriteBuf(nLen
); }
1851 void DoUngetWriteBuf()
1852 { m_impl
.DoUngetWriteBuf(); }
1853 void DoUngetWriteBuf(size_t nLen
)
1854 { m_impl
.DoUngetWriteBuf(nLen
); }
1856 friend class WXDLLIMPEXP_BASE wxStringBuffer
;
1857 friend class WXDLLIMPEXP_BASE wxStringBufferLength
;
1858 #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1860 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1861 int DoPrintf(const wxChar
*format
, ...) ATTRIBUTE_PRINTF_2
;
1862 static wxString
DoFormat(const wxChar
*format
, ...) ATTRIBUTE_PRINTF_1
;
1865 #if !wxUSE_STL_BASED_WXSTRING
1866 // check string's data validity
1867 bool IsValid() const { return m_impl
.GetStringData()->IsValid(); }
1871 wxStringImpl m_impl
;
1874 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
1875 #pragma warning (default:4275)
1878 // string iterator operators that satisfy STL Random Access Iterator
1880 inline wxString::iterator
operator+(int n
, wxString::iterator i
)
1882 inline wxString::iterator
operator+(size_t n
, wxString::iterator i
)
1884 inline wxString::const_iterator
operator+(int n
, wxString::const_iterator i
)
1886 inline wxString::const_iterator
operator+(size_t n
, wxString::const_iterator i
)
1888 inline wxString::reverse_iterator
operator+(int n
, wxString::reverse_iterator i
)
1890 inline wxString::reverse_iterator
operator+(size_t n
, wxString::reverse_iterator i
)
1892 inline wxString::const_reverse_iterator
operator+(int n
, wxString::const_reverse_iterator i
)
1894 inline wxString::const_reverse_iterator
operator+(size_t n
, wxString::const_reverse_iterator i
)
1897 // notice that even though for many compilers the friend declarations above are
1898 // enough, from the point of view of C++ standard we must have the declarations
1899 // here as friend ones are not injected in the enclosing namespace and without
1900 // them the code fails to compile with conforming compilers such as xlC or g++4
1901 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
, const wxString
& string2
);
1902 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const char *psz
);
1903 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const wchar_t *pwz
);
1904 wxString WXDLLIMPEXP_BASE
operator+(const char *psz
, const wxString
& string
);
1905 wxString WXDLLIMPEXP_BASE
operator+(const wchar_t *pwz
, const wxString
& string
);
1907 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
1908 wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
1910 inline wxString
operator+(const wxString
& string
, wxUniCharRef ch
)
1911 { return string
+ (wxUniChar
)ch
; }
1912 inline wxString
operator+(const wxString
& string
, char ch
)
1913 { return string
+ wxUniChar(ch
); }
1914 inline wxString
operator+(const wxString
& string
, wchar_t ch
)
1915 { return string
+ wxUniChar(ch
); }
1916 inline wxString
operator+(wxUniCharRef ch
, const wxString
& string
)
1917 { return (wxUniChar
)ch
+ string
; }
1918 inline wxString
operator+(char ch
, const wxString
& string
)
1919 { return wxUniChar(ch
) + string
; }
1920 inline wxString
operator+(wchar_t ch
, const wxString
& string
)
1921 { return wxUniChar(ch
) + string
; }
1924 #if wxUSE_STL_BASED_WXSTRING
1925 // return an empty wxString (not very useful with wxUSE_STL == 1)
1926 inline const wxString
wxGetEmptyString() { return wxString(); }
1927 #else // !wxUSE_STL_BASED_WXSTRING
1928 // return an empty wxString (more efficient than wxString() here)
1929 inline const wxString
& wxGetEmptyString()
1931 return *(wxString
*)&wxEmptyString
;
1933 #endif // wxUSE_STL_BASED_WXSTRING/!wxUSE_STL_BASED_WXSTRING
1935 // ----------------------------------------------------------------------------
1936 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
1937 // ----------------------------------------------------------------------------
1939 #if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
1941 class WXDLLIMPEXP_BASE wxStringBuffer
1944 wxStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
1945 : m_str(str
), m_buf(lenWanted
)
1948 ~wxStringBuffer() { m_str
.assign(m_buf
.data(), wxStrlen(m_buf
.data())); }
1950 operator wxChar
*() { return m_buf
.data(); }
1955 wxWCharBuffer m_buf
;
1960 DECLARE_NO_COPY_CLASS(wxStringBuffer
)
1963 class WXDLLIMPEXP_BASE wxStringBufferLength
1966 wxStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
1967 : m_str(str
), m_buf(lenWanted
), m_len(0), m_lenSet(false)
1970 ~wxStringBufferLength()
1973 m_str
.assign(m_buf
.data(), m_len
);
1976 operator wxChar
*() { return m_buf
.data(); }
1977 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
1982 wxWCharBuffer m_buf
;
1989 DECLARE_NO_COPY_CLASS(wxStringBufferLength
)
1992 #else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1994 class WXDLLIMPEXP_BASE wxStringBuffer
1997 wxStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
1998 : m_str(str
), m_buf(NULL
)
1999 { m_buf
= m_str
.DoGetWriteBuf(lenWanted
); }
2001 ~wxStringBuffer() { m_str
.DoUngetWriteBuf(); }
2003 operator wxChar
*() const { return m_buf
; }
2009 DECLARE_NO_COPY_CLASS(wxStringBuffer
)
2012 class WXDLLIMPEXP_BASE wxStringBufferLength
2015 wxStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
2016 : m_str(str
), m_buf(NULL
), m_len(0), m_lenSet(false)
2018 m_buf
= m_str
.DoGetWriteBuf(lenWanted
);
2019 wxASSERT(m_buf
!= NULL
);
2022 ~wxStringBufferLength()
2025 m_str
.DoUngetWriteBuf(m_len
);
2028 operator wxChar
*() const { return m_buf
; }
2029 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
2037 DECLARE_NO_COPY_CLASS(wxStringBufferLength
)
2040 #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2042 // ---------------------------------------------------------------------------
2043 // wxString comparison functions: operator versions are always case sensitive
2044 // ---------------------------------------------------------------------------
2046 #define wxCMP_WXCHAR_STRING(p, s, op) s.Cmp(p) op 0
2048 wxDEFINE_ALL_COMPARISONS(const wxChar
*, const wxString
&, wxCMP_WXCHAR_STRING
)
2050 #undef wxCMP_WXCHAR_STRING
2052 // note that there is an optimization in operator==() and !=(): we (quickly)
2053 // checks the strings length first, before comparing their data
2054 inline bool operator==(const wxString
& s1
, const wxString
& s2
)
2055 { return (s1
.Len() == s2
.Len()) && (s1
.Cmp(s2
) == 0); }
2056 inline bool operator!=(const wxString
& s1
, const wxString
& s2
)
2057 { return (s1
.Len() != s2
.Len()) || (s1
.Cmp(s2
) != 0); }
2058 inline bool operator< (const wxString
& s1
, const wxString
& s2
)
2059 { return s1
.Cmp(s2
) < 0; }
2060 inline bool operator> (const wxString
& s1
, const wxString
& s2
)
2061 { return s1
.Cmp(s2
) > 0; }
2062 inline bool operator<=(const wxString
& s1
, const wxString
& s2
)
2063 { return s1
.Cmp(s2
) <= 0; }
2064 inline bool operator>=(const wxString
& s1
, const wxString
& s2
)
2065 { return s1
.Cmp(s2
) >= 0; }
2068 inline bool operator==(const wxString
& s1
, const wxWCharBuffer
& s2
)
2069 { return (s1
.Cmp((const wchar_t *)s2
) == 0); }
2070 inline bool operator==(const wxWCharBuffer
& s1
, const wxString
& s2
)
2071 { return (s2
.Cmp((const wchar_t *)s1
) == 0); }
2072 inline bool operator!=(const wxString
& s1
, const wxWCharBuffer
& s2
)
2073 { return (s1
.Cmp((const wchar_t *)s2
) != 0); }
2074 inline bool operator!=(const wxWCharBuffer
& s1
, const wxString
& s2
)
2075 { return (s2
.Cmp((const wchar_t *)s1
) != 0); }
2076 #else // !wxUSE_UNICODE
2077 inline bool operator==(const wxString
& s1
, const wxCharBuffer
& s2
)
2078 { return (s1
.Cmp((const char *)s2
) == 0); }
2079 inline bool operator==(const wxCharBuffer
& s1
, const wxString
& s2
)
2080 { return (s2
.Cmp((const char *)s1
) == 0); }
2081 inline bool operator!=(const wxString
& s1
, const wxCharBuffer
& s2
)
2082 { return (s1
.Cmp((const char *)s2
) != 0); }
2083 inline bool operator!=(const wxCharBuffer
& s1
, const wxString
& s2
)
2084 { return (s2
.Cmp((const char *)s1
) != 0); }
2085 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
2088 inline wxString
operator+(const wxString
& string
, const wxWCharBuffer
& buf
)
2089 { return string
+ (const wchar_t *)buf
; }
2090 inline wxString
operator+(const wxWCharBuffer
& buf
, const wxString
& string
)
2091 { return (const wchar_t *)buf
+ string
; }
2092 #else // !wxUSE_UNICODE
2093 inline wxString
operator+(const wxString
& string
, const wxCharBuffer
& buf
)
2094 { return string
+ (const char *)buf
; }
2095 inline wxString
operator+(const wxCharBuffer
& buf
, const wxString
& string
)
2096 { return (const char *)buf
+ string
; }
2097 #endif // wxUSE_UNICODE/!wxUSE_UNICODE
2099 // comparison with char
2100 inline bool operator==(const wxUniChar
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2101 inline bool operator==(const wxUniCharRef
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2102 inline bool operator==(char c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2103 inline bool operator==(wchar_t c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2104 inline bool operator==(int c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2105 inline bool operator==(const wxString
& s
, const wxUniChar
& c
) { return s
.IsSameAs(c
); }
2106 inline bool operator==(const wxString
& s
, const wxUniCharRef
& c
) { return s
.IsSameAs(c
); }
2107 inline bool operator==(const wxString
& s
, char c
) { return s
.IsSameAs(c
); }
2108 inline bool operator==(const wxString
& s
, wchar_t c
) { return s
.IsSameAs(c
); }
2109 inline bool operator!=(const wxUniChar
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2110 inline bool operator!=(const wxUniCharRef
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2111 inline bool operator!=(char c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2112 inline bool operator!=(wchar_t c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2113 inline bool operator!=(int c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2114 inline bool operator!=(const wxString
& s
, const wxUniChar
& c
) { return !s
.IsSameAs(c
); }
2115 inline bool operator!=(const wxString
& s
, const wxUniCharRef
& c
) { return !s
.IsSameAs(c
); }
2116 inline bool operator!=(const wxString
& s
, char c
) { return !s
.IsSameAs(c
); }
2117 inline bool operator!=(const wxString
& s
, wchar_t c
) { return !s
.IsSameAs(c
); }
2119 // comparison with C string in Unicode build
2122 #define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s
2124 wxDEFINE_ALL_COMPARISONS(const char *, const wxString
&, wxCMP_CHAR_STRING
)
2126 #undef wxCMP_CHAR_STRING
2128 #endif // wxUSE_UNICODE
2130 // we also need to provide the operators for comparison with wxCStrData to
2131 // resolve ambiguity between operator(const wxChar *,const wxString &) and
2132 // operator(const wxChar *, const wxChar *) for "p == s.c_str()"
2134 // notice that these are (shallow) pointer comparisons, not (deep) string ones
2135 #define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar()
2136 #define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar()
2138 // FIXME: these ifdefs must be removed when wxCStrData has both conversions
2140 wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData
&, wxCMP_WCHAR_CSTRDATA
)
2142 wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData
&, wxCMP_CHAR_CSTRDATA
)
2145 #undef wxCMP_CHAR_CSTRDATA
2146 #undef wxCMP_WCHAR_CSTRDATA
2148 // ---------------------------------------------------------------------------
2149 // Implementation only from here until the end of file
2150 // ---------------------------------------------------------------------------
2152 #if wxUSE_STD_IOSTREAM
2154 #include "wx/iosfwrap.h"
2156 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxString
&);
2157 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxCStrData
&);
2159 #endif // wxSTD_STRING_COMPATIBILITY
2161 // ---------------------------------------------------------------------------
2162 // wxCStrData implementation
2163 // ---------------------------------------------------------------------------
2165 inline wxCStrData::wxCStrData(char *buf
)
2166 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
2167 inline wxCStrData::wxCStrData(wchar_t *buf
)
2168 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
2170 inline wxCStrData::~wxCStrData()
2177 inline const wchar_t* wxCStrData::AsWChar() const
2179 inline const char* wxCStrData::AsChar() const
2182 // FIXME-UTF8: incorrect position, incorrect charset
2183 return m_str
->wx_str() + m_offset
;
2186 inline wxString
wxCStrData::AsString() const
2188 if ( m_offset
== 0 )
2191 return m_str
->Mid(m_offset
);
2194 inline wxUniChar
wxCStrData::operator*() const
2196 if ( m_str
->empty() )
2197 return wxUniChar(_T('\0'));
2199 return (*m_str
)[m_offset
];
2202 inline wxUniChar
wxCStrData::operator[](size_t n
) const
2204 return m_str
->at(m_offset
+ n
);
2207 // ----------------------------------------------------------------------------
2208 // implementation of wx[W]CharBuffer inline methods using wxCStrData
2209 // ----------------------------------------------------------------------------
2211 // FIXME-UTF8: move this to buffer.h; provide versions for both variants
2212 inline wxWxCharBuffer::wxWxCharBuffer(const wxCStrData
& cstr
)
2213 : wxCharTypeBufferBase((const wxChar
*)cstr
)
2217 #endif // _WX_WXSTRING_H__