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;