| 5 | #include <string_view> |
| 6 | |
| 7 | std::expected<double, std::errc> parse_number(std::string_view &str) |
| 8 | { |
| 9 | double result; |
| 10 | auto begin = str.data(); |
| 11 | auto [end, ec] = std::from_chars(begin, begin + str.size(), result); |
| 12 | |
| 13 | if (ec != std::errc{}) |
| 14 | return std::unexpected{ ec }; |
| 15 | if (std::isinf(result)) // we regard inf as out of range too. |
| 16 | return std::unexpected{ std::errc::result_out_of_range }; |
| 17 | |
| 18 | str.remove_prefix(end - begin); |
| 19 | return result; |
| 20 | } |
| 21 | |
| 22 | int main() |
| 23 | { |