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 ¶
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 ¶
Float64Slices creates an edit script for two slices of float64s.
func StringSlices ¶
StringSlices creates an edit script for two slices of strings.