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
- func Apply(text string, hunks []Hunk, back bool) (string, bool)
- func Close(word string, candidates []string, n int, cutoff float64) []string
- func Ends(line string) bool
- func Files(before, after, name string) string
- func Lines(s string) []string
- func Ratio(a, b string) float64
- func Split(s string) []string
- func Unified(a, b []string, from, to string, n int) string
- type File
- type Hunk
- type Kind
- type Match
- type Matcher
- type Opcode
Constants ¶
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 ¶
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 ¶
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 ¶
Whether a line carries a line end of its own, which only the last line of a text may lack
func Lines ¶
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 ¶
Similarity of two texts from zero to one, twice the characters they share over the characters of both
func Unified ¶
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
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
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 ¶
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 (*Matcher[T]) Blocks ¶
Matching blocks in order, adjacent ones joined, ending with a block of size zero at the ends of both
func (*Matcher[T]) Grouped ¶
Opcodes in groups of changes with up to n equal elements of context around them