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 |
x3
x26
x26
x26
x51
x51
x51
x51
x26
x26
x26
x51
x51
x51
x26
x26
x25
x51
x50
x50
x226
x50
x50
x226
x172
x172
x24
x24
x172
x148
x148
x172
x172
x226
x60
x60
x226
x116
x116
x51
x51
x116
x14
x14
x116
x226
x226
x51
x51
x51
x51
x51
x2
x2
x1
x1
x2
x1
x1
x2
x2
x25
x25
x25
x51
x51
x51
x222
x222
x7
x222
x39
x222
x40
x222
x25
x222
x111
x222
x51
x51
x51
x3
x100
x100
x100
x3
x316
x316
x511
x511
x96
x96
x96
x96
x96
x415
x415
x357
x316
x3
x158
x158
x158
x158
x3
x29
x29
x29
x127
x127
x450
x450
x127
x127
x127
x29
x25
x25
x91
x91
x25
x29
x29
x3
x242
x242
x242
x40
x40
x242
x39
x39
x242
x242
x242
x3
x132
x152
x152
x132
x132
x11
x11
x11
x132
x132
x3
x132
x129
x40
x40
x129
x39
x39
x129
x132
x11
x11
x132
x3
x29
x29
x4
x4
x4
x29
x12
x12
x25
x29
x91
x91
x25
x25
x25
x29
x3
x26
x26
x26
x3
x1
x1
x3
x3
x2
x2
x26
x23
x2
x2
x2
x23
x2
x2
x2
x23
x19
x19
x23
x26
x3
x26
x26
x26
x26
x26
x26
x26
|
I
|
export function diff(a: string, b: string, { colors = false, context = 3 } = {}): string {
const hunks = [] as string[]
const { lines } = patience(tokenize(a), tokenize(b))
for (let after = -1; (after < lines.length) && (!Number.isNaN(after));) {
const { next, patch } = _diff(lines, { after, colors, context })
hunks.push(patch)
after = next
}
const patch = hunks.join("").replace(/\n?\n$/, "")
return patch ? `--- a\n+++ b\n${patch}` : ""
}
function _diff(lines: line[], { after = -1, colors = false, context = 0 }: { after?: number; context?: number; colors?: boolean }): { next: number; patch: string } {
const k = lines.findIndex(({ a, b, token }, i) => ((a < 0) || (b < 0)) && (!token) && (i > after))
if (k < 0) {
return { next: NaN, patch: "" }
}
const patch = [lines[k]]
for (const delta of [-1, 1]) {
let contextual = context
for (let i = k; (i >= 0) && (i < lines.length); i += delta) {
if (i === k) {
continue
}
if ((delta < 0) && (lines[i].token === "!\n")) {
break
}
if ((lines[i].a < 0) || (lines[i].b < 0) || ((contextual > 0) && (lines[i].a >= 0) && (lines[i].b >= 0))) {
switch (true) {
case delta < 0:
patch.unshift(lines[i])
break
case delta > 0:
patch.push(lines[i])
break
}
}
if ((lines[i].a < 0) || (lines[i].b < 0)) {
contextual = context
}
if ((lines[i].a >= 0) && (lines[i].b >= 0)) {
contextual--
if ((delta > 0) && (!lines.slice(i + 1, i + 1 + context + 1).every(({ a, b }) => (a >= 0) && (b >= 0)))) {
contextual = context
}
if (contextual <= 0) {
break
}
}
}
}
let a = (patch.find(({ a, token }) => (a >= 0) && (!token))?.a ?? -1) + 1
let b = (patch.find(({ b, token }) => (b >= 0) && (!token))?.b ?? -1) + 1
const A = patch.filter(({ a, token }) => (a >= 0) && (!token)).length
const B = patch.filter(({ b, token }) => (b >= 0) && (!token)).length
if (context <= 1) {
switch (true) {
case A > 0:
b = a - 1
break
case B > 0:
a = b
break
}
}
return {
next: lines.indexOf(patch.findLast(({ a, b, token }) => ((a < 0) || (b < 0)) && (!token))!),
patch: [
{ line: `@@ -${a}${(A !== 1) || (a === 0) ? `,${A}` : ""} +${b}${(B !== 1) || (b === 0) ? `,${B}` : ""} @@\n`, a: NaN, b: NaN },
...patch,
].map(({ line, a, b, token }) => {
switch (true) {
case token === "!\n":
return "\\ No newline at end of file\n"
case a < 0:
return colors ? `\x1b[32m+${line}\x1b[0m` : `+${line}`
case b < 0:
return colors ? `\x1b[31m-${line}\x1b[0m` : `-${line}`
case Number.isNaN(a) && Number.isNaN(b):
return colors ? `\x1b[36m${line}\x1b[0m` : line
default:
return line === "\n" ? line : ` ${line}`
}
}).join(""),
}
}
export function tokenize(text: string): string[] {
text += "\n"
return [...text.matchAll(/.*(?:\r?\n)/g)].map(([token]) => token.replace(/\r\n$/, "\n")).filter((token) => token)
}
function unique({ lines }: operand, i: number, j: number) {
const occurrences = new Map<string, { index: number; count: number }>()
for (; i <= j; i++) {
const line = lines[i]
if (occurrences.has(line)) {
const entry = occurrences.get(line)!
entry.count++
entry.index = i
continue
}
occurrences.set(line, { index: i, count: 1 })
}
return new Map<string, number>([...occurrences].filter(([_, { count }]) => count === 1).map(([key, { index }]) => [key, index]))
}
function common(A: operand, B: operand, Ai: number, Aj: number, Bi: number, Bj: number) {
const a = unique(A, Ai, Aj)
const b = unique(B, Bi, Bj)
return new Map<string, position>([...a].filter(([line]) => b.has(line)).map(([line, index]) => [line, { a: index, b: b.get(line)! }]))
}
function lcs(ab: ReturnType<typeof common>) {
const lcs = [] as Array<position & { previous?: position }>
const subsequences = [] as Array<typeof lcs>
for (const [_, v] of ab) {
let i = 0
while (subsequences.at(i)?.at(-1)?.b! < v.b) {
i++
}
subsequences[i] ??= []
subsequences[i].push({ ...v, previous: i > 0 ? subsequences.at(i - 1)?.at(-1) : undefined })
}
if (subsequences.length) {
lcs.push(subsequences.at(-1)!.at(-1)!)
while (lcs.at(-1)!.previous) {
lcs.push(lcs.at(-1)!.previous!)
}
}
return lcs.reverse()
}
function register(result: result, A: operand, B: operand, a: number, b: number) {
const line = { line: 0 <= a ? A.lines[a] : B.lines[b], a, b } as line
switch (true) {
case b < 0:
result.deleted++
break
case a < 0:
result.added++
break
}
result.lines.push(line)
}
function submatch(result: result, A: operand, B: operand, Ai: number, Aj: number, Bi: number, Bj: number) {
while ((Ai <= Aj) && (Bi <= Bj) && (A.lines[Ai] === B.lines[Bi])) {
register(result, A, B, Ai++, Bi++)
}
const Tj = Aj
while ((Ai <= Aj) && (Bi <= Bj) && (A.lines[Aj] === B.lines[Bj])) {
Aj--
Bj--
}
const ab = common(A, B, Ai, Aj, Bi, Bj)
if (ab.size) {
recurse(result, A, B, Ai, Aj, Bi, Bj, ab)
} else {
while (Ai <= Aj) {
register(result, A, B, Ai++, -1)
}
while (Bi <= Bj) {
register(result, A, B, -1, Bi++)
}
}
while (Aj < Tj) {
register(result, A, B, ++Aj, ++Bj)
}
}
function recurse(result: result, A: operand, B: operand, Ai: number, Aj: number, Bi: number, Bj: number, ab?: ReturnType<typeof common>) {
const x = lcs(ab ?? common(A, B, Ai, Aj, Bi, Bj))
if (!x.length) {
submatch(result, A, B, Ai, Aj, Bi, Bj)
return
}
if ((Ai < x[0].a) || (Bi < x[0].b)) {
submatch(result, A, B, Ai, x[0].a - 1, Bi, x[0].b - 1)
}
let i = 0
for (; i < x.length - 1; i++) {
submatch(result, A, B, x[i].a, x[i + 1].a - 1, x[i].b, x[i + 1].b - 1)
}
if (+(x[i].a <= Aj) | +(x[i].b <= Bj)) {
submatch(result, A, B, x[i].a, Aj, x[i].b, Bj)
}
}
function postprocess(result: result) {
const la = result.lines.findLast(({ a }) => a >= 0)!
const lb = result.lines.findLast(({ b }) => b >= 0)!
if ((la.line !== "\n") && (lb.line !== "\n")) {
if (la !== lb) {
result.lines.splice(result.lines.indexOf(la) + 1, 0, { line: "", a: la.a + 1, b: -1, token: "!\n" })
result.lines.splice(result.lines.indexOf(lb) + 1, 0, { line: "", a: -1, b: lb.b + 1, token: "!\n" })
}
else if (la === lb) {
result.lines.splice(result.lines.indexOf(la) + 1, 0, { line: "", a: la.a + 1, b: lb.b + 1, token: "!\n" })
}
} else {
if ((lb.line === "\n") && (lb.b >= 0) && (lb.a < 0)) {
result.lines.splice(result.lines.indexOf(la), 1, { ...la, b: -1 }, { line: "", a: la.a + 1, b: -1, token: "!\n" }, ...((la.a >= 0) && (la.b >= 0) ? [{ ...la, b: la.a, a: -1 }] : []))
result.lines.splice(result.lines.indexOf(lb), 1)
}
if ((la.line === "\n") && (la.a >= 0) && (la.b < 0)) {
result.lines.splice(result.lines.indexOf(lb), 1, ...((lb.a >= 0) && (lb.b >= 0) ? [{ ...lb, a: lb.b, b: -1 }] : []), { ...lb, a: -1 }, { line: "", a: -1, b: lb.b + 1, token: "!\n" })
result.lines.splice(result.lines.indexOf(la), 1)
}
if ((la === lb) && (la.line === "\n")) {
la.token = "\n"
}
}
}
function patience(a: string[], b: string[]) {
const A = { lines: a } as operand
const B = { lines: b } as operand
const result = { lines: [], added: 0, deleted: 0 } as result
recurse(result, A, B, 0, A.lines.length - 1, 0, B.lines.length - 1)
postprocess(result)
return result
}
type result = {
lines: line[]
added: number
deleted: number
}
type line = { line: string; token?: string } & position
type position = { a: number; b: number }
type operand = { lines: string[] }
|