Skip to content

Commit fa3cd28

Browse files
author
MarcoFalke
committed
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
1 parent 35a31d5 commit fa3cd28

File tree

3 files changed

+41
-28
lines changed

3 files changed

+41
-28
lines changed

src/test/util_tests.cpp

Lines changed: 37 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1474,6 +1474,35 @@ BOOST_AUTO_TEST_CASE(test_ParseInt32)
14741474
BOOST_CHECK(!ParseInt32("32482348723847471234", nullptr));
14751475
}
14761476

1477+
template <typename T>
1478+
static void RunToIntegralTests()
1479+
{
1480+
BOOST_CHECK(!ToIntegral<T>(STRING_WITH_EMBEDDED_NULL_CHAR));
1481+
BOOST_CHECK(!ToIntegral<T>(" 1"));
1482+
BOOST_CHECK(!ToIntegral<T>("1 "));
1483+
BOOST_CHECK(!ToIntegral<T>("1a"));
1484+
BOOST_CHECK(!ToIntegral<T>("1.1"));
1485+
BOOST_CHECK(!ToIntegral<T>("1.9"));
1486+
BOOST_CHECK(!ToIntegral<T>("+01.9"));
1487+
BOOST_CHECK(!ToIntegral<T>("-"));
1488+
BOOST_CHECK(!ToIntegral<T>("+"));
1489+
BOOST_CHECK(!ToIntegral<T>(" -1"));
1490+
BOOST_CHECK(!ToIntegral<T>("-1 "));
1491+
BOOST_CHECK(!ToIntegral<T>(" -1 "));
1492+
BOOST_CHECK(!ToIntegral<T>("+1"));
1493+
BOOST_CHECK(!ToIntegral<T>(" +1"));
1494+
BOOST_CHECK(!ToIntegral<T>(" +1 "));
1495+
BOOST_CHECK(!ToIntegral<T>("+-1"));
1496+
BOOST_CHECK(!ToIntegral<T>("-+1"));
1497+
BOOST_CHECK(!ToIntegral<T>("++1"));
1498+
BOOST_CHECK(!ToIntegral<T>("--1"));
1499+
BOOST_CHECK(!ToIntegral<T>(""));
1500+
BOOST_CHECK(!ToIntegral<T>("aap"));
1501+
BOOST_CHECK(!ToIntegral<T>("0x1"));
1502+
BOOST_CHECK(!ToIntegral<T>("-32482348723847471234"));
1503+
BOOST_CHECK(!ToIntegral<T>("32482348723847471234"));
1504+
}
1505+
14771506
BOOST_AUTO_TEST_CASE(test_ToIntegral)
14781507
{
14791508
BOOST_CHECK_EQUAL(ToIntegral<int32_t>("1234").value(), 1'234);
@@ -1486,27 +1515,14 @@ BOOST_AUTO_TEST_CASE(test_ToIntegral)
14861515
BOOST_CHECK_EQUAL(ToIntegral<int32_t>("-1234").value(), -1'234);
14871516
BOOST_CHECK_EQUAL(ToIntegral<int32_t>("-1").value(), -1);
14881517

1489-
BOOST_CHECK(!ToIntegral<int32_t>(" 1"));
1490-
BOOST_CHECK(!ToIntegral<int32_t>("1 "));
1491-
BOOST_CHECK(!ToIntegral<int32_t>("1a"));
1492-
BOOST_CHECK(!ToIntegral<int32_t>("1.1"));
1493-
BOOST_CHECK(!ToIntegral<int32_t>("1.9"));
1494-
BOOST_CHECK(!ToIntegral<int32_t>("+01.9"));
1495-
BOOST_CHECK(!ToIntegral<int32_t>(" -1"));
1496-
BOOST_CHECK(!ToIntegral<int32_t>("-1 "));
1497-
BOOST_CHECK(!ToIntegral<int32_t>(" -1 "));
1498-
BOOST_CHECK(!ToIntegral<int32_t>("+1"));
1499-
BOOST_CHECK(!ToIntegral<int32_t>(" +1"));
1500-
BOOST_CHECK(!ToIntegral<int32_t>(" +1 "));
1501-
BOOST_CHECK(!ToIntegral<int32_t>("+-1"));
1502-
BOOST_CHECK(!ToIntegral<int32_t>("-+1"));
1503-
BOOST_CHECK(!ToIntegral<int32_t>("++1"));
1504-
BOOST_CHECK(!ToIntegral<int32_t>("--1"));
1505-
BOOST_CHECK(!ToIntegral<int32_t>(""));
1506-
BOOST_CHECK(!ToIntegral<int32_t>("aap"));
1507-
BOOST_CHECK(!ToIntegral<int32_t>("0x1"));
1508-
BOOST_CHECK(!ToIntegral<int32_t>("-32482348723847471234"));
1509-
BOOST_CHECK(!ToIntegral<int32_t>("32482348723847471234"));
1518+
RunToIntegralTests<uint64_t>();
1519+
RunToIntegralTests<int64_t>();
1520+
RunToIntegralTests<uint32_t>();
1521+
RunToIntegralTests<int32_t>();
1522+
RunToIntegralTests<uint16_t>();
1523+
RunToIntegralTests<int16_t>();
1524+
RunToIntegralTests<uint8_t>();
1525+
RunToIntegralTests<int8_t>();
15101526

15111527
BOOST_CHECK(!ToIntegral<int64_t>("-9223372036854775809"));
15121528
BOOST_CHECK_EQUAL(ToIntegral<int64_t>("-9223372036854775808").value(), -9'223'372'036'854'775'807LL - 1LL);

src/util/strencodings.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,16 +281,11 @@ std::string DecodeBase32(const std::string& str, bool* pf_invalid)
281281
return std::string((const char*)vchRet.data(), vchRet.size());
282282
}
283283

284-
[[nodiscard]] static bool ParsePrechecks(const std::string&);
285-
286284
namespace {
287285
template <typename T>
288286
bool ParseIntegral(const std::string& str, T* out)
289287
{
290288
static_assert(std::is_integral<T>::value);
291-
if (!ParsePrechecks(str)) {
292-
return false;
293-
}
294289
// Replicate the exact behavior of strtol/strtoll/strtoul/strtoull when
295290
// handling leading +/- for backwards compatibility.
296291
if (str.length() >= 2 && str[0] == '+' && str[1] == '-') {

src/util/strencodings.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,9 @@ constexpr inline bool IsSpace(char c) noexcept {
9797
}
9898

9999
/**
100-
* Convert string to integral type T.
100+
* Convert string to integral type T. Leading whitespace, a leading +, or any
101+
* trailing character fail the parsing. The required format expressed as regex
102+
* is `-?[0-9]+`.
101103
*
102104
* @returns std::nullopt if the entire string could not be parsed, or if the
103105
* parsed value is not in the range representable by the type T.
@@ -111,7 +113,7 @@ std::optional<T> ToIntegral(const std::string& str)
111113
if (first_nonmatching != str.data() + str.size() || error_condition != std::errc{}) {
112114
return std::nullopt;
113115
}
114-
return {result};
116+
return result;
115117
}
116118

117119
/**

0 commit comments

Comments
 (0)