diff --git a/Userland/Libraries/LibDiff/Forward.h b/Userland/Libraries/LibDiff/Forward.h index c21cad11dae..d4d2acb27a4 100644 --- a/Userland/Libraries/LibDiff/Forward.h +++ b/Userland/Libraries/LibDiff/Forward.h @@ -8,8 +8,11 @@ namespace Diff { +enum class Format; + class Parser; +struct Header; struct Hunk; struct HunkLocation; struct Line; diff --git a/Userland/Libraries/LibDiff/Hunks.cpp b/Userland/Libraries/LibDiff/Hunks.cpp index cecce7c9b32..0a89863341b 100644 --- a/Userland/Libraries/LibDiff/Hunks.cpp +++ b/Userland/Libraries/LibDiff/Hunks.cpp @@ -57,6 +57,33 @@ bool Parser::consume_line_number(size_t& number) return true; } +ErrorOr
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> Parser::parse_hunks() { Vector hunks; diff --git a/Userland/Libraries/LibDiff/Hunks.h b/Userland/Libraries/LibDiff/Hunks.h index 17b3d4a4bcc..ac7d42dd4e8 100644 --- a/Userland/Libraries/LibDiff/Hunks.h +++ b/Userland/Libraries/LibDiff/Hunks.h @@ -59,12 +59,26 @@ struct Hunk { Vector lines; }; +enum class Format { + Unified, + Unknown, +}; + +struct Header { + Format format { Format::Unknown }; + + String old_file_path; + String new_file_path; +}; + class Parser : public GenericLexer { public: using GenericLexer::GenericLexer; ErrorOr> parse_hunks(); + ErrorOr
parse_header(); + private: Optional consume_unified_location(); bool consume_line_number(size_t& number);