X-Git-Url: https://git.saurik.com/wxWidgets.git/blobdiff_plain/32c62191d791de6be1ef54a91ab140d960705f29..5fa150e233fa449ee270ac26ecf074f5c40acb1e:/src/common/string.cpp diff --git a/src/common/string.cpp b/src/common/string.cpp index 99bb5a55a3..c2bab603e2 100644 --- a/src/common/string.cpp +++ b/src/common/string.cpp @@ -86,44 +86,8 @@ extern const wxChar WXDLLIMPEXP_BASE *wxEmptyString = &g_strEmpty.dummy; #if wxUSE_STD_IOSTREAM -// MS Visual C++ version 5.0 provides the new STL headers as well as the old -// iostream ones. -// -// ATTN: you can _not_ use both of these in the same program! - #include -wxSTD istream& operator>>(wxSTD istream& is, wxString& WXUNUSED(str)) -{ -#if 0 - int w = is.width(0); - if ( is.ipfx(0) ) { - streambuf *sb = is.rdbuf(); - str.erase(); - while ( true ) { - int ch = sb->sbumpc (); - if ( ch == EOF ) { - is.setstate(ios::eofbit); - break; - } - else if ( isspace(ch) ) { - sb->sungetc(); - break; - } - - str += ch; - if ( --w == 1 ) - break; - } - } - - is.isfx(); - if ( str.length() == 0 ) - is.setstate(ios::failbit); -#endif - return is; -} - wxSTD ostream& operator<<(wxSTD ostream& os, const wxString& str) { #ifdef __BORLANDC__ @@ -210,8 +174,16 @@ void wxStringBase::InitWith(const wxChar *psz, size_t nPos, size_t nLength) // poor man's iterators are "void *" pointers wxStringBase::wxStringBase(const void *pStart, const void *pEnd) { - InitWith((const wxChar *)pStart, 0, - (const wxChar *)pEnd - (const wxChar *)pStart); + if ( pEnd >= pStart ) + { + InitWith((const wxChar *)pStart, 0, + (const wxChar *)pEnd - (const wxChar *)pStart); + } + else + { + wxFAIL_MSG( _T("pStart is not before pEnd") ); + Init(); + } } wxStringBase::wxStringBase(size_type n, wxChar ch) @@ -470,36 +442,47 @@ void wxStringBase::swap(wxStringBase& str) size_t wxStringBase::find(const wxStringBase& str, size_t nStart) const { + // deal with the special case of empty string first + const size_t nLen = length(); + const size_t nLenOther = str.length(); + + if ( !nLenOther ) + { + // empty string is a substring of anything + return 0; + } + + if ( !nLen ) + { + // the other string is non empty so can't be our substring + return npos; + } + wxASSERT( str.GetStringData()->IsValid() ); - wxASSERT( nStart <= length() ); + wxASSERT( nStart <= nLen ); - //anchor + const wxChar * const other = str.c_str(); + + // anchor const wxChar* p = (const wxChar*)wxTmemchr(c_str() + nStart, - str.c_str()[0], - length() - nStart); + *other, + nLen - nStart); - if(!p) + if ( !p ) return npos; - while(p - c_str() + str.length() <= length() && - wxTmemcmp(p, str.c_str(), str.length()) ) + while ( p - c_str() + nLenOther <= nLen && wxTmemcmp(p, other, nLenOther) ) { - //Previosly passed as the first argument to wxTmemchr, - //but C/C++ standard does not specify evaluation order - //of arguments to functions - - //http://embedded.com/showArticle.jhtml?articleID=9900607 - ++p; - - //anchor again - p = (const wxChar*)wxTmemchr(p, - str.c_str()[0], - length() - (p - c_str())); - - if(!p) + p++; + + // anchor again + p = (const wxChar*)wxTmemchr(p, *other, nLen - (p - c_str())); + + if ( !p ) return npos; } - return (p - c_str() + str.length() <= length()) ? p - c_str() : npos; + return p - c_str() + nLenOther <= nLen ? p - c_str() : npos; } size_t wxStringBase::find(const wxChar* sz, size_t nStart, size_t n) const