A text diff that reads only as far as it must, ported from my C# competition entry with its bugs fixed.
Live at iguigova.github.io/fiff: it runs the port in your browser, on the original's test files or any text, and checks every answer it gives.
FIFF reads two files in chunks until they differ, then token by token until a token of one is found among the other's. A difference ends there, so the diff never looks past the end of a change.
original/is FIFFConsole 0.2 in C#, unchanged, from theFIFFConsolebranch ofsnippets_csate9d8cb1: its source, documentation and test files. Its CodeProject article, whichoriginal/READMElinks, is gone from codeproject.com; the Wayback Machine holds it as of 2012-01-10: http://web.archive.org/web/20120110151009/http://www.codeproject.com:80/KB/recipes/FIFF.aspxcsharp/record.shbuilds it on .NET 8 in Docker and runs it on every case incheck/cases.js, writingrecord/csharp/<case>.out.src/fiff.jsis the JS port, for Node and the browser, with no dependencies;src/cli.jstakes FIFFConsole's arguments.index.htmlanddemo/are the page. It marks each difference in both files and judges the answer withcheck/check.js. It loads../brand.cssand../brand.jsfrom the site root.check/judges a result. A correct answer counts as many differences as it lists, and each sits in its file where it says; in ordered mode, removing them leaves the same text in both files; in unordered mode, each side reports exactly the tokens the other file lacks, each token cancelling one copy on the other side, so order does not matter and repeats count, as the pseudo-code oforiginal/doc/FIFF.htmlclears a matched pair. Its size is the tokens the differences hold, against the smallest diff by longest common subsequence.
The cases are those of original/test/TestHarness.bat whose files the branch
holds, under the five argument sets of FIFFConsoleTestHarness.bat, and the
competition test on grid1.html and grid2.html.
npm run original fails 11 of its 130 tests. Each failure traces to one of
five causes, all in original/src/FIFFBroker.cs, confirmed by tracing each
failing case through the code:
| Cause | Lines | Failing cases |
|---|---|---|
Unordered mode removes a token as matched when its lookup found nothing: both Process calls pass matched = true whatever IndexOf returned, so "a" vanishes from the differences. |
135–138 | 31 |
| When the two windows each hold the other's current token at different places, the whole window is reported as one difference instead of syncing on the nearer match, so a swapped word pair is 4 tokens where 2 do. | 142–145 | 10, 12 |
| When either file ends, the difference is reported in the same pass that reads the other file's remaining tokens, before they are searched for a match, so a shared line end is reported as changed. | 90–91, 107–119 | 5, 7 |
| An empty side's position is read from its empty token list, which answers -1. | 73–74 | 1, 6, 21, 26, 31 |
A difference ends at the first token both windows share, and a line repeated through the file, such as <TR vAlign=top>, ends it early, so the competition test reports 25 lines where 19 do. This is the algorithm, not a slip in the code: original/doc/FIFF.html names it, a deletion reported "as edition followed by deletion when there is a repeating token present in both files such as | ||
| 128–147 | 35 |
The port fixes the first four causes at their source in src/fiff.js:
- Unordered mode takes one token a step: file 1's first token, found anywhere in file 2, leaves with its match; else file 2's first token, found anywhere in file 1; else both are a difference.
- Ordered mode, when each side holds the other's current token, ends the difference at the nearer of the two matches.
- Ordered mode, at the end of a file, reports the rest only once every token has been looked for in the other side.
- A side with no tokens left in a difference gives as its position how far its file has been read: in ordered mode where the other side's text goes, in unordered mode the end of the file.
node src/cli.js file1.txt file2.txt # FIFFConsole's arguments: <file1> <file2> [<delimiter code> [<ordered> [<buffer> [<format>]]]]
npm test # the checks on the port's answers, and the port's and the checker's own tests
npm run original # the checks and the smallest diff on the original's record
npm run record # rebuild the record from the C# original, in Dockernpm test holds the port to no larger than the original in ordered mode, and
to the original's answer wherever the original passes every check and the
smallest diff.
The fifth cause is FIFF's design, which trades the smallest diff for time
linear in the differences when they are few (original/README,
original/doc/FIFF.html). The port keeps it: on the competition test it
reports 25 lines where the smallest diff is 19.
MIT. See LICENSE.