HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
iso_639.hpp
1// Copyright Take Vos 2022.
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 "../utility/module.hpp"
8#include "../strings.hpp"
9#include <cctype>
10
11namespace hi::inline v1 {
12
24class iso_639 {
25public:
32 template<std::size_t I>
33 constexpr friend iso_639& set(iso_639& rhs, char c)
34 {
36 c == 0 or (c >= 'a' and c <= 'z') or (c >= 'A' and c <= 'Z') or (c >= '1' and c <= '5'),
37 "Must be letters or the digits between '1' and '5', or nul");
38
39 // clang-format off
40 uint16_t const x =
41 (c >= 'a' and c <= 'z') ? c - 'a' + 1 :
42 (c >= 'A' and c <= 'Z') ? c - 'A' + 1 :
43 (c >= '1' and c <= '5') ? c - '1' + 27 :
44 0;
45 // clang-format on
46
47 hi_assert(x <= 0x1f);
48 constexpr auto shift = I * 5;
49 rhs._v &= ~(0x1f << shift);
50 rhs._v |= x << shift;
51 return rhs;
52 }
53
60 template<std::size_t I>
61 [[nodiscard]] constexpr friend char get(iso_639 const& rhs) noexcept
62 {
63 constexpr auto shift = I * 5;
64 hilet x = (rhs._v >> shift) & 0x1f;
65 if (x == 0) {
66 return 0;
67 } else if (x <= 26) {
68 return 'a' + narrow_cast<char>(x - 1);
69 } else {
70 return '1' + narrow_cast<char>(x - 27);
71 }
72 }
73
74 constexpr iso_639(iso_639 const&) noexcept = default;
75 constexpr iso_639(iso_639&&) noexcept = default;
76 constexpr iso_639& operator=(iso_639 const&) noexcept = default;
77 constexpr iso_639& operator=(iso_639&&) noexcept = default;
78
81 constexpr iso_639() noexcept : _v(0) {}
82
85 constexpr iso_639(std::string_view str) : _v(0)
86 {
87 try {
88 hi_check(str.size() == 2 or str.size() == 3, "ISO-639 incorrect length.");
89
90 set<0>(*this, str[0]);
91 set<1>(*this, str[1]);
92 if (str.size() == 3) {
93 set<2>(*this, str[2]);
94 }
95 } catch (...) {
96 throw parse_error(std::format("A ISO-639 language code must be 2 or 3 letters in length, got '{}'", str));
97 }
98 }
99
104 [[nodiscard]] constexpr std::size_t size() const noexcept
105 {
106 hilet tmp = _v & 0x7fff;
107 // clang-format off
108 return
109 tmp == 0 ? 0 :
110 tmp <= 0x1f ? 1 :
111 tmp <= 0x3ff ? 2 :
112 3;
113 // clang-format on
114 }
115
118 [[nodiscard]] constexpr bool empty() const noexcept
119 {
120 return _v == 0;
121 }
122
125 constexpr explicit operator bool() const noexcept
126 {
127 return not empty();
128 }
129
132 [[nodiscard]] size_t hash() const noexcept
133 {
134 return std::hash<uint16_t>{}(_v);
135 }
136
139 [[nodiscard]] constexpr std::string code() const noexcept
140 {
141 auto r = std::string{};
142 if (size() >= 2) {
143 r += get<0>(*this);
144 r += get<1>(*this);
145 }
146 if (size() == 3) {
147 r += get<2>(*this);
148 }
149 return r;
150 }
151
154 [[nodiscard]] constexpr friend bool operator==(iso_639 const& lhs, iso_639 const& rhs) noexcept = default;
155
158 [[nodiscard]] constexpr friend auto operator<=>(iso_639 const& lhs, iso_639 const& rhs) noexcept = default;
159
160private:
170 uint16_t _v;
171};
172
173} // namespace hi::inline v1
174
175template<>
176struct std::hash<hi::iso_639> {
177 [[nodiscard]] size_t operator()(hi::iso_639 const& rhs) const noexcept
178 {
179 return rhs.hash();
180 }
181};
#define hi_check(expression, message,...)
Check if the expression is valid, or throw a parse_error.
Definition assert.hpp:95
#define hi_assert(expression,...)
Assert if expression is true.
Definition assert.hpp:184
#define hilet
Invariant should be the default for variables.
Definition utility.hpp:23
DOXYGEN BUG.
Definition algorithm.hpp:13
@ shift
The shift key is being held.
geometry/margins.hpp
Definition cache.hpp:11
ISO-639 language code.
Definition iso_639.hpp:24
constexpr std::string code() const noexcept
Get the 2 or 3 letter ISO-639 code.
Definition iso_639.hpp:139
constexpr bool empty() const noexcept
Check if the language is empty.
Definition iso_639.hpp:118
size_t hash() const noexcept
Get the hash value for this language code.
Definition iso_639.hpp:132
constexpr friend char get(iso_639 const &rhs) noexcept
Get the letter at a specific position.
Definition iso_639.hpp:61
constexpr friend iso_639 & set(iso_639 &rhs, char c)
Set the letter at a specific position.
Definition iso_639.hpp:33
constexpr friend auto operator<=>(iso_639 const &lhs, iso_639 const &rhs) noexcept=default
Compare two language codes.
constexpr iso_639(std::string_view str)
Construct a language from the 2 or 3 letter code.
Definition iso_639.hpp:85
constexpr friend bool operator==(iso_639 const &lhs, iso_639 const &rhs) noexcept=default
Compare two language codes.
constexpr std::size_t size() const noexcept
Get the number of character.
Definition iso_639.hpp:104
T operator()(T... args)