-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiff.go
More file actions
416 lines (353 loc) · 8.2 KB
/
diff.go
File metadata and controls
416 lines (353 loc) · 8.2 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
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
/*
Golang test helper library: sztest.
Copyright (C) 2023-2025 Leslie Dancsecs
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
package sztest
import (
"fmt"
"sort"
"strings"
)
type diffType rune
// Represents the type of output displayed when diffing strings.
const (
diffWant = diffType('W')
diffGot = diffType('G')
diffMerge = diffType('M')
)
// Default compare function.
func defaultCmpFunc[T chkType](got, want T) bool {
return got == want
}
// compareArrays returns "" an empty string if there are no differences
// otherwise it returns a string outlining the differences.
func compareArrays[T chkType](got, wnt []T) string {
differencesFound := false
ret := "" +
strings.Join(
diffSlice(
got,
wnt,
newDiffLnFmt(len(got), len(wnt)),
&differencesFound,
settingDiffSlice,
settingDiffChars,
defaultCmpFunc[T],
),
"\n",
)
if !differencesFound {
ret = ""
}
return ret
}
// compareSlices checks two slices for differences.
func compareSlices[T chkType](
title string,
got, want []T,
minRunSlice, minRunString int,
cmp func(a, b T) bool,
_ func(any) string,
) string {
differencesFound := false
ret := "" +
strings.Join(
diffSlice(
got,
want,
newDiffLnFmt(len(got), len(want)),
&differencesFound,
minRunSlice,
minRunString,
cmp,
),
"\n",
)
if differencesFound {
if title != "" {
title += ": "
}
ret = fmt.Sprint(
title,
"got (", len(got), " lines)",
" - ",
"want (", len(want), " lines)",
"\n",
) + ret
return ret
}
return ""
}
// diffSlice compares two Slices.
//
//nolint:funlen,cyclop,gocognit // Ok.
func diffSlice[T chkType](
gotSlice, wntSlice []T,
dFmt *diffLnFmt,
changed *bool,
minRunSlice int,
minRunString int,
cmp func(a, b T) bool,
) []string {
lenGotSlice := len(gotSlice)
lenWntSlice := len(wntSlice)
result := make([]string, 0, lenGotSlice+lenWntSlice)
same := lenGotSlice == lenWntSlice
for i := 0; i < lenGotSlice && same; i++ {
if !cmp(gotSlice[i], wntSlice[i]) {
same = false
}
}
if same {
// Just return a clean unmarked list with changed equal to false.
for lnNbr, s := range gotSlice {
result = append(result, dFmt.same(lnNbr, lnNbr, s))
}
return result
}
if lenGotSlice == 0 {
for lnNbr, s := range wntSlice {
result = append(result, dFmt.justWnt(lnNbr, s))
}
*changed = true
return result
}
if lenWntSlice == 0 {
for lnNbr, s := range gotSlice {
result = append(result, dFmt.justGot(lnNbr, s))
}
*changed = true
return result
}
// biggest matching window between slices
gotIdx, wntInd, numLines := bestNextRun(
gotSlice, wntSlice, minRunSlice, cmp,
)
if numLines == 0 {
// nothing matched. All changed
*changed = true
switch {
case lenGotSlice < lenWntSlice:
for i := range lenGotSlice {
result = append(
result,
dFmt.changed(i, i,
diffString(
fmt.Sprint(gotSlice[i]),
fmt.Sprint(wntSlice[i]),
diffMerge,
minRunString,
)),
)
}
for i := lenGotSlice; i < lenWntSlice; i++ {
result = append(result, dFmt.justWnt(i, wntSlice[i]))
}
case lenGotSlice > lenWntSlice:
for i := range lenWntSlice {
result = append(
result,
dFmt.changed(i, i,
diffString(
fmt.Sprint(gotSlice[i]),
fmt.Sprint(wntSlice[i]),
diffMerge,
minRunString,
)),
)
}
for i := lenWntSlice; i < lenGotSlice; i++ {
result = append(result, dFmt.justGot(i, gotSlice[i]))
}
default: // lengths are equal
for i := range lenGotSlice {
result = append(
result,
dFmt.changed(i, i,
diffString(
fmt.Sprint(gotSlice[i]),
fmt.Sprint(wntSlice[i]),
diffMerge,
minRunString,
)),
)
}
}
return result
}
// Return a composition of the largest matching segment prefixed
// by recursively calling DiffString with the two strings prefix
// before the matching section and suffixed by the recursive call
// to diff string once again.
// Check lines before largest identical section
result = append(result, diffSlice(
gotSlice[:gotIdx],
wntSlice[:wntInd],
dFmt,
changed,
minRunSlice,
minRunString,
cmp,
)...)
// Add unchanged lines
for i := range numLines {
result = append(
result,
dFmt.same(i+gotIdx, i+wntInd, gotSlice[i+gotIdx]),
)
}
// Check lines after largest identical section
var (
endOld []T
endNew []T
)
if gotIdx+numLines < lenGotSlice {
endOld = gotSlice[gotIdx+numLines:]
}
if wntInd+numLines < lenWntSlice {
endNew = wntSlice[wntInd+numLines:]
}
result = append(result, diffSlice(
endOld,
endNew,
dFmt.newOffset(gotIdx+numLines, wntInd+numLines),
changed,
minRunSlice,
minRunString,
cmp,
)...)
return result
}
// diffString checks two strings for differences.
//
//nolint:cyclop // Ok.
func diffString(gotStr, wntStr string, dType diffType, minRun int) string {
if gotStr == wntStr {
// unchanged
return gotStr
}
switch dType { //nolint:exhaustive // Default handles all other cases.
case diffWant:
if wntStr == "" {
// missing from section
return wntStr
}
if gotStr == "" {
// new to section
return markAsDel(wntStr)
}
case diffGot:
if gotStr == "" {
// new to section
return gotStr
}
if wntStr == "" {
// missing from section
return markAsIns(gotStr)
}
default: // DiffMerge.
if gotStr == "" {
// new to section
return markAsDel(wntStr)
}
if wntStr == "" {
// missing from section
return markAsIns(gotStr)
}
}
// Biggest matching window between strings.
gotIdx, wntIdx, numChars := bestNextRunString(gotStr, wntStr, minRun)
if numChars == 0 {
// nothing matched. All changed
return markAsChg(gotStr, wntStr, dType)
}
// Return a composition of the largest matching segment prefixed and
// suffixed by recursively calling DiffString with prefix and suffix
// strings respectively.
return "" +
diffString(gotStr[:gotIdx], wntStr[:wntIdx], dType, minRun) +
gotStr[gotIdx:gotIdx+numChars] +
diffString(
gotStr[gotIdx+numChars:], wntStr[wntIdx+numChars:], dType, minRun,
)
}
func bestNextRunString(got, wnt string, minRun int) (int, int, int) {
return bestNextRun(
strings.Split(got, ""),
strings.Split(wnt, ""),
minRun,
defaultCmpFunc[string],
)
}
type match struct {
gotStart int
wntStart int
length int
}
func calculateScores[V chkType](
gotSlice, wntSlice []V,
minRun int,
cmp func(a, b V) bool,
) []match {
scores := []match{}
gotMax := len(gotSlice)
wntMax := len(wntSlice)
for gotIdx := range gotSlice {
for wntIdx := range wntSlice {
if cmp(gotSlice[gotIdx], wntSlice[wntIdx]) {
numProcessed := 1
cGotIdx := gotIdx + 1
cWntIdx := wntIdx + 1
for cGotIdx < gotMax &&
cWntIdx < wntMax &&
cmp(gotSlice[cGotIdx], wntSlice[cWntIdx]) {
//
numProcessed++
cGotIdx++
cWntIdx++
}
if numProcessed >= minRun {
scores = append(scores, match{
gotStart: gotIdx,
wntStart: wntIdx,
length: numProcessed,
})
}
}
}
}
return scores
}
func bestNextRun[V chkType](
gotSlice, wntSlice []V,
minRun int,
cmp func(a, b V) bool,
) (int, int, int) {
scores := calculateScores(gotSlice, wntSlice, minRun, cmp)
sort.Slice(scores, func(i int, j int) bool {
// return true if i < j
iMatch := scores[i]
jMatch := scores[j]
if iMatch.length == jMatch.length {
if iMatch.gotStart == jMatch.gotStart {
return iMatch.wntStart <= jMatch.wntStart
}
return iMatch.gotStart <= jMatch.gotStart
}
return iMatch.length >= jMatch.length
})
if len(scores) == 0 {
return 0, 0, 0
}
return scores[0].gotStart, scores[0].wntStart, scores[0].length
}