Fix diff case
authorTom Clegg <tom@curii.com>
Wed, 27 Jul 2022 20:56:09 +0000 (16:56 -0400)
committerTom Clegg <tom@curii.com>
Wed, 27 Jul 2022 20:56:09 +0000 (16:56 -0400)
refs #19236 #note-15.6

Arvados-DCO-1.1-Signed-off-by: Tom Clegg <tom@curii.com>

hgvs/diff.go
hgvs/diff_test.go

index 65823931c8908f80e3464655fb7ae38be9547628..7fe72d9d263d060b5811e8bb300f109e4bf65e6b 100644 (file)
@@ -264,12 +264,25 @@ func cleanup(in []diffmatchpatch.Diff) (out []diffmatchpatch.Diff) {
                        continue
                }
                // [=AB,insCB,=D] => [=A,insBC,=BD]
+               // and
+               // [=AB,delCB,=D] => [=A,delBC,=BD]
                if i < len(in)-2 &&
                        d.Type == diffmatchpatch.DiffEqual &&
-                       in[i+1].Type == diffmatchpatch.DiffInsert &&
+                       in[i+1].Type != diffmatchpatch.DiffEqual &&
                        in[i+2].Type == diffmatchpatch.DiffEqual &&
                        len(d.Text) > 0 && len(in[i+1].Text) > 0 &&
-                       d.Text[len(d.Text)-1] == in[i+1].Text[len(in[i+1].Text)-1] {
+                       d.Text[len(d.Text)-1] == in[i+1].Text[len(in[i+1].Text)-1] &&
+                       !(i+3 < len(in) &&
+                               // Except: leave deletion alone if an
+                               // upcoming insertion will be moved up
+                               // against it: e.g., for
+                               // [=AB,delCB,=D,insED] we want
+                               // [=AB,delCB,=D,insED] for now, so it
+                               // can become [=AB,delCB,insDE,=D] on
+                               // the next iteration.
+                               in[i+1].Type == diffmatchpatch.DiffDelete &&
+                               in[i+3].Type == diffmatchpatch.DiffInsert &&
+                               strings.HasSuffix(in[i+3].Text, in[i+2].Text)) {
                        // Find x, length of common suffix B
                        x := 1
                        for ; x <= len(d.Text) && x <= len(in[i+1].Text); x++ {
index 1116b40c9fae994deb6c856e03cbd3e799a123ed..3e8be60d17e2cbc85e1766d01b580244f7af2df9 100644 (file)
@@ -222,6 +222,11 @@ func (s *diffSuite) TestDiff(c *check.C) {
                        b:      "cTTCGttaaa",
                        expect: []string{"1_2insTTCG"},
                },
+               {
+                       a:      "tcAACaggg",
+                       b:      "tcaggg",
+                       expect: []string{"2_4del"},
+               },
        } {
                c.Log(trial)
                var vars []string