1 ///////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxString class
4 // Author: Vadim Zeitlin
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
13 Efficient string class [more or less] compatible with MFC CString,
14 wxWidgets version 1 wxString and std::string and some handy functions
15 missing from string.h.
18 #ifndef _WX_WXSTRING_H__
19 #define _WX_WXSTRING_H__
21 // ----------------------------------------------------------------------------
23 // ----------------------------------------------------------------------------
25 #include "wx/defs.h" // everybody should include this
27 #if defined(__WXMAC__) || defined(__VISAGECPP__)
31 #if defined(__VISAGECPP__) && __IBMCPP__ >= 400
32 // problem in VACPP V4 with including stdlib.h multiple times
33 // strconv includes it anyway
46 #ifdef HAVE_STRCASECMP_IN_STRINGS_H
47 #include <strings.h> // for strcasecmp()
48 #endif // HAVE_STRCASECMP_IN_STRINGS_H
51 #include <StringMgr.h>
54 #include "wx/wxcrtbase.h" // for wxChar, wxStrlen() etc.
55 #include "wx/strvararg.h"
56 #include "wx/buffer.h" // for wxCharBuffer
57 #include "wx/strconv.h" // for wxConvertXXX() macros and wxMBConv classes
58 #include "wx/stringimpl.h"
59 #include "wx/stringops.h"
60 #include "wx/unichar.h"
62 class WXDLLIMPEXP_FWD_BASE wxString
;
64 // unless this symbol is predefined to disable the compatibility functions, do
66 #ifndef WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
67 #define WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER 1
70 // ---------------------------------------------------------------------------
72 // ---------------------------------------------------------------------------
74 // casts [unfortunately!] needed to call some broken functions which require
75 // "char *" instead of "const char *"
76 #define WXSTRINGCAST (wxChar *)(const wxChar *)
77 #define wxCSTRINGCAST (wxChar *)(const wxChar *)
78 #define wxMBSTRINGCAST (char *)(const char *)
79 #define wxWCSTRINGCAST (wchar_t *)(const wchar_t *)
81 // like _T(), but for literals in wxString's internal representation, i.e.
82 // char* in UTF-8 build and wxChar* otherwise:
83 #if wxUSE_UNICODE_UTF8
84 #define wxSTRING_TEXT(str) str
86 #define wxSTRING_TEXT(str) _T(str)
89 // ----------------------------------------------------------------------------
91 // ----------------------------------------------------------------------------
93 #if WXWIN_COMPATIBILITY_2_6
95 // deprecated in favour of wxString::npos, don't use in new code
97 // maximum possible length for a string means "take all string" everywhere
98 #define wxSTRING_MAXLEN wxString::npos
100 #endif // WXWIN_COMPATIBILITY_2_6
102 // ---------------------------------------------------------------------------
103 // global functions complementing standard C string library replacements for
104 // strlen() and portable strcasecmp()
105 //---------------------------------------------------------------------------
107 #if WXWIN_COMPATIBILITY_2_8
108 // Use wxXXX() functions from wxcrt.h instead! These functions are for
109 // backwards compatibility only.
111 // checks whether the passed in pointer is NULL and if the string is empty
112 wxDEPRECATED( inline bool IsEmpty(const char *p
) );
113 inline bool IsEmpty(const char *p
) { return (!p
|| !*p
); }
115 // safe version of strlen() (returns 0 if passed NULL pointer)
116 wxDEPRECATED( inline size_t Strlen(const char *psz
) );
117 inline size_t Strlen(const char *psz
)
118 { return psz
? strlen(psz
) : 0; }
120 // portable strcasecmp/_stricmp
121 wxDEPRECATED( inline int Stricmp(const char *psz1
, const char *psz2
) );
122 inline int Stricmp(const char *psz1
, const char *psz2
)
124 #if defined(__VISUALC__) && defined(__WXWINCE__)
125 register char c1
, c2
;
127 c1
= tolower(*psz1
++);
128 c2
= tolower(*psz2
++);
129 } while ( c1
&& (c1
== c2
) );
132 #elif defined(__VISUALC__) || ( defined(__MWERKS__) && defined(__INTEL__) )
133 return _stricmp(psz1
, psz2
);
134 #elif defined(__SC__)
135 return _stricmp(psz1
, psz2
);
136 #elif defined(__BORLANDC__)
137 return stricmp(psz1
, psz2
);
138 #elif defined(__WATCOMC__)
139 return stricmp(psz1
, psz2
);
140 #elif defined(__DJGPP__)
141 return stricmp(psz1
, psz2
);
142 #elif defined(__EMX__)
143 return stricmp(psz1
, psz2
);
144 #elif defined(__WXPM__)
145 return stricmp(psz1
, psz2
);
146 #elif defined(__WXPALMOS__) || \
147 defined(HAVE_STRCASECMP_IN_STRING_H) || \
148 defined(HAVE_STRCASECMP_IN_STRINGS_H) || \
149 defined(__GNUWIN32__)
150 return strcasecmp(psz1
, psz2
);
151 #elif defined(__MWERKS__) && !defined(__INTEL__)
152 register char c1
, c2
;
154 c1
= tolower(*psz1
++);
155 c2
= tolower(*psz2
++);
156 } while ( c1
&& (c1
== c2
) );
160 // almost all compilers/libraries provide this function (unfortunately under
161 // different names), that's why we don't implement our own which will surely
162 // be more efficient than this code (uncomment to use):
164 register char c1, c2;
166 c1 = tolower(*psz1++);
167 c2 = tolower(*psz2++);
168 } while ( c1 && (c1 == c2) );
173 #error "Please define string case-insensitive compare for your OS/compiler"
174 #endif // OS/compiler
177 #endif // WXWIN_COMPATIBILITY_2_8
179 // ----------------------------------------------------------------------------
181 // ----------------------------------------------------------------------------
183 // Lightweight object returned by wxString::c_str() and implicitly convertible
184 // to either const char* or const wchar_t*.
185 class WXDLLIMPEXP_BASE wxCStrData
188 // Ctors; for internal use by wxString and wxCStrData only
189 wxCStrData(const wxString
*str
, size_t offset
= 0, bool owned
= false)
190 : m_str(str
), m_offset(offset
), m_owned(owned
) {}
193 // Ctor constructs the object from char literal; they are needed to make
194 // operator?: compile and they intentionally take char*, not const char*
195 inline wxCStrData(char *buf
);
196 inline wxCStrData(wchar_t *buf
);
197 inline wxCStrData(const wxCStrData
& data
);
199 inline ~wxCStrData();
201 // methods defined inline below must be declared inline or mingw32 3.4.5
202 // warns about "<symbol> defined locally after being referenced with
203 // dllimport linkage"
204 #if wxUSE_UNICODE_WCHAR
207 const wchar_t* AsWChar() const;
208 operator const wchar_t*() const { return AsWChar(); }
210 #if !wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY
213 const char* AsChar() const;
214 const unsigned char* AsUnsignedChar() const
215 { return (const unsigned char *) AsChar(); }
216 operator const char*() const { return AsChar(); }
217 operator const unsigned char*() const { return AsUnsignedChar(); }
219 operator const void*() const { return AsChar(); }
221 inline const wxCharBuffer
AsCharBuf() const;
222 inline const wxWCharBuffer
AsWCharBuf() const;
224 inline wxString
AsString() const;
226 // returns the value as C string in internal representation (equivalent
227 // to AsString().wx_str(), but more efficient)
228 const wxStringCharType
*AsInternal() const;
230 // allow expressions like "c_str()[0]":
231 inline wxUniChar
operator[](size_t n
) const;
232 wxUniChar
operator[](int n
) const { return operator[](size_t(n
)); }
233 wxUniChar
operator[](long n
) const { return operator[](size_t(n
)); }
234 #ifndef wxSIZE_T_IS_UINT
235 wxUniChar
operator[](unsigned int n
) const { return operator[](size_t(n
)); }
236 #endif // size_t != unsigned int
238 // these operators are needed to emulate the pointer semantics of c_str():
239 // expressions like "wxChar *p = str.c_str() + 1;" should continue to work
240 // (we need both versions to resolve ambiguities):
241 wxCStrData
operator+(int n
) const
242 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
243 wxCStrData
operator+(long n
) const
244 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
245 wxCStrData
operator+(size_t n
) const
246 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
248 // and these for "str.c_str() + n - 2":
249 wxCStrData
operator-(int n
) const
251 wxASSERT_MSG( n
<= (int)m_offset
,
252 _T("attempt to construct address before the beginning of the string") );
253 return wxCStrData(m_str
, m_offset
- n
, m_owned
);
255 wxCStrData
operator-(long n
) const
257 wxASSERT_MSG( n
<= (int)m_offset
,
258 _T("attempt to construct address before the beginning of the string") );
259 return wxCStrData(m_str
, m_offset
- n
, m_owned
);
261 wxCStrData
operator-(size_t n
) const
263 wxASSERT_MSG( n
<= m_offset
,
264 _T("attempt to construct address before the beginning of the string") );
265 return wxCStrData(m_str
, m_offset
- n
, m_owned
);
268 // this operator is needed to make expressions like "*c_str()" or
269 // "*(c_str() + 2)" work
270 inline wxUniChar
operator*() const;
273 const wxString
*m_str
;
277 friend class WXDLLIMPEXP_FWD_BASE wxString
;
280 // ----------------------------------------------------------------------------
281 // wxStringPrintfMixin
282 // ---------------------------------------------------------------------------
284 // NB: VC6 has a bug that causes linker errors if you have template methods
285 // in a class using __declspec(dllimport). The solution is to split such
286 // class into two classes, one that contains the template methods and does
287 // *not* use WXDLLIMPEXP_BASE and another class that contains the rest
288 // (with DLL linkage).
290 // We only do this for VC6 here, because the code is less efficient
291 // (Printf() has to use dynamic_cast<>) and because OpenWatcom compiler
292 // cannot compile this code.
294 #if defined(__VISUALC__) && __VISUALC__ < 1300
295 #define wxNEEDS_WXSTRING_PRINTF_MIXIN
298 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
299 // this class contains implementation of wxString's vararg methods, it's
300 // exported from wxBase DLL
301 class WXDLLIMPEXP_BASE wxStringPrintfMixinBase
304 wxStringPrintfMixinBase() {}
306 #if !wxUSE_UTF8_LOCALE_ONLY
307 int DoPrintfWchar(const wxChar
*format
, ...);
308 static wxString
DoFormatWchar(const wxChar
*format
, ...);
310 #if wxUSE_UNICODE_UTF8
311 int DoPrintfUtf8(const char *format
, ...);
312 static wxString
DoFormatUtf8(const char *format
, ...);
316 // this class contains template wrappers for wxString's vararg methods, it's
317 // intentionally *not* exported from the DLL in order to fix the VC6 bug
319 class wxStringPrintfMixin
: public wxStringPrintfMixinBase
322 // to further complicate things, we can't return wxString from
323 // wxStringPrintfMixin::Format() because wxString is not yet declared at
324 // this point; the solution is to use this fake type trait template - this
325 // way the compiler won't know the return type until Format() is used
326 // (this doesn't compile with Watcom, but VC6 compiles it just fine):
327 template<typename T
> struct StringReturnType
329 typedef wxString type
;
333 // these are duplicated wxString methods, they're also declared below
334 // if !wxNEEDS_WXSTRING_PRINTF_MIXIN:
336 // static wxString Format(const wString& format, ...) ATTRIBUTE_PRINTF_1;
337 WX_DEFINE_VARARG_FUNC_SANS_N0(static typename StringReturnType
<T1
>::type
,
338 Format
, 1, (const wxFormatString
&),
339 DoFormatWchar
, DoFormatUtf8
)
340 // We have to implement the version without template arguments manually
341 // because of the StringReturnType<> hack, although WX_DEFINE_VARARG_FUNC
342 // normally does it itself. It has to be a template so that we can use
343 // the hack, even though there's no real template parameter. We can't move
344 // it to wxStrig, because it would shadow these versions of Format() then.
346 inline static typename StringReturnType
<T
>::type
349 // NB: this doesn't compile if T is not (some form of) a string;
350 // this makes Format's prototype equivalent to
351 // Format(const wxFormatString& fmt)
352 return DoFormatWchar(wxFormatString(fmt
));
355 // int Printf(const wxString& format, ...);
356 WX_DEFINE_VARARG_FUNC(int, Printf
, 1, (const wxFormatString
&),
357 DoPrintfWchar
, DoPrintfUtf8
)
358 // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2;
359 WX_DEFINE_VARARG_FUNC(int, sprintf
, 1, (const wxFormatString
&),
360 DoPrintfWchar
, DoPrintfUtf8
)
363 wxStringPrintfMixin() : wxStringPrintfMixinBase() {}
365 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
368 // ----------------------------------------------------------------------------
369 // wxString: string class trying to be compatible with std::string, MFC
370 // CString and wxWindows 1.x wxString all at once
371 // ---------------------------------------------------------------------------
373 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
374 // "non dll-interface class 'wxStringPrintfMixin' used as base interface
375 // for dll-interface class 'wxString'" -- this is OK in our case
376 #pragma warning (disable:4275)
379 #if wxUSE_UNICODE_UTF8
380 // see the comment near wxString::iterator for why we need this
381 class WXDLLIMPEXP_BASE wxStringIteratorNode
384 wxStringIteratorNode()
385 : m_str(NULL
), m_citer(NULL
), m_iter(NULL
), m_prev(NULL
), m_next(NULL
) {}
386 wxStringIteratorNode(const wxString
*str
,
387 wxStringImpl::const_iterator
*citer
)
388 { DoSet(str
, citer
, NULL
); }
389 wxStringIteratorNode(const wxString
*str
, wxStringImpl::iterator
*iter
)
390 { DoSet(str
, NULL
, iter
); }
391 ~wxStringIteratorNode()
394 inline void set(const wxString
*str
, wxStringImpl::const_iterator
*citer
)
395 { clear(); DoSet(str
, citer
, NULL
); }
396 inline void set(const wxString
*str
, wxStringImpl::iterator
*iter
)
397 { clear(); DoSet(str
, NULL
, iter
); }
399 const wxString
*m_str
;
400 wxStringImpl::const_iterator
*m_citer
;
401 wxStringImpl::iterator
*m_iter
;
402 wxStringIteratorNode
*m_prev
, *m_next
;
406 inline void DoSet(const wxString
*str
,
407 wxStringImpl::const_iterator
*citer
,
408 wxStringImpl::iterator
*iter
);
410 // the node belongs to a particular iterator instance, it's not copied
411 // when a copy of the iterator is made
412 DECLARE_NO_COPY_CLASS(wxStringIteratorNode
)
414 #endif // wxUSE_UNICODE_UTF8
416 class WXDLLIMPEXP_BASE wxString
417 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
418 : public wxStringPrintfMixin
421 // NB: special care was taken in arranging the member functions in such order
422 // that all inline functions can be effectively inlined, verify that all
423 // performance critical functions are still inlined if you change order!
425 // an 'invalid' value for string index, moved to this place due to a CW bug
426 static const size_t npos
;
429 // if we hadn't made these operators private, it would be possible to
430 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
431 // converted to char in C and we do have operator=(char)
433 // NB: we don't need other versions (short/long and unsigned) as attempt
434 // to assign another numeric type to wxString will now result in
435 // ambiguity between operator=(char) and operator=(int)
436 wxString
& operator=(int);
438 // these methods are not implemented - there is _no_ conversion from int to
439 // string, you're doing something wrong if the compiler wants to call it!
441 // try `s << i' or `s.Printf("%d", i)' instead
445 // buffer for holding temporary substring when using any of the methods
446 // that take (char*,size_t) or (wchar_t*,size_t) arguments:
448 struct SubstrBufFromType
453 SubstrBufFromType(const T
& data_
, size_t len_
)
454 : data(data_
), len(len_
) {}
457 #if wxUSE_UNICODE_UTF8
458 // even char* -> char* needs conversion, from locale charset to UTF-8
459 typedef SubstrBufFromType
<wxCharBuffer
> SubstrBufFromWC
;
460 typedef SubstrBufFromType
<wxCharBuffer
> SubstrBufFromMB
;
461 #elif wxUSE_UNICODE_WCHAR
462 typedef SubstrBufFromType
<const wchar_t*> SubstrBufFromWC
;
463 typedef SubstrBufFromType
<wxWCharBuffer
> SubstrBufFromMB
;
465 typedef SubstrBufFromType
<const char*> SubstrBufFromMB
;
466 typedef SubstrBufFromType
<wxCharBuffer
> SubstrBufFromWC
;
470 // Functions implementing primitive operations on string data; wxString
471 // methods and iterators are implemented in terms of it. The differences
472 // between UTF-8 and wchar_t* representations of the string are mostly
475 #if wxUSE_UNICODE_UTF8
476 static SubstrBufFromMB
ConvertStr(const char *psz
, size_t nLength
,
477 const wxMBConv
& conv
);
478 static SubstrBufFromWC
ConvertStr(const wchar_t *pwz
, size_t nLength
,
479 const wxMBConv
& conv
);
480 #elif wxUSE_UNICODE_WCHAR
481 static SubstrBufFromMB
ConvertStr(const char *psz
, size_t nLength
,
482 const wxMBConv
& conv
);
484 static SubstrBufFromWC
ConvertStr(const wchar_t *pwz
, size_t nLength
,
485 const wxMBConv
& conv
);
488 #if !wxUSE_UNICODE_UTF8 // wxUSE_UNICODE_WCHAR or !wxUSE_UNICODE
489 // returns C string encoded as the implementation expects:
491 static const wchar_t* ImplStr(const wchar_t* str
)
492 { return str
? str
: wxT(""); }
493 static const SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
494 { return SubstrBufFromWC(str
, (str
&& n
== npos
) ? wxWcslen(str
) : n
); }
495 static wxWCharBuffer
ImplStr(const char* str
,
496 const wxMBConv
& conv
= wxConvLibc
)
497 { return ConvertStr(str
, npos
, conv
).data
; }
498 static SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
499 const wxMBConv
& conv
= wxConvLibc
)
500 { return ConvertStr(str
, n
, conv
); }
502 static const char* ImplStr(const char* str
,
503 const wxMBConv
& WXUNUSED(conv
) = wxConvLibc
)
504 { return str
? str
: ""; }
505 static const SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
506 const wxMBConv
& WXUNUSED(conv
) = wxConvLibc
)
507 { return SubstrBufFromMB(str
, (str
&& n
== npos
) ? wxStrlen(str
) : n
); }
508 static wxCharBuffer
ImplStr(const wchar_t* str
)
509 { return ConvertStr(str
, npos
, wxConvLibc
).data
; }
510 static SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
511 { return ConvertStr(str
, n
, wxConvLibc
); }
514 // translates position index in wxString to/from index in underlying
516 static size_t PosToImpl(size_t pos
) { return pos
; }
517 static void PosLenToImpl(size_t pos
, size_t len
,
518 size_t *implPos
, size_t *implLen
)
519 { *implPos
= pos
; *implLen
= len
; }
520 static size_t LenToImpl(size_t len
) { return len
; }
521 static size_t PosFromImpl(size_t pos
) { return pos
; }
523 #else // wxUSE_UNICODE_UTF8
525 static wxCharBuffer
ImplStr(const char* str
,
526 const wxMBConv
& conv
= wxConvLibc
)
527 { return ConvertStr(str
, npos
, conv
).data
; }
528 static SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
529 const wxMBConv
& conv
= wxConvLibc
)
530 { return ConvertStr(str
, n
, conv
); }
532 static wxCharBuffer
ImplStr(const wchar_t* str
)
533 { return ConvertStr(str
, npos
, wxMBConvUTF8()).data
; }
534 static SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
535 { return ConvertStr(str
, n
, wxMBConvUTF8()); }
537 size_t PosToImpl(size_t pos
) const
539 if ( pos
== 0 || pos
== npos
)
542 return (begin() + pos
).impl() - m_impl
.begin();
545 void PosLenToImpl(size_t pos
, size_t len
, size_t *implPos
, size_t *implLen
) const;
547 size_t LenToImpl(size_t len
) const
550 PosLenToImpl(0, len
, &pos
, &len2
);
554 size_t PosFromImpl(size_t pos
) const
556 if ( pos
== 0 || pos
== npos
)
559 return const_iterator(this, m_impl
.begin() + pos
) - begin();
561 #endif // !wxUSE_UNICODE_UTF8/wxUSE_UNICODE_UTF8
565 typedef wxUniChar value_type
;
566 typedef wxUniChar char_type
;
567 typedef wxUniCharRef reference
;
568 typedef wxChar
* pointer
;
569 typedef const wxChar
* const_pointer
;
571 typedef size_t size_type
;
572 typedef wxUniChar const_reference
;
575 #if wxUSE_UNICODE_UTF8
576 // random access is not O(1), as required by Random Access Iterator
577 #define WX_STR_ITERATOR_TAG std::bidirectional_iterator_tag
579 #define WX_STR_ITERATOR_TAG std::random_access_iterator_tag
582 #define WX_STR_ITERATOR_TAG void /* dummy type */
585 #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, reference_type) \
587 typedef wxStringImpl::iterator_name underlying_iterator; \
589 typedef WX_STR_ITERATOR_TAG iterator_category; \
590 typedef wxUniChar value_type; \
591 typedef int difference_type; \
592 typedef reference_type reference; \
593 typedef pointer_type pointer; \
595 reference operator[](size_t n) const { return *(*this + n); } \
597 iterator_name& operator++() \
598 { wxStringOperations::IncIter(m_cur); return *this; } \
599 iterator_name& operator--() \
600 { wxStringOperations::DecIter(m_cur); return *this; } \
601 iterator_name operator++(int) \
603 iterator_name tmp = *this; \
604 wxStringOperations::IncIter(m_cur); \
607 iterator_name operator--(int) \
609 iterator_name tmp = *this; \
610 wxStringOperations::DecIter(m_cur); \
614 iterator_name& operator+=(int n) \
616 m_cur = wxStringOperations::AddToIter(m_cur, n); \
619 iterator_name& operator+=(size_t n) \
621 m_cur = wxStringOperations::AddToIter(m_cur, (int)n); \
624 iterator_name& operator-=(int n) \
626 m_cur = wxStringOperations::AddToIter(m_cur, -n); \
629 iterator_name& operator-=(size_t n) \
631 m_cur = wxStringOperations::AddToIter(m_cur, -(int)n); \
635 difference_type operator-(const iterator_name& i) const \
636 { return wxStringOperations::DiffIters(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; } \
643 bool operator<(const iterator_name& i) const \
644 { return m_cur < i.m_cur; } \
645 bool operator>(const iterator_name& i) const \
646 { return m_cur > i.m_cur; } \
647 bool operator<=(const iterator_name& i) const \
648 { return m_cur <= i.m_cur; } \
649 bool operator>=(const iterator_name& i) const \
650 { return m_cur >= i.m_cur; } \
653 /* for internal wxString use only: */ \
654 underlying_iterator impl() const { return m_cur; } \
656 friend class wxString; \
657 friend class wxCStrData; \
660 underlying_iterator m_cur
662 class WXDLLIMPEXP_FWD_BASE const_iterator
;
664 #if wxUSE_UNICODE_UTF8
665 // NB: In UTF-8 build, (non-const) iterator needs to keep reference
666 // to the underlying wxStringImpl, because UTF-8 is variable-length
667 // encoding and changing the value pointer to by an iterator (using
668 // its operator*) requires calling wxStringImpl::replace() if the old
669 // and new values differ in their encoding's length.
671 // Furthermore, the replace() call may invalid all iterators for the
672 // string, so we have to keep track of outstanding iterators and update
673 // them if replace() happens.
675 // This is implemented by maintaining linked list of iterators for every
676 // string and traversing it in wxUniCharRef::operator=(). Head of the
677 // list is stored in wxString. (FIXME-UTF8)
679 class WXDLLIMPEXP_BASE iterator
681 WX_STR_ITERATOR_IMPL(iterator
, wxChar
*, wxUniCharRef
);
685 iterator(const iterator
& i
)
686 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
687 iterator
& operator=(const iterator
& i
)
688 { m_cur
= i
.m_cur
; m_node
.set(i
.str(), &m_cur
); return *this; }
690 reference
operator*()
691 { return wxUniCharRef::CreateForString(m_node
, m_cur
); }
693 iterator
operator+(int n
) const
694 { return iterator(str(), wxStringOperations::AddToIter(m_cur
, n
)); }
695 iterator
operator+(size_t n
) const
696 { return iterator(str(), wxStringOperations::AddToIter(m_cur
, (int)n
)); }
697 iterator
operator-(int n
) const
698 { return iterator(str(), wxStringOperations::AddToIter(m_cur
, -n
)); }
699 iterator
operator-(size_t n
) const
700 { return iterator(str(), wxStringOperations::AddToIter(m_cur
, -(int)n
)); }
703 iterator(wxString
*str
, underlying_iterator ptr
)
704 : m_cur(ptr
), m_node(str
, &m_cur
) {}
706 wxString
* str() const { return wx_const_cast(wxString
*, m_node
.m_str
); }
708 wxStringIteratorNode m_node
;
710 friend class const_iterator
;
713 class WXDLLIMPEXP_BASE const_iterator
715 // NB: reference_type is intentionally value, not reference, the character
716 // may be encoded differently in wxString data:
717 WX_STR_ITERATOR_IMPL(const_iterator
, const wxChar
*, wxUniChar
);
721 const_iterator(const const_iterator
& i
)
722 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
723 const_iterator(const iterator
& i
)
724 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
726 const_iterator
& operator=(const const_iterator
& i
)
727 { m_cur
= i
.m_cur
; m_node
.set(i
.str(), &m_cur
); return *this; }
728 const_iterator
& operator=(const iterator
& i
)
729 { m_cur
= i
.m_cur
; m_node
.set(i
.str(), &m_cur
); return *this; }
731 reference
operator*() const
732 { return wxStringOperations::DecodeChar(m_cur
); }
734 const_iterator
operator+(int n
) const
735 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur
, n
)); }
736 const_iterator
operator+(size_t n
) const
737 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur
, (int)n
)); }
738 const_iterator
operator-(int n
) const
739 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur
, -n
)); }
740 const_iterator
operator-(size_t n
) const
741 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur
, -(int)n
)); }
744 // for internal wxString use only:
745 const_iterator(const wxString
*str
, underlying_iterator ptr
)
746 : m_cur(ptr
), m_node(str
, &m_cur
) {}
748 const wxString
* str() const { return m_node
.m_str
; }
750 wxStringIteratorNode m_node
;
753 size_t IterToImplPos(wxString::iterator i
) const
754 { return wxStringImpl::const_iterator(i
.impl()) - m_impl
.begin(); }
756 #else // !wxUSE_UNICODE_UTF8
758 class WXDLLIMPEXP_BASE iterator
760 WX_STR_ITERATOR_IMPL(iterator
, wxChar
*, wxUniCharRef
);
764 iterator(const iterator
& i
) : m_cur(i
.m_cur
) {}
766 reference
operator*()
767 { return wxUniCharRef::CreateForString(m_cur
); }
769 iterator
operator+(int n
) const
770 { return iterator(wxStringOperations::AddToIter(m_cur
, n
)); }
771 iterator
operator+(size_t n
) const
772 { return iterator(wxStringOperations::AddToIter(m_cur
, (int)n
)); }
773 iterator
operator-(int n
) const
774 { return iterator(wxStringOperations::AddToIter(m_cur
, -n
)); }
775 iterator
operator-(size_t n
) const
776 { return iterator(wxStringOperations::AddToIter(m_cur
, -(int)n
)); }
779 // for internal wxString use only:
780 iterator(underlying_iterator ptr
) : m_cur(ptr
) {}
781 iterator(wxString
*WXUNUSED(str
), underlying_iterator ptr
) : m_cur(ptr
) {}
783 friend class const_iterator
;
786 class WXDLLIMPEXP_BASE const_iterator
788 // NB: reference_type is intentionally value, not reference, the character
789 // may be encoded differently in wxString data:
790 WX_STR_ITERATOR_IMPL(const_iterator
, const wxChar
*, wxUniChar
);
794 const_iterator(const const_iterator
& i
) : m_cur(i
.m_cur
) {}
795 const_iterator(const iterator
& i
) : m_cur(i
.m_cur
) {}
797 reference
operator*() const
798 { return wxStringOperations::DecodeChar(m_cur
); }
800 const_iterator
operator+(int n
) const
801 { return const_iterator(wxStringOperations::AddToIter(m_cur
, n
)); }
802 const_iterator
operator+(size_t n
) const
803 { return const_iterator(wxStringOperations::AddToIter(m_cur
, (int)n
)); }
804 const_iterator
operator-(int n
) const
805 { return const_iterator(wxStringOperations::AddToIter(m_cur
, -n
)); }
806 const_iterator
operator-(size_t n
) const
807 { return const_iterator(wxStringOperations::AddToIter(m_cur
, -(int)n
)); }
810 // for internal wxString use only:
811 const_iterator(underlying_iterator ptr
) : m_cur(ptr
) {}
812 const_iterator(const wxString
*WXUNUSED(str
), underlying_iterator ptr
)
815 #endif // wxUSE_UNICODE_UTF8/!wxUSE_UNICODE_UTF8
817 #undef WX_STR_ITERATOR_TAG
818 #undef WX_STR_ITERATOR_IMPL
820 friend class iterator
;
821 friend class const_iterator
;
823 template <typename T
>
824 class reverse_iterator_impl
827 typedef T iterator_type
;
829 typedef typename
T::iterator_category iterator_category
;
830 typedef typename
T::value_type value_type
;
831 typedef typename
T::difference_type difference_type
;
832 typedef typename
T::reference reference
;
833 typedef typename
T::pointer
*pointer
;
835 reverse_iterator_impl() {}
836 reverse_iterator_impl(iterator_type i
) : m_cur(i
) {}
837 reverse_iterator_impl(const reverse_iterator_impl
& ri
)
840 iterator_type
base() const { return m_cur
; }
842 reference
operator*() const { return *(m_cur
-1); }
843 reference
operator[](size_t n
) const { return *(*this + n
); }
845 reverse_iterator_impl
& operator++()
846 { --m_cur
; return *this; }
847 reverse_iterator_impl
operator++(int)
848 { reverse_iterator_impl tmp
= *this; --m_cur
; return tmp
; }
849 reverse_iterator_impl
& operator--()
850 { ++m_cur
; return *this; }
851 reverse_iterator_impl
operator--(int)
852 { reverse_iterator_impl tmp
= *this; ++m_cur
; return tmp
; }
854 // NB: explicit <T> in the functions below is to keep BCC 5.5 happy
855 reverse_iterator_impl
operator+(int n
) const
856 { return reverse_iterator_impl
<T
>(m_cur
- n
); }
857 reverse_iterator_impl
operator+(size_t n
) const
858 { return reverse_iterator_impl
<T
>(m_cur
- n
); }
859 reverse_iterator_impl
operator-(int n
) const
860 { return reverse_iterator_impl
<T
>(m_cur
+ n
); }
861 reverse_iterator_impl
operator-(size_t n
) const
862 { return reverse_iterator_impl
<T
>(m_cur
+ n
); }
863 reverse_iterator_impl
operator+=(int n
)
864 { m_cur
-= n
; return *this; }
865 reverse_iterator_impl
operator+=(size_t n
)
866 { m_cur
-= n
; return *this; }
867 reverse_iterator_impl
operator-=(int n
)
868 { m_cur
+= n
; return *this; }
869 reverse_iterator_impl
operator-=(size_t n
)
870 { m_cur
+= n
; return *this; }
872 unsigned operator-(const reverse_iterator_impl
& i
) const
873 { return i
.m_cur
- m_cur
; }
875 bool operator==(const reverse_iterator_impl
& ri
) const
876 { return m_cur
== ri
.m_cur
; }
877 bool operator!=(const reverse_iterator_impl
& ri
) const
878 { return !(*this == ri
); }
880 bool operator<(const reverse_iterator_impl
& i
) const
881 { return m_cur
> i
.m_cur
; }
882 bool operator>(const reverse_iterator_impl
& i
) const
883 { return m_cur
< i
.m_cur
; }
884 bool operator<=(const reverse_iterator_impl
& i
) const
885 { return m_cur
>= i
.m_cur
; }
886 bool operator>=(const reverse_iterator_impl
& i
) const
887 { return m_cur
<= i
.m_cur
; }
893 typedef reverse_iterator_impl
<iterator
> reverse_iterator
;
894 typedef reverse_iterator_impl
<const_iterator
> const_reverse_iterator
;
897 // used to transform an expression built using c_str() (and hence of type
898 // wxCStrData) to an iterator into the string
899 static const_iterator
CreateConstIterator(const wxCStrData
& data
)
901 return const_iterator(data
.m_str
,
902 (data
.m_str
->begin() + data
.m_offset
).impl());
905 // in UTF-8 STL build, creation from std::string requires conversion under
906 // non-UTF8 locales, so we can't have and use wxString(wxStringImpl) ctor;
907 // instead we define dummy type that lets us have wxString ctor for creation
908 // from wxStringImpl that couldn't be used by user code (in all other builds,
909 // "standard" ctors can be used):
910 #if wxUSE_UNICODE_UTF8 && wxUSE_STL_BASED_WXSTRING
911 struct CtorFromStringImplTag
{};
913 wxString(CtorFromStringImplTag
* WXUNUSED(dummy
), const wxStringImpl
& src
)
916 static wxString
FromImpl(const wxStringImpl
& src
)
917 { return wxString((CtorFromStringImplTag
*)NULL
, src
); }
919 #if !wxUSE_STL_BASED_WXSTRING
920 wxString(const wxStringImpl
& src
) : m_impl(src
) { }
921 // else: already defined as wxString(wxStdString) below
923 static wxString
FromImpl(const wxStringImpl
& src
) { return wxString(src
); }
927 // constructors and destructor
928 // ctor for an empty string
932 wxString(const wxString
& stringSrc
) : m_impl(stringSrc
.m_impl
) { }
934 // string containing nRepeat copies of ch
935 wxString(wxUniChar ch
, size_t nRepeat
= 1 )
936 { assign(nRepeat
, ch
); }
937 wxString(size_t nRepeat
, wxUniChar ch
)
938 { assign(nRepeat
, ch
); }
939 wxString(wxUniCharRef ch
, size_t nRepeat
= 1)
940 { assign(nRepeat
, ch
); }
941 wxString(size_t nRepeat
, wxUniCharRef ch
)
942 { assign(nRepeat
, ch
); }
943 wxString(char ch
, size_t nRepeat
= 1)
944 { assign(nRepeat
, ch
); }
945 wxString(size_t nRepeat
, char ch
)
946 { assign(nRepeat
, ch
); }
947 wxString(wchar_t ch
, size_t nRepeat
= 1)
948 { assign(nRepeat
, ch
); }
949 wxString(size_t nRepeat
, wchar_t ch
)
950 { assign(nRepeat
, ch
); }
952 // ctors from char* strings:
953 wxString(const char *psz
)
954 : m_impl(ImplStr(psz
)) {}
955 wxString(const char *psz
, const wxMBConv
& conv
)
956 : m_impl(ImplStr(psz
, conv
)) {}
957 wxString(const char *psz
, size_t nLength
)
958 { assign(psz
, nLength
); }
959 wxString(const char *psz
, const wxMBConv
& conv
, size_t nLength
)
961 SubstrBufFromMB
str(ImplStr(psz
, nLength
, conv
));
962 m_impl
.assign(str
.data
, str
.len
);
965 // and unsigned char*:
966 wxString(const unsigned char *psz
)
967 : m_impl(ImplStr((const char*)psz
)) {}
968 wxString(const unsigned char *psz
, const wxMBConv
& conv
)
969 : m_impl(ImplStr((const char*)psz
, conv
)) {}
970 wxString(const unsigned char *psz
, size_t nLength
)
971 { assign((const char*)psz
, nLength
); }
972 wxString(const unsigned char *psz
, const wxMBConv
& conv
, size_t nLength
)
974 SubstrBufFromMB
str(ImplStr((const char*)psz
, nLength
, conv
));
975 m_impl
.assign(str
.data
, str
.len
);
978 // ctors from wchar_t* strings:
979 wxString(const wchar_t *pwz
)
980 : m_impl(ImplStr(pwz
)) {}
981 wxString(const wchar_t *pwz
, const wxMBConv
& WXUNUSED(conv
))
982 : m_impl(ImplStr(pwz
)) {}
983 wxString(const wchar_t *pwz
, size_t nLength
)
984 { assign(pwz
, nLength
); }
985 wxString(const wchar_t *pwz
, const wxMBConv
& WXUNUSED(conv
), size_t nLength
)
986 { assign(pwz
, nLength
); }
988 wxString(const wxCharBuffer
& buf
)
989 { assign(buf
.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
990 wxString(const wxWCharBuffer
& buf
)
991 { assign(buf
.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
993 wxString(const wxCStrData
& cstr
)
994 : m_impl(cstr
.AsString().m_impl
) { }
996 // as we provide both ctors with this signature for both char and unsigned
997 // char string, we need to provide one for wxCStrData to resolve ambiguity
998 wxString(const wxCStrData
& cstr
, size_t nLength
)
999 : m_impl(cstr
.AsString().Mid(0, nLength
).m_impl
) {}
1001 // and because wxString is convertible to wxCStrData and const wxChar *
1002 // we also need to provide this one
1003 wxString(const wxString
& str
, size_t nLength
)
1004 { assign(str
, nLength
); }
1006 // even if we're not built with wxUSE_STL == 1 it is very convenient to allow
1007 // implicit conversions from std::string to wxString and vice verse as this
1008 // allows to use the same strings in non-GUI and GUI code, however we don't
1009 // want to unconditionally add this ctor as it would make wx lib dependent on
1010 // libstdc++ on some Linux versions which is bad, so instead we ask the
1011 // client code to define this wxUSE_STD_STRING symbol if they need it
1012 #if wxUSE_STD_STRING
1013 #if wxUSE_UNICODE_WCHAR
1014 wxString(const wxStdWideString
& str
) : m_impl(str
) {}
1015 #else // UTF-8 or ANSI
1016 wxString(const wxStdWideString
& str
)
1017 { assign(str
.c_str(), str
.length()); }
1020 #if !wxUSE_UNICODE // ANSI build
1021 // FIXME-UTF8: do this in UTF8 build #if wxUSE_UTF8_LOCALE_ONLY, too
1022 wxString(const std::string
& str
) : m_impl(str
) {}
1024 wxString(const std::string
& str
)
1025 { assign(str
.c_str(), str
.length()); }
1027 #endif // wxUSE_STD_STRING
1029 // Unlike ctor from std::string, we provide conversion to std::string only
1030 // if wxUSE_STL and not merely wxUSE_STD_STRING (which is on by default),
1031 // because it conflicts with operator const char/wchar_t*:
1033 #if wxUSE_UNICODE_WCHAR && wxUSE_STL_BASED_WXSTRING
1034 // wxStringImpl is std::string in the encoding we want
1035 operator const wxStdWideString
&() const { return m_impl
; }
1037 // wxStringImpl is either not std::string or needs conversion
1038 operator wxStdWideString() const
1039 // FIXME-UTF8: broken for embedded NULs
1040 { return wxStdWideString(wc_str()); }
1043 #if (!wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY) && wxUSE_STL_BASED_WXSTRING
1044 // wxStringImpl is std::string in the encoding we want
1045 operator const std::string
&() const { return m_impl
; }
1047 // wxStringImpl is either not std::string or needs conversion
1048 operator std::string() const
1049 // FIXME-UTF8: broken for embedded NULs
1050 { return std::string(mb_str()); }
1054 // first valid index position
1055 const_iterator
begin() const { return const_iterator(this, m_impl
.begin()); }
1056 iterator
begin() { return iterator(this, m_impl
.begin()); }
1057 // position one after the last valid one
1058 const_iterator
end() const { return const_iterator(this, m_impl
.end()); }
1059 iterator
end() { return iterator(this, m_impl
.end()); }
1061 // first element of the reversed string
1062 const_reverse_iterator
rbegin() const
1063 { return const_reverse_iterator(end()); }
1064 reverse_iterator
rbegin()
1065 { return reverse_iterator(end()); }
1066 // one beyond the end of the reversed string
1067 const_reverse_iterator
rend() const
1068 { return const_reverse_iterator(begin()); }
1069 reverse_iterator
rend()
1070 { return reverse_iterator(begin()); }
1072 // std::string methods:
1073 #if wxUSE_UNICODE_UTF8
1074 size_t length() const { return end() - begin(); } // FIXME-UTF8: optimize!
1076 size_t length() const { return m_impl
.length(); }
1079 size_type
size() const { return length(); }
1080 size_type
max_size() const { return npos
; }
1082 bool empty() const { return m_impl
.empty(); }
1084 size_type
capacity() const { return m_impl
.capacity(); } // FIXME-UTF8
1085 void reserve(size_t sz
) { m_impl
.reserve(sz
); } // FIXME-UTF8
1087 void resize(size_t nSize
, wxUniChar ch
= wxT('\0'))
1089 const size_t len
= length();
1093 #if wxUSE_UNICODE_UTF8
1096 // we can't use wxStringImpl::resize() for truncating the string as it
1097 // counts in bytes, not characters
1102 // we also can't use (presumably more efficient) resize() if we have to
1103 // append characters taking more than one byte
1104 if ( !ch
.IsAscii() )
1105 append(nSize
- len
, ch
);
1107 #endif // wxUSE_UNICODE_UTF8
1108 m_impl
.resize(nSize
, (wxStringCharType
)ch
);
1111 wxString
substr(size_t nStart
= 0, size_t nLen
= npos
) const
1114 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
1115 return FromImpl(m_impl
.substr(pos
, len
));
1118 // generic attributes & operations
1119 // as standard strlen()
1120 size_t Len() const { return length(); }
1121 // string contains any characters?
1122 bool IsEmpty() const { return empty(); }
1123 // empty string is "false", so !str will return true
1124 bool operator!() const { return empty(); }
1125 // truncate the string to given length
1126 wxString
& Truncate(size_t uiLen
);
1127 // empty string contents
1132 wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") );
1134 // empty the string and free memory
1137 wxString
tmp(wxEmptyString
);
1142 // Is an ascii value
1143 bool IsAscii() const;
1145 bool IsNumber() const;
1147 bool IsWord() const;
1149 // data access (all indexes are 0 based)
1151 wxUniChar
at(size_t n
) const
1152 { return *(begin() + n
); } // FIXME-UTF8: optimize?
1153 wxUniChar
GetChar(size_t n
) const
1155 // read/write access
1156 wxUniCharRef
at(size_t n
)
1157 { return *(begin() + n
); } // FIXME-UTF8: optimize?
1158 wxUniCharRef
GetWritableChar(size_t n
)
1161 void SetChar(size_t n
, wxUniChar ch
)
1164 // get last character
1165 wxUniChar
Last() const
1167 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1171 // get writable last character
1174 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1179 Note that we we must define all of the overloads below to avoid
1180 ambiguity when using str[0].
1182 wxUniChar
operator[](int n
) const
1184 wxUniChar
operator[](long n
) const
1186 wxUniChar
operator[](size_t n
) const
1188 #ifndef wxSIZE_T_IS_UINT
1189 wxUniChar
operator[](unsigned int n
) const
1191 #endif // size_t != unsigned int
1193 // operator versions of GetWriteableChar()
1194 wxUniCharRef
operator[](int n
)
1196 wxUniCharRef
operator[](long n
)
1198 wxUniCharRef
operator[](size_t n
)
1200 #ifndef wxSIZE_T_IS_UINT
1201 wxUniCharRef
operator[](unsigned int n
)
1203 #endif // size_t != unsigned int
1205 // explicit conversion to C string (use this with printf()!)
1206 wxCStrData
c_str() const { return wxCStrData(this); }
1207 wxCStrData
data() const { return c_str(); }
1209 // implicit conversion to C string
1210 operator wxCStrData() const { return c_str(); }
1212 // the first two operators conflict with operators for conversion to
1213 // std::string and they must be disabled in STL build; the next one only
1214 // makes sense if conversions to char* are also defined and not defining it
1215 // in STL build also helps us to get more clear error messages for the code
1216 // which relies on implicit conversion to char* in STL build
1218 operator const char*() const { return c_str(); }
1219 operator const wchar_t*() const { return c_str(); }
1221 // implicit conversion to untyped pointer for compatibility with previous
1222 // wxWidgets versions: this is the same as conversion to const char * so it
1224 operator const void*() const { return c_str(); }
1227 // identical to c_str(), for MFC compatibility
1228 const wxCStrData
GetData() const { return c_str(); }
1230 // explicit conversion to C string in internal representation (char*,
1231 // wchar_t*, UTF-8-encoded char*, depending on the build):
1232 const wxStringCharType
*wx_str() const { return m_impl
.c_str(); }
1234 // conversion to *non-const* multibyte or widestring buffer; modifying
1235 // returned buffer won't affect the string, these methods are only useful
1236 // for passing values to const-incorrect functions
1237 wxWritableCharBuffer
char_str(const wxMBConv
& conv
= wxConvLibc
) const
1238 { return mb_str(conv
); }
1239 wxWritableWCharBuffer
wchar_str() const { return wc_str(); }
1241 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
1242 // converting numbers or strings which are certain not to contain special
1243 // chars (typically system functions, X atoms, environment variables etc.)
1245 // the behaviour of these functions with the strings containing anything
1246 // else than 7 bit ASCII characters is undefined, use at your own risk.
1248 static wxString
FromAscii(const char *ascii
, size_t len
);
1249 static wxString
FromAscii(const char *ascii
);
1250 static wxString
FromAscii(char ascii
);
1251 const wxCharBuffer
ToAscii() const;
1253 static wxString
FromAscii(const char *ascii
) { return wxString( ascii
); }
1254 static wxString
FromAscii(const char *ascii
, size_t len
)
1255 { return wxString( ascii
, len
); }
1256 static wxString
FromAscii(char ascii
) { return wxString( ascii
); }
1257 const char *ToAscii() const { return c_str(); }
1258 #endif // Unicode/!Unicode
1260 // also provide unsigned char overloads as signed/unsigned doesn't matter
1261 // for 7 bit ASCII characters
1262 static wxString
FromAscii(const unsigned char *ascii
)
1263 { return FromAscii((const char *)ascii
); }
1264 static wxString
FromAscii(const unsigned char *ascii
, size_t len
)
1265 { return FromAscii((const char *)ascii
, len
); }
1267 // conversion to/from UTF-8:
1268 #if wxUSE_UNICODE_UTF8
1269 static wxString
FromUTF8(const char *utf8
)
1272 return wxEmptyString
;
1274 wxASSERT( wxStringOperations::IsValidUtf8String(utf8
) );
1275 return FromImpl(wxStringImpl(utf8
));
1277 static wxString
FromUTF8(const char *utf8
, size_t len
)
1280 return wxEmptyString
;
1282 return FromUTF8(utf8
);
1284 wxASSERT( wxStringOperations::IsValidUtf8String(utf8
, len
) );
1285 return FromImpl(wxStringImpl(utf8
, len
));
1287 const char* utf8_str() const { return wx_str(); }
1288 const char* ToUTF8() const { return wx_str(); }
1289 #elif wxUSE_UNICODE_WCHAR
1290 static wxString
FromUTF8(const char *utf8
)
1291 { return wxString(utf8
, wxMBConvUTF8()); }
1292 static wxString
FromUTF8(const char *utf8
, size_t len
)
1293 { return wxString(utf8
, wxMBConvUTF8(), len
); }
1294 const wxCharBuffer
utf8_str() const { return mb_str(wxMBConvUTF8()); }
1295 const wxCharBuffer
ToUTF8() const { return utf8_str(); }
1297 static wxString
FromUTF8(const char *utf8
)
1298 { return wxString(wxMBConvUTF8().cMB2WC(utf8
)); }
1299 static wxString
FromUTF8(const char *utf8
, size_t len
)
1302 wxWCharBuffer
buf(wxMBConvUTF8().cMB2WC(utf8
, len
== npos
? wxNO_LEN
: len
, &wlen
));
1303 return wxString(buf
.data(), wlen
);
1305 const wxCharBuffer
utf8_str() const
1306 { return wxMBConvUTF8().cWC2MB(wc_str()); }
1307 const wxCharBuffer
ToUTF8() const { return utf8_str(); }
1310 // functions for storing binary data in wxString:
1312 static wxString
From8BitData(const char *data
, size_t len
)
1313 { return wxString(data
, wxConvISO8859_1
, len
); }
1314 // version for NUL-terminated data:
1315 static wxString
From8BitData(const char *data
)
1316 { return wxString(data
, wxConvISO8859_1
); }
1317 const wxCharBuffer
To8BitData() const { return mb_str(wxConvISO8859_1
); }
1319 static wxString
From8BitData(const char *data
, size_t len
)
1320 { return wxString(data
, len
); }
1321 // version for NUL-terminated data:
1322 static wxString
From8BitData(const char *data
)
1323 { return wxString(data
); }
1324 const char *To8BitData() const { return c_str(); }
1325 #endif // Unicode/ANSI
1327 // conversions with (possible) format conversions: have to return a
1328 // buffer with temporary data
1330 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
1331 // return an ANSI (multibyte) string, wc_str() to return a wide string and
1332 // fn_str() to return a string which should be used with the OS APIs
1333 // accepting the file names. The return value is always the same, but the
1334 // type differs because a function may either return pointer to the buffer
1335 // directly or have to use intermediate buffer for translation.
1338 #if wxUSE_UTF8_LOCALE_ONLY
1339 const char* mb_str() const { return wx_str(); }
1340 const wxCharBuffer
mb_str(const wxMBConv
& conv
) const;
1342 const wxCharBuffer
mb_str(const wxMBConv
& conv
= wxConvLibc
) const;
1345 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
1347 #if wxUSE_UNICODE_WCHAR
1348 const wxChar
* wc_str() const { return wx_str(); }
1349 #elif wxUSE_UNICODE_UTF8
1350 const wxWCharBuffer
wc_str() const;
1352 // for compatibility with !wxUSE_UNICODE version
1353 const wxWX2WCbuf
wc_str(const wxMBConv
& WXUNUSED(conv
)) const
1354 { return wc_str(); }
1357 const wxCharBuffer
fn_str() const { return mb_str(wxConvFile
); }
1359 const wxWX2WCbuf
fn_str() const { return wc_str(); }
1360 #endif // wxMBFILES/!wxMBFILES
1363 const wxChar
* mb_str() const { return wx_str(); }
1365 // for compatibility with wxUSE_UNICODE version
1366 const wxChar
* mb_str(const wxMBConv
& WXUNUSED(conv
)) const { return wx_str(); }
1368 const wxWX2MBbuf
mbc_str() const { return mb_str(); }
1371 const wxWCharBuffer
wc_str(const wxMBConv
& conv
= wxConvLibc
) const;
1372 #endif // wxUSE_WCHAR_T
1373 const wxCharBuffer
fn_str() const { return wxConvFile
.cWC2WX( wc_str( wxConvLibc
) ); }
1374 #endif // Unicode/ANSI
1376 // overloaded assignment
1377 // from another wxString
1378 wxString
& operator=(const wxString
& stringSrc
)
1379 { m_impl
= stringSrc
.m_impl
; return *this; }
1380 wxString
& operator=(const wxCStrData
& cstr
)
1381 { return *this = cstr
.AsString(); }
1383 wxString
& operator=(wxUniChar ch
)
1384 { m_impl
= wxStringOperations::EncodeChar(ch
); return *this; }
1385 wxString
& operator=(wxUniCharRef ch
)
1386 { return operator=((wxUniChar
)ch
); }
1387 wxString
& operator=(char ch
)
1388 { return operator=(wxUniChar(ch
)); }
1389 wxString
& operator=(unsigned char ch
)
1390 { return operator=(wxUniChar(ch
)); }
1391 wxString
& operator=(wchar_t ch
)
1392 { return operator=(wxUniChar(ch
)); }
1393 // from a C string - STL probably will crash on NULL,
1394 // so we need to compensate in that case
1395 #if wxUSE_STL_BASED_WXSTRING
1396 wxString
& operator=(const char *psz
)
1397 { if (psz
) m_impl
= ImplStr(psz
); else Clear(); return *this; }
1398 wxString
& operator=(const wchar_t *pwz
)
1399 { if (pwz
) m_impl
= ImplStr(pwz
); else Clear(); return *this; }
1401 wxString
& operator=(const char *psz
)
1402 { m_impl
= ImplStr(psz
); return *this; }
1403 wxString
& operator=(const wchar_t *pwz
)
1404 { m_impl
= ImplStr(pwz
); return *this; }
1406 wxString
& operator=(const unsigned char *psz
)
1407 { return operator=((const char*)psz
); }
1409 // from wxWCharBuffer
1410 wxString
& operator=(const wxWCharBuffer
& s
)
1411 { return operator=(s
.data()); } // FIXME-UTF8: fix for embedded NULs
1412 // from wxCharBuffer
1413 wxString
& operator=(const wxCharBuffer
& s
)
1414 { return operator=(s
.data()); } // FIXME-UTF8: fix for embedded NULs
1416 // string concatenation
1417 // in place concatenation
1419 Concatenate and return the result. Note that the left to right
1420 associativity of << allows to write things like "str << str1 << str2
1421 << ..." (unlike with +=)
1424 wxString
& operator<<(const wxString
& s
)
1426 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1427 wxASSERT_MSG( s
.IsValid(),
1428 _T("did you forget to call UngetWriteBuf()?") );
1434 // string += C string
1435 wxString
& operator<<(const char *psz
)
1436 { append(psz
); return *this; }
1437 wxString
& operator<<(const wchar_t *pwz
)
1438 { append(pwz
); return *this; }
1439 wxString
& operator<<(const wxCStrData
& psz
)
1440 { append(psz
.AsString()); return *this; }
1442 wxString
& operator<<(wxUniChar ch
) { append(1, ch
); return *this; }
1443 wxString
& operator<<(wxUniCharRef ch
) { append(1, ch
); return *this; }
1444 wxString
& operator<<(char ch
) { append(1, ch
); return *this; }
1445 wxString
& operator<<(unsigned char ch
) { append(1, ch
); return *this; }
1446 wxString
& operator<<(wchar_t ch
) { append(1, ch
); return *this; }
1448 // string += buffer (i.e. from wxGetString)
1449 wxString
& operator<<(const wxWCharBuffer
& s
)
1450 { return operator<<((const wchar_t *)s
); }
1451 wxString
& operator<<(const wxCharBuffer
& s
)
1452 { return operator<<((const char *)s
); }
1454 // string += C string
1455 wxString
& Append(const wxString
& s
)
1457 // test for empty() to share the string if possible
1464 wxString
& Append(const char* psz
)
1465 { append(psz
); return *this; }
1466 wxString
& Append(const wchar_t* pwz
)
1467 { append(pwz
); return *this; }
1468 wxString
& Append(const wxCStrData
& psz
)
1469 { append(psz
); return *this; }
1470 wxString
& Append(const wxCharBuffer
& psz
)
1471 { append(psz
); return *this; }
1472 wxString
& Append(const wxWCharBuffer
& psz
)
1473 { append(psz
); return *this; }
1474 // append count copies of given character
1475 wxString
& Append(wxUniChar ch
, size_t count
= 1u)
1476 { append(count
, ch
); return *this; }
1477 wxString
& Append(wxUniCharRef ch
, size_t count
= 1u)
1478 { append(count
, ch
); return *this; }
1479 wxString
& Append(char ch
, size_t count
= 1u)
1480 { append(count
, ch
); return *this; }
1481 wxString
& Append(unsigned char ch
, size_t count
= 1u)
1482 { append(count
, ch
); return *this; }
1483 wxString
& Append(wchar_t ch
, size_t count
= 1u)
1484 { append(count
, ch
); return *this; }
1485 wxString
& Append(const char* psz
, size_t nLen
)
1486 { append(psz
, nLen
); return *this; }
1487 wxString
& Append(const wchar_t* pwz
, size_t nLen
)
1488 { append(pwz
, nLen
); return *this; }
1490 // prepend a string, return the string itself
1491 wxString
& Prepend(const wxString
& str
)
1492 { *this = str
+ *this; return *this; }
1494 // non-destructive concatenation
1496 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
,
1497 const wxString
& string2
);
1498 // string with a single char
1499 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
1500 // char with a string
1501 friend wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
1502 // string with C string
1503 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
1505 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
1506 const wchar_t *pwz
);
1507 // C string with string
1508 friend wxString WXDLLIMPEXP_BASE
operator+(const char *psz
,
1509 const wxString
& string
);
1510 friend wxString WXDLLIMPEXP_BASE
operator+(const wchar_t *pwz
,
1511 const wxString
& string
);
1513 // stream-like functions
1514 // insert an int into string
1515 wxString
& operator<<(int i
)
1516 { return (*this) << Format(_T("%d"), i
); }
1517 // insert an unsigned int into string
1518 wxString
& operator<<(unsigned int ui
)
1519 { return (*this) << Format(_T("%u"), ui
); }
1520 // insert a long into string
1521 wxString
& operator<<(long l
)
1522 { return (*this) << Format(_T("%ld"), l
); }
1523 // insert an unsigned long into string
1524 wxString
& operator<<(unsigned long ul
)
1525 { return (*this) << Format(_T("%lu"), ul
); }
1526 #if defined wxLongLong_t && !defined wxLongLongIsLong
1527 // insert a long long if they exist and aren't longs
1528 wxString
& operator<<(wxLongLong_t ll
)
1530 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("d");
1531 return (*this) << Format(fmt
, ll
);
1533 // insert an unsigned long long
1534 wxString
& operator<<(wxULongLong_t ull
)
1536 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("u");
1537 return (*this) << Format(fmt
, ull
);
1540 // insert a float into string
1541 wxString
& operator<<(float f
)
1542 { return (*this) << Format(_T("%f"), f
); }
1543 // insert a double into string
1544 wxString
& operator<<(double d
)
1545 { return (*this) << Format(_T("%g"), d
); }
1547 // string comparison
1548 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
1549 int Cmp(const char *psz
) const
1550 { return compare(psz
); }
1551 int Cmp(const wchar_t *pwz
) const
1552 { return compare(pwz
); }
1553 int Cmp(const wxString
& s
) const
1554 { return compare(s
); }
1555 int Cmp(const wxCStrData
& s
) const
1556 { return compare(s
); }
1557 int Cmp(const wxCharBuffer
& s
) const
1558 { return compare(s
); }
1559 int Cmp(const wxWCharBuffer
& s
) const
1560 { return compare(s
); }
1561 // same as Cmp() but not case-sensitive
1562 int CmpNoCase(const wxString
& s
) const;
1563 // test for the string equality, either considering case or not
1564 // (if compareWithCase then the case matters)
1565 bool IsSameAs(const wxString
& str
, bool compareWithCase
= true) const
1566 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
1567 bool IsSameAs(const char *str
, bool compareWithCase
= true) const
1568 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
1569 bool IsSameAs(const wchar_t *str
, bool compareWithCase
= true) const
1570 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
1571 bool IsSameAs(const wxCStrData
& str
, bool compareWithCase
= true) const
1572 { return IsSameAs(str
.AsString(), compareWithCase
); }
1573 bool IsSameAs(const wxCharBuffer
& str
, bool compareWithCase
= true) const
1574 { return IsSameAs(str
.data(), compareWithCase
); }
1575 bool IsSameAs(const wxWCharBuffer
& str
, bool compareWithCase
= true) const
1576 { return IsSameAs(str
.data(), compareWithCase
); }
1577 // comparison with a single character: returns true if equal
1578 bool IsSameAs(wxUniChar c
, bool compareWithCase
= true) const;
1579 // FIXME-UTF8: remove these overloads
1580 bool IsSameAs(wxUniCharRef c
, bool compareWithCase
= true) const
1581 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1582 bool IsSameAs(char c
, bool compareWithCase
= true) const
1583 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1584 bool IsSameAs(unsigned char c
, bool compareWithCase
= true) const
1585 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1586 bool IsSameAs(wchar_t c
, bool compareWithCase
= true) const
1587 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1588 bool IsSameAs(int c
, bool compareWithCase
= true) const
1589 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1591 // simple sub-string extraction
1592 // return substring starting at nFirst of length nCount (or till the end
1593 // if nCount = default value)
1594 wxString
Mid(size_t nFirst
, size_t nCount
= npos
) const;
1596 // operator version of Mid()
1597 wxString
operator()(size_t start
, size_t len
) const
1598 { return Mid(start
, len
); }
1600 // check if the string starts with the given prefix and return the rest
1601 // of the string in the provided pointer if it is not NULL; otherwise
1603 bool StartsWith(const wxString
& prefix
, wxString
*rest
= NULL
) const;
1604 // check if the string ends with the given suffix and return the
1605 // beginning of the string before the suffix in the provided pointer if
1606 // it is not NULL; otherwise return false
1607 bool EndsWith(const wxString
& suffix
, wxString
*rest
= NULL
) const;
1609 // get first nCount characters
1610 wxString
Left(size_t nCount
) const;
1611 // get last nCount characters
1612 wxString
Right(size_t nCount
) const;
1613 // get all characters before the first occurance of ch
1614 // (returns the whole string if ch not found)
1615 wxString
BeforeFirst(wxUniChar ch
) const;
1616 // get all characters before the last occurence of ch
1617 // (returns empty string if ch not found)
1618 wxString
BeforeLast(wxUniChar ch
) const;
1619 // get all characters after the first occurence of ch
1620 // (returns empty string if ch not found)
1621 wxString
AfterFirst(wxUniChar ch
) const;
1622 // get all characters after the last occurence of ch
1623 // (returns the whole string if ch not found)
1624 wxString
AfterLast(wxUniChar ch
) const;
1626 // for compatibility only, use more explicitly named functions above
1627 wxString
Before(wxUniChar ch
) const { return BeforeLast(ch
); }
1628 wxString
After(wxUniChar ch
) const { return AfterFirst(ch
); }
1631 // convert to upper case in place, return the string itself
1632 wxString
& MakeUpper();
1633 // convert to upper case, return the copy of the string
1634 // Here's something to remember: BC++ doesn't like returns in inlines.
1635 wxString
Upper() const ;
1636 // convert to lower case in place, return the string itself
1637 wxString
& MakeLower();
1638 // convert to lower case, return the copy of the string
1639 wxString
Lower() const ;
1641 // trimming/padding whitespace (either side) and truncating
1642 // remove spaces from left or from right (default) side
1643 wxString
& Trim(bool bFromRight
= true);
1644 // add nCount copies chPad in the beginning or at the end (default)
1645 wxString
& Pad(size_t nCount
, wxUniChar chPad
= wxT(' '), bool bFromRight
= true);
1647 // searching and replacing
1648 // searching (return starting index, or -1 if not found)
1649 int Find(wxUniChar ch
, bool bFromEnd
= false) const; // like strchr/strrchr
1650 int Find(wxUniCharRef ch
, bool bFromEnd
= false) const
1651 { return Find(wxUniChar(ch
), bFromEnd
); }
1652 int Find(char ch
, bool bFromEnd
= false) const
1653 { return Find(wxUniChar(ch
), bFromEnd
); }
1654 int Find(unsigned char ch
, bool bFromEnd
= false) const
1655 { return Find(wxUniChar(ch
), bFromEnd
); }
1656 int Find(wchar_t ch
, bool bFromEnd
= false) const
1657 { return Find(wxUniChar(ch
), bFromEnd
); }
1658 // searching (return starting index, or -1 if not found)
1659 int Find(const wxString
& sub
) const // like strstr
1661 size_type idx
= find(sub
);
1662 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
1664 int Find(const char *sub
) const // like strstr
1666 size_type idx
= find(sub
);
1667 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
1669 int Find(const wchar_t *sub
) const // like strstr
1671 size_type idx
= find(sub
);
1672 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
1675 int Find(const wxCStrData
& sub
) const
1676 { return Find(sub
.AsString()); }
1677 int Find(const wxCharBuffer
& sub
) const
1678 { return Find(sub
.data()); }
1679 int Find(const wxWCharBuffer
& sub
) const
1680 { return Find(sub
.data()); }
1682 // replace first (or all of bReplaceAll) occurences of substring with
1683 // another string, returns the number of replacements made
1684 size_t Replace(const wxString
& strOld
,
1685 const wxString
& strNew
,
1686 bool bReplaceAll
= true);
1688 // check if the string contents matches a mask containing '*' and '?'
1689 bool Matches(const wxString
& mask
) const;
1691 // conversion to numbers: all functions return true only if the whole
1692 // string is a number and put the value of this number into the pointer
1693 // provided, the base is the numeric base in which the conversion should be
1694 // done and must be comprised between 2 and 36 or be 0 in which case the
1695 // standard C rules apply (leading '0' => octal, "0x" => hex)
1696 // convert to a signed integer
1697 bool ToLong(long *val
, int base
= 10) const;
1698 // convert to an unsigned integer
1699 bool ToULong(unsigned long *val
, int base
= 10) const;
1700 // convert to wxLongLong
1701 #if defined(wxLongLong_t)
1702 bool ToLongLong(wxLongLong_t
*val
, int base
= 10) const;
1703 // convert to wxULongLong
1704 bool ToULongLong(wxULongLong_t
*val
, int base
= 10) const;
1705 #endif // wxLongLong_t
1706 // convert to a double
1707 bool ToDouble(double *val
) const;
1710 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1711 // formatted input/output
1712 // as sprintf(), returns the number of characters written or < 0 on error
1713 // (take 'this' into account in attribute parameter count)
1714 // int Printf(const wxString& format, ...);
1715 WX_DEFINE_VARARG_FUNC(int, Printf
, 1, (const wxFormatString
&),
1716 DoPrintfWchar
, DoPrintfUtf8
)
1718 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1719 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wxString
&),
1720 (wxFormatString(f1
)));
1721 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wxCStrData
&),
1722 (wxFormatString(f1
)));
1723 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const char*),
1724 (wxFormatString(f1
)));
1725 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wchar_t*),
1726 (wxFormatString(f1
)));
1728 #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN
1729 // as vprintf(), returns the number of characters written or < 0 on error
1730 int PrintfV(const wxString
& format
, va_list argptr
);
1732 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1733 // returns the string containing the result of Printf() to it
1734 // static wxString Format(const wxString& format, ...) ATTRIBUTE_PRINTF_1;
1735 WX_DEFINE_VARARG_FUNC(static wxString
, Format
, 1, (const wxFormatString
&),
1736 DoFormatWchar
, DoFormatUtf8
)
1738 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1739 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wxString
&),
1740 (wxFormatString(f1
)));
1741 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wxCStrData
&),
1742 (wxFormatString(f1
)));
1743 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const char*),
1744 (wxFormatString(f1
)));
1745 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wchar_t*),
1746 (wxFormatString(f1
)));
1749 // the same as above, but takes a va_list
1750 static wxString
FormatV(const wxString
& format
, va_list argptr
);
1752 // raw access to string memory
1753 // ensure that string has space for at least nLen characters
1754 // only works if the data of this string is not shared
1755 bool Alloc(size_t nLen
) { reserve(nLen
); /*return capacity() >= nLen;*/ return true; }
1756 // minimize the string's memory
1757 // only works if the data of this string is not shared
1759 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1760 // These are deprecated, use wxStringBuffer or wxStringBufferLength instead
1762 // get writable buffer of at least nLen bytes. Unget() *must* be called
1763 // a.s.a.p. to put string back in a reasonable state!
1764 wxDEPRECATED( wxStringCharType
*GetWriteBuf(size_t nLen
) );
1765 // call this immediately after GetWriteBuf() has been used
1766 wxDEPRECATED( void UngetWriteBuf() );
1767 wxDEPRECATED( void UngetWriteBuf(size_t nLen
) );
1768 #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && wxUSE_UNICODE_UTF8
1770 // wxWidgets version 1 compatibility functions
1773 wxString
SubString(size_t from
, size_t to
) const
1774 { return Mid(from
, (to
- from
+ 1)); }
1775 // values for second parameter of CompareTo function
1776 enum caseCompare
{exact
, ignoreCase
};
1777 // values for first parameter of Strip function
1778 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
1780 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1782 // (take 'this' into account in attribute parameter count)
1783 // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2;
1784 WX_DEFINE_VARARG_FUNC(int, sprintf
, 1, (const wxFormatString
&),
1785 DoPrintfWchar
, DoPrintfUtf8
)
1787 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1788 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wxString
&),
1789 (wxFormatString(f1
)));
1790 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wxCStrData
&),
1791 (wxFormatString(f1
)));
1792 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const char*),
1793 (wxFormatString(f1
)));
1794 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wchar_t*),
1795 (wxFormatString(f1
)));
1797 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
1800 inline int CompareTo(const wxChar
* psz
, caseCompare cmp
= exact
) const
1801 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
1804 size_t Length() const { return length(); }
1805 // Count the number of characters
1806 int Freq(wxUniChar ch
) const;
1808 void LowerCase() { MakeLower(); }
1810 void UpperCase() { MakeUpper(); }
1811 // use Trim except that it doesn't change this string
1812 wxString
Strip(stripType w
= trailing
) const;
1814 // use Find (more general variants not yet supported)
1815 size_t Index(const wxChar
* psz
) const { return Find(psz
); }
1816 size_t Index(wxUniChar ch
) const { return Find(ch
); }
1818 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
1819 wxString
& RemoveLast(size_t n
= 1) { return Truncate(length() - n
); }
1821 wxString
& Remove(size_t nStart
, size_t nLen
)
1822 { return (wxString
&)erase( nStart
, nLen
); }
1825 int First( wxUniChar ch
) const { return Find(ch
); }
1826 int First( wxUniCharRef ch
) const { return Find(ch
); }
1827 int First( char ch
) const { return Find(ch
); }
1828 int First( unsigned char ch
) const { return Find(ch
); }
1829 int First( wchar_t ch
) const { return Find(ch
); }
1830 int First( const wxString
& str
) const { return Find(str
); }
1831 int Last( wxUniChar ch
) const { return Find(ch
, true); }
1832 bool Contains(const wxString
& str
) const { return Find(str
) != wxNOT_FOUND
; }
1835 bool IsNull() const { return empty(); }
1837 // std::string compatibility functions
1839 // take nLen chars starting at nPos
1840 wxString(const wxString
& str
, size_t nPos
, size_t nLen
)
1841 { assign(str
, nPos
, nLen
); }
1842 // take all characters from first to last
1843 wxString(const_iterator first
, const_iterator last
)
1844 : m_impl(first
.impl(), last
.impl()) { }
1845 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1846 // the 2 overloads below are for compatibility with the existing code using
1847 // pointers instead of iterators
1848 wxString(const char *first
, const char *last
)
1850 SubstrBufFromMB
str(ImplStr(first
, last
- first
));
1851 m_impl
.assign(str
.data
, str
.len
);
1853 wxString(const wchar_t *first
, const wchar_t *last
)
1855 SubstrBufFromWC
str(ImplStr(first
, last
- first
));
1856 m_impl
.assign(str
.data
, str
.len
);
1858 // and this one is needed to compile code adding offsets to c_str() result
1859 wxString(const wxCStrData
& first
, const wxCStrData
& last
)
1860 : m_impl(CreateConstIterator(first
).impl(),
1861 CreateConstIterator(last
).impl())
1863 wxASSERT_MSG( first
.m_str
== last
.m_str
,
1864 _T("pointers must be into the same string") );
1866 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1868 // lib.string.modifiers
1869 // append elements str[pos], ..., str[pos+n]
1870 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
1873 str
.PosLenToImpl(pos
, n
, &from
, &len
);
1874 m_impl
.append(str
.m_impl
, from
, len
);
1878 wxString
& append(const wxString
& str
)
1879 { m_impl
.append(str
.m_impl
); return *this; }
1880 // append first n (or all if n == npos) characters of sz
1881 wxString
& append(const char *sz
)
1882 { m_impl
.append(ImplStr(sz
)); return *this; }
1883 wxString
& append(const wchar_t *sz
)
1884 { m_impl
.append(ImplStr(sz
)); return *this; }
1885 wxString
& append(const char *sz
, size_t n
)
1887 SubstrBufFromMB
str(ImplStr(sz
, n
));
1888 m_impl
.append(str
.data
, str
.len
);
1891 wxString
& append(const wchar_t *sz
, size_t n
)
1893 SubstrBufFromWC
str(ImplStr(sz
, n
));
1894 m_impl
.append(str
.data
, str
.len
);
1898 wxString
& append(const wxCStrData
& str
)
1899 { return append(str
.AsString()); }
1900 wxString
& append(const wxCharBuffer
& str
)
1901 { return append(str
.data()); }
1902 wxString
& append(const wxWCharBuffer
& str
)
1903 { return append(str
.data()); }
1905 // append n copies of ch
1906 wxString
& append(size_t n
, wxUniChar ch
)
1908 #if wxUSE_UNICODE_UTF8
1909 if ( !ch
.IsAscii() )
1910 m_impl
.append(wxStringOperations::EncodeNChars(n
, ch
));
1913 m_impl
.append(n
, (wxStringCharType
)ch
);
1916 // append from first to last
1917 wxString
& append(const_iterator first
, const_iterator last
)
1918 { m_impl
.append(first
.impl(), last
.impl()); return *this; }
1919 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1920 wxString
& append(const char *first
, const char *last
)
1921 { return append(first
, last
- first
); }
1922 wxString
& append(const wchar_t *first
, const wchar_t *last
)
1923 { return append(first
, last
- first
); }
1924 wxString
& append(const wxCStrData
& first
, const wxCStrData
& last
)
1925 { return append(CreateConstIterator(first
), CreateConstIterator(last
)); }
1926 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1928 // same as `this_string = str'
1929 wxString
& assign(const wxString
& str
)
1930 { m_impl
= str
.m_impl
; return *this; }
1931 wxString
& assign(const wxString
& str
, size_t len
)
1933 m_impl
.assign(str
.m_impl
, 0, str
.LenToImpl(len
));
1936 // same as ` = str[pos..pos + n]
1937 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
1940 str
.PosLenToImpl(pos
, n
, &from
, &len
);
1941 m_impl
.assign(str
.m_impl
, from
, len
);
1944 // same as `= first n (or all if n == npos) characters of sz'
1945 wxString
& assign(const char *sz
)
1946 { m_impl
.assign(ImplStr(sz
)); return *this; }
1947 wxString
& assign(const wchar_t *sz
)
1948 { m_impl
.assign(ImplStr(sz
)); return *this; }
1949 wxString
& assign(const char *sz
, size_t n
)
1951 SubstrBufFromMB
str(ImplStr(sz
, n
));
1952 m_impl
.assign(str
.data
, str
.len
);
1955 wxString
& assign(const wchar_t *sz
, size_t n
)
1957 SubstrBufFromWC
str(ImplStr(sz
, n
));
1958 m_impl
.assign(str
.data
, str
.len
);
1962 wxString
& assign(const wxCStrData
& str
)
1963 { return assign(str
.AsString()); }
1964 wxString
& assign(const wxCharBuffer
& str
)
1965 { return assign(str
.data()); }
1966 wxString
& assign(const wxWCharBuffer
& str
)
1967 { return assign(str
.data()); }
1968 wxString
& assign(const wxCStrData
& str
, size_t len
)
1969 { return assign(str
.AsString(), len
); }
1970 wxString
& assign(const wxCharBuffer
& str
, size_t len
)
1971 { return assign(str
.data(), len
); }
1972 wxString
& assign(const wxWCharBuffer
& str
, size_t len
)
1973 { return assign(str
.data(), len
); }
1975 // same as `= n copies of ch'
1976 wxString
& assign(size_t n
, wxUniChar ch
)
1978 #if wxUSE_UNICODE_UTF8
1979 if ( !ch
.IsAscii() )
1980 m_impl
.assign(wxStringOperations::EncodeNChars(n
, ch
));
1983 m_impl
.assign(n
, (wxStringCharType
)ch
);
1987 wxString
& assign(size_t n
, wxUniCharRef ch
)
1988 { return assign(n
, wxUniChar(ch
)); }
1989 wxString
& assign(size_t n
, char ch
)
1990 { return assign(n
, wxUniChar(ch
)); }
1991 wxString
& assign(size_t n
, unsigned char ch
)
1992 { return assign(n
, wxUniChar(ch
)); }
1993 wxString
& assign(size_t n
, wchar_t ch
)
1994 { return assign(n
, wxUniChar(ch
)); }
1996 // assign from first to last
1997 wxString
& assign(const_iterator first
, const_iterator last
)
1998 { m_impl
.assign(first
.impl(), last
.impl()); return *this; }
1999 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2000 wxString
& assign(const char *first
, const char *last
)
2001 { return assign(first
, last
- first
); }
2002 wxString
& assign(const wchar_t *first
, const wchar_t *last
)
2003 { return assign(first
, last
- first
); }
2004 wxString
& assign(const wxCStrData
& first
, const wxCStrData
& last
)
2005 { return assign(CreateConstIterator(first
), CreateConstIterator(last
)); }
2006 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2008 // string comparison
2009 int compare(const wxString
& str
) const;
2010 int compare(const char* sz
) const;
2011 int compare(const wchar_t* sz
) const;
2012 int compare(const wxCStrData
& str
) const
2013 { return compare(str
.AsString()); }
2014 int compare(const wxCharBuffer
& str
) const
2015 { return compare(str
.data()); }
2016 int compare(const wxWCharBuffer
& str
) const
2017 { return compare(str
.data()); }
2018 // comparison with a substring
2019 int compare(size_t nStart
, size_t nLen
, const wxString
& str
) const;
2020 // comparison of 2 substrings
2021 int compare(size_t nStart
, size_t nLen
,
2022 const wxString
& str
, size_t nStart2
, size_t nLen2
) const;
2023 // substring comparison with first nCount characters of sz
2024 int compare(size_t nStart
, size_t nLen
,
2025 const char* sz
, size_t nCount
= npos
) const;
2026 int compare(size_t nStart
, size_t nLen
,
2027 const wchar_t* sz
, size_t nCount
= npos
) const;
2029 // insert another string
2030 wxString
& insert(size_t nPos
, const wxString
& str
)
2031 { insert(begin() + nPos
, str
.begin(), str
.end()); return *this; }
2032 // insert n chars of str starting at nStart (in str)
2033 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
2036 str
.PosLenToImpl(nStart
, n
, &from
, &len
);
2037 m_impl
.insert(PosToImpl(nPos
), str
.m_impl
, from
, len
);
2040 // insert first n (or all if n == npos) characters of sz
2041 wxString
& insert(size_t nPos
, const char *sz
)
2042 { m_impl
.insert(PosToImpl(nPos
), ImplStr(sz
)); return *this; }
2043 wxString
& insert(size_t nPos
, const wchar_t *sz
)
2044 { m_impl
.insert(PosToImpl(nPos
), ImplStr(sz
)); return *this; }
2045 wxString
& insert(size_t nPos
, const char *sz
, size_t n
)
2047 SubstrBufFromMB
str(ImplStr(sz
, n
));
2048 m_impl
.insert(PosToImpl(nPos
), str
.data
, str
.len
);
2051 wxString
& insert(size_t nPos
, const wchar_t *sz
, size_t n
)
2053 SubstrBufFromWC
str(ImplStr(sz
, n
));
2054 m_impl
.insert(PosToImpl(nPos
), str
.data
, str
.len
);
2057 // insert n copies of ch
2058 wxString
& insert(size_t nPos
, size_t n
, wxUniChar ch
)
2060 #if wxUSE_UNICODE_UTF8
2061 if ( !ch
.IsAscii() )
2062 m_impl
.insert(PosToImpl(nPos
), wxStringOperations::EncodeNChars(n
, ch
));
2065 m_impl
.insert(PosToImpl(nPos
), n
, (wxStringCharType
)ch
);
2068 iterator
insert(iterator it
, wxUniChar ch
)
2070 #if wxUSE_UNICODE_UTF8
2071 if ( !ch
.IsAscii() )
2073 size_t pos
= IterToImplPos(it
);
2074 m_impl
.insert(pos
, wxStringOperations::EncodeChar(ch
));
2075 return iterator(this, m_impl
.begin() + pos
);
2079 return iterator(this, m_impl
.insert(it
.impl(), (wxStringCharType
)ch
));
2081 void insert(iterator it
, const_iterator first
, const_iterator last
)
2082 { m_impl
.insert(it
.impl(), first
.impl(), last
.impl()); }
2083 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2084 void insert(iterator it
, const char *first
, const char *last
)
2085 { insert(it
- begin(), first
, last
- first
); }
2086 void insert(iterator it
, const wchar_t *first
, const wchar_t *last
)
2087 { insert(it
- begin(), first
, last
- first
); }
2088 void insert(iterator it
, const wxCStrData
& first
, const wxCStrData
& last
)
2089 { insert(it
, CreateConstIterator(first
), CreateConstIterator(last
)); }
2090 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2092 void insert(iterator it
, size_type n
, wxUniChar ch
)
2094 #if wxUSE_UNICODE_UTF8
2095 if ( !ch
.IsAscii() )
2096 m_impl
.insert(IterToImplPos(it
), wxStringOperations::EncodeNChars(n
, ch
));
2099 m_impl
.insert(it
.impl(), n
, (wxStringCharType
)ch
);
2102 // delete characters from nStart to nStart + nLen
2103 wxString
& erase(size_type pos
= 0, size_type n
= npos
)
2106 PosLenToImpl(pos
, n
, &from
, &len
);
2107 m_impl
.erase(from
, len
);
2110 // delete characters from first up to last
2111 iterator
erase(iterator first
, iterator last
)
2112 { return iterator(this, m_impl
.erase(first
.impl(), last
.impl())); }
2113 iterator
erase(iterator first
)
2114 { return iterator(this, m_impl
.erase(first
.impl())); }
2116 #ifdef wxSTRING_BASE_HASNT_CLEAR
2117 void clear() { erase(); }
2119 void clear() { m_impl
.clear(); }
2122 // replaces the substring of length nLen starting at nStart
2123 wxString
& replace(size_t nStart
, size_t nLen
, const char* sz
)
2126 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2127 m_impl
.replace(from
, len
, ImplStr(sz
));
2130 wxString
& replace(size_t nStart
, size_t nLen
, const wchar_t* sz
)
2133 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2134 m_impl
.replace(from
, len
, ImplStr(sz
));
2137 // replaces the substring of length nLen starting at nStart
2138 wxString
& replace(size_t nStart
, size_t nLen
, const wxString
& str
)
2141 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2142 m_impl
.replace(from
, len
, str
.m_impl
);
2145 // replaces the substring with nCount copies of ch
2146 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxUniChar ch
)
2149 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2150 #if wxUSE_UNICODE_UTF8
2151 if ( !ch
.IsAscii() )
2152 m_impl
.replace(from
, len
, wxStringOperations::EncodeNChars(nCount
, ch
));
2155 m_impl
.replace(from
, len
, nCount
, (wxStringCharType
)ch
);
2158 // replaces a substring with another substring
2159 wxString
& replace(size_t nStart
, size_t nLen
,
2160 const wxString
& str
, size_t nStart2
, size_t nLen2
)
2163 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2166 str
.PosLenToImpl(nStart2
, nLen2
, &from2
, &len2
);
2168 m_impl
.replace(from
, len
, str
.m_impl
, from2
, len2
);
2171 // replaces the substring with first nCount chars of sz
2172 wxString
& replace(size_t nStart
, size_t nLen
,
2173 const char* sz
, size_t nCount
)
2176 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2178 SubstrBufFromMB
str(ImplStr(sz
, nCount
));
2180 m_impl
.replace(from
, len
, str
.data
, str
.len
);
2183 wxString
& replace(size_t nStart
, size_t nLen
,
2184 const wchar_t* sz
, size_t nCount
)
2187 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2189 SubstrBufFromWC
str(ImplStr(sz
, nCount
));
2191 m_impl
.replace(from
, len
, str
.data
, str
.len
);
2194 wxString
& replace(size_t nStart
, size_t nLen
,
2195 const wxString
& s
, size_t nCount
)
2198 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2199 m_impl
.replace(from
, len
, s
.m_impl
.c_str(), s
.LenToImpl(nCount
));
2203 wxString
& replace(iterator first
, iterator last
, const char* s
)
2204 { m_impl
.replace(first
.impl(), last
.impl(), ImplStr(s
)); return *this; }
2205 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
)
2206 { m_impl
.replace(first
.impl(), last
.impl(), ImplStr(s
)); return *this; }
2207 wxString
& replace(iterator first
, iterator last
, const char* s
, size_type n
)
2209 SubstrBufFromMB
str(ImplStr(s
, n
));
2210 m_impl
.replace(first
.impl(), last
.impl(), str
.data
, str
.len
);
2213 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
, size_type n
)
2215 SubstrBufFromWC
str(ImplStr(s
, n
));
2216 m_impl
.replace(first
.impl(), last
.impl(), str
.data
, str
.len
);
2219 wxString
& replace(iterator first
, iterator last
, const wxString
& s
)
2220 { m_impl
.replace(first
.impl(), last
.impl(), s
.m_impl
); return *this; }
2221 wxString
& replace(iterator first
, iterator last
, size_type n
, wxUniChar ch
)
2223 #if wxUSE_UNICODE_UTF8
2224 if ( !ch
.IsAscii() )
2225 m_impl
.replace(first
.impl(), last
.impl(),
2226 wxStringOperations::EncodeNChars(n
, ch
));
2229 m_impl
.replace(first
.impl(), last
.impl(), n
, (wxStringCharType
)ch
);
2232 wxString
& replace(iterator first
, iterator last
,
2233 const_iterator first1
, const_iterator last1
)
2235 m_impl
.replace(first
.impl(), last
.impl(), first1
.impl(), last1
.impl());
2238 wxString
& replace(iterator first
, iterator last
,
2239 const char *first1
, const char *last1
)
2240 { replace(first
, last
, first1
, last1
- first1
); return *this; }
2241 wxString
& replace(iterator first
, iterator last
,
2242 const wchar_t *first1
, const wchar_t *last1
)
2243 { replace(first
, last
, first1
, last1
- first1
); return *this; }
2246 void swap(wxString
& str
)
2247 { m_impl
.swap(str
.m_impl
); }
2250 size_t find(const wxString
& str
, size_t nStart
= 0) const
2251 { return PosFromImpl(m_impl
.find(str
.m_impl
, PosToImpl(nStart
))); }
2253 // find first n characters of sz
2254 size_t find(const char* sz
, size_t nStart
= 0, size_t n
= npos
) const
2256 SubstrBufFromMB
str(ImplStr(sz
, n
));
2257 return PosFromImpl(m_impl
.find(str
.data
, PosToImpl(nStart
), str
.len
));
2259 size_t find(const wchar_t* sz
, size_t nStart
= 0, size_t n
= npos
) const
2261 SubstrBufFromWC
str(ImplStr(sz
, n
));
2262 return PosFromImpl(m_impl
.find(str
.data
, PosToImpl(nStart
), str
.len
));
2264 size_t find(const wxCharBuffer
& s
, size_t nStart
= 0, size_t n
= npos
) const
2265 { return find(s
.data(), nStart
, n
); }
2266 size_t find(const wxWCharBuffer
& s
, size_t nStart
= 0, size_t n
= npos
) const
2267 { return find(s
.data(), nStart
, n
); }
2268 size_t find(const wxCStrData
& s
, size_t nStart
= 0, size_t n
= npos
) const
2269 { return find(s
.AsWChar(), nStart
, n
); }
2271 // find the first occurence of character ch after nStart
2272 size_t find(wxUniChar ch
, size_t nStart
= 0) const
2274 return PosFromImpl(m_impl
.find(wxStringOperations::EncodeChar(ch
),
2275 PosToImpl(nStart
)));
2277 size_t find(wxUniCharRef ch
, size_t nStart
= 0) const
2278 { return find(wxUniChar(ch
), nStart
); }
2279 size_t find(char ch
, size_t nStart
= 0) const
2280 { return find(wxUniChar(ch
), nStart
); }
2281 size_t find(unsigned char ch
, size_t nStart
= 0) const
2282 { return find(wxUniChar(ch
), nStart
); }
2283 size_t find(wchar_t ch
, size_t nStart
= 0) const
2284 { return find(wxUniChar(ch
), nStart
); }
2286 // rfind() family is exactly like find() but works right to left
2288 // as find, but from the end
2289 size_t rfind(const wxString
& str
, size_t nStart
= npos
) const
2290 { return PosFromImpl(m_impl
.rfind(str
.m_impl
, PosToImpl(nStart
))); }
2292 // as find, but from the end
2293 size_t rfind(const char* sz
, size_t nStart
= npos
, size_t n
= npos
) const
2295 SubstrBufFromMB
str(ImplStr(sz
, n
));
2296 return PosFromImpl(m_impl
.rfind(str
.data
, PosToImpl(nStart
), str
.len
));
2298 size_t rfind(const wchar_t* sz
, size_t nStart
= npos
, size_t n
= npos
) const
2300 SubstrBufFromWC
str(ImplStr(sz
, n
));
2301 return PosFromImpl(m_impl
.rfind(str
.data
, PosToImpl(nStart
), str
.len
));
2303 size_t rfind(const wxCharBuffer
& s
, size_t nStart
= npos
, size_t n
= npos
) const
2304 { return rfind(s
.data(), nStart
, n
); }
2305 size_t rfind(const wxWCharBuffer
& s
, size_t nStart
= npos
, size_t n
= npos
) const
2306 { return rfind(s
.data(), nStart
, n
); }
2307 size_t rfind(const wxCStrData
& s
, size_t nStart
= npos
, size_t n
= npos
) const
2308 { return rfind(s
.AsWChar(), nStart
, n
); }
2309 // as find, but from the end
2310 size_t rfind(wxUniChar ch
, size_t nStart
= npos
) const
2312 return PosFromImpl(m_impl
.rfind(wxStringOperations::EncodeChar(ch
),
2313 PosToImpl(nStart
)));
2315 size_t rfind(wxUniCharRef ch
, size_t nStart
= npos
) const
2316 { return rfind(wxUniChar(ch
), nStart
); }
2317 size_t rfind(char ch
, size_t nStart
= npos
) const
2318 { return rfind(wxUniChar(ch
), nStart
); }
2319 size_t rfind(unsigned char ch
, size_t nStart
= npos
) const
2320 { return rfind(wxUniChar(ch
), nStart
); }
2321 size_t rfind(wchar_t ch
, size_t nStart
= npos
) const
2322 { return rfind(wxUniChar(ch
), nStart
); }
2324 // find first/last occurence of any character (not) in the set:
2325 #if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2326 // FIXME-UTF8: this is not entirely correct, because it doesn't work if
2327 // sizeof(wchar_t)==2 and surrogates are present in the string;
2328 // should we care? Probably not.
2329 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
2330 { return m_impl
.find_first_of(str
.m_impl
, nStart
); }
2331 size_t find_first_of(const char* sz
, size_t nStart
= 0) const
2332 { return m_impl
.find_first_of(ImplStr(sz
), nStart
); }
2333 size_t find_first_of(const wchar_t* sz
, size_t nStart
= 0) const
2334 { return m_impl
.find_first_of(ImplStr(sz
), nStart
); }
2335 size_t find_first_of(const char* sz
, size_t nStart
, size_t n
) const
2336 { return m_impl
.find_first_of(ImplStr(sz
), nStart
, n
); }
2337 size_t find_first_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
2338 { return m_impl
.find_first_of(ImplStr(sz
), nStart
, n
); }
2339 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const
2340 { return m_impl
.find_first_of((wxChar
)c
, nStart
); }
2342 size_t find_last_of(const wxString
& str
, size_t nStart
= npos
) const
2343 { return m_impl
.find_last_of(str
.m_impl
, nStart
); }
2344 size_t find_last_of(const char* sz
, size_t nStart
= npos
) const
2345 { return m_impl
.find_last_of(ImplStr(sz
), nStart
); }
2346 size_t find_last_of(const wchar_t* sz
, size_t nStart
= npos
) const
2347 { return m_impl
.find_last_of(ImplStr(sz
), nStart
); }
2348 size_t find_last_of(const char* sz
, size_t nStart
, size_t n
) const
2349 { return m_impl
.find_last_of(ImplStr(sz
), nStart
, n
); }
2350 size_t find_last_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
2351 { return m_impl
.find_last_of(ImplStr(sz
), nStart
, n
); }
2352 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const
2353 { return m_impl
.find_last_of((wxChar
)c
, nStart
); }
2355 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
2356 { return m_impl
.find_first_not_of(str
.m_impl
, nStart
); }
2357 size_t find_first_not_of(const char* sz
, size_t nStart
= 0) const
2358 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
); }
2359 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
= 0) const
2360 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
); }
2361 size_t find_first_not_of(const char* sz
, size_t nStart
, size_t n
) const
2362 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
, n
); }
2363 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
2364 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
, n
); }
2365 size_t find_first_not_of(wxUniChar c
, size_t nStart
= 0) const
2366 { return m_impl
.find_first_not_of((wxChar
)c
, nStart
); }
2368 size_t find_last_not_of(const wxString
& str
, size_t nStart
= npos
) const
2369 { return m_impl
.find_last_not_of(str
.m_impl
, nStart
); }
2370 size_t find_last_not_of(const char* sz
, size_t nStart
= npos
) const
2371 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
); }
2372 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
= npos
) const
2373 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
); }
2374 size_t find_last_not_of(const char* sz
, size_t nStart
, size_t n
) const
2375 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
, n
); }
2376 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
2377 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
, n
); }
2378 size_t find_last_not_of(wxUniChar c
, size_t nStart
= npos
) const
2379 { return m_impl
.find_last_not_of((wxChar
)c
, nStart
); }
2381 // we can't use std::string implementation in UTF-8 build, because the
2382 // character sets would be interpreted wrongly:
2384 // as strpbrk() but starts at nStart, returns npos if not found
2385 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
2386 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2387 { return find_first_of(str
.wc_str(), nStart
); }
2389 { return find_first_of(str
.mb_str(), nStart
); }
2392 size_t find_first_of(const char* sz
, size_t nStart
= 0) const;
2393 size_t find_first_of(const wchar_t* sz
, size_t nStart
= 0) const;
2394 size_t find_first_of(const char* sz
, size_t nStart
, size_t n
) const;
2395 size_t find_first_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2396 // same as find(char, size_t)
2397 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const
2398 { return find(c
, nStart
); }
2399 // find the last (starting from nStart) char from str in this string
2400 size_t find_last_of (const wxString
& str
, size_t nStart
= npos
) const
2401 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2402 { return find_last_of(str
.wc_str(), nStart
); }
2404 { return find_last_of(str
.mb_str(), nStart
); }
2407 size_t find_last_of (const char* sz
, size_t nStart
= npos
) const;
2408 size_t find_last_of (const wchar_t* sz
, size_t nStart
= npos
) const;
2409 size_t find_last_of(const char* sz
, size_t nStart
, size_t n
) const;
2410 size_t find_last_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2412 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const
2413 { return rfind(c
, nStart
); }
2415 // find first/last occurence of any character not in the set
2417 // as strspn() (starting from nStart), returns npos on failure
2418 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
2419 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2420 { return find_first_not_of(str
.wc_str(), nStart
); }
2422 { return find_first_not_of(str
.mb_str(), nStart
); }
2425 size_t find_first_not_of(const char* sz
, size_t nStart
= 0) const;
2426 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
= 0) const;
2427 size_t find_first_not_of(const char* sz
, size_t nStart
, size_t n
) const;
2428 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2430 size_t find_first_not_of(wxUniChar ch
, size_t nStart
= 0) const;
2432 size_t find_last_not_of(const wxString
& str
, size_t nStart
= npos
) const
2433 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2434 { return find_last_not_of(str
.wc_str(), nStart
); }
2436 { return find_last_not_of(str
.mb_str(), nStart
); }
2439 size_t find_last_not_of(const char* sz
, size_t nStart
= npos
) const;
2440 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
= npos
) const;
2441 size_t find_last_not_of(const char* sz
, size_t nStart
, size_t n
) const;
2442 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2444 size_t find_last_not_of(wxUniChar ch
, size_t nStart
= npos
) const;
2445 #endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not
2447 // provide char/wchar_t/wxUniCharRef overloads for char-finding functions
2448 // above to resolve ambiguities:
2449 size_t find_first_of(wxUniCharRef ch
, size_t nStart
= 0) const
2450 { return find_first_of(wxUniChar(ch
), nStart
); }
2451 size_t find_first_of(char ch
, size_t nStart
= 0) const
2452 { return find_first_of(wxUniChar(ch
), nStart
); }
2453 size_t find_first_of(unsigned char ch
, size_t nStart
= 0) const
2454 { return find_first_of(wxUniChar(ch
), nStart
); }
2455 size_t find_first_of(wchar_t ch
, size_t nStart
= 0) const
2456 { return find_first_of(wxUniChar(ch
), nStart
); }
2457 size_t find_last_of(wxUniCharRef ch
, size_t nStart
= npos
) const
2458 { return find_last_of(wxUniChar(ch
), nStart
); }
2459 size_t find_last_of(char ch
, size_t nStart
= npos
) const
2460 { return find_last_of(wxUniChar(ch
), nStart
); }
2461 size_t find_last_of(unsigned char ch
, size_t nStart
= npos
) const
2462 { return find_last_of(wxUniChar(ch
), nStart
); }
2463 size_t find_last_of(wchar_t ch
, size_t nStart
= npos
) const
2464 { return find_last_of(wxUniChar(ch
), nStart
); }
2465 size_t find_first_not_of(wxUniCharRef ch
, size_t nStart
= 0) const
2466 { return find_first_not_of(wxUniChar(ch
), nStart
); }
2467 size_t find_first_not_of(char ch
, size_t nStart
= 0) const
2468 { return find_first_not_of(wxUniChar(ch
), nStart
); }
2469 size_t find_first_not_of(unsigned char ch
, size_t nStart
= 0) const
2470 { return find_first_not_of(wxUniChar(ch
), nStart
); }
2471 size_t find_first_not_of(wchar_t ch
, size_t nStart
= 0) const
2472 { return find_first_not_of(wxUniChar(ch
), nStart
); }
2473 size_t find_last_not_of(wxUniCharRef ch
, size_t nStart
= npos
) const
2474 { return find_last_not_of(wxUniChar(ch
), nStart
); }
2475 size_t find_last_not_of(char ch
, size_t nStart
= npos
) const
2476 { return find_last_not_of(wxUniChar(ch
), nStart
); }
2477 size_t find_last_not_of(unsigned char ch
, size_t nStart
= npos
) const
2478 { return find_last_not_of(wxUniChar(ch
), nStart
); }
2479 size_t find_last_not_of(wchar_t ch
, size_t nStart
= npos
) const
2480 { return find_last_not_of(wxUniChar(ch
), nStart
); }
2482 // and additional overloads for the versions taking strings:
2483 size_t find_first_of(const wxCStrData
& sz
, size_t nStart
= 0) const
2484 { return find_first_of(sz
.AsString(), nStart
); }
2485 size_t find_first_of(const wxCharBuffer
& sz
, size_t nStart
= 0) const
2486 { return find_first_of(sz
.data(), nStart
); }
2487 size_t find_first_of(const wxWCharBuffer
& sz
, size_t nStart
= 0) const
2488 { return find_first_of(sz
.data(), nStart
); }
2489 size_t find_first_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
2490 { return find_first_of(sz
.AsWChar(), nStart
, n
); }
2491 size_t find_first_of(const wxCharBuffer
& sz
, size_t nStart
, size_t n
) const
2492 { return find_first_of(sz
.data(), nStart
, n
); }
2493 size_t find_first_of(const wxWCharBuffer
& sz
, size_t nStart
, size_t n
) const
2494 { return find_first_of(sz
.data(), nStart
, n
); }
2496 size_t find_last_of(const wxCStrData
& sz
, size_t nStart
= 0) const
2497 { return find_last_of(sz
.AsString(), nStart
); }
2498 size_t find_last_of(const wxCharBuffer
& sz
, size_t nStart
= 0) const
2499 { return find_last_of(sz
.data(), nStart
); }
2500 size_t find_last_of(const wxWCharBuffer
& sz
, size_t nStart
= 0) const
2501 { return find_last_of(sz
.data(), nStart
); }
2502 size_t find_last_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
2503 { return find_last_of(sz
.AsWChar(), nStart
, n
); }
2504 size_t find_last_of(const wxCharBuffer
& sz
, size_t nStart
, size_t n
) const
2505 { return find_last_of(sz
.data(), nStart
, n
); }
2506 size_t find_last_of(const wxWCharBuffer
& sz
, size_t nStart
, size_t n
) const
2507 { return find_last_of(sz
.data(), nStart
, n
); }
2509 size_t find_first_not_of(const wxCStrData
& sz
, size_t nStart
= 0) const
2510 { return find_first_not_of(sz
.AsString(), nStart
); }
2511 size_t find_first_not_of(const wxCharBuffer
& sz
, size_t nStart
= 0) const
2512 { return find_first_not_of(sz
.data(), nStart
); }
2513 size_t find_first_not_of(const wxWCharBuffer
& sz
, size_t nStart
= 0) const
2514 { return find_first_not_of(sz
.data(), nStart
); }
2515 size_t find_first_not_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
2516 { return find_first_not_of(sz
.AsWChar(), nStart
, n
); }
2517 size_t find_first_not_of(const wxCharBuffer
& sz
, size_t nStart
, size_t n
) const
2518 { return find_first_not_of(sz
.data(), nStart
, n
); }
2519 size_t find_first_not_of(const wxWCharBuffer
& sz
, size_t nStart
, size_t n
) const
2520 { return find_first_not_of(sz
.data(), nStart
, n
); }
2522 size_t find_last_not_of(const wxCStrData
& sz
, size_t nStart
= 0) const
2523 { return find_last_not_of(sz
.AsString(), nStart
); }
2524 size_t find_last_not_of(const wxCharBuffer
& sz
, size_t nStart
= 0) const
2525 { return find_last_not_of(sz
.data(), nStart
); }
2526 size_t find_last_not_of(const wxWCharBuffer
& sz
, size_t nStart
= 0) const
2527 { return find_last_not_of(sz
.data(), nStart
); }
2528 size_t find_last_not_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
2529 { return find_last_not_of(sz
.AsWChar(), nStart
, n
); }
2530 size_t find_last_not_of(const wxCharBuffer
& sz
, size_t nStart
, size_t n
) const
2531 { return find_last_not_of(sz
.data(), nStart
, n
); }
2532 size_t find_last_not_of(const wxWCharBuffer
& sz
, size_t nStart
, size_t n
) const
2533 { return find_last_not_of(sz
.data(), nStart
, n
); }
2536 wxString
& operator+=(const wxString
& s
)
2537 { m_impl
+= s
.m_impl
; return *this; }
2538 // string += C string
2539 wxString
& operator+=(const char *psz
)
2540 { m_impl
+= ImplStr(psz
); return *this; }
2541 wxString
& operator+=(const wchar_t *pwz
)
2542 { m_impl
+= ImplStr(pwz
); return *this; }
2543 wxString
& operator+=(const wxCStrData
& s
)
2544 { m_impl
+= s
.AsString().m_impl
; return *this; }
2545 wxString
& operator+=(const wxCharBuffer
& s
)
2546 { return operator+=(s
.data()); }
2547 wxString
& operator+=(const wxWCharBuffer
& s
)
2548 { return operator+=(s
.data()); }
2550 wxString
& operator+=(wxUniChar ch
)
2551 { m_impl
+= wxStringOperations::EncodeChar(ch
); return *this; }
2552 wxString
& operator+=(wxUniCharRef ch
) { return *this += wxUniChar(ch
); }
2553 wxString
& operator+=(int ch
) { return *this += wxUniChar(ch
); }
2554 wxString
& operator+=(char ch
) { return *this += wxUniChar(ch
); }
2555 wxString
& operator+=(unsigned char ch
) { return *this += wxUniChar(ch
); }
2556 wxString
& operator+=(wchar_t ch
) { return *this += wxUniChar(ch
); }
2559 #if !wxUSE_STL_BASED_WXSTRING
2560 // helpers for wxStringBuffer and wxStringBufferLength
2561 wxStringCharType
*DoGetWriteBuf(size_t nLen
)
2562 { return m_impl
.DoGetWriteBuf(nLen
); }
2563 void DoUngetWriteBuf()
2564 { m_impl
.DoUngetWriteBuf(); }
2565 void DoUngetWriteBuf(size_t nLen
)
2566 { m_impl
.DoUngetWriteBuf(nLen
); }
2567 #endif // !wxUSE_STL_BASED_WXSTRING
2569 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2570 #if !wxUSE_UTF8_LOCALE_ONLY
2571 int DoPrintfWchar(const wxChar
*format
, ...);
2572 static wxString
DoFormatWchar(const wxChar
*format
, ...);
2574 #if wxUSE_UNICODE_UTF8
2575 int DoPrintfUtf8(const char *format
, ...);
2576 static wxString
DoFormatUtf8(const char *format
, ...);
2580 #if !wxUSE_STL_BASED_WXSTRING
2581 // check string's data validity
2582 bool IsValid() const { return m_impl
.GetStringData()->IsValid(); }
2586 wxStringImpl m_impl
;
2588 // buffers for compatibility conversion from (char*)c_str() and
2589 // (wchar_t*)c_str():
2590 // FIXME-UTF8: bechmark various approaches to keeping compatibility buffers
2591 template<typename T
>
2592 struct ConvertedBuffer
2594 ConvertedBuffer() : m_buf(NULL
) {}
2598 operator T
*() const { return m_buf
; }
2600 ConvertedBuffer
& operator=(T
*str
)
2609 #if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
2610 ConvertedBuffer
<char> m_convertedToChar
;
2612 #if !wxUSE_UNICODE_WCHAR
2613 ConvertedBuffer
<wchar_t> m_convertedToWChar
;
2616 #if wxUSE_UNICODE_UTF8
2617 // FIXME-UTF8: (try to) move this elsewhere (TLS) or solve differently
2618 // assigning to character pointer to by wxString::interator may
2619 // change the underlying wxStringImpl iterator, so we have to
2620 // keep track of all iterators and update them as necessary:
2621 struct wxStringIteratorNodeHead
2623 wxStringIteratorNodeHead() : ptr(NULL
) {}
2624 wxStringIteratorNode
*ptr
;
2626 // copying is disallowed as it would result in more than one pointer into
2627 // the same linked list
2628 DECLARE_NO_COPY_CLASS(wxStringIteratorNodeHead
)
2631 wxStringIteratorNodeHead m_iterators
;
2633 friend class WXDLLIMPEXP_FWD_BASE wxStringIteratorNode
;
2634 friend class WXDLLIMPEXP_FWD_BASE wxUniCharRef
;
2635 #endif // wxUSE_UNICODE_UTF8
2637 friend class WXDLLIMPEXP_FWD_BASE wxCStrData
;
2638 friend class wxStringInternalBuffer
;
2639 friend class wxStringInternalBufferLength
;
2642 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
2643 #pragma warning (default:4275)
2646 // string iterator operators that satisfy STL Random Access Iterator
2648 inline wxString::iterator
operator+(int n
, wxString::iterator i
)
2650 inline wxString::iterator
operator+(size_t n
, wxString::iterator i
)
2652 inline wxString::const_iterator
operator+(int n
, wxString::const_iterator i
)
2654 inline wxString::const_iterator
operator+(size_t n
, wxString::const_iterator i
)
2656 inline wxString::reverse_iterator
operator+(int n
, wxString::reverse_iterator i
)
2658 inline wxString::reverse_iterator
operator+(size_t n
, wxString::reverse_iterator i
)
2660 inline wxString::const_reverse_iterator
operator+(int n
, wxString::const_reverse_iterator i
)
2662 inline wxString::const_reverse_iterator
operator+(size_t n
, wxString::const_reverse_iterator i
)
2665 // notice that even though for many compilers the friend declarations above are
2666 // enough, from the point of view of C++ standard we must have the declarations
2667 // here as friend ones are not injected in the enclosing namespace and without
2668 // them the code fails to compile with conforming compilers such as xlC or g++4
2669 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
, const wxString
& string2
);
2670 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const char *psz
);
2671 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, const wchar_t *pwz
);
2672 wxString WXDLLIMPEXP_BASE
operator+(const char *psz
, const wxString
& string
);
2673 wxString WXDLLIMPEXP_BASE
operator+(const wchar_t *pwz
, const wxString
& string
);
2675 wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
2676 wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
2678 inline wxString
operator+(const wxString
& string
, wxUniCharRef ch
)
2679 { return string
+ (wxUniChar
)ch
; }
2680 inline wxString
operator+(const wxString
& string
, char ch
)
2681 { return string
+ wxUniChar(ch
); }
2682 inline wxString
operator+(const wxString
& string
, wchar_t ch
)
2683 { return string
+ wxUniChar(ch
); }
2684 inline wxString
operator+(wxUniCharRef ch
, const wxString
& string
)
2685 { return (wxUniChar
)ch
+ string
; }
2686 inline wxString
operator+(char ch
, const wxString
& string
)
2687 { return wxUniChar(ch
) + string
; }
2688 inline wxString
operator+(wchar_t ch
, const wxString
& string
)
2689 { return wxUniChar(ch
) + string
; }
2692 #define wxGetEmptyString() wxString()
2694 // ----------------------------------------------------------------------------
2695 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
2696 // ----------------------------------------------------------------------------
2698 #if !wxUSE_STL_BASED_WXSTRING
2699 // string buffer for direct access to string data in their native
2701 class wxStringInternalBuffer
2704 typedef wxStringCharType CharType
;
2706 wxStringInternalBuffer(wxString
& str
, size_t lenWanted
= 1024)
2707 : m_str(str
), m_buf(NULL
)
2708 { m_buf
= m_str
.DoGetWriteBuf(lenWanted
); }
2710 ~wxStringInternalBuffer() { m_str
.DoUngetWriteBuf(); }
2712 operator wxStringCharType
*() const { return m_buf
; }
2716 wxStringCharType
*m_buf
;
2718 DECLARE_NO_COPY_CLASS(wxStringInternalBuffer
)
2721 class wxStringInternalBufferLength
2724 typedef wxStringCharType CharType
;
2726 wxStringInternalBufferLength(wxString
& str
, size_t lenWanted
= 1024)
2727 : m_str(str
), m_buf(NULL
), m_len(0), m_lenSet(false)
2729 m_buf
= m_str
.DoGetWriteBuf(lenWanted
);
2730 wxASSERT(m_buf
!= NULL
);
2733 ~wxStringInternalBufferLength()
2736 m_str
.DoUngetWriteBuf(m_len
);
2739 operator wxStringCharType
*() const { return m_buf
; }
2740 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
2744 wxStringCharType
*m_buf
;
2748 DECLARE_NO_COPY_CLASS(wxStringInternalBufferLength
)
2751 #endif // !wxUSE_STL_BASED_WXSTRING
2753 template<typename T
>
2754 class WXDLLIMPEXP_BASE wxStringTypeBufferBase
2759 wxStringTypeBufferBase(wxString
& str
, size_t lenWanted
= 1024)
2760 : m_str(str
), m_buf(lenWanted
)
2764 operator CharType
*() { return m_buf
.data(); }
2768 wxCharTypeBuffer
<CharType
> m_buf
;
2771 template<typename T
>
2772 class WXDLLIMPEXP_BASE wxStringTypeBufferLengthBase
2777 wxStringTypeBufferLengthBase(wxString
& str
, size_t lenWanted
= 1024)
2778 : m_str(str
), m_buf(lenWanted
), m_len(0), m_lenSet(false)
2781 operator CharType
*() { return m_buf
.data(); }
2782 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
2786 wxCharTypeBuffer
<CharType
> m_buf
;
2791 template<typename T
>
2792 class wxStringTypeBuffer
: public wxStringTypeBufferBase
<T
>
2795 wxStringTypeBuffer(wxString
& str
, size_t lenWanted
= 1024)
2796 : wxStringTypeBufferBase
<T
>(str
, lenWanted
) {}
2797 ~wxStringTypeBuffer()
2799 this->m_str
.assign(this->m_buf
.data());
2802 DECLARE_NO_COPY_CLASS(wxStringTypeBuffer
)
2805 template<typename T
>
2806 class wxStringTypeBufferLength
: public wxStringTypeBufferLengthBase
<T
>
2809 wxStringTypeBufferLength(wxString
& str
, size_t lenWanted
= 1024)
2810 : wxStringTypeBufferLengthBase
<T
>(str
, lenWanted
) {}
2812 ~wxStringTypeBufferLength()
2814 wxASSERT(this->m_lenSet
);
2815 this->m_str
.assign(this->m_buf
.data(), this->m_len
);
2818 DECLARE_NO_COPY_CLASS(wxStringTypeBufferLength
)
2821 #if wxUSE_STL_BASED_WXSTRING
2823 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase
<wxStringCharType
> )
2825 class wxStringInternalBuffer
: public wxStringTypeBufferBase
<wxStringCharType
>
2828 wxStringInternalBuffer(wxString
& str
, size_t lenWanted
= 1024)
2829 : wxStringTypeBufferBase
<wxStringCharType
>(str
, lenWanted
) {}
2830 ~wxStringInternalBuffer()
2831 { m_str
.m_impl
.assign(m_buf
.data()); }
2833 DECLARE_NO_COPY_CLASS(wxStringInternalBuffer
)
2836 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE(
2837 wxStringTypeBufferLengthBase
<wxStringCharType
> )
2839 class wxStringInternalBufferLength
2840 : public wxStringTypeBufferLengthBase
<wxStringCharType
>
2843 wxStringInternalBufferLength(wxString
& str
, size_t lenWanted
= 1024)
2844 : wxStringTypeBufferLengthBase
<wxStringCharType
>(str
, lenWanted
) {}
2846 ~wxStringInternalBufferLength()
2849 m_str
.m_impl
.assign(m_buf
.data(), m_len
);
2852 DECLARE_NO_COPY_CLASS(wxStringInternalBufferLength
)
2854 #endif // wxUSE_STL_BASED_WXSTRING
2857 #if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
2858 typedef wxStringTypeBuffer
<wxChar
> wxStringBuffer
;
2859 typedef wxStringTypeBufferLength
<wxChar
> wxStringBufferLength
;
2860 #else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2861 typedef wxStringInternalBuffer wxStringBuffer
;
2862 typedef wxStringInternalBufferLength wxStringBufferLength
;
2863 #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2865 #if wxUSE_UNICODE_UTF8
2866 typedef wxStringInternalBuffer wxUTF8StringBuffer
;
2867 typedef wxStringInternalBufferLength wxUTF8StringBufferLength
;
2868 #elif wxUSE_UNICODE_WCHAR
2870 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferBase
<char> )
2872 class WXDLLIMPEXP_BASE wxUTF8StringBuffer
: public wxStringTypeBufferBase
<char>
2875 wxUTF8StringBuffer(wxString
& str
, size_t lenWanted
= 1024)
2876 : wxStringTypeBufferBase
<char>(str
, lenWanted
) {}
2877 ~wxUTF8StringBuffer();
2879 DECLARE_NO_COPY_CLASS(wxUTF8StringBuffer
)
2882 WXDLLIMPEXP_TEMPLATE_INSTANCE_BASE( wxStringTypeBufferLengthBase
<char> )
2884 class WXDLLIMPEXP_BASE wxUTF8StringBufferLength
2885 : public wxStringTypeBufferLengthBase
<char>
2888 wxUTF8StringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
2889 : wxStringTypeBufferLengthBase
<char>(str
, lenWanted
) {}
2890 ~wxUTF8StringBufferLength();
2892 DECLARE_NO_COPY_CLASS(wxUTF8StringBufferLength
)
2894 #endif // wxUSE_UNICODE_UTF8/wxUSE_UNICODE_WCHAR
2897 // ---------------------------------------------------------------------------
2898 // wxString comparison functions: operator versions are always case sensitive
2899 // ---------------------------------------------------------------------------
2901 #define wxCMP_WXCHAR_STRING(p, s, op) 0 op s.Cmp(p)
2903 wxDEFINE_ALL_COMPARISONS(const wxChar
*, const wxString
&, wxCMP_WXCHAR_STRING
)
2905 #undef wxCMP_WXCHAR_STRING
2907 // note that there is an optimization in operator==() and !=(): we (quickly)
2908 // checks the strings length first, before comparing their data
2909 inline bool operator==(const wxString
& s1
, const wxString
& s2
)
2910 { return (s1
.Len() == s2
.Len()) && (s1
.Cmp(s2
) == 0); }
2911 inline bool operator!=(const wxString
& s1
, const wxString
& s2
)
2912 { return (s1
.Len() != s2
.Len()) || (s1
.Cmp(s2
) != 0); }
2913 inline bool operator< (const wxString
& s1
, const wxString
& s2
)
2914 { return s1
.Cmp(s2
) < 0; }
2915 inline bool operator> (const wxString
& s1
, const wxString
& s2
)
2916 { return s1
.Cmp(s2
) > 0; }
2917 inline bool operator<=(const wxString
& s1
, const wxString
& s2
)
2918 { return s1
.Cmp(s2
) <= 0; }
2919 inline bool operator>=(const wxString
& s1
, const wxString
& s2
)
2920 { return s1
.Cmp(s2
) >= 0; }
2922 inline bool operator==(const wxString
& s1
, const wxCStrData
& s2
)
2923 { return s1
== s2
.AsString(); }
2924 inline bool operator==(const wxCStrData
& s1
, const wxString
& s2
)
2925 { return s1
.AsString() == s2
; }
2926 inline bool operator!=(const wxString
& s1
, const wxCStrData
& s2
)
2927 { return s1
!= s2
.AsString(); }
2928 inline bool operator!=(const wxCStrData
& s1
, const wxString
& s2
)
2929 { return s1
.AsString() != s2
; }
2931 inline bool operator==(const wxString
& s1
, const wxWCharBuffer
& s2
)
2932 { return (s1
.Cmp((const wchar_t *)s2
) == 0); }
2933 inline bool operator==(const wxWCharBuffer
& s1
, const wxString
& s2
)
2934 { return (s2
.Cmp((const wchar_t *)s1
) == 0); }
2935 inline bool operator!=(const wxString
& s1
, const wxWCharBuffer
& s2
)
2936 { return (s1
.Cmp((const wchar_t *)s2
) != 0); }
2937 inline bool operator!=(const wxWCharBuffer
& s1
, const wxString
& s2
)
2938 { return (s2
.Cmp((const wchar_t *)s1
) != 0); }
2940 inline bool operator==(const wxString
& s1
, const wxCharBuffer
& s2
)
2941 { return (s1
.Cmp((const char *)s2
) == 0); }
2942 inline bool operator==(const wxCharBuffer
& s1
, const wxString
& s2
)
2943 { return (s2
.Cmp((const char *)s1
) == 0); }
2944 inline bool operator!=(const wxString
& s1
, const wxCharBuffer
& s2
)
2945 { return (s1
.Cmp((const char *)s2
) != 0); }
2946 inline bool operator!=(const wxCharBuffer
& s1
, const wxString
& s2
)
2947 { return (s2
.Cmp((const char *)s1
) != 0); }
2949 inline wxString
operator+(const wxString
& string
, const wxWCharBuffer
& buf
)
2950 { return string
+ (const wchar_t *)buf
; }
2951 inline wxString
operator+(const wxWCharBuffer
& buf
, const wxString
& string
)
2952 { return (const wchar_t *)buf
+ string
; }
2954 inline wxString
operator+(const wxString
& string
, const wxCharBuffer
& buf
)
2955 { return string
+ (const char *)buf
; }
2956 inline wxString
operator+(const wxCharBuffer
& buf
, const wxString
& string
)
2957 { return (const char *)buf
+ string
; }
2959 // comparison with char
2960 inline bool operator==(const wxUniChar
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2961 inline bool operator==(const wxUniCharRef
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2962 inline bool operator==(char c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2963 inline bool operator==(wchar_t c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2964 inline bool operator==(int c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2965 inline bool operator==(const wxString
& s
, const wxUniChar
& c
) { return s
.IsSameAs(c
); }
2966 inline bool operator==(const wxString
& s
, const wxUniCharRef
& c
) { return s
.IsSameAs(c
); }
2967 inline bool operator==(const wxString
& s
, char c
) { return s
.IsSameAs(c
); }
2968 inline bool operator==(const wxString
& s
, wchar_t c
) { return s
.IsSameAs(c
); }
2969 inline bool operator!=(const wxUniChar
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2970 inline bool operator!=(const wxUniCharRef
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2971 inline bool operator!=(char c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2972 inline bool operator!=(wchar_t c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2973 inline bool operator!=(int c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2974 inline bool operator!=(const wxString
& s
, const wxUniChar
& c
) { return !s
.IsSameAs(c
); }
2975 inline bool operator!=(const wxString
& s
, const wxUniCharRef
& c
) { return !s
.IsSameAs(c
); }
2976 inline bool operator!=(const wxString
& s
, char c
) { return !s
.IsSameAs(c
); }
2977 inline bool operator!=(const wxString
& s
, wchar_t c
) { return !s
.IsSameAs(c
); }
2979 // comparison with C string in Unicode build
2982 #define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s
2984 wxDEFINE_ALL_COMPARISONS(const char *, const wxString
&, wxCMP_CHAR_STRING
)
2986 #undef wxCMP_CHAR_STRING
2988 #endif // wxUSE_UNICODE
2990 // we also need to provide the operators for comparison with wxCStrData to
2991 // resolve ambiguity between operator(const wxChar *,const wxString &) and
2992 // operator(const wxChar *, const wxChar *) for "p == s.c_str()"
2994 // notice that these are (shallow) pointer comparisons, not (deep) string ones
2995 #define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar()
2996 #define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar()
2998 wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData
&, wxCMP_WCHAR_CSTRDATA
)
2999 wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData
&, wxCMP_CHAR_CSTRDATA
)
3001 #undef wxCMP_CHAR_CSTRDATA
3002 #undef wxCMP_WCHAR_CSTRDATA
3004 // ---------------------------------------------------------------------------
3005 // Implementation only from here until the end of file
3006 // ---------------------------------------------------------------------------
3008 #if wxUSE_STD_IOSTREAM
3010 #include "wx/iosfwrap.h"
3012 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxString
&);
3013 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxCStrData
&);
3014 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxCharBuffer
&);
3015 #ifndef __BORLANDC__
3016 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxWCharBuffer
&);
3019 #if wxUSE_UNICODE && defined(HAVE_WOSTREAM)
3021 WXDLLIMPEXP_BASE wxSTD wostream
& operator<<(wxSTD wostream
&, const wxString
&);
3022 WXDLLIMPEXP_BASE wxSTD wostream
& operator<<(wxSTD wostream
&, const wxCStrData
&);
3023 WXDLLIMPEXP_BASE wxSTD wostream
& operator<<(wxSTD wostream
&, const wxWCharBuffer
&);
3025 #endif // wxUSE_UNICODE && defined(HAVE_WOSTREAM)
3027 #endif // wxUSE_STD_IOSTREAM
3029 // ---------------------------------------------------------------------------
3030 // wxCStrData implementation
3031 // ---------------------------------------------------------------------------
3033 inline wxCStrData::wxCStrData(char *buf
)
3034 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
3035 inline wxCStrData::wxCStrData(wchar_t *buf
)
3036 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
3038 inline wxCStrData::wxCStrData(const wxCStrData
& data
)
3039 : m_str(data
.m_owned
? new wxString(*data
.m_str
) : data
.m_str
),
3040 m_offset(data
.m_offset
),
3041 m_owned(data
.m_owned
)
3045 inline wxCStrData::~wxCStrData()
3048 delete wx_const_cast(wxString
*, m_str
); // cast to silence warnings
3051 // simple cases for AsChar() and AsWChar(), the complicated ones are
3053 #if wxUSE_UNICODE_WCHAR
3054 inline const wchar_t* wxCStrData::AsWChar() const
3056 return m_str
->wx_str() + m_offset
;
3058 #endif // wxUSE_UNICODE_WCHAR
3061 inline const char* wxCStrData::AsChar() const
3063 return m_str
->wx_str() + m_offset
;
3065 #endif // !wxUSE_UNICODE
3067 #if wxUSE_UTF8_LOCALE_ONLY
3068 inline const char* wxCStrData::AsChar() const
3070 return wxStringOperations::AddToIter(m_str
->wx_str(), m_offset
);
3072 #endif // wxUSE_UTF8_LOCALE_ONLY
3074 inline const wxCharBuffer
wxCStrData::AsCharBuf() const
3077 return wxCharBuffer::CreateNonOwned(AsChar());
3079 return AsString().mb_str();
3083 inline const wxWCharBuffer
wxCStrData::AsWCharBuf() const
3085 #if wxUSE_UNICODE_WCHAR
3086 return wxWCharBuffer::CreateNonOwned(AsWChar());
3088 return AsString().wc_str();
3092 inline wxString
wxCStrData::AsString() const
3094 if ( m_offset
== 0 )
3097 return m_str
->Mid(m_offset
);
3100 inline const wxStringCharType
*wxCStrData::AsInternal() const
3102 #if wxUSE_UNICODE_UTF8
3103 return wxStringOperations::AddToIter(m_str
->wx_str(), m_offset
);
3105 return m_str
->wx_str() + m_offset
;
3109 inline wxUniChar
wxCStrData::operator*() const
3111 if ( m_str
->empty() )
3112 return wxUniChar(_T('\0'));
3114 return (*m_str
)[m_offset
];
3117 inline wxUniChar
wxCStrData::operator[](size_t n
) const
3119 // NB: we intentionally use operator[] and not at() here because the former
3120 // works for the terminating NUL while the latter does not
3121 return (*m_str
)[m_offset
+ n
];
3124 // ----------------------------------------------------------------------------
3125 // more wxCStrData operators
3126 // ----------------------------------------------------------------------------
3128 // we need to define those to allow "size_t pos = p - s.c_str()" where p is
3129 // some pointer into the string
3130 inline size_t operator-(const char *p
, const wxCStrData
& cs
)
3132 return p
- cs
.AsChar();
3135 inline size_t operator-(const wchar_t *p
, const wxCStrData
& cs
)
3137 return p
- cs
.AsWChar();
3140 // ----------------------------------------------------------------------------
3141 // implementation of wx[W]CharBuffer inline methods using wxCStrData
3142 // ----------------------------------------------------------------------------
3144 // FIXME-UTF8: move this to buffer.h
3145 inline wxCharBuffer::wxCharBuffer(const wxCStrData
& cstr
)
3146 : wxCharTypeBufferBase(cstr
.AsCharBuf())
3150 inline wxWCharBuffer::wxWCharBuffer(const wxCStrData
& cstr
)
3151 : wxCharTypeBufferBase(cstr
.AsWCharBuf())
3155 #if wxUSE_UNICODE_UTF8
3156 // ----------------------------------------------------------------------------
3157 // implementation of wxStringIteratorNode inline methods
3158 // ----------------------------------------------------------------------------
3160 void wxStringIteratorNode::DoSet(const wxString
*str
,
3161 wxStringImpl::const_iterator
*citer
,
3162 wxStringImpl::iterator
*iter
)
3170 m_next
= str
->m_iterators
.ptr
;
3171 wx_const_cast(wxString
*, m_str
)->m_iterators
.ptr
= this;
3173 m_next
->m_prev
= this;
3181 void wxStringIteratorNode::clear()
3184 m_next
->m_prev
= m_prev
;
3186 m_prev
->m_next
= m_next
;
3187 else if ( m_str
) // first in the list
3188 wx_const_cast(wxString
*, m_str
)->m_iterators
.ptr
= m_next
;
3190 m_next
= m_prev
= NULL
;
3195 #endif // wxUSE_UNICODE_UTF8
3197 #if WXWIN_COMPATIBILITY_2_8
3198 // lot of code out there doesn't explicitly include wx/crt.h, but uses
3199 // CRT wrappers that are now declared in wx/wxcrt.h and wx/wxcrtvararg.h,
3200 // so let's include this header now that wxString is defined and it's safe
3205 #endif // _WX_WXSTRING_H_