1 /////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/string.cpp
3 // Purpose: wxString class
4 // Author: Vadim Zeitlin, Ryan Norton
8 // Copyright: (c) 1998 Vadim Zeitlin <zeitlin@dptmaths.ens-cachan.fr>
9 // (c) 2004 Ryan Norton <wxprojects@comcast.net>
10 // Licence: wxWindows licence
11 /////////////////////////////////////////////////////////////////////////////
13 // ===========================================================================
14 // headers, declarations, constants
15 // ===========================================================================
17 // For compilers that support precompilation, includes "wx.h".
18 #include "wx/wxprec.h"
25 #include "wx/string.h"
26 #include "wx/wxcrtvararg.h"
42 #include "wx/hashmap.h"
44 // string handling functions used by wxString:
45 #if wxUSE_UNICODE_UTF8
46 #define wxStringMemcpy memcpy
47 #define wxStringMemcmp memcmp
48 #define wxStringMemchr memchr
49 #define wxStringStrlen strlen
51 #define wxStringMemcpy wxTmemcpy
52 #define wxStringMemcmp wxTmemcmp
53 #define wxStringMemchr wxTmemchr
54 #define wxStringStrlen wxStrlen
58 // ---------------------------------------------------------------------------
59 // static class variables definition
60 // ---------------------------------------------------------------------------
62 //According to STL _must_ be a -1 size_t
63 const size_t wxString::npos
= (size_t) -1;
65 // ----------------------------------------------------------------------------
67 // ----------------------------------------------------------------------------
69 #if wxUSE_STD_IOSTREAM
73 wxSTD ostream
& operator<<(wxSTD ostream
& os
, const wxCStrData
& str
)
75 // FIXME-UTF8: always, not only if wxUSE_UNICODE
76 #if wxUSE_UNICODE && !defined(__BORLANDC__)
77 return os
<< (const wchar_t*)str
.AsWCharBuf();
79 return os
<< (const char*)str
.AsCharBuf();
83 wxSTD ostream
& operator<<(wxSTD ostream
& os
, const wxString
& str
)
85 return os
<< str
.c_str();
88 wxSTD ostream
& operator<<(wxSTD ostream
& os
, const wxCharBuffer
& str
)
90 return os
<< str
.data();
94 wxSTD ostream
& operator<<(wxSTD ostream
& os
, const wxWCharBuffer
& str
)
96 return os
<< str
.data();
100 #endif // wxUSE_STD_IOSTREAM
102 // ===========================================================================
103 // wxString class core
104 // ===========================================================================
106 #if wxUSE_UNICODE_UTF8
108 void wxString::PosLenToImpl(size_t pos
, size_t len
,
109 size_t *implPos
, size_t *implLen
) const
115 const_iterator i
= begin() + pos
;
116 *implPos
= wxStringImpl::const_iterator(i
.impl()) - m_impl
.begin();
121 // too large length is interpreted as "to the end of the string"
122 // FIXME-UTF8: verify this is the case in std::string, assert
124 if ( pos
+ len
> length() )
125 len
= length() - pos
;
127 *implLen
= (i
+ len
).impl() - i
.impl();
132 #endif // wxUSE_UNICODE_UTF8
134 // ----------------------------------------------------------------------------
135 // wxCStrData converted strings caching
136 // ----------------------------------------------------------------------------
138 // FIXME-UTF8: temporarily disabled because it doesn't work with global
139 // string objects; re-enable after fixing this bug and benchmarking
140 // performance to see if using a hash is a good idea at all
143 // For backward compatibility reasons, it must be possible to assign the value
144 // returned by wxString::c_str() to a char* or wchar_t* variable and work with
145 // it. Returning wxCharBuffer from (const char*)c_str() wouldn't do the trick,
146 // because the memory would be freed immediately, but it has to be valid as long
147 // as the string is not modified, so that code like this still works:
149 // const wxChar *s = str.c_str();
150 // while ( s ) { ... }
152 // FIXME-UTF8: not thread safe!
153 // FIXME-UTF8: we currently clear the cached conversion only when the string is
154 // destroyed, but we should do it when the string is modified, to
155 // keep memory usage down
156 // FIXME-UTF8: we do the conversion every time As[W]Char() is called, but if we
157 // invalidated the cache on every change, we could keep the previous
159 // FIXME-UTF8: add tracing of usage of these two methods - new code is supposed
160 // to use mb_str() or wc_str() instead of (const [w]char*)c_str()
163 static inline void DeleteStringFromConversionCache(T
& hash
, const wxString
*s
)
165 typename
T::iterator i
= hash
.find(wxConstCast(s
, wxString
));
166 if ( i
!= hash
.end() )
174 // NB: non-STL implementation doesn't compile with "const wxString*" key type,
175 // so we have to use wxString* here and const-cast when used
176 WX_DECLARE_HASH_MAP(wxString
*, char*, wxPointerHash
, wxPointerEqual
,
177 wxStringCharConversionCache
);
178 static wxStringCharConversionCache gs_stringsCharCache
;
180 const char* wxCStrData::AsChar() const
182 // remove previously cache value, if any (see FIXMEs above):
183 DeleteStringFromConversionCache(gs_stringsCharCache
, m_str
);
185 // convert the string and keep it:
186 const char *s
= gs_stringsCharCache
[wxConstCast(m_str
, wxString
)] =
187 m_str
->mb_str().release();
191 #endif // wxUSE_UNICODE
193 #if !wxUSE_UNICODE_WCHAR
194 WX_DECLARE_HASH_MAP(wxString
*, wchar_t*, wxPointerHash
, wxPointerEqual
,
195 wxStringWCharConversionCache
);
196 static wxStringWCharConversionCache gs_stringsWCharCache
;
198 const wchar_t* wxCStrData::AsWChar() const
200 // remove previously cache value, if any (see FIXMEs above):
201 DeleteStringFromConversionCache(gs_stringsWCharCache
, m_str
);
203 // convert the string and keep it:
204 const wchar_t *s
= gs_stringsWCharCache
[wxConstCast(m_str
, wxString
)] =
205 m_str
->wc_str().release();
209 #endif // !wxUSE_UNICODE_WCHAR
211 wxString::~wxString()
214 // FIXME-UTF8: do this only if locale is not UTF8 if wxUSE_UNICODE_UTF8
215 DeleteStringFromConversionCache(gs_stringsCharCache
, this);
217 #if !wxUSE_UNICODE_WCHAR
218 DeleteStringFromConversionCache(gs_stringsWCharCache
, this);
224 const char* wxCStrData::AsChar() const
226 wxString
*str
= wxConstCast(m_str
, wxString
);
228 // convert the string:
229 wxCharBuffer
buf(str
->mb_str());
231 // FIXME-UTF8: do the conversion in-place in the existing buffer
232 if ( str
->m_convertedToChar
&&
233 strlen(buf
) == strlen(str
->m_convertedToChar
) )
235 // keep the same buffer for as long as possible, so that several calls
236 // to c_str() in a row still work:
237 strcpy(str
->m_convertedToChar
, buf
);
241 str
->m_convertedToChar
= buf
.release();
245 return str
->m_convertedToChar
+ m_offset
;
247 #endif // wxUSE_UNICODE
249 #if !wxUSE_UNICODE_WCHAR
250 const wchar_t* wxCStrData::AsWChar() const
252 wxString
*str
= wxConstCast(m_str
, wxString
);
254 // convert the string:
255 wxWCharBuffer
buf(str
->wc_str());
257 // FIXME-UTF8: do the conversion in-place in the existing buffer
258 if ( str
->m_convertedToWChar
&&
259 wxWcslen(buf
) == wxWcslen(str
->m_convertedToWChar
) )
261 // keep the same buffer for as long as possible, so that several calls
262 // to c_str() in a row still work:
263 memcpy(str
->m_convertedToWChar
, buf
, sizeof(wchar_t) * wxWcslen(buf
));
267 str
->m_convertedToWChar
= buf
.release();
271 return str
->m_convertedToWChar
+ m_offset
;
273 #endif // !wxUSE_UNICODE_WCHAR
275 // ===========================================================================
276 // wxString class core
277 // ===========================================================================
279 // ---------------------------------------------------------------------------
280 // construction and conversion
281 // ---------------------------------------------------------------------------
283 #if wxUSE_UNICODE_WCHAR
285 wxString::SubstrBufFromMB
wxString::ConvertStr(const char *psz
, size_t nLength
,
286 const wxMBConv
& conv
)
289 if ( !psz
|| nLength
== 0 )
290 return SubstrBufFromMB(L
"", 0);
292 if ( nLength
== npos
)
296 wxWCharBuffer
wcBuf(conv
.cMB2WC(psz
, nLength
, &wcLen
));
298 return SubstrBufFromMB(_T(""), 0);
300 return SubstrBufFromMB(wcBuf
, wcLen
);
302 #endif // wxUSE_UNICODE_WCHAR
304 #if wxUSE_UNICODE_UTF8
306 wxString::SubstrBufFromMB
wxString::ConvertStr(const char *psz
, size_t nLength
,
307 const wxMBConv
& conv
)
309 // FIXME-UTF8: return as-is without copying under UTF8 locale, return
310 // converted string under other locales - needs wxCharBuffer
314 if ( !psz
|| nLength
== 0 )
315 return SubstrBufFromMB("", 0);
317 if ( nLength
== npos
)
320 // first convert to wide string:
322 wxWCharBuffer
wcBuf(conv
.cMB2WC(psz
, nLength
, &wcLen
));
324 return SubstrBufFromMB("", 0);
326 // and then to UTF-8:
327 SubstrBufFromMB
buf(ConvertStr(wcBuf
, wcLen
, wxConvUTF8
));
328 // widechar -> UTF-8 conversion isn't supposed to ever fail:
329 wxASSERT_MSG( buf
.data
, _T("conversion to UTF-8 failed") );
333 #endif // wxUSE_UNICODE_UTF8
335 #if wxUSE_UNICODE_UTF8 || !wxUSE_UNICODE
337 wxString::SubstrBufFromWC
wxString::ConvertStr(const wchar_t *pwz
, size_t nLength
,
338 const wxMBConv
& conv
)
341 if ( !pwz
|| nLength
== 0 )
342 return SubstrBufFromWC("", 0);
344 if ( nLength
== npos
)
348 wxCharBuffer
mbBuf(conv
.cWC2MB(pwz
, nLength
, &mbLen
));
350 return SubstrBufFromWC("", 0);
352 return SubstrBufFromWC(mbBuf
, mbLen
);
354 #endif // wxUSE_UNICODE_UTF8 || !wxUSE_UNICODE
357 #if wxUSE_UNICODE_WCHAR
359 //Convert wxString in Unicode mode to a multi-byte string
360 const wxCharBuffer
wxString::mb_str(const wxMBConv
& conv
) const
362 return conv
.cWC2MB(wx_str(), length() + 1 /* size, not length */, NULL
);
365 #elif wxUSE_UNICODE_UTF8
367 const wxWCharBuffer
wxString::wc_str() const
369 return wxConvUTF8
.cMB2WC(m_impl
.c_str(),
370 m_impl
.length() + 1 /* size, not length */,
374 const wxCharBuffer
wxString::mb_str(const wxMBConv
& conv
) const
376 // FIXME-UTF8: optimize the case when conv==wxConvUTF8 or wxConvLibc
378 // FIXME-UTF8: use wc_str() here once we have buffers with length
382 wxConvUTF8
.cMB2WC(m_impl
.c_str(),
383 m_impl
.length() + 1 /* size, not length */,
386 return wxCharBuffer("");
388 return conv
.cWC2MB(wcBuf
, wcLen
, NULL
);
393 //Converts this string to a wide character string if unicode
394 //mode is not enabled and wxUSE_WCHAR_T is enabled
395 const wxWCharBuffer
wxString::wc_str(const wxMBConv
& conv
) const
397 return conv
.cMB2WC(wx_str(), length() + 1 /* size, not length */, NULL
);
400 #endif // Unicode/ANSI
402 // shrink to minimal size (releasing extra memory)
403 bool wxString::Shrink()
405 wxString
tmp(begin(), end());
407 return tmp
.length() == length();
410 // deprecated compatibility code:
411 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
412 wxStringCharType
*wxString::GetWriteBuf(size_t nLen
)
414 return DoGetWriteBuf(nLen
);
417 void wxString::UngetWriteBuf()
422 void wxString::UngetWriteBuf(size_t nLen
)
424 DoUngetWriteBuf(nLen
);
426 #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
429 // ---------------------------------------------------------------------------
431 // ---------------------------------------------------------------------------
433 // all functions are inline in string.h
435 // ---------------------------------------------------------------------------
436 // concatenation operators
437 // ---------------------------------------------------------------------------
440 * concatenation functions come in 5 flavours:
442 * char + string and string + char
443 * C str + string and string + C str
446 wxString
operator+(const wxString
& str1
, const wxString
& str2
)
448 #if !wxUSE_STL_BASED_WXSTRING
449 wxASSERT( str1
.IsValid() );
450 wxASSERT( str2
.IsValid() );
459 wxString
operator+(const wxString
& str
, wxUniChar ch
)
461 #if !wxUSE_STL_BASED_WXSTRING
462 wxASSERT( str
.IsValid() );
471 wxString
operator+(wxUniChar ch
, const wxString
& str
)
473 #if !wxUSE_STL_BASED_WXSTRING
474 wxASSERT( str
.IsValid() );
483 wxString
operator+(const wxString
& str
, const char *psz
)
485 #if !wxUSE_STL_BASED_WXSTRING
486 wxASSERT( str
.IsValid() );
490 if ( !s
.Alloc(strlen(psz
) + str
.length()) ) {
491 wxFAIL_MSG( _T("out of memory in wxString::operator+") );
499 wxString
operator+(const wxString
& str
, const wchar_t *pwz
)
501 #if !wxUSE_STL_BASED_WXSTRING
502 wxASSERT( str
.IsValid() );
506 if ( !s
.Alloc(wxWcslen(pwz
) + str
.length()) ) {
507 wxFAIL_MSG( _T("out of memory in wxString::operator+") );
515 wxString
operator+(const char *psz
, const wxString
& str
)
517 #if !wxUSE_STL_BASED_WXSTRING
518 wxASSERT( str
.IsValid() );
522 if ( !s
.Alloc(strlen(psz
) + str
.length()) ) {
523 wxFAIL_MSG( _T("out of memory in wxString::operator+") );
531 wxString
operator+(const wchar_t *pwz
, const wxString
& str
)
533 #if !wxUSE_STL_BASED_WXSTRING
534 wxASSERT( str
.IsValid() );
538 if ( !s
.Alloc(wxWcslen(pwz
) + str
.length()) ) {
539 wxFAIL_MSG( _T("out of memory in wxString::operator+") );
547 // ---------------------------------------------------------------------------
549 // ---------------------------------------------------------------------------
551 #ifdef HAVE_STD_STRING_COMPARE
553 // NB: Comparison code (both if HAVE_STD_STRING_COMPARE and if not) works with
554 // UTF-8 encoded strings too, thanks to UTF-8's design which allows us to
555 // sort strings in characters code point order by sorting the byte sequence
556 // in byte values order (i.e. what strcmp() and memcmp() do).
558 int wxString::compare(const wxString
& str
) const
560 return m_impl
.compare(str
.m_impl
);
563 int wxString::compare(size_t nStart
, size_t nLen
,
564 const wxString
& str
) const
567 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
568 return m_impl
.compare(pos
, len
, str
.m_impl
);
571 int wxString::compare(size_t nStart
, size_t nLen
,
573 size_t nStart2
, size_t nLen2
) const
576 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
579 str
.PosLenToImpl(nStart2
, nLen2
, &pos2
, &len2
);
581 return m_impl
.compare(pos
, len
, str
.m_impl
, pos2
, len2
);
584 int wxString::compare(const char* sz
) const
586 return m_impl
.compare(ImplStr(sz
));
589 int wxString::compare(const wchar_t* sz
) const
591 return m_impl
.compare(ImplStr(sz
));
594 int wxString::compare(size_t nStart
, size_t nLen
,
595 const char* sz
, size_t nCount
) const
598 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
600 SubstrBufFromMB
str(ImplStr(sz
, nCount
));
602 return m_impl
.compare(pos
, len
, str
.data
, str
.len
);
605 int wxString::compare(size_t nStart
, size_t nLen
,
606 const wchar_t* sz
, size_t nCount
) const
609 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
611 SubstrBufFromWC
str(ImplStr(sz
, nCount
));
613 return m_impl
.compare(pos
, len
, str
.data
, str
.len
);
616 #else // !HAVE_STD_STRING_COMPARE
618 static inline int wxDoCmp(const wxStringCharType
* s1
, size_t l1
,
619 const wxStringCharType
* s2
, size_t l2
)
622 return wxStringMemcmp(s1
, s2
, l1
);
625 int ret
= wxStringMemcmp(s1
, s2
, l1
);
626 return ret
== 0 ? -1 : ret
;
630 int ret
= wxStringMemcmp(s1
, s2
, l2
);
631 return ret
== 0 ? +1 : ret
;
635 int wxString::compare(const wxString
& str
) const
637 return ::wxDoCmp(m_impl
.data(), m_impl
.length(),
638 str
.m_impl
.data(), str
.m_impl
.length());
641 int wxString::compare(size_t nStart
, size_t nLen
,
642 const wxString
& str
) const
644 wxASSERT(nStart
<= length());
645 size_type strLen
= length() - nStart
;
646 nLen
= strLen
< nLen
? strLen
: nLen
;
649 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
651 return ::wxDoCmp(m_impl
.data() + pos
, len
,
652 str
.m_impl
.data(), str
.m_impl
.length());
655 int wxString::compare(size_t nStart
, size_t nLen
,
657 size_t nStart2
, size_t nLen2
) const
659 wxASSERT(nStart
<= length());
660 wxASSERT(nStart2
<= str
.length());
661 size_type strLen
= length() - nStart
,
662 strLen2
= str
.length() - nStart2
;
663 nLen
= strLen
< nLen
? strLen
: nLen
;
664 nLen2
= strLen2
< nLen2
? strLen2
: nLen2
;
667 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
669 str
.PosLenToImpl(nStart2
, nLen2
, &pos2
, &len2
);
671 return ::wxDoCmp(m_impl
.data() + pos
, len
,
672 str
.m_impl
.data() + pos2
, len2
);
675 int wxString::compare(const char* sz
) const
677 SubstrBufFromMB
str(ImplStr(sz
, npos
));
678 if ( str
.len
== npos
)
679 str
.len
= wxStringStrlen(str
.data
);
680 return ::wxDoCmp(m_impl
.data(), m_impl
.length(), str
.data
, str
.len
);
683 int wxString::compare(const wchar_t* sz
) const
685 SubstrBufFromWC
str(ImplStr(sz
, npos
));
686 if ( str
.len
== npos
)
687 str
.len
= wxStringStrlen(str
.data
);
688 return ::wxDoCmp(m_impl
.data(), m_impl
.length(), str
.data
, str
.len
);
691 int wxString::compare(size_t nStart
, size_t nLen
,
692 const char* sz
, size_t nCount
) const
694 wxASSERT(nStart
<= length());
695 size_type strLen
= length() - nStart
;
696 nLen
= strLen
< nLen
? strLen
: nLen
;
699 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
701 SubstrBufFromMB
str(ImplStr(sz
, nCount
));
702 if ( str
.len
== npos
)
703 str
.len
= wxStringStrlen(str
.data
);
705 return ::wxDoCmp(m_impl
.data() + pos
, len
, str
.data
, str
.len
);
708 int wxString::compare(size_t nStart
, size_t nLen
,
709 const wchar_t* sz
, size_t nCount
) const
711 wxASSERT(nStart
<= length());
712 size_type strLen
= length() - nStart
;
713 nLen
= strLen
< nLen
? strLen
: nLen
;
716 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
718 SubstrBufFromWC
str(ImplStr(sz
, nCount
));
719 if ( str
.len
== npos
)
720 str
.len
= wxStringStrlen(str
.data
);
722 return ::wxDoCmp(m_impl
.data() + pos
, len
, str
.data
, str
.len
);
725 #endif // HAVE_STD_STRING_COMPARE/!HAVE_STD_STRING_COMPARE
728 // ---------------------------------------------------------------------------
729 // find_{first,last}_[not]_of functions
730 // ---------------------------------------------------------------------------
732 #if !wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
734 // NB: All these functions are implemented with the argument being wxChar*,
735 // i.e. widechar string in any Unicode build, even though native string
736 // representation is char* in the UTF-8 build. This is because we couldn't
737 // use memchr() to determine if a character is in a set encoded as UTF-8.
739 size_t wxString::find_first_of(const wxChar
* sz
, size_t nStart
) const
741 return find_first_of(sz
, nStart
, wxStrlen(sz
));
744 size_t wxString::find_first_not_of(const wxChar
* sz
, size_t nStart
) const
746 return find_first_not_of(sz
, nStart
, wxStrlen(sz
));
749 size_t wxString::find_first_of(const wxChar
* sz
, size_t nStart
, size_t n
) const
751 wxASSERT_MSG( nStart
<= length(), _T("invalid index") );
754 for ( const_iterator i
= begin() + nStart
; i
!= end(); ++idx
, ++i
)
756 if ( wxTmemchr(sz
, *i
, n
) )
763 size_t wxString::find_first_not_of(const wxChar
* sz
, size_t nStart
, size_t n
) const
765 wxASSERT_MSG( nStart
<= length(), _T("invalid index") );
768 for ( const_iterator i
= begin() + nStart
; i
!= end(); ++idx
, ++i
)
770 if ( !wxTmemchr(sz
, *i
, n
) )
778 size_t wxString::find_last_of(const wxChar
* sz
, size_t nStart
) const
780 return find_last_of(sz
, nStart
, wxStrlen(sz
));
783 size_t wxString::find_last_not_of(const wxChar
* sz
, size_t nStart
) const
785 return find_last_not_of(sz
, nStart
, wxStrlen(sz
));
788 size_t wxString::find_last_of(const wxChar
* sz
, size_t nStart
, size_t n
) const
790 size_t len
= length();
792 if ( nStart
== npos
)
798 wxASSERT_MSG( nStart
<= len
, _T("invalid index") );
802 for ( const_reverse_iterator i
= rbegin() + (len
- nStart
- 1);
803 i
!= rend(); --idx
, ++i
)
805 if ( wxTmemchr(sz
, *i
, n
) )
812 size_t wxString::find_last_not_of(const wxChar
* sz
, size_t nStart
, size_t n
) const
814 size_t len
= length();
816 if ( nStart
== npos
)
822 wxASSERT_MSG( nStart
<= len
, _T("invalid index") );
826 for ( const_reverse_iterator i
= rbegin() + (len
- nStart
- 1);
827 i
!= rend(); --idx
, ++i
)
829 if ( !wxTmemchr(sz
, *i
, n
) )
836 size_t wxString::find_first_not_of(wxUniChar ch
, size_t nStart
) const
838 wxASSERT_MSG( nStart
<= length(), _T("invalid index") );
841 for ( const_iterator i
= begin() + nStart
; i
!= end(); ++idx
, ++i
)
850 size_t wxString::find_last_not_of(wxUniChar ch
, size_t nStart
) const
852 size_t len
= length();
854 if ( nStart
== npos
)
860 wxASSERT_MSG( nStart
<= len
, _T("invalid index") );
864 for ( const_reverse_iterator i
= rbegin() + (len
- nStart
- 1);
865 i
!= rend(); --idx
, ++i
)
874 // the functions above were implemented for wchar_t* arguments in Unicode
875 // build and char* in ANSI build; below are implementations for the other
878 #define wxOtherCharType char
879 #define STRCONV (const wxChar*)wxConvLibc.cMB2WC
881 #define wxOtherCharType wchar_t
882 #define STRCONV (const wxChar*)wxConvLibc.cWC2MB
885 size_t wxString::find_first_of(const wxOtherCharType
* sz
, size_t nStart
) const
886 { return find_first_of(STRCONV(sz
), nStart
); }
888 size_t wxString::find_first_of(const wxOtherCharType
* sz
, size_t nStart
,
890 { return find_first_of(STRCONV(sz
, n
, NULL
), nStart
, n
); }
891 size_t wxString::find_last_of(const wxOtherCharType
* sz
, size_t nStart
) const
892 { return find_last_of(STRCONV(sz
), nStart
); }
893 size_t wxString::find_last_of(const wxOtherCharType
* sz
, size_t nStart
,
895 { return find_last_of(STRCONV(sz
, n
, NULL
), nStart
, n
); }
896 size_t wxString::find_first_not_of(const wxOtherCharType
* sz
, size_t nStart
) const
897 { return find_first_not_of(STRCONV(sz
), nStart
); }
898 size_t wxString::find_first_not_of(const wxOtherCharType
* sz
, size_t nStart
,
900 { return find_first_not_of(STRCONV(sz
, n
, NULL
), nStart
, n
); }
901 size_t wxString::find_last_not_of(const wxOtherCharType
* sz
, size_t nStart
) const
902 { return find_last_not_of(STRCONV(sz
), nStart
); }
903 size_t wxString::find_last_not_of(const wxOtherCharType
* sz
, size_t nStart
,
905 { return find_last_not_of(STRCONV(sz
, n
, NULL
), nStart
, n
); }
907 #undef wxOtherCharType
910 #endif // !wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
912 // ===========================================================================
913 // other common string functions
914 // ===========================================================================
916 int wxString::CmpNoCase(const wxString
& s
) const
918 // FIXME-UTF8: use wxUniChar::ToLower/ToUpper once added
921 const_iterator i1
= begin();
922 const_iterator end1
= end();
923 const_iterator i2
= s
.begin();
924 const_iterator end2
= s
.end();
926 for ( ; i1
!= end1
&& i2
!= end2
; ++idx
, ++i1
, ++i2
)
928 wxUniChar lower1
= (wxChar
)wxTolower(*i1
);
929 wxUniChar lower2
= (wxChar
)wxTolower(*i2
);
930 if ( lower1
!= lower2
)
931 return lower1
< lower2
? -1 : 1;
934 size_t len1
= length();
935 size_t len2
= s
.length();
939 else if ( len1
> len2
)
948 #ifndef __SCHAR_MAX__
949 #define __SCHAR_MAX__ 127
953 wxString
wxString::FromAscii(const char *ascii
)
956 return wxEmptyString
;
958 size_t len
= strlen( ascii
);
963 wxStringBuffer
buf(res
, len
);
969 if ( (*dest
++ = (wchar_t)(unsigned char)*ascii
++) == L
'\0' )
977 wxString
wxString::FromAscii(const char ascii
)
979 // What do we do with '\0' ?
982 res
+= (wchar_t)(unsigned char) ascii
;
987 const wxCharBuffer
wxString::ToAscii() const
989 // this will allocate enough space for the terminating NUL too
990 wxCharBuffer
buffer(length());
993 char *dest
= buffer
.data();
995 const wchar_t *pwc
= c_str();
998 *dest
++ = (char)(*pwc
> SCHAR_MAX
? wxT('_') : *pwc
);
1000 // the output string can't have embedded NULs anyhow, so we can safely
1001 // stop at first of them even if we do have any
1011 // extract string of length nCount starting at nFirst
1012 wxString
wxString::Mid(size_t nFirst
, size_t nCount
) const
1014 size_t nLen
= length();
1016 // default value of nCount is npos and means "till the end"
1017 if ( nCount
== npos
)
1019 nCount
= nLen
- nFirst
;
1022 // out-of-bounds requests return sensible things
1023 if ( nFirst
+ nCount
> nLen
)
1025 nCount
= nLen
- nFirst
;
1028 if ( nFirst
> nLen
)
1030 // AllocCopy() will return empty string
1031 return wxEmptyString
;
1034 wxString
dest(*this, nFirst
, nCount
);
1035 if ( dest
.length() != nCount
)
1037 wxFAIL_MSG( _T("out of memory in wxString::Mid") );
1043 // check that the string starts with prefix and return the rest of the string
1044 // in the provided pointer if it is not NULL, otherwise return false
1045 bool wxString::StartsWith(const wxChar
*prefix
, wxString
*rest
) const
1047 wxASSERT_MSG( prefix
, _T("invalid parameter in wxString::StartsWith") );
1049 // first check if the beginning of the string matches the prefix: note
1050 // that we don't have to check that we don't run out of this string as
1051 // when we reach the terminating NUL, either prefix string ends too (and
1052 // then it's ok) or we break out of the loop because there is no match
1053 const wxChar
*p
= c_str();
1056 if ( *prefix
++ != *p
++ )
1065 // put the rest of the string into provided pointer
1073 // check that the string ends with suffix and return the rest of it in the
1074 // provided pointer if it is not NULL, otherwise return false
1075 bool wxString::EndsWith(const wxChar
*suffix
, wxString
*rest
) const
1077 wxASSERT_MSG( suffix
, _T("invalid parameter in wxString::EndssWith") );
1079 int start
= length() - wxStrlen(suffix
);
1081 if ( start
< 0 || compare(start
, npos
, suffix
) != 0 )
1086 // put the rest of the string into provided pointer
1087 rest
->assign(*this, 0, start
);
1094 // extract nCount last (rightmost) characters
1095 wxString
wxString::Right(size_t nCount
) const
1097 if ( nCount
> length() )
1100 wxString
dest(*this, length() - nCount
, nCount
);
1101 if ( dest
.length() != nCount
) {
1102 wxFAIL_MSG( _T("out of memory in wxString::Right") );
1107 // get all characters after the last occurence of ch
1108 // (returns the whole string if ch not found)
1109 wxString
wxString::AfterLast(wxUniChar ch
) const
1112 int iPos
= Find(ch
, true);
1113 if ( iPos
== wxNOT_FOUND
)
1116 str
= wx_str() + iPos
+ 1;
1121 // extract nCount first (leftmost) characters
1122 wxString
wxString::Left(size_t nCount
) const
1124 if ( nCount
> length() )
1127 wxString
dest(*this, 0, nCount
);
1128 if ( dest
.length() != nCount
) {
1129 wxFAIL_MSG( _T("out of memory in wxString::Left") );
1134 // get all characters before the first occurence of ch
1135 // (returns the whole string if ch not found)
1136 wxString
wxString::BeforeFirst(wxUniChar ch
) const
1138 int iPos
= Find(ch
);
1139 if ( iPos
== wxNOT_FOUND
) iPos
= length();
1140 return wxString(*this, 0, iPos
);
1143 /// get all characters before the last occurence of ch
1144 /// (returns empty string if ch not found)
1145 wxString
wxString::BeforeLast(wxUniChar ch
) const
1148 int iPos
= Find(ch
, true);
1149 if ( iPos
!= wxNOT_FOUND
&& iPos
!= 0 )
1150 str
= wxString(c_str(), iPos
);
1155 /// get all characters after the first occurence of ch
1156 /// (returns empty string if ch not found)
1157 wxString
wxString::AfterFirst(wxUniChar ch
) const
1160 int iPos
= Find(ch
);
1161 if ( iPos
!= wxNOT_FOUND
)
1162 str
= wx_str() + iPos
+ 1;
1167 // replace first (or all) occurences of some substring with another one
1168 size_t wxString::Replace(const wxString
& strOld
,
1169 const wxString
& strNew
, bool bReplaceAll
)
1171 // if we tried to replace an empty string we'd enter an infinite loop below
1172 wxCHECK_MSG( !strOld
.empty(), 0,
1173 _T("wxString::Replace(): invalid parameter") );
1175 size_t uiCount
= 0; // count of replacements made
1177 size_t uiOldLen
= strOld
.length();
1178 size_t uiNewLen
= strNew
.length();
1182 while ( (*this)[dwPos
] != wxT('\0') )
1184 //DO NOT USE STRSTR HERE
1185 //this string can contain embedded null characters,
1186 //so strstr will function incorrectly
1187 dwPos
= find(strOld
, dwPos
);
1188 if ( dwPos
== npos
)
1189 break; // exit the loop
1192 //replace this occurance of the old string with the new one
1193 replace(dwPos
, uiOldLen
, strNew
, uiNewLen
);
1195 //move up pos past the string that was replaced
1198 //increase replace count
1203 break; // exit the loop
1210 bool wxString::IsAscii() const
1212 for ( const_iterator i
= begin(); i
!= end(); ++i
)
1214 if ( !(*i
).IsAscii() )
1221 bool wxString::IsWord() const
1223 for ( const_iterator i
= begin(); i
!= end(); ++i
)
1225 if ( !wxIsalpha(*i
) )
1232 bool wxString::IsNumber() const
1237 const_iterator i
= begin();
1239 if ( *i
== _T('-') || *i
== _T('+') )
1242 for ( ; i
!= end(); ++i
)
1244 if ( !wxIsdigit(*i
) )
1251 wxString
wxString::Strip(stripType w
) const
1254 if ( w
& leading
) s
.Trim(false);
1255 if ( w
& trailing
) s
.Trim(true);
1259 // ---------------------------------------------------------------------------
1261 // ---------------------------------------------------------------------------
1263 wxString
& wxString::MakeUpper()
1265 for ( iterator it
= begin(), en
= end(); it
!= en
; ++it
)
1266 *it
= (wxChar
)wxToupper(*it
);
1271 wxString
& wxString::MakeLower()
1273 for ( iterator it
= begin(), en
= end(); it
!= en
; ++it
)
1274 *it
= (wxChar
)wxTolower(*it
);
1279 // ---------------------------------------------------------------------------
1280 // trimming and padding
1281 // ---------------------------------------------------------------------------
1283 // some compilers (VC++ 6.0 not to name them) return true for a call to
1284 // isspace('ê') in the C locale which seems to be broken to me, but we have to
1285 // live with this by checking that the character is a 7 bit one - even if this
1286 // may fail to detect some spaces (I don't know if Unicode doesn't have
1287 // space-like symbols somewhere except in the first 128 chars), it is arguably
1288 // still better than trimming away accented letters
1289 inline int wxSafeIsspace(wxChar ch
) { return (ch
< 127) && wxIsspace(ch
); }
1291 // trims spaces (in the sense of isspace) from left or right side
1292 wxString
& wxString::Trim(bool bFromRight
)
1294 // first check if we're going to modify the string at all
1297 (bFromRight
&& wxSafeIsspace(GetChar(length() - 1))) ||
1298 (!bFromRight
&& wxSafeIsspace(GetChar(0u)))
1304 // find last non-space character
1305 reverse_iterator psz
= rbegin();
1306 while ( (psz
!= rend()) && wxSafeIsspace(*psz
) )
1309 // truncate at trailing space start
1310 erase(psz
.base(), end());
1314 // find first non-space character
1315 iterator psz
= begin();
1316 while ( (psz
!= end()) && wxSafeIsspace(*psz
) )
1319 // fix up data and length
1320 erase(begin(), psz
);
1327 // adds nCount characters chPad to the string from either side
1328 wxString
& wxString::Pad(size_t nCount
, wxUniChar chPad
, bool bFromRight
)
1330 wxString
s(chPad
, nCount
);
1343 // truncate the string
1344 wxString
& wxString::Truncate(size_t uiLen
)
1346 if ( uiLen
< length() )
1348 erase(begin() + uiLen
, end());
1350 //else: nothing to do, string is already short enough
1355 // ---------------------------------------------------------------------------
1356 // finding (return wxNOT_FOUND if not found and index otherwise)
1357 // ---------------------------------------------------------------------------
1360 int wxString::Find(wxUniChar ch
, bool bFromEnd
) const
1362 size_type idx
= bFromEnd
? find_last_of(ch
) : find_first_of(ch
);
1364 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
1367 // ----------------------------------------------------------------------------
1368 // conversion to numbers
1369 // ----------------------------------------------------------------------------
1371 // the implementation of all the functions below is exactly the same so factor
1374 template <typename T
, typename F
>
1375 bool wxStringToIntType(const wxChar
*start
,
1380 wxCHECK_MSG( val
, false, _T("NULL output pointer") );
1381 wxASSERT_MSG( !base
|| (base
> 1 && base
<= 36), _T("invalid base") );
1388 *val
= (*func
)(start
, &end
, base
);
1390 // return true only if scan was stopped by the terminating NUL and if the
1391 // string was not empty to start with and no under/overflow occurred
1392 return !*end
&& (end
!= start
)
1394 && (errno
!= ERANGE
)
1399 bool wxString::ToLong(long *val
, int base
) const
1401 return wxStringToIntType((const wxChar
*)c_str(), val
, base
, wxStrtol
);
1404 bool wxString::ToULong(unsigned long *val
, int base
) const
1406 return wxStringToIntType((const wxChar
*)c_str(), val
, base
, wxStrtoul
);
1409 bool wxString::ToLongLong(wxLongLong_t
*val
, int base
) const
1411 return wxStringToIntType((const wxChar
*)c_str(), val
, base
, wxStrtoll
);
1414 bool wxString::ToULongLong(wxULongLong_t
*val
, int base
) const
1416 return wxStringToIntType((const wxChar
*)c_str(), val
, base
, wxStrtoull
);
1419 bool wxString::ToDouble(double *val
) const
1421 wxCHECK_MSG( val
, false, _T("NULL pointer in wxString::ToDouble") );
1427 const wxChar
*start
= c_str();
1429 *val
= wxStrtod(start
, &end
);
1431 // return true only if scan was stopped by the terminating NUL and if the
1432 // string was not empty to start with and no under/overflow occurred
1433 return !*end
&& (end
!= start
)
1435 && (errno
!= ERANGE
)
1440 // ---------------------------------------------------------------------------
1442 // ---------------------------------------------------------------------------
1445 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
1446 wxString
wxStringPrintfMixinBase::DoFormat(const wxString
& format
, ...)
1448 wxString
wxString::DoFormat(const wxString
& format
, ...)
1452 va_start(argptr
, format
);
1455 s
.PrintfV(format
, argptr
);
1463 wxString
wxString::FormatV(const wxString
& format
, va_list argptr
)
1466 s
.PrintfV(format
, argptr
);
1470 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
1471 int wxStringPrintfMixinBase::DoPrintf(const wxString
& format
, ...)
1473 int wxString::DoPrintf(const wxString
& format
, ...)
1477 va_start(argptr
, format
);
1479 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
1480 // get a pointer to the wxString instance; we have to use dynamic_cast<>
1481 // because it's the only cast that works safely for downcasting when
1482 // multiple inheritance is used:
1483 wxString
*str
= static_cast<wxString
*>(this);
1485 wxString
*str
= this;
1488 int iLen
= str
->PrintfV(format
, argptr
);
1495 #if wxUSE_UNICODE_UTF8
1496 template<typename BufferType
>
1498 // we only need one version in non-UTF8 builds and at least two Windows
1499 // compilers have problems with this function template, so use just one
1500 // normal function here
1502 static int DoStringPrintfV(wxString
& str
,
1503 const wxString
& format
, va_list argptr
)
1509 #if wxUSE_UNICODE_UTF8
1510 BufferType
tmp(str
, size
+ 1);
1511 typename
BufferType::CharType
*buf
= tmp
;
1513 wxStringBuffer
tmp(str
, size
+ 1);
1523 // wxVsnprintf() may modify the original arg pointer, so pass it
1526 wxVaCopy(argptrcopy
, argptr
);
1527 int len
= wxVsnprintf(buf
, size
, format
, argptrcopy
);
1530 // some implementations of vsnprintf() don't NUL terminate
1531 // the string if there is not enough space for it so
1532 // always do it manually
1533 buf
[size
] = _T('\0');
1535 // vsnprintf() may return either -1 (traditional Unix behaviour) or the
1536 // total number of characters which would have been written if the
1537 // buffer were large enough (newer standards such as Unix98)
1540 #if wxUSE_WXVSNPRINTF
1541 // we know that our own implementation of wxVsnprintf() returns -1
1542 // only for a format error - thus there's something wrong with
1543 // the user's format string
1545 #else // assume that system version only returns error if not enough space
1546 // still not enough, as we don't know how much we need, double the
1547 // current size of the buffer
1549 #endif // wxUSE_WXVSNPRINTF/!wxUSE_WXVSNPRINTF
1551 else if ( len
>= size
)
1553 #if wxUSE_WXVSNPRINTF
1554 // we know that our own implementation of wxVsnprintf() returns
1555 // size+1 when there's not enough space but that's not the size
1556 // of the required buffer!
1557 size
*= 2; // so we just double the current size of the buffer
1559 // some vsnprintf() implementations NUL-terminate the buffer and
1560 // some don't in len == size case, to be safe always add 1
1564 else // ok, there was enough space
1570 // we could have overshot
1573 return str
.length();
1576 int wxString::PrintfV(const wxString
& format
, va_list argptr
)
1579 wxVaCopy(argcopy
, argptr
);
1581 #if wxUSE_UNICODE_UTF8
1582 #if wxUSE_STL_BASED_WXSTRING
1583 typedef wxStringTypeBuffer
<char> Utf8Buffer
;
1585 typedef wxImplStringBuffer Utf8Buffer
;
1589 #if wxUSE_UTF8_LOCALE_ONLY
1590 return DoStringPrintfV
<Utf8Buffer
>(*this, format
, argcopy
);
1592 #if wxUSE_UNICODE_UTF8
1593 if ( wxLocaleIsUtf8
)
1594 return DoStringPrintfV
<Utf8Buffer
>(*this, format
, argcopy
);
1597 return DoStringPrintfV
<wxStringBuffer
>(*this, format
, argcopy
);
1599 return DoStringPrintfV(*this, format
, argcopy
);
1600 #endif // UTF8/WCHAR
1604 // ----------------------------------------------------------------------------
1605 // misc other operations
1606 // ----------------------------------------------------------------------------
1608 // returns true if the string matches the pattern which may contain '*' and
1609 // '?' metacharacters (as usual, '?' matches any character and '*' any number
1611 bool wxString::Matches(const wxString
& mask
) const
1613 // I disable this code as it doesn't seem to be faster (in fact, it seems
1614 // to be much slower) than the old, hand-written code below and using it
1615 // here requires always linking with libregex even if the user code doesn't
1617 #if 0 // wxUSE_REGEX
1618 // first translate the shell-like mask into a regex
1620 pattern
.reserve(wxStrlen(pszMask
));
1632 pattern
+= _T(".*");
1643 // these characters are special in a RE, quote them
1644 // (however note that we don't quote '[' and ']' to allow
1645 // using them for Unix shell like matching)
1646 pattern
+= _T('\\');
1650 pattern
+= *pszMask
;
1658 return wxRegEx(pattern
, wxRE_NOSUB
| wxRE_EXTENDED
).Matches(c_str());
1659 #else // !wxUSE_REGEX
1660 // TODO: this is, of course, awfully inefficient...
1662 // FIXME-UTF8: implement using iterators, remove #if
1663 #if wxUSE_UNICODE_UTF8
1664 wxWCharBuffer maskBuf
= mask
.wc_str();
1665 wxWCharBuffer txtBuf
= wc_str();
1666 const wxChar
*pszMask
= maskBuf
.data();
1667 const wxChar
*pszTxt
= txtBuf
.data();
1669 const wxChar
*pszMask
= mask
.wx_str();
1670 // the char currently being checked
1671 const wxChar
*pszTxt
= wx_str();
1674 // the last location where '*' matched
1675 const wxChar
*pszLastStarInText
= NULL
;
1676 const wxChar
*pszLastStarInMask
= NULL
;
1679 for ( ; *pszMask
!= wxT('\0'); pszMask
++, pszTxt
++ ) {
1680 switch ( *pszMask
) {
1682 if ( *pszTxt
== wxT('\0') )
1685 // pszTxt and pszMask will be incremented in the loop statement
1691 // remember where we started to be able to backtrack later
1692 pszLastStarInText
= pszTxt
;
1693 pszLastStarInMask
= pszMask
;
1695 // ignore special chars immediately following this one
1696 // (should this be an error?)
1697 while ( *pszMask
== wxT('*') || *pszMask
== wxT('?') )
1700 // if there is nothing more, match
1701 if ( *pszMask
== wxT('\0') )
1704 // are there any other metacharacters in the mask?
1706 const wxChar
*pEndMask
= wxStrpbrk(pszMask
, wxT("*?"));
1708 if ( pEndMask
!= NULL
) {
1709 // we have to match the string between two metachars
1710 uiLenMask
= pEndMask
- pszMask
;
1713 // we have to match the remainder of the string
1714 uiLenMask
= wxStrlen(pszMask
);
1717 wxString
strToMatch(pszMask
, uiLenMask
);
1718 const wxChar
* pMatch
= wxStrstr(pszTxt
, strToMatch
);
1719 if ( pMatch
== NULL
)
1722 // -1 to compensate "++" in the loop
1723 pszTxt
= pMatch
+ uiLenMask
- 1;
1724 pszMask
+= uiLenMask
- 1;
1729 if ( *pszMask
!= *pszTxt
)
1735 // match only if nothing left
1736 if ( *pszTxt
== wxT('\0') )
1739 // if we failed to match, backtrack if we can
1740 if ( pszLastStarInText
) {
1741 pszTxt
= pszLastStarInText
+ 1;
1742 pszMask
= pszLastStarInMask
;
1744 pszLastStarInText
= NULL
;
1746 // don't bother resetting pszLastStarInMask, it's unnecessary
1752 #endif // wxUSE_REGEX/!wxUSE_REGEX
1755 // Count the number of chars
1756 int wxString::Freq(wxUniChar ch
) const
1759 for ( const_iterator i
= begin(); i
!= end(); ++i
)
1767 // convert to upper case, return the copy of the string
1768 wxString
wxString::Upper() const
1769 { wxString
s(*this); return s
.MakeUpper(); }
1771 // convert to lower case, return the copy of the string
1772 wxString
wxString::Lower() const { wxString
s(*this); return s
.MakeLower(); }