-
Notifications
You must be signed in to change notification settings - Fork 0
/
myersdiff.go
229 lines (192 loc) · 4.44 KB
/
myersdiff.go
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
89
90
91
92
93
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
package myersdiff
// This implementation of the Myers Diff Algorythm was written using http://simplygenius.net/Article/DiffTutorial1,
// and https://github.com/cj1128/myers-diff as references.
import (
"bytes"
)
const (
ADD opType = 1
DELETE = 2
NOOP = 3
)
type opType int
func (op opType) String() string {
switch op {
case ADD:
return "ADD"
case DELETE:
return "DEL"
case NOOP:
return "NOP"
}
return ""
}
// DiffOptions changes the behavior of how the diff is run or outputted.
type DiffOptions struct {
PrintAdd bool
PrintNoOp bool
PrintDelete bool
}
type trace struct {
v []map[int]int
srcLen int
dstLen int
}
func (t *trace) length() int {
return len(t.v)
}
func (t *trace) append(v map[int]int) {
t.v = append(t.v, v)
}
// GenerateDiff provides the diff of two string slices. To diff two files, split them by lines and provide each as a
// slice.
func GenerateDiff(src, dst []string, opts *DiffOptions) *bytes.Buffer {
script := shortestEditScript(src, dst)
return writeDiff(src, dst, script, opts)
}
// NewOptions returns a default set of options.
func NewOptions() *DiffOptions {
return &DiffOptions{
PrintAdd: true,
PrintNoOp: true,
PrintDelete: true,
}
}
func writeDiff(src, dst []string, script []opType, opts *DiffOptions) *bytes.Buffer {
buffer := bytes.Buffer{}
srcIndex, dstIndex := 0, 0
for _, op := range script {
switch op {
case ADD:
if opts.PrintAdd {
buffer.Write([]byte("+ "))
buffer.Write([]byte(dst[dstIndex]))
buffer.Write([]byte("\n"))
}
dstIndex += 1
case NOOP:
if opts.PrintNoOp {
buffer.Write([]byte(" "))
buffer.Write([]byte(src[srcIndex]))
buffer.Write([]byte("\n"))
}
srcIndex += 1
dstIndex += 1
case DELETE:
if opts.PrintDelete {
buffer.Write([]byte("- "))
buffer.Write([]byte(src[srcIndex]))
buffer.Write([]byte("\n"))
}
srcIndex += 1
}
}
return &buffer
}
func createTrace(src, dst []string) *trace {
srcLen := len(src)
dstLen := len(dst)
maxLen := srcLen + dstLen
var x, y int
trace := trace{
srcLen: len(src),
dstLen: len(dst),
}
for d := 0; d <= maxLen; d++ {
v := make(map[int]int, d+2)
trace.append(v)
// Find the first difference
if d == 0 {
firstDiff := 0
// Keep looking until the files differ
for len(src) > firstDiff && len(dst) > firstDiff && src[firstDiff] == dst[firstDiff] {
firstDiff++
}
// The first diff is at line firstDiff
v[0] = firstDiff
// If firstDiff is the end of the file, there is no diff
if firstDiff == len(src) && firstDiff == len(dst) {
return &trace
}
continue
}
lastV := trace.v[d-1]
for k := -d; k <= d; k += 2 {
switch {
// Go down (insert dest) if at the lowest k-line
case k == -d:
x = lastV[k+1]
// Go down (insert dest) lower k-line x is behind the higher k-line x. This comparison can't be made at the
// highest k-line (k==d).
case k != d && lastV[k-1] < lastV[k+1]:
x = lastV[k+1]
// Move right (del source) if at the highest k-line (k==d) or if the lower k-line x is further along.
default:
x = lastV[k-1] + 1
}
// Get y using the slope function
y = x - k
// Look for next diff along the diagonal (snake)
for x < srcLen && y < dstLen && src[x] == dst[y] {
x, y = x+1, y+1
}
// Set the k-line/x intercept
v[k] = x
// Reached the end of the source or dest.
if x == srcLen && y == dstLen {
return &trace
}
}
}
return &trace
}
func createScript(trace *trace) []opType {
var x, y int
var script []opType
x = trace.srcLen
y = trace.dstLen
var k, prevK, prevX, prevY int
for d := trace.length() - 1; d > 0; d-- {
k = x - y
lastV := trace.v[d-1]
switch {
case k == -d:
prevK = k + 1
case k != d && lastV[k-1] < lastV[k+1]:
prevK = k + 1
default:
prevK = k - 1
}
prevX = lastV[prevK]
prevY = prevX - prevK
for x > prevX && y > prevY {
script = append(script, NOOP)
x -= 1
y -= 1
}
if x == prevX {
script = append(script, ADD)
} else {
script = append(script, DELETE)
}
x, y = prevX, prevY
}
if trace.v[0][0] != 0 {
for i := 0; i < trace.v[0][0]; i++ {
script = append(script, NOOP)
}
}
return reverse(script)
}
func shortestEditScript(src, dst []string) []opType {
trace := createTrace(src, dst)
return createScript(trace)
}
func reverse(s []opType) []opType {
result := make([]opType, len(s))
end := len(s) - 1
for i, v := range s {
result[end-i] = v
}
return result
}