25 if (s.empty())
return std::nullopt;
26 const std::size_t sign = s[0] ==
'-' ? 1 : 0;
27 if (sign == s.size())
return std::nullopt;
28 const char c = s[sign];
29 if (!((c >=
'0' && c <=
'9') || c ==
'.' || c ==
'i' || c ==
'I' || c ==
'n' || c ==
'N'))
return std::nullopt;
30 if (c ==
'0' && sign + 1 < s.size() && (s[sign + 1] ==
'x' || s[sign + 1] ==
'X'))
return std::nullopt;
31 static const locale_t c_locale = newlocale(LC_ALL_MASK,
"C", locale_t{});
32 const std::string buf(s);
35 const double v = strtod_l(buf.c_str(), &end, c_locale);
36 if (end != buf.c_str() + buf.size())
return std::nullopt;
39 if (errno == ERANGE && (v == 0.0 || v == HUGE_VAL || v == -HUGE_VAL))
return std::nullopt;