differ

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2023 License: Apache-2.0 Imports: 5 Imported by: 0

README

differ

Differ is a Go library for finding the differences between two sequences.

License

Apache-2.0


Documentation

Overview

Differ is a package for finding the differences between two sequences.

Differ uses a sequence matcher based on a slightly simplified version of Python's difflib sequence matcher.

Thanks to generics Differ can compare any two slices of comparables. (Although comparing float sequences is not recommended.)

See New for how to create a Differ value.

Index

Examples

Constants

This section is empty.

Variables

View Source
var Version string

Functions

This section is empty.

Types

type Block

type Block[T comparable] struct {
	Tag   Tag
	Items []T
}

type Differ

type Differ[T comparable] struct {
	A []T
	B []T
	// contains filtered or unexported fields
}

func New

func New[T comparable](a, b []T) *Differ[T]

New returns a Differ value based on the provided a and b slices. These slices are only ever read and may be accessed as .A and .B. After creating a Differ, call [Blocks] (or [Spans]) to see the differences.

func (*Differ[T]) Blocks

func (me *Differ[T]) Blocks() []Block[T]

Blocks returns a sequence of Block values representing how to go from a to b. Each block has a Tag and a sequence of items. This is the easiest method for seeing the differences in two sequences. See also [Spans].

Example (Common)
a := strings.Fields("foo\nbar\nbaz\nquux")
b := strings.Fields("foo\nbaz\nbar\nquux")
differ := New(a, b)
blocks := differ.Blocks()
for _, block := range blocks {
	fmt.Println(block.Tag, strings.Join(block.Items, " "))
}
Output:

= foo
+ baz
= bar
- baz
= quux
Example (Ints)
a := []int{1, 2, 3, 4, 5, 6}
b := []int{2, 3, 5, 7}
differ := New(a, b)
blocks := differ.Blocks()
for _, block := range blocks {
	fmt.Println(block.Tag, gong.StringForSlice(block.Items))
}
Output:

- 1
= 2 3
- 4
= 5
% 7
Example (Strings)
a := strings.Fields("the quick brown fox jumped over the lazy dogs")
b := strings.Fields("a quick red fox jumped over some lazy hogs")
differ := New(a, b)
blocks := differ.Blocks()
for _, block := range blocks {
	fmt.Println(block.Tag, strings.Join(block.Items, " "))
}
Output:

% a
= quick
% red
= fox jumped over
% some
= lazy
% hogs

func (*Differ[T]) Spans

func (me *Differ[T]) Spans() []Span

Spans returns a sequence of Span values representing how to go from a to b. Each span has a Tag and a sequence of [Quad]s. Each Quad holds a pair of start/end indexes into a and b. The easiest method for seeing the differences in two sequences is [Blocks].

type Quad

type Quad struct {
	Astart int
	Aend   int
	Bstart int
	Bend   int
}

type Span

type Span struct {
	Tag Tag
	Quad
}

func (Span) String

func (me Span) String() string

type Tag

type Tag uint8
const (
	Equal Tag = iota
	Insert
	Delete
	Replace
)

func (Tag) String

func (me Tag) String() string

Jump to

Keyboard shortcuts

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