| 805 | }; |
| 806 | |
| 807 | static bool diff_halfMatch(const string_t &text1, const string_t &text2, HalfMatchResult& hm) { |
| 808 | const string_t longtext = text1.length() > text2.length() ? text1 : text2; |
| 809 | const string_t shorttext = text1.length() > text2.length() ? text2 : text1; |
| 810 | if (longtext.length() < 4 || shorttext.length() * 2 < longtext.length()) { |
| 811 | return false; // Pointless. |
| 812 | } |
| 813 | |
| 814 | HalfMatchResult res1, res2; |
| 815 | // First check if the second quarter is the seed for a half-match. |
| 816 | bool hm1 = diff_halfMatchI(longtext, shorttext, |
| 817 | (longtext.length() + 3) / 4, res1); |
| 818 | // Check again based on the third quarter. |
| 819 | bool hm2 = diff_halfMatchI(longtext, shorttext, |
| 820 | (longtext.length() + 1) / 2, res2); |
| 821 | if (!hm1 && !hm2) { |
| 822 | return false; |
| 823 | } else if (!hm2) { |
| 824 | hm.swap(res1); |
| 825 | } else if (!hm1) { |
| 826 | hm.swap(res2); |
| 827 | } else { |
| 828 | // Both matched. Select the longest. |
| 829 | hm.swap(res1.mid_common.length() > res2.mid_common.length() ? res1 : res2); |
| 830 | } |
| 831 | |
| 832 | // A half-match was found, sort out the return data. |
| 833 | if (text1.length() <= text2.length()) { |
| 834 | hm.text1_a.swap(hm.text2_a); |
| 835 | hm.text1_b.swap(hm.text2_b); |
| 836 | } |
| 837 | return true; |
| 838 | } |
| 839 | |
| 840 | /** |
| 841 | * Does a substring of shorttext exist within longtext such that the |