-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexample_custom_test.go
More file actions
88 lines (74 loc) · 1.64 KB
/
Copy pathexample_custom_test.go
File metadata and controls
88 lines (74 loc) · 1.64 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package diff_test
import (
"fmt"
"strings"
"mibk.dev/diff"
)
// columns presents two grids of ASCII characters to Diff
// as their sequences of columns.
// A column is not a slice of its own,
// which is what the Data interface is for:
// it describes a sequence by index,
// leaving it to exist however it already does.
type columns struct {
a, b []string // rows, equally long within a grid
}
func (c columns) Lens() (n, m int) { return len(c.a[0]), len(c.b[0]) }
func (c columns) Equal(i, j int) bool {
for r := range c.a {
if c.a[r][i] != c.b[r][j] {
return false
}
}
return true
}
func Example_customType() {
a := []string{
"abcd",
"1234",
}
b := []string{
"abxd",
"12y4",
}
for _, ed := range diff.Diff(columns{a, b}) {
switch ed.Op {
case diff.Delete:
fmt.Println("-", column(a, ed.Index))
case diff.Insert:
fmt.Println("+", column(b, ed.Bindex))
}
}
// output:
// - c3
// + xy
}
func column(rows []string, i int) string {
col := make([]byte, len(rows))
for r, row := range rows {
col[r] = row[i]
}
return string(col)
}
// missingIgnoreCase returns elements that are moved or deleted from a
// compared to b.
func missingIgnoreCase(a, b []string) []string {
eds := diff.SlicesFunc(a, b, strings.EqualFold)
var miss []string
for _, ed := range eds {
if ed.Op == diff.Insert {
miss = append(miss, b[ed.Bindex])
}
}
return miss
}
func Example_ignoreCase() {
a := []string{"black", "#31ad1d", "#8923dd", "#baddad", "yellow"}
b := []string{"#31AD1D", "#8924dd", "#BadDad", "black", "YELLOW"}
for _, m := range missingIgnoreCase(a, b) {
fmt.Println("-", m)
}
// output:
// - #8924dd
// - black
}