Find the differences between two texts. Assumes that the texts do not have any common prefix or suffix. @param text1 Old string to be diffed. @param text2 New string to be diffed. @param checklines Speedup flag. If false, then don't run a line-level diff first to identify the changed areas. If tr
(String text1, String text2,
boolean checklines, long deadline)
| 220 | * @return Linked List of Diff objects. |
| 221 | */ |
| 222 | private LinkedList<Diff> diff_compute(String text1, String text2, |
| 223 | boolean checklines, long deadline) { |
| 224 | LinkedList<Diff> diffs = new LinkedList<Diff>(); |
| 225 | |
| 226 | if (text1.length() == 0) { |
| 227 | // Just add some text (speedup). |
| 228 | diffs.add(new Diff(Operation.INSERT, text2)); |
| 229 | return diffs; |
| 230 | } |
| 231 | |
| 232 | if (text2.length() == 0) { |
| 233 | // Just remove some text (speedup). |
| 234 | diffs.add(new Diff(Operation.DELETE, text1)); |
| 235 | return diffs; |
| 236 | } |
| 237 | |
| 238 | String longtext = text1.length() > text2.length() ? text1 : text2; |
| 239 | String shorttext = text1.length() > text2.length() ? text2 : text1; |
| 240 | int i = longtext.indexOf(shorttext); |
| 241 | if (i != -1) { |
| 242 | // Shorter text is inside the longer text (speedup). |
| 243 | Operation op = (text1.length() > text2.length()) ? |
| 244 | Operation.DELETE : Operation.INSERT; |
| 245 | diffs.add(new Diff(op, longtext.substring(0, i))); |
| 246 | diffs.add(new Diff(Operation.EQUAL, shorttext)); |
| 247 | diffs.add(new Diff(op, longtext.substring(i + shorttext.length()))); |
| 248 | return diffs; |
| 249 | } |
| 250 | |
| 251 | if (shorttext.length() == 1) { |
| 252 | // Single character string. |
| 253 | // After the previous speedup, the character can't be an equality. |
| 254 | diffs.add(new Diff(Operation.DELETE, text1)); |
| 255 | diffs.add(new Diff(Operation.INSERT, text2)); |
| 256 | return diffs; |
| 257 | } |
| 258 | |
| 259 | // Check to see if the problem can be split in two. |
| 260 | String[] hm = diff_halfMatch(text1, text2); |
| 261 | if (hm != null) { |
| 262 | // A half-match was found, sort out the return data. |
| 263 | String text1_a = hm[0]; |
| 264 | String text1_b = hm[1]; |
| 265 | String text2_a = hm[2]; |
| 266 | String text2_b = hm[3]; |
| 267 | String mid_common = hm[4]; |
| 268 | // Send both pairs off for separate processing. |
| 269 | LinkedList<Diff> diffs_a = diff_main(text1_a, text2_a, |
| 270 | checklines, deadline); |
| 271 | LinkedList<Diff> diffs_b = diff_main(text1_b, text2_b, |
| 272 | checklines, deadline); |
| 273 | // Merge the results. |
| 274 | diffs = diffs_a; |
| 275 | diffs.add(new Diff(Operation.EQUAL, mid_common)); |
| 276 | diffs.addAll(diffs_b); |
| 277 | return diffs; |
| 278 | } |
| 279 |
no test coverage detected