mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 13:37:45 +00:00
AK: Fix StringUtils::contains() case insensitive search
It would incorrectly return false if needle was at the end the string.
This commit is contained in:
parent
b1754bf8f8
commit
d3ee3fc68a
2 changed files with 9 additions and 8 deletions
|
@ -247,18 +247,17 @@ bool contains(const StringView& str, const StringView& needle, CaseSensitivity c
|
||||||
return memmem(str_chars, str.length(), needle_chars, needle.length()) != nullptr;
|
return memmem(str_chars, str.length(), needle_chars, needle.length()) != nullptr;
|
||||||
|
|
||||||
auto needle_first = to_lowercase(needle_chars[0]);
|
auto needle_first = to_lowercase(needle_chars[0]);
|
||||||
size_t slen = str.length() - needle.length();
|
for (size_t si = 0; si < str.length(); si++) {
|
||||||
for (size_t si = 0; si < slen; si++) {
|
|
||||||
if (to_lowercase(str_chars[si]) != needle_first)
|
if (to_lowercase(str_chars[si]) != needle_first)
|
||||||
continue;
|
continue;
|
||||||
size_t ni = 1;
|
for (size_t ni = 0; si + ni < str.length(); ni++) {
|
||||||
while (ni < needle.length()) {
|
if (to_lowercase(str_chars[si + ni]) != to_lowercase(needle_chars[ni])) {
|
||||||
if (to_lowercase(str_chars[si + ni]) != to_lowercase(needle_chars[ni]))
|
si += ni;
|
||||||
break;
|
break;
|
||||||
ni++;
|
}
|
||||||
|
if (ni + 1 == needle.length())
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
if (ni == needle.length())
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
|
@ -203,6 +203,8 @@ TEST_CASE(contains)
|
||||||
EXPECT(AK::StringUtils::contains(test_string, "BCX", CaseSensitivity::CaseSensitive));
|
EXPECT(AK::StringUtils::contains(test_string, "BCX", CaseSensitivity::CaseSensitive));
|
||||||
EXPECT(AK::StringUtils::contains(test_string, "BCX", CaseSensitivity::CaseInsensitive));
|
EXPECT(AK::StringUtils::contains(test_string, "BCX", CaseSensitivity::CaseInsensitive));
|
||||||
EXPECT(AK::StringUtils::contains(test_string, "BcX", CaseSensitivity::CaseInsensitive));
|
EXPECT(AK::StringUtils::contains(test_string, "BcX", CaseSensitivity::CaseInsensitive));
|
||||||
|
EXPECT(!AK::StringUtils::contains(test_string, "xyz", CaseSensitivity::CaseSensitive));
|
||||||
|
EXPECT(AK::StringUtils::contains(test_string, "xyz", CaseSensitivity::CaseInsensitive));
|
||||||
EXPECT(!AK::StringUtils::contains(test_string, "EFG", CaseSensitivity::CaseSensitive));
|
EXPECT(!AK::StringUtils::contains(test_string, "EFG", CaseSensitivity::CaseSensitive));
|
||||||
EXPECT(!AK::StringUtils::contains(test_string, "EfG", CaseSensitivity::CaseInsensitive));
|
EXPECT(!AK::StringUtils::contains(test_string, "EfG", CaseSensitivity::CaseInsensitive));
|
||||||
EXPECT(AK::StringUtils::contains(test_string, "", CaseSensitivity::CaseSensitive));
|
EXPECT(AK::StringUtils::contains(test_string, "", CaseSensitivity::CaseSensitive));
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue