diff
Line-by-line and word diff - LCS-based diff of two line arrays, diff of two strings, unified-style format output, check whether a diff has any changes, count additions and removals, and word-level diff of two strings.
Load with: use diff
What this module does
diff computes the difference between two sequences of lines (or words).
The core algorithm is LCS (longest common subsequence) via a dynamic-programming
table - the same approach as GNU diff. Each diff result is a list of records
{t, ln} where t is "+" (added),
"-" (removed), or " " (unchanged) and ln is
the line content.
This is used by dif3 for three-way merges and is useful for test
output comparison, patch generation, and code review tooling.
Quick example
use diff
# Diff two strings line by line
a = "hello\nworld\nfoo"
b = "hello\nearth\nfoo\nbar"
d = diffs(a, b)
# [{t:" ", ln:"hello"}, {t:"-", ln:"world"}, {t:"+", ln:"earth"}, {t:" ", ln:"foo"}, {t:"+", ln:"bar"}]
# Format as unified output
prn(difff(d, nil))
# Did anything change?
prn(diffo(d)) # fls (there are changes)
# Count changes
prn(diffc(d)) # {a: 2, r: 1}
# Word diff
wd = diffw("the quick brown fox", "the slow red fox")
Functions
Core diff
diffl(a, b)
LCS-based diff of two line arrays. Builds an O(m×n) DP table, backtracks to produce the minimal edit sequence, and returns a list of {t, ln} records where t is "+", "-", or " ". This is the primary high-quality diff function used by dif3.
diffs(s1, s2)
Splits two strings on newlines and calls diffl. Convenience wrapper for string inputs.
Output and analysis
difff(diff, ctx)
Formats a diff list as a unified-style string. Each record becomes "{t}{ln}\n" - added lines start with +, removed with -, unchanged with a space. The ctx parameter is reserved for future context-line support.
diffo(diff)
Returns fls if the diff contains any "+" or "-" records (i.e. the sequences differ), tru if they are identical.
diffc(diff)
Counts the number of added and removed lines. Returns {a: additions, r: removals}.
Word diff
diffw(s1, s2)
Splits two strings on spaces and runs an LCS diff on the resulting word arrays. Returns the same {t, ln} record format, where ln is now a word rather than a line.
Notes
- The LCS algorithm is O(m×n) space and time - avoid very large inputs in memory-constrained environments.
- Used as a dependency by
dif3for three-way merges. - Requires
trlandtxt.