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 ¶
- type Cursor
- type Snapshot
- func (s Snapshot) ByteAt(index int) byte
- func (s Snapshot) ByteOffset(index int) int
- func (s Snapshot) ChangedRange(other Snapshot) (start, oldEnd, newEnd int)
- func (s Snapshot) Chunks(start, end int) iter.Seq[string]
- func (s Snapshot) Cursor(start, end int) Cursor
- func (s Snapshot) DecodeRune(offset int) (rune, int)
- func (s Snapshot) Equal(other Snapshot) bool
- func (s Snapshot) EqualRange(start, end int, other Snapshot, otherStart, otherEnd int) bool
- func (s Snapshot) IndexByte(start int, b byte) int
- func (s Snapshot) Len() int
- func (s Snapshot) LineStart(line int) int
- func (s Snapshot) Lines() int
- func (s Snapshot) Materialized() bool
- func (s Snapshot) Position(offset int) (line, column int)
- func (s Snapshot) ReadAt(p []byte, offset int64) (int, error)
- func (s Snapshot) Replace(start, end int, value string) Snapshot
- func (s Snapshot) RuneOffset(offset int) int
- func (s Snapshot) Runes() int
- func (s Snapshot) Slice(start, end int) string
- func (s Snapshot) String() string
- func (s Snapshot) TrimSpaceEmpty() bool
- type Tree
- func (t *Tree) ApplySnapshot(snapshot string, start, oldEnd, newEnd int)
- func (t *Tree) ByteOffset(index int) int
- func (t *Tree) Compact()
- func (t *Tree) Len() int
- func (t *Tree) LineStart(line int) int
- func (t *Tree) Lines() int
- func (t *Tree) Matches(value string) bool
- func (t *Tree) Position(offset int) (line, column int)
- func (t *Tree) ReadAt(dst []byte, offset int64) (int, error)
- func (t *Tree) Replace(start, end int, value string)
- func (t *Tree) Reserve(editBytes, nodes int)
- func (t *Tree) Reset(value string)
- func (t *Tree) RetainedBytes() int
- func (t *Tree) RetainedEditBytes() int
- func (t *Tree) RuneOffset(offset int) int
- func (t *Tree) Runes() int
- func (t *Tree) Slice(start, end int) string
- func (t *Tree) String() string
- type Value
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) DecodeRune ¶
DecodeRune peeks at the next rune. A zero size marks the end of the range.
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 ¶
FromString wraps immutable text without allocating or indexing it.
func (Snapshot) ByteOffset ¶
func (Snapshot) ChangedRange ¶
ChangedRange finds a single encompassing edit, preserving rune boundaries. Chunk comparison skips unchanged text without materializing either version.
func (Snapshot) EqualRange ¶
EqualRange compares two byte ranges without joining pieces into strings.
func (Snapshot) Materialized ¶
func (Snapshot) Replace ¶
Replace returns a new version, owning the inserted bytes. Callers provide UTF-8 byte boundaries, as with Tree.Replace.
func (Snapshot) RuneOffset ¶
func (Snapshot) Slice ¶
Slice shares a contiguous piece, copying only when the range crosses pieces.
func (Snapshot) TrimSpaceEmpty ¶
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 ¶
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 ¶
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) Lines ¶
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) ReadAt ¶
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 ¶
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) RetainedBytes ¶
RetainedBytes includes the original buffer and discarded edit bytes.
func (*Tree) RetainedEditBytes ¶
RetainedEditBytes reports append-buffer usage, including deleted bytes.
func (*Tree) RuneOffset ¶
RuneOffset maps a clamped byte offset to a rune offset, rounding down inside a rune.