3 * Class used internally by Diff to actually compute the diffs.
5 * This class uses the xdiff PECL package (http://pecl.php.net/package/xdiff)
6 * to compute the differences between the two input arrays.
8 * $Horde: framework/Text_Diff/Diff/Engine/xdiff.php,v 1.6 2008/01/04 10:07:50 jan Exp $
10 * Copyright 2004-2008 The Horde Project (http://www.horde.org/)
12 * See the enclosed file COPYING for license information (LGPL). If you did
13 * not receive this file, see http://opensource.org/licenses/lgpl-license.php.
15 * @author Jon Parise <jon@horde.org>
18 class Text_Diff_Engine_xdiff {
22 function diff($from_lines, $to_lines)
24 array_walk($from_lines, array('Text_Diff', 'trimNewlines'));
25 array_walk($to_lines, array('Text_Diff', 'trimNewlines'));
27 /* Convert the two input arrays into strings for xdiff processing. */
28 $from_string = implode("\n", $from_lines);
29 $to_string = implode("\n", $to_lines);
31 /* Diff the two strings and convert the result to an array. */
32 $diff = xdiff_string_diff($from_string, $to_string, count($to_lines));
33 $diff = explode("\n", $diff);
35 /* Walk through the diff one line at a time. We build the $edits
36 * array of diff operations by reading the first character of the
37 * xdiff output (which is in the "unified diff" format).
39 * Note that we don't have enough information to detect "changed"
40 * lines using this approach, so we can't add Text_Diff_Op_changed
41 * instances to the $edits array. The result is still perfectly
42 * valid, albeit a little less descriptive and efficient. */
44 foreach ($diff as $line) {
47 $edits[] = &new Text_Diff_Op_copy(array(substr($line, 1)));
51 $edits[] = &new Text_Diff_Op_add(array(substr($line, 1)));
55 $edits[] = &new Text_Diff_Op_delete(array(substr($line, 1)));