7#include "../utility/module.hpp"
8#include "../strings.hpp"
9#include "../stable_set.hpp"
11#include "unicode_normalization.hpp"
12#include "ucd_general_categories.hpp"
22namespace hi::inline
v1 {
25inline auto long_graphemes = ::hi::stable_set<std::u32string>{};
43 using value_type = uint32_t;
55 constexpr grapheme() noexcept = default;
61 constexpr
grapheme(intrinsic_t, value_type value) : _value(value) {}
63 constexpr value_type& intrinsic() noexcept
68 constexpr value_type
const& intrinsic() const noexcept
75 constexpr grapheme(
char32_t code_point) noexcept : _value(truncate<value_type>(code_point))
80 constexpr grapheme(
char ascii_char) noexcept : _value(truncate<value_type>(ascii_char))
82 hi_axiom(ascii_char >= 0 and ascii_char <= 0x7f);
89 _value = truncate<value_type>(code_point);
97 hi_axiom(ascii_char >= 0 and ascii_char <= 0x7f);
98 _value = truncate<value_type>(ascii_char);
108 switch (code_points.size()) {
113 _value = truncate<value_type>(code_points[0]);
118 if (index < 0x0f'0000) {
119 _value = narrow_cast<value_type>(index + 0x11'0000);
121 [[unlikely]] hi_log_error_once(
122 "grapheme::error::too-many",
"Too many long graphemes encoded, replacing with U+fffd");
132 constexpr explicit grapheme(std::u32string_view code_points) noexcept :
141 return unicode_decompose(composed(), unicode_normalize_config::NFD());
144 [[nodiscard]]
std::u32string const& long_grapheme() const noexcept
146 hi_assert(_value > 0x10'ffff and _value <= 0x1f'ffff);
147 return detail::long_graphemes[_value - 0x11'0000];
154 if (_value <= 0x10'ffff) {
158 return long_grapheme().size();
168 [[nodiscard]]
constexpr char32_t operator[](
size_t i)
const noexcept
172 if (_value <= 0x10'ffff) {
173 return truncate<char32_t>(_value);
175 return long_grapheme()[i];
187 [[nodiscard]]
friend constexpr char32_t get(
grapheme const& rhs)
noexcept
191 if (rhs._value <= 0x10'ffff) {
194 return rhs.long_grapheme()[I];
202 if (_value <= 0x10'ffff) {
205 return long_grapheme();
213 [[nodiscard]]
friend constexpr bool operator==(
grapheme const& lhs,
char32_t const& rhs)
noexcept
215 return lhs._value == char_cast<value_type>(rhs);
218 [[nodiscard]]
friend constexpr bool operator==(
grapheme const& lhs,
char const& rhs)
noexcept
221 return lhs._value == char_cast<value_type>(rhs);
228 return lhs.decomposed() <=> rhs.decomposed();
231 [[nodiscard]]
friend constexpr std::strong_ordering operator<=>(
grapheme const& lhs,
char32_t const& rhs)
noexcept
236 [[nodiscard]]
friend constexpr std::strong_ordering operator<=>(grapheme
const& lhs,
char const& rhs)
noexcept
253 return rhs.composed();
#define hi_assert_bounds(x,...)
Assert if a value is within bounds.
Definition assert.hpp:225
#define hi_assert(expression,...)
Assert if expression is true.
Definition assert.hpp:199
#define hi_no_default(...)
This part of the code should not be reachable, unless a programming bug.
Definition assert.hpp:279
#define hi_axiom(expression,...)
Specify an axiom; an expression that is true.
Definition assert.hpp:253
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
constexpr std::string to_string(std::u32string_view rhs) noexcept
Conversion from UTF-32 to UTF-8.
Definition to_string.hpp:215
constexpr std::u32string to_u32string(std::u32string_view rhs) noexcept
Identity conversion from UTF-32 to UTF-32.
Definition to_string.hpp:23
constexpr std::wstring to_wstring(std::u32string_view rhs) noexcept
Conversion from UTF-32 to wide-string (UTF-16/32).
Definition to_string.hpp:155
@ grapheme
The gui_event has grapheme data.
DOXYGEN BUG.
Definition algorithm.hpp:13
constexpr std::u32string unicode_normalize(std::u32string_view text, unicode_normalize_config config=unicode_normalize_config::NFC()) noexcept
Convert text to a Unicode composed normal form.
Definition unicode_normalization.hpp:303
geometry/margins.hpp
Definition cache.hpp:11
Definition grapheme.hpp:29
A grapheme-cluster, what a user thinks a character is.
Definition grapheme.hpp:42
constexpr grapheme(std::u32string_view code_points) noexcept
Encode a grapheme from a list of code-points.
Definition grapheme.hpp:132
friend constexpr std::strong_ordering operator<=>(grapheme const &lhs, grapheme const &rhs) noexcept
Compare two graphemes lexicographically.
Definition grapheme.hpp:226
constexpr char32_t operator[](size_t i) const noexcept
Get the code-point at the given index.
Definition grapheme.hpp:168
constexpr std::u32string decomposed() const noexcept
Get a list of code-point normalized to NFD.
Definition grapheme.hpp:139
constexpr grapheme(composed_t, std::u32string_view code_points) noexcept
Encode a grapheme from a list of code-points.
Definition grapheme.hpp:106
constexpr grapheme(char32_t code_point) noexcept
Encode a single code-point.
Definition grapheme.hpp:75
constexpr grapheme & operator=(char ascii_char) noexcept
Encode a single code-point.
Definition grapheme.hpp:95
value_type _value
A pointer to a grapheme.
Definition grapheme.hpp:53
constexpr std::u32string composed() const noexcept
Get a list of code-point normalized to NFC.
Definition grapheme.hpp:200
friend constexpr bool operator==(grapheme const &, grapheme const &) noexcept=default
Compare equivalence of two graphemes.
constexpr grapheme & operator=(char32_t code_point) noexcept
Encode a single code-point.
Definition grapheme.hpp:87
constexpr std::size_t size() const noexcept
Return the number of code-points encoded in the grapheme.
Definition grapheme.hpp:152
friend constexpr char32_t get(grapheme const &rhs) noexcept
Get the code-point at the given index.
Definition grapheme.hpp:187