text

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Nov 21, 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 text works with plain text, such as diffing it line by line.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func UnifiedDiff

func UnifiedDiff(old, updated string, context int) string

UnifiedDiff returns the differences between old and updated as unified diff hunks with the given lines of context, without file headers, or "" if they are equal.

Types

type DiffKind

type DiffKind byte

DiffKind is what a DiffOp does to a line.

const (
	DiffEqual  DiffKind = ' '
	DiffDelete DiffKind = '-'
	DiffInsert DiffKind = '+'
)

type DiffOp

type DiffOp struct {
	Kind DiffKind
	Line string
	// OldLine and NewLine are the 1-based positions the line has, or would
	// have, in each input.
	OldLine, NewLine int
}

DiffOp is one line of a diff.

func Diff

func Diff(old, updated string) []DiffOp

Diff compares old and updated line by line, each keeping its trailing newline. A final line without one differs from the same line with one.

func DiffLines

func DiffLines(a, b []string) []DiffOp

DiffLines compares two sequences of lines, giving a shortest edit script with deletions before insertions within each change. If the lines between the common prefix and suffix are too many to compare, they are reported as all deleted and then all inserted.

type Hunk

type Hunk struct {
	// Skipped is how many unchanged lines come between the previous hunk (or
	// the start) and this one.
	Skipped int
	Ops     []DiffOp
}

Hunk is a run of changes with the unchanged lines around them.

func Hunks

func Hunks(ops []DiffOp, context int) []Hunk

Hunks groups ops into hunks, keeping up to context unchanged lines either side of each change and dropping the rest. Changes separated by no more than 2*context unchanged lines share a hunk. It returns nil if nothing changed.

Jump to

Keyboard shortcuts

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