blob: 8539230a8cbfe3a7d755045e27c1c3722afa8c90 (
plain) (
blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
// Package diff implements line oriented diffs, similar to the ancient
// Unix diff command.
//
// The current implementation is just a wrapper around Sergi's
// go-diff/diffmatchpatch library, which is a go port of Neil
// Fraser's google-diff-match-patch code
package diff
import (
"bytes"
"time"
"github.com/sergi/go-diff/diffmatchpatch"
)
// Do computes the (line oriented) modifications needed to turn the src
// string into the dst string.
func Do(src, dst string) (diffs []diffmatchpatch.Diff) {
dmp := diffmatchpatch.New()
dmp.DiffTimeout = time.Hour // the default is time.Second which may be too little under heavy load
wSrc, wDst, warray := dmp.DiffLinesToRunes(src, dst)
diffs = dmp.DiffMainRunes(wSrc, wDst, false)
diffs = dmp.DiffCharsToLines(diffs, warray)
return diffs
}
// Dst computes and returns the destination text.
func Dst(diffs []diffmatchpatch.Diff) string {
var text bytes.Buffer
for _, d := range diffs {
if d.Type != diffmatchpatch.DiffDelete {
text.WriteString(d.Text)
}
}
return text.String()
}
// Src computes and returns the source text
func Src(diffs []diffmatchpatch.Diff) string {
var text bytes.Buffer
for _, d := range diffs {
if d.Type != diffmatchpatch.DiffInsert {
text.WriteString(d.Text)
}
}
return text.String()
}
|