27b0f325724b09ba31deb109af71a2086c0db250
[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         $found_empty = FALSE;
205         foreach ($matches as $y) {
206           if (!$found_empty) {
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];
211               $found_empty = TRUE;
212             }
213           }
214           else {
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;
220               $this->seq[$k] = $y;
221               $this->in_seq[$y] = 1;
222             }
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];
227             }
228           }
229         }
230       }
231     }
232
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);
237       $y1 = $ymid[$n] + 1;
238       $seps[] = $flip ? [$y1, $x1] : [$x1, $y1];
239     }
240     $seps[] = $flip ? [$ylim, $xlim] : [$xlim, $ylim];
241
242     return [$this->lcs, $seps];
243   }
244
245   protected function _lcs_pos($ypos) {
246
247     $end = $this->lcs;
248     if ($end == 0 || $ypos > $this->seq[$end]) {
249       $this->seq[++$this->lcs] = $ypos;
250       $this->in_seq[$ypos] = 1;
251       return $this->lcs;
252     }
253
254     $beg = 1;
255     while ($beg < $end) {
256       $mid = (int)(($beg + $end) / 2);
257       if ($ypos > $this->seq[$mid]) {
258         $beg = $mid + 1;
259       }
260       else {
261         $end = $mid;
262       }
263     }
264
265     $this::USE_ASSERTS && assert($ypos != $this->seq[$end]);
266
267     $this->in_seq[$this->seq[$end]] = FALSE;
268     $this->seq[$end] = $ypos;
269     $this->in_seq[$ypos] = 1;
270     return $end;
271   }
272
273   /**
274    * Find LCS of two sequences.
275    *
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).
279    *
280    * The subsequence of file 0 is [XOFF, XLIM) and likewise for file 1.
281    *
282    * Note that XLIM, YLIM are exclusive bounds.
283    * All line numbers are origin-0 and discarded lines are not counted.
284    */
285   protected function _compareseq($xoff, $xlim, $yoff, $ylim) {
286
287     // Slide down the bottom initial diagonal.
288     while ($xoff < $xlim && $yoff < $ylim && $this->xv[$xoff] == $this->yv[$yoff]) {
289       ++$xoff;
290       ++$yoff;
291     }
292
293     // Slide up the top initial diagonal.
294     while ($xlim > $xoff && $ylim > $yoff && $this->xv[$xlim - 1] == $this->yv[$ylim - 1]) {
295       --$xlim;
296       --$ylim;
297     }
298
299     if ($xoff == $xlim || $yoff == $ylim) {
300       $lcs = 0;
301     }
302     else {
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);
308     }
309
310     if ($lcs == 0) {
311       // X and Y sequences have no common subsequence:
312       // mark all changed.
313       while ($yoff < $ylim) {
314         $this->ychanged[$this->yind[$yoff++]] = 1;
315       }
316       while ($xoff < $xlim) {
317         $this->xchanged[$this->xind[$xoff++]] = 1;
318       }
319     }
320     else {
321       // Use the partitions to split this problem into subproblems.
322       reset($seps);
323       $pt1 = $seps[0];
324       while ($pt2 = next($seps)) {
325         $this->_compareseq ($pt1[0], $pt2[0], $pt1[1], $pt2[1]);
326         $pt1 = $pt2;
327       }
328     }
329   }
330
331   /**
332    * Adjust inserts/deletes of identical lines to join changes
333    * as much as possible.
334    *
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".
341    *
342    * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
343    */
344   protected function _shift_boundaries($lines, &$changed, $other_changed) {
345     $i = 0;
346     $j = 0;
347
348     $this::USE_ASSERTS && assert(sizeof($lines) == sizeof($changed));
349     $len = sizeof($lines);
350     $other_len = sizeof($other_changed);
351
352     while (1) {
353       /*
354        * Scan forwards to find beginning of another run of changes.
355        * Also keep track of the corresponding point in the other file.
356        *
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
360        * (unchanged lines).
361        * Furthermore, $j is always kept so that $j == $other_len or
362        * $other_changed[$j] == FALSE.
363        */
364       while ($j < $other_len && $other_changed[$j]) {
365         $j++;
366       }
367       while ($i < $len && !$changed[$i]) {
368         $this::USE_ASSERTS && assert($j < $other_len && ! $other_changed[$j]);
369         $i++;
370         $j++;
371         while ($j < $other_len && $other_changed[$j]) {
372           $j++;
373         }
374       }
375
376       if ($i == $len) {
377         break;
378       }
379       $start = $i;
380
381       // Find the end of this run of changes.
382       while (++$i < $len && $changed[$i]) {
383         continue;
384       }
385
386       do {
387         /*
388          * Record the length of this run of changes, so that
389          * we can later determine whether the run has grown.
390          */
391         $runlength = $i - $start;
392
393         /*
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.
397          */
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]) {
402             $start--;
403           }
404           $this::USE_ASSERTS && assert($j > 0);
405           while ($other_changed[--$j]) {
406             continue;
407           }
408           $this::USE_ASSERTS && assert($j >= 0 && !$other_changed[$j]);
409         }
410
411         /*
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.
415          */
416         $corresponding = $j < $other_len ? $i : $len;
417
418         /*
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.
424          */
425         while ($i < $len && $lines[$start] == $lines[$i]) {
426           $changed[$start++] = FALSE;
427           $changed[$i++] = 1;
428           while ($i < $len && $changed[$i]) {
429             $i++;
430           }
431           $this::USE_ASSERTS && assert($j < $other_len && ! $other_changed[$j]);
432           $j++;
433           if ($j < $other_len && $other_changed[$j]) {
434             $corresponding = $i;
435             while ($j < $other_len && $other_changed[$j]) {
436               $j++;
437             }
438           }
439         }
440       } while ($runlength != $i - $start);
441
442       /*
443        * If possible, move the fully-merged run of changes
444        * back to a corresponding run in the other file.
445        */
446       while ($corresponding < $i) {
447         $changed[--$start] = 1;
448         $changed[--$i] = 0;
449         $this::USE_ASSERTS && assert($j > 0);
450         while ($other_changed[--$j]) {
451           continue;
452         }
453         $this::USE_ASSERTS && assert($j >= 0 && !$other_changed[$j]);
454       }
455     }
456   }
457
458 }