differ

package module
v0.0.0-...-02f26cf Latest Latest
Warning

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

Go to latest
Published: Apr 23, 2022 License: MIT Imports: 7 Imported by: 0

README

differ

This package is an importable implementation of Myers Diff Algorithm in Go.

"The easiest way to compare and print two slices in diff edit script format!"

— Someone, hopefully... well at least me.

Usage

// import "go.husin.dev/differ"

left := []int{1, 2, 3, 1, 2, 2, 1}
right := []int{3, 2, 3, 1, 1, 3}

d := differ.Diff(left, right)

p := differ.NewPrinter(differ.StrconvInt, differ.PrintUnified, differ.PrintNumber)
p.Add(d...)

fmt.Printf("diff: \n%s", p.String())
// import "go.husin.dev/differ"

left := strings.Split("the quick brown fox jumps over the lazy dog", " ") 
right := strings.Split("the slow yellow fox jumps over the dog", " ") 

d := differ.Diff(left, right)

_, _ = io.Copy(os.Stdout, differ.Reader(d, differ.StrconvString, differ.PrintNumber, differ.PrintColor))

Not for you?

This library was created because I was not successful in finding the one I need. More generally, "Here are 2 slices, diff it for me".

However, if you find this library not for you, here are others that I found in my research which might suit your needs:

  • pkg/diff (I wish I had seen this one before I decided to implement myself, although the ergonomics is a bit awkward like container/ standard library)
  • sergi/go-diff
  • r3labs/diff

References

Documentation

Index

Constants

View Source
const (
	UNKNOWN = iota
	EQUAL
	ADDED
	DELETED
	CHANGED
)

Variables

This section is empty.

Functions

func PrintColor

func PrintColor(p *PrintOption)

func PrintNumber

func PrintNumber(p *PrintOption)

func PrintUnified

func PrintUnified(p *PrintOption)

func Reader

func Reader[T comparable](d []Delta[T], toString func(T) string, opts ...PrinterOption) io.Reader

func StrconvAny

func StrconvAny(e any) string

func StrconvFmt

func StrconvFmt[T any](e T) string

func StrconvInt

func StrconvInt(i int) string

func StrconvString

func StrconvString(str string) string

func String

func String[T comparable](d []Delta[T], toString func(T) string, opts ...PrinterOption) string

Types

type Delta

type Delta[T comparable] struct {
	Left  Subset[T]
	Right Subset[T]

	Kind Kind
}

func Diff

func Diff[T comparable](left, right []T) []Delta[T]

Diff implements Myers Diff algorithm, slice of Delta which contains the merged content of the parameters.

type Kind

type Kind int

func (Kind) String

func (k Kind) String() string

type PrintOption

type PrintOption struct {
	// contains filtered or unexported fields
}

type Printer

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

func NewPrinter

func NewPrinter[T comparable](toString func(T) string, opts ...PrinterOption) *Printer[T]

func (*Printer[T]) Add

func (p *Printer[T]) Add(deltas ...Delta[T])

func (*Printer[T]) Configure

func (p *Printer[T]) Configure(opts ...PrinterOption)

func (*Printer[T]) Reader

func (p *Printer[T]) Reader() io.Reader

func (*Printer[T]) String

func (p *Printer[T]) String() string

type PrinterOption

type PrinterOption func(p *PrintOption)

func PrintPrefix

func PrintPrefix(left, right string) PrinterOption

type Range

type Range struct {
	Start int
	End   int
}

func (Range) String

func (r Range) String() string

type Subset

type Subset[T comparable] struct {
	Index Range
	Value []T
}

func (Subset[T]) Empty

func (s Subset[T]) Empty() bool

func (Subset[T]) Stringer

func (s Subset[T]) Stringer(toString func(T) string, prefix string) *strings.Builder

Directories

Path Synopsis
cmd
differ command

Jump to

Keyboard shortcuts

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