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 tt {
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 tt_axiom(first <= 0x10'ffff);
17 tt_axiom(second <= 0x10'ffff);
18 tt_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 [[nodiscard]] constexpr friend bool operator<(unicode_composition const &lhs, unicode_composition const &rhs) noexcept
37 {
38 return lhs.value < rhs.value;
39 }
40
41 [[nodiscard]] constexpr friend bool operator==(unicode_composition const &lhs, unicode_composition const &rhs) noexcept
42 {
43 // Don't check the composed value, when searching through the composition table.
44 return (lhs.value >> 21) == (rhs.value >> 21);
45 }
46
47private:
48 int64_t value;
49};
50
51template<typename It>
52[[nodiscard]] constexpr It unicode_composition_find(It first, It last, unicode_composition value) noexcept
53{
54 auto it = std::lower_bound(first, last, value);
55 if (it == last || *it != value) {
56 return last;
57 } else {
58 return it;
59 }
60}
61
62template<typename It>
63[[nodiscard]] constexpr It unicode_composition_find(It first, It last, char32_t first_cp, char32_t second_cp) noexcept
64{
65 return unicode_composition_find(first, last, unicode_composition{first_cp, second_cp});
66}
67
71[[nodiscard]] char32_t unicode_composition_find(char32_t first, char32_t second) noexcept;
72
73}
Definition unicode_composition.hpp:11
T lower_bound(T... args)