PEAR is archived and read-only

This mirror preserves historical PEAR package releases and metadata so existing references remain available.

Home » Text » Text_Diff » Bug #7839

wrong output by render function of Renderer.php

Details

Submitted2006-06-08 17:19 UTC
Fromviraj dot kanwade at yahoo dot com
Assignedchagenbu
StatusClosed
PackageText_Diff
PHP Version5.0.1
OSWindows XP
Roadmaps(Not assigned)

Comments

[2006-06-08 17:19 UTC] viraj dot kanwade at yahoo dot com

Description:
------------
I am using Renderer.php for generating a output similar to a diff (GNU diffutils 2.8.4).The getDiff() function returns correct diff arrays. But the diff displayed by render() function is wrong. I have changed the render function to generate the correct output. Also a few changes to _lines, _added, _deleted and _changed for formatting. Where and how can I submit my code for review?

Test script:
---------------
1.txt
This is line 1.
This is line 2.
This is line 3.
This is line 4.
This is line 5.
This is line 6.
This is line 7.
This is line 8.
This is line 9.

==========================================
2.txt
This is line 1.
This was line 2.
This is line 3.
This is line 5.
This was line 6.
This was line 7.
This was line 8.
This is line 9.
This is line 10.

Expected result:
----------------
2c2
< This is line 2.
---
> This was line 2.
4d3
< This is line 4.
6,8c5,7
< This is line 6.
< This is line 7.
< This is line 8.
---
> This was line 6.
> This was line 7.
> This was line 8.
9a9
> This is line 10.

Actual result:
--------------
2c2 This was line 2. 4d4 This was line 6. >This was line 7. >This was line 8. 10a9 >This is line 10.

[2006-06-08 17:23 UTC] chagenbu at php dot net

Best would be if you could post a link to a unified diff here, but put the text file elsewhere; if that's not possible, paste the diff into the ticket.

[2006-06-08 17:33 UTC] viraj dot kanwade at yahoo dot com

I dont have some place to upload the file or the diff. I will paste the functions here. Kindly adjust.

function render($diff)
{
$output = "";
$diffs = $diff->getDiff();
$x0 = 0;$y0 = 0;$x1 = 0;$y1 = 0;
foreach ($diffs as $i => $edit) {
switch (strtolower(get_class($edit))) {
case 'text_diff_op_copy':
$x0 += count($edit->orig);
$x1 += count($edit->orig);
break;
case 'text_diff_op_add':
$y1 = count($edit->final);
$output .= $this->_blockHeader($x0,0,$x1+1,$y1);
$output .= $this->_added($edit->final);
$x1 += $y1;
break;
case 'text_diff_op_delete':
$y0 = count($edit->orig);
$output .= $this->_blockHeader($x0+1,$y0,$x1,0);
$output .= $this->_deleted($edit->orig);
$x0 += $y0;
break;
case 'text_diff_op_change':
$y0 = count($edit->orig);
$y1 = count($edit->final);
$output .= $this->_blockHeader($x0+1,$y0,$x1+1,$y1);
$output .=$this->_changed($edit->orig,$edit->final);
$x0 += $y0;
$x1 += $y1;
break;
}
}
return "$output";
}

function _lines($lines, $prefix = ' ')
{
return $prefix . implode("<br>$prefix", $lines) . "<br>";
}

function _added($lines)
{
return $this->_lines($lines, '> ');
}

function _deleted($lines)
{
return $this->_lines($lines, '< ');
}

function _changed($orig, $final)
{
return $this->_deleted($orig)."---<br>".$this->_added($final);
}

[2006-06-08 17:48 UTC] chagenbu at php dot net

If you can post a diff instead this will get looked at a lot sooner. Otherwise, we'll get to it when we can.

Thanks.

[2006-06-09 04:35 UTC] viraj dot kanwade at yahoo dot com

Sorry, but I could not understand the logic in your render function. So I wrote a new function.
Also when the output is rendered, the newline doesnt get print. So when the output is to be rendered use
echo nl2br($renderer->render($diff));
instead of
echo $renderer->render($diff);

67,68c67,75
< function render($diff)
< {
---
> function render($diff)
> {
> $xi = $yi = 1;
> $block = false;
> $context = array();
>
> $nlead = $this->_leading_context_lines;
> $ntrail = $this->_trailing_context_lines;
>
71a79,108
> foreach ($diffs as $i => $edit) {
> if (is_a($edit, 'Text_Diff_Op_copy')) {
> if (is_array($block)) {
> $keep = $i == count($diffs) - 1 ? $ntrail : $nlead + $ntrail;
> if (count($edit->orig) <= $keep) {
> $block[] = $edit;
> } else {
> if ($ntrail) {
> $context = array_slice($edit->orig, 0, $ntrail);
> $block[] = &new Text_Diff_Op_copy($context);
> }
> $output .= $this->_block($x0, $ntrail + $xi - $x0,
> $y0, $ntrail + $yi - $y0,
> $block);
> $block = false;
> }
> }
> $context = $edit->orig;
> } else {
> if (!is_array($block)) {
> $context = array_slice($context, count($context) - $nlead);
> $x0 = $xi - count($context);
> $y0 = $yi - count($context);
> $block = array();
> if ($context) {
> $block[] = &new Text_Diff_Op_copy($context);
> }
> }
> $block[] = $edit;
> }
73,89c110,116
< $x0 = 0;
< $y0 = 0;
< $x1 = 0;
< $y1 = 0;
< foreach ($diffs as $i => $edit) {
< switch (strtolower(get_class($edit))) {
< case 'text_diff_op_copy':
< $x0 += count($edit->orig);
< $x1 += count($edit->orig);
< break;
<
< case 'text_diff_op_add':
< $y1 = count($edit->final);
< $output .= $this->_startBlock($this->_blockHeader($x0,0,$x1+1,$y1));
< $output .= $this->_added($edit->final);
< $x1 += $y1;
< break;
---
> if ($edit->orig) {
> $xi += count($edit->orig);
> }
> if ($edit->final) {
> $yi += count($edit->final);
> }
> }
91,96c118,122
< case 'text_diff_op_delete':
< $y0 = count($edit->orig);
< $output .= $this->_startBlock($this->_blockHeader($x0+1,$y0,$x1,0));
< $output .= $this->_deleted($edit->orig);
< $x0 += $y0;
< break;
---
> if (is_array($block)) {
> $output .= $this->_block($x0, $xi - $x0,
> $y0, $yi - $y0,
> $block);
> }
98,108c124
< case 'text_diff_op_change':
< $y0 = count($edit->orig);
< $y1 = count($edit->final);
< $output .= $this->_startBlock($this->_blockHeader($x0+1,$y0,$x1+1,$y1));
< $output .= $this->_changed($edit->orig, $edit->final);
< $x0 += $y0;
< $x1 += $y1;
< break;
< }
< }
< return $output . $this->_endDiff();
---
> return $output . $this->_endDiff();
182c198
< return $this->_lines($lines, '> ');
---
> return $this->_lines($lines, '>');
187c203
< return $this->_lines($lines, '< ');
---
> return $this->_lines($lines, '<');

[2006-07-01 11:08 UTC] viraj dot kanwade at yahoo dot com

hey i forgot to add htmlspecialchars to the code.
The line to be printed also needs to be passed through htmlspecialchars.

[2006-07-01 11:16 UTC] viraj dot kanwade at yahoo dot com

What happened about my code changes? Did you not find it suitable?