9 * |
9 * |
10 * @access private |
10 * @access private |
11 */ |
11 */ |
12 class Text_Diff_Engine_xdiff { |
12 class Text_Diff_Engine_xdiff { |
13 |
13 |
14 function diff($from_lines, $to_lines) |
14 function diff($from_lines, $to_lines) |
15 { |
15 { |
16 array_walk($from_lines, array('Text_Diff', 'trimNewlines')); |
16 array_walk($from_lines, array('Text_Diff', 'trimNewlines')); |
17 array_walk($to_lines, array('Text_Diff', 'trimNewlines')); |
17 array_walk($to_lines, array('Text_Diff', 'trimNewlines')); |
18 |
18 |
19 /* Convert the two input arrays into strings for xdiff processing. */ |
19 /* Convert the two input arrays into strings for xdiff processing. */ |
20 $from_string = implode("\n", $from_lines); |
20 $from_string = implode("\n", $from_lines); |
21 $to_string = implode("\n", $to_lines); |
21 $to_string = implode("\n", $to_lines); |
22 |
22 |
23 /* Diff the two strings and convert the result to an array. */ |
23 /* Diff the two strings and convert the result to an array. */ |
24 $diff = xdiff_string_diff($from_string, $to_string, count($to_lines)); |
24 $diff = xdiff_string_diff($from_string, $to_string, count($to_lines)); |
25 $diff = explode("\n", $diff); |
25 $diff = explode("\n", $diff); |
26 |
26 |
27 /* Walk through the diff one line at a time. We build the $edits |
27 /* Walk through the diff one line at a time. We build the $edits |
28 * array of diff operations by reading the first character of the |
28 * array of diff operations by reading the first character of the |
29 * xdiff output (which is in the "unified diff" format). |
29 * xdiff output (which is in the "unified diff" format). |
30 * |
30 * |
31 * Note that we don't have enough information to detect "changed" |
31 * Note that we don't have enough information to detect "changed" |
32 * lines using this approach, so we can't add Text_Diff_Op_changed |
32 * lines using this approach, so we can't add Text_Diff_Op_changed |
33 * instances to the $edits array. The result is still perfectly |
33 * instances to the $edits array. The result is still perfectly |
34 * valid, albeit a little less descriptive and efficient. */ |
34 * valid, albeit a little less descriptive and efficient. */ |
35 $edits = array(); |
35 $edits = array(); |
36 foreach ($diff as $line) { |
36 foreach ($diff as $line) { |
37 switch ($line[0]) { |
37 switch ($line[0]) { |
38 case ' ': |
38 case ' ': |
39 $edits[] = &new Text_Diff_Op_copy(array(substr($line, 1))); |
39 $edits[] = &new Text_Diff_Op_copy(array(substr($line, 1))); |
40 break; |
40 break; |
41 |
41 |
42 case '+': |
42 case '+': |
43 $edits[] = &new Text_Diff_Op_add(array(substr($line, 1))); |
43 $edits[] = &new Text_Diff_Op_add(array(substr($line, 1))); |
44 break; |
44 break; |
45 |
45 |
46 case '-': |
46 case '-': |
47 $edits[] = &new Text_Diff_Op_delete(array(substr($line, 1))); |
47 $edits[] = &new Text_Diff_Op_delete(array(substr($line, 1))); |
48 break; |
48 break; |
49 } |
49 } |
50 } |
50 } |
51 |
51 |
52 return $edits; |
52 return $edits; |
53 } |
53 } |
54 |
54 |
55 } |
55 } |