6 * @copyright (c) 2006 phpBB Group
7 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
14 if (!defined('IN_PHPBB'))
20 * Code from pear.php.net, Text_Diff-1.1.0 package
21 * http://pear.php.net/package/Text_Diff/ (native engine)
23 * Modified by phpBB Group to meet our coding standards
24 * and being able to integrate into phpBB
26 * Class used internally by Text_Diff to actually compute the diffs. This
27 * class is implemented using native PHP code.
29 * The algorithm used here is mostly lifted from the perl module
30 * Algorithm::Diff (version 1.06) by Ned Konz, which is available at:
31 * http://www.perl.com/CPAN/authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip
33 * More ideas are taken from: http://www.ics.uci.edu/~eppstein/161/960229.html
35 * Some ideas (and a bit of code) are taken from analyze.c, of GNU
36 * diffutils-2.7, which can be found at:
37 * ftp://gnudist.gnu.org/pub/gnu/diffutils/diffutils-2.7.tar.gz
39 * Some ideas (subdivision by NCHUNKS > 2, and some optimizations) are from
40 * Geoffrey T. Dairiki <dairiki@dairiki.org>. The original PHP version of this
41 * code was written by him, and is used/adapted with his permission.
43 * Copyright 2004-2008 The Horde Project (http://www.horde.org/)
45 * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
53 * If set to true we trim all lines before we compare them. This ensures that sole space/tab changes do not trigger diffs.
55 var $skip_whitespace_changes = true;
57 function diff(&$from_lines, &$to_lines, $preserve_cr = true)
59 // Remove empty lines...
60 // 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
61 // 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
63 if (is_array($from_lines))
65 $from_lines = implode("\n", $from_lines);
68 if (is_array($to_lines))
70 $to_lines = implode("\n", $to_lines);
75 $from_lines = explode("\n", str_replace("\r", "\n", str_replace("\r\n", "\n", $from_lines)));
76 $to_lines = explode("\n", str_replace("\r", "\n", str_replace("\r\n", "\n", $to_lines)));
80 $from_lines = explode("\n", preg_replace('#[\n\r]+#', "\n", $from_lines));
81 $to_lines = explode("\n", preg_replace('#[\n\r]+#', "\n", $to_lines));
84 $n_from = sizeof($from_lines);
85 $n_to = sizeof($to_lines);
87 $this->xchanged = $this->ychanged = $this->xv = $this->yv = $this->xind = $this->yind = array();
88 unset($this->seq, $this->in_seq, $this->lcs);
90 // Skip leading common lines.
91 for ($skip = 0; $skip < $n_from && $skip < $n_to; $skip++)
93 if (trim($from_lines[$skip]) !== trim($to_lines[$skip]))
97 $this->xchanged[$skip] = $this->ychanged[$skip] = false;
100 // Skip trailing common lines.
104 for ($endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++)
106 if (trim($from_lines[$xi]) !== trim($to_lines[$yi]))
110 $this->xchanged[$xi] = $this->ychanged[$yi] = false;
113 // Ignore lines which do not exist in both files.
114 for ($xi = $skip; $xi < $n_from - $endskip; $xi++)
116 if ($this->skip_whitespace_changes) $xhash[trim($from_lines[$xi])] = 1; else $xhash[$from_lines[$xi]] = 1;
119 for ($yi = $skip; $yi < $n_to - $endskip; $yi++)
121 $line = ($this->skip_whitespace_changes) ? trim($to_lines[$yi]) : $to_lines[$yi];
123 if (($this->ychanged[$yi] = empty($xhash[$line])))
132 for ($xi = $skip; $xi < $n_from - $endskip; $xi++)
134 $line = ($this->skip_whitespace_changes) ? trim($from_lines[$xi]) : $from_lines[$xi];
136 if (($this->xchanged[$xi] = empty($yhash[$line])))
145 $this->_compareseq(0, sizeof($this->xv), 0, sizeof($this->yv));
147 // Merge edits when possible.
148 if ($this->skip_whitespace_changes)
150 $from_lines_clean = array_map('trim', $from_lines);
151 $to_lines_clean = array_map('trim', $to_lines);
153 $this->_shift_boundaries($from_lines_clean, $this->xchanged, $this->ychanged);
154 $this->_shift_boundaries($to_lines_clean, $this->ychanged, $this->xchanged);
156 unset($from_lines_clean, $to_lines_clean);
160 $this->_shift_boundaries($from_lines, $this->xchanged, $this->ychanged);
161 $this->_shift_boundaries($to_lines, $this->ychanged, $this->xchanged);
164 // Compute the edit operations.
168 while ($xi < $n_from || $yi < $n_to)
170 // Skip matching "snake".
173 while ($xi < $n_from && $yi < $n_to && !$this->xchanged[$xi] && !$this->ychanged[$yi])
175 $copy[] = $from_lines[$xi++];
181 $edits[] = new diff_op_copy($copy);
184 // Find deletes & adds.
186 while ($xi < $n_from && $this->xchanged[$xi])
188 $delete[] = $from_lines[$xi++];
192 while ($yi < $n_to && $this->ychanged[$yi])
194 $add[] = $to_lines[$yi++];
199 $edits[] = new diff_op_change($delete, $add);
203 $edits[] = new diff_op_delete($delete);
207 $edits[] = new diff_op_add($add);
215 * Divides the Largest Common Subsequence (LCS) of the sequences (XOFF,
216 * XLIM) and (YOFF, YLIM) into NCHUNKS approximately equally sized segments.
218 * Returns (LCS, PTS). LCS is the length of the LCS. PTS is an array of
219 * NCHUNKS+1 (X, Y) indexes giving the diving points between sub
220 * sequences. The first sub-sequence is contained in (X0, X1), (Y0, Y1),
221 * the second in (X1, X2), (Y1, Y2) and so on. Note that (X0, Y0) ==
222 * (XOFF, YOFF) and (X[NCHUNKS], Y[NCHUNKS]) == (XLIM, YLIM).
224 * This function assumes that the first lines of the specified portions of
225 * the two files do not match, and likewise that the last lines do not
226 * match. The caller must trim matching lines from the beginning and end
227 * of the portions it is going to specify.
229 function _diag($xoff, $xlim, $yoff, $ylim, $nchunks)
233 if ($xlim - $xoff > $ylim - $yoff)
235 // Things seems faster (I'm not sure I understand why) when the shortest sequence is in X.
237 list($xoff, $xlim, $yoff, $ylim) = array($yoff, $ylim, $xoff, $xlim);
242 for ($i = $ylim - 1; $i >= $yoff; $i--)
244 $ymatches[$this->xv[$i]][] = $i;
249 for ($i = $ylim - 1; $i >= $yoff; $i--)
251 $ymatches[$this->yv[$i]][] = $i;
256 $this->seq[0]= $yoff - 1;
257 $this->in_seq = array();
260 $numer = $xlim - $xoff + $nchunks - 1;
263 for ($chunk = 0; $chunk < $nchunks; $chunk++)
267 for ($i = 0; $i <= $this->lcs; $i++)
269 $ymids[$i][$chunk - 1] = $this->seq[$i];
273 $x1 = $xoff + (int)(($numer + ($xlim - $xoff) * $chunk) / $nchunks);
275 for (; $x < $x1; $x++)
277 $line = $flip ? $this->yv[$x] : $this->xv[$x];
278 if (empty($ymatches[$line]))
282 $matches = $ymatches[$line];
285 while (list(, $y) = each($matches))
287 if (empty($this->in_seq[$y]))
289 $k = $this->_lcs_pos($y);
290 $ymids[$k] = $ymids[$k - 1];
296 while (list(, $y) = each($matches))
298 if ($y > $this->seq[$k - 1])
300 // Optimization: this is a common case: next match is just replacing previous match.
301 $this->in_seq[$this->seq[$k]] = false;
303 $this->in_seq[$y] = 1;
305 else if (empty($this->in_seq[$y]))
307 $k = $this->_lcs_pos($y);
308 $ymids[$k] = $ymids[$k - 1];
314 $seps[] = $flip ? array($yoff, $xoff) : array($xoff, $yoff);
315 $ymid = $ymids[$this->lcs];
317 for ($n = 0; $n < $nchunks - 1; $n++)
319 $x1 = $xoff + (int)(($numer + ($xlim - $xoff) * $n) / $nchunks);
321 $seps[] = $flip ? array($y1, $x1) : array($x1, $y1);
323 $seps[] = $flip ? array($ylim, $xlim) : array($xlim, $ylim);
325 return array($this->lcs, $seps);
328 function _lcs_pos($ypos)
332 if ($end == 0 || $ypos > $this->seq[$end])
334 $this->seq[++$this->lcs] = $ypos;
335 $this->in_seq[$ypos] = 1;
342 $mid = (int)(($beg + $end) / 2);
343 if ($ypos > $this->seq[$mid])
353 $this->in_seq[$this->seq[$end]] = false;
354 $this->seq[$end] = $ypos;
355 $this->in_seq[$ypos] = 1;
361 * Finds LCS of two sequences.
363 * The results are recorded in the vectors $this->{x,y}changed[], by
364 * storing a 1 in the element for each line that is an insertion or
365 * deletion (ie. is not in the LCS).
367 * The subsequence of file 0 is (XOFF, XLIM) and likewise for file 1.
369 * Note that XLIM, YLIM are exclusive bounds. All line numbers are
370 * origin-0 and discarded lines are not counted.
372 function _compareseq($xoff, $xlim, $yoff, $ylim)
374 // Slide down the bottom initial diagonal.
375 while ($xoff < $xlim && $yoff < $ylim && $this->xv[$xoff] == $this->yv[$yoff])
381 // Slide up the top initial diagonal.
382 while ($xlim > $xoff && $ylim > $yoff && $this->xv[$xlim - 1] == $this->yv[$ylim - 1])
388 if ($xoff == $xlim || $yoff == $ylim)
394 // This is ad hoc but seems to work well.
395 // $nchunks = sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5);
396 // $nchunks = max(2,min(8,(int)$nchunks));
397 $nchunks = min(7, $xlim - $xoff, $ylim - $yoff) + 1;
398 list($lcs, $seps) = $this->_diag($xoff, $xlim, $yoff, $ylim, $nchunks);
403 // X and Y sequences have no common subsequence: mark all changed.
404 while ($yoff < $ylim)
406 $this->ychanged[$this->yind[$yoff++]] = 1;
409 while ($xoff < $xlim)
411 $this->xchanged[$this->xind[$xoff++]] = 1;
416 // Use the partitions to split this problem into subproblems.
420 while ($pt2 = next($seps))
422 $this->_compareseq($pt1[0], $pt2[0], $pt1[1], $pt2[1]);
429 * Adjusts inserts/deletes of identical lines to join changes as much as possible.
431 * We do something when a run of changed lines include a line at one end
432 * and has an excluded, identical line at the other. We are free to
433 * choose which identical line is included. 'compareseq' usually chooses
434 * the one at the beginning, but usually it is cleaner to consider the
435 * following identical line to be the "change".
437 * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
439 function _shift_boundaries($lines, &$changed, $other_changed)
444 $len = sizeof($lines);
445 $other_len = sizeof($other_changed);
449 // Scan forward to find the beginning of another run of
450 // changes. Also keep track of the corresponding point in the other file.
452 // Throughout this code, $i and $j are adjusted together so that
453 // the first $i elements of $changed and the first $j elements of
454 // $other_changed both contain the same number of zeros (unchanged lines).
456 // Furthermore, $j is always kept so that $j == $other_len or $other_changed[$j] == false.
457 while ($j < $other_len && $other_changed[$j])
462 while ($i < $len && ! $changed[$i])
467 while ($j < $other_len && $other_changed[$j])
480 // Find the end of this run of changes.
481 while (++$i < $len && $changed[$i])
488 // Record the length of this run of changes, so that we can later determine whether the run has grown.
489 $runlength = $i - $start;
491 // Move the changed region back, so long as the previous unchanged line matches the last changed one.
492 // This merges with previous changed regions.
493 while ($start > 0 && $lines[$start - 1] == $lines[$i - 1])
495 $changed[--$start] = 1;
496 $changed[--$i] = false;
498 while ($start > 0 && $changed[$start - 1])
503 while ($other_changed[--$j])
509 // Set CORRESPONDING to the end of the changed run, at the last point where it corresponds to a changed run in the
510 // other file. CORRESPONDING == LEN means no such point has been found.
511 $corresponding = $j < $other_len ? $i : $len;
513 // Move the changed region forward, so long as the first changed line matches the following unchanged one.
514 // This merges with following changed regions.
515 // Do this second, so that if there are no merges, the changed region is moved forward as far as possible.
516 while ($i < $len && $lines[$start] == $lines[$i])
518 $changed[$start++] = false;
521 while ($i < $len && $changed[$i])
527 if ($j < $other_len && $other_changed[$j])
530 while ($j < $other_len && $other_changed[$j])
537 while ($runlength != $i - $start);
539 // If possible, move the fully-merged run of changes back to a corresponding run in the other file.
540 while ($corresponding < $i)
542 $changed[--$start] = 1;
545 while ($other_changed[--$j])