From 574604765ffce939d7b8e706cb3963ebb7ae0b41 Mon Sep 17 00:00:00 2001 From: Rosen Penev Date: Wed, 9 Aug 2023 10:09:28 -0700 Subject: [PATCH] Use std algorithms (#1107) Found with: readability-use-anyofallof Signed-off-by: Rosen Penev --- taglib/ape/apetag.cpp | 8 +------- taglib/mp4/mp4atom.cpp | 13 +++---------- taglib/mp4/mp4file.cpp | 11 +---------- .../mpeg/id3v2/frames/textidentificationframe.cpp | 13 +++++-------- taglib/mpeg/id3v2/id3v2frame.cpp | 7 +------ taglib/mpeg/id3v2/id3v2framefactory.cpp | 8 +++----- taglib/ogg/xiphcomment.cpp | 14 ++------------ taglib/riff/riffutils.h | 8 +------- taglib/toolkit/tpropertymap.cpp | 8 +------- taglib/toolkit/tstring.cpp | 12 ++---------- 10 files changed, 20 insertions(+), 82 deletions(-) diff --git a/taglib/ape/apetag.cpp b/taglib/ape/apetag.cpp index 58d9a479..d7beadf1 100644 --- a/taglib/ape/apetag.cpp +++ b/taglib/ape/apetag.cpp @@ -65,13 +65,7 @@ namespace return false; } - const String upperKey = String(key).upper(); - for(auto k : invalidKeys) { - if(upperKey == k) - return false; - } - - return true; + return std::none_of(invalidKeys.begin(), invalidKeys.end(), [upperKey = String(key).upper()](auto k) { return upperKey == k; }); } } // namespace diff --git a/taglib/mp4/mp4atom.cpp b/taglib/mp4/mp4atom.cpp index eadc54ef..0a434c72 100644 --- a/taglib/mp4/mp4atom.cpp +++ b/taglib/mp4/mp4atom.cpp @@ -97,19 +97,12 @@ MP4::Atom::Atom(File *file) if(name == c) { if(name == "meta") { offset_t posAfterMeta = file->tell(); - ByteVector nextSize = file->readBlock(8).mid(4, 4); static constexpr std::array metaChildrenNames { "hdlr", "ilst", "mhdr", "ctry", "lang" }; - bool metaIsFullAtom = true; - for(auto child : metaChildrenNames) { - if(nextSize == child) { - // meta is not a full atom (i.e. not followed by version, flags). It - // is followed by the size and type of the first child atom. - metaIsFullAtom = false; - break; - } - } + // meta is not a full atom (i.e. not followed by version, flags). It + // is followed by the size and type of the first child atom. + auto metaIsFullAtom = std::none_of(metaChildrenNames.begin(), metaChildrenNames.end(), [nextSize = file->readBlock(8).mid(4, 4)](const auto &child) { return nextSize == child; }); // Only skip next four bytes, which contain version and flags, if meta // is a full atom. file->seek(posAfterMeta + (metaIsFullAtom ? 4 : 0)); diff --git a/taglib/mp4/mp4file.cpp b/taglib/mp4/mp4file.cpp index 22a1b783..68e66cfa 100644 --- a/taglib/mp4/mp4file.cpp +++ b/taglib/mp4/mp4file.cpp @@ -36,16 +36,7 @@ namespace { bool checkValid(const MP4::AtomList &list) { - for(auto it = list.begin(); it != list.end(); ++it) { - - if((*it)->length == 0) - return false; - - if(!checkValid((*it)->children)) - return false; - } - - return true; + return std::none_of(list.begin(), list.end(), [](const auto &a) { return a->length == 0 || !checkValid(a->children); }); } } // namespace diff --git a/taglib/mpeg/id3v2/frames/textidentificationframe.cpp b/taglib/mpeg/id3v2/frames/textidentificationframe.cpp index 181da84d..8fc245e2 100644 --- a/taglib/mpeg/id3v2/frames/textidentificationframe.cpp +++ b/taglib/mpeg/id3v2/frames/textidentificationframe.cpp @@ -294,14 +294,11 @@ PropertyMap TextIdentificationFrame::makeTIPLProperties() const } const StringList l = fieldList(); for(auto it = l.begin(); it != l.end(); ++it) { - bool found = false; - for(const auto &[o, t] : involvedPeople) - if(*it == o) { - map.insert(t, (++it)->split(",")); - found = true; - break; - } - if(!found){ + auto found = std::find_if(involvedPeople.begin(), involvedPeople.end(), [=](const auto &person) { return *it == person.first; }); + if(found != involvedPeople.end()) { + map.insert(found->second, (++it)->split(",")); + } + else { // invalid involved role -> mark whole frame as unsupported in order to be consistent with writing map.clear(); map.unsupportedData().append(frameID()); diff --git a/taglib/mpeg/id3v2/id3v2frame.cpp b/taglib/mpeg/id3v2/id3v2frame.cpp index 26ae8c3e..a1d59360 100644 --- a/taglib/mpeg/id3v2/id3v2frame.cpp +++ b/taglib/mpeg/id3v2/id3v2frame.cpp @@ -72,12 +72,7 @@ namespace if(frameID.size() != 4) return false; - for(auto it = frameID.begin(); it != frameID.end(); it++) { - if( (*it < 'A' || *it > 'Z') && (*it < '0' || *it > '9') ) { - return false; - } - } - return true; + return std::none_of(frameID.begin(), frameID.end(), [](auto c) { return (c < 'A' || c > 'Z') && (c < '0' || c > '9'); }); } } // namespace diff --git a/taglib/mpeg/id3v2/id3v2framefactory.cpp b/taglib/mpeg/id3v2/id3v2framefactory.cpp index 4f03c142..44571c0a 100644 --- a/taglib/mpeg/id3v2/id3v2framefactory.cpp +++ b/taglib/mpeg/id3v2/id3v2framefactory.cpp @@ -147,11 +147,9 @@ std::pair FrameFactory::prepareFrameHeader( } #endif - for(auto it = frameID.cbegin(); it != frameID.cend(); it++) { - if( (*it < 'A' || *it > 'Z') && (*it < '0' || *it > '9') ) { - delete header; - return {nullptr, false}; - } + if(std::any_of(frameID.begin(), frameID.end(), [](auto c) { return (c < 'A' || c > 'Z') && (c < '0' || c > '9'); })) { + delete header; + return { nullptr, false }; } if(version > 3 && (tagHeader->unsynchronisation() || header->unsynchronisation())) { diff --git a/taglib/ogg/xiphcomment.cpp b/taglib/ogg/xiphcomment.cpp index 1f326221..fd565581 100644 --- a/taglib/ogg/xiphcomment.cpp +++ b/taglib/ogg/xiphcomment.cpp @@ -189,12 +189,7 @@ void Ogg::XiphComment::setTrack(unsigned int i) bool Ogg::XiphComment::isEmpty() const { - for(auto it = d->fieldListMap.cbegin(); it != d->fieldListMap.cend(); ++it) { - if(!(*it).second.isEmpty()) - return false; - } - - return true; + return std::all_of(d->fieldListMap.begin(), d->fieldListMap.end(), [](const auto &field) { return field.second.isEmpty(); }); } unsigned int Ogg::XiphComment::fieldCount() const @@ -261,12 +256,7 @@ bool Ogg::XiphComment::checkKey(const String &key) // A key may consist of ASCII 0x20 through 0x7D, 0x3D ('=') excluded. - for(String::ConstIterator it = key.begin(); it != key.end(); it++) { - if(*it < 0x20 || *it > 0x7D || *it == 0x3D) - return false; - } - - return true; + return std::none_of(key.begin(), key.end(), [](auto c) { return c < 0x20 || c > 0x7D || c == 0x3D; }); } String Ogg::XiphComment::vendorID() const diff --git a/taglib/riff/riffutils.h b/taglib/riff/riffutils.h index 9973b044..869f137d 100644 --- a/taglib/riff/riffutils.h +++ b/taglib/riff/riffutils.h @@ -44,13 +44,7 @@ namespace TagLib if(name.size() != 4) return false; - for(auto it = name.begin(); it != name.end(); ++it) { - const int c = static_cast(*it); - if(c < 32 || 127 < c) - return false; - } - - return true; + return std::none_of(name.begin(), name.end(), [](unsigned char c) { return c < 32 || 127 < c; }); } } // namespace diff --git a/taglib/toolkit/tpropertymap.cpp b/taglib/toolkit/tpropertymap.cpp index f3f8e34c..3e1d144d 100644 --- a/taglib/toolkit/tpropertymap.cpp +++ b/taglib/toolkit/tpropertymap.cpp @@ -95,13 +95,7 @@ bool PropertyMap::contains(const String &key) const bool PropertyMap::contains(const PropertyMap &other) const { - for(auto it = other.begin(); it != other.end(); ++it) { - if(!SimplePropertyMap::contains(it->first)) - return false; - if ((*this)[it->first] != it->second) - return false; - } - return true; + return std::all_of(other.begin(), other.end(), [this](const auto &o) { return contains(o.first) && (*this)[o.first] == o.second; }); } PropertyMap &PropertyMap::erase(const String &key) diff --git a/taglib/toolkit/tstring.cpp b/taglib/toolkit/tstring.cpp index 1d6e3ab2..006e9f25 100644 --- a/taglib/toolkit/tstring.cpp +++ b/taglib/toolkit/tstring.cpp @@ -510,20 +510,12 @@ String String::stripWhiteSpace() const bool String::isLatin1() const { - for(ConstIterator it = begin(); it != end(); ++it) { - if(*it >= 256) - return false; - } - return true; + return std::none_of(this->begin(), this->end(), [](auto c) { return c >= 256; }); } bool String::isAscii() const { - for(ConstIterator it = begin(); it != end(); ++it) { - if(*it >= 128) - return false; - } - return true; + return std::none_of(this->begin(), this->end(), [](auto c) { return c >= 128; }); } String String::number(int n) // static