1 // Copyright (C) The Lightning Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
12 "github.com/sergi/go-diff/diffmatchpatch"
19 Left string // base preceding an indel, if Ref or New is empty
22 func (v *Variant) String() string {
24 case len(v.New) == 0 && len(v.Ref) == 0:
25 return fmt.Sprintf("%d=", v.Position)
26 case len(v.New) == 1 && v.New == v.Ref:
27 return fmt.Sprintf("%d=", v.Position)
29 return fmt.Sprintf("%d_%d=", v.Position, v.Position+len(v.Ref)-1)
30 case len(v.New) == 0 && len(v.Ref) == 1:
31 return fmt.Sprintf("%ddel", v.Position)
33 return fmt.Sprintf("%d_%ddel", v.Position, v.Position+len(v.Ref)-1)
34 case len(v.Ref) == 1 && len(v.New) == 1:
35 return fmt.Sprintf("%d%s>%s", v.Position, v.Ref, v.New)
37 return fmt.Sprintf("%d_%dins%s", v.Position-1, v.Position, v.New)
38 case len(v.Ref) == 1 && len(v.New) > 0:
39 return fmt.Sprintf("%ddelins%s", v.Position, v.New)
41 return fmt.Sprintf("%d_%ddelins%s", v.Position, v.Position+len(v.Ref)-1, v.New)
45 // PadLeft returns a Variant that is equivalent to v but (if possible)
46 // uses the stashed preceding base (the Left field) to avoid having a
47 // non-empty Ref or New part, even for an insertion or deletion.
49 // For example, if v is {Position: 45, Ref: "", New: "A"}, PadLeft
50 // might return {Position: 44, Ref: "T", New: "TA"}.
51 func (v *Variant) PadLeft() Variant {
52 if len(v.Ref) == 0 || len(v.New) == 0 {
54 Position: v.Position - len(v.Left),
63 func Diff(a, b string, timeout time.Duration) ([]Variant, bool) {
64 dmp := diffmatchpatch.New()
65 var deadline time.Time
67 deadline = time.Now().Add(timeout)
69 diffs := dmp.DiffBisect(a, b, deadline)
71 if timeout > 0 && time.Now().After(deadline) {
74 diffs = cleanup(dmp.DiffCleanupEfficiency(diffs))
76 var variants []Variant
77 for i := 0; i < len(diffs); {
78 left := "" // last char before an insertion or deletion
79 for ; i < len(diffs) && diffs[i].Type == diffmatchpatch.DiffEqual; i++ {
80 pos += len(diffs[i].Text)
81 if tlen := len(diffs[i].Text); tlen > 0 {
82 left = diffs[i].Text[tlen-1:]
88 v := Variant{Position: pos, Left: left}
89 for ; i < len(diffs) && diffs[i].Type != diffmatchpatch.DiffEqual; i++ {
90 if diffs[i].Type == diffmatchpatch.DiffDelete {
91 v.Ref += diffs[i].Text
93 v.New += diffs[i].Text
96 if len(v.Ref) == 2 && len(v.New) == 2 {
105 variants = append(variants, v1)
108 variants = append(variants, v)
111 return variants, timedOut
114 func cleanup(in []diffmatchpatch.Diff) (out []diffmatchpatch.Diff) {
115 out = make([]diffmatchpatch.Diff, 0, len(in))
116 for i := 0; i < len(in); i++ {
118 // Merge consecutive entries of same type (e.g.,
119 // "insert A; insert B")
120 for i < len(in)-1 && in[i].Type == in[i+1].Type {
121 d.Text += in[i+1].Text
126 in, out = out, make([]diffmatchpatch.Diff, 0, len(in))
127 for i := 0; i < len(in); i++ {
129 // diffmatchpatch solves diff("AAX","XTX") with
130 // [delAA,=X,insTX] but we prefer to spell it
133 // So, when we see a [del,=,ins] sequence where the
134 // "=" part is a suffix of the "ins" part -- e.g.,
135 // [delAAA,=CGG,insTTTCGG] -- we rearrange it to the
136 // equivalent spelling [delAAA,insCGGTTT,=CGG].
138 d.Type == diffmatchpatch.DiffDelete &&
139 in[i+1].Type == diffmatchpatch.DiffEqual &&
140 in[i+2].Type == diffmatchpatch.DiffInsert &&
141 strings.HasSuffix(in[i+2].Text, in[i+1].Text) {
142 eq, ins := in[i+1], in[i+2]
143 ins.Text = eq.Text + ins.Text[:len(ins.Text)-len(eq.Text)]
147 // when diffmatchpatch says [=yyyyXXXX, delX, =zzz],
148 // we really want [=yyyy, delX, =XXXXzzz] (ditto for
149 // ins instead of del)
151 d.Type == diffmatchpatch.DiffEqual &&
152 in[i+1].Type != diffmatchpatch.DiffEqual &&
153 in[i+2].Type == diffmatchpatch.DiffEqual &&
154 len(in[i+1].Text) <= len(d.Text) {
155 for cut := 0; cut < len(d.Text); cut++ {
156 skip := strings.Index(d.Text[cut:], in[i+1].Text)
161 if d.Text[cut:]+in[i+1].Text == in[i+1].Text+d.Text[cut:] {
162 in[i+2].Text = d.Text[cut:] + in[i+2].Text
163 d.Text = d.Text[:cut]
168 // diffmatchpatch solves diff("AXX","XXX") with
169 // [delA,=XX,insX] but we prefer to spell it
172 // So, when we see a [del,=,ins] sequence that has the
173 // same effect after swapping the "=" and "ins" parts,
176 d.Type == diffmatchpatch.DiffDelete &&
177 in[i+1].Type == diffmatchpatch.DiffEqual &&
178 in[i+2].Type == diffmatchpatch.DiffInsert &&
179 in[i+1].Text+in[i+2].Text == in[i+2].Text+in[i+1].Text {
180 in[i+2], in[i+1] = in[i+1], in[i+2]
182 // when diffmatchpatch says [delAAA, insXAY] and
183 // len(X)==1, we prefer to treat the A>X as a snp.
185 d.Type == diffmatchpatch.DiffDelete &&
186 in[i+1].Type == diffmatchpatch.DiffInsert &&
188 len(in[i+1].Text) >= 2 &&
189 d.Text[1] == in[i+1].Text[1] {
191 for ; eqend < len(d.Text) && eqend < len(in[i+1].Text) && d.Text[eqend] == in[i+1].Text[eqend]; eqend++ {
194 diffmatchpatch.Diff{diffmatchpatch.DiffDelete, d.Text[:1]},
195 diffmatchpatch.Diff{diffmatchpatch.DiffInsert, in[i+1].Text[:1]},
196 diffmatchpatch.Diff{diffmatchpatch.DiffEqual, d.Text[1:eqend]})
197 in[i].Text, in[i+1].Text = in[i].Text[eqend:], in[i+1].Text[eqend:]
201 // when diffmatchpatch says [delAAA, insXaY] and
202 // len(Y)==1, we prefer to treat the A>Y as a snp.
204 d.Type == diffmatchpatch.DiffDelete &&
205 in[i+1].Type == diffmatchpatch.DiffInsert &&
207 len(in[i+1].Text) >= 2 &&
208 d.Text[len(d.Text)-2] == in[i+1].Text[len(in[i+1].Text)-2] {
209 // eqstart will be the number of equal chars
210 // before the terminal snp, plus 1 for the snp
211 // itself. Example, for [delAAAA, insTTAAG],
212 // eqstart will be 3.
214 for ; eqstart < len(d.Text) && eqstart < len(in[i+1].Text) && d.Text[len(d.Text)-eqstart] == in[i+1].Text[len(in[i+1].Text)-eqstart]; eqstart++ {
218 diffmatchpatch.Diff{diffmatchpatch.DiffDelete, d.Text[:len(d.Text)-eqstart]},
219 diffmatchpatch.Diff{diffmatchpatch.DiffInsert, in[i+1].Text[:len(in[i+1].Text)-eqstart]},
220 diffmatchpatch.Diff{diffmatchpatch.DiffEqual, d.Text[len(d.Text)-eqstart : len(d.Text)-1]},
221 diffmatchpatch.Diff{diffmatchpatch.DiffDelete, d.Text[len(d.Text)-1:]},
222 diffmatchpatch.Diff{diffmatchpatch.DiffInsert, in[i+1].Text[len(in[i+1].Text)-1:]})
228 // for i := 0; i < len(out)-1; i++ {
229 // if out[i].Type == diffmatchpatch.DiffDelete && len(out[i].Text) == 2 &&
230 // out[i+1].Type == diffmatchpatch.DiffInsert && len(out[i+1].Text) == 2 {
231 // out = append(out, diffmatchpatch.Diff{}, diffmatchpatch.Diff{})
232 // copy(out[i+4:], out[i+2:])
233 // out[i+2] = diffmatchpatch.Diff{diffmatchpatch.DiffDelete, out[i].Text[1:]}
234 // out[i+3] = diffmatchpatch.Diff{diffmatchpatch.DiffInsert, out[i+1].Text[1:]}
235 // out[i].Text = out[i].Text[:1]
236 // out[i+1].Text = out[i+1].Text[:1]
242 func Less(a, b Variant) bool {
243 if a.Position != b.Position {
244 return a.Position < b.Position
245 } else if a.New != b.New {