]>
git.saurik.com Git - wxWidgets.git/blob - src/common/string.cpp
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"
49 // string handling functions used by wxString:
50 #if wxUSE_UNICODE_UTF8
51 #define wxStringMemcpy memcpy
52 #define wxStringMemcmp memcmp
53 #define wxStringMemchr memchr
54 #define wxStringStrlen strlen
56 #define wxStringMemcpy wxTmemcpy
57 #define wxStringMemcmp wxTmemcmp
58 #define wxStringMemchr wxTmemchr
59 #define wxStringStrlen wxStrlen
63 // ---------------------------------------------------------------------------
64 // static class variables definition
65 // ---------------------------------------------------------------------------
67 //According to STL _must_ be a -1 size_t
68 const size_t wxString::npos
= (size_t) -1;
70 // ----------------------------------------------------------------------------
72 // ----------------------------------------------------------------------------
74 #if wxUSE_STD_IOSTREAM
78 wxSTD ostream
& operator<<(wxSTD ostream
& os
, const wxCStrData
& str
)
80 // FIXME-UTF8: always, not only if wxUSE_UNICODE
81 #if wxUSE_UNICODE && !defined(__BORLANDC__)
82 return os
<< str
.AsWChar();
84 return os
<< str
.AsChar();
88 wxSTD ostream
& operator<<(wxSTD ostream
& os
, const wxString
& str
)
90 return os
<< str
.c_str();
93 wxSTD ostream
& operator<<(wxSTD ostream
& os
, const wxCharBuffer
& str
)
95 return os
<< str
.data();
99 wxSTD ostream
& operator<<(wxSTD ostream
& os
, const wxWCharBuffer
& str
)
101 return os
<< str
.data();
105 #endif // wxUSE_STD_IOSTREAM
107 // ===========================================================================
108 // wxString class core
109 // ===========================================================================
111 // ---------------------------------------------------------------------------
112 // construction and conversion
113 // ---------------------------------------------------------------------------
117 wxString::SubstrBufFromMB
wxString::ConvertStr(const char *psz
, size_t nLength
,
118 const wxMBConv
& conv
)
121 if ( !psz
|| nLength
== 0 )
122 return SubstrBufFromMB();
124 if ( nLength
== npos
)
128 wxWCharBuffer
wcBuf(conv
.cMB2WC(psz
, nLength
, &wcLen
));
130 return SubstrBufFromMB();
132 return SubstrBufFromMB(wcBuf
, wcLen
);
136 wxString::SubstrBufFromWC
wxString::ConvertStr(const wchar_t *pwz
, size_t nLength
,
137 const wxMBConv
& conv
)
140 if ( !pwz
|| nLength
== 0 )
141 return SubstrBufFromWC();
143 if ( nLength
== npos
)
147 wxCharBuffer
mbBuf(conv
.cWC2MB(pwz
, nLength
, &mbLen
));
149 return SubstrBufFromWC();
151 return SubstrBufFromWC(mbBuf
, mbLen
);
158 //Convert wxString in Unicode mode to a multi-byte string
159 const wxCharBuffer
wxString::mb_str(const wxMBConv
& conv
) const
161 return conv
.cWC2MB(c_str(), length() + 1 /* size, not length */, NULL
);
168 //Converts this string to a wide character string if unicode
169 //mode is not enabled and wxUSE_WCHAR_T is enabled
170 const wxWCharBuffer
wxString::wc_str(const wxMBConv
& conv
) const
172 return conv
.cMB2WC(c_str(), length() + 1 /* size, not length */, NULL
);
175 #endif // wxUSE_WCHAR_T
177 #endif // Unicode/ANSI
179 // shrink to minimal size (releasing extra memory)
180 bool wxString::Shrink()
182 wxString
tmp(begin(), end());
184 return tmp
.length() == length();
187 // deprecated compatibility code:
188 #if WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
189 wxChar
*wxString::GetWriteBuf(size_t nLen
)
191 return DoGetWriteBuf(nLen
);
194 void wxString::UngetWriteBuf()
199 void wxString::UngetWriteBuf(size_t nLen
)
201 DoUngetWriteBuf(nLen
);
203 #endif // WXWIN_COMPATIBILITY_2_8 && !wxUSE_STL_BASED_WXSTRING && !wxUSE_UNICODE_UTF8
206 // ---------------------------------------------------------------------------
208 // ---------------------------------------------------------------------------
210 // all functions are inline in string.h
212 // ---------------------------------------------------------------------------
213 // concatenation operators
214 // ---------------------------------------------------------------------------
217 * concatenation functions come in 5 flavours:
219 * char + string and string + char
220 * C str + string and string + C str
223 wxString
operator+(const wxString
& str1
, const wxString
& str2
)
225 #if !wxUSE_STL_BASED_WXSTRING
226 wxASSERT( str1
.IsValid() );
227 wxASSERT( str2
.IsValid() );
236 wxString
operator+(const wxString
& str
, wxUniChar ch
)
238 #if !wxUSE_STL_BASED_WXSTRING
239 wxASSERT( str
.IsValid() );
248 wxString
operator+(wxUniChar ch
, const wxString
& str
)
250 #if !wxUSE_STL_BASED_WXSTRING
251 wxASSERT( str
.IsValid() );
260 wxString
operator+(const wxString
& str
, const char *psz
)
262 #if !wxUSE_STL_BASED_WXSTRING
263 wxASSERT( str
.IsValid() );
267 if ( !s
.Alloc(strlen(psz
) + str
.length()) ) {
268 wxFAIL_MSG( _T("out of memory in wxString::operator+") );
276 wxString
operator+(const wxString
& str
, const wchar_t *pwz
)
278 #if !wxUSE_STL_BASED_WXSTRING
279 wxASSERT( str
.IsValid() );
283 if ( !s
.Alloc(wxWcslen(pwz
) + str
.length()) ) {
284 wxFAIL_MSG( _T("out of memory in wxString::operator+") );
292 wxString
operator+(const char *psz
, const wxString
& str
)
294 #if !wxUSE_STL_BASED_WXSTRING
295 wxASSERT( str
.IsValid() );
299 if ( !s
.Alloc(strlen(psz
) + str
.length()) ) {
300 wxFAIL_MSG( _T("out of memory in wxString::operator+") );
308 wxString
operator+(const wchar_t *pwz
, const wxString
& str
)
310 #if !wxUSE_STL_BASED_WXSTRING
311 wxASSERT( str
.IsValid() );
315 if ( !s
.Alloc(wxWcslen(pwz
) + str
.length()) ) {
316 wxFAIL_MSG( _T("out of memory in wxString::operator+") );
324 // ---------------------------------------------------------------------------
326 // ---------------------------------------------------------------------------
328 #ifdef HAVE_STD_STRING_COMPARE
330 // NB: Comparison code (both if HAVE_STD_STRING_COMPARE and if not) works with
331 // UTF-8 encoded strings too, thanks to UTF-8's design which allows us to
332 // sort strings in characters code point order by sorting the byte sequence
333 // in byte values order (i.e. what strcmp() and memcmp() do).
335 int wxString::compare(const wxString
& str
) const
337 return m_impl
.compare(str
.m_impl
);
340 int wxString::compare(size_t nStart
, size_t nLen
,
341 const wxString
& str
) const
344 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
345 return m_impl
.compare(pos
, len
, str
.m_impl
);
348 int wxString::compare(size_t nStart
, size_t nLen
,
350 size_t nStart2
, size_t nLen2
) const
353 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
356 str
.PosLenToImpl(nStart2
, nLen2
, &pos2
, &len2
);
358 return m_impl
.compare(pos
, len
, str
.m_impl
, pos2
, len2
);
361 int wxString::compare(const char* sz
) const
363 return m_impl
.compare(ImplStr(sz
));
366 int wxString::compare(const wchar_t* sz
) const
368 return m_impl
.compare(ImplStr(sz
));
371 int wxString::compare(size_t nStart
, size_t nLen
,
372 const char* sz
, size_t nCount
) const
375 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
377 SubstrBufFromMB
str(ImplStr(sz
, nCount
));
379 return m_impl
.compare(pos
, len
, str
.data
, str
.len
);
382 int wxString::compare(size_t nStart
, size_t nLen
,
383 const wchar_t* sz
, size_t nCount
) const
386 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
388 SubstrBufFromWC
str(ImplStr(sz
, nCount
));
390 return m_impl
.compare(pos
, len
, str
.data
, str
.len
);
393 #else // !HAVE_STD_STRING_COMPARE
395 static inline int wxDoCmp(const wxStringCharType
* s1
, size_t l1
,
396 const wxStringCharType
* s2
, size_t l2
)
399 return wxStringMemcmp(s1
, s2
, l1
);
402 int ret
= wxStringMemcmp(s1
, s2
, l1
);
403 return ret
== 0 ? -1 : ret
;
407 int ret
= wxStringMemcmp(s1
, s2
, l2
);
408 return ret
== 0 ? +1 : ret
;
412 int wxString::compare(const wxString
& str
) const
414 return ::wxDoCmp(m_impl
.data(), m_impl
.length(),
415 str
.m_impl
.data(), str
.m_impl
.length());
418 int wxString::compare(size_t nStart
, size_t nLen
,
419 const wxString
& str
) const
421 wxASSERT(nStart
<= length());
422 size_type strLen
= length() - nStart
;
423 nLen
= strLen
< nLen
? strLen
: nLen
;
426 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
428 return ::wxDoCmp(m_impl
.data() + pos
, len
,
429 str
.m_impl
.data(), str
.m_impl
.length());
432 int wxString::compare(size_t nStart
, size_t nLen
,
434 size_t nStart2
, size_t nLen2
) const
436 wxASSERT(nStart
<= length());
437 wxASSERT(nStart2
<= str
.length());
438 size_type strLen
= length() - nStart
,
439 strLen2
= str
.length() - nStart2
;
440 nLen
= strLen
< nLen
? strLen
: nLen
;
441 nLen2
= strLen2
< nLen2
? strLen2
: nLen2
;
444 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
446 str
.PosLenToImpl(nStart2
, nLen2
, &pos2
, &len2
);
448 return ::wxDoCmp(m_impl
.data() + pos
, len
,
449 str
.m_impl
.data() + pos2
, len2
);
452 int wxString::compare(const char* sz
) const
454 SubstrBufFromMB
str(ImplStr(sz
, npos
));
455 if ( str
.len
== npos
)
456 str
.len
= wxStringStrlen(str
.data
);
457 return ::wxDoCmp(m_impl
.data(), m_impl
.length(), str
.data
, str
.len
);
460 int wxString::compare(const wchar_t* sz
) const
462 SubstrBufFromWC
str(ImplStr(sz
, npos
));
463 if ( str
.len
== npos
)
464 str
.len
= wxStringStrlen(str
.data
);
465 return ::wxDoCmp(m_impl
.data(), m_impl
.length(), str
.data
, str
.len
);
468 int wxString::compare(size_t nStart
, size_t nLen
,
469 const char* sz
, size_t nCount
) const
471 wxASSERT(nStart
<= length());
472 size_type strLen
= length() - nStart
;
473 nLen
= strLen
< nLen
? strLen
: nLen
;
476 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
478 SubstrBufFromMB
str(ImplStr(sz
, nCount
));
479 if ( str
.len
== npos
)
480 str
.len
= wxStringStrlen(str
.data
);
482 return ::wxDoCmp(m_impl
.data() + pos
, len
, str
.data
, str
.len
);
485 int wxString::compare(size_t nStart
, size_t nLen
,
486 const wchar_t* sz
, size_t nCount
) const
488 wxASSERT(nStart
<= length());
489 size_type strLen
= length() - nStart
;
490 nLen
= strLen
< nLen
? strLen
: nLen
;
493 PosLenToImpl(nStart
, nLen
, &pos
, &len
);
495 SubstrBufFromWC
str(ImplStr(sz
, nCount
));
496 if ( str
.len
== npos
)
497 str
.len
= wxStringStrlen(str
.data
);
499 return ::wxDoCmp(m_impl
.data() + pos
, len
, str
.data
, str
.len
);
502 #endif // HAVE_STD_STRING_COMPARE/!HAVE_STD_STRING_COMPARE
505 // ---------------------------------------------------------------------------
506 // find_{first,last}_[not]_of functions
507 // ---------------------------------------------------------------------------
509 #if !wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
511 // NB: All these functions are implemented with the argument being wxChar*,
512 // i.e. widechar string in any Unicode build, even though native string
513 // representation is char* in the UTF-8 build. This is because we couldn't
514 // use memchr() to determine if a character is in a set encoded as UTF-8.
516 size_t wxString::find_first_of(const wxChar
* sz
, size_t nStart
) const
518 return find_first_of(sz
, nStart
, wxStrlen(sz
));
521 size_t wxString::find_first_not_of(const wxChar
* sz
, size_t nStart
) const
523 return find_first_not_of(sz
, nStart
, wxStrlen(sz
));
526 size_t wxString::find_first_of(const wxChar
* sz
, size_t nStart
, size_t n
) const
528 wxASSERT_MSG( nStart
<= length(), _T("invalid index") );
531 for ( const_iterator i
= begin() + nStart
; i
!= end(); ++idx
, ++i
)
533 if ( wxTmemchr(sz
, *i
, n
) )
540 size_t wxString::find_first_not_of(const wxChar
* sz
, size_t nStart
, size_t n
) const
542 wxASSERT_MSG( nStart
<= length(), _T("invalid index") );
545 for ( const_iterator i
= begin() + nStart
; i
!= end(); ++idx
, ++i
)
547 if ( !wxTmemchr(sz
, *i
, n
) )
555 size_t wxString::find_last_of(const wxChar
* sz
, size_t nStart
) const
557 return find_last_of(sz
, nStart
, wxStrlen(sz
));
560 size_t wxString::find_last_not_of(const wxChar
* sz
, size_t nStart
) const
562 return find_last_not_of(sz
, nStart
, wxStrlen(sz
));
565 size_t wxString::find_last_of(const wxChar
* sz
, size_t nStart
, size_t n
) const
567 size_t len
= length();
569 if ( nStart
== npos
)
575 wxASSERT_MSG( nStart
<= len
, _T("invalid index") );
579 for ( const_reverse_iterator i
= rbegin() + (len
- nStart
- 1);
580 i
!= rend(); --idx
, ++i
)
582 if ( wxTmemchr(sz
, *i
, n
) )
589 size_t wxString::find_last_not_of(const wxChar
* sz
, size_t nStart
, size_t n
) const
591 size_t len
= length();
593 if ( nStart
== npos
)
599 wxASSERT_MSG( nStart
<= len
, _T("invalid index") );
603 for ( const_reverse_iterator i
= rbegin() + (len
- nStart
- 1);
604 i
!= rend(); --idx
, ++i
)
606 if ( !wxTmemchr(sz
, *i
, n
) )
613 size_t wxString::find_first_not_of(wxUniChar ch
, size_t nStart
) const
615 wxASSERT_MSG( nStart
<= length(), _T("invalid index") );
618 for ( const_iterator i
= begin() + nStart
; i
!= end(); ++idx
, ++i
)
627 size_t wxString::find_last_not_of(wxUniChar ch
, size_t nStart
) const
629 size_t len
= length();
631 if ( nStart
== npos
)
637 wxASSERT_MSG( nStart
<= len
, _T("invalid index") );
641 for ( const_reverse_iterator i
= rbegin() + (len
- nStart
- 1);
642 i
!= rend(); --idx
, ++i
)
651 // the functions above were implemented for wchar_t* arguments in Unicode
652 // build and char* in ANSI build; below are implementations for the other
655 #define wxOtherCharType char
656 #define STRCONV (const wxChar*)wxConvLibc.cMB2WC
658 #define wxOtherCharType wchar_t
659 #define STRCONV (const wxChar*)wxConvLibc.cWC2MB
662 size_t wxString::find_first_of(const wxOtherCharType
* sz
, size_t nStart
) const
663 { return find_first_of(STRCONV(sz
), nStart
); }
665 size_t wxString::find_first_of(const wxOtherCharType
* sz
, size_t nStart
,
667 { return find_first_of(STRCONV(sz
, n
, NULL
), nStart
, n
); }
668 size_t wxString::find_last_of(const wxOtherCharType
* sz
, size_t nStart
) const
669 { return find_last_of(STRCONV(sz
), nStart
); }
670 size_t wxString::find_last_of(const wxOtherCharType
* sz
, size_t nStart
,
672 { return find_last_of(STRCONV(sz
, n
, NULL
), nStart
, n
); }
673 size_t wxString::find_first_not_of(const wxOtherCharType
* sz
, size_t nStart
) const
674 { return find_first_not_of(STRCONV(sz
), nStart
); }
675 size_t wxString::find_first_not_of(const wxOtherCharType
* sz
, size_t nStart
,
677 { return find_first_not_of(STRCONV(sz
, n
, NULL
), nStart
, n
); }
678 size_t wxString::find_last_not_of(const wxOtherCharType
* sz
, size_t nStart
) const
679 { return find_last_not_of(STRCONV(sz
), nStart
); }
680 size_t wxString::find_last_not_of(const wxOtherCharType
* sz
, size_t nStart
,
682 { return find_last_not_of(STRCONV(sz
, n
, NULL
), nStart
, n
); }
684 #undef wxOtherCharType
687 #endif // !wxUSE_STL_BASED_WXSTRING || wxUSE_UNICODE_UTF8
689 // ===========================================================================
690 // other common string functions
691 // ===========================================================================
693 int wxString::CmpNoCase(const wxString
& s
) const
695 // FIXME-UTF8: use wxUniChar::ToLower/ToUpper once added
698 const_iterator i1
= begin();
699 const_iterator end1
= end();
700 const_iterator i2
= s
.begin();
701 const_iterator end2
= s
.end();
703 for ( ; i1
!= end1
&& i2
!= end2
; ++idx
, ++i1
, ++i2
)
705 wxUniChar lower1
= (wxChar
)wxTolower(*i1
);
706 wxUniChar lower2
= (wxChar
)wxTolower(*i2
);
707 if ( lower1
!= lower2
)
708 return lower1
< lower2
? -1 : 1;
711 size_t len1
= length();
712 size_t len2
= s
.length();
716 else if ( len1
> len2
)
725 #ifndef __SCHAR_MAX__
726 #define __SCHAR_MAX__ 127
730 wxString
wxString::FromAscii(const char *ascii
)
733 return wxEmptyString
;
735 size_t len
= strlen( ascii
);
740 wxStringBuffer
buf(res
, len
);
746 if ( (*dest
++ = (wchar_t)(unsigned char)*ascii
++) == L
'\0' )
754 wxString
wxString::FromAscii(const char ascii
)
756 // What do we do with '\0' ?
759 res
+= (wchar_t)(unsigned char) ascii
;
764 const wxCharBuffer
wxString::ToAscii() const
766 // this will allocate enough space for the terminating NUL too
767 wxCharBuffer
buffer(length());
770 char *dest
= buffer
.data();
772 const wchar_t *pwc
= c_str();
775 *dest
++ = (char)(*pwc
> SCHAR_MAX
? wxT('_') : *pwc
);
777 // the output string can't have embedded NULs anyhow, so we can safely
778 // stop at first of them even if we do have any
788 // extract string of length nCount starting at nFirst
789 wxString
wxString::Mid(size_t nFirst
, size_t nCount
) const
791 size_t nLen
= length();
793 // default value of nCount is npos and means "till the end"
794 if ( nCount
== npos
)
796 nCount
= nLen
- nFirst
;
799 // out-of-bounds requests return sensible things
800 if ( nFirst
+ nCount
> nLen
)
802 nCount
= nLen
- nFirst
;
807 // AllocCopy() will return empty string
808 return wxEmptyString
;
811 wxString
dest(*this, nFirst
, nCount
);
812 if ( dest
.length() != nCount
)
814 wxFAIL_MSG( _T("out of memory in wxString::Mid") );
820 // check that the string starts with prefix and return the rest of the string
821 // in the provided pointer if it is not NULL, otherwise return false
822 bool wxString::StartsWith(const wxChar
*prefix
, wxString
*rest
) const
824 wxASSERT_MSG( prefix
, _T("invalid parameter in wxString::StartsWith") );
826 // first check if the beginning of the string matches the prefix: note
827 // that we don't have to check that we don't run out of this string as
828 // when we reach the terminating NUL, either prefix string ends too (and
829 // then it's ok) or we break out of the loop because there is no match
830 const wxChar
*p
= c_str();
833 if ( *prefix
++ != *p
++ )
842 // put the rest of the string into provided pointer
850 // check that the string ends with suffix and return the rest of it in the
851 // provided pointer if it is not NULL, otherwise return false
852 bool wxString::EndsWith(const wxChar
*suffix
, wxString
*rest
) const
854 wxASSERT_MSG( suffix
, _T("invalid parameter in wxString::EndssWith") );
856 int start
= length() - wxStrlen(suffix
);
857 if ( start
< 0 || wxStrcmp(wx_str() + start
, suffix
) != 0 )
862 // put the rest of the string into provided pointer
863 rest
->assign(*this, 0, start
);
870 // extract nCount last (rightmost) characters
871 wxString
wxString::Right(size_t nCount
) const
873 if ( nCount
> length() )
876 wxString
dest(*this, length() - nCount
, nCount
);
877 if ( dest
.length() != nCount
) {
878 wxFAIL_MSG( _T("out of memory in wxString::Right") );
883 // get all characters after the last occurence of ch
884 // (returns the whole string if ch not found)
885 wxString
wxString::AfterLast(wxUniChar ch
) const
888 int iPos
= Find(ch
, true);
889 if ( iPos
== wxNOT_FOUND
)
892 str
= wx_str() + iPos
+ 1;
897 // extract nCount first (leftmost) characters
898 wxString
wxString::Left(size_t nCount
) const
900 if ( nCount
> length() )
903 wxString
dest(*this, 0, nCount
);
904 if ( dest
.length() != nCount
) {
905 wxFAIL_MSG( _T("out of memory in wxString::Left") );
910 // get all characters before the first occurence of ch
911 // (returns the whole string if ch not found)
912 wxString
wxString::BeforeFirst(wxUniChar ch
) const
915 if ( iPos
== wxNOT_FOUND
) iPos
= length();
916 return wxString(*this, 0, iPos
);
919 /// get all characters before the last occurence of ch
920 /// (returns empty string if ch not found)
921 wxString
wxString::BeforeLast(wxUniChar ch
) const
924 int iPos
= Find(ch
, true);
925 if ( iPos
!= wxNOT_FOUND
&& iPos
!= 0 )
926 str
= wxString(c_str(), iPos
);
931 /// get all characters after the first occurence of ch
932 /// (returns empty string if ch not found)
933 wxString
wxString::AfterFirst(wxUniChar ch
) const
937 if ( iPos
!= wxNOT_FOUND
)
938 str
= wx_str() + iPos
+ 1;
943 // replace first (or all) occurences of some substring with another one
944 size_t wxString::Replace(const wxString
& strOld
,
945 const wxString
& strNew
, bool bReplaceAll
)
947 // if we tried to replace an empty string we'd enter an infinite loop below
948 wxCHECK_MSG( !strOld
.empty(), 0,
949 _T("wxString::Replace(): invalid parameter") );
951 size_t uiCount
= 0; // count of replacements made
953 size_t uiOldLen
= strOld
.length();
954 size_t uiNewLen
= strNew
.length();
958 while ( (*this)[dwPos
] != wxT('\0') )
960 //DO NOT USE STRSTR HERE
961 //this string can contain embedded null characters,
962 //so strstr will function incorrectly
963 dwPos
= find(strOld
, dwPos
);
965 break; // exit the loop
968 //replace this occurance of the old string with the new one
969 replace(dwPos
, uiOldLen
, strNew
, uiNewLen
);
971 //move up pos past the string that was replaced
974 //increase replace count
979 break; // exit the loop
986 bool wxString::IsAscii() const
988 const wxChar
*s
= (const wxChar
*) *this;
990 if(!isascii(*s
)) return(false);
996 bool wxString::IsWord() const
998 const wxChar
*s
= (const wxChar
*) *this;
1000 if(!wxIsalpha(*s
)) return(false);
1006 bool wxString::IsNumber() const
1008 const wxChar
*s
= (const wxChar
*) *this;
1010 if ((s
[0] == wxT('-')) || (s
[0] == wxT('+'))) s
++;
1012 if(!wxIsdigit(*s
)) return(false);
1018 wxString
wxString::Strip(stripType w
) const
1021 if ( w
& leading
) s
.Trim(false);
1022 if ( w
& trailing
) s
.Trim(true);
1026 // ---------------------------------------------------------------------------
1028 // ---------------------------------------------------------------------------
1030 wxString
& wxString::MakeUpper()
1032 for ( iterator it
= begin(), en
= end(); it
!= en
; ++it
)
1033 *it
= (wxChar
)wxToupper(*it
);
1038 wxString
& wxString::MakeLower()
1040 for ( iterator it
= begin(), en
= end(); it
!= en
; ++it
)
1041 *it
= (wxChar
)wxTolower(*it
);
1046 // ---------------------------------------------------------------------------
1047 // trimming and padding
1048 // ---------------------------------------------------------------------------
1050 // some compilers (VC++ 6.0 not to name them) return true for a call to
1051 // isspace('ê') in the C locale which seems to be broken to me, but we have to
1052 // live with this by checking that the character is a 7 bit one - even if this
1053 // may fail to detect some spaces (I don't know if Unicode doesn't have
1054 // space-like symbols somewhere except in the first 128 chars), it is arguably
1055 // still better than trimming away accented letters
1056 inline int wxSafeIsspace(wxChar ch
) { return (ch
< 127) && wxIsspace(ch
); }
1058 // trims spaces (in the sense of isspace) from left or right side
1059 wxString
& wxString::Trim(bool bFromRight
)
1061 // first check if we're going to modify the string at all
1064 (bFromRight
&& wxSafeIsspace(GetChar(length() - 1))) ||
1065 (!bFromRight
&& wxSafeIsspace(GetChar(0u)))
1071 // find last non-space character
1072 reverse_iterator psz
= rbegin();
1073 while ( (psz
!= rend()) && wxSafeIsspace(*psz
) )
1076 // truncate at trailing space start
1077 erase(psz
.base(), end());
1081 // find first non-space character
1082 iterator psz
= begin();
1083 while ( (psz
!= end()) && wxSafeIsspace(*psz
) )
1086 // fix up data and length
1087 erase(begin(), psz
);
1094 // adds nCount characters chPad to the string from either side
1095 wxString
& wxString::Pad(size_t nCount
, wxUniChar chPad
, bool bFromRight
)
1097 wxString
s(chPad
, nCount
);
1110 // truncate the string
1111 wxString
& wxString::Truncate(size_t uiLen
)
1113 if ( uiLen
< length() )
1115 erase(begin() + uiLen
, end());
1117 //else: nothing to do, string is already short enough
1122 // ---------------------------------------------------------------------------
1123 // finding (return wxNOT_FOUND if not found and index otherwise)
1124 // ---------------------------------------------------------------------------
1127 int wxString::Find(wxUniChar ch
, bool bFromEnd
) const
1129 size_type idx
= bFromEnd
? find_last_of(ch
) : find_first_of(ch
);
1131 return (idx
== npos
) ? wxNOT_FOUND
: (int)idx
;
1134 // ----------------------------------------------------------------------------
1135 // conversion to numbers
1136 // ----------------------------------------------------------------------------
1138 // the implementation of all the functions below is exactly the same so factor
1141 template <typename T
, typename F
>
1142 bool wxStringToIntType(const wxChar
*start
,
1147 wxCHECK_MSG( val
, false, _T("NULL output pointer") );
1148 wxASSERT_MSG( !base
|| (base
> 1 && base
<= 36), _T("invalid base") );
1155 *val
= (*func
)(start
, &end
, base
);
1157 // return true only if scan was stopped by the terminating NUL and if the
1158 // string was not empty to start with and no under/overflow occurred
1159 return !*end
&& (end
!= start
)
1161 && (errno
!= ERANGE
)
1166 bool wxString::ToLong(long *val
, int base
) const
1168 return wxStringToIntType((const wxChar
*)c_str(), val
, base
, wxStrtol
);
1171 bool wxString::ToULong(unsigned long *val
, int base
) const
1173 return wxStringToIntType((const wxChar
*)c_str(), val
, base
, wxStrtoul
);
1176 bool wxString::ToLongLong(wxLongLong_t
*val
, int base
) const
1178 #ifdef wxHAS_STRTOLL
1179 return wxStringToIntType((const wxChar
*)c_str(), val
, base
, wxStrtoll
);
1181 // TODO: implement this ourselves
1185 #endif // wxHAS_STRTOLL
1188 bool wxString::ToULongLong(wxULongLong_t
*val
, int base
) const
1190 #ifdef wxHAS_STRTOLL
1191 return wxStringToIntType((const wxChar
*)c_str(), val
, base
, wxStrtoull
);
1193 // TODO: implement this ourselves
1200 bool wxString::ToDouble(double *val
) const
1202 wxCHECK_MSG( val
, false, _T("NULL pointer in wxString::ToDouble") );
1208 const wxChar
*start
= c_str();
1210 *val
= wxStrtod(start
, &end
);
1212 // return true only if scan was stopped by the terminating NUL and if the
1213 // string was not empty to start with and no under/overflow occurred
1214 return !*end
&& (end
!= start
)
1216 && (errno
!= ERANGE
)
1221 // ---------------------------------------------------------------------------
1223 // ---------------------------------------------------------------------------
1226 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
1227 wxString
wxStringPrintfMixinBase::DoFormat(const wxChar
*format
, ...)
1229 wxString
wxString::DoFormat(const wxChar
*format
, ...)
1233 va_start(argptr
, format
);
1236 s
.PrintfV(format
, argptr
);
1244 wxString
wxString::FormatV(const wxString
& format
, va_list argptr
)
1247 s
.PrintfV(format
, argptr
);
1251 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
1252 int wxStringPrintfMixinBase::DoPrintf(const wxChar
*format
, ...)
1254 int wxString::DoPrintf(const wxChar
*format
, ...)
1258 va_start(argptr
, format
);
1260 #ifdef wxNEEDS_WXSTRING_PRINTF_MIXIN
1261 // get a pointer to the wxString instance; we have to use dynamic_cast<>
1262 // because it's the only cast that works safely for downcasting when
1263 // multiple inheritance is used:
1264 wxString
*str
= static_cast<wxString
*>(this);
1266 wxString
*str
= this;
1269 int iLen
= str
->PrintfV(format
, argptr
);
1276 int wxString::PrintfV(const wxString
& format
, va_list argptr
)
1282 wxStringBuffer
tmp(*this, size
+ 1);
1291 // wxVsnprintf() may modify the original arg pointer, so pass it
1294 wxVaCopy(argptrcopy
, argptr
);
1295 int len
= wxVsnprintf(buf
, size
, format
, argptrcopy
);
1298 // some implementations of vsnprintf() don't NUL terminate
1299 // the string if there is not enough space for it so
1300 // always do it manually
1301 buf
[size
] = _T('\0');
1303 // vsnprintf() may return either -1 (traditional Unix behaviour) or the
1304 // total number of characters which would have been written if the
1305 // buffer were large enough (newer standards such as Unix98)
1308 #if wxUSE_WXVSNPRINTF
1309 // we know that our own implementation of wxVsnprintf() returns -1
1310 // only for a format error - thus there's something wrong with
1311 // the user's format string
1313 #else // assume that system version only returns error if not enough space
1314 // still not enough, as we don't know how much we need, double the
1315 // current size of the buffer
1317 #endif // wxUSE_WXVSNPRINTF/!wxUSE_WXVSNPRINTF
1319 else if ( len
>= size
)
1321 #if wxUSE_WXVSNPRINTF
1322 // we know that our own implementation of wxVsnprintf() returns
1323 // size+1 when there's not enough space but that's not the size
1324 // of the required buffer!
1325 size
*= 2; // so we just double the current size of the buffer
1327 // some vsnprintf() implementations NUL-terminate the buffer and
1328 // some don't in len == size case, to be safe always add 1
1332 else // ok, there was enough space
1338 // we could have overshot
1344 // ----------------------------------------------------------------------------
1345 // misc other operations
1346 // ----------------------------------------------------------------------------
1348 // returns true if the string matches the pattern which may contain '*' and
1349 // '?' metacharacters (as usual, '?' matches any character and '*' any number
1351 bool wxString::Matches(const wxString
& mask
) const
1353 // I disable this code as it doesn't seem to be faster (in fact, it seems
1354 // to be much slower) than the old, hand-written code below and using it
1355 // here requires always linking with libregex even if the user code doesn't
1357 #if 0 // wxUSE_REGEX
1358 // first translate the shell-like mask into a regex
1360 pattern
.reserve(wxStrlen(pszMask
));
1372 pattern
+= _T(".*");
1383 // these characters are special in a RE, quote them
1384 // (however note that we don't quote '[' and ']' to allow
1385 // using them for Unix shell like matching)
1386 pattern
+= _T('\\');
1390 pattern
+= *pszMask
;
1398 return wxRegEx(pattern
, wxRE_NOSUB
| wxRE_EXTENDED
).Matches(c_str());
1399 #else // !wxUSE_REGEX
1400 // TODO: this is, of course, awfully inefficient...
1402 // FIXME-UTF8: implement using iterators, remove #if
1403 #if wxUSE_UNICODE_UTF8
1404 wxWCharBuffer maskBuf
= mask
.wc_str();
1405 wxWCharBuffer txtBuf
= wc_str();
1406 const wxChar
*pszMask
= maskBuf
.data();
1407 const wxChar
*pszTxt
= txtBuf
.data();
1409 const wxChar
*pszMask
= mask
.wx_str();
1410 // the char currently being checked
1411 const wxChar
*pszTxt
= wx_str();
1414 // the last location where '*' matched
1415 const wxChar
*pszLastStarInText
= NULL
;
1416 const wxChar
*pszLastStarInMask
= NULL
;
1419 for ( ; *pszMask
!= wxT('\0'); pszMask
++, pszTxt
++ ) {
1420 switch ( *pszMask
) {
1422 if ( *pszTxt
== wxT('\0') )
1425 // pszTxt and pszMask will be incremented in the loop statement
1431 // remember where we started to be able to backtrack later
1432 pszLastStarInText
= pszTxt
;
1433 pszLastStarInMask
= pszMask
;
1435 // ignore special chars immediately following this one
1436 // (should this be an error?)
1437 while ( *pszMask
== wxT('*') || *pszMask
== wxT('?') )
1440 // if there is nothing more, match
1441 if ( *pszMask
== wxT('\0') )
1444 // are there any other metacharacters in the mask?
1446 const wxChar
*pEndMask
= wxStrpbrk(pszMask
, wxT("*?"));
1448 if ( pEndMask
!= NULL
) {
1449 // we have to match the string between two metachars
1450 uiLenMask
= pEndMask
- pszMask
;
1453 // we have to match the remainder of the string
1454 uiLenMask
= wxStrlen(pszMask
);
1457 wxString
strToMatch(pszMask
, uiLenMask
);
1458 const wxChar
* pMatch
= wxStrstr(pszTxt
, strToMatch
);
1459 if ( pMatch
== NULL
)
1462 // -1 to compensate "++" in the loop
1463 pszTxt
= pMatch
+ uiLenMask
- 1;
1464 pszMask
+= uiLenMask
- 1;
1469 if ( *pszMask
!= *pszTxt
)
1475 // match only if nothing left
1476 if ( *pszTxt
== wxT('\0') )
1479 // if we failed to match, backtrack if we can
1480 if ( pszLastStarInText
) {
1481 pszTxt
= pszLastStarInText
+ 1;
1482 pszMask
= pszLastStarInMask
;
1484 pszLastStarInText
= NULL
;
1486 // don't bother resetting pszLastStarInMask, it's unnecessary
1492 #endif // wxUSE_REGEX/!wxUSE_REGEX
1495 // Count the number of chars
1496 int wxString::Freq(wxUniChar ch
) const
1499 for ( const_iterator i
= begin(); i
!= end(); ++i
)
1507 // convert to upper case, return the copy of the string
1508 wxString
wxString::Upper() const
1509 { wxString
s(*this); return s
.MakeUpper(); }
1511 // convert to lower case, return the copy of the string
1512 wxString
wxString::Lower() const { wxString
s(*this); return s
.MakeLower(); }