From 4b4177f39c46eb030b01b9aeae954725391ce6cf Mon Sep 17 00:00:00 2001 From: Daniel Bertalan Date: Fri, 25 Feb 2022 22:06:54 +0100 Subject: [PATCH] LibDiff: Generate hunks for new/deleted files Previously we would fail to generate any hunks if the old or the new file was empty. We now do, with the original/target line index set to 0, as specified by POSIX. --- Userland/Libraries/LibDiff/Generator.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Userland/Libraries/LibDiff/Generator.cpp b/Userland/Libraries/LibDiff/Generator.cpp index 43bdee9207..cf65e18b61 100644 --- a/Userland/Libraries/LibDiff/Generator.cpp +++ b/Userland/Libraries/LibDiff/Generator.cpp @@ -107,12 +107,12 @@ Vector from_text(StringView old_text, StringView new_text) } } - while (i < old_lines.size() && new_lines.size() > 0) { - update_hunk(i, new_lines.size() - 1, Direction::Right); // Remove a line + while (i < old_lines.size()) { + update_hunk(i, new_lines.is_empty() ? 0 : new_lines.size() - 1, Direction::Right); // Remove a line ++i; } - while (j < new_lines.size() && old_lines.size() > 0) { - update_hunk(old_lines.size() - 1, j, Direction::Down); // Add a line + while (j < new_lines.size()) { + update_hunk(old_lines.is_empty() ? 0 : old_lines.size() - 1, j, Direction::Down); // Add a line ++j; }