Donner SVG main
0.8.0 ยท 766e862a0.8.0 main
SVG editor and embeddable C⁠+⁠+⁠20 engine.
Loading...
Searching...
No Matches
donner::editor::TextBuffer Class Reference

Manages a collection of lines (the raw text) plus related text operations like insert, delete, and substring extraction. More...

#include "donner/editor/TextBuffer.h"

Classes

struct  LineColumnCursor
 Progress through a line when mapping nondecreasing UTF-8 byte indices. More...

Public Types

using Lines = std::vector<Line>
 Ordered line storage for the text buffer.

Public Member Functions

 TextBuffer ()
 Create an empty text buffer.
void setText (std::string_view text)
 Replace the entire buffer with new text.
std::string getText () const
 Get the entire text buffer as a single string, joined by newlines.
std::size_t byteLength () const
 Byte count of the joined text without constructing a copy of it.
std::string getText (const Coordinates &start, const Coordinates &end) const
 Get the text in [start, end), inclusive of start and exclusive of end.
const Line & getLineGlyphs (int line) const
 Get the text in a given line.
Line & getLineGlyphsMutable (int line)
 Get the text in a given line.
int insertTextAt (Coordinates &where, std::string_view text, bool indent=false)
 Insert text at a given position.
void deleteRange (const Coordinates &start, const Coordinates &end)
 Remove text in [start, end).
Line & insertLine (int index, int column=0)
 Insert an empty line at index, or optionally split at a column in an existing line.
void removeLine (int index)
 Remove a single line at the given index if valid.
void removeLine (int start, int end)
 Remove lines in the range [start, end).
int getLineMaxColumn (int line) const
 Query the max column index of a given line (i.e.
int getLineCharacterCount (int line) const
 Query how many characters (glyphs) are on a given line.
int getCharacterIndex (const Coordinates &coords) const
 Convert (line, column) to the internal character index within its line.
int getCharacterColumn (int line, int index, LineColumnCursor &cursor) const
 Resolve a byte index using a cursor for this line. Reset the cursor when changing lines.
int getCharacterColumn (int line, int index) const
 Resolve a single byte index to its editor column.
std::size_t getByteOffset (const Coordinates &coords) const
 Resolve a coordinate to a byte offset in the full buffer text.
Coordinates getCoordinatesAtByteOffset (std::size_t offset) const
 Resolve a full-buffer byte offset to editor coordinates.
int getTotalLines () const
 The total number of lines in the buffer.

Static Public Member Functions

static int glyphByteLength (char c)
 Byte length of the glyph that starts with c: its UTF-8 sequence length, or 1 for a byte that cannot start a sequence (a stray continuation byte or 0xF8-0xFF), so every walk over glyph bytes advances.

Detailed Description

Manages a collection of lines (the raw text) plus related text operations like insert, delete, and substring extraction.


Class Documentation

◆ donner::editor::TextBuffer::LineColumnCursor

struct donner::editor::TextBuffer::LineColumnCursor

Progress through a line when mapping nondecreasing UTF-8 byte indices.

Class Members
int column = 0 Corresponding editor column, including tab expansion.
int nextByte = 0 Byte position after the last decoded glyph.

Member Function Documentation

◆ deleteRange()

void donner::editor::TextBuffer::deleteRange ( const Coordinates & start,
const Coordinates & end )
inline

Remove text in [start, end).

Merges lines if needed.

Example:

Line0
Line1
Line2

If you remove 'Line0' entirely, you'd pass start.column=0 and end.column=5. If you want to remove multi-line, for example removing 0Lin, pass start (0,4) and end (1,3).

Parameters
startThe start of the range to remove (inclusive).
endThe end of the range to remove (exclusive).

◆ getByteOffset()

std::size_t donner::editor::TextBuffer::getByteOffset ( const Coordinates & coords) const
inline

Resolve a coordinate to a byte offset in the full buffer text.

Parameters
coordsCoordinate to resolve.

◆ getCharacterColumn()

int donner::editor::TextBuffer::getCharacterColumn ( int line,
int index,
LineColumnCursor & cursor ) const
inline

Resolve a byte index using a cursor for this line. Reset the cursor when changing lines.

Parameters
lineZero-based line index.
indexUTF-8 byte index, nondecreasing between calls using the same cursor.
cursorCurrent byte and column position, advanced by this call.
Returns
Editor column at the requested byte index.

◆ getCoordinatesAtByteOffset()

Coordinates donner::editor::TextBuffer::getCoordinatesAtByteOffset ( std::size_t offset) const
inline

Resolve a full-buffer byte offset to editor coordinates.

Offsets are interpreted against getText(), where line breaks are represented by single \n bytes between lines. Offsets that point at a line break resolve to the end of the preceding line; offsets after a line break resolve to the start of the next line.

Parameters
offsetByte offset in the full buffer text.

◆ getLineGlyphs()

const Line & donner::editor::TextBuffer::getLineGlyphs ( int line) const
inline

Get the text in a given line.

Parameters
lineThe line number to get.

◆ getLineGlyphsMutable()

Line & donner::editor::TextBuffer::getLineGlyphsMutable ( int line)
inline

Get the text in a given line.

Parameters
lineThe line number to get.

◆ getLineMaxColumn()

int donner::editor::TextBuffer::getLineMaxColumn ( int line) const
inline

Query the max column index of a given line (i.e.

length of that line).

◆ getText()

std::string donner::editor::TextBuffer::getText ( const Coordinates & start,
const Coordinates & end ) const
inline

Get the text in [start, end), inclusive of start and exclusive of end.

Columns are visual columns, as everywhere else in this buffer, so they are converted to glyph indices before the glyphs are read.

◆ glyphByteLength()

int donner::editor::TextBuffer::glyphByteLength ( char c)
inlinestatic

Byte length of the glyph that starts with c: its UTF-8 sequence length, or 1 for a byte that cannot start a sequence (a stray continuation byte or 0xF8-0xFF), so every walk over glyph bytes advances.

Parameters
cFirst byte of the glyph.

◆ insertLine()

Line & donner::editor::TextBuffer::insertLine ( int index,
int column = 0 )
inline

Insert an empty line at index, or optionally split at a column in an existing line.

Returns a reference to the newly inserted line.

◆ insertTextAt()

int donner::editor::TextBuffer::insertTextAt ( Coordinates & where,
std::string_view text,
bool indent = false )
inline

Insert text at a given position.

Splits lines, handles newlines, etc.

Parameters
whereCoordinates where the text should be inserted.
textThe text to insert (can include newlines).
indentWhether to apply special indentation logic if desired.
Returns
The number of lines inserted (not counting the original partial line).

◆ setText()

void donner::editor::TextBuffer::setText ( std::string_view text)
inline

Replace the entire buffer with new text.

Parameters
textThe new text to load into the buffer.

The documentation for this class was generated from the following file: