udiff

package
v0.48.3 Latest Latest
Warning

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

Go to latest
Published: Mar 12, 2024 License: MIT, MIT Imports: 9 Imported by: 0

README

µDiff

Latest Release Go Docs Build Status Go Report Card

Micro diff (µDiff) is a Go library that implements the Myers' diffing algorithm. It aims to provide a minimal API to compute and apply diffs with zero dependencies. It also supports generating diffs in the Unified Format. If you are looking for a way to parse unified diffs, check out sourcegraph/go-diff.

This is merely a copy of the Golang tools internal diff package with a few modifications to export package symbols. All credit goes to the Go authors.

Usage

You can import the package using the following command:

go get github.com/aymanbagabas/go-udiff

Examples

Generate a unified diff for strings a and b.

package main

import (
    "fmt"

    "github.com/aymanbagabas/go-udiff"
    "github.com/aymanbagabas/go-udiff/myers"
)

func main() {
    a := "Hello, world!\n"
    b := "Hello, Go!\nSay hi to µDiff"
    edits := myers.ComputeEdits(a, b)
    unified, _ := udiff.ToUnified("a.txt", "b.txt", a, edits)
    fmt.Println(unified)
}
--- a.txt
+++ b.txt
@@ -1 +1,2 @@
-Hello, world!
+Hello, Go!
+Say hi to µDiff
\ No newline at end of file

Apply changes to a string.

package main

import (
    "fmt"

    "github.com/aymanbagabas/go-udiff"
    "github.com/aymanbagabas/go-udiff/myers"
)

func main() {
    a := "Hello, world!\n"
    b := "Hello, Go!\nSay hi to µDiff"

    edits := myers.ComputeEdits(a, b)
    final, err := udiff.Apply(a, edits)
    if err != nil {
        panic(err)
    }

    fmt.Println(final)
}
Hello, Go!
Say hi to µDiff

To get a line-by-line diff and edits:

package main

import (
    "fmt"

    "github.com/aymanbagabas/go-udiff"
    "github.com/aymanbagabas/go-udiff/myers"
)

func main() {
    a := "Hello, world!\n"
    b := "Hello, Go!\nSay hi to µDiff"

    edits := myers.ComputeEdits(a, b)
    d, err := udiff.ToUnifiedDiff("a.txt", "b.txt", a, edits)
    if err != nil {
        panic(err)
    }

    for _, h := range d.Hunks {
        fmt.Printf("hunk: -%d, +%d\n", h.FromLine, h.ToLine)
        for _, l := range h.Lines {
            fmt.Printf("%s %q\n", l.Kind, l.Content)
        }
    }
}
hunk: -1, +1
delete "Hello, world!\n"
insert "Hello, Go!\n"
insert "Say hi to µDiff"

Alternatives

Contributing

Please send any contributions upstream. Pull requests made against the upstream diff package are welcome.

License

BSD 3-Clause and MIT.

Documentation

Overview

Package udiff computes differences between text files or strings.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Apply

func Apply(src string, edits []Edit) (string, error)

Apply applies a sequence of edits to the src buffer and returns the result. Edits are applied in order of start offset; edits with the same start offset are applied in they order they were provided.

Apply returns an error if any edit is out of bounds, or if any pair of edits is overlapping.

func ApplyBytes

func ApplyBytes(src []byte, edits []Edit) ([]byte, error)

ApplyBytes is like Apply, but it accepts a byte slice. The result is always a new array.

func SortEdits

func SortEdits(edits []Edit)

SortEdits orders a slice of Edits by (start, end) offset. This ordering puts insertions (end = start) before deletions (end > start) at the same point, but uses a stable sort to preserve the order of multiple insertions at the same point. (Apply detects multiple deletions at the same point as an error.)

func ToUnified

func ToUnified(oldLabel, newLabel, content string, edits []Edit, numLines int) (string, error)

ToUnified applies the edits to content and returns a unified diff. The old and new labels are the names of the content and result files. It returns an error if the edits are inconsistent; see ApplyEdits.

func Unified

func Unified(oldLabel, newLabel, old, new string, numLines int) string

Unified returns a unified diff of the old and new strings. The old and new labels are the names of the old and new files. If the strings are equal, it returns the empty string.

Types

type Edit

type Edit struct {
	New   string // the replacement
	Start int
	End   int
}

An Edit describes the replacement of a portion of a text file.

func Bytes

func Bytes(before, after []byte) []Edit

Bytes computes the differences between two byte slices. The resulting edits respect rune boundaries.

func Strings

func Strings(before, after string) []Edit

Strings computes the differences between two strings. The resulting edits respect rune boundaries.

func (Edit) String

func (e Edit) String() string

type OpKind

type OpKind int

OpKind is used to denote the type of operation a line represents. TODO(adonovan): hide this once the myers package no longer references it.

const (
	// Delete is the operation kind for a line that is present in the input
	// but not in the output.
	Delete OpKind = iota
	// Insert is the operation kind for a line that is new in the output.
	Insert
	// Equal is the operation kind for a line that is the same in the input and
	// output, often used to provide context around edited lines.
	Equal
)

func (OpKind) String

func (k OpKind) String() string

String returns a human readable representation of an OpKind. It is not intended for machine processing.

type UnifiedDiff

type UnifiedDiff = unified

UnifiedDiff is a unified diff.

func ToUnifiedDiff

func ToUnifiedDiff(fromName, toName, content string, edits []Edit, numLines int) (UnifiedDiff, error)

ToUnifiedDiff takes a file contents and a sequence of edits, and calculates a unified diff that represents those edits.

Directories

Path Synopsis
_examples
Package difftest supplies a set of tests that will operate on any implementation of a diff algorithm as exposed by diff "github.com/neilotoole/sq/libsq/core/diffdoc/internal/go-udiff"
Package difftest supplies a set of tests that will operate on any implementation of a diff algorithm as exposed by diff "github.com/neilotoole/sq/libsq/core/diffdoc/internal/go-udiff"
package lcs contains code to find longest-common-subsequences (and diffs)
package lcs contains code to find longest-common-subsequences (and diffs)
Package myers implements the Myers diff algorithm.
Package myers implements the Myers diff algorithm.

Jump to

Keyboard shortcuts

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