linediff

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 6, 2026 License: MIT Imports: 3 Imported by: ? 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 linediff compares two sequences of strings with the Myers O(ND) algorithm in linear space. The same core compares lines and words.

Index

Constants

View Source
const MaxEdits = 10000

MaxEdits caps the edit distance. Above it, Diff gives up.

View Source
const MaxWordText = 10000

MaxWordText is the longest text, in bytes, that Words compares.

View Source
const Similarity = 0.5

Similarity is the lowest share of equal text for which Words marks words.

Variables

This section is empty.

Functions

func Lines

func Lines(text string) []string

Lines splits text after each "\n". Each line keeps its "\n", so a missing final newline is a difference.

func Tokens

func Tokens(s string) []string

Tokens splits s into runs of letters, digits, and underscores, runs of white space, and single other characters.

Types

type Edit

type Edit struct {
	Op               Op
	OldStart, OldEnd int
	NewStart, NewEnd int
}

Edit is one run of one operation. Old and new are half-open ranges of positions. A Delete has an empty new range at its position, and an Insert has an empty old range at its position.

func Diff

func Diff(a, b []string) (edits []Edit, approximate bool)

Diff returns the edits that change a into b. Equal runs alternate with changes, and each change is at most one Delete followed by at most one Insert. When the edit distance is above MaxEdits, the result replaces everything between the common prefix and suffix, and approximate is true.

type Hunk

type Hunk struct {
	OldStart, OldEnd int
	NewStart, NewEnd int
	Edits            []Edit
}

Hunk is a group of changes with up to context equal items around each change. Its edits cover the hunk ranges.

func Hunks

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

Hunks groups edits into hunks. Two changes share a hunk when at most 2*context equal items separate them.

type Op

type Op uint8

Op is the operation of an edit.

const (
	Equal Op = iota
	Delete
	Insert
)

type Pair

type Pair struct{ Old, New int }

Pair is one removed item and one added item of the same change.

func Pairs

func Pairs(edits []Edit) []Pair

Pairs pairs the removed and added items of each change in order: the first removed item with the first added item, and so on, up to the shorter side. Extra items stay unpaired.

type Range

type Range struct{ Start, End int }

Range is a half-open byte range in a text.

func Words

func Words(old, new string) (oldRanges, newRanges []Range, ok bool)

Words compares two texts word by word. It returns the ranges of old that are not in new and the ranges of new that are not in old. Marks that only white space separates join into one. ok is false, with no ranges, when a text is longer than MaxWordText, when the token diff is approximate, or when less than Similarity of the text is equal.

Jump to

Keyboard shortcuts

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