Skip to content
Merged
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
4 changes: 3 additions & 1 deletion .horde.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,9 @@ dependencies:
horde/cli: ^3
horde/test: ^3
vendor: horde
keywords: null
keywords:
- diff
- patch
ci-platform:
'8.1':
- php: ^7.4 || ^8
Expand Down
11 changes: 7 additions & 4 deletions src/NativeEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,10 @@ public function diff(): OperationList
$this->xchanged = $this->ychanged = [];
$this->xv = $this->yv = [];
$this->xind = $this->yind = [];
unset($this->seq);
unset($this->in_seq);
unset($this->lcs);
// In modern PHP we cannot unset as NativeEngine may be subclassed and subclasses may have hooks
$this->seq = null;
$this->in_seq = null;
$this->lcs = null;

// Skip leading common lines.
for ($skip = 0; $skip < $n_from && $skip < $n_to; $skip++) {
Expand Down Expand Up @@ -243,6 +244,8 @@ protected function _diag($xoff, $xlim, $yoff, $ylim, $nchunks)

$numer = $xlim - $xoff + $nchunks - 1;
$x = $xoff;
// $k should be defined by the first iteration reaching an empty inseq(y)
$k = null;
for ($chunk = 0; $chunk < $nchunks; $chunk++) {
if ($chunk > 0) {
for ($i = 0; $i <= $this->lcs; $i++) {
Expand All @@ -263,7 +266,7 @@ protected function _diag($xoff, $xlim, $yoff, $ylim, $nchunks)
assert($k > 0);
$ymids[$k] = $ymids[$k - 1];
break;
} elseif ($y > $this->seq[$k - 1]) {
} elseif (is_int($k) && ($k > 0) && ($y > $this->seq[$k - 1])) {
/*
* Disable this assertion for now until we know why it
* causes https://github.com/maintaina-com/Text_Diff/issues/12
Expand Down
20 changes: 20 additions & 0 deletions test/EngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@

namespace Horde\Text\Diff\Test;

use Horde\Text\Diff\AddOperation;
use Horde\Text\Diff\ChangeOperation;
use Horde\Text\Diff\CopyOperation;
use Horde\Text\Diff\DeleteOperation;
use Horde\Text\Diff\Diff;
use Horde\Text\Diff\Exception;
use Horde\Text\Diff\NativeEngine;
Expand Down Expand Up @@ -61,6 +63,24 @@ public function testNativeEngine()
$this->_testDiff($diff);
}

public function testNativeEngineWithRepeatedLines()
{
$diff = Diff::fromFileLineArrays(
['B', 'C', 'C', 'C', 'C', 'D'],
['C', 'D', 'C', 'D', 'B', 'C'],
NativeEngine::class
);
$edits = $diff->getDiff()->toArray();

$this->assertCount(3, $edits);
$this->assertInstanceOf(DeleteOperation::class, $edits[0]);
$this->assertSame(['B', 'C', 'C', 'C'], $edits[0]->orig);
$this->assertInstanceOf(CopyOperation::class, $edits[1]);
$this->assertSame(['C', 'D'], $edits[1]->orig);
$this->assertInstanceOf(AddOperation::class, $edits[2]);
$this->assertSame(['C', 'D', 'B', 'C'], $edits[2]->final);
}

public function testShellEngine()
{
if (!exec('which diff')) {
Expand Down
17 changes: 17 additions & 0 deletions test/Unnamespaced/EngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,23 @@ public function testNativeEngine()
$this->_testDiff($diff);
}

public function testNativeEngineWithRepeatedLines()
{
$diff = new Horde_Text_Diff('Native', [
['B', 'C', 'C', 'C', 'C', 'D'],
['C', 'D', 'C', 'D', 'B', 'C'],
]);
$edits = $diff->getDiff();

$this->assertCount(3, $edits);
$this->assertInstanceOf('Horde_Text_Diff_Op_Delete', $edits[0]);
$this->assertSame(['B', 'C', 'C', 'C'], $edits[0]->orig);
$this->assertInstanceOf('Horde_Text_Diff_Op_Copy', $edits[1]);
$this->assertSame(['C', 'D'], $edits[1]->orig);
$this->assertInstanceOf('Horde_Text_Diff_Op_Add', $edits[2]);
$this->assertSame(['C', 'D', 'B', 'C'], $edits[2]->final);
}

public function testShellEngine()
{
if (!exec('which diff')) {
Expand Down
Loading