diffmatchpatch

package module
v0.0.0-...-20e4e01 Latest Latest
Warning

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

Go to latest
Published: Dec 17, 2020 License: MIT Imports: 11 Imported by: 0

README

diffmatchpatch GoDoc Build Status Coverage Status

diffmatchpatch offers algorithms to perform operations required for synchronizing plain text:

  • Compare two texts and return their differences.
  • Perform fuzzy matching of text.
  • Apply patches onto text.

Installation

go get -u github.com/sergi/go-diff/...

Usage

The following example compares two texts and writes out the differences to standard output.

Found a bug or are you missing a feature in go-diff?

Please make sure to have the latest version of go-diff. If the problem still persists go through the open issues in the tracker first. If you cannot find your request just open up a new issue.

How to contribute?

You want to contribute to go-diff? GREAT! If you are here because of a bug you want to fix or a feature you want to add, you can just read on. Otherwise we have a list of open issues in the tracker. Just choose something you think you can work on and discuss your plans in the issue by commenting on it.

Please make sure that every behavioral change is accompanied by test cases. Additionally, every contribution must pass the lint and test Makefile targets which can be run using the following commands in the repository root directory.

After your contribution passes these commands, create a PR and we will review your contribution.

Origins

go-diff is a Go language port of Neil Fraser's google-diff-match-patch code. His original code is available at http://code.google.com/p/google-diff-match-patch/.

The original Google Diff, Match and Patch Library is licensed under the Apache License 2.0. The full terms of that license are included here in the APACHE-LICENSE-2.0 file.

Diff, Match and Patch Library

Written by Neil Fraser Copyright (c) 2006 Google Inc. http://code.google.com/p/google-diff-match-patch/

This Go version of Diff, Match and Patch Library is licensed under the MIT License (a.k.a. the Expat License) which is included here in the LICENSE file.

Go version of Diff, Match and Patch Library

Copyright (c) 2012-2016 The go-diff authors. All rights reserved. https://github.com/sergi/go-diff

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Documentation

Overview

Package diffmatchpatch offers robust algorithms to perform the operations required for synchronizing plain text.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// DiffTimeout is the number of seconds to map a diff before giving up (0
	// for infinity).
	DiffTimeout time.Duration
	// Cost of an empty edit operation in terms of edit characters.
	DiffEditCost 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
	// 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

	// 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
}

Config is the configuration for diff-match-patch operations.

func NewDefaultConfig

func NewDefaultConfig() *Config

NewDefaultConfig creates a new configuration with default parameters.

func (*Config) Diff

func (config *Config) 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.

func (*Config) DiffBisect

func (config *Config) DiffBisect(text1, text2 string, deadline time.Time) []Diff

DiffBisect finds the 'middle snake' of a diff, split the problem in two and return the recursively constructed diff. If an invalid UTF-8 sequence is encountered, it will be replaced by the Unicode replacement character.

See Myers 1986 paper: An O(ND) Difference Algorithm and Its Variations.

func (*Config) DiffCharsToLines

func (config *Config) DiffCharsToLines(diffs []Diff, lineArray []string) []Diff

DiffCharsToLines rehydrates the text in a diff from a string of line hashes to real lines of text.

func (*Config) DiffCleanupEfficiency

func (config *Config) DiffCleanupEfficiency(diffs []Diff) []Diff

DiffCleanupEfficiency reduces the number of edits by eliminating operationally trivial equalities.

func (*Config) DiffCleanupMerge

func (config *Config) DiffCleanupMerge(diffs []Diff) []Diff

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

func (*Config) DiffCleanupSemantic

func (config *Config) DiffCleanupSemantic(diffs []Diff) []Diff

DiffCleanupSemantic reduces the number of edits by eliminating semantically trivial equalities.

func (*Config) DiffCleanupSemanticLossless

func (config *Config) DiffCleanupSemanticLossless(diffs []Diff) []Diff

DiffCleanupSemanticLossless looks 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 (*Config) DiffCommonOverlap

func (config *Config) DiffCommonOverlap(text1 string, text2 string) int

DiffCommonOverlap determines if the suffix of one string is the prefix of another.

func (*Config) DiffCommonPrefix

func (config *Config) DiffCommonPrefix(text1, text2 string) int

DiffCommonPrefix determines the common prefix length of two strings.

func (*Config) DiffCommonSuffix

func (config *Config) DiffCommonSuffix(text1, text2 string) int

DiffCommonSuffix determines the common suffix length of two strings.

func (*Config) DiffFromDelta

func (config *Config) DiffFromDelta(text1 string, delta string) (diffs []Diff, err error)

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

func (*Config) DiffHalfMatch

func (config *Config) 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 (*Config) DiffLevenshtein

func (config *Config) DiffLevenshtein(diffs []Diff) int

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

func (*Config) DiffLinesToChars

func (config *Config) DiffLinesToChars(text1, text2 string) (string, string, []string)

DiffLinesToChars splits two texts into a list of strings, and educes the texts to a string of hashes where each Unicode character represents one line. It's slightly faster to call DiffLinesToRunes first, followed by DiffRunes.

func (*Config) DiffLinesToRunes

func (config *Config) DiffLinesToRunes(text1, text2 string) ([]rune, []rune, []string)

DiffLinesToRunes splits two texts into a list of runes.

func (*Config) DiffPrettyHtml

func (config *Config) DiffPrettyHtml(diffs []Diff) string

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

func (*Config) DiffPrettyText

func (config *Config) DiffPrettyText(diffs []Diff) string

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

func (*Config) DiffRunes

func (config *Config) 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.

func (*Config) DiffText1

func (config *Config) DiffText1(diffs []Diff) string

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

func (*Config) DiffText2

func (config *Config) DiffText2(diffs []Diff) string

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

func (*Config) DiffToDelta

func (config *Config) DiffToDelta(diffs []Diff) string

DiffToDelta 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.

func (*Config) DiffXIndex

func (config *Config) DiffXIndex(diffs []Diff, loc int) int

DiffXIndex returns the equivalent location in s2.

func (*Config) Match

func (config *Config) Match(text, pattern string, loc int) int

Match locates the best instance of 'pattern' in 'text' near 'loc'. Returns -1 if no match found.

func (*Config) MatchAlphabet

func (config *Config) MatchAlphabet(pattern string) map[byte]int

MatchAlphabet initialises the alphabet for the Bitap algorithm.

func (*Config) MatchBitap

func (config *Config) MatchBitap(text, pattern string, loc int) int

MatchBitap locates the best instance of 'pattern' in 'text' near 'loc' using the Bitap algorithm. Returns -1 if no match was found.

func (*Config) PatchAddContext

func (config *Config) PatchAddContext(patch Patch, text string) Patch

PatchAddContext increases the context until it is unique, but doesn't let the pattern expand beyond MatchMaxBits.

func (*Config) PatchAddPadding

func (config *Config) PatchAddPadding(patches []Patch) string

PatchAddPadding adds some padding on text start and end so that edges can match something. Intended to be called only from within patchApply.

func (*Config) PatchApply

func (config *Config) PatchApply(patches []Patch, text string) (string, []bool)

PatchApply merges a set of patches onto the text. Returns a patched text, as well as an array of true/false values indicating which patches were applied.

func (*Config) PatchDeepCopy

func (config *Config) PatchDeepCopy(patches []Patch) []Patch

PatchDeepCopy returns an array that is identical to a given array of patches.

func (*Config) PatchFromText

func (config *Config) PatchFromText(textline string) ([]Patch, error)

PatchFromText parses a textual representation of patches and returns a List of Patch objects.

func (*Config) PatchMake

func (config *Config) PatchMake(opt ...interface{}) []Patch

PatchMake computes a list of patches.

func (*Config) PatchSplitMax

func (config *Config) PatchSplitMax(patches []Patch) []Patch

PatchSplitMax looks through the patches and breaks up any which are longer than the maximum limit of the match algorithm. Intended to be called only from within patchApply.

func (*Config) PatchToText

func (config *Config) PatchToText(patches []Patch) string

PatchToText takes a list of patches and returns a textual representation.

type Diff

type Diff struct {
	Op   Op
	Text string
}

Diff contains information about a single diff operation.

type Op

type Op int

Op is the diff operation enum.

const (
	// OpDelete item represents a delete diff.
	OpDelete Op = -1
	// OpInsert item represents an insert diff.
	OpInsert Op = 1
	// OpEqual item represents an equal diff.
	OpEqual Op = 0
)

Op values.

func (Op) String

func (i Op) String() string

type Patch

type Patch struct {
	Diffs   []Diff
	Start1  int
	Start2  int
	Length1 int
	Length2 int
}

Patch holds information about a patch.

func (*Patch) String

func (p *Patch) String() string

String satisfies the fmt.Stringer interface.

Generates a string that emulates GNU diff's format like the following:

Header: @@ -382,8 +481,9 @@

Indices are printed as 1-based, not 0-based.

Directories

Path Synopsis
_example/example.go
_example/example.go

Jump to

Keyboard shortcuts

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