1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 18:27:35 +00:00

AK+Everywhere: Make GenericLexer::ignore_until() stop before the value

`consume_until(foo)` stops before foo, and so does
`ignore_until(Predicate)`, so let's make the other `ignore_until()`
overloads consistent with that so they're less confusing.
This commit is contained in:
Sam Atkins 2023-02-28 11:11:39 +00:00 committed by Sam Atkins
parent 0511059d60
commit c06f4ac6f5
5 changed files with 13 additions and 16 deletions

View file

@ -142,7 +142,6 @@ public:
while (!is_eof() && peek() != stop) { while (!is_eof() && peek() != stop) {
++m_index; ++m_index;
} }
ignore();
} }
constexpr void ignore_until(char const* stop) constexpr void ignore_until(char const* stop)
@ -150,7 +149,6 @@ public:
while (!is_eof() && !next_is(stop)) { while (!is_eof() && !next_is(stop)) {
++m_index; ++m_index;
} }
ignore(__builtin_strlen(stop));
} }
/* /*
@ -205,8 +203,7 @@ public:
++m_index; ++m_index;
} }
// Ignore characters until `pred` return true // Ignore characters until `pred` returns true
// We don't skip the stop character as it may not be a unique value
template<typename TPredicate> template<typename TPredicate>
constexpr void ignore_until(TPredicate pred) constexpr void ignore_until(TPredicate pred)
{ {

View file

@ -105,7 +105,7 @@ Vector<Endpoint> parse(ByteBuffer const& file_contents)
auto consume_whitespace = [&lexer] { auto consume_whitespace = [&lexer] {
lexer.ignore_while([](char ch) { return isspace(ch); }); lexer.ignore_while([](char ch) { return isspace(ch); });
if (lexer.peek() == '/' && lexer.peek(1) == '/') if (lexer.peek() == '/' && lexer.peek(1) == '/')
lexer.ignore_until([](char ch) { return ch == '\n'; }); lexer.ignore_until('\n');
}; };
auto parse_parameter = [&](Vector<Parameter>& storage) { auto parse_parameter = [&](Vector<Parameter>& storage) {

View file

@ -107,7 +107,7 @@ TEST_CASE(should_constexpr_ignore_until)
sut.ignore_until('d'); sut.ignore_until('d');
return sut; return sut;
}(); }();
static_assert(sut.peek() == 'e'); static_assert(sut.peek() == 'd');
} }
TEST_CASE(should_constexpr_ignore_until_cstring) TEST_CASE(should_constexpr_ignore_until_cstring)
@ -117,7 +117,7 @@ TEST_CASE(should_constexpr_ignore_until_cstring)
sut.ignore_until("cde"); sut.ignore_until("cde");
return sut; return sut;
}(); }();
static_assert(sut.peek() == 'f'); static_assert(sut.peek() == 'c');
} }
TEST_CASE(should_constexpr_next_is_pred) TEST_CASE(should_constexpr_next_is_pred)

View file

@ -72,21 +72,24 @@ static void consume_whitespace(GenericLexer& lexer)
lexer.ignore(2); lexer.ignore(2);
} else { } else {
lexer.ignore_until('\n'); lexer.ignore_until('\n');
lexer.ignore();
break; break;
} }
} }
}; };
for (;;) { for (;;) {
if (lexer.consume_specific("//"sv)) if (lexer.consume_specific("//"sv)) {
ignore_line(); ignore_line();
else if (lexer.consume_specific("/*"sv)) } else if (lexer.consume_specific("/*"sv)) {
lexer.ignore_until("*/"); lexer.ignore_until("*/");
else if (lexer.next_is("\\\n"sv))
lexer.ignore(2); lexer.ignore(2);
else if (lexer.is_eof() || !lexer.next_is(isspace)) } else if (lexer.next_is("\\\n"sv)) {
lexer.ignore(2);
} else if (lexer.is_eof() || !lexer.next_is(isspace)) {
break; break;
else } else {
lexer.ignore(); lexer.ignore();
}
} }
} }

View file

@ -139,10 +139,7 @@ Optional<MimeType> MimeType::from_string(StringView string)
parameter_value = Fetch::Infrastructure::collect_an_http_quoted_string(lexer, Fetch::Infrastructure::HttpQuotedStringExtractValue::Yes); parameter_value = Fetch::Infrastructure::collect_an_http_quoted_string(lexer, Fetch::Infrastructure::HttpQuotedStringExtractValue::Yes);
// 2. Collect a sequence of code points that are not U+003B (;) from input, given position. // 2. Collect a sequence of code points that are not U+003B (;) from input, given position.
// NOTE: This uses the predicate version as the ignore_until(char) version will also ignore the ';'. lexer.ignore_until(';');
lexer.ignore_until([](char ch) {
return ch == ';';
});
} }
// 9. Otherwise: // 9. Otherwise: