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 /////////////////////////////////////////////////////////////////////////////
15 * 1) all empty strings use g_strEmpty, nRefs = -1 (set in Init())
16 * 2) AllocBuffer() sets nRefs to 1, Lock() increments it by one
17 * 3) Unlock() decrements nRefs and frees memory if it goes to 0
20 // ===========================================================================
21 // headers, declarations, constants
22 // ===========================================================================
24 // For compilers that support precompilation, includes "wx.h".
25 #include "wx/wxprec.h"
32 #include "wx/string.h"
48 #include "wx/hashmap.h"
50 // string handling functions used by wxString:
51 #if wxUSE_UNICODE_UTF8
52 #define wxStringMemcpy memcpy
53 #define wxStringMemcmp memcmp
54 #define wxStringMemchr memchr
55 #define wxStringStrlen strlen
57 #define wxStringMemcpy wxTmemcpy
58 #define wxStringMemcmp wxTmemcmp
59 #define wxStringMemchr wxTmemchr
60 #define wxStringStrlen wxStrlen
64 // ---------------------------------------------------------------------------
65 // static class variables definition
66 // ---------------------------------------------------------------------------
68 //According to STL _must_ be a -1 size_t
69 const size_t wxString::npos
= (size_t) -1;
71 // ----------------------------------------------------------------------------
73 // ----------------------------------------------------------------------------
75 #if wxUSE_STD_IOSTREAM
79 wxSTD ostream
& operator<<(wxSTD ostream
& os
, const wxCStrData
& str
)
81 // FIXME-UTF8: always, not only if wxUSE_UNICODE
82 #if wxUSE_UNICODE && !defined(__BORLANDC__)
83 return os
<< str
.AsWChar();
85 return os
<< str
.AsChar();
89 wxSTD ostream
& operator<<(wxSTD ostream
& os
, const wxString
& str
)
91 return os
<< str
.c_str();
94 wxSTD ostream
& operator<<(wxSTD ostream
& os
, const wxCharBuffer
& str
)
96 return os
<< str
.data();
100 wxSTD ostream
& operator<<(wxSTD ostream
& os
, const wxWCharBuffer
& str
)
102 return os
<< str
.data();
106 #endif // wxUSE_STD_IOSTREAM
108 // ===========================================================================
109 // wxString class core
110 // ===========================================================================
112 #if wxUSE_UNICODE_UTF8
114 // ---------------------------------------------------------------------------
116 // ---------------------------------------------------------------------------
119 // Table 3.1B from Unicode spec: Legal UTF-8 Byte Sequences
121 // Code Points | 1st Byte | 2nd Byte | 3rd Byte | 4th Byte |
122 // -------------------+----------+----------+----------+----------+
123 // U+0000..U+007F | 00..7F | | | |
124 // U+0080..U+07FF | C2..DF | 80..BF | | |
125 // U+0800..U+0FFF | E0 | A0..BF | 80..BF | |
126 // U+1000..U+FFFF | E1..EF | 80..BF | 80..BF | |
127 // U+10000..U+3FFFF | F0 | 90..BF | 80..BF | 80..BF |
128 // U+40000..U+FFFFF | F1..F3 | 80..BF | 80..BF | 80..BF |
129 // U+100000..U+10FFFF | F4 | 80..8F | 80..BF | 80..BF |
130 // -------------------+----------+----------+----------+----------+
132 bool wxString::IsValidUtf8String(const char *str
)
135 return true; // empty string is UTF8 string
137 const unsigned char *c
= (const unsigned char*)str
;
141 unsigned char b
= *c
;
143 if ( b
<= 0x7F ) // 00..7F
146 else if ( b
< 0xC2 ) // invalid lead bytes: 80..C1
149 // two-byte sequences:
150 else if ( b
<= 0xDF ) // C2..DF
153 if ( !(b
>= 0x80 && b
<= 0xBF ) )
157 // three-byte sequences:
158 else if ( b
== 0xE0 )
161 if ( !(b
>= 0xA0 && b
<= 0xBF ) )
164 if ( !(b
>= 0x80 && b
<= 0xBF ) )
167 else if ( b
<= 0xEF ) // E1..EF
169 for ( int i
= 0; i
< 2; ++i
)
172 if ( !(b
>= 0x80 && b
<= 0xBF ) )
177 // four-byte sequences:
178 else if ( b
== 0xF0 )
181 if ( !(b
>= 0x90 && b
<= 0xBF ) )
183 for ( int i
= 0; i
< 2; ++i
)
186 if ( !(b
>= 0x80 && b
<= 0xBF ) )
190 else if ( b
<= 0xF3 ) // F1..F3
192 for ( int i
= 0; i
< 3; ++i
)
195 if ( !(b
>= 0x80 && b
<= 0xBF ) )
199 else if ( b
== 0xF4 )
202 if ( !(b
>= 0x80 && b
<= 0x8F ) )
204 for ( int i
= 0; i
< 2; ++i
)
207 if ( !(b
>= 0x80 && b
<= 0xBF ) )
211 else // otherwise, it's invalid lead byte
220 bool wxString::IsValidUtf8LeadByte(unsigned char c
)
222 return (c
<= 0x7F) || (c
>= 0xC2 && c
<= 0xF4);
226 unsigned char wxString::ms_utf8IterTable
[256] = {
227 // single-byte sequences (ASCII):
228 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 00..0F
229 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 10..1F
230 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 20..2F
231 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 30..3F
232 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 40..4F
233 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 50..5F
234 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 60..6F
235 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 70..7F
237 // these are invalid, we use step 1 to skip
238 // over them (should never happen):
239 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 80..8F
240 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 90..9F
241 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // A0..AF
242 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // B0..BF
245 // two-byte sequences:
246 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // C2..CF
247 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, // D0..DF
249 // three-byte sequences:
250 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, // E0..EF
252 // four-byte sequences:
253 4, 4, 4, 4, 4, // F0..F4
255 // these are invalid again (5- or 6-byte
256 // sequences and sequences for code points
257 // above U+10FFFF, as restricted by RFC 3629):
258 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 // F5..FF
262 void wxString::DecIter(wxStringImpl::const_iterator
& i
)
264 wxASSERT( IsValidUtf8LeadByte(*i
) );
266 // Non-lead bytes are all in the 0x80..0xBF range (i.e. 10xxxxxx in
267 // binary), so we just have to go back until we hit a byte that is either
268 // < 0x80 (i.e. 0xxxxxxx in binary) or 0xC0..0xFF (11xxxxxx in binary; this
269 // includes some invalid values, but we can ignore it here, because we
270 // assume valid UTF-8 input for the purpose of efficient implementation).
272 while ( ((*i
) & 0xC0) == 0x80 /* 2 highest bits are '10' */ )
277 void wxString::DecIter(wxStringImpl::iterator
& i
)
279 // FIXME-UTF8: use template instead
280 wxASSERT( IsValidUtf8LeadByte(*i
) );
282 while ( ((*i
) & 0xC0) == 0x80 /* 2 highest bits are '10' */ )
287 wxStringImpl::const_iterator
288 wxString::AddToIter(wxStringImpl::const_iterator i
, int n
)
290 wxStringImpl::const_iterator
out(i
);
294 for ( int j
= 0; j
< n
; ++j
)
299 for ( int j
= 0; j
> n
; --j
)
306 wxStringImpl::iterator
307 wxString::AddToIter(wxStringImpl::iterator i
, int n
)
309 // FIXME-UTF8: use template instead
310 wxStringImpl::iterator
out(i
);
314 for ( int j
= 0; j
< n
; ++j
)
319 for ( int j
= 0; j
> n
; --j
)
328 int wxString::DiffIters(wxStringImpl::const_iterator i1
,
329 wxStringImpl::const_iterator i2
)
353 int wxString::DiffIters(wxStringImpl::iterator i1
, wxStringImpl::iterator i2
)
355 // FIXME-UTF8: use template instead
379 wxString::Utf8CharBuffer
wxString::EncodeChar(wxUniChar ch
)
382 char *out
= buf
.data
;
384 wxUniChar::value_type code
= ch
.GetValue();
386 // Char. number range | UTF-8 octet sequence
387 // (hexadecimal) | (binary)
388 // ----------------------+---------------------------------------------
389 // 0000 0000 - 0000 007F | 0xxxxxxx
390 // 0000 0080 - 0000 07FF | 110xxxxx 10xxxxxx
391 // 0000 0800 - 0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
392 // 0001 0000 - 0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
394 // Code point value is stored in bits marked with 'x', lowest-order bit
395 // of the value on the right side in the diagram above.
403 else if ( code
<= 0x07FF )
406 // NB: this line takes 6 least significant bits, encodes them as
407 // 10xxxxxx and discards them so that the next byte can be encoded:
408 out
[1] = 0x80 | (code
& 0x3F); code
>>= 6;
409 out
[0] = 0xC0 | code
;
411 else if ( code
< 0xFFFF )
414 out
[2] = 0x80 | (code
& 0x3F); code
>>= 6;
415 out
[1] = 0x80 | (code
& 0x3F); code
>>= 6;
416 out
[0] = 0xE0 | code
;
418 else if ( code
<= 0x10FFFF )
421 out
[3] = 0x80 | (code
& 0x3F); code
>>= 6;
422 out
[2] = 0x80 | (code
& 0x3F); code
>>= 6;
423 out
[1] = 0x80 | (code
& 0x3F); code
>>= 6;
424 out
[0] = 0xF0 | code
;
428 wxFAIL_MSG( _T("trying to encode undefined Unicode character") );
436 wxUniChar
wxUniCharRef::DecodeChar(wxStringImpl::const_iterator i
)
438 wxASSERT( wxString::IsValidUtf8LeadByte(*i
) ); // FIXME-UTF8: no "wxString::"
440 wxUniChar::value_type code
= 0;
441 size_t len
= wxString::GetUtf8CharLength(*i
);
442 wxASSERT_MSG( len
<= 4, _T("invalid UTF-8 sequence length") );
444 // Char. number range | UTF-8 octet sequence
445 // (hexadecimal) | (binary)
446 // ----------------------+---------------------------------------------
447 // 0000 0000 - 0000 007F | 0xxxxxxx
448 // 0000 0080 - 0000 07FF | 110xxxxx 10xxxxxx
449 // 0000 0800 - 0000 FFFF | 1110xxxx 10xxxxxx 10xxxxxx
450 // 0001 0000 - 0010 FFFF | 11110xxx 10xxxxxx 10xxxxxx 10xxxxxx
452 // Code point value is stored in bits marked with 'x', lowest-order bit
453 // of the value on the right side in the diagram above.
456 // mask to extract lead byte's value ('x' bits above), by sequence's length:
457 static const unsigned char s_leadValueMask
[4] = { 0x7F, 0x1F, 0x0F, 0x07 };
459 // mask and value of lead byte's most significant bits, by length:
460 static const unsigned char s_leadMarkerMask
[4] = { 0x80, 0xE0, 0xF0, 0xF8 };
461 static const unsigned char s_leadMarkerVal
[4] = { 0x00, 0xC0, 0xE0, 0xF0 };
464 // extract the lead byte's value bits:
465 wxASSERT_MSG( ((unsigned char)*i
& s_leadMarkerMask
[len
-1]) ==
466 s_leadMarkerVal
[len
-1],
467 _T("invalid UTF-8 lead byte") );
468 code
= (unsigned char)*i
& s_leadValueMask
[len
-1];
470 // all remaining bytes, if any, are handled in the same way regardless of
471 // sequence's length:
472 for ( ++i
; len
> 1; --len
, ++i
)
474 wxASSERT_MSG( ((unsigned char)*i
& 0xC0) == 0x80,
475 _T("invalid UTF-8 byte") );
478 code
|= (unsigned char)*i
& 0x3F;
481 return wxUniChar(code
);
485 wxCharBuffer
wxString::EncodeNChars(size_t n
, wxUniChar ch
)
487 Utf8CharBuffer
once(EncodeChar(ch
));
488 // the IncIter() table can be used to determine the length of ch's encoding:
489 size_t len
= ms_utf8IterTable
[(unsigned char)once
.data
[0]];
491 wxCharBuffer
buf(n
* len
);
492 char *ptr
= buf
.data();
493 for ( size_t i
= 0; i
< n
; i
++, ptr
+= len
)
495 memcpy(ptr
, once
.data
, len
);
502 void wxString::PosLenToImpl(size_t pos
, size_t len
,
503 size_t *implPos
, size_t *implLen
) const
509 const_iterator i
= begin() + pos
;
510 *implPos
= wxStringImpl::const_iterator(i
) - m_impl
.begin();
515 // too large length is interpreted as "to the end of the string"
516 // FIXME-UTF8: verify this is the case in std::string, assert
518 if ( pos
+ len
> length() )
519 len
= length() - pos
;
521 *implLen
= wxStringImpl::const_iterator(i
+ len
) -
522 wxStringImpl::const_iterator(i
);
527 #endif // wxUSE_UNICODE_UTF8
529 // ----------------------------------------------------------------------------
530 // wxCStrData converted strings caching
531 // ----------------------------------------------------------------------------
533 // FIXME-UTF8: temporarily disabled because it doesn't work with global
534 // string objects; re-enable after fixing this bug and benchmarking
535 // performance to see if using a hash is a good idea at all
538 // For backward compatibility reasons, it must be possible to assign the value
539 // returned by wxString::c_str() to a char* or wchar_t* variable and work with
540 // it. Returning wxCharBuffer from (const char*)c_str() wouldn't do the trick,
541 // because the memory would be freed immediately, but it has to be valid as long
542 // as the string is not modified, so that code like this still works:
544 // const wxChar *s = str.c_str();
545 // while ( s ) { ... }
547 // FIXME-UTF8: not thread safe!
548 // FIXME-UTF8: we currently clear the cached conversion only when the string is
549 // destroyed, but we should do it when the string is modified, to
550 // keep memory usage down
551 // FIXME-UTF8: we do the conversion every time As[W]Char() is called, but if we
552 // invalidated the cache on every change, we could keep the previous
554 // FIXME-UTF8: add tracing of usage of these two methods - new code is supposed
555 // to use mb_str() or wc_str() instead of (const [w]char*)c_str()
558 static inline void DeleteStringFromConversionCache(T
& hash
, const wxString
*s
)
560 typename
T::iterator i
= hash
.find(wxConstCast(s
, wxString
));
561 if ( i
!= hash
.end() )
569 // NB: non-STL implementation doesn't compile with "const wxString*" key type,
570 // so we have to use wxString* here and const-cast when used
571 WX_DECLARE_HASH_MAP(wxString
*, char*, wxPointerHash
, wxPointerEqual
,
572 wxStringCharConversionCache
);
573 static wxStringCharConversionCache gs_stringsCharCache
;
575 const char* wxCStrData::AsChar() const
577 // remove previously cache value, if any (see FIXMEs above):
578 DeleteStringFromConversionCache(gs_stringsCharCache
, m_str
);
580 // convert the string and keep it:
581 const char *s
= gs_stringsCharCache
[wxConstCast(m_str
, wxString
)] =
582 m_str
->mb_str().release();
586 #endif // wxUSE_UNICODE
588 #if !wxUSE_UNICODE_WCHAR
589 WX_DECLARE_HASH_MAP(wxString
*, wchar_t*, wxPointerHash
, wxPointerEqual
,
590 wxStringWCharConversionCache
);
591 static wxStringWCharConversionCache gs_stringsWCharCache
;
593 const wchar_t* wxCStrData::AsWChar() const
595 // remove previously cache value, if any (see FIXMEs above):
596 DeleteStringFromConversionCache(gs_stringsWCharCache
, m_str
);
598 // convert the string and keep it:
599 const wchar_t *s
= gs_stringsWCharCache
[wxConstCast(m_str
, wxString
)] =
600 m_str
->wc_str().release();
604 #endif // !wxUSE_UNICODE_WCHAR
606 wxString::~wxString()
609 // FIXME-UTF8: do this only if locale is not UTF8 if wxUSE_UNICODE_UTF8
610 DeleteStringFromConversionCache(gs_stringsCharCache
, this);
612 #if !wxUSE_UNICODE_WCHAR
613 DeleteStringFromConversionCache(gs_stringsWCharCache
, this);
619 const char* wxCStrData::AsChar() const
621 wxString
*str
= wxConstCast(m_str
, wxString
);
623 // convert the string:
624 wxCharBuffer
buf(str
->mb_str());
626 // FIXME-UTF8: do the conversion in-place in the existing buffer
627 if ( str
->m_convertedToChar
&&
628 strlen(buf
) == strlen(str
->m_convertedToChar
) )
630 // keep the same buffer for as long as possible, so that several calls
631 // to c_str() in a row still work:
632 strcpy(str
->m_convertedToChar
, buf
);
636 str
->m_convertedToChar
= buf
.release();
640 return str
->m_convertedToChar
+ m_offset
;
642 #endif // wxUSE_UNICODE
644 #if !wxUSE_UNICODE_WCHAR
645 const wchar_t* wxCStrData::AsWChar() const
647 wxString
*str
= wxConstCast(m_str
, wxString
);
649 // convert the string:
650 wxWCharBuffer
buf(str
->wc_str());
652 // FIXME-UTF8: do the conversion in-place in the existing buffer
653 if ( str
->m_convertedToWChar
&&
654 wxWcslen(buf
) == wxWcslen(str
->m_convertedToWChar
) )
656 // keep the same buffer for as long as possible, so that several calls
657 // to c_str() in a row still work:
658 memcpy(str
->m_convertedToWChar
, buf
, sizeof(wchar_t) * wxWcslen(buf
));
662 str
->m_convertedToWChar
= buf
.release();
666 return str
->m_convertedToWChar
+ m_offset
;
668 #endif // !wxUSE_UNICODE_WCHAR
670 // ===========================================================================
671 // wxString class core
672 // ===========================================================================
674 // ---------------------------------------------------------------------------
675 // construction and conversion
676 // ---------------------------------------------------------------------------
678 #if wxUSE_UNICODE_WCHAR
680 wxString::SubstrBufFromMB
wxString::ConvertStr(const char *psz
, size_t nLength
,
681 const wxMBConv
& conv
)
684 if ( !psz
|| nLength
== 0 )
685 return SubstrBufFromMB(L
"", 0);
687 if ( nLength
== npos
)
691 wxWCharBuffer
wcBuf(conv
.cMB2WC(psz
, nLength
, &wcLen
));
693 return SubstrBufFromMB(_T(""), 0);
695 return SubstrBufFromMB(wcBuf
, wcLen
);
697 #endif // wxUSE_UNICODE_WCHAR
699 #if wxUSE_UNICODE_UTF8
701 wxString::SubstrBufFromMB
wxString::ConvertStr(const char *psz
, size_t nLength
,
702 const wxMBConv
& conv
)
704 // FIXME-UTF8: return as-is without copying under UTF8 locale, return
705 // converted string under other locales - needs wxCharBuffer
709 if ( !psz
|| nLength
== 0 )
710 return SubstrBufFromMB("", 0);
712 if ( nLength
== npos
)
715 // first convert to wide string:
717 wxWCharBuffer
wcBuf(conv
.cMB2WC(psz
, nLength
, &wcLen
));
719 return SubstrBufFromMB("", 0);
721 // and then to UTF-8:
722 SubstrBufFromMB
buf(ConvertStr(wcBuf
, wcLen
, wxConvUTF8
));
723 // widechar -> UTF-8 conversion isn't supposed to ever fail:
724 wxASSERT_MSG( buf
.data
, _T("conversion to UTF-8 failed") );
728 #endif // wxUSE_UNICODE_UTF8
730 #if wxUSE_UNICODE_UTF8 || !wxUSE_UNICODE
732 wxString::SubstrBufFromWC
wxString::ConvertStr(const wchar_t *pwz
, size_t nLength
,
733 const wxMBConv
& conv
)
736 if ( !pwz
|| nLength
== 0 )
737 return SubstrBufFromWC("", 0);
739 if ( nLength
== npos
)
743 wxCharBuffer
mbBuf(conv
.cWC2MB(pwz
, nLength
, &mbLen
));
745 return SubstrBufFromWC("", 0);
747 return SubstrBufFromWC(mbBuf
, mbLen
);
749 #endif // wxUSE_UNICODE_UTF8 || !wxUSE_UNICODE
752 #if wxUSE_UNICODE_WCHAR
754 //Convert wxString in Unicode mode to a multi-byte string
755 const wxCharBuffer
wxString::mb_str(const wxMBConv
& conv
) const
757 return conv
.cWC2MB(wx_str(), length() + 1 /* size, not length */, NULL
);
760 #elif wxUSE_UNICODE_UTF8
762 const wxWCharBuffer
wxString::wc_str() const
764 return wxConvUTF8
.cMB2WC(m_impl
.c_str(),
765 m_impl
.length() + 1 /* size, not length */,
769 const wxCharBuffer
wxString::mb_str(const wxMBConv
& conv
) const
771 // FIXME-UTF8: optimize the case when conv==wxConvUTF8 or wxConvLibc
773 // FIXME-UTF8: use wc_str() here once we have buffers with length
777 wxConvUTF8
.cMB2WC(m_impl
.c_str(),
778 m_impl
.length() + 1 /* size, not length */,
781 return wxCharBuffer("");
783 return conv
.cWC2MB(wcBuf
, wcLen
, NULL
);
788 //Converts this string to a wide character string if unicode
789 //mode is not enabled and wxUSE_WCHAR_T is enabled
790 const wxWCharBuffer
wxString::wc_str(const wxMBConv
& conv
) const
792 return conv
.cMB2WC(wx_str(), length() + 1 /* size, not length */, NULL
);
795 #endif // Unicode/ANSI
797 // shrink to minimal size (releasing extra memory)
798 bool wxString::Shrink()
800 wxString
tmp(begin(), end());
802 return tmp
.length() == length();
805 // deprecated compatibility code:
806 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
807 wxChar
*wxString::GetWriteBuf(size_t nLen
)
809 return DoGetWriteBuf(nLen
);
812 void wxString::UngetWriteBuf()
817 void wxString::UngetWriteBuf(size_t nLen
)
819 DoUngetWriteBuf(nLen
);
821 #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
824 // ---------------------------------------------------------------------------
826 // ---------------------------------------------------------------------------
828 // all functions are inline in string.h
830 // ---------------------------------------------------------------------------
831 // concatenation operators
832 // ---------------------------------------------------------------------------
835 * concatenation functions come in 5 flavours:
837 * char + string and string + char
838 * C str + string and string + C str
841 wxString
operator+(const wxString
& str1
, const wxString
& str2
)
843 #if !wxUSE_STL_BASED_WXSTRING
844 wxASSERT( str1
.IsValid() );
845 wxASSERT( str2
.IsValid() );
854 wxString
operator+(const wxString
& str
, wxUniChar ch
)
856 #if !wxUSE_STL_BASED_WXSTRING
857 wxASSERT( str
.IsValid() );
866 wxString
operator+(wxUniChar ch
, const wxString
& str
)
868 #if !wxUSE_STL_BASED_WXSTRING
869 wxASSERT( str
.IsValid() );
878 wxString
operator+(const wxString
& str
, const char *psz
)
880 #if !wxUSE_STL_BASED_WXSTRING
881 wxASSERT( str
.IsValid() );
885 if ( !s
.Alloc(strlen(psz
) + str
.length()) ) {
886 wxFAIL_MSG( _T("out of memory in wxString::operator+") );
894 wxString
operator+(const wxString
& str
, const wchar_t *pwz
)
896 #if !wxUSE_STL_BASED_WXSTRING
897 wxASSERT( str
.IsValid() );
901 if ( !s
.Alloc(wxWcslen(pwz
) + str
.length()) ) {
902 wxFAIL_MSG( _T("out of memory in wxString::operator+") );
910 wxString
operator+(const char *psz
, const wxString
& str
)
912 #if !wxUSE_STL_BASED_WXSTRING
913 wxASSERT( str
.IsValid() );
917 if ( !s
.Alloc(strlen(psz
) + str
.length()) ) {
918 wxFAIL_MSG( _T("out of memory in wxString::operator+") );
926 wxString
operator+(const wchar_t *pwz
, const wxString
& str
)
928 #if !wxUSE_STL_BASED_WXSTRING
929 wxASSERT( str
.IsValid() );
933 if ( !s
.Alloc(wxWcslen(pwz
) + str
.length()) ) {
934 wxFAIL_MSG( _T("out of memory in wxString::operator+") );
942 // ---------------------------------------------------------------------------
944 // ---------------------------------------------------------------------------
946 #ifdef HAVE_STD_STRING_COMPARE
948 // NB: Comparison code (both if HAVE_STD_STRING_COMPARE and if not) works with
949 // UTF-8 encoded strings too, thanks to UTF-8's design which allows us to
950 // sort strings in characters code point order by sorting the byte sequence
951 // in byte values order (i.e. what strcmp() and memcmp() do).
953 int wxString::compare(const wxString
& str
) const
955 return m_impl
.compare(str
.m_impl
);
958 int wxString::compare(size_t nStart
, size_t nLen
,
959 const wxString
& str
) const
962 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
963 return m_impl
.compare(pos
, len
, str
.m_impl
);
966 int wxString::compare(size_t nStart
, size_t nLen
,
968 size_t nStart2
, size_t nLen2
) const
971 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
974 str
.PosLenToImpl(nStart2
, nLen2
, &pos2
, &len2
);
976 return m_impl
.compare(pos
, len
, str
.m_impl
, pos2
, len2
);
979 int wxString::compare(const char* sz
) const
981 return m_impl
.compare(ImplStr(sz
));
984 int wxString::compare(const wchar_t* sz
) const
986 return m_impl
.compare(ImplStr(sz
));
989 int wxString::compare(size_t nStart
, size_t nLen
,
990 const char* sz
, size_t nCount
) const
993 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
995 SubstrBufFromMB
str(ImplStr(sz
, nCount
));
997 return m_impl
.compare(pos
, len
, str
.data
, str
.len
);
1000 int wxString::compare(size_t nStart
, size_t nLen
,
1001 const wchar_t* sz
, size_t nCount
) const
1004 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
1006 SubstrBufFromWC
str(ImplStr(sz
, nCount
));
1008 return m_impl
.compare(pos
, len
, str
.data
, str
.len
);
1011 #else // !HAVE_STD_STRING_COMPARE
1013 static inline int wxDoCmp(const wxStringCharType
* s1
, size_t l1
,
1014 const wxStringCharType
* s2
, size_t l2
)
1017 return wxStringMemcmp(s1
, s2
, l1
);
1020 int ret
= wxStringMemcmp(s1
, s2
, l1
);
1021 return ret
== 0 ? -1 : ret
;
1025 int ret
= wxStringMemcmp(s1
, s2
, l2
);
1026 return ret
== 0 ? +1 : ret
;
1030 int wxString::compare(const wxString
& str
) const
1032 return ::wxDoCmp(m_impl
.data(), m_impl
.length(),
1033 str
.m_impl
.data(), str
.m_impl
.length());
1036 int wxString::compare(size_t nStart
, size_t nLen
,
1037 const wxString
& str
) const
1039 wxASSERT(nStart
<= length());
1040 size_type strLen
= length() - nStart
;
1041 nLen
= strLen
< nLen
? strLen
: nLen
;
1044 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
1046 return ::wxDoCmp(m_impl
.data() + pos
, len
,
1047 str
.m_impl
.data(), str
.m_impl
.length());
1050 int wxString::compare(size_t nStart
, size_t nLen
,
1051 const wxString
& str
,
1052 size_t nStart2
, size_t nLen2
) const
1054 wxASSERT(nStart
<= length());
1055 wxASSERT(nStart2
<= str
.length());
1056 size_type strLen
= length() - nStart
,
1057 strLen2
= str
.length() - nStart2
;
1058 nLen
= strLen
< nLen
? strLen
: nLen
;
1059 nLen2
= strLen2
< nLen2
? strLen2
: nLen2
;
1062 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
1064 str
.PosLenToImpl(nStart2
, nLen2
, &pos2
, &len2
);
1066 return ::wxDoCmp(m_impl
.data() + pos
, len
,
1067 str
.m_impl
.data() + pos2
, len2
);
1070 int wxString::compare(const char* sz
) const
1072 SubstrBufFromMB
str(ImplStr(sz
, npos
));
1073 if ( str
.len
== npos
)
1074 str
.len
= wxStringStrlen(str
.data
);
1075 return ::wxDoCmp(m_impl
.data(), m_impl
.length(), str
.data
, str
.len
);
1078 int wxString::compare(const wchar_t* sz
) const
1080 SubstrBufFromWC
str(ImplStr(sz
, npos
));
1081 if ( str
.len
== npos
)
1082 str
.len
= wxStringStrlen(str
.data
);
1083 return ::wxDoCmp(m_impl
.data(), m_impl
.length(), str
.data
, str
.len
);
1086 int wxString::compare(size_t nStart
, size_t nLen
,
1087 const char* sz
, size_t nCount
) const
1089 wxASSERT(nStart
<= length());
1090 size_type strLen
= length() - nStart
;
1091 nLen
= strLen
< nLen
? strLen
: nLen
;
1094 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
1096 SubstrBufFromMB
str(ImplStr(sz
, nCount
));
1097 if ( str
.len
== npos
)
1098 str
.len
= wxStringStrlen(str
.data
);
1100 return ::wxDoCmp(m_impl
.data() + pos
, len
, str
.data
, str
.len
);
1103 int wxString::compare(size_t nStart
, size_t nLen
,
1104 const wchar_t* sz
, size_t nCount
) const
1106 wxASSERT(nStart
<= length());
1107 size_type strLen
= length() - nStart
;
1108 nLen
= strLen
< nLen
? strLen
: nLen
;
1111 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
1113 SubstrBufFromWC
str(ImplStr(sz
, nCount
));
1114 if ( str
.len
== npos
)
1115 str
.len
= wxStringStrlen(str
.data
);
1117 return ::wxDoCmp(m_impl
.data() + pos
, len
, str
.data
, str
.len
);
1120 #endif // HAVE_STD_STRING_COMPARE/!HAVE_STD_STRING_COMPARE
1123 // ---------------------------------------------------------------------------
1124 // find_{first,last}_[not]_of functions
1125 // ---------------------------------------------------------------------------
1127 #if !wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
1129 // NB: All these functions are implemented with the argument being wxChar*,
1130 // i.e. widechar string in any Unicode build, even though native string
1131 // representation is char* in the UTF-8 build. This is because we couldn't
1132 // use memchr() to determine if a character is in a set encoded as UTF-8.
1134 size_t wxString::find_first_of(const wxChar
* sz
, size_t nStart
) const
1136 return find_first_of(sz
, nStart
, wxStrlen(sz
));
1139 size_t wxString::find_first_not_of(const wxChar
* sz
, size_t nStart
) const
1141 return find_first_not_of(sz
, nStart
, wxStrlen(sz
));
1144 size_t wxString::find_first_of(const wxChar
* sz
, size_t nStart
, size_t n
) const
1146 wxASSERT_MSG( nStart
<= length(), _T("invalid index") );
1148 size_t idx
= nStart
;
1149 for ( const_iterator i
= begin() + nStart
; i
!= end(); ++idx
, ++i
)
1151 if ( wxTmemchr(sz
, *i
, n
) )
1158 size_t wxString::find_first_not_of(const wxChar
* sz
, size_t nStart
, size_t n
) const
1160 wxASSERT_MSG( nStart
<= length(), _T("invalid index") );
1162 size_t idx
= nStart
;
1163 for ( const_iterator i
= begin() + nStart
; i
!= end(); ++idx
, ++i
)
1165 if ( !wxTmemchr(sz
, *i
, n
) )
1173 size_t wxString::find_last_of(const wxChar
* sz
, size_t nStart
) const
1175 return find_last_of(sz
, nStart
, wxStrlen(sz
));
1178 size_t wxString::find_last_not_of(const wxChar
* sz
, size_t nStart
) const
1180 return find_last_not_of(sz
, nStart
, wxStrlen(sz
));
1183 size_t wxString::find_last_of(const wxChar
* sz
, size_t nStart
, size_t n
) const
1185 size_t len
= length();
1187 if ( nStart
== npos
)
1193 wxASSERT_MSG( nStart
<= len
, _T("invalid index") );
1196 size_t idx
= nStart
;
1197 for ( const_reverse_iterator i
= rbegin() + (len
- nStart
- 1);
1198 i
!= rend(); --idx
, ++i
)
1200 if ( wxTmemchr(sz
, *i
, n
) )
1207 size_t wxString::find_last_not_of(const wxChar
* sz
, size_t nStart
, size_t n
) const
1209 size_t len
= length();
1211 if ( nStart
== npos
)
1217 wxASSERT_MSG( nStart
<= len
, _T("invalid index") );
1220 size_t idx
= nStart
;
1221 for ( const_reverse_iterator i
= rbegin() + (len
- nStart
- 1);
1222 i
!= rend(); --idx
, ++i
)
1224 if ( !wxTmemchr(sz
, *i
, n
) )
1231 size_t wxString::find_first_not_of(wxUniChar ch
, size_t nStart
) const
1233 wxASSERT_MSG( nStart
<= length(), _T("invalid index") );
1235 size_t idx
= nStart
;
1236 for ( const_iterator i
= begin() + nStart
; i
!= end(); ++idx
, ++i
)
1245 size_t wxString::find_last_not_of(wxUniChar ch
, size_t nStart
) const
1247 size_t len
= length();
1249 if ( nStart
== npos
)
1255 wxASSERT_MSG( nStart
<= len
, _T("invalid index") );
1258 size_t idx
= nStart
;
1259 for ( const_reverse_iterator i
= rbegin() + (len
- nStart
- 1);
1260 i
!= rend(); --idx
, ++i
)
1269 // the functions above were implemented for wchar_t* arguments in Unicode
1270 // build and char* in ANSI build; below are implementations for the other
1273 #define wxOtherCharType char
1274 #define STRCONV (const wxChar*)wxConvLibc.cMB2WC
1276 #define wxOtherCharType wchar_t
1277 #define STRCONV (const wxChar*)wxConvLibc.cWC2MB
1280 size_t wxString::find_first_of(const wxOtherCharType
* sz
, size_t nStart
) const
1281 { return find_first_of(STRCONV(sz
), nStart
); }
1283 size_t wxString::find_first_of(const wxOtherCharType
* sz
, size_t nStart
,
1285 { return find_first_of(STRCONV(sz
, n
, NULL
), nStart
, n
); }
1286 size_t wxString::find_last_of(const wxOtherCharType
* sz
, size_t nStart
) const
1287 { return find_last_of(STRCONV(sz
), nStart
); }
1288 size_t wxString::find_last_of(const wxOtherCharType
* sz
, size_t nStart
,
1290 { return find_last_of(STRCONV(sz
, n
, NULL
), nStart
, n
); }
1291 size_t wxString::find_first_not_of(const wxOtherCharType
* sz
, size_t nStart
) const
1292 { return find_first_not_of(STRCONV(sz
), nStart
); }
1293 size_t wxString::find_first_not_of(const wxOtherCharType
* sz
, size_t nStart
,
1295 { return find_first_not_of(STRCONV(sz
, n
, NULL
), nStart
, n
); }
1296 size_t wxString::find_last_not_of(const wxOtherCharType
* sz
, size_t nStart
) const
1297 { return find_last_not_of(STRCONV(sz
), nStart
); }
1298 size_t wxString::find_last_not_of(const wxOtherCharType
* sz
, size_t nStart
,
1300 { return find_last_not_of(STRCONV(sz
, n
, NULL
), nStart
, n
); }
1302 #undef wxOtherCharType
1305 #endif // !wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
1307 // ===========================================================================
1308 // other common string functions
1309 // ===========================================================================
1311 int wxString::CmpNoCase(const wxString
& s
) const
1313 // FIXME-UTF8: use wxUniChar::ToLower/ToUpper once added
1316 const_iterator i1
= begin();
1317 const_iterator end1
= end();
1318 const_iterator i2
= s
.begin();
1319 const_iterator end2
= s
.end();
1321 for ( ; i1
!= end1
&& i2
!= end2
; ++idx
, ++i1
, ++i2
)
1323 wxUniChar lower1
= (wxChar
)wxTolower(*i1
);
1324 wxUniChar lower2
= (wxChar
)wxTolower(*i2
);
1325 if ( lower1
!= lower2
)
1326 return lower1
< lower2
? -1 : 1;
1329 size_t len1
= length();
1330 size_t len2
= s
.length();
1334 else if ( len1
> len2
)
1343 #ifndef __SCHAR_MAX__
1344 #define __SCHAR_MAX__ 127
1348 wxString
wxString::FromAscii(const char *ascii
)
1351 return wxEmptyString
;
1353 size_t len
= strlen( ascii
);
1358 wxStringBuffer
buf(res
, len
);
1360 wchar_t *dest
= buf
;
1364 if ( (*dest
++ = (wchar_t)(unsigned char)*ascii
++) == L
'\0' )
1372 wxString
wxString::FromAscii(const char ascii
)
1374 // What do we do with '\0' ?
1377 res
+= (wchar_t)(unsigned char) ascii
;
1382 const wxCharBuffer
wxString::ToAscii() const
1384 // this will allocate enough space for the terminating NUL too
1385 wxCharBuffer
buffer(length());
1388 char *dest
= buffer
.data();
1390 const wchar_t *pwc
= c_str();
1393 *dest
++ = (char)(*pwc
> SCHAR_MAX
? wxT('_') : *pwc
);
1395 // the output string can't have embedded NULs anyhow, so we can safely
1396 // stop at first of them even if we do have any
1406 // extract string of length nCount starting at nFirst
1407 wxString
wxString::Mid(size_t nFirst
, size_t nCount
) const
1409 size_t nLen
= length();
1411 // default value of nCount is npos and means "till the end"
1412 if ( nCount
== npos
)
1414 nCount
= nLen
- nFirst
;
1417 // out-of-bounds requests return sensible things
1418 if ( nFirst
+ nCount
> nLen
)
1420 nCount
= nLen
- nFirst
;
1423 if ( nFirst
> nLen
)
1425 // AllocCopy() will return empty string
1426 return wxEmptyString
;
1429 wxString
dest(*this, nFirst
, nCount
);
1430 if ( dest
.length() != nCount
)
1432 wxFAIL_MSG( _T("out of memory in wxString::Mid") );
1438 // check that the string starts with prefix and return the rest of the string
1439 // in the provided pointer if it is not NULL, otherwise return false
1440 bool wxString::StartsWith(const wxChar
*prefix
, wxString
*rest
) const
1442 wxASSERT_MSG( prefix
, _T("invalid parameter in wxString::StartsWith") );
1444 // first check if the beginning of the string matches the prefix: note
1445 // that we don't have to check that we don't run out of this string as
1446 // when we reach the terminating NUL, either prefix string ends too (and
1447 // then it's ok) or we break out of the loop because there is no match
1448 const wxChar
*p
= c_str();
1451 if ( *prefix
++ != *p
++ )
1460 // put the rest of the string into provided pointer
1468 // check that the string ends with suffix and return the rest of it in the
1469 // provided pointer if it is not NULL, otherwise return false
1470 bool wxString::EndsWith(const wxChar
*suffix
, wxString
*rest
) const
1472 wxASSERT_MSG( suffix
, _T("invalid parameter in wxString::EndssWith") );
1474 int start
= length() - wxStrlen(suffix
);
1476 if ( start
< 0 || compare(start
, npos
, suffix
) != 0 )
1481 // put the rest of the string into provided pointer
1482 rest
->assign(*this, 0, start
);
1489 // extract nCount last (rightmost) characters
1490 wxString
wxString::Right(size_t nCount
) const
1492 if ( nCount
> length() )
1495 wxString
dest(*this, length() - nCount
, nCount
);
1496 if ( dest
.length() != nCount
) {
1497 wxFAIL_MSG( _T("out of memory in wxString::Right") );
1502 // get all characters after the last occurence of ch
1503 // (returns the whole string if ch not found)
1504 wxString
wxString::AfterLast(wxUniChar ch
) const
1507 int iPos
= Find(ch
, true);
1508 if ( iPos
== wxNOT_FOUND
)
1511 str
= wx_str() + iPos
+ 1;
1516 // extract nCount first (leftmost) characters
1517 wxString
wxString::Left(size_t nCount
) const
1519 if ( nCount
> length() )
1522 wxString
dest(*this, 0, nCount
);
1523 if ( dest
.length() != nCount
) {
1524 wxFAIL_MSG( _T("out of memory in wxString::Left") );
1529 // get all characters before the first occurence of ch
1530 // (returns the whole string if ch not found)
1531 wxString
wxString::BeforeFirst(wxUniChar ch
) const
1533 int iPos
= Find(ch
);
1534 if ( iPos
== wxNOT_FOUND
) iPos
= length();
1535 return wxString(*this, 0, iPos
);
1538 /// get all characters before the last occurence of ch
1539 /// (returns empty string if ch not found)
1540 wxString
wxString::BeforeLast(wxUniChar ch
) const
1543 int iPos
= Find(ch
, true);
1544 if ( iPos
!= wxNOT_FOUND
&& iPos
!= 0 )
1545 str
= wxString(c_str(), iPos
);
1550 /// get all characters after the first occurence of ch
1551 /// (returns empty string if ch not found)
1552 wxString
wxString::AfterFirst(wxUniChar ch
) const
1555 int iPos
= Find(ch
);
1556 if ( iPos
!= wxNOT_FOUND
)
1557 str
= wx_str() + iPos
+ 1;
1562 // replace first (or all) occurences of some substring with another one
1563 size_t wxString::Replace(const wxString
& strOld
,
1564 const wxString
& strNew
, bool bReplaceAll
)
1566 // if we tried to replace an empty string we'd enter an infinite loop below
1567 wxCHECK_MSG( !strOld
.empty(), 0,
1568 _T("wxString::Replace(): invalid parameter") );
1570 size_t uiCount
= 0; // count of replacements made
1572 size_t uiOldLen
= strOld
.length();
1573 size_t uiNewLen
= strNew
.length();
1577 while ( (*this)[dwPos
] != wxT('\0') )
1579 //DO NOT USE STRSTR HERE
1580 //this string can contain embedded null characters,
1581 //so strstr will function incorrectly
1582 dwPos
= find(strOld
, dwPos
);
1583 if ( dwPos
== npos
)
1584 break; // exit the loop
1587 //replace this occurance of the old string with the new one
1588 replace(dwPos
, uiOldLen
, strNew
, uiNewLen
);
1590 //move up pos past the string that was replaced
1593 //increase replace count
1598 break; // exit the loop
1605 bool wxString::IsAscii() const
1607 for ( const_iterator i
= begin(); i
!= end(); ++i
)
1609 if ( !(*i
).IsAscii() )
1616 bool wxString::IsWord() const
1618 for ( const_iterator i
= begin(); i
!= end(); ++i
)
1620 if ( !wxIsalpha(*i
) )
1627 bool wxString::IsNumber() const
1632 const_iterator i
= begin();
1634 if ( *i
== _T('-') || *i
== _T('+') )
1637 for ( ; i
!= end(); ++i
)
1639 if ( !wxIsdigit(*i
) )
1646 wxString
wxString::Strip(stripType w
) const
1649 if ( w
& leading
) s
.Trim(false);
1650 if ( w
& trailing
) s
.Trim(true);
1654 // ---------------------------------------------------------------------------
1656 // ---------------------------------------------------------------------------
1658 wxString
& wxString::MakeUpper()
1660 for ( iterator it
= begin(), en
= end(); it
!= en
; ++it
)
1661 *it
= (wxChar
)wxToupper(*it
);
1666 wxString
& wxString::MakeLower()
1668 for ( iterator it
= begin(), en
= end(); it
!= en
; ++it
)
1669 *it
= (wxChar
)wxTolower(*it
);
1674 // ---------------------------------------------------------------------------
1675 // trimming and padding
1676 // ---------------------------------------------------------------------------
1678 // some compilers (VC++ 6.0 not to name them) return true for a call to
1679 // isspace('ê') in the C locale which seems to be broken to me, but we have to
1680 // live with this by checking that the character is a 7 bit one - even if this
1681 // may fail to detect some spaces (I don't know if Unicode doesn't have
1682 // space-like symbols somewhere except in the first 128 chars), it is arguably
1683 // still better than trimming away accented letters
1684 inline int wxSafeIsspace(wxChar ch
) { return (ch
< 127) && wxIsspace(ch
); }
1686 // trims spaces (in the sense of isspace) from left or right side
1687 wxString
& wxString::Trim(bool bFromRight
)
1689 // first check if we're going to modify the string at all
1692 (bFromRight
&& wxSafeIsspace(GetChar(length() - 1))) ||
1693 (!bFromRight
&& wxSafeIsspace(GetChar(0u)))
1699 // find last non-space character
1700 reverse_iterator psz
= rbegin();
1701 while ( (psz
!= rend()) && wxSafeIsspace(*psz
) )
1704 // truncate at trailing space start
1705 erase(psz
.base(), end());
1709 // find first non-space character
1710 iterator psz
= begin();
1711 while ( (psz
!= end()) && wxSafeIsspace(*psz
) )
1714 // fix up data and length
1715 erase(begin(), psz
);
1722 // adds nCount characters chPad to the string from either side
1723 wxString
& wxString::Pad(size_t nCount
, wxUniChar chPad
, bool bFromRight
)
1725 wxString
s(chPad
, nCount
);
1738 // truncate the string
1739 wxString
& wxString::Truncate(size_t uiLen
)
1741 if ( uiLen
< length() )
1743 erase(begin() + uiLen
, end());
1745 //else: nothing to do, string is already short enough
1750 // ---------------------------------------------------------------------------
1751 // finding (return wxNOT_FOUND if not found and index otherwise)
1752 // ---------------------------------------------------------------------------
1755 int wxString::Find(wxUniChar ch
, bool bFromEnd
) const
1757 size_type idx
= bFromEnd
? find_last_of(ch
) : find_first_of(ch
);
1759 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
1762 // ----------------------------------------------------------------------------
1763 // conversion to numbers
1764 // ----------------------------------------------------------------------------
1766 // the implementation of all the functions below is exactly the same so factor
1769 template <typename T
, typename F
>
1770 bool wxStringToIntType(const wxChar
*start
,
1775 wxCHECK_MSG( val
, false, _T("NULL output pointer") );
1776 wxASSERT_MSG( !base
|| (base
> 1 && base
<= 36), _T("invalid base") );
1783 *val
= (*func
)(start
, &end
, base
);
1785 // return true only if scan was stopped by the terminating NUL and if the
1786 // string was not empty to start with and no under/overflow occurred
1787 return !*end
&& (end
!= start
)
1789 && (errno
!= ERANGE
)
1794 bool wxString::ToLong(long *val
, int base
) const
1796 return wxStringToIntType((const wxChar
*)c_str(), val
, base
, wxStrtol
);
1799 bool wxString::ToULong(unsigned long *val
, int base
) const
1801 return wxStringToIntType((const wxChar
*)c_str(), val
, base
, wxStrtoul
);
1804 bool wxString::ToLongLong(wxLongLong_t
*val
, int base
) const
1806 return wxStringToIntType((const wxChar
*)c_str(), val
, base
, wxStrtoll
);
1809 bool wxString::ToULongLong(wxULongLong_t
*val
, int base
) const
1811 return wxStringToIntType((const wxChar
*)c_str(), val
, base
, wxStrtoull
);
1814 bool wxString::ToDouble(double *val
) const
1816 wxCHECK_MSG( val
, false, _T("NULL pointer in wxString::ToDouble") );
1822 const wxChar
*start
= c_str();
1824 *val
= wxStrtod(start
, &end
);
1826 // return true only if scan was stopped by the terminating NUL and if the
1827 // string was not empty to start with and no under/overflow occurred
1828 return !*end
&& (end
!= start
)
1830 && (errno
!= ERANGE
)
1835 // ---------------------------------------------------------------------------
1837 // ---------------------------------------------------------------------------
1840 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
1841 wxString
wxStringPrintfMixinBase::DoFormat(const wxChar
*format
, ...)
1843 wxString
wxString::DoFormat(const wxChar
*format
, ...)
1847 va_start(argptr
, format
);
1850 s
.PrintfV(format
, argptr
);
1858 wxString
wxString::FormatV(const wxString
& format
, va_list argptr
)
1861 s
.PrintfV(format
, argptr
);
1865 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
1866 int wxStringPrintfMixinBase::DoPrintf(const wxChar
*format
, ...)
1868 int wxString::DoPrintf(const wxChar
*format
, ...)
1872 va_start(argptr
, format
);
1874 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
1875 // get a pointer to the wxString instance; we have to use dynamic_cast<>
1876 // because it's the only cast that works safely for downcasting when
1877 // multiple inheritance is used:
1878 wxString
*str
= static_cast<wxString
*>(this);
1880 wxString
*str
= this;
1883 int iLen
= str
->PrintfV(format
, argptr
);
1890 int wxString::PrintfV(const wxString
& format
, va_list argptr
)
1896 wxStringBuffer
tmp(*this, size
+ 1);
1905 // wxVsnprintf() may modify the original arg pointer, so pass it
1908 wxVaCopy(argptrcopy
, argptr
);
1909 int len
= wxVsnprintf(buf
, size
, (const wxChar
*)/*FIXME-UTF8*/format
, argptrcopy
);
1912 // some implementations of vsnprintf() don't NUL terminate
1913 // the string if there is not enough space for it so
1914 // always do it manually
1915 buf
[size
] = _T('\0');
1917 // vsnprintf() may return either -1 (traditional Unix behaviour) or the
1918 // total number of characters which would have been written if the
1919 // buffer were large enough (newer standards such as Unix98)
1922 #if wxUSE_WXVSNPRINTF
1923 // we know that our own implementation of wxVsnprintf() returns -1
1924 // only for a format error - thus there's something wrong with
1925 // the user's format string
1927 #else // assume that system version only returns error if not enough space
1928 // still not enough, as we don't know how much we need, double the
1929 // current size of the buffer
1931 #endif // wxUSE_WXVSNPRINTF/!wxUSE_WXVSNPRINTF
1933 else if ( len
>= size
)
1935 #if wxUSE_WXVSNPRINTF
1936 // we know that our own implementation of wxVsnprintf() returns
1937 // size+1 when there's not enough space but that's not the size
1938 // of the required buffer!
1939 size
*= 2; // so we just double the current size of the buffer
1941 // some vsnprintf() implementations NUL-terminate the buffer and
1942 // some don't in len == size case, to be safe always add 1
1946 else // ok, there was enough space
1952 // we could have overshot
1958 // ----------------------------------------------------------------------------
1959 // misc other operations
1960 // ----------------------------------------------------------------------------
1962 // returns true if the string matches the pattern which may contain '*' and
1963 // '?' metacharacters (as usual, '?' matches any character and '*' any number
1965 bool wxString::Matches(const wxString
& mask
) const
1967 // I disable this code as it doesn't seem to be faster (in fact, it seems
1968 // to be much slower) than the old, hand-written code below and using it
1969 // here requires always linking with libregex even if the user code doesn't
1971 #if 0 // wxUSE_REGEX
1972 // first translate the shell-like mask into a regex
1974 pattern
.reserve(wxStrlen(pszMask
));
1986 pattern
+= _T(".*");
1997 // these characters are special in a RE, quote them
1998 // (however note that we don't quote '[' and ']' to allow
1999 // using them for Unix shell like matching)
2000 pattern
+= _T('\\');
2004 pattern
+= *pszMask
;
2012 return wxRegEx(pattern
, wxRE_NOSUB
| wxRE_EXTENDED
).Matches(c_str());
2013 #else // !wxUSE_REGEX
2014 // TODO: this is, of course, awfully inefficient...
2016 // FIXME-UTF8: implement using iterators, remove #if
2017 #if wxUSE_UNICODE_UTF8
2018 wxWCharBuffer maskBuf
= mask
.wc_str();
2019 wxWCharBuffer txtBuf
= wc_str();
2020 const wxChar
*pszMask
= maskBuf
.data();
2021 const wxChar
*pszTxt
= txtBuf
.data();
2023 const wxChar
*pszMask
= mask
.wx_str();
2024 // the char currently being checked
2025 const wxChar
*pszTxt
= wx_str();
2028 // the last location where '*' matched
2029 const wxChar
*pszLastStarInText
= NULL
;
2030 const wxChar
*pszLastStarInMask
= NULL
;
2033 for ( ; *pszMask
!= wxT('\0'); pszMask
++, pszTxt
++ ) {
2034 switch ( *pszMask
) {
2036 if ( *pszTxt
== wxT('\0') )
2039 // pszTxt and pszMask will be incremented in the loop statement
2045 // remember where we started to be able to backtrack later
2046 pszLastStarInText
= pszTxt
;
2047 pszLastStarInMask
= pszMask
;
2049 // ignore special chars immediately following this one
2050 // (should this be an error?)
2051 while ( *pszMask
== wxT('*') || *pszMask
== wxT('?') )
2054 // if there is nothing more, match
2055 if ( *pszMask
== wxT('\0') )
2058 // are there any other metacharacters in the mask?
2060 const wxChar
*pEndMask
= wxStrpbrk(pszMask
, wxT("*?"));
2062 if ( pEndMask
!= NULL
) {
2063 // we have to match the string between two metachars
2064 uiLenMask
= pEndMask
- pszMask
;
2067 // we have to match the remainder of the string
2068 uiLenMask
= wxStrlen(pszMask
);
2071 wxString
strToMatch(pszMask
, uiLenMask
);
2072 const wxChar
* pMatch
= wxStrstr(pszTxt
, strToMatch
);
2073 if ( pMatch
== NULL
)
2076 // -1 to compensate "++" in the loop
2077 pszTxt
= pMatch
+ uiLenMask
- 1;
2078 pszMask
+= uiLenMask
- 1;
2083 if ( *pszMask
!= *pszTxt
)
2089 // match only if nothing left
2090 if ( *pszTxt
== wxT('\0') )
2093 // if we failed to match, backtrack if we can
2094 if ( pszLastStarInText
) {
2095 pszTxt
= pszLastStarInText
+ 1;
2096 pszMask
= pszLastStarInMask
;
2098 pszLastStarInText
= NULL
;
2100 // don't bother resetting pszLastStarInMask, it's unnecessary
2106 #endif // wxUSE_REGEX/!wxUSE_REGEX
2109 // Count the number of chars
2110 int wxString::Freq(wxUniChar ch
) const
2113 for ( const_iterator i
= begin(); i
!= end(); ++i
)
2121 // convert to upper case, return the copy of the string
2122 wxString
wxString::Upper() const
2123 { wxString
s(*this); return s
.MakeUpper(); }
2125 // convert to lower case, return the copy of the string
2126 wxString
wxString::Lower() const { wxString
s(*this); return s
.MakeLower(); }