diff

package
v0.29.1 Latest Latest
Warning

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

Go to latest
Published: May 9, 2022 License: MIT Imports: 9 Imported by: 0

README

Overview

A simple lib to diff two string with pretty output. It also provide some low-level API to customize the output.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var SplitKey = struct{}{}

SplitKey for context

View Source
var ThemeDefault = func(t Type) []gop.Style {
	switch t {
	case AddSymbol:
		return []gop.Style{gop.Green}
	case DelSymbol:
		return []gop.Style{gop.Red}
	case AddWords:
		return []gop.Style{gop.Green}
	case DelWords:
		return []gop.Style{gop.Red}
	case ChunkStart:
		return []gop.Style{gop.Black, gop.BgMagenta}
	}
	return []gop.Style{gop.None}
}

ThemeDefault colors for Sprint

View Source
var ThemeNone = func(t Type) []gop.Style {
	return []gop.Style{gop.None}
}

ThemeNone colors for Sprint

Functions

func Diff

func Diff(x, y string) string

Diff x and y into a human readable string.

func Format

func Format(ts []*Token, theme Theme) string

Format tokens into a human readable string

func Split added in v0.26.0

func Split(s string) []string

Split a line into words

func TokenizeLine

func TokenizeLine(ctx context.Context, x, y string) ([]*Token, []*Token)

TokenizeLine two different lines

func Words added in v0.26.0

func Words(ctx context.Context, lines []*TokenLine)

Words diff

Types

type Comparable

type Comparable interface {
	// Hash for comparison
	Hash() string
	// String returns the full content
	String() string
}

Comparable interface

type Element added in v0.28.3

type Element struct {
	// contains filtered or unexported fields
}

Element of a line or a word

func (*Element) Hash added in v0.28.3

func (e *Element) Hash() string

Hash for comparison

func (*Element) String added in v0.28.3

func (e *Element) String() string

String returns the full content

type Histogram added in v0.28.3

type Histogram map[string][]int

Histogram of a Comparables

type LIS added in v0.28.3

type LIS struct {
	// contains filtered or unexported fields
}

LIS https://en.wikipedia.org/wiki/Longest_increasing_subsequence

func NewLIS added in v0.29.0

func NewLIS(len int, x func(int) int) *LIS

NewLIS helper

func (*LIS) Get added in v0.29.0

func (lis *LIS) Get() []int

Get the LIS

func (*LIS) Length added in v0.29.0

func (lis *LIS) Length() int

Length of LIS

type Occurrence added in v0.29.0

type Occurrence [][]int

Occurrence histogram

func (Occurrence) Complexity added in v0.29.0

func (o Occurrence) Complexity() int

Complexity to find the LCS in m

func (Occurrence) Permutate added in v0.29.0

func (o Occurrence) Permutate(p []int, i int)

Permutate p with i

type Sequence added in v0.28.3

type Sequence []Comparable

Sequence list

func NewText

func NewText(s string) Sequence

NewText from string. It will split the s via newlines.

func NewWords added in v0.26.0

func NewWords(words []string) Sequence

NewWords from string

func (Sequence) Common added in v0.28.3

func (x Sequence) Common(y Sequence) (left, right int)

Common returns the common prefix and suffix between x and y. This function scales down the problem via the first property: https://en.wikipedia.org/wiki/Longest_common_subsequence_problem#First_property

func (Sequence) Histogram added in v0.28.3

func (x Sequence) Histogram() Histogram

Histogram of each Comparable

func (Sequence) IsSubsequenceOf added in v0.28.3

func (x Sequence) IsSubsequenceOf(y Sequence) bool

IsSubsequenceOf returns true if x is a subsequence of y

func (Sequence) LCS added in v0.28.3

func (x Sequence) LCS(ctx context.Context, y Sequence) Sequence

LCS between x and y. This implementation converts the LCS problem into LIS sub problems without recursion. The memory complexity is O(x.Occurrence(y)). The time complexicy is O(x.Occurrence(y).Complexity()). The time complexicy is similar with Myer's diff algorithm, but with more modulized steps, which allows further optimization easier. https://en.wikipedia.org/wiki/Longest_common_subsequence_problem

func (Sequence) Occurrence added in v0.28.3

func (x Sequence) Occurrence(y Sequence) Occurrence

Occurrence returns the position of each element of y in x.

func (Sequence) Reduce added in v0.28.3

func (x Sequence) Reduce(y Sequence) Sequence

Reduce Comparables from x that doesn't exist in y

func (Sequence) String added in v0.28.3

func (x Sequence) String() string

String interface

type Theme added in v0.24.0

type Theme func(t Type) []gop.Style

Theme for diff

type Token

type Token struct {
	Type    Type
	Literal string
}

Token presents a symbol in diff layout

func SpreadTokenLines added in v0.23.2

func SpreadTokenLines(lines []*TokenLine) []*Token

SpreadTokenLines to tokens

func Tokenize added in v0.24.0

func Tokenize(ctx context.Context, x, y string) []*Token

Tokenize x and y into diff tokens with diff words and narrow chunks.

func TokenizeText

func TokenizeText(ctx context.Context, x, y string) []*Token

TokenizeText text block a and b into diff tokens.

type TokenLine added in v0.23.2

type TokenLine struct {
	Type   Type
	Tokens []*Token
}

TokenLine of tokens

func Narrow added in v0.23.2

func Narrow(n int, lines []*TokenLine) []*TokenLine

Narrow the context around each diff section to n lines.

func ParseTokenLines added in v0.23.2

func ParseTokenLines(ts []*Token) []*TokenLine

ParseTokenLines of tokens

type Type

type Type int

Type of token

const (
	// Newline type
	Newline Type = iota
	// Space type
	Space

	// ChunkStart type
	ChunkStart
	// ChunkEnd type
	ChunkEnd

	// SameSymbol type
	SameSymbol
	// SameLine type
	SameLine

	// AddSymbol type
	AddSymbol
	// AddLine type
	AddLine

	// DelSymbol typ
	DelSymbol
	// DelLine type
	DelLine

	// SameWords type
	SameWords
	// AddWords type
	AddWords
	// DelWords type
	DelWords

	// EmptyLine type
	EmptyLine
)

Jump to

Keyboard shortcuts

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