Class diff_match_patch


  • public class diff_match_patch
    extends Object
    Class containing the diff, match and patch methods. Also contains the behaviour settings.
    • Field Detail

      • Diff_Timeout

        public float Diff_Timeout
      • Diff_EditCost

        public short Diff_EditCost
      • Diff_DualThreshold

        public short Diff_DualThreshold
      • Match_Balance

        public float Match_Balance
      • Match_Threshold

        public float Match_Threshold
      • Match_MinLength

        public int Match_MinLength
      • Match_MaxLength

        public int Match_MaxLength
      • Patch_Margin

        public short Patch_Margin
    • Constructor Detail

      • diff_match_patch

        public diff_match_patch()
    • Method Detail

      • diff_main

        public LinkedList<diff_match_patch.Diff> diff_main​(String text1,
                                                           String text2)
        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
      • diff_main

        public LinkedList<diff_match_patch.Diff> diff_main​(String text1,
                                                           String text2,
                                                           boolean checklines)
        Find the differences between two texts. Simplifies the problem by stripping any common prefix or suffix off the texts before diffing.
        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
      • diff_compute

        protected LinkedList<diff_match_patch.Diff> diff_compute​(String text1,
                                                                 String text2,
                                                                 boolean checklines)
        Find the differences between two texts. Assumes that the texts do not have any common prefix or suffix.
        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
      • diff_linesToChars

        protected Object[] diff_linesToChars​(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:
        Three element Object array, 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.
      • diff_charsToLines

        protected void diff_charsToLines​(LinkedList<diff_match_patch.Diff> diffs,
                                         List<String> lineArray)
        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
      • diff_map

        protected LinkedList<diff_match_patch.Diff> diff_map​(String text1,
                                                             String text2)
        Explore the intersection points between the two texts.
        Parameters:
        text1 - Old string to be diffed
        text2 - New string to be diffed
        Returns:
        LinkedList of Diff objects or null if no diff available
      • diff_path1

        protected LinkedList<diff_match_patch.Diff> diff_path1​(List<Set<Long>> v_map,
                                                               String text1,
                                                               String text2)
        Work from the middle back to the start to determine the path.
        Parameters:
        v_map - List of path sets.
        text1 - Old string fragment to be diffed
        text2 - New string fragment to be diffed
        Returns:
        LinkedList of Diff objects
      • diff_path2

        protected LinkedList<diff_match_patch.Diff> diff_path2​(List<Set<Long>> v_map,
                                                               String text1,
                                                               String text2)
        Work from the middle back to the end to determine the path.
        Parameters:
        v_map - List of path sets.
        text1 - Old string fragment to be diffed
        text2 - New string fragment to be diffed
        Returns:
        LinkedList of Diff objects
      • diff_footprint

        protected Long diff_footprint​(int x,
                                      int y)
        Compute a good hash of two integers.
        Parameters:
        x - First int
        y - Second int
        Returns:
        A long made up of both ints.
      • diff_commonPrefix

        public int diff_commonPrefix​(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.
      • diff_commonSuffix

        public int diff_commonSuffix​(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.
      • diff_halfMatch

        protected String[] diff_halfMatch​(String text1,
                                          String text2)
        Do the two texts share a substring which is at least half the length of the longer text?
        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.
      • diff_cleanupSemantic

        public void diff_cleanupSemantic​(LinkedList<diff_match_patch.Diff> diffs)
        Reduce the number of edits by eliminating semantically trivial equalities.
        Parameters:
        diffs - LinkedList of Diff objects
      • diff_cleanupSemanticLossless

        public void diff_cleanupSemanticLossless​(LinkedList<diff_match_patch.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
      • diff_cleanupEfficiency

        public void diff_cleanupEfficiency​(LinkedList<diff_match_patch.Diff> diffs)
        Reduce the number of edits by eliminating operationally trivial equalities.
        Parameters:
        diffs - LinkedList of Diff objects
      • diff_cleanupMerge

        public void diff_cleanupMerge​(LinkedList<diff_match_patch.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
      • diff_xIndex

        public int diff_xIndex​(LinkedList<diff_match_patch.Diff> diffs,
                               int loc)
        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 - LinkedList of Diff objects
        loc - Location within text1
        Returns:
        Location within text2
      • diff_prettyHtml

        public String diff_prettyHtml​(LinkedList<diff_match_patch.Diff> diffs)
        Convert a Diff list into a pretty HTML report.
        Parameters:
        diffs - LinkedList of Diff objects
        Returns:
        HTML representation
      • diff_text1

        public String diff_text1​(LinkedList<diff_match_patch.Diff> diffs)
        Compute and return the source text (all equalities and deletions).
        Parameters:
        diffs - LinkedList of Diff objects
        Returns:
        Source text
      • diff_text2

        public String diff_text2​(LinkedList<diff_match_patch.Diff> diffs)
        Compute and return the destination text (all equalities and insertions).
        Parameters:
        diffs - LinkedList of Diff objects
        Returns:
        Destination text
      • diff_toDelta

        public String diff_toDelta​(LinkedList<diff_match_patch.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 - Array of diff tuples
        Returns:
        Delta text
      • match_main

        public int match_main​(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
      • match_bitap

        protected int match_bitap​(String text,
                                  String pattern,
                                  int loc)
        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
      • match_alphabet

        protected Map<Character,​Integer> match_alphabet​(String pattern)
        Initialise the alphabet for the Bitap algorithm.
        Parameters:
        pattern - The text to encode
        Returns:
        Hash of character locations
      • patch_addContext

        protected void patch_addContext​(diff_match_patch.Patch patch,
                                        String text)
        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
      • patch_make

        public LinkedList<diff_match_patch.Patch> patch_make​(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 Patch objects.
      • patch_make

        public LinkedList<diff_match_patch.Patch> patch_make​(LinkedList<diff_match_patch.Diff> diffs)
        Compute a list of patches to turn text1 into text2. text1 and text2 will be derived from the provided diff.
        Parameters:
        diffs - Array of diff tuples for text1 to text2.
        Returns:
        LinkedList of Patch objects.
      • patch_apply

        public Object[] patch_apply​(LinkedList<diff_match_patch.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 of patch objects
        text - Old text
        Returns:
        Two element Object array, containing the new text and an array of boolean values
      • patch_addPadding

        protected String patch_addPadding​(LinkedList<diff_match_patch.Patch> patches)
        Add some padding on text start and end so that edges can match something.
        Parameters:
        patches - Array of patch objects
        Returns:
        The padding string added to each side.
      • patch_splitMax

        public void patch_splitMax​(LinkedList<diff_match_patch.Patch> patches)
        Look through the patches and break up any which are longer than the maximum limit of the match algorithm.
        Parameters:
        patches - LinkedList of Patch objects.
      • patch_toText

        public String patch_toText​(List<diff_match_patch.Patch> patches)
        Take a list of patches and return a textual representation.
        Parameters:
        patches - List of Patch objects.
        Returns:
        Text representation of patches.