Class DiffMatchPatch
- java.lang.Object
-
- org.bitbucket.cowwoc.diffmatchpatch.DiffMatchPatch
-
public final class DiffMatchPatch extends Object
Class containing the diff, match and patch methods. Also contains the behaviour settings.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classDiffMatchPatch.DiffClass representing one diff operation.static classDiffMatchPatch.OperationThe data structure representing a diff is a Linked list of Diff objects: {Diff(Operation.DELETE, "Hello"), Diff(Operation.INSERT, "Goodbye"), Diff(Operation.EQUAL, " world.")} which means: delete "Hello", add "Goodbye" and keep " world."static classDiffMatchPatch.PatchClass representing one patch operation.
-
Field Summary
Fields Modifier and Type Field Description shortdiffEditCostCost of an empty edit operation in terms of edit characters.floatdiffTimeoutNumber of seconds to map a diff before giving up (0 for infinity).intmatchDistanceHow far to search for a match (0 = exact location, 1000+ = broad match).floatmatchThresholdAt what point is no match declared (0.0 = perfection, 1.0 = very loose).floatpatchDeleteThresholdWhen deleting a large block of text (over ~64 characters), how close do the contents have to be to match the expected contents.shortpatchMarginChunk size for context length.
-
Constructor Summary
Constructors Constructor Description DiffMatchPatch()
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description protected LinkedList<DiffMatchPatch.Diff>diffBisect(String text1, String text2, long deadline)Find the 'middle snake' of a diff, split the problem in two and return the recursively constructed diff.protected voiddiffCharsToLines(List<DiffMatchPatch.Diff> diffs, List<String> lineArray)Rehydrate the text in a diff from a string of line hashes to real lines of text.voiddiffCleanupEfficiency(LinkedList<DiffMatchPatch.Diff> diffs)Reduce the number of edits by eliminating operationally trivial equalities.voiddiffCleanupMerge(LinkedList<DiffMatchPatch.Diff> diffs)Reorder and merge like edit sections.voiddiffCleanupSemantic(LinkedList<DiffMatchPatch.Diff> diffs)Reduce the number of edits by eliminating semantically trivial equalities.voiddiffCleanupSemanticLossless(LinkedList<DiffMatchPatch.Diff> diffs)Look for single edits surrounded on both sides by equalities which can be shifted sideways to align the edit to a word boundary.intdiffCommonPrefix(String text1, String text2)Determine the common prefix of two stringsintdiffCommonSuffix(String text1, String text2)Determine the common suffix of two stringsLinkedList<DiffMatchPatch.Diff>diffFromDelta(String text1, String delta)Given the original text1, and an encoded string which describes the operations required to transform text1 into text2, compute the full diff.protected String[]diffHalfMatch(String text1, String text2)Do the two texts share a substring which is at least half the length of the longer text? This speedup can produce non-minimal diffs.intdiffLevenshtein(List<DiffMatchPatch.Diff> diffs)Compute the Levenshtein distance; the number of inserted, deleted or substituted characters.protected org.bitbucket.cowwoc.diffmatchpatch.DiffMatchPatch.LinesToCharsResultdiffLinesToChars(String text1, String text2)Split two texts into a list of strings.LinkedList<DiffMatchPatch.Diff>diffMain(String text1, String text2)Find the differences between two texts.LinkedList<DiffMatchPatch.Diff>diffMain(String text1, String text2, boolean checklines)Find the differences between two texts.StringdiffPrettyHtml(List<DiffMatchPatch.Diff> diffs)Convert a Diff list into a pretty HTML report.StringdiffText1(List<DiffMatchPatch.Diff> diffs)Compute and return the source text (all equalities and deletions).StringdiffText2(List<DiffMatchPatch.Diff> diffs)Compute and return the destination text (all equalities and insertions).StringdiffToDelta(List<DiffMatchPatch.Diff> diffs)Crush the diff into an encoded string which describes the operations required to transform text1 into text2.intdiffXIndex(List<DiffMatchPatch.Diff> diffs, int loc)locis a location in text1, compute and return the equivalent location in text2.intmatchMain(String text, String pattern, int loc)Locate the best instance of 'pattern' in 'text' near 'loc'.protected voidpatchAddContext(DiffMatchPatch.Patch patch, String text)Increase the context until it is unique, but don't let the pattern expand beyondMATCH_MAX_BITS.Object[]patchApply(LinkedList<DiffMatchPatch.Patch> patches, String text)Merge a set of patches onto the text.LinkedList<DiffMatchPatch.Patch>patchDeepCopy(LinkedList<DiffMatchPatch.Patch> patches)Given an array of patches, return another array that is identical.List<DiffMatchPatch.Patch>patchFromText(String textline)Parse a textual representation of patches and return a List ofPatchobjects.LinkedList<DiffMatchPatch.Patch>patchMake(String text1, String text2)Compute a list of patches to turn text1 into text2.LinkedList<DiffMatchPatch.Patch>patchMake(String text1, String text2, LinkedList<DiffMatchPatch.Diff> diffs)Deprecated.PreferpatchMake(String, LinkedList).LinkedList<DiffMatchPatch.Patch>patchMake(String text1, LinkedList<DiffMatchPatch.Diff> diffs)Compute a list of patches to turn text1 into text2.LinkedList<DiffMatchPatch.Patch>patchMake(LinkedList<DiffMatchPatch.Diff> diffs)Compute a list of patches to turn text1 into text2.StringpatchToText(List<DiffMatchPatch.Patch> patches)Take a list of patches and return a textual representation.
-
-
-
Field Detail
-
diffTimeout
public float diffTimeout
Number of seconds to map a diff before giving up (0 for infinity).
-
diffEditCost
public short diffEditCost
Cost of an empty edit operation in terms of edit characters.
-
matchThreshold
public float matchThreshold
At what point is no match declared (0.0 = perfection, 1.0 = very loose).
-
matchDistance
public int matchDistance
How far to search for a match (0 = exact location, 1000+ = broad match). A match this many characters away from the expected location will add 1.0 to the score (0.0 is a perfect match).
-
patchDeleteThreshold
public float patchDeleteThreshold
When deleting a large block of text (over ~64 characters), how close do the contents have to be to match the expected contents. (0.0 = perfection, 1.0 = very loose). Note that matchThreshold controls how closely the end points of a delete need to match.
-
patchMargin
public short patchMargin
Chunk size for context length.
-
-
Method Detail
-
diffMain
public LinkedList<DiffMatchPatch.Diff> diffMain(String text1, String text2)
Find the differences between two texts. Run a faster, slightly less optimal diff. This method allows the 'checklines' of diffMain() to be optional. Most of the time checklines is wanted, so default to true.- Parameters:
text1- Old string to be diffed.text2- New string to be diffed.- Returns:
- Linked List of Diff objects.
-
diffMain
public LinkedList<DiffMatchPatch.Diff> diffMain(String text1, String text2, boolean checklines)
Find the differences between two texts.- Parameters:
text1- Old string to be diffed.text2- New string to be diffed.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.- Returns:
- Linked List of Diff objects.
-
diffBisect
protected LinkedList<DiffMatchPatch.Diff> diffBisect(String text1, String text2, long deadline)
Find the 'middle snake' of a diff, split the problem in two and return the recursively constructed diff. See Myers 1986 paper: An O(ND) Difference Algorithm and Its Variations.- Parameters:
text1- Old string to be diffed.text2- New string to be diffed.deadline- Time at which to bail if not yet complete.- Returns:
- LinkedList of Diff objects.
-
diffLinesToChars
protected org.bitbucket.cowwoc.diffmatchpatch.DiffMatchPatch.LinesToCharsResult diffLinesToChars(String text1, String text2)
Split two texts into a list of strings. Reduce the texts to a string of hashes where each Unicode character represents one line.- Parameters:
text1- First string.text2- Second string.- Returns:
- 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.
-
diffCharsToLines
protected void diffCharsToLines(List<DiffMatchPatch.Diff> diffs, List<String> lineArray)
Rehydrate the text in a diff from a string of line hashes to real lines of text.- Parameters:
diffs- List of Diff objects.lineArray- List of unique strings.
-
diffCommonPrefix
public int diffCommonPrefix(String text1, String text2)
Determine the common prefix of two strings- Parameters:
text1- First string.text2- Second string.- Returns:
- The number of characters common to the start of each string.
-
diffCommonSuffix
public int diffCommonSuffix(String text1, String text2)
Determine the common suffix of two strings- Parameters:
text1- First string.text2- Second string.- Returns:
- The number of characters common to the end of each string.
-
diffHalfMatch
protected String[] diffHalfMatch(String text1, String text2)
Do the two texts share a substring which is at least half the length of the longer text? This speedup can produce non-minimal diffs.- Parameters:
text1- First string.text2- Second string.- Returns:
- Five element String array, containing the prefix of text1, the suffix of text1, the prefix of text2, the suffix of text2 and the common middle. Or null if there was no match.
-
diffCleanupSemantic
public void diffCleanupSemantic(LinkedList<DiffMatchPatch.Diff> diffs)
Reduce the number of edits by eliminating semantically trivial equalities.- Parameters:
diffs- LinkedList of Diff objects.
-
diffCleanupSemanticLossless
public void diffCleanupSemanticLossless(LinkedList<DiffMatchPatch.Diff> diffs)
Look for single edits surrounded on both sides by equalities which can be shifted sideways to align the edit to a word boundary. e.g:The c<ins>at c</ins>ame. -> The <ins>cat </ins>came.- Parameters:
diffs- LinkedList of Diff objects.
-
diffCleanupEfficiency
public void diffCleanupEfficiency(LinkedList<DiffMatchPatch.Diff> diffs)
Reduce the number of edits by eliminating operationally trivial equalities.- Parameters:
diffs- LinkedList of Diff objects.
-
diffCleanupMerge
public void diffCleanupMerge(LinkedList<DiffMatchPatch.Diff> diffs)
Reorder and merge like edit sections. Merge equalities. Any edit section can move as long as it doesn't cross an equality.- Parameters:
diffs- LinkedList of Diff objects.
-
diffXIndex
public int diffXIndex(List<DiffMatchPatch.Diff> diffs, int loc)
locis a location in text1, compute and return the equivalent location in text2. e.g. "The cat" vs "The big cat",1->1,5->8- Parameters:
diffs- List of Diff objects.loc- Location within text1.- Returns:
- Location within text2.
-
diffPrettyHtml
public String diffPrettyHtml(List<DiffMatchPatch.Diff> diffs)
Convert a Diff list into a pretty HTML report.- Parameters:
diffs- List of Diff objects.- Returns:
- HTML representation.
-
diffText1
public String diffText1(List<DiffMatchPatch.Diff> diffs)
Compute and return the source text (all equalities and deletions).- Parameters:
diffs- List of Diff objects.- Returns:
- Source text.
-
diffText2
public String diffText2(List<DiffMatchPatch.Diff> diffs)
Compute and return the destination text (all equalities and insertions).- Parameters:
diffs- List of Diff objects.- Returns:
- Destination text.
-
diffLevenshtein
public int diffLevenshtein(List<DiffMatchPatch.Diff> diffs)
Compute the Levenshtein distance; the number of inserted, deleted or substituted characters.- Parameters:
diffs- List of Diff objects.- Returns:
- Number of changes.
-
diffToDelta
public String diffToDelta(List<DiffMatchPatch.Diff> diffs)
Crush the diff into an encoded string which describes the operations required to transform text1 into text2. E.g.=3\t-2\t+ing ->Keep 3 chars, delete 2 chars, insert 'ing'. Operations are tab-separated. Inserted text is escaped using %xx notation.- Parameters:
diffs- List of Diff objects.- Returns:
- Delta text.
-
diffFromDelta
public LinkedList<DiffMatchPatch.Diff> diffFromDelta(String text1, String delta) throws IllegalArgumentException
Given the original text1, and an encoded string which describes the operations required to transform text1 into text2, compute the full diff.- Parameters:
text1- Source string for the diff.delta- Delta text.- Returns:
- Array of Diff objects or null if invalid.
- Throws:
IllegalArgumentException- If invalid input.
-
matchMain
public int matchMain(String text, String pattern, int loc)
Locate the best instance of 'pattern' in 'text' near 'loc'. Returns -1 if no match found.- Parameters:
text- The text to search.pattern- The pattern to search for.loc- The location to search around.- Returns:
- Best match index or -1.
-
patchAddContext
protected void patchAddContext(DiffMatchPatch.Patch patch, String text)
Increase the context until it is unique, but don't let the pattern expand beyondMATCH_MAX_BITS.- Parameters:
patch- The patch to grow.text- Source text.
-
patchMake
public LinkedList<DiffMatchPatch.Patch> patchMake(String text1, String text2)
Compute a list of patches to turn text1 into text2. A set of diffs will be computed.- Parameters:
text1- Old text.text2- New text.- Returns:
- LinkedList of
Patchobjects.
-
patchMake
public LinkedList<DiffMatchPatch.Patch> patchMake(LinkedList<DiffMatchPatch.Diff> diffs)
Compute a list of patches to turn text1 into text2. text1 will be derived from the provided diffs.- Parameters:
diffs- Array of Diff objects for text1 to text2.- Returns:
- LinkedList of
Patchobjects.
-
patchMake
@Deprecated public LinkedList<DiffMatchPatch.Patch> patchMake(String text1, String text2, LinkedList<DiffMatchPatch.Diff> diffs)
Deprecated.PreferpatchMake(String, LinkedList).Compute a list of patches to turn text1 into text2. text2 is ignored, diffs are the delta between text1 and text2.- Parameters:
text1- Old texttext2- Ignored.diffs- Array of Diff objects for text1 to text2.- Returns:
- LinkedList of
Patchobjects.
-
patchMake
public LinkedList<DiffMatchPatch.Patch> patchMake(String text1, LinkedList<DiffMatchPatch.Diff> diffs)
Compute a list of patches to turn text1 into text2. text2 is not provided, diffs are the delta between text1 and text2.- Parameters:
text1- Old text.diffs- Array of Diff objects for text1 to text2.- Returns:
- LinkedList of
Patchobjects.
-
patchDeepCopy
public LinkedList<DiffMatchPatch.Patch> patchDeepCopy(LinkedList<DiffMatchPatch.Patch> patches)
Given an array of patches, return another array that is identical.- Parameters:
patches- Array ofPatchobjects.- Returns:
- Array of
Patchobjects.
-
patchApply
public Object[] patchApply(LinkedList<DiffMatchPatch.Patch> patches, String text)
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.- Parameters:
patches- Array ofPatchobjectstext- Old text.- Returns:
- Two element Object array, containing the new text and an array of boolean values.
-
patchToText
public String patchToText(List<DiffMatchPatch.Patch> patches)
Take a list of patches and return a textual representation.- Parameters:
patches- List ofPatchobjects.- Returns:
- Text representation of patches.
-
patchFromText
public List<DiffMatchPatch.Patch> patchFromText(String textline) throws IllegalArgumentException
Parse a textual representation of patches and return a List ofPatchobjects.- Parameters:
textline- Text representation of patches.- Returns:
- List of
Patchobjects. - Throws:
IllegalArgumentException- If invalid input.
-
-