1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 01:57:44 +00:00

LibDiff: Implement ability to parse a patch header

This is a somewhat naive implementation, but it is enough to parse a
simple unified patch header.

After parsing the patch header, the parser will be at the beginning of
the first hunks range, ready for that hunk to be parsed.
This commit is contained in:
Shannon Booth 2023-07-06 20:52:26 +12:00 committed by Sam Atkins
parent ef45221c21
commit efb26b1781
3 changed files with 44 additions and 0 deletions

View file

@ -57,6 +57,33 @@ bool Parser::consume_line_number(size_t& number)
return true;
}
ErrorOr<Header> Parser::parse_header()
{
Header header;
while (!is_eof()) {
if (consume_specific("+++ ")) {
header.new_file_path = TRY(String::from_utf8(consume_line()));
continue;
}
if (consume_specific("--- ")) {
header.old_file_path = TRY(String::from_utf8(consume_line()));
continue;
}
if (next_is("@@ ")) {
header.format = Format::Unified;
return header;
}
consume_line();
}
return Error::from_string_literal("Unable to find any patch");
}
ErrorOr<Vector<Hunk>> Parser::parse_hunks()
{
Vector<Hunk> hunks;