Help us shape the future of Go! Take the 2026 Go Developer Survey

textdiff

package
v0.348.0 Latest Latest
Warning

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

Go to latest
Published: Oct 31, 2026 License: BSD-3-Clause Imports: 2 Imported by: 0

Documentation

Overview

Package textdiff compares two texts line by line: the shortest edit script (Myers, "An O(ND) Difference Algorithm and Its Variations", 1986), grouped into hunks with context, as a unified diff, or as rows for a side-by-side view.

The search is Myers' linear-space refinement: it finds the middle snake of the edit graph from both ends at once and recurses on the two halves, so memory is O(N+M) whatever the distance, and time O((N+M)·D) for D edits. Lines are interned to integers first, so a comparison is one integer test, and the common prefix and suffix are taken off before the search.

Two texts that share almost nothing can make D close to N+M, and the time quadratic. A budget bounds the work: when a sub-problem exceeds it, that part is reported as deleted and inserted as a whole. The script is then still correct (applying it to a gives b) but no longer minimal there, as GNU diff does past its "too expensive" heuristic. Options.Budget sets it; the default keeps two 10 000-line texts with nothing in common well under a second.

Index

Constants

View Source
const DefaultBudget = 20_000_000

DefaultBudget is the work allowed when Options.Budget is 0.

Variables

This section is empty.

Functions

func Split

func Split(s string) []string

Split cuts a text into lines, without their "\n". A final newline does not make an empty last line, and an empty text has no lines.

func Stats

func Stats(edits []Edit) (added, removed int)

Stats counts the lines a script adds and removes.

func Unified

func Unified(aName, bName string, a, b []string, context int) string

Unified writes the script as a unified diff (diff -u) with context lines; the empty string when a and b are the same.

Types

type Edit

type Edit struct {
	Op   Op
	A, B int
}

Edit is one step of the script, in order: A indexes a (Equal, Delete), B indexes b (Equal, Insert). The index an edit does not use is the position in that text where it happens, so a script can be walked with both cursors.

func Lines

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

Lines returns the shortest edit script turning a into b.

func LinesWith

func LinesWith(a, b []string, o Options) []Edit

LinesWith is Lines with options.

type Hunk

type Hunk struct {
	AStart, ALen, BStart, BLen int
	Edits                      []Edit
}

Hunk is a run of changes with the lines of context around it: lines AStart..AStart+ALen of a against BStart..BStart+BLen of b (0-based).

func Hunks

func Hunks(edits []Edit, context int) []Hunk

Hunks groups a script into hunks, each change with up to context equal lines around it; changes closer than 2·context lines share a hunk.

type Op

type Op uint8

Op is what an edit does to a line.

const (
	// Equal keeps line A of a, which is line B of b.
	Equal Op = iota
	// Delete removes line A of a.
	Delete
	// Insert adds line B of b.
	Insert
)

func (Op) String

func (o Op) String() string

type Options

type Options struct {
	// Budget bounds the work of the search, in steps of the edit graph
	// (0: DefaultBudget; negative: no bound).
	Budget int
}

Options tune the comparison.

type Row

type Row struct {
	Op   Op
	A, B int
}

Row is one line of a side-by-side view: A is the line of a on the left, B the line of b on the right, -1 for a blank side. Op is Equal for an unchanged line, Delete for a line only on the left, Insert for a line only on the right; a changed line has both sides and Op Delete.

func SideBySide

func SideBySide(edits []Edit) []Row

SideBySide lays a script out in rows: equal lines face each other, and in each block of changes the deleted lines face the inserted ones in order, the longer side running on against blanks.

func (Row) Changed

func (r Row) Changed() bool

Changed reports whether the row shows a line that was replaced (both sides, different).

Jump to

Keyboard shortcuts

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