11#ifndef ALICAT_BASIS2_DETAIL_HPP
12#define ALICAT_BASIS2_DETAIL_HPP
23namespace alicat_basis2::detail {
38template <
typename Port,
typename Sig>
39 requires std::is_class_v<Port> && std::is_function_v<Sig>
43template <
typename Port,
typename R,
typename... Args>
44 requires std::is_class_v<Port>
45struct ExactHook<Port, R(Args...) noexcept> {
47 static void Take(R (Port::*hook)(Args...) noexcept) noexcept;
49 static void Take(R (Port::*hook)(Args...) const noexcept) noexcept;
54concept ClassType = std::is_class_v<T>;
59inline constexpr bool kAlwaysFalse =
false;
63concept Enumeration = std::is_enum_v<E>;
69template <Enumeration E>
70[[nodiscard]]
constexpr std::underlying_type_t<E> ToUnderlying(E e)
noexcept {
71 return static_cast<std::underlying_type_t<E>
>(e);
78template <Enumeration E>
85template <Enumeration E>
86[[nodiscard]]
constexpr bool IsEnumerator(E e)
noexcept {
87 return static_cast<uint32_t
>(ToUnderlying(e)) <=
88 static_cast<uint32_t
>(ToUnderlying(EnumLast<E>::kValue));
96template <
typename Reg, u
int32_t Shift, u
int32_t W
idth>
97 requires std::unsigned_integral<Reg> && (Width > 0U) && (Width < 32U) &&
98 ((Shift + Width) <=
static_cast<uint32_t
>(std::numeric_limits<Reg>::digits))
101 static constexpr uint32_t kMax = (uint32_t{1} << Width) - 1U;
103 static constexpr Reg kMask =
static_cast<Reg
>(kMax << Shift);
106 [[nodiscard]]
static constexpr Reg Insert(Reg reg, uint32_t value)
noexcept {
107 return static_cast<Reg
>((
static_cast<uint32_t
>(reg) & ~
static_cast<uint32_t
>(kMask)) |
108 ((value & kMax) << Shift));
111 [[nodiscard]]
static constexpr uint32_t Extract(Reg reg)
noexcept {
112 return (
static_cast<uint32_t
>(reg) >> Shift) & kMax;
117inline constexpr std::array<char, 10> kDigitChars{
'0',
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9'};
119inline constexpr std::array<char, 26> kUpperChars{
'A',
'B',
'C',
'D',
'E',
'F',
'G',
'H',
'I',
120 'J',
'K',
'L',
'M',
'N',
'O',
'P',
'Q',
'R',
121 'S',
'T',
'U',
'V',
'W',
'X',
'Y',
'Z'};
123inline constexpr std::array<char, 26> kLowerChars{
'a',
'b',
'c',
'd',
'e',
'f',
'g',
'h',
'i',
124 'j',
'k',
'l',
'm',
'n',
'o',
'p',
'q',
'r',
125 's',
't',
'u',
'v',
'w',
'x',
'y',
'z'};
129template <std::
size_t N>
130[[nodiscard]]
constexpr int32_t PlaceOf(
const std::array<char, N>& table,
char c)
noexcept {
131 const auto*
const found = std::find(table.begin(), table.end(), c);
132 return (found == table.end()) ? -1 :
static_cast<int32_t
>(found - table.begin());
136[[nodiscard]]
constexpr int32_t DigitValue(
char c)
noexcept {
137 return PlaceOf(kDigitChars, c);
140[[nodiscard]]
constexpr bool IsDigit(
char c)
noexcept {
141 return DigitValue(c) >= 0;
144[[nodiscard]]
constexpr char DigitChar(uint64_t value)
noexcept {
145 return kDigitChars[
static_cast<std::size_t
>(value % 10U)];
148[[nodiscard]]
constexpr int32_t UpperIndex(
char c)
noexcept {
149 return PlaceOf(kUpperChars, c);
152[[nodiscard]]
constexpr bool IsUpper(
char c)
noexcept {
153 return UpperIndex(c) >= 0;
156[[nodiscard]]
constexpr char UpperChar(uint32_t index)
noexcept {
157 return kUpperChars[
static_cast<std::size_t
>(index % 26U)];
160[[nodiscard]]
constexpr bool IsLower(
char c)
noexcept {
161 return PlaceOf(kLowerChars, c) >= 0;
164[[nodiscard]]
constexpr bool IsAlnum(
char c)
noexcept {
165 return IsUpper(c) || IsLower(c) || IsDigit(c);
168[[nodiscard]]
constexpr uint8_t ByteOf(
char c)
noexcept {
169 return std::bit_cast<uint8_t>(c);
173[[nodiscard]]
constexpr bool SameChar(
char first,
char second)
noexcept {
174 return ByteOf(first) == ByteOf(second);
177[[nodiscard]]
constexpr char CharOf(uint8_t
byte)
noexcept {
178 return std::bit_cast<char>(
byte);
181[[nodiscard]]
constexpr bool IsPrintableByte(uint8_t
byte)
noexcept {
182 return (
byte >= 0x20U) && (
byte <= 0x7EU);