Class DiffMatchPatch
java.lang.Object
com.jsql.model.injection.strategy.blind.patch.DiffMatchPatch
Class containing the diff, match and patch methods.
Also contains the behaviour settings.
-
Nested Class Summary
Modifier and TypeClassDescriptionprotected static class
Internal class for returning results from diff_linesToChars().static enum
The data structure representing a diff is a Linked list of Diff objects: {Diff(Operation.DELETE, "Hello"), Diff(Operation.INSERT, "Goodbye"), Diff(Operation.EQUAL, " world.")} -
Field Summary
Modifier and TypeFieldDescriptionstatic final short
Cost of an empty edit operation in terms of edit characters.static final float
Number of seconds to map a diff before giving up (0 for infinity).static final int
How far to search for a match (0 = exact location, 1000+ = broad match).static final float
At what point is no match declared (0.0 = perfection, 1.0 = very loose).static final float
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).static final short
Chunk size for context length. -
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionprotected LinkedList<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 void
diffCharsToLines
(List<Diff> diffs, List<String> lineArray) Rehydrate the text in a diff from a string of line hashes to real lines of text.void
diffCleanupEfficiency
(LinkedList<Diff> diffs) Reduce the number of edits by eliminating operationally trivial equalities.void
diffCleanupMerge
(LinkedList<Diff> diffs) Reorder and merge like edit sections.void
diffCleanupSemantic
(LinkedList<Diff> diffs) Reduce the number of edits by eliminating semantically trivial equalities.void
diffCleanupSemanticLossless
(List<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.protected int
diffCommonOverlap
(String valueText1, String valueText2) Determine if the suffix of one string is the prefix of another.int
diffCommonPrefix
(String text1, String text2) Determine the common prefix of two stringsint
diffCommonSuffix
(String text1, String text2) Determine the common suffix of two stringsdiffFromDelta
(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?int
diffLevenshtein
(List<Diff> diffs) Compute the Levenshtein distance; the number of inserted, deleted or substituted characters.protected DiffMatchPatch.LinesToCharsResult
diffLinesToChars
(String text1, String text2) Split two texts into a list of strings.Find the differences between two texts.Find the differences between two texts.diffPrettyHtml
(List<Diff> diffs) Convert a Diff list into a pretty HTML report.Compute and return the source text (all equalities and deletions).Compute and return the destination text (all equalities and insertions).diffToDelta
(List<Diff> diffs) Crush the diff into an encoded string which describes the operations required to transform text1 into text2.int
diffXIndex
(List<Diff> diffs, int loc) loc is a location in text1, compute and return the equivalent location in text2.matchAlphabet
(String pattern) Initialise the alphabet for the Bitap algorithm.protected int
matchBitap
(String text, String pattern, int loc) Locate the best instance of 'pattern' in 'text' near 'loc' using the Bitap algorithm.int
Locate the best instance of 'pattern' in 'text' near 'loc'.protected void
patchAddContext
(Patch patch, String text) Increase the context until it is unique, but don't let the pattern expand beyond Match_MaxBits.patchAddPadding
(Deque<Patch> patches) Add some padding on text start and end so that edges can match something.Object[]
patchApply
(LinkedList<Patch> valuePatches, String valueText) Merge a set of patches onto the text.patchDeepCopy
(List<Patch> patches) Given an array of patches, return another array that is identical.patchFromText
(String textline) Parse a textual representation of patches and return a List of Patch objects.Compute a list of patches to turn text1 into text2.Compute a list of patches to turn text1 into text2.patchMake
(LinkedList<Diff> diffs) Compute a list of patches to turn text1 into text2.void
patchSplitMax
(List<Patch> patches) Look through the patches and break up any which are longer than the maximum limit of the match algorithm.patchToText
(List<Patch> patches) Take a list of patches and return a textual representation.
-
Field Details
-
DIFF_TIMEOUT
public static final float DIFF_TIMEOUTNumber of seconds to map a diff before giving up (0 for infinity).- See Also:
-
DIFF_EDIT_COST
public static final short DIFF_EDIT_COSTCost of an empty edit operation in terms of edit characters.- See Also:
-
MATCH_THRESHOLD
public static final float MATCH_THRESHOLDAt what point is no match declared (0.0 = perfection, 1.0 = very loose).- See Also:
-
MATCH_DISTANCE
public static final int MATCH_DISTANCEHow 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).- See Also:
-
PATCH_DELETE_THRESHOLD
public static final float PATCH_DELETE_THRESHOLDWhen 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 Match_Threshold controls how closely the end points of a delete need to match.- See Also:
-
PATCH_MARGIN
public static final short PATCH_MARGINChunk size for context length.- See Also:
-
-
Constructor Details
-
DiffMatchPatch
public DiffMatchPatch()
-
-
Method Details
-
diffMain
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.- Parameters:
text1
- Old string to be diffed.text2
- New string to be diffed.- Returns:
- Linked List of Diff objects.
-
diffMain
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
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
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
Rehydrate the text in a diff from a string of line hashes to real lines of text.- Parameters:
diffs
- LinkedList of Diff objects.lineArray
- List of unique strings.
-
diffCommonPrefix
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
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.
-
diffCommonOverlap
Determine if the suffix of one string is the prefix of another.- Parameters:
valueText1
- First string.valueText2
- Second string.- Returns:
- The number of characters common to the end of the first string and the start of the second string.
-
diffHalfMatch
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
Reduce the number of edits by eliminating semantically trivial equalities.- Parameters:
diffs
- LinkedList of Diff objects.
-
diffCleanupSemanticLossless
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 cat came. -> The cat came.- Parameters:
diffs
- LinkedList of Diff objects.
-
diffCleanupEfficiency
Reduce the number of edits by eliminating operationally trivial equalities.- Parameters:
diffs
- LinkedList of Diff objects.
-
diffCleanupMerge
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
loc is 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
Convert a Diff list into a pretty HTML report.- Parameters:
diffs
- List of Diff objects.- Returns:
- HTML representation.
-
diffText1
Compute and return the source text (all equalities and deletions).- Parameters:
diffs
- List of Diff objects.- Returns:
- Source text.
-
diffText2
Compute and return the destination text (all equalities and insertions).- Parameters:
diffs
- List of Diff objects.- Returns:
- Destination text.
-
diffLevenshtein
Compute the Levenshtein distance; the number of inserted, deleted or substituted characters.- Parameters:
diffs
- List of Diff objects.- Returns:
- Number of changes.
-
diffToDelta
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
- Array of Diff objects.- Returns:
- Delta text.
-
diffFromDelta
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.
-
matchMain
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.valueLoc
- The location to search around.- Returns:
- Best match index or -1.
-
matchBitap
Locate the best instance of 'pattern' in 'text' near 'loc' using the Bitap algorithm. 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.
-
matchAlphabet
Initialise the alphabet for the Bitap algorithm.- Parameters:
pattern
- The text to encode.- Returns:
- Hash of character locations.
-
patchAddContext
Increase the context until it is unique, but don't let the pattern expand beyond Match_MaxBits.- Parameters:
patch
- The patch to grow.text
- Source text.
-
patchMake
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 Patch objects.
-
patchMake
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 Patch objects.
-
patchMake
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:
- Deque of Patch objects.
-
patchDeepCopy
Given an array of patches, return another array that is identical.- Parameters:
patches
- Array of Patch objects.- Returns:
- Array of Patch objects.
-
patchApply
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:
valuePatches
- Array of Patch objectsvalueText
- Old text.- Returns:
- Two element Object array, containing the new text and an array of boolean values.
-
patchAddPadding
Add some padding on text start and end so that edges can match something. Intended to be called only from within patch_apply.- Parameters:
patches
- Array of Patch objects.- Returns:
- The padding string added to each side.
-
patchSplitMax
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.- Parameters:
patches
- List of Patch objects.
-
patchToText
Take a list of patches and return a textual representation.- Parameters:
patches
- List of Patch objects.- Returns:
- Text representation of patches.
-
patchFromText
Parse a textual representation of patches and return a List of Patch objects.- Parameters:
textline
- Text representation of patches.- Returns:
- List of Patch objects.
-