Skip to content

Commit 5c896fa

Browse files
committed
Clean up some loops with range-based for.
1 parent 727da48 commit 5c896fa

File tree

6 files changed

+70
-81
lines changed

6 files changed

+70
-81
lines changed

ASTNode.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ void ASTNodeList::removeLast()
1010

1111
void ASTNodeList::removeFirst()
1212
{
13-
list_t::iterator it = m_nodes.begin();
14-
m_nodes.erase(it);
13+
m_nodes.erase(m_nodes.cbegin());
1514
}
1615

1716

@@ -69,8 +68,7 @@ void ASTBlock::removeLast()
6968

7069
void ASTBlock::removeFirst()
7170
{
72-
list_t::iterator it = m_nodes.begin();
73-
m_nodes.erase(it);
71+
m_nodes.erase(m_nodes.begin());
7472
}
7573

7674
const char* ASTBlock::type_str() const

ASTree.cpp

Lines changed: 40 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -2241,7 +2241,7 @@ static void print_block(PycRef<ASTBlock> blk, PycModule* mod) {
22412241
print_src(pass, mod);
22422242
}
22432243

2244-
for (ASTBlock::list_t::const_iterator ln = lines.begin(); ln != lines.end();) {
2244+
for (auto ln = lines.cbegin(); ln != lines.cend();) {
22452245
if ((*ln).cast<ASTNode>().type() != ASTNode::NODE_NODELIST) {
22462246
start_line(cur_indent);
22472247
}
@@ -2283,22 +2283,22 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
22832283
print_src(call->func(), mod);
22842284
fputs("(", pyc_output);
22852285
bool first = true;
2286-
for (ASTCall::pparam_t::const_iterator p = call->pparams().begin(); p != call->pparams().end(); ++p) {
2286+
for (const auto& param : call->pparams()) {
22872287
if (!first)
22882288
fputs(", ", pyc_output);
2289-
print_src(*p, mod);
2289+
print_src(param, mod);
22902290
first = false;
22912291
}
2292-
for (ASTCall::kwparam_t::const_iterator p = call->kwparams().begin(); p != call->kwparams().end(); ++p) {
2292+
for (const auto& param : call->kwparams()) {
22932293
if (!first)
22942294
fputs(", ", pyc_output);
2295-
if (p->first.type() == ASTNode::NODE_NAME) {
2296-
fprintf(pyc_output, "%s = ", p->first.cast<ASTName>()->name()->value());
2295+
if (param.first.type() == ASTNode::NODE_NAME) {
2296+
fprintf(pyc_output, "%s = ", param.first.cast<ASTName>()->name()->value());
22972297
} else {
2298-
PycRef<PycString> str_name = p->first.cast<ASTObject>()->object().require_cast<PycString>();
2298+
PycRef<PycString> str_name = param.first.cast<ASTObject>()->object().require_cast<PycString>();
22992299
fprintf(pyc_output, "%s = ", str_name->value());
23002300
}
2301-
print_src(p->second, mod);
2301+
print_src(param.second, mod);
23022302
first = false;
23032303
}
23042304
if (call->hasVar()) {
@@ -2347,17 +2347,16 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
23472347
break;
23482348
case ASTNode::NODE_LIST:
23492349
{
2350-
ASTList::value_t values = node.cast<ASTList>()->values();
23512350
fputs("[", pyc_output);
23522351
bool first = true;
23532352
cur_indent++;
2354-
for (ASTList::value_t::const_iterator b = values.begin(); b != values.end(); ++b) {
2353+
for (const auto& val : node.cast<ASTList>()->values()) {
23552354
if (first)
23562355
fputs("\n", pyc_output);
23572356
else
23582357
fputs(",\n", pyc_output);
23592358
start_line(cur_indent);
2360-
print_src(*b, mod);
2359+
print_src(val, mod);
23612360
first = false;
23622361
}
23632362
cur_indent--;
@@ -2367,35 +2366,33 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
23672366
case ASTNode::NODE_COMPREHENSION:
23682367
{
23692368
PycRef<ASTComprehension> comp = node.cast<ASTComprehension>();
2370-
ASTComprehension::generator_t values = comp->generators();
23712369

23722370
fputs("[ ", pyc_output);
23732371
print_src(comp->result(), mod);
23742372

2375-
for (ASTComprehension::generator_t::const_iterator it = values.begin(); it != values.end(); ++it) {
2373+
for (const auto& gen : comp->generators()) {
23762374
fputs(" for ", pyc_output);
2377-
print_src((*it)->index(), mod);
2375+
print_src(gen->index(), mod);
23782376
fputs(" in ", pyc_output);
2379-
print_src((*it)->iter(), mod);
2377+
print_src(gen->iter(), mod);
23802378
}
23812379
fputs(" ]", pyc_output);
23822380
}
23832381
break;
23842382
case ASTNode::NODE_MAP:
23852383
{
2386-
ASTMap::map_t values = node.cast<ASTMap>()->values();
23872384
fputs("{", pyc_output);
23882385
bool first = true;
23892386
cur_indent++;
2390-
for (ASTMap::map_t::const_iterator b = values.begin(); b != values.end(); ++b) {
2387+
for (const auto& val : node.cast<ASTMap>()->values()) {
23912388
if (first)
23922389
fputs("\n", pyc_output);
23932390
else
23942391
fputs(",\n", pyc_output);
23952392
start_line(cur_indent);
2396-
print_src(b->first, mod);
2393+
print_src(val.first, mod);
23972394
fputs(": ", pyc_output);
2398-
print_src(b->second, mod);
2395+
print_src(val.second, mod);
23992396
first = false;
24002397
}
24012398
cur_indent--;
@@ -2408,12 +2405,11 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
24082405
case ASTNode::NODE_NODELIST:
24092406
{
24102407
cur_indent++;
2411-
ASTNodeList::list_t lines = node.cast<ASTNodeList>()->nodes();
2412-
for (ASTNodeList::list_t::const_iterator ln = lines.begin(); ln != lines.end(); ++ln) {
2413-
if ((*ln).cast<ASTNode>().type() != ASTNode::NODE_NODELIST) {
2408+
for (const auto& ln : node.cast<ASTNodeList>()->nodes()) {
2409+
if (ln.cast<ASTNode>().type() != ASTNode::NODE_NODELIST) {
24142410
start_line(cur_indent);
24152411
}
2416-
print_src(*ln, mod);
2412+
print_src(ln, mod);
24172413
end_line();
24182414
}
24192415
cur_indent--;
@@ -2517,10 +2513,10 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
25172513
PycRef<ASTRaise> raise = node.cast<ASTRaise>();
25182514
fputs("raise ", pyc_output);
25192515
bool first = true;
2520-
for (ASTRaise::param_t::const_iterator p = raise->params().begin(); p != raise->params().end(); ++p) {
2516+
for (const auto& param : raise->params()) {
25212517
if (!first)
25222518
fputs(", ", pyc_output);
2523-
print_src(*p, mod);
2519+
print_src(param, mod);
25242520
first = false;
25252521
}
25262522
}
@@ -2567,25 +2563,26 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
25672563
print_src(import->name(), mod);
25682564
fputs(" import ", pyc_output);
25692565

2570-
ASTImport::list_t::const_iterator ii = stores.begin();
25712566
if (stores.size() == 1) {
2572-
print_src((*ii)->src(), mod);
2567+
auto src = stores.front()->src();
2568+
auto dest = stores.front()->dest();
2569+
print_src(src, mod);
25732570

2574-
if ((*ii)->src().cast<ASTName>()->name()->value() != (*ii)->dest().cast<ASTName>()->name()->value()) {
2571+
if (src.cast<ASTName>()->name()->value() != dest.cast<ASTName>()->name()->value()) {
25752572
fputs(" as ", pyc_output);
2576-
print_src((*ii)->dest(), mod);
2573+
print_src(dest, mod);
25772574
}
25782575
} else {
25792576
bool first = true;
2580-
for (; ii != stores.end(); ++ii) {
2577+
for (const auto& st : stores) {
25812578
if (!first)
25822579
fputs(", ", pyc_output);
2583-
print_src((*ii)->src(), mod);
2580+
print_src(st->src(), mod);
25842581
first = false;
25852582

2586-
if ((*ii)->src().cast<ASTName>()->name()->value() != (*ii)->dest().cast<ASTName>()->name()->value()) {
2583+
if (st->src().cast<ASTName>()->name()->value() != st->dest().cast<ASTName>()->name()->value()) {
25872584
fputs(" as ", pyc_output);
2588-
print_src((*ii)->dest(), mod);
2585+
print_src(st->dest(), mod);
25892586
}
25902587
}
25912588
}
@@ -2602,7 +2599,7 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
26022599
PycRef<ASTNode> code = node.cast<ASTFunction>()->code();
26032600
PycRef<PycCode> code_src = code.cast<ASTObject>()->object().cast<PycCode>();
26042601
ASTFunction::defarg_t defargs = node.cast<ASTFunction>()->defargs();
2605-
ASTFunction::defarg_t::iterator da = defargs.begin();
2602+
auto da = defargs.cbegin();
26062603
for (int i=0; i<code_src->argCount(); i++) {
26072604
if (i > 0)
26082605
fputs(", ", pyc_output);
@@ -2647,7 +2644,7 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
26472644
}
26482645

26492646
ASTFunction::defarg_t defargs = src.cast<ASTFunction>()->defargs();
2650-
ASTFunction::defarg_t::iterator da = defargs.begin();
2647+
auto da = defargs.cbegin();
26512648
bool first = true;
26522649
for (int i=0; i<code_src->argCount(); i++) {
26532650
if (!first)
@@ -2699,10 +2696,10 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
26992696
if (bases->values().size() > 0) {
27002697
fputs("(", pyc_output);
27012698
bool first = true;
2702-
for (ASTTuple::value_t::const_iterator b = bases->values().begin(); b != bases->values().end(); ++b) {
2699+
for (const auto& val : bases->values()) {
27032700
if (!first)
27042701
fputs(", ", pyc_output);
2705-
print_src(*b, mod);
2702+
print_src(val, mod);
27062703
first = false;
27072704
}
27082705
fputs("):\n", pyc_output);
@@ -2728,11 +2725,10 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
27282725
if (fromlist.type() == PycObject::TYPE_TUPLE ||
27292726
fromlist.type() == PycObject::TYPE_SMALL_TUPLE) {
27302727
bool first = true;
2731-
PycTuple::value_t::const_iterator ii = fromlist.cast<PycTuple>()->values().begin();
2732-
for (; ii != fromlist.cast<PycTuple>()->values().end(); ++ii) {
2728+
for (const auto& val : fromlist.cast<PycTuple>()->values()) {
27332729
if (!first)
27342730
fputs(", ", pyc_output);
2735-
fprintf(pyc_output, "%s", ii->cast<PycString>()->value());
2731+
fprintf(pyc_output, "%s", val.cast<PycString>()->value());
27362732
first = false;
27372733
}
27382734
} else {
@@ -2783,10 +2779,10 @@ void print_src(PycRef<ASTNode> node, PycModule* mod)
27832779
if (tuple->requireParens())
27842780
fputc('(', pyc_output);
27852781
bool first = true;
2786-
for (ASTTuple::value_t::const_iterator b = values.begin(); b != values.end(); ++b) {
2782+
for (const auto& val : values) {
27872783
if (!first)
27882784
fputs(", ", pyc_output);
2789-
print_src(*b, mod);
2785+
print_src(val, mod);
27902786
first = false;
27912787
}
27922788
if (values.size() == 1)
@@ -2891,11 +2887,10 @@ void decompyle(PycRef<PycCode> code, PycModule* mod)
28912887
start_line(cur_indent + 1);
28922888
fputs("global ", pyc_output);
28932889
bool first = true;
2894-
PycCode::globals_t::iterator it;
2895-
for (it = globs.begin(); it != globs.end(); ++it) {
2890+
for (const auto& glob : globs) {
28962891
if (!first)
28972892
fputs(", ", pyc_output);
2898-
fprintf(pyc_output, "%s", (*it)->value());
2893+
fprintf(pyc_output, "%s", glob->value());
28992894
first = false;
29002895
}
29012896
fputs("\n", pyc_output);

bytecode.cpp

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -169,10 +169,10 @@ void print_const(PycRef<PycObject> obj, PycModule* mod)
169169
{
170170
fputs("(", pyc_output);
171171
PycTuple::value_t values = obj.cast<PycTuple>()->values();
172-
PycTuple::value_t::const_iterator it = values.begin();
173-
if (it != values.end()) {
172+
auto it = values.cbegin();
173+
if (it != values.cend()) {
174174
print_const(*it, mod);
175-
while (++it != values.end()) {
175+
while (++it != values.cend()) {
176176
fputs(", ", pyc_output);
177177
print_const(*it, mod);
178178
}
@@ -187,10 +187,10 @@ void print_const(PycRef<PycObject> obj, PycModule* mod)
187187
{
188188
fputs("[", pyc_output);
189189
PycList::value_t values = obj.cast<PycList>()->values();
190-
PycList::value_t::const_iterator it = values.begin();
191-
if (it != values.end()) {
190+
auto it = values.cbegin();
191+
if (it != values.cend()) {
192192
print_const(*it, mod);
193-
while (++it != values.end()) {
193+
while (++it != values.cend()) {
194194
fputs(", ", pyc_output);
195195
print_const(*it, mod);
196196
}
@@ -203,13 +203,13 @@ void print_const(PycRef<PycObject> obj, PycModule* mod)
203203
fputs("{", pyc_output);
204204
PycDict::key_t keys = obj.cast<PycDict>()->keys();
205205
PycDict::value_t values = obj.cast<PycDict>()->values();
206-
PycDict::key_t::const_iterator ki = keys.begin();
207-
PycDict::value_t::const_iterator vi = values.begin();
208-
if (ki != keys.end()) {
206+
auto ki = keys.cbegin();
207+
auto vi = values.cbegin();
208+
if (ki != keys.cend()) {
209209
print_const(*ki, mod);
210210
fputs(": ", pyc_output);
211211
print_const(*vi, mod);
212-
while (++ki != keys.end()) {
212+
while (++ki != keys.cend()) {
213213
++vi;
214214
fputs(", ", pyc_output);
215215
print_const(*ki, mod);
@@ -224,10 +224,10 @@ void print_const(PycRef<PycObject> obj, PycModule* mod)
224224
{
225225
fputs("{", pyc_output);
226226
PycSet::value_t values = obj.cast<PycSet>()->values();
227-
PycSet::value_t::const_iterator it = values.begin();
228-
if (it != values.end()) {
227+
auto it = values.cbegin();
228+
if (it != values.cend()) {
229229
print_const(*it, mod);
230-
while (++it != values.end()) {
230+
while (++it != values.cend()) {
231231
fputs(", ", pyc_output);
232232
print_const(*it, mod);
233233
}

pyc_module.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,10 @@ PycRef<PycString> PycModule::getIntern(int ref) const
189189
if (ref < 0)
190190
throw std::out_of_range("Intern index out of range");
191191

192-
std::list<PycRef<PycString> >::const_iterator it = m_interns.begin();
193-
while (ref-- && it != m_interns.end())
192+
auto it = m_interns.cbegin();
193+
while (ref-- && it != m_interns.cend())
194194
++it;
195-
if (it == m_interns.end())
195+
if (it == m_interns.cend())
196196
throw std::out_of_range("Intern index out of range");
197197
return *it;
198198
}
@@ -202,10 +202,10 @@ PycRef<PycObject> PycModule::getRef(int ref) const
202202
if (ref < 0)
203203
throw std::out_of_range("Ref index out of range");
204204

205-
std::list<PycRef<PycObject> >::const_iterator it = m_refs.begin();
206-
while (ref-- && it != m_refs.end())
205+
auto it = m_refs.cbegin();
206+
while (ref-- && it != m_refs.cend())
207207
++it;
208-
if (it == m_refs.end())
208+
if (it == m_refs.cend())
209209
throw std::out_of_range("Ref index out of range");
210210
return *it;
211211
}

pyc_numeric.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,14 @@ std::string PycLong::repr() const
6161

6262
// Realign to 32 bits, since Python uses only 15
6363
std::list<unsigned> bits;
64-
std::list<int>::const_iterator bit;
6564
int shift = 0, temp = 0;
66-
for (bit = m_value.begin(); bit != m_value.end(); ++bit) {
67-
temp |= unsigned(*bit & 0xFFFF) << shift;
65+
for (auto bit : m_value) {
66+
temp |= unsigned(bit & 0xFFFF) << shift;
6867
shift += 15;
6968
if (shift >= 32) {
7069
bits.push_back(temp);
7170
shift -= 32;
72-
temp = unsigned(*bit & 0xFFFF) >> (15 - shift);
71+
temp = unsigned(bit & 0xFFFF) >> (15 - shift);
7372
}
7473
}
7574
if (temp)

pycdas.cpp

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,18 +151,16 @@ void output_object(PycRef<PycObject> obj, PycModule* mod, int indent)
151151
case PycObject::TYPE_SMALL_TUPLE:
152152
{
153153
iputs(indent, "(\n");
154-
PycTuple::value_t values = obj.cast<PycTuple>()->values();
155-
for (PycTuple::value_t::const_iterator i = values.begin(); i != values.end(); i++)
156-
output_object(*i, mod, indent + 1);
154+
for (const auto& val : obj.cast<PycTuple>()->values())
155+
output_object(val, mod, indent + 1);
157156
iputs(indent, ")\n");
158157
}
159158
break;
160159
case PycObject::TYPE_LIST:
161160
{
162161
iputs(indent, "[\n");
163-
PycList::value_t values = obj.cast<PycList>()->values();
164-
for (PycList::value_t::const_iterator i = values.begin(); i != values.end(); i++)
165-
output_object(*i, mod, indent + 1);
162+
for (const auto& val : obj.cast<PycList>()->values())
163+
output_object(val, mod, indent + 1);
166164
iputs(indent, "]\n");
167165
}
168166
break;
@@ -184,9 +182,8 @@ void output_object(PycRef<PycObject> obj, PycModule* mod, int indent)
184182
case PycObject::TYPE_SET:
185183
{
186184
iputs(indent, "{\n");
187-
PycSet::value_t values = obj.cast<PycSet>()->values();
188-
for (PycSet::value_t::const_iterator i = values.begin(); i != values.end(); i++)
189-
output_object(*i, mod, indent + 1);
185+
for (const auto& val : obj.cast<PycSet>()->values())
186+
output_object(val, mod, indent + 1);
190187
iputs(indent, "}\n");
191188
}
192189
break;

0 commit comments

Comments
 (0)