GeezerVison is showing github.com — first implementation of meyers diff with linear space · marisaroman/java-diff-utils@9765052
File treeExpand file treeCollapse file tree Open diff view settings main/java/com/github/difflib/algorithm test/java/com/github/difflib/algorithm/myers
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: java-diff-utils/src/main/java/com/github/difflib/algorithm/Change.java +8Lines changed: 8 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -36,4 +36,12 @@ public Change(DeltaType deltaType, int startOriginal, int endOriginal, int start | 36 | 36 | this.startRevised = startRevised; | 37 | 37 | this.endRevised = endRevised; | 38 | 38 | } | | 39 | + | | 40 | + public Change withEndOriginal(int endOriginal) { | | 41 | + return new Change(deltaType, startOriginal, endOriginal, startRevised, endRevised); | | 42 | + } | | 43 | + | | 44 | + public Change withEndRevised(int endRevised) { | | 45 | + return new Change(deltaType, startOriginal, endOriginal, startRevised, endRevised); | | 46 | + } | 39 | 47 | } |
Collapse file Copy file name to clipboardExpand all lines: java-diff-utils/src/main/java/com/github/difflib/algorithm/myers/MeyersDiffWithLinearSpace.java +14-3Lines changed: 14 additions & 3 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -65,11 +65,22 @@ private void buildScript(DiffData data, int start1, int end1, int start2, int en | 65 | 65 | //TODO: compress these commands. | 66 | 66 | if (end1 - start1 > end2 - start2) { | 67 | 67 | //script.append(new DeleteCommand<>(left.charAt(i))); | 68 | | - data.script.add(new Change(DeltaType.DELETE, i, i + 1, j, j)); | | 68 | + if (data.script.isEmpty() | | 69 | + || data.script.get(data.script.size() - 1).endOriginal != i | | 70 | + || data.script.get(data.script.size() - 1).deltaType != DeltaType.DELETE) { | | 71 | + data.script.add(new Change(DeltaType.DELETE, i, i + 1, j, j)); | | 72 | + } else { | | 73 | + data.script.set(data.script.size() - 1, data.script.get(data.script.size() - 1).withEndOriginal(i + 1)); | | 74 | + } | 69 | 75 | ++i; | 70 | 76 | } else { | 71 | | - //script.append(new InsertCommand<>(right.charAt(j))); | 72 | | - data.script.add(new Change(DeltaType.INSERT, i, i, j, j + 1)); | | 77 | + if (data.script.isEmpty() | | 78 | + || data.script.get(data.script.size() - 1).endRevised != j | | 79 | + || data.script.get(data.script.size() - 1).deltaType != DeltaType.INSERT) { | | 80 | + data.script.add(new Change(DeltaType.INSERT, i, i, j, j + 1)); | | 81 | + } else { | | 82 | + data.script.set(data.script.size() - 1, data.script.get(data.script.size() - 1).withEndRevised(j + 1)); | | 83 | + } | 73 | 84 | ++j; | 74 | 85 | } | 75 | 86 | } | |
Collapse file Copy file name to clipboardExpand all lines: java-diff-utils/src/test/java/com/github/difflib/algorithm/myers/WithMeyersDiffWithLinearSpacePatchTest.java +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -93,7 +93,7 @@ public void testPatch_Change_withExceptionProcessor() { | 93 | 93 |
| 94 | 94 | try { | 95 | 95 | List<String> data = DiffUtils.patch(changeTest_from, patch); | 96 | | - assertEquals(9, data.size()); | | 96 | + assertEquals(11, data.size()); | 97 | 97 |
| 98 | 98 | assertEquals(Arrays.asList("aaa", "<<<<<< HEAD", "bbb", "CDC", "======", "bbb", "ccc", ">>>>>>> PATCH", "ddd"), data); | 99 | 99 |
| |
|