difflib

package
v0.1.6 Latest Latest
Warning

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

Go to latest
Published: Oct 11, 2026 License: Apache-2.0 Imports: 1 Imported by: 0

Documentation

Overview

Package difflib computes line-level diffs and a similarity ratio between two texts, with no dependencies.

Two consumers: findings attach a unified-style diff of a changed block so a reviewer or an agent can judge without opening the file (docs/SPEC.md §17), and the matcher uses Ratio to decide `rewritten?` when a def vanished and a similar unmarked block appeared (§16, threshold in config, default 0.8).

The algorithm is a longest-common-subsequence over lines. Blocks are small (a function, a section), so O(n·m) memory is fine and simplicity wins over Myers. Ratio follows difflib's definition, 2·matches/(len(a)+len(b)), so the 0.8 default in the spec means what people expect from Python's difflib.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Format

func Format(edits []Edit) string

Format renders edits in the familiar `-`/`+`/space prefixed form, one per line, with a trailing newline after each line. An empty script renders "".

func Lines

func Lines(text string) []string

Lines splits text into lines without a trailing empty element for a final newline, so "a\nb\n" and "a\nb" diff as equal.

func Ratio

func Ratio(a, b []string) float64

Ratio is 2·M/T where M is the number of matching lines (the LCS length) and T the total number of lines in both inputs. Two empty inputs are identical (1.0); one empty input is entirely different (0.0).

func Unified

func Unified(oldText, newText string, context int) string

Unified is the convenience most callers want: diff two texts, keep context lines around changes, render. Identical texts render "".

Types

type Edit

type Edit struct {
	Op   Op
	Line string
}

Edit is one line of a diff with its operation.

func Compact

func Compact(edits []Edit, context int) []Edit

Compact drops equal lines except up to context lines around each change, which is what a finding shows. context of 0 keeps only changed lines.

func Diff

func Diff(a, b []string) []Edit

Diff returns the edit script transforming a into b. Equal lines are kept so callers can render context; use Compact to drop them.

type Op

type Op byte

Op is one edit operation in a diff.

const (
	// OpEqual is a line present in both texts.
	OpEqual Op = ' '
	// OpDelete is a line only in the old text.
	OpDelete Op = '-'
	// OpInsert is a line only in the new text.
	OpInsert Op = '+'
)

Jump to

Keyboard shortcuts

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