-
Notifications
You must be signed in to change notification settings - Fork 4
/
unformat.h
341 lines (292 loc) · 7.97 KB
/
unformat.h
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#pragma once
#ifdef _MSC_VER
#if _MSC_VER >= 1910 && _HAS_CXX17
#define UNFORMAT_CPP17
#endif
#endif
#include <string>
#ifdef UNFORMAT_CPP17
#include <string_view>
#endif
namespace
{
template <typename T>
void unformat_signed_int(const char* input, const char* inputEnd, T& output) noexcept
{
output = 0;
int sign = 1;
if (*input == '-')
{
sign = -1;
++input;
}
#ifndef UNFORMAT_DISABLE_PLUS_SIGN
// Ignore plus
if (*input == '+')
{
++input;
}
#endif
while (input != inputEnd)
{
output *= 10;
output += (*input - '0');
++input;
}
output *= sign;
}
template <typename T>
void unformat_unsigned_int(const char* input, const char* inputEnd, T& output) noexcept
{
output = 0;
while (input != inputEnd)
{
output *= 10;
output += (*input - '0');
++input;
}
}
template <typename T>
void unformat_real(const char* input, const char* inputEnd, T& output) noexcept
{
long double f = 0;
// Check for negative
if (*input == '-')
{
f = -f;
++input;
}
#ifndef UNFORMAT_DISABLE_PLUS_SIGN
// Ignore plus
if (*input == '+')
{
++input;
}
#endif
// Parse units
while (*input != '.' && input != inputEnd && *input != 'e')
{
f *= 10;
f += (*input - '0');
++input;
}
// Parse decimal
if (*input == '.')
{
++input;
long double decimal = 1.0L;
while (input != inputEnd && *input != 'e')
{
decimal *= 0.1L;
f += (*input - '0') * decimal;
++input;
}
}
// Parse exponent
if (*input == 'e')
{
++input;
if (*input == '+')
{
++input; // Skip + as unformat_signed_int can't handle it
}
int e;
unformat_signed_int(input, inputEnd, e);
f *= pow(10L, e);
}
output = static_cast<T>(f);
}
}
namespace ay
{
// Not defined on purpose for the general case. Note that you can define custom unformatters by defining new specialisations
// for this function.
template <typename T>
void unformat_arg(const char* input, const char* inputEnd, T& output) noexcept;
template <typename T>
T unformat_arg(const char* input, const char* inputEnd) noexcept
{
T output;
unformat_arg(input, inputEnd, output);
return output;
}
#ifdef UNFORMAT_CPP17
template <typename T>
T unformat_arg(std::string_view input) noexcept
{
return unformat_arg<T>(input.data(), input.data() + input.length());
}
#endif
template <typename T>
T unformat_arg(std::string input) noexcept
{
return unformat_arg<T>(input.c_str(), input.c_str() + input.length());
}
template <>
inline void unformat_arg<char>(const char* input, const char* inputEnd, char& output) noexcept
{
output = input[0];
}
template <>
inline void unformat_arg<unsigned char>(const char* input, const char* inputEnd, unsigned char& output) noexcept
{
output = input[0];
}
#ifdef UNFORMAT_CPP17
template <>
inline void unformat_arg<std::string_view>(const char* input, const char* inputEnd, std::string_view& output) noexcept
{
output = std::string_view(input, inputEnd - input);
}
#endif
template <>
inline void unformat_arg<std::string>(const char* input, const char* inputEnd, std::string& output) noexcept
{
output.assign(input, inputEnd - input);
}
template <>
inline void unformat_arg<float>(const char* input, const char* inputEnd, float& output) noexcept
{
unformat_real<float>(input, inputEnd, output);
}
template <>
inline void unformat_arg<double>(const char* input, const char* inputEnd, double& output) noexcept
{
unformat_real<double>(input, inputEnd, output);
}
template <>
inline void unformat_arg<short>(const char* input, const char* inputEnd, short& output) noexcept
{
unformat_signed_int<short>(input, inputEnd, output);
}
template <>
inline void unformat_arg<int>(const char* input, const char* inputEnd, int& output) noexcept
{
unformat_signed_int<int>(input, inputEnd, output);
}
template <>
inline void unformat_arg<long>(const char* input, const char* inputEnd, long& output) noexcept
{
unformat_signed_int<long>(input, inputEnd, output);
}
template <>
inline void unformat_arg<long long>(const char* input, const char* inputEnd, long long& output) noexcept
{
unformat_signed_int<long long>(input, inputEnd, output);
}
template <>
inline void unformat_arg<unsigned short>(const char* input, const char* inputEnd, unsigned short& output) noexcept
{
unformat_unsigned_int<unsigned short>(input, inputEnd, output);
}
template <>
inline void unformat_arg<unsigned int>(const char* input, const char* inputEnd, unsigned int& output) noexcept
{
unformat_unsigned_int<unsigned int>(input, inputEnd, output);
}
template <>
inline void unformat_arg<unsigned long>(const char* input, const char* inputEnd, unsigned long& output) noexcept
{
unformat_unsigned_int<unsigned long>(input, inputEnd, output);
}
template <>
inline void unformat_arg<unsigned long long>(const char* input, const char* inputEnd, unsigned long long& output) noexcept
{
unformat_unsigned_int<unsigned long long>(input, inputEnd, output);
}
struct format;
constexpr format make_format_non_template(const char* str, std::size_t N);
struct format
{
constexpr format() {}
template <std::size_t N>
constexpr format(const char(&str)[N])
{
*this = make_format_non_template(str, N);
}
format(const char* str)
{
*this = make_format_non_template(str, std::strlen(str));
}
static constexpr std::size_t MAX_COUNT{ 16 };
std::size_t offsets[MAX_COUNT]{};
char endChar[MAX_COUNT]{};
std::size_t count{ 0 };
};
template <std::size_t N>
constexpr format make_format(const char(&str)[N]) noexcept
{
return make_format_non_template(str, N);
}
constexpr format make_format_non_template(const char* str, std::size_t N)
{
format format;
std::size_t from = 0;
for (std::size_t to = 0; to < N - 1; to++)
{
if (str[to] == '{' && str[to + 1] == '}')
{
if (format.count >= format::MAX_COUNT)
{
// Stops compilation in a constexpr context
throw std::exception("Max number of {} has been exceeded.");
}
format.offsets[format.count] = to - from;
format.endChar[format.count] = str[to + 2]; // May be '\0' which is fine
++format.count;
// Advance markers
from = to + 2;
to = from;
}
}
return format;
}
// Empty function to end recursion, no more args to process
inline void unformat_internal(std::size_t inputPos, const char* input, const format& format) noexcept
{
}
template <typename T, typename... TRest>
void unformat_internal(std::size_t inputPos, const char* input, const format& format, T& first, TRest&... rest) noexcept
{
const std::size_t argNo = format.count - sizeof...(rest) - 1;
// Get the location of the first brace
const auto offset = format.offsets[argNo];
// Find input string
inputPos += offset;
auto inputEnd = inputPos;
while (input[inputEnd] != format.endChar[argNo])
{
++inputEnd;
}
// Process this arg
unformat_arg(&input[inputPos], &input[inputEnd], first);
// Process TRest
unformat_internal(inputEnd, input, format, rest...);
}
// Parses and extracts data from 'input' given a braced styled "{}" 'format' into 'args...'
// For example:
// std::string name, int age;
// constexpr auto format = make_format("{} is {} years old.");
// unformat("Harry is 18 years old.", format, name, age);
//
// Then the following data is extracted:
// name == "Harry" and age == 18
template <typename... Args>
void unformat(const char* input, const format& format, Args&... args) noexcept
{
unformat_internal(0, input, format, args...);
}
// Parses and extracts data from 'input' given a braced styled "{}" 'format' into 'args...'
// For example:
// std::string name, int age;
// constexpr auto format = make_format("{} is {} years old.");
// unformat("Harry is 18 years old.", format, name, age);
//
// Then the following data is extracted:
// name == "Harry" and age == 18
template <typename... Args>
void unformat(const std::string& input, const format& format, Args&... args) noexcept
{
unformat_internal(0, input.c_str(), format, args...);
}
}