MayaFlux 1.0.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches

◆ parse_numbers()

template<typename T>
requires (std::is_integral_v<T> || std::is_floating_point_v<T>)
std::optional< std::vector< T > > MayaFlux::Reflect::parse_numbers ( std::string_view source)
nodiscard

Parse whitespace separated finite numbers.

Returns
The values, or std::nullopt for any malformed or non-finite token.

Definition at line 17 of file TextCodec.hpp.

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}

Referenced by MayaFlux::Reflect::TextCodec< std::vector< T > >::decode(), MayaFlux::Reflect::TextCodec< T >::decode(), and parse_tuple().

Here is the caller graph for this function: