|
3 | 3 |
|
4 | 4 | #include <charconv> |
5 | 5 | #include <concepts> |
6 | | -#include <limits> |
7 | 6 | #include <map> |
8 | 7 | #include <optional> |
9 | 8 | #include <set> |
@@ -72,45 +71,18 @@ rfl::Result<T> parse_value( |
72 | 71 | return value; |
73 | 72 | } |
74 | 73 |
|
75 | | -template <class T> requires (std::is_unsigned_v<T> && !std::same_as<T, bool>) |
| 74 | +template <class T> requires (std::is_integral_v<T> && !std::same_as<T, bool>) |
76 | 75 | rfl::Result<T> parse_value( |
77 | 76 | const std::string& _str, const std::string& _path |
78 | 77 | ) noexcept { |
79 | | - try { |
80 | | - if (!_str.empty() && _str[0] == '-') { |
81 | | - return error( |
82 | | - "Value '" + _str + "' is negative, cannot convert to unsigned integer for key '" + _path + "'."); |
83 | | - } |
84 | | - const auto val = std::stoull(_str); |
85 | | - if (val > static_cast<unsigned long long>(std::numeric_limits<T>::max())) { |
86 | | - return error( |
87 | | - "Value '" + _str + "' is out of range for key '" + _path + "'."); |
88 | | - } |
89 | | - return static_cast<T>(val); |
90 | | - } |
91 | | - catch (...) { |
92 | | - return error( |
93 | | - "Could not cast '" + _str + "' to unsigned integer for key '" + _path + "'."); |
94 | | - } |
95 | | -} |
96 | | - |
97 | | -template <class T> requires (std::is_integral_v<T> && std::is_signed_v<T>) |
98 | | -rfl::Result<T> parse_value( |
99 | | - const std::string& _str, const std::string& _path |
100 | | -) noexcept { |
101 | | - try { |
102 | | - const auto val = std::stoll(_str); |
103 | | - if (val < static_cast<long long>(std::numeric_limits<T>::min()) |
104 | | - || val > static_cast<long long>(std::numeric_limits<T>::max())) { |
105 | | - return error( |
106 | | - "Value '" + _str + "' is out of range for key '" + _path + "'."); |
107 | | - } |
108 | | - return static_cast<T>(val); |
109 | | - } |
110 | | - catch (...) { |
| 78 | + T value; |
| 79 | + const auto [ptr, ec] = |
| 80 | + std::from_chars(_str.data(), _str.data() + _str.size(), value); |
| 81 | + if (ec != std::errc() || ptr != _str.data() + _str.size()) { |
111 | 82 | return error( |
112 | 83 | "Could not cast '" + _str + "' to integer for key '" + _path + "'."); |
113 | 84 | } |
| 85 | + return value; |
114 | 86 | } |
115 | 87 |
|
116 | 88 | struct Reader { |
|
0 commit comments