2 rcs_id('$Id: difflib.php,v 1.3 2001-12-15 02:39:43 dairiki Exp $');
5 // A PHP diff engine for phpwiki.
7 // Copyright (C) 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org>
8 // You may copy this code freely under the conditions of the GPL.
11 // FIXME: possibly remove assert()'s for production version?
13 // PHP3 does not have assert()
14 define('USE_ASSERTS', function_exists('assert'));
22 trigger_error("pure virtual", E_USER_ERROR);
26 class _DiffOp_Copy extends _DiffOp {
29 function _DiffOp_Copy ($orig, $final = false) {
30 if (!is_array($final))
33 $this->final = $final;
37 return new _DiffOp_Copy($this->final, $this->orig);
41 class _DiffOp_Delete extends _DiffOp {
44 function _DiffOp_Delete ($lines) {
50 return new _DiffOp_Add($this->orig);
54 class _DiffOp_Add extends _DiffOp {
57 function _DiffOp_Add ($lines) {
58 $this->final = $lines;
63 return new _DiffOp_Delete($this->final);
67 class _DiffOp_Change extends _DiffOp {
70 function _DiffOp_Change ($orig, $final) {
72 $this->final = $final;
76 return new _DiffOp_Change($this->final, $this->orig);
82 * Class used internally by Diff to actually compute the diffs.
84 * The algorithm used here is mostly lifted from the perl module
85 * Algorithm::Diff (version 1.06) by Ned Konz, which is available at:
86 * http://www.perl.com/CPAN/authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip
88 * More ideas are taken from:
89 * http://www.ics.uci.edu/~eppstein/161/960229.html
91 * Some ideas are (and a bit of code) are from from analyze.c, from GNU
92 * diffutils-2.7, which can be found at:
93 * ftp://gnudist.gnu.org/pub/gnu/diffutils/diffutils-2.7.tar.gz
95 * Finally, some ideas (subdivision by NCHUNKS > 2, and some optimizations)
98 * @author Geoffrey T. Dairiki
103 var $xv = array(), $yv = array();
105 function diff ($from_lines, $to_lines) {
106 $n_from = sizeof($from_lines);
107 $n_to = sizeof($to_lines);
110 // Skip leading common lines.
111 for ($skip = 0; $skip < $n_from && $skip < $n_to; $skip++) {
112 if ($from_lines[$skip] != $to_lines[$skip])
114 $this->xchanged[$skip] = $this->ychanged[$skip] = false;
116 // Skip trailing common lines.
117 $xi = $n_from; $yi = $n_to;
118 for ($endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++) {
119 if ($from_lines[$xi] != $to_lines[$yi])
121 $this->xchanged[$xi] = $this->ychanged[$yi] = false;
124 // Ignore lines which do not exist in both files.
125 for ($xi = $skip; $xi < $n_from - $endskip; $xi++)
126 $xhash[$from_lines[$xi]] = 1;
127 for ($yi = $skip; $yi < $n_to - $endskip; $yi++) {
128 $line = $to_lines[$yi];
129 if ( ($this->ychanged[$yi] = empty($xhash[$line])) )
135 for ($xi = $skip; $xi < $n_from - $endskip; $xi++) {
136 $line = $from_lines[$xi];
137 if ( ($this->xchanged[$xi] = empty($yhash[$line])) )
144 $this->_compareseq(0, sizeof($this->xv), 0, sizeof($this->yv));
146 // Merge edits when possible
147 $this->_shift_boundaries($from_lines, $this->xchanged, $this->ychanged);
148 $this->_shift_boundaries($to_lines, $this->ychanged, $this->xchanged);
150 // Compute the edit operations.
153 while ($xi < $n_from || $yi < $n_to) {
154 USE_ASSERTS && assert($yi < $n_to || $this->xchanged[$xi]);
155 USE_ASSERTS && assert($xi < $n_from || $this->ychanged[$yi]);
157 // Skip matching "snake".
159 while ( $xi < $n_from && $yi < $n_to
160 && !$this->xchanged[$xi] && !$this->ychanged[$yi]) {
161 $copy[] = $from_lines[$xi++];
165 $edits[] = new _DiffOp_Copy($copy);
167 // Find deletes & adds.
169 while ($xi < $n_from && $this->xchanged[$xi])
170 $delete[] = $from_lines[$xi++];
173 while ($yi < $n_to && $this->ychanged[$yi])
174 $add[] = $to_lines[$yi++];
177 $edits[] = new _DiffOp_Change($delete, $add);
179 $edits[] = new _DiffOp_Delete($delete);
181 $edits[] = new _DiffOp_Add($add);
187 /* Divide the Largest Common Subsequence (LCS) of the sequences
188 * [XOFF, XLIM) and [YOFF, YLIM) into NCHUNKS approximately equally
191 * Returns (LCS, PTS). LCS is the length of the LCS. PTS is an
192 * array of NCHUNKS+1 (X, Y) indexes giving the diving points between
193 * sub sequences. The first sub-sequence is contained in [X0, X1),
194 * [Y0, Y1), the second in [X1, X2), [Y1, Y2) and so on. Note
195 * that (X0, Y0) == (XOFF, YOFF) and
196 * (X[NCHUNKS], Y[NCHUNKS]) == (XLIM, YLIM).
198 * This function assumes that the first lines of the specified portions
199 * of the two files do not match, and likewise that the last lines do not
200 * match. The caller must trim matching lines from the beginning and end
201 * of the portions it is going to specify.
203 function _diag ($xoff, $xlim, $yoff, $ylim, $nchunks) {
206 if ($xlim - $xoff > $ylim - $yoff) {
207 // Things seems faster (I'm not sure I understand why)
208 // when the shortest sequence in X.
210 list ($xoff, $xlim, $yoff, $ylim)
211 = array( $yoff, $ylim, $xoff, $xlim);
215 for ($i = $ylim - 1; $i >= $yoff; $i--)
216 $ymatches[$this->xv[$i]][] = $i;
218 for ($i = $ylim - 1; $i >= $yoff; $i--)
219 $ymatches[$this->yv[$i]][] = $i;
222 $this->seq[0]= $yoff - 1;
223 $this->in_seq = array();
226 $numer = $xlim - $xoff + $nchunks - 1;
228 for ($chunk = 0; $chunk < $nchunks; $chunk++) {
230 for ($i = 0; $i <= $this->lcs; $i++)
231 $ymids[$i][$chunk-1] = $this->seq[$i];
233 $x1 = $xoff + (int)(($numer + ($xlim-$xoff)*$chunk) / $nchunks);
234 for ( ; $x < $x1; $x++) {
235 $line = $flip ? $this->yv[$x] : $this->xv[$x];
236 if (empty($ymatches[$line]))
238 $matches = $ymatches[$line];
240 while (list ($junk, $y) = each($matches))
241 if (empty($this->in_seq[$y])) {
242 $k = $this->_lcs_pos($y);
243 USE_ASSERTS && assert($k > 0);
244 $ymids[$k] = $ymids[$k-1];
247 while (list ($junk, $y) = each($matches)) {
248 if ($y > $this->seq[$k-1]) {
249 USE_ASSERTS && assert($y < $this->seq[$k]);
250 // Optimization: this is a common case:
251 // next match is just replacing previous match.
252 $this->in_seq[$this->seq[$k]] = false;
254 $this->in_seq[$y] = 1;
256 else if (empty($this->in_seq[$y])) {
257 $k = $this->_lcs_pos($y);
258 USE_ASSERTS && assert($k > 0);
259 $ymids[$k] = $ymids[$k-1];
265 $seps[] = $flip ? array($yoff, $xoff) : array($xoff, $yoff);
266 $ymid = $ymids[$this->lcs];
267 for ($n = 0; $n < $nchunks - 1; $n++) {
268 $x1 = $xoff + (int)(($numer + ($xlim - $xoff) * $n) / $nchunks);
270 $seps[] = $flip ? array($y1, $x1) : array($x1, $y1);
272 $seps[] = $flip ? array($ylim, $xlim) : array($xlim, $ylim);
274 return array($this->lcs, $seps);
277 function _lcs_pos ($ypos) {
279 if ($end == 0 || $ypos > $this->seq[$end]) {
280 $this->seq[++$this->lcs] = $ypos;
281 $this->in_seq[$ypos] = 1;
286 while ($beg < $end) {
287 $mid = (int)(($beg + $end) / 2);
288 if ( $ypos > $this->seq[$mid] )
294 USE_ASSERTS && assert($ypos != $this->seq[$end]);
296 $this->in_seq[$this->seq[$end]] = false;
297 $this->seq[$end] = $ypos;
298 $this->in_seq[$ypos] = 1;
302 /* Find LCS of two sequences.
304 * The results are recorded in the vectors $this->{x,y}changed[], by
305 * storing a 1 in the element for each line that is an insertion
306 * or deletion (ie. is not in the LCS).
308 * The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
310 * Note that XLIM, YLIM are exclusive bounds.
311 * All line numbers are origin-0 and discarded lines are not counted.
313 function _compareseq ($xoff, $xlim, $yoff, $ylim) {
314 // Slide down the bottom initial diagonal.
315 while ($xoff < $xlim && $yoff < $ylim
316 && $this->xv[$xoff] == $this->yv[$yoff]) {
321 // Slide up the top initial diagonal.
322 while ($xlim > $xoff && $ylim > $yoff
323 && $this->xv[$xlim - 1] == $this->yv[$ylim - 1]) {
328 if ($xoff == $xlim || $yoff == $ylim)
331 // This is ad hoc but seems to work well.
332 //$nchunks = sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5);
333 //$nchunks = max(2,min(8,(int)$nchunks));
334 $nchunks = min(7, $xlim - $xoff, $ylim - $yoff) + 1;
336 = $this->_diag($xoff,$xlim,$yoff, $ylim,$nchunks);
340 // X and Y sequences have no common subsequence:
342 while ($yoff < $ylim)
343 $this->ychanged[$this->yind[$yoff++]] = 1;
344 while ($xoff < $xlim)
345 $this->xchanged[$this->xind[$xoff++]] = 1;
348 // Use the partitions to split this problem into subproblems.
351 while ($pt2 = next($seps)) {
352 $this->_compareseq ($pt1[0], $pt2[0], $pt1[1], $pt2[1]);
358 /* Adjust inserts/deletes of identical lines to join changes
359 * as much as possible.
361 * We do something when a run of changed lines include a
362 * line at one end and has an excluded, identical line at the other.
363 * We are free to choose which identical line is included.
364 * `compareseq' usually chooses the one at the beginning,
365 * but usually it is cleaner to consider the following identical line
366 * to be the "change".
368 * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
370 function _shift_boundaries ($lines, &$changed, $other_changed) {
374 USE_ASSERTS && assert('sizeof($lines) == sizeof($changed)');
375 $len = sizeof($lines);
376 $other_len = sizeof($other_changed);
380 * Scan forwards to find beginning of another run of changes.
381 * Also keep track of the corresponding point in the other file.
383 * Throughout this code, $i and $j are adjusted together so that
384 * the first $i elements of $changed and the first $j elements
385 * of $other_changed both contain the same number of zeros
387 * Furthermore, $j is always kept so that $j == $other_len or
388 * $other_changed[$j] == false.
390 while ($j < $other_len && $other_changed[$j])
393 while ($i < $len && ! $changed[$i]) {
394 USE_ASSERTS && assert('$j < $other_len && ! $other_changed[$j]');
396 while ($j < $other_len && $other_changed[$j])
405 // Find the end of this run of changes.
406 while (++$i < $len && $changed[$i])
411 * Record the length of this run of changes, so that
412 * we can later determine whether the run has grown.
414 $runlength = $i - $start;
417 * Move the changed region back, so long as the
418 * previous unchanged line matches the last changed one.
419 * This merges with previous changed regions.
421 while ($start > 0 && $lines[$start - 1] == $lines[$i - 1]) {
422 $changed[--$start] = 1;
423 $changed[--$i] = false;
424 while ($start > 0 && $changed[$start - 1])
426 USE_ASSERTS && assert('$j > 0');
427 while ($other_changed[--$j])
429 USE_ASSERTS && assert('$j >= 0 && !$other_changed[$j]');
433 * Set CORRESPONDING to the end of the changed run, at the last
434 * point where it corresponds to a changed run in the other file.
435 * CORRESPONDING == LEN means no such point has been found.
437 $corresponding = $j < $other_len ? $i : $len;
440 * Move the changed region forward, so long as the
441 * first changed line matches the following unchanged one.
442 * This merges with following changed regions.
443 * Do this second, so that if there are no merges,
444 * the changed region is moved forward as far as possible.
446 while ($i < $len && $lines[$start] == $lines[$i]) {
447 $changed[$start++] = false;
449 while ($i < $len && $changed[$i])
452 USE_ASSERTS && assert('$j < $other_len && ! $other_changed[$j]');
454 if ($j < $other_len && $other_changed[$j]) {
456 while ($j < $other_len && $other_changed[$j])
460 } while ($runlength != $i - $start);
463 * If possible, move the fully-merged run of changes
464 * back to a corresponding run in the other file.
466 while ($corresponding < $i) {
467 $changed[--$start] = 1;
469 USE_ASSERTS && assert('$j > 0');
470 while ($other_changed[--$j])
472 USE_ASSERTS && assert('$j >= 0 && !$other_changed[$j]');
479 * Class representing a 'diff' between two sequences of strings.
487 * Computes diff between sequences of strings.
489 * @param $from_lines array An array of strings.
490 * (Typically these are lines from a file.)
491 * @param $to_lines array An array of strings.
493 function Diff($from_lines, $to_lines) {
494 $eng = new _DiffEngine;
495 $this->edits = $eng->diff($from_lines, $to_lines);
496 //$this->_check($from_lines, $to_lines);
500 * Compute reversed Diff.
504 * $diff = new Diff($lines1, $lines2);
505 * $rev = $diff->reverse();
506 * @return object A Diff object representing the inverse of the
509 function reverse () {
511 $rev->edits = array();
512 foreach ($this->edits as $edit) {
513 $rev->edits[] = $edit->reverse();
519 * Check for empty diff.
521 * @return bool True iff two sequences were identical.
523 function isEmpty () {
524 foreach ($this->edits as $edit) {
525 if ($edit->type != 'copy')
532 * Compute the length of the Longest Common Subsequence (LCS).
534 * This is mostly for diagnostic purposed.
536 * @return int The length of the LCS.
540 foreach ($this->edits as $edit) {
541 if ($edit->type == 'copy')
542 $lcs += sizeof($edit->orig);
548 * Get the original set of lines.
550 * This reconstructs the $from_lines parameter passed to the
553 * @return array The original sequence of strings.
558 foreach ($this->edits as $edit) {
560 array_splice($lines, sizeof($lines), 0, $edit->orig);
566 * Get the final set of lines.
568 * This reconstructs the $to_lines parameter passed to the
571 * @return array The sequence of strings.
576 foreach ($this->edits as $edit) {
578 array_splice($lines, sizeof($lines), 0, $edit->final);
584 * Check a Diff for validity.
586 * This is here only for debugging purposes.
588 function _check ($from_lines, $to_lines) {
589 if (serialize($from_lines) != serialize($this->orig()))
590 trigger_error("Reconstructed original doesn't match", E_USER_ERROR);
591 if (serialize($to_lines) != serialize($this->final()))
592 trigger_error("Reconstructed final doesn't match", E_USER_ERROR);
594 $rev = $this->reverse();
595 if (serialize($to_lines) != serialize($rev->orig()))
596 trigger_error("Reversed original doesn't match", E_USER_ERROR);
597 if (serialize($from_lines) != serialize($rev->final()))
598 trigger_error("Reversed final doesn't match", E_USER_ERROR);
602 foreach ($this->edits as $edit) {
603 if ( $prevtype == $edit->type )
604 trigger_error("Edit sequence is non-optimal", E_USER_ERROR);
605 $prevtype = $edit->type;
609 trigger_error("Diff okay: LCS = $lcs", E_USER_NOTICE);
614 if (!function_exists('array_map')) {
615 function array_map($map_func, $array) {
617 foreach ($array as $x)
618 $result[] = call_user_func($map_func, $x);
633 * Computes diff between sequences of strings.
635 * This can be used to compute things like
636 * case-insensitve diffs, or diffs which ignore
637 * changes in white-space.
639 * @param $from_lines array An array of strings.
640 * (Typically these are lines from a file.)
642 * @param $to_lines array An array of strings.
644 * @param $mapped_from_lines array This array should
645 * have the same size number of elements as $from_lines.
646 * The elements in $mapped_from_lines and
647 * $mapped_to_lines are what is actually compared
648 * when computing the diff.
650 * @param $mapped_to_lines array This array should
651 * have the same number of elements as $to_lines.
653 function MappedDiff($from_lines, $to_lines,
654 $mapped_from_lines, $mapped_to_lines) {
656 assert(sizeof($from_lines) == sizeof($mapped_from_lines));
657 assert(sizeof($to_lines) == sizeof($mapped_to_lines));
659 $this->Diff($mapped_from_lines, $mapped_to_lines);
662 for ($i = 0; $i < sizeof($this->edits); $i++) {
663 $orig = &$this->edits[$i]->orig;
664 if (is_array($orig)) {
665 $orig = array_slice($from_lines, $xi, sizeof($orig));
666 $xi += sizeof($orig);
669 $final = &$this->edits[$i]->final;
670 if (is_array($final)) {
671 $final = array_slice($to_lines, $yi, sizeof($final));
672 $yi += sizeof($final);
680 * A class to format Diffs
682 * This class formats the diff in classic diff format.
683 * It is intended that this class be customized via inheritance,
684 * to obtain fancier outputs.
689 * Number of leading context "lines" to preserve.
691 * This should be left at zero for this class, but subclasses
692 * may want to set this to other values.
694 var $leading_context_lines = 0;
697 * Number of trailing context "lines" to preserve.
699 * This should be left at zero for this class, but subclasses
700 * may want to set this to other values.
702 var $trailing_context_lines = 0;
707 * @param $diff object A Diff object.
708 * @return string The formatted output.
710 function format($diff) {
716 $nlead = $this->leading_context_lines;
717 $ntrail = $this->trailing_context_lines;
720 $this->_start_diff();
722 foreach ($diff->edits as $edit) {
723 if ($edit->type == 'copy') {
724 if (is_array($block)) {
725 if (sizeof($edit->orig) <= $nlead + $ntrail) {
730 $context = array_slice($edit->orig, 0, $ntrail);
731 $block[] = new _DiffOp_Copy($context);
733 $this->_block($x0, $ntrail + $xi - $x0,
734 $y0, $ntrail + $yi - $y0,
739 $context = $edit->orig;
742 if (! is_array($block)) {
743 $context = array_slice($context, sizeof($context) - $nlead);
744 $x0 = $xi - sizeof($context);
745 $y0 = $yi - sizeof($context);
748 $block[] = new _DiffOp_Copy($context);
754 $xi += sizeof($edit->orig);
756 $yi += sizeof($edit->final);
759 if (is_array($block))
760 $this->_block($x0, $xi - $x0,
766 $val = ob_get_contents();
771 function _block($xbeg, $xlen, $ybeg, $ylen, &$edits) {
772 $this->_start_block($this->_block_header($xbeg, $xlen, $ybeg, $ylen));
773 foreach ($edits as $edit) {
774 if ($edit->type == 'copy')
775 $this->_context($edit->orig);
776 elseif ($edit->type == 'add')
777 $this->_added($edit->final);
778 elseif ($edit->type == 'delete')
779 $this->_deleted($edit->orig);
780 elseif ($edit->type == 'change')
781 $this->_changed($edit->orig, $edit->final);
783 trigger_error("Unknown edit type", E_USER_ERROR);
788 function _start_diff() {
791 function _end_diff() {
794 function _block_header($xbeg, $xlen, $ybeg, $ylen) {
796 $xbeg .= "," . ($xbeg + $xlen - 1);
798 $ybeg .= "," . ($ybeg + $ylen - 1);
800 return $xbeg . ($xlen ? ($ylen ? 'c' : 'd') : 'a') . $ybeg;
803 function _start_block($header) {
807 function _end_block() {
810 function _lines($lines, $prefix = ' ') {
811 foreach ($lines as $line)
812 echo "$prefix $line\n";
815 function _context($lines) {
816 $this->_lines($lines);
819 function _added($lines) {
820 $this->_lines($lines, ">");
822 function _deleted($lines) {
823 $this->_lines($lines, "<");
826 function _changed($orig, $final) {
827 $this->_deleted($orig);
829 $this->_added($final);
834 * "Unified" diff formatter.
836 * This class formats the diff in classic "unified diff" format.
838 class UnifiedDiffFormatter extends DiffFormatter
840 function UnifiedDiffFormatter($context_lines = 4) {
841 $this->leading_context_lines = $context_lines;
842 $this->trailing_context_lines = $context_lines;
845 function _block_header($xbeg, $xlen, $ybeg, $ylen) {
847 $xbeg .= "," . $xlen;
849 $ybeg .= "," . $ylen;
850 return "@@ -$xbeg +$ybeg @@";
853 function _added($lines) {
854 $this->_lines($lines, "+");
856 function _deleted($lines) {
857 $this->_lines($lines, "-");
859 function _changed($orig, $final) {
860 $this->_deleted($orig);
861 $this->_added($final);
869 // c-hanging-comment-ender-p: nil
870 // indent-tabs-mode: nil