From b10f8d29f44e4ba42dd4e52703cb04d143815f26 Mon Sep 17 00:00:00 2001 From: 251 <13120787+251Labs@users.noreply.github.com> Date: Sun, 22 Jul 2018 21:34:45 +0200 Subject: [PATCH] Removes Boost predicate.hpp dependency This is a squashed commit that squashes the following commits: This commit removes the `boost/algorithm/string/predicate.hpp` dependenc from the project by replacing the function calls to `boost::algorithm::starts_with` `boost::algorithm::ends_with` and `all` with respectively C++11' `std::basic_string::front`, `std::basic_string::back`, `std::all_of` function calls This commit replaces `boost::algorithm::is_digit` with a locale independent isdigi function, because the use of the standard library's `isdigit` and `std::isdigit functions is discoraged in the developer notes --- src/bitcoind.cpp | 3 +-- src/core_read.cpp | 11 ++++++----- src/init.cpp | 1 - src/netbase.cpp | 5 +---- src/test/util_tests.cpp | 15 +++++++++++++++ src/torcontrol.cpp | 1 - src/util.cpp | 4 +--- src/utilstrencodings.cpp | 10 +++++----- src/utilstrencodings.h | 10 ++++++++++ src/wallet/rpcdump.cpp | 4 ++-- 10 files changed, 41 insertions(+), 23 deletions(-) diff --git a/src/bitcoind.cpp b/src/bitcoind.cpp index 34b786fdd9d..d803ddb2aed 100644 --- a/src/bitcoind.cpp +++ b/src/bitcoind.cpp @@ -21,7 +21,6 @@ #include "httprpc.h" #include "utilstrencodings.h" -#include #include #include @@ -121,7 +120,7 @@ bool AppInit(int argc, char* argv[]) // Command-line RPC bool fCommandLine = false; for (int i = 1; i < argc; i++) - if (!IsSwitchChar(argv[i][0]) && !boost::algorithm::istarts_with(argv[i], "dogecoin:")) + if (!IsSwitchChar(argv[i][0]) && strncmp(argv[i], "dogecoin:", strlen("dogecoin:")) != 0) fCommandLine = true; if (fCommandLine) diff --git a/src/core_read.cpp b/src/core_read.cpp index bb716aa26bd..fe7a6f926bc 100644 --- a/src/core_read.cpp +++ b/src/core_read.cpp @@ -15,10 +15,11 @@ #include "version.h" #include -#include #include #include +#include + CScript ParseScript(const std::string& s) { CScript result; @@ -53,20 +54,20 @@ CScript ParseScript(const std::string& s) { // Empty string, ignore. (boost::split given '' will return one word) } - else if (all(*w, boost::algorithm::is_digit()) || - (boost::algorithm::starts_with(*w, "-") && all(std::string(w->begin()+1, w->end()), boost::algorithm::is_digit()))) + else if (std::all_of(w->begin(), w->end(), ::IsDigit) || + (w->front() == '-' && w->size() > 1 && std::all_of(w->begin()+1, w->end(), ::IsDigit))) { // Number int64_t n = atoi64(*w); result << n; } - else if (boost::algorithm::starts_with(*w, "0x") && (w->begin()+2 != w->end()) && IsHex(std::string(w->begin()+2, w->end()))) + else if (w->substr(0,2) == "0x" && w->size() > 2 && IsHex(std::string(w->begin()+2, w->end()))) { // Raw hex data, inserted NOT pushed onto stack: std::vector raw = ParseHex(std::string(w->begin()+2, w->end())); result.insert(result.end(), raw.begin(), raw.end()); } - else if (w->size() >= 2 && boost::algorithm::starts_with(*w, "'") && boost::algorithm::ends_with(*w, "'")) + else if (w->size() >= 2 && w->front() == '\'' && w->back() == '\'') { // Single-quoted string, pushed as data. NOTE: this is poor-man's // parsing, spaces/tabs/newlines in single-quoted strings won't work. diff --git a/src/init.cpp b/src/init.cpp index ebbd5495d14..317b0fa7b3f 100644 --- a/src/init.cpp +++ b/src/init.cpp @@ -54,7 +54,6 @@ #endif #include -#include #include #include #include diff --git a/src/netbase.cpp b/src/netbase.cpp index 78995953e2a..0970f339e9b 100644 --- a/src/netbase.cpp +++ b/src/netbase.cpp @@ -24,8 +24,6 @@ #include #endif -#include // for startswith() and endswith() - #if !defined(HAVE_MSG_NOSIGNAL) && !defined(MSG_NOSIGNAL) #define MSG_NOSIGNAL 0 #endif @@ -142,8 +140,7 @@ bool LookupHost(const std::string& name, std::vector& vIP, unsigned in std::string strHost = name; if (strHost.empty()) return false; - if (boost::algorithm::starts_with(strHost, "[") && boost::algorithm::ends_with(strHost, "]")) - { + if (strHost.front() == '[' && strHost.back() == ']') { strHost = strHost.substr(1, strHost.size() - 2); } diff --git a/src/test/util_tests.cpp b/src/test/util_tests.cpp index 4414ff10ea4..32db0f8fc84 100644 --- a/src/test/util_tests.cpp +++ b/src/test/util_tests.cpp @@ -311,6 +311,21 @@ BOOST_AUTO_TEST_CASE(gettime) BOOST_CHECK((GetTime() & ~0xFFFFFFFFLL) == 0); } +BOOST_AUTO_TEST_CASE(test_IsDigit) +{ + BOOST_CHECK_EQUAL(IsDigit('0'), true); + BOOST_CHECK_EQUAL(IsDigit('1'), true); + BOOST_CHECK_EQUAL(IsDigit('8'), true); + BOOST_CHECK_EQUAL(IsDigit('9'), true); + + BOOST_CHECK_EQUAL(IsDigit('0' - 1), false); + BOOST_CHECK_EQUAL(IsDigit('9' + 1), false); + BOOST_CHECK_EQUAL(IsDigit(0), false); + BOOST_CHECK_EQUAL(IsDigit(1), false); + BOOST_CHECK_EQUAL(IsDigit(8), false); + BOOST_CHECK_EQUAL(IsDigit(9), false); +} + BOOST_AUTO_TEST_CASE(test_ParseInt32) { int32_t n; diff --git a/src/torcontrol.cpp b/src/torcontrol.cpp index 2a74d070015..9d5e368034a 100644 --- a/src/torcontrol.cpp +++ b/src/torcontrol.cpp @@ -17,7 +17,6 @@ #include #include -#include #include #include #include diff --git a/src/util.cpp b/src/util.cpp index 3e082a028cc..cec3466d6d8 100644 --- a/src/util.cpp +++ b/src/util.cpp @@ -78,8 +78,6 @@ #include #endif -#include // for to_lower() -#include // for startswith() and endswith() #include #include #include @@ -354,7 +352,7 @@ void ParseParameters(int argc, const char* const argv[]) } #ifdef WIN32 Downcase(str); - if (boost::algorithm::starts_with(str, "/")) + if (str.front() == '/') str = "-" + str.substr(1); #endif diff --git a/src/utilstrencodings.cpp b/src/utilstrencodings.cpp index d3b74cca426..5682064ee3d 100644 --- a/src/utilstrencodings.cpp +++ b/src/utilstrencodings.cpp @@ -637,7 +637,7 @@ bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out) /* pass single 0 */ ++ptr; } else if (val[ptr] >= '1' && val[ptr] <= '9') { - while (ptr < end && val[ptr] >= '0' && val[ptr] <= '9') { + while (ptr < end && IsDigit(val[ptr])) { if (!ProcessMantissaDigit(val[ptr], mantissa, mantissa_tzeros)) return false; /* overflow */ ++ptr; @@ -647,9 +647,9 @@ bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out) if (ptr < end && val[ptr] == '.') { ++ptr; - if (ptr < end && val[ptr] >= '0' && val[ptr] <= '9') + if (ptr < end && IsDigit(val[ptr])) { - while (ptr < end && val[ptr] >= '0' && val[ptr] <= '9') { + while (ptr < end && IsDigit(val[ptr])) { if (!ProcessMantissaDigit(val[ptr], mantissa, mantissa_tzeros)) return false; /* overflow */ ++ptr; @@ -666,8 +666,8 @@ bool ParseFixedPoint(const std::string &val, int decimals, int64_t *amount_out) exponent_sign = true; ++ptr; } - if (ptr < end && val[ptr] >= '0' && val[ptr] <= '9') { - while (ptr < end && val[ptr] >= '0' && val[ptr] <= '9') { + if (ptr < end && IsDigit(val[ptr])) { + while (ptr < end && IsDigit(val[ptr])) { if (exponent > (UPPER_BOUND / 10LL)) return false; /* overflow */ exponent = exponent * 10 + val[ptr] - '0'; diff --git a/src/utilstrencodings.h b/src/utilstrencodings.h index 234390fb193..68aa7938f2e 100644 --- a/src/utilstrencodings.h +++ b/src/utilstrencodings.h @@ -53,6 +53,16 @@ int64_t atoi64(const char* psz); int64_t atoi64(const std::string& str); int atoi(const std::string& str); +/** + * Tests if the given character is a decimal digit. + * @param[in] c character to test + * @return true if the argument is a decimal digit; otherwise false. + */ +constexpr bool IsDigit(char c) +{ + return c >= '0' && c <= '9'; +} + /** * Convert string to signed 32-bit integer with strict parse error feedback. * @returns true if the entire string could be parsed as valid integer, diff --git a/src/wallet/rpcdump.cpp b/src/wallet/rpcdump.cpp index 9357b49261e..39ebc38c196 100644 --- a/src/wallet/rpcdump.cpp +++ b/src/wallet/rpcdump.cpp @@ -508,13 +508,13 @@ UniValue importwallet(const JSONRPCRequest& request) std::string strLabel; bool fLabel = true; for (unsigned int nStr = 2; nStr < vstr.size(); nStr++) { - if (boost::algorithm::starts_with(vstr[nStr], "#")) + if (vstr[nStr].front() == '#') break; if (vstr[nStr] == "change=1") fLabel = false; if (vstr[nStr] == "reserve=1") fLabel = false; - if (boost::algorithm::starts_with(vstr[nStr], "label=")) { + if (vstr[nStr].substr(0,6) == "label=") { strLabel = DecodeDumpString(vstr[nStr].substr(6)); fLabel = true; }