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
|
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
|
-
-
-
-
-
+
-
-
-
-
-
+
+
|
var hd hashDiff
hd.hash = fnv.New64a()
hd.first = read(args[0], hd.hash)
hd.second = read(args[1], hd.hash)
var result diff.Delta = diff.Diff(&hd)
//var marks []diff.Mark = make([]diff.Mark, len(result.Added)+len(result.Removed))
//var added, removed []diff.Mark = result.Added, result.Removed
fmt.Println(result)
gen := source(result)
for have, added, mark := gen(); have; have, added, mark = gen() {
var from []line
var from []line = hd.line(!added)
if added {
from = hd.second
} else {
from = hd.first
}
fmt.Println()
for i := mark.From; i < mark.Length; i++ {
fmt.Print(i)
if added {
fmt.Print(" > ")
} else {
fmt.Print(" < ")
}
fmt.Println(from[i].text)
}
}
}
// Returns a closure over the provided diff.Delta
// that returns diff.Mark entries while prioritizing removals when possible
func source(d diff.Delta) func() (bool, bool, diff.Mark) {
var addedAt, removedAt int = 0, 0
return func() (bool, bool, diff.Mark) {
var addsOver bool = addedAt == len(d.Added)
var removesOver bool = removedAt == len(d.Removed)
var add, remove diff.Mark
|
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
|
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
+
-
+
+
+
+
+
+
+
+
+
+
|
// A line-based diff.Interface implementation
type hashDiff struct {
first, second []line
hash hash.Hash64
}
// Required per diff.Interface
func (h *hashDiff) Equal(i, j int) bool {
// return h.first[i].text == h.second[j].text
if h.first[i].hash != h.second[j].hash {
return false
} else {
return h.first[i].text == h.second[j].text
}
}
// Required per diff.Interface
func (h *hashDiff) Len() (int, int) {
return len(h.first), len(h.second)
}
// A helper method for getting a line slice
func (h *hashDiff) line(first bool) []line {
if first {
return h.first
}
return h.second
}
// Holds a text line and its hash
type line struct {
hash uint64
text string
}
// Reads all lines in a file and returns a line entry for each
func read(name string, h hash.Hash64) []line {
|