HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
iso_15924.hpp
1// Copyright Take Vos 2021.
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 <cctype>
8#include "../assert.hpp"
9#include "../strings.hpp"
10
11namespace tt {
12
16class iso_15924 {
17public:
18 constexpr iso_639(iso_639 const &) noexcept = default;
19 constexpr iso_639(iso_639 &&) noexcept = default;
20 constexpr iso_639 &operator=(iso_639 const &) noexcept = default;
21 constexpr iso_639 &operator=(iso_639 &&) noexcept = default;
22
23 constexpr iso_639() noexcept : v0(0), v1(0), v2(0) {}
24
25 constexpr iso_639(std::string_view str) noexcept
26 {
27 if (std::size(str) == 0) {
28 _v0 = 0;
29 _v1 = 0;
30 _v2 = 0;
31 } else if (std::size(str) == 2) {
32 _v0 = to_lower(str[0]);
33 _v1 = to_lower(str[1]);
34 _v2 = 0;
35 } else if (std::size(str) == 3) {
36 _v0 = to_lower(str[0]);
37 _v1 = to_lower(str[1]);
38 _v2 = to_lower(str[2]);
39 } else {
40 tt_no_default();
41 }
42 }
43
44 constexpr explicit operator bool () const noexcept
45 {
46 return _v0 == 0 and _v1 == 0 and _v2 == 0;
47 }
48
49 constexpr explicit operator std::string() const noexcept
50 {
51 auto r = std::string{};
52 if (_v0 == 0) {
53 return r;
54 }
55
56 r += _v0;
57 if (_v1 == 0) {
58 return r;
59 }
60
61 r += _v1;
62 if (_v2 == 0) {
63 return r;
64 }
65
66 r += _v2;
67 return r;
68 }
69
70 [[nodiscard]] constexpr friend operator==(iso_639 const &lhs, iso_639 const &rhs) noexcept = default;
71 [[nodiscard]] constexpr friend operator<=>(iso_639 const &lhs, iso_639 const &rhs) noexcept = default;
72
73private:
74 uint8_t _v0;
75 uint8_t _v1;
76 uint8_t _v2;
77
78
84 [[nodiscard]] constexpr uint32_t to_int() const noexcept
85 {
86 return static_cast<uint32_t>(_v0) | (static_cast<uint32_t>(_v1) << 8) | (static_cast<uint32_t>(_v2) << 16);
87 }
88
89 constexpr void from_int(uint32_t v) noexcept
90 {
91 tt_axiom((v >> 24) == 0);
92 _v0 = static_cast<uint8_t>(v);
93 _v1 = static_cast<uint8_t>(v >> 8);
94 _v2 = static_cast<uint8_t>(v >> 16);
95 }
96};
97
98}
99
ISO-15924 script code.
Definition iso_15924.hpp:16
ISO-639 language code.
Definition iso_639.hpp:20