From 6c69a747b9f7a801be2ad58985f35886bd38239e Mon Sep 17 00:00:00 2001 From: m-holger Date: Sat, 21 May 2022 15:18:15 +0100 Subject: Code clean up: use range-style for loops wherever possible Remove variables obsoleted by commit 4f24617. --- libqpdf/QPDF.cc | 3 +- libqpdf/QPDFAcroFormDocumentHelper.cc | 16 +++------- libqpdf/QPDFJob.cc | 55 ++++++++++------------------------- libqpdf/QPDFObjectHandle.cc | 16 ++++------ libqpdf/QPDFPageDocumentHelper.cc | 9 ++---- libqpdf/QPDFTokenizer.cc | 3 +- libqpdf/QPDFWriter.cc | 21 +++++-------- libqpdf/QPDF_linearization.cc | 9 ++---- libqpdf/QPDF_optimization.cc | 20 +++++-------- 9 files changed, 48 insertions(+), 104 deletions(-) (limited to 'libqpdf') diff --git a/libqpdf/QPDF.cc b/libqpdf/QPDF.cc index 2b79e6e3..a40b70b3 100644 --- a/libqpdf/QPDF.cc +++ b/libqpdf/QPDF.cc @@ -2369,8 +2369,7 @@ QPDF::reserveObjects(QPDFObjectHandle foreign, ObjCopier& obj_copier, bool top) } } else if (foreign.isDictionary()) { QTC::TC("qpdf", "QPDF reserve dictionary"); - std::set keys = foreign.getKeys(); - for (auto const& key: keys) { + for (auto const& key: foreign.getKeys()) { reserveObjects(foreign.getKey(key), obj_copier, false); } } else if (foreign.isStream()) { diff --git a/libqpdf/QPDFAcroFormDocumentHelper.cc b/libqpdf/QPDFAcroFormDocumentHelper.cc index 70d01b30..ddf10e6d 100644 --- a/libqpdf/QPDFAcroFormDocumentHelper.cc +++ b/libqpdf/QPDFAcroFormDocumentHelper.cc @@ -294,12 +294,8 @@ QPDFAcroFormDocumentHelper::analyze() // a file that contains this kind of error will probably not // actually work with most viewers. - QPDFPageDocumentHelper dh(this->qpdf); - std::vector pages = dh.getAllPages(); - for (auto const& ph: pages) { - std::vector annots = - getWidgetAnnotationsForPage(ph); - for (auto const& iter: annots) { + for (auto const& ph: QPDFPageDocumentHelper(this->qpdf).getAllPages()) { + for (auto const& iter: getWidgetAnnotationsForPage(ph)) { QPDFObjectHandle annot(iter.getObjectHandle()); QPDFObjGen og(annot.getObjGen()); if (this->m->annotation_to_field.count(og) == 0) { @@ -451,12 +447,8 @@ QPDFAcroFormDocumentHelper::generateAppearancesIfNeeded() return; } - QPDFPageDocumentHelper pdh(this->qpdf); - std::vector pages = pdh.getAllPages(); - for (auto const& page: pages) { - std::vector annotations = - getWidgetAnnotationsForPage(page); - for (auto& aoh: annotations) { + for (auto const& page: QPDFPageDocumentHelper(this->qpdf).getAllPages()) { + for (auto& aoh: getWidgetAnnotationsForPage(page)) { QPDFFormFieldObjectHelper ffh = getFieldForAnnotation(aoh); if (ffh.getFieldType() == "/Btn") { // Rather than generating appearances for button diff --git a/libqpdf/QPDFJob.cc b/libqpdf/QPDFJob.cc index 3190e39f..7c9e9bed 100644 --- a/libqpdf/QPDFJob.cc +++ b/libqpdf/QPDFJob.cc @@ -867,11 +867,9 @@ QPDFJob::doCheck(QPDF& pdf) w.write(); // Parse all content streams - QPDFPageDocumentHelper dh(pdf); - std::vector pages = dh.getAllPages(); DiscardContents discard_contents; int pageno = 0; - for (auto& page: pages) { + for (auto& page: QPDFPageDocumentHelper(pdf).getAllPages()) { ++pageno; try { page.parseContents(&discard_contents); @@ -939,11 +937,9 @@ QPDFJob::doShowObj(QPDF& pdf) void QPDFJob::doShowPages(QPDF& pdf) { - QPDFPageDocumentHelper dh(pdf); - std::vector pages = dh.getAllPages(); int pageno = 0; auto& cout = *this->m->cout; - for (auto& ph: pages) { + for (auto& ph: QPDFPageDocumentHelper(pdf).getAllPages()) { QPDFObjectHandle page = ph.getObjectHandle(); ++pageno; @@ -966,8 +962,7 @@ QPDFJob::doShowPages(QPDF& pdf) } cout << " content:" << std::endl; - std::vector content = ph.getPageContents(); - for (auto& iter2: content) { + for (auto& iter2: ph.getPageContents()) { cout << " " << iter2.unparse() << std::endl; } } @@ -1085,8 +1080,7 @@ QPDFJob::doJSONObjects(Pipeline* p, bool& first, QPDF& pdf) JSON::writeDictionaryOpen(p, first_object, 1); bool all_objects = m->json_objects.empty(); std::set wanted_og = getWantedJSONObjects(); - std::vector objects = pdf.getAllObjects(); - for (auto& obj: objects) { + for (auto& obj: pdf.getAllObjects()) { std::string key = obj.unparse(); if (this->m->json_version > 1) { key = "obj:" + key; @@ -1140,20 +1134,17 @@ QPDFJob::doJSONPages(Pipeline* p, bool& first, QPDF& pdf) JSON::writeDictionaryKey(p, first, "pages", 0); bool first_page = true; JSON::writeArrayOpen(p, first_page, 1); - QPDFPageDocumentHelper pdh(pdf); QPDFPageLabelDocumentHelper pldh(pdf); QPDFOutlineDocumentHelper odh(pdf); - std::vector pages = pdh.getAllPages(); int pageno = -1; - for (auto& ph: pages) { + for (auto& ph: QPDFPageDocumentHelper(pdf).getAllPages()) { ++pageno; JSON j_page = JSON::makeDictionary(); QPDFObjectHandle page = ph.getObjectHandle(); j_page.addDictionaryMember( "object", page.getJSON(this->m->json_version)); JSON j_images = j_page.addDictionaryMember("images", JSON::makeArray()); - std::map images = ph.getImages(); - for (auto const& iter2: images) { + for (auto const& iter2: ph.getImages()) { JSON j_image = j_images.addArrayElement(JSON::makeDictionary()); j_image.addDictionaryMember("name", JSON::makeString(iter2.first)); QPDFObjectHandle image = iter2.second; @@ -1195,8 +1186,7 @@ QPDFJob::doJSONPages(Pipeline* p, bool& first, QPDF& pdf) j_page.addDictionaryMember("images", j_images); JSON j_contents = j_page.addDictionaryMember("contents", JSON::makeArray()); - std::vector content = ph.getPageContents(); - for (auto& iter2: content) { + for (auto& iter2: ph.getPageContents()) { j_contents.addArrayElement(iter2.getJSON(this->m->json_version)); } j_page.addDictionaryMember( @@ -1285,10 +1275,8 @@ void QPDFJob::doJSONOutlines(Pipeline* p, bool& first, QPDF& pdf) { std::map page_numbers; - QPDFPageDocumentHelper dh(pdf); - std::vector pages = dh.getAllPages(); int n = 0; - for (auto const& ph: pages) { + for (auto const& ph: QPDFPageDocumentHelper(pdf).getAllPages()) { QPDFObjectHandle oh = ph.getObjectHandle(); page_numbers[oh.getObjGen()] = ++n; } @@ -1309,14 +1297,10 @@ QPDFJob::doJSONAcroform(Pipeline* p, bool& first, QPDF& pdf) j_acroform.addDictionaryMember( "needappearances", JSON::makeBool(afdh.getNeedAppearances())); JSON j_fields = j_acroform.addDictionaryMember("fields", JSON::makeArray()); - QPDFPageDocumentHelper pdh(pdf); - std::vector pages = pdh.getAllPages(); int pagepos1 = 0; - for (auto const& page: pages) { + for (auto const& page: QPDFPageDocumentHelper(pdf).getAllPages()) { ++pagepos1; - std::vector annotations = - afdh.getWidgetAnnotationsForPage(page); - for (auto& aoh: annotations) { + for (auto& aoh: afdh.getWidgetAnnotationsForPage(page)) { QPDFFormFieldObjectHelper ffh = afdh.getFieldForAnnotation(aoh); JSON j_field = j_fields.addArrayElement(JSON::makeDictionary()); j_field.addDictionaryMember( @@ -1355,8 +1339,7 @@ QPDFJob::doJSONAcroform(Pipeline* p, bool& first, QPDF& pdf) j_field.addDictionaryMember("istext", JSON::makeBool(ffh.isText())); JSON j_choices = j_field.addDictionaryMember("choices", JSON::makeArray()); - std::vector choices = ffh.getChoices(); - for (auto const& choice: choices) { + for (auto const& choice: ffh.getChoices()) { j_choices.addArrayElement(JSON::makeString(choice)); } JSON j_annot = j_field.addDictionaryMember( @@ -2282,19 +2265,16 @@ QPDFJob::handleTransformations(QPDF& pdf) }; if (m->externalize_inline_images || (m->optimize_images && (!m->keep_inline_images))) { - std::vector pages = dh.getAllPages(); - for (auto& ph: pages) { + for (auto& ph: dh.getAllPages()) { ph.externalizeInlineImages(m->ii_min_bytes); } } if (m->optimize_images) { int pageno = 0; - std::vector pages = dh.getAllPages(); - for (auto& ph: pages) { + for (auto& ph: dh.getAllPages()) { ++pageno; QPDFObjectHandle page = ph.getObjectHandle(); - std::map images = ph.getImages(); - for (auto& iter2: images) { + for (auto& iter2: ph.getImages()) { std::string name = iter2.first; QPDFObjectHandle& image = iter2.second; ImageOptimizer* io = new ImageOptimizer( @@ -2330,8 +2310,7 @@ QPDFJob::handleTransformations(QPDF& pdf) m->flatten_annotations_required, m->flatten_annotations_forbidden); } if (m->coalesce_contents) { - std::vector pages = dh.getAllPages(); - for (auto& page: pages) { + for (auto& page: dh.getAllPages()) { page.coalesceContentStreams(); } } @@ -2805,9 +2784,7 @@ QPDFJob::handleRotations(QPDF& pdf) std::string const& range = iter.first; QPDFJob::RotationSpec const& rspec = iter.second; // range has been previously validated - std::vector to_rotate = - QUtil::parse_numrange(range.c_str(), npages); - for (int pageno_iter: to_rotate) { + for (int pageno_iter: QUtil::parse_numrange(range.c_str(), npages)) { int pageno = pageno_iter - 1; if ((pageno >= 0) && (pageno < npages)) { pages.at(QIntC::to_size(pageno)) diff --git a/libqpdf/QPDFObjectHandle.cc b/libqpdf/QPDFObjectHandle.cc index 6ac69ab1..56a545d6 100644 --- a/libqpdf/QPDFObjectHandle.cc +++ b/libqpdf/QPDFObjectHandle.cc @@ -1240,12 +1240,10 @@ QPDFObjectHandle::getResourceNames() if (!isDictionary()) { return result; } - std::set keys = getKeys(); - for (auto const& key: keys) { + for (auto const& key: getKeys()) { QPDFObjectHandle val = getKey(key); if (val.isDictionary()) { - std::set val_keys = val.getKeys(); - for (auto const& val_key: val_keys) { + for (auto const& val_key: val.getKeys()) { result.insert(val_key); } } @@ -1642,14 +1640,12 @@ QPDFObjectHandle::addPageContents(QPDFObjectHandle new_contents, bool first) { new_contents.assertStream(); - std::vector orig_contents = getPageContents(); - std::vector content_streams; if (first) { QTC::TC("qpdf", "QPDFObjectHandle prepend page contents"); content_streams.push_back(new_contents); } - for (auto const& iter: orig_contents) { + for (auto const& iter: getPageContents()) { QTC::TC("qpdf", "QPDFObjectHandle append page contents"); content_streams.push_back(iter); } @@ -1657,8 +1653,7 @@ QPDFObjectHandle::addPageContents(QPDFObjectHandle new_contents, bool first) content_streams.push_back(new_contents); } - QPDFObjectHandle contents = QPDFObjectHandle::newArray(content_streams); - this->replaceKey("/Contents", contents); + this->replaceKey("/Contents", newArray(content_streams)); } void @@ -2927,9 +2922,8 @@ QPDFObjectHandle::copyObject( new_obj = std::shared_ptr(new QPDF_Array(items)); } else if (isDictionary()) { QTC::TC("qpdf", "QPDFObjectHandle clone dictionary"); - std::set keys = getKeys(); std::map items; - for (auto const& key: keys) { + for (auto const& key: getKeys()) { items[key] = getKey(key); if ((!first_level_only) && (cross_indirect || (!items[key].isIndirect()))) { diff --git a/libqpdf/QPDFPageDocumentHelper.cc b/libqpdf/QPDFPageDocumentHelper.cc index f54ad843..021b4369 100644 --- a/libqpdf/QPDFPageDocumentHelper.cc +++ b/libqpdf/QPDFPageDocumentHelper.cc @@ -12,9 +12,8 @@ QPDFPageDocumentHelper::QPDFPageDocumentHelper(QPDF& qpdf) : std::vector QPDFPageDocumentHelper::getAllPages() { - std::vector const& pages_v = this->qpdf.getAllPages(); std::vector pages; - for (auto const& iter: pages_v) { + for (auto const& iter: this->qpdf.getAllPages()) { pages.push_back(QPDFPageObjectHelper(iter)); } return pages; @@ -29,8 +28,7 @@ QPDFPageDocumentHelper::pushInheritedAttributesToPage() void QPDFPageDocumentHelper::removeUnreferencedResources() { - std::vector pages = getAllPages(); - for (auto& ph: pages) { + for (auto& ph: getAllPages()) { ph.removeUnreferencedResources(); } } @@ -66,8 +64,7 @@ QPDFPageDocumentHelper::flattenAnnotations( .warnIfPossible("document does not have updated appearance streams," " so form fields will not be flattened"); } - std::vector pages = getAllPages(); - for (auto& ph: pages) { + for (auto& ph: getAllPages()) { QPDFObjectHandle resources = ph.getAttribute("/Resources", true); if (!resources.isDictionary()) { // This should never happen and is not exercised in the diff --git a/libqpdf/QPDFTokenizer.cc b/libqpdf/QPDFTokenizer.cc index a38b9ef8..d6467cc0 100644 --- a/libqpdf/QPDFTokenizer.cc +++ b/libqpdf/QPDFTokenizer.cc @@ -567,8 +567,7 @@ QPDFTokenizer::findEI(std::shared_ptr input) bool found_alpha = false; bool found_non_printable = false; bool found_other = false; - std::string value = t.getValue(); - for (char ch: value) { + for (char ch: t.getValue()) { if (((ch >= 'a') && (ch <= 'z')) || ((ch >= 'A') && (ch <= 'Z')) || (ch == '*')) { // Treat '*' as alpha since there are valid diff --git a/libqpdf/QPDFWriter.cc b/libqpdf/QPDFWriter.cc index 70c65c5d..b1bb4ad7 100644 --- a/libqpdf/QPDFWriter.cc +++ b/libqpdf/QPDFWriter.cc @@ -1237,8 +1237,7 @@ QPDFWriter::enqueueObject(QPDFObjectHandle object) } } } else if (object.isDictionary()) { - std::set keys = object.getKeys(); - for (auto const& key: keys) { + for (auto const& key: object.getKeys()) { if (!this->m->linearized) { enqueueObject(object.getKey(key)); } @@ -1283,8 +1282,7 @@ QPDFWriter::writeTrailer( writeString(" /Size "); writeString(QUtil::int_to_string(size)); } else { - std::set keys = trailer.getKeys(); - for (auto const& key: keys) { + for (auto const& key: trailer.getKeys()) { writeStringQDF(" "); writeStringNoQDF(" "); writeString(QPDF_Name::normalizeName(key)); @@ -1644,8 +1642,7 @@ QPDFWriter::unparseObject( writeString("<<"); writeStringQDF("\n"); - std::set keys = object.getKeys(); - for (auto const& key: keys) { + for (auto const& key: object.getKeys()) { writeStringQDF(indent); writeStringQDF(" "); writeStringNoQDF(" "); @@ -2074,8 +2071,7 @@ QPDFWriter::generateID() seed += " QPDF "; if (trailer.hasKey("/Info")) { QPDFObjectHandle info = trailer.getKey("/Info"); - std::set keys = info.getKeys(); - for (auto const& key: keys) { + for (auto const& key: info.getKeys()) { QPDFObjectHandle obj = info.getKey(key); if (obj.isString()) { seed += " "; @@ -2365,8 +2361,7 @@ QPDFWriter::doWriteSetup() if (this->m->linearized) { // Page dictionaries are not allowed to be compressed objects. - std::vector pages = this->m->pdf.getAllPages(); - for (auto& page: pages) { + for (auto& page: this->m->pdf.getAllPages()) { QPDFObjGen og = page.getObjGen(); if (this->m->object_to_object_stream.count(og)) { QTC::TC("qpdf", "QPDFWriter uncompressing page dictionary"); @@ -3251,8 +3246,7 @@ QPDFWriter::enqueueObjectsStandard() // dictionary into the queue, handling direct objects recursively. // Root is already there, so enqueuing it a second time is a // no-op. - std::set keys = trailer.getKeys(); - for (auto const& key: keys) { + for (auto const& key: trailer.getKeys()) { enqueueObject(trailer.getKey(key)); } } @@ -3276,8 +3270,7 @@ QPDFWriter::enqueueObjectsPCLm() // enqueue all the strips for each page QPDFObjectHandle strips = page.getKey("/Resources").getKey("/XObject"); - std::set keys = strips.getKeys(); - for (auto const& image: keys) { + for (auto const& image: strips.getKeys()) { enqueueObject(strips.getKey(image)); enqueueObject(QPDFObjectHandle::newStream( &this->m->pdf, image_transform_content)); diff --git a/libqpdf/QPDF_linearization.cc b/libqpdf/QPDF_linearization.cc index cf88bb70..ad1d9769 100644 --- a/libqpdf/QPDF_linearization.cc +++ b/libqpdf/QPDF_linearization.cc @@ -663,9 +663,8 @@ QPDF::maxEnd(ObjUser const& ou) if (this->m->obj_user_to_objects.count(ou) == 0) { stopOnError("no entry in object user table for requested object user"); } - std::set const& ogs = this->m->obj_user_to_objects[ou]; qpdf_offset_t end = 0; - for (auto const& og: ogs) { + for (auto const& og: this->m->obj_user_to_objects[ou]) { if (this->m->obj_cache.count(og) == 0) { stopOnError("unknown object referenced in object user table"); } @@ -1474,8 +1473,7 @@ QPDF::calculateLinearizationData(std::map const& object_stream_data) stopOnError("found unreferenced page while" " calculating linearization data"); } - std::set ogs = this->m->obj_user_to_objects[ou]; - for (auto const& og: ogs) { + for (auto const& og: this->m->obj_user_to_objects[ou]) { if (lc_other_page_private.count(og)) { lc_other_page_private.erase(og); this->m->part7.push_back(objGenToIndirect(og)); @@ -1637,8 +1635,7 @@ QPDF::calculateLinearizationData(std::map const& object_stream_data) stopOnError("found unreferenced page while" " calculating linearization data"); } - std::set const& ogs = this->m->obj_user_to_objects[ou]; - for (auto const& og: ogs) { + for (auto const& og: this->m->obj_user_to_objects[ou]) { if ((this->m->object_to_obj_users[og].size() > 1) && (obj_to_index.count(og.getObj()) > 0)) { int idx = obj_to_index[og.getObj()]; diff --git a/libqpdf/QPDF_optimization.cc b/libqpdf/QPDF_optimization.cc index 4204a20d..da0db663 100644 --- a/libqpdf/QPDF_optimization.cc +++ b/libqpdf/QPDF_optimization.cc @@ -90,8 +90,7 @@ QPDF::optimize( } // Traverse document-level items - std::set keys = this->m->trailer.getKeys(); - for (auto const& key: keys) { + for (auto const& key: this->m->trailer.getKeys()) { if (key == "/Root") { // handled separately } else { @@ -102,8 +101,7 @@ QPDF::optimize( } } - keys = root.getKeys(); - for (auto const& key: keys) { + for (auto const& key: root.getKeys()) { // Technically, /I keys from /Thread dictionaries are supposed // to be handled separately, but we are going to disregard // that specification for now. There is loads of evidence @@ -205,8 +203,7 @@ QPDF::pushInheritedAttributesToPageInternal( // that have values for this attribute. std::set inheritable_keys; - std::set keys = cur_pages.getKeys(); - for (auto const& key: keys) { + for (auto const& key: cur_pages.getKeys()) { if ((key == "/MediaBox") || (key == "/CropBox") || (key == "/Resources") || (key == "/Rotate")) { if (!allow_changes) { @@ -387,8 +384,7 @@ QPDF::updateObjectMapsInternal( } } - std::set keys = dict.getKeys(); - for (auto const& key: keys) { + for (auto const& key: dict.getKeys()) { if (is_page_node && (key == "/Thumb")) { // Traverse page thumbnail dictionaries as a special // case. @@ -437,8 +433,8 @@ QPDF::filterCompressedObjects(std::map const& object_stream_data) for (auto const& i1: this->m->obj_user_to_objects) { ObjUser const& ou = i1.first; - std::set const& objects = i1.second; - for (auto const& og: objects) { + // Loop over objects. + for (auto const& og: i1.second) { auto i2 = object_stream_data.find(og.getObj()); if (i2 == object_stream_data.end()) { t_obj_user_to_objects[ou].insert(og); @@ -450,8 +446,8 @@ QPDF::filterCompressedObjects(std::map const& object_stream_data) for (auto const& i1: this->m->object_to_obj_users) { QPDFObjGen const& og = i1.first; - std::set const& objusers = i1.second; - for (auto const& ou: objusers) { + // Loop over obj_users. + for (auto const& ou: i1.second) { auto i2 = object_stream_data.find(og.getObj()); if (i2 == object_stream_data.end()) { t_object_to_obj_users[og].insert(ou); -- cgit v1.2.3-54-g00ecf