qhttpheaderparser: use algorithms more

No raw loops. Also de-duplicate functors by lambda generator

Change-Id: I053890a0a472bfe2846d7007fda8cca779d812a8
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Anton Kudryavtsev 2023-08-31 18:56:57 +03:00 committed by Anton Kudryavtsev
parent c59b34b8cf
commit 0e5f60a6ca

View File

@ -133,13 +133,19 @@ const QList<QPair<QByteArray, QByteArray> >& QHttpHeaderParser::headers() const
return fields;
}
static auto firstEqualsName(QByteArrayView name)
{
return [name](const QPair<QByteArray, QByteArray> &header) {
return name.compare(header.first, Qt::CaseInsensitive) == 0;
};
}
QByteArray QHttpHeaderParser::firstHeaderField(QByteArrayView name,
const QByteArray &defaultValue) const
{
for (auto it = fields.constBegin(); it != fields.constEnd(); ++it) {
if (name.compare(it->first, Qt::CaseInsensitive) == 0)
return it->second;
}
const auto it = std::find_if(fields.begin(), fields.end(), firstEqualsName(name));
if (it != fields.end())
return it->second;
return defaultValue;
}
@ -163,10 +169,7 @@ QList<QByteArray> QHttpHeaderParser::headerFieldValues(QByteArrayView name) cons
void QHttpHeaderParser::removeHeaderField(QByteArrayView name)
{
auto firstEqualsName = [name](const QPair<QByteArray, QByteArray> &header) {
return name.compare(header.first, Qt::CaseInsensitive) == 0;
};
fields.removeIf(firstEqualsName);
fields.removeIf(firstEqualsName(name));
}
void QHttpHeaderParser::setHeaderField(const QByteArray &name, const QByteArray &data)