diff

package
v0.12.0-nightly.20241004 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2024 License: Apache-2.0 Imports: 7 Imported by: 0

README

Diff

This package contains code taken from https://github.com/golang/tools/tree/master/internal/diff on February 15th, 2024. We need the code to create a unified diff between two strings.

The code is left as-is, except 3 changes:

  • The imports were changed to reference the Conduit module path. This was done using the following command:

    find . -type f -exec sed -i '' 's/golang.org\/x\/tools\/internal/github.com\/conduitio\/conduit\/pkg\/plugin\/processor\/builtin\/internal/g' {} +
    
  • The package golang.org/x/tools/internal/diff/myers was removed, as it's deprecated.

  • The package golang.org/x/tools/internal/testenv was added into the diff package, as that's the only place it's used. It also only includes the required functions.

Documentation

Overview

Package diff computes differences between text files or strings.

Index

Constants

View Source
const DefaultContextLines = 3

DefaultContextLines is the number of unchanged lines of surrounding context displayed by Unified. Use ToUnified to specify a different value.

Variables

This section is empty.

Functions

func Apply

func Apply(src string, edits []Edit) (string, error)

Apply applies a sequence of edits to the src buffer and returns the result. Edits are applied in order of start offset; edits with the same start offset are applied in they order they were provided.

Apply returns an error if any edit is out of bounds, or if any pair of edits is overlapping.

func ApplyBytes

func ApplyBytes(src []byte, edits []Edit) ([]byte, error)

ApplyBytes is like Apply, but it accepts a byte slice. The result is always a new array.

func SortEdits

func SortEdits(edits []Edit)

SortEdits orders a slice of Edits by (start, end) offset. This ordering puts insertions (end = start) before deletions (end > start) at the same point, but uses a stable sort to preserve the order of multiple insertions at the same point. (Apply detects multiple deletions at the same point as an error.)

func ToUnified

func ToUnified(oldLabel, newLabel, content string, edits []Edit, contextLines int) (string, error)

ToUnified applies the edits to content and returns a unified diff, with contextLines lines of (unchanged) context around each diff hunk. The old and new labels are the names of the content and result files. It returns an error if the edits are inconsistent; see ApplyEdits.

func Unified

func Unified(oldLabel, newLabel, old, new string) string

Unified returns a unified diff of the old and new strings. The old and new labels are the names of the old and new files. If the strings are equal, it returns the empty string.

Types

type Edit

type Edit struct {
	Start, End int    // byte offsets of the region to replace
	New        string // the replacement
}

An Edit describes the replacement of a portion of a text file.

func Bytes

func Bytes(before, after []byte) []Edit

Bytes computes the differences between two byte slices. The resulting edits respect rune boundaries.

func Strings

func Strings(before, after string) []Edit

Strings computes the differences between two strings. The resulting edits respect rune boundaries.

func (Edit) String

func (e Edit) String() string

Directories

Path Synopsis
Package difftest supplies a set of tests that will operate on any implementation of a diff algorithm as exposed by "github.com/conduitio/conduit/pkg/plugin/processor/builtin/internal/diff"
Package difftest supplies a set of tests that will operate on any implementation of a diff algorithm as exposed by "github.com/conduitio/conduit/pkg/plugin/processor/builtin/internal/diff"
package lcs contains code to find longest-common-subsequences (and diffs)
package lcs contains code to find longest-common-subsequences (and diffs)

Jump to

Keyboard shortcuts

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