package diff

import "github.com/kylelemons/godebug/diff"

Package diff implements a linewise diff algorithm.

Index

Examples

Functions

func Diff

func Diff(A, B string) string

Diff returns a string containing a line-by-line unified diff of the linewise changes required to make A into B. Each line is prefixed with '+', '-', or ' ' to indicate if it should be added, removed, or is correct respectively.

Example

Code:

{
	constitution := strings.TrimSpace(`
We the People of the United States, in Order to form a more perfect Union,
establish Justice, insure domestic Tranquility, provide for the common defence,
promote the general Welfare, and secure the Blessings of Liberty to ourselves
and our Posterity, do ordain and establish this Constitution for the United
States of America.
`)

	got := strings.TrimSpace(`
:wq
We the People of the United States, in Order to form a more perfect Union,
establish Justice, insure domestic Tranquility, provide for the common defence,
and secure the Blessings of Liberty to ourselves
and our Posterity, do ordain and establish this Constitution for the United
States of America.
`)

	fmt.Println(Diff(got, constitution))

	// Output:
	// -:wq
	//  We the People of the United States, in Order to form a more perfect Union,
	//  establish Justice, insure domestic Tranquility, provide for the common defence,
	// -and secure the Blessings of Liberty to ourselves
	// +promote the general Welfare, and secure the Blessings of Liberty to ourselves
	//  and our Posterity, do ordain and establish this Constitution for the United
	//  States of America.
}

Output:

-:wq
 We the People of the United States, in Order to form a more perfect Union,
 establish Justice, insure domestic Tranquility, provide for the common defence,
-and secure the Blessings of Liberty to ourselves
+promote the general Welfare, and secure the Blessings of Liberty to ourselves
 and our Posterity, do ordain and establish this Constitution for the United
 States of America.

Types

type Chunk

type Chunk struct {
	Added   []string
	Deleted []string
	Equal   []string
}

Chunk represents a piece of the diff. A chunk will not have both added and deleted lines. Equal lines are always after any added or deleted lines. A Chunk may or may not have any lines in it, especially for the first or last chunk in a computation.

func DiffChunks

func DiffChunks(a, b []string) []Chunk

DiffChunks uses an O(D(N+M)) shortest-edit-script algorithm to compute the edits required from A to B and returns the edit chunks.

Source Files

diff.go

Version
v1.1.0 (latest)
Published
May 5, 2019
Platform
linux/amd64
Imports
3 packages
Last checked
2 minutes ago

Tools for package owners.