Do the two texts share a substring which is at least half the length of the longer text? This speedup can produce non-minimal diffs. @param text1 First string. @param text2 Second string. @return Five element String array, containing the prefix of text1, the suffix of text1, the prefix of text2, th
(String text1, String text2)
| 681 | * prefix of text2, the suffix of text2 and the common middle. Or null if there was no match. |
| 682 | */ |
| 683 | public String[] diff_halfMatch(String text1, String text2) { |
| 684 | if (Diff_Timeout <= 0) { |
| 685 | // Don't risk returning a non-optimal diff if we have unlimited time. |
| 686 | return null; |
| 687 | } |
| 688 | String longtext = text1.length() > text2.length() ? text1 : text2; |
| 689 | String shorttext = text1.length() > text2.length() ? text2 : text1; |
| 690 | if (longtext.length() < 4 || shorttext.length() * 2 < longtext.length()) { |
| 691 | return null; // Pointless. |
| 692 | } |
| 693 | |
| 694 | // First check if the second quarter is the seed for a half-match. |
| 695 | String[] hm1 = diff_halfMatchI(longtext, shorttext, |
| 696 | (longtext.length() + 3) / 4); |
| 697 | // Check again based on the third quarter. |
| 698 | String[] hm2 = diff_halfMatchI(longtext, shorttext, |
| 699 | (longtext.length() + 1) / 2); |
| 700 | String[] hm; |
| 701 | if (hm1 == null && hm2 == null) { |
| 702 | return null; |
| 703 | } else if (hm2 == null) { |
| 704 | hm = hm1; |
| 705 | } else if (hm1 == null) { |
| 706 | hm = hm2; |
| 707 | } else { |
| 708 | // Both matched. Select the longest. |
| 709 | hm = hm1[4].length() > hm2[4].length() ? hm1 : hm2; |
| 710 | } |
| 711 | |
| 712 | // A half-match was found, sort out the return data. |
| 713 | if (text1.length() > text2.length()) { |
| 714 | return hm; |
| 715 | //return new String[]{hm[0], hm[1], hm[2], hm[3], hm[4]}; |
| 716 | } else { |
| 717 | return new String[]{hm[2], hm[3], hm[0], hm[1], hm[4]}; |
| 718 | } |
| 719 | } |
| 720 | |
| 721 | /** |
| 722 | * Does a substring of shorttext exist within longtext such that the |
no test coverage detected