Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions src/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -550,7 +551,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,
Expand Down
40 changes: 40 additions & 0 deletions tests/DiffTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,46 @@ 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 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.
Expand Down