From 5c896fa7435918281c6dc0aa8ac2ac19aacce097 Mon Sep 17 00:00:00 2001 From: Michael Hansen Date: Fri, 4 Oct 2019 15:56:24 -0700 Subject: [PATCH] Clean up some loops with range-based for. --- ASTNode.cpp | 6 ++-- ASTree.cpp | 85 +++++++++++++++++++++++-------------------------- bytecode.cpp | 26 +++++++-------- pyc_module.cpp | 12 +++---- pyc_numeric.cpp | 7 ++-- pycdas.cpp | 15 ++++----- 6 files changed, 70 insertions(+), 81 deletions(-) diff --git a/ASTNode.cpp b/ASTNode.cpp index efb9911..dbd39d8 100644 --- a/ASTNode.cpp +++ b/ASTNode.cpp @@ -10,8 +10,7 @@ void ASTNodeList::removeLast() void ASTNodeList::removeFirst() { - list_t::iterator it = m_nodes.begin(); - m_nodes.erase(it); + m_nodes.erase(m_nodes.cbegin()); } @@ -69,8 +68,7 @@ void ASTBlock::removeLast() void ASTBlock::removeFirst() { - list_t::iterator it = m_nodes.begin(); - m_nodes.erase(it); + m_nodes.erase(m_nodes.begin()); } const char* ASTBlock::type_str() const diff --git a/ASTree.cpp b/ASTree.cpp index 334a2a3..15ebfcc 100644 --- a/ASTree.cpp +++ b/ASTree.cpp @@ -2241,7 +2241,7 @@ static void print_block(PycRef blk, PycModule* mod) { print_src(pass, mod); } - for (ASTBlock::list_t::const_iterator ln = lines.begin(); ln != lines.end();) { + for (auto ln = lines.cbegin(); ln != lines.cend();) { if ((*ln).cast().type() != ASTNode::NODE_NODELIST) { start_line(cur_indent); } @@ -2283,22 +2283,22 @@ void print_src(PycRef node, PycModule* mod) print_src(call->func(), mod); fputs("(", pyc_output); bool first = true; - for (ASTCall::pparam_t::const_iterator p = call->pparams().begin(); p != call->pparams().end(); ++p) { + for (const auto& param : call->pparams()) { if (!first) fputs(", ", pyc_output); - print_src(*p, mod); + print_src(param, mod); first = false; } - for (ASTCall::kwparam_t::const_iterator p = call->kwparams().begin(); p != call->kwparams().end(); ++p) { + for (const auto& param : call->kwparams()) { if (!first) fputs(", ", pyc_output); - if (p->first.type() == ASTNode::NODE_NAME) { - fprintf(pyc_output, "%s = ", p->first.cast()->name()->value()); + if (param.first.type() == ASTNode::NODE_NAME) { + fprintf(pyc_output, "%s = ", param.first.cast()->name()->value()); } else { - PycRef str_name = p->first.cast()->object().require_cast(); + PycRef str_name = param.first.cast()->object().require_cast(); fprintf(pyc_output, "%s = ", str_name->value()); } - print_src(p->second, mod); + print_src(param.second, mod); first = false; } if (call->hasVar()) { @@ -2347,17 +2347,16 @@ void print_src(PycRef node, PycModule* mod) break; case ASTNode::NODE_LIST: { - ASTList::value_t values = node.cast()->values(); fputs("[", pyc_output); bool first = true; cur_indent++; - for (ASTList::value_t::const_iterator b = values.begin(); b != values.end(); ++b) { + for (const auto& val : node.cast()->values()) { if (first) fputs("\n", pyc_output); else fputs(",\n", pyc_output); start_line(cur_indent); - print_src(*b, mod); + print_src(val, mod); first = false; } cur_indent--; @@ -2367,35 +2366,33 @@ void print_src(PycRef node, PycModule* mod) case ASTNode::NODE_COMPREHENSION: { PycRef comp = node.cast(); - ASTComprehension::generator_t values = comp->generators(); fputs("[ ", pyc_output); print_src(comp->result(), mod); - for (ASTComprehension::generator_t::const_iterator it = values.begin(); it != values.end(); ++it) { + for (const auto& gen : comp->generators()) { fputs(" for ", pyc_output); - print_src((*it)->index(), mod); + print_src(gen->index(), mod); fputs(" in ", pyc_output); - print_src((*it)->iter(), mod); + print_src(gen->iter(), mod); } fputs(" ]", pyc_output); } break; case ASTNode::NODE_MAP: { - ASTMap::map_t values = node.cast()->values(); fputs("{", pyc_output); bool first = true; cur_indent++; - for (ASTMap::map_t::const_iterator b = values.begin(); b != values.end(); ++b) { + for (const auto& val : node.cast()->values()) { if (first) fputs("\n", pyc_output); else fputs(",\n", pyc_output); start_line(cur_indent); - print_src(b->first, mod); + print_src(val.first, mod); fputs(": ", pyc_output); - print_src(b->second, mod); + print_src(val.second, mod); first = false; } cur_indent--; @@ -2408,12 +2405,11 @@ void print_src(PycRef node, PycModule* mod) case ASTNode::NODE_NODELIST: { cur_indent++; - ASTNodeList::list_t lines = node.cast()->nodes(); - for (ASTNodeList::list_t::const_iterator ln = lines.begin(); ln != lines.end(); ++ln) { - if ((*ln).cast().type() != ASTNode::NODE_NODELIST) { + for (const auto& ln : node.cast()->nodes()) { + if (ln.cast().type() != ASTNode::NODE_NODELIST) { start_line(cur_indent); } - print_src(*ln, mod); + print_src(ln, mod); end_line(); } cur_indent--; @@ -2517,10 +2513,10 @@ void print_src(PycRef node, PycModule* mod) PycRef raise = node.cast(); fputs("raise ", pyc_output); bool first = true; - for (ASTRaise::param_t::const_iterator p = raise->params().begin(); p != raise->params().end(); ++p) { + for (const auto& param : raise->params()) { if (!first) fputs(", ", pyc_output); - print_src(*p, mod); + print_src(param, mod); first = false; } } @@ -2567,25 +2563,26 @@ void print_src(PycRef node, PycModule* mod) print_src(import->name(), mod); fputs(" import ", pyc_output); - ASTImport::list_t::const_iterator ii = stores.begin(); if (stores.size() == 1) { - print_src((*ii)->src(), mod); + auto src = stores.front()->src(); + auto dest = stores.front()->dest(); + print_src(src, mod); - if ((*ii)->src().cast()->name()->value() != (*ii)->dest().cast()->name()->value()) { + if (src.cast()->name()->value() != dest.cast()->name()->value()) { fputs(" as ", pyc_output); - print_src((*ii)->dest(), mod); + print_src(dest, mod); } } else { bool first = true; - for (; ii != stores.end(); ++ii) { + for (const auto& st : stores) { if (!first) fputs(", ", pyc_output); - print_src((*ii)->src(), mod); + print_src(st->src(), mod); first = false; - if ((*ii)->src().cast()->name()->value() != (*ii)->dest().cast()->name()->value()) { + if (st->src().cast()->name()->value() != st->dest().cast()->name()->value()) { fputs(" as ", pyc_output); - print_src((*ii)->dest(), mod); + print_src(st->dest(), mod); } } } @@ -2602,7 +2599,7 @@ void print_src(PycRef node, PycModule* mod) PycRef code = node.cast()->code(); PycRef code_src = code.cast()->object().cast(); ASTFunction::defarg_t defargs = node.cast()->defargs(); - ASTFunction::defarg_t::iterator da = defargs.begin(); + auto da = defargs.cbegin(); for (int i=0; iargCount(); i++) { if (i > 0) fputs(", ", pyc_output); @@ -2647,7 +2644,7 @@ void print_src(PycRef node, PycModule* mod) } ASTFunction::defarg_t defargs = src.cast()->defargs(); - ASTFunction::defarg_t::iterator da = defargs.begin(); + auto da = defargs.cbegin(); bool first = true; for (int i=0; iargCount(); i++) { if (!first) @@ -2699,10 +2696,10 @@ void print_src(PycRef node, PycModule* mod) if (bases->values().size() > 0) { fputs("(", pyc_output); bool first = true; - for (ASTTuple::value_t::const_iterator b = bases->values().begin(); b != bases->values().end(); ++b) { + for (const auto& val : bases->values()) { if (!first) fputs(", ", pyc_output); - print_src(*b, mod); + print_src(val, mod); first = false; } fputs("):\n", pyc_output); @@ -2728,11 +2725,10 @@ void print_src(PycRef node, PycModule* mod) if (fromlist.type() == PycObject::TYPE_TUPLE || fromlist.type() == PycObject::TYPE_SMALL_TUPLE) { bool first = true; - PycTuple::value_t::const_iterator ii = fromlist.cast()->values().begin(); - for (; ii != fromlist.cast()->values().end(); ++ii) { + for (const auto& val : fromlist.cast()->values()) { if (!first) fputs(", ", pyc_output); - fprintf(pyc_output, "%s", ii->cast()->value()); + fprintf(pyc_output, "%s", val.cast()->value()); first = false; } } else { @@ -2783,10 +2779,10 @@ void print_src(PycRef node, PycModule* mod) if (tuple->requireParens()) fputc('(', pyc_output); bool first = true; - for (ASTTuple::value_t::const_iterator b = values.begin(); b != values.end(); ++b) { + for (const auto& val : values) { if (!first) fputs(", ", pyc_output); - print_src(*b, mod); + print_src(val, mod); first = false; } if (values.size() == 1) @@ -2891,11 +2887,10 @@ void decompyle(PycRef code, PycModule* mod) start_line(cur_indent + 1); fputs("global ", pyc_output); bool first = true; - PycCode::globals_t::iterator it; - for (it = globs.begin(); it != globs.end(); ++it) { + for (const auto& glob : globs) { if (!first) fputs(", ", pyc_output); - fprintf(pyc_output, "%s", (*it)->value()); + fprintf(pyc_output, "%s", glob->value()); first = false; } fputs("\n", pyc_output); diff --git a/bytecode.cpp b/bytecode.cpp index 4f010f1..1f5bf2c 100644 --- a/bytecode.cpp +++ b/bytecode.cpp @@ -169,10 +169,10 @@ void print_const(PycRef obj, PycModule* mod) { fputs("(", pyc_output); PycTuple::value_t values = obj.cast()->values(); - PycTuple::value_t::const_iterator it = values.begin(); - if (it != values.end()) { + auto it = values.cbegin(); + if (it != values.cend()) { print_const(*it, mod); - while (++it != values.end()) { + while (++it != values.cend()) { fputs(", ", pyc_output); print_const(*it, mod); } @@ -187,10 +187,10 @@ void print_const(PycRef obj, PycModule* mod) { fputs("[", pyc_output); PycList::value_t values = obj.cast()->values(); - PycList::value_t::const_iterator it = values.begin(); - if (it != values.end()) { + auto it = values.cbegin(); + if (it != values.cend()) { print_const(*it, mod); - while (++it != values.end()) { + while (++it != values.cend()) { fputs(", ", pyc_output); print_const(*it, mod); } @@ -203,13 +203,13 @@ void print_const(PycRef obj, PycModule* mod) fputs("{", pyc_output); PycDict::key_t keys = obj.cast()->keys(); PycDict::value_t values = obj.cast()->values(); - PycDict::key_t::const_iterator ki = keys.begin(); - PycDict::value_t::const_iterator vi = values.begin(); - if (ki != keys.end()) { + auto ki = keys.cbegin(); + auto vi = values.cbegin(); + if (ki != keys.cend()) { print_const(*ki, mod); fputs(": ", pyc_output); print_const(*vi, mod); - while (++ki != keys.end()) { + while (++ki != keys.cend()) { ++vi; fputs(", ", pyc_output); print_const(*ki, mod); @@ -224,10 +224,10 @@ void print_const(PycRef obj, PycModule* mod) { fputs("{", pyc_output); PycSet::value_t values = obj.cast()->values(); - PycSet::value_t::const_iterator it = values.begin(); - if (it != values.end()) { + auto it = values.cbegin(); + if (it != values.cend()) { print_const(*it, mod); - while (++it != values.end()) { + while (++it != values.cend()) { fputs(", ", pyc_output); print_const(*it, mod); } diff --git a/pyc_module.cpp b/pyc_module.cpp index 0780935..4e782ae 100644 --- a/pyc_module.cpp +++ b/pyc_module.cpp @@ -189,10 +189,10 @@ PycRef PycModule::getIntern(int ref) const if (ref < 0) throw std::out_of_range("Intern index out of range"); - std::list >::const_iterator it = m_interns.begin(); - while (ref-- && it != m_interns.end()) + auto it = m_interns.cbegin(); + while (ref-- && it != m_interns.cend()) ++it; - if (it == m_interns.end()) + if (it == m_interns.cend()) throw std::out_of_range("Intern index out of range"); return *it; } @@ -202,10 +202,10 @@ PycRef PycModule::getRef(int ref) const if (ref < 0) throw std::out_of_range("Ref index out of range"); - std::list >::const_iterator it = m_refs.begin(); - while (ref-- && it != m_refs.end()) + auto it = m_refs.cbegin(); + while (ref-- && it != m_refs.cend()) ++it; - if (it == m_refs.end()) + if (it == m_refs.cend()) throw std::out_of_range("Ref index out of range"); return *it; } diff --git a/pyc_numeric.cpp b/pyc_numeric.cpp index b673a21..adc60a8 100644 --- a/pyc_numeric.cpp +++ b/pyc_numeric.cpp @@ -61,15 +61,14 @@ std::string PycLong::repr() const // Realign to 32 bits, since Python uses only 15 std::list bits; - std::list::const_iterator bit; int shift = 0, temp = 0; - for (bit = m_value.begin(); bit != m_value.end(); ++bit) { - temp |= unsigned(*bit & 0xFFFF) << shift; + for (auto bit : m_value) { + temp |= unsigned(bit & 0xFFFF) << shift; shift += 15; if (shift >= 32) { bits.push_back(temp); shift -= 32; - temp = unsigned(*bit & 0xFFFF) >> (15 - shift); + temp = unsigned(bit & 0xFFFF) >> (15 - shift); } } if (temp) diff --git a/pycdas.cpp b/pycdas.cpp index 36b8bf3..f8096f0 100644 --- a/pycdas.cpp +++ b/pycdas.cpp @@ -151,18 +151,16 @@ void output_object(PycRef obj, PycModule* mod, int indent) case PycObject::TYPE_SMALL_TUPLE: { iputs(indent, "(\n"); - PycTuple::value_t values = obj.cast()->values(); - for (PycTuple::value_t::const_iterator i = values.begin(); i != values.end(); i++) - output_object(*i, mod, indent + 1); + for (const auto& val : obj.cast()->values()) + output_object(val, mod, indent + 1); iputs(indent, ")\n"); } break; case PycObject::TYPE_LIST: { iputs(indent, "[\n"); - PycList::value_t values = obj.cast()->values(); - for (PycList::value_t::const_iterator i = values.begin(); i != values.end(); i++) - output_object(*i, mod, indent + 1); + for (const auto& val : obj.cast()->values()) + output_object(val, mod, indent + 1); iputs(indent, "]\n"); } break; @@ -184,9 +182,8 @@ void output_object(PycRef obj, PycModule* mod, int indent) case PycObject::TYPE_SET: { iputs(indent, "{\n"); - PycSet::value_t values = obj.cast()->values(); - for (PycSet::value_t::const_iterator i = values.begin(); i != values.end(); i++) - output_object(*i, mod, indent + 1); + for (const auto& val : obj.cast()->values()) + output_object(val, mod, indent + 1); iputs(indent, "}\n"); } break;