b56aa99aa7ca43b438c4712b0a1b64ee5b273537
[yaffs-website] / web / core / lib / Drupal / Component / Diff / Engine / DiffEngine.php
1 <?php
2
3 namespace Drupal\Component\Diff\Engine;
4
5 use Drupal\Component\Utility\Unicode;
6
7 /**
8  * Class used internally by Diff to actually compute the diffs.
9  *
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
13  *
14  * More ideas are taken from:
15  *   http://www.ics.uci.edu/~eppstein/161/960229.html
16  *
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
20  *
21  * closingly, some ideas (subdivision by NCHUNKS > 2, and some optimizations)
22  * are my own.
23  *
24  * Line length limits for robustness added by Tim Starling, 2005-08-31
25  *
26  * @author Geoffrey T. Dairiki, Tim Starling
27  * @private
28  * @subpackage DifferenceEngine
29  */
30 class DiffEngine {
31
32   const USE_ASSERTS = FALSE;
33
34   const MAX_XREF_LENGTH = 10000;
35
36   public function diff($from_lines, $to_lines) {
37
38     $n_from = sizeof($from_lines);
39     $n_to = sizeof($to_lines);
40
41     $this->xchanged = $this->ychanged = [];
42     $this->xv = $this->yv = [];
43     $this->xind = $this->yind = [];
44     unset($this->seq);
45     unset($this->in_seq);
46     unset($this->lcs);
47
48     // Skip leading common lines.
49     for ($skip = 0; $skip < $n_from && $skip < $n_to; $skip++) {
50       if ($from_lines[$skip] !== $to_lines[$skip]) {
51         break;
52       }
53       $this->xchanged[$skip] = $this->ychanged[$skip] = FALSE;
54     }
55     // Skip trailing common lines.
56     $xi = $n_from;
57     $yi = $n_to;
58     for ($endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++) {
59       if ($from_lines[$xi] !== $to_lines[$yi]) {
60         break;
61       }
62       $this->xchanged[$xi] = $this->ychanged[$yi] = FALSE;
63     }
64
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;
68     }
69
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)])) {
73         continue;
74       }
75       $yhash[$this->_line_hash($line)] = 1;
76       $this->yv[] = $line;
77       $this->yind[] = $yi;
78     }
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)])) {
82         continue;
83       }
84       $this->xv[] = $line;
85       $this->xind[] = $xi;
86     }
87
88     // Find the LCS.
89     $this->_compareseq(0, sizeof($this->xv), 0, sizeof($this->yv));
90
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);
94
95     // Compute the edit operations.
96     $edits = [];
97     $xi = $yi = 0;
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]);
101
102       // Skip matching "snake".
103       $copy = [];
104       while ( $xi < $n_from && $yi < $n_to && !$this->xchanged[$xi] && !$this->ychanged[$yi]) {
105         $copy[] = $from_lines[$xi++];
106         ++$yi;
107       }
108       if ($copy) {
109         $edits[] = new DiffOpCopy($copy);
110       }
111       // Find deletes & adds.
112       $delete = [];
113       while ($xi < $n_from && $this->xchanged[$xi]) {
114         $delete[] = $from_lines[$xi++];
115       }
116       $add = [];
117       while ($yi < $n_to && $this->ychanged[$yi]) {
118         $add[] = $to_lines[$yi++];
119       }
120       if ($delete && $add) {
121         $edits[] = new DiffOpChange($delete, $add);
122       }
123       elseif ($delete) {
124         $edits[] = new DiffOpDelete($delete);
125       }
126       elseif ($add) {
127         $edits[] = new DiffOpAdd($add);
128       }
129     }
130     return $edits;
131   }
132
133   /**
134    * Returns the whole line if it's small enough, or the MD5 hash otherwise.
135    */
136   protected function _line_hash($line) {
137     if (Unicode::strlen($line) > $this::MAX_XREF_LENGTH) {
138       return md5($line);
139     }
140     else {
141       return $line;
142     }
143   }
144
145
146   /**
147    * Divide the Largest Common Subsequence (LCS) of the sequences
148    * [XOFF, XLIM) and [YOFF, YLIM) into NCHUNKS approximately equally
149    * sized segments.
150    *
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).
157    *
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.
162    */
163   protected function _diag($xoff, $xlim, $yoff, $ylim, $nchunks) {
164     $flip = FALSE;
165
166     if ($xlim - $xoff > $ylim - $yoff) {
167       // Things seems faster (I'm not sure I understand why)
168       // when the shortest sequence in X.
169       $flip = TRUE;
170       list($xoff, $xlim, $yoff, $ylim) = [$yoff, $ylim, $xoff, $xlim];
171     }
172
173     if ($flip) {
174       for ($i = $ylim - 1; $i >= $yoff; $i--) {
175         $ymatches[$this->xv[$i]][] = $i;
176       }
177     }
178     else {
179       for ($i = $ylim - 1; $i >= $yoff; $i--) {
180         $ymatches[$this->yv[$i]][] = $i;
181       }
182     }
183     $this->lcs = 0;
184     $this->seq[0] = $yoff - 1;
185     $this->in_seq = [];
186     $ymids[0] = [];
187
188     $numer = $xlim - $xoff + $nchunks - 1;
189     $x = $xoff;
190     for ($chunk = 0; $chunk < $nchunks; $chunk++) {
191       if ($chunk > 0) {
192         for ($i = 0; $i <= $this->lcs; $i++) {
193           $ymids[$i][$chunk - 1] = $this->seq[$i];
194         }
195       }
196
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])) {
201           continue;
202         }
203         $matches = $ymatches[$line];
204         reset($matches);
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];
210             break;
211           }
212         }
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;
219             $this->seq[$k] = $y;
220             $this->in_seq[$y] = 1;
221           }
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];
226           }
227         }
228       }
229     }
230
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);
235       $y1 = $ymid[$n] + 1;
236       $seps[] = $flip ? [$y1, $x1] : [$x1, $y1];
237     }
238     $seps[] = $flip ? [$ylim, $xlim] : [$xlim, $ylim];
239
240     return [$this->lcs, $seps];
241   }
242
243   protected function _lcs_pos($ypos) {
244
245     $end = $this->lcs;
246     if ($end == 0 || $ypos > $this->seq[$end]) {
247       $this->seq[++$this->lcs] = $ypos;
248       $this->in_seq[$ypos] = 1;
249       return $this->lcs;
250     }
251
252     $beg = 1;
253     while ($beg < $end) {
254       $mid = (int)(($beg + $end) / 2);
255       if ($ypos > $this->seq[$mid]) {
256         $beg = $mid + 1;
257       }
258       else {
259         $end = $mid;
260       }
261     }
262
263     $this::USE_ASSERTS && assert($ypos != $this->seq[$end]);
264
265     $this->in_seq[$this->seq[$end]] = FALSE;
266     $this->seq[$end] = $ypos;
267     $this->in_seq[$ypos] = 1;
268     return $end;
269   }
270
271   /**
272    * Find LCS of two sequences.
273    *
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).
277    *
278    * The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
279    *
280    * Note that XLIM, YLIM are exclusive bounds.
281    * All line numbers are origin-0 and discarded lines are not counted.
282    */
283   protected function _compareseq($xoff, $xlim, $yoff, $ylim) {
284
285     // Slide down the bottom initial diagonal.
286     while ($xoff < $xlim && $yoff < $ylim && $this->xv[$xoff] == $this->yv[$yoff]) {
287       ++$xoff;
288       ++$yoff;
289     }
290
291     // Slide up the top initial diagonal.
292     while ($xlim > $xoff && $ylim > $yoff && $this->xv[$xlim - 1] == $this->yv[$ylim - 1]) {
293       --$xlim;
294       --$ylim;
295     }
296
297     if ($xoff == $xlim || $yoff == $ylim) {
298       $lcs = 0;
299     }
300     else {
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);
306     }
307
308     if ($lcs == 0) {
309       // X and Y sequences have no common subsequence:
310       // mark all changed.
311       while ($yoff < $ylim) {
312         $this->ychanged[$this->yind[$yoff++]] = 1;
313       }
314       while ($xoff < $xlim) {
315         $this->xchanged[$this->xind[$xoff++]] = 1;
316       }
317     }
318     else {
319       // Use the partitions to split this problem into subproblems.
320       reset($seps);
321       $pt1 = $seps[0];
322       while ($pt2 = next($seps)) {
323         $this->_compareseq ($pt1[0], $pt2[0], $pt1[1], $pt2[1]);
324         $pt1 = $pt2;
325       }
326     }
327   }
328
329   /**
330    * Adjust inserts/deletes of identical lines to join changes
331    * as much as possible.
332    *
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".
339    *
340    * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
341    */
342   protected function _shift_boundaries($lines, &$changed, $other_changed) {
343     $i = 0;
344     $j = 0;
345
346     $this::USE_ASSERTS && assert('sizeof($lines) == sizeof($changed)');
347     $len = sizeof($lines);
348     $other_len = sizeof($other_changed);
349
350     while (1) {
351       /*
352        * Scan forwards to find beginning of another run of changes.
353        * Also keep track of the corresponding point in the other file.
354        *
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
358        * (unchanged lines).
359        * Furthermore, $j is always kept so that $j == $other_len or
360        * $other_changed[$j] == FALSE.
361        */
362       while ($j < $other_len && $other_changed[$j]) {
363         $j++;
364       }
365       while ($i < $len && !$changed[$i]) {
366         $this::USE_ASSERTS && assert('$j < $other_len && ! $other_changed[$j]');
367         $i++;
368         $j++;
369         while ($j < $other_len && $other_changed[$j]) {
370           $j++;
371         }
372       }
373
374       if ($i == $len) {
375         break;
376       }
377       $start = $i;
378
379       // Find the end of this run of changes.
380       while (++$i < $len && $changed[$i]) {
381         continue;
382       }
383
384       do {
385         /*
386          * Record the length of this run of changes, so that
387          * we can later determine whether the run has grown.
388          */
389         $runlength = $i - $start;
390
391         /*
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.
395          */
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]) {
400             $start--;
401           }
402           $this::USE_ASSERTS && assert('$j > 0');
403           while ($other_changed[--$j]) {
404             continue;
405           }
406           $this::USE_ASSERTS && assert('$j >= 0 && !$other_changed[$j]');
407         }
408
409         /*
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.
413          */
414         $corresponding = $j < $other_len ? $i : $len;
415
416         /*
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.
422          */
423         while ($i < $len && $lines[$start] == $lines[$i]) {
424           $changed[$start++] = FALSE;
425           $changed[$i++] = 1;
426           while ($i < $len && $changed[$i]) {
427             $i++;
428           }
429           $this::USE_ASSERTS && assert('$j < $other_len && ! $other_changed[$j]');
430           $j++;
431           if ($j < $other_len && $other_changed[$j]) {
432             $corresponding = $i;
433             while ($j < $other_len && $other_changed[$j]) {
434               $j++;
435             }
436           }
437         }
438       } while ($runlength != $i - $start);
439
440       /*
441        * If possible, move the fully-merged run of changes
442        * back to a corresponding run in the other file.
443        */
444       while ($corresponding < $i) {
445         $changed[--$start] = 1;
446         $changed[--$i] = 0;
447         $this::USE_ASSERTS && assert('$j > 0');
448         while ($other_changed[--$j]) {
449           continue;
450         }
451         $this::USE_ASSERTS && assert('$j >= 0 && !$other_changed[$j]');
452       }
453     }
454   }
455
456 }