/
diff.go
269 lines (232 loc) · 5.65 KB
/
diff.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
267
268
269
// Copyright 2018 Shulhan <ms@kilabit.info>. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
//
// Package diff implement text comparison.
//
package diff
import (
"bytes"
"fmt"
"github.com/shuLhan/share/lib/text"
)
var (
// DefDelimiter define default delimiter for new line.
DefDelimiter = byte('\n')
)
//
// Data represent additions, deletions, and changes between two text.
//
type Data struct {
Adds text.Lines
Dels text.Lines
Changes LineChanges
}
//
// Text search the difference between two texts.
//
func Text(before, after []byte, level int) (diffs Data) {
beforeLines := text.ParseLines(before)
afterLines := text.ParseLines(after)
return Lines(beforeLines, afterLines, level)
}
//
// Lines search the difference between two Lines.
//
func Lines(oldlines, newlines text.Lines, level int) (diffs Data) {
oldlen := len(oldlines)
newlen := len(newlines)
x := 0
y := 0
for x < oldlen {
if y == newlen {
// New text has been full examined. Leave out the old
// text that means deletion at the end of text.
diffs.PushDel(oldlines[x])
oldlines[x].V = nil
x++
continue
}
// Compare old line with new line.
if IsEqual(oldlines[x].V, newlines[y].V) {
oldlines[x].V = nil
newlines[y].V = nil
x++
y++
continue
}
// Check for whitespace changes
oldlinetrim := bytes.TrimSpace(oldlines[x].V)
newlinetrim := bytes.TrimSpace(newlines[y].V)
oldtrimlen := len(oldlinetrim)
newtrimlen := len(newlinetrim)
// Both are empty, probably one of them is changing
if oldtrimlen <= 0 && newtrimlen <= 0 {
diffs.PushChange(oldlines[x], newlines[y])
oldlines[x].V = nil
newlines[y].V = nil
x++
y++
continue
}
// Old is empty or contain only whitespaces.
if oldtrimlen <= 0 {
diffs.PushDel(oldlines[x])
oldlines[x].V = nil
x++
continue
}
// New is empty or contain only whitespaces.
if newtrimlen <= 0 {
diffs.PushAdd(newlines[y])
newlines[y].V = nil
y++
continue
}
ratio, _, _ := BytesRatio(oldlines[x].V, newlines[y].V,
DefMatchLen)
if ratio > DefMatchRatio {
// Ratio of similar bytes is higher than minimum
// expectation. So, it must be changes
diffs.PushChange(oldlines[x], newlines[y])
oldlines[x].V = nil
newlines[y].V = nil
x++
y++
continue
}
// x is not equal with y, search down...
foundx, xaty := findLine(oldlines[x], newlines, y+1)
// Cross check the y with the rest of x...
foundy, yatx := findLine(newlines[y], oldlines, x+1)
// Both line is missing, its mean changes on current line
if !foundx && !foundy {
diffs.PushChange(oldlines[x], newlines[y])
oldlines[x].V = nil
newlines[y].V = nil
x++
y++
continue
}
// x still missing, means deletion in old text.
if !foundx && foundy {
for ; x < yatx && x < oldlen; x++ {
diffs.PushDel(oldlines[x])
oldlines[x].V = nil
}
continue
}
// we found x but y is missing, its mean addition in new text.
if foundx && !foundy {
for ; y < xaty && y < newlen; y++ {
diffs.PushAdd(newlines[y])
newlines[y].V = nil
}
continue
}
if foundx && foundy {
// We found x and y. Check which one is the
// addition or deletion based on line range.
addlen := xaty - y
dellen := yatx - x
switch {
case addlen < dellen:
for ; y < xaty && y < newlen; y++ {
diffs.PushAdd(newlines[y])
newlines[y].V = nil
}
case addlen == dellen:
// Both changes occur between lines
for x < yatx && y < xaty {
diffs.PushChange(oldlines[x],
newlines[y])
oldlines[x].V = nil
newlines[y].V = nil
x++
y++
}
default:
for ; x < yatx && x < oldlen; x++ {
diffs.PushDel(oldlines[x])
oldlines[x].V = nil
}
}
continue
}
}
// Check if there is a left over from new text.
for ; y < newlen; y++ {
diffs.PushAdd(newlines[y])
newlines[y].V = nil
}
if level == LevelWords {
// Process each changes to find modified chunkes.
for x, change := range diffs.Changes {
adds, dels := Bytes(change.Old.V, change.New.V, 0, 0)
diffs.Changes[x].Adds = adds
diffs.Changes[x].Dels = dels
}
}
return diffs
}
//
// PushAdd will add new line to diff set.
//
func (diffs *Data) PushAdd(new text.Line) {
diffs.Adds = append(diffs.Adds, new)
}
//
// PushDel will add deletion line to diff set.
//
func (diffs *Data) PushDel(old text.Line) {
diffs.Dels = append(diffs.Dels, old)
}
//
// PushChange set to diff data.
//
func (diffs *Data) PushChange(old, new text.Line) {
change := NewLineChange(old, new)
diffs.Changes = append(diffs.Changes, *change)
}
//
// GetAllAdds return chunks of additions including in line changes.
//
func (diffs *Data) GetAllAdds() (chunks text.Chunks) {
for _, add := range diffs.Adds {
chunks = append(chunks, text.Chunk{StartAt: 0, V: add.V})
}
chunks = append(chunks, diffs.Changes.GetAllAdds()...)
return
}
//
// GetAllDels return chunks of deletions including in line changes.
//
func (diffs *Data) GetAllDels() (chunks text.Chunks) {
for _, del := range diffs.Dels {
chunks = append(chunks, text.Chunk{StartAt: 0, V: del.V})
}
chunks = append(chunks, diffs.Changes.GetAllDels()...)
return
}
//
// String return formatted data.
//
func (diffs Data) String() (s string) {
s += "Diffs:\n"
if len(diffs.Adds) > 0 {
s += ">>> Adds:\n"
for _, add := range diffs.Adds {
s += fmt.Sprintf(" + %d : %s", add.N, string(add.V))
}
}
if len(diffs.Dels) > 0 {
s += ">>> Dels:\n"
for _, del := range diffs.Dels {
s += fmt.Sprintf(" - %d : %s", del.N, string(del.V))
}
}
if len(diffs.Changes) > 0 {
s += ">>> Changes:\n" + fmt.Sprint(diffs.Changes)
}
return
}