Look through the patches and break up any which are longer than the maximum limit of the match algorithm. Intended to be called only from within patch_apply. @param patches LinkedList of Patch objects.
(LinkedList<Patch> patches)
| 2129 | * @param patches LinkedList of Patch objects. |
| 2130 | */ |
| 2131 | public void patch_splitMax(LinkedList<Patch> patches) { |
| 2132 | short patch_size = Match_MaxBits; |
| 2133 | String precontext, postcontext; |
| 2134 | Patch patch; |
| 2135 | int start1, start2; |
| 2136 | boolean empty; |
| 2137 | Operation diff_type; |
| 2138 | String diff_text; |
| 2139 | ListIterator<Patch> pointer = patches.listIterator(); |
| 2140 | Patch bigpatch = pointer.hasNext() ? pointer.next() : null; |
| 2141 | while (bigpatch != null) { |
| 2142 | if (bigpatch.length1 <= Match_MaxBits) { |
| 2143 | bigpatch = pointer.hasNext() ? pointer.next() : null; |
| 2144 | continue; |
| 2145 | } |
| 2146 | // Remove the big old patch. |
| 2147 | pointer.remove(); |
| 2148 | start1 = bigpatch.start1; |
| 2149 | start2 = bigpatch.start2; |
| 2150 | precontext = ""; |
| 2151 | while (!bigpatch.diffs.isEmpty()) { |
| 2152 | // Create one of several smaller patches. |
| 2153 | patch = new Patch(); |
| 2154 | empty = true; |
| 2155 | patch.start1 = start1 - precontext.length(); |
| 2156 | patch.start2 = start2 - precontext.length(); |
| 2157 | if (precontext.length() != 0) { |
| 2158 | patch.length1 = patch.length2 = precontext.length(); |
| 2159 | patch.diffs.add(new Diff(Operation.EQUAL, precontext)); |
| 2160 | } |
| 2161 | while (!bigpatch.diffs.isEmpty() |
| 2162 | && patch.length1 < patch_size - Patch_Margin) { |
| 2163 | diff_type = bigpatch.diffs.getFirst().operation; |
| 2164 | diff_text = bigpatch.diffs.getFirst().text; |
| 2165 | if (diff_type == Operation.INSERT) { |
| 2166 | // Insertions are harmless. |
| 2167 | patch.length2 += diff_text.length(); |
| 2168 | start2 += diff_text.length(); |
| 2169 | patch.diffs.addLast(bigpatch.diffs.removeFirst()); |
| 2170 | empty = false; |
| 2171 | } else if (diff_type == Operation.DELETE && patch.diffs.size() == 1 |
| 2172 | && patch.diffs.getFirst().operation == Operation.EQUAL |
| 2173 | && diff_text.length() > 2 * patch_size) { |
| 2174 | // This is a large deletion. Let it pass in one chunk. |
| 2175 | patch.length1 += diff_text.length(); |
| 2176 | start1 += diff_text.length(); |
| 2177 | empty = false; |
| 2178 | patch.diffs.add(new Diff(diff_type, diff_text)); |
| 2179 | bigpatch.diffs.removeFirst(); |
| 2180 | } else { |
| 2181 | // Deletion or equality. Only take as much as we can stomach. |
| 2182 | diff_text = diff_text.substring(0, Math.min(diff_text.length(), |
| 2183 | patch_size - patch.length1 - Patch_Margin)); |
| 2184 | patch.length1 += diff_text.length(); |
| 2185 | start1 += diff_text.length(); |
| 2186 | if (diff_type == Operation.EQUAL) { |
| 2187 | patch.length2 += diff_text.length(); |
| 2188 | start2 += diff_text.length(); |
no test coverage detected