From 4d51143eec019f1fb5bc3101baf417945f93e2b4 Mon Sep 17 00:00:00 2001 From: Paul Melekhov Date: Tue, 25 Aug 2026 04:50:00 +0500 Subject: [PATCH 1/2] Fix reverse overlap handling in line mode --- src/Diff.php | 2 +- tests/DiffTest.php | 13 +++++++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/src/Diff.php b/src/Diff.php index c425b75..afa0ca7 100644 --- a/src/Diff.php +++ b/src/Diff.php @@ -550,7 +550,7 @@ public function cleanupSemantic() ))); $diffs[$pointer - 1] = array( self::INSERT, - mb_substr($insertion, 0, $overlap_length2), + mb_substr($insertion, 0, -$overlap_length2), ); $diffs[$pointer + 1] = array( self::DELETE, diff --git a/tests/DiffTest.php b/tests/DiffTest.php index 5fc6fa8..e34be9a 100644 --- a/tests/DiffTest.php +++ b/tests/DiffTest.php @@ -443,6 +443,19 @@ public function testCleanupSemantic() ), $this->d->getChanges()); } + public function testLineModePreservesTextsAfterReverseOverlap() + { + $overlap = str_repeat('x', Diff::LINEMODE_THRESOLD - 2); + $source = $overlap . 'old'; + $destination = 'new' . $overlap; + + // Skip half-match so the smallest possible input reaches line-mode. + $this->d->setTimeout(0)->main($source, $destination, true); + + $this->assertSame($source, $this->d->text1()); + $this->assertSame($destination, $this->d->text2()); + } + public function testCleanupEfficiency() { // Cleanup operationally trivial equalities. From 947e60403ebe39e9afec2452c7bb42b511c60f0e Mon Sep 17 00:00:00 2001 From: Paul Melekhov Date: Sun, 6 Dec 2026 01:26:32 +0500 Subject: [PATCH 2/2] Preserve nonempty edits when semantic overlap is zero --- src/Diff.php | 3 ++- tests/DiffTest.php | 27 +++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 1 deletion(-) diff --git a/src/Diff.php b/src/Diff.php index afa0ca7..bcd0ad8 100644 --- a/src/Diff.php +++ b/src/Diff.php @@ -530,7 +530,8 @@ public function cleanupSemantic() $overlap_length2 = $this->getToolkit()->commontOverlap($insertion, $deletion); if ($overlap_length1 >= $overlap_length2) { - if ($overlap_length1 >= mb_strlen($deletion) / 2 || $overlap_length1 >= mb_strlen($insertion) / 2) { + // An empty edit has no overlap, even though half its length is zero. + if ($overlap_length1 > 0 && ($overlap_length1 >= mb_strlen($deletion) / 2 || $overlap_length1 >= mb_strlen($insertion) / 2)) { // Overlap found. Insert an equality and trim the surrounding edits. array_splice($diffs, $pointer, 0, array(array( self::EQUAL, diff --git a/tests/DiffTest.php b/tests/DiffTest.php index e34be9a..76d8310 100644 --- a/tests/DiffTest.php +++ b/tests/DiffTest.php @@ -456,6 +456,33 @@ public function testLineModePreservesTextsAfterReverseOverlap() $this->assertSame($destination, $this->d->text2()); } + public function testLineModePreservesTextsWhenSemanticCleanupFindsNoOverlap() + { + $line = str_repeat('x', Diff::LINEMODE_THRESOLD); + $source = "a\n\n{$line}\nb\nc"; + $destination = "\nc\n{$line}\nb"; + + $this->d->setTimeout(0)->main($source, $destination, true); + + $this->assertSame($source, $this->d->text1()); + $this->assertSame($destination, $this->d->text2()); + } + + public function testCleanupSemanticPreservesNonEmptyEditPairedWithAnEmptyEdit() + { + foreach (array(array('abc', ''), array('', 'abc')) as list($source, $destination)) { + $this->d->setChanges(array( + array(Diff::DELETE, $source), + array(Diff::INSERT, $destination), + )); + + $this->d->cleanupSemantic(); + + $this->assertSame($source, $this->d->text1()); + $this->assertSame($destination, $this->d->text2()); + } + } + public function testCleanupEfficiency() { // Cleanup operationally trivial equalities.