diff options
author | MarcoFalke <falke.marco@gmail.com> | 2021-10-01 17:33:35 +0200 |
---|---|---|
committer | MarcoFalke <falke.marco@gmail.com> | 2021-10-01 18:05:33 +0200 |
commit | fa3cd2853530c86c261ac7266ffe4f1726fe9ce6 (patch) | |
tree | 6defa00c71da38b5286979f5fa8e4ae110223a2a /src/util | |
parent | 35a31d5f7e9cd71a210c1ed10abc9d772ff36049 (diff) |
refactor: Remove unused ParsePrechecks from ParseIntegral
Also:
* Remove redundant {} from return statement
* Add missing failing c-string test case and "-" and "+" strings
* Add missing failing test cases for non-int32_t integral types
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/strencodings.cpp | 5 | ||||
-rw-r--r-- | src/util/strencodings.h | 6 |
2 files changed, 4 insertions, 7 deletions
diff --git a/src/util/strencodings.cpp b/src/util/strencodings.cpp index 0aa80ea0ae..90bf39f010 100644 --- a/src/util/strencodings.cpp +++ b/src/util/strencodings.cpp @@ -281,16 +281,11 @@ std::string DecodeBase32(const std::string& str, bool* pf_invalid) return std::string((const char*)vchRet.data(), vchRet.size()); } -[[nodiscard]] static bool ParsePrechecks(const std::string&); - namespace { template <typename T> bool ParseIntegral(const std::string& str, T* out) { static_assert(std::is_integral<T>::value); - if (!ParsePrechecks(str)) { - return false; - } // Replicate the exact behavior of strtol/strtoll/strtoul/strtoull when // handling leading +/- for backwards compatibility. if (str.length() >= 2 && str[0] == '+' && str[1] == '-') { diff --git a/src/util/strencodings.h b/src/util/strencodings.h index 1217572c45..07e1966890 100644 --- a/src/util/strencodings.h +++ b/src/util/strencodings.h @@ -97,7 +97,9 @@ constexpr inline bool IsSpace(char c) noexcept { } /** - * Convert string to integral type T. + * Convert string to integral type T. Leading whitespace, a leading +, or any + * trailing character fail the parsing. The required format expressed as regex + * is `-?[0-9]+`. * * @returns std::nullopt if the entire string could not be parsed, or if the * parsed value is not in the range representable by the type T. @@ -111,7 +113,7 @@ std::optional<T> ToIntegral(const std::string& str) if (first_nonmatching != str.data() + str.size() || error_condition != std::errc{}) { return std::nullopt; } - return {result}; + return result; } /** |