From 77ab5e968906de3e279c2bd0f5b153bc74dc7a45 Mon Sep 17 00:00:00 2001 From: Urs Fleisch Date: Sun, 16 Jul 2023 06:41:53 +0200 Subject: [PATCH] Narrow the scope of iterators Also make sure that it is visible at first glance that iterators are not taken from temporaries. --- examples/framelist.cpp | 8 +++---- taglib/ape/apetag.cpp | 14 ++++++------- taglib/asf/asftag.cpp | 12 ++++------- taglib/fileref.cpp | 9 +++----- taglib/mpeg/id3v2/frames/chapterframe.cpp | 6 ++---- taglib/mpeg/id3v2/frames/commentsframe.cpp | 4 +--- .../id3v2/frames/eventtimingcodesframe.cpp | 4 +--- .../mpeg/id3v2/frames/relativevolumeframe.cpp | 7 ++----- .../id3v2/frames/synchronizedlyricsframe.cpp | 4 +--- .../id3v2/frames/tableofcontentsframe.cpp | 7 ++----- .../frames/uniquefileidentifierframe.cpp | 4 +--- taglib/mpeg/id3v2/id3v2tag.cpp | 21 ++++++++----------- taglib/ogg/oggpage.cpp | 6 ++---- taglib/ogg/xiphcomment.cpp | 9 +++----- taglib/riff/wav/infotag.cpp | 3 +-- taglib/toolkit/tlist.tcc | 3 +-- taglib/toolkit/tstringlist.cpp | 3 +-- 17 files changed, 44 insertions(+), 80 deletions(-) diff --git a/examples/framelist.cpp b/examples/framelist.cpp index 37b692c2..a770728c 100644 --- a/examples/framelist.cpp +++ b/examples/framelist.cpp @@ -65,8 +65,8 @@ int main(int argc, char *argv[]) << " bytes in tag" << endl; - auto it = id3v2tag->frameList().begin(); - for(; it != id3v2tag->frameList().end(); it++) { + const auto &frames = id3v2tag->frameList(); + for(auto it = frames.begin(); it != frames.end(); it++) { cout << (*it)->frameID(); if(auto comment = dynamic_cast(*it)) @@ -100,8 +100,8 @@ int main(int argc, char *argv[]) cout << endl << "APE" << endl; if(ape) { - for(auto it = ape->itemListMap().begin(); - it != ape->itemListMap().end(); ++it) + const auto &items = ape->itemListMap(); + for(auto it = items.begin(); it != items.end(); ++it) { if((*it).second.type() != APE::Item::Binary) cout << (*it).first << " - \"" << (*it).second.toString() << "\"" << endl; diff --git a/taglib/ape/apetag.cpp b/taglib/ape/apetag.cpp index 879c524a..ab1e69ef 100644 --- a/taglib/ape/apetag.cpp +++ b/taglib/ape/apetag.cpp @@ -217,8 +217,8 @@ namespace PropertyMap APE::Tag::properties() const { PropertyMap properties; - auto it = itemListMap().begin(); - for(; it != itemListMap().end(); ++it) { + const auto &items = itemListMap(); + for(auto it = items.begin(); it != items.end(); ++it) { String tagName = it->first.upper(); // if the item is Binary or Locator, or if the key is an invalid string, // add to unsupportedData @@ -239,8 +239,7 @@ PropertyMap APE::Tag::properties() const void APE::Tag::removeUnsupportedProperties(const StringList &properties) { - auto it = properties.begin(); - for(; it != properties.end(); ++it) + for(auto it = properties.begin(); it != properties.end(); ++it) removeItem(*it); } @@ -257,8 +256,8 @@ PropertyMap APE::Tag::setProperties(const PropertyMap &origProps) // first check if tags need to be removed completely StringList toRemove; - auto remIt = itemListMap().begin(); - for(; remIt != itemListMap().end(); ++remIt) { + const auto &items = itemListMap(); + for(auto remIt = items.begin(); remIt != items.end(); ++remIt) { String key = remIt->first.upper(); // only remove if a) key is valid, b) type is text, c) key not contained in new properties if(!key.isEmpty() && remIt->second.type() == APE::Item::Text && !properties.contains(key)) @@ -269,9 +268,8 @@ PropertyMap APE::Tag::setProperties(const PropertyMap &origProps) removeItem(*removeIt); // now sync in the "forward direction" - auto it = properties.cbegin(); PropertyMap invalid; - for(; it != properties.cend(); ++it) { + for(auto it = properties.begin(); it != properties.cend(); ++it) { const String &tagName = it->first; if(!checkKey(tagName)) invalid.insert(it->first, it->second); diff --git a/taglib/asf/asftag.cpp b/taglib/asf/asftag.cpp index c116eae5..a445cf03 100644 --- a/taglib/asf/asftag.cpp +++ b/taglib/asf/asftag.cpp @@ -287,12 +287,10 @@ PropertyMap ASF::Tag::properties() const props["COMMENT"] = d->comment; } - auto it = d->attributeListMap.cbegin(); - for(; it != d->attributeListMap.cend(); ++it) { + for(auto it = d->attributeListMap.cbegin(); it != d->attributeListMap.cend(); ++it) { const String key = translateKey(it->first); if(!key.isEmpty()) { - auto it2 = it->second.begin(); - for(; it2 != it->second.end(); ++it2) { + for(auto it2 = it->second.begin(); it2 != it->second.end(); ++it2) { if(key == "TRACKNUMBER") { if(it2->type() == ASF::Attribute::DWordType) props.insert(key, String::number(it2->toUInt())); @@ -313,8 +311,7 @@ PropertyMap ASF::Tag::properties() const void ASF::Tag::removeUnsupportedProperties(const StringList &props) { - auto it = props.begin(); - for(; it != props.end(); ++it) + for(auto it = props.begin(); it != props.end(); ++it) d->attributeListMap.erase(*it); } @@ -355,8 +352,7 @@ PropertyMap ASF::Tag::setProperties(const PropertyMap &props) if(reverseKeyMap.contains(it->first)) { String name = reverseKeyMap[it->first]; removeItem(name); - auto it2 = it->second.begin(); - for(; it2 != it->second.end(); ++it2) { + for(auto it2 = it->second.begin(); it2 != it->second.end(); ++it2) { addAttribute(name, *it2); } } diff --git a/taglib/fileref.cpp b/taglib/fileref.cpp index 44fe1250..e9f8a5a8 100644 --- a/taglib/fileref.cpp +++ b/taglib/fileref.cpp @@ -74,8 +74,7 @@ namespace if(::strlen(fileName) == 0) return nullptr; #endif - auto it = fileTypeResolvers.cbegin(); - for(; it != fileTypeResolvers.cend(); ++it) { + for(auto it = fileTypeResolvers.cbegin(); it != fileTypeResolvers.cend(); ++it) { File *file = (*it)->createFile(fileName, readAudioProperties, audioPropertiesStyle); if(file) return file; @@ -87,10 +86,8 @@ namespace File *detectByResolvers(IOStream* stream, bool readAudioProperties, AudioProperties::ReadStyle audioPropertiesStyle) { - for(auto it = fileTypeResolvers.cbegin(); - it != fileTypeResolvers.cend(); ++it) { - if(auto streamResolver = - dynamic_cast(*it)) { + for(auto it = fileTypeResolvers.cbegin(); it != fileTypeResolvers.cend(); ++it) { + if(auto streamResolver = dynamic_cast(*it)) { if(File *file = streamResolver->createFileFromStream( stream, readAudioProperties, audioPropertiesStyle)) return file; diff --git a/taglib/mpeg/id3v2/frames/chapterframe.cpp b/taglib/mpeg/id3v2/frames/chapterframe.cpp index d5164470..171b1ee3 100644 --- a/taglib/mpeg/id3v2/frames/chapterframe.cpp +++ b/taglib/mpeg/id3v2/frames/chapterframe.cpp @@ -85,8 +85,7 @@ ChapterFrame::ChapterFrame(const ByteVector &elementID, d->startOffset = startOffset; d->endOffset = endOffset; - for(auto it = embeddedFrames.begin(); - it != embeddedFrames.end(); ++it) + for(auto it = embeddedFrames.begin(); it != embeddedFrames.end(); ++it) addEmbeddedFrame(*it); } @@ -205,8 +204,7 @@ String ChapterFrame::toString() const if(!d->embeddedFrameList.isEmpty()) { StringList frameIDs; - for(auto it = d->embeddedFrameList.cbegin(); - it != d->embeddedFrameList.cend(); ++it) + for(auto it = d->embeddedFrameList.cbegin(); it != d->embeddedFrameList.cend(); ++it) frameIDs.append((*it)->frameID()); s += ", sub-frames: [ " + frameIDs.toString(", ") + " ]"; } diff --git a/taglib/mpeg/id3v2/frames/commentsframe.cpp b/taglib/mpeg/id3v2/frames/commentsframe.cpp index eb7e8c2f..9b703338 100644 --- a/taglib/mpeg/id3v2/frames/commentsframe.cpp +++ b/taglib/mpeg/id3v2/frames/commentsframe.cpp @@ -127,9 +127,7 @@ CommentsFrame *CommentsFrame::findByDescription(const ID3v2::Tag *tag, const Str { const ID3v2::FrameList comments = tag->frameList("COMM"); - for(auto it = comments.begin(); - it != comments.end(); - ++it) + for(auto it = comments.begin(); it != comments.end(); ++it) { auto frame = dynamic_cast(*it); if(frame && frame->description() == d) diff --git a/taglib/mpeg/id3v2/frames/eventtimingcodesframe.cpp b/taglib/mpeg/id3v2/frames/eventtimingcodesframe.cpp index 11909e29..d09b6c97 100644 --- a/taglib/mpeg/id3v2/frames/eventtimingcodesframe.cpp +++ b/taglib/mpeg/id3v2/frames/eventtimingcodesframe.cpp @@ -121,9 +121,7 @@ ByteVector EventTimingCodesFrame::renderFields() const ByteVector v; v.append(static_cast(d->timestampFormat)); - for(auto it = d->synchedEvents.cbegin(); - it != d->synchedEvents.cend(); - ++it) { + for(auto it = d->synchedEvents.cbegin(); it != d->synchedEvents.cend(); ++it) { const SynchedEvent &entry = *it; v.append(static_cast(entry.type)); v.append(ByteVector::fromUInt(entry.time)); diff --git a/taglib/mpeg/id3v2/frames/relativevolumeframe.cpp b/taglib/mpeg/id3v2/frames/relativevolumeframe.cpp index 5447b46b..85fade44 100644 --- a/taglib/mpeg/id3v2/frames/relativevolumeframe.cpp +++ b/taglib/mpeg/id3v2/frames/relativevolumeframe.cpp @@ -78,8 +78,7 @@ List RelativeVolumeFrame::channels() const { List l; - auto it = d->channels.cbegin(); - for(; it != d->channels.cend(); ++it) + for(auto it = d->channels.cbegin(); it != d->channels.cend(); ++it) l.append((*it).first); return l; @@ -162,9 +161,7 @@ ByteVector RelativeVolumeFrame::renderFields() const data.append(d->identification.data(String::Latin1)); data.append(textDelimiter(String::Latin1)); - auto it = d->channels.cbegin(); - - for(; it != d->channels.cend(); ++it) { + for(auto it = d->channels.cbegin(); it != d->channels.cend(); ++it) { ChannelType type = (*it).first; const ChannelData &channel = (*it).second; diff --git a/taglib/mpeg/id3v2/frames/synchronizedlyricsframe.cpp b/taglib/mpeg/id3v2/frames/synchronizedlyricsframe.cpp index 0b3a7fde..481b39d3 100644 --- a/taglib/mpeg/id3v2/frames/synchronizedlyricsframe.cpp +++ b/taglib/mpeg/id3v2/frames/synchronizedlyricsframe.cpp @@ -218,9 +218,7 @@ ByteVector SynchronizedLyricsFrame::renderFields() const v.append(static_cast(d->type)); v.append(d->description.data(encoding)); v.append(textDelimiter(encoding)); - for(auto it = d->synchedText.cbegin(); - it != d->synchedText.cend(); - ++it) { + for(auto it = d->synchedText.cbegin(); it != d->synchedText.cend(); ++it) { const SynchedText &entry = *it; v.append(entry.text.data(encoding)); v.append(textDelimiter(encoding)); diff --git a/taglib/mpeg/id3v2/frames/tableofcontentsframe.cpp b/taglib/mpeg/id3v2/frames/tableofcontentsframe.cpp index d267b268..32e1f826 100644 --- a/taglib/mpeg/id3v2/frames/tableofcontentsframe.cpp +++ b/taglib/mpeg/id3v2/frames/tableofcontentsframe.cpp @@ -230,8 +230,7 @@ String TableOfContentsFrame::toString() const if(!d->embeddedFrameList.isEmpty()) { StringList frameIDs; - for(auto it = d->embeddedFrameList.cbegin(); - it != d->embeddedFrameList.cend(); ++it) + for(auto it = d->embeddedFrameList.cbegin(); it != d->embeddedFrameList.cend(); ++it) frameIDs.append((*it)->frameID()); s += ", sub-frames: [ " + frameIDs.toString(", ") + " ]"; } @@ -269,9 +268,7 @@ TableOfContentsFrame *TableOfContentsFrame::findTopLevel(const ID3v2::Tag *tag) { const ID3v2::FrameList tablesOfContents = tag->frameList("CTOC"); - for(auto it = tablesOfContents.begin(); - it != tablesOfContents.end(); - ++it) + for(auto it = tablesOfContents.begin(); it != tablesOfContents.end(); ++it) { auto frame = dynamic_cast(*it); if(frame && frame->isTopLevel()) diff --git a/taglib/mpeg/id3v2/frames/uniquefileidentifierframe.cpp b/taglib/mpeg/id3v2/frames/uniquefileidentifierframe.cpp index d8a55cf8..44ad20ce 100644 --- a/taglib/mpeg/id3v2/frames/uniquefileidentifierframe.cpp +++ b/taglib/mpeg/id3v2/frames/uniquefileidentifierframe.cpp @@ -105,9 +105,7 @@ UniqueFileIdentifierFrame *UniqueFileIdentifierFrame::findByOwner(const ID3v2::T { const ID3v2::FrameList comments = tag->frameList("UFID"); - for(auto it = comments.begin(); - it != comments.end(); - ++it) + for(auto it = comments.begin(); it != comments.end(); ++it) { auto frame = dynamic_cast(*it); if(frame && frame->owner() == o) diff --git a/taglib/mpeg/id3v2/id3v2tag.cpp b/taglib/mpeg/id3v2/id3v2tag.cpp index b9b5339e..f78af096 100644 --- a/taglib/mpeg/id3v2/id3v2tag.cpp +++ b/taglib/mpeg/id3v2/id3v2tag.cpp @@ -386,7 +386,8 @@ void ID3v2::Tag::removeFrames(const ByteVector &id) PropertyMap ID3v2::Tag::properties() const { PropertyMap properties; - for(auto it = frameList().begin(); it != frameList().end(); ++it) { + const auto &frames = frameList(); + for(auto it = frames.begin(); it != frames.end(); ++it) { PropertyMap props = (*it)->asProperties(); properties.merge(props); } @@ -442,7 +443,8 @@ PropertyMap ID3v2::Tag::setProperties(const PropertyMap &origProps) PropertyMap tiplProperties; PropertyMap tmclProperties; Frame::splitProperties(origProps, properties, tiplProperties, tmclProperties); - for(auto it = frameListMap().begin(); it != frameListMap().end(); ++it){ + const auto &frames = frameListMap(); + for(auto it = frames.begin(); it != frames.end(); ++it){ for(auto lit = it->second.begin(); lit != it->second.end(); ++lit){ PropertyMap frameProperties = (*lit)->asProperties(); if(it->first == "TIPL") { @@ -531,8 +533,7 @@ void ID3v2::Tag::downgradeFrames(FrameList *frames, FrameList *newFrames) const String content = frameTDOR->toString(); if(content.size() >= 4) { - auto frameTORY = - new ID3v2::TextIdentificationFrame("TORY", String::Latin1); + auto frameTORY = new ID3v2::TextIdentificationFrame("TORY", String::Latin1); frameTORY->setText(content.substr(0, 4)); frames->append(frameTORY); newFrames->append(frameTORY); @@ -542,20 +543,17 @@ void ID3v2::Tag::downgradeFrames(FrameList *frames, FrameList *newFrames) const if(frameTDRC) { String content = frameTDRC->toString(); if(content.size() >= 4) { - auto frameTYER = - new ID3v2::TextIdentificationFrame("TYER", String::Latin1); + auto frameTYER = new ID3v2::TextIdentificationFrame("TYER", String::Latin1); frameTYER->setText(content.substr(0, 4)); frames->append(frameTYER); newFrames->append(frameTYER); if(content.size() >= 10 && content[4] == '-' && content[7] == '-') { - auto frameTDAT = - new ID3v2::TextIdentificationFrame("TDAT", String::Latin1); + auto frameTDAT = new ID3v2::TextIdentificationFrame("TDAT", String::Latin1); frameTDAT->setText(content.substr(8, 2) + content.substr(5, 2)); frames->append(frameTDAT); newFrames->append(frameTDAT); if(content.size() >= 16 && content[10] == 'T' && content[13] == ':') { - auto frameTIME = - new ID3v2::TextIdentificationFrame("TIME", String::Latin1); + auto frameTIME = new ID3v2::TextIdentificationFrame("TIME", String::Latin1); frameTIME->setText(content.substr(11, 2) + content.substr(14, 2)); frames->append(frameTIME); newFrames->append(frameTIME); @@ -565,8 +563,7 @@ void ID3v2::Tag::downgradeFrames(FrameList *frames, FrameList *newFrames) const } if(frameTIPL || frameTMCL) { - auto frameIPLS = - new ID3v2::TextIdentificationFrame("IPLS", String::Latin1); + auto frameIPLS = new ID3v2::TextIdentificationFrame("IPLS", String::Latin1); StringList people; diff --git a/taglib/ogg/oggpage.cpp b/taglib/ogg/oggpage.cpp index ce76d474..9774c1e2 100644 --- a/taglib/ogg/oggpage.cpp +++ b/taglib/ogg/oggpage.cpp @@ -213,8 +213,7 @@ ByteVectorList Ogg::Page::packets() const const List packetSizes = d->header.packetSizes(); - auto it = packetSizes.begin(); - for(; it != packetSizes.end(); ++it) + for(auto it = packetSizes.begin(); it != packetSizes.end(); ++it) l.append(d->file->readBlock(*it)); } else @@ -243,8 +242,7 @@ ByteVector Ogg::Page::render() const debug("Ogg::Page::render() -- this page is empty!"); } else { - auto it = d->packets.cbegin(); - for(; it != d->packets.cend(); ++it) + for(auto it = d->packets.cbegin(); it != d->packets.cend(); ++it) data.append(*it); } diff --git a/taglib/ogg/xiphcomment.cpp b/taglib/ogg/xiphcomment.cpp index ebeac2cc..e0da5d3e 100644 --- a/taglib/ogg/xiphcomment.cpp +++ b/taglib/ogg/xiphcomment.cpp @@ -234,8 +234,7 @@ PropertyMap Ogg::XiphComment::setProperties(const PropertyMap &properties) // now go through keys in \a properties and check that the values match those in the xiph comment PropertyMap invalid; - auto it = properties.begin(); - for(; it != properties.end(); ++it) + for(auto it = properties.begin(); it != properties.end(); ++it) { if(!checkKey(it->first)) invalid.insert(it->first, it->second); @@ -366,16 +365,14 @@ ByteVector Ogg::XiphComment::render(bool addFramingBit) const // std::pair where the first String is the field name and // the StringList is the values associated with that field. - auto it = d->fieldListMap.cbegin(); - for(; it != d->fieldListMap.cend(); ++it) { + for(auto it = d->fieldListMap.cbegin(); it != d->fieldListMap.cend(); ++it) { // And now iterate over the values of the current list. String fieldName = (*it).first; const StringList values = (*it).second; - auto valuesIt = values.begin(); - for(; valuesIt != values.end(); ++valuesIt) { + for(auto valuesIt = values.begin(); valuesIt != values.end(); ++valuesIt) { ByteVector fieldData = fieldName.data(String::UTF8); fieldData.append('='); fieldData.append((*valuesIt).data(String::UTF8)); diff --git a/taglib/riff/wav/infotag.cpp b/taglib/riff/wav/infotag.cpp index ed131c50..1f834090 100644 --- a/taglib/riff/wav/infotag.cpp +++ b/taglib/riff/wav/infotag.cpp @@ -201,8 +201,7 @@ ByteVector RIFF::Info::Tag::render() const { ByteVector data("INFO"); - auto it = d->fieldListMap.cbegin(); - for(; it != d->fieldListMap.cend(); ++it) { + for(auto it = d->fieldListMap.cbegin(); it != d->fieldListMap.cend(); ++it) { ByteVector text = stringHandler->render(it->second); if(text.isEmpty()) continue; diff --git a/taglib/toolkit/tlist.tcc b/taglib/toolkit/tlist.tcc index a56f316d..856c5c45 100644 --- a/taglib/toolkit/tlist.tcc +++ b/taglib/toolkit/tlist.tcc @@ -74,8 +74,7 @@ public: } void clear() { if(autoDelete) { - auto it = list.begin(); - for(; it != list.end(); ++it) + for(auto it = list.begin(); it != list.end(); ++it) delete *it; } list.clear(); diff --git a/taglib/toolkit/tstringlist.cpp b/taglib/toolkit/tstringlist.cpp index a6d3260c..436fb913 100644 --- a/taglib/toolkit/tstringlist.cpp +++ b/taglib/toolkit/tstringlist.cpp @@ -72,8 +72,7 @@ StringList::StringList(const String &s) StringList::StringList(const ByteVectorList &bl, String::Type t) { - auto i = bl.begin(); - for(;i != bl.end(); i++) { + for(auto i = bl.begin(); i != bl.end(); i++) { append(String(*i, t)); } }