Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

lib/diff.x

Line differences between two texts.

Primary API

FunctionSummary
Diff.linesReturns the line edits that turn old into new: a List of (same line), (delete line), and (insert line) forms in order, with each line’s ending removed.
Diff.unifiedReturns the unified difference between old and new, as diff -u prints it with old_name and new_name in the header and three lines of context, or NULL when the texts are equal line for line.

Diff

Diff.lines

meta native List Diff.lines(String old, String new)

Returns the line edits that turn old into new: a List of (same line), (delete line), and (insert line) forms in order, with each line’s ending removed. Two equal texts give only same forms. Past 2,000 edits the differing middle is one run of deletions followed by one run of insertions.

Source: lib/diff.x:34

Diff.unified

meta native String Diff.unified( String old, String new, String old_name, String new_name)

Returns the unified difference between old and new, as diff -u prints it with old_name and new_name in the header and three lines of context, or NULL when the texts are equal line for line.

Source: lib/diff.x:179

Public types

TypeKindSummary
DiffaliasThe receiverless owner of the difference operations.

Diff

typedef Symbol Diff

The receiverless owner of the difference operations.

Source: lib/diff.x:16

Design notes

Diff owns line differences: a shortest edit script between two texts, found with the Myers algorithm after trimming the lines both share at each end, and its unified spelling. Each search step keeps only its frontier, so the cost is the square of the edit distance; past _LIMIT edits the middle becomes one deletion run and one insertion run.

Tests and examples

make verify (unittest/test-diff.x).