3 namespace Drupal\Component\Diff\Engine;
5 use Drupal\Component\Utility\Unicode;
8 * Class used internally by Diff to actually compute the diffs.
10 * The algorithm used here is mostly lifted from the perl module
11 * Algorithm::Diff (version 1.06) by Ned Konz, which is available at:
12 * http://www.perl.com/CPAN/authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip
14 * More ideas are taken from:
15 * http://www.ics.uci.edu/~eppstein/161/960229.html
17 * Some ideas (and a bit of code) are from analyze.c, from GNU
18 * diffutils-2.7, which can be found at:
19 * ftp://gnudist.gnu.org/pub/gnu/diffutils/diffutils-2.7.tar.gz
21 * closingly, some ideas (subdivision by NCHUNKS > 2, and some optimizations)
24 * Line length limits for robustness added by Tim Starling, 2005-08-31
26 * @author Geoffrey T. Dairiki, Tim Starling
28 * @subpackage DifferenceEngine
32 const USE_ASSERTS = FALSE;
34 const MAX_XREF_LENGTH = 10000;
36 public function diff($from_lines, $to_lines) {
38 $n_from = sizeof($from_lines);
39 $n_to = sizeof($to_lines);
41 $this->xchanged = $this->ychanged = [];
42 $this->xv = $this->yv = [];
43 $this->xind = $this->yind = [];
48 // Skip leading common lines.
49 for ($skip = 0; $skip < $n_from && $skip < $n_to; $skip++) {
50 if ($from_lines[$skip] !== $to_lines[$skip]) {
53 $this->xchanged[$skip] = $this->ychanged[$skip] = FALSE;
55 // Skip trailing common lines.
58 for ($endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++) {
59 if ($from_lines[$xi] !== $to_lines[$yi]) {
62 $this->xchanged[$xi] = $this->ychanged[$yi] = FALSE;
65 // Ignore lines which do not exist in both files.
66 for ($xi = $skip; $xi < $n_from - $endskip; $xi++) {
67 $xhash[$this->_line_hash($from_lines[$xi])] = 1;
70 for ($yi = $skip; $yi < $n_to - $endskip; $yi++) {
71 $line = $to_lines[$yi];
72 if ($this->ychanged[$yi] = empty($xhash[$this->_line_hash($line)])) {
75 $yhash[$this->_line_hash($line)] = 1;
79 for ($xi = $skip; $xi < $n_from - $endskip; $xi++) {
80 $line = $from_lines[$xi];
81 if ($this->xchanged[$xi] = empty($yhash[$this->_line_hash($line)])) {
89 $this->_compareseq(0, sizeof($this->xv), 0, sizeof($this->yv));
91 // Merge edits when possible
92 $this->_shift_boundaries($from_lines, $this->xchanged, $this->ychanged);
93 $this->_shift_boundaries($to_lines, $this->ychanged, $this->xchanged);
95 // Compute the edit operations.
98 while ($xi < $n_from || $yi < $n_to) {
99 $this::USE_ASSERTS && assert($yi < $n_to || $this->xchanged[$xi]);
100 $this::USE_ASSERTS && assert($xi < $n_from || $this->ychanged[$yi]);
102 // Skip matching "snake".
104 while ( $xi < $n_from && $yi < $n_to && !$this->xchanged[$xi] && !$this->ychanged[$yi]) {
105 $copy[] = $from_lines[$xi++];
109 $edits[] = new DiffOpCopy($copy);
111 // Find deletes & adds.
113 while ($xi < $n_from && $this->xchanged[$xi]) {
114 $delete[] = $from_lines[$xi++];
117 while ($yi < $n_to && $this->ychanged[$yi]) {
118 $add[] = $to_lines[$yi++];
120 if ($delete && $add) {
121 $edits[] = new DiffOpChange($delete, $add);
124 $edits[] = new DiffOpDelete($delete);
127 $edits[] = new DiffOpAdd($add);
134 * Returns the whole line if it's small enough, or the MD5 hash otherwise.
136 protected function _line_hash($line) {
137 if (Unicode::strlen($line) > $this::MAX_XREF_LENGTH) {
147 * Divide the Largest Common Subsequence (LCS) of the sequences
148 * [XOFF, XLIM) and [YOFF, YLIM) into NCHUNKS approximately equally
151 * Returns (LCS, PTS). LCS is the length of the LCS. PTS is an
152 * array of NCHUNKS+1 (X, Y) indexes giving the diving points between
153 * sub sequences. The first sub-sequence is contained in [X0, X1),
154 * [Y0, Y1), the second in [X1, X2), [Y1, Y2) and so on. Note
155 * that (X0, Y0) == (XOFF, YOFF) and
156 * (X[NCHUNKS], Y[NCHUNKS]) == (XLIM, YLIM).
158 * This function assumes that the first lines of the specified portions
159 * of the two files do not match, and likewise that the last lines do not
160 * match. The caller must trim matching lines from the beginning and end
161 * of the portions it is going to specify.
163 protected function _diag($xoff, $xlim, $yoff, $ylim, $nchunks) {
166 if ($xlim - $xoff > $ylim - $yoff) {
167 // Things seems faster (I'm not sure I understand why)
168 // when the shortest sequence in X.
170 list($xoff, $xlim, $yoff, $ylim) = [$yoff, $ylim, $xoff, $xlim];
174 for ($i = $ylim - 1; $i >= $yoff; $i--) {
175 $ymatches[$this->xv[$i]][] = $i;
179 for ($i = $ylim - 1; $i >= $yoff; $i--) {
180 $ymatches[$this->yv[$i]][] = $i;
184 $this->seq[0] = $yoff - 1;
188 $numer = $xlim - $xoff + $nchunks - 1;
190 for ($chunk = 0; $chunk < $nchunks; $chunk++) {
192 for ($i = 0; $i <= $this->lcs; $i++) {
193 $ymids[$i][$chunk - 1] = $this->seq[$i];
197 $x1 = $xoff + (int)(($numer + ($xlim - $xoff) * $chunk) / $nchunks);
198 for (; $x < $x1; $x++) {
199 $line = $flip ? $this->yv[$x] : $this->xv[$x];
200 if (empty($ymatches[$line])) {
203 $matches = $ymatches[$line];
205 while (list ($junk, $y) = each($matches)) {
206 if (empty($this->in_seq[$y])) {
207 $k = $this->_lcs_pos($y);
208 $this::USE_ASSERTS && assert($k > 0);
209 $ymids[$k] = $ymids[$k - 1];
213 while (list ($junk, $y) = each($matches)) {
214 if ($y > $this->seq[$k - 1]) {
215 $this::USE_ASSERTS && assert($y < $this->seq[$k]);
216 // Optimization: this is a common case:
217 // next match is just replacing previous match.
218 $this->in_seq[$this->seq[$k]] = FALSE;
220 $this->in_seq[$y] = 1;
222 elseif (empty($this->in_seq[$y])) {
223 $k = $this->_lcs_pos($y);
224 $this::USE_ASSERTS && assert($k > 0);
225 $ymids[$k] = $ymids[$k - 1];
231 $seps[] = $flip ? [$yoff, $xoff] : [$xoff, $yoff];
232 $ymid = $ymids[$this->lcs];
233 for ($n = 0; $n < $nchunks - 1; $n++) {
234 $x1 = $xoff + (int)(($numer + ($xlim - $xoff) * $n) / $nchunks);
236 $seps[] = $flip ? [$y1, $x1] : [$x1, $y1];
238 $seps[] = $flip ? [$ylim, $xlim] : [$xlim, $ylim];
240 return [$this->lcs, $seps];
243 protected function _lcs_pos($ypos) {
246 if ($end == 0 || $ypos > $this->seq[$end]) {
247 $this->seq[++$this->lcs] = $ypos;
248 $this->in_seq[$ypos] = 1;
253 while ($beg < $end) {
254 $mid = (int)(($beg + $end) / 2);
255 if ($ypos > $this->seq[$mid]) {
263 $this::USE_ASSERTS && assert($ypos != $this->seq[$end]);
265 $this->in_seq[$this->seq[$end]] = FALSE;
266 $this->seq[$end] = $ypos;
267 $this->in_seq[$ypos] = 1;
272 * Find LCS of two sequences.
274 * The results are recorded in the vectors $this->{x,y}changed[], by
275 * storing a 1 in the element for each line that is an insertion
276 * or deletion (ie. is not in the LCS).
278 * The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
280 * Note that XLIM, YLIM are exclusive bounds.
281 * All line numbers are origin-0 and discarded lines are not counted.
283 protected function _compareseq($xoff, $xlim, $yoff, $ylim) {
285 // Slide down the bottom initial diagonal.
286 while ($xoff < $xlim && $yoff < $ylim && $this->xv[$xoff] == $this->yv[$yoff]) {
291 // Slide up the top initial diagonal.
292 while ($xlim > $xoff && $ylim > $yoff && $this->xv[$xlim - 1] == $this->yv[$ylim - 1]) {
297 if ($xoff == $xlim || $yoff == $ylim) {
301 // This is ad hoc but seems to work well.
302 //$nchunks = sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5);
303 //$nchunks = max(2, min(8, (int)$nchunks));
304 $nchunks = min(7, $xlim - $xoff, $ylim - $yoff) + 1;
305 list($lcs, $seps) = $this->_diag($xoff, $xlim, $yoff, $ylim, $nchunks);
309 // X and Y sequences have no common subsequence:
311 while ($yoff < $ylim) {
312 $this->ychanged[$this->yind[$yoff++]] = 1;
314 while ($xoff < $xlim) {
315 $this->xchanged[$this->xind[$xoff++]] = 1;
319 // Use the partitions to split this problem into subproblems.
322 while ($pt2 = next($seps)) {
323 $this->_compareseq ($pt1[0], $pt2[0], $pt1[1], $pt2[1]);
330 * Adjust inserts/deletes of identical lines to join changes
331 * as much as possible.
333 * We do something when a run of changed lines include a
334 * line at one end and has an excluded, identical line at the other.
335 * We are free to choose which identical line is included.
336 * `compareseq' usually chooses the one at the beginning,
337 * but usually it is cleaner to consider the following identical line
338 * to be the "change".
340 * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
342 protected function _shift_boundaries($lines, &$changed, $other_changed) {
346 $this::USE_ASSERTS && assert('sizeof($lines) == sizeof($changed)');
347 $len = sizeof($lines);
348 $other_len = sizeof($other_changed);
352 * Scan forwards to find beginning of another run of changes.
353 * Also keep track of the corresponding point in the other file.
355 * Throughout this code, $i and $j are adjusted together so that
356 * the first $i elements of $changed and the first $j elements
357 * of $other_changed both contain the same number of zeros
359 * Furthermore, $j is always kept so that $j == $other_len or
360 * $other_changed[$j] == FALSE.
362 while ($j < $other_len && $other_changed[$j]) {
365 while ($i < $len && !$changed[$i]) {
366 $this::USE_ASSERTS && assert('$j < $other_len && ! $other_changed[$j]');
369 while ($j < $other_len && $other_changed[$j]) {
379 // Find the end of this run of changes.
380 while (++$i < $len && $changed[$i]) {
386 * Record the length of this run of changes, so that
387 * we can later determine whether the run has grown.
389 $runlength = $i - $start;
392 * Move the changed region back, so long as the
393 * previous unchanged line matches the last changed one.
394 * This merges with previous changed regions.
396 while ($start > 0 && $lines[$start - 1] == $lines[$i - 1]) {
397 $changed[--$start] = 1;
398 $changed[--$i] = FALSE;
399 while ($start > 0 && $changed[$start - 1]) {
402 $this::USE_ASSERTS && assert('$j > 0');
403 while ($other_changed[--$j]) {
406 $this::USE_ASSERTS && assert('$j >= 0 && !$other_changed[$j]');
410 * Set CORRESPONDING to the end of the changed run, at the last
411 * point where it corresponds to a changed run in the other file.
412 * CORRESPONDING == LEN means no such point has been found.
414 $corresponding = $j < $other_len ? $i : $len;
417 * Move the changed region forward, so long as the
418 * first changed line matches the following unchanged one.
419 * This merges with following changed regions.
420 * Do this second, so that if there are no merges,
421 * the changed region is moved forward as far as possible.
423 while ($i < $len && $lines[$start] == $lines[$i]) {
424 $changed[$start++] = FALSE;
426 while ($i < $len && $changed[$i]) {
429 $this::USE_ASSERTS && assert('$j < $other_len && ! $other_changed[$j]');
431 if ($j < $other_len && $other_changed[$j]) {
433 while ($j < $other_len && $other_changed[$j]) {
438 } while ($runlength != $i - $start);
441 * If possible, move the fully-merged run of changes
442 * back to a corresponding run in the other file.
444 while ($corresponding < $i) {
445 $changed[--$start] = 1;
447 $this::USE_ASSERTS && assert('$j > 0');
448 while ($other_changed[--$j]) {
451 $this::USE_ASSERTS && assert('$j >= 0 && !$other_changed[$j]');