Lines 15–21 at 318 (were 15–21 at 8)
1616/** Join lines into file text. A non-empty file always ends with "\n". */ 1717export function joinLines(lines: readonly string[]): string { 18− return lines.length === 0 ? "" : lines.join("\n") + "\n";
18+ return lines.length === 0 ? "" : `${lines.join("\n")}\n`; 2121/** Normalize, then make a non-empty text end with "\n" (how files are projected). */ Lines 43–171 at 318 (were 43–54 at 8)
4343 * file's length times MAX_COST instead of its length squared. 4545export function diffLines(a: readonly string[], b: readonly string[]): Hunk[] { 46− const matches = commonLines(a, b);
47− const hunks: Hunk[] = [];
46+ return hunksFromMatches(a.length, b, commonLines(a, b)); 49+/** Diff a single-pass old-line source without retaining its strings. */ 50+export function diffLineStream(a: Iterable<string>, b: readonly string[]): Hunk[] { 51+ const ids = new Map<string, number>(); 52+ const next = b.map((line) => { 53+ let id = ids.get(line); 54+ if (id === undefined) { 60+ const previous: number[] = []; 61+ for (const line of a) previous.push(ids.get(line) ?? -1); 62+ return hunksFromMatches(previous.length, b, commonLines(previous, next)); 65+/** Compare replayable old-line chunks without assembling historical strings. */ 66+export function diffChunkedLines(a: Iterable<() => Iterable<string>>, b: readonly string[]): Hunk[] { 67+ const keys = new Map<string, number>(); 68+ const buckets = new Map<string, number[]>(); 69+ const next = b.map((line, index) => { 70+ let id = keys.get(line); 71+ if (id === undefined) { 74+ const key = chunkKey([line]); 75+ const bucket = buckets.get(key) ?? []; 77+ buckets.set(key, bucket); 81+ const previous: number[] = []; 82+ for (const chunks of a) { 83+ const iterator = chunks()[Symbol.iterator](); 85+ const aligned = b[previous.length]; 86+ let alignedOffset = 0; 87+ let alignedEqual = aligned !== undefined; 89+ const first = iterator.next(); 91+ previous.push(keys.get("") ?? -1); 94+ const second = iterator.next(); 96+ previous.push(keys.get(first.value) ?? -1); 101+ function* remaining() { 104+ for (let next = iterator.next(); !next.done; next = iterator.next()) yield next.value; 106+ for (const chunk of remaining()) { 107+ if (alignedEqual && !aligned!.startsWith(chunk, alignedOffset)) alignedEqual = false; 108+ alignedOffset += chunk.length; 114+ iterator.return?.(); 116+ // Same-position unchanged lines are common. Confirm them during hashing, 117+ // retaining exact comparison and the collision-safe fallback for moved lines. 118+ if (alignedEqual && alignedOffset === aligned!.length) { 119+ previous.push(next[previous.length]!); 122+ const candidates = buckets.get(key); 124+ for (const candidate of candidates ?? []) { 127+ for (const chunk of chunks()) { 128+ if (!b[candidate]!.startsWith(chunk, offset)) equal = false; 129+ offset += chunk.length; 131+ if (equal && offset === b[candidate]!.length) { 136+ previous.push(match); 138+ return hunksFromMatches(previous.length, b, commonLines(previous, next)); 141+function chunkKey(chunks: Iterable<string>): string { 142+ let hash = 2166136261; 144+ for (const chunk of chunks) { 145+ length += chunk.length; 146+ for (let i = 0; i < chunk.length; i++) hash = Math.imul(hash ^ chunk.charCodeAt(i), 16777619) >>> 0; 148+ return `${length}:${hash}`; 151+/** The existing diff algorithm over exact line symbols, without retaining text. */ 152+export function diffLineSymbols( 153+ a: readonly number[], 154+ b: readonly number[], 155+): { start: number; end: number; lines: number[] }[] { 156+ return hunksFromMatches(a.length, b, commonLines(a, b)); 159+function hunksFromMatches<T>( 162+ matches: readonly number[], 163+): { start: number; end: number; lines: T[] }[] { 164+ const hunks: { start: number; end: number; lines: T[] }[] = []; 50167 for (let m = 0; m <= matches.length; m += 2) { 51− const mi = m < matches.length ? matches[m]! : a.length;
168+ const mi = m < matches.length ? matches[m]! : length; 52169 const mj = m < matches.length ? matches[m + 1]! : b.length; 53170 if (mi > i || mj > j) hunks.push({ start: i, end: mi, lines: b.slice(j, mj) }); Lines 178–184 at 318 (were 61–67 at 8)
61178 * The matched pairs of a longest common subsequence of `a` and `b`, flattened 62179 * as [i0, j0, i1, j1, ...] in increasing order. 64−function commonLines(a: readonly string[], b: readonly string[]): number[] {
181+function commonLines<T>(a: readonly T[], b: readonly T[]): number[] { 65182 // Common prefix and suffix match directly. 67184 while (pre < a.length && pre < b.length && a[pre] === b[pre]) pre++; Lines 189–196 at 318 (were 72–79 at 8)
73190 // A line that occurs on only one side can never be matched; leave it out of 74191 // the search (this also makes wholly different texts instant). 75− const ids = new Map<string, number>();
76− const intern = (s: string): number => {
192+ const ids = new Map<T, number>(); 193+ const intern = (s: T): number => { 77194 let id = ids.get(s); 78195 if (id === undefined) {