diff options
author | MarcoFalke <falke.marco@gmail.com> | 2021-10-04 15:05:55 +0200 |
---|---|---|
committer | MarcoFalke <falke.marco@gmail.com> | 2021-10-04 15:06:37 +0200 |
commit | 42fedb4acd3cfa813059fcc3f96b2a41f78d9074 (patch) | |
tree | 9fe2a104daa2bc50d0b7d1d3b2c56edb5a2de779 /src/util | |
parent | cdb4dfcbf1c82ef4c4eb7bb982b35219d5dbf827 (diff) | |
parent | fa9d72a7947d2cff541794e21e0040c3c1d43b32 (diff) |
Merge bitcoin/bitcoin#23156: refactor: Remove unused ParsePrechecks and ParseDouble
fa9d72a7947d2cff541794e21e0040c3c1d43b32 Remove unused ParseDouble and ParsePrechecks (MarcoFalke)
fa3cd2853530c86c261ac7266ffe4f1726fe9ce6 refactor: Remove unused ParsePrechecks from ParseIntegral (MarcoFalke)
Pull request description:
All of the `ParsePrechecks` are already done by `ToIntegral`, so remove them from `ParseIntegral`.
Also:
* Remove redundant `{}`. See https://github.com/bitcoin/bitcoin/pull/20457#discussion_r720116866
* Add missing failing c-string test case
* Add missing failing test cases for non-int32_t integral types
ACKs for top commit:
laanwj:
Code review ACK fa9d72a7947d2cff541794e21e0040c3c1d43b32, good find on ParseDouble not being used at all, and testing for behavior of embedded NULL characters is always a good thing.
practicalswift:
cr ACK fa9d72a7947d2cff541794e21e0040c3c1d43b32
Tree-SHA512: 3d654dcaebbf312dd57e54241f9aa6d35b1d1d213c37e4c6b8b9a69bcbe8267a397474a8b86b57740fbdd8e3d03b4cdb6a189a9eb8e05cd38035dab195410aa7
Diffstat (limited to 'src/util')
-rw-r--r-- | src/util/strencodings.cpp | 30 | ||||
-rw-r--r-- | src/util/strencodings.h | 13 |
2 files changed, 4 insertions, 39 deletions
diff --git a/src/util/strencodings.cpp b/src/util/strencodings.cpp index 88fec6740a..15bd07b374 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] == '-') { @@ -307,17 +302,6 @@ bool ParseIntegral(const std::string& str, T* out) } }; // namespace -[[nodiscard]] static bool ParsePrechecks(const std::string& str) -{ - if (str.empty()) // No empty string allowed - return false; - if (str.size() >= 1 && (IsSpace(str[0]) || IsSpace(str[str.size()-1]))) // No padding allowed - return false; - if (!ValidAsCString(str)) // No embedded NUL characters allowed - return false; - return true; -} - bool ParseInt32(const std::string& str, int32_t* out) { return ParseIntegral<int32_t>(str, out); @@ -348,20 +332,6 @@ bool ParseUInt64(const std::string& str, uint64_t* out) return ParseIntegral<uint64_t>(str, out); } -bool ParseDouble(const std::string& str, double *out) -{ - if (!ParsePrechecks(str)) - return false; - if (str.size() >= 2 && str[0] == '0' && str[1] == 'x') // No hexadecimal floats allowed - return false; - std::istringstream text(str); - text.imbue(std::locale::classic()); - double result; - text >> result; - if(out) *out = result; - return text.eof() && !text.fail(); -} - std::string FormatParagraph(const std::string& in, size_t width, size_t indent) { std::stringstream out; diff --git a/src/util/strencodings.h b/src/util/strencodings.h index 166352c42f..1f7762aeef 100644 --- a/src/util/strencodings.h +++ b/src/util/strencodings.h @@ -123,7 +123,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. @@ -137,7 +139,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; } /** @@ -183,13 +185,6 @@ std::optional<T> ToIntegral(const std::string& str) [[nodiscard]] bool ParseUInt64(const std::string& str, uint64_t *out); /** - * Convert string to double with strict parse error feedback. - * @returns true if the entire string could be parsed as valid double, - * false if not the entire string could be parsed or when overflow or underflow occurred. - */ -[[nodiscard]] bool ParseDouble(const std::string& str, double *out); - -/** * Convert a span of bytes to a lower-case hexadecimal string. */ std::string HexStr(const Span<const uint8_t> s); |