string.php 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. <?php
  2. /**
  3. * Parses unified or context diffs output from eg. the diff utility.
  4. *
  5. * Example:
  6. * <code>
  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);
  11. * </code>
  12. *
  13. * $Horde: framework/Text_Diff/Diff/Engine/string.php,v 1.5.2.7 2009/07/24 13:04:43 jan Exp $
  14. *
  15. * Copyright 2005 Örjan Persson <o@42mm.org>
  16. * Copyright 2005-2009 The Horde Project (http://www.horde.org/)
  17. *
  18. * 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.
  20. *
  21. * @author Örjan Persson <o@42mm.org>
  22. * @package Text_Diff
  23. * @since 0.2.0
  24. */
  25. class Text_Diff_Engine_string {
  26. /**
  27. * Parses a unified or context diff.
  28. *
  29. * First param contains the whole diff and the second can be used to force
  30. * a specific diff type. If the second parameter is 'autodetect', the
  31. * diff will be examined to find out which type of diff this is.
  32. *
  33. * @param string $diff The diff content.
  34. * @param string $mode The diff mode of the content in $diff. One of
  35. * 'context', 'unified', or 'autodetect'.
  36. *
  37. * @return array List of all diff operations.
  38. */
  39. function diff($diff, $mode = 'autodetect')
  40. {
  41. // Detect line breaks.
  42. $lnbr = "\n";
  43. if (strpos($diff, "\r\n") !== false) {
  44. $lnbr = "\r\n";
  45. } elseif (strpos($diff, "\r") !== false) {
  46. $lnbr = "\r";
  47. }
  48. // Make sure we have a line break at the EOF.
  49. if (substr($diff, -strlen($lnbr)) != $lnbr) {
  50. $diff .= $lnbr;
  51. }
  52. if ($mode != 'autodetect' && $mode != 'context' && $mode != 'unified') {
  53. return PEAR::raiseError('Type of diff is unsupported');
  54. }
  55. if ($mode == 'autodetect') {
  56. $context = strpos($diff, '***');
  57. $unified = strpos($diff, '---');
  58. if ($context === $unified) {
  59. return PEAR::raiseError('Type of diff could not be detected');
  60. } elseif ($context === false || $unified === false) {
  61. $mode = $context !== false ? 'context' : 'unified';
  62. } else {
  63. $mode = $context < $unified ? 'context' : 'unified';
  64. }
  65. }
  66. // Split by new line and remove the diff header, if there is one.
  67. $diff = explode($lnbr, $diff);
  68. if (($mode == 'context' && strpos($diff[0], '***') === 0) ||
  69. ($mode == 'unified' && strpos($diff[0], '---') === 0)) {
  70. array_shift($diff);
  71. array_shift($diff);
  72. }
  73. if ($mode == 'context') {
  74. return $this->parseContextDiff($diff);
  75. } else {
  76. return $this->parseUnifiedDiff($diff);
  77. }
  78. }
  79. /**
  80. * Parses an array containing the unified diff.
  81. *
  82. * @param array $diff Array of lines.
  83. *
  84. * @return array List of all diff operations.
  85. */
  86. function parseUnifiedDiff($diff)
  87. {
  88. $edits = array();
  89. $end = count($diff) - 1;
  90. for ($i = 0; $i < $end;) {
  91. $diff1 = array();
  92. switch (substr($diff[$i], 0, 1)) {
  93. case ' ':
  94. do {
  95. $diff1[] = substr($diff[$i], 1);
  96. } while (++$i < $end && substr($diff[$i], 0, 1) == ' ');
  97. $edits[] = new Text_Diff_Op_copy($diff1);
  98. break;
  99. case '+':
  100. // get all new lines
  101. do {
  102. $diff1[] = substr($diff[$i], 1);
  103. } while (++$i < $end && substr($diff[$i], 0, 1) == '+');
  104. $edits[] = new Text_Diff_Op_add($diff1);
  105. break;
  106. case '-':
  107. // get changed or removed lines
  108. $diff2 = array();
  109. do {
  110. $diff1[] = substr($diff[$i], 1);
  111. } while (++$i < $end && substr($diff[$i], 0, 1) == '-');
  112. while ($i < $end && substr($diff[$i], 0, 1) == '+') {
  113. $diff2[] = substr($diff[$i++], 1);
  114. }
  115. if (count($diff2) == 0) {
  116. $edits[] = new Text_Diff_Op_delete($diff1);
  117. } else {
  118. $edits[] = new Text_Diff_Op_change($diff1, $diff2);
  119. }
  120. break;
  121. default:
  122. $i++;
  123. break;
  124. }
  125. }
  126. return $edits;
  127. }
  128. /**
  129. * Parses an array containing the context diff.
  130. *
  131. * @param array $diff Array of lines.
  132. *
  133. * @return array List of all diff operations.
  134. */
  135. function parseContextDiff(&$diff)
  136. {
  137. $edits = array();
  138. $i = $max_i = $j = $max_j = 0;
  139. $end = count($diff) - 1;
  140. while ($i < $end && $j < $end) {
  141. while ($i >= $max_i && $j >= $max_j) {
  142. // Find the boundaries of the diff output of the two files
  143. for ($i = $j;
  144. $i < $end && substr($diff[$i], 0, 3) == '***';
  145. $i++);
  146. for ($max_i = $i;
  147. $max_i < $end && substr($diff[$max_i], 0, 3) != '---';
  148. $max_i++);
  149. for ($j = $max_i;
  150. $j < $end && substr($diff[$j], 0, 3) == '---';
  151. $j++);
  152. for ($max_j = $j;
  153. $max_j < $end && substr($diff[$max_j], 0, 3) != '***';
  154. $max_j++);
  155. }
  156. // find what hasn't been changed
  157. $array = array();
  158. while ($i < $max_i &&
  159. $j < $max_j &&
  160. strcmp($diff[$i], $diff[$j]) == 0) {
  161. $array[] = substr($diff[$i], 2);
  162. $i++;
  163. $j++;
  164. }
  165. while ($i < $max_i && ($max_j-$j) <= 1) {
  166. if ($diff[$i] != '' && substr($diff[$i], 0, 1) != ' ') {
  167. break;
  168. }
  169. $array[] = substr($diff[$i++], 2);
  170. }
  171. while ($j < $max_j && ($max_i-$i) <= 1) {
  172. if ($diff[$j] != '' && substr($diff[$j], 0, 1) != ' ') {
  173. break;
  174. }
  175. $array[] = substr($diff[$j++], 2);
  176. }
  177. if (count($array) > 0) {
  178. $edits[] = new Text_Diff_Op_copy($array);
  179. }
  180. if ($i < $max_i) {
  181. $diff1 = array();
  182. switch (substr($diff[$i], 0, 1)) {
  183. case '!':
  184. $diff2 = array();
  185. do {
  186. $diff1[] = substr($diff[$i], 2);
  187. if ($j < $max_j && substr($diff[$j], 0, 1) == '!') {
  188. $diff2[] = substr($diff[$j++], 2);
  189. }
  190. } while (++$i < $max_i && substr($diff[$i], 0, 1) == '!');
  191. $edits[] = new Text_Diff_Op_change($diff1, $diff2);
  192. break;
  193. case '+':
  194. do {
  195. $diff1[] = substr($diff[$i], 2);
  196. } while (++$i < $max_i && substr($diff[$i], 0, 1) == '+');
  197. $edits[] = new Text_Diff_Op_add($diff1);
  198. break;
  199. case '-':
  200. do {
  201. $diff1[] = substr($diff[$i], 2);
  202. } while (++$i < $max_i && substr($diff[$i], 0, 1) == '-');
  203. $edits[] = new Text_Diff_Op_delete($diff1);
  204. break;
  205. }
  206. }
  207. if ($j < $max_j) {
  208. $diff2 = array();
  209. switch (substr($diff[$j], 0, 1)) {
  210. case '+':
  211. do {
  212. $diff2[] = substr($diff[$j++], 2);
  213. } while ($j < $max_j && substr($diff[$j], 0, 1) == '+');
  214. $edits[] = new Text_Diff_Op_add($diff2);
  215. break;
  216. case '-':
  217. do {
  218. $diff2[] = substr($diff[$j++], 2);
  219. } while ($j < $max_j && substr($diff[$j], 0, 1) == '-');
  220. $edits[] = new Text_Diff_Op_delete($diff2);
  221. break;
  222. }
  223. }
  224. }
  225. return $edits;
  226. }
  227. }