/** * Find the differences between two texts. * Run a faster, slightly less optimal diff. * This method allows the 'checklines' of diff_main() to be optional. * Most of the time checklines is wanted, so default to true. * @param text1 Old string to be diffed. * @param text2 New string to be diffed. * @return Linked List of Diff objects. */ public LinkedList<Diff> diff_main(String text1, String text2) { return diff_main(text1, text2, true); }
/** * Compute a list of patches to turn text1 into text2. * text2 is ignored, diffs are the delta between text1 and text2. * @param text1 Old text * @param text2 Ignored. * @param diffs Array of Diff objects for text1 to text2. * @return LinkedList of Patch objects. * @deprecated Prefer patch_make(String text1, LinkedList<Diff> diffs). */ public LinkedList<Patch> patch_make(String text1, String text2, LinkedList<Diff> diffs) { return patch_make(text1, diffs); }
/** * Compute a list of patches to turn text1 into text2. * text1 will be derived from the provided diffs. * @param diffs Array of Diff objects for text1 to text2. * @return LinkedList of Patch objects. */ public LinkedList<Patch> patch_make(LinkedList<Diff> diffs) { if (diffs == null) { throw new IllegalArgumentException("Null inputs. (patch_make)"); } // No origin string provided, compute our own. String text1 = diff_text1(diffs); return patch_make(text1, diffs); }
long deadline) { LinesToCharsResult b = diff_linesToChars(text1, text2); text1 = b.chars1; text2 = b.chars2; List<String> linearray = b.lineArray; LinkedList<Diff> diffs = diff_main(text1, text2, false, deadline); diff_charsToLines(diffs, linearray); diff_cleanupSemantic(diffs); diffs.add(new Diff(Operation.EQUAL, "")); int count_delete = 0; int count_insert = 0; pointer.remove(); for (Diff newDiff : diff_main(text_delete, text_insert, false, deadline)) { pointer.add(newDiff);
/** * Compute a list of patches to turn text1 into text2. * A set of diffs will be computed. * @param text1 Old text. * @param text2 New text. * @return LinkedList of Patch objects. */ public LinkedList<Patch> patch_make(String text1, String text2) { if (text1 == null || text2 == null) { throw new IllegalArgumentException("Null inputs. (patch_make)"); } // No diffs provided, compute our own. LinkedList<Diff> diffs = diff_main(text1, text2, true); if (diffs.size() > 2) { diff_cleanupSemantic(diffs); diff_cleanupEfficiency(diffs); } return patch_make(text1, diffs); }
diffs.add(new Diff(Operation.INSERT, text2)); return diffs; diffs.add(new Diff(Operation.DELETE, text1)); return diffs; diffs.add(new Diff(op, longtext.substring(0, i))); diffs.add(new Diff(Operation.EQUAL, shorttext)); diffs.add(new Diff(op, longtext.substring(i + shorttext.length()))); return diffs; diffs.add(new Diff(Operation.DELETE, text1)); diffs.add(new Diff(Operation.INSERT, text2)); return diffs; String[] hm = diff_halfMatch(text1, text2); if (hm != null) { LinkedList<Diff> diffs_a = diff_main(text1_a, text2_a, checklines, deadline); LinkedList<Diff> diffs_b = diff_main(text1_b, text2_b, checklines, deadline); diffs.add(new Diff(Operation.EQUAL, mid_common)); diffs.addAll(diffs_b); return diffs; return diff_lineMode(text1, text2, deadline);
diffs = new LinkedList<Diff>(); if (text1.length() != 0) { diffs.add(new Diff(Operation.EQUAL, text1)); int commonlength = diff_commonPrefix(text1, text2); String commonprefix = text1.substring(0, commonlength); text1 = text1.substring(commonlength); commonlength = diff_commonSuffix(text1, text2); String commonsuffix = text1.substring(text1.length() - commonlength); text1 = text1.substring(0, text1.length() - commonlength); diffs = diff_compute(text1, text2, checklines, deadline); diffs.addFirst(new Diff(Operation.EQUAL, commonprefix)); diffs.addLast(new Diff(Operation.EQUAL, commonsuffix)); diff_cleanupMerge(diffs); return diffs;
protected LinkedList<DiffMatchPatch.Diff> getDiffs(String first, String second) { LinkedList<DiffMatchPatch.Diff> diffs = diffMatchPatch.diff_main(cleanupValue(first), cleanupValue(second)); diffMatchPatch.diff_cleanupSemantic(diffs); return diffs; }
patch = new Patch(); empty = true; patch.start1 = start1 - precontext.length(); if (precontext.length() != 0) { patch.length1 = patch.length2 = precontext.length(); patch.diffs.add(new Diff(Operation.EQUAL, precontext)); start1 += diff_text.length(); empty = false; patch.diffs.add(new Diff(diff_type, diff_text)); bigpatch.diffs.removeFirst(); } else { patch.diffs.add(new Diff(diff_type, diff_text)); if (diff_text.equals(bigpatch.diffs.getFirst().text)) { bigpatch.diffs.removeFirst(); precontext = diff_text2(patch.diffs); precontext = precontext.substring(Math.max(0, precontext.length() - Patch_Margin)); if (diff_text1(bigpatch.diffs).length() > Patch_Margin) { postcontext = diff_text1(bigpatch.diffs).substring(0, Patch_Margin); } else { postcontext = diff_text1(bigpatch.diffs); patch.diffs.getLast().text += postcontext; } else {
/** * Given an array of patches, return another array that is identical. * @param patches Array of Patch objects. * @return Array of Patch objects. */ public LinkedList<Patch> patch_deepCopy(LinkedList<Patch> patches) { LinkedList<Patch> patchesCopy = new LinkedList<Patch>(); for (Patch aPatch : patches) { Patch patchCopy = new Patch(); for (Diff aDiff : aPatch.diffs) { Diff diffCopy = new Diff(aDiff.operation, aDiff.text); patchCopy.diffs.add(diffCopy); } patchCopy.start1 = aPatch.start1; patchCopy.start2 = aPatch.start2; patchCopy.length1 = aPatch.length1; patchCopy.length2 = aPatch.length2; patchesCopy.add(patchCopy); } return patchesCopy; }
/** * Split two texts into a list of strings. Reduce the texts to a string of * hashes where each Unicode character represents one line. * @param text1 First string. * @param text2 Second string. * @return An object containing the encoded text1, the encoded text2 and * the List of unique strings. The zeroth element of the List of * unique strings is intentionally blank. */ protected LinesToCharsResult diff_linesToChars(String text1, String text2) { List<String> lineArray = new ArrayList<String>(); Map<String, Integer> lineHash = new HashMap<String, Integer>(); // e.g. linearray[4] == "Hello\n" // e.g. linehash.get("Hello\n") == 4 // "\x00" is a valid character, but various debuggers don't like it. // So we'll insert a junk entry to avoid generating a null character. lineArray.add(""); String chars1 = diff_linesToCharsMunge(text1, lineArray, lineHash); String chars2 = diff_linesToCharsMunge(text2, lineArray, lineHash); return new LinesToCharsResult(chars1, chars2, lineArray); }
String best_shorttext_a = "", best_shorttext_b = ""; while ((j = shorttext.indexOf(seed, j + 1)) != -1) { int prefixLength = diff_commonPrefix(longtext.substring(i), shorttext.substring(j)); int suffixLength = diff_commonSuffix(longtext.substring(0, i), shorttext.substring(0, j)); if (best_common.length() < suffixLength + prefixLength) {
if (x1 >= x2) { return diff_bisectSplit(text1, text2, x1, y1, deadline); if (x1 >= x2) { return diff_bisectSplit(text1, text2, x1, y1, deadline); diffs.add(new Diff(Operation.DELETE, text1)); diffs.add(new Diff(Operation.INSERT, text2)); return diffs;
return patches; // Get rid of the null case. Patch patch = new Patch(); int char_count1 = 0; // Number of characters into the text1 string. int char_count2 = 0; // Number of characters into the text2 string. patch_addContext(patch, prepatch_text); patches.add(patch); patch = new Patch(); patch_addContext(patch, prepatch_text); patches.add(patch);
/** * Create a numeric hash value for a Diff. * This function is not used by DMP. * @return Hash value. */ @Override public int hashCode() { final int prime = 31; int result = (operation == null) ? 0 : operation.hashCode(); result += prime * ((text == null) ? 0 : text.hashCode()); return result; }
String[] hm1 = diff_halfMatchI(longtext, shorttext, (longtext.length() + 3) / 4); String[] hm2 = diff_halfMatchI(longtext, shorttext, (longtext.length() + 1) / 2); String[] hm;
/** * Locate the best instance of 'pattern' in 'text' near 'loc'. * Returns -1 if no match found. * @param text The text to search. * @param pattern The pattern to search for. * @param loc The location to search around. * @return Best match index or -1. */ public int match_main(String text, String pattern, int loc) { // Check for null inputs. if (text == null || pattern == null) { throw new IllegalArgumentException("Null inputs. (match_main)"); } loc = Math.max(0, Math.min(loc, text.length())); if (text.equals(pattern)) { // Shortcut (potentially not guaranteed by the algorithm) return 0; } else if (text.length() == 0) { // Nothing to match. return -1; } else if (loc + pattern.length() <= text.length() && text.substring(loc, loc + pattern.length()).equals(pattern)) { // Perfect match at the perfect spot! (Includes case of null pattern) return loc; } else { // Do a fuzzy compare. return match_bitap(text, pattern, loc); } }
if (diffs.isEmpty() || diffs.getFirst().operation != Operation.EQUAL) { diffs.addFirst(new Diff(Operation.EQUAL, nullPadding)); if (diffs.isEmpty() || diffs.getLast().operation != Operation.EQUAL) { diffs.addLast(new Diff(Operation.EQUAL, nullPadding)); patch.length1 += paddingLength; patch.length2 += paddingLength;
/** * Find the differences between two texts. * @param text1 Old string to be diffed. * @param text2 New string to be diffed. * @param checklines Speedup flag. If false, then don't run a * line-level diff first to identify the changed areas. * If true, then run a faster slightly less optimal diff. * @return Linked List of Diff objects. */ public LinkedList<Diff> diff_main(String text1, String text2, boolean checklines) { // Set a deadline by which time the diff must be complete. long deadline; if (Diff_Timeout <= 0) { deadline = Long.MAX_VALUE; } else { deadline = System.currentTimeMillis() + (long) (Diff_Timeout * 1000); } return diff_main(text1, text2, checklines, deadline); }
/** * Given the location of the 'middle snake', split the diff in two parts * and recurse. * @param text1 Old string to be diffed. * @param text2 New string to be diffed. * @param x Index of split point in text1. * @param y Index of split point in text2. * @param deadline Time at which to bail if not yet complete. * @return LinkedList of Diff objects. */ private LinkedList<Diff> diff_bisectSplit(String text1, String text2, int x, int y, long deadline) { String text1a = text1.substring(0, x); String text2a = text2.substring(0, y); String text1b = text1.substring(x); String text2b = text2.substring(y); // Compute both diffs serially. LinkedList<Diff> diffs = diff_main(text1a, text2a, false, deadline); LinkedList<Diff> diffsb = diff_main(text1b, text2b, false, deadline); diffs.addAll(diffsb); return diffs; }