diff: correctness fixes

This commit is contained in:
mappu 2017-07-11 17:53:06 +12:00
parent f2155babe0
commit 0fe5b74319

View File

@ -1,3 +1,7 @@
// Paul's Simple Diff Algorithm v 0.1
// (C) Paul Butler 2007 <http://www.paulbutler.org/>
// May be used and distributed under the zlib/libpng license.
// https://github.com/paulgb/simplediff/
package diff
import (
@ -6,13 +10,6 @@ import (
"strings"
)
/*
Paul's Simple Diff Algorithm v 0.1
(C) Paul Butler 2007 <http://www.paulbutler.org/>
May be used and distributed under the zlib/libpng license.
https://github.com/paulgb/simplediff/
*/
var Separator string = "\n"
type Operation int8
@ -29,17 +26,30 @@ type Instruction struct {
}
func (this *Instruction) RenderHTML() string {
sc := strings.Join(this.Content, Separator)
if len(sc) == 0 {
return ""
}
switch this.Op {
case OP_INSERTED:
return `<ins>` + strings.Join(this.Content, Separator) + `</ins>`
return `<ins>` + sc + `</ins>`
case OP_REMOVED:
return `<del>` + strings.Join(this.Content, Separator) + `</del>`
return `<del>` + sc + `</del>`
default: //OP_UNCHANGED:
return strings.Join(this.Content, Separator)
return sc
}
}
type twoints struct {
O, N int
}
func Diff(o, n []string) []Instruction {
if len(o) == 0 && len(n) == 0 {
return []Instruction{}
}
maxl := 0
omax := 0
nmax := 0
@ -56,7 +66,7 @@ func Diff(o, n []string) []Instruction {
}
// Build diff matrix
for nindex, _ := range n {
for nindex, _ := range nkeys {
if _, ok := matrix[oindex]; !ok {
matrix[oindex] = make(map[int]int, len(n)+1)
}
@ -79,9 +89,9 @@ func Diff(o, n []string) []Instruction {
ret := Diff(o[0:omax], n[0:nmax])
if maxl != nmax {
ret = append(ret, Instruction{OP_UNCHANGED, n[nmax:maxl]})
ret = append(ret, Instruction{OP_UNCHANGED, n[nmax : maxl-nmax]})
}
ret = append(ret, Diff(o[0:omax+maxl], n[0:nmax+maxl])...)
ret = append(ret, Diff(o[omax+maxl:], n[nmax+maxl:])...)
return ret
}