-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathdiff_test.go
More file actions
34 lines (31 loc) · 846 Bytes
/
Copy pathdiff_test.go
File metadata and controls
34 lines (31 loc) · 846 Bytes
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
package httpsign
import (
"strings"
"github.com/sergi/go-diff/diffmatchpatch"
)
// characterDiff returns an inline diff using (++added++) and (~~deleted~~) markup.
// Same formatting as the old github.com/andreyvit/diff CharacterDiff helper.
func characterDiff(a, b string) string {
dmp := diffmatchpatch.New()
diffs := dmp.DiffMain(a, b, true)
if len(diffs) > 2 {
diffs = dmp.DiffCleanupSemantic(diffs)
diffs = dmp.DiffCleanupEfficiency(diffs)
}
var bld strings.Builder
for _, d := range diffs {
switch d.Type {
case diffmatchpatch.DiffInsert:
bld.WriteString("(++")
bld.WriteString(d.Text)
bld.WriteString("++)")
case diffmatchpatch.DiffDelete:
bld.WriteString("(~~")
bld.WriteString(d.Text)
bld.WriteString("~~)")
case diffmatchpatch.DiffEqual:
bld.WriteString(d.Text)
}
}
return bld.String()
}