some diff engine updates (only minor, but will hopefully not break anything)

git-svn-id: file:///svn/phpbb/branches/phpBB-3_0_0@8692 89ea8834-ac86-4346-8a33-228a782c2dd0
This commit is contained in:
Meik Sievertsen 2008-07-28 13:27:08 +00:00
parent 8904d95d59
commit bba6488d3d
3 changed files with 46 additions and 21 deletions

View file

@ -17,7 +17,7 @@ if (!defined('IN_PHPBB'))
} }
/** /**
* Code from pear.php.net, Text_Diff-0.2.1 (beta) package * Code from pear.php.net, Text_Diff-1.0.0 package
* http://pear.php.net/package/Text_Diff/ * http://pear.php.net/package/Text_Diff/
* *
* Modified by phpBB Group to meet our coding standards * Modified by phpBB Group to meet our coding standards
@ -26,6 +26,9 @@ if (!defined('IN_PHPBB'))
* General API for generating and formatting diffs - the differences between * General API for generating and formatting diffs - the differences between
* two sequences of strings. * two sequences of strings.
* *
* Copyright 2004 Geoffrey T. Dairiki <dairiki@dairiki.org>
* Copyright 2004-2008 The Horde Project (http://www.horde.org/)
*
* @package diff * @package diff
* @author Geoffrey T. Dairiki <dairiki@dairiki.org> * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
*/ */
@ -45,7 +48,7 @@ class diff
*/ */
function diff(&$from_content, &$to_content, $preserve_cr = true) function diff(&$from_content, &$to_content, $preserve_cr = true)
{ {
$diff_engine = &new diff_engine(); $diff_engine = new diff_engine();
$this->_edits = $diff_engine->diff($from_content, $to_content, $preserve_cr); $this->_edits = $diff_engine->diff($from_content, $to_content, $preserve_cr);
} }
@ -62,7 +65,7 @@ class diff
* *
* Example: * Example:
* <code> * <code>
* $diff = &new diff($lines1, $lines2); * $diff = new diff($lines1, $lines2);
* $rev = $diff->reverse(); * $rev = $diff->reverse();
* </code> * </code>
* *
@ -285,7 +288,7 @@ class diff_op
var $orig; var $orig;
var $final; var $final;
function reverse() function &reverse()
{ {
trigger_error('[diff] Abstract method', E_USER_ERROR); trigger_error('[diff] Abstract method', E_USER_ERROR);
} }
@ -413,7 +416,7 @@ class diff3 extends diff
*/ */
function diff3(&$orig, &$final1, &$final2) function diff3(&$orig, &$final1, &$final2)
{ {
$diff_engine = &new diff_engine(); $diff_engine = new diff_engine();
$diff_1 = $diff_engine->diff($orig, $final1); $diff_1 = $diff_engine->diff($orig, $final1);
$diff_2 = $diff_engine->diff($orig, $final2); $diff_2 = $diff_engine->diff($orig, $final2);
@ -548,7 +551,7 @@ class diff3 extends diff
function _diff3(&$edits1, &$edits2) function _diff3(&$edits1, &$edits2)
{ {
$edits = array(); $edits = array();
$bb = &new diff3_block_builder(); $bb = new diff3_block_builder();
$e1 = current($edits1); $e1 = current($edits1);
$e2 = current($edits2); $e2 = current($edits2);
@ -565,7 +568,7 @@ class diff3 extends diff
} }
$ncopy = min($e1->norig(), $e2->norig()); $ncopy = min($e1->norig(), $e2->norig());
$edits[] = &new diff3_op_copy(array_slice($e1->orig, 0, $ncopy)); $edits[] = new diff3_op_copy(array_slice($e1->orig, 0, $ncopy));
if ($e1->norig() > $ncopy) if ($e1->norig() > $ncopy)
{ {
@ -759,7 +762,7 @@ class diff3_block_builder
} }
else else
{ {
$edit = &new diff3_op($this->orig, $this->final1, $this->final2); $edit = new diff3_op($this->orig, $this->final1, $this->final2);
$this->_init(); $this->_init();
return $edit; return $edit;
} }

View file

@ -17,21 +17,20 @@ if (!defined('IN_PHPBB'))
} }
/** /**
* Code from pear.php.net, Text_Diff-0.2.1 (beta) package * Code from pear.php.net, Text_Diff-1.0.0 package
* http://pear.php.net/package/Text_Diff/ * http://pear.php.net/package/Text_Diff/ (native engine)
* *
* Modified by phpBB Group to meet our coding standards * Modified by phpBB Group to meet our coding standards
* and being able to integrate into phpBB * and being able to integrate into phpBB
* *
* Class used internally by Diff to actually compute the diffs. This class is * Class used internally by Text_Diff to actually compute the diffs. This
* implemented using native PHP code. * class is implemented using native PHP code.
* *
* The algorithm used here is mostly lifted from the perl module * The algorithm used here is mostly lifted from the perl module
* Algorithm::Diff (version 1.06) by Ned Konz, which is available at: * 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 * http://www.perl.com/CPAN/authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip
* *
* More ideas are taken from: * More ideas are taken from: http://www.ics.uci.edu/~eppstein/161/960229.html
* http://www.ics.uci.edu/~eppstein/161/960229.html
* *
* Some ideas (and a bit of code) are taken from analyze.c, of GNU * Some ideas (and a bit of code) are taken from analyze.c, of GNU
* diffutils-2.7, which can be found at: * diffutils-2.7, which can be found at:
@ -41,6 +40,8 @@ if (!defined('IN_PHPBB'))
* Geoffrey T. Dairiki <dairiki@dairiki.org>. The original PHP version of this * Geoffrey T. Dairiki <dairiki@dairiki.org>. The original PHP version of this
* code was written by him, and is used/adapted with his permission. * code was written by him, and is used/adapted with his permission.
* *
* Copyright 2004-2008 The Horde Project (http://www.horde.org/)
*
* @author Geoffrey T. Dairiki <dairiki@dairiki.org> * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
* @package diff * @package diff
* *
@ -262,7 +263,8 @@ class diff_engine
} }
$matches = $ymatches[$line]; $matches = $ymatches[$line];
foreach ($matches as $y) reset($matches);
while (list(, $y) = each($matches))
{ {
if (empty($this->in_seq[$y])) if (empty($this->in_seq[$y]))
{ {
@ -273,7 +275,7 @@ class diff_engine
} }
// no reset() here // no reset() here
while (list($junk, $y) = each($matches)) while (list(, $y) = each($matches))
{ {
if ($y > $this->seq[$k - 1]) if ($y > $this->seq[$k - 1])
{ {

View file

@ -17,7 +17,7 @@ if (!defined('IN_PHPBB'))
} }
/** /**
* Code from pear.php.net, Text_Diff-0.2.1 (beta) package * Code from pear.php.net, Text_Diff-1.0.0 package
* http://pear.php.net/package/Text_Diff/ * http://pear.php.net/package/Text_Diff/
* *
* Modified by phpBB Group to meet our coding standards * Modified by phpBB Group to meet our coding standards
@ -28,6 +28,8 @@ if (!defined('IN_PHPBB'))
* This class renders the diff in classic diff format. It is intended that * This class renders the diff in classic diff format. It is intended that
* this class be customized via inheritance, to obtain fancier outputs. * this class be customized via inheritance, to obtain fancier outputs.
* *
* Copyright 2004-2008 The Horde Project (http://www.horde.org/)
*
* @package diff * @package diff
*/ */
class diff_renderer class diff_renderer
@ -105,7 +107,7 @@ class diff_renderer
unset($diff3); unset($diff3);
$diff = &new diff($diff_1, $diff_2); $diff = new diff($diff_1, $diff_2);
} }
$nlead = $this->_leading_context_lines; $nlead = $this->_leading_context_lines;
@ -116,19 +118,24 @@ class diff_renderer
foreach ($diffs as $i => $edit) foreach ($diffs as $i => $edit)
{ {
// If these are unchanged (copied) lines, and we want to keep leading or trailing context lines, extract them from the copy block.
if (is_a($edit, 'diff_op_copy')) if (is_a($edit, 'diff_op_copy'))
{ {
// Do we have any diff blocks yet?
if (is_array($block)) if (is_array($block))
{ {
// How many lines to keep as context from the copy block.
$keep = ($i == sizeof($diffs) - 1) ? $ntrail : $nlead + $ntrail; $keep = ($i == sizeof($diffs) - 1) ? $ntrail : $nlead + $ntrail;
if (sizeof($edit->orig) <= $keep) if (sizeof($edit->orig) <= $keep)
{ {
// We have less lines in the block than we want for context => keep the whole block.
$block[] = $edit; $block[] = $edit;
} }
else else
{ {
if ($ntrail) if ($ntrail)
{ {
// Create a new block with as many lines as we need for the trailing context.
$context = array_slice($edit->orig, 0, $ntrail); $context = array_slice($edit->orig, 0, $ntrail);
$block[] = &new diff_op_copy($context); $block[] = &new diff_op_copy($context);
} }
@ -137,12 +144,15 @@ class diff_renderer
$block = false; $block = false;
} }
} }
// Keep the copy block as the context for the next block.
$context = $edit->orig; $context = $edit->orig;
} }
else else
{ {
// Don't we have any diff blocks yet?
if (!is_array($block)) if (!is_array($block))
{ {
// Extract context lines from the preceding copy block.
$context = array_slice($context, sizeof($context) - $nlead); $context = array_slice($context, sizeof($context) - $nlead);
$x0 = $xi - sizeof($context); $x0 = $xi - sizeof($context);
$y0 = $yi - sizeof($context); $y0 = $yi - sizeof($context);
@ -219,6 +229,16 @@ class diff_renderer
$ybeg .= ',' . ($ybeg + $ylen - 1); $ybeg .= ',' . ($ybeg + $ylen - 1);
} }
// this matches the GNU Diff behaviour
if ($xlen && !$ylen)
{
$ybeg--;
}
else if (!$xlen)
{
$xbeg--;
}
return $xbeg . ($xlen ? ($ylen ? 'c' : 'd') : 'a') . $ybeg; return $xbeg . ($xlen ? ($ylen ? 'c' : 'd') : 'a') . $ybeg;
} }
@ -449,11 +469,11 @@ class diff_renderer_inline extends diff_renderer
$splitted_text_1 = $this->_split_on_words($text1, $nl); $splitted_text_1 = $this->_split_on_words($text1, $nl);
$splitted_text_2 = $this->_split_on_words($text2, $nl); $splitted_text_2 = $this->_split_on_words($text2, $nl);
$diff = &new diff($splitted_text_1, $splitted_text_2); $diff = new diff($splitted_text_1, $splitted_text_2);
unset($splitted_text_1, $splitted_text_2); unset($splitted_text_1, $splitted_text_2);
// Get the diff in inline format. // Get the diff in inline format.
$renderer = &new diff_renderer_inline(array_merge($this->get_params(), array('split_level' => 'words'))); $renderer = new diff_renderer_inline(array_merge($this->get_params(), array('split_level' => 'words')));
// Run the diff and get the output. // Run the diff and get the output.
return str_replace($nl, "\n", $renderer->render($diff)) . "\n"; return str_replace($nl, "\n", $renderer->render($diff)) . "\n";