From 55c11448ffddfefa593745f3939f4f976920181a Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Fri, 7 Mar 2025 09:44:52 +0100 Subject: [PATCH] util/NumberParser: add ParseIntegerTo() An version of the function without the `std::optional` overhead. --- src/util/NumberParser.hxx | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/util/NumberParser.hxx b/src/util/NumberParser.hxx index 35cf9153a..823946620 100644 --- a/src/util/NumberParser.hxx +++ b/src/util/NumberParser.hxx @@ -18,6 +18,17 @@ FromChars(std::string_view s, std::integral auto &value, int base=10) noexcept return std::from_chars(s.data(), s.data() + s.size(), value, base); } +/** + * A wrapper for FromChars() which translates the #from_chars_result + * to a boolean (true on success, false on error). + */ +inline bool +ParseIntegerTo(std::string_view s, std::integral auto &value, int base=10) noexcept +{ + auto [ptr, ec] = FromChars(s, value, base); + return ptr == s.data() + s.size() && ec == std::errc{}; +} + template [[gnu::pure]] std::optional