Skip to content

Commit cd66d8b

Browse files
author
MarcoFalke
committed
Merge #20429: refactor: replace (sizeof(a)/sizeof(a[0])) with C++17 std::size
e829c9a refactor: replace sizeof(a)/sizeof(a[0]) by std::size (C++17) (Sebastian Falbesoner) 365539c refactor: init vectors via std::{begin,end} to avoid pointer arithmetic (Sebastian Falbesoner) 63d4ee1 refactor: iterate arrays via C++11 range-based for loops if idx is not needed (Sebastian Falbesoner) Pull request description: This refactoring PR picks up the idea of #19626 and replaces all occurences of `sizeof(x)/sizeof(x[0])` (or `sizeof(x)/sizeof(*x)`, respectively) with the now-available C++17 [`std::size`](https://en.cppreference.com/w/cpp/iterator/size) (as [suggested by sipa](bitcoin/bitcoin#19626 (comment))), making the macro `ARRAYLEN` obsolete. As preparation for this, two other changes are done to eliminate `sizeof(x)/sizeof(x[0])` usage: * all places where arrays are iterated via an index are changed to use C++11 range-based for loops If the index' only purpose is to access the array element (as [suggested by MarcoFalke](bitcoin/bitcoin#19626 (comment))). * `std::vector` initializations are done via `std::begin` and `std::end` rather than using pointer arithmetic to calculate the end (also [suggested by MarcoFalke](bitcoin/bitcoin#20429 (comment))). ACKs for top commit: practicalswift: cr ACK e829c9a: patch looks correct fanquake: ACK e829c9a MarcoFalke: review ACK e829c9a 🌩 Tree-SHA512: b01d32c04b9e04d562b7717cae00a651ec9a718645047a90761be6959e0cc2adbd67494e058fe894641076711bb09c3b47a047d0275c736f0b2218e1ce0d193d
2 parents 372dd8d + e829c9a commit cd66d8b

15 files changed

+41
-51
lines changed

src/base58.cpp

Lines changed: 1 addition & 1 deletion
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/bench/data.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace benchmark {
88
namespace data {
99

1010
#include <bench/data/block413567.raw.h>
11-
const std::vector<uint8_t> block413567{block413567_raw, block413567_raw + sizeof(block413567_raw) / sizeof(block413567_raw[0])};
11+
const std::vector<uint8_t> block413567{std::begin(block413567_raw), std::end(block413567_raw)};
1212

1313
} // namespace data
1414
} // namespace benchmark

src/chainparams.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include <consensus/merkle.h>
1010
#include <hash.h> // for signet block challenge hash
1111
#include <util/system.h>
12-
#include <util/strencodings.h>
1312
#include <versionbitsinfo.h>
1413

1514
#include <assert.h>
@@ -135,7 +134,7 @@ class CMainParams : public CChainParams {
135134

136135
bech32_hrp = "bc";
137136

138-
vFixedSeeds = std::vector<SeedSpec6>(pnSeed6_main, pnSeed6_main + ARRAYLEN(pnSeed6_main));
137+
vFixedSeeds = std::vector<SeedSpec6>(std::begin(pnSeed6_main), std::end(pnSeed6_main));
139138

140139
fDefaultConsistencyChecks = false;
141140
fRequireStandard = true;
@@ -240,7 +239,7 @@ class CTestNetParams : public CChainParams {
240239

241240
bech32_hrp = "tb";
242241

243-
vFixedSeeds = std::vector<SeedSpec6>(pnSeed6_test, pnSeed6_test + ARRAYLEN(pnSeed6_test));
242+
vFixedSeeds = std::vector<SeedSpec6>(std::begin(pnSeed6_test), std::end(pnSeed6_test));
244243

245244
fDefaultConsistencyChecks = false;
246245
fRequireStandard = false;

src/protocol.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
#include <protocol.h>
77

8-
#include <util/strencodings.h>
98
#include <util/system.h>
109

1110
static std::atomic<bool> g_initial_block_download_completed(false);
@@ -86,7 +85,7 @@ const static std::string allNetMessageTypes[] = {
8685
NetMsgType::CFCHECKPT,
8786
NetMsgType::WTXIDRELAY,
8887
};
89-
const static std::vector<std::string> allNetMessageTypesVec(allNetMessageTypes, allNetMessageTypes+ARRAYLEN(allNetMessageTypes));
88+
const static std::vector<std::string> allNetMessageTypesVec(std::begin(allNetMessageTypes), std::end(allNetMessageTypes));
9089

9190
CMessageHeader::CMessageHeader()
9291
{

src/qt/networkstyle.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ static const struct {
2222
{"signet", QAPP_APP_NAME_SIGNET, 35, 15},
2323
{"regtest", QAPP_APP_NAME_REGTEST, 160, 30},
2424
};
25-
static const unsigned network_styles_count = sizeof(network_styles)/sizeof(*network_styles);
2625

2726
// titleAddText needs to be const char* for tr()
2827
NetworkStyle::NetworkStyle(const QString &_appName, const int iconColorHueShift, const int iconColorSaturationReduction, const char *_titleAddText):
@@ -81,14 +80,12 @@ NetworkStyle::NetworkStyle(const QString &_appName, const int iconColorHueShift,
8180
const NetworkStyle* NetworkStyle::instantiate(const std::string& networkId)
8281
{
8382
std::string titleAddText = networkId == CBaseChainParams::MAIN ? "" : strprintf("[%s]", networkId);
84-
for (unsigned x=0; x<network_styles_count; ++x)
85-
{
86-
if (networkId == network_styles[x].networkId)
87-
{
83+
for (const auto& network_style : network_styles) {
84+
if (networkId == network_style.networkId) {
8885
return new NetworkStyle(
89-
network_styles[x].appName,
90-
network_styles[x].iconColorHueShift,
91-
network_styles[x].iconColorSaturationReduction,
86+
network_style.appName,
87+
network_style.iconColorHueShift,
88+
network_style.iconColorSaturationReduction,
9289
titleAddText.c_str());
9390
}
9491
}

src/qt/platformstyle.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ static const struct {
2323
/* Other: linux, unix, ... */
2424
{"other", true, true, false}
2525
};
26-
static const unsigned platform_styles_count = sizeof(platform_styles)/sizeof(*platform_styles);
2726

2827
namespace {
2928
/* Local functions for colorizing single-color images */
@@ -121,15 +120,13 @@ QIcon PlatformStyle::TextColorIcon(const QIcon& icon) const
121120

122121
const PlatformStyle *PlatformStyle::instantiate(const QString &platformId)
123122
{
124-
for (unsigned x=0; x<platform_styles_count; ++x)
125-
{
126-
if (platformId == platform_styles[x].platformId)
127-
{
123+
for (const auto& platform_style : platform_styles) {
124+
if (platformId == platform_style.platformId) {
128125
return new PlatformStyle(
129-
platform_styles[x].platformId,
130-
platform_styles[x].imagesOnButtons,
131-
platform_styles[x].colorizeIcons,
132-
platform_styles[x].useExtraSpacing);
126+
platform_style.platformId,
127+
platform_style.imagesOnButtons,
128+
platform_style.colorizeIcons,
129+
platform_style.useExtraSpacing);
133130
}
134131
}
135132
return nullptr;

src/random.cpp

Lines changed: 1 addition & 2 deletions
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/rest.cpp

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
#include <txmempool.h>
2020
#include <util/check.h>
2121
#include <util/ref.h>
22-
#include <util/strencodings.h>
2322
#include <validation.h>
2423
#include <version.h>
2524

@@ -117,9 +116,10 @@ static RetFormat ParseDataFormat(std::string& param, const std::string& strReq)
117116
param = strReq.substr(0, pos);
118117
const std::string suff(strReq, pos + 1);
119118

120-
for (unsigned int i = 0; i < ARRAYLEN(rf_names); i++)
121-
if (suff == rf_names[i].name)
122-
return rf_names[i].rf;
119+
for (const auto& rf_name : rf_names) {
120+
if (suff == rf_name.name)
121+
return rf_name.rf;
122+
}
123123

124124
/* If no suffix is found, return original string. */
125125
param = strReq;
@@ -129,12 +129,13 @@ static RetFormat ParseDataFormat(std::string& param, const std::string& strReq)
129129
static std::string AvailableDataFormatsString()
130130
{
131131
std::string formats;
132-
for (unsigned int i = 0; i < ARRAYLEN(rf_names); i++)
133-
if (strlen(rf_names[i].name) > 0) {
132+
for (const auto& rf_name : rf_names) {
133+
if (strlen(rf_name.name) > 0) {
134134
formats.append(".");
135-
formats.append(rf_names[i].name);
135+
formats.append(rf_name.name);
136136
formats.append(", ");
137137
}
138+
}
138139

139140
if (formats.length() > 0)
140141
return formats.substr(0, formats.length() - 2);
@@ -695,6 +696,7 @@ void InterruptREST()
695696

696697
void StopREST()
697698
{
698-
for (unsigned int i = 0; i < ARRAYLEN(uri_prefixes); i++)
699-
UnregisterHTTPHandler(uri_prefixes[i].prefix, false);
699+
for (const auto& up : uri_prefixes) {
700+
UnregisterHTTPHandler(up.prefix, false);
701+
}
700702
}

src/test/base32_tests.cpp

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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]);

0 commit comments

Comments
 (0)