Skip to content

Commit e829c9a

Browse files
committedJan 31, 2021
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.
1 parent 365539c commit e829c9a

9 files changed

+12
-15
lines changed
 

‎src/base58.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ static const int8_t mapBase58[256] = {
5252
int size = strlen(psz) * 733 /1000 + 1; // log(58) / log(256), rounded up.
5353
std::vector<unsigned char> b256(size);
5454
// Process the characters.
55-
static_assert(sizeof(mapBase58)/sizeof(mapBase58[0]) == 256, "mapBase58.size() should be 256"); // guarantee not out of range
55+
static_assert(std::size(mapBase58) == 256, "mapBase58.size() should be 256"); // guarantee not out of range
5656
while (*psz && !IsSpace(*psz)) {
5757
// Decode base58 character
5858
int carry = mapBase58[(uint8_t)*psz];

‎src/random.cpp

+1-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
#include <sys/random.h>
3939
#endif
4040
#ifdef HAVE_SYSCTL_ARND
41-
#include <util/strencodings.h> // for ARRAYLEN
4241
#include <sys/sysctl.h>
4342
#endif
4443

@@ -333,7 +332,7 @@ void GetOSRand(unsigned char *ent32)
333332
int have = 0;
334333
do {
335334
size_t len = NUM_OS_RANDOM_BYTES - have;
336-
if (sysctl(name, ARRAYLEN(name), ent32 + have, &len, nullptr, 0) != 0) {
335+
if (sysctl(name, std::size(name), ent32 + have, &len, nullptr, 0) != 0) {
337336
RandFailure();
338337
}
339338
have += len;

‎src/test/base32_tests.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ BOOST_AUTO_TEST_CASE(base32_testvectors)
1717
static const std::string vstrIn[] = {"","f","fo","foo","foob","fooba","foobar"};
1818
static const std::string vstrOut[] = {"","my======","mzxq====","mzxw6===","mzxw6yq=","mzxw6ytb","mzxw6ytboi======"};
1919
static const std::string vstrOutNoPadding[] = {"","my","mzxq","mzxw6","mzxw6yq","mzxw6ytb","mzxw6ytboi"};
20-
for (unsigned int i=0; i<sizeof(vstrIn)/sizeof(vstrIn[0]); i++)
20+
for (unsigned int i=0; i<std::size(vstrIn); i++)
2121
{
2222
std::string strEnc = EncodeBase32(vstrIn[i]);
2323
BOOST_CHECK_EQUAL(strEnc, vstrOut[i]);

‎src/test/base64_tests.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ BOOST_AUTO_TEST_CASE(base64_testvectors)
1616
{
1717
static const std::string vstrIn[] = {"","f","fo","foo","foob","fooba","foobar"};
1818
static const std::string vstrOut[] = {"","Zg==","Zm8=","Zm9v","Zm9vYg==","Zm9vYmE=","Zm9vYmFy"};
19-
for (unsigned int i=0; i<sizeof(vstrIn)/sizeof(vstrIn[0]); i++)
19+
for (unsigned int i=0; i<std::size(vstrIn); i++)
2020
{
2121
std::string strEnc = EncodeBase64(vstrIn[i]);
2222
BOOST_CHECK_EQUAL(strEnc, vstrOut[i]);

‎src/test/hash_tests.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -107,14 +107,14 @@ BOOST_AUTO_TEST_CASE(siphash)
107107

108108
// Check test vectors from spec, one byte at a time
109109
CSipHasher hasher2(0x0706050403020100ULL, 0x0F0E0D0C0B0A0908ULL);
110-
for (uint8_t x=0; x<ARRAYLEN(siphash_4_2_testvec); ++x)
110+
for (uint8_t x=0; x<std::size(siphash_4_2_testvec); ++x)
111111
{
112112
BOOST_CHECK_EQUAL(hasher2.Finalize(), siphash_4_2_testvec[x]);
113113
hasher2.Write(&x, 1);
114114
}
115115
// Check test vectors from spec, eight bytes at a time
116116
CSipHasher hasher3(0x0706050403020100ULL, 0x0F0E0D0C0B0A0908ULL);
117-
for (uint8_t x=0; x<ARRAYLEN(siphash_4_2_testvec); x+=8)
117+
for (uint8_t x=0; x<std::size(siphash_4_2_testvec); x+=8)
118118
{
119119
BOOST_CHECK_EQUAL(hasher3.Finalize(), siphash_4_2_testvec[x]);
120120
hasher3.Write(uint64_t(x)|(uint64_t(x+1)<<8)|(uint64_t(x+2)<<16)|(uint64_t(x+3)<<24)|

‎src/test/miner_tests.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
216216

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

‎src/test/scriptnum_tests.cpp

+4-4
Original file line numberDiff line numberDiff line change
@@ -164,9 +164,9 @@ static void RunOperators(const int64_t& num1, const int64_t& num2)
164164

165165
BOOST_AUTO_TEST_CASE(creation)
166166
{
167-
for(size_t i = 0; i < sizeof(values) / sizeof(values[0]); ++i)
167+
for(size_t i = 0; i < std::size(values); ++i)
168168
{
169-
for(size_t j = 0; j < sizeof(offsets) / sizeof(offsets[0]); ++j)
169+
for(size_t j = 0; j < std::size(offsets); ++j)
170170
{
171171
RunCreate(values[i]);
172172
RunCreate(values[i] + offsets[j]);
@@ -177,9 +177,9 @@ BOOST_AUTO_TEST_CASE(creation)
177177

178178
BOOST_AUTO_TEST_CASE(operators)
179179
{
180-
for(size_t i = 0; i < sizeof(values) / sizeof(values[0]); ++i)
180+
for(size_t i = 0; i < std::size(values); ++i)
181181
{
182-
for(size_t j = 0; j < sizeof(offsets) / sizeof(offsets[0]); ++j)
182+
for(size_t j = 0; j < std::size(offsets); ++j)
183183
{
184184
RunOperators(values[i], values[i]);
185185
RunOperators(values[i], -values[i]);

‎src/test/sighash_tests.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ void static RandomScript(CScript &script) {
8888
script = CScript();
8989
int ops = (InsecureRandRange(10));
9090
for (int i=0; i<ops; i++)
91-
script << oplist[InsecureRandRange(sizeof(oplist)/sizeof(oplist[0]))];
91+
script << oplist[InsecureRandRange(std::size(oplist))];
9292
}
9393

9494
void static RandomTransaction(CMutableTransaction &tx, bool fSingle) {

‎src/util/strencodings.h

-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@
1717
#include <string>
1818
#include <vector>
1919

20-
#define ARRAYLEN(array) (sizeof(array)/sizeof((array)[0]))
21-
2220
/** Used by SanitizeString() */
2321
enum SafeChars
2422
{

0 commit comments

Comments
 (0)
Please sign in to comment.