-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCharList.hpp
252 lines (225 loc) · 6.31 KB
/
CharList.hpp
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#pragma once
#include <string_view>
#include <array>
template <char ... a>
struct CharList;
using EmptyString = CharList<>;
template <typename T>
struct PopFrontImpl
{
using type = EmptyString;
};
template <char f, char...a>
struct PopFrontImpl<CharList<f, a...>>
{
using type = CharList<a...>;
};
template <typename T, typename O = EmptyString>
struct ReverseImpl
{
using type = O;
};
template <char f, char ... a, char ... o>
struct ReverseImpl<CharList<f, a...>, CharList<o...>>
{
using type = typename ReverseImpl<CharList<a...>, CharList<f, o...>>::type;
};
template <typename T>
struct PopBackImpl
{
using type = typename ReverseImpl<typename PopFrontImpl<typename ReverseImpl<T>::type>::type>::type;
};
template <typename T>
struct SizeImpl;
template <char ... a>
struct SizeImpl<CharList<a...>>
{
static constexpr size_t value = sizeof...(a);
};
template <typename T, size_t i, size_t desired_length, bool smaller_or_equal = (desired_length >= SizeImpl<T>::value)>
struct SubstringImpl
{
using type = typename SubstringImpl<typename PopFrontImpl<T>::type, i - 1, desired_length>::type;
};
template <typename T, size_t desired_length>
struct SubstringImpl<T, 0, desired_length, true>
{
using type = T;
};
template <typename T, size_t desired_length, bool smaller_or_equal>
struct SubstringImpl<T, 0, desired_length, smaller_or_equal>
{
using type = typename SubstringImpl<typename PopBackImpl<T>::type, 0, desired_length>::type;
};
template <typename T>
struct FrontImpl
{
static constexpr char value = '\0';
};
template <char f, char ... a>
struct FrontImpl<CharList<f, a...>>
{
static constexpr char value = f;
};
template <typename T>
struct BackImpl : FrontImpl<typename ReverseImpl<T>::type> {};
template <typename T, size_t i>
struct AtImpl
{
static constexpr char value = AtImpl<typename PopFrontImpl<T>::type, i - 1>::value;
};
template <typename T>
struct AtImpl<T, 0>
{
static constexpr char value = '\0';
};
template <char f, char ... a>
struct AtImpl<CharList<f, a...>, 0>
{
static constexpr char value = f;
};
template <char ... a>
struct CharList
{
template <size_t i>
static constexpr char At() noexcept
{
return AtImpl<CharList, i>::value;
}
static constexpr char Front() noexcept
{
return FrontImpl<CharList>::value;
}
static constexpr char Back() noexcept
{
return BackImpl<CharList>::value;
}
static constexpr size_t Size() noexcept
{
return SizeImpl<CharList>::value;
}
template <size_t i, size_t len>
static constexpr auto Substring() noexcept -> typename SubstringImpl<CharList, i, len>::type
{
return {};
}
static constexpr auto Reverse() noexcept -> typename ReverseImpl<CharList>::type
{
return {};
}
static constexpr auto PopFront() noexcept -> typename PopFrontImpl<CharList>::type
{
return {};
}
static constexpr auto PopBack() noexcept -> typename PopBackImpl<CharList>::type
{
return {};
}
template <char ... b>
static constexpr auto Append(CharList<b...>) noexcept -> CharList<a..., b...>
{
return {};
}
template <char ... b, char ... c, typename ... Args>
static constexpr auto Append(CharList<b...>, CharList<c...>, Args...args)
{
return Append(CharList<b..., c...>{}, args...);
}
static constexpr size_t length = sizeof...(a);
static constexpr size_t array_size = (length == 0) ? 1 : length;
static constexpr char value[ array_size ]{ a ... };
};
template <auto V>
struct ToCharListImpl
{
private:
template <auto Value>
class Int
{
private:
using T = decltype(Value);
using uint = std::make_unsigned_t<T>;
static constexpr uint abs = static_cast<uint>(Value >= 0 ? Value : -Value);
static constexpr uint Digits() noexcept
{
uint value = abs;
uint digits = 1;
while (value /= 10) ++digits;
return digits;
}
static constexpr bool negative = Value < 0;
static constexpr size_t digits = Digits();
static constexpr size_t length = (negative ? 1 : 0) + digits;
private:
using array_t = std::array<char, length>;
static constexpr array_t ToString() noexcept
{
auto reverse = [](auto first, auto last) constexpr
{
while ((first != last) && (first != --last))
{
auto& a = *first++;
auto& b = *last;
auto temp = b;
b = std::move(a);
a = std::move(temp);
}
};
array_t output{};
auto value = abs;
size_t index = 0;
do output[index++] = "0123456789"[value % 10];
while (value /= 10);
if constexpr (negative) output[index++] = '-';
reverse(output.begin(), output.begin() + length);
return output;
}
static constexpr array_t value = ToString();
template <typename L>
struct IntToLabel;
template <size_t ... I>
struct IntToLabel<std::index_sequence<I...>>
{
using type = CharList<value[I]...>;
};
public:
using type = typename IntToLabel<std::make_index_sequence<length>>::type;
};
template <auto V>
using Vt = decltype(V);
template <auto V, typename T = Vt<V>>
struct Dispatch;
template <auto Value> struct Dispatch<Value, char> { using type = CharList<Value>; };
template <auto Value> struct Dispatch<Value, int8_t> { using type = typename Int<Value>::type; };
template <auto Value> struct Dispatch<Value, int16_t> { using type = typename Int<Value>::type; };
template <auto Value> struct Dispatch<Value, int32_t> { using type = typename Int<Value>::type; };
template <auto Value> struct Dispatch<Value, int64_t> { using type = typename Int<Value>::type; };
template <auto Value> struct Dispatch<Value, uint8_t> { using type = typename Int<Value>::type; };
template <auto Value> struct Dispatch<Value, uint16_t> { using type = typename Int<Value>::type; };
template <auto Value> struct Dispatch<Value, uint32_t> { using type = typename Int<Value>::type; };
template <auto Value> struct Dispatch<Value, uint64_t> { using type = typename Int<Value>::type; };
public:
using type = typename Dispatch<V>::type;
};
template <>
struct ToCharListImpl<true>
{
using type = CharList<'t', 'r', 'u', 'e'>;
};
template <>
struct ToCharListImpl<false>
{
using type = CharList<'f', 'a', 'l', 's', 'e'>;
};
template <auto ... Vs>
struct CharListImpl
{
using type = decltype(CharList<>::Append((typename ToCharListImpl<Vs>::type{})...));
};
template <auto ... Vs>
using CreateCharList = typename CharListImpl<Vs...>::type;
template <char ... c>
constexpr auto ToStringView(CharList<c...>) noexcept -> std::string_view
{
return { std::data(CharList<c...>::value), CharList<c...>::length };
}