From 85427522961633c99ec4038f29c28e2554081b13 Mon Sep 17 00:00:00 2001 From: Kittywhiskers Van Gogh <63189531+kittywhiskers@users.noreply.github.com> Date: Sun, 30 Oct 2022 22:12:34 +0530 Subject: [PATCH] merge bitcoin#20429: replace (sizeof(a)/sizeof(a[0])) with C++17 std::size Co-authored-by: UdjinM6 --- src/base58.cpp | 2 +- src/bench/data.cpp | 2 +- src/chainparams.cpp | 5 ++--- src/protocol.cpp | 5 ++--- src/qt/networkstyle.cpp | 13 ++++++------- src/random.cpp | 3 +-- src/rest.cpp | 20 +++++++++++--------- src/rpc/blockchain.cpp | 5 +++-- src/rpc/coinjoin.cpp | 5 +++-- src/rpc/evo.cpp | 4 ++-- src/rpc/governance.cpp | 5 +++-- src/rpc/masternode.cpp | 5 +++-- src/rpc/mining.cpp | 5 +++-- src/rpc/misc.cpp | 5 +++-- src/rpc/net.cpp | 5 +++-- src/rpc/quorums.cpp | 5 +++-- src/rpc/rawtransaction.cpp | 5 +++-- src/test/base32_tests.cpp | 2 +- src/test/base64_tests.cpp | 2 +- src/test/hash_tests.cpp | 4 ++-- src/test/miner_tests.cpp | 3 +-- src/test/script_tests.cpp | 16 ++++++++++------ src/test/scriptnum_tests.cpp | 8 ++++---- src/test/sighash_tests.cpp | 2 +- src/util/strencodings.h | 2 -- 25 files changed, 73 insertions(+), 65 deletions(-) diff --git a/src/base58.cpp b/src/base58.cpp index 4be95be173..822c9306f2 100644 --- a/src/base58.cpp +++ b/src/base58.cpp @@ -52,7 +52,7 @@ bool DecodeBase58(const char* psz, std::vector& vch, int max_ret_ int size = strlen(psz) * 733 /1000 + 1; // log(58) / log(256), rounded up. std::vector b256(size); // Process the characters. - static_assert(sizeof(mapBase58)/sizeof(mapBase58[0]) == 256, "mapBase58.size() should be 256"); // guarantee not out of range + static_assert(std::size(mapBase58) == 256, "mapBase58.size() should be 256"); // guarantee not out of range while (*psz && !IsSpace(*psz)) { // Decode base58 character int carry = mapBase58[(uint8_t)*psz]; diff --git a/src/bench/data.cpp b/src/bench/data.cpp index 16f1169fd2..e910cddad9 100644 --- a/src/bench/data.cpp +++ b/src/bench/data.cpp @@ -8,7 +8,7 @@ namespace benchmark { namespace data { #include -const std::vector block813851{raw_bench::block813851_raw, raw_bench::block813851_raw + sizeof(raw_bench::block813851_raw) / sizeof(raw_bench::block813851_raw[0])}; +const std::vector block813851{std::begin(raw_bench::block813851_raw), std::end(raw_bench::block813851_raw)}; } // namespace data } // namespace benchmark diff --git a/src/chainparams.cpp b/src/chainparams.cpp index 1d82f8cbf9..60ef60638d 100644 --- a/src/chainparams.cpp +++ b/src/chainparams.cpp @@ -12,7 +12,6 @@ #include #include #include -#include #include #include @@ -296,7 +295,7 @@ public: // Dash BIP44 coin type is '5' nExtCoinType = 5; - vFixedSeeds = std::vector(pnSeed6_main, pnSeed6_main + ARRAYLEN(pnSeed6_main)); + vFixedSeeds = std::vector(std::begin(pnSeed6_main), std::end(pnSeed6_main)); // long living quorum params AddLLMQ(Consensus::LLMQType::LLMQ_50_60); @@ -496,7 +495,7 @@ public: assert(genesis.hashMerkleRoot == uint256S("0xe0028eb9648db56b1ac77cf090b99048a8007e2bb64b68f092c03c7f56a662c7")); vFixedSeeds.clear(); - vFixedSeeds = std::vector(pnSeed6_test, pnSeed6_test + ARRAYLEN(pnSeed6_test)); + vFixedSeeds = std::vector(std::begin(pnSeed6_test), std::end(pnSeed6_test)); vSeeds.clear(); // nodes with support for servicebits filtering should be at the top diff --git a/src/protocol.cpp b/src/protocol.cpp index 5dcb7bdd21..d2f69ba034 100644 --- a/src/protocol.cpp +++ b/src/protocol.cpp @@ -5,7 +5,6 @@ #include -#include #include static std::atomic g_initial_block_download_completed(false); @@ -172,7 +171,7 @@ const static std::string allNetMessageTypes[] = { NetMsgType::GETHEADERS2, NetMsgType::SENDHEADERS2, NetMsgType::HEADERS2}; -const static std::vector allNetMessageTypesVec(allNetMessageTypes, allNetMessageTypes+ARRAYLEN(allNetMessageTypes)); +const static std::vector allNetMessageTypesVec(std::begin(allNetMessageTypes), std::end(allNetMessageTypes)); /** Message types that are not allowed by blocks-relay-only policy. * We do not want most of CoinJoin, DKG or LLMQ signing messages to be relayed @@ -206,7 +205,7 @@ const static std::string netMessageTypesViolateBlocksOnly[] = { NetMsgType::QWATCH, NetMsgType::TX, }; -const static std::set netMessageTypesViolateBlocksOnlySet(netMessageTypesViolateBlocksOnly, netMessageTypesViolateBlocksOnly+ARRAYLEN(netMessageTypesViolateBlocksOnly)); +const static std::set netMessageTypesViolateBlocksOnlySet(std::begin(netMessageTypesViolateBlocksOnly), std::end(netMessageTypesViolateBlocksOnly)); CMessageHeader::CMessageHeader() { diff --git a/src/qt/networkstyle.cpp b/src/qt/networkstyle.cpp index d22e2cd1b8..9b28277122 100644 --- a/src/qt/networkstyle.cpp +++ b/src/qt/networkstyle.cpp @@ -26,7 +26,6 @@ static const struct { {"devnet", QAPP_APP_NAME_DEVNET, 190, 20, "[devnet: %s]"}, {"regtest", QAPP_APP_NAME_REGTEST, 160, 30, "[regtest]"} }; -static const unsigned network_styles_count = sizeof(network_styles)/sizeof(*network_styles); void NetworkStyle::rotateColor(QColor& col, const int iconColorHueShift, const int iconColorSaturationReduction) { @@ -89,12 +88,12 @@ NetworkStyle::NetworkStyle(const QString &_appName, const int iconColorHueShift, const NetworkStyle *NetworkStyle::instantiate(const QString &networkId) { - for (unsigned x=0; x #endif #ifdef HAVE_SYSCTL_ARND -#include // for ARRAYLEN #include #endif @@ -334,7 +333,7 @@ void GetOSRand(unsigned char *ent32) int have = 0; do { size_t len = NUM_OS_RANDOM_BYTES - have; - if (sysctl(name, ARRAYLEN(name), ent32 + have, &len, nullptr, 0) != 0) { + if (sysctl(name, std::size(name), ent32 + have, &len, nullptr, 0) != 0) { RandFailure(); } have += len; diff --git a/src/rest.cpp b/src/rest.cpp index 61bf145ba4..f1dd490849 100644 --- a/src/rest.cpp +++ b/src/rest.cpp @@ -19,7 +19,6 @@ #include #include #include -#include #include #include @@ -117,9 +116,10 @@ static RetFormat ParseDataFormat(std::string& param, const std::string& strReq) param = strReq.substr(0, pos); const std::string suff(strReq, pos + 1); - for (unsigned int i = 0; i < ARRAYLEN(rf_names); i++) - if (suff == rf_names[i].name) - return rf_names[i].rf; + for (const auto& rf_name : rf_names) { + if (suff == rf_name.name) + return rf_name.rf; + } /* If no suffix is found, return original string. */ param = strReq; @@ -129,12 +129,13 @@ static RetFormat ParseDataFormat(std::string& param, const std::string& strReq) static std::string AvailableDataFormatsString() { std::string formats; - for (unsigned int i = 0; i < ARRAYLEN(rf_names); i++) - if (strlen(rf_names[i].name) > 0) { + for (const auto& rf_name : rf_names) { + if (strlen(rf_name.name) > 0) { formats.append("."); - formats.append(rf_names[i].name); + formats.append(rf_name.name); formats.append(", "); } + } if (formats.length() > 0) return formats.substr(0, formats.length() - 2); @@ -695,6 +696,7 @@ void InterruptREST() void StopREST() { - for (unsigned int i = 0; i < ARRAYLEN(uri_prefixes); i++) - UnregisterHTTPHandler(uri_prefixes[i].prefix, false); + for (const auto& up : uri_prefixes) { + UnregisterHTTPHandler(up.prefix, false); + } } diff --git a/src/rpc/blockchain.cpp b/src/rpc/blockchain.cpp index a1e441f7f0..62d5c2bc0f 100644 --- a/src/rpc/blockchain.cpp +++ b/src/rpc/blockchain.cpp @@ -2813,6 +2813,7 @@ static const CRPCCommand commands[] = void RegisterBlockchainRPCCommands(CRPCTable &t) { - for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++) - t.appendCommand(commands[vcidx].name, &commands[vcidx]); + for (const auto& command : commands) { + t.appendCommand(command.name, &command); + } } diff --git a/src/rpc/coinjoin.cpp b/src/rpc/coinjoin.cpp index ae13d4bee9..44130a423a 100644 --- a/src/rpc/coinjoin.cpp +++ b/src/rpc/coinjoin.cpp @@ -181,6 +181,7 @@ static const CRPCCommand commands[] = // clang-format on void RegisterCoinJoinRPCCommands(CRPCTable &t) { - for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++) - t.appendCommand(commands[vcidx].name, &commands[vcidx]); + for (const auto& command : commands) { + t.appendCommand(command.name, &command); + } } diff --git a/src/rpc/evo.cpp b/src/rpc/evo.cpp index 57f98b6a03..d981c31a64 100644 --- a/src/rpc/evo.cpp +++ b/src/rpc/evo.cpp @@ -1333,7 +1333,7 @@ static const CRPCCommand commands[] = // clang-format on void RegisterEvoRPCCommands(CRPCTable &tableRPC) { - for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++) { - tableRPC.appendCommand(commands[vcidx].name, &commands[vcidx]); + for (const auto& command : commands) { + tableRPC.appendCommand(command.name, &command); } } diff --git a/src/rpc/governance.cpp b/src/rpc/governance.cpp index 3a948cfae9..c253d2e842 100644 --- a/src/rpc/governance.cpp +++ b/src/rpc/governance.cpp @@ -1218,6 +1218,7 @@ static const CRPCCommand commands[] = // clang-format on void RegisterGovernanceRPCCommands(CRPCTable &t) { - for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++) - t.appendCommand(commands[vcidx].name, &commands[vcidx]); + for (const auto& command : commands) { + t.appendCommand(command.name, &command); + } } diff --git a/src/rpc/masternode.cpp b/src/rpc/masternode.cpp index 180ca1d53b..e2f3ef69d9 100644 --- a/src/rpc/masternode.cpp +++ b/src/rpc/masternode.cpp @@ -702,6 +702,7 @@ static const CRPCCommand commands[] = // clang-format on void RegisterMasternodeRPCCommands(CRPCTable &t) { - for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++) - t.appendCommand(commands[vcidx].name, &commands[vcidx]); + for (const auto& command : commands) { + t.appendCommand(command.name, &command); + } } diff --git a/src/rpc/mining.cpp b/src/rpc/mining.cpp index 636963e1e0..de75e2b446 100644 --- a/src/rpc/mining.cpp +++ b/src/rpc/mining.cpp @@ -1258,6 +1258,7 @@ static const CRPCCommand commands[] = void RegisterMiningRPCCommands(CRPCTable &t) { - for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++) - t.appendCommand(commands[vcidx].name, &commands[vcidx]); + for (const auto& command : commands) { + t.appendCommand(command.name, &command); + } } diff --git a/src/rpc/misc.cpp b/src/rpc/misc.cpp index f41d9c7621..fe8d95dc7e 100644 --- a/src/rpc/misc.cpp +++ b/src/rpc/misc.cpp @@ -1320,6 +1320,7 @@ static const CRPCCommand commands[] = void RegisterMiscRPCCommands(CRPCTable &t) { - for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++) - t.appendCommand(commands[vcidx].name, &commands[vcidx]); + for (const auto& command : commands) { + t.appendCommand(command.name, &command); + } } diff --git a/src/rpc/net.cpp b/src/rpc/net.cpp index acde004086..ae5e5f962c 100644 --- a/src/rpc/net.cpp +++ b/src/rpc/net.cpp @@ -831,6 +831,7 @@ static const CRPCCommand commands[] = void RegisterNetRPCCommands(CRPCTable &t) { - for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++) - t.appendCommand(commands[vcidx].name, &commands[vcidx]); + for (const auto& command : commands) { + t.appendCommand(command.name, &command); + } } diff --git a/src/rpc/quorums.cpp b/src/rpc/quorums.cpp index 40834f784d..4a89542e24 100644 --- a/src/rpc/quorums.cpp +++ b/src/rpc/quorums.cpp @@ -858,6 +858,7 @@ static const CRPCCommand commands[] = // clang-format on void RegisterQuorumsRPCCommands(CRPCTable &tableRPC) { - for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++) - tableRPC.appendCommand(commands[vcidx].name, &commands[vcidx]); + for (const auto& command : commands) { + tableRPC.appendCommand(command.name, &command); + } } diff --git a/src/rpc/rawtransaction.cpp b/src/rpc/rawtransaction.cpp index fb43def00c..8aa8482e88 100644 --- a/src/rpc/rawtransaction.cpp +++ b/src/rpc/rawtransaction.cpp @@ -1706,6 +1706,7 @@ static const CRPCCommand commands[] = void RegisterRawTransactionRPCCommands(CRPCTable &t) { - for (unsigned int vcidx = 0; vcidx < ARRAYLEN(commands); vcidx++) - t.appendCommand(commands[vcidx].name, &commands[vcidx]); + for (const auto& command : commands) { + t.appendCommand(command.name, &command); + } } diff --git a/src/test/base32_tests.cpp b/src/test/base32_tests.cpp index 5fa3d2cb36..e4ee9e3dda 100644 --- a/src/test/base32_tests.cpp +++ b/src/test/base32_tests.cpp @@ -14,7 +14,7 @@ BOOST_AUTO_TEST_CASE(base32_testvectors) static const std::string vstrIn[] = {"","f","fo","foo","foob","fooba","foobar"}; static const std::string vstrOut[] = {"","my======","mzxq====","mzxw6===","mzxw6yq=","mzxw6ytb","mzxw6ytboi======"}; static const std::string vstrOutNoPadding[] = {"","my","mzxq","mzxw6","mzxw6yq","mzxw6ytb","mzxw6ytboi"}; - for (unsigned int i=0; ihashPrevBlock = pblock->GetHash(); }; - for (unsigned int i = 0; i < sizeof(blockinfo)/sizeof(*blockinfo) - 1; ++i) - { + for ([[maybe_unused]] const auto& _ : blockinfo) { createAndProcessEmptyBlock(); } diff --git a/src/test/script_tests.cpp b/src/test/script_tests.cpp index f729f0c4cc..589ef77384 100644 --- a/src/test/script_tests.cpp +++ b/src/test/script_tests.cpp @@ -100,18 +100,22 @@ static ScriptErrorDesc script_errors[]={ static const char *FormatScriptError(ScriptError_t err) { - for (unsigned int i=0; i #include -#define ARRAYLEN(array) (sizeof(array)/sizeof((array)[0])) - /** Used by SanitizeString() */ enum SafeChars {