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(__SALFORDC__)
137 return stricmp(psz1
, psz2
);
138 #elif defined(__BORLANDC__)
139 return stricmp(psz1
, psz2
);
140 #elif defined(__WATCOMC__)
141 return stricmp(psz1
, psz2
);
142 #elif defined(__DJGPP__)
143 return stricmp(psz1
, psz2
);
144 #elif defined(__EMX__)
145 return stricmp(psz1
, psz2
);
146 #elif defined(__WXPM__)
147 return stricmp(psz1
, psz2
);
148 #elif defined(__WXPALMOS__) || \
149 defined(HAVE_STRCASECMP_IN_STRING_H) || \
150 defined(HAVE_STRCASECMP_IN_STRINGS_H) || \
151 defined(__GNUWIN32__)
152 return strcasecmp(psz1
, psz2
);
153 #elif defined(__MWERKS__) && !defined(__INTEL__)
154 register char c1
, c2
;
156 c1
= tolower(*psz1
++);
157 c2
= tolower(*psz2
++);
158 } while ( c1
&& (c1
== c2
) );
162 // almost all compilers/libraries provide this function (unfortunately under
163 // different names), that's why we don't implement our own which will surely
164 // be more efficient than this code (uncomment to use):
166 register char c1, c2;
168 c1 = tolower(*psz1++);
169 c2 = tolower(*psz2++);
170 } while ( c1 && (c1 == c2) );
175 #error "Please define string case-insensitive compare for your OS/compiler"
176 #endif // OS/compiler
179 #endif // WXWIN_COMPATIBILITY_2_8
181 // ----------------------------------------------------------------------------
183 // ----------------------------------------------------------------------------
185 // Lightweight object returned by wxString::c_str() and implicitly convertible
186 // to either const char* or const wchar_t*.
187 class WXDLLIMPEXP_BASE wxCStrData
190 // Ctors; for internal use by wxString and wxCStrData only
191 wxCStrData(const wxString
*str
, size_t offset
= 0, bool owned
= false)
192 : m_str(str
), m_offset(offset
), m_owned(owned
) {}
195 // Ctor constructs the object from char literal; they are needed to make
196 // operator?: compile and they intentionally take char*, not const char*
197 inline wxCStrData(char *buf
);
198 inline wxCStrData(wchar_t *buf
);
199 inline wxCStrData(const wxCStrData
& data
);
201 inline ~wxCStrData();
203 // methods defined inline below must be declared inline or mingw32 3.4.5
204 // warns about "<symbol> defined locally after being referenced with
205 // dllimport linkage"
206 #if wxUSE_UNICODE_WCHAR
209 const wchar_t* AsWChar() const;
210 operator const wchar_t*() const { return AsWChar(); }
212 #if !wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY
215 const char* AsChar() const;
216 const unsigned char* AsUnsignedChar() const
217 { return (const unsigned char *) AsChar(); }
218 operator const char*() const { return AsChar(); }
219 operator const unsigned char*() const { return AsUnsignedChar(); }
221 operator const void*() const { return AsChar(); }
223 inline const wxCharBuffer
AsCharBuf() const;
224 inline const wxWCharBuffer
AsWCharBuf() const;
226 inline wxString
AsString() const;
228 // returns the value as C string in internal representation (equivalent
229 // to AsString().wx_str(), but more efficient)
230 const wxStringCharType
*AsInternal() const;
232 // allow expressions like "c_str()[0]":
233 inline wxUniChar
operator[](size_t n
) const;
234 wxUniChar
operator[](int n
) const { return operator[](size_t(n
)); }
235 wxUniChar
operator[](long n
) const { return operator[](size_t(n
)); }
236 #ifndef wxSIZE_T_IS_UINT
237 wxUniChar
operator[](unsigned int n
) const { return operator[](size_t(n
)); }
238 #endif // size_t != unsigned int
240 // these operators are needed to emulate the pointer semantics of c_str():
241 // expressions like "wxChar *p = str.c_str() + 1;" should continue to work
242 // (we need both versions to resolve ambiguities):
243 wxCStrData
operator+(int n
) const
244 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
245 wxCStrData
operator+(long n
) const
246 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
247 wxCStrData
operator+(size_t n
) const
248 { return wxCStrData(m_str
, m_offset
+ n
, m_owned
); }
250 // and these for "str.c_str() + n - 2":
251 wxCStrData
operator-(int n
) const
253 wxASSERT_MSG( n
<= (int)m_offset
,
254 _T("attempt to construct address before the beginning of the string") );
255 return wxCStrData(m_str
, m_offset
- n
, m_owned
);
257 wxCStrData
operator-(long n
) const
259 wxASSERT_MSG( n
<= (int)m_offset
,
260 _T("attempt to construct address before the beginning of the string") );
261 return wxCStrData(m_str
, m_offset
- n
, m_owned
);
263 wxCStrData
operator-(size_t n
) const
265 wxASSERT_MSG( n
<= m_offset
,
266 _T("attempt to construct address before the beginning of the string") );
267 return wxCStrData(m_str
, m_offset
- n
, m_owned
);
270 // this operator is needed to make expressions like "*c_str()" or
271 // "*(c_str() + 2)" work
272 inline wxUniChar
operator*() const;
275 const wxString
*m_str
;
279 friend class WXDLLIMPEXP_FWD_BASE wxString
;
282 // ----------------------------------------------------------------------------
283 // wxStringPrintfMixin
284 // ---------------------------------------------------------------------------
286 // NB: VC6 has a bug that causes linker errors if you have template methods
287 // in a class using __declspec(dllimport). The solution is to split such
288 // class into two classes, one that contains the template methods and does
289 // *not* use WXDLLIMPEXP_BASE and another class that contains the rest
290 // (with DLL linkage).
292 // We only do this for VC6 here, because the code is less efficient
293 // (Printf() has to use dynamic_cast<>) and because OpenWatcom compiler
294 // cannot compile this code.
296 #if defined(__VISUALC__) && __VISUALC__ < 1300
297 #define wxNEEDS_WXSTRING_PRINTF_MIXIN
300 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
301 // this class contains implementation of wxString's vararg methods, it's
302 // exported from wxBase DLL
303 class WXDLLIMPEXP_BASE wxStringPrintfMixinBase
306 wxStringPrintfMixinBase() {}
308 #if !wxUSE_UTF8_LOCALE_ONLY
309 int DoPrintfWchar(const wxChar
*format
, ...);
310 static wxString
DoFormatWchar(const wxChar
*format
, ...);
312 #if wxUSE_UNICODE_UTF8
313 int DoPrintfUtf8(const char *format
, ...);
314 static wxString
DoFormatUtf8(const char *format
, ...);
318 // this class contains template wrappers for wxString's vararg methods, it's
319 // intentionally *not* exported from the DLL in order to fix the VC6 bug
321 class wxStringPrintfMixin
: public wxStringPrintfMixinBase
324 // to further complicate things, we can't return wxString from
325 // wxStringPrintfMixin::Format() because wxString is not yet declared at
326 // this point; the solution is to use this fake type trait template - this
327 // way the compiler won't know the return type until Format() is used
328 // (this doesn't compile with Watcom, but VC6 compiles it just fine):
329 template<typename T
> struct StringReturnType
331 typedef wxString type
;
335 // these are duplicated wxString methods, they're also declared below
336 // if !wxNEEDS_WXSTRING_PRINTF_MIXIN:
338 // static wxString Format(const wString& format, ...) ATTRIBUTE_PRINTF_1;
339 WX_DEFINE_VARARG_FUNC_SANS_N0(static typename StringReturnType
<T1
>::type
,
340 Format
, 1, (const wxFormatString
&),
341 DoFormatWchar
, DoFormatUtf8
)
342 // We have to implement the version without template arguments manually
343 // because of the StringReturnType<> hack, although WX_DEFINE_VARARG_FUNC
344 // normally does it itself. It has to be a template so that we can use
345 // the hack, even though there's no real template parameter:
346 struct FormatDummyArg
{} ;
349 inline static typename StringReturnType
<T
>::type
350 Format(const wxFormatString
& fmt
, FormatDummyArg dummy
= FormatDummyArg())
352 return DoFormatWchar(fmt
);
355 // int Printf(const wxString& format, ...);
356 WX_DEFINE_VARARG_FUNC(int, Printf
, 1, (const wxFormatString
&),
357 DoPrintfWchar
, DoPrintfUtf8
)
358 // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2;
359 WX_DEFINE_VARARG_FUNC(int, sprintf
, 1, (const wxFormatString
&),
360 DoPrintfWchar
, DoPrintfUtf8
)
363 wxStringPrintfMixin() : wxStringPrintfMixinBase() {}
365 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
368 // ----------------------------------------------------------------------------
369 // wxString: string class trying to be compatible with std::string, MFC
370 // CString and wxWindows 1.x wxString all at once
371 // ---------------------------------------------------------------------------
373 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
374 // "non dll-interface class 'wxStringPrintfMixin' used as base interface
375 // for dll-interface class 'wxString'" -- this is OK in our case
376 #pragma warning (disable:4275)
379 #if wxUSE_UNICODE_UTF8
380 // see the comment near wxString::iterator for why we need this
381 struct WXDLLIMPEXP_BASE wxStringIteratorNode
383 wxStringIteratorNode()
384 : m_str(NULL
), m_citer(NULL
), m_iter(NULL
), m_prev(NULL
), m_next(NULL
) {}
385 wxStringIteratorNode(const wxString
*str
,
386 wxStringImpl::const_iterator
*citer
)
387 { DoSet(str
, citer
, NULL
); }
388 wxStringIteratorNode(const wxString
*str
, wxStringImpl::iterator
*iter
)
389 { DoSet(str
, NULL
, iter
); }
390 ~wxStringIteratorNode()
393 inline void set(const wxString
*str
, wxStringImpl::const_iterator
*citer
)
394 { clear(); DoSet(str
, citer
, NULL
); }
395 inline void set(const wxString
*str
, wxStringImpl::iterator
*iter
)
396 { clear(); DoSet(str
, NULL
, iter
); }
398 const wxString
*m_str
;
399 wxStringImpl::const_iterator
*m_citer
;
400 wxStringImpl::iterator
*m_iter
;
401 wxStringIteratorNode
*m_prev
, *m_next
;
405 inline void DoSet(const wxString
*str
,
406 wxStringImpl::const_iterator
*citer
,
407 wxStringImpl::iterator
*iter
);
409 // the node belongs to a particular iterator instance, it's not copied
410 // when a copy of the iterator is made
411 DECLARE_NO_COPY_CLASS(wxStringIteratorNode
)
413 #endif // wxUSE_UNICODE_UTF8
415 class WXDLLIMPEXP_BASE wxString
416 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
417 : public wxStringPrintfMixin
420 // NB: special care was taken in arranging the member functions in such order
421 // that all inline functions can be effectively inlined, verify that all
422 // performance critical functions are still inlined if you change order!
424 // an 'invalid' value for string index, moved to this place due to a CW bug
425 static const size_t npos
;
428 // if we hadn't made these operators private, it would be possible to
429 // compile "wxString s; s = 17;" without any warnings as 17 is implicitly
430 // converted to char in C and we do have operator=(char)
432 // NB: we don't need other versions (short/long and unsigned) as attempt
433 // to assign another numeric type to wxString will now result in
434 // ambiguity between operator=(char) and operator=(int)
435 wxString
& operator=(int);
437 // these methods are not implemented - there is _no_ conversion from int to
438 // string, you're doing something wrong if the compiler wants to call it!
440 // try `s << i' or `s.Printf("%d", i)' instead
444 // buffer for holding temporary substring when using any of the methods
445 // that take (char*,size_t) or (wchar_t*,size_t) arguments:
447 struct SubstrBufFromType
452 SubstrBufFromType(const T
& data_
, size_t len_
)
453 : data(data_
), len(len_
) {}
456 #if wxUSE_UNICODE_UTF8
457 // even char* -> char* needs conversion, from locale charset to UTF-8
458 typedef SubstrBufFromType
<wxCharBuffer
> SubstrBufFromWC
;
459 typedef SubstrBufFromType
<wxCharBuffer
> SubstrBufFromMB
;
460 #elif wxUSE_UNICODE_WCHAR
461 typedef SubstrBufFromType
<const wchar_t*> SubstrBufFromWC
;
462 typedef SubstrBufFromType
<wxWCharBuffer
> SubstrBufFromMB
;
464 typedef SubstrBufFromType
<const char*> SubstrBufFromMB
;
465 typedef SubstrBufFromType
<wxCharBuffer
> SubstrBufFromWC
;
469 // Functions implementing primitive operations on string data; wxString
470 // methods and iterators are implemented in terms of it. The differences
471 // between UTF-8 and wchar_t* representations of the string are mostly
474 #if wxUSE_UNICODE_UTF8
475 static SubstrBufFromMB
ConvertStr(const char *psz
, size_t nLength
,
476 const wxMBConv
& conv
);
477 static SubstrBufFromWC
ConvertStr(const wchar_t *pwz
, size_t nLength
,
478 const wxMBConv
& conv
);
479 #elif wxUSE_UNICODE_WCHAR
480 static SubstrBufFromMB
ConvertStr(const char *psz
, size_t nLength
,
481 const wxMBConv
& conv
);
483 static SubstrBufFromWC
ConvertStr(const wchar_t *pwz
, size_t nLength
,
484 const wxMBConv
& conv
);
487 #if !wxUSE_UNICODE_UTF8 // wxUSE_UNICODE_WCHAR or !wxUSE_UNICODE
488 // returns C string encoded as the implementation expects:
490 static const wchar_t* ImplStr(const wchar_t* str
)
491 { return str
? str
: wxT(""); }
492 static const SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
493 { return SubstrBufFromWC(str
, (str
&& n
== npos
) ? wxWcslen(str
) : n
); }
494 static wxWCharBuffer
ImplStr(const char* str
,
495 const wxMBConv
& conv
= wxConvLibc
)
496 { return ConvertStr(str
, npos
, conv
).data
; }
497 static SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
498 const wxMBConv
& conv
= wxConvLibc
)
499 { return ConvertStr(str
, n
, conv
); }
501 static const char* ImplStr(const char* str
,
502 const wxMBConv
& WXUNUSED(conv
) = wxConvLibc
)
503 { return str
? str
: ""; }
504 static const SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
505 const wxMBConv
& WXUNUSED(conv
) = wxConvLibc
)
506 { return SubstrBufFromMB(str
, (str
&& n
== npos
) ? wxStrlen(str
) : n
); }
507 static wxCharBuffer
ImplStr(const wchar_t* str
)
508 { return ConvertStr(str
, npos
, wxConvLibc
).data
; }
509 static SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
510 { return ConvertStr(str
, n
, wxConvLibc
); }
513 // translates position index in wxString to/from index in underlying
515 static size_t PosToImpl(size_t pos
) { return pos
; }
516 static void PosLenToImpl(size_t pos
, size_t len
,
517 size_t *implPos
, size_t *implLen
)
518 { *implPos
= pos
; *implLen
= len
; }
519 static size_t LenToImpl(size_t len
) { return len
; }
520 static size_t PosFromImpl(size_t pos
) { return pos
; }
522 #else // wxUSE_UNICODE_UTF8
524 static wxCharBuffer
ImplStr(const char* str
,
525 const wxMBConv
& conv
= wxConvLibc
)
526 { return ConvertStr(str
, npos
, conv
).data
; }
527 static SubstrBufFromMB
ImplStr(const char* str
, size_t n
,
528 const wxMBConv
& conv
= wxConvLibc
)
529 { return ConvertStr(str
, n
, conv
); }
531 static wxCharBuffer
ImplStr(const wchar_t* str
)
532 { return ConvertStr(str
, npos
, wxMBConvUTF8()).data
; }
533 static SubstrBufFromWC
ImplStr(const wchar_t* str
, size_t n
)
534 { return ConvertStr(str
, n
, wxMBConvUTF8()); }
536 size_t PosToImpl(size_t pos
) const
538 if ( pos
== 0 || pos
== npos
)
541 return (begin() + pos
).impl() - m_impl
.begin();
544 void PosLenToImpl(size_t pos
, size_t len
, size_t *implPos
, size_t *implLen
) const;
546 size_t LenToImpl(size_t len
) const
549 PosLenToImpl(0, len
, &pos
, &len2
);
553 size_t PosFromImpl(size_t pos
) const
555 if ( pos
== 0 || pos
== npos
)
558 return const_iterator(this, m_impl
.begin() + pos
) - begin();
560 #endif // !wxUSE_UNICODE_UTF8/wxUSE_UNICODE_UTF8
564 typedef wxUniChar value_type
;
565 typedef wxUniChar char_type
;
566 typedef wxUniCharRef reference
;
567 typedef wxChar
* pointer
;
568 typedef const wxChar
* const_pointer
;
570 typedef size_t size_type
;
571 typedef wxUniChar const_reference
;
574 #if wxUSE_UNICODE_UTF8
575 // random access is not O(1), as required by Random Access Iterator
576 #define WX_STR_ITERATOR_TAG std::bidirectional_iterator_tag
578 #define WX_STR_ITERATOR_TAG std::random_access_iterator_tag
581 #define WX_STR_ITERATOR_TAG void /* dummy type */
584 #define WX_STR_ITERATOR_IMPL(iterator_name, pointer_type, reference_type) \
586 typedef wxStringImpl::iterator_name underlying_iterator; \
588 typedef WX_STR_ITERATOR_TAG iterator_category; \
589 typedef wxUniChar value_type; \
590 typedef int difference_type; \
591 typedef reference_type reference; \
592 typedef pointer_type pointer; \
594 reference operator[](size_t n) const { return *(*this + n); } \
596 iterator_name& operator++() \
597 { wxStringOperations::IncIter(m_cur); return *this; } \
598 iterator_name& operator--() \
599 { wxStringOperations::DecIter(m_cur); return *this; } \
600 iterator_name operator++(int) \
602 iterator_name tmp = *this; \
603 wxStringOperations::IncIter(m_cur); \
606 iterator_name operator--(int) \
608 iterator_name tmp = *this; \
609 wxStringOperations::DecIter(m_cur); \
613 iterator_name& operator+=(int n) \
615 m_cur = wxStringOperations::AddToIter(m_cur, n); \
618 iterator_name& operator+=(size_t n) \
620 m_cur = wxStringOperations::AddToIter(m_cur, (int)n); \
623 iterator_name& operator-=(int n) \
625 m_cur = wxStringOperations::AddToIter(m_cur, -n); \
628 iterator_name& operator-=(size_t n) \
630 m_cur = wxStringOperations::AddToIter(m_cur, -(int)n); \
634 difference_type operator-(const iterator_name& i) const \
635 { return wxStringOperations::DiffIters(m_cur, i.m_cur); } \
637 bool operator==(const iterator_name& i) const \
638 { return m_cur == i.m_cur; } \
639 bool operator!=(const iterator_name& i) const \
640 { return m_cur != i.m_cur; } \
642 bool operator<(const iterator_name& i) const \
643 { return m_cur < i.m_cur; } \
644 bool operator>(const iterator_name& i) const \
645 { return m_cur > i.m_cur; } \
646 bool operator<=(const iterator_name& i) const \
647 { return m_cur <= i.m_cur; } \
648 bool operator>=(const iterator_name& i) const \
649 { return m_cur >= i.m_cur; } \
652 /* for internal wxString use only: */ \
653 underlying_iterator impl() const { return m_cur; } \
655 friend class wxString; \
656 friend class wxCStrData; \
659 underlying_iterator m_cur
661 class WXDLLIMPEXP_FWD_BASE const_iterator
;
663 #if wxUSE_UNICODE_UTF8
664 // NB: In UTF-8 build, (non-const) iterator needs to keep reference
665 // to the underlying wxStringImpl, because UTF-8 is variable-length
666 // encoding and changing the value pointer to by an iterator (using
667 // its operator*) requires calling wxStringImpl::replace() if the old
668 // and new values differ in their encoding's length.
670 // Furthermore, the replace() call may invalid all iterators for the
671 // string, so we have to keep track of outstanding iterators and update
672 // them if replace() happens.
674 // This is implemented by maintaining linked list of iterators for every
675 // string and traversing it in wxUniCharRef::operator=(). Head of the
676 // list is stored in wxString. (FIXME-UTF8)
678 class WXDLLIMPEXP_BASE iterator
680 WX_STR_ITERATOR_IMPL(iterator
, wxChar
*, wxUniCharRef
);
684 iterator(const iterator
& i
)
685 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
686 iterator
& operator=(const iterator
& i
)
687 { m_cur
= i
.m_cur
; m_node
.set(i
.str(), &m_cur
); return *this; }
689 reference
operator*()
690 { return wxUniCharRef::CreateForString(m_node
, m_cur
); }
692 iterator
operator+(int n
) const
693 { return iterator(str(), wxStringOperations::AddToIter(m_cur
, n
)); }
694 iterator
operator+(size_t n
) const
695 { return iterator(str(), wxStringOperations::AddToIter(m_cur
, (int)n
)); }
696 iterator
operator-(int n
) const
697 { return iterator(str(), wxStringOperations::AddToIter(m_cur
, -n
)); }
698 iterator
operator-(size_t n
) const
699 { return iterator(str(), wxStringOperations::AddToIter(m_cur
, -(int)n
)); }
702 iterator(wxString
*str
, underlying_iterator ptr
)
703 : m_cur(ptr
), m_node(str
, &m_cur
) {}
705 wxString
* str() const { return wx_const_cast(wxString
*, m_node
.m_str
); }
707 wxStringIteratorNode m_node
;
709 friend class const_iterator
;
712 class WXDLLIMPEXP_BASE const_iterator
714 // NB: reference_type is intentionally value, not reference, the character
715 // may be encoded differently in wxString data:
716 WX_STR_ITERATOR_IMPL(const_iterator
, const wxChar
*, wxUniChar
);
720 const_iterator(const const_iterator
& i
)
721 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
722 const_iterator(const iterator
& i
)
723 : m_cur(i
.m_cur
), m_node(i
.str(), &m_cur
) {}
725 const_iterator
& operator=(const const_iterator
& i
)
726 { m_cur
= i
.m_cur
; m_node
.set(i
.str(), &m_cur
); return *this; }
727 const_iterator
& operator=(const iterator
& i
)
728 { m_cur
= i
.m_cur
; m_node
.set(i
.str(), &m_cur
); return *this; }
730 reference
operator*() const
731 { return wxStringOperations::DecodeChar(m_cur
); }
733 const_iterator
operator+(int n
) const
734 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur
, n
)); }
735 const_iterator
operator+(size_t n
) const
736 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur
, (int)n
)); }
737 const_iterator
operator-(int n
) const
738 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur
, -n
)); }
739 const_iterator
operator-(size_t n
) const
740 { return const_iterator(str(), wxStringOperations::AddToIter(m_cur
, -(int)n
)); }
743 // for internal wxString use only:
744 const_iterator(const wxString
*str
, underlying_iterator ptr
)
745 : m_cur(ptr
), m_node(str
, &m_cur
) {}
747 const wxString
* str() const { return m_node
.m_str
; }
749 wxStringIteratorNode m_node
;
752 size_t IterToImplPos(wxString::iterator i
) const
753 { return wxStringImpl::const_iterator(i
.impl()) - m_impl
.begin(); }
755 #else // !wxUSE_UNICODE_UTF8
757 class WXDLLIMPEXP_BASE iterator
759 WX_STR_ITERATOR_IMPL(iterator
, wxChar
*, wxUniCharRef
);
763 iterator(const iterator
& i
) : m_cur(i
.m_cur
) {}
765 reference
operator*()
766 { return wxUniCharRef::CreateForString(m_cur
); }
768 iterator
operator+(int n
) const
769 { return iterator(wxStringOperations::AddToIter(m_cur
, n
)); }
770 iterator
operator+(size_t n
) const
771 { return iterator(wxStringOperations::AddToIter(m_cur
, (int)n
)); }
772 iterator
operator-(int n
) const
773 { return iterator(wxStringOperations::AddToIter(m_cur
, -n
)); }
774 iterator
operator-(size_t n
) const
775 { return iterator(wxStringOperations::AddToIter(m_cur
, -(int)n
)); }
778 // for internal wxString use only:
779 iterator(underlying_iterator ptr
) : m_cur(ptr
) {}
780 iterator(wxString
*WXUNUSED(str
), underlying_iterator ptr
) : m_cur(ptr
) {}
782 friend class const_iterator
;
785 class WXDLLIMPEXP_BASE const_iterator
787 // NB: reference_type is intentionally value, not reference, the character
788 // may be encoded differently in wxString data:
789 WX_STR_ITERATOR_IMPL(const_iterator
, const wxChar
*, wxUniChar
);
793 const_iterator(const const_iterator
& i
) : m_cur(i
.m_cur
) {}
794 const_iterator(const iterator
& i
) : m_cur(i
.m_cur
) {}
796 reference
operator*() const
797 { return wxStringOperations::DecodeChar(m_cur
); }
799 const_iterator
operator+(int n
) const
800 { return const_iterator(wxStringOperations::AddToIter(m_cur
, n
)); }
801 const_iterator
operator+(size_t n
) const
802 { return const_iterator(wxStringOperations::AddToIter(m_cur
, (int)n
)); }
803 const_iterator
operator-(int n
) const
804 { return const_iterator(wxStringOperations::AddToIter(m_cur
, -n
)); }
805 const_iterator
operator-(size_t n
) const
806 { return const_iterator(wxStringOperations::AddToIter(m_cur
, -(int)n
)); }
809 // for internal wxString use only:
810 const_iterator(underlying_iterator ptr
) : m_cur(ptr
) {}
811 const_iterator(const wxString
*WXUNUSED(str
), underlying_iterator ptr
)
814 #endif // wxUSE_UNICODE_UTF8/!wxUSE_UNICODE_UTF8
816 #undef WX_STR_ITERATOR_TAG
817 #undef WX_STR_ITERATOR_IMPL
819 friend class iterator
;
820 friend class const_iterator
;
822 template <typename T
>
823 class reverse_iterator_impl
826 typedef T iterator_type
;
828 typedef typename
T::iterator_category iterator_category
;
829 typedef typename
T::value_type value_type
;
830 typedef typename
T::difference_type difference_type
;
831 typedef typename
T::reference reference
;
832 typedef typename
T::pointer
*pointer
;
834 reverse_iterator_impl() {}
835 reverse_iterator_impl(iterator_type i
) : m_cur(i
) {}
836 reverse_iterator_impl(const reverse_iterator_impl
& ri
)
839 iterator_type
base() const { return m_cur
; }
841 reference
operator*() const { return *(m_cur
-1); }
842 reference
operator[](size_t n
) const { return *(*this + n
); }
844 reverse_iterator_impl
& operator++()
845 { --m_cur
; return *this; }
846 reverse_iterator_impl
operator++(int)
847 { reverse_iterator_impl tmp
= *this; --m_cur
; return tmp
; }
848 reverse_iterator_impl
& operator--()
849 { ++m_cur
; return *this; }
850 reverse_iterator_impl
operator--(int)
851 { reverse_iterator_impl tmp
= *this; ++m_cur
; return tmp
; }
853 // NB: explicit <T> in the functions below is to keep BCC 5.5 happy
854 reverse_iterator_impl
operator+(int n
) const
855 { return reverse_iterator_impl
<T
>(m_cur
- n
); }
856 reverse_iterator_impl
operator+(size_t n
) const
857 { return reverse_iterator_impl
<T
>(m_cur
- n
); }
858 reverse_iterator_impl
operator-(int n
) const
859 { return reverse_iterator_impl
<T
>(m_cur
+ n
); }
860 reverse_iterator_impl
operator-(size_t n
) const
861 { return reverse_iterator_impl
<T
>(m_cur
+ n
); }
862 reverse_iterator_impl
operator+=(int n
)
863 { m_cur
-= n
; return *this; }
864 reverse_iterator_impl
operator+=(size_t n
)
865 { m_cur
-= n
; return *this; }
866 reverse_iterator_impl
operator-=(int n
)
867 { m_cur
+= n
; return *this; }
868 reverse_iterator_impl
operator-=(size_t n
)
869 { m_cur
+= n
; return *this; }
871 unsigned operator-(const reverse_iterator_impl
& i
) const
872 { return i
.m_cur
- m_cur
; }
874 bool operator==(const reverse_iterator_impl
& ri
) const
875 { return m_cur
== ri
.m_cur
; }
876 bool operator!=(const reverse_iterator_impl
& ri
) const
877 { return !(*this == ri
); }
879 bool operator<(const reverse_iterator_impl
& i
) const
880 { return m_cur
> i
.m_cur
; }
881 bool operator>(const reverse_iterator_impl
& i
) const
882 { return m_cur
< i
.m_cur
; }
883 bool operator<=(const reverse_iterator_impl
& i
) const
884 { return m_cur
>= i
.m_cur
; }
885 bool operator>=(const reverse_iterator_impl
& i
) const
886 { return m_cur
<= i
.m_cur
; }
892 typedef reverse_iterator_impl
<iterator
> reverse_iterator
;
893 typedef reverse_iterator_impl
<const_iterator
> const_reverse_iterator
;
896 // used to transform an expression built using c_str() (and hence of type
897 // wxCStrData) to an iterator into the string
898 static const_iterator
CreateConstIterator(const wxCStrData
& data
)
900 return const_iterator(data
.m_str
,
901 (data
.m_str
->begin() + data
.m_offset
).impl());
904 // in UTF-8 STL build, creation from std::string requires conversion under
905 // non-UTF8 locales, so we can't have and use wxString(wxStringImpl) ctor;
906 // instead we define dummy type that lets us have wxString ctor for creation
907 // from wxStringImpl that couldn't be used by user code (in all other builds,
908 // "standard" ctors can be used):
909 #if wxUSE_UNICODE_UTF8 && wxUSE_STL_BASED_WXSTRING
910 struct CtorFromStringImplTag
{};
912 wxString(CtorFromStringImplTag
* WXUNUSED(dummy
), const wxStringImpl
& src
)
915 static wxString
FromImpl(const wxStringImpl
& src
)
916 { return wxString((CtorFromStringImplTag
*)NULL
, src
); }
918 #if !wxUSE_STL_BASED_WXSTRING
919 wxString(const wxStringImpl
& src
) : m_impl(src
) { }
920 // else: already defined as wxString(wxStdString) below
922 static wxString
FromImpl(const wxStringImpl
& src
) { return wxString(src
); }
926 // constructors and destructor
927 // ctor for an empty string
931 wxString(const wxString
& stringSrc
) : m_impl(stringSrc
.m_impl
) { }
933 // string containing nRepeat copies of ch
934 wxString(wxUniChar ch
, size_t nRepeat
= 1)
935 { assign(nRepeat
, ch
); }
936 wxString(size_t nRepeat
, wxUniChar ch
)
937 { assign(nRepeat
, ch
); }
938 wxString(wxUniCharRef ch
, size_t nRepeat
= 1)
939 { assign(nRepeat
, ch
); }
940 wxString(size_t nRepeat
, wxUniCharRef ch
)
941 { assign(nRepeat
, ch
); }
942 wxString(char ch
, size_t nRepeat
= 1)
943 { assign(nRepeat
, ch
); }
944 wxString(size_t nRepeat
, char ch
)
945 { assign(nRepeat
, ch
); }
946 wxString(wchar_t ch
, size_t nRepeat
= 1)
947 { assign(nRepeat
, ch
); }
948 wxString(size_t nRepeat
, wchar_t ch
)
949 { assign(nRepeat
, ch
); }
951 // ctors from char* strings:
952 wxString(const char *psz
)
953 : m_impl(ImplStr(psz
)) {}
954 wxString(const char *psz
, const wxMBConv
& conv
)
955 : m_impl(ImplStr(psz
, conv
)) {}
956 wxString(const char *psz
, size_t nLength
)
957 { assign(psz
, nLength
); }
958 wxString(const char *psz
, const wxMBConv
& conv
, size_t nLength
)
960 SubstrBufFromMB
str(ImplStr(psz
, nLength
, conv
));
961 m_impl
.assign(str
.data
, str
.len
);
964 // and unsigned char*:
965 wxString(const unsigned char *psz
)
966 : m_impl(ImplStr((const char*)psz
)) {}
967 wxString(const unsigned char *psz
, const wxMBConv
& conv
)
968 : m_impl(ImplStr((const char*)psz
, conv
)) {}
969 wxString(const unsigned char *psz
, size_t nLength
)
970 { assign((const char*)psz
, nLength
); }
971 wxString(const unsigned char *psz
, const wxMBConv
& conv
, size_t nLength
)
973 SubstrBufFromMB
str(ImplStr((const char*)psz
, nLength
, conv
));
974 m_impl
.assign(str
.data
, str
.len
);
977 // ctors from wchar_t* strings:
978 wxString(const wchar_t *pwz
)
979 : m_impl(ImplStr(pwz
)) {}
980 wxString(const wchar_t *pwz
, const wxMBConv
& WXUNUSED(conv
))
981 : m_impl(ImplStr(pwz
)) {}
982 wxString(const wchar_t *pwz
, size_t nLength
)
983 { assign(pwz
, nLength
); }
984 wxString(const wchar_t *pwz
, const wxMBConv
& WXUNUSED(conv
), size_t nLength
)
985 { assign(pwz
, nLength
); }
987 wxString(const wxCharBuffer
& buf
)
988 { assign(buf
.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
989 wxString(const wxWCharBuffer
& buf
)
990 { assign(buf
.data()); } // FIXME-UTF8: fix for embedded NUL and buffer length
992 wxString(const wxCStrData
& cstr
)
993 : m_impl(cstr
.AsString().m_impl
) { }
995 // as we provide both ctors with this signature for both char and unsigned
996 // char string, we need to provide one for wxCStrData to resolve ambiguity
997 wxString(const wxCStrData
& cstr
, size_t nLength
)
998 : m_impl(cstr
.AsString().Mid(0, nLength
).m_impl
) {}
1000 // and because wxString is convertible to wxCStrData and const wxChar *
1001 // we also need to provide this one
1002 wxString(const wxString
& str
, size_t nLength
)
1003 { assign(str
, nLength
); }
1005 // even if we're not built with wxUSE_STL == 1 it is very convenient to allow
1006 // implicit conversions from std::string to wxString and vice verse as this
1007 // allows to use the same strings in non-GUI and GUI code, however we don't
1008 // want to unconditionally add this ctor as it would make wx lib dependent on
1009 // libstdc++ on some Linux versions which is bad, so instead we ask the
1010 // client code to define this wxUSE_STD_STRING symbol if they need it
1011 #if wxUSE_STD_STRING
1012 #if wxUSE_UNICODE_WCHAR
1013 wxString(const wxStdWideString
& str
) : m_impl(str
) {}
1014 #else // UTF-8 or ANSI
1015 wxString(const wxStdWideString
& str
)
1016 { assign(str
.c_str(), str
.length()); }
1019 #if !wxUSE_UNICODE // ANSI build
1020 // FIXME-UTF8: do this in UTF8 build #if wxUSE_UTF8_LOCALE_ONLY, too
1021 wxString(const std::string
& str
) : m_impl(str
) {}
1023 wxString(const std::string
& str
)
1024 { assign(str
.c_str(), str
.length()); }
1026 #endif // wxUSE_STD_STRING
1028 // Unlike ctor from std::string, we provide conversion to std::string only
1029 // if wxUSE_STL and not merely wxUSE_STD_STRING (which is on by default),
1030 // because it conflicts with operator const char/wchar_t*:
1032 #if wxUSE_UNICODE_WCHAR && wxUSE_STL_BASED_WXSTRING
1033 // wxStringImpl is std::string in the encoding we want
1034 operator const wxStdWideString
&() const { return m_impl
; }
1036 // wxStringImpl is either not std::string or needs conversion
1037 operator wxStdWideString() const
1038 // FIXME-UTF8: broken for embedded NULs
1039 { return wxStdWideString(wc_str()); }
1042 #if (!wxUSE_UNICODE || wxUSE_UTF8_LOCALE_ONLY) && wxUSE_STL_BASED_WXSTRING
1043 // wxStringImpl is std::string in the encoding we want
1044 operator const std::string
&() const { return m_impl
; }
1046 // wxStringImpl is either not std::string or needs conversion
1047 operator std::string() const
1048 // FIXME-UTF8: broken for embedded NULs
1049 { return std::string(mb_str()); }
1053 // first valid index position
1054 const_iterator
begin() const { return const_iterator(this, m_impl
.begin()); }
1055 iterator
begin() { return iterator(this, m_impl
.begin()); }
1056 // position one after the last valid one
1057 const_iterator
end() const { return const_iterator(this, m_impl
.end()); }
1058 iterator
end() { return iterator(this, m_impl
.end()); }
1060 // first element of the reversed string
1061 const_reverse_iterator
rbegin() const
1062 { return const_reverse_iterator(end()); }
1063 reverse_iterator
rbegin()
1064 { return reverse_iterator(end()); }
1065 // one beyond the end of the reversed string
1066 const_reverse_iterator
rend() const
1067 { return const_reverse_iterator(begin()); }
1068 reverse_iterator
rend()
1069 { return reverse_iterator(begin()); }
1071 // std::string methods:
1072 #if wxUSE_UNICODE_UTF8
1073 size_t length() const { return end() - begin(); } // FIXME-UTF8: optimize!
1075 size_t length() const { return m_impl
.length(); }
1078 size_type
size() const { return length(); }
1079 size_type
max_size() const { return npos
; }
1081 bool empty() const { return m_impl
.empty(); }
1083 size_type
capacity() const { return m_impl
.capacity(); } // FIXME-UTF8
1084 void reserve(size_t sz
) { m_impl
.reserve(sz
); } // FIXME-UTF8
1086 void resize(size_t nSize
, wxUniChar ch
= wxT('\0'))
1088 #if wxUSE_UNICODE_UTF8
1089 if ( !ch
.IsAscii() )
1091 size_t len
= length();
1094 else if ( nSize
< len
)
1097 append(nSize
- len
, ch
);
1101 m_impl
.resize(nSize
, (wxStringCharType
)ch
);
1104 wxString
substr(size_t nStart
= 0, size_t nLen
= npos
) const
1107 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
1108 return FromImpl(m_impl
.substr(pos
, len
));
1111 // generic attributes & operations
1112 // as standard strlen()
1113 size_t Len() const { return length(); }
1114 // string contains any characters?
1115 bool IsEmpty() const { return empty(); }
1116 // empty string is "false", so !str will return true
1117 bool operator!() const { return empty(); }
1118 // truncate the string to given length
1119 wxString
& Truncate(size_t uiLen
);
1120 // empty string contents
1125 wxASSERT_MSG( empty(), _T("string not empty after call to Empty()?") );
1127 // empty the string and free memory
1130 wxString
tmp(wxEmptyString
);
1135 // Is an ascii value
1136 bool IsAscii() const;
1138 bool IsNumber() const;
1140 bool IsWord() const;
1142 // data access (all indexes are 0 based)
1144 wxUniChar
at(size_t n
) const
1145 { return *(begin() + n
); } // FIXME-UTF8: optimize?
1146 wxUniChar
GetChar(size_t n
) const
1148 // read/write access
1149 wxUniCharRef
at(size_t n
)
1150 { return *(begin() + n
); } // FIXME-UTF8: optimize?
1151 wxUniCharRef
GetWritableChar(size_t n
)
1154 void SetChar(size_t n
, wxUniChar ch
)
1157 // get last character
1158 wxUniChar
Last() const
1160 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1164 // get writable last character
1167 wxASSERT_MSG( !empty(), _T("wxString: index out of bounds") );
1172 Note that we we must define all of the overloads below to avoid
1173 ambiguity when using str[0].
1175 wxUniChar
operator[](int n
) const
1177 wxUniChar
operator[](long n
) const
1179 wxUniChar
operator[](size_t n
) const
1181 #ifndef wxSIZE_T_IS_UINT
1182 wxUniChar
operator[](unsigned int n
) const
1184 #endif // size_t != unsigned int
1186 // operator versions of GetWriteableChar()
1187 wxUniCharRef
operator[](int n
)
1189 wxUniCharRef
operator[](long n
)
1191 wxUniCharRef
operator[](size_t n
)
1193 #ifndef wxSIZE_T_IS_UINT
1194 wxUniCharRef
operator[](unsigned int n
)
1196 #endif // size_t != unsigned int
1198 // explicit conversion to C string (use this with printf()!)
1199 wxCStrData
c_str() const { return wxCStrData(this); }
1200 wxCStrData
data() const { return c_str(); }
1202 // implicit conversion to C string
1203 operator wxCStrData() const { return c_str(); }
1205 // the first two operators conflict with operators for conversion to
1206 // std::string and they must be disabled in STL build; the next one only
1207 // makes sense if conversions to char* are also defined and not defining it
1208 // in STL build also helps us to get more clear error messages for the code
1209 // which relies on implicit conversion to char* in STL build
1211 operator const char*() const { return c_str(); }
1212 operator const wchar_t*() const { return c_str(); }
1214 // implicit conversion to untyped pointer for compatibility with previous
1215 // wxWidgets versions: this is the same as conversion to const char * so it
1217 operator const void*() const { return c_str(); }
1220 // identical to c_str(), for MFC compatibility
1221 const wxCStrData
GetData() const { return c_str(); }
1223 // explicit conversion to C string in internal representation (char*,
1224 // wchar_t*, UTF-8-encoded char*, depending on the build):
1225 const wxStringCharType
*wx_str() const { return m_impl
.c_str(); }
1227 // conversion to *non-const* multibyte or widestring buffer; modifying
1228 // returned buffer won't affect the string, these methods are only useful
1229 // for passing values to const-incorrect functions
1230 wxWritableCharBuffer
char_str(const wxMBConv
& conv
= wxConvLibc
) const
1231 { return mb_str(conv
); }
1232 wxWritableWCharBuffer
wchar_str() const { return wc_str(); }
1234 // conversion to/from plain (i.e. 7 bit) ASCII: this is useful for
1235 // converting numbers or strings which are certain not to contain special
1236 // chars (typically system functions, X atoms, environment variables etc.)
1238 // the behaviour of these functions with the strings containing anything
1239 // else than 7 bit ASCII characters is undefined, use at your own risk.
1241 static wxString
FromAscii(const char *ascii
, size_t len
); // string
1242 static wxString
FromAscii(const char *ascii
); // string
1243 static wxString
FromAscii(const char ascii
); // char
1244 const wxCharBuffer
ToAscii() const;
1246 static wxString
FromAscii(const char *ascii
) { return wxString( ascii
); }
1247 static wxString
FromAscii(const char *ascii
, size_t len
)
1248 { return wxString( ascii
, len
); }
1249 static wxString
FromAscii(const char ascii
) { return wxString( ascii
); }
1250 const char *ToAscii() const { return c_str(); }
1251 #endif // Unicode/!Unicode
1253 // conversion to/from UTF-8:
1254 #if wxUSE_UNICODE_UTF8
1255 static wxString
FromUTF8(const char *utf8
)
1258 return wxEmptyString
;
1260 wxASSERT( wxStringOperations::IsValidUtf8String(utf8
) );
1261 return FromImpl(wxStringImpl(utf8
));
1263 static wxString
FromUTF8(const char *utf8
, size_t len
)
1266 return wxEmptyString
;
1268 return FromUTF8(utf8
);
1270 wxASSERT( wxStringOperations::IsValidUtf8String(utf8
, len
) );
1271 return FromImpl(wxStringImpl(utf8
, len
));
1273 const char* utf8_str() const { return wx_str(); }
1274 const char* ToUTF8() const { return wx_str(); }
1275 #elif wxUSE_UNICODE_WCHAR
1276 static wxString
FromUTF8(const char *utf8
)
1277 { return wxString(utf8
, wxMBConvUTF8()); }
1278 static wxString
FromUTF8(const char *utf8
, size_t len
)
1279 { return wxString(utf8
, wxMBConvUTF8(), len
); }
1280 const wxCharBuffer
utf8_str() const { return mb_str(wxMBConvUTF8()); }
1281 const wxCharBuffer
ToUTF8() const { return utf8_str(); }
1283 static wxString
FromUTF8(const char *utf8
)
1284 { return wxString(wxMBConvUTF8().cMB2WC(utf8
)); }
1285 static wxString
FromUTF8(const char *utf8
, size_t len
)
1288 wxWCharBuffer
buf(wxMBConvUTF8().cMB2WC(utf8
, len
== npos
? wxNO_LEN
: len
, &wlen
));
1289 return wxString(buf
.data(), wlen
);
1291 const wxCharBuffer
utf8_str() const
1292 { return wxMBConvUTF8().cWC2MB(wc_str()); }
1293 const wxCharBuffer
ToUTF8() const { return utf8_str(); }
1296 // functions for storing binary data in wxString:
1298 static wxString
From8BitData(const char *data
, size_t len
)
1299 { return wxString(data
, wxConvISO8859_1
, len
); }
1300 // version for NUL-terminated data:
1301 static wxString
From8BitData(const char *data
)
1302 { return wxString(data
, wxConvISO8859_1
); }
1303 const wxCharBuffer
To8BitData() const { return mb_str(wxConvISO8859_1
); }
1305 static wxString
From8BitData(const char *data
, size_t len
)
1306 { return wxString(data
, len
); }
1307 // version for NUL-terminated data:
1308 static wxString
From8BitData(const char *data
)
1309 { return wxString(data
); }
1310 const char *To8BitData() const { return c_str(); }
1311 #endif // Unicode/ANSI
1313 // conversions with (possible) format conversions: have to return a
1314 // buffer with temporary data
1316 // the functions defined (in either Unicode or ANSI) mode are mb_str() to
1317 // return an ANSI (multibyte) string, wc_str() to return a wide string and
1318 // fn_str() to return a string which should be used with the OS APIs
1319 // accepting the file names. The return value is always the same, but the
1320 // type differs because a function may either return pointer to the buffer
1321 // directly or have to use intermediate buffer for translation.
1324 #if wxUSE_UTF8_LOCALE_ONLY
1325 const char* mb_str() const { return wx_str(); }
1326 const wxCharBuffer
mb_str(const wxMBConv
& conv
) const;
1328 const wxCharBuffer
mb_str(const wxMBConv
& conv
= wxConvLibc
) const;
1331 const wxWX2MBbuf
mbc_str() const { return mb_str(*wxConvCurrent
); }
1333 #if wxUSE_UNICODE_WCHAR
1334 const wxChar
* wc_str() const { return wx_str(); }
1335 #elif wxUSE_UNICODE_UTF8
1336 const wxWCharBuffer
wc_str() const;
1338 // for compatibility with !wxUSE_UNICODE version
1339 const wxWX2WCbuf
wc_str(const wxMBConv
& WXUNUSED(conv
)) const
1340 { return wc_str(); }
1343 const wxCharBuffer
fn_str() const { return mb_str(wxConvFile
); }
1345 const wxWX2WCbuf
fn_str() const { return wc_str(); }
1346 #endif // wxMBFILES/!wxMBFILES
1349 const wxChar
* mb_str() const { return wx_str(); }
1351 // for compatibility with wxUSE_UNICODE version
1352 const wxChar
* mb_str(const wxMBConv
& WXUNUSED(conv
)) const { return wx_str(); }
1354 const wxWX2MBbuf
mbc_str() const { return mb_str(); }
1357 const wxWCharBuffer
wc_str(const wxMBConv
& conv
= wxConvLibc
) const;
1358 #endif // wxUSE_WCHAR_T
1360 const wxCharBuffer
fn_str() const { return wxConvFile
.cWC2WX( wc_str( wxConvLocal
) ); }
1362 const wxChar
* fn_str() const { return c_str(); }
1364 #endif // Unicode/ANSI
1366 // overloaded assignment
1367 // from another wxString
1368 wxString
& operator=(const wxString
& stringSrc
)
1369 { m_impl
= stringSrc
.m_impl
; return *this; }
1370 wxString
& operator=(const wxCStrData
& cstr
)
1371 { return *this = cstr
.AsString(); }
1373 wxString
& operator=(wxUniChar ch
)
1374 { m_impl
= wxStringOperations::EncodeChar(ch
); return *this; }
1375 wxString
& operator=(wxUniCharRef ch
)
1376 { return operator=((wxUniChar
)ch
); }
1377 wxString
& operator=(char ch
)
1378 { return operator=(wxUniChar(ch
)); }
1379 wxString
& operator=(unsigned char ch
)
1380 { return operator=(wxUniChar(ch
)); }
1381 wxString
& operator=(wchar_t ch
)
1382 { return operator=(wxUniChar(ch
)); }
1383 // from a C string - STL probably will crash on NULL,
1384 // so we need to compensate in that case
1385 #if wxUSE_STL_BASED_WXSTRING
1386 wxString
& operator=(const char *psz
)
1387 { if (psz
) m_impl
= ImplStr(psz
); else Clear(); return *this; }
1388 wxString
& operator=(const wchar_t *pwz
)
1389 { if (pwz
) m_impl
= ImplStr(pwz
); else Clear(); return *this; }
1391 wxString
& operator=(const char *psz
)
1392 { m_impl
= ImplStr(psz
); return *this; }
1393 wxString
& operator=(const wchar_t *pwz
)
1394 { m_impl
= ImplStr(pwz
); return *this; }
1396 wxString
& operator=(const unsigned char *psz
)
1397 { return operator=((const char*)psz
); }
1399 // from wxWCharBuffer
1400 wxString
& operator=(const wxWCharBuffer
& s
)
1401 { return operator=(s
.data()); } // FIXME-UTF8: fix for embedded NULs
1402 // from wxCharBuffer
1403 wxString
& operator=(const wxCharBuffer
& s
)
1404 { return operator=(s
.data()); } // FIXME-UTF8: fix for embedded NULs
1406 // string concatenation
1407 // in place concatenation
1409 Concatenate and return the result. Note that the left to right
1410 associativity of << allows to write things like "str << str1 << str2
1411 << ..." (unlike with +=)
1414 wxString
& operator<<(const wxString
& s
)
1416 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1417 wxASSERT_MSG( s
.IsValid(),
1418 _T("did you forget to call UngetWriteBuf()?") );
1424 // string += C string
1425 wxString
& operator<<(const char *psz
)
1426 { append(psz
); return *this; }
1427 wxString
& operator<<(const wchar_t *pwz
)
1428 { append(pwz
); return *this; }
1429 wxString
& operator<<(const wxCStrData
& psz
)
1430 { append(psz
.AsString()); return *this; }
1432 wxString
& operator<<(wxUniChar ch
) { append(1, ch
); return *this; }
1433 wxString
& operator<<(wxUniCharRef ch
) { append(1, ch
); return *this; }
1434 wxString
& operator<<(char ch
) { append(1, ch
); return *this; }
1435 wxString
& operator<<(unsigned char ch
) { append(1, ch
); return *this; }
1436 wxString
& operator<<(wchar_t ch
) { append(1, ch
); return *this; }
1438 // string += buffer (i.e. from wxGetString)
1439 wxString
& operator<<(const wxWCharBuffer
& s
)
1440 { return operator<<((const wchar_t *)s
); }
1441 wxString
& operator<<(const wxCharBuffer
& s
)
1442 { return operator<<((const char *)s
); }
1444 // string += C string
1445 wxString
& Append(const wxString
& s
)
1447 // test for empty() to share the string if possible
1454 wxString
& Append(const char* psz
)
1455 { append(psz
); return *this; }
1456 wxString
& Append(const wchar_t* pwz
)
1457 { append(pwz
); return *this; }
1458 wxString
& Append(const wxCStrData
& psz
)
1459 { append(psz
); return *this; }
1460 wxString
& Append(const wxCharBuffer
& psz
)
1461 { append(psz
); return *this; }
1462 wxString
& Append(const wxWCharBuffer
& psz
)
1463 { append(psz
); return *this; }
1464 // append count copies of given character
1465 wxString
& Append(wxUniChar ch
, size_t count
= 1u)
1466 { append(count
, ch
); return *this; }
1467 wxString
& Append(wxUniCharRef ch
, size_t count
= 1u)
1468 { append(count
, ch
); return *this; }
1469 wxString
& Append(char ch
, size_t count
= 1u)
1470 { append(count
, ch
); return *this; }
1471 wxString
& Append(unsigned char ch
, size_t count
= 1u)
1472 { append(count
, ch
); return *this; }
1473 wxString
& Append(wchar_t ch
, size_t count
= 1u)
1474 { append(count
, ch
); return *this; }
1475 wxString
& Append(const char* psz
, size_t nLen
)
1476 { append(psz
, nLen
); return *this; }
1477 wxString
& Append(const wchar_t* pwz
, size_t nLen
)
1478 { append(pwz
, nLen
); return *this; }
1480 // prepend a string, return the string itself
1481 wxString
& Prepend(const wxString
& str
)
1482 { *this = str
+ *this; return *this; }
1484 // non-destructive concatenation
1486 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string1
,
1487 const wxString
& string2
);
1488 // string with a single char
1489 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
, wxUniChar ch
);
1490 // char with a string
1491 friend wxString WXDLLIMPEXP_BASE
operator+(wxUniChar ch
, const wxString
& string
);
1492 // string with C string
1493 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
1495 friend wxString WXDLLIMPEXP_BASE
operator+(const wxString
& string
,
1496 const wchar_t *pwz
);
1497 // C string with string
1498 friend wxString WXDLLIMPEXP_BASE
operator+(const char *psz
,
1499 const wxString
& string
);
1500 friend wxString WXDLLIMPEXP_BASE
operator+(const wchar_t *pwz
,
1501 const wxString
& string
);
1503 // stream-like functions
1504 // insert an int into string
1505 wxString
& operator<<(int i
)
1506 { return (*this) << Format(_T("%d"), i
); }
1507 // insert an unsigned int into string
1508 wxString
& operator<<(unsigned int ui
)
1509 { return (*this) << Format(_T("%u"), ui
); }
1510 // insert a long into string
1511 wxString
& operator<<(long l
)
1512 { return (*this) << Format(_T("%ld"), l
); }
1513 // insert an unsigned long into string
1514 wxString
& operator<<(unsigned long ul
)
1515 { return (*this) << Format(_T("%lu"), ul
); }
1516 #if defined wxLongLong_t && !defined wxLongLongIsLong
1517 // insert a long long if they exist and aren't longs
1518 wxString
& operator<<(wxLongLong_t ll
)
1520 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("d");
1521 return (*this) << Format(fmt
, ll
);
1523 // insert an unsigned long long
1524 wxString
& operator<<(wxULongLong_t ull
)
1526 const wxChar
*fmt
= _T("%") wxLongLongFmtSpec
_T("u");
1527 return (*this) << Format(fmt
, ull
);
1530 // insert a float into string
1531 wxString
& operator<<(float f
)
1532 { return (*this) << Format(_T("%f"), f
); }
1533 // insert a double into string
1534 wxString
& operator<<(double d
)
1535 { return (*this) << Format(_T("%g"), d
); }
1537 // string comparison
1538 // case-sensitive comparison (returns a value < 0, = 0 or > 0)
1539 int Cmp(const char *psz
) const
1540 { return compare(psz
); }
1541 int Cmp(const wchar_t *pwz
) const
1542 { return compare(pwz
); }
1543 int Cmp(const wxString
& s
) const
1544 { return compare(s
); }
1545 int Cmp(const wxCStrData
& s
) const
1546 { return compare(s
); }
1547 int Cmp(const wxCharBuffer
& s
) const
1548 { return compare(s
); }
1549 int Cmp(const wxWCharBuffer
& s
) const
1550 { return compare(s
); }
1551 // same as Cmp() but not case-sensitive
1552 int CmpNoCase(const wxString
& s
) const;
1553 // test for the string equality, either considering case or not
1554 // (if compareWithCase then the case matters)
1555 bool IsSameAs(const wxString
& str
, bool compareWithCase
= true) const
1556 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
1557 bool IsSameAs(const char *str
, bool compareWithCase
= true) const
1558 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
1559 bool IsSameAs(const wchar_t *str
, bool compareWithCase
= true) const
1560 { return (compareWithCase
? Cmp(str
) : CmpNoCase(str
)) == 0; }
1561 bool IsSameAs(const wxCStrData
& str
, bool compareWithCase
= true) const
1562 { return IsSameAs(str
.AsString(), compareWithCase
); }
1563 bool IsSameAs(const wxCharBuffer
& str
, bool compareWithCase
= true) const
1564 { return IsSameAs(str
.data(), compareWithCase
); }
1565 bool IsSameAs(const wxWCharBuffer
& str
, bool compareWithCase
= true) const
1566 { return IsSameAs(str
.data(), compareWithCase
); }
1567 // comparison with a single character: returns true if equal
1568 bool IsSameAs(wxUniChar c
, bool compareWithCase
= true) const;
1569 // FIXME-UTF8: remove these overloads
1570 bool IsSameAs(wxUniCharRef c
, bool compareWithCase
= true) const
1571 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1572 bool IsSameAs(char c
, bool compareWithCase
= true) const
1573 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1574 bool IsSameAs(unsigned char c
, bool compareWithCase
= true) const
1575 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1576 bool IsSameAs(wchar_t c
, bool compareWithCase
= true) const
1577 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1578 bool IsSameAs(int c
, bool compareWithCase
= true) const
1579 { return IsSameAs(wxUniChar(c
), compareWithCase
); }
1581 // simple sub-string extraction
1582 // return substring starting at nFirst of length nCount (or till the end
1583 // if nCount = default value)
1584 wxString
Mid(size_t nFirst
, size_t nCount
= npos
) const;
1586 // operator version of Mid()
1587 wxString
operator()(size_t start
, size_t len
) const
1588 { return Mid(start
, len
); }
1590 // check if the string starts with the given prefix and return the rest
1591 // of the string in the provided pointer if it is not NULL; otherwise
1593 bool StartsWith(const wxString
& prefix
, wxString
*rest
= NULL
) const;
1594 // check if the string ends with the given suffix and return the
1595 // beginning of the string before the suffix in the provided pointer if
1596 // it is not NULL; otherwise return false
1597 bool EndsWith(const wxString
& suffix
, wxString
*rest
= NULL
) const;
1599 // get first nCount characters
1600 wxString
Left(size_t nCount
) const;
1601 // get last nCount characters
1602 wxString
Right(size_t nCount
) const;
1603 // get all characters before the first occurance of ch
1604 // (returns the whole string if ch not found)
1605 wxString
BeforeFirst(wxUniChar ch
) const;
1606 // get all characters before the last occurence of ch
1607 // (returns empty string if ch not found)
1608 wxString
BeforeLast(wxUniChar ch
) const;
1609 // get all characters after the first occurence of ch
1610 // (returns empty string if ch not found)
1611 wxString
AfterFirst(wxUniChar ch
) const;
1612 // get all characters after the last occurence of ch
1613 // (returns the whole string if ch not found)
1614 wxString
AfterLast(wxUniChar ch
) const;
1616 // for compatibility only, use more explicitly named functions above
1617 wxString
Before(wxUniChar ch
) const { return BeforeLast(ch
); }
1618 wxString
After(wxUniChar ch
) const { return AfterFirst(ch
); }
1621 // convert to upper case in place, return the string itself
1622 wxString
& MakeUpper();
1623 // convert to upper case, return the copy of the string
1624 // Here's something to remember: BC++ doesn't like returns in inlines.
1625 wxString
Upper() const ;
1626 // convert to lower case in place, return the string itself
1627 wxString
& MakeLower();
1628 // convert to lower case, return the copy of the string
1629 wxString
Lower() const ;
1631 // trimming/padding whitespace (either side) and truncating
1632 // remove spaces from left or from right (default) side
1633 wxString
& Trim(bool bFromRight
= true);
1634 // add nCount copies chPad in the beginning or at the end (default)
1635 wxString
& Pad(size_t nCount
, wxUniChar chPad
= wxT(' '), bool bFromRight
= true);
1637 // searching and replacing
1638 // searching (return starting index, or -1 if not found)
1639 int Find(wxUniChar ch
, bool bFromEnd
= false) const; // like strchr/strrchr
1640 int Find(wxUniCharRef ch
, bool bFromEnd
= false) const
1641 { return Find(wxUniChar(ch
), bFromEnd
); }
1642 int Find(char ch
, bool bFromEnd
= false) const
1643 { return Find(wxUniChar(ch
), bFromEnd
); }
1644 int Find(unsigned char ch
, bool bFromEnd
= false) const
1645 { return Find(wxUniChar(ch
), bFromEnd
); }
1646 int Find(wchar_t ch
, bool bFromEnd
= false) const
1647 { return Find(wxUniChar(ch
), bFromEnd
); }
1648 // searching (return starting index, or -1 if not found)
1649 int Find(const wxString
& sub
) const // like strstr
1651 size_type idx
= find(sub
);
1652 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
1654 int Find(const char *sub
) const // like strstr
1656 size_type idx
= find(sub
);
1657 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
1659 int Find(const wchar_t *sub
) const // like strstr
1661 size_type idx
= find(sub
);
1662 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
1665 int Find(const wxCStrData
& sub
) const
1666 { return Find(sub
.AsString()); }
1667 int Find(const wxCharBuffer
& sub
) const
1668 { return Find(sub
.data()); }
1669 int Find(const wxWCharBuffer
& sub
) const
1670 { return Find(sub
.data()); }
1672 // replace first (or all of bReplaceAll) occurences of substring with
1673 // another string, returns the number of replacements made
1674 size_t Replace(const wxString
& strOld
,
1675 const wxString
& strNew
,
1676 bool bReplaceAll
= true);
1678 // check if the string contents matches a mask containing '*' and '?'
1679 bool Matches(const wxString
& mask
) const;
1681 // conversion to numbers: all functions return true only if the whole
1682 // string is a number and put the value of this number into the pointer
1683 // provided, the base is the numeric base in which the conversion should be
1684 // done and must be comprised between 2 and 36 or be 0 in which case the
1685 // standard C rules apply (leading '0' => octal, "0x" => hex)
1686 // convert to a signed integer
1687 bool ToLong(long *val
, int base
= 10) const;
1688 // convert to an unsigned integer
1689 bool ToULong(unsigned long *val
, int base
= 10) const;
1690 // convert to wxLongLong
1691 #if defined(wxLongLong_t)
1692 bool ToLongLong(wxLongLong_t
*val
, int base
= 10) const;
1693 // convert to wxULongLong
1694 bool ToULongLong(wxULongLong_t
*val
, int base
= 10) const;
1695 #endif // wxLongLong_t
1696 // convert to a double
1697 bool ToDouble(double *val
) const;
1700 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1701 // formatted input/output
1702 // as sprintf(), returns the number of characters written or < 0 on error
1703 // (take 'this' into account in attribute parameter count)
1704 // int Printf(const wxString& format, ...);
1705 WX_DEFINE_VARARG_FUNC(int, Printf
, 1, (const wxFormatString
&),
1706 DoPrintfWchar
, DoPrintfUtf8
)
1708 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1709 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wxString
&),
1710 (wxFormatString(f1
)));
1711 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wxCStrData
&),
1712 (wxFormatString(f1
)));
1713 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const char*),
1714 (wxFormatString(f1
)));
1715 WX_VARARG_WATCOM_WORKAROUND(int, Printf
, 1, (const wchar_t*),
1716 (wxFormatString(f1
)));
1718 #endif // !wxNEEDS_WXSTRING_PRINTF_MIXIN
1719 // as vprintf(), returns the number of characters written or < 0 on error
1720 int PrintfV(const wxString
& format
, va_list argptr
);
1722 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1723 // returns the string containing the result of Printf() to it
1724 // static wxString Format(const wxString& format, ...) ATTRIBUTE_PRINTF_1;
1725 WX_DEFINE_VARARG_FUNC(static wxString
, Format
, 1, (const wxFormatString
&),
1726 DoFormatWchar
, DoFormatUtf8
)
1728 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1729 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wxString
&),
1730 (wxFormatString(f1
)));
1731 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wxCStrData
&),
1732 (wxFormatString(f1
)));
1733 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const char*),
1734 (wxFormatString(f1
)));
1735 WX_VARARG_WATCOM_WORKAROUND(static wxString
, Format
, 1, (const wchar_t*),
1736 (wxFormatString(f1
)));
1739 // the same as above, but takes a va_list
1740 static wxString
FormatV(const wxString
& format
, va_list argptr
);
1742 // raw access to string memory
1743 // ensure that string has space for at least nLen characters
1744 // only works if the data of this string is not shared
1745 bool Alloc(size_t nLen
) { reserve(nLen
); /*return capacity() >= nLen;*/ return true; }
1746 // minimize the string's memory
1747 // only works if the data of this string is not shared
1749 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
1750 // These are deprecated, use wxStringBuffer or wxStringBufferLength instead
1752 // get writable buffer of at least nLen bytes. Unget() *must* be called
1753 // a.s.a.p. to put string back in a reasonable state!
1754 wxDEPRECATED( wxStringCharType
*GetWriteBuf(size_t nLen
) );
1755 // call this immediately after GetWriteBuf() has been used
1756 wxDEPRECATED( void UngetWriteBuf() );
1757 wxDEPRECATED( void UngetWriteBuf(size_t nLen
) );
1758 #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && wxUSE_UNICODE_UTF8
1760 // wxWidgets version 1 compatibility functions
1763 wxString
SubString(size_t from
, size_t to
) const
1764 { return Mid(from
, (to
- from
+ 1)); }
1765 // values for second parameter of CompareTo function
1766 enum caseCompare
{exact
, ignoreCase
};
1767 // values for first parameter of Strip function
1768 enum stripType
{leading
= 0x1, trailing
= 0x2, both
= 0x3};
1770 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
1772 // (take 'this' into account in attribute parameter count)
1773 // int sprintf(const wxString& format, ...) ATTRIBUTE_PRINTF_2;
1774 WX_DEFINE_VARARG_FUNC(int, sprintf
, 1, (const wxFormatString
&),
1775 DoPrintfWchar
, DoPrintfUtf8
)
1777 // workaround for http://bugzilla.openwatcom.org/show_bug.cgi?id=351
1778 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wxString
&),
1779 (wxFormatString(f1
)));
1780 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wxCStrData
&),
1781 (wxFormatString(f1
)));
1782 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const char*),
1783 (wxFormatString(f1
)));
1784 WX_VARARG_WATCOM_WORKAROUND(int, sprintf
, 1, (const wchar_t*),
1785 (wxFormatString(f1
)));
1787 #endif // wxNEEDS_WXSTRING_PRINTF_MIXIN
1790 inline int CompareTo(const wxChar
* psz
, caseCompare cmp
= exact
) const
1791 { return cmp
== exact
? Cmp(psz
) : CmpNoCase(psz
); }
1794 size_t Length() const { return length(); }
1795 // Count the number of characters
1796 int Freq(wxUniChar ch
) const;
1798 void LowerCase() { MakeLower(); }
1800 void UpperCase() { MakeUpper(); }
1801 // use Trim except that it doesn't change this string
1802 wxString
Strip(stripType w
= trailing
) const;
1804 // use Find (more general variants not yet supported)
1805 size_t Index(const wxChar
* psz
) const { return Find(psz
); }
1806 size_t Index(wxUniChar ch
) const { return Find(ch
); }
1808 wxString
& Remove(size_t pos
) { return Truncate(pos
); }
1809 wxString
& RemoveLast(size_t n
= 1) { return Truncate(length() - n
); }
1811 wxString
& Remove(size_t nStart
, size_t nLen
)
1812 { return (wxString
&)erase( nStart
, nLen
); }
1815 int First( wxUniChar ch
) const { return Find(ch
); }
1816 int First( wxUniCharRef ch
) const { return Find(ch
); }
1817 int First( char ch
) const { return Find(ch
); }
1818 int First( unsigned char ch
) const { return Find(ch
); }
1819 int First( wchar_t ch
) const { return Find(ch
); }
1820 int First( const wxString
& str
) const { return Find(str
); }
1821 int Last( wxUniChar ch
) const { return Find(ch
, true); }
1822 bool Contains(const wxString
& str
) const { return Find(str
) != wxNOT_FOUND
; }
1825 bool IsNull() const { return empty(); }
1827 // std::string compatibility functions
1829 // take nLen chars starting at nPos
1830 wxString(const wxString
& str
, size_t nPos
, size_t nLen
)
1831 { assign(str
, nPos
, nLen
); }
1832 // take all characters from first to last
1833 wxString(const_iterator first
, const_iterator last
)
1834 : m_impl(first
.impl(), last
.impl()) { }
1835 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1836 // the 2 overloads below are for compatibility with the existing code using
1837 // pointers instead of iterators
1838 wxString(const char *first
, const char *last
)
1840 SubstrBufFromMB
str(ImplStr(first
, last
- first
));
1841 m_impl
.assign(str
.data
, str
.len
);
1843 wxString(const wchar_t *first
, const wchar_t *last
)
1845 SubstrBufFromWC
str(ImplStr(first
, last
- first
));
1846 m_impl
.assign(str
.data
, str
.len
);
1848 // and this one is needed to compile code adding offsets to c_str() result
1849 wxString(const wxCStrData
& first
, const wxCStrData
& last
)
1850 : m_impl(CreateConstIterator(first
).impl(),
1851 CreateConstIterator(last
).impl())
1853 wxASSERT_MSG( first
.m_str
== last
.m_str
,
1854 _T("pointers must be into the same string") );
1856 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1858 // lib.string.modifiers
1859 // append elements str[pos], ..., str[pos+n]
1860 wxString
& append(const wxString
& str
, size_t pos
, size_t n
)
1863 str
.PosLenToImpl(pos
, n
, &from
, &len
);
1864 m_impl
.append(str
.m_impl
, from
, len
);
1868 wxString
& append(const wxString
& str
)
1869 { m_impl
.append(str
.m_impl
); return *this; }
1870 // append first n (or all if n == npos) characters of sz
1871 wxString
& append(const char *sz
)
1872 { m_impl
.append(ImplStr(sz
)); return *this; }
1873 wxString
& append(const wchar_t *sz
)
1874 { m_impl
.append(ImplStr(sz
)); return *this; }
1875 wxString
& append(const char *sz
, size_t n
)
1877 SubstrBufFromMB
str(ImplStr(sz
, n
));
1878 m_impl
.append(str
.data
, str
.len
);
1881 wxString
& append(const wchar_t *sz
, size_t n
)
1883 SubstrBufFromWC
str(ImplStr(sz
, n
));
1884 m_impl
.append(str
.data
, str
.len
);
1888 wxString
& append(const wxCStrData
& str
)
1889 { return append(str
.AsString()); }
1890 wxString
& append(const wxCharBuffer
& str
)
1891 { return append(str
.data()); }
1892 wxString
& append(const wxWCharBuffer
& str
)
1893 { return append(str
.data()); }
1895 // append n copies of ch
1896 wxString
& append(size_t n
, wxUniChar ch
)
1898 #if wxUSE_UNICODE_UTF8
1899 if ( !ch
.IsAscii() )
1900 m_impl
.append(wxStringOperations::EncodeNChars(n
, ch
));
1903 m_impl
.append(n
, (wxStringCharType
)ch
);
1906 // append from first to last
1907 wxString
& append(const_iterator first
, const_iterator last
)
1908 { m_impl
.append(first
.impl(), last
.impl()); return *this; }
1909 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1910 wxString
& append(const char *first
, const char *last
)
1911 { return append(first
, last
- first
); }
1912 wxString
& append(const wchar_t *first
, const wchar_t *last
)
1913 { return append(first
, last
- first
); }
1914 wxString
& append(const wxCStrData
& first
, const wxCStrData
& last
)
1915 { return append(CreateConstIterator(first
), CreateConstIterator(last
)); }
1916 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1918 // same as `this_string = str'
1919 wxString
& assign(const wxString
& str
)
1920 { m_impl
= str
.m_impl
; return *this; }
1921 wxString
& assign(const wxString
& str
, size_t len
)
1923 m_impl
.assign(str
.m_impl
, 0, str
.LenToImpl(len
));
1926 // same as ` = str[pos..pos + n]
1927 wxString
& assign(const wxString
& str
, size_t pos
, size_t n
)
1930 str
.PosLenToImpl(pos
, n
, &from
, &len
);
1931 m_impl
.assign(str
.m_impl
, from
, len
);
1934 // same as `= first n (or all if n == npos) characters of sz'
1935 wxString
& assign(const char *sz
)
1936 { m_impl
.assign(ImplStr(sz
)); return *this; }
1937 wxString
& assign(const wchar_t *sz
)
1938 { m_impl
.assign(ImplStr(sz
)); return *this; }
1939 wxString
& assign(const char *sz
, size_t n
)
1941 SubstrBufFromMB
str(ImplStr(sz
, n
));
1942 m_impl
.assign(str
.data
, str
.len
);
1945 wxString
& assign(const wchar_t *sz
, size_t n
)
1947 SubstrBufFromWC
str(ImplStr(sz
, n
));
1948 m_impl
.assign(str
.data
, str
.len
);
1952 wxString
& assign(const wxCStrData
& str
)
1953 { return assign(str
.AsString()); }
1954 wxString
& assign(const wxCharBuffer
& str
)
1955 { return assign(str
.data()); }
1956 wxString
& assign(const wxWCharBuffer
& str
)
1957 { return assign(str
.data()); }
1958 wxString
& assign(const wxCStrData
& str
, size_t len
)
1959 { return assign(str
.AsString(), len
); }
1960 wxString
& assign(const wxCharBuffer
& str
, size_t len
)
1961 { return assign(str
.data(), len
); }
1962 wxString
& assign(const wxWCharBuffer
& str
, size_t len
)
1963 { return assign(str
.data(), len
); }
1965 // same as `= n copies of ch'
1966 wxString
& assign(size_t n
, wxUniChar ch
)
1968 #if wxUSE_UNICODE_UTF8
1969 if ( !ch
.IsAscii() )
1970 m_impl
.assign(wxStringOperations::EncodeNChars(n
, ch
));
1973 m_impl
.assign(n
, (wxStringCharType
)ch
);
1977 wxString
& assign(size_t n
, wxUniCharRef ch
)
1978 { return assign(n
, wxUniChar(ch
)); }
1979 wxString
& assign(size_t n
, char ch
)
1980 { return assign(n
, wxUniChar(ch
)); }
1981 wxString
& assign(size_t n
, unsigned char ch
)
1982 { return assign(n
, wxUniChar(ch
)); }
1983 wxString
& assign(size_t n
, wchar_t ch
)
1984 { return assign(n
, wxUniChar(ch
)); }
1986 // assign from first to last
1987 wxString
& assign(const_iterator first
, const_iterator last
)
1988 { m_impl
.assign(first
.impl(), last
.impl()); return *this; }
1989 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1990 wxString
& assign(const char *first
, const char *last
)
1991 { return assign(first
, last
- first
); }
1992 wxString
& assign(const wchar_t *first
, const wchar_t *last
)
1993 { return assign(first
, last
- first
); }
1994 wxString
& assign(const wxCStrData
& first
, const wxCStrData
& last
)
1995 { return assign(CreateConstIterator(first
), CreateConstIterator(last
)); }
1996 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
1998 // string comparison
1999 int compare(const wxString
& str
) const;
2000 int compare(const char* sz
) const;
2001 int compare(const wchar_t* sz
) const;
2002 int compare(const wxCStrData
& str
) const
2003 { return compare(str
.AsString()); }
2004 int compare(const wxCharBuffer
& str
) const
2005 { return compare(str
.data()); }
2006 int compare(const wxWCharBuffer
& str
) const
2007 { return compare(str
.data()); }
2008 // comparison with a substring
2009 int compare(size_t nStart
, size_t nLen
, const wxString
& str
) const;
2010 // comparison of 2 substrings
2011 int compare(size_t nStart
, size_t nLen
,
2012 const wxString
& str
, size_t nStart2
, size_t nLen2
) const;
2013 // substring comparison with first nCount characters of sz
2014 int compare(size_t nStart
, size_t nLen
,
2015 const char* sz
, size_t nCount
= npos
) const;
2016 int compare(size_t nStart
, size_t nLen
,
2017 const wchar_t* sz
, size_t nCount
= npos
) const;
2019 // insert another string
2020 wxString
& insert(size_t nPos
, const wxString
& str
)
2021 { insert(begin() + nPos
, str
.begin(), str
.end()); return *this; }
2022 // insert n chars of str starting at nStart (in str)
2023 wxString
& insert(size_t nPos
, const wxString
& str
, size_t nStart
, size_t n
)
2026 str
.PosLenToImpl(nStart
, n
, &from
, &len
);
2027 m_impl
.insert(PosToImpl(nPos
), str
.m_impl
, from
, len
);
2030 // insert first n (or all if n == npos) characters of sz
2031 wxString
& insert(size_t nPos
, const char *sz
)
2032 { m_impl
.insert(PosToImpl(nPos
), ImplStr(sz
)); return *this; }
2033 wxString
& insert(size_t nPos
, const wchar_t *sz
)
2034 { m_impl
.insert(PosToImpl(nPos
), ImplStr(sz
)); return *this; }
2035 wxString
& insert(size_t nPos
, const char *sz
, size_t n
)
2037 SubstrBufFromMB
str(ImplStr(sz
, n
));
2038 m_impl
.insert(PosToImpl(nPos
), str
.data
, str
.len
);
2041 wxString
& insert(size_t nPos
, const wchar_t *sz
, size_t n
)
2043 SubstrBufFromWC
str(ImplStr(sz
, n
));
2044 m_impl
.insert(PosToImpl(nPos
), str
.data
, str
.len
);
2047 // insert n copies of ch
2048 wxString
& insert(size_t nPos
, size_t n
, wxUniChar ch
)
2050 #if wxUSE_UNICODE_UTF8
2051 if ( !ch
.IsAscii() )
2052 m_impl
.insert(PosToImpl(nPos
), wxStringOperations::EncodeNChars(n
, ch
));
2055 m_impl
.insert(PosToImpl(nPos
), n
, (wxStringCharType
)ch
);
2058 iterator
insert(iterator it
, wxUniChar ch
)
2060 #if wxUSE_UNICODE_UTF8
2061 if ( !ch
.IsAscii() )
2063 size_t pos
= IterToImplPos(it
);
2064 m_impl
.insert(pos
, wxStringOperations::EncodeChar(ch
));
2065 return iterator(this, m_impl
.begin() + pos
);
2069 return iterator(this, m_impl
.insert(it
.impl(), (wxStringCharType
)ch
));
2071 void insert(iterator it
, const_iterator first
, const_iterator last
)
2072 { m_impl
.insert(it
.impl(), first
.impl(), last
.impl()); }
2073 #if WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2074 void insert(iterator it
, const char *first
, const char *last
)
2075 { insert(it
- begin(), first
, last
- first
); }
2076 void insert(iterator it
, const wchar_t *first
, const wchar_t *last
)
2077 { insert(it
- begin(), first
, last
- first
); }
2078 void insert(iterator it
, const wxCStrData
& first
, const wxCStrData
& last
)
2079 { insert(it
, CreateConstIterator(first
), CreateConstIterator(last
)); }
2080 #endif // WXWIN_COMPATIBILITY_STRING_PTR_AS_ITER
2082 void insert(iterator it
, size_type n
, wxUniChar ch
)
2084 #if wxUSE_UNICODE_UTF8
2085 if ( !ch
.IsAscii() )
2086 m_impl
.insert(IterToImplPos(it
), wxStringOperations::EncodeNChars(n
, ch
));
2089 m_impl
.insert(it
.impl(), n
, (wxStringCharType
)ch
);
2092 // delete characters from nStart to nStart + nLen
2093 wxString
& erase(size_type pos
= 0, size_type n
= npos
)
2096 PosLenToImpl(pos
, n
, &from
, &len
);
2097 m_impl
.erase(from
, len
);
2100 // delete characters from first up to last
2101 iterator
erase(iterator first
, iterator last
)
2102 { return iterator(this, m_impl
.erase(first
.impl(), last
.impl())); }
2103 iterator
erase(iterator first
)
2104 { return iterator(this, m_impl
.erase(first
.impl())); }
2106 #ifdef wxSTRING_BASE_HASNT_CLEAR
2107 void clear() { erase(); }
2109 void clear() { m_impl
.clear(); }
2112 // replaces the substring of length nLen starting at nStart
2113 wxString
& replace(size_t nStart
, size_t nLen
, const char* sz
)
2116 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2117 m_impl
.replace(from
, len
, ImplStr(sz
));
2120 wxString
& replace(size_t nStart
, size_t nLen
, const wchar_t* sz
)
2123 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2124 m_impl
.replace(from
, len
, ImplStr(sz
));
2127 // replaces the substring of length nLen starting at nStart
2128 wxString
& replace(size_t nStart
, size_t nLen
, const wxString
& str
)
2131 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2132 m_impl
.replace(from
, len
, str
.m_impl
);
2135 // replaces the substring with nCount copies of ch
2136 wxString
& replace(size_t nStart
, size_t nLen
, size_t nCount
, wxUniChar ch
)
2139 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2140 #if wxUSE_UNICODE_UTF8
2141 if ( !ch
.IsAscii() )
2142 m_impl
.replace(from
, len
, wxStringOperations::EncodeNChars(nCount
, ch
));
2145 m_impl
.replace(from
, len
, nCount
, (wxStringCharType
)ch
);
2148 // replaces a substring with another substring
2149 wxString
& replace(size_t nStart
, size_t nLen
,
2150 const wxString
& str
, size_t nStart2
, size_t nLen2
)
2153 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2156 str
.PosLenToImpl(nStart2
, nLen2
, &from2
, &len2
);
2158 m_impl
.replace(from
, len
, str
.m_impl
, from2
, len2
);
2161 // replaces the substring with first nCount chars of sz
2162 wxString
& replace(size_t nStart
, size_t nLen
,
2163 const char* sz
, size_t nCount
)
2166 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2168 SubstrBufFromMB
str(ImplStr(sz
, nCount
));
2170 m_impl
.replace(from
, len
, str
.data
, str
.len
);
2173 wxString
& replace(size_t nStart
, size_t nLen
,
2174 const wchar_t* sz
, size_t nCount
)
2177 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2179 SubstrBufFromWC
str(ImplStr(sz
, nCount
));
2181 m_impl
.replace(from
, len
, str
.data
, str
.len
);
2184 wxString
& replace(size_t nStart
, size_t nLen
,
2185 const wxString
& s
, size_t nCount
)
2188 PosLenToImpl(nStart
, nLen
, &from
, &len
);
2189 m_impl
.replace(from
, len
, s
.m_impl
.c_str(), s
.LenToImpl(nCount
));
2193 wxString
& replace(iterator first
, iterator last
, const char* s
)
2194 { m_impl
.replace(first
.impl(), last
.impl(), ImplStr(s
)); return *this; }
2195 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
)
2196 { m_impl
.replace(first
.impl(), last
.impl(), ImplStr(s
)); return *this; }
2197 wxString
& replace(iterator first
, iterator last
, const char* s
, size_type n
)
2199 SubstrBufFromMB
str(ImplStr(s
, n
));
2200 m_impl
.replace(first
.impl(), last
.impl(), str
.data
, str
.len
);
2203 wxString
& replace(iterator first
, iterator last
, const wchar_t* s
, size_type n
)
2205 SubstrBufFromWC
str(ImplStr(s
, n
));
2206 m_impl
.replace(first
.impl(), last
.impl(), str
.data
, str
.len
);
2209 wxString
& replace(iterator first
, iterator last
, const wxString
& s
)
2210 { m_impl
.replace(first
.impl(), last
.impl(), s
.m_impl
); return *this; }
2211 wxString
& replace(iterator first
, iterator last
, size_type n
, wxUniChar ch
)
2213 #if wxUSE_UNICODE_UTF8
2214 if ( !ch
.IsAscii() )
2215 m_impl
.replace(first
.impl(), last
.impl(),
2216 wxStringOperations::EncodeNChars(n
, ch
));
2219 m_impl
.replace(first
.impl(), last
.impl(), n
, (wxStringCharType
)ch
);
2222 wxString
& replace(iterator first
, iterator last
,
2223 const_iterator first1
, const_iterator last1
)
2225 m_impl
.replace(first
.impl(), last
.impl(), first1
.impl(), last1
.impl());
2228 wxString
& replace(iterator first
, iterator last
,
2229 const char *first1
, const char *last1
)
2230 { replace(first
, last
, first1
, last1
- first1
); return *this; }
2231 wxString
& replace(iterator first
, iterator last
,
2232 const wchar_t *first1
, const wchar_t *last1
)
2233 { replace(first
, last
, first1
, last1
- first1
); return *this; }
2236 void swap(wxString
& str
)
2237 { m_impl
.swap(str
.m_impl
); }
2240 size_t find(const wxString
& str
, size_t nStart
= 0) const
2241 { return PosFromImpl(m_impl
.find(str
.m_impl
, PosToImpl(nStart
))); }
2243 // find first n characters of sz
2244 size_t find(const char* sz
, size_t nStart
= 0, size_t n
= npos
) const
2246 SubstrBufFromMB
str(ImplStr(sz
, n
));
2247 return PosFromImpl(m_impl
.find(str
.data
, PosToImpl(nStart
), str
.len
));
2249 size_t find(const wchar_t* sz
, size_t nStart
= 0, size_t n
= npos
) const
2251 SubstrBufFromWC
str(ImplStr(sz
, n
));
2252 return PosFromImpl(m_impl
.find(str
.data
, PosToImpl(nStart
), str
.len
));
2254 size_t find(const wxCharBuffer
& s
, size_t nStart
= 0, size_t n
= npos
) const
2255 { return find(s
.data(), nStart
, n
); }
2256 size_t find(const wxWCharBuffer
& s
, size_t nStart
= 0, size_t n
= npos
) const
2257 { return find(s
.data(), nStart
, n
); }
2258 size_t find(const wxCStrData
& s
, size_t nStart
= 0, size_t n
= npos
) const
2259 { return find(s
.AsWChar(), nStart
, n
); }
2261 // find the first occurence of character ch after nStart
2262 size_t find(wxUniChar ch
, size_t nStart
= 0) const
2264 return PosFromImpl(m_impl
.find(wxStringOperations::EncodeChar(ch
),
2265 PosToImpl(nStart
)));
2267 size_t find(wxUniCharRef ch
, size_t nStart
= 0) const
2268 { return find(wxUniChar(ch
), nStart
); }
2269 size_t find(char ch
, size_t nStart
= 0) const
2270 { return find(wxUniChar(ch
), nStart
); }
2271 size_t find(unsigned char ch
, size_t nStart
= 0) const
2272 { return find(wxUniChar(ch
), nStart
); }
2273 size_t find(wchar_t ch
, size_t nStart
= 0) const
2274 { return find(wxUniChar(ch
), nStart
); }
2276 // rfind() family is exactly like find() but works right to left
2278 // as find, but from the end
2279 size_t rfind(const wxString
& str
, size_t nStart
= npos
) const
2280 { return PosFromImpl(m_impl
.rfind(str
.m_impl
, PosToImpl(nStart
))); }
2282 // as find, but from the end
2283 size_t rfind(const char* sz
, size_t nStart
= npos
, size_t n
= npos
) const
2285 SubstrBufFromMB
str(ImplStr(sz
, n
));
2286 return PosFromImpl(m_impl
.rfind(str
.data
, PosToImpl(nStart
), str
.len
));
2288 size_t rfind(const wchar_t* sz
, size_t nStart
= npos
, size_t n
= npos
) const
2290 SubstrBufFromWC
str(ImplStr(sz
, n
));
2291 return PosFromImpl(m_impl
.rfind(str
.data
, PosToImpl(nStart
), str
.len
));
2293 size_t rfind(const wxCharBuffer
& s
, size_t nStart
= npos
, size_t n
= npos
) const
2294 { return rfind(s
.data(), nStart
, n
); }
2295 size_t rfind(const wxWCharBuffer
& s
, size_t nStart
= npos
, size_t n
= npos
) const
2296 { return rfind(s
.data(), nStart
, n
); }
2297 size_t rfind(const wxCStrData
& s
, size_t nStart
= npos
, size_t n
= npos
) const
2298 { return rfind(s
.AsWChar(), nStart
, n
); }
2299 // as find, but from the end
2300 size_t rfind(wxUniChar ch
, size_t nStart
= npos
) const
2302 return PosFromImpl(m_impl
.rfind(wxStringOperations::EncodeChar(ch
),
2303 PosToImpl(nStart
)));
2305 size_t rfind(wxUniCharRef ch
, size_t nStart
= npos
) const
2306 { return rfind(wxUniChar(ch
), nStart
); }
2307 size_t rfind(char ch
, size_t nStart
= npos
) const
2308 { return rfind(wxUniChar(ch
), nStart
); }
2309 size_t rfind(unsigned char ch
, size_t nStart
= npos
) const
2310 { return rfind(wxUniChar(ch
), nStart
); }
2311 size_t rfind(wchar_t ch
, size_t nStart
= npos
) const
2312 { return rfind(wxUniChar(ch
), nStart
); }
2314 // find first/last occurence of any character (not) in the set:
2315 #if wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2316 // FIXME-UTF8: this is not entirely correct, because it doesn't work if
2317 // sizeof(wchar_t)==2 and surrogates are present in the string;
2318 // should we care? Probably not.
2319 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
2320 { return m_impl
.find_first_of(str
.m_impl
, nStart
); }
2321 size_t find_first_of(const char* sz
, size_t nStart
= 0) const
2322 { return m_impl
.find_first_of(ImplStr(sz
), nStart
); }
2323 size_t find_first_of(const wchar_t* sz
, size_t nStart
= 0) const
2324 { return m_impl
.find_first_of(ImplStr(sz
), nStart
); }
2325 size_t find_first_of(const char* sz
, size_t nStart
, size_t n
) const
2326 { return m_impl
.find_first_of(ImplStr(sz
), nStart
, n
); }
2327 size_t find_first_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
2328 { return m_impl
.find_first_of(ImplStr(sz
), nStart
, n
); }
2329 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const
2330 { return m_impl
.find_first_of((wxChar
)c
, nStart
); }
2332 size_t find_last_of(const wxString
& str
, size_t nStart
= npos
) const
2333 { return m_impl
.find_last_of(str
.m_impl
, nStart
); }
2334 size_t find_last_of(const char* sz
, size_t nStart
= npos
) const
2335 { return m_impl
.find_last_of(ImplStr(sz
), nStart
); }
2336 size_t find_last_of(const wchar_t* sz
, size_t nStart
= npos
) const
2337 { return m_impl
.find_last_of(ImplStr(sz
), nStart
); }
2338 size_t find_last_of(const char* sz
, size_t nStart
, size_t n
) const
2339 { return m_impl
.find_last_of(ImplStr(sz
), nStart
, n
); }
2340 size_t find_last_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
2341 { return m_impl
.find_last_of(ImplStr(sz
), nStart
, n
); }
2342 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const
2343 { return m_impl
.find_last_of((wxChar
)c
, nStart
); }
2345 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
2346 { return m_impl
.find_first_not_of(str
.m_impl
, nStart
); }
2347 size_t find_first_not_of(const char* sz
, size_t nStart
= 0) const
2348 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
); }
2349 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
= 0) const
2350 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
); }
2351 size_t find_first_not_of(const char* sz
, size_t nStart
, size_t n
) const
2352 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
, n
); }
2353 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
2354 { return m_impl
.find_first_not_of(ImplStr(sz
), nStart
, n
); }
2355 size_t find_first_not_of(wxUniChar c
, size_t nStart
= 0) const
2356 { return m_impl
.find_first_not_of((wxChar
)c
, nStart
); }
2358 size_t find_last_not_of(const wxString
& str
, size_t nStart
= npos
) const
2359 { return m_impl
.find_last_not_of(str
.m_impl
, nStart
); }
2360 size_t find_last_not_of(const char* sz
, size_t nStart
= npos
) const
2361 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
); }
2362 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
= npos
) const
2363 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
); }
2364 size_t find_last_not_of(const char* sz
, size_t nStart
, size_t n
) const
2365 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
, n
); }
2366 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const
2367 { return m_impl
.find_last_not_of(ImplStr(sz
), nStart
, n
); }
2368 size_t find_last_not_of(wxUniChar c
, size_t nStart
= npos
) const
2369 { return m_impl
.find_last_not_of((wxChar
)c
, nStart
); }
2371 // we can't use std::string implementation in UTF-8 build, because the
2372 // character sets would be interpreted wrongly:
2374 // as strpbrk() but starts at nStart, returns npos if not found
2375 size_t find_first_of(const wxString
& str
, size_t nStart
= 0) const
2376 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2377 { return find_first_of(str
.wc_str(), nStart
); }
2379 { return find_first_of(str
.mb_str(), nStart
); }
2382 size_t find_first_of(const char* sz
, size_t nStart
= 0) const;
2383 size_t find_first_of(const wchar_t* sz
, size_t nStart
= 0) const;
2384 size_t find_first_of(const char* sz
, size_t nStart
, size_t n
) const;
2385 size_t find_first_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2386 // same as find(char, size_t)
2387 size_t find_first_of(wxUniChar c
, size_t nStart
= 0) const
2388 { return find(c
, nStart
); }
2389 // find the last (starting from nStart) char from str in this string
2390 size_t find_last_of (const wxString
& str
, size_t nStart
= npos
) const
2391 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2392 { return find_last_of(str
.wc_str(), nStart
); }
2394 { return find_last_of(str
.mb_str(), nStart
); }
2397 size_t find_last_of (const char* sz
, size_t nStart
= npos
) const;
2398 size_t find_last_of (const wchar_t* sz
, size_t nStart
= npos
) const;
2399 size_t find_last_of(const char* sz
, size_t nStart
, size_t n
) const;
2400 size_t find_last_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2402 size_t find_last_of(wxUniChar c
, size_t nStart
= npos
) const
2403 { return rfind(c
, nStart
); }
2405 // find first/last occurence of any character not in the set
2407 // as strspn() (starting from nStart), returns npos on failure
2408 size_t find_first_not_of(const wxString
& str
, size_t nStart
= 0) const
2409 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2410 { return find_first_not_of(str
.wc_str(), nStart
); }
2412 { return find_first_not_of(str
.mb_str(), nStart
); }
2415 size_t find_first_not_of(const char* sz
, size_t nStart
= 0) const;
2416 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
= 0) const;
2417 size_t find_first_not_of(const char* sz
, size_t nStart
, size_t n
) const;
2418 size_t find_first_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2420 size_t find_first_not_of(wxUniChar ch
, size_t nStart
= 0) const;
2422 size_t find_last_not_of(const wxString
& str
, size_t nStart
= npos
) const
2423 #if wxUSE_UNICODE // FIXME-UTF8: temporary
2424 { return find_last_not_of(str
.wc_str(), nStart
); }
2426 { return find_last_not_of(str
.mb_str(), nStart
); }
2429 size_t find_last_not_of(const char* sz
, size_t nStart
= npos
) const;
2430 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
= npos
) const;
2431 size_t find_last_not_of(const char* sz
, size_t nStart
, size_t n
) const;
2432 size_t find_last_not_of(const wchar_t* sz
, size_t nStart
, size_t n
) const;
2434 size_t find_last_not_of(wxUniChar ch
, size_t nStart
= npos
) const;
2435 #endif // wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8 or not
2437 // provide char/wchar_t/wxUniCharRef overloads for char-finding functions
2438 // above to resolve ambiguities:
2439 size_t find_first_of(wxUniCharRef ch
, size_t nStart
= 0) const
2440 { return find_first_of(wxUniChar(ch
), nStart
); }
2441 size_t find_first_of(char ch
, size_t nStart
= 0) const
2442 { return find_first_of(wxUniChar(ch
), nStart
); }
2443 size_t find_first_of(unsigned char ch
, size_t nStart
= 0) const
2444 { return find_first_of(wxUniChar(ch
), nStart
); }
2445 size_t find_first_of(wchar_t ch
, size_t nStart
= 0) const
2446 { return find_first_of(wxUniChar(ch
), nStart
); }
2447 size_t find_last_of(wxUniCharRef ch
, size_t nStart
= npos
) const
2448 { return find_last_of(wxUniChar(ch
), nStart
); }
2449 size_t find_last_of(char ch
, size_t nStart
= npos
) const
2450 { return find_last_of(wxUniChar(ch
), nStart
); }
2451 size_t find_last_of(unsigned char ch
, size_t nStart
= npos
) const
2452 { return find_last_of(wxUniChar(ch
), nStart
); }
2453 size_t find_last_of(wchar_t ch
, size_t nStart
= npos
) const
2454 { return find_last_of(wxUniChar(ch
), nStart
); }
2455 size_t find_first_not_of(wxUniCharRef ch
, size_t nStart
= 0) const
2456 { return find_first_not_of(wxUniChar(ch
), nStart
); }
2457 size_t find_first_not_of(char ch
, size_t nStart
= 0) const
2458 { return find_first_not_of(wxUniChar(ch
), nStart
); }
2459 size_t find_first_not_of(unsigned char ch
, size_t nStart
= 0) const
2460 { return find_first_not_of(wxUniChar(ch
), nStart
); }
2461 size_t find_first_not_of(wchar_t ch
, size_t nStart
= 0) const
2462 { return find_first_not_of(wxUniChar(ch
), nStart
); }
2463 size_t find_last_not_of(wxUniCharRef ch
, size_t nStart
= npos
) const
2464 { return find_last_not_of(wxUniChar(ch
), nStart
); }
2465 size_t find_last_not_of(char ch
, size_t nStart
= npos
) const
2466 { return find_last_not_of(wxUniChar(ch
), nStart
); }
2467 size_t find_last_not_of(unsigned char ch
, size_t nStart
= npos
) const
2468 { return find_last_not_of(wxUniChar(ch
), nStart
); }
2469 size_t find_last_not_of(wchar_t ch
, size_t nStart
= npos
) const
2470 { return find_last_not_of(wxUniChar(ch
), nStart
); }
2472 // and additional overloads for the versions taking strings:
2473 size_t find_first_of(const wxCStrData
& sz
, size_t nStart
= 0) const
2474 { return find_first_of(sz
.AsString(), nStart
); }
2475 size_t find_first_of(const wxCharBuffer
& sz
, size_t nStart
= 0) const
2476 { return find_first_of(sz
.data(), nStart
); }
2477 size_t find_first_of(const wxWCharBuffer
& sz
, size_t nStart
= 0) const
2478 { return find_first_of(sz
.data(), nStart
); }
2479 size_t find_first_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
2480 { return find_first_of(sz
.AsWChar(), nStart
, n
); }
2481 size_t find_first_of(const wxCharBuffer
& sz
, size_t nStart
, size_t n
) const
2482 { return find_first_of(sz
.data(), nStart
, n
); }
2483 size_t find_first_of(const wxWCharBuffer
& sz
, size_t nStart
, size_t n
) const
2484 { return find_first_of(sz
.data(), nStart
, n
); }
2486 size_t find_last_of(const wxCStrData
& sz
, size_t nStart
= 0) const
2487 { return find_last_of(sz
.AsString(), nStart
); }
2488 size_t find_last_of(const wxCharBuffer
& sz
, size_t nStart
= 0) const
2489 { return find_last_of(sz
.data(), nStart
); }
2490 size_t find_last_of(const wxWCharBuffer
& sz
, size_t nStart
= 0) const
2491 { return find_last_of(sz
.data(), nStart
); }
2492 size_t find_last_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
2493 { return find_last_of(sz
.AsWChar(), nStart
, n
); }
2494 size_t find_last_of(const wxCharBuffer
& sz
, size_t nStart
, size_t n
) const
2495 { return find_last_of(sz
.data(), nStart
, n
); }
2496 size_t find_last_of(const wxWCharBuffer
& sz
, size_t nStart
, size_t n
) const
2497 { return find_last_of(sz
.data(), nStart
, n
); }
2499 size_t find_first_not_of(const wxCStrData
& sz
, size_t nStart
= 0) const
2500 { return find_first_not_of(sz
.AsString(), nStart
); }
2501 size_t find_first_not_of(const wxCharBuffer
& sz
, size_t nStart
= 0) const
2502 { return find_first_not_of(sz
.data(), nStart
); }
2503 size_t find_first_not_of(const wxWCharBuffer
& sz
, size_t nStart
= 0) const
2504 { return find_first_not_of(sz
.data(), nStart
); }
2505 size_t find_first_not_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
2506 { return find_first_not_of(sz
.AsWChar(), nStart
, n
); }
2507 size_t find_first_not_of(const wxCharBuffer
& sz
, size_t nStart
, size_t n
) const
2508 { return find_first_not_of(sz
.data(), nStart
, n
); }
2509 size_t find_first_not_of(const wxWCharBuffer
& sz
, size_t nStart
, size_t n
) const
2510 { return find_first_not_of(sz
.data(), nStart
, n
); }
2512 size_t find_last_not_of(const wxCStrData
& sz
, size_t nStart
= 0) const
2513 { return find_last_not_of(sz
.AsString(), nStart
); }
2514 size_t find_last_not_of(const wxCharBuffer
& sz
, size_t nStart
= 0) const
2515 { return find_last_not_of(sz
.data(), nStart
); }
2516 size_t find_last_not_of(const wxWCharBuffer
& sz
, size_t nStart
= 0) const
2517 { return find_last_not_of(sz
.data(), nStart
); }
2518 size_t find_last_not_of(const wxCStrData
& sz
, size_t nStart
, size_t n
) const
2519 { return find_last_not_of(sz
.AsWChar(), nStart
, n
); }
2520 size_t find_last_not_of(const wxCharBuffer
& sz
, size_t nStart
, size_t n
) const
2521 { return find_last_not_of(sz
.data(), nStart
, n
); }
2522 size_t find_last_not_of(const wxWCharBuffer
& sz
, size_t nStart
, size_t n
) const
2523 { return find_last_not_of(sz
.data(), nStart
, n
); }
2526 wxString
& operator+=(const wxString
& s
)
2527 { m_impl
+= s
.m_impl
; return *this; }
2528 // string += C string
2529 wxString
& operator+=(const char *psz
)
2530 { m_impl
+= ImplStr(psz
); return *this; }
2531 wxString
& operator+=(const wchar_t *pwz
)
2532 { m_impl
+= ImplStr(pwz
); return *this; }
2533 wxString
& operator+=(const wxCStrData
& s
)
2534 { m_impl
+= s
.AsString().m_impl
; return *this; }
2535 wxString
& operator+=(const wxCharBuffer
& s
)
2536 { return operator+=(s
.data()); }
2537 wxString
& operator+=(const wxWCharBuffer
& s
)
2538 { return operator+=(s
.data()); }
2540 wxString
& operator+=(wxUniChar ch
)
2541 { m_impl
+= wxStringOperations::EncodeChar(ch
); return *this; }
2542 wxString
& operator+=(wxUniCharRef ch
) { return *this += wxUniChar(ch
); }
2543 wxString
& operator+=(int ch
) { return *this += wxUniChar(ch
); }
2544 wxString
& operator+=(char ch
) { return *this += wxUniChar(ch
); }
2545 wxString
& operator+=(unsigned char ch
) { return *this += wxUniChar(ch
); }
2546 wxString
& operator+=(wchar_t ch
) { return *this += wxUniChar(ch
); }
2549 #if !wxUSE_STL_BASED_WXSTRING
2550 // helpers for wxStringBuffer and wxStringBufferLength
2551 wxStringCharType
*DoGetWriteBuf(size_t nLen
)
2552 { return m_impl
.DoGetWriteBuf(nLen
); }
2553 void DoUngetWriteBuf()
2554 { m_impl
.DoUngetWriteBuf(); }
2555 void DoUngetWriteBuf(size_t nLen
)
2556 { m_impl
.DoUngetWriteBuf(nLen
); }
2557 #endif // !wxUSE_STL_BASED_WXSTRING
2559 #ifndef wxNEEDS_WXSTRING_PRINTF_MIXIN
2560 #if !wxUSE_UTF8_LOCALE_ONLY
2561 int DoPrintfWchar(const wxChar
*format
, ...);
2562 static wxString
DoFormatWchar(const wxChar
*format
, ...);
2564 #if wxUSE_UNICODE_UTF8
2565 int DoPrintfUtf8(const char *format
, ...);
2566 static wxString
DoFormatUtf8(const char *format
, ...);
2570 #if !wxUSE_STL_BASED_WXSTRING
2571 // check string's data validity
2572 bool IsValid() const { return m_impl
.GetStringData()->IsValid(); }
2576 wxStringImpl m_impl
;
2579 // "struct 'ConvertedBuffer<T>' needs to have dll-interface to be used by
2580 // clients of class 'wxString'" - this is private, we don't care
2581 #pragma warning (disable:4251)
2584 // buffers for compatibility conversion from (char*)c_str() and
2585 // (wchar_t*)c_str():
2586 // FIXME-UTF8: bechmark various approaches to keeping compatibility buffers
2587 template<typename T
>
2588 struct ConvertedBuffer
2590 ConvertedBuffer() : m_buf(NULL
) {}
2594 operator T
*() const { return m_buf
; }
2596 ConvertedBuffer
& operator=(T
*str
)
2605 #if wxUSE_UNICODE && !wxUSE_UTF8_LOCALE_ONLY
2606 ConvertedBuffer
<char> m_convertedToChar
;
2608 #if !wxUSE_UNICODE_WCHAR
2609 ConvertedBuffer
<wchar_t> m_convertedToWChar
;
2613 #pragma warning (default:4251)
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 wxImplStringBuffer
;
2639 friend class wxImplStringBufferLength
;
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 #if wxUSE_STL_BASED_WXSTRING
2693 // return an empty wxString (not very useful with wxUSE_STL == 1)
2694 inline const wxString
wxGetEmptyString() { return wxString(); }
2695 #else // !wxUSE_STL_BASED_WXSTRING
2696 // return an empty wxString (more efficient than wxString() here)
2697 inline const wxString
& wxGetEmptyString()
2699 return *(wxString
*)&wxEmptyString
;
2701 #endif // wxUSE_STL_BASED_WXSTRING/!wxUSE_STL_BASED_WXSTRING
2703 // ----------------------------------------------------------------------------
2704 // wxStringBuffer: a tiny class allowing to get a writable pointer into string
2705 // ----------------------------------------------------------------------------
2707 #if !wxUSE_STL_BASED_WXSTRING
2708 // string buffer for direct access to string data in their native
2710 class wxImplStringBuffer
2713 typedef wxStringCharType CharType
;
2715 wxImplStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
2716 : m_str(str
), m_buf(NULL
)
2717 { m_buf
= m_str
.DoGetWriteBuf(lenWanted
); }
2719 ~wxImplStringBuffer() { m_str
.DoUngetWriteBuf(); }
2721 operator wxStringCharType
*() const { return m_buf
; }
2725 wxStringCharType
*m_buf
;
2727 DECLARE_NO_COPY_CLASS(wxImplStringBuffer
)
2730 class wxImplStringBufferLength
2733 typedef wxStringCharType CharType
;
2735 wxImplStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
2736 : m_str(str
), m_buf(NULL
), m_len(0), m_lenSet(false)
2738 m_buf
= m_str
.DoGetWriteBuf(lenWanted
);
2739 wxASSERT(m_buf
!= NULL
);
2742 ~wxImplStringBufferLength()
2745 m_str
.DoUngetWriteBuf(m_len
);
2748 operator wxStringCharType
*() const { return m_buf
; }
2749 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
2753 wxStringCharType
*m_buf
;
2757 DECLARE_NO_COPY_CLASS(wxImplStringBufferLength
)
2760 #endif // !wxUSE_STL_BASED_WXSTRING
2762 template<typename T
>
2763 class wxStringTypeBufferBase
2768 wxStringTypeBufferBase(wxString
& str
, size_t lenWanted
= 1024)
2769 : m_str(str
), m_buf(lenWanted
)
2773 operator CharType
*() { return m_buf
.data(); }
2777 wxCharTypeBuffer
<CharType
> m_buf
;
2780 template<typename T
>
2781 class wxStringTypeBufferLengthBase
2786 wxStringTypeBufferLengthBase(wxString
& str
, size_t lenWanted
= 1024)
2787 : m_str(str
), m_buf(lenWanted
), m_len(0), m_lenSet(false)
2790 ~wxStringTypeBufferLengthBase()
2793 m_str
.assign(m_buf
.data(), m_len
);
2796 operator CharType
*() { return m_buf
.data(); }
2797 void SetLength(size_t length
) { m_len
= length
; m_lenSet
= true; }
2801 wxCharTypeBuffer
<CharType
> m_buf
;
2806 template<typename T
>
2807 class wxStringTypeBuffer
: public wxStringTypeBufferBase
<T
>
2810 wxStringTypeBuffer(wxString
& str
, size_t lenWanted
= 1024)
2811 : wxStringTypeBufferBase
<T
>(str
, lenWanted
) {}
2812 ~wxStringTypeBuffer()
2814 this->m_str
.assign(this->m_buf
.data());
2817 DECLARE_NO_COPY_CLASS(wxStringTypeBuffer
)
2820 template<typename T
>
2821 class wxStringTypeBufferLength
: public wxStringTypeBufferLengthBase
<T
>
2824 wxStringTypeBufferLength(wxString
& str
, size_t lenWanted
= 1024)
2825 : wxStringTypeBufferLengthBase
<T
>(str
, lenWanted
) {}
2827 ~wxStringTypeBufferLength()
2829 wxASSERT(this->m_lenSet
);
2830 this->m_str
.assign(this->m_buf
.data(), this->m_len
);
2833 DECLARE_NO_COPY_CLASS(wxStringTypeBufferLength
)
2836 #if wxUSE_STL_BASED_WXSTRING
2837 class wxImplStringBuffer
: public wxStringTypeBufferBase
<wxStringCharType
>
2840 wxImplStringBuffer(wxString
& str
, size_t lenWanted
= 1024)
2841 : wxStringTypeBufferBase
<wxStringCharType
>(str
, lenWanted
) {}
2842 ~wxImplStringBuffer()
2843 { m_str
.m_impl
.assign(m_buf
.data()); }
2845 DECLARE_NO_COPY_CLASS(wxImplStringBuffer
)
2848 class wxImplStringBufferLength
: public wxStringTypeBufferLengthBase
<wxStringCharType
>
2851 wxImplStringBufferLength(wxString
& str
, size_t lenWanted
= 1024)
2852 : wxStringTypeBufferLengthBase
<wxStringCharType
>(str
, lenWanted
) {}
2854 ~wxImplStringBufferLength()
2857 m_str
.m_impl
.assign(m_buf
.data(), m_len
);
2860 DECLARE_NO_COPY_CLASS(wxImplStringBufferLength
)
2862 #endif // wxUSE_STL_BASED_WXSTRING
2865 #if wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
2866 typedef wxStringTypeBuffer
<wxChar
> wxStringBuffer
;
2867 typedef wxStringTypeBufferLength
<wxChar
> wxStringBufferLength
;
2868 #else // if !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2869 typedef wxImplStringBuffer wxStringBuffer
;
2870 typedef wxImplStringBufferLength wxStringBufferLength
;
2871 #endif // !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
2873 // ---------------------------------------------------------------------------
2874 // wxString comparison functions: operator versions are always case sensitive
2875 // ---------------------------------------------------------------------------
2877 #define wxCMP_WXCHAR_STRING(p, s, op) 0 op s.Cmp(p)
2879 wxDEFINE_ALL_COMPARISONS(const wxChar
*, const wxString
&, wxCMP_WXCHAR_STRING
)
2881 #undef wxCMP_WXCHAR_STRING
2883 // note that there is an optimization in operator==() and !=(): we (quickly)
2884 // checks the strings length first, before comparing their data
2885 inline bool operator==(const wxString
& s1
, const wxString
& s2
)
2886 { return (s1
.Len() == s2
.Len()) && (s1
.Cmp(s2
) == 0); }
2887 inline bool operator!=(const wxString
& s1
, const wxString
& s2
)
2888 { return (s1
.Len() != s2
.Len()) || (s1
.Cmp(s2
) != 0); }
2889 inline bool operator< (const wxString
& s1
, const wxString
& s2
)
2890 { return s1
.Cmp(s2
) < 0; }
2891 inline bool operator> (const wxString
& s1
, const wxString
& s2
)
2892 { return s1
.Cmp(s2
) > 0; }
2893 inline bool operator<=(const wxString
& s1
, const wxString
& s2
)
2894 { return s1
.Cmp(s2
) <= 0; }
2895 inline bool operator>=(const wxString
& s1
, const wxString
& s2
)
2896 { return s1
.Cmp(s2
) >= 0; }
2898 inline bool operator==(const wxString
& s1
, const wxCStrData
& s2
)
2899 { return s1
== s2
.AsString(); }
2900 inline bool operator==(const wxCStrData
& s1
, const wxString
& s2
)
2901 { return s1
.AsString() == s2
; }
2902 inline bool operator!=(const wxString
& s1
, const wxCStrData
& s2
)
2903 { return s1
!= s2
.AsString(); }
2904 inline bool operator!=(const wxCStrData
& s1
, const wxString
& s2
)
2905 { return s1
.AsString() != s2
; }
2907 inline bool operator==(const wxString
& s1
, const wxWCharBuffer
& s2
)
2908 { return (s1
.Cmp((const wchar_t *)s2
) == 0); }
2909 inline bool operator==(const wxWCharBuffer
& s1
, const wxString
& s2
)
2910 { return (s2
.Cmp((const wchar_t *)s1
) == 0); }
2911 inline bool operator!=(const wxString
& s1
, const wxWCharBuffer
& s2
)
2912 { return (s1
.Cmp((const wchar_t *)s2
) != 0); }
2913 inline bool operator!=(const wxWCharBuffer
& s1
, const wxString
& s2
)
2914 { return (s2
.Cmp((const wchar_t *)s1
) != 0); }
2916 inline bool operator==(const wxString
& s1
, const wxCharBuffer
& s2
)
2917 { return (s1
.Cmp((const char *)s2
) == 0); }
2918 inline bool operator==(const wxCharBuffer
& s1
, const wxString
& s2
)
2919 { return (s2
.Cmp((const char *)s1
) == 0); }
2920 inline bool operator!=(const wxString
& s1
, const wxCharBuffer
& s2
)
2921 { return (s1
.Cmp((const char *)s2
) != 0); }
2922 inline bool operator!=(const wxCharBuffer
& s1
, const wxString
& s2
)
2923 { return (s2
.Cmp((const char *)s1
) != 0); }
2925 inline wxString
operator+(const wxString
& string
, const wxWCharBuffer
& buf
)
2926 { return string
+ (const wchar_t *)buf
; }
2927 inline wxString
operator+(const wxWCharBuffer
& buf
, const wxString
& string
)
2928 { return (const wchar_t *)buf
+ string
; }
2930 inline wxString
operator+(const wxString
& string
, const wxCharBuffer
& buf
)
2931 { return string
+ (const char *)buf
; }
2932 inline wxString
operator+(const wxCharBuffer
& buf
, const wxString
& string
)
2933 { return (const char *)buf
+ string
; }
2935 // comparison with char
2936 inline bool operator==(const wxUniChar
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2937 inline bool operator==(const wxUniCharRef
& c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2938 inline bool operator==(char c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2939 inline bool operator==(wchar_t c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2940 inline bool operator==(int c
, const wxString
& s
) { return s
.IsSameAs(c
); }
2941 inline bool operator==(const wxString
& s
, const wxUniChar
& c
) { return s
.IsSameAs(c
); }
2942 inline bool operator==(const wxString
& s
, const wxUniCharRef
& c
) { return s
.IsSameAs(c
); }
2943 inline bool operator==(const wxString
& s
, char c
) { return s
.IsSameAs(c
); }
2944 inline bool operator==(const wxString
& s
, wchar_t c
) { return s
.IsSameAs(c
); }
2945 inline bool operator!=(const wxUniChar
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2946 inline bool operator!=(const wxUniCharRef
& c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2947 inline bool operator!=(char c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2948 inline bool operator!=(wchar_t c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2949 inline bool operator!=(int c
, const wxString
& s
) { return !s
.IsSameAs(c
); }
2950 inline bool operator!=(const wxString
& s
, const wxUniChar
& c
) { return !s
.IsSameAs(c
); }
2951 inline bool operator!=(const wxString
& s
, const wxUniCharRef
& c
) { return !s
.IsSameAs(c
); }
2952 inline bool operator!=(const wxString
& s
, char c
) { return !s
.IsSameAs(c
); }
2953 inline bool operator!=(const wxString
& s
, wchar_t c
) { return !s
.IsSameAs(c
); }
2955 // comparison with C string in Unicode build
2958 #define wxCMP_CHAR_STRING(p, s, op) wxString(p) op s
2960 wxDEFINE_ALL_COMPARISONS(const char *, const wxString
&, wxCMP_CHAR_STRING
)
2962 #undef wxCMP_CHAR_STRING
2964 #endif // wxUSE_UNICODE
2966 // we also need to provide the operators for comparison with wxCStrData to
2967 // resolve ambiguity between operator(const wxChar *,const wxString &) and
2968 // operator(const wxChar *, const wxChar *) for "p == s.c_str()"
2970 // notice that these are (shallow) pointer comparisons, not (deep) string ones
2971 #define wxCMP_CHAR_CSTRDATA(p, s, op) p op s.AsChar()
2972 #define wxCMP_WCHAR_CSTRDATA(p, s, op) p op s.AsWChar()
2974 wxDEFINE_ALL_COMPARISONS(const wchar_t *, const wxCStrData
&, wxCMP_WCHAR_CSTRDATA
)
2975 wxDEFINE_ALL_COMPARISONS(const char *, const wxCStrData
&, wxCMP_CHAR_CSTRDATA
)
2977 #undef wxCMP_CHAR_CSTRDATA
2978 #undef wxCMP_WCHAR_CSTRDATA
2980 // ---------------------------------------------------------------------------
2981 // Implementation only from here until the end of file
2982 // ---------------------------------------------------------------------------
2984 #if wxUSE_STD_IOSTREAM
2986 #include "wx/iosfwrap.h"
2988 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxString
&);
2989 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxCStrData
&);
2990 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxCharBuffer
&);
2991 #ifndef __BORLANDC__
2992 WXDLLIMPEXP_BASE wxSTD ostream
& operator<<(wxSTD ostream
&, const wxWCharBuffer
&);
2995 #endif // wxSTD_STRING_COMPATIBILITY
2997 // ---------------------------------------------------------------------------
2998 // wxCStrData implementation
2999 // ---------------------------------------------------------------------------
3001 inline wxCStrData::wxCStrData(char *buf
)
3002 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
3003 inline wxCStrData::wxCStrData(wchar_t *buf
)
3004 : m_str(new wxString(buf
)), m_offset(0), m_owned(true) {}
3006 inline wxCStrData::wxCStrData(const wxCStrData
& data
)
3007 : m_str(data
.m_owned
? new wxString(*data
.m_str
) : data
.m_str
),
3008 m_offset(data
.m_offset
),
3009 m_owned(data
.m_owned
)
3013 inline wxCStrData::~wxCStrData()
3016 delete wx_const_cast(wxString
*, m_str
); // cast to silence warnings
3019 // simple cases for AsChar() and AsWChar(), the complicated ones are
3021 #if wxUSE_UNICODE_WCHAR
3022 inline const wchar_t* wxCStrData::AsWChar() const
3024 return m_str
->wx_str() + m_offset
;
3026 #endif // wxUSE_UNICODE_WCHAR
3029 inline const char* wxCStrData::AsChar() const
3031 return m_str
->wx_str() + m_offset
;
3033 #endif // !wxUSE_UNICODE
3035 #if wxUSE_UTF8_LOCALE_ONLY
3036 inline const char* wxCStrData::AsChar() const
3038 return wxStringOperations::AddToIter(m_str
->wx_str(), m_offset
);
3040 #endif // wxUSE_UTF8_LOCALE_ONLY
3042 inline const wxCharBuffer
wxCStrData::AsCharBuf() const
3045 return wxCharBuffer::CreateNonOwned(AsChar());
3047 return AsString().mb_str();
3051 inline const wxWCharBuffer
wxCStrData::AsWCharBuf() const
3053 #if wxUSE_UNICODE_WCHAR
3054 return wxWCharBuffer::CreateNonOwned(AsWChar());
3056 return AsString().wc_str();
3060 inline wxString
wxCStrData::AsString() const
3062 if ( m_offset
== 0 )
3065 return m_str
->Mid(m_offset
);
3068 inline const wxStringCharType
*wxCStrData::AsInternal() const
3070 #if wxUSE_UNICODE_UTF8
3071 return wxStringOperations::AddToIter(m_str
->wx_str(), m_offset
);
3073 return m_str
->wx_str() + m_offset
;
3077 inline wxUniChar
wxCStrData::operator*() const
3079 if ( m_str
->empty() )
3080 return wxUniChar(_T('\0'));
3082 return (*m_str
)[m_offset
];
3085 inline wxUniChar
wxCStrData::operator[](size_t n
) const
3087 // NB: we intentionally use operator[] and not at() here because the former
3088 // works for the terminating NUL while the latter does not
3089 return (*m_str
)[m_offset
+ n
];
3092 // ----------------------------------------------------------------------------
3093 // more wxCStrData operators
3094 // ----------------------------------------------------------------------------
3096 // we need to define those to allow "size_t pos = p - s.c_str()" where p is
3097 // some pointer into the string
3098 inline size_t operator-(const char *p
, const wxCStrData
& cs
)
3100 return p
- cs
.AsChar();
3103 inline size_t operator-(const wchar_t *p
, const wxCStrData
& cs
)
3105 return p
- cs
.AsWChar();
3108 // ----------------------------------------------------------------------------
3109 // implementation of wx[W]CharBuffer inline methods using wxCStrData
3110 // ----------------------------------------------------------------------------
3112 // FIXME-UTF8: move this to buffer.h
3113 inline wxCharBuffer::wxCharBuffer(const wxCStrData
& cstr
)
3114 : wxCharTypeBufferBase(cstr
.AsCharBuf())
3118 inline wxWCharBuffer::wxWCharBuffer(const wxCStrData
& cstr
)
3119 : wxCharTypeBufferBase(cstr
.AsWCharBuf())
3123 #if wxUSE_UNICODE_UTF8
3124 // ----------------------------------------------------------------------------
3125 // implementation of wxStringIteratorNode inline methods
3126 // ----------------------------------------------------------------------------
3128 void wxStringIteratorNode::DoSet(const wxString
*str
,
3129 wxStringImpl::const_iterator
*citer
,
3130 wxStringImpl::iterator
*iter
)
3138 m_next
= str
->m_iterators
.ptr
;
3139 wx_const_cast(wxString
*, m_str
)->m_iterators
.ptr
= this;
3141 m_next
->m_prev
= this;
3149 void wxStringIteratorNode::clear()
3152 m_next
->m_prev
= m_prev
;
3154 m_prev
->m_next
= m_next
;
3155 else if ( m_str
) // first in the list
3156 wx_const_cast(wxString
*, m_str
)->m_iterators
.ptr
= m_next
;
3158 m_next
= m_prev
= NULL
;
3163 #endif // wxUSE_UNICODE_UTF8
3165 #if WXWIN_COMPATIBILITY_2_8
3166 // lot of code out there doesn't explicitly include wx/crt.h, but uses
3167 // CRT wrappers that are now declared in wx/wxcrt.h and wx/wxcrtvararg.h,
3168 // so let's include this header now that wxString is defined and it's safe
3173 #endif // _WX_WXSTRING_H_