Blame Identity/Models/Html/phpBB/3.0.4/includes/diff/engine.php

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