i++
continue
}
- // when diffmatchpatch says [delA, =X, delBX], we
- // prefer [delAB, =X].
+ // [=AB,insCB,=D] => [=A,insBC,=BD]
+ // and
+ // [=AB,delCB,=D] => [=A,delBC,=BD]
if i < len(in)-2 &&
- d.Type == diffmatchpatch.DiffDelete &&
- in[i+1].Type == diffmatchpatch.DiffEqual &&
- in[i+2].Type == diffmatchpatch.DiffDelete &&
- strings.HasSuffix(in[i+2].Text, in[i+1].Text) {
- out = append(out,
- diffmatchpatch.Diff{diffmatchpatch.DiffDelete, d.Text + in[i+2].Text[:len(in[i+2].Text)-len(in[i+1].Text)]},
- in[i+1])
- i += 2
- continue
+ d.Type == diffmatchpatch.DiffEqual &&
+ in[i+1].Type != diffmatchpatch.DiffEqual &&
+ in[i+2].Type == diffmatchpatch.DiffEqual &&
+ len(d.Text) > 0 && len(in[i+1].Text) > 0 &&
+ !(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)) {
+ if i+3 < len(in) && in[i+1].Type == in[i+3].Type && strings.HasSuffix(in[i+3].Text, in[i+2].Text) {
+ // [=AB,delC,=E,delDBE] => [=AB,delCEDB,=E,=]
+ in[i+1], in[i+2], in[i+3] = diffmatchpatch.Diff{in[i+1].Type, in[i+1].Text + in[i+2].Text + in[i+3].Text[:len(in[i+3].Text)-len(in[i+2].Text)]},
+ diffmatchpatch.Diff{diffmatchpatch.DiffEqual, in[i+2].Text},
+ diffmatchpatch.Diff{diffmatchpatch.DiffEqual, ""}
+ }
+ // Find x, length of common suffix B
+ x := 1
+ for ; x <= len(d.Text) && x <= len(in[i+1].Text); x++ {
+ if d.Text[len(d.Text)-x] != in[i+1].Text[len(in[i+1].Text)-x] {
+ break
+ }
+ }
+ x--
+ d.Text, in[i+1].Text, in[i+2].Text =
+ d.Text[:len(d.Text)-x],
+ d.Text[len(d.Text)-x:]+
+ in[i+1].Text[:len(in[i+1].Text)-x],
+ in[i+1].Text[len(in[i+1].Text)-x:]+in[i+2].Text
+ }
+ // [=X,delAX] => [delXA,=X]
+ if i < len(in)-1 &&
+ d.Type == diffmatchpatch.DiffEqual &&
+ in[i+1].Type == diffmatchpatch.DiffDelete && false {
}
out = append(out, d)
}