diff

package module
v0.0.0-...-bd26f6c Latest Latest
Warning

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

Go to latest
Published: Mar 26, 2020 License: MIT Imports: 5 Imported by: 0

README

diff

offers algorithms to perform operations required for synchronizing bytes.

Based on myestr diff, is backward compatible, and closures provide better room for expansion

Usage

The following example compares two text1 and text2, then patch differences to text1.

package main

import (
	"fmt"

	"github.com/arex0/diff"
)

var text1 = `Hamlet: Do you see yonder cloud that's almost in shape of a camel?
Polonius: By the mass, and 'tis like a camel, indeed.
Hamlet: Methinks it is like a weasel.
Polonius: It is backed like a weasel.
Hamlet: Or like a whale?
Polonius: Very like a whale.
-- Shakespeare`
var text2 = `Hamlet: Do you see the cloud over there that's almost the shape of a camel?
Polonius: By golly, it is like a camel, indeed.
Hamlet: I think it looks like a weasel.
Polonius: It is shaped like a weasel.
Hamlet: Or like a whale?
Polonius: It's totally like a whale.
-- Shakespeare`

var b1 = []byte(text1)
var b2 = []byte(text2)

func main() {
    diffs := diff.Get(b1, b2, diff.WithChecklines(true), diff.WithSemantic(true))
    patch := diffs.ToPatch()
    newText, err := diff.Patch(b1, patch)
    if err != nil{
        // log
    }
    // newText == text2
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Patch

func Patch(src, patch []byte) ([]byte, error)

Patch update src with a patch, return a newer []byte and nil or nil and a error.

Types

type Diff

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

Diff contains diff type and bytes.

type Diffs

type Diffs []Diff

Diffs is []Diff define to call func(Diffs){}.

func Get

func Get(b1, b2 []byte, opts ...Option) Diffs

Get return difference of two []byte, using [ Myer's diff algorithm ](https://neil.fraser.name/writing/diff/myestr.pdf). Export to help users use extended options.

func (Diffs) ToPatch

func (diffs Diffs) ToPatch() []byte

ToPatch prase a Diffs to []byte for transfer, than call Patch(src,patch) to sync difference.

type Operation

type Operation int

Operation explains diff type.

const (
	Equals Operation = iota
	Delete
	Insert
)

diff include three type operation.

type Option

type Option func(*options)

Option export to user for easy expansion.

func WithChecklines

func WithChecklines(checklines bool) Option

WithChecklines is a speedup flag, if false, then don't run a ine-level diff first to identify the changed areas, if true, then run a faster slightly less optimal diff.

func WithDeadline

func WithDeadline(deadline time.Time) Option

WithDeadline set last time when the diff should be complete by.

func WithEditcost

func WithEditcost(editcost int) Option

WithEditcost expanding the length of a diff by editcost(defalut is 4)(16: size of struct diff, 4: size of patch token), then that optimisation will reduce the total costs.

func WithSemantic

func WithSemantic(semantic bool) Option

WithSemantic is a flag, default is false, if true make Diffs is to be human-readable.

Jump to

Keyboard shortcuts

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