|
|
ef5584 |
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* @package diff
|
|
|
ef5584 |
* @version $Id: engine.php 8765 2008-08-16 22:18:25Z aptx $
|
|
|
ef5584 |
* @copyright (c) 2006 phpBB Group
|
|
|
ef5584 |
* @license http://opensource.org/licenses/gpl-license.php GNU Public License
|
|
|
ef5584 |
*
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
* @ignore
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
if (!defined('IN_PHPBB'))
|
|
|
ef5584 |
{
|
|
|
ef5584 |
exit;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
* Code from pear.php.net, Text_Diff-1.0.0 package
|
|
|
ef5584 |
* http://pear.php.net/package/Text_Diff/ (native engine)
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* Modified by phpBB Group to meet our coding standards
|
|
|
ef5584 |
* and being able to integrate into phpBB
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* Class used internally by Text_Diff to actually compute the diffs. This
|
|
|
ef5584 |
* class is implemented using native PHP code.
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* The algorithm used here is mostly lifted from the perl module
|
|
|
ef5584 |
* Algorithm::Diff (version 1.06) by Ned Konz, which is available at:
|
|
|
ef5584 |
* http://www.perl.com/CPAN/authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* More ideas are taken from: http://www.ics.uci.edu/~eppstein/161/960229.html
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* Some ideas (and a bit of code) are taken from analyze.c, of GNU
|
|
|
ef5584 |
* diffutils-2.7, which can be found at:
|
|
|
ef5584 |
* ftp://gnudist.gnu.org/pub/gnu/diffutils/diffutils-2.7.tar.gz
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* Some ideas (subdivision by NCHUNKS > 2, and some optimizations) are from
|
|
|
ef5584 |
* Geoffrey T. Dairiki <dairiki@dairiki.org>. The original PHP version of this
|
|
|
ef5584 |
* code was written by him, and is used/adapted with his permission.
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* Copyright 2004-2008 The Horde Project (http://www.horde.org/)
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* @author Geoffrey T. Dairiki <dairiki@dairiki.org>
|
|
|
ef5584 |
* @package diff
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* @access private
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
class diff_engine
|
|
|
ef5584 |
{
|
|
|
ef5584 |
function diff(&$from_lines, &$to_lines, $preserve_cr = true)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
// Remove empty lines...
|
|
|
ef5584 |
// If preserve_cr is true, we basically only change \r\n and bare \r to \n to get the same carriage returns for both files
|
|
|
ef5584 |
// If it is false, we try to only use \n once per line and ommit all empty lines to be able to get a proper data diff
|
|
|
ef5584 |
|
|
|
ef5584 |
if (is_array($from_lines))
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$from_lines = implode("\n", $from_lines);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
if (is_array($to_lines))
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$to_lines = implode("\n", $to_lines);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
if ($preserve_cr)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$from_lines = explode("\n", str_replace("\r", "\n", str_replace("\r\n", "\n", $from_lines)));
|
|
|
ef5584 |
$to_lines = explode("\n", str_replace("\r", "\n", str_replace("\r\n", "\n", $to_lines)));
|
|
|
ef5584 |
}
|
|
|
ef5584 |
else
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$from_lines = explode("\n", preg_replace('#[\n\r]+#', "\n", $from_lines));
|
|
|
ef5584 |
$to_lines = explode("\n", preg_replace('#[\n\r]+#', "\n", $to_lines));
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
$n_from = sizeof($from_lines);
|
|
|
ef5584 |
$n_to = sizeof($to_lines);
|
|
|
ef5584 |
|
|
|
ef5584 |
$this->xchanged = $this->ychanged = $this->xv = $this->yv = $this->xind = $this->yind = array();
|
|
|
ef5584 |
unset($this->seq, $this->in_seq, $this->lcs);
|
|
|
ef5584 |
|
|
|
ef5584 |
// Skip leading common lines.
|
|
|
ef5584 |
for ($skip = 0; $skip < $n_from && $skip < $n_to; $skip++)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
if ($from_lines[$skip] !== $to_lines[$skip])
|
|
|
ef5584 |
{
|
|
|
ef5584 |
break;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
$this->xchanged[$skip] = $this->ychanged[$skip] = false;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
// Skip trailing common lines.
|
|
|
ef5584 |
$xi = $n_from;
|
|
|
ef5584 |
$yi = $n_to;
|
|
|
ef5584 |
|
|
|
ef5584 |
for ($endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
if ($from_lines[$xi] !== $to_lines[$yi])
|
|
|
ef5584 |
{
|
|
|
ef5584 |
break;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
$this->xchanged[$xi] = $this->ychanged[$yi] = false;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
// Ignore lines which do not exist in both files.
|
|
|
ef5584 |
for ($xi = $skip; $xi < $n_from - $endskip; $xi++)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$xhash[$from_lines[$xi]] = 1;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
for ($yi = $skip; $yi < $n_to - $endskip; $yi++)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$line = $to_lines[$yi];
|
|
|
ef5584 |
|
|
|
ef5584 |
if (($this->ychanged[$yi] = empty($xhash[$line])))
|
|
|
ef5584 |
{
|
|
|
ef5584 |
continue;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
$yhash[$line] = 1;
|
|
|
ef5584 |
$this->yv[] = $line;
|
|
|
ef5584 |
$this->yind[] = $yi;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
for ($xi = $skip; $xi < $n_from - $endskip; $xi++)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$line = $from_lines[$xi];
|
|
|
ef5584 |
|
|
|
ef5584 |
if (($this->xchanged[$xi] = empty($yhash[$line])))
|
|
|
ef5584 |
{
|
|
|
ef5584 |
continue;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
$this->xv[] = $line;
|
|
|
ef5584 |
$this->xind[] = $xi;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
// Find the LCS.
|
|
|
ef5584 |
$this->_compareseq(0, sizeof($this->xv), 0, sizeof($this->yv));
|
|
|
ef5584 |
|
|
|
ef5584 |
// Merge edits when possible.
|
|
|
ef5584 |
$this->_shift_boundaries($from_lines, $this->xchanged, $this->ychanged);
|
|
|
ef5584 |
$this->_shift_boundaries($to_lines, $this->ychanged, $this->xchanged);
|
|
|
ef5584 |
|
|
|
ef5584 |
// Compute the edit operations.
|
|
|
ef5584 |
$edits = array();
|
|
|
ef5584 |
$xi = $yi = 0;
|
|
|
ef5584 |
|
|
|
ef5584 |
while ($xi < $n_from || $yi < $n_to)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
// Skip matching "snake".
|
|
|
ef5584 |
$copy = array();
|
|
|
ef5584 |
|
|
|
ef5584 |
while ($xi < $n_from && $yi < $n_to && !$this->xchanged[$xi] && !$this->ychanged[$yi])
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$copy[] = $from_lines[$xi++];
|
|
|
ef5584 |
$yi++;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
if ($copy)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$edits[] = new diff_op_copy($copy);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
// Find deletes & adds.
|
|
|
ef5584 |
$delete = array();
|
|
|
ef5584 |
while ($xi < $n_from && $this->xchanged[$xi])
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$delete[] = $from_lines[$xi++];
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
$add = array();
|
|
|
ef5584 |
while ($yi < $n_to && $this->ychanged[$yi])
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$add[] = $to_lines[$yi++];
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
if ($delete && $add)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$edits[] = new diff_op_change($delete, $add);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
else if ($delete)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$edits[] = new diff_op_delete($delete);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
else if ($add)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$edits[] = new diff_op_add($add);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
return $edits;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
* Divides the Largest Common Subsequence (LCS) of the sequences (XOFF,
|
|
|
ef5584 |
* XLIM) and (YOFF, YLIM) into NCHUNKS approximately equally sized segments.
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* Returns (LCS, PTS). LCS is the length of the LCS. PTS is an array of
|
|
|
ef5584 |
* NCHUNKS+1 (X, Y) indexes giving the diving points between sub
|
|
|
ef5584 |
* sequences. The first sub-sequence is contained in (X0, X1), (Y0, Y1),
|
|
|
ef5584 |
* the second in (X1, X2), (Y1, Y2) and so on. Note that (X0, Y0) ==
|
|
|
ef5584 |
* (XOFF, YOFF) and (X[NCHUNKS], Y[NCHUNKS]) == (XLIM, YLIM).
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* This function assumes that the first lines of the specified portions of
|
|
|
ef5584 |
* the two files do not match, and likewise that the last lines do not
|
|
|
ef5584 |
* match. The caller must trim matching lines from the beginning and end
|
|
|
ef5584 |
* of the portions it is going to specify.
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
function _diag($xoff, $xlim, $yoff, $ylim, $nchunks)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$flip = false;
|
|
|
ef5584 |
|
|
|
ef5584 |
if ($xlim - $xoff > $ylim - $yoff)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
// Things seems faster (I'm not sure I understand why) when the shortest sequence is in X.
|
|
|
ef5584 |
$flip = true;
|
|
|
ef5584 |
list($xoff, $xlim, $yoff, $ylim) = array($yoff, $ylim, $xoff, $xlim);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
if ($flip)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
for ($i = $ylim - 1; $i >= $yoff; $i--)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$ymatches[$this->xv[$i]][] = $i;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
else
|
|
|
ef5584 |
{
|
|
|
ef5584 |
for ($i = $ylim - 1; $i >= $yoff; $i--)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$ymatches[$this->yv[$i]][] = $i;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
$this->lcs = 0;
|
|
|
ef5584 |
$this->seq[0]= $yoff - 1;
|
|
|
ef5584 |
$this->in_seq = array();
|
|
|
ef5584 |
$ymids[0] = array();
|
|
|
ef5584 |
|
|
|
ef5584 |
$numer = $xlim - $xoff + $nchunks - 1;
|
|
|
ef5584 |
$x = $xoff;
|
|
|
ef5584 |
|
|
|
ef5584 |
for ($chunk = 0; $chunk < $nchunks; $chunk++)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
if ($chunk > 0)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
for ($i = 0; $i <= $this->lcs; $i++)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$ymids[$i][$chunk - 1] = $this->seq[$i];
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
$x1 = $xoff + (int)(($numer + ($xlim - $xoff) * $chunk) / $nchunks);
|
|
|
ef5584 |
|
|
|
ef5584 |
for (; $x < $x1; $x++)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$line = $flip ? $this->yv[$x] : $this->xv[$x];
|
|
|
ef5584 |
if (empty($ymatches[$line]))
|
|
|
ef5584 |
{
|
|
|
ef5584 |
continue;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
$matches = $ymatches[$line];
|
|
|
ef5584 |
|
|
|
ef5584 |
reset($matches);
|
|
|
ef5584 |
while (list(, $y) = each($matches))
|
|
|
ef5584 |
{
|
|
|
ef5584 |
if (empty($this->in_seq[$y]))
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$k = $this->_lcs_pos($y);
|
|
|
ef5584 |
$ymids[$k] = $ymids[$k - 1];
|
|
|
ef5584 |
break;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
// no reset() here
|
|
|
ef5584 |
while (list(, $y) = each($matches))
|
|
|
ef5584 |
{
|
|
|
ef5584 |
if ($y > $this->seq[$k - 1])
|
|
|
ef5584 |
{
|
|
|
ef5584 |
// Optimization: this is a common case: next match is just replacing previous match.
|
|
|
ef5584 |
$this->in_seq[$this->seq[$k]] = false;
|
|
|
ef5584 |
$this->seq[$k] = $y;
|
|
|
ef5584 |
$this->in_seq[$y] = 1;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
else if (empty($this->in_seq[$y]))
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$k = $this->_lcs_pos($y);
|
|
|
ef5584 |
$ymids[$k] = $ymids[$k - 1];
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
$seps[] = $flip ? array($yoff, $xoff) : array($xoff, $yoff);
|
|
|
ef5584 |
$ymid = $ymids[$this->lcs];
|
|
|
ef5584 |
|
|
|
ef5584 |
for ($n = 0; $n < $nchunks - 1; $n++)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$x1 = $xoff + (int)(($numer + ($xlim - $xoff) * $n) / $nchunks);
|
|
|
ef5584 |
$y1 = $ymid[$n] + 1;
|
|
|
ef5584 |
$seps[] = $flip ? array($y1, $x1) : array($x1, $y1);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
$seps[] = $flip ? array($ylim, $xlim) : array($xlim, $ylim);
|
|
|
ef5584 |
|
|
|
ef5584 |
return array($this->lcs, $seps);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
function _lcs_pos($ypos)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$end = $this->lcs;
|
|
|
ef5584 |
|
|
|
ef5584 |
if ($end == 0 || $ypos > $this->seq[$end])
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$this->seq[++$this->lcs] = $ypos;
|
|
|
ef5584 |
$this->in_seq[$ypos] = 1;
|
|
|
ef5584 |
return $this->lcs;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
$beg = 1;
|
|
|
ef5584 |
while ($beg < $end)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$mid = (int)(($beg + $end) / 2);
|
|
|
ef5584 |
if ($ypos > $this->seq[$mid])
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$beg = $mid + 1;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
else
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$end = $mid;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
$this->in_seq[$this->seq[$end]] = false;
|
|
|
ef5584 |
$this->seq[$end] = $ypos;
|
|
|
ef5584 |
$this->in_seq[$ypos] = 1;
|
|
|
ef5584 |
|
|
|
ef5584 |
return $end;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
* Finds LCS of two sequences.
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* The results are recorded in the vectors $this->{x,y}changed[], by
|
|
|
ef5584 |
* storing a 1 in the element for each line that is an insertion or
|
|
|
ef5584 |
* deletion (ie. is not in the LCS).
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* The subsequence of file 0 is (XOFF, XLIM) and likewise for file 1.
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* Note that XLIM, YLIM are exclusive bounds. All line numbers are
|
|
|
ef5584 |
* origin-0 and discarded lines are not counted.
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
function _compareseq($xoff, $xlim, $yoff, $ylim)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
// Slide down the bottom initial diagonal.
|
|
|
ef5584 |
while ($xoff < $xlim && $yoff < $ylim && $this->xv[$xoff] == $this->yv[$yoff])
|
|
|
ef5584 |
{
|
|
|
ef5584 |
++$xoff;
|
|
|
ef5584 |
++$yoff;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
// Slide up the top initial diagonal.
|
|
|
ef5584 |
while ($xlim > $xoff && $ylim > $yoff && $this->xv[$xlim - 1] == $this->yv[$ylim - 1])
|
|
|
ef5584 |
{
|
|
|
ef5584 |
--$xlim;
|
|
|
ef5584 |
--$ylim;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
if ($xoff == $xlim || $yoff == $ylim)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$lcs = 0;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
else
|
|
|
ef5584 |
{
|
|
|
ef5584 |
// This is ad hoc but seems to work well.
|
|
|
ef5584 |
// $nchunks = sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5);
|
|
|
ef5584 |
// $nchunks = max(2,min(8,(int)$nchunks));
|
|
|
ef5584 |
$nchunks = min(7, $xlim - $xoff, $ylim - $yoff) + 1;
|
|
|
ef5584 |
list($lcs, $seps) = $this->_diag($xoff, $xlim, $yoff, $ylim, $nchunks);
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
if ($lcs == 0)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
// X and Y sequences have no common subsequence: mark all changed.
|
|
|
ef5584 |
while ($yoff < $ylim)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$this->ychanged[$this->yind[$yoff++]] = 1;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
while ($xoff < $xlim)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$this->xchanged[$this->xind[$xoff++]] = 1;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
else
|
|
|
ef5584 |
{
|
|
|
ef5584 |
// Use the partitions to split this problem into subproblems.
|
|
|
ef5584 |
reset($seps);
|
|
|
ef5584 |
$pt1 = $seps[0];
|
|
|
ef5584 |
|
|
|
ef5584 |
while ($pt2 = next($seps))
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$this->_compareseq($pt1[0], $pt2[0], $pt1[1], $pt2[1]);
|
|
|
ef5584 |
$pt1 = $pt2;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
/**
|
|
|
ef5584 |
* Adjusts inserts/deletes of identical lines to join changes as much as possible.
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* We do something when a run of changed lines include a line at one end
|
|
|
ef5584 |
* and has an excluded, identical line at the other. We are free to
|
|
|
ef5584 |
* choose which identical line is included. 'compareseq' usually chooses
|
|
|
ef5584 |
* the one at the beginning, but usually it is cleaner to consider the
|
|
|
ef5584 |
* following identical line to be the "change".
|
|
|
ef5584 |
*
|
|
|
ef5584 |
* This is extracted verbatim from analyze.c (GNU diffutils-2.7).
|
|
|
ef5584 |
*/
|
|
|
ef5584 |
function _shift_boundaries($lines, &$changed, $other_changed)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$i = 0;
|
|
|
ef5584 |
$j = 0;
|
|
|
ef5584 |
|
|
|
ef5584 |
$len = sizeof($lines);
|
|
|
ef5584 |
$other_len = sizeof($other_changed);
|
|
|
ef5584 |
|
|
|
ef5584 |
while (1)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
// Scan forward to find the beginning of another run of
|
|
|
ef5584 |
// changes. Also keep track of the corresponding point in the other file.
|
|
|
ef5584 |
//
|
|
|
ef5584 |
// Throughout this code, $i and $j are adjusted together so that
|
|
|
ef5584 |
// the first $i elements of $changed and the first $j elements of
|
|
|
ef5584 |
// $other_changed both contain the same number of zeros (unchanged lines).
|
|
|
ef5584 |
//
|
|
|
ef5584 |
// Furthermore, $j is always kept so that $j == $other_len or $other_changed[$j] == false.
|
|
|
ef5584 |
while ($j < $other_len && $other_changed[$j])
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$j++;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
while ($i < $len && ! $changed[$i])
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$i++;
|
|
|
ef5584 |
$j++;
|
|
|
ef5584 |
|
|
|
ef5584 |
while ($j < $other_len && $other_changed[$j])
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$j++;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
if ($i == $len)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
break;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
$start = $i;
|
|
|
ef5584 |
|
|
|
ef5584 |
// Find the end of this run of changes.
|
|
|
ef5584 |
while (++$i < $len && $changed[$i])
|
|
|
ef5584 |
{
|
|
|
ef5584 |
continue;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
do
|
|
|
ef5584 |
{
|
|
|
ef5584 |
// Record the length of this run of changes, so that we can later determine whether the run has grown.
|
|
|
ef5584 |
$runlength = $i - $start;
|
|
|
ef5584 |
|
|
|
ef5584 |
// Move the changed region back, so long as the previous unchanged line matches the last changed one.
|
|
|
ef5584 |
// This merges with previous changed regions.
|
|
|
ef5584 |
while ($start > 0 && $lines[$start - 1] == $lines[$i - 1])
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$changed[--$start] = 1;
|
|
|
ef5584 |
$changed[--$i] = false;
|
|
|
ef5584 |
|
|
|
ef5584 |
while ($start > 0 && $changed[$start - 1])
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$start--;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
while ($other_changed[--$j])
|
|
|
ef5584 |
{
|
|
|
ef5584 |
continue;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
// Set CORRESPONDING to the end of the changed run, at the last point where it corresponds to a changed run in the
|
|
|
ef5584 |
// other file. CORRESPONDING == LEN means no such point has been found.
|
|
|
ef5584 |
$corresponding = $j < $other_len ? $i : $len;
|
|
|
ef5584 |
|
|
|
ef5584 |
// Move the changed region forward, so long as the first changed line matches the following unchanged one.
|
|
|
ef5584 |
// This merges with following changed regions.
|
|
|
ef5584 |
// Do this second, so that if there are no merges, the changed region is moved forward as far as possible.
|
|
|
ef5584 |
while ($i < $len && $lines[$start] == $lines[$i])
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$changed[$start++] = false;
|
|
|
ef5584 |
$changed[$i++] = 1;
|
|
|
ef5584 |
|
|
|
ef5584 |
while ($i < $len && $changed[$i])
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$i++;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
$j++;
|
|
|
ef5584 |
if ($j < $other_len && $other_changed[$j])
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$corresponding = $i;
|
|
|
ef5584 |
while ($j < $other_len && $other_changed[$j])
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$j++;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
while ($runlength != $i - $start);
|
|
|
ef5584 |
|
|
|
ef5584 |
// If possible, move the fully-merged run of changes back to a corresponding run in the other file.
|
|
|
ef5584 |
while ($corresponding < $i)
|
|
|
ef5584 |
{
|
|
|
ef5584 |
$changed[--$start] = 1;
|
|
|
ef5584 |
$changed[--$i] = 0;
|
|
|
ef5584 |
|
|
|
ef5584 |
while ($other_changed[--$j])
|
|
|
ef5584 |
{
|
|
|
ef5584 |
continue;
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
}
|
|
|
ef5584 |
|
|
|
ef5584 |
?>
|