8#include "../strings.hpp"
9#include "../unfair_mutex.hpp"
11#include "../stable_set.hpp"
21namespace hi::inline v1 {
24inline auto long_graphemes = ::hi::stable_set<std::u32string>{};
42 using value_type = uint32_t;
55 constexpr grapheme() noexcept = default;
63 constexpr explicit
grapheme(
char32_t code_point) noexcept : _value(truncate<value_type>(code_point))
65 hi_axiom(code_point <= 0x10ffff);
68 constexpr explicit grapheme(
char ascii_char) noexcept : _value(truncate<value_type>(ascii_char))
70 hi_axiom(ascii_char >= 0 and ascii_char <= 0x7f);
77 _value = truncate<value_type>(code_point);
85 hi_axiom(ascii_char >= 0 and ascii_char <= 0x7f);
86 _value = truncate<value_type>(ascii_char);
94 explicit grapheme(std::u32string_view code_points)
noexcept;
120 [[nodiscard]]
constexpr bool empty() const noexcept
122 return _value == 0x1f'ffff;
127 constexpr operator bool() const noexcept
139 [[nodiscard]]
bool valid() const noexcept;
141 [[nodiscard]]
std::u32string const &long_grapheme() const noexcept
143 hi_axiom(_value >= 0x10'0000 and _value < 0x1f'ffff);
144 return detail::long_graphemes[_value - 0x11'0000];
151 if (_value == 0x1f'ffff) {
154 }
else if (_value <= 0x10'ffff) {
158 return long_grapheme().size();
168 [[nodiscard]]
constexpr char32_t operator[](
size_t i)
const noexcept
170 hi_axiom(i < size());
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
189 hi_axiom(I < rhs.size());
191 if (rhs._value <= 0x10'ffff) {
194 return rhs.long_grapheme()[I];
202 if (_value <= 0x10'ffff) {
205 return long_grapheme();
215 [[nodiscard]] friend constexpr
bool operator==(
grapheme const&,
grapheme const&) noexcept = default;
219 [[nodiscard]] friend constexpr
std::strong_ordering operator<=>(
grapheme const& lhs,
grapheme const& rhs) noexcept
221 return lhs.decomposed() <=> rhs.decomposed();
224 [[nodiscard]]
friend constexpr bool operator==(
grapheme const& lhs,
char32_t const& rhs)
noexcept
229 [[nodiscard]]
friend constexpr std::strong_ordering operator<=>(grapheme
const& lhs,
char32_t const& rhs)
noexcept
231 return lhs <=> grapheme{rhs};
234 [[nodiscard]]
friend constexpr bool operator==(grapheme
const& lhs,
char const& rhs)
noexcept
236 return lhs == grapheme{rhs};
239 [[nodiscard]]
friend constexpr std::strong_ordering operator<=>(grapheme
const& lhs,
char const& rhs)
noexcept
241 return lhs <=> grapheme{rhs};
246 return hi::to_string(rhs.composed());
249 [[nodiscard]]
friend std::u32string to_u32string(grapheme
const& rhs)
noexcept
251 return rhs.composed();
This file includes required definitions.
Definition grapheme.hpp:28
A grapheme-cluster, what a user thinks a character is.
Definition grapheme.hpp:41
std::u32string decomposed() const noexcept
Get a list of code-point normalized to NFD.
constexpr bool empty() const noexcept
Check if the grapheme is empty.
Definition grapheme.hpp:120
bool valid() const noexcept
Check if the grapheme is valid.
constexpr char32_t operator[](size_t i) const noexcept
Get the code-point at the given index.
Definition grapheme.hpp:168
static constexpr grapheme eof() noexcept
Create empty grapheme / end-of-file.
Definition grapheme.hpp:104
grapheme(composed_t, std::u32string_view code_points) noexcept
Encode a grapheme from a list of code-points.
constexpr void clear() noexcept
Clear the grapheme.
Definition grapheme.hpp:113
constexpr grapheme & operator=(char ascii_char) noexcept
Encode a single code-point.
Definition grapheme.hpp:83
value_type _value
A pointer to a grapheme.
Definition grapheme.hpp:53
grapheme(std::u32string_view code_points) noexcept
Encode a grapheme from a list of code-points.
constexpr std::u32string composed() const noexcept
Get a list of code-point normalized to NFC.
Definition grapheme.hpp:200
constexpr grapheme & operator=(char32_t code_point) noexcept
Encode a single code-point.
Definition grapheme.hpp:75
constexpr std::size_t size() const noexcept
Return the number of code-points encoded in the grapheme.
Definition grapheme.hpp:149
friend constexpr char32_t get(grapheme const &rhs) noexcept
Get the code-point at the given index.
Definition grapheme.hpp:187
T operator()(T... args)
Definition datum.hpp:2458