Expand description
fc-diff — line diffing, Myers’ O(ND) algorithm.
§Why this is in Rust and what that buys
A diff is the one place in this system where the cost is superlinear in the input. Myers is O(ND) rather than the O(N*M) a dynamic-programming table would need, but on a 20,000-line file that is still ~400M comparisons in JavaScript’s worst case. In Rust the same walk is memory-safe, allocation-free on the hot path, and roughly an order of magnitude faster — which is what makes live diffing while a user types viable at all.
§The invariants, and why they are the whole contract
A diff that is subtly wrong is worse than no diff: a reviewer cannot tell a missing line from a mis-attributed one. Two properties are therefore asserted on every test case rather than described:
- Reconstruction. Dropping the
Addlines reproduces the original; dropping theRemovelines reproduces the revision. This single property catches every possible backtracking off-by-one at once. - Text preservation. No line’s text is altered, only its attribution.
§Line numbers are 1-based
What a human reads, and what git blame prints. The algorithm works in
0-based indices internally; the conversion is part of the public contract.
None means the line does not exist on that side, and callers must render
nothing rather than 0 — inventing a line number is how a reviewer is misled
about where a change landed.
Structs§
- Diff
Line - One line of output.
old_line/new_lineare 1-based, orNonewhen the line does not exist on that side. - Diff
Stats - Counts for a diff header.
Enums§
- Diff
Kind - How a line relates to the other side of the diff.
Functions§
- diff_
lines - Compute a line diff between two texts.
- diff_
stats - Counts for a diff header.
- js_
diff_ identical - Whether the two inputs are identical after line splitting.
- js_
diff_ kinds - The per-line kinds, as a flat
Int32Arrayof lengthlineCount. - js_
diff_ new_ lines - 1-based line numbers in the revision; -1 where the line was removed.
- js_
diff_ old_ lines - 1-based line numbers in the original; -1 where the line is new.
- js_
diff_ stats [added, removed]as a flat two-element array.- js_
diff_ text_ at - The text of line
index, or""when out of range. - js_
line_ count - Line count without materialising the text array.
- split_
lines - Split text into lines, without a phantom trailing empty line.