string.php 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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.5 2008/09/10 08:31:58 jan Exp $
  14. *
  15. * Copyright 2005 Örjan Persson <o@42mm.org>
  16. * Copyright 2005-2008 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. if ($mode != 'autodetect' && $mode != 'context' && $mode != 'unified') {
  42. return PEAR::raiseError('Type of diff is unsupported');
  43. }
  44. if ($mode == 'autodetect') {
  45. $context = strpos($diff, '***');
  46. $unified = strpos($diff, '---');
  47. if ($context === $unified) {
  48. return PEAR::raiseError('Type of diff could not be detected');
  49. } elseif ($context === false || $unified === false) {
  50. $mode = $context !== false ? 'context' : 'unified';
  51. } else {
  52. $mode = $context < $unified ? 'context' : 'unified';
  53. }
  54. }
  55. // Split by new line and remove the diff header, if there is one.
  56. $diff = explode("\n", $diff);
  57. if (($mode == 'context' && strpos($diff[0], '***') === 0) ||
  58. ($mode == 'unified' && strpos($diff[0], '---') === 0)) {
  59. array_shift($diff);
  60. array_shift($diff);
  61. }
  62. if ($mode == 'context') {
  63. return $this->parseContextDiff($diff);
  64. } else {
  65. return $this->parseUnifiedDiff($diff);
  66. }
  67. }
  68. /**
  69. * Parses an array containing the unified diff.
  70. *
  71. * @param array $diff Array of lines.
  72. *
  73. * @return array List of all diff operations.
  74. */
  75. function parseUnifiedDiff($diff)
  76. {
  77. $edits = array();
  78. $end = count($diff) - 1;
  79. for ($i = 0; $i < $end;) {
  80. $diff1 = array();
  81. switch (substr($diff[$i], 0, 1)) {
  82. case ' ':
  83. do {
  84. $diff1[] = substr($diff[$i], 1);
  85. } while (++$i < $end && substr($diff[$i], 0, 1) == ' ');
  86. $edits[] = new Text_Diff_Op_copy($diff1);
  87. break;
  88. case '+':
  89. // get all new lines
  90. do {
  91. $diff1[] = substr($diff[$i], 1);
  92. } while (++$i < $end && substr($diff[$i], 0, 1) == '+');
  93. $edits[] = new Text_Diff_Op_add($diff1);
  94. break;
  95. case '-':
  96. // get changed or removed lines
  97. $diff2 = array();
  98. do {
  99. $diff1[] = substr($diff[$i], 1);
  100. } while (++$i < $end && substr($diff[$i], 0, 1) == '-');
  101. while ($i < $end && substr($diff[$i], 0, 1) == '+') {
  102. $diff2[] = substr($diff[$i++], 1);
  103. }
  104. if (count($diff2) == 0) {
  105. $edits[] = new Text_Diff_Op_delete($diff1);
  106. } else {
  107. $edits[] = new Text_Diff_Op_change($diff1, $diff2);
  108. }
  109. break;
  110. default:
  111. $i++;
  112. break;
  113. }
  114. }
  115. return $edits;
  116. }
  117. /**
  118. * Parses an array containing the context diff.
  119. *
  120. * @param array $diff Array of lines.
  121. *
  122. * @return array List of all diff operations.
  123. */
  124. function parseContextDiff(&$diff)
  125. {
  126. $edits = array();
  127. $i = $max_i = $j = $max_j = 0;
  128. $end = count($diff) - 1;
  129. while ($i < $end && $j < $end) {
  130. while ($i >= $max_i && $j >= $max_j) {
  131. // Find the boundaries of the diff output of the two files
  132. for ($i = $j;
  133. $i < $end && substr($diff[$i], 0, 3) == '***';
  134. $i++);
  135. for ($max_i = $i;
  136. $max_i < $end && substr($diff[$max_i], 0, 3) != '---';
  137. $max_i++);
  138. for ($j = $max_i;
  139. $j < $end && substr($diff[$j], 0, 3) == '---';
  140. $j++);
  141. for ($max_j = $j;
  142. $max_j < $end && substr($diff[$max_j], 0, 3) != '***';
  143. $max_j++);
  144. }
  145. // find what hasn't been changed
  146. $array = array();
  147. while ($i < $max_i &&
  148. $j < $max_j &&
  149. strcmp($diff[$i], $diff[$j]) == 0) {
  150. $array[] = substr($diff[$i], 2);
  151. $i++;
  152. $j++;
  153. }
  154. while ($i < $max_i && ($max_j-$j) <= 1) {
  155. if ($diff[$i] != '' && substr($diff[$i], 0, 1) != ' ') {
  156. break;
  157. }
  158. $array[] = substr($diff[$i++], 2);
  159. }
  160. while ($j < $max_j && ($max_i-$i) <= 1) {
  161. if ($diff[$j] != '' && substr($diff[$j], 0, 1) != ' ') {
  162. break;
  163. }
  164. $array[] = substr($diff[$j++], 2);
  165. }
  166. if (count($array) > 0) {
  167. $edits[] = new Text_Diff_Op_copy($array);
  168. }
  169. if ($i < $max_i) {
  170. $diff1 = array();
  171. switch (substr($diff[$i], 0, 1)) {
  172. case '!':
  173. $diff2 = array();
  174. do {
  175. $diff1[] = substr($diff[$i], 2);
  176. if ($j < $max_j && substr($diff[$j], 0, 1) == '!') {
  177. $diff2[] = substr($diff[$j++], 2);
  178. }
  179. } while (++$i < $max_i && substr($diff[$i], 0, 1) == '!');
  180. $edits[] = new Text_Diff_Op_change($diff1, $diff2);
  181. break;
  182. case '+':
  183. do {
  184. $diff1[] = substr($diff[$i], 2);
  185. } while (++$i < $max_i && substr($diff[$i], 0, 1) == '+');
  186. $edits[] = new Text_Diff_Op_add($diff1);
  187. break;
  188. case '-':
  189. do {
  190. $diff1[] = substr($diff[$i], 2);
  191. } while (++$i < $max_i && substr($diff[$i], 0, 1) == '-');
  192. $edits[] = new Text_Diff_Op_delete($diff1);
  193. break;
  194. }
  195. }
  196. if ($j < $max_j) {
  197. $diff2 = array();
  198. switch (substr($diff[$j], 0, 1)) {
  199. case '+':
  200. do {
  201. $diff2[] = substr($diff[$j++], 2);
  202. } while ($j < $max_j && substr($diff[$j], 0, 1) == '+');
  203. $edits[] = new Text_Diff_Op_add($diff2);
  204. break;
  205. case '-':
  206. do {
  207. $diff2[] = substr($diff[$j++], 2);
  208. } while ($j < $max_j && substr($diff[$j], 0, 1) == '-');
  209. $edits[] = new Text_Diff_Op_delete($diff2);
  210. break;
  211. }
  212. }
  213. }
  214. return $edits;
  215. }
  216. }