merge bitcoin#20429: replace (sizeof(a)/sizeof(a[0])) with C++17 std::size

Co-authored-by: UdjinM6 <UdjinM6@users.noreply.github.com>
This commit is contained in:
Kittywhiskers Van Gogh 2022-10-30 22:12:34 +05:30 committed by PastaPastaPasta
parent 8133469746
commit 8542752296
25 changed files with 73 additions and 65 deletions

View File

@ -52,7 +52,7 @@ bool DecodeBase58(const char* psz, std::vector<unsigned char>& vch, int max_ret_
int size = strlen(psz) * 733 /1000 + 1; // log(58) / log(256), rounded up.
std::vector<unsigned char> 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];

View File

@ -8,7 +8,7 @@ namespace benchmark {
namespace data {
#include <bench/data/block813851.raw.h>
const std::vector<uint8_t> block813851{raw_bench::block813851_raw, raw_bench::block813851_raw + sizeof(raw_bench::block813851_raw) / sizeof(raw_bench::block813851_raw[0])};
const std::vector<uint8_t> block813851{std::begin(raw_bench::block813851_raw), std::end(raw_bench::block813851_raw)};
} // namespace data
} // namespace benchmark

View File

@ -12,7 +12,6 @@
#include <tinyformat.h>
#include <util/ranges.h>
#include <util/system.h>
#include <util/strencodings.h>
#include <versionbitsinfo.h>
#include <arith_uint256.h>
@ -296,7 +295,7 @@ public:
// Dash BIP44 coin type is '5'
nExtCoinType = 5;
vFixedSeeds = std::vector<SeedSpec6>(pnSeed6_main, pnSeed6_main + ARRAYLEN(pnSeed6_main));
vFixedSeeds = std::vector<SeedSpec6>(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<SeedSpec6>(pnSeed6_test, pnSeed6_test + ARRAYLEN(pnSeed6_test));
vFixedSeeds = std::vector<SeedSpec6>(std::begin(pnSeed6_test), std::end(pnSeed6_test));
vSeeds.clear();
// nodes with support for servicebits filtering should be at the top

View File

@ -5,7 +5,6 @@
#include <protocol.h>
#include <util/strencodings.h>
#include <util/system.h>
static std::atomic<bool> 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<std::string> allNetMessageTypesVec(allNetMessageTypes, allNetMessageTypes+ARRAYLEN(allNetMessageTypes));
const static std::vector<std::string> 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<std::string> netMessageTypesViolateBlocksOnlySet(netMessageTypesViolateBlocksOnly, netMessageTypesViolateBlocksOnly+ARRAYLEN(netMessageTypesViolateBlocksOnly));
const static std::set<std::string> netMessageTypesViolateBlocksOnlySet(std::begin(netMessageTypesViolateBlocksOnly), std::end(netMessageTypesViolateBlocksOnly));
CMessageHeader::CMessageHeader()
{

View File

@ -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<network_styles_count; ++x)
for (const auto& network_style : network_styles)
{
if (networkId == network_styles[x].networkId)
if (networkId == network_style.networkId)
{
std::string appName = network_styles[x].appName;
std::string titleAddText = network_styles[x].titleAddText;
std::string appName = network_style.appName;
std::string titleAddText = network_style.titleAddText;
if (networkId == QString(CBaseChainParams::DEVNET.c_str())) {
appName = strprintf(appName, gArgs.GetDevNetName());
@ -103,8 +102,8 @@ const NetworkStyle *NetworkStyle::instantiate(const QString &networkId)
return new NetworkStyle(
appName.c_str(),
network_styles[x].iconColorHueShift,
network_styles[x].iconColorSaturationReduction,
network_style.iconColorHueShift,
network_style.iconColorSaturationReduction,
titleAddText.c_str());
}
}

View File

@ -39,7 +39,6 @@
#include <sys/random.h>
#endif
#ifdef HAVE_SYSCTL_ARND
#include <util/strencodings.h> // for ARRAYLEN
#include <sys/sysctl.h>
#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;

View File

@ -19,7 +19,6 @@
#include <sync.h>
#include <txmempool.h>
#include <util/check.h>
#include <util/strencodings.h>
#include <validation.h>
#include <version.h>
@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -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; i<sizeof(vstrIn)/sizeof(vstrIn[0]); i++)
for (unsigned int i=0; i<std::size(vstrIn); i++)
{
std::string strEnc = EncodeBase32(vstrIn[i]);
BOOST_CHECK_EQUAL(strEnc, vstrOut[i]);

View File

@ -13,7 +13,7 @@ BOOST_AUTO_TEST_CASE(base64_testvectors)
{
static const std::string vstrIn[] = {"","f","fo","foo","foob","fooba","foobar"};
static const std::string vstrOut[] = {"","Zg==","Zm8=","Zm9v","Zm9vYg==","Zm9vYmE=","Zm9vYmFy"};
for (unsigned int i=0; i<sizeof(vstrIn)/sizeof(vstrIn[0]); i++)
for (unsigned int i=0; i<std::size(vstrIn); i++)
{
std::string strEnc = EncodeBase64(vstrIn[i]);
BOOST_CHECK_EQUAL(strEnc, vstrOut[i]);

View File

@ -107,14 +107,14 @@ BOOST_AUTO_TEST_CASE(siphash)
// Check test vectors from spec, one byte at a time
CSipHasher hasher2(0x0706050403020100ULL, 0x0F0E0D0C0B0A0908ULL);
for (uint8_t x=0; x<ARRAYLEN(siphash_4_2_testvec); ++x)
for (uint8_t x=0; x<std::size(siphash_4_2_testvec); ++x)
{
BOOST_CHECK_EQUAL(hasher2.Finalize(), siphash_4_2_testvec[x]);
hasher2.Write(&x, 1);
}
// Check test vectors from spec, eight bytes at a time
CSipHasher hasher3(0x0706050403020100ULL, 0x0F0E0D0C0B0A0908ULL);
for (uint8_t x=0; x<ARRAYLEN(siphash_4_2_testvec); x+=8)
for (uint8_t x=0; x<std::size(siphash_4_2_testvec); x+=8)
{
BOOST_CHECK_EQUAL(hasher3.Finalize(), siphash_4_2_testvec[x]);
hasher3.Write(uint64_t(x)|(uint64_t(x+1)<<8)|(uint64_t(x+2)<<16)|(uint64_t(x+3)<<24)|

View File

@ -260,8 +260,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
pblock->hashPrevBlock = pblock->GetHash();
};
for (unsigned int i = 0; i < sizeof(blockinfo)/sizeof(*blockinfo) - 1; ++i)
{
for ([[maybe_unused]] const auto& _ : blockinfo) {
createAndProcessEmptyBlock();
}

View File

@ -100,18 +100,22 @@ static ScriptErrorDesc script_errors[]={
static const char *FormatScriptError(ScriptError_t err)
{
for (unsigned int i=0; i<ARRAYLEN(script_errors); ++i)
if (script_errors[i].err == err)
return script_errors[i].name;
for (const auto& script_error : script_errors) {
if (script_error.err == err) {
return script_error.name;
}
}
BOOST_ERROR("Unknown scripterror enumeration value, update script_errors in script_tests.cpp.");
return "";
}
static ScriptError_t ParseScriptError(const std::string& name)
{
for (unsigned int i=0; i<ARRAYLEN(script_errors); ++i)
if (script_errors[i].name == name)
return script_errors[i].err;
for (const auto& script_error : script_errors) {
if (script_error.name == name) {
return script_error.err;
}
}
BOOST_ERROR("Unknown scripterror \"" << name << "\" in test description");
return SCRIPT_ERR_UNKNOWN_ERROR;
}

View File

@ -163,9 +163,9 @@ static void RunOperators(const int64_t& num1, const int64_t& num2)
BOOST_AUTO_TEST_CASE(creation)
{
for(size_t i = 0; i < sizeof(values) / sizeof(values[0]); ++i)
for(size_t i = 0; i < std::size(values); ++i)
{
for(size_t j = 0; j < sizeof(offsets) / sizeof(offsets[0]); ++j)
for(size_t j = 0; j < std::size(offsets); ++j)
{
RunCreate(values[i]);
RunCreate(values[i] + offsets[j]);
@ -176,9 +176,9 @@ BOOST_AUTO_TEST_CASE(creation)
BOOST_AUTO_TEST_CASE(operators)
{
for(size_t i = 0; i < sizeof(values) / sizeof(values[0]); ++i)
for(size_t i = 0; i < std::size(values); ++i)
{
for(size_t j = 0; j < sizeof(offsets) / sizeof(offsets[0]); ++j)
for(size_t j = 0; j < std::size(offsets); ++j)
{
RunOperators(values[i], values[i]);
RunOperators(values[i], -values[i]);

View File

@ -85,7 +85,7 @@ void static RandomScript(CScript &script) {
script = CScript();
int ops = (InsecureRandRange(10));
for (int i=0; i<ops; i++)
script << oplist[InsecureRandRange(sizeof(oplist)/sizeof(oplist[0]))];
script << oplist[InsecureRandRange(std::size(oplist))];
}
void static RandomTransaction(CMutableTransaction &tx, bool fSingle) {

View File

@ -18,8 +18,6 @@
#include <string>
#include <vector>
#define ARRAYLEN(array) (sizeof(array)/sizeof((array)[0]))
/** Used by SanitizeString() */
enum SafeChars
{