ignore whitespace in try parse (#1536)

This commit is contained in:
David Markowitz 2024-04-08 13:13:19 -07:00 committed by GitHub
parent feeac2e041
commit 3260a063cb
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -156,8 +156,11 @@ namespace GeneralUtils {
* @returns An std::optional containing the desired value if it is equivalent to the string * @returns An std::optional containing the desired value if it is equivalent to the string
*/ */
template <Numeric T> template <Numeric T>
[[nodiscard]] std::optional<T> TryParse(const std::string_view str) { [[nodiscard]] std::optional<T> TryParse(std::string_view str) {
numeric_parse_t<T> result; numeric_parse_t<T> result;
if (!str.empty()) {
while (std::isspace(str.front())) str.remove_prefix(1);
}
const char* const strEnd = str.data() + str.size(); const char* const strEnd = str.data() + str.size();
const auto [parseEnd, ec] = std::from_chars(str.data(), strEnd, result); 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 * @returns An std::optional containing the desired value if it is equivalent to the string
*/ */
template <std::floating_point T> template <std::floating_point T>
[[nodiscard]] std::optional<T> TryParse(const std::string_view str) noexcept [[nodiscard]] std::optional<T> TryParse(std::string_view str) noexcept
try { try {
if (!str.empty()) {
while (std::isspace(str.front())) str.remove_prefix(1);
}
size_t parseNum; size_t parseNum;
const T result = details::_parse<T>(str, parseNum); const T result = details::_parse<T>(str, parseNum);
const bool isParsed = str.length() == parseNum; const bool isParsed = str.length() == parseNum;