From 0fe5b743194ecce030f9b77d1f739b65352d62d2 Mon Sep 17 00:00:00 2001 From: mappu Date: Tue, 11 Jul 2017 17:53:06 +1200 Subject: [PATCH] diff: correctness fixes --- diff/diff.go | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-) diff --git a/diff/diff.go b/diff/diff.go index e6c3213..534f454 100644 --- a/diff/diff.go +++ b/diff/diff.go @@ -1,3 +1,7 @@ +// Paul's Simple Diff Algorithm v 0.1 +// (C) Paul Butler 2007 +// 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 -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 `` + strings.Join(this.Content, Separator) + `` + return `` + sc + `` case OP_REMOVED: - return `` + strings.Join(this.Content, Separator) + `` + return `` + sc + `` 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 }