forked from ebhomengo/niki
1
0
Fork 0
niki/vendor/github.com/pmezard/go-difflib/difflib/difflib.go

1438 lines
23 KiB
Go
Raw Normal View History

2024-04-26 19:30:35 +00:00
// Package difflib is a partial port of Python difflib module.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// It provides tools to compare sequences of strings and generate textual diffs.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// The following class and functions have been ported:
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// - SequenceMatcher
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// - unified_diff
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// - context_diff
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// Getting unified diffs was the main goal of the port. Keep in mind this code
2024-04-26 19:30:35 +00:00
// is mostly suitable to output text differences in a human friendly way, there
2024-04-26 19:30:35 +00:00
// are no guarantees generated diffs are consumable by patch(1).
2024-04-26 19:30:35 +00:00
package difflib
import (
"bufio"
"bytes"
"fmt"
"io"
"strings"
)
func min(a, b int) int {
2024-04-26 19:30:35 +00:00
if a < b {
2024-04-26 19:30:35 +00:00
return a
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
return b
2024-04-26 19:30:35 +00:00
}
func max(a, b int) int {
2024-04-26 19:30:35 +00:00
if a > b {
2024-04-26 19:30:35 +00:00
return a
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
return b
2024-04-26 19:30:35 +00:00
}
func calculateRatio(matches, length int) float64 {
2024-04-26 19:30:35 +00:00
if length > 0 {
2024-04-26 19:30:35 +00:00
return 2.0 * float64(matches) / float64(length)
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
return 1.0
2024-04-26 19:30:35 +00:00
}
type Match struct {
A int
B int
2024-04-26 19:30:35 +00:00
Size int
}
type OpCode struct {
Tag byte
I1 int
I2 int
J1 int
J2 int
2024-04-26 19:30:35 +00:00
}
// SequenceMatcher compares sequence of strings. The basic
2024-04-26 19:30:35 +00:00
// algorithm predates, and is a little fancier than, an algorithm
2024-04-26 19:30:35 +00:00
// published in the late 1980's by Ratcliff and Obershelp under the
2024-04-26 19:30:35 +00:00
// hyperbolic name "gestalt pattern matching". The basic idea is to find
2024-04-26 19:30:35 +00:00
// the longest contiguous matching subsequence that contains no "junk"
2024-04-26 19:30:35 +00:00
// elements (R-O doesn't address junk). The same idea is then applied
2024-04-26 19:30:35 +00:00
// recursively to the pieces of the sequences to the left and to the right
2024-04-26 19:30:35 +00:00
// of the matching subsequence. This does not yield minimal edit
2024-04-26 19:30:35 +00:00
// sequences, but does tend to yield matches that "look right" to people.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// SequenceMatcher tries to compute a "human-friendly diff" between two
2024-04-26 19:30:35 +00:00
// sequences. Unlike e.g. UNIX(tm) diff, the fundamental notion is the
2024-04-26 19:30:35 +00:00
// longest *contiguous* & junk-free matching subsequence. That's what
2024-04-26 19:30:35 +00:00
// catches peoples' eyes. The Windows(tm) windiff has another interesting
2024-04-26 19:30:35 +00:00
// notion, pairing up elements that appear uniquely in each sequence.
2024-04-26 19:30:35 +00:00
// That, and the method here, appear to yield more intuitive difference
2024-04-26 19:30:35 +00:00
// reports than does diff. This method appears to be the least vulnerable
2024-04-26 19:30:35 +00:00
// to synching up on blocks of "junk lines", though (like blank lines in
2024-04-26 19:30:35 +00:00
// ordinary text files, or maybe "<P>" lines in HTML files). That may be
2024-04-26 19:30:35 +00:00
// because this is the only method of the 3 that has a *concept* of
2024-04-26 19:30:35 +00:00
// "junk" <wink>.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// Timing: Basic R-O is cubic time worst case and quadratic time expected
2024-04-26 19:30:35 +00:00
// case. SequenceMatcher is quadratic time for the worst case and has
2024-04-26 19:30:35 +00:00
// expected-case behavior dependent in a complicated way on how many
2024-04-26 19:30:35 +00:00
// elements the sequences have in common; best case time is linear.
2024-04-26 19:30:35 +00:00
type SequenceMatcher struct {
a []string
b []string
b2j map[string][]int
IsJunk func(string) bool
autoJunk bool
bJunk map[string]struct{}
2024-04-26 19:30:35 +00:00
matchingBlocks []Match
fullBCount map[string]int
bPopular map[string]struct{}
opCodes []OpCode
2024-04-26 19:30:35 +00:00
}
func NewMatcher(a, b []string) *SequenceMatcher {
2024-04-26 19:30:35 +00:00
m := SequenceMatcher{autoJunk: true}
2024-04-26 19:30:35 +00:00
m.SetSeqs(a, b)
2024-04-26 19:30:35 +00:00
return &m
2024-04-26 19:30:35 +00:00
}
func NewMatcherWithJunk(a, b []string, autoJunk bool,
2024-04-26 19:30:35 +00:00
isJunk func(string) bool) *SequenceMatcher {
m := SequenceMatcher{IsJunk: isJunk, autoJunk: autoJunk}
2024-04-26 19:30:35 +00:00
m.SetSeqs(a, b)
2024-04-26 19:30:35 +00:00
return &m
2024-04-26 19:30:35 +00:00
}
// Set two sequences to be compared.
2024-04-26 19:30:35 +00:00
func (m *SequenceMatcher) SetSeqs(a, b []string) {
2024-04-26 19:30:35 +00:00
m.SetSeq1(a)
2024-04-26 19:30:35 +00:00
m.SetSeq2(b)
2024-04-26 19:30:35 +00:00
}
// Set the first sequence to be compared. The second sequence to be compared is
2024-04-26 19:30:35 +00:00
// not changed.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// SequenceMatcher computes and caches detailed information about the second
2024-04-26 19:30:35 +00:00
// sequence, so if you want to compare one sequence S against many sequences,
2024-04-26 19:30:35 +00:00
// use .SetSeq2(s) once and call .SetSeq1(x) repeatedly for each of the other
2024-04-26 19:30:35 +00:00
// sequences.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// See also SetSeqs() and SetSeq2().
2024-04-26 19:30:35 +00:00
func (m *SequenceMatcher) SetSeq1(a []string) {
2024-04-26 19:30:35 +00:00
if &a == &m.a {
2024-04-26 19:30:35 +00:00
return
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
m.a = a
2024-04-26 19:30:35 +00:00
m.matchingBlocks = nil
2024-04-26 19:30:35 +00:00
m.opCodes = nil
2024-04-26 19:30:35 +00:00
}
// Set the second sequence to be compared. The first sequence to be compared is
2024-04-26 19:30:35 +00:00
// not changed.
2024-04-26 19:30:35 +00:00
func (m *SequenceMatcher) SetSeq2(b []string) {
2024-04-26 19:30:35 +00:00
if &b == &m.b {
2024-04-26 19:30:35 +00:00
return
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
m.b = b
2024-04-26 19:30:35 +00:00
m.matchingBlocks = nil
2024-04-26 19:30:35 +00:00
m.opCodes = nil
2024-04-26 19:30:35 +00:00
m.fullBCount = nil
2024-04-26 19:30:35 +00:00
m.chainB()
2024-04-26 19:30:35 +00:00
}
func (m *SequenceMatcher) chainB() {
2024-04-26 19:30:35 +00:00
// Populate line -> index mapping
2024-04-26 19:30:35 +00:00
b2j := map[string][]int{}
2024-04-26 19:30:35 +00:00
for i, s := range m.b {
2024-04-26 19:30:35 +00:00
indices := b2j[s]
2024-04-26 19:30:35 +00:00
indices = append(indices, i)
2024-04-26 19:30:35 +00:00
b2j[s] = indices
2024-04-26 19:30:35 +00:00
}
// Purge junk elements
2024-04-26 19:30:35 +00:00
m.bJunk = map[string]struct{}{}
2024-04-26 19:30:35 +00:00
if m.IsJunk != nil {
2024-04-26 19:30:35 +00:00
junk := m.bJunk
2024-04-26 19:30:35 +00:00
for s, _ := range b2j {
2024-04-26 19:30:35 +00:00
if m.IsJunk(s) {
2024-04-26 19:30:35 +00:00
junk[s] = struct{}{}
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
for s, _ := range junk {
2024-04-26 19:30:35 +00:00
delete(b2j, s)
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
// Purge remaining popular elements
2024-04-26 19:30:35 +00:00
popular := map[string]struct{}{}
2024-04-26 19:30:35 +00:00
n := len(m.b)
2024-04-26 19:30:35 +00:00
if m.autoJunk && n >= 200 {
2024-04-26 19:30:35 +00:00
ntest := n/100 + 1
2024-04-26 19:30:35 +00:00
for s, indices := range b2j {
2024-04-26 19:30:35 +00:00
if len(indices) > ntest {
2024-04-26 19:30:35 +00:00
popular[s] = struct{}{}
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
for s, _ := range popular {
2024-04-26 19:30:35 +00:00
delete(b2j, s)
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
m.bPopular = popular
2024-04-26 19:30:35 +00:00
m.b2j = b2j
2024-04-26 19:30:35 +00:00
}
func (m *SequenceMatcher) isBJunk(s string) bool {
2024-04-26 19:30:35 +00:00
_, ok := m.bJunk[s]
2024-04-26 19:30:35 +00:00
return ok
2024-04-26 19:30:35 +00:00
}
// Find longest matching block in a[alo:ahi] and b[blo:bhi].
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// If IsJunk is not defined:
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// Return (i,j,k) such that a[i:i+k] is equal to b[j:j+k], where
2024-06-14 08:41:36 +00:00
//
2024-06-14 08:41:36 +00:00
// alo <= i <= i+k <= ahi
2024-06-14 08:41:36 +00:00
// blo <= j <= j+k <= bhi
2024-06-14 08:41:36 +00:00
//
2024-04-26 19:30:35 +00:00
// and for all (i',j',k') meeting those conditions,
2024-06-14 08:41:36 +00:00
//
2024-06-14 08:41:36 +00:00
// k >= k'
2024-06-14 08:41:36 +00:00
// i <= i'
2024-06-14 08:41:36 +00:00
// and if i == i', j <= j'
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// In other words, of all maximal matching blocks, return one that
2024-04-26 19:30:35 +00:00
// starts earliest in a, and of all those maximal matching blocks that
2024-04-26 19:30:35 +00:00
// start earliest in a, return the one that starts earliest in b.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// If IsJunk is defined, first the longest matching block is
2024-04-26 19:30:35 +00:00
// determined as above, but with the additional restriction that no
2024-04-26 19:30:35 +00:00
// junk element appears in the block. Then that block is extended as
2024-04-26 19:30:35 +00:00
// far as possible by matching (only) junk elements on both sides. So
2024-04-26 19:30:35 +00:00
// the resulting block never matches on junk except as identical junk
2024-04-26 19:30:35 +00:00
// happens to be adjacent to an "interesting" match.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// If no blocks match, return (alo, blo, 0).
2024-04-26 19:30:35 +00:00
func (m *SequenceMatcher) findLongestMatch(alo, ahi, blo, bhi int) Match {
2024-04-26 19:30:35 +00:00
// CAUTION: stripping common prefix or suffix would be incorrect.
2024-04-26 19:30:35 +00:00
// E.g.,
2024-04-26 19:30:35 +00:00
// ab
2024-04-26 19:30:35 +00:00
// acab
2024-04-26 19:30:35 +00:00
// Longest matching block is "ab", but if common prefix is
2024-04-26 19:30:35 +00:00
// stripped, it's "a" (tied with "b"). UNIX(tm) diff does so
2024-04-26 19:30:35 +00:00
// strip, so ends up claiming that ab is changed to acab by
2024-04-26 19:30:35 +00:00
// inserting "ca" in the middle. That's minimal but unintuitive:
2024-04-26 19:30:35 +00:00
// "it's obvious" that someone inserted "ac" at the front.
2024-04-26 19:30:35 +00:00
// Windiff ends up at the same place as diff, but by pairing up
2024-04-26 19:30:35 +00:00
// the unique 'b's and then matching the first two 'a's.
2024-04-26 19:30:35 +00:00
besti, bestj, bestsize := alo, blo, 0
// find longest junk-free match
2024-04-26 19:30:35 +00:00
// during an iteration of the loop, j2len[j] = length of longest
2024-04-26 19:30:35 +00:00
// junk-free match ending with a[i-1] and b[j]
2024-04-26 19:30:35 +00:00
j2len := map[int]int{}
2024-04-26 19:30:35 +00:00
for i := alo; i != ahi; i++ {
2024-04-26 19:30:35 +00:00
// look at all instances of a[i] in b; note that because
2024-04-26 19:30:35 +00:00
// b2j has no junk keys, the loop is skipped if a[i] is junk
2024-04-26 19:30:35 +00:00
newj2len := map[int]int{}
2024-04-26 19:30:35 +00:00
for _, j := range m.b2j[m.a[i]] {
2024-04-26 19:30:35 +00:00
// a[i] matches b[j]
2024-04-26 19:30:35 +00:00
if j < blo {
2024-04-26 19:30:35 +00:00
continue
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
if j >= bhi {
2024-04-26 19:30:35 +00:00
break
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
k := j2len[j-1] + 1
2024-04-26 19:30:35 +00:00
newj2len[j] = k
2024-04-26 19:30:35 +00:00
if k > bestsize {
2024-04-26 19:30:35 +00:00
besti, bestj, bestsize = i-k+1, j-k+1, k
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
j2len = newj2len
2024-04-26 19:30:35 +00:00
}
// Extend the best by non-junk elements on each end. In particular,
2024-04-26 19:30:35 +00:00
// "popular" non-junk elements aren't in b2j, which greatly speeds
2024-04-26 19:30:35 +00:00
// the inner loop above, but also means "the best" match so far
2024-04-26 19:30:35 +00:00
// doesn't contain any junk *or* popular non-junk elements.
2024-04-26 19:30:35 +00:00
for besti > alo && bestj > blo && !m.isBJunk(m.b[bestj-1]) &&
2024-04-26 19:30:35 +00:00
m.a[besti-1] == m.b[bestj-1] {
2024-04-26 19:30:35 +00:00
besti, bestj, bestsize = besti-1, bestj-1, bestsize+1
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
for besti+bestsize < ahi && bestj+bestsize < bhi &&
2024-04-26 19:30:35 +00:00
!m.isBJunk(m.b[bestj+bestsize]) &&
2024-04-26 19:30:35 +00:00
m.a[besti+bestsize] == m.b[bestj+bestsize] {
2024-04-26 19:30:35 +00:00
bestsize += 1
2024-04-26 19:30:35 +00:00
}
// Now that we have a wholly interesting match (albeit possibly
2024-04-26 19:30:35 +00:00
// empty!), we may as well suck up the matching junk on each
2024-04-26 19:30:35 +00:00
// side of it too. Can't think of a good reason not to, and it
2024-04-26 19:30:35 +00:00
// saves post-processing the (possibly considerable) expense of
2024-04-26 19:30:35 +00:00
// figuring out what to do with it. In the case of an empty
2024-04-26 19:30:35 +00:00
// interesting match, this is clearly the right thing to do,
2024-04-26 19:30:35 +00:00
// because no other kind of match is possible in the regions.
2024-04-26 19:30:35 +00:00
for besti > alo && bestj > blo && m.isBJunk(m.b[bestj-1]) &&
2024-04-26 19:30:35 +00:00
m.a[besti-1] == m.b[bestj-1] {
2024-04-26 19:30:35 +00:00
besti, bestj, bestsize = besti-1, bestj-1, bestsize+1
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
for besti+bestsize < ahi && bestj+bestsize < bhi &&
2024-04-26 19:30:35 +00:00
m.isBJunk(m.b[bestj+bestsize]) &&
2024-04-26 19:30:35 +00:00
m.a[besti+bestsize] == m.b[bestj+bestsize] {
2024-04-26 19:30:35 +00:00
bestsize += 1
2024-04-26 19:30:35 +00:00
}
return Match{A: besti, B: bestj, Size: bestsize}
2024-04-26 19:30:35 +00:00
}
// Return list of triples describing matching subsequences.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// Each triple is of the form (i, j, n), and means that
2024-04-26 19:30:35 +00:00
// a[i:i+n] == b[j:j+n]. The triples are monotonically increasing in
2024-04-26 19:30:35 +00:00
// i and in j. It's also guaranteed that if (i, j, n) and (i', j', n') are
2024-04-26 19:30:35 +00:00
// adjacent triples in the list, and the second is not the last triple in the
2024-04-26 19:30:35 +00:00
// list, then i+n != i' or j+n != j'. IOW, adjacent triples never describe
2024-04-26 19:30:35 +00:00
// adjacent equal blocks.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// The last triple is a dummy, (len(a), len(b), 0), and is the only
2024-04-26 19:30:35 +00:00
// triple with n==0.
2024-04-26 19:30:35 +00:00
func (m *SequenceMatcher) GetMatchingBlocks() []Match {
2024-04-26 19:30:35 +00:00
if m.matchingBlocks != nil {
2024-04-26 19:30:35 +00:00
return m.matchingBlocks
2024-04-26 19:30:35 +00:00
}
var matchBlocks func(alo, ahi, blo, bhi int, matched []Match) []Match
2024-04-26 19:30:35 +00:00
matchBlocks = func(alo, ahi, blo, bhi int, matched []Match) []Match {
2024-04-26 19:30:35 +00:00
match := m.findLongestMatch(alo, ahi, blo, bhi)
2024-04-26 19:30:35 +00:00
i, j, k := match.A, match.B, match.Size
2024-04-26 19:30:35 +00:00
if match.Size > 0 {
2024-04-26 19:30:35 +00:00
if alo < i && blo < j {
2024-04-26 19:30:35 +00:00
matched = matchBlocks(alo, i, blo, j, matched)
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
matched = append(matched, match)
2024-04-26 19:30:35 +00:00
if i+k < ahi && j+k < bhi {
2024-04-26 19:30:35 +00:00
matched = matchBlocks(i+k, ahi, j+k, bhi, matched)
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
return matched
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
matched := matchBlocks(0, len(m.a), 0, len(m.b), nil)
// It's possible that we have adjacent equal blocks in the
2024-04-26 19:30:35 +00:00
// matching_blocks list now.
2024-04-26 19:30:35 +00:00
nonAdjacent := []Match{}
2024-04-26 19:30:35 +00:00
i1, j1, k1 := 0, 0, 0
2024-04-26 19:30:35 +00:00
for _, b := range matched {
2024-04-26 19:30:35 +00:00
// Is this block adjacent to i1, j1, k1?
2024-04-26 19:30:35 +00:00
i2, j2, k2 := b.A, b.B, b.Size
2024-04-26 19:30:35 +00:00
if i1+k1 == i2 && j1+k1 == j2 {
2024-04-26 19:30:35 +00:00
// Yes, so collapse them -- this just increases the length of
2024-04-26 19:30:35 +00:00
// the first block by the length of the second, and the first
2024-04-26 19:30:35 +00:00
// block so lengthened remains the block to compare against.
2024-04-26 19:30:35 +00:00
k1 += k2
2024-04-26 19:30:35 +00:00
} else {
2024-04-26 19:30:35 +00:00
// Not adjacent. Remember the first block (k1==0 means it's
2024-04-26 19:30:35 +00:00
// the dummy we started with), and make the second block the
2024-04-26 19:30:35 +00:00
// new block to compare against.
2024-04-26 19:30:35 +00:00
if k1 > 0 {
2024-04-26 19:30:35 +00:00
nonAdjacent = append(nonAdjacent, Match{i1, j1, k1})
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
i1, j1, k1 = i2, j2, k2
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
if k1 > 0 {
2024-04-26 19:30:35 +00:00
nonAdjacent = append(nonAdjacent, Match{i1, j1, k1})
2024-04-26 19:30:35 +00:00
}
nonAdjacent = append(nonAdjacent, Match{len(m.a), len(m.b), 0})
2024-04-26 19:30:35 +00:00
m.matchingBlocks = nonAdjacent
2024-04-26 19:30:35 +00:00
return m.matchingBlocks
2024-04-26 19:30:35 +00:00
}
// Return list of 5-tuples describing how to turn a into b.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// Each tuple is of the form (tag, i1, i2, j1, j2). The first tuple
2024-04-26 19:30:35 +00:00
// has i1 == j1 == 0, and remaining tuples have i1 == the i2 from the
2024-04-26 19:30:35 +00:00
// tuple preceding it, and likewise for j1 == the previous j2.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// The tags are characters, with these meanings:
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// 'r' (replace): a[i1:i2] should be replaced by b[j1:j2]
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// 'd' (delete): a[i1:i2] should be deleted, j1==j2 in this case.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// 'i' (insert): b[j1:j2] should be inserted at a[i1:i1], i1==i2 in this case.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// 'e' (equal): a[i1:i2] == b[j1:j2]
2024-04-26 19:30:35 +00:00
func (m *SequenceMatcher) GetOpCodes() []OpCode {
2024-04-26 19:30:35 +00:00
if m.opCodes != nil {
2024-04-26 19:30:35 +00:00
return m.opCodes
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
i, j := 0, 0
2024-04-26 19:30:35 +00:00
matching := m.GetMatchingBlocks()
2024-04-26 19:30:35 +00:00
opCodes := make([]OpCode, 0, len(matching))
2024-04-26 19:30:35 +00:00
for _, m := range matching {
2024-04-26 19:30:35 +00:00
// invariant: we've pumped out correct diffs to change
2024-04-26 19:30:35 +00:00
// a[:i] into b[:j], and the next matching block is
2024-04-26 19:30:35 +00:00
// a[ai:ai+size] == b[bj:bj+size]. So we need to pump
2024-04-26 19:30:35 +00:00
// out a diff to change a[i:ai] into b[j:bj], pump out
2024-04-26 19:30:35 +00:00
// the matching block, and move (i,j) beyond the match
2024-04-26 19:30:35 +00:00
ai, bj, size := m.A, m.B, m.Size
2024-04-26 19:30:35 +00:00
tag := byte(0)
2024-04-26 19:30:35 +00:00
if i < ai && j < bj {
2024-04-26 19:30:35 +00:00
tag = 'r'
2024-04-26 19:30:35 +00:00
} else if i < ai {
2024-04-26 19:30:35 +00:00
tag = 'd'
2024-04-26 19:30:35 +00:00
} else if j < bj {
2024-04-26 19:30:35 +00:00
tag = 'i'
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
if tag > 0 {
2024-04-26 19:30:35 +00:00
opCodes = append(opCodes, OpCode{tag, i, ai, j, bj})
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
i, j = ai+size, bj+size
2024-04-26 19:30:35 +00:00
// the list of matching blocks is terminated by a
2024-04-26 19:30:35 +00:00
// sentinel with size 0
2024-04-26 19:30:35 +00:00
if size > 0 {
2024-04-26 19:30:35 +00:00
opCodes = append(opCodes, OpCode{'e', ai, i, bj, j})
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
m.opCodes = opCodes
2024-04-26 19:30:35 +00:00
return m.opCodes
2024-04-26 19:30:35 +00:00
}
// Isolate change clusters by eliminating ranges with no changes.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// Return a generator of groups with up to n lines of context.
2024-04-26 19:30:35 +00:00
// Each group is in the same format as returned by GetOpCodes().
2024-04-26 19:30:35 +00:00
func (m *SequenceMatcher) GetGroupedOpCodes(n int) [][]OpCode {
2024-04-26 19:30:35 +00:00
if n < 0 {
2024-04-26 19:30:35 +00:00
n = 3
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
codes := m.GetOpCodes()
2024-04-26 19:30:35 +00:00
if len(codes) == 0 {
2024-04-26 19:30:35 +00:00
codes = []OpCode{OpCode{'e', 0, 1, 0, 1}}
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
// Fixup leading and trailing groups if they show no changes.
2024-04-26 19:30:35 +00:00
if codes[0].Tag == 'e' {
2024-04-26 19:30:35 +00:00
c := codes[0]
2024-04-26 19:30:35 +00:00
i1, i2, j1, j2 := c.I1, c.I2, c.J1, c.J2
2024-04-26 19:30:35 +00:00
codes[0] = OpCode{c.Tag, max(i1, i2-n), i2, max(j1, j2-n), j2}
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
if codes[len(codes)-1].Tag == 'e' {
2024-04-26 19:30:35 +00:00
c := codes[len(codes)-1]
2024-04-26 19:30:35 +00:00
i1, i2, j1, j2 := c.I1, c.I2, c.J1, c.J2
2024-04-26 19:30:35 +00:00
codes[len(codes)-1] = OpCode{c.Tag, i1, min(i2, i1+n), j1, min(j2, j1+n)}
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
nn := n + n
2024-04-26 19:30:35 +00:00
groups := [][]OpCode{}
2024-04-26 19:30:35 +00:00
group := []OpCode{}
2024-04-26 19:30:35 +00:00
for _, c := range codes {
2024-04-26 19:30:35 +00:00
i1, i2, j1, j2 := c.I1, c.I2, c.J1, c.J2
2024-04-26 19:30:35 +00:00
// End the current group and start a new one whenever
2024-04-26 19:30:35 +00:00
// there is a large range with no changes.
2024-04-26 19:30:35 +00:00
if c.Tag == 'e' && i2-i1 > nn {
2024-04-26 19:30:35 +00:00
group = append(group, OpCode{c.Tag, i1, min(i2, i1+n),
2024-04-26 19:30:35 +00:00
j1, min(j2, j1+n)})
2024-04-26 19:30:35 +00:00
groups = append(groups, group)
2024-04-26 19:30:35 +00:00
group = []OpCode{}
2024-04-26 19:30:35 +00:00
i1, j1 = max(i1, i2-n), max(j1, j2-n)
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
group = append(group, OpCode{c.Tag, i1, i2, j1, j2})
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
if len(group) > 0 && !(len(group) == 1 && group[0].Tag == 'e') {
2024-04-26 19:30:35 +00:00
groups = append(groups, group)
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
return groups
2024-04-26 19:30:35 +00:00
}
// Return a measure of the sequences' similarity (float in [0,1]).
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// Where T is the total number of elements in both sequences, and
2024-04-26 19:30:35 +00:00
// M is the number of matches, this is 2.0*M / T.
2024-04-26 19:30:35 +00:00
// Note that this is 1 if the sequences are identical, and 0 if
2024-04-26 19:30:35 +00:00
// they have nothing in common.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// .Ratio() is expensive to compute if you haven't already computed
2024-04-26 19:30:35 +00:00
// .GetMatchingBlocks() or .GetOpCodes(), in which case you may
2024-04-26 19:30:35 +00:00
// want to try .QuickRatio() or .RealQuickRation() first to get an
2024-04-26 19:30:35 +00:00
// upper bound.
2024-04-26 19:30:35 +00:00
func (m *SequenceMatcher) Ratio() float64 {
2024-04-26 19:30:35 +00:00
matches := 0
2024-04-26 19:30:35 +00:00
for _, m := range m.GetMatchingBlocks() {
2024-04-26 19:30:35 +00:00
matches += m.Size
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
return calculateRatio(matches, len(m.a)+len(m.b))
2024-04-26 19:30:35 +00:00
}
// Return an upper bound on ratio() relatively quickly.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// This isn't defined beyond that it is an upper bound on .Ratio(), and
2024-04-26 19:30:35 +00:00
// is faster to compute.
2024-04-26 19:30:35 +00:00
func (m *SequenceMatcher) QuickRatio() float64 {
2024-04-26 19:30:35 +00:00
// viewing a and b as multisets, set matches to the cardinality
2024-04-26 19:30:35 +00:00
// of their intersection; this counts the number of matches
2024-04-26 19:30:35 +00:00
// without regard to order, so is clearly an upper bound
2024-04-26 19:30:35 +00:00
if m.fullBCount == nil {
2024-04-26 19:30:35 +00:00
m.fullBCount = map[string]int{}
2024-04-26 19:30:35 +00:00
for _, s := range m.b {
2024-04-26 19:30:35 +00:00
m.fullBCount[s] = m.fullBCount[s] + 1
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
// avail[x] is the number of times x appears in 'b' less the
2024-04-26 19:30:35 +00:00
// number of times we've seen it in 'a' so far ... kinda
2024-04-26 19:30:35 +00:00
avail := map[string]int{}
2024-04-26 19:30:35 +00:00
matches := 0
2024-04-26 19:30:35 +00:00
for _, s := range m.a {
2024-04-26 19:30:35 +00:00
n, ok := avail[s]
2024-04-26 19:30:35 +00:00
if !ok {
2024-04-26 19:30:35 +00:00
n = m.fullBCount[s]
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
avail[s] = n - 1
2024-04-26 19:30:35 +00:00
if n > 0 {
2024-04-26 19:30:35 +00:00
matches += 1
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
return calculateRatio(matches, len(m.a)+len(m.b))
2024-04-26 19:30:35 +00:00
}
// Return an upper bound on ratio() very quickly.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// This isn't defined beyond that it is an upper bound on .Ratio(), and
2024-04-26 19:30:35 +00:00
// is faster to compute than either .Ratio() or .QuickRatio().
2024-04-26 19:30:35 +00:00
func (m *SequenceMatcher) RealQuickRatio() float64 {
2024-04-26 19:30:35 +00:00
la, lb := len(m.a), len(m.b)
2024-04-26 19:30:35 +00:00
return calculateRatio(min(la, lb), la+lb)
2024-04-26 19:30:35 +00:00
}
// Convert range to the "ed" format
2024-04-26 19:30:35 +00:00
func formatRangeUnified(start, stop int) string {
2024-04-26 19:30:35 +00:00
// Per the diff spec at http://www.unix.org/single_unix_specification/
2024-04-26 19:30:35 +00:00
beginning := start + 1 // lines start numbering with one
2024-04-26 19:30:35 +00:00
length := stop - start
2024-04-26 19:30:35 +00:00
if length == 1 {
2024-04-26 19:30:35 +00:00
return fmt.Sprintf("%d", beginning)
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
if length == 0 {
2024-04-26 19:30:35 +00:00
beginning -= 1 // empty ranges begin at line just before the range
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
return fmt.Sprintf("%d,%d", beginning, length)
2024-04-26 19:30:35 +00:00
}
// Unified diff parameters
2024-04-26 19:30:35 +00:00
type UnifiedDiff struct {
A []string // First sequence lines
FromFile string // First file name
FromDate string // First file time
B []string // Second sequence lines
ToFile string // Second file name
ToDate string // Second file time
Eol string // Headers end of line, defaults to LF
Context int // Number of context lines
2024-04-26 19:30:35 +00:00
}
// Compare two sequences of lines; generate the delta as a unified diff.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// Unified diffs are a compact way of showing line changes and a few
2024-04-26 19:30:35 +00:00
// lines of context. The number of context lines is set by 'n' which
2024-04-26 19:30:35 +00:00
// defaults to three.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// By default, the diff control lines (those with ---, +++, or @@) are
2024-04-26 19:30:35 +00:00
// created with a trailing newline. This is helpful so that inputs
2024-04-26 19:30:35 +00:00
// created from file.readlines() result in diffs that are suitable for
2024-04-26 19:30:35 +00:00
// file.writelines() since both the inputs and outputs have trailing
2024-04-26 19:30:35 +00:00
// newlines.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// For inputs that do not have trailing newlines, set the lineterm
2024-04-26 19:30:35 +00:00
// argument to "" so that the output will be uniformly newline free.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// The unidiff format normally has a header for filenames and modification
2024-04-26 19:30:35 +00:00
// times. Any or all of these may be specified using strings for
2024-04-26 19:30:35 +00:00
// 'fromfile', 'tofile', 'fromfiledate', and 'tofiledate'.
2024-04-26 19:30:35 +00:00
// The modification times are normally expressed in the ISO 8601 format.
2024-04-26 19:30:35 +00:00
func WriteUnifiedDiff(writer io.Writer, diff UnifiedDiff) error {
2024-04-26 19:30:35 +00:00
buf := bufio.NewWriter(writer)
2024-04-26 19:30:35 +00:00
defer buf.Flush()
2024-04-26 19:30:35 +00:00
wf := func(format string, args ...interface{}) error {
2024-04-26 19:30:35 +00:00
_, err := buf.WriteString(fmt.Sprintf(format, args...))
2024-04-26 19:30:35 +00:00
return err
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
ws := func(s string) error {
2024-04-26 19:30:35 +00:00
_, err := buf.WriteString(s)
2024-04-26 19:30:35 +00:00
return err
2024-04-26 19:30:35 +00:00
}
if len(diff.Eol) == 0 {
2024-04-26 19:30:35 +00:00
diff.Eol = "\n"
2024-04-26 19:30:35 +00:00
}
started := false
2024-04-26 19:30:35 +00:00
m := NewMatcher(diff.A, diff.B)
2024-04-26 19:30:35 +00:00
for _, g := range m.GetGroupedOpCodes(diff.Context) {
2024-04-26 19:30:35 +00:00
if !started {
2024-04-26 19:30:35 +00:00
started = true
2024-04-26 19:30:35 +00:00
fromDate := ""
2024-04-26 19:30:35 +00:00
if len(diff.FromDate) > 0 {
2024-04-26 19:30:35 +00:00
fromDate = "\t" + diff.FromDate
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
toDate := ""
2024-04-26 19:30:35 +00:00
if len(diff.ToDate) > 0 {
2024-04-26 19:30:35 +00:00
toDate = "\t" + diff.ToDate
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
if diff.FromFile != "" || diff.ToFile != "" {
2024-04-26 19:30:35 +00:00
err := wf("--- %s%s%s", diff.FromFile, fromDate, diff.Eol)
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return err
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
err = wf("+++ %s%s%s", diff.ToFile, toDate, diff.Eol)
2024-04-26 19:30:35 +00:00
if err != nil {
2024-04-26 19:30:35 +00:00
return err
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
first, last := g[0], g[len(g)-1]
2024-04-26 19:30:35 +00:00
range1 := formatRangeUnified(first.I1, last.I2)
2024-04-26 19:30:35 +00:00
range2 := formatRangeUnified(first.J1, last.J2)
2024-04-26 19:30:35 +00:00
if err := wf("@@ -%s +%s @@%s", range1, range2, diff.Eol); err != nil {
2024-04-26 19:30:35 +00:00
return err
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
for _, c := range g {
2024-04-26 19:30:35 +00:00
i1, i2, j1, j2 := c.I1, c.I2, c.J1, c.J2
2024-04-26 19:30:35 +00:00
if c.Tag == 'e' {
2024-04-26 19:30:35 +00:00
for _, line := range diff.A[i1:i2] {
2024-04-26 19:30:35 +00:00
if err := ws(" " + line); err != nil {
2024-04-26 19:30:35 +00:00
return err
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
continue
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
if c.Tag == 'r' || c.Tag == 'd' {
2024-04-26 19:30:35 +00:00
for _, line := range diff.A[i1:i2] {
2024-04-26 19:30:35 +00:00
if err := ws("-" + line); err != nil {
2024-04-26 19:30:35 +00:00
return err
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
if c.Tag == 'r' || c.Tag == 'i' {
2024-04-26 19:30:35 +00:00
for _, line := range diff.B[j1:j2] {
2024-04-26 19:30:35 +00:00
if err := ws("+" + line); err != nil {
2024-04-26 19:30:35 +00:00
return err
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
return nil
2024-04-26 19:30:35 +00:00
}
// Like WriteUnifiedDiff but returns the diff a string.
2024-04-26 19:30:35 +00:00
func GetUnifiedDiffString(diff UnifiedDiff) (string, error) {
2024-04-26 19:30:35 +00:00
w := &bytes.Buffer{}
2024-04-26 19:30:35 +00:00
err := WriteUnifiedDiff(w, diff)
2024-04-26 19:30:35 +00:00
return string(w.Bytes()), err
2024-04-26 19:30:35 +00:00
}
// Convert range to the "ed" format.
2024-04-26 19:30:35 +00:00
func formatRangeContext(start, stop int) string {
2024-04-26 19:30:35 +00:00
// Per the diff spec at http://www.unix.org/single_unix_specification/
2024-04-26 19:30:35 +00:00
beginning := start + 1 // lines start numbering with one
2024-04-26 19:30:35 +00:00
length := stop - start
2024-04-26 19:30:35 +00:00
if length == 0 {
2024-04-26 19:30:35 +00:00
beginning -= 1 // empty ranges begin at line just before the range
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
if length <= 1 {
2024-04-26 19:30:35 +00:00
return fmt.Sprintf("%d", beginning)
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
return fmt.Sprintf("%d,%d", beginning, beginning+length-1)
2024-04-26 19:30:35 +00:00
}
type ContextDiff UnifiedDiff
// Compare two sequences of lines; generate the delta as a context diff.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// Context diffs are a compact way of showing line changes and a few
2024-04-26 19:30:35 +00:00
// lines of context. The number of context lines is set by diff.Context
2024-04-26 19:30:35 +00:00
// which defaults to three.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// By default, the diff control lines (those with *** or ---) are
2024-04-26 19:30:35 +00:00
// created with a trailing newline.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// For inputs that do not have trailing newlines, set the diff.Eol
2024-04-26 19:30:35 +00:00
// argument to "" so that the output will be uniformly newline free.
2024-04-26 19:30:35 +00:00
//
2024-04-26 19:30:35 +00:00
// The context diff format normally has a header for filenames and
2024-04-26 19:30:35 +00:00
// modification times. Any or all of these may be specified using
2024-04-26 19:30:35 +00:00
// strings for diff.FromFile, diff.ToFile, diff.FromDate, diff.ToDate.
2024-04-26 19:30:35 +00:00
// The modification times are normally expressed in the ISO 8601 format.
2024-04-26 19:30:35 +00:00
// If not specified, the strings default to blanks.
2024-04-26 19:30:35 +00:00
func WriteContextDiff(writer io.Writer, diff ContextDiff) error {
2024-04-26 19:30:35 +00:00
buf := bufio.NewWriter(writer)
2024-04-26 19:30:35 +00:00
defer buf.Flush()
2024-04-26 19:30:35 +00:00
var diffErr error
2024-04-26 19:30:35 +00:00
wf := func(format string, args ...interface{}) {
2024-04-26 19:30:35 +00:00
_, err := buf.WriteString(fmt.Sprintf(format, args...))
2024-04-26 19:30:35 +00:00
if diffErr == nil && err != nil {
2024-04-26 19:30:35 +00:00
diffErr = err
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
ws := func(s string) {
2024-04-26 19:30:35 +00:00
_, err := buf.WriteString(s)
2024-04-26 19:30:35 +00:00
if diffErr == nil && err != nil {
2024-04-26 19:30:35 +00:00
diffErr = err
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
if len(diff.Eol) == 0 {
2024-04-26 19:30:35 +00:00
diff.Eol = "\n"
2024-04-26 19:30:35 +00:00
}
prefix := map[byte]string{
2024-04-26 19:30:35 +00:00
'i': "+ ",
2024-04-26 19:30:35 +00:00
'd': "- ",
2024-04-26 19:30:35 +00:00
'r': "! ",
2024-04-26 19:30:35 +00:00
'e': " ",
}
started := false
2024-04-26 19:30:35 +00:00
m := NewMatcher(diff.A, diff.B)
2024-04-26 19:30:35 +00:00
for _, g := range m.GetGroupedOpCodes(diff.Context) {
2024-04-26 19:30:35 +00:00
if !started {
2024-04-26 19:30:35 +00:00
started = true
2024-04-26 19:30:35 +00:00
fromDate := ""
2024-04-26 19:30:35 +00:00
if len(diff.FromDate) > 0 {
2024-04-26 19:30:35 +00:00
fromDate = "\t" + diff.FromDate
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
toDate := ""
2024-04-26 19:30:35 +00:00
if len(diff.ToDate) > 0 {
2024-04-26 19:30:35 +00:00
toDate = "\t" + diff.ToDate
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
if diff.FromFile != "" || diff.ToFile != "" {
2024-04-26 19:30:35 +00:00
wf("*** %s%s%s", diff.FromFile, fromDate, diff.Eol)
2024-04-26 19:30:35 +00:00
wf("--- %s%s%s", diff.ToFile, toDate, diff.Eol)
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
first, last := g[0], g[len(g)-1]
2024-04-26 19:30:35 +00:00
ws("***************" + diff.Eol)
range1 := formatRangeContext(first.I1, last.I2)
2024-04-26 19:30:35 +00:00
wf("*** %s ****%s", range1, diff.Eol)
2024-04-26 19:30:35 +00:00
for _, c := range g {
2024-04-26 19:30:35 +00:00
if c.Tag == 'r' || c.Tag == 'd' {
2024-04-26 19:30:35 +00:00
for _, cc := range g {
2024-04-26 19:30:35 +00:00
if cc.Tag == 'i' {
2024-04-26 19:30:35 +00:00
continue
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
for _, line := range diff.A[cc.I1:cc.I2] {
2024-04-26 19:30:35 +00:00
ws(prefix[cc.Tag] + line)
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
break
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
range2 := formatRangeContext(first.J1, last.J2)
2024-04-26 19:30:35 +00:00
wf("--- %s ----%s", range2, diff.Eol)
2024-04-26 19:30:35 +00:00
for _, c := range g {
2024-04-26 19:30:35 +00:00
if c.Tag == 'r' || c.Tag == 'i' {
2024-04-26 19:30:35 +00:00
for _, cc := range g {
2024-04-26 19:30:35 +00:00
if cc.Tag == 'd' {
2024-04-26 19:30:35 +00:00
continue
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
for _, line := range diff.B[cc.J1:cc.J2] {
2024-04-26 19:30:35 +00:00
ws(prefix[cc.Tag] + line)
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
break
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
}
2024-04-26 19:30:35 +00:00
return diffErr
2024-04-26 19:30:35 +00:00
}
// Like WriteContextDiff but returns the diff a string.
2024-04-26 19:30:35 +00:00
func GetContextDiffString(diff ContextDiff) (string, error) {
2024-04-26 19:30:35 +00:00
w := &bytes.Buffer{}
2024-04-26 19:30:35 +00:00
err := WriteContextDiff(w, diff)
2024-04-26 19:30:35 +00:00
return string(w.Bytes()), err
2024-04-26 19:30:35 +00:00
}
// Split a string on "\n" while preserving them. The output can be used
2024-04-26 19:30:35 +00:00
// as input for UnifiedDiff and ContextDiff structures.
2024-04-26 19:30:35 +00:00
func SplitLines(s string) []string {
2024-04-26 19:30:35 +00:00
lines := strings.SplitAfter(s, "\n")
2024-04-26 19:30:35 +00:00
lines[len(lines)-1] += "\n"
2024-04-26 19:30:35 +00:00
return lines
2024-04-26 19:30:35 +00:00
}