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];
204 $found_empty = FALSE;
205 foreach ($matches as $y) {
207 if (empty($this->in_seq[$y])) {
208 $k = $this->_lcs_pos($y);
209 $this::USE_ASSERTS && assert($k > 0);
210 $ymids[$k] = $ymids[$k - 1];
215 if ($y > $this->seq[$k - 1]) {
216 $this::USE_ASSERTS && assert($y < $this->seq[$k]);
217 // Optimization: this is a common case:
218 // next match is just replacing previous match.
219 $this->in_seq[$this->seq[$k]] = FALSE;
221 $this->in_seq[$y] = 1;
223 elseif (empty($this->in_seq[$y])) {
224 $k = $this->_lcs_pos($y);
225 $this::USE_ASSERTS && assert($k > 0);
226 $ymids[$k] = $ymids[$k - 1];
233 $seps[] = $flip ? [$yoff, $xoff] : [$xoff, $yoff];
234 $ymid = $ymids[$this->lcs];
235 for ($n = 0; $n < $nchunks - 1; $n++) {
236 $x1 = $xoff + (int)(($numer + ($xlim - $xoff) * $n) / $nchunks);
238 $seps[] = $flip ? [$y1, $x1] : [$x1, $y1];
240 $seps[] = $flip ? [$ylim, $xlim] : [$xlim, $ylim];
242 return [$this->lcs, $seps];
245 protected function _lcs_pos($ypos) {
248 if ($end == 0 || $ypos > $this->seq[$end]) {
249 $this->seq[++$this->lcs] = $ypos;
250 $this->in_seq[$ypos] = 1;
255 while ($beg < $end) {
256 $mid = (int)(($beg + $end) / 2);
257 if ($ypos > $this->seq[$mid]) {
265 $this::USE_ASSERTS && assert($ypos != $this->seq[$end]);
267 $this->in_seq[$this->seq[$end]] = FALSE;
268 $this->seq[$end] = $ypos;
269 $this->in_seq[$ypos] = 1;
274 * Find LCS of two sequences.
276 * The results are recorded in the vectors $this->{x,y}changed[], by
277 * storing a 1 in the element for each line that is an insertion
278 * or deletion (ie. is not in the LCS).
280 * The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
282 * Note that XLIM, YLIM are exclusive bounds.
283 * All line numbers are origin-0 and discarded lines are not counted.
285 protected function _compareseq($xoff, $xlim, $yoff, $ylim) {
287 // Slide down the bottom initial diagonal.
288 while ($xoff < $xlim && $yoff < $ylim && $this->xv[$xoff] == $this->yv[$yoff]) {
293 // Slide up the top initial diagonal.
294 while ($xlim > $xoff && $ylim > $yoff && $this->xv[$xlim - 1] == $this->yv[$ylim - 1]) {
299 if ($xoff == $xlim || $yoff == $ylim) {
303 // This is ad hoc but seems to work well.
304 //$nchunks = sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5);
305 //$nchunks = max(2, min(8, (int)$nchunks));
306 $nchunks = min(7, $xlim - $xoff, $ylim - $yoff) + 1;
307 list($lcs, $seps) = $this->_diag($xoff, $xlim, $yoff, $ylim, $nchunks);
311 // X and Y sequences have no common subsequence:
313 while ($yoff < $ylim) {
314 $this->ychanged[$this->yind[$yoff++]] = 1;
316 while ($xoff < $xlim) {
317 $this->xchanged[$this->xind[$xoff++]] = 1;
321 // Use the partitions to split this problem into subproblems.
324 while ($pt2 = next($seps)) {
325 $this->_compareseq ($pt1[0], $pt2[0], $pt1[1], $pt2[1]);
332 * Adjust inserts/deletes of identical lines to join changes
333 * as much as possible.
335 * We do something when a run of changed lines include a
336 * line at one end and has an excluded, identical line at the other.
337 * We are free to choose which identical line is included.
338 * `compareseq' usually chooses the one at the beginning,
339 * but usually it is cleaner to consider the following identical line
340 * to be the "change".
342 * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
344 protected function _shift_boundaries($lines, &$changed, $other_changed) {
348 $this::USE_ASSERTS && assert(sizeof($lines) == sizeof($changed));
349 $len = sizeof($lines);
350 $other_len = sizeof($other_changed);
354 * Scan forwards to find beginning of another run of changes.
355 * Also keep track of the corresponding point in the other file.
357 * Throughout this code, $i and $j are adjusted together so that
358 * the first $i elements of $changed and the first $j elements
359 * of $other_changed both contain the same number of zeros
361 * Furthermore, $j is always kept so that $j == $other_len or
362 * $other_changed[$j] == FALSE.
364 while ($j < $other_len && $other_changed[$j]) {
367 while ($i < $len && !$changed[$i]) {
368 $this::USE_ASSERTS && assert($j < $other_len && ! $other_changed[$j]);
371 while ($j < $other_len && $other_changed[$j]) {
381 // Find the end of this run of changes.
382 while (++$i < $len && $changed[$i]) {
388 * Record the length of this run of changes, so that
389 * we can later determine whether the run has grown.
391 $runlength = $i - $start;
394 * Move the changed region back, so long as the
395 * previous unchanged line matches the last changed one.
396 * This merges with previous changed regions.
398 while ($start > 0 && $lines[$start - 1] == $lines[$i - 1]) {
399 $changed[--$start] = 1;
400 $changed[--$i] = FALSE;
401 while ($start > 0 && $changed[$start - 1]) {
404 $this::USE_ASSERTS && assert($j > 0);
405 while ($other_changed[--$j]) {
408 $this::USE_ASSERTS && assert($j >= 0 && !$other_changed[$j]);
412 * Set CORRESPONDING to the end of the changed run, at the last
413 * point where it corresponds to a changed run in the other file.
414 * CORRESPONDING == LEN means no such point has been found.
416 $corresponding = $j < $other_len ? $i : $len;
419 * Move the changed region forward, so long as the
420 * first changed line matches the following unchanged one.
421 * This merges with following changed regions.
422 * Do this second, so that if there are no merges,
423 * the changed region is moved forward as far as possible.
425 while ($i < $len && $lines[$start] == $lines[$i]) {
426 $changed[$start++] = FALSE;
428 while ($i < $len && $changed[$i]) {
431 $this::USE_ASSERTS && assert($j < $other_len && ! $other_changed[$j]);
433 if ($j < $other_len && $other_changed[$j]) {
435 while ($j < $other_len && $other_changed[$j]) {
440 } while ($runlength != $i - $start);
443 * If possible, move the fully-merged run of changes
444 * back to a corresponding run in the other file.
446 while ($corresponding < $i) {
447 $changed[--$start] = 1;
449 $this::USE_ASSERTS && assert($j > 0);
450 while ($other_changed[--$j]) {
453 $this::USE_ASSERTS && assert($j >= 0 && !$other_changed[$j]);