diff

package
v0.0.4 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 26, 2026 License: Apache-2.0 Imports: 2 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package diff implements line-based text differencing, unified diff formatting, and line change statistics for TaskSmith.

Shortest Edit Script (Myers Algorithm)

MyersDiff implements Eugene W. Myers' O((N+M)D) greedy shortest-edit-script algorithm ("An O(ND) Difference Algorithm and Its Variations", 1986). Given two slices of lines A and B, it searches the edit graph along diagonals k = x - y in increasing order of edit distance D (the number of non-diagonal steps), advancing greedily along "snakes" of matching lines (OpEqual) before backtracking through the recorded frontier trace to reconstruct the minimal sequence of Edit operations (OpEqual, OpDelete, and OpInsert).

Unified Diff Formatting

FormatUnified normalizes CRLF ("\r\n") and LF ("\n") line endings, computes the shortest edit script via MyersDiff, and groups contiguous changes into standard unified diff hunks surrounded by up to 3 lines of unchanged context. Adjacent changes separated by 6 or fewer unchanged lines are coalesced into a single hunk. If both inputs are empty or produce no net edits, FormatUnified returns an empty string.

Line Change Statistics

Stats computes the line-level addition (OpInsert) and deletion (OpDelete) counts between two text payloads using the same line splitting and CRLF normalization rules as FormatUnified.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FormatUnified

func FormatUnified(nameA, nameB, contentA, contentB string) string

FormatUnified compares contentA and contentB and returns a unified diff string with 3 lines of context. It normalizes "\r\n" line endings to "\n". If the contents are identical (after line splitting), it returns "".

func Merge3

func Merge3(ancestor, left, right []string) ([]string, bool)

Merge3 performs a line-based three-way merge using ancestor as the common base between left and right. It returns the merged lines and a boolean reporting whether an overlapping conflict was detected.

func Stats

func Stats(contentA, contentB string) (added int, removed int)

Stats computes the number of inserted (OpInsert) and deleted (OpDelete) lines between contentA and contentB.

Types

type Edit

type Edit struct {
	Op   Op
	Line string
}

Edit represents a single line edit operation produced by MyersDiff.

func MyersDiff

func MyersDiff(a, b []string) []Edit

MyersDiff implements the Myers shortest-edit-script algorithm to compute the minimal sequence of Edit operations transforming slice a into slice b.

type Op

type Op int

Op represents the type of line edit operation in an edit script.

const (
	// OpEqual indicates a line that is unchanged between both inputs.
	OpEqual Op = iota
	// OpDelete indicates a line deleted from the first input.
	OpDelete
	// OpInsert indicates a line inserted from the second input.
	OpInsert
)

func (Op) String

func (op Op) String() string

String returns a human-readable name for the edit operation.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL