-
Notifications
You must be signed in to change notification settings - Fork 0
/
textdiff.go
266 lines (237 loc) · 6.65 KB
/
textdiff.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
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
// Copyright 2020 cloudeng llc. All rights reserved.
// Use of this source code is governed by the Apache-2.0
// license that can be found in the LICENSE file.
// Package textdiff providers support for diff'ing text.
package textdiff
// TODO(cnicolaou): adjust the lcs algorithms to be identical to diff?
import (
"bytes"
"fmt"
"hash/fnv"
"strings"
"unicode/utf8"
"cloudeng.io/algo/codec"
"cloudeng.io/algo/lcs"
)
// LineFNVHashDecoder decodes a byte slice into newline delimited blocks each
// of which is represented by a 64 bit hash obtained from fnv.New64a.
func LineFNVHashDecoder(data []byte) (string, int64, int) {
if len(data) == 0 {
return "", 0, 0
}
idx := bytes.Index(data, []byte{'\n'})
if idx < 0 {
idx = len(data)
}
h := fnv.New64a()
h.Write(data[:idx])
sum := h.Sum64()
return string(data[:idx]), int64(sum), idx + 1
}
// LineDecoder represents a decoder that can be used to split a byte stream
// into lines for use with the cloudeng.io/algo/lcs package.
type LineDecoder struct {
lines []string
hashes []uint64
fn func([]byte) (string, int64, int)
}
// NewLineDecoder returns a new instance of LineDecoder.
func NewLineDecoder(fn func(data []byte) (string, int64, int)) *LineDecoder {
return &LineDecoder{fn: fn}
}
// Decode can be used as the decode function when creating a new
// decoder using cloudeng.io/algo.codec.NewDecoder.
func (ld *LineDecoder) Decode(data []byte) (int64, int) {
line, sum, n := LineFNVHashDecoder(data)
ld.lines = append(ld.lines, line)
ld.hashes = append(ld.hashes, uint64(sum))
return sum, n
}
// NumLines returns the number of lines decoded.
func (ld *LineDecoder) NumLines() int {
return len(ld.lines)
}
// Line returns the i'th line.
func (ld *LineDecoder) Line(i int) (string, uint64) {
return ld.lines[i], ld.hashes[i]
}
func text(orig []string, lines []int) string {
out := strings.Builder{}
for _, l := range lines {
out.WriteString(orig[l])
out.WriteString("\n")
}
return out.String()
}
func lineRange(lines []int) string {
nl := len(lines)
switch nl {
case 0:
return ""
case 1:
return fmt.Sprintf("%d", lines[0]+1)
default:
return fmt.Sprintf("%d,%d", lines[0]+1, lines[len(lines)-1]+1)
}
}
// getEditsForGroup splts edits into groups of contiguous of related edit
// operations.
// 1. groups split at an 'identical' boundary
// 2. multiple insertions at a single point
// 3. runs of contiguous deletions
func getEditsForGroup(edits lcs.EditScript) (group, script lcs.EditScript) {
last := 0
firstIns, firstDel := true, true
nextIns, nextDel := 0, 0
for i, edit := range edits {
switch edit.Op {
case lcs.Identical:
return group, edits[last+1:]
case lcs.Insert:
if firstIns {
nextIns = edit.A
}
if edit.A > nextIns {
goto done
}
nextIns = edit.A + 1
group = append(group, edit)
firstIns = false
case lcs.Delete:
if firstDel {
nextDel = edit.A
}
if edit.A > nextDel {
goto done
}
nextDel = edit.A + 1
firstDel = false
group = append(group, edit)
}
last = i
}
done:
return group, edits[last+1:]
}
// Group represents a single diff 'group', that is a set of insertions/deletions
// that are pertain to the same set of lines.
type Group struct {
edits lcs.EditScript
insertions, deletions map[int][]lcs.Edit
insertedLines, deletedLines []int
insertedText, deletedText string
}
func (d *Diff) newGroup(edits lcs.EditScript) *Group {
insertions, deletions := map[int][]lcs.Edit{}, map[int][]lcs.Edit{}
insertedLines, deletedLines := []int{}, []int{}
for _, edit := range edits {
switch edit.Op {
case lcs.Insert:
insertions[edit.A] = append(insertions[edit.A], edit)
insertedLines = append(insertedLines, edit.B)
case lcs.Delete:
deletions[edit.A] = append(deletions[edit.A], edit)
deletedLines = append(deletedLines, edit.A)
}
}
return &Group{
insertions: insertions,
deletions: deletions,
insertedLines: insertedLines,
deletedLines: deletedLines,
edits: edits,
insertedText: text(d.linesB, insertedLines),
deletedText: text(d.linesA, deletedLines),
}
}
// Summary returns a summary message in the style of the unix/linux diff
// command line tool, eg. 1,2a3.
func (g *Group) Summary() string {
onlyKey := func(m map[int][]lcs.Edit) int {
for k := range m {
return k
}
panic("unreachable")
}
ins, dels := g.insertions, g.deletions
ni, nd := len(ins), len(dels)
switch {
case ni == 1 && nd == 0:
l := onlyKey(ins)
return fmt.Sprintf("%da%s", ins[l][0].A, lineRange(g.insertedLines))
case nd >= 1 && ni == 0:
l := g.deletedLines[0]
return fmt.Sprintf("%sd%v", lineRange(g.deletedLines), dels[l][0].B)
default:
return fmt.Sprintf("%sc%s", lineRange(g.deletedLines), lineRange(g.insertedLines))
}
}
// Inserted returns the text to be inserted.
func (g *Group) Inserted() string {
return g.insertedText
}
// Deleted returns the text would be deleted.
func (g *Group) Deleted() string {
return g.deletedText
}
// Diff represents the ability to diff two slices.
type Diff struct {
utf8Decoder codec.Decoder
linesA, linesB []string
groups []*Group
}
// Same returns true if there were no diffs.
func (d *Diff) Same() bool {
return len(d.groups) == 0
}
// NumGroups returns the number of 'diff groups' created.
func (d *Diff) NumGroups() int {
return len(d.groups)
}
// Group returns the i'th 'diff group'.
func (d *Diff) Group(i int) *Group {
return d.groups[i]
}
// DiffByLines calls DiffByLinesUsing with the Myers function.
func DiffByLines(a, b []byte) *Diff {
return DiffByLinesUsing(a, b, Myers)
}
// Myers uses cloudeng.io/algo/myers to generate diffs.
func Myers(a, b interface{}) lcs.EditScript {
return lcs.NewMyers(a, b).SES()
}
// DP uses cloudeng.io/algo/myers to generate diffs.
func DP(a, b interface{}) lcs.EditScript {
return lcs.NewDP(a, b).SES()
}
// DiffByLinesUsing diffs the supplied strings on a line-by-line basis using
// the supplied function to generate the diffs.
func DiffByLinesUsing(a, b []byte, engine func(a, b interface{}) lcs.EditScript) *Diff {
lda, ldb := NewLineDecoder(LineFNVHashDecoder), NewLineDecoder(LineFNVHashDecoder)
decA, err := codec.NewDecoder(lda.Decode)
if err != nil {
panic(err)
}
decB, _ := codec.NewDecoder(ldb.Decode)
da, db := decA.Decode(a), decB.Decode(b)
utf8Dec, err := codec.NewDecoder(utf8.DecodeRune)
if err != nil {
panic(err)
}
diff := &Diff{
utf8Decoder: utf8Dec,
linesA: lda.lines,
linesB: ldb.lines,
}
lineDiffs := engine(da, db)
script := lineDiffs
for len(script) > 0 {
var edits lcs.EditScript
edits, script = getEditsForGroup(script)
if len(edits) == 0 {
continue
}
diff.groups = append(diff.groups, diff.newGroup(edits))
}
return diff
}