Documentation
¶
Overview ¶
Package diffmatchpatch implements the Diff Match Patch algorithms for computing differences between two texts, fuzzy matching, and applying patches. Ported from Neil Fraser's original implementation at https://github.com/google/diff-match-patch.
Index ¶
- func TranslateIndex(diffs []Diff, loc int) int
- type Diff
- func CleanupEfficiency(diffs []Diff, editCost int) []Diff
- func CleanupMerge(diffs []Diff) []Diff
- func CleanupSemantic(diffs []Diff) []Diff
- func CleanupSemanticLossless(diffs []Diff) []Diff
- func DiffLines(ctx context.Context, s1, s2 string) []Diff
- func DiffRunes(ctx context.Context, r1, r2 []rune) []Diff
- func DiffStrings(ctx context.Context, s1, s2 string) []Diff
- type LinesToRunesResult
- type Matcher
- type Operation
- type Patch
- type Patcher
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func TranslateIndex ¶
TranslateIndex maps a rune index in text1 to the corresponding rune index in text2, accounting for insertions and deletions described by diffs.
Types ¶
type Diff ¶
Diff represents a single edit operation on a piece of text.
func CleanupEfficiency ¶
CleanupEfficiency reduces diffs by eliminating operationally trivial equalities. editCost is the minimum rune count of an equality that is worth preserving; equalities shorter than this threshold are converted to insert/delete pairs. A value of 4 is typical.
func CleanupMerge ¶
CleanupMerge reorders and merges like edit sections.
func CleanupSemantic ¶
CleanupSemantic reduces diffs by eliminating semantically trivial equalities.
func CleanupSemanticLossless ¶
CleanupSemanticLossless shifts edits to align on word/line boundaries.
func DiffLines ¶
DiffLines diffs two strings using a two-pass algorithm. The first pass operates at line granularity to quickly locate changed regions; the second pass re-diffs each changed region at character level to produce precise intra-line edits. The returned diffs therefore contain character-level operations, not whole-line ones. To diff at line granularity only, encode lines as runes with DiffRunes. Use context.WithTimeout to bound execution time; context.Background() for no limit.
func DiffRunes ¶
DiffRunes computes the differences between two rune slices. Use context.WithTimeout to bound execution time; context.Background() for no limit.
func DiffStrings ¶
DiffStrings computes character-level differences between two strings. Use context.WithTimeout to bound execution time; context.Background() for no limit.
type LinesToRunesResult ¶
type LinesToRunesResult struct {
Text1 []rune // text1, encoded as one rune per line
Text2 []rune // text2, encoded as one rune per line
Lines []string // Lines[r] is the line text for rune value r
}
LinesToRunesResult holds the output of LinesToRunes: two texts encoded one rune per line, plus the table mapping each rune value back to its line. Diffing Text1/Text2 with DiffRunes and expanding the result through Lines yields a line-granularity diff.
func LinesToRunes ¶
func LinesToRunes(text1, text2 string) LinesToRunesResult
LinesToRunes encodes two texts into rune sequences where each rune value is an index into the returned Lines table — the encoding DiffLines uses internally to line-diff before re-diffing changed regions at character level. Exposed for callers that want pure line-level diffing instead: feed Text1/Text2 to DiffRunes, then look up each result rune in Lines.
Line counts are capped (40000 for text1, 65535 for text2, matching other diff-match-patch ports); text beyond the cap is folded into one final line per text so encoding stays bounded on huge inputs.
type Matcher ¶
type Matcher struct {
// Threshold controls how loosely to match (0.0 = perfect, 1.0 = very loose).
Threshold float32
// Distance is how far from loc to search (0 = exact location only).
Distance int
}
Matcher performs fuzzy text matching with configurable accuracy.
type Patch ¶
type Patch struct {
Diffs []Diff
Start1 int // start position in text1 (source)
Start2 int // start position in text2 (target)
Length1 int // length of the affected region in text1
Length2 int // length of the affected region in text2
}
Patch represents a set of diffs to apply to a text.
type Patcher ¶
type Patcher struct {
// DeleteThreshold is the maximum acceptable edit-distance ratio between the
// expected and matched text when applying a patch fuzzily. Patches whose
// ratio exceeds this value are rejected. 0 requires an exact match; 0.5
// tolerates up to half the source text being different.
DeleteThreshold float32
// Margin is the number of context runes included around each change in a
// patch, and the minimum buffer size used when splitting oversized patches.
// A value of 4 is typical.
Margin int
// EditCost is passed to CleanupEfficiency in Make to convert short equalities
// into insert/delete pairs before building patches. A value of 4 is typical.
// It is not used by MakeFromTextAndDiffs or MakeFromDiffs.
EditCost int
// Matcher is the fuzzy-match configuration used to locate patch positions
// during Apply.
Matcher Matcher
}
Patcher holds configuration for computing and applying patches. The zero value is valid but conservative: a DeleteThreshold of 0 rejects any imperfect match, and a Margin of 0 includes no context around changes. Typical values: DeleteThreshold 0.5, Margin 4, EditCost 4, Matcher{Threshold: 0.5, Distance: 1000}.
func (Patcher) Apply ¶
Apply applies patches to text and returns the patched text along with a boolean result per patch indicating whether it was applied successfully. Patches that span more than bitapMaxBits runes are split internally, so the results slice may be longer than the input patches slice. A patch is rejected if its location cannot be found within Matcher.Threshold or if the fuzzy edit-distance ratio exceeds DeleteThreshold. Use context.WithTimeout to limit time spent on fuzzy re-diffing.
func (Patcher) Make ¶
Make computes patches to transform text1 into text2. It diffs the two texts, applies CleanupSemantic and CleanupEfficiency (using EditCost), then builds the patch list. Use context.WithTimeout to limit the diff computation time.
func (Patcher) MakeFromDiffs ¶
MakeFromDiffs computes patches from diffs alone, reconstructing text1 from the Delete and Equal segments. Prefer MakeFromTextAndDiffs when text1 is already available.
func (Patcher) MakeFromTextAndDiffs ¶
MakeFromTextAndDiffs computes patches that transform text1 into text2, where diffs describes that transformation. text1 must be consistent with the Delete and Equal operations in diffs. Returns nil if diffs is empty.