Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
text.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3
4#pragma once
5
6#include <cstddef>
7#include <cstdint>
8#include <expected>
9#include <span>
10#include <string>
11#include <string_view>
12#include <vector>
13
14namespace portals {
15namespace detail {
16struct EditStorageAccess;
17}
18
20enum class AmbiguousWidth {
25};
26
28enum class EmojiWidth {
30 ONE = 1,
32 TWO = 2
33};
34
48 std::uint64_t revision{};
53 bool operator==(const WidthPolicy &other) const noexcept = default;
54};
55
57struct TextLimits {
59 std::size_t max_input_bytes{1024 * 1024};
61 std::size_t max_cluster_bytes{64 * 1024};
63 std::size_t max_graphemes{1024 * 1024};
65 std::size_t max_cells{2 * 1024 * 1024};
66};
67
85
87struct TextError {
94 std::size_t byte_offset{};
99 bool operator==(const TextError &other) const noexcept = default;
100};
101
109
111struct Grapheme {
113 std::size_t byte_begin{};
115 std::size_t byte_end{};
117 std::uint8_t cells{};
120};
121
123struct TextClip {
125 std::size_t byte_end{};
127 std::size_t graphemes{};
129 std::int32_t cells{};
130};
131
146public:
151 GraphemeText(const GraphemeText &other) = default;
168
173 bool operator==(const GraphemeText &other) const noexcept;
174
183 [[nodiscard]] static std::expected<GraphemeText, TextError> from(std::string_view bytes, WidthPolicy policy = {},
184 TextLimits limits = {});
188 [[nodiscard]] std::string_view bytes() const noexcept;
192 [[nodiscard]] std::span<const Grapheme> graphemes() const noexcept;
196 [[nodiscard]] std::int32_t measure() const noexcept;
200 [[nodiscard]] WidthPolicy policy() const noexcept;
207 [[nodiscard]] std::expected<std::size_t, TextError> nextCursor(std::size_t byte_offset) const noexcept;
214 [[nodiscard]] std::expected<std::size_t, TextError> previousCursor(std::size_t byte_offset) const noexcept;
221 [[nodiscard]] TextClip clipPrefix(std::int32_t max_cells) const noexcept;
222
223private:
224 friend struct detail::EditStorageAccess;
225 GraphemeText() = default;
226 // Count constructors let MSVC Debug iterator-proxy allocation failures propagate.
227 std::string bytes_ = std::string(0, '\0');
228 std::vector<Grapheme> graphemes_ = std::vector<Grapheme>(0);
229 WidthPolicy policy_;
230 std::int32_t cells_{};
231};
232
233} // namespace portals
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.
bool operator==(const GraphemeText &other) const noexcept
Compare logical UTF-8 bytes and the complete captured width policy without allocating.
GraphemeText & operator=(const GraphemeText &other)
Replace storage with an independent copy; invalidates borrowed views.
std::span< const Grapheme > graphemes() const noexcept
Borrow metadata until this object's assignment/move/destruction.
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.
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...
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.
WidthPolicy policy() const noexcept
Return the owning captured cache-key policy; no errors.
std::int32_t measure() const noexcept
Return cached int32_t-compatible total cells; no allocation or errors.
std::string_view bytes() const noexcept
Borrow original bytes until this object's assignment/move/destruction.
static std::expected< GraphemeText, TextError > from(std::string_view bytes, WidthPolicy policy={}, TextLimits limits={})
Validate, segment and measure a single logical UTF-8 buffer.
GraphemeText(const GraphemeText &other)=default
Copy bytes and metadata into independent storage; may allocate.
Public Portals API.
Definition action.hpp:8
@ PRINTABLE
ASCII graphic character.
Definition document_hex.hpp:20
@ UNSUPPORTED
The application explicitly excludes this annotated phase.
Definition key_binding.hpp:44
EmojiWidth
Terminal policy for recognized emoji clusters, including single emoji.
Definition text.hpp:28
@ TWO
Use two cells.
Definition text.hpp:32
@ ONE
Use one cell.
Definition text.hpp:30
AmbiguousWidth
Terminal policy for printable East Asian Ambiguous scalars.
Definition text.hpp:20
@ NARROW
Use one cell.
Definition text.hpp:22
@ WIDE
Use two cells.
Definition text.hpp:24
TextDisplay
How a cell renderer must display a complete logical cluster.
Definition text.hpp:103
TextErrorKind
Observable text construction/cursor failure.
Definition text.hpp:69
@ CLUSTER_LIMIT
One whole cluster exceeds its byte limit.
Definition text.hpp:75
@ NOT_BOUNDARY
A cursor offset is outside the text or inside a cluster.
Definition text.hpp:83
@ GRAPHEME_LIMIT
The number of clusters exceeds its limit.
Definition text.hpp:77
@ INPUT_LIMIT
Input bytes exceed the selected budget.
Definition pss_data.hpp:155
@ INVALID_UTF8
Invalid UTF-8 sequence anywhere in strict input.
Definition pss_data.hpp:162
@ CELL_LIMIT
Checked cell product exceeds the cap or representation.
Definition cell_buffer.hpp:144
@ INVALID_POLICY
An unrecognized width policy value.
Definition cell_buffer.hpp:138
Owning metadata for a half-open UTF-8 byte range.
Definition text.hpp:111
std::size_t byte_begin
Inclusive byte offset in GraphemeText::bytes().
Definition text.hpp:113
std::uint8_t cells
One or two cells, including a one-cell unsupported placeholder.
Definition text.hpp:117
std::size_t byte_end
Exclusive byte offset in GraphemeText::bytes().
Definition text.hpp:115
TextDisplay display
Renderer disposition; preserves the original logical bytes.
Definition text.hpp:119
Owning metadata for the maximal whole-cluster prefix in a cell budget.
Definition text.hpp:123
std::size_t graphemes
Number of included clusters.
Definition text.hpp:127
std::size_t byte_end
Exclusive byte end at an extended grapheme boundary.
Definition text.hpp:125
std::int32_t cells
Measured cells in the prefix.
Definition text.hpp:129
Owning error value, independent of the failed input's lifetime.
Definition text.hpp:87
bool operator==(const TextError &other) const noexcept=default
Compare category and byte offset.
std::size_t byte_offset
Byte offset of an invalid sequence, rejected cluster or cursor. Input-limit and invalid-policy errors...
Definition text.hpp:94
TextErrorKind kind
Failure category.
Definition text.hpp:89
Owning limits checked without silently truncating input or clusters.
Definition text.hpp:57
std::size_t max_cells
Maximum measured cells; additionally bounded by INT32_MAX.
Definition text.hpp:65
std::size_t max_cluster_bytes
Maximum bytes in one extended grapheme cluster.
Definition text.hpp:61
std::size_t max_input_bytes
Maximum input bytes; also bounded by PTRDIFF_MAX.
Definition text.hpp:59
std::size_t max_graphemes
Maximum cluster records.
Definition text.hpp:63
Owning deterministic width policy; no font or terminal matching promise.
Definition text.hpp:42
EmojiWidth emoji
Width of recognized emoji clusters.
Definition text.hpp:46
std::uint64_t revision
Caller-owned terminal capability revision, included in equality.
Definition text.hpp:48
bool operator==(const WidthPolicy &other) const noexcept=default
Compare the full cache-key policy.
AmbiguousWidth ambiguous
Width of printable Ambiguous scalars.
Definition text.hpp:44