Portals0.7.0 · dev (544a5c68c72e · 2026-11-28 UTC)
Loading...
Searching...
No Matches
text_transform.hpp
1// Copyright (c) 2026 ttldtor.
2// SPDX-License-Identifier: BSL-1.0
3#pragma once
4
5#include <portals/widget.hpp>
6
7namespace portals {
11 std::size_t maxTokens{4096};
12};
13
14namespace detail {
15struct TextSegmentsData;
17std::optional<TextError> textSplitLimit(const GraphemeText &input, const TextLimits &limits) noexcept;
19} // namespace detail
20
22class TextSegment {
23public:
25 ~TextSegment() = default;
27 std::string_view bytes() const noexcept;
29 WidthPolicy policy() const noexcept;
35 bool operator==(const TextSegment &other) const noexcept;
36
37private:
38 friend class TextSegments;
39
40 TextSegment(std::shared_ptr<const detail::TextSegmentsData> owner, std::size_t first, std::size_t last)
41 : owner_(std::move(owner)), first_(first), last_(last) {
42 }
43
44 std::shared_ptr<const detail::TextSegmentsData> owner_;
45 std::size_t first_{};
46 std::size_t last_{};
47};
48
50class TextSegments {
51public:
53 ~TextSegments() = default;
55 std::size_t size() const noexcept;
59 std::expected<TextSegment, TreeError> at(std::size_t index) const;
63 bool operator==(const TextSegments &other) const noexcept;
64
65private:
66 friend std::expected<TextSegments, TreeError> splitText(const GraphemeText &, bool, TextSplitOptions);
67
68 explicit TextSegments(std::shared_ptr<const detail::TextSegmentsData> owner) : owner_(std::move(owner)) {
69 }
70
71 std::shared_ptr<const detail::TextSegmentsData> owner_;
72};
73
81std::expected<TextSegments, TreeError> words(const GraphemeText &input, TextSplitOptions options = {});
82
88std::expected<TextSegments, TreeError> graphemes(const GraphemeText &input, TextSplitOptions options = {});
89} // namespace portals
Owning validated UTF-8 and immutable extended grapheme measurements.
Definition text.hpp:145
One immutable whole-grapheme byte range retaining its original owning input.
Definition text_transform.hpp:22
WidthPolicy policy() const noexcept
std::string_view bytes() const noexcept
~TextSegment()=default
Release a shared input owner; rvalue transfer uses the nonallocating shared-owner copy.
GraphemeText text() const
~TextSegments()=default
Release one immutable collection owner; rvalue transfer retains a valid shared-owner source.
std::size_t size() const noexcept
std::expected< TextSegment, TreeError > at(std::size_t index) const
Public Portals API.
Definition action.hpp:8
std::expected< TextSegments, TreeError > graphemes(const GraphemeText &input, TextSplitOptions options={})
Return one token for every existing extended grapheme, including whitespace.
std::expected< TextSegments, TreeError > words(const GraphemeText &input, TextSplitOptions options={})
Split on whole graphemes consisting only of scalars in Unicode White_Space.
TreeError
Checked tree failures; rejected operations never start the requested callback.
Definition widget_types.hpp:26
Owning limits checked without silently truncating input or clusters.
Definition text.hpp:57
Owning text-analysis limits; words are Unicode-whitespace-delimited tokens, not UAX #29 words.
Definition text_transform.hpp:9
TextLimits limits
Whole input validation caps, including bytes, clusters, graphemes and cells.
Definition text_transform.hpp:10
std::size_t maxTokens
Positive token cap; exceeding it rejects the complete output.
Definition text_transform.hpp:11
Owning deterministic width policy; no font or terminal matching promise.
Definition text.hpp:42