Something went wrong. Try again.
A monorepo for writing specs and other serious text on the AT Protocol.
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115// The smallest edits that turn one string into another, by word. A replace// op that rewrites a sentence then shows as the words that go and the words// that come, not as the whole sentence struck through and typed again; and// a rewrite that changes nothing (or only quote marks and spacing) shows as// nothing at all.
/** A changed run: `a` offsets into the old string, `b` offsets into the new. */export type Run = { a0: number; a1: number; b0: number; b1: number };
// words (with apostrophes and hyphens), numbers, whitespace runs, anything else one character at a timeconst TOKEN = /\p{L}[\p{L}\p{M}\p{N}'’-]*|\p{N}+|\s+|./gsu;
/** Too many tokens to align word by word: the whole span is one run. */const CELLS = 4_000_000;
export function tokens(s: string): string[] { return s.match(TOKEN) ?? [];}
/** Two tokens count as the same when they differ only in quote marks or in what whitespace they are. */function norm(t: string): string { if (/^\s+$/.test(t)) return ' '; return t.replace(/[‘’‚]/g, "'").replace(/[“”„]/g, '"');}
/** * The runs that differ between `a` and `b`, in order, by word; none when * they are the same (after `norm`). Adjacent runs separated only by * whitespace merge, so "a very big dog" → "a tiny dog" is one change. */export function diffRuns(a: string, b: string): Run[] { const ta = tokens(a); const tb = tokens(b); const na = ta.map(norm); const nb = tb.map(norm); // the common head and tail cost nothing let head = 0; while (head < na.length && head < nb.length && na[head] === nb[head]) head++; let tail = 0; while (tail < na.length - head && tail < nb.length - head && na[na.length - 1 - tail] === nb[nb.length - 1 - tail]) tail++; const A = na.slice(head, na.length - tail); const B = nb.slice(head, nb.length - tail); if (!A.length && !B.length) return []; const offA = offsets(ta); const offB = offsets(tb); if (A.length * B.length > CELLS) return [{ a0: offA[head]!, a1: offA[na.length - tail]!, b0: offB[head]!, b1: offB[nb.length - tail]! }]; // what each middle token is: kept, or part of a change const keptA = new Array<boolean>(A.length).fill(false); const keptB = new Array<boolean>(B.length).fill(false); for (const [i, j] of lcs(A, B)) { keptA[i] = true; keptB[j] = true; } // walk both, gathering changed stretches const runs: Run[] = []; let i = 0; let j = 0; while (i < A.length || j < B.length) { if (i < A.length && j < B.length && keptA[i] && keptB[j]) { i++; j++; continue; } const i0 = i; const j0 = j; while (i < A.length && !keptA[i]) i++; while (j < B.length && !keptB[j]) j++; runs.push({ a0: head + i0, a1: head + i, b0: head + j0, b1: head + j }); } // merge runs with only whitespace between them const merged: Run[] = []; for (const r of runs) { const prev = merged[merged.length - 1]; const gapA = prev ? na.slice(prev.a1, r.a0) : []; const gapB = prev ? nb.slice(prev.b1, r.b0) : []; if (prev && gapA.every((t) => t === ' ') && gapB.every((t) => t === ' ') && gapA.length === gapB.length) { prev.a1 = r.a1; prev.b1 = r.b1; } else merged.push({ ...r }); } // token indices become character offsets return merged.map((r) => ({ a0: offA[r.a0]!, a1: offA[r.a1]!, b0: offB[r.b0]!, b1: offB[r.b1]! }));}
function offsets(toks: string[]): number[] { const out = [0]; for (const t of toks) out.push(out[out.length - 1]! + t.length); return out;}
/** The longest common subsequence of two token lists, as index pairs. */function lcs(A: string[], B: string[]): [number, number][] { const n = A.length; const m = B.length; const w = m + 1; const L = new Uint16Array((n + 1) * w); for (let i = n - 1; i >= 0; i--) { for (let j = m - 1; j >= 0; j--) { L[i * w + j] = A[i] === B[j] ? L[(i + 1) * w + j + 1]! + 1 : Math.max(L[(i + 1) * w + j]!, L[i * w + j + 1]!); } } const pairs: [number, number][] = []; let i = 0; let j = 0; while (i < n && j < m) { if (A[i] === B[j]) { pairs.push([i, j]); i++; j++; } else if (L[(i + 1) * w + j]! >= L[i * w + j + 1]!) i++; else j++; } return pairs;}