refactor: iterate arrays via C++11 range-based for loops if idx is not needed

This commit is contained in:
Sebastian Falbesoner 2020-11-20 01:01:35 +01:00
parent 4c55f92c76
commit 63d4ee1968
4 changed files with 25 additions and 30 deletions

View File

@ -22,7 +22,6 @@ static const struct {
{"signet", QAPP_APP_NAME_SIGNET, 35, 15}, {"signet", QAPP_APP_NAME_SIGNET, 35, 15},
{"regtest", QAPP_APP_NAME_REGTEST, 160, 30}, {"regtest", QAPP_APP_NAME_REGTEST, 160, 30},
}; };
static const unsigned network_styles_count = sizeof(network_styles)/sizeof(*network_styles);
// titleAddText needs to be const char* for tr() // titleAddText needs to be const char* for tr()
NetworkStyle::NetworkStyle(const QString &_appName, const int iconColorHueShift, const int iconColorSaturationReduction, const char *_titleAddText): 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,
const NetworkStyle* NetworkStyle::instantiate(const std::string& networkId) const NetworkStyle* NetworkStyle::instantiate(const std::string& networkId)
{ {
std::string titleAddText = networkId == CBaseChainParams::MAIN ? "" : strprintf("[%s]", networkId); std::string titleAddText = networkId == CBaseChainParams::MAIN ? "" : strprintf("[%s]", networkId);
for (unsigned x=0; x<network_styles_count; ++x) for (const auto& network_style : network_styles) {
{ if (networkId == network_style.networkId) {
if (networkId == network_styles[x].networkId)
{
return new NetworkStyle( return new NetworkStyle(
network_styles[x].appName, network_style.appName,
network_styles[x].iconColorHueShift, network_style.iconColorHueShift,
network_styles[x].iconColorSaturationReduction, network_style.iconColorSaturationReduction,
titleAddText.c_str()); titleAddText.c_str());
} }
} }

View File

@ -23,7 +23,6 @@ static const struct {
/* Other: linux, unix, ... */ /* Other: linux, unix, ... */
{"other", true, true, false} {"other", true, true, false}
}; };
static const unsigned platform_styles_count = sizeof(platform_styles)/sizeof(*platform_styles);
namespace { namespace {
/* Local functions for colorizing single-color images */ /* Local functions for colorizing single-color images */
@ -121,15 +120,13 @@ QIcon PlatformStyle::TextColorIcon(const QIcon& icon) const
const PlatformStyle *PlatformStyle::instantiate(const QString &platformId) const PlatformStyle *PlatformStyle::instantiate(const QString &platformId)
{ {
for (unsigned x=0; x<platform_styles_count; ++x) for (const auto& platform_style : platform_styles) {
{ if (platformId == platform_style.platformId) {
if (platformId == platform_styles[x].platformId)
{
return new PlatformStyle( return new PlatformStyle(
platform_styles[x].platformId, platform_style.platformId,
platform_styles[x].imagesOnButtons, platform_style.imagesOnButtons,
platform_styles[x].colorizeIcons, platform_style.colorizeIcons,
platform_styles[x].useExtraSpacing); platform_style.useExtraSpacing);
} }
} }
return nullptr; return nullptr;

View File

@ -19,7 +19,6 @@
#include <txmempool.h> #include <txmempool.h>
#include <util/check.h> #include <util/check.h>
#include <util/ref.h> #include <util/ref.h>
#include <util/strencodings.h>
#include <validation.h> #include <validation.h>
#include <version.h> #include <version.h>
@ -117,9 +116,10 @@ static RetFormat ParseDataFormat(std::string& param, const std::string& strReq)
param = strReq.substr(0, pos); param = strReq.substr(0, pos);
const std::string suff(strReq, pos + 1); const std::string suff(strReq, pos + 1);
for (unsigned int i = 0; i < ARRAYLEN(rf_names); i++) for (const auto& rf_name : rf_names) {
if (suff == rf_names[i].name) if (suff == rf_name.name)
return rf_names[i].rf; return rf_name.rf;
}
/* If no suffix is found, return original string. */ /* If no suffix is found, return original string. */
param = strReq; param = strReq;
@ -129,12 +129,13 @@ static RetFormat ParseDataFormat(std::string& param, const std::string& strReq)
static std::string AvailableDataFormatsString() static std::string AvailableDataFormatsString()
{ {
std::string formats; std::string formats;
for (unsigned int i = 0; i < ARRAYLEN(rf_names); i++) for (const auto& rf_name : rf_names) {
if (strlen(rf_names[i].name) > 0) { if (strlen(rf_name.name) > 0) {
formats.append("."); formats.append(".");
formats.append(rf_names[i].name); formats.append(rf_name.name);
formats.append(", "); formats.append(", ");
} }
}
if (formats.length() > 0) if (formats.length() > 0)
return formats.substr(0, formats.length() - 2); return formats.substr(0, formats.length() - 2);
@ -695,6 +696,7 @@ void InterruptREST()
void StopREST() void StopREST()
{ {
for (unsigned int i = 0; i < ARRAYLEN(uri_prefixes); i++) for (const auto& up : uri_prefixes) {
UnregisterHTTPHandler(uri_prefixes[i].prefix, false); UnregisterHTTPHandler(up.prefix, false);
}
} }

View File

@ -219,8 +219,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
static_assert(sizeof(blockinfo) / sizeof(*blockinfo) == 110, "Should have 110 blocks to import"); static_assert(sizeof(blockinfo) / sizeof(*blockinfo) == 110, "Should have 110 blocks to import");
int baseheight = 0; int baseheight = 0;
std::vector<CTransactionRef> txFirst; std::vector<CTransactionRef> txFirst;
for (unsigned int i = 0; i < sizeof(blockinfo)/sizeof(*blockinfo); ++i) for (const auto& bi : blockinfo) {
{
CBlock *pblock = &pblocktemplate->block; // pointer for convenience CBlock *pblock = &pblocktemplate->block; // pointer for convenience
{ {
LOCK(cs_main); LOCK(cs_main);
@ -229,7 +228,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
CMutableTransaction txCoinbase(*pblock->vtx[0]); CMutableTransaction txCoinbase(*pblock->vtx[0]);
txCoinbase.nVersion = 1; txCoinbase.nVersion = 1;
txCoinbase.vin[0].scriptSig = CScript(); txCoinbase.vin[0].scriptSig = CScript();
txCoinbase.vin[0].scriptSig.push_back(blockinfo[i].extranonce); txCoinbase.vin[0].scriptSig.push_back(bi.extranonce);
txCoinbase.vin[0].scriptSig.push_back(::ChainActive().Height()); txCoinbase.vin[0].scriptSig.push_back(::ChainActive().Height());
txCoinbase.vout.resize(1); // Ignore the (optional) segwit commitment added by CreateNewBlock (as the hardcoded nonces don't account for this) txCoinbase.vout.resize(1); // Ignore the (optional) segwit commitment added by CreateNewBlock (as the hardcoded nonces don't account for this)
txCoinbase.vout[0].scriptPubKey = CScript(); txCoinbase.vout[0].scriptPubKey = CScript();
@ -239,7 +238,7 @@ BOOST_AUTO_TEST_CASE(CreateNewBlock_validity)
if (txFirst.size() < 4) if (txFirst.size() < 4)
txFirst.push_back(pblock->vtx[0]); txFirst.push_back(pblock->vtx[0]);
pblock->hashMerkleRoot = BlockMerkleRoot(*pblock); pblock->hashMerkleRoot = BlockMerkleRoot(*pblock);
pblock->nNonce = blockinfo[i].nonce; pblock->nNonce = bi.nonce;
} }
std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(*pblock); std::shared_ptr<const CBlock> shared_pblock = std::make_shared<const CBlock>(*pblock);
BOOST_CHECK(Assert(m_node.chainman)->ProcessNewBlock(chainparams, shared_pblock, true, nullptr)); BOOST_CHECK(Assert(m_node.chainman)->ProcessNewBlock(chainparams, shared_pblock, true, nullptr));