manual range loop conversions (#1126)

* manual range loop conversions

Signed-off-by: Rosen Penev <rosenp@gmail.com>

* Restore const containers where non const temporaries are iterated

* Use std::as_const() instead of const container copies where possible

---------

Signed-off-by: Rosen Penev <rosenp@gmail.com>
Co-authored-by: Urs Fleisch <ufleisch@users.sourceforge.net>
This commit is contained in:
Rosen Penev
2023-09-06 11:58:13 -07:00
committed by GitHub
parent 303b55fb97
commit 524b588a1e
37 changed files with 464 additions and 481 deletions

View File

@ -27,6 +27,7 @@
#include <algorithm>
#include <array>
#include <utility>
#include "tstring.h"
#include "tdebug.h"
@ -92,8 +93,8 @@ unsigned int pageChecksum(const ByteVector &data)
};
unsigned int sum = 0;
for(auto it = data.begin(); it != data.end(); ++it)
sum = (sum << 8) ^ crcTable[((sum >> 24) & 0xff) ^ static_cast<unsigned char>(*it)];
for(const auto &byte : data)
sum = (sum << 8) ^ crcTable[((sum >> 24) & 0xff) ^ static_cast<unsigned char>(byte)];
return sum;
}
@ -212,9 +213,8 @@ ByteVectorList Ogg::Page::packets() const
d->file->seek(d->fileOffset + d->header.size());
const List<int> packetSizes = d->header.packetSizes();
for(auto it = packetSizes.begin(); it != packetSizes.end(); ++it)
l.append(d->file->readBlock(*it));
for(const auto &size : packetSizes)
l.append(d->file->readBlock(size));
}
else
debug("Ogg::Page::packets() -- attempting to read packets from an invalid page.");
@ -242,8 +242,8 @@ ByteVector Ogg::Page::render() const
debug("Ogg::Page::render() -- this page is empty!");
}
else {
for(auto it = d->packets.cbegin(); it != d->packets.cend(); ++it)
data.append(*it);
for(const auto &packet : std::as_const(d->packets))
data.append(packet);
}
// Compute and set the checksum for the Ogg page. The checksum is taken over
@ -274,8 +274,8 @@ List<Ogg::Page *> Ogg::Page::paginate(const ByteVectorList &packets,
if(strategy != Repaginate) {
size_t tableSize = 0;
for(auto it = packets.begin(); it != packets.end(); ++it)
tableSize += it->size() / 255 + 1;
for(const auto &packet : packets)
tableSize += packet.size() / 255 + 1;
if(tableSize > 255)
strategy = Repaginate;
@ -354,9 +354,9 @@ Ogg::Page::Page(const ByteVectorList &packets,
ByteVector data;
List<int> packetSizes;
for(auto it = packets.begin(); it != packets.end(); ++it) {
packetSizes.append((*it).size());
data.append(*it);
for(const auto &packet : packets) {
packetSizes.append(packet.size());
data.append(packet);
}
d->packets = packets;
d->header.setPacketSizes(packetSizes);

View File

@ -290,7 +290,7 @@ ByteVector Ogg::PageHeader::lacingValues() const
data.resize(data.size() + (*it / 255), '\xff');
if(it != --d->packetSizes.cend() || d->lastPacketCompleted)
if(it != std::prev(d->packetSizes.cend()) || d->lastPacketCompleted)
data.append(static_cast<unsigned char>(*it % 255));
}

View File

@ -31,6 +31,8 @@
#include "flacpicture.h"
#include "tpropertymap.h"
#include <utility>
using namespace TagLib;
namespace
@ -196,8 +198,8 @@ unsigned int Ogg::XiphComment::fieldCount() const
{
unsigned int count = 0;
for(auto it = d->fieldListMap.cbegin(); it != d->fieldListMap.cend(); ++it)
count += (*it).second.size();
for(const auto &[_, list] : std::as_const(d->fieldListMap))
count += list.size();
count += d->pictureList.size();
@ -218,31 +220,27 @@ PropertyMap Ogg::XiphComment::setProperties(const PropertyMap &properties)
{
// check which keys are to be deleted
StringList toRemove;
for(auto it = d->fieldListMap.cbegin(); it != d->fieldListMap.cend(); ++it)
if (!properties.contains(it->first))
toRemove.append(it->first);
for(const auto &[field, _] : std::as_const(d->fieldListMap))
if(!properties.contains(field))
toRemove.append(field);
for(auto it = toRemove.cbegin(); it != toRemove.cend(); ++it)
removeFields(*it);
for(const auto &field : std::as_const(toRemove))
removeFields(field);
// now go through keys in \a properties and check that the values match those in the xiph comment
PropertyMap invalid;
for(auto it = properties.begin(); it != properties.end(); ++it)
{
if(!checkKey(it->first))
invalid.insert(it->first, it->second);
else if(!d->fieldListMap.contains(it->first) || !(it->second == d->fieldListMap[it->first])) {
const StringList &sl = it->second;
for(const auto &[key, sl] : properties) {
if(!checkKey(key))
invalid.insert(key, sl);
else if(!d->fieldListMap.contains(key) || !(sl == d->fieldListMap[key])) {
if(sl.isEmpty())
// zero size string list -> remove the tag with all values
removeFields(it->first);
removeFields(key);
else {
// replace all strings in the list for the tag
auto valueIterator = sl.begin();
addField(it->first, *valueIterator, true);
++valueIterator;
for(; valueIterator != sl.end(); ++valueIterator)
addField(it->first, *valueIterator, false);
addField(key, *sl.begin(), true);
for(auto it = std::next(sl.begin()); it != sl.end(); ++it)
addField(key, *it, false);
}
}
}
@ -353,25 +351,20 @@ ByteVector Ogg::XiphComment::render(bool addFramingBit) const
// std::pair<String, StringList> where the first String is the field name and
// the StringList is the values associated with that field.
for(auto it = d->fieldListMap.cbegin(); it != d->fieldListMap.cend(); ++it) {
for(const auto &[fieldName, values] : std::as_const(d->fieldListMap)) {
// And now iterate over the values of the current list.
String fieldName = (*it).first;
const StringList values = (*it).second;
for(auto valuesIt = values.begin(); valuesIt != values.end(); ++valuesIt) {
for(const auto &value : values) {
ByteVector fieldData = fieldName.data(String::UTF8);
fieldData.append('=');
fieldData.append((*valuesIt).data(String::UTF8));
fieldData.append(value.data(String::UTF8));
data.append(ByteVector::fromUInt(fieldData.size(), false));
data.append(fieldData);
}
}
for(auto it = d->pictureList.cbegin(); it != d->pictureList.cend(); ++it) {
ByteVector picture = (*it)->render().toBase64();
for(const auto &p : std::as_const(d->pictureList)) {
ByteVector picture = p->render().toBase64();
data.append(ByteVector::fromUInt(picture.size() + 23, false));
data.append("METADATA_BLOCK_PICTURE=");
data.append(picture);