-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnumeric_utils.h
More file actions
60 lines (54 loc) · 1.64 KB
/
numeric_utils.h
File metadata and controls
60 lines (54 loc) · 1.64 KB
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
#ifndef ABURI_NUMERIC_UTILS_H
#define ABURI_NUMERIC_UTILS_H
#include <cerrno>
#include <cstdint>
#include <cstdlib>
#include <optional>
#include <string>
inline std::optional<uint64_t> parse_integer_literal_u64(const std::string& text) {
if (text.size() >= 2 && text[0] == '0' && (text[1] == 'b' || text[1] == 'B')) {
if (text.size() == 2) {
return std::nullopt;
}
uint64_t value = 0;
for (size_t i = 2; i < text.size(); ++i) {
char c = text[i];
if (c != '0' && c != '1') {
return std::nullopt;
}
value = (value << 1) | static_cast<uint64_t>(c - '0');
}
return value;
}
errno = 0;
char* end = nullptr;
unsigned long long value = std::strtoull(text.c_str(), &end, 0);
if (end == text.c_str() || errno == ERANGE) {
return std::nullopt;
}
// Allow trailing integer suffixes (L, l, U, u, LL, ll, etc.)
while (*end == 'L' || *end == 'l' || *end == 'U' || *end == 'u') {
++end;
}
if (*end != '\0') {
return std::nullopt;
}
return static_cast<uint64_t>(value);
}
inline std::optional<long double> parse_floating_literal_ld(const std::string& text) {
errno = 0;
char* end = nullptr;
long double value = std::strtold(text.c_str(), &end);
if (end == text.c_str() || errno == ERANGE) {
return std::nullopt;
}
// Allow floating suffixes like f/F/l/L.
while (*end == 'f' || *end == 'F' || *end == 'l' || *end == 'L') {
++end;
}
if (*end != '\0') {
return std::nullopt;
}
return value;
}
#endif // ABURI_NUMERIC_UTILS_H