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 ¶
- type Buffer
- func (b *Buffer) AppendRunes(dst []rune, begin, end int) []rune
- func (b *Buffer) ForEachLine(fn func(line, start, end int) bool)
- func (b *Buffer) Gen() uint64
- func (b *Buffer) Len() int
- func (b *Buffer) LineCount() int
- func (b *Buffer) LineOfPos(pos int) int
- func (b *Buffer) LineStart(i int) int
- func (b *Buffer) RuneAt(pos int) rune
- func (b *Buffer) Slice(begin, end int) []rune
- func (b *Buffer) Splice(low, high int, ins []rune)
- func (b *Buffer) SpliceString(low, high int, ins string)
- func (b *Buffer) String() string
- func (b *Buffer) Substring(begin, end int) string
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 ¶
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 (*Buffer) AppendRunes ¶
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 ¶
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) LineCount ¶
LineCount returns the number of logical (newline-delimited) lines. An empty buffer has one empty line. O(1).
func (*Buffer) LineOfPos ¶
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 ¶
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) Slice ¶
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 ¶
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 ¶
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 ¶
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 ¶
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).