Parse whitespace separated finite numbers.
18{
19 const auto whitespace = [](char value) {
20 return value == ' ' || value == '\t' || value == '\n' || value == '\r';
21 };
22
23 std::vector<T> values;
24 const char* current = source.data();
25 const char* end = current + source.size();
26 while (current != end) {
27 while (current != end && whitespace(*current))
28 ++current;
29 if (current == end)
30 break;
31
32 const bool positive_sign = *current == '+';
33 if (positive_sign)
34 ++current;
35
36 T value {};
37 const auto [next, status] = std::from_chars(current, end, value);
38 if ((positive_sign && (current == end || *current == '+' || *current == '-'))
39 || status != std::errc {} || next == current
40 || (std::is_floating_point_v<T> && !std::isfinite(value))
41 || (next != end && !whitespace(*next))) {
42 return std::nullopt;
43 }
44 values.push_back(value);
45 current = next;
46 }
47 return values;
48}