diff

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2018 License: MIT Imports: 0 Imported by: 0

README

diff

Package diff implements methods for comparing objects and producing edit scripts. The motivation to create the package was to be able to use the diff output format in tests where the output of go-cmp wasn't suitable. It isn't optimized for performance and as of now, it is a non-goal. See the package documentation for more information.

Instalation

$ go get github.com/mibk/diff

Documentation

Overview

Package diff implements methods for comparing objects and producing edit scripts.

The implementation is based on the algorithm described in the paper "An O(ND) Difference Algorithm and Its Variations" by Eugene W. Myers, Algorithmica Vol. 1 No. 2, 1986, p. 251.

Example (CustomType)
package main

import (
	"fmt"
	"strings"

	"github.com/mibk/diff"
)

type ignoreCase struct {
	a, b []string
}

func (ic ignoreCase) Lens() (n, m int) { return len(ic.a), len(ic.b) }
func (ic ignoreCase) Equal(i, j int) bool {
	return strings.ToLower(ic.a[i]) == strings.ToLower(ic.b[j])
}

// missingIgnoreCase returns elements that are move or deleted from a
// compared to b.
func missingIgnoreCase(a, b []string) []string {
	eds := diff.Diff(ignoreCase{a, b})

	var miss []string
	for _, ed := range eds {
		if ed.Op == diff.Insert {
			miss = append(miss, b[ed.Bindex])
		}
	}
	return miss
}

func main() {
	a := []string{"black", "#31ad1d", "#8923dd", "#baddad", "yellow"}
	b := []string{"#31AD1D", "#8924dd", "#BadDad", "black", "YELLOW"}

	for _, m := range missingIgnoreCase(a, b) {
		fmt.Println("-", m)
	}

}
Output:

- #8924dd
- black
Example (SimpleDiffOutput)
package main

import (
	"fmt"

	"github.com/mibk/diff"
)

func main() {
	a := []string{"Alice", "Bob", "Cyril", "Alice", "Bob", "Bob", "Alice", "Daniel"}
	b := []string{"Cyril", "Bob", "Alice", "Bob", "Alice", "Cyril", "Daniel"}
	eds := diff.StringSlices(a, b)

	eds = append(eds, diff.Edit{Index: len(a), Op: diff.None})
	var i int
	for _, ed := range eds {
		for ; i < ed.Index; i++ {
			fmt.Printf(" %s\n", a[i])
		}
		if ed.Op == diff.Delete {
			fmt.Printf("-%s\n", a[i])
			i++
		} else if ed.Op == diff.Insert {
			fmt.Printf("+%s\n", b[ed.Bindex])
		}
	}

}
Output:

-Alice
-Bob
 Cyril
+Bob
 Alice
 Bob
-Bob
 Alice
+Cyril
 Daniel

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Data

type Data interface {
	// Lens returns the lengths of the underlying sequences.
	Lens() (n, m int)
	// Equal reports whether the elements from the two sequences
	// with indexes i and j are equal.
	Equal(i, j int) bool
}

Data is the interface that is used by the Diff function to produce an edit script. A type that satisfies the Data interface is typically a wrapper around two collections. The method requires that the elements of the sequences be enumerated by integer indexes.

type Edit

type Edit struct {
	Index  int // index where the operation should occur
	Op     Operation
	Bindex int // only valid for Insert
}

An Edit represents an element in the edit script, produced by the Diff function. The edit script represents a set of operations describing how to convert the sequence A into the sequence B.

Each Edit has an operation Op (either Insert or Delete) that should occur at Index in A in order to convert it into B. If Op is Insert, Bindex represents the index of the element in B that should be inserted into A at Index.

func Diff

func Diff(data Data) []Edit

Diff creates an edit script for data. The edit script represents a set of operations describing how to convert the sequence A into the sequence B.

func Float64Slices

func Float64Slices(a, b []float64) []Edit

Float64Slices creates an edit script for two slices of float64s.

func IntSlices

func IntSlices(a, b []int) []Edit

IntSlices creates an edit script for two slices of ints.

func StringSlices

func StringSlices(a, b []string) []Edit

StringSlices creates an edit script for two slices of strings.

type Operation

type Operation int

Operation represents an operation in the edit script.

const (
	None Operation = iota
	Delete
	Insert
)

The list of possible operations.

None is never used by the package. It can be used as a sentinel operation for Edit.Op; the value is reserved. See the simple diff output example for illustration of usage.

Jump to

Keyboard shortcuts

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