Bug 413488 - "nsACString::Find in nsStringAPI is buggy!" [p=prasad@medhas.org (Prasad Sunkari [prasad]) r=bsmedberg a=blocking1.9+]

git-svn-id: svn://10.0.0.236/trunk@245582 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
reed%reedloden.com 2008-02-13 10:49:32 +00:00
parent 80f9143d4c
commit f063b40361
2 changed files with 55 additions and 11 deletions

View File

@ -328,12 +328,12 @@ nsAString::Find(const self_type& aStr, PRUint32 aOffset,
const char_type *begin, *end;
PRUint32 selflen = BeginReading(&begin, &end);
const char_type *other;
PRUint32 otherlen = aStr.BeginReading(&other);
if (aOffset > selflen)
return -1;
const char_type *other;
PRUint32 otherlen = aStr.BeginReading(&other);
if (otherlen > selflen - aOffset)
return -1;
@ -389,7 +389,7 @@ nsAString::Find(const char *aStr, PRUint32 aOffset, PRBool aIgnoreCase) const
PRUint32 otherlen = strlen(aStr);
if (otherlen > selflen)
if (otherlen > selflen - aOffset)
return -1;
// We want to stop searching otherlen characters before the end of the string
@ -710,17 +710,26 @@ PRInt32
nsACString::Find(const self_type& aStr, PRUint32 aOffset,
ComparatorFunc c) const
{
const char_type *begin;
PRUint32 len = aStr.BeginReading(&begin);
const char_type *begin, *end;
PRUint32 selflen = BeginReading(&begin, &end);
if (aOffset > len)
if (aOffset > selflen)
return -1;
PRInt32 found = Find(begin + aOffset, len - aOffset, c);
if (found == -1)
return found;
const char_type *other;
PRUint32 otherlen = aStr.BeginReading(&other);
return found + aOffset;
if (otherlen > selflen - aOffset)
return -1;
// We want to stop searching otherlen characters before the end of the string
end -= otherlen;
for (const char_type *cur = begin + aOffset; cur <= end; ++cur) {
if (!c(cur, other, otherlen))
return cur - begin;
}
return -1;
}
PRInt32

View File

@ -106,6 +106,40 @@ int testWrite() {
return res;
}
int testFind() {
nsString str_haystack;
nsString str_needle;
str_needle.AssignLiteral("world");
PRInt32 ret = 0;
ret += CHECK(-1 == str_haystack.Find("world"));
ret += CHECK(-1 == str_haystack.Find(str_needle));
str_haystack.AssignLiteral("hello world hello world hello");
ret += CHECK( 6 == str_haystack.Find("world"));
ret += CHECK( 6 == str_haystack.Find(str_needle));
ret += CHECK(-1 == str_haystack.Find("world", 20, PR_FALSE));
ret += CHECK(-1 == str_haystack.Find(str_needle, 20));
ret += CHECK(18 == str_haystack.Find("world", 12, PR_FALSE));
ret += CHECK(18 == str_haystack.Find(str_needle, 12));
nsCString cstr_haystack;
nsCString cstr_needle;
cstr_needle.AssignLiteral("world");
ret += CHECK(-1 == cstr_haystack.Find("world"));
ret += CHECK(-1 == cstr_haystack.Find(cstr_needle));
cstr_haystack.AssignLiteral("hello world hello world hello");
ret += CHECK( 6 == cstr_haystack.Find("world"));
ret += CHECK( 6 == cstr_haystack.Find(cstr_needle));
ret += CHECK(-1 == cstr_haystack.Find(cstr_needle, 20));
ret += CHECK(18 == cstr_haystack.Find(cstr_needle, 12));
ret += CHECK( 6 == cstr_haystack.Find("world", 5));
return ret;
}
int testVoid() {
nsString s;
int ret = CHECK(!s.IsVoid());
@ -126,6 +160,7 @@ int main() {
rv += testEmpty();
rv += testAccess();
rv += testWrite();
rv += testFind();
rv += testVoid();
if (0 == rv) {
fprintf(stderr, "PASS: StringAPI tests\n");