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