Capstone — Quarry's Diff Engine
In short: a line-by-line diff is the LCS table from lesson 2, run over
whole lines instead of letters, with the walk-back from lesson 4 turning the
table into kept/removed/inserted output lines in file order. Splitting text
into lines is part of the algorithm — splitlines() and split("\n") disagree
about a trailing newline, and that difference shows up as a phantom diff line
if you're not careful.

Quarry now has four pieces of version history. The reorderer knows which blocks can stay put. The survival score says how much of a document lived through an edit. The typo checker measures how far apart two strings are. The edit script turns a table into keep, delete, insert. What Quarry still can't do is the thing a person actually clicks on: show me what changed between these two revisions, as text, line by line.
That's a diff. You'll build it from the table you already know, run it on real documents, and then, in the exercises, apply it, reverse it, colour it word by word and cut it into hunks.
What the engine returns
Two revisions come in as whole strings. The engine:
- splits each into lines,
- fills the LCS table over the lines,
- walks back from the corner with the tie rule from lesson 4 (a match goes
diagonal; otherwise left when
table[i][j-1] >= table[i-1][j], else up), - writes each step as one line of output:
" "+ line for a kept line,"-"+ line for a deleted one and"+"+ line for an inserted one, - and counts them.
Here's the table for a small settings file. Old version down the side, new version across the top:
The same LCS table as lesson 2, over whole lines. The path back from the corner is the diff.
Row 0 and column 0 compare against an empty file. 5 by 6 table. At row 0, column 0 — value 0. 10 of 30 cells filled.
Reversed, that walk is the diff:
[store]
-shards=2
+shards=4
cache=on
+backup=daily
log=warnFor m and n lines, filling the table is O(m · n) time, one constant-work
comparison per cell, and keeping it costs O(m · n) space — the walk back
afterwards only adds O(m + n) on top.