piecetree

package
v0.1.3 Latest Latest
Warning

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

Go to latest
Published: Nov 9, 2026 License: MIT Imports: 7 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 piecetree stores UTF-8 text in an AVL tree of references into an immutable original buffer and an append-only edit buffer. Tree is owned by one goroutine; String returns immutable snapshots safe to share with workers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Cursor

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

Cursor reads a byte range in order, looking up the tree only when crossing a piece. Copies are independent and keep their immutable snapshot alive.

func (*Cursor) Advance

func (c *Cursor) Advance(bytes int)

func (*Cursor) Byte

func (c *Cursor) Byte() byte

Byte returns the next byte, or zero at the end of the range.

func (*Cursor) DecodeRune

func (c *Cursor) DecodeRune() (rune, int)

DecodeRune peeks at the next rune. A zero size marks the end of the range.

func (*Cursor) Offset

func (c *Cursor) Offset() int

type Snapshot

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

Snapshot is an immutable, comparable text value. Copies share unchanged pieces; neither editing nor reading a range requires a whole-document copy. String explicitly materializes a version, once, and is safe for worker use. The zero value is empty. Snapshot and its nodes must never be pooled: readers may retain any published version independently of the editing buffer.

func FromString

func FromString(value string) Snapshot

FromString wraps immutable text without allocating or indexing it.

func Text

func Text[V Value](value V) Snapshot

func (Snapshot) ByteAt

func (s Snapshot) ByteAt(index int) byte

func (Snapshot) ByteOffset

func (s Snapshot) ByteOffset(index int) int

func (Snapshot) ChangedRange

func (s Snapshot) ChangedRange(other Snapshot) (start, oldEnd, newEnd int)

ChangedRange finds a single encompassing edit, preserving rune boundaries. Chunk comparison skips unchanged text without materializing either version.

func (Snapshot) Chunks

func (s Snapshot) Chunks(start, end int) iter.Seq[string]

Chunks visits immutable pieces of a byte range without allocating.

func (Snapshot) Cursor

func (s Snapshot) Cursor(start, end int) Cursor

func (Snapshot) DecodeRune

func (s Snapshot) DecodeRune(offset int) (rune, int)

func (Snapshot) Equal

func (s Snapshot) Equal(other Snapshot) bool

func (Snapshot) EqualRange

func (s Snapshot) EqualRange(start, end int, other Snapshot, otherStart, otherEnd int) bool

EqualRange compares two byte ranges without joining pieces into strings.

func (Snapshot) IndexByte

func (s Snapshot) IndexByte(start int, b byte) int

func (Snapshot) Len

func (s Snapshot) Len() int

func (Snapshot) LineStart

func (s Snapshot) LineStart(line int) int

func (Snapshot) Lines

func (s Snapshot) Lines() int

func (Snapshot) Materialized

func (s Snapshot) Materialized() bool

func (Snapshot) Position

func (s Snapshot) Position(offset int) (line, column int)

func (Snapshot) ReadAt

func (s Snapshot) ReadAt(p []byte, offset int64) (int, error)

func (Snapshot) Replace

func (s Snapshot) Replace(start, end int, value string) Snapshot

Replace returns a new version, owning the inserted bytes. Callers provide UTF-8 byte boundaries, as with Tree.Replace.

func (Snapshot) RuneOffset

func (s Snapshot) RuneOffset(offset int) int

func (Snapshot) Runes

func (s Snapshot) Runes() int

func (Snapshot) Slice

func (s Snapshot) Slice(start, end int) string

Slice shares a contiguous piece, copying only when the range crosses pieces.

func (Snapshot) String

func (s Snapshot) String() string

func (Snapshot) TrimSpaceEmpty

func (s Snapshot) TrimSpaceEmpty() bool

type Tree

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

Tree's zero value is an empty document. Do not copy a populated Tree. Nodes use arena indices so rotations and free-list reuse do not allocate.

func (*Tree) ApplySnapshot

func (t *Tree) ApplySnapshot(snapshot string, start, oldEnd, newEnd int)

ApplySnapshot applies a known change from the current text to snapshot and caches the supplied immutable snapshot without copying it. The caller must supply a complete changed range; text outside that range must be unchanged.

func (*Tree) ByteOffset

func (t *Tree) ByteOffset(index int) int

ByteOffset maps a clamped rune offset to bytes in O(log(pieces)+pieceBytes).

func (*Tree) Compact

func (t *Tree) Compact()

Compact reclaims deleted bytes and unused arena nodes. Copy directly from the tree: String may still share a large backing store after tail deletion. Previously published snapshots remain immutable.

func (*Tree) Len

func (t *Tree) Len() int

func (*Tree) LineStart

func (t *Tree) LineStart(line int) int

LineStart returns the byte offset of a clamped zero-based line.

func (*Tree) Lines

func (t *Tree) Lines() int

Lines counts LF-delimited lines, including the empty line after a final LF. CRLF is preserved verbatim and counts as one line break.

func (*Tree) Matches

func (t *Tree) Matches(value string) bool

Matches checks the cached snapshot without materializing a new version.

func (*Tree) Position

func (t *Tree) Position(offset int) (line, column int)

Position returns zero-based LF line and rune column for a byte offset.

func (*Tree) ReadAt

func (t *Tree) ReadAt(dst []byte, offset int64) (int, error)

ReadAt copies bytes from the tree without materializing a string snapshot. Like other tree operations, it must not run concurrently with mutations.

func (*Tree) Replace

func (t *Tree) Replace(start, end int, value string)

Replace edits a byte range. Offsets must be at UTF-8 rune boundaries. It copies only the inserted text; unchanged document bytes never move.

func (*Tree) Reserve

func (t *Tree) Reserve(editBytes, nodes int)

Reserve preallocates edit bytes and nodes for allocation-free mutations.

func (*Tree) Reset

func (t *Tree) Reset(value string)

Reset releases the previous document and indexes the original in one pass.

func (*Tree) RetainedBytes

func (t *Tree) RetainedBytes() int

RetainedBytes includes the original buffer and discarded edit bytes.

func (*Tree) RetainedEditBytes

func (t *Tree) RetainedEditBytes() int

RetainedEditBytes reports append-buffer usage, including deleted bytes.

func (*Tree) RuneOffset

func (t *Tree) RuneOffset(offset int) int

RuneOffset maps a clamped byte offset to a rune offset, rounding down inside a rune.

func (*Tree) Runes

func (t *Tree) Runes() int

func (*Tree) Slice

func (t *Tree) Slice(start, end int) string

Slice returns an owned string for the byte range.

func (*Tree) String

func (t *Tree) String() string

String materializes at most one immutable snapshot per changed version.

type Value

type Value interface{ string | Snapshot }

Value permits the same geometry helpers to accept labels and document text.

Jump to

Keyboard shortcuts

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