Do a quick line-level diff on both strings, then rediff the parts for greater accuracy. This speedup can produce non-minimal diffs. @param text1 Old string to be diffed. @param text2 New string to be diffed. @param deadline Time when the diff should be complete by. @return Linked List of Diff objec
(String text1, String text2,
long deadline)
| 295 | * @return Linked List of Diff objects. |
| 296 | */ |
| 297 | private LinkedList<Diff> diff_lineMode(String text1, String text2, |
| 298 | long deadline) { |
| 299 | // Scan the text on a line-by-line basis first. |
| 300 | LinesToCharsResult b = diff_linesToChars(text1, text2); |
| 301 | text1 = b.chars1; |
| 302 | text2 = b.chars2; |
| 303 | List<String> linearray = b.lineArray; |
| 304 | |
| 305 | LinkedList<Diff> diffs = diff_main(text1, text2, false, deadline); |
| 306 | |
| 307 | // Convert the diff back to original text. |
| 308 | diff_charsToLines(diffs, linearray); |
| 309 | // Eliminate freak matches (e.g. blank lines) |
| 310 | diff_cleanupSemantic(diffs); |
| 311 | |
| 312 | // Rediff any replacement blocks, this time character-by-character. |
| 313 | // Add a dummy entry at the end. |
| 314 | diffs.add(new Diff(Operation.EQUAL, "")); |
| 315 | int count_delete = 0; |
| 316 | int count_insert = 0; |
| 317 | String text_delete = ""; |
| 318 | String text_insert = ""; |
| 319 | ListIterator<Diff> pointer = diffs.listIterator(); |
| 320 | Diff thisDiff = pointer.next(); |
| 321 | while (thisDiff != null) { |
| 322 | switch (thisDiff.operation) { |
| 323 | case INSERT: |
| 324 | count_insert++; |
| 325 | text_insert += thisDiff.text; |
| 326 | break; |
| 327 | case DELETE: |
| 328 | count_delete++; |
| 329 | text_delete += thisDiff.text; |
| 330 | break; |
| 331 | case EQUAL: |
| 332 | // Upon reaching an equality, check for prior redundancies. |
| 333 | if (count_delete >= 1 && count_insert >= 1) { |
| 334 | // Delete the offending records and add the merged ones. |
| 335 | pointer.previous(); |
| 336 | for (int j = 0; j < count_delete + count_insert; j++) { |
| 337 | pointer.previous(); |
| 338 | pointer.remove(); |
| 339 | } |
| 340 | for (Diff newDiff : diff_main(text_delete, text_insert, false, |
| 341 | deadline)) { |
| 342 | pointer.add(newDiff); |
| 343 | } |
| 344 | } |
| 345 | count_insert = 0; |
| 346 | count_delete = 0; |
| 347 | text_delete = ""; |
| 348 | text_insert = ""; |
| 349 | break; |
| 350 | } |
| 351 | thisDiff = pointer.hasNext() ? pointer.next() : null; |
| 352 | } |
| 353 | diffs.removeLast(); // Remove the dummy entry at the end. |
| 354 |
no test coverage detected