Closes #19
This commit is contained in:
1 parent
d87c100398
commit
4ae4e527b8
5 files changed
+138
-10
No files matched your search
@@ -35,14 +35,18 @@ namespace {
|
||||
R"(^[+-]?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)(?:[eE][+-]?[0-9]+)?$)");
|
||||
if (!std::regex_match(text, number))
|
||||
throw std::runtime_error(std::string("Invalid ") + field + ": " + text);
|
||||
std::size_t consumed = 0U;
|
||||
double value;
|
||||
try {
|
||||
value = std::stod(text, &consumed);
|
||||
} catch (const std::exception &) {
|
||||
throw std::runtime_error(std::string("Invalid ") + field + ": " + text);
|
||||
}
|
||||
if (consumed != text.size() || !std::isfinite(value))
|
||||
// Floating-point from_chars is locale-independent and does not accept
|
||||
// a leading '+', which the DBC numeric grammar permits.
|
||||
const char *begin = text.data();
|
||||
const char *end = begin + text.size();
|
||||
if (*begin == '+')
|
||||
++begin;
|
||||
double value = 0.0;
|
||||
const std::from_chars_result parsed =
|
||||
std::from_chars(begin, end, value, std::chars_format::general);
|
||||
// Keep the existing rejection of conversion underflow into subnormal values.
|
||||
if (parsed.ec != std::errc() || parsed.ptr != end || !std::isfinite(value) ||
|
||||
std::fpclassify(value) == FP_SUBNORMAL)
|
||||
throw std::runtime_error(std::string("Invalid ") + field + ": " + text);
|
||||
return value;
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user