From da0059fb217195fbb3c6734b9cb06b37449b36b6 Mon Sep 17 00:00:00 2001 From: Connor McLaughlin Date: Fri, 28 Feb 2020 17:00:05 +1000 Subject: [PATCH] Common/StringUtil: Add a FromChars std::optional wrapper --- src/common/string_util.h | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/src/common/string_util.h b/src/common/string_util.h index a0100173e..fb6ff676c 100644 --- a/src/common/string_util.h +++ b/src/common/string_util.h @@ -1,7 +1,9 @@ #pragma once -#include +#include #include +#include #include +#include #include namespace StringUtil { @@ -26,4 +28,16 @@ inline int Strcasecmp(const char* s1, const char* s2) #endif } +/// Wrapper arond std::from_chars +template +std::optional FromChars(const std::string_view str) +{ + T value; + const std::from_chars_result result = std::from_chars(str.data(), str.data() + str.length(), value); + if (result.ec != std::errc()) + return std::nullopt; + + return value; +} + } // namespace StringUtil \ No newline at end of file