Skip to content

Commit 63d4ee1

Browse files
committed
refactor: iterate arrays via C++11 range-based for loops if idx is not needed
1 parent 4c55f92 commit 63d4ee1

File tree

4 files changed

+25
-30
lines changed

4 files changed

+25
-30
lines changed

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/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/miner_tests.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
219219
static_assert(sizeof(blockinfo) / sizeof(*blockinfo) == 110, "Should have 110 blocks to import");
220220
int baseheight = 0;
221221
std::vector<CTransactionRef> txFirst;
222-
for (unsigned int i = 0; i < sizeof(blockinfo)/sizeof(*blockinfo); ++i)
223-
{
222+
for (const auto& bi : blockinfo) {
224223
CBlock *pblock = &pblocktemplate->block; // pointer for convenience
225224
{
226225
LOCK(cs_main);
@@ -229,7 +228,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
229228
CMutableTransaction txCoinbase(*pblock->vtx[0]);
230229
txCoinbase.nVersion = 1;
231230
txCoinbase.vin[0].scriptSig = CScript();
232-
txCoinbase.vin[0].scriptSig.push_back(blockinfo[i].extranonce);
231+
txCoinbase.vin[0].scriptSig.push_back(bi.extranonce);
233232
txCoinbase.vin[0].scriptSig.push_back(::ChainActive().Height());
234233
txCoinbase.vout.resize(1); // Ignore the (optional) segwit commitment added by CreateNewBlock (as the hardcoded nonces don't account for this)
235234
txCoinbase.vout[0].scriptPubKey = CScript();
@@ -239,7 +238,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
239238
if (txFirst.size() < 4)
240239
txFirst.push_back(pblock->vtx[0]);
241240
pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);
242-
pblock->nNonce = blockinfo[i].nonce;
241+
pblock->nNonce = bi.nonce;
243242
}
244243
std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(*pblock);
245244
BOOST_CHECK(Assert(m_node.chainman)->ProcessNewBlock(chainparams, shared_pblock, true, nullptr));

0 commit comments

Comments
 (0)