QXmlStream: use even more ranged for loops

Write a small range adapter that maps begin() to rbegin() and end() to
rend() of the underlying range, use it to replace indexed reverse
loops with ranged for ones.

Change-Id: I5498a23106b0432ce885e72e5e3912910f50b5c1
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
Marc Mutz 2020-05-10 08:51:09 +02:00
parent 2e298c5f82
commit 0c17dca4f0

View File

@ -71,12 +71,31 @@ private:
#endif
#include <private/qmemory_p.h>
#include <iterator>
QT_BEGIN_NAMESPACE
#include "qxmlstream_p.h"
enum { StreamEOF = ~0U };
namespace {
template <typename Range>
auto reversed(Range &r)
{
struct R {
Range *r;
auto begin() { return std::make_reverse_iterator(std::end(*r)); }
auto end() { return std::make_reverse_iterator(std::begin(*r)); }
};
return R{&r};
}
template <typename Range>
void reversed(const Range &&) = delete;
}
/*!
\enum QXmlStreamReader::TokenType
@ -1562,8 +1581,7 @@ uint QXmlStreamReaderPrivate::getChar_helper()
QStringRef QXmlStreamReaderPrivate::namespaceForPrefix(const QStringRef &prefix)
{
for (int j = namespaceDeclarations.size() - 1; j >= 0; --j) {
const NamespaceDeclaration &namespaceDeclaration = namespaceDeclarations.at(j);
for (const NamespaceDeclaration &namespaceDeclaration : reversed(namespaceDeclarations)) {
if (namespaceDeclaration.prefix == prefix) {
return namespaceDeclaration.namespaceUri;
}
@ -3240,8 +3258,7 @@ bool QXmlStreamWriterPrivate::finishStartElement(bool contents)
QXmlStreamPrivateTagStack::NamespaceDeclaration &QXmlStreamWriterPrivate::findNamespace(const QString &namespaceUri, bool writeDeclaration, bool noDefault)
{
for (int j = namespaceDeclarations.size() - 1; j >= 0; --j) {
NamespaceDeclaration &namespaceDeclaration = namespaceDeclarations[j];
for (NamespaceDeclaration &namespaceDeclaration : reversed(namespaceDeclarations)) {
if (namespaceDeclaration.namespaceUri == namespaceUri) {
if (!noDefault || !namespaceDeclaration.prefix.isEmpty())
return namespaceDeclaration;