diff

package
v2.0.3 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2026 License: MIT Imports: 6 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 compares sequences the way Python's difflib does: matching blocks and opcodes of a sequence matcher, the similarity ratio of two texts, and unified diffs of their lines. It reads unified diffs back too: into the files and hunks they hold, applied to a text or taken back from it, and line by line as what each line is

Index

Constants

View Source
const NoNewline = `\ No newline at end of file`

What a unified diff writes under a line that ends its text without a line end

Variables

This section is empty.

Functions

func Apply

func Apply(text string, hunks []Hunk, back bool) (string, bool)

The text of a file with the hunks of its diff applied, or with back the text before them: false when the text is not the one the hunks were made for, each hunk checked against the lines it names before it is applied

func Close

func Close(word string, candidates []string, n int, cutoff float64) []string

The n candidates most like the word whose ratio reaches the cutoff, best first and the greater text first between equals, as Python's get_close_matches gives them

func Ends

func Ends(line string) bool

Whether a line carries a line end of its own, which only the last line of a text may lack

func Files

func Files(before, after, name string) string

Unified diff of two texts under a/ and b/ names of the file

func Lines

func Lines(s string) []string

Lines of a text each with its line end, at every break a text editor knows: \n, \r\n, \r, vertical tab, form feed, the file, group and record separators, next line and the Unicode line and paragraph separators

func Ratio

func Ratio(a, b string) float64

Similarity of two texts from zero to one, twice the characters they share over the characters of both

func Split

func Split(s string) []string

Lines of a text without their line ends

func Unified

func Unified(a, b []string, from, to string, n int) string

Unified diff of lines that keep their own line ends, under the two file names with n lines of context, empty when they are equal. A line that carries no line end, the last of a text that ends without one, is closed and marked as such, so every line of the diff stands on a line of its own

Types

type File

type File struct {
	// Names of the file before and after as the headers give them, /dev/null for a file that is not there
	From, To string
	Hunks    []Hunk
	// Lines of the file's part that belong to no hunk, such as the note of a file that was moved, without their line ends
	Notes []string
}

One file of a unified diff

func Parse

func Parse(text string) []File

The files of a unified diff, such as the one a patch answers with: each with its hunks, read by the counts of their heads, so a line taken out that starts with two more dashes is no new file. The lines of a part that has no hunk head are one hunk without a place

func (File) Name

func (f File) Name() string

Name of the file a part of a diff is about, as the tools write it: the name after the change without its b/, else the one before without its a/

type Hunk

type Hunk struct {
	// Its head as written, empty for a hunk that has none
	Head string
	// Where the run starts in the file before and after, counted from one, and how many lines it spans in each; zero starts for a hunk
	// whose head gives none
	OldStart, OldCount, NewStart, NewCount int
	// Its lines, each with the sign it starts with, a space, a minus or a plus, and the line end the file gives it
	Lines []string
}

One run of changed lines of a file with the lines around it

func (Hunk) Texts

func (h Hunk) Texts() (before, after string)

What the hunk takes out of the file and what it puts in, the lines around the change in both

type Kind

type Kind int

What a line of a diff is

const (
	// A line outside the hunks, such as the headers that name a file
	Header Kind = iota
	// The head of a hunk
	Head
	// A line of a hunk: one around the change, one put in, one taken out
	Context
	Added
	Removed
	// What a diff says of the line above, such as that it ends its file without a line end
	Note
)

func Kinds

func Kinds(lines []string) []Kind

What each line of a unified diff is, such as the one git writes, the lines given without their line ends. The lines of a hunk are told by the counts of its head, so a line taken out that starts with two more dashes is no header of a file. A hunk whose head gives no counts, such as one of a merge, runs to the next header

type Match

type Match struct{ I, J, Size int }

Run of size equal elements starting at I in a and J in b

type Matcher

type Matcher[T comparable] struct {
	// contains filtered or unexported fields
}

Finds the longest matching blocks of two sequences, elements of b that are junk or too popular left out of the index

func New

func New[T comparable](isjunk func(T) bool, a, b []T, autojunk bool) *Matcher[T]

Matcher over a and b; isjunk may be nil, and autojunk drops elements of b more popular than one percent past 200 of them

func Runes

func Runes(a, b string) *Matcher[rune]

Matcher over the characters of two texts

func (*Matcher[T]) Blocks

func (m *Matcher[T]) Blocks() []Match

Matching blocks in order, adjacent ones joined, ending with a block of size zero at the ends of both

func (*Matcher[T]) Grouped

func (m *Matcher[T]) Grouped(n int) [][]Opcode

Opcodes in groups of changes with up to n equal elements of context around them

func (*Matcher[T]) Opcodes

func (m *Matcher[T]) Opcodes() []Opcode

Changes turning a into b, equal runs included

func (*Matcher[T]) Ratio

func (m *Matcher[T]) Ratio() float64

Similarity from zero to one, twice the matched elements over the elements of both

type Opcode

type Opcode struct {
	Tag            string
	I1, I2, J1, J2 int
}

Change turning a[I1:I2] into b[J1:J2]: equal, replace, delete or insert

Jump to

Keyboard shortcuts

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