Involved Source Files
Package difflib is a partial port of Python difflib module.
Original source: https://github.com/pmezard/go-difflib
This file is trimmed to only the parts used by this repository.
Package-Level Type Names (total 3, all are exported)
SequenceMatcher compares sequence of strings. The basic
algorithm predates, and is a little fancier than, an algorithm
published in the late 1980's by Ratcliff and Obershelp under the
hyperbolic name "gestalt pattern matching". The basic idea is to find
the longest contiguous matching subsequence that contains no "junk"
elements (R-O doesn't address junk). The same idea is then applied
recursively to the pieces of the sequences to the left and to the right
of the matching subsequence. This does not yield minimal edit
sequences, but does tend to yield matches that "look right" to people.
SequenceMatcher tries to compute a "human-friendly diff" between two
sequences. Unlike e.g. UNIX(tm) diff, the fundamental notion is the
longest *contiguous* & junk-free matching subsequence. That's what
catches peoples' eyes. The Windows(tm) windiff has another interesting
notion, pairing up elements that appear uniquely in each sequence.
That, and the method here, appear to yield more intuitive difference
reports than does diff. This method appears to be the least vulnerable
to synching up on blocks of "junk lines", though (like blank lines in
ordinary text files, or maybe "<P>" lines in HTML files). That may be
because this is the only method of the 3 that has a *concept* of
"junk" <wink>.
Timing: Basic R-O is cubic time worst case and quadratic time expected
case. SequenceMatcher is quadratic time for the worst case and has
expected-case behavior dependent in a complicated way on how many
elements the sequences have in common; best case time is linear.
IsJunkfunc(string) boola[]stringautoJunkboolb[]stringb2jmap[string][]intbJunkmap[string]struct{}bPopularmap[string]struct{}fullBCountmap[string]intmatchingBlocks[]MatchopCodes[]OpCode
GetGroupedOpCodes isolates change clusters by eliminating ranges with no changes.
Return a generator of groups with up to n lines of context.
Each group is in the same format as returned by GetOpCodes().
GetMatchingBlocks returns a list of triples describing matching subsequences.
Each triple is of the form (i, j, n), and means that
a[i:i+n] == b[j:j+n]. The triples are monotonically increasing in
i and in j. It's also guaranteed that if (i, j, n) and (i', j', n') are
adjacent triples in the list, and the second is not the last triple in the
list, then i+n != i' or j+n != j'. IOW, adjacent triples never describe
adjacent equal blocks.
The last triple is a dummy, (len(a), len(b), 0), and is the only
triple with n==0.
GetOpCodes returns a list of 5-tuples describing how to turn a into b.
Each tuple is of the form (tag, i1, i2, j1, j2). The first tuple
has i1 == j1 == 0, and remaining tuples have i1 == the i2 from the
tuple preceding it, and likewise for j1 == the previous j2.
The tags are characters, with these meanings:
'r' (replace): a[i1:i2] should be replaced by b[j1:j2]
'd' (delete): a[i1:i2] should be deleted, j1==j2 in this case.
'i' (insert): b[j1:j2] should be inserted at a[i1:i1], i1==i2 in this case.
'e' (equal): a[i1:i2] == b[j1:j2]
SetSeq1 sets the first sequence to be compared. The second sequence to be compared is
not changed.
SequenceMatcher computes and caches detailed information about the second
sequence, so if you want to compare one sequence S against many sequences,
use .SetSeq2(s) once and call .SetSeq1(x) repeatedly for each of the other
sequences.
See also SetSeqs() and SetSeq2().
SetSeq2 sets the second sequence to be compared. The first sequence to be compared is
not changed.
SetSeqs sets two sequences to be compared.
(*SequenceMatcher) chainB()
Find longest matching block in a[alo:ahi] and b[blo:bhi].
If IsJunk is not defined:
Return (i,j,k) such that a[i:i+k] is equal to b[j:j+k], where
alo <= i <= i+k <= ahi
blo <= j <= j+k <= bhi
and for all (i',j',k') meeting those conditions,
k >= k'
i <= i'
and if i == i', j <= j'
In other words, of all maximal matching blocks, return one that
starts earliest in a, and of all those maximal matching blocks that
start earliest in a, return the one that starts earliest in b.
If IsJunk is defined, first the longest matching block is
determined as above, but with the additional restriction that no
junk element appears in the block. Then that block is extended as
far as possible by matching (only) junk elements on both sides. So
the resulting block never matches on junk except as identical junk
happens to be adjacent to an "interesting" match.
If no blocks match, return (alo, blo, 0).
(*SequenceMatcher) isBJunk(s string) bool
func NewMatcher(a, b []string) *SequenceMatcher
Package-Level Functions (total 3, in which 1 are exported)