Increase the context until it is unique, but don't let the pattern expand beyond Match_MaxBits. @param patch The patch to grow. @param text Source text.
(Patch patch, String text)
| 1747 | * @param text Source text. |
| 1748 | */ |
| 1749 | public void patch_addContext(Patch patch, String text) { |
| 1750 | if (text.length() == 0) { |
| 1751 | return; |
| 1752 | } |
| 1753 | String pattern = text.substring(patch.start2, patch.start2 + patch.length1); |
| 1754 | int padding = 0; |
| 1755 | |
| 1756 | // Look for the first and last matches of pattern in text. If two different |
| 1757 | // matches are found, increase the pattern length. |
| 1758 | while (text.indexOf(pattern) != text.lastIndexOf(pattern) |
| 1759 | && pattern.length() < Match_MaxBits - Patch_Margin - Patch_Margin) { |
| 1760 | padding += Patch_Margin; |
| 1761 | pattern = text.substring(Math.max(0, patch.start2 - padding), |
| 1762 | Math.min(text.length(), patch.start2 + patch.length1 + padding)); |
| 1763 | } |
| 1764 | // Add one chunk for good luck. |
| 1765 | padding += Patch_Margin; |
| 1766 | |
| 1767 | // Add the prefix. |
| 1768 | String prefix = text.substring(Math.max(0, patch.start2 - padding), |
| 1769 | patch.start2); |
| 1770 | if (prefix.length() != 0) { |
| 1771 | patch.diffs.addFirst(new Diff(Operation.EQUAL, prefix)); |
| 1772 | } |
| 1773 | // Add the suffix. |
| 1774 | String suffix = text.substring(patch.start2 + patch.length1, |
| 1775 | Math.min(text.length(), patch.start2 + patch.length1 + padding)); |
| 1776 | if (suffix.length() != 0) { |
| 1777 | patch.diffs.addLast(new Diff(Operation.EQUAL, suffix)); |
| 1778 | } |
| 1779 | |
| 1780 | // Roll back the start points. |
| 1781 | patch.start1 -= prefix.length(); |
| 1782 | patch.start2 -= prefix.length(); |
| 1783 | // Extend the lengths. |
| 1784 | patch.length1 += prefix.length() + suffix.length(); |
| 1785 | patch.length2 += prefix.length() + suffix.length(); |
| 1786 | } |
| 1787 | |
| 1788 | /** |
| 1789 | * Compute a list of patches to turn text1 into text2. |