Merge a set of patches onto the text. Return a patched text, as well as an array of true/false values indicating which patches were applied. @param patches Array of Patch objects @param text Old text. @return Two element Object array, containing the new text and an array of boolean values.
(LinkedList<Patch> patches, String text)
| 1957 | * @return Two element Object array, containing the new text and an array of boolean values. |
| 1958 | */ |
| 1959 | public Object[] patch_apply(LinkedList<Patch> patches, String text) { |
| 1960 | if (patches.isEmpty()) { |
| 1961 | return new Object[]{text, new boolean[0]}; |
| 1962 | } |
| 1963 | |
| 1964 | // Deep copy the patches so that no changes are made to originals. |
| 1965 | patches = patch_deepCopy(patches); |
| 1966 | |
| 1967 | String nullPadding = patch_addPadding(patches); |
| 1968 | text = nullPadding + text + nullPadding; |
| 1969 | patch_splitMax(patches); |
| 1970 | |
| 1971 | int x = 0; |
| 1972 | // delta keeps track of the offset between the expected and actual location |
| 1973 | // of the previous patch. If there are patches expected at positions 10 and |
| 1974 | // 20, but the first patch was found at 12, delta is 2 and the second patch |
| 1975 | // has an effective expected position of 22. |
| 1976 | int delta = 0; |
| 1977 | boolean[] results = new boolean[patches.size()]; |
| 1978 | for (Patch aPatch : patches) { |
| 1979 | int expected_loc = aPatch.start2 + delta; |
| 1980 | String text1 = diff_text1(aPatch.diffs); |
| 1981 | int start_loc; |
| 1982 | int end_loc = -1; |
| 1983 | if (text1.length() > this.Match_MaxBits) { |
| 1984 | // patch_splitMax will only provide an oversized pattern in the case of |
| 1985 | // a monster remove. |
| 1986 | start_loc = match_main(text, |
| 1987 | text1.substring(0, this.Match_MaxBits), expected_loc); |
| 1988 | if (start_loc != -1) { |
| 1989 | end_loc = match_main(text, |
| 1990 | text1.substring(text1.length() - this.Match_MaxBits), |
| 1991 | expected_loc + text1.length() - this.Match_MaxBits); |
| 1992 | if (end_loc == -1 || start_loc >= end_loc) { |
| 1993 | // Can't find valid trailing context. Drop this patch. |
| 1994 | start_loc = -1; |
| 1995 | } |
| 1996 | } |
| 1997 | } else { |
| 1998 | start_loc = match_main(text, text1, expected_loc); |
| 1999 | } |
| 2000 | if (start_loc == -1) { |
| 2001 | // No match found. :( |
| 2002 | results[x] = false; |
| 2003 | // Subtract the delta for this failed patch from subsequent patches. |
| 2004 | delta -= aPatch.length2 - aPatch.length1; |
| 2005 | } else { |
| 2006 | // Found a match. :) |
| 2007 | results[x] = true; |
| 2008 | delta = start_loc - expected_loc; |
| 2009 | String text2; |
| 2010 | if (end_loc == -1) { |
| 2011 | text2 = text.substring(start_loc, |
| 2012 | Math.min(start_loc + text1.length(), text.length())); |
| 2013 | } else { |
| 2014 | text2 = text.substring(start_loc, |
| 2015 | Math.min(end_loc + this.Match_MaxBits, text.length())); |
| 2016 | } |
nothing calls this directly
no test coverage detected