MCPcopy Create free account
hub / github.com/Extra-Creativity/Modern-Cpp-Basics / parse_number

Function parse_number

07-Error Handling/code/expected.cpp:7–20  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5#include <string_view>
6
7std::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
22int main()
23{

Callers 1

mainFunction · 0.85

Calls 2

dataMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected