Documentation
¶
Overview ¶
Package diff implements the Myers diff algorithm for computing the shortest edit script (SES) between two sequences.
The algorithm is described in Eugene W. Myers' paper "An O(ND) Difference Algorithm and Its Variations" (1986). It runs in O(ND) time 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.
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.