native.php 16 KB

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