1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
pub struct HexBytes<T>(pub T);
struct Iter<'a> {
s: &'a [u8],
i: usize,
}
impl<'a> Iter<'a> {
const fn new(s: &'a str) -> Self {
Self {
s: s.as_bytes(),
i: 0,
}
}
const fn next(mut self) -> (Self, Option<u8>) {
let mut i = self.i;
let s = self.s;
macro_rules! next {
() => {{
if i < s.len() {
let b = s[i];
i += 1;
Some(b)
} else {
None
}
}};
}
while let Some(b) = next!() {
let high = match b {
b'0'..=b'9' => b - b'0',
b'a'..=b'f' => b - b'a' + 10,
b'A'..=b'F' => b - b'A' + 10,
b' ' | b'\r' | b'\n' | b'\t' => continue,
_ => constfn_panic!("invalid character"),
};
let low = match next!() {
None => constfn_panic!("expected even number of hex characters"),
Some(b) => match b {
b'0'..=b'9' => b - b'0',
b'a'..=b'f' => b - b'a' + 10,
b'A'..=b'F' => b - b'A' + 10,
_ => constfn_panic!("expected hex character"),
},
};
self.i = i;
let val = (high << 4) | low;
return (self, Some(val));
}
(self, None)
}
}
impl<'a> HexBytes<&'a str> {
pub const fn output_len(&self) -> usize {
let mut ans = 0;
let mut iter = Iter::new(self.0);
while let (next, Some(_)) = iter.next() {
iter = next;
ans += 1;
}
ans
}
pub const fn const_eval<const N: usize>(&self) -> [u8; N] {
let mut buf = [0; N];
let mut pos = 0;
let mut iter = Iter::new(self.0);
while let (next, Some(val)) = iter.next() {
iter = next;
buf[pos] = val;
pos += 1;
}
constfn_assert!(pos == N);
buf
}
}
impl<'a, 'b> HexBytes<&'b [&'a str]> {
pub const fn output_len(&self) -> usize {
let mut i = 0;
let mut ans = 0;
while i < self.0.len() {
let mut iter = Iter::new(self.0[i]);
while let (next, Some(_)) = iter.next() {
iter = next;
ans += 1;
}
i += 1;
}
ans
}
pub const fn const_eval<const N: usize>(&self) -> [u8; N] {
let mut buf = [0; N];
let mut pos = 0;
let mut i = 0;
while i < self.0.len() {
let mut iter = Iter::new(self.0[i]);
while let (next, Some(val)) = iter.next() {
iter = next;
buf[pos] = val;
pos += 1;
}
i += 1;
}
constfn_assert!(pos == N);
buf
}
}
impl<'a, const L: usize> HexBytes<[&'a str; L]> {
pub const fn output_len(&self) -> usize {
let ss: &[&str] = &self.0;
HexBytes(ss).output_len()
}
pub const fn const_eval<const N: usize>(&self) -> [u8; N] {
let ss: &[&str] = &self.0;
HexBytes(ss).const_eval()
}
}
#[macro_export]
macro_rules! hex_bytes {
($s: expr) => {{
const OUTPUT_LEN: usize = $crate::__ctfe::HexBytes($s).output_len();
const OUTPUT_BUF: [u8; OUTPUT_LEN] = $crate::__ctfe::HexBytes($s).const_eval();
OUTPUT_BUF
}};
}