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 $from_lines array An array of strings.
31 * (Typically these are lines from a file.)
32 * @param $to_lines array An array of strings.
34 public function __construct($from_lines, $to_lines) {
35 $eng = new DiffEngine();
36 $this->edits = $eng->diff($from_lines, $to_lines);
37 //$this->_check($from_lines, $to_lines);
41 * Compute reversed Diff.
45 * $diff = new Diff($lines1, $lines2);
46 * $rev = $diff->reverse();
47 * @return object A Diff object representing the inverse of the
50 public function reverse() {
53 foreach ($this->edits as $edit) {
54 $rev->edits[] = $edit->reverse();
60 * Check for empty diff.
62 * @return bool True iff two sequences were identical.
64 public function isEmpty() {
65 foreach ($this->edits as $edit) {
66 if ($edit->type != 'copy') {
74 * Compute the length of the Longest Common Subsequence (LCS).
76 * This is mostly for diagnostic purposed.
78 * @return int The length of the LCS.
80 public function lcs() {
82 foreach ($this->edits as $edit) {
83 if ($edit->type == 'copy') {
84 $lcs += sizeof($edit->orig);
91 * Gets the original set of lines.
93 * This reconstructs the $from_lines parameter passed to the
96 * @return array The original sequence of strings.
98 public function orig() {
101 foreach ($this->edits as $edit) {
103 array_splice($lines, sizeof($lines), 0, $edit->orig);
110 * Gets the closing set of lines.
112 * This reconstructs the $to_lines parameter passed to the
115 * @return array The sequence of strings.
117 public function closing() {
120 foreach ($this->edits as $edit) {
121 if ($edit->closing) {
122 array_splice($lines, sizeof($lines), 0, $edit->closing);
129 * Check a Diff for validity.
131 * This is here only for debugging purposes.
133 public function check($from_lines, $to_lines) {
134 if (serialize($from_lines) != serialize($this->orig())) {
135 trigger_error("Reconstructed original doesn't match", E_USER_ERROR);
137 if (serialize($to_lines) != serialize($this->closing())) {
138 trigger_error("Reconstructed closing doesn't match", E_USER_ERROR);
141 $rev = $this->reverse();
142 if (serialize($to_lines) != serialize($rev->orig())) {
143 trigger_error("Reversed original doesn't match", E_USER_ERROR);
145 if (serialize($from_lines) != serialize($rev->closing())) {
146 trigger_error("Reversed closing doesn't match", E_USER_ERROR);
151 foreach ($this->edits as $edit) {
152 if ( $prevtype == $edit->type ) {
153 trigger_error("Edit sequence is non-optimal", E_USER_ERROR);
155 $prevtype = $edit->type;
159 trigger_error('Diff okay: LCS = ' . $lcs, E_USER_NOTICE);
163 * Gets the list of differences as an array of diff operations.
165 * @return \Drupal\Component\Diff\Engine\DiffOp[]
166 * The list of differences as an array of diff operations.
168 public function getEdits() {