MCPcopy Index your code
hub / github.com/nodejs/node / myersDiff

Function myersDiff

lib/internal/assert/myers_diff.js:34–78  ·  view source on GitHub ↗
(actual, expected, checkCommaDisparity = false)

Source from the content-addressed store, hash-verified

32}
33
34function myersDiff(actual, expected, checkCommaDisparity = false) {
35 const actualLength = actual.length;
36 const expectedLength = expected.length;
37 const max = actualLength + expectedLength;
38
39 if (max > 2 ** 31 - 1) {
40 throw new ERR_OUT_OF_RANGE(
41 'myersDiff input size',
42 '< 2^31',
43 max,
44 );
45 }
46
47 const v = new Int32Array(2 * max + 1);
48 const trace = [];
49
50 for (let diffLevel = 0; diffLevel <= max; diffLevel++) {
51 ArrayPrototypePush(trace, new Int32Array(v)); // Clone the current state of `v`
52
53 for (let diagonalIndex = -diffLevel; diagonalIndex <= diffLevel; diagonalIndex += 2) {
54 const offset = diagonalIndex + max;
55 const previousOffset = v[offset - 1];
56 const nextOffset = v[offset + 1];
57 let x = diagonalIndex === -diffLevel || (diagonalIndex !== diffLevel && previousOffset < nextOffset) ?
58 nextOffset :
59 previousOffset + 1;
60 let y = x - diagonalIndex;
61
62 while (
63 x < actualLength &&
64 y < expectedLength &&
65 areLinesEqual(actual[x], expected[y], checkCommaDisparity)
66 ) {
67 x++;
68 y++;
69 }
70
71 v[offset] = x;
72
73 if (x >= actualLength && y >= expectedLength) {
74 return backtrack(trace, actual, expected, checkCommaDisparity);
75 }
76 }
77 }
78}
79
80function backtrack(trace, actual, expected, checkCommaDisparity) {
81 const actualLength = actual.length;

Callers 4

getColoredMyersDiffFunction · 0.85
createErrDiffFunction · 0.85
diffFunction · 0.85

Calls 2

areLinesEqualFunction · 0.85
backtrackFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…