Blame Extras/phpBB/3.0.4/includes/diff/engine.php

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