From 0b8fd6aa207fed0fb5e3e553ac259fecfb414529 Mon Sep 17 00:00:00 2001 From: IbaiBuR Date: Tue, 15 Oct 2024 12:21:13 +0200 Subject: [PATCH] feat: Add parsing utils BENCH: 21337504 --- src/utils/parsing.hpp | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/utils/parsing.hpp diff --git a/src/utils/parsing.hpp b/src/utils/parsing.hpp new file mode 100644 index 0000000..712bfa4 --- /dev/null +++ b/src/utils/parsing.hpp @@ -0,0 +1,32 @@ +#pragma once + +#include +#include +#include +#include +#include + +namespace utils::parsing { + +namespace internal { + +template +concept NumberInput = std::is_same_v || std::is_same_v; + +template +concept NumberOutput = std::is_integral_v || std::is_floating_point_v; + +} // namespace internal + +template +constexpr std::optional to_number(const Input& s) { + Output value{}; + const auto result = std::from_chars(s.data(), s.data() + s.size(), value); + + if (result.ec == std::errc()) + return value; + + return std::nullopt; +} + +} // namespace utils::parsing