piecetable

package
v0.1.1-alpha Latest Latest
Warning

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

Go to latest
Published: May 1, 2026 License: BSD-3-Clause Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrLoading = errors.New("data is loading")

Functions

This section is empty.

Types

type Buffer

type Buffer interface {
	Size() int
	Read(offset, length int) ([]byte, error)
}

BufferType indicates which buffer a text fragment is in.

type BufferType

type BufferType int
const (
	Original BufferType = iota
	Add
)

type LineIndex

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

LineIndex stores start offsets of each line.

func NewLineIndex

func NewLineIndex() *LineIndex

NewLineIndex creates a new empty index.

func (*LineIndex) AppendOffsets

func (li *LineIndex) AppendOffsets(offsets []int, maxAllowed int)

AppendOffsets adds pre-calculated line offsets (used by background indexer). It performs a safety check to ensure offsets are within reasonable bounds.

func (*LineIndex) GetLineAtOffset

func (li *LineIndex) GetLineAtOffset(offset int) int

func (*LineIndex) GetLineOffset

func (li *LineIndex) GetLineOffset(line int) int

GetLineOffset returns byte offset of the specified line start (0-based).

func (*LineIndex) LineCount

func (li *LineIndex) LineCount() int

LineCount returns total number of lines.

func (*LineIndex) Rebuild

func (li *LineIndex) Rebuild(pt *PieceTable)

Rebuild completely reconstructs the line index based on PieceTable.

func (*LineIndex) UpdateAfterDelete

func (li *LineIndex) UpdateAfterDelete(offset, length int)

UpdateAfterDelete incrementally updates the index after data deletion.

func (*LineIndex) UpdateAfterInsert

func (li *LineIndex) UpdateAfterInsert(offset int, data []byte)

UpdateAfterInsert incrementally updates the index after data insertion.

type MemoryBuffer

type MemoryBuffer []byte

func (MemoryBuffer) Read

func (m MemoryBuffer) Read(offset, length int) ([]byte, error)

func (MemoryBuffer) Size

func (m MemoryBuffer) Size() int

type Piece

type Piece struct {
	Buf    BufferType
	Start  int // Offset of the fragment start in the corresponding buffer
	Length int // Piece length
}

Piece describes one text fragment.

type PieceTable

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

PieceTable is a structure for efficient editing of large texts.

func New

func New(text []byte) *PieceTable

New creates a new piece table from original text.

func NewWithBuffer

func NewWithBuffer(buf Buffer) *PieceTable

func (*PieceTable) AppendRange

func (pt *PieceTable) AppendRange(dest []byte, offset, length int) ([]byte, error)

AppendRange appends the specified range to the dest slice without new allocations.

func (*PieceTable) Bytes

func (pt *PieceTable) Bytes() ([]byte, error)

Bytes assembles and returns all current text. Note: for large file rendering in future we'll write ReadAt methods, so as not to unload entire buffer into memory.

func (*PieceTable) Delete

func (pt *PieceTable) Delete(offset, length int)

Delete removes a text fragment of specified length starting from offset.

func (*PieceTable) ForEachRange

func (pt *PieceTable) ForEachRange(fn func(data []byte) error) error

ForEachRange sequentially calls a function for each data fragment. This allows processing text without allocating a single large slice.

func (*PieceTable) GetOriginalBuffer

func (pt *PieceTable) GetOriginalBuffer() Buffer

GetOriginalBuffer returns the underlying original buffer.

func (*PieceTable) GetRange

func (pt *PieceTable) GetRange(offset, length int) ([]byte, error)

GetRange returns a byte slice for the specified range.

func (*PieceTable) GetState

func (pt *PieceTable) GetState() TableState

GetState returns a snapshot of the current table structure.

func (*PieceTable) Insert

func (pt *PieceTable) Insert(offset int, data []byte)

Insert inserts data at the specified offset.

func (*PieceTable) LoadState

func (pt *PieceTable) LoadState(s TableState)

LoadState restores the table structure from a snapshot.

func (*PieceTable) Size

func (pt *PieceTable) Size() int

Size returns current logical length of the text.

func (*PieceTable) String

func (pt *PieceTable) String() string

String returns current text as a string (convenient for tests). Ignore errors here to keep tests simple.

func (*PieceTable) UpdateOriginalBuffer

func (pt *PieceTable) UpdateOriginalBuffer(buf Buffer)

UpdateOriginalBuffer safely replaces the original underlying buffer without losing the current logical state and additions. Used primarily for state recovery after a failed I/O operation.

type TableState

type TableState struct {
	Pieces []Piece
	Size   int
}

TableState represents a snapshot of the PieceTable structure.

func (TableState) Equals

func (s TableState) Equals(other TableState) bool

Equals compares two table states for structural identity.

Jump to

Keyboard shortcuts

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