8 * $diff = new Text_Diff('string', array($patch)); |
8 * $diff = new Text_Diff('string', array($patch)); |
9 * $renderer = new Text_Diff_Renderer_inline(); |
9 * $renderer = new Text_Diff_Renderer_inline(); |
10 * echo $renderer->render($diff); |
10 * echo $renderer->render($diff); |
11 * </code> |
11 * </code> |
12 * |
12 * |
13 * $Horde: framework/Text_Diff/Diff/Engine/string.php,v 1.7 2008/01/04 10:07:50 jan Exp $ |
|
14 * |
|
15 * Copyright 2005 Örjan Persson <o@42mm.org> |
13 * Copyright 2005 Örjan Persson <o@42mm.org> |
16 * Copyright 2005-2008 The Horde Project (http://www.horde.org/) |
14 * Copyright 2005-2010 The Horde Project (http://www.horde.org/) |
17 * |
15 * |
18 * See the enclosed file COPYING for license information (LGPL). If you did |
16 * See the enclosed file COPYING for license information (LGPL). If you did |
19 * not receive this file, see http://opensource.org/licenses/lgpl-license.php. |
17 * not receive this file, see http://opensource.org/licenses/lgpl-license.php. |
20 * |
18 * |
21 * @author Örjan Persson <o@42mm.org> |
19 * @author Örjan Persson <o@42mm.org> |
37 * |
35 * |
38 * @return array List of all diff operations. |
36 * @return array List of all diff operations. |
39 */ |
37 */ |
40 function diff($diff, $mode = 'autodetect') |
38 function diff($diff, $mode = 'autodetect') |
41 { |
39 { |
|
40 // Detect line breaks. |
|
41 $lnbr = "\n"; |
|
42 if (strpos($diff, "\r\n") !== false) { |
|
43 $lnbr = "\r\n"; |
|
44 } elseif (strpos($diff, "\r") !== false) { |
|
45 $lnbr = "\r"; |
|
46 } |
|
47 |
|
48 // Make sure we have a line break at the EOF. |
|
49 if (substr($diff, -strlen($lnbr)) != $lnbr) { |
|
50 $diff .= $lnbr; |
|
51 } |
|
52 |
42 if ($mode != 'autodetect' && $mode != 'context' && $mode != 'unified') { |
53 if ($mode != 'autodetect' && $mode != 'context' && $mode != 'unified') { |
43 return PEAR::raiseError('Type of diff is unsupported'); |
54 return PEAR::raiseError('Type of diff is unsupported'); |
44 } |
55 } |
45 |
56 |
46 if ($mode == 'autodetect') { |
57 if ($mode == 'autodetect') { |
47 $context = strpos($diff, '***'); |
58 $context = strpos($diff, '***'); |
48 $unified = strpos($diff, '---'); |
59 $unified = strpos($diff, '---'); |
49 if ($context === $unified) { |
60 if ($context === $unified) { |
50 return PEAR::raiseError('Type of diff could not be detected'); |
61 return PEAR::raiseError('Type of diff could not be detected'); |
51 } elseif ($context === false || $context === false) { |
62 } elseif ($context === false || $unified === false) { |
52 $mode = $context !== false ? 'context' : 'unified'; |
63 $mode = $context !== false ? 'context' : 'unified'; |
53 } else { |
64 } else { |
54 $mode = $context < $unified ? 'context' : 'unified'; |
65 $mode = $context < $unified ? 'context' : 'unified'; |
55 } |
66 } |
56 } |
67 } |
57 |
68 |
58 // split by new line and remove the diff header |
69 // Split by new line and remove the diff header, if there is one. |
59 $diff = explode("\n", $diff); |
70 $diff = explode($lnbr, $diff); |
60 array_shift($diff); |
71 if (($mode == 'context' && strpos($diff[0], '***') === 0) || |
61 array_shift($diff); |
72 ($mode == 'unified' && strpos($diff[0], '---') === 0)) { |
|
73 array_shift($diff); |
|
74 array_shift($diff); |
|
75 } |
62 |
76 |
63 if ($mode == 'context') { |
77 if ($mode == 'context') { |
64 return $this->parseContextDiff($diff); |
78 return $this->parseContextDiff($diff); |
65 } else { |
79 } else { |
66 return $this->parseUnifiedDiff($diff); |
80 return $this->parseUnifiedDiff($diff); |