dmp

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2021 License: Apache-2.0 Imports: 9 Imported by: 0

README

This is a port of the diff part of Neil Fraser's diff_match_patch to Go, derived from the Java implementation. All diff-related tests have been ported too.

The implementation is based on processing items of type string. An utility package ./rstring, which is partly based on Go standard package exp/utf8string, helps navigating runewise (rather than bytewise). Perhaps it would be preferable to use []rune instead of string, but this hasn't been tried yet.

Measured in 2012, the performance on a i386 system was between that of the Java and the Qt based C++ implementations. On amd64 systems the Go port performed as fast as the Java implementation (the 6g compiler is able to generate faster code than 8g, because of the larger number of available registers on 64bit processors).

Documentation

Index

Constants

View Source
const (
	DefaultEditCost = 4
	DefaultTimeout  = time.Second
	NoTimeout       = -1
)
View Source
const (
	Equal  = '='
	Insert = '+'
	Delete = '-'
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Diff

type Diff struct {
	Op   int
	Text string
}

func (Diff) String

func (d Diff) String() string

type Diffs

type Diffs []Diff

The data structure representing a diff is a slice of Diff objects:

[]Diff{ {Delete, "Hello"}, {Insert, "Goodbye"}, {Equal, " world."}}

which means: delete "Hello", add "Goodbye" and keep " world."

func DiffMain

func DiffMain(text1, text2 string, checkLines bool, timeout time.Duration) Diffs

Find the differences between two texts. If checkLines is false, then don't run a line-level diff first to identify the changed areas. If it is true, then run a faster slightly less optimal diff.

If timeout is NoTimeout, or -1, it timeout will be inactive. If it is 0, DefaultTimeout will be used.

func (*Diffs) CleanupEfficiency

func (pDiffs *Diffs) CleanupEfficiency(editCost int) (diffs Diffs)

Reduce the number of edits by eliminating operationally trivial equalities.

func (*Diffs) CleanupMerge

func (pDiffs *Diffs) CleanupMerge() (diffs Diffs)

Reorder and merge like edit sections. Merge equalities. Any edit section can move as long as it doesn't cross an equality.

func (*Diffs) CleanupSemantic

func (pdiffs *Diffs) CleanupSemantic() (diffs Diffs)

Reduce the number of edits by eliminating semantically trivial equalities.

func (*Diffs) CleanupSemanticLossless

func (pDiffs *Diffs) CleanupSemanticLossless() (diffs Diffs)

Look for single edits surrounded on both sides by equalities which can be shifted sideways to align the edit to a word boundary.

e.g.: The c<ins>at c</ins>ame. -> The <ins>cat </ins>came.

func (Diffs) Levenshtein

func (diffs Diffs) Levenshtein() (levenshtein int)

Compute the Levenshtein distance – the number of inserted, deleted or substituted characters.

func (Diffs) PrettyHTML

func (diffs Diffs) PrettyHTML() (s string)

Convert a Diff list into a pretty HTML report.

func (Diffs) Text1

func (diffs Diffs) Text1() (source string)

Compute and return the source text (all equalities and deletions).

func (Diffs) Text2

func (diffs Diffs) Text2() (dest string)

Compute and return the destination text (all equalities and insertions).

func (Diffs) XIndex

func (diffs Diffs) XIndex(loc1 int) (loc2 int)

Loc1 is a location in text1; compute and return the equivalent location in text2.

e.g. "The cat" vs "The big cat", 1->1, 5->8

Directories

Path Synopsis
Package rstring provides an efficient way to index strings by rune rather than by byte.
Package rstring provides an efficient way to index strings by rune rather than by byte.

Jump to

Keyboard shortcuts

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