Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 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 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 | 5x 10x 5x 5x 3312x 3312x 3312x 2953x 2953x 2953x 2953x 2953x 2953x 2953x 2953x 15812x 15812x 15812x 4105x 3813x 3813x 3813x 3813x 3813x 3813x 3813x 292x 4105x 5791x 5791x 5791x 5916x 5916x 5916x 2953x 2953x 2953x 2953x 2953x 2953x 7325x 7325x 4185x 4185x 4185x 4185x 4185x 4185x 559x 537x 537x 537x 537x 537x 537x 22x 22x 22x 559x 6766x 2953x 2953x 2953x 6229x 6229x 6229x 2953x 5x 5x 5x 5x 5x 5x 2228x 2222x 2222x 2222x 2222x 2222x 2222x 2222x 2222x 2222x 2222x 2222x 2186x 1706x 1614x 892x 590x 5x 274x 274x 5x 5x 1233x 1233x 5449x 1232x 5x 10917x 2465x 2465x 6307x 982x 982x 982x 982x 982x 46x 46x 46x 46x 982x 982x 982x 982x 982x 142x 142x 142x 142x 132x 132x 77x 77x 77x 77x 982x 19x 2x 2x 19x 19x 4x 4x 6307x 2465x 5x 1232x 1232x 1233x 1233x 1x 1232x 2698x 5x 3029x 611x 611x 611x 611x 611x 611x 611x 611x 611x 9101x 1033x 1033x 1033x 1033x 1033x 1033x 8068x 4210x 8068x 643x 643x 643x 643x 643x 643x 643x 643x 643x 643x 643x 643x 9101x 611x 611x 611x 611x 861x 564x 564x 564x 564x 564x 507x 8x 8x 8x 8x 57x 11x 11x 11x 11x 564x 861x 611x 5x 4686x 4686x 4686x 4686x 4686x 18904x 92569x 92569x 92569x 18904x 14218x 14218x 20940x 20408x 14218x 15353x 15353x 7969x 7969x 7969x 7969x 7969x 7969x 8543x 15353x 15353x 10157x 10157x 10157x 10157x 10157x 10157x 10157x 8203x 8203x 14218x 2063x 2063x 2063x 2063x 2063x 2063x 5x 4362x 96520x 3888x 5x 5x 1959x 1959x 1959x 1959x 1959x 1959x 1959x 54445x 54445x 54445x 20537x 20537x 20537x 1959x 1869x 1869x 1869x 2343x 2343x 2343x 2343x 2343x 1869x 1869x 1869x 1869x 34968x 10262x 10262x 34968x 10744x 10744x 34968x 33099x 33099x 33099x 1869x | import {LINE_PATCH_OP_TYPE, type LinePatch} from './line';
import {normalize, overlap, PATCH_OP_TYPE, type Patch, type PatchOperation, sfx} from './str';
/**
* Cost model for {@link coarsen}. Costs are unit-less, only ratios matter.
*/
export interface CoarsenModel {
/** Fixed cost of an equality operation. */
eql: number;
/** Fixed cost of a delete operation. */
del: number;
/** Fixed cost of an insert operation. */
ins: number;
/** Cost of duplicating one equality character into the edit payloads. */
unit: number;
}
/**
* Trades edit volume for fewer operations. `opCost` is the per-operation
* overhead expressed in characters of edit volume; with the default, an
* equality flanked by full edit pairs is absorbed below 6 characters, by a
* single edit on each side below 4, by one side only below 2.
*/
export const opCountModel = (opCost: number = 4): CoarsenModel => ({eql: opCost, del: opCost, ins: opCost, unit: 2});
/**
* Approximates serialized patch size: each op costs `opOverhead` bytes of
* instruction framing, each absorbed equality character costs one inserted
* payload byte. Exact for latin-1 `bin` patches, an approximation for
* multi-byte UTF-8 text.
*/
export const byteSizeModel = (opOverhead: number = 2): CoarsenModel => ({
eql: opOverhead,
del: opOverhead,
ins: opOverhead,
unit: 1,
});
/**
* Coarsens a patch: absorbs equalities which cost more to keep than to
* duplicate into the surrounding edits, producing fewer (or cheaper, per the
* model) operations at the expense of a larger edit volume. An equality `e`
* is absorbed when
*
* ```
* eql + del * (delBefore + delAfter - 1) + ins * (insBefore + insAfter - 1) > unit * e.length
* ```
*
* where the flanking terms are 0/1 flags. Reconstruction is preserved:
* `src()` and `dst()` of the result equal those of the input. The result is
* normalized, with each edit run canonicalized to a single DEL-then-INS pair.
*
* @param patch The patch to coarsen.
* @param model Cost model, defaults to {@link opCountModel}.
* @returns A new coarsened patch.
*/
export const coarsen = (patch: Patch, model: CoarsenModel = opCountModel()): Patch => {
patch = normalize(patch);
const length = patch.length;
if (length < 2) return patch;
// Alternating structure: eqs[i] precedes the edit run (dels[i], inss[i]).
const eqs: string[] = [];
const dels: string[] = [];
const inss: string[] = [];
let eq = '';
let del = '';
let ins = '';
let inRun = false;
for (let i = 0; i < length; i++) {
const op = patch[i];
const txt = op[1];
switch (op[0]) {
case PATCH_OP_TYPE.EQL:
if (inRun) {
eqs.push(eq);
dels.push(del);
inss.push(ins);
eq = txt;
del = '';
ins = '';
inRun = false;
} else eq += txt;
break;
case PATCH_OP_TYPE.DEL:
del += txt;
inRun = true;
break;
case PATCH_OP_TYPE.INS:
ins += txt;
inRun = true;
break;
}
}
eqs.push(eq);
dels.push(del);
inss.push(ins);
const {eql: cEql, del: cDel, ins: cIns, unit} = model;
let i = 0;
while (i < eqs.length) {
const e = eqs[i];
if (e) {
const delB = dels[i] ? 1 : 0;
const insB = inss[i] ? 1 : 0;
const delA = i > 0 && dels[i - 1] ? 1 : 0;
const insA = i > 0 && inss[i - 1] ? 1 : 0;
const benefit = cEql + cDel * (delA + delB - 1) + cIns * (insA + insB - 1) - unit * e.length;
if (benefit > 0) {
if (i > 0) {
dels[i - 1] += e + dels[i];
inss[i - 1] += e + inss[i];
eqs.splice(i, 1);
dels.splice(i, 1);
inss.splice(i, 1);
// The previous equality's neighborhood changed, re-evaluate it.
i--;
} else {
dels[0] = e + dels[0];
inss[0] = e + inss[0];
eqs[0] = '';
}
continue;
}
}
i++;
}
const result: Patch = [];
const count = eqs.length;
for (let j = 0; j < count; j++) {
if (eqs[j]) result.push([PATCH_OP_TYPE.EQL, eqs[j]]);
if (dels[j]) result.push([PATCH_OP_TYPE.DEL, dels[j]]);
if (inss[j]) result.push([PATCH_OP_TYPE.INS, inss[j]]);
}
return result;
};
const wordRegex = /[\p{L}\p{N}\p{M}]/u;
const whitespaceRegex = /\s/;
const linebreakRegex = /[\r\n\u2028\u2029]/;
const blanklineEndRegex = /\n\r?\n$/;
const blanklineStartRegex = /^\r?\n\r?\n/;
/**
* Scores the seam between the end of `left` and the start of `right`; higher is
* more human-meaningful. Port of diff-match-patch's semantic score, with
* Unicode-aware (`\p{L}`) word detection instead of ASCII-only. A surrogate
* half scores as non-word, same as the whole code point it belongs to.
*/
const seamScore = (left: string, right: string): number => {
if (!left || !right) return 6;
const c1 = left.charAt(left.length - 1);
const c2 = right.charAt(0);
const nonWord1 = !wordRegex.test(c1);
const nonWord2 = !wordRegex.test(c2);
const ws1 = nonWord1 && whitespaceRegex.test(c1);
const ws2 = nonWord2 && whitespaceRegex.test(c2);
const lineBreak1 = ws1 && linebreakRegex.test(c1);
const lineBreak2 = ws2 && linebreakRegex.test(c2);
const blankLine1 = lineBreak1 && blanklineEndRegex.test(left);
const blankLine2 = lineBreak2 && blanklineStartRegex.test(right);
if (blankLine1 || blankLine2) return 5;
if (lineBreak1 || lineBreak2) return 4;
if (nonWord1 && !ws1 && ws2) return 3;
if (ws1 || ws2) return 2;
if (nonWord1 || nonWord2) return 1;
return 0;
};
// For well-formed content a boundary splits a surrogate pair exactly when the
// preceding text ends on a high surrogate (its low half sits after the seam).
const endsWithHighSurrogate = (str: string): boolean => {
const code = str.charCodeAt(str.length - 1);
return code >= 0xd800 && code <= 0xdbff;
};
const ALIGN_ROUNDS = 4;
const samePatch = (a: Patch, b: Patch): boolean => {
const length = a.length;
if (length !== b.length) return false;
for (let i = 0; i < length; i++) Iif (a[i][0] !== b[i][0] || a[i][1] !== b[i][1]) return false;
return true;
};
const alignOnce = (patch: Patch): Patch => {
const diff: Patch = normalize(patch).map((op) => [op[0], op[1]] as PatchOperation);
let pointer = 1;
while (pointer < diff.length - 1) {
if (diff[pointer - 1][0] === PATCH_OP_TYPE.EQL && diff[pointer + 1][0] === PATCH_OP_TYPE.EQL) {
let equality1 = diff[pointer - 1][1];
let edit = diff[pointer][1];
let equality2 = diff[pointer + 1][1];
// Shift the edit as far left as possible.
const commonOffset = sfx(equality1, edit);
if (commonOffset) {
const common = edit.slice(edit.length - commonOffset);
equality1 = equality1.slice(0, equality1.length - commonOffset);
edit = common + edit.slice(0, edit.length - commonOffset);
equality2 = common + equality2;
}
// Step right, keeping the best-scoring pair-safe position.
let bestEquality1 = equality1;
let bestEdit = edit;
let bestEquality2 = equality2;
let bestScore = seamScore(equality1, edit) + seamScore(edit, equality2);
while (equality2 && edit.charCodeAt(0) === equality2.charCodeAt(0)) {
equality1 += edit[0];
edit = edit.slice(1) + equality2[0];
equality2 = equality2.slice(1);
if (endsWithHighSurrogate(equality1) || endsWithHighSurrogate(edit)) continue;
const score = seamScore(equality1, edit) + seamScore(edit, equality2);
// `>=` prefers trailing over leading whitespace on the edit.
if (score >= bestScore) {
bestScore = score;
bestEquality1 = equality1;
bestEdit = edit;
bestEquality2 = equality2;
}
}
if (diff[pointer - 1][1] !== bestEquality1) {
if (bestEquality1) diff[pointer - 1][1] = bestEquality1;
else {
diff.splice(pointer - 1, 1);
pointer--;
}
diff[pointer][1] = bestEdit;
if (bestEquality2) diff[pointer + 1][1] = bestEquality2;
else {
diff.splice(pointer + 1, 1);
pointer--;
}
}
}
pointer++;
}
return canonicalMerge(diff);
};
export const align = (patch: Patch): Patch => {
let result = alignOnce(patch);
for (let round = 1; round < ALIGN_ROUNDS; round++) {
const next = alignOnce(result);
if (samePatch(next, result)) break;
result = next;
}
return result;
};
// Canonicalizes edit runs (all DEL text before all INS text, adjacent same-type
// merged, empties dropped) without changing which characters are edited.
const canonicalMerge = (patch: Patch): Patch => coarsen(patch, {eql: 0, del: 0, ins: 0, unit: 1});
/**
* Coarsens a patch toward human-meaningful hunks (diff-match-patch's semantic
* cleanup): eliminates an equality no larger than the edits on both sides by
* folding it into a substitution, slides the survivors onto word/line
* boundaries via {@link align}, then factors an overlap between an adjacent
* deletion and insertion into a shared equality when it covers at least half of
* either. Reconstruction is preserved; output is normalized. Coarser and more
* readable than the input, but no longer minimal.
*
* @param patch The patch to clean up.
* @returns A new, semantically cleaned patch.
*/
export const semantic = (patch: Patch): Patch => {
let diff: Patch = normalize(patch).map((op) => [op[0], op[1]] as PatchOperation);
const equalities: number[] = [];
let lastEquality: string | null = null;
let insertions1 = 0;
let deletions1 = 0;
let insertions2 = 0;
let deletions2 = 0;
let pointer = 0;
let changes = false;
while (pointer < diff.length) {
if (diff[pointer][0] === PATCH_OP_TYPE.EQL) {
equalities.push(pointer);
insertions1 = insertions2;
deletions1 = deletions2;
insertions2 = 0;
deletions2 = 0;
lastEquality = diff[pointer][1];
} else {
if (diff[pointer][0] === PATCH_OP_TYPE.INS) insertions2 += diff[pointer][1].length;
else deletions2 += diff[pointer][1].length;
if (
lastEquality !== null &&
lastEquality.length <= Math.max(insertions1, deletions1) &&
lastEquality.length <= Math.max(insertions2, deletions2)
) {
const index = equalities[equalities.length - 1];
diff.splice(index, 0, [PATCH_OP_TYPE.DEL, lastEquality]);
diff[index + 1] = [PATCH_OP_TYPE.INS, diff[index + 1][1]];
equalities.pop();
equalities.pop();
pointer = equalities.length > 0 ? equalities[equalities.length - 1] : -1;
insertions1 = 0;
deletions1 = 0;
insertions2 = 0;
deletions2 = 0;
lastEquality = null;
changes = true;
}
}
pointer++;
}
if (changes) diff = canonicalMerge(diff);
diff = align(diff);
pointer = 1;
while (pointer < diff.length) {
if (diff[pointer - 1][0] === PATCH_OP_TYPE.DEL && diff[pointer][0] === PATCH_OP_TYPE.INS) {
const deletion = diff[pointer - 1][1];
const insertion = diff[pointer][1];
const overlap1 = overlap(deletion, insertion);
const overlap2 = overlap(insertion, deletion);
if (overlap1 >= overlap2) {
if (overlap1 * 2 >= deletion.length || overlap1 * 2 >= insertion.length) {
diff.splice(pointer, 0, [PATCH_OP_TYPE.EQL, insertion.slice(0, overlap1)]);
diff[pointer - 1] = [PATCH_OP_TYPE.DEL, deletion.slice(0, deletion.length - overlap1)];
diff[pointer + 1] = [PATCH_OP_TYPE.INS, insertion.slice(overlap1)];
pointer++;
}
} else if (overlap2 * 2 >= deletion.length || overlap2 * 2 >= insertion.length) {
diff.splice(pointer, 0, [PATCH_OP_TYPE.EQL, deletion.slice(0, overlap2)]);
diff[pointer - 1] = [PATCH_OP_TYPE.INS, insertion.slice(0, insertion.length - overlap2)];
diff[pointer + 1] = [PATCH_OP_TYPE.DEL, deletion.slice(overlap2)];
pointer++;
}
pointer++;
}
pointer++;
}
return normalize(diff);
};
const shiftBoundaries = (lns: string[], changed: Uint8Array, otherChanged: Uint8Array): void => {
const iEnd = lns.length;
const otherEnd = otherChanged.length;
let i = 0;
let j = 0;
while (true) {
// Scan forward to the next run of changes, tracking the matching point in
// the other file: one unchanged line there for each unchanged line here.
while (i < iEnd && !changed[i]) {
while (j < otherEnd && otherChanged[j]) j++;
j++;
i++;
}
if (i >= iEnd) break;
let start = i;
i++;
while (i < iEnd && changed[i]) i++;
while (j < otherEnd && otherChanged[j]) j++;
let runLength: number;
let corresponding: number;
do {
runLength = i - start;
// Back, merging with the run before.
while (start > 0 && lns[start - 1] === lns[i - 1]) {
start--;
changed[start] = 1;
i--;
changed[i] = 0;
while (start > 0 && changed[start - 1]) start--;
j--;
while (j >= 0 && otherChanged[j]) j--;
}
// `iEnd` means no point where the run lines up with the other file.
corresponding = j > 0 && otherChanged[j - 1] ? i : iEnd;
// Forward, absorbing the run after. Absorbing is load-bearing: writing
// the flag onto an already-changed line would be a no-op while the clear
// at `start` still fired, dropping a changed line and breaking
// reconstruction.
while (i !== iEnd && lns[start] === lns[i]) {
changed[start] = 0;
start++;
changed[i] = 1;
i++;
while (i < iEnd && changed[i]) i++;
j++;
while (j < otherEnd && otherChanged[j]) {
corresponding = i;
j++;
}
}
} while (runLength !== i - start);
// Pull the merged run back to where it lines up with the other file.
while (corresponding < i) {
start--;
changed[start] = 1;
i--;
changed[i] = 0;
j--;
while (j >= 0 && otherChanged[j]) j--;
}
}
};
const sameFlags = (a: Uint8Array, b: Uint8Array): boolean => {
const length = a.length;
for (let i = 0; i < length; i++) if (a[i] !== b[i]) return false;
return true;
};
/**
* Rounds of {@link shiftBoundaries} over both files. GNU runs one and never
* looks at its own output again, so it does not care that one round is not a
* fixed point: its closing back-off can free room an earlier run could still
* have used, and the pass never revisits. Repeating until the flags stop
* changing is what makes this idempotent, and it agrees with GNU on marginally
* more inputs than a single round does.
*
* The comparison has to be on the flags, not on "did a write happen". The
* backward loop routinely moves a run back and the forward loop returns it to
* exactly where it started, so a write-happened flag never clears and the cap
* becomes the ordinary exit path rather than a limit.
*
* Measured over 260k pairs of 20 to 640 lines, mixing `lines.diff` output with
* arbitrary valid alignments: two rounds settle all but four, and those four
* need three and are all at the longest length. Rebuilt with a cap of three and
* run over another 240k, none needed a fourth. Extra rounds are a long-input
* phenomenon and are not confined to `lines.diff` output, so the cap is set well
* above the observed maximum. Hitting it would leave a patch that still
* reconstructs `dst` with the same edit volume but is not necessarily a fixed
* point; the suite asserts convergence on the input class that gets closest.
*/
const ROUNDS = 8;
/**
* Canonicalizes where a line-level patch puts its hunks. A minimal edit script
* is rarely unique — repeated lines make several equally short scripts possible
* — so instead of emitting whichever one the traceback happened to reach, this
* moves every run of changed lines to the position GNU `diff` would choose:
* as far down as it will go, merged with the runs it meets on the way, and
* lined up with the corresponding run in the other file. Hunks then land where
* a reader expects them, and byte-for-byte agreement with GNU goes up sharply.
*
* @param src Source lines the patch was computed from.
* @param dst Destination lines the patch was computed from.
* @param patch The line-level patch to canonicalize.
* @returns A new patch with the same reconstruction and GNU's hunk placement.
*/
export const shiftDown = (src: string[], dst: string[], patch: LinePatch): LinePatch => {
const srcLength = src.length;
const dstLength = dst.length;
const changedSrc = new Uint8Array(srcLength);
const changedDst = new Uint8Array(dstLength);
const length = patch.length;
let changes = false;
for (let k = 0; k < length; k++) {
const op = patch[k];
const type = op[0];
if (type === LINE_PATCH_OP_TYPE.EQL) continue;
// The index of the other side is an anchor, not a covered line.
if (type !== LINE_PATCH_OP_TYPE.INS) changedSrc[op[1]] = 1;
if (type !== LINE_PATCH_OP_TYPE.DEL) changedDst[op[2]] = 1;
changes = true;
}
if (!changes) return patch.map((op) => [op[0], op[1], op[2]]);
const beforeSrc = new Uint8Array(srcLength);
const beforeDst = new Uint8Array(dstLength);
for (let round = 0; round < ROUNDS; round++) {
beforeSrc.set(changedSrc);
beforeDst.set(changedDst);
shiftBoundaries(src, changedSrc, changedDst);
shiftBoundaries(dst, changedDst, changedSrc);
if (sameFlags(beforeSrc, changedSrc) && sameFlags(beforeDst, changedDst)) break;
}
const result: LinePatch = [];
let i = 0;
let j = 0;
while (true) {
while (i < srcLength && changedSrc[i]) {
result.push([LINE_PATCH_OP_TYPE.DEL, i, j - 1]);
i++;
}
while (j < dstLength && changedDst[j]) {
result.push([LINE_PATCH_OP_TYPE.INS, i - 1, j]);
j++;
}
if (i >= srcLength || j >= dstLength) break;
result.push([LINE_PATCH_OP_TYPE.EQL, i, j]);
i++;
j++;
}
return result;
};
|