textbuf

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Oct 22, 2026 License: BSD-3-Clause Imports: 4 Imported by: 0 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 textbuf provides the editable text storage backing the lazy single-segment editing path of widget.Entry / widget.RichText (pre-rebrand tracker #381).

A Buffer is addressed by rune offset and keeps a logical-line index (newline-delimited) alongside the content. Its String method is memoized: repeated calls without an intervening splice return the same string object, which callers use as an O(1) pointer-equality identity anchor (seg.Text == buf.String()) to detect external writes.

Buffers are not safe for concurrent use; like all widget state they must be accessed from the main goroutine only.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Buffer

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

Buffer is an editable UTF-8 text buffer: a flat-spine rope of fixed-capacity byte chunks with per-chunk rune and newline counts, addressed by rune offset. Edits are O(edit + chunkMax) plus an O(chunks) deferred index rebuild; the full-string materialization is one alloc + memcpy (the payload is already UTF-8 — nothing is re-encoded).

func FromString

func FromString(s string) *Buffer

FromString builds a Buffer holding s in one pass (chunk copy + counts; no []rune decode). The memo is seeded with s itself, so the first String call returns the same string object that was passed in. Invalid UTF-8 in s is normalized to U+FFFD, exactly as []rune(s) does.

func New

func New() *Buffer

New returns an empty Buffer.

func (*Buffer) AppendRunes

func (b *Buffer) AppendRunes(dst []rune, begin, end int) []rune

AppendRunes appends the rune range [begin, end) to dst and returns the extended slice, letting callers reuse a scratch buffer. Panics if the range is invalid.

func (*Buffer) ForEachLine

func (b *Buffer) ForEachLine(fn func(line, start, end int) bool)

ForEachLine calls fn for every logical line with its rune range [start, end) excluding the trailing newline, stopping early if fn returns false. One O(bytes) chunk walk using vectorized newline search and rune counting — no per-line binary searches or per-rune decode loops.

func (*Buffer) Gen

func (b *Buffer) Gen() uint64

Gen returns a generation counter that increases on every mutation.

func (*Buffer) Len

func (b *Buffer) Len() int

Len returns the content length in runes. O(1).

func (*Buffer) LineCount

func (b *Buffer) LineCount() int

LineCount returns the number of logical (newline-delimited) lines. An empty buffer has one empty line. O(1).

func (*Buffer) LineOfPos

func (b *Buffer) LineOfPos(pos int) int

LineOfPos returns the logical line containing rune position pos: the largest line whose start is <= pos. Positions past the end map to the last line.

func (*Buffer) LineStart

func (b *Buffer) LineStart(i int) int

LineStart returns the rune offset of line i's first rune. Panics if i is out of range. O(log chunks + one chunk scan), newline search vectorized.

func (*Buffer) RuneAt

func (b *Buffer) RuneAt(pos int) rune

RuneAt returns the rune at position pos. Panics if pos is out of range.

func (*Buffer) Slice

func (b *Buffer) Slice(begin, end int) []rune

Slice returns a copy of the rune range [begin, end). The result does not alias the buffer and stays valid across later splices.

func (*Buffer) Splice

func (b *Buffer) Splice(low, high int, ins []rune)

Splice replaces the rune range [low, high) with ins. Out-of-range bounds are clamped. Runes that are not valid Unicode scalar values are stored as U+FFFD, matching how string(ins) would encode them.

func (*Buffer) SpliceString

func (b *Buffer) SpliceString(low, high int, ins string)

SpliceString is Splice with the insertion provided as a string, avoiding a redundant re-encode of ins when the caller already has one.

func (*Buffer) String

func (b *Buffer) String() string

String materializes the whole buffer. Memoized: repeated calls without an intervening splice return the same string object — the identity contract widget.RichText's fast edit path relies on. A miss costs one allocation and a chunk-walk memcpy of the content bytes.

func (*Buffer) Substring

func (b *Buffer) Substring(begin, end int) string

Substring returns the text of the rune range [begin, end) as a string: from the memo when it is clean, otherwise gathered from the chunks. The whole range is the memo itself (the string-identity contract), but any part of it is a copy with memory of its own. A zero-copy slice shares the memo's memory, and callers keep short ones (row text, line caches) long after the next edit replaces the memo: each kept a whole document alive, a full copy per keystroke (#508).

Jump to

Keyboard shortcuts

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