refactor: replace sizeof(a)/sizeof(a[0]) by std::size (C++17)

Removes the macro ARRAYLEN and also substitutes all other uses of the same
"sizeof(a)/sizeof(a[0])" pattern by std::size, available since C++17.
This commit is contained in:
Sebastian Falbesoner 2020-11-20 00:14:32 +01:00
parent 365539c846
commit e829c9afbf
9 changed files with 12 additions and 15 deletions

View File

@ -52,7 +52,7 @@ static const int8_t mapBase58[256] = {
int size = strlen(psz) * 733 /1000 + 1; // log(58) / log(256), rounded up. int size = strlen(psz) * 733 /1000 + 1; // log(58) / log(256), rounded up.
std::vector<unsigned char> b256(size); std::vector<unsigned char> b256(size);
// Process the characters. // 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)) { while (*psz && !IsSpace(*psz)) {
// Decode base58 character // Decode base58 character
int carry = mapBase58[(uint8_t)*psz]; int carry = mapBase58[(uint8_t)*psz];

View File

@ -38,7 +38,6 @@
#include <sys/random.h> #include <sys/random.h>
#endif #endif
#ifdef HAVE_SYSCTL_ARND #ifdef HAVE_SYSCTL_ARND
#include <util/strencodings.h> // for ARRAYLEN
#include <sys/sysctl.h> #include <sys/sysctl.h>
#endif #endif
@ -333,7 +332,7 @@ void GetOSRand(unsigned char *ent32)
int have = 0; int have = 0;
do { do {
size_t len = NUM_OS_RANDOM_BYTES - have; 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(); RandFailure();
} }
have += len; have += len;

View File

@ -17,7 +17,7 @@ BOOST_AUTO_TEST_CASE(base32_testvectors)
static const std::string vstrIn[] = {"","f","fo","foo","foob","fooba","foobar"}; 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 vstrOut[] = {"","my======","mzxq====","mzxw6===","mzxw6yq=","mzxw6ytb","mzxw6ytboi======"};
static const std::string vstrOutNoPadding[] = {"","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]); std::string strEnc = EncodeBase32(vstrIn[i]);
BOOST_CHECK_EQUAL(strEnc, vstrOut[i]); BOOST_CHECK_EQUAL(strEnc, vstrOut[i]);

View File

@ -16,7 +16,7 @@ BOOST_AUTO_TEST_CASE(base64_testvectors)
{ {
static const std::string vstrIn[] = {"","f","fo","foo","foob","fooba","foobar"}; static const std::string vstrIn[] = {"","f","fo","foo","foob","fooba","foobar"};
static const std::string vstrOut[] = {"","Zg==","Zm8=","Zm9v","Zm9vYg==","Zm9vYmE=","Zm9vYmFy"}; 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]); std::string strEnc = EncodeBase64(vstrIn[i]);
BOOST_CHECK_EQUAL(strEnc, vstrOut[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 // Check test vectors from spec, one byte at a time
CSipHasher hasher2(0x0706050403020100ULL, 0x0F0E0D0C0B0A0908ULL); 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]); BOOST_CHECK_EQUAL(hasher2.Finalize(), siphash_4_2_testvec[x]);
hasher2.Write(&x, 1); hasher2.Write(&x, 1);
} }
// Check test vectors from spec, eight bytes at a time // Check test vectors from spec, eight bytes at a time
CSipHasher hasher3(0x0706050403020100ULL, 0x0F0E0D0C0B0A0908ULL); 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]); 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)| hasher3.Write(uint64_t(x)|(uint64_t(x+1)<<8)|(uint64_t(x+2)<<16)|(uint64_t(x+3)<<24)|

View File

@ -216,7 +216,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
// We can't make transactions until we have inputs // We can't make transactions until we have inputs
// Therefore, load 110 blocks :) // Therefore, load 110 blocks :)
static_assert(sizeof(blockinfo) / sizeof(*blockinfo) == 110, "Should have 110 blocks to import"); static_assert(std::size(blockinfo) == 110, "Should have 110 blocks to import");
int baseheight = 0; int baseheight = 0;
std::vector<CTransactionRef> txFirst; std::vector<CTransactionRef> txFirst;
for (const auto& bi : blockinfo) { for (const auto& bi : blockinfo) {

View File

@ -164,9 +164,9 @@ static void RunOperators(const int64_t& num1, const int64_t& num2)
BOOST_AUTO_TEST_CASE(creation) 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]);
RunCreate(values[i] + offsets[j]); RunCreate(values[i] + offsets[j]);
@ -177,9 +177,9 @@ BOOST_AUTO_TEST_CASE(creation)
BOOST_AUTO_TEST_CASE(operators) 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]);
RunOperators(values[i], -values[i]); RunOperators(values[i], -values[i]);

View File

@ -88,7 +88,7 @@ void static RandomScript(CScript &script) {
script = CScript(); script = CScript();
int ops = (InsecureRandRange(10)); int ops = (InsecureRandRange(10));
for (int i=0; i<ops; i++) 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) { void static RandomTransaction(CMutableTransaction &tx, bool fSingle) {

View File

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