1<?php
2/**
3 * Class used internally by Text_Diff to actually compute the diffs.
4 *
5 * This class is implemented using native PHP code.
6 *
7 * The algorithm used here is mostly lifted from the perl module
8 * Algorithm::Diff (version 1.06) by Ned Konz, which is available at:
9 * https://cpan.metacpan.org/authors/id/N/NE/NEDKONZ/Algorithm-Diff-1.06.zip
10 *
11 * More ideas are taken from: http://www.ics.uci.edu/~eppstein/161/960229.html
12 *
13 * Some ideas (and a bit of code) are taken from analyze.c, of GNU
14 * diffutils-2.7, which can be found at:
15 * ftp://gnudist.gnu.org/pub/gnu/diffutils/diffutils-2.7.tar.gz
16 *
17 * Some ideas (subdivision by NCHUNKS > 2, and some optimizations) are from
18 * Geoffrey T. Dairiki <dairiki@dairiki.org>. The original PHP version of this
19 * code was written by him, and is used/adapted with his permission.
20 *
21 * Copyright 2004-2010 The Horde Project (http://www.horde.org/)
22 *
23 * See the enclosed file COPYING for license information (LGPL). If you did
24 * not receive this file, see https://opensource.org/license/lgpl-2-1/.
25 *
26 * @author Geoffrey T. Dairiki <dairiki@dairiki.org>
27 * @package Text_Diff
28 */
29class Text_Diff_Engine_native {
30
31 public $xchanged;
32 public $ychanged;
33 public $xv;
34 public $yv;
35 public $xind;
36 public $yind;
37 public $seq;
38 public $in_seq;
39 public $lcs;
40
41 function diff($from_lines, $to_lines)
42 {
43 array_walk($from_lines, array('Text_Diff', 'trimNewlines'));
44 array_walk($to_lines, array('Text_Diff', 'trimNewlines'));
45
46 $n_from = count($from_lines);
47 $n_to = count($to_lines);
48
49 $this->xchanged = $this->ychanged = array();
50 $this->xv = $this->yv = array();
51 $this->xind = $this->yind = array();
52 unset($this->seq);
53 unset($this->in_seq);
54 unset($this->lcs);
55
56 // Skip leading common lines.
57 for ($skip = 0; $skip < $n_from && $skip < $n_to; $skip++) {
58 if ($from_lines[$skip] !== $to_lines[$skip]) {
59 break;
60 }
61 $this->xchanged[$skip] = $this->ychanged[$skip] = false;
62 }
63
64 // Skip trailing common lines.
65 $xi = $n_from; $yi = $n_to;
66 for ($endskip = 0; --$xi > $skip && --$yi > $skip; $endskip++) {
67 if ($from_lines[$xi] !== $to_lines[$yi]) {
68 break;
69 }
70 $this->xchanged[$xi] = $this->ychanged[$yi] = false;
71 }
72
73 // Ignore lines which do not exist in both files.
74 for ($xi = $skip; $xi < $n_from - $endskip; $xi++) {
75 $xhash[$from_lines[$xi]] = 1;
76 }
77 for ($yi = $skip; $yi < $n_to - $endskip; $yi++) {
78 $line = $to_lines[$yi];
79 if (($this->ychanged[$yi] = empty($xhash[$line]))) {
80 continue;
81 }
82 $yhash[$line] = 1;
83 $this->yv[] = $line;
84 $this->yind[] = $yi;
85 }
86 for ($xi = $skip; $xi < $n_from - $endskip; $xi++) {
87 $line = $from_lines[$xi];
88 if (($this->xchanged[$xi] = empty($yhash[$line]))) {
89 continue;
90 }
91 $this->xv[] = $line;
92 $this->xind[] = $xi;
93 }
94
95 // Find the LCS.
96 $this->_compareseq(0, count($this->xv), 0, count($this->yv));
97
98 // Merge edits when possible.
99 $this->_shiftBoundaries($from_lines, $this->xchanged, $this->ychanged);
100 $this->_shiftBoundaries($to_lines, $this->ychanged, $this->xchanged);
101
102 // Compute the edit operations.
103 $edits = array();
104 $xi = $yi = 0;
105 while ($xi < $n_from || $yi < $n_to) {
106 assert($yi < $n_to || $this->xchanged[$xi]);
107 assert($xi < $n_from || $this->ychanged[$yi]);
108
109 // Skip matching "snake".
110 $copy = array();
111 while ($xi < $n_from && $yi < $n_to
112 && !$this->xchanged[$xi] && !$this->ychanged[$yi]) {
113 $copy[] = $from_lines[$xi++];
114 ++$yi;
115 }
116 if ($copy) {
117 $edits[] = new Text_Diff_Op_copy($copy);
118 }
119
120 // Find deletes & adds.
121 $delete = array();
122 while ($xi < $n_from && $this->xchanged[$xi]) {
123 $delete[] = $from_lines[$xi++];
124 }
125
126 $add = array();
127 while ($yi < $n_to && $this->ychanged[$yi]) {
128 $add[] = $to_lines[$yi++];
129 }
130
131 if ($delete && $add) {
132 $edits[] = new Text_Diff_Op_change($delete, $add);
133 } elseif ($delete) {
134 $edits[] = new Text_Diff_Op_delete($delete);
135 } elseif ($add) {
136 $edits[] = new Text_Diff_Op_add($add);
137 }
138 }
139
140 return $edits;
141 }
142
143 /**
144 * Divides the Largest Common Subsequence (LCS) of the sequences (XOFF,
145 * XLIM) and (YOFF, YLIM) into NCHUNKS approximately equally sized
146 * segments.
147 *
148 * Returns (LCS, PTS). LCS is the length of the LCS. PTS is an array of
149 * NCHUNKS+1 (X, Y) indexes giving the diving points between sub
150 * sequences. The first sub-sequence is contained in (X0, X1), (Y0, Y1),
151 * the second in (X1, X2), (Y1, Y2) and so on. Note that (X0, Y0) ==
152 * (XOFF, YOFF) and (X[NCHUNKS], Y[NCHUNKS]) == (XLIM, YLIM).
153 *
154 * This function assumes that the first lines of the specified portions of
155 * the two files do not match, and likewise that the last lines do not
156 * match. The caller must trim matching lines from the beginning and end
157 * of the portions it is going to specify.
158 */
159 function _diag ($xoff, $xlim, $yoff, $ylim, $nchunks)
160 {
161 $flip = false;
162
163 if ($xlim - $xoff > $ylim - $yoff) {
164 /* Things seems faster (I'm not sure I understand why) when the
165 * shortest sequence is in X. */
166 $flip = true;
167 list ($xoff, $xlim, $yoff, $ylim)
168 = array($yoff, $ylim, $xoff, $xlim);
169 }
170
171 if ($flip) {
172 for ($i = $ylim - 1; $i >= $yoff; $i--) {
173 $ymatches[$this->xv[$i]][] = $i;
174 }
175 } else {
176 for ($i = $ylim - 1; $i >= $yoff; $i--) {
177 $ymatches[$this->yv[$i]][] = $i;
178 }
179 }
180
181 $this->lcs = 0;
182 $this->seq[0]= $yoff - 1;
183 $this->in_seq = array();
184 $ymids[0] = array();
185
186 $numer = $xlim - $xoff + $nchunks - 1;
187 $x = $xoff;
188 for ($chunk = 0; $chunk < $nchunks; $chunk++) {
189 if ($chunk > 0) {
190 for ($i = 0; $i <= $this->lcs; $i++) {
191 $ymids[$i][$chunk - 1] = $this->seq[$i];
192 }
193 }
194
195 $x1 = $xoff + (int)(($numer + ($xlim - $xoff) * $chunk) / $nchunks);
196 for (; $x < $x1; $x++) {
197 $line = $flip ? $this->yv[$x] : $this->xv[$x];
198 if (empty($ymatches[$line])) {
199 continue;
200 }
201 $matches = $ymatches[$line];
202 reset($matches);
203 while ($y = current($matches)) {
204 if (empty($this->in_seq[$y])) {
205 $k = $this->_lcsPos($y);
206 assert($k > 0);
207 $ymids[$k] = $ymids[$k - 1];
208 break;
209 }
210 next($matches);
211 }
212 while ($y = current($matches)) {
213 if ($y > $this->seq[$k - 1]) {
214 assert($y <= $this->seq[$k]);
215 /* Optimization: this is a common case: next match is
216 * just replacing previous match. */
217 $this->in_seq[$this->seq[$k]] = false;
218 $this->seq[$k] = $y;
219 $this->in_seq[$y] = 1;
220 } elseif (empty($this->in_seq[$y])) {
221 $k = $this->_lcsPos($y);
222 assert($k > 0);
223 $ymids[$k] = $ymids[$k - 1];
224 }
225 next($matches);
226 }
227 }
228 }
229
230 $seps[] = $flip ? array($yoff, $xoff) : array($xoff, $yoff);
231 $ymid = $ymids[$this->lcs];
232 for ($n = 0; $n < $nchunks - 1; $n++) {
233 $x1 = $xoff + (int)(($numer + ($xlim - $xoff) * $n) / $nchunks);
234 $y1 = $ymid[$n] + 1;
235 $seps[] = $flip ? array($y1, $x1) : array($x1, $y1);
236 }
237 $seps[] = $flip ? array($ylim, $xlim) : array($xlim, $ylim);
238
239 return array($this->lcs, $seps);
240 }
241
242 function _lcsPos($ypos)
243 {
244 $end = $this->lcs;
245 if ($end == 0 || $ypos > $this->seq[$end]) {
246 $this->seq[++$this->lcs] = $ypos;
247 $this->in_seq[$ypos] = 1;
248 return $this->lcs;
249 }
250
251 $beg = 1;
252 while ($beg < $end) {
253 $mid = (int)(($beg + $end) / 2);
254 if ($ypos > $this->seq[$mid]) {
255 $beg = $mid + 1;
256 } else {
257 $end = $mid;
258 }
259 }
260
261 assert($ypos != $this->seq[$end]);
262
263 $this->in_seq[$this->seq[$end]] = false;
264 $this->seq[$end] = $ypos;
265 $this->in_seq[$ypos] = 1;
266 return $end;
267 }
268
269 /**
270 * Finds LCS of two sequences.
271 *
272 * The results are recorded in the vectors $this->{x,y}changed[], by
273 * storing a 1 in the element for each line that is an insertion or
274 * deletion (ie. is not in the LCS).
275 *
276 * The subsequence of file 0 is (XOFF, XLIM) and likewise for file 1.
277 *
278 * Note that XLIM, YLIM are exclusive bounds. All line numbers are
279 * origin-0 and discarded lines are not counted.
280 */
281 function _compareseq ($xoff, $xlim, $yoff, $ylim)
282 {
283 /* Slide down the bottom initial diagonal. */
284 while ($xoff < $xlim && $yoff < $ylim
285 && $this->xv[$xoff] == $this->yv[$yoff]) {
286 ++$xoff;
287 ++$yoff;
288 }
289
290 /* Slide up the top initial diagonal. */
291 while ($xlim > $xoff && $ylim > $yoff
292 && $this->xv[$xlim - 1] == $this->yv[$ylim - 1]) {
293 --$xlim;
294 --$ylim;
295 }
296
297 if ($xoff == $xlim || $yoff == $ylim) {
298 $lcs = 0;
299 } else {
300 /* This is ad hoc but seems to work well. $nchunks =
301 * sqrt(min($xlim - $xoff, $ylim - $yoff) / 2.5); $nchunks =
302 * max(2,min(8,(int)$nchunks)); */
303 $nchunks = min(7, $xlim - $xoff, $ylim - $yoff) + 1;
304 list($lcs, $seps)
305 = $this->_diag($xoff, $xlim, $yoff, $ylim, $nchunks);
306 }
307
308 if ($lcs == 0) {
309 /* X and Y sequences have no common subsequence: mark all
310 * 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 } else {
318 /* Use the partitions to split this problem into subproblems. */
319 reset($seps);
320 $pt1 = $seps[0];
321 while ($pt2 = next($seps)) {
322 $this->_compareseq ($pt1[0], $pt2[0], $pt1[1], $pt2[1]);
323 $pt1 = $pt2;
324 }
325 }
326 }
327
328 /**
329 * Adjusts inserts/deletes of identical lines to join changes as much as
330 * possible.
331 *
332 * We do something when a run of changed lines include a line at one end
333 * and has an excluded, identical line at the other. We are free to
334 * choose which identical line is included. `compareseq' usually chooses
335 * the one at the beginning, but usually it is cleaner to consider the
336 * following identical line to be the "change".
337 *
338 * This is extracted verbatim from analyze.c (GNU diffutils-2.7).
339 */
340 function _shiftBoundaries($lines, &$changed, $other_changed)
341 {
342 $i = 0;
343 $j = 0;
344
345 assert(count($lines) == count($changed));
346 $len = count($lines);
347 $other_len = count($other_changed);
348
349 while (1) {
350 /* Scan forward to find the beginning of another run of
351 * changes. Also keep track of the corresponding point in the
352 * other file.
353 *
354 * Throughout this code, $i and $j are adjusted together so that
355 * the first $i elements of $changed and the first $j elements of
356 * $other_changed both contain the same number of zeros (unchanged
357 * lines).
358 *
359 * Furthermore, $j is always kept so that $j == $other_len or
360 * $other_changed[$j] == false. */
361 while ($j < $other_len && $other_changed[$j]) {
362 $j++;
363 }
364
365 while ($i < $len && ! $changed[$i]) {
366 assert($j < $other_len && ! $other_changed[$j]);
367 $i++; $j++;
368 while ($j < $other_len && $other_changed[$j]) {
369 $j++;
370 }
371 }
372
373 if ($i == $len) {
374 break;
375 }
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 /* Record the length of this run of changes, so that we can
386 * later determine whether the run has grown. */
387 $runlength = $i - $start;
388
389 /* Move the changed region back, so long as the previous
390 * unchanged line matches the last changed one. This merges
391 * with previous changed regions. */
392 while ($start > 0 && $lines[$start - 1] == $lines[$i - 1]) {
393 $changed[--$start] = 1;
394 $changed[--$i] = false;
395 while ($start > 0 && $changed[$start - 1]) {
396 $start--;
397 }
398 assert($j > 0);
399 while ($other_changed[--$j]) {
400 continue;
401 }
402 assert($j >= 0 && !$other_changed[$j]);
403 }
404
405 /* Set CORRESPONDING to the end of the changed run, at the
406 * last point where it corresponds to a changed run in the
407 * other file. CORRESPONDING == LEN means no such point has
408 * been found. */
409 $corresponding = $j < $other_len ? $i : $len;
410
411 /* Move the changed region forward, so long as the first
412 * changed line matches the following unchanged one. This
413 * merges with following changed regions. Do this second, so
414 * that if there are no merges, the changed region is moved
415 * forward as far as possible. */
416 while ($i < $len && $lines[$start] == $lines[$i]) {
417 $changed[$start++] = false;
418 $changed[$i++] = 1;
419 while ($i < $len && $changed[$i]) {
420 $i++;
421 }
422
423 assert($j < $other_len && ! $other_changed[$j]);
424 $j++;
425 if ($j < $other_len && $other_changed[$j]) {
426 $corresponding = $i;
427 while ($j < $other_len && $other_changed[$j]) {
428 $j++;
429 }
430 }
431 }
432 } while ($runlength != $i - $start);
433
434 /* If possible, move the fully-merged run of changes back to a
435 * corresponding run in the other file. */
436 while ($corresponding < $i) {
437 $changed[--$start] = 1;
438 $changed[--$i] = 0;
439 assert($j > 0);
440 while ($other_changed[--$j]) {
441 continue;
442 }
443 assert($j >= 0 && !$other_changed[$j]);
444 }
445 }
446 }
447
448}
449