7#include "unicode_db_non_starter.hpp"
9#include "../strings.hpp"
14namespace hi::inline v1 {
35 using value_type = uint64_t;
47 constexpr grapheme() noexcept = default;
55 constexpr explicit
grapheme(
char32_t code_point) noexcept : value((static_cast<value_type>(code_point) << 43) | 1) {}
61 value = (
static_cast<value_type
>(code_point) << 43) | 1;
67 constexpr explicit grapheme(
char code_point) noexcept : value((
static_cast<value_type
>(code_point) << 43) | 1) {}
73 value = (
static_cast<value_type
>(code_point) << 43) | 1;
81 explicit grapheme(std::u32string_view code_points)
noexcept;
113 [[nodiscard]]
constexpr bool empty() const noexcept
120 constexpr operator bool() const noexcept
132 [[nodiscard]]
bool valid() const noexcept;
136 [[nodiscard]] constexpr
std::
size_t size() const noexcept
138 hilet size_ = value & 0x7;
139 return size_ <= 5 ? size_ : 5;
144 [[nodiscard]]
constexpr bool overlong() const noexcept
146 return (value & 0x7) == 6;
155 [[nodiscard]]
constexpr char32_t operator[](
size_t i)
const noexcept
157 hi_axiom(i < size());
160 return static_cast<char32_t>(value >> 43);
163 hilet shift = (4 - i) * 10 + 3;
164 return detail::unicode_db_non_starter_table[(value >> shift) & 0x3ff];
176 [[nodiscard]]
friend constexpr char32_t get(
grapheme const &rhs)
noexcept
178 if constexpr (I == 0) {
179 return static_cast<char32_t>(rhs.value >> 43);
182 constexpr auto shift = (4 - I) * 10 + 3;
183 return detail::unicode_db_non_starter_table[(rhs.value >> shift) & 0x3ff];
205 [[nodiscard]] friend constexpr
bool operator==(
grapheme const &a,
grapheme const &b) noexcept = default;
209 [[nodiscard]] friend constexpr
std::strong_ordering operator<=>(
grapheme const &a,
grapheme const &b) noexcept = default;
211 [[nodiscard]] friend constexpr
bool operator==(
grapheme const &lhs,
char32_t const &rhs) noexcept
216 [[nodiscard]]
friend constexpr std::strong_ordering operator<=>(
grapheme const &lhs,
char32_t const &rhs)
noexcept
221 [[nodiscard]]
friend constexpr bool operator==(grapheme
const &lhs,
char const &rhs)
noexcept
223 return lhs == grapheme{rhs};
226 [[nodiscard]]
friend constexpr std::strong_ordering operator<=>(grapheme
const &lhs,
char const &rhs)
noexcept
228 return lhs <=> grapheme{rhs};
233 return hi::to_string(rhs.composed());
236 [[nodiscard]]
friend std::u32string to_u32string(grapheme
const &rhs)
noexcept
238 return rhs.composed();
This file includes required definitions.
#define hilet
Invariant should be the default for variables.
Definition required.hpp:23
A grapheme, what a user thinks a character is.
Definition grapheme.hpp:34
static grapheme from_composed(std::u32string_view code_points) noexcept
Encode a grapheme from a list of NFKC-normalized code-points.
std::u32string decomposed() const noexcept
Get a list of code-point normalized to NFD.
value_type value
[63:43] Starter code-point 0.
Definition grapheme.hpp:45
constexpr bool empty() const noexcept
Check if the grapheme is empty.
Definition grapheme.hpp:113
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:155
static constexpr grapheme eof() noexcept
Create empty grapheme / end-of-file.
Definition grapheme.hpp:97
constexpr void clear() noexcept
Clear the grapheme.
Definition grapheme.hpp:106
constexpr grapheme & operator=(char code_point) noexcept
Encode a single code-point.
Definition grapheme.hpp:71
grapheme & operator=(std::u32string_view code_points) noexcept
Encode a grapheme from a list of code-points.
constexpr grapheme(char code_point) noexcept
Encode a single code-point.
Definition grapheme.hpp:67
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:189
constexpr bool overlong() const noexcept
Check if the grapheme was initialized with more than 4 combining characters.
Definition grapheme.hpp:144
constexpr grapheme & operator=(char32_t code_point) noexcept
Encode a single code-point.
Definition grapheme.hpp:59
friend constexpr char32_t get(grapheme const &rhs) noexcept
Get the code-point at the given index.
Definition grapheme.hpp:176