-
-
Notifications
You must be signed in to change notification settings - Fork 35.5k
Expand file tree
/
Copy pathmyers_diff.js
More file actions
357 lines (297 loc) Β· 10.8 KB
/
myers_diff.js
File metadata and controls
357 lines (297 loc) Β· 10.8 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
'use strict';
const {
ArrayPrototypePush,
ArrayPrototypeSlice,
Int32Array,
MathFloor,
MathMax,
MathMin,
MathRound,
RegExpPrototypeExec,
StringPrototypeEndsWith,
} = primordials;
const {
codes: {
ERR_OUT_OF_RANGE,
},
} = require('internal/errors');
const colors = require('internal/util/colors');
const kChunkSize = 512;
const kNopLinesToCollapse = 5;
// Lines that are just structural characters make poor alignment anchors
// because they appear many times and don't uniquely identify a position.
const kTrivialLinePattern = /^\s*[{}[\],]+\s*$/;
const kOperations = {
DELETE: -1,
NOP: 0,
INSERT: 1,
};
function areLinesEqual(actual, expected, checkCommaDisparity) {
if (actual === expected) {
return true;
}
if (checkCommaDisparity) {
return (actual + ',') === expected || actual === (expected + ',');
}
return false;
}
function myersDiffInternal(actual, expected, checkCommaDisparity) {
const actualLength = actual.length;
const expectedLength = expected.length;
const max = actualLength + expectedLength;
const v = new Int32Array(2 * max + 1);
const trace = [];
for (let diffLevel = 0; diffLevel <= max; diffLevel++) {
ArrayPrototypePush(trace, new Int32Array(v)); // Clone the current state of `v`
for (let diagonalIndex = -diffLevel; diagonalIndex <= diffLevel; diagonalIndex += 2) {
const offset = diagonalIndex + max;
const previousOffset = v[offset - 1];
const nextOffset = v[offset + 1];
let x = diagonalIndex === -diffLevel || (diagonalIndex !== diffLevel && previousOffset < nextOffset) ?
nextOffset :
previousOffset + 1;
let y = x - diagonalIndex;
while (
x < actualLength &&
y < expectedLength &&
areLinesEqual(actual[x], expected[y], checkCommaDisparity)
) {
x++;
y++;
}
v[offset] = x;
if (x >= actualLength && y >= expectedLength) {
return backtrack(trace, actual, expected, checkCommaDisparity);
}
}
}
}
function backtrack(trace, actual, expected, checkCommaDisparity) {
const actualLength = actual.length;
const expectedLength = expected.length;
const max = actualLength + expectedLength;
let x = actualLength;
let y = expectedLength;
const result = [];
for (let diffLevel = trace.length - 1; diffLevel >= 0; diffLevel--) {
const v = trace[diffLevel];
const diagonalIndex = x - y;
const offset = diagonalIndex + max;
let prevDiagonalIndex;
if (
diagonalIndex === -diffLevel ||
(diagonalIndex !== diffLevel && v[offset - 1] < v[offset + 1])
) {
prevDiagonalIndex = diagonalIndex + 1;
} else {
prevDiagonalIndex = diagonalIndex - 1;
}
const prevX = v[prevDiagonalIndex + max];
const prevY = prevX - prevDiagonalIndex;
while (x > prevX && y > prevY) {
const actualItem = actual[x - 1];
const value = checkCommaDisparity && !StringPrototypeEndsWith(actualItem, ',') ? expected[y - 1] : actualItem;
ArrayPrototypePush(result, [ kOperations.NOP, value ]);
x--;
y--;
}
if (diffLevel > 0) {
if (x > prevX) {
ArrayPrototypePush(result, [ kOperations.INSERT, actual[--x] ]);
} else {
ArrayPrototypePush(result, [ kOperations.DELETE, expected[--y] ]);
}
}
}
return result;
}
function myersDiff(actual, expected, checkCommaDisparity = false) {
const actualLength = actual.length;
const expectedLength = expected.length;
const max = actualLength + expectedLength;
if (max > 2 ** 31 - 1) {
throw new ERR_OUT_OF_RANGE(
'myersDiff input size',
'< 2^31',
max,
);
}
// For small inputs, run the algorithm directly
if (actualLength <= kChunkSize && expectedLength <= kChunkSize) {
return myersDiffInternal(actual, expected, checkCommaDisparity);
}
const boundaries = findAlignedBoundaries(
actual, expected, checkCommaDisparity,
);
// Process chunks and concatenate results (last chunk first for reversed order)
const result = [];
for (let i = boundaries.length - 2; i >= 0; i--) {
const actualStart = boundaries[i].actualIdx;
const actualEnd = boundaries[i + 1].actualIdx;
const expectedStart = boundaries[i].expectedIdx;
const expectedEnd = boundaries[i + 1].expectedIdx;
const actualChunk = ArrayPrototypeSlice(actual, actualStart, actualEnd);
const expectedChunk = ArrayPrototypeSlice(expected, expectedStart, expectedEnd);
if (actualChunk.length === 0 && expectedChunk.length === 0) continue;
if (actualChunk.length === 0) {
for (let j = expectedChunk.length - 1; j >= 0; j--) {
ArrayPrototypePush(result, [kOperations.DELETE, expectedChunk[j]]);
}
continue;
}
if (expectedChunk.length === 0) {
for (let j = actualChunk.length - 1; j >= 0; j--) {
ArrayPrototypePush(result, [kOperations.INSERT, actualChunk[j]]);
}
continue;
}
const chunkDiff = myersDiffInternal(actualChunk, expectedChunk, checkCommaDisparity);
for (let j = 0; j < chunkDiff.length; j++) {
ArrayPrototypePush(result, chunkDiff[j]);
}
}
return result;
}
function findAlignedBoundaries(actual, expected, checkCommaDisparity) {
const actualLen = actual.length;
const expectedLen = expected.length;
const boundaries = [{ actualIdx: 0, expectedIdx: 0 }];
const searchRadius = kChunkSize / 2;
const numTargets = MathMax(
MathFloor((actualLen - 1) / kChunkSize),
1,
);
for (let i = 1; i <= numTargets; i++) {
const targetActual = MathMin(i * kChunkSize, actualLen);
if (targetActual >= actualLen) {
break;
}
const targetExpected = MathMin(
MathRound(targetActual * expectedLen / actualLen),
expectedLen - 1,
);
const prevBoundary = boundaries[boundaries.length - 1];
const anchor = findAnchorNear(
actual, expected, targetActual, targetExpected,
prevBoundary, searchRadius, checkCommaDisparity,
);
if (anchor !== undefined) {
ArrayPrototypePush(boundaries, anchor);
} else {
// Fallback: use proportional position, ensuring strictly increasing
const fallbackActual = MathMax(targetActual, prevBoundary.actualIdx + 1);
const fallbackExpected = MathMax(targetExpected, prevBoundary.expectedIdx + 1);
if (fallbackActual < actualLen && fallbackExpected < expectedLen) {
ArrayPrototypePush(boundaries, { actualIdx: fallbackActual, expectedIdx: fallbackExpected });
}
}
}
ArrayPrototypePush(boundaries, { actualIdx: actualLen, expectedIdx: expectedLen });
return boundaries;
}
// Search outward from targetActual and targetExpected for a non-trivial
// line that matches in both arrays, with adjacent context verification.
function findAnchorNear(actual, expected, targetActual, targetExpected,
prevBoundary, searchRadius, checkCommaDisparity) {
const actualLen = actual.length;
const expectedLen = expected.length;
for (let offset = 0; offset <= searchRadius; offset++) {
const candidates = offset === 0 ? [targetActual] : [targetActual + offset, targetActual - offset];
for (let i = 0; i < candidates.length; i++) {
const actualIdx = candidates[i];
if (actualIdx <= prevBoundary.actualIdx || actualIdx >= actualLen) {
continue;
}
const line = actual[actualIdx];
if (isTrivialLine(line)) {
continue;
}
const searchStart = MathMax(prevBoundary.expectedIdx + 1, targetExpected - searchRadius);
const searchEnd = MathMin(expectedLen - 1, targetExpected + searchRadius);
for (let j = 0; j <= searchRadius; j++) {
const offsets = j === 0 ? [0] : [j, -j];
for (let k = 0; k < offsets.length; k++) {
const expectedIdx = targetExpected + offsets[k];
if (expectedIdx < searchStart || expectedIdx > searchEnd || expectedIdx <= prevBoundary.expectedIdx) {
continue;
}
if (
areLinesEqual(line, expected[expectedIdx], checkCommaDisparity) &&
hasAdjacentMatch(actual, expected, actualIdx, expectedIdx, checkCommaDisparity)
) {
return { actualIdx, expectedIdx };
}
}
}
}
}
return undefined;
}
function hasAdjacentMatch(actual, expected, actualIdx, expectedIdx, checkCommaDisparity) {
if (actualIdx > 0 && expectedIdx > 0 &&
areLinesEqual(actual[actualIdx - 1], expected[expectedIdx - 1], checkCommaDisparity)) {
return true;
}
if (actualIdx < actual.length - 1 && expectedIdx < expected.length - 1 &&
areLinesEqual(actual[actualIdx + 1], expected[expectedIdx + 1], checkCommaDisparity)) {
return true;
}
return false;
}
function isTrivialLine(line) {
return RegExpPrototypeExec(kTrivialLinePattern, line) !== null;
}
function printSimpleMyersDiff(diff) {
let message = '';
for (let diffIdx = diff.length - 1; diffIdx >= 0; diffIdx--) {
const { 0: operation, 1: value } = diff[diffIdx];
let color = colors.white;
if (operation === kOperations.INSERT) {
color = colors.green;
} else if (operation === kOperations.DELETE) {
color = colors.red;
}
message += `${color}${value}${colors.white}`;
}
return `\n${message}`;
}
function printMyersDiff(diff, operator) {
let message = '';
let skipped = false;
let nopCount = 0;
for (let diffIdx = diff.length - 1; diffIdx >= 0; diffIdx--) {
const { 0: operation, 1: value } = diff[diffIdx];
const previousOperation = diffIdx < diff.length - 1 ? diff[diffIdx + 1][0] : null;
// Avoid grouping if only one line would have been grouped otherwise
if (previousOperation === kOperations.NOP && operation !== previousOperation) {
if (nopCount === kNopLinesToCollapse + 1) {
message += `${colors.white} ${diff[diffIdx + 1][1]}\n`;
} else if (nopCount === kNopLinesToCollapse + 2) {
message += `${colors.white} ${diff[diffIdx + 2][1]}\n`;
message += `${colors.white} ${diff[diffIdx + 1][1]}\n`;
} else if (nopCount >= kNopLinesToCollapse + 3) {
message += `${colors.blue}...${colors.white}\n`;
message += `${colors.white} ${diff[diffIdx + 1][1]}\n`;
skipped = true;
}
nopCount = 0;
}
if (operation === kOperations.INSERT) {
if (operator === 'partialDeepStrictEqual') {
message += `${colors.gray}${colors.hasColors ? ' ' : '+'} ${value}${colors.white}\n`;
} else {
message += `${colors.green}+${colors.white} ${value}\n`;
}
} else if (operation === kOperations.DELETE) {
message += `${colors.red}-${colors.white} ${value}\n`;
} else if (operation === kOperations.NOP) {
if (nopCount < kNopLinesToCollapse) {
message += `${colors.white} ${value}\n`;
}
nopCount++;
}
}
message = message.trimEnd();
return { message: `\n${message}`, skipped };
}
module.exports = { myersDiff, printMyersDiff, printSimpleMyersDiff };