HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
unicode_composition.hpp
1// Copyright Take Vos 2020.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5#pragma once
6
7namespace tt {
8
10public:
11 [[nodiscard]] constexpr unicode_composition(char32_t first, char32_t second, char32_t composed = 0) noexcept :
12 value(static_cast<int64_t>(first) << 42 | static_cast<int64_t>(second) << 21 | static_cast<int64_t>(composed))
13 {
14 tt_axiom(first <= 0x10'ffff);
15 tt_axiom(second <= 0x10'ffff);
16 tt_axiom(composed <= 0x10'ffff);
17 }
18
19 [[nodiscard]] constexpr char32_t first() const noexcept
20 {
21 return static_cast<char32_t>(value >> 42);
22 }
23
24 [[nodiscard]] constexpr char32_t second() const noexcept
25 {
26 return static_cast<char32_t>((value >> 21) & 0x1f'ffff);
27 }
28
29 [[nodiscard]] constexpr char32_t composed() const noexcept
30 {
31 return static_cast<char32_t>(value & 0x1f'ffff);
32 }
33
34 [[nodiscard]] constexpr friend bool operator<(unicode_composition const &lhs, unicode_composition const &rhs) noexcept
35 {
36 return lhs.value < rhs.value;
37 }
38
39 [[nodiscard]] constexpr friend bool operator==(unicode_composition const &lhs, unicode_composition const &rhs) noexcept
40 {
41 // Don't check the composed value, when searching through the composition table.
42 return (lhs.value >> 21) == (rhs.value >> 21);
43 }
44
45private:
46 int64_t value;
47};
48
49template<typename It>
50[[nodiscard]] constexpr It unicode_composition_find(It first, It last, unicode_composition value) noexcept
51{
52 auto it = std::lower_bound(first, last, value);
53 if (it == last || *it != value) {
54 return last;
55 } else {
56 return it;
57 }
58}
59
60template<typename It>
61[[nodiscard]] constexpr It unicode_composition_find(It first, It last, char32_t first_cp, char32_t second_cp) noexcept
62{
63 return unicode_composition_find(first, last, unicode_composition{first_cp, second_cp});
64}
65
69[[nodiscard]] char32_t unicode_composition_find(char32_t first, char32_t second) noexcept;
70
71}
Definition unicode_composition.hpp:9
T lower_bound(T... args)