diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 80bbeed..ea564de 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -10,12 +10,13 @@ jobs: strategy: matrix: php: + - 8.3 + - 8.2 + - 8.1 + - 8.0 - 7.4 - 7 - 5.6 - - 5.5 - - 5.4 - - 5.3 os: [ubuntu-latest, ubuntu-22.04] steps: - name: Checkout diff --git a/Text/Diff/Engine/native.php b/Text/Diff/Engine/native.php index 25f7ebf..d41eb96 100644 --- a/Text/Diff/Engine/native.php +++ b/Text/Diff/Engine/native.php @@ -6,9 +6,9 @@ * * The algorithm used here is mostly lifted from the perl module * Algorithm::Diff (version 1.06) by Ned Konz, which is available at: - * http://www.perl.com/CPAN/authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip + * https://cpan.metacpan.org/authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip * - * More ideas are taken from: http://www.ics.uci.edu/~eppstein/161/960229.html + * More ideas are taken from: https://www.ics.uci.edu/~eppstein/161/960229.html * * Some ideas (and a bit of code) are taken from analyze.c, of GNU * diffutils-2.7, which can be found at: @@ -20,15 +20,26 @@ * * $Horde: framework/Text_Diff/Diff/Engine/native.php,v 1.7.2.5 2009/01/06 15:23:41 jan Exp $ * - * Copyright 2004-2009 The Horde Project (http://www.horde.org/) + * Copyright 2004-2009 The Horde Project (https://www.horde.org/) * * See the enclosed file COPYING for license information (LGPL). If you did - * not receive this file, see http://opensource.org/licenses/lgpl-license.php. + * not receive this file, see https://opensource.org/license/lgpl-3-0/ . * * @author Geoffrey T. Dairiki * @package Text_Diff */ -class Text_Diff_Engine_native { +class Text_Diff_Engine_native +{ + + public $xchanged; + public $ychanged; + public $xv; + public $yv; + public $xind; + public $yind; + public $seq; + public $in_seq; + public $lcs; function diff($from_lines, $to_lines) { @@ -63,9 +74,11 @@ function diff($from_lines, $to_lines) } // Ignore lines which do not exist in both files. + $xhash = []; for ($xi = $skip; $xi < $n_from - $endskip; $xi++) { $xhash[$from_lines[$xi]] = 1; } + $yhash = []; for ($yi = $skip; $yi < $n_to - $endskip; $yi++) { $line = $to_lines[$yi]; if (($this->ychanged[$yi] = empty($xhash[$line]))) { @@ -148,7 +161,7 @@ function diff($from_lines, $to_lines) * match. The caller must trim matching lines from the beginning and end * of the portions it is going to specify. */ - function _diag ($xoff, $xlim, $yoff, $ylim, $nchunks) + function _diag($xoff, $xlim, $yoff, $ylim, $nchunks) { $flip = false; @@ -160,6 +173,7 @@ function _diag ($xoff, $xlim, $yoff, $ylim, $nchunks) = array($yoff, $ylim, $xoff, $xlim); } + $ymatches = array(); if ($flip) { for ($i = $ylim - 1; $i >= $yoff; $i--) { $ymatches[$this->xv[$i]][] = $i; @@ -173,7 +187,7 @@ function _diag ($xoff, $xlim, $yoff, $ylim, $nchunks) $this->lcs = 0; $this->seq[0]= $yoff - 1; $this->in_seq = array(); - $ymids[0] = array(); + $ymids = array(array()); $numer = $xlim - $xoff + $nchunks - 1; $x = $xoff; @@ -192,15 +206,16 @@ function _diag ($xoff, $xlim, $yoff, $ylim, $nchunks) } $matches = $ymatches[$line]; reset($matches); - foreach ($matches as list(, $y)) { + while ($y = current($matches)) { if (empty($this->in_seq[$y])) { $k = $this->_lcsPos($y); assert($k > 0); $ymids[$k] = $ymids[$k - 1]; break; } + next($matches); } - foreach ($matches as list(, $y)) { + while ($y = current($matches)) { if ($y > $this->seq[$k - 1]) { assert($y <= $this->seq[$k]); /* Optimization: this is a common case: next match is @@ -213,11 +228,12 @@ function _diag ($xoff, $xlim, $yoff, $ylim, $nchunks) assert($k > 0); $ymids[$k] = $ymids[$k - 1]; } + next($matches); } } } - $seps[] = $flip ? array($yoff, $xoff) : array($xoff, $yoff); + $seps = array($flip ? array($yoff, $xoff) : array($xoff, $yoff)); $ymid = $ymids[$this->lcs]; for ($n = 0; $n < $nchunks - 1; $n++) { $x1 = $xoff + (int)(($numer + ($xlim - $xoff) * $n) / $nchunks); @@ -268,7 +284,7 @@ function _lcsPos($ypos) * Note that XLIM, YLIM are exclusive bounds. All line numbers are * origin-0 and discarded lines are not counted. */ - function _compareseq ($xoff, $xlim, $yoff, $ylim) + function _compareseq($xoff, $xlim, $yoff, $ylim) { /* Slide down the bottom initial diagonal. */ while ($xoff < $xlim && $yoff < $ylim @@ -309,7 +325,7 @@ function _compareseq ($xoff, $xlim, $yoff, $ylim) reset($seps); $pt1 = $seps[0]; while ($pt2 = next($seps)) { - $this->_compareseq ($pt1[0], $pt2[0], $pt1[1], $pt2[1]); + $this->_compareseq($pt1[0], $pt2[0], $pt1[1], $pt2[1]); $pt1 = $pt2; } }