diff

package module
v0.0.0-...-7cdfd63 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2025 License: MIT Imports: 0 Imported by: 0

README

go.diff

A Go implementation of "An O(NP) Sequence Comparison Algorithm".

pkg.go.dev GitHub Actions Appveyor Codecov

Installation

$ go get github.com/hattya/go.diff

Usage

package main

import (
	"fmt"

	"github.com/hattya/go.diff"
)

func main() {
	a := []rune("acbdeacbed")
	b := []rune("acebdabbabed")
	cl := diff.Runes(a, b)
	i := 0
	for _, c := range cl {
		for ; i < c.A; i++ {
			fmt.Printf("  %c\n", a[i])
		}
		for j := c.A; j < c.A+c.Del; j++ {
			fmt.Printf("- %c\n", a[j])
		}
		for j := c.B; j < c.B+c.Ins; j++ {
			fmt.Printf("+ %c\n", b[j])
		}
		i += c.Del
	}
	for ; i < len(a); i++ {
		fmt.Printf("  %c\n", a[i])
	}
}

License

go.diff is distributed under the terms of the MIT License.

References

S. Wu, U. Manber, G. Myers, and W. Miller, "An O(NP) Sequence Comparison Algorithm" August 1989

Documentation

Overview

Package diff implements the difference algorithm, which is based upon S. Wu, U. Manber, G. Myers, and W. Miller, "An O(NP) Sequence Comparison Algorithm" August 1989.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Change

type Change struct {
	A, B int // position in a and b
	Del  int // number of elements that deleted from a
	Ins  int // number of elements that inserted into b
}

func Bytes

func Bytes(a, b []byte) []Change

Bytes returns the differences between byte slices.

func Diff

func Diff(m, n int, data Interface) []Change

Diff returns the differences between data. It makes O(NP) (the worst case) calls to data.Equal.

func Ints

func Ints(a, b []int) []Change

Ints returns the differences between int slices.

func Runes

func Runes(a, b []rune) []Change

Runes returns the differences between rune slices.

func Strings

func Strings(a, b []string) []Change

Strings returns the differences between string slices.

type Interface

type Interface interface {
	// Equal returns whether the elements at i and j are equal.
	Equal(i, j int) bool
}

Jump to

Keyboard shortcuts

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