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
7#include "../assert.hpp"
8
9namespace hi::inline v1 {
10
12public:
13 [[nodiscard]] constexpr unicode_composition(char32_t first, char32_t second, char32_t composed = 0) noexcept :
14 value(static_cast<int64_t>(first) << 42 | static_cast<int64_t>(second) << 21 | static_cast<int64_t>(composed))
15 {
16 hi_axiom(first <= 0x10'ffff);
17 hi_axiom(second <= 0x10'ffff);
18 hi_axiom(composed <= 0x10'ffff);
19 }
20
21 [[nodiscard]] constexpr char32_t first() const noexcept
22 {
23 return static_cast<char32_t>(value >> 42);
24 }
25
26 [[nodiscard]] constexpr char32_t second() const noexcept
27 {
28 return static_cast<char32_t>((value >> 21) & 0x1f'ffff);
29 }
30
31 [[nodiscard]] constexpr char32_t composed() const noexcept
32 {
33 return static_cast<char32_t>(value & 0x1f'ffff);
34 }
35
36
37 [[nodiscard]] constexpr friend bool operator==(unicode_composition const &lhs, unicode_composition const &rhs) noexcept
38 {
39 // Don't check the composed value, when searching through the composition table.
40 return (lhs.value >> 21) == (rhs.value >> 21);
41 }
42
43 [[nodiscard]] constexpr friend bool operator<(unicode_composition const &lhs, unicode_composition const &rhs) noexcept
44 {
45 return lhs.value < rhs.value;
46 }
47
48private:
49 int64_t value;
50};
51
52template<typename It>
53[[nodiscard]] constexpr It unicode_composition_find(It first, It last, unicode_composition value) noexcept
54{
55 auto it = std::lower_bound(first, last, value);
56 if (it == last || *it != value) {
57 return last;
58 } else {
59 return it;
60 }
61}
62
63template<typename It>
64[[nodiscard]] constexpr It unicode_composition_find(It first, It last, char32_t first_cp, char32_t second_cp) noexcept
65{
66 return unicode_composition_find(first, last, unicode_composition{first_cp, second_cp});
67}
68
72[[nodiscard]] char32_t unicode_composition_find(char32_t first, char32_t second) noexcept;
73
74} // namespace hi::inline v1
Definition unicode_composition.hpp:11
T lower_bound(T... args)