Browse Source

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.
Daniel Bertalan 3 năm trước cách đây
mục cha
commit
4b4177f39c
1 tập tin đã thay đổi với 4 bổ sung4 xóa
  1. 4 4
      Userland/Libraries/LibDiff/Generator.cpp

+ 4 - 4
Userland/Libraries/LibDiff/Generator.cpp

@@ -107,12 +107,12 @@ Vector<Hunk> 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;
     }