Blame Identity/Webenv/App/phpBB/3.0.4/includes/diff/engine.php

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