/** * Find the differences between two texts. Simplifies the problem by stripping any common prefix * or suffix off the texts before diffing. * * @return List of Difference objects */ public List<Difference> compare() { // Check for equality (speedup) List<Difference> diffs; if (source.equals(target)) { diffs = new ArrayList<Difference>(); diffs.add(new Difference(EditType.EQUAL, source)); return diffs; } // Trim off common prefix (speedup) int commonLength = Commonality.prefix(source, target); String commonPrefix = source.substring(0, commonLength); source = source.substring(commonLength); target = target.substring(commonLength); // Trim off common suffix (speedup) commonLength = Commonality.suffix(source, target); String commonSuffix = source.substring(source.length() - commonLength); source = source.substring(0, source.length() - commonLength); target = target.substring(0, target.length() - commonLength); // Compute the diff on the middle block diffs = compute(); // Restore the prefix and suffix if (!"".equals(commonPrefix)) { diffs.add(0, new Difference(EditType.EQUAL, commonPrefix)); } if (!"".equals(commonSuffix)) { diffs.add(new Difference(EditType.EQUAL, commonSuffix)); } DiffCleanup.cleanupMerge(diffs); return diffs; }