diff

package
v1.20210615.7 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2021 License: MIT Imports: 11 Imported by: 0

Documentation

Overview

Package diff includes text diffing functions.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Levenshtein

func Levenshtein(diffs []Diff) int

Levenshtein computes the Levenshtein distance that is the number of inserted, deleted or substituted characters.

func PrettyHTML

func PrettyHTML(diffs []Diff) string

PrettyHTML converts a []Diff into a pretty HTML report. It is intended as an example from which to write one's own display functions.

func PrettyText

func PrettyText(diffs []Diff) string

PrettyText converts a []Diff into a colored text report.

func Text

func Text(diffs []Diff) string

Text converts a []Diff into a text report.

func Text1

func Text1(diffs []Diff) string

Text1 computes and returns the source text (all equalities and deletions).

func Text2

func Text2(diffs []Diff) string

Text2 computes and returns the destination text (all equalities and insertions).

func ToDelta

func ToDelta(diffs []Diff) string

ToDelta crushes the diff into an encoded string which describes the operations required to transform text1 into text2. E.g. =3\t-2\t+ing -> Keep 3 chars, delete 2 chars, insert 'ing'. Operations are tab-separated. Inserted text is escaped using %xx notation.

Types

type Diff

type Diff struct {
	Type Operation
	Text string
}

Diff represents one diff operation

func FromDelta

func FromDelta(corpus string, delta string) (diffs []Diff, err error)

FromDelta given the original text1, and an encoded string which describes the operations required to transform text1 into text2, comAdde the full diff.

type MatchPatch

type MatchPatch struct {
	// Number of seconds to map a diff before giving up (0 for infinity).
	Timeout time.Duration
	// Cost of an empty edit operation in terms of edit characters.
	EditCost int
	// How far to search for a match (0 = exact location, 1000+ = broad match). A match this many characters away from the expected location will add 1.0 to the score (0.0 is a perfect match).
	MatchDistance int
	// When deleting a large block of text (over ~64 characters), how close do the contents have to be to match the expected contents. (0.0 = perfection, 1.0 = very loose).  Note that MatchThreshold controls how closely the end points of a delete need to match.
	PatchDeleteThreshold float64
	// Chunk size for context length.
	PatchMargin int
	// The number of bits in an int.
	MatchMaxBits int
	// At what point is no match declared (0.0 = perfection, 1.0 = very loose).
	MatchThreshold float64
}

MatchPatch holds the configuration for diff-match-patch operations.

func New

func New() *MatchPatch

New creates a new MatchPatch object with default parameters.

func (*MatchPatch) Diff

func (dmp *MatchPatch) Diff(text1, text2 string, checklines bool) []Diff

Diff finds the differences between two texts.

If an invalid UTF-8 sequence is encountered, it will be replaced by the Unicode replacement character.

`checklines` indicates if we should do a line level diff, or treat the text as an atomic unit.

func (*MatchPatch) DiffHalfMatch

func (dmp *MatchPatch) DiffHalfMatch(text1, text2 string) []string

DiffHalfMatch checks whether the two texts share a substring which is at least half the length of the longer text. This speedup can produce non-minimal diffs.

func (*MatchPatch) DiffRunes

func (dmp *MatchPatch) DiffRunes(text1, text2 []rune, checklines bool) []Diff

DiffRunes finds the differences between two rune sequences.

If an invalid UTF-8 sequence is encountered, it will be replaced by the Unicode replacement character.

`checklines` indicates if we should do a line level diff, or treat the text as an atomic unit.

type Operation

type Operation int8

Operation defines the operation of a diff item.

const (
	// DiffDelete item represents a delete diff.
	DiffDelete Operation = -1
	// DiffInsert item represents an insert diff.
	DiffInsert Operation = 1
	// DiffEqual item represents an equal diff.
	DiffEqual Operation = 0
	//IndexSeparator is used to seperate the array indexes in an index string
	IndexSeparator = ","
)

Operation constants.

func (Operation) String

func (i Operation) String() string

Jump to

Keyboard shortcuts

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