Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
portals::GraphemeText Class Reference

Owning validated UTF-8 and immutable extended grapheme measurements. More...

#include <text.hpp>

Public Member Functions

 GraphemeText (const GraphemeText &other)=default
 Copy bytes and metadata into independent storage; may allocate.
GraphemeText & operator= (const GraphemeText &other)
 Replace storage with an independent copy; invalidates borrowed views.
 GraphemeText (GraphemeText &&other)
 Transfer storage and empty the source; may allocate debug bookkeeping before changing the source.
GraphemeText & operator= (GraphemeText &&other) noexcept
 Transfer storage; invalidates both objects' views and empties a distinct source. Self-move is a no-op.
bool operator== (const GraphemeText &other) const noexcept
 Compare logical UTF-8 bytes and the complete captured width policy without allocating.
std::string_view bytes () const noexcept
 Borrow original bytes until this object's assignment/move/destruction.
std::span< const Grapheme > graphemes () const noexcept
 Borrow metadata until this object's assignment/move/destruction.
std::int32_t measure () const noexcept
 Return cached int32_t-compatible total cells; no allocation or errors.
WidthPolicy policy () const noexcept
 Return the owning captured cache-key policy; no errors.
std::expected< std::size_t, TextError > nextCursor (std::size_t byte_offset) const noexcept
 Advance a byte-boundary cursor by one cluster; end stays at end.
std::expected< std::size_t, TextError > previousCursor (std::size_t byte_offset) const noexcept
 Retreat a byte-boundary cursor by one cluster; zero stays at zero.
TextClip clipPrefix (std::int32_t max_cells) const noexcept
 Return the maximal prefix fitting a cell budget, stopping before a wide cluster that cannot fit.

Static Public Member Functions

static std::expected< GraphemeText, TextError > from (std::string_view bytes, WidthPolicy policy={}, TextLimits limits={})
 Validate, segment and measure a single logical UTF-8 buffer.

Friends

struct detail::EditStorageAccess

Detailed Description

Owning validated UTF-8 and immutable extended grapheme measurements.

Unicode 18.0.0 boundaries are independent of display support: controls, standalone marks and unsupported widths remain whole logical clusters. No I/O, callbacks or mutable global state. Construction/copy can allocate and throw std::bad_alloc or std::length_error; semantic failures use TextError. Any thread may own a value; concurrent reads of one unchanged value are safe. Assignment/move/destruction require caller synchronization and invalidate all borrowed views. Moving resets a distinct source to empty; self-move assignment is a no-op. Policy is captured at construction; replacing a value under a new policy requires layout/paint invalidation by the caller. This component does not own terminal cells.

Constructor & Destructor Documentation

◆ GraphemeText() [1/2]

portals::GraphemeText::GraphemeText ( const GraphemeText & other)
default

Copy bytes and metadata into independent storage; may allocate.

Parameters
otherValidated text whose bytes, metadata and policy are copied.
Exceptions
std::bad_allocIf independent storage or debug bookkeeping cannot be allocated.

◆ GraphemeText() [2/2]

portals::GraphemeText::GraphemeText ( GraphemeText && other)

Transfer storage and empty the source; may allocate debug bookkeeping before changing the source.

Parameters
otherSource whose storage is transferred and whose size becomes zero on success.
Exceptions
std::bad_allocIf preliminary bookkeeping allocation fails; other remains unchanged.

Member Function Documentation

◆ bytes()

std::string_view portals::GraphemeText::bytes ( ) const
nodiscardnoexcept

Borrow original bytes until this object's assignment/move/destruction.

Returns
A read-only view of the original validated UTF-8, including unsupported logical clusters.

◆ clipPrefix()

TextClip portals::GraphemeText::clipPrefix ( std::int32_t max_cells) const
nodiscardnoexcept

Return the maximal prefix fitting a cell budget, stopping before a wide cluster that cannot fit.

Parameters
max_cellsAvailable cells; negative values normalize to zero.
Returns
Independent metadata at a whole-cluster boundary, with no partial glyph.
Note
Any thread; no mutation, allocation, exceptions or invalidation.

◆ from()

std::expected< GraphemeText, TextError > portals::GraphemeText::from ( std::string_view bytes,
WidthPolicy policy = {},
TextLimits limits = {} )
staticnodiscard

Validate, segment and measure a single logical UTF-8 buffer.

Parameters
bytesBorrowed only for this synchronous call; copied on success.
policyOwning policy value, retained on success.
limitsOwning resource caps; zero caps permit only empty input.
Returns
An independent text value or TextError; never a partial result.
Note
Any thread; allocations can throw. No input repair or normalization.

◆ graphemes()

std::span< const Grapheme > portals::GraphemeText::graphemes ( ) const
nodiscardnoexcept

Borrow metadata until this object's assignment/move/destruction.

Returns
Ordered cluster metadata indexing bytes(); empty for an empty or moved-from value.

◆ measure()

std::int32_t portals::GraphemeText::measure ( ) const
nodiscardnoexcept

Return cached int32_t-compatible total cells; no allocation or errors.

Returns
The sum of cluster display widths in terminal cells.

◆ nextCursor()

std::expected< std::size_t, TextError > portals::GraphemeText::nextCursor ( std::size_t byte_offset) const
nodiscardnoexcept

Advance a byte-boundary cursor by one cluster; end stays at end.

Parameters
byte_offsetValid boundary including zero and byte length.
Returns
Next boundary or NOT_BOUNDARY with the supplied offset.
Note
Any thread; no mutation, allocation, exceptions or invalidation.

◆ operator=() [1/2]

GraphemeText & portals::GraphemeText::operator= ( const GraphemeText & other)

Replace storage with an independent copy; invalidates borrowed views.

Parameters
otherSource value; self-assignment is supported.
Returns
This object after successful replacement.
Exceptions
std::bad_allocIf copying fails; this object retains its previous value.

◆ operator=() [2/2]

GraphemeText & portals::GraphemeText::operator= ( GraphemeText && other)
noexcept

Transfer storage; invalidates both objects' views and empties a distinct source. Self-move is a no-op.

Parameters
otherSource whose storage is transferred; a distinct source becomes empty.
Returns
This object, with the source policy and measurements; self-move preserves its value.

◆ operator==()

bool portals::GraphemeText::operator== ( const GraphemeText & other) const
noexcept

Compare logical UTF-8 bytes and the complete captured width policy without allocating.

Parameters
otherOwning immutable text to compare; segmentation is determined by bytes and policy.
Returns
True for equal bytes and policy, independently of storage identity.

◆ policy()

WidthPolicy portals::GraphemeText::policy ( ) const
nodiscardnoexcept

Return the owning captured cache-key policy; no errors.

Returns
An independent copy of the policy used for these measurements.

◆ previousCursor()

std::expected< std::size_t, TextError > portals::GraphemeText::previousCursor ( std::size_t byte_offset) const
nodiscardnoexcept

Retreat a byte-boundary cursor by one cluster; zero stays at zero.

Parameters
byte_offsetValid boundary including zero and byte length.
Returns
Previous boundary or NOT_BOUNDARY with the supplied offset.
Note
Any thread; no mutation, allocation, exceptions or invalidation.

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