diff

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2026 License: Apache-2.0 Imports: 2 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package diff computes Myers line diffs and unified-diff hunks.

It replaces the original `pi-diff` crate, which the TypeScript side reached through a NAPI binding. The run structure is a faithful port of the original's O(ND) implementation, including its tie-breaking, so a diff produced here matches one produced there token for token.

Index

Constants

View Source
const DefaultContextLines = 3

DefaultContextLines is the context either side of a change when the caller does not choose. It matches git's default.

View Source
const NoNewlineMarker = `\ No newline at end of file`

NoNewlineMarker is the line unified diff emits after a row that ended without a trailing newline.

Variables

This section is empty.

Functions

func Intern

func Intern(oldTokens, newTokens []string) ([]uint32, []uint32)

Intern maps tokens to dense ids under exact equality.

Interning is what makes the comparison O(1) per token: the algorithm works on integers, and a generic token would be compared with a hash per step.

func LineTokens

func LineTokens(text string) []string

LineTokens splits text into jsdiff-compatible line tokens.

The trailing newline stays with its line, so joining the tokens reproduces the input exactly — including a final line that lacks one.

Go's SplitAfter emits a trailing empty element for a text ending in a newline, where the original's split_inclusive does not. That empty token would become a phantom context line, so it is dropped here: the difference is a hunk header claiming one more line than the file has.

func Render

func Render(hunks []Hunk) string

Render renders hunks as a unified diff body.

func Unified

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

Unified renders the complete unified diff between two texts.

Types

type Change

type Change struct {
	// Value is the joined token text for this run.
	Value string
	// Count is the number of tokens in this run.
	Count int
	// Added is true when the run exists only in the new input.
	Added bool
	// Removed is true when the run exists only in the old input.
	Removed bool
}

Change is one run mapped back to its text.

func BuildChanges

func BuildChanges(runs []Run, oldTokens, newTokens []string) []Change

BuildChanges maps runs back to values.

Common runs take their text from the new tokens, because the two texts agree there and only the new one carries the version that was kept.

func DiffLines

func DiffLines(oldText, newText string) []Change

DiffLines returns the line-level changes between two texts.

type Hunk

type Hunk struct {
	// OldStart is the 1-based first line in the old text.
	OldStart int
	// OldLines counts old-text lines covered.
	OldLines int
	// NewStart is the 1-based first line in the new text.
	NewStart int
	// NewLines counts new-text lines covered.
	NewLines int
	// Lines are the body rows.
	Lines []HunkLine
}

Hunk is one contiguous region of a unified diff.

Starts are 1-based and lengths count the lines of each text the hunk covers, matching the `@@ -old_start,old_lines +new_start,new_lines @@` header.

func HunksFromTokens

func HunksFromTokens(oldTokens, newTokens []string, runs []Run, contextLines int) []Hunk

HunksFromTokens builds hunks from precomputed line tokens and runs.

Splitting once and reusing the tokens is what lets a caller render the same diff twice without re-tokenising.

func StructuredHunks

func StructuredHunks(oldText, newText string, contextLines int) []Hunk

StructuredHunks builds unified hunks from two texts.

contextLines is the number of unchanged lines either side of a change; zero uses DefaultContextLines. Two changes within contextLines of each other are merged into one hunk, which is what keeps a scattered edit readable.

func (Hunk) Header

func (h Hunk) Header() string

Header renders the "@@ -a,b +c,d @@" line for this hunk.

A single-line side omits its count, as unified diff does: `-1` rather than `-1,1`.

type HunkLine

type HunkLine struct {
	// Kind is the row's role.
	Kind HunkLineKind
	// Text is the line content, without its trailing newline.
	Text string
}

HunkLine is one row of a unified hunk.

type HunkLineKind

type HunkLineKind string

HunkLineKind is the role of one row in a unified hunk.

const (
	// KindContext is " ": the line is in both texts.
	KindContext HunkLineKind = "context"
	// KindRemoved is "-": the line is only in the old text.
	KindRemoved HunkLineKind = "removed"
	// KindAdded is "+": the line is only in the new text.
	KindAdded HunkLineKind = "added"
	// KindNoNewlineAtEof marks a preceding row that ended without a newline.
	KindNoNewlineAtEof HunkLineKind = "no-newline"
)

Hunk row kinds.

type Run

type Run struct {
	// Count is the number of tokens in this run.
	Count int
	// Added is true when the run exists only in the new input.
	Added bool
	// Removed is true when the run exists only in the old input.
	Removed bool
}

Run is one contiguous span of the diff.

Exactly one of Added or Removed is set; a run with neither is a common run present in both inputs.

func LineRuns

func LineRuns(oldText, newText string) []Run

LineRuns returns the diff runs for two texts at line granularity.

func Myers

func Myers(oldTokens, newTokens []uint32) []Run

Myers computes the O(ND) diff of two interned token sequences.

The tie-breaking is deliberate and load-bearing: when a removal and an insertion reach equally far, the insertion path is preferred, matching the original. Changing the preference produces a different — still valid — edit script, which is why the run structure is asserted rather than just the resulting text.

Jump to

Keyboard shortcuts

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