7#include "../utility/utility.hpp"
8#include "../i18n/i18n.hpp"
9#include "../telemetry/telemetry.hpp"
10#include "../concurrency/concurrency.hpp"
11#include "unicode_normalization.hpp"
12#include "ucd_general_categories.hpp"
13#include "ucd_canonical_combining_classes.hpp"
14#include "ucd_scripts.hpp"
15#include "phrasing.hpp"
16#include "../macros.hpp"
52 hilet length = *src >> 21;
55 r.resize_and_overwrite(length, [&](
char32_t *dst,
size_t count) {
73 return _table[start] >> 21;
86 return char32_t{_table[start] & 0x1f'ffff};
96 template<
typename CodePo
ints>
97 [[nodiscard]] int32_t
add_grapheme(CodePoints&& code_points)
noexcept
99 static_assert(std::is_same_v<typename std::remove_cvref_t<CodePoints>::value_type,
char32_t>);
101 hi_axiom(code_points.size() >= 2);
104 hilet lock = std::scoped_lock(_mutex);
107 if (hilet it = _indices.find(code_points); it != _indices.end()) {
112 if (_head + code_points.size() >= _table.size()) {
116 hilet insert_index = _head;
117 _head += narrow_cast<uint32_t>(code_points.size());
120 std::copy(code_points.cbegin(), code_points.cend(), _table.begin() + insert_index);
121 _table[insert_index] |= char_cast<char32_t>(code_points.size() << 21);
124 _indices.emplace(std::forward<CodePoints>(code_points), insert_index);
130 mutable unfair_mutex _mutex = {};
143inline long_grapheme_table long_graphemes = {};
161 using value_type = uint64_t;
177 constexpr grapheme() noexcept = default;
183 constexpr
grapheme(intrinsic_t, value_type value) : _value(value) {}
185 constexpr value_type& intrinsic() noexcept
190 constexpr value_type
const& intrinsic() const noexcept
197 constexpr grapheme(
char32_t code_point) noexcept : _value(char_cast<value_type>(code_point))
199 hi_axiom(code_point <= 0x10'ffff);
200 hi_axiom(ucd_get_canonical_combining_class(code_point) == 0,
"Single code-point must be a starter");
204 constexpr grapheme(
char ascii_char) noexcept : _value(char_cast<value_type>(ascii_char))
206 hi_axiom(ascii_char >= 0 and ascii_char <= 0x7f);
215 hi_axiom(code_point <= 0x10'ffff);
216 hi_axiom(ucd_get_canonical_combining_class(code_point) == 0,
"Single code-point must be a starter");
218 _value = char_cast<value_type>(code_point);
227 hi_axiom(ascii_char >= 0 and ascii_char <= 0x7f);
230 _value = char_cast<value_type>(ascii_char);
239 template<
typename CodePo
ints>
242 static_assert(std::is_same_v<typename std::remove_cvref_t<CodePoints>::value_type,
char32_t>);
244 hi_axiom(not code_points.empty());
245 if (code_points.size() == 1) {
246 hilet code_point = code_points.front();
247 hi_axiom(code_point <= 0x10'ffff);
248 hi_axiom(ucd_get_canonical_combining_class(code_point) == 0);
249 _value = char_cast<value_type>(code_point);
252 hilet index = detail::long_graphemes.add_grapheme(std::forward<CodePoints>(code_points));
254 _value = narrow_cast<value_type>(index + 0x11'0000);
257 [[unlikely]] hi_log_error_once(
258 "grapheme::error::too-many",
"Too many long graphemes encoded, replacing with U+fffd");
259 _value = char_cast<value_type>(U
'\ufffd');
269 constexpr explicit grapheme(std::u32string_view code_points) noexcept :
276 [[nodiscard]]
constexpr uint32_t
index() const noexcept
278 return _value & 0x1f'ffff;
281 [[nodiscard]]
constexpr iso_639 language() const noexcept
283 return iso_639{intrinsic_t{}, narrow_cast<uint16_t>((_value >> 21) & 0x7fff)};
288 hi_axiom(rhs.intrinsic() <= 0x7fff);
290 constexpr auto mask = ~(value_type{0x7fff} << 21);
292 _value |= wide_cast<value_type>(rhs.intrinsic()) << 21;
302 return ucd_get_script(starter());
312 [[nodiscard]]
constexpr iso_15924
starter_script(iso_15924 default_script)
const noexcept
314 hilet starter_script_ = starter_script();
315 if (starter_script_ == iso_15924::common() and starter_script_ == iso_15924::inherited()) {
316 return default_script;
318 return starter_script_;
322 [[nodiscard]]
constexpr iso_15924 script() const noexcept
324 return iso_15924{intrinsic_t{}, narrow_cast<uint16_t>((_value >> 36) & 0x3ff)};
334 hilet new_script = starter_script(rhs);
335 hi_axiom(new_script.intrinsic() < 1000);
337 constexpr auto mask = ~(value_type{0x3ff} << 36);
339 _value |= wide_cast<value_type>(new_script.intrinsic()) << 36;
346 set_script(starter_script());
349 [[nodiscard]]
constexpr iso_3166 region() const noexcept
351 return iso_3166{intrinsic_t{}, narrow_cast<uint16_t>((_value >> 46) & 0x3ff)};
354 constexpr void set_region(iso_3166 rhs)
noexcept
356 hi_axiom(rhs.intrinsic() < 1000);
358 constexpr auto mask = ~(value_type{0x3ff} << 46);
360 _value |= wide_cast<value_type>(rhs.intrinsic()) << 46;
367 hilet language_ = iso_639{intrinsic_t{}, narrow_cast<uint16_t>(tmp & 0x7fff)};
369 hilet script_ = iso_15924{intrinsic_t{}, narrow_cast<uint16_t>(tmp & 0x3ff)};
371 hilet region_ = iso_3166{intrinsic_t{}, narrow_cast<uint16_t>(tmp & 0x3ff)};
377 hi_axiom(rhs.region.intrinsic() < 1000);
378 hi_axiom(rhs.language.intrinsic() <= 0x7fff);
380 hilet new_script = starter_script(rhs.script);
381 hi_axiom(new_script.intrinsic() < 1000);
383 auto tmp = wide_cast<value_type>(rhs.region.intrinsic());
385 tmp |= new_script.intrinsic();
387 tmp |= rhs.language.intrinsic();
390 constexpr auto mask = ~(uint64_t{0x7'ffff'ffff} << 21);
395 [[nodiscard]]
constexpr hi::phrasing
phrasing() const noexcept
397 return static_cast<hi::phrasing
>((_value >> 56) & 0x3f);
400 constexpr void set_phrasing(hi::phrasing rhs)
noexcept
402 hi_axiom(std::to_underlying(rhs) <= 0x3f);
404 constexpr auto mask = ~(value_type{0x3f} << 56);
406 _value |=
static_cast<value_type
>(rhs) << 56;
413 if (
auto i = index(); i <= 0x10'ffff) {
416 return detail::long_graphemes.get_grapheme_size(i - 0x11'0000);
420 [[nodiscard]]
constexpr char32_t starter() const noexcept
422 if (
auto i = index(); i <= 0x10'ffff) {
423 return char_cast<char32_t>(i);
425 return detail::long_graphemes.get_grapheme_starter(i - 0x11'0000);
429 [[nodiscard]]
constexpr bool is_ascii() const noexcept
431 return index() <= 127;
438 if (hilet i = index(); i <= 0x10'ffff) {
441 return detail::long_graphemes.get_grapheme(i - 0x11'0000);
450 return unicode_decompose(composed(), config);
459 return lhs.index() == rhs.index();
462 [[nodiscard]]
friend constexpr bool operator==(
grapheme const& lhs,
char32_t const& rhs)
noexcept
464 hi_axiom(char_cast<value_type>(rhs) <= 0x10'ffff);
465 return lhs.index() == char_cast<value_type>(rhs);
468 [[nodiscard]]
friend constexpr bool operator==(grapheme
const& lhs,
char const& rhs)
noexcept
470 hi_axiom(char_cast<value_type>(rhs) <= 0x7f);
471 return lhs.index() == char_cast<value_type>(rhs);
478 return lhs.decomposed() <=> rhs.decomposed();
481 [[nodiscard]]
friend constexpr std::strong_ordering operator<=>(
grapheme const& lhs,
char32_t const& rhs)
noexcept
486 [[nodiscard]]
friend constexpr std::strong_ordering operator<=>(grapheme
const& lhs,
char const& rhs)
noexcept
493 return hi::to_string(rhs.composed());
503 return rhs.composed();
constexpr std::u32string to_u32string(std::u32string_view rhs) noexcept
Identity conversion from UTF-32 to UTF-32.
Definition to_string.hpp:24
constexpr std::wstring to_wstring(std::u32string_view rhs) noexcept
Conversion from UTF-32 to wide-string (UTF-16/32).
Definition to_string.hpp:156
@ grapheme
The gui_event has grapheme data.
phrasing
Phrasing.
Definition phrasing.hpp:30
DOXYGEN BUG.
Definition algorithm.hpp:16
constexpr bool unicode_is_NFC_grapheme(It it, ItEnd last) noexcept
Check if the string of code-points is a single grapheme in NFC normal form.
Definition unicode_normalization.hpp:322
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:306
hi_export constexpr void set_language(It first, ItEnd last, language_tag language) noexcept
Set the language for the string.
Definition gstring.hpp:178
geometry/margins.hpp
Definition lookahead_iterator.hpp:5
constexpr Out narrow_cast(In const &rhs) noexcept
Cast numeric values without loss of precision.
Definition cast.hpp:377
ISO-3166 country code.
Definition iso_3166_intf.hpp:17
ISO-639 language code.
Definition iso_639.hpp:25
The IETF BCP 47 language tag.
Definition language_tag_intf.hpp:28
Definition grapheme.hpp:31
size_t get_grapheme_size(uint32_t start) const noexcept
Get the size of the grapheme.
Definition grapheme.hpp:71
char32_t get_grapheme_starter(uint32_t start) const noexcept
Get the starter (first) code-point of a grapheme.
Definition grapheme.hpp:84
std::u32string get_grapheme(uint32_t start) const noexcept
Get the grapheme from the table.
Definition grapheme.hpp:47
int32_t add_grapheme(CodePoints &&code_points) noexcept
Find or insert a grapheme in the table.
Definition grapheme.hpp:97
Definition grapheme.hpp:147
A grapheme-cluster, what a user thinks a character is.
Definition grapheme.hpp:160
constexpr grapheme(std::u32string_view code_points) noexcept
Encode a grapheme from a list of code-points.
Definition grapheme.hpp:269
friend constexpr bool operator==(grapheme const &lhs, grapheme const &rhs) noexcept
Compare equivalence of two graphemes.
Definition grapheme.hpp:457
constexpr void set_script() noexcept
Get the script of the grapheme to the starter script.
Definition grapheme.hpp:344
friend constexpr std::strong_ordering operator<=>(grapheme const &lhs, grapheme const &rhs) noexcept
Compare two graphemes lexicographically.
Definition grapheme.hpp:476
constexpr iso_15924 starter_script(iso_15924 default_script) const noexcept
Get the script of the starter code-point.
Definition grapheme.hpp:312
constexpr uint32_t index() const noexcept
Get the codepoint/index part of the grapheme.
Definition grapheme.hpp:276
constexpr grapheme(char32_t code_point) noexcept
Encode a single code-point.
Definition grapheme.hpp:197
constexpr grapheme & operator=(char ascii_char) noexcept
Encode a single code-point.
Definition grapheme.hpp:225
constexpr grapheme(composed_t, CodePoints &&code_points) noexcept
Encode a grapheme from a list of code-points.
Definition grapheme.hpp:240
value_type _value
The grapheme's value.
Definition grapheme.hpp:175
constexpr std::u32string decomposed(unicode_normalize_config config=unicode_normalize_config::NFD()) const noexcept
Get a list of code-point normalized to NFD.
Definition grapheme.hpp:448
constexpr iso_15924 starter_script() const noexcept
Get the script of the starter code-point.
Definition grapheme.hpp:300
constexpr std::u32string composed() const noexcept
Get a list of code-point normalized to NFC.
Definition grapheme.hpp:436
constexpr void set_script(iso_15924 rhs) noexcept
Set the script of the grapheme.
Definition grapheme.hpp:332
constexpr grapheme & operator=(char32_t code_point) noexcept
Encode a single code-point.
Definition grapheme.hpp:213
constexpr std::size_t size() const noexcept
Return the number of code-points encoded in the grapheme.
Definition grapheme.hpp:411
Definition unicode_normalization.hpp:22