Documentation
¶
Overview ¶
Package diff implements the Myers diff algorithm for computing the shortest edit script (SES) between two sequences.
It uses the linear space variant (Section 4b) described in Eugene W. Myers' paper "An O(ND) Difference Algorithm and Its Variations" (1986). It runs in O(ND) time and O(N) space where N is the sum of the lengths of the two sequences and D is the size of the minimum edit script.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Write ¶
Write writes the edits to w. By default it produces unified diff output with hunk headers and 3 lines of context. Use WithGutter and WithContext to configure the output.
Types ¶
type Edit ¶
type Edit struct {
Op OpType
OldLine string // line from the old sequence (for Del and Eq)
NewLine string // line from the new sequence (for Ins and Eq)
}
Edit represents a single edit operation in the diff. Line values may include a trailing '\n' delimiter. A line without a trailing '\n' represents the last line of a sequence that has no final newline.
func Lines ¶
Lines computes the shortest edit script to transform oldLines into newLines. It returns a slice of Edit operations that, when applied in order, convert oldLines to newLines. It uses the linear space variant of the Myers algorithm (Section 4b of the paper), which runs in O(ND) time and O(N) space.
type Option ¶
type Option func(*config)
Option configures how Write formats its output.
func WithColor ¶ added in v0.0.2
func WithColor() Option
WithColor enables ANSI color output: deletions are red (\033[31m) and insertions are green (\033[32m). The caller is responsible for terminal detection and NO_COLOR handling.
func WithContext ¶
WithContext sets the number of unchanged lines to show around each change. It panics if lines is negative. The default is 3.
func WithGutter ¶
func WithGutter() Option
WithGutter enables gutter format: each line is prefixed with a line number from the old sequence, an operation indicator, and a │ separator. Whitespace in changed lines is made visible (spaces as ·, tabs as →, trailing newlines as ↵). Runs of identical lines beyond the context window are collapsed into a summary line.