3 * Parses unified or context diffs output from eg. the diff utility.
7 * $patch = file_get_contents('example.patch');
8 * $diff = new Text_Diff('string', array($patch));
9 * $renderer = new Text_Diff_Renderer_inline();
10 * echo $renderer->render($diff);
13 * Copyright 2005 Örjan Persson <o@42mm.org>
14 * Copyright 2005-2010 The Horde Project (http://www.horde.org/)
16 * See the enclosed file COPYING for license information (LGPL). If you did
17 * not receive this file, see http://opensource.org/licenses/lgpl-license.php.
19 * @author Örjan Persson <o@42mm.org>
23 class Text_Diff_Engine_string {
26 * Parses a unified or context diff.
28 * First param contains the whole diff and the second can be used to force
29 * a specific diff type. If the second parameter is 'autodetect', the
30 * diff will be examined to find out which type of diff this is.
32 * @param string $diff The diff content.
33 * @param string $mode The diff mode of the content in $diff. One of
34 * 'context', 'unified', or 'autodetect'.
36 * @return array List of all diff operations.
38 function diff($diff, $mode = 'autodetect')
40 // Detect line breaks.
42 if (strpos($diff, "\r\n") !== false) {
44 } elseif (strpos($diff, "\r") !== false) {
48 // Make sure we have a line break at the EOF.
49 if (substr($diff, -strlen($lnbr)) != $lnbr) {
53 if ($mode != 'autodetect' && $mode != 'context' && $mode != 'unified') {
54 return PEAR::raiseError('Type of diff is unsupported');
57 if ($mode == 'autodetect') {
58 $context = strpos($diff, '***');
59 $unified = strpos($diff, '---');
60 if ($context === $unified) {
61 return PEAR::raiseError('Type of diff could not be detected');
62 } elseif ($context === false || $unified === false) {
63 $mode = $context !== false ? 'context' : 'unified';
65 $mode = $context < $unified ? 'context' : 'unified';
69 // Split by new line and remove the diff header, if there is one.
70 $diff = explode($lnbr, $diff);
71 if (($mode == 'context' && strpos($diff[0], '***') === 0) ||
72 ($mode == 'unified' && strpos($diff[0], '---') === 0)) {
77 if ($mode == 'context') {
78 return $this->parseContextDiff($diff);
80 return $this->parseUnifiedDiff($diff);
85 * Parses an array containing the unified diff.
87 * @param array $diff Array of lines.
89 * @return array List of all diff operations.
91 function parseUnifiedDiff($diff)
94 $end = count($diff) - 1;
95 for ($i = 0; $i < $end;) {
97 switch (substr($diff[$i], 0, 1)) {
100 $diff1[] = substr($diff[$i], 1);
101 } while (++$i < $end && substr($diff[$i], 0, 1) == ' ');
102 $edits[] = &new Text_Diff_Op_copy($diff1);
108 $diff1[] = substr($diff[$i], 1);
109 } while (++$i < $end && substr($diff[$i], 0, 1) == '+');
110 $edits[] = &new Text_Diff_Op_add($diff1);
114 // get changed or removed lines
117 $diff1[] = substr($diff[$i], 1);
118 } while (++$i < $end && substr($diff[$i], 0, 1) == '-');
120 while ($i < $end && substr($diff[$i], 0, 1) == '+') {
121 $diff2[] = substr($diff[$i++], 1);
123 if (count($diff2) == 0) {
124 $edits[] = &new Text_Diff_Op_delete($diff1);
126 $edits[] = &new Text_Diff_Op_change($diff1, $diff2);
140 * Parses an array containing the context diff.
142 * @param array $diff Array of lines.
144 * @return array List of all diff operations.
146 function parseContextDiff(&$diff)
149 $i = $max_i = $j = $max_j = 0;
150 $end = count($diff) - 1;
151 while ($i < $end && $j < $end) {
152 while ($i >= $max_i && $j >= $max_j) {
153 // Find the boundaries of the diff output of the two files
155 $i < $end && substr($diff[$i], 0, 3) == '***';
158 $max_i < $end && substr($diff[$max_i], 0, 3) != '---';
161 $j < $end && substr($diff[$j], 0, 3) == '---';
164 $max_j < $end && substr($diff[$max_j], 0, 3) != '***';
168 // find what hasn't been changed
170 while ($i < $max_i &&
172 strcmp($diff[$i], $diff[$j]) == 0) {
173 $array[] = substr($diff[$i], 2);
178 while ($i < $max_i && ($max_j-$j) <= 1) {
179 if ($diff[$i] != '' && substr($diff[$i], 0, 1) != ' ') {
182 $array[] = substr($diff[$i++], 2);
185 while ($j < $max_j && ($max_i-$i) <= 1) {
186 if ($diff[$j] != '' && substr($diff[$j], 0, 1) != ' ') {
189 $array[] = substr($diff[$j++], 2);
191 if (count($array) > 0) {
192 $edits[] = &new Text_Diff_Op_copy($array);
197 switch (substr($diff[$i], 0, 1)) {
201 $diff1[] = substr($diff[$i], 2);
202 if ($j < $max_j && substr($diff[$j], 0, 1) == '!') {
203 $diff2[] = substr($diff[$j++], 2);
205 } while (++$i < $max_i && substr($diff[$i], 0, 1) == '!');
206 $edits[] = &new Text_Diff_Op_change($diff1, $diff2);
211 $diff1[] = substr($diff[$i], 2);
212 } while (++$i < $max_i && substr($diff[$i], 0, 1) == '+');
213 $edits[] = &new Text_Diff_Op_add($diff1);
218 $diff1[] = substr($diff[$i], 2);
219 } while (++$i < $max_i && substr($diff[$i], 0, 1) == '-');
220 $edits[] = &new Text_Diff_Op_delete($diff1);
227 switch (substr($diff[$j], 0, 1)) {
230 $diff2[] = substr($diff[$j++], 2);
231 } while ($j < $max_j && substr($diff[$j], 0, 1) == '+');
232 $edits[] = &new Text_Diff_Op_add($diff2);
237 $diff2[] = substr($diff[$j++], 2);
238 } while ($j < $max_j && substr($diff[$j], 0, 1) == '-');
239 $edits[] = &new Text_Diff_Op_delete($diff2);