From 3260a063cb46d2ad24102c225fa3c138046ac82e Mon Sep 17 00:00:00 2001 From: David Markowitz <39972741+EmosewaMC@users.noreply.github.com> Date: Mon, 8 Apr 2024 13:13:19 -0700 Subject: [PATCH] ignore whitespace in try parse (#1536) --- dCommon/GeneralUtils.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/dCommon/GeneralUtils.h b/dCommon/GeneralUtils.h index d502f55a..f59ec71f 100644 --- a/dCommon/GeneralUtils.h +++ b/dCommon/GeneralUtils.h @@ -156,8 +156,11 @@ namespace GeneralUtils { * @returns An std::optional containing the desired value if it is equivalent to the string */ template - [[nodiscard]] std::optional TryParse(const std::string_view str) { + [[nodiscard]] std::optional TryParse(std::string_view str) { numeric_parse_t result; + if (!str.empty()) { + while (std::isspace(str.front())) str.remove_prefix(1); + } const char* const strEnd = str.data() + str.size(); const auto [parseEnd, ec] = std::from_chars(str.data(), strEnd, result); @@ -181,8 +184,12 @@ namespace GeneralUtils { * @returns An std::optional containing the desired value if it is equivalent to the string */ template - [[nodiscard]] std::optional TryParse(const std::string_view str) noexcept + [[nodiscard]] std::optional TryParse(std::string_view str) noexcept try { + if (!str.empty()) { + while (std::isspace(str.front())) str.remove_prefix(1); + } + size_t parseNum; const T result = details::_parse(str, parseNum); const bool isParsed = str.length() == parseNum;