diff

package
v0.53.1 Latest Latest
Warning

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

Go to latest
Published: Mar 2, 2024 License: BSD-3-Clause Imports: 8 Imported by: 0

Documentation

Overview

Package diff implement text comparison.

Index

Constants

View Source
const (
	// LevelLines define that we want only lines change set.
	LevelLines = iota
	// LevelWords define that we want the change not only capture the
	// different per line, but also changes inside the line.
	LevelWords
)
View Source
const (
	// DefMatchLen minimum number of bytes used for searching the next
	// matched chunk in line.
	DefMatchLen = 5
	// DefMatchRatio define default minimum match ratio to be considered as
	// change.
	DefMatchRatio = 0.7
)

Variables

View Source
var (
	// DefDelimiter define default delimiter for new line.
	DefDelimiter = byte('\n')
)

Functions

func Bytes

func Bytes(old, new []byte, atx, aty int) (adds, dels text.Chunks)

Bytes given two similar lines, find and return the differences (additions and deletion) between them.

Case 1: addition on new or deletion on old.

old: 00000
new: 00000111

or

old: 00000111
new: 00000

Case 2: addition on new line

old: 000000
new: 0001000

Case 3: deletion on old line (reverse of case 2)

old: 0001000
new: 000000

Case 4: change happened in the beginning

old: 11000
new: 22000

Case 5: both changed

old: 0001000
new: 0002000

func BytesRatio

func BytesRatio(old, newline []byte, minTokenLen int) (ratio float32, m int, maxlen int)

BytesRatio compare two slice of bytes and return ratio of matching bytes. The ratio in in range of 0.0 to 1.0, where 1.0 if both are similar, and 0.0 if no matchs even found. `minTokenLen` define the minimum length of token for searching in both of slice.

func IsEqual added in v0.29.1

func IsEqual(oldb, newb []byte) (equal bool)

IsEqual compare two slice of bytes and return true if equal or false otherwise.

func ReadLines

func ReadLines(f string) (lines text.Lines, e error)

ReadLines return lines in the file `f`.

Types

type Data

type Data struct {
	Adds    text.Lines
	Dels    text.Lines
	Changes LineChanges

	IsMatched bool
}

Data represent additions, deletions, and changes between two text. If no difference found, the IsMatched will be true.

func Files

func Files(oldf, newf string, level int) (diffs Data, e error)

Files compare two files.

func Lines

func Lines(oldlines, newlines text.Lines, level int) (diffs Data)

Lines search the difference between two Lines.

func Text added in v0.29.1

func Text(before, after []byte, level int) (diffs Data)

Text search the difference between two texts.

func (*Data) GetAllAdds

func (diffs *Data) GetAllAdds() (chunks text.Chunks)

GetAllAdds return chunks of additions including in line changes.

func (*Data) GetAllDels

func (diffs *Data) GetAllDels() (chunks text.Chunks)

GetAllDels return chunks of deletions including in line changes.

func (*Data) PushAdd

func (diffs *Data) PushAdd(new text.Line)

PushAdd will add new line to diff set.

func (*Data) PushChange

func (diffs *Data) PushChange(old, new text.Line)

PushChange set to diff data.

func (*Data) PushDel

func (diffs *Data) PushDel(old text.Line)

PushDel will add deletion line to diff set.

func (Data) String

func (diffs Data) String() (s string)

String return formatted data.

type LineChange

type LineChange struct {
	Adds text.Chunks
	Dels text.Chunks

	Old text.Line
	New text.Line
}

LineChange represent one change in text.

func NewLineChange

func NewLineChange(old, new text.Line) *LineChange

NewLineChange create a pointer to new LineChange object.

func (LineChange) String

func (change LineChange) String() string

String return formatted content of LineChange.

type LineChanges

type LineChanges []LineChange

LineChanges represents a set of change in text.

func (*LineChanges) GetAllAdds

func (changes *LineChanges) GetAllAdds() (allAdds text.Chunks)

GetAllAdds return all addition chunks.

func (*LineChanges) GetAllDels

func (changes *LineChanges) GetAllDels() (allDels text.Chunks)

GetAllDels return all deleted chunks.

Jump to

Keyboard shortcuts

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