-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathParser.hxx
314 lines (254 loc) · 8.55 KB
/
Parser.hxx
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
/* Copyright 2015, 2016 Robert Haberlach
Distributed under the Boost Software License, Version 1.0.
(See accompanying file LICENSE_1_0.txt or http://www.boost.org/LICENSE_1_0.txt) */
#pragma once
#include "String.hxx"
#include "Math.hxx"
namespace Constainer {
enum class PState {Good, Eof, Fail};
template <typename Iterator>
struct ParserState {
Iterator iterator;
PState state;
};
namespace detail {
template <typename InputIt>
constexpr InputIt skipWS(InputIt first, InputIt last) {
String whitespace = " \t\n\f\v\r";
return Constainer::find_first_not_of(first, last, whitespace.begin(), whitespace.end());
}
template <typename Arithmetic, typename InputIt>
constexpr auto parseSign( InputIt& first )
-> typename STD::conditional_t<STD::is_integral<Arithmetic>{}, STD::make_signed<Arithmetic>, STD::remove_cv<Arithmetic>>::type
{
if (*first == '-') {
++first;
return -1;
}
if (*first == '+')
++first;
return 1;
}
}
/** \brief Extracts an integer value from the character range specified by [first, last).
* Very similar to STD::strtol. Errors are indicated via the second member of the
* returned struct, which is of type PState.
*
* Valid values for the base range from 2 to 36, but 0 is also one;
* In the latter case, the bases actual value will be deduced from the prefix.
* If the prefix is 0x or 0X, the base is 16. If the prefix is a sole 0, the
* base is 8. The base is 10 otherwise.
*
* If the parsed value is larger or smaller than Int can represent, the returned state
* will be PState::Fail - in that case the value of the object referred to by res is set
* to the clamped value of the parsed value, that is, the largest or smallest one representable
* by Int, respectively. If the string does not represent a valid integer, the state is Eof.
* It is Good otherwise.
*
* \param first An iterator to the first element of the character sequence to extract the values from.
* \param first An iterator to the first element of the character sequence to extract the values from.
* \param res A reference to an object in which the result will be stored
* \param base The base of the representation to parse.
* Can be zero, in which case it will be deduced from the character sequence.
* \return A pair that contains both the iterator to the first non-consumed character
* and the state that the parser had at the end of the operation.
*
*/
template <typename Int, typename InputIt>
constexpr auto strToInt( InputIt first, InputIt last, Int& res, int base=10 )
-> require<STD::is_integral<Int>, ParserState<InputIt>>
{
constexpr auto MAX = STD::numeric_limits<Int>::max(),
MIN = STD::numeric_limits<Int>::lowest();
String digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
res = 0; // So premature error returns set res to zero
first = detail::skipWS(first, last);
if (first == last)
return {first, PState::Eof};
auto sign = detail::parseSign<Int>(first);
if (base == 0) {
if (first == last) return {first, PState::Eof};
auto suffix_1 = *first++;
if (suffix_1 != '0')
base = 10;
else {
if (first == last) return {first, PState::Eof};
auto suffix_2 = *first;
if (suffix_2 == 'X' || suffix_2 == 'x') {
base = 16;
++first;
}
else
base = 8;
}
}
res = 0;
bool read = false;
STD::size_t found=0;
while (first != last
&& (found = digits.rfind(toupper(*first), base-1)) != digits.npos)
{
const auto summand = sign*Int(found);
// Overflow check. Separate because of two's complement.
if (sign == 1)
if (res > MAX/base || res*base > MAX-summand) {
res = MAX;
return {first, PState::Fail};
}
if (sign == -1)
if (res < MIN/base || res*base < MIN-summand) {
res = MIN;
return {first, PState::Fail};
}
(res *= base) += summand;
read = true;
++first;
}
if (!read) {res = 0; return {first, PState::Eof};}
return {first, PState::Good};
}
template <typename Int>
constexpr auto strToInt( char const* str, STD::size_t len, STD::size_t* pos = 0, int base = 10 )
-> require<STD::is_integral<Int>, Int> {
Int ret = 0;
auto st = strToInt<Int>(str, str+len, ret, base);
AssertExcept<STD::invalid_argument>(st.state != PState::Eof, "Could not extract any integer");
AssertExcept<STD::out_of_range> (st.state != PState::Fail, "Integer represented is out of bounds");
if (pos)
*pos = st.iterator - str;
return ret;
}
template <typename Int>
constexpr auto strToInt( char const* str, STD::size_t* pos = 0, int base = 10 )
-> require<STD::is_integral<Int>, Int> {
return strToInt<Int>(str, CharTraits<char>::length(str), pos, base);
}
template <typename Int, STD::size_t N>
constexpr auto strToInt( BasicString<char, N> const& str, STD::size_t str_pos = 0,
STD::size_t* pos = 0, int base = 10 )
-> require<STD::is_integral<Int>, Int> {
return strToInt<Int>(str.data()+str_pos, str.size(), pos, base);
}
template <typename Float, typename InputIt>
constexpr auto strToFloat( InputIt first, InputIt last, Float& res )
-> require<STD::is_floating_point<Float>, ParserState<InputIt>>
{
String digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
res = 0; // So a premature return (see below) does not leave res unset
first = detail::skipWS(first, last);
if (first == last)
return {first, PState::Eof};
auto sign = detail::parseSign<Float>(first);
if (first == last) return {first, PState::Eof};
int base = 10;
bool read = false;
switch (toupper(*first)) {
case '0':
++first;
if (first == last) {
res = sign*0.;
return {first, PState::Good};
}
if (toupper(*first) == 'X') {
base = 16;
++first;
if (first == last) return {first, PState::Eof};
}
else
read = true;
break;
case 'I': {
++first;
char const inf_str[] = "NFINITY",
*p = inf_str;
while (first != last && *p && toupper(*first) == *p) {
++first; ++p;
}
switch (p-inf_str) {
case 2: case 7:
if (!STD::numeric_limits<Float>::has_infinity) {
return {first, PState::Fail};
}
res = sign * STD::numeric_limits<Float>::infinity();
return {first, PState::Good};
default:
return {first, PState::Fail};
}
}
case 'N': {
++first;
char const nan_str[] = "AN",
*p = nan_str;
while (first != last && *p && toupper(*first) == *p) {
++first;
++p;
}
if (p-nan_str != 2)
return {first, PState::Eof};
if (not STD::numeric_limits<Float>::has_quiet_NaN)
return {first, PState::Fail};
if (first != last && *first == '(') {
//! TODO: Implement proper NaN-String recognition
first = Constainer::find_first_not_of(first+1, last, digits.begin(), digits.end());
if (first == last || *first != ')')
return {first, PState::Eof};
}
res = STD::numeric_limits<Float>::quiet_NaN();
return {first, PState::Good};
}
}
Float multiplier = 1;
for (; first != last; ++first)
{
STD::size_t found = digits.rfind(toupper(*first), base-1);
if (found == digits.npos) {
if (*first != '.' || multiplier != 1)
break;
multiplier /= base;
continue;
}
const auto summand = sign*multiplier*Float(found);
constexpr auto bound = STD::numeric_limits<Float>::max();
if (abs(res) > bound/base || abs(res)*base > bound-abs(summand)) {
res = bound;
return {first, PState::Fail};
}
(res *= (multiplier == 1? base : 1)) += summand;
read = true;
if (multiplier != 1)
multiplier /= base;
}
if (!read) {res = 0; return {first, PState::Eof};}
// Apply exponent
const char exp_char = base==10? 'E' : 'P';
if (first != last && toupper(*first) == exp_char) {
++first;
int exp=0;
auto p_state = strToInt(first, last, exp, 10); // exponent is always decimal
first = p_state.iterator;
if (p_state.state != PState::Good) {
res = 0;
return {first, p_state.state};
}
auto factor = pow(Float(2), exp);
res = safeMul(res, factor);
}
return {first, PState::Good};
}
template <typename Float>
constexpr auto strToFloat( char const* str, STD::size_t len, STD::size_t* pos = 0 )
-> require<STD::is_floating_point<Float>, Float> {
Float ret = 0;
auto st = strToFloat<Float>(str, str+len, ret);
AssertExcept<STD::invalid_argument>(st.state != PState::Eof, "Could not extract any number");
AssertExcept<STD::out_of_range> (st.state != PState::Fail, "Floating point represented is out of bounds");
if (pos)
*pos = st.iterator - str;
return ret;
}
template <typename Float>
constexpr auto strToFloat( char const* str, STD::size_t* pos = 0 )
-> require<STD::is_floating_point<Float>, Float> {
return strToFloat<Float>(str, CharTraits<char>::length(str), pos);
}
}