PageRenderTime 25ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/web/core/lib/Drupal/Component/Diff/Engine/DiffEngine.php

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