6 // A PHP diff engine for phpwiki.
8 // Copyright (C) 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org>
9 // You may copy this code freely under the conditions of the GPL.
12 // FIXME: possibly remove assert()'s for production version?
14 // PHP3 does not have assert()
15 define('USE_ASSERTS', function_exists('assert'));
23 trigger_error("pure virtual", E_USER_ERROR);
27 return $this->orig ? sizeof($this->orig) : 0;
31 return $this->final ? sizeof($this->final) : 0;
35 class _DiffOp_Copy extends _DiffOp {
38 function _DiffOp_Copy ($orig, $final = false) {
39 if (!is_array($final))
42 $this->final = $final;
46 return new _DiffOp_Copy($this->final, $this->orig);
50 class _DiffOp_Delete extends _DiffOp {
53 function _DiffOp_Delete ($lines) {
59 return new _DiffOp_Add($this->orig);
63 class _DiffOp_Add extends _DiffOp {
66 function _DiffOp_Add ($lines) {
67 $this->final = $lines;
72 return new _DiffOp_Delete($this->final);
76 class _DiffOp_Change extends _DiffOp {
79 function _DiffOp_Change ($orig, $final) {
81 $this->final = $final;
85 return new _DiffOp_Change($this->final, $this->orig);
91 * Class used internally by Diff to actually compute the diffs.
93 * The algorithm used here is mostly lifted from the perl module
94 * Algorithm::Diff (version 1.06) by Ned Konz, which is available at:
95 * http://www.perl.com/CPAN/authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip
97 * More ideas are taken from:
98 * http://www.ics.uci.edu/~eppstein/161/960229.html
100 * Some ideas are (and a bit of code) are from from analyze.c, from GNU
101 * diffutils-2.7, which can be found at:
102 * ftp://gnudist.gnu.org/pub/gnu/diffutils/diffutils-2.7.tar.gz
104 * Finally, some ideas (subdivision by NCHUNKS > 2, and some optimizations)
107 * @author Geoffrey T. Dairiki
112 function diff ($from_lines, $to_lines) {
113 $n_from = sizeof($from_lines);
114 $n_to = sizeof($to_lines);
116 $this->xchanged = $this->ychanged = array();
117 $this->xv = $this->yv = array();
118 $this->xind = $this->yind = array();
120 unset($this->in_seq);
123 // Skip leading common lines.
124 for ($skip = 0; $skip < $n_from && $skip < $n_to; $skip++) {
125 if ($from_lines[$skip] != $to_lines[$skip])
127 $this->xchanged[$skip] = $this->ychanged[$skip] = false;
129 // Skip trailing common lines.
130 $xi = $n_from; $yi = $n_to;
131 for ($endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++) {
132 if ($from_lines[$xi] != $to_lines[$yi])
134 $this->xchanged[$xi] = $this->ychanged[$yi] = false;
137 // Ignore lines which do not exist in both files.
138 for ($xi = $skip; $xi < $n_from - $endskip; $xi++)
139 $xhash[$from_lines[$xi]] = 1;
140 for ($yi = $skip; $yi < $n_to - $endskip; $yi++) {
141 $line = $to_lines[$yi];
142 if ( ($this->ychanged[$yi] = empty($xhash[$line])) )
148 for ($xi = $skip; $xi < $n_from - $endskip; $xi++) {
149 $line = $from_lines[$xi];
150 if ( ($this->xchanged[$xi] = empty($yhash[$line])) )
157 $this->_compareseq(0, sizeof($this->xv), 0, sizeof($this->yv));
159 // Merge edits when possible
160 $this->_shift_boundaries($from_lines, $this->xchanged, $this->ychanged);
161 $this->_shift_boundaries($to_lines, $this->ychanged, $this->xchanged);
163 // Compute the edit operations.
166 while ($xi < $n_from || $yi < $n_to) {
167 USE_ASSERTS && assert($yi < $n_to || $this->xchanged[$xi]);
168 USE_ASSERTS && assert($xi < $n_from || $this->ychanged[$yi]);
170 // Skip matching "snake".
172 while ( $xi < $n_from && $yi < $n_to
173 && !$this->xchanged[$xi] && !$this->ychanged[$yi]) {
174 $copy[] = $from_lines[$xi++];
178 $edits[] = new _DiffOp_Copy($copy);
180 // Find deletes & adds.
182 while ($xi < $n_from && $this->xchanged[$xi])
183 $delete[] = $from_lines[$xi++];
186 while ($yi < $n_to && $this->ychanged[$yi])
187 $add[] = $to_lines[$yi++];
190 $edits[] = new _DiffOp_Change($delete, $add);
192 $edits[] = new _DiffOp_Delete($delete);
194 $edits[] = new _DiffOp_Add($add);
200 /* Divide the Largest Common Subsequence (LCS) of the sequences
201 * [XOFF, XLIM) and [YOFF, YLIM) into NCHUNKS approximately equally
204 * Returns (LCS, PTS). LCS is the length of the LCS. PTS is an
205 * array of NCHUNKS+1 (X, Y) indexes giving the diving points between
206 * sub sequences. The first sub-sequence is contained in [X0, X1),
207 * [Y0, Y1), the second in [X1, X2), [Y1, Y2) and so on. Note
208 * that (X0, Y0) == (XOFF, YOFF) and
209 * (X[NCHUNKS], Y[NCHUNKS]) == (XLIM, YLIM).
211 * This function assumes that the first lines of the specified portions
212 * of the two files do not match, and likewise that the last lines do not
213 * match. The caller must trim matching lines from the beginning and end
214 * of the portions it is going to specify.
216 function _diag ($xoff, $xlim, $yoff, $ylim, $nchunks) {
219 if ($xlim - $xoff > $ylim - $yoff) {
220 // Things seems faster (I'm not sure I understand why)
221 // when the shortest sequence in X.
223 list ($xoff, $xlim, $yoff, $ylim)
224 = array( $yoff, $ylim, $xoff, $xlim);
228 for ($i = $ylim - 1; $i >= $yoff; $i--)
229 $ymatches[$this->xv[$i]][] = $i;
231 for ($i = $ylim - 1; $i >= $yoff; $i--)
232 $ymatches[$this->yv[$i]][] = $i;
235 $this->seq[0]= $yoff - 1;
236 $this->in_seq = array();
239 $numer = $xlim - $xoff + $nchunks - 1;
241 for ($chunk = 0; $chunk < $nchunks; $chunk++) {
243 for ($i = 0; $i <= $this->lcs; $i++)
244 $ymids[$i][$chunk-1] = $this->seq[$i];
246 $x1 = $xoff + (int)(($numer + ($xlim-$xoff)*$chunk) / $nchunks);
247 for ( ; $x < $x1; $x++) {
248 $line = $flip ? $this->yv[$x] : $this->xv[$x];
249 if (empty($ymatches[$line]))
251 $matches = $ymatches[$line];
253 while (list ($junk, $y) = each($matches))
254 if (empty($this->in_seq[$y])) {
255 $k = $this->_lcs_pos($y);
256 USE_ASSERTS && assert($k > 0);
257 $ymids[$k] = $ymids[$k-1];
260 while (list ($junk, $y) = each($matches)) {
261 if ($y > $this->seq[$k-1]) {
262 USE_ASSERTS && assert($y < $this->seq[$k]);
263 // Optimization: this is a common case:
264 // next match is just replacing previous match.
265 $this->in_seq[$this->seq[$k]] = false;
267 $this->in_seq[$y] = 1;
269 else if (empty($this->in_seq[$y])) {
270 $k = $this->_lcs_pos($y);
271 USE_ASSERTS && assert($k > 0);
272 $ymids[$k] = $ymids[$k-1];
278 $seps[] = $flip ? array($yoff, $xoff) : array($xoff, $yoff);
279 $ymid = $ymids[$this->lcs];
280 for ($n = 0; $n < $nchunks - 1; $n++) {
281 $x1 = $xoff + (int)(($numer + ($xlim - $xoff) * $n) / $nchunks);
283 $seps[] = $flip ? array($y1, $x1) : array($x1, $y1);
285 $seps[] = $flip ? array($ylim, $xlim) : array($xlim, $ylim);
287 return array($this->lcs, $seps);
290 function _lcs_pos ($ypos) {
292 if ($end == 0 || $ypos > $this->seq[$end]) {
293 $this->seq[++$this->lcs] = $ypos;
294 $this->in_seq[$ypos] = 1;
299 while ($beg < $end) {
300 $mid = (int)(($beg + $end) / 2);
301 if ( $ypos > $this->seq[$mid] )
307 USE_ASSERTS && assert($ypos != $this->seq[$end]);
309 $this->in_seq[$this->seq[$end]] = false;
310 $this->seq[$end] = $ypos;
311 $this->in_seq[$ypos] = 1;
315 /* Find LCS of two sequences.
317 * The results are recorded in the vectors $this->{x,y}changed[], by
318 * storing a 1 in the element for each line that is an insertion
319 * or deletion (ie. is not in the LCS).
321 * The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
323 * Note that XLIM, YLIM are exclusive bounds.
324 * All line numbers are origin-0 and discarded lines are not counted.
326 function _compareseq ($xoff, $xlim, $yoff, $ylim) {
327 // Slide down the bottom initial diagonal.
328 while ($xoff < $xlim && $yoff < $ylim
329 && $this->xv[$xoff] == $this->yv[$yoff]) {
334 // Slide up the top initial diagonal.
335 while ($xlim > $xoff && $ylim > $yoff
336 && $this->xv[$xlim - 1] == $this->yv[$ylim - 1]) {
341 if ($xoff == $xlim || $yoff == $ylim)
344 // This is ad hoc but seems to work well.
345 //$nchunks = sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5);
346 //$nchunks = max(2,min(8,(int)$nchunks));
347 $nchunks = min(7, $xlim - $xoff, $ylim - $yoff) + 1;
349 = $this->_diag($xoff,$xlim,$yoff, $ylim,$nchunks);
353 // X and Y sequences have no common subsequence:
355 while ($yoff < $ylim)
356 $this->ychanged[$this->yind[$yoff++]] = 1;
357 while ($xoff < $xlim)
358 $this->xchanged[$this->xind[$xoff++]] = 1;
361 // Use the partitions to split this problem into subproblems.
364 while ($pt2 = next($seps)) {
365 $this->_compareseq ($pt1[0], $pt2[0], $pt1[1], $pt2[1]);
371 /* Adjust inserts/deletes of identical lines to join changes
372 * as much as possible.
374 * We do something when a run of changed lines include a
375 * line at one end and has an excluded, identical line at the other.
376 * We are free to choose which identical line is included.
377 * `compareseq' usually chooses the one at the beginning,
378 * but usually it is cleaner to consider the following identical line
379 * to be the "change".
381 * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
383 function _shift_boundaries ($lines, &$changed, $other_changed) {
387 USE_ASSERTS && assert('sizeof($lines) == sizeof($changed)');
388 $len = sizeof($lines);
389 $other_len = sizeof($other_changed);
393 * Scan forwards to find beginning of another run of changes.
394 * Also keep track of the corresponding point in the other file.
396 * Throughout this code, $i and $j are adjusted together so that
397 * the first $i elements of $changed and the first $j elements
398 * of $other_changed both contain the same number of zeros
400 * Furthermore, $j is always kept so that $j == $other_len or
401 * $other_changed[$j] == false.
403 while ($j < $other_len && $other_changed[$j])
406 while ($i < $len && ! $changed[$i]) {
407 USE_ASSERTS && assert('$j < $other_len && ! $other_changed[$j]');
409 while ($j < $other_len && $other_changed[$j])
418 // Find the end of this run of changes.
419 while (++$i < $len && $changed[$i])
424 * Record the length of this run of changes, so that
425 * we can later determine whether the run has grown.
427 $runlength = $i - $start;
430 * Move the changed region back, so long as the
431 * previous unchanged line matches the last changed one.
432 * This merges with previous changed regions.
434 while ($start > 0 && $lines[$start - 1] == $lines[$i - 1]) {
435 $changed[--$start] = 1;
436 $changed[--$i] = false;
437 while ($start > 0 && $changed[$start - 1])
439 USE_ASSERTS && assert('$j > 0');
440 while ($other_changed[--$j])
442 USE_ASSERTS && assert('$j >= 0 && !$other_changed[$j]');
446 * Set CORRESPONDING to the end of the changed run, at the last
447 * point where it corresponds to a changed run in the other file.
448 * CORRESPONDING == LEN means no such point has been found.
450 $corresponding = $j < $other_len ? $i : $len;
453 * Move the changed region forward, so long as the
454 * first changed line matches the following unchanged one.
455 * This merges with following changed regions.
456 * Do this second, so that if there are no merges,
457 * the changed region is moved forward as far as possible.
459 while ($i < $len && $lines[$start] == $lines[$i]) {
460 $changed[$start++] = false;
462 while ($i < $len && $changed[$i])
465 USE_ASSERTS && assert('$j < $other_len && ! $other_changed[$j]');
467 if ($j < $other_len && $other_changed[$j]) {
469 while ($j < $other_len && $other_changed[$j])
473 } while ($runlength != $i - $start);
476 * If possible, move the fully-merged run of changes
477 * back to a corresponding run in the other file.
479 while ($corresponding < $i) {
480 $changed[--$start] = 1;
482 USE_ASSERTS && assert('$j > 0');
483 while ($other_changed[--$j])
485 USE_ASSERTS && assert('$j >= 0 && !$other_changed[$j]');
492 * Class representing a 'diff' between two sequences of strings.
500 * Computes diff between sequences of strings.
502 * @param $from_lines array An array of strings.
503 * (Typically these are lines from a file.)
504 * @param $to_lines array An array of strings.
506 function Diff($from_lines, $to_lines) {
507 $eng = new _DiffEngine;
508 $this->edits = $eng->diff($from_lines, $to_lines);
509 //$this->_check($from_lines, $to_lines);
513 * Compute reversed Diff.
517 * $diff = new Diff($lines1, $lines2);
518 * $rev = $diff->reverse();
519 * @return object A Diff object representing the inverse of the
522 function reverse () {
524 $rev->edits = array();
525 foreach ($this->edits as $edit) {
526 $rev->edits[] = $edit->reverse();
532 * Check for empty diff.
534 * @return bool True iff two sequences were identical.
536 function isEmpty () {
537 foreach ($this->edits as $edit) {
538 if ($edit->type != 'copy')
545 * Compute the length of the Longest Common Subsequence (LCS).
547 * This is mostly for diagnostic purposed.
549 * @return int The length of the LCS.
553 foreach ($this->edits as $edit) {
554 if ($edit->type == 'copy')
555 $lcs += sizeof($edit->orig);
561 * Get the original set of lines.
563 * This reconstructs the $from_lines parameter passed to the
566 * @return array The original sequence of strings.
571 foreach ($this->edits as $edit) {
573 array_splice($lines, sizeof($lines), 0, $edit->orig);
579 * Get the final set of lines.
581 * This reconstructs the $to_lines parameter passed to the
584 * @return array The sequence of strings.
589 foreach ($this->edits as $edit) {
591 array_splice($lines, sizeof($lines), 0, $edit->final);
597 * Check a Diff for validity.
599 * This is here only for debugging purposes.
601 function _check ($from_lines, $to_lines) {
602 if (serialize($from_lines) != serialize($this->orig()))
603 trigger_error("Reconstructed original doesn't match", E_USER_ERROR);
604 if (serialize($to_lines) != serialize($this->_final()))
605 trigger_error("Reconstructed final doesn't match", E_USER_ERROR);
607 $rev = $this->reverse();
608 if (serialize($to_lines) != serialize($rev->orig()))
609 trigger_error("Reversed original doesn't match", E_USER_ERROR);
610 if (serialize($from_lines) != serialize($rev->_final()))
611 trigger_error("Reversed final doesn't match", E_USER_ERROR);
615 foreach ($this->edits as $edit) {
616 if ( $prevtype == $edit->type )
617 trigger_error("Edit sequence is non-optimal", E_USER_ERROR);
618 $prevtype = $edit->type;
622 trigger_error("Diff okay: LCS = $lcs", E_USER_NOTICE);
638 * Computes diff between sequences of strings.
640 * This can be used to compute things like
641 * case-insensitve diffs, or diffs which ignore
642 * changes in white-space.
644 * @param $from_lines array An array of strings.
645 * (Typically these are lines from a file.)
647 * @param $to_lines array An array of strings.
649 * @param $mapped_from_lines array This array should
650 * have the same size number of elements as $from_lines.
651 * The elements in $mapped_from_lines and
652 * $mapped_to_lines are what is actually compared
653 * when computing the diff.
655 * @param $mapped_to_lines array This array should
656 * have the same number of elements as $to_lines.
658 function MappedDiff($from_lines, $to_lines,
659 $mapped_from_lines, $mapped_to_lines) {
661 assert(sizeof($from_lines) == sizeof($mapped_from_lines));
662 assert(sizeof($to_lines) == sizeof($mapped_to_lines));
664 $this->Diff($mapped_from_lines, $mapped_to_lines);
667 // Optimizing loop invariants:
668 // http://phplens.com/lens/php-book/optimizing-debugging-php.php
669 for ($i = 0, $max = sizeof($this->edits); $i < $max; $i++) {
670 $orig = &$this->edits[$i]->orig;
671 if (is_array($orig)) {
672 $orig = array_slice($from_lines, $xi, sizeof($orig));
673 $xi += sizeof($orig);
676 $final = &$this->edits[$i]->final;
677 if (is_array($final)) {
678 $final = array_slice($to_lines, $yi, sizeof($final));
679 $yi += sizeof($final);
687 * A class to format Diffs
689 * This class formats the diff in classic diff format.
690 * It is intended that this class be customized via inheritance,
691 * to obtain fancier outputs.
696 * Number of leading context "lines" to preserve.
698 * This should be left at zero for this class, but subclasses
699 * may want to set this to other values.
701 var $leading_context_lines = 0;
704 * Number of trailing context "lines" to preserve.
706 * This should be left at zero for this class, but subclasses
707 * may want to set this to other values.
709 var $trailing_context_lines = 0;
714 * @param $diff object A Diff object.
715 * @return string The formatted output.
717 function format($diff) {
723 $nlead = $this->leading_context_lines;
724 $ntrail = $this->trailing_context_lines;
726 $this->_start_diff();
728 foreach ($diff->edits as $edit) {
729 if ($edit->type == 'copy') {
730 if (is_array($block)) {
731 if (sizeof($edit->orig) <= $nlead + $ntrail) {
736 $context = array_slice($edit->orig, 0, $ntrail);
737 $block[] = new _DiffOp_Copy($context);
739 $this->_block($x0, $ntrail + $xi - $x0,
740 $y0, $ntrail + $yi - $y0,
745 $context = $edit->orig;
748 if (! is_array($block)) {
749 $context = array_slice($context, max(0, sizeof($context) - $nlead));
750 $x0 = $xi - sizeof($context);
751 $y0 = $yi - sizeof($context);
754 $block[] = new _DiffOp_Copy($context);
760 $xi += sizeof($edit->orig);
762 $yi += sizeof($edit->final);
765 if (is_array($block))
766 $this->_block($x0, $xi - $x0,
770 return $this->_end_diff();
773 function _block($xbeg, $xlen, $ybeg, $ylen, &$edits) {
774 $this->_start_block($this->_block_header($xbeg, $xlen, $ybeg, $ylen));
775 foreach ($edits as $edit) {
776 if ($edit->type == 'copy')
777 $this->_context($edit->orig);
778 elseif ($edit->type == 'add')
779 $this->_added($edit->final);
780 elseif ($edit->type == 'delete')
781 $this->_deleted($edit->orig);
782 elseif ($edit->type == 'change')
783 $this->_changed($edit->orig, $edit->final);
785 trigger_error("Unknown edit type", E_USER_ERROR);
790 function _start_diff() {
794 function _end_diff() {
795 $val = ob_get_contents();
800 function _block_header($xbeg, $xlen, $ybeg, $ylen) {
802 $xbeg .= "," . ($xbeg + $xlen - 1);
804 $ybeg .= "," . ($ybeg + $ylen - 1);
806 return $xbeg . ($xlen ? ($ylen ? 'c' : 'd') : 'a') . $ybeg;
809 function _start_block($header) {
813 function _end_block() {
816 function _lines($lines, $prefix = ' ') {
817 foreach ($lines as $line)
818 echo "$prefix $line\n";
821 function _context($lines) {
822 $this->_lines($lines);
825 function _added($lines) {
826 $this->_lines($lines, ">");
828 function _deleted($lines) {
829 $this->_lines($lines, "<");
832 function _changed($orig, $final) {
833 $this->_deleted($orig);
835 $this->_added($final);
840 * "Unified" diff formatter.
842 * This class formats the diff in classic "unified diff" format.
844 class UnifiedDiffFormatter extends DiffFormatter
846 function UnifiedDiffFormatter($context_lines = 4) {
847 $this->leading_context_lines = $context_lines;
848 $this->trailing_context_lines = $context_lines;
851 function _block_header($xbeg, $xlen, $ybeg, $ylen) {
853 $xbeg .= "," . $xlen;
855 $ybeg .= "," . $ylen;
856 return "@@ -$xbeg +$ybeg @@\n";
859 function _added($lines) {
860 $this->_lines($lines, "+");
862 function _deleted($lines) {
863 $this->_lines($lines, "-");
865 function _changed($orig, $final) {
866 $this->_deleted($orig);
867 $this->_added($final);
872 * block conflict diff formatter.
874 * This class will format a diff identical to Diff3 (i.e. editpage
875 * conflicts), but when there are only two source files. To be used by
876 * future enhancements to reloading / upgrading pgsrc.
878 * Functional but not finished yet, need to eliminate redundant block
879 * suffixes (i.e. "=======" immediately followed by another prefix)
880 * see class LoadFileConflictPageEditor
882 class BlockDiffFormatter extends DiffFormatter
884 function BlockDiffFormatter($context_lines = 4) {
885 $this->leading_context_lines = $context_lines;
886 $this->trailing_context_lines = $context_lines;
888 function _lines($lines, $prefix = '') {
891 foreach ($lines as $line)
896 function _added($lines) {
897 $this->_lines($lines, ">>>>>>>");
899 function _deleted($lines) {
900 $this->_lines($lines, "<<<<<<<");
902 function _block_header($xbeg, $xlen, $ybeg, $ylen) {
905 function _changed($orig, $final) {
906 $this->_deleted($orig);
907 $this->_added($final);
915 // c-hanging-comment-ender-p: nil
916 // indent-tabs-mode: nil