Nix (Dev) 3.5.10
dev - 3.5.10 - 1af9301
Loading...
Searching...
No Matches
parsers.hpp
Go to the documentation of this file.
1#ifndef IV_SRC_CORE_PARSERS_HPP_
2#define IV_SRC_CORE_PARSERS_HPP_
3
4#include "core/concepts.hpp"
5
6#include <charconv>
7
8namespace iv::utils
9{
10
11template<typename T, iv::concepts::StringRepresentable Str>
12std::optional<T> parseNumber(const Str &stringNumber)
13{
14 std::string_view strView(stringNumber);
15 T number = 0;
16
17 if (not strView.empty() && strView[0] == '+')
18 {
19 strView.remove_prefix(1);
20 }
21
22 auto [p, ec] = std::from_chars(strView.data(), strView.data() + strView.size(), number);
23
24 if (ec == std::errc() && p == strView.data() + strView.size())
25 {
26 return number;
27 }
28
29 return std::nullopt;
30}
31
32}// namespace iv::utils
33
34#endif//IV_SRC_CORE_PARSERS_HPP_
Definition utils.cpp:6
std::optional< T > parseNumber(const Str &stringNumber)
Definition parsers.hpp:12