package edit

import "github.com/pkg/diff/edit"

Package edit provides edit scripts. Edit scripts are a core notion for diffs. They represent a way to go from A to B by a sequence of insertions, deletions, and equal elements.

Index

Types

type Op

type Op int8

An Op is a edit operation in a Script.

const (
	Del Op = -1 // delete
	Eq  Op = 0  // equal
	Ins Op = 1  // insert
)

func (Op) String

func (i Op) String() string

type Range

type Range struct {
	LowA, HighA int
	LowB, HighB int
}

A Range is a pair of clopen index ranges. It represents the elements A[LowA:HighA] and B[LowB:HighB].

func (*Range) IsDelete

func (r *Range) IsDelete() bool

IsDelete reports whether r represents a deletion in a Script. If so, the deleted elements are A[LowA:HighA].

func (*Range) IsEqual

func (r *Range) IsEqual() bool

IsEqual reports whether r represents a series of equal elements in a Script. If so, the elements A[LowA:HighA] are equal to the elements B[LowB:HighB].

func (*Range) IsInsert

func (r *Range) IsInsert() bool

IsInsert reports whether r represents an insertion in a Script. If so, the inserted elements are B[LowB:HighB].

func (*Range) Len

func (r *Range) Len() int

Len reports the number of elements in r. In a deletion, it is the number of deleted elements. In an insertion, it is the number of inserted elements. For equal elements, it is the number of equal elements.

func (*Range) Op

func (r *Range) Op() Op

Op reports what kind of operation r represents. This can also be determined by calling r.IsInsert, r.IsDelete, and r.IsEqual, but this form is sometimes more convenient to use.

type Script

type Script struct {
	Ranges []Range
}

A Script is an edit script to alter A into B.

func NewScript

func NewScript(r ...Range) Script

NewScript returns a Script containing the ranges r. It is only a convenience wrapper used to reduce line noise.

func (*Script) IsIdentity

func (s *Script) IsIdentity() bool

IsIdentity reports whether s is the identity edit script, that is, whether A and B are identical.

func (*Script) Stat

func (s *Script) Stat() (ins, del int)

Stat reports the total number of insertions and deletions in s.

Source Files

edit.go op_string.go

Version
v0.0.0-20241224192749-4e6772a4315c (latest)
Published
Dec 24, 2024
Platform
js/wasm
Imports
3 packages
Last checked
17 hours ago

Tools for package owners.