mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-06-09 09:34:57 +09:00
AK: Make StringUtils::matches() handle escaping correctly
Previously any backslash and the character following it were ignored. This commit adds a fall through to match the character following the backslash without checking whether it is "special".
This commit is contained in:
parent
0e26f2657e
commit
af2ffcaba8
Notes:
sideshowbarker
2024-07-19 17:00:29 +09:00
Author: https://github.com/f-cramer 🔰
Commit: af2ffcaba8
Pull-request: https://github.com/SerenityOS/serenity/pull/16537
Reviewed-by: https://github.com/MacDue
Reviewed-by: https://github.com/alimpfard
2 changed files with 20 additions and 2 deletions
|
@ -62,8 +62,11 @@ bool matches(StringView str, StringView mask, CaseSensitivity case_sensitivity,
|
||||||
record_span(string_ptr - string_start, 1);
|
record_span(string_ptr - string_start, 1);
|
||||||
break;
|
break;
|
||||||
case '\\':
|
case '\\':
|
||||||
++mask_ptr;
|
// if backslash is last character in mask, just treat it as an exact match
|
||||||
break;
|
// otherwise use it as escape for next character
|
||||||
|
if (mask_ptr + 1 < mask_end)
|
||||||
|
++mask_ptr;
|
||||||
|
[[fallthrough]];
|
||||||
default:
|
default:
|
||||||
auto p = *mask_ptr;
|
auto p = *mask_ptr;
|
||||||
auto ch = *string_ptr;
|
auto ch = *string_ptr;
|
||||||
|
|
|
@ -76,6 +76,21 @@ TEST_CASE(matches_trailing)
|
||||||
EXPECT(AK::StringUtils::matches("ab"sv, "*ab****"sv));
|
EXPECT(AK::StringUtils::matches("ab"sv, "*ab****"sv));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE(match_backslash_escape)
|
||||||
|
{
|
||||||
|
EXPECT(AK::StringUtils::matches("ab*"sv, "ab\\*"sv));
|
||||||
|
EXPECT(!AK::StringUtils::matches("abc"sv, "ab\\*"sv));
|
||||||
|
EXPECT(!AK::StringUtils::matches("abcd"sv, "ab\\*"sv));
|
||||||
|
EXPECT(AK::StringUtils::matches("ab?"sv, "ab\\?"sv));
|
||||||
|
EXPECT(!AK::StringUtils::matches("abc"sv, "ab\\?"sv));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE(match_trailing_backslash)
|
||||||
|
{
|
||||||
|
EXPECT(AK::StringUtils::matches("x\\"sv, "x\\"sv));
|
||||||
|
EXPECT(AK::StringUtils::matches("x\\"sv, "x\\\\"sv));
|
||||||
|
}
|
||||||
|
|
||||||
TEST_CASE(convert_to_int)
|
TEST_CASE(convert_to_int)
|
||||||
{
|
{
|
||||||
auto value = AK::StringUtils::convert_to_int(StringView());
|
auto value = AK::StringUtils::convert_to_int(StringView());
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue