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