summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLinus Groh <mail@linusgroh.de>2020-11-12 23:44:32 +0000
committerAndreas Kling <kling@serenityos.org>2020-11-14 10:11:26 +0100
commitd3ee3fc68a67403940c22ad7ce20a9fb514e603f (patch)
treead8c210990b67a882f8c1fdbf116314845935ca5
parentb1754bf8f8e9a42c6a0803e2c2a19164153a38cc (diff)
downloadserenity-d3ee3fc68a67403940c22ad7ce20a9fb514e603f.zip
AK: Fix StringUtils::contains() case insensitive search
It would incorrectly return false if needle was at the end the string.
-rw-r--r--AK/StringUtils.cpp15
-rw-r--r--AK/Tests/TestStringUtils.cpp2
2 files changed, 9 insertions, 8 deletions
diff --git a/AK/StringUtils.cpp b/AK/StringUtils.cpp
index eb17b558b6..531b1ff505 100644
--- a/AK/StringUtils.cpp
+++ b/AK/StringUtils.cpp
@@ -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;
auto needle_first = to_lowercase(needle_chars[0]);
- size_t slen = str.length() - needle.length();
- for (size_t si = 0; si < slen; si++) {
+ for (size_t si = 0; si < str.length(); si++) {
if (to_lowercase(str_chars[si]) != needle_first)
continue;
- size_t ni = 1;
- while (ni < needle.length()) {
- if (to_lowercase(str_chars[si + ni]) != to_lowercase(needle_chars[ni]))
+ for (size_t ni = 0; si + ni < str.length(); ni++) {
+ if (to_lowercase(str_chars[si + ni]) != to_lowercase(needle_chars[ni])) {
+ si += ni;
break;
- ni++;
+ }
+ if (ni + 1 == needle.length())
+ return true;
}
- if (ni == needle.length())
- return true;
}
return false;
}
diff --git a/AK/Tests/TestStringUtils.cpp b/AK/Tests/TestStringUtils.cpp
index 55b4ab5fc7..6b608859ca 100644
--- a/AK/Tests/TestStringUtils.cpp
+++ b/AK/Tests/TestStringUtils.cpp
@@ -203,6 +203,8 @@ TEST_CASE(contains)
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, "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::CaseInsensitive));
EXPECT(AK::StringUtils::contains(test_string, "", CaseSensitivity::CaseSensitive));