fixed Contains() to pass the unit test (didn't work for empty strings)

git-svn-id: https://svn.wxwidgets.org/svn/wx/wxWidgets/trunk@41487 c3d73ce0-8a6f-49c7-b76d-6d57e0e08775
This commit is contained in:
Vadim Zeitlin
2006-09-28 11:38:51 +00:00
parent c4f35063ce
commit e66eb2dfae

View File

@@ -434,36 +434,47 @@ void wxStringBase::swap(wxStringBase& str)
size_t wxStringBase::find(const wxStringBase& str, size_t nStart) const 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( 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, const wxChar* p = (const wxChar*)wxTmemchr(c_str() + nStart,
str.c_str()[0], *other,
length() - nStart); nLen - nStart);
if(!p) if ( !p )
return npos; return npos;
while(p - c_str() + str.length() <= length() && while ( p - c_str() + nLenOther <= nLen && wxTmemcmp(p, other, nLenOther) )
wxTmemcmp(p, str.c_str(), str.length()) )
{ {
//Previosly passed as the first argument to wxTmemchr, p++;
//but C/C++ standard does not specify evaluation order
//of arguments to functions -
//http://embedded.com/showArticle.jhtml?articleID=9900607
++p;
//anchor again // anchor again
p = (const wxChar*)wxTmemchr(p, p = (const wxChar*)wxTmemchr(p, *other, nLen - (p - c_str()));
str.c_str()[0],
length() - (p - c_str()));
if(!p) if ( !p )
return npos; 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 size_t wxStringBase::find(const wxChar* sz, size_t nStart, size_t n) const