PageRenderTime 55ms CodeModel.GetById 31ms RepoModel.GetById 0ms app.codeStats 1ms

/wp-includes/Text/Diff/Engine/native.php

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