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

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:117

Diff.unified

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:137

Public types

TypeKindSummary
DiffenumThe receiverless owner of the difference operations.

Diff

typedef enum Diff { DIFF_NAMESPACE } Diff

The receiverless owner of the difference operations.

Source: lib/diff.x:17

Design notes

Diff.lines finds a shortest edit script between two texts with the Myers algorithm after trimming the lines the texts share at both ends. Each step keeps only the frontier it reached, so the search costs the square of the edit distance and nothing more. An edit distance past _LIMIT is reported as one deletion of the old middle and one insertion of the new, which is what a reader wants of two unrelated texts anyway.

Tests and examples

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