3 namespace Drupal\Component\Diff;
5 use Drupal\Component\Diff\Engine\DiffEngine;
8 * Class representing a 'diff' between two sequences of strings.
10 * @subpackage DifferenceEngine
12 * Copied from https://www.drupal.org/project/diff which was based PHP diff
13 * engine for phpwiki. (Taken from phpwiki-1.3.3) The original code in phpwiki
14 * was copyright (C) 2000, 2001 Geoffrey T. Dairiki <dairiki@dairiki.org> and
20 * The list of differences as an array of diff operations.
22 * @var \Drupal\Component\Diff\Engine\DiffOp[]
28 * Computes diff between sequences of strings.
30 * @param array $from_lines
31 * An array of strings.
32 * (Typically these are lines from a file.)
33 * @param array $to_lines
34 * An array of strings.
36 public function __construct($from_lines, $to_lines) {
37 $eng = new DiffEngine();
38 $this->edits = $eng->diff($from_lines, $to_lines);
39 //$this->_check($from_lines, $to_lines);
43 * Compute reversed Diff.
47 * $diff = new Diff($lines1, $lines2);
48 * $rev = $diff->reverse();
50 * A Diff object representing the inverse of the original diff.
52 public function reverse() {
55 foreach ($this->edits as $edit) {
56 $rev->edits[] = $edit->reverse();
62 * Check for empty diff.
64 * @return bool True iff two sequences were identical.
66 public function isEmpty() {
67 foreach ($this->edits as $edit) {
68 if ($edit->type != 'copy') {
76 * Compute the length of the Longest Common Subsequence (LCS).
78 * This is mostly for diagnostic purposed.
80 * @return int The length of the LCS.
82 public function lcs() {
84 foreach ($this->edits as $edit) {
85 if ($edit->type == 'copy') {
86 $lcs += sizeof($edit->orig);
93 * Gets the original set of lines.
95 * This reconstructs the $from_lines parameter passed to the
98 * @return array The original sequence of strings.
100 public function orig() {
103 foreach ($this->edits as $edit) {
105 array_splice($lines, sizeof($lines), 0, $edit->orig);
112 * Gets the closing set of lines.
114 * This reconstructs the $to_lines parameter passed to the
117 * @return array The sequence of strings.
119 public function closing() {
122 foreach ($this->edits as $edit) {
123 if ($edit->closing) {
124 array_splice($lines, sizeof($lines), 0, $edit->closing);
131 * Check a Diff for validity.
133 * This is here only for debugging purposes.
135 public function check($from_lines, $to_lines) {
136 if (serialize($from_lines) != serialize($this->orig())) {
137 trigger_error("Reconstructed original doesn't match", E_USER_ERROR);
139 if (serialize($to_lines) != serialize($this->closing())) {
140 trigger_error("Reconstructed closing doesn't match", E_USER_ERROR);
143 $rev = $this->reverse();
144 if (serialize($to_lines) != serialize($rev->orig())) {
145 trigger_error("Reversed original doesn't match", E_USER_ERROR);
147 if (serialize($from_lines) != serialize($rev->closing())) {
148 trigger_error("Reversed closing doesn't match", E_USER_ERROR);
153 foreach ($this->edits as $edit) {
154 if ( $prevtype == $edit->type ) {
155 trigger_error("Edit sequence is non-optimal", E_USER_ERROR);
157 $prevtype = $edit->type;
161 trigger_error('Diff okay: LCS = ' . $lcs, E_USER_NOTICE);
165 * Gets the list of differences as an array of diff operations.
167 * @return \Drupal\Component\Diff\Engine\DiffOp[]
168 * The list of differences as an array of diff operations.
170 public function getEdits() {