From 25fff849e8f34af6d41ff36f2891bb4099b89360 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Sun, 11 Jul 2021 19:28:35 +0200 Subject: QDirIterator: add nextFileInfo() MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before this change, next() was the only way to advance the iterator, whether the caller was ultimately interested in just the filePath() (good) or not (bad luck, had to call .fileInfo()). Add a new function, nextFileInfo(), with returns fileInfo() instead. Incidentally, the returned object has already been constructed as part of advance()ing the iterator, so the new function is faster than next() even if the result is ignored, because we're not calculating a QString result the caller may not be interested in. Use the new function around the code. Fix a couple of cases of next(); fileInfo().filePath() (just use next()'s return value) as a drive-by. [ChangeLog][QtCore][QDirIterator] Added nextFileInfo(), which is like next(), but returns fileInfo() instead of filePath(). Change-Id: I601220575961169b44139fc55b9eae6c3197afb4 Reviewed-by: MÃ¥rten Nordheim --- tests/auto/corelib/io/qdir/tst_qdir.cpp | 4 +--- .../corelib/io/qdiriterator/tst_qdiriterator.cpp | 26 ++++++++-------------- .../io/qresourceengine/tst_qresourceengine.cpp | 4 +--- .../io/qtemporaryfile/tst_qtemporaryfile.cpp | 4 +--- tests/auto/tools/rcc/tst_rcc.cpp | 3 +-- .../corelib/io/qdir/10000/bench_qdir_10000.cpp | 12 +++++----- .../corelib/io/qdir/tree/bench_qdir_tree.cpp | 3 +-- tests/benchmarks/corelib/io/qdiriterator/main.cpp | 8 +++---- 8 files changed, 24 insertions(+), 40 deletions(-) (limited to 'tests') diff --git a/tests/auto/corelib/io/qdir/tst_qdir.cpp b/tests/auto/corelib/io/qdir/tst_qdir.cpp index a132750113..7b7a996cdd 100644 --- a/tests/auto/corelib/io/qdir/tst_qdir.cpp +++ b/tests/auto/corelib/io/qdir/tst_qdir.cpp @@ -242,9 +242,7 @@ tst_QDir::tst_QDir() QString resourceSourcePath = QStringLiteral(":/android_testdata/"); QDirIterator it(resourceSourcePath, QDirIterator::Subdirectories); while (it.hasNext()) { - it.next(); - - QFileInfo fileInfo = it.fileInfo(); + QFileInfo fileInfo = it.nextFileInfo(); if (!fileInfo.isDir()) { QString destination = m_dataPath + QLatin1Char('/') + fileInfo.filePath().mid(resourceSourcePath.length()); diff --git a/tests/auto/corelib/io/qdiriterator/tst_qdiriterator.cpp b/tests/auto/corelib/io/qdiriterator/tst_qdiriterator.cpp index b41352185d..35282ccaa3 100644 --- a/tests/auto/corelib/io/qdiriterator/tst_qdiriterator.cpp +++ b/tests/auto/corelib/io/qdiriterator/tst_qdiriterator.cpp @@ -127,9 +127,7 @@ void tst_QDirIterator::initTestCase() QString resourceSourcePath = QStringLiteral(":/testdata"); QDirIterator it(resourceSourcePath, QDirIterator::Subdirectories); while (it.hasNext()) { - it.next(); - - QFileInfo fileInfo = it.fileInfo(); + QFileInfo fileInfo = it.nextFileInfo(); if (!fileInfo.isDir()) { QString destination = testdata_dir + QLatin1Char('/') + fileInfo.filePath().mid(resourceSourcePath.length()); @@ -467,7 +465,7 @@ void tst_QDirIterator::stopLinkLoop() QStringList list; int max = 200; while (--max && it.hasNext()) - it.next(); + it.nextFileInfo(); QVERIFY(max); // The goal of this test is only to ensure that the test above don't malfunction @@ -508,10 +506,8 @@ void tst_QDirIterator::engineWithNoIterator() void tst_QDirIterator::absoluteFilePathsFromRelativeIteratorPath() { QDirIterator it("entrylist/", QDir::NoDotAndDotDot); - while (it.hasNext()) { - it.next(); - QVERIFY(QFileInfo(it.filePath()).absoluteFilePath().contains("entrylist")); - } + while (it.hasNext()) + QVERIFY(it.nextFileInfo().absoluteFilePath().contains("entrylist")); } void tst_QDirIterator::recurseWithFilters() const @@ -528,11 +524,9 @@ void tst_QDirIterator::recurseWithFilters() const expectedEntries.insert(QString::fromLatin1("recursiveDirs/textFileA.txt")); QVERIFY(it.hasNext()); - it.next(); - actualEntries.insert(it.fileInfo().filePath()); + actualEntries.insert(it.next()); QVERIFY(it.hasNext()); - it.next(); - actualEntries.insert(it.fileInfo().filePath()); + actualEntries.insert(it.next()); QCOMPARE(actualEntries, expectedEntries); QVERIFY(!it.hasNext()); @@ -555,7 +549,7 @@ void tst_QDirIterator::longPath() int m = 0; while (it.hasNext()) { ++m; - it.next(); + it.nextFileInfo(); } QCOMPARE(n, m); @@ -622,8 +616,7 @@ void tst_QDirIterator::hiddenDirs_hiddenFiles() QDirIterator di("hiddenDirs_hiddenFiles", QDir::Files | QDir::Hidden | QDir::NoDotAndDotDot, QDirIterator::Subdirectories); while (di.hasNext()) { ++matches; - QString filename = di.next(); - if (QFileInfo(filename).isDir()) + if (di.nextFileInfo().isDir()) ++failures; // search was only supposed to find files } QCOMPARE(matches, 6); @@ -636,8 +629,7 @@ void tst_QDirIterator::hiddenDirs_hiddenFiles() QDirIterator di("hiddenDirs_hiddenFiles", QDir::Dirs | QDir::Hidden | QDir::NoDotAndDotDot, QDirIterator::Subdirectories); while (di.hasNext()) { ++matches; - QString filename = di.next(); - if (!QFileInfo(filename).isDir()) + if (!di.nextFileInfo().isDir()) ++failures; // search was only supposed to find files } QCOMPARE(matches, 6); diff --git a/tests/auto/corelib/io/qresourceengine/tst_qresourceengine.cpp b/tests/auto/corelib/io/qresourceengine/tst_qresourceengine.cpp index b2f7aedab0..317a57a44c 100644 --- a/tests/auto/corelib/io/qresourceengine/tst_qresourceengine.cpp +++ b/tests/auto/corelib/io/qresourceengine/tst_qresourceengine.cpp @@ -77,9 +77,7 @@ void tst_QResourceEngine::initTestCase() QDirIterator it(sourcePath, QDirIterator::Subdirectories); while (it.hasNext()) { - it.next(); - - QFileInfo fileInfo = it.fileInfo(); + QFileInfo fileInfo = it.nextFileInfo(); if (!fileInfo.isDir()) { QString destination(dataPath + QLatin1Char('/') + fileInfo.filePath().mid(sourcePath.length())); QFileInfo destinationFileInfo(destination); diff --git a/tests/auto/corelib/io/qtemporaryfile/tst_qtemporaryfile.cpp b/tests/auto/corelib/io/qtemporaryfile/tst_qtemporaryfile.cpp index 5c7d69a788..dcf3486449 100644 --- a/tests/auto/corelib/io/qtemporaryfile/tst_qtemporaryfile.cpp +++ b/tests/auto/corelib/io/qtemporaryfile/tst_qtemporaryfile.cpp @@ -111,9 +111,7 @@ void tst_QTemporaryFile::initTestCase() QString sourceDir(":/android_testdata/"); QDirIterator it(sourceDir, QDirIterator::Subdirectories); while (it.hasNext()) { - it.next(); - - QFileInfo sourceFileInfo = it.fileInfo(); + QFileInfo sourceFileInfo = it.nextFileInfo(); if (!sourceFileInfo.isDir()) { QFileInfo destinationFileInfo(QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QLatin1Char('/') + sourceFileInfo.filePath().mid(sourceDir.length())); diff --git a/tests/auto/tools/rcc/tst_rcc.cpp b/tests/auto/tools/rcc/tst_rcc.cpp index 9b04987168..359ee7021e 100644 --- a/tests/auto/tools/rcc/tst_rcc.cpp +++ b/tests/auto/tools/rcc/tst_rcc.cpp @@ -274,8 +274,7 @@ void tst_rcc::binary_data() QDirIterator iter(dataPath, QStringList() << QLatin1String("*.qrc")); while (iter.hasNext()) { - iter.next(); - QFileInfo qrcFileInfo = iter.fileInfo(); + QFileInfo qrcFileInfo = iter.nextFileInfo(); QString absoluteBaseName = QFileInfo(qrcFileInfo.absolutePath(), qrcFileInfo.baseName()).absoluteFilePath(); QString rccFileName = absoluteBaseName + QLatin1String(".rcc"); diff --git a/tests/benchmarks/corelib/io/qdir/10000/bench_qdir_10000.cpp b/tests/benchmarks/corelib/io/qdir/10000/bench_qdir_10000.cpp index 3184df0d7b..e02cfb3099 100644 --- a/tests/benchmarks/corelib/io/qdir/10000/bench_qdir_10000.cpp +++ b/tests/benchmarks/corelib/io/qdir/10000/bench_qdir_10000.cpp @@ -83,9 +83,9 @@ private slots: QBENCHMARK { QDirIterator dit(testdir.path(), QDir::Files); while (dit.hasNext()) { - dit.next(); - dit.fileInfo().isDir(); - dit.fileInfo().size(); + const auto fi = dit.nextFileInfo(); + (void)fi.isDir(); + (void)fi.size(); } } } @@ -104,9 +104,9 @@ private slots: QBENCHMARK { QDirIterator dit(testdir.path()); while (dit.hasNext()) { - dit.next(); - dit.fileInfo().isDir(); - dit.fileInfo().size(); + const auto fi = dit.nextFileInfo(); + (void)fi.isDir(); + (void)fi.size(); } } } diff --git a/tests/benchmarks/corelib/io/qdir/tree/bench_qdir_tree.cpp b/tests/benchmarks/corelib/io/qdir/tree/bench_qdir_tree.cpp index 0abf8769a6..3a7db36ea2 100644 --- a/tests/benchmarks/corelib/io/qdir/tree/bench_qdir_tree.cpp +++ b/tests/benchmarks/corelib/io/qdir/tree/bench_qdir_tree.cpp @@ -211,9 +211,8 @@ private slots: int count = 0; QDirIterator iter(dirName, QDir::Files, QDirIterator::Subdirectories); while(iter.hasNext()) { - iter.next(); count++; - totalsize += iter.fileInfo().size(); + totalsize += iter.nextFileInfo().size(); } QCOMPARE(count, 1000); QCOMPARE(totalsize, expectedSize); diff --git a/tests/benchmarks/corelib/io/qdiriterator/main.cpp b/tests/benchmarks/corelib/io/qdiriterator/main.cpp index 8c180f8757..acf1ede368 100644 --- a/tests/benchmarks/corelib/io/qdiriterator/main.cpp +++ b/tests/benchmarks/corelib/io/qdiriterator/main.cpp @@ -189,13 +189,13 @@ void tst_qdiriterator::diriterator() QDirIterator::Subdirectories); while (dir.hasNext()) { - dir.next(); + const auto fi = dir.nextFileInfo(); //printf("%s\n", qPrintable(dir.fileName())); 0 && printf("%d %s\n", - dir.fileInfo().isDir(), - //qPrintable(dir.fileInfo().absoluteFilePath()), + fi.isDir(), + //qPrintable(fi.absoluteFilePath()), //qPrintable(dir.path()), - qPrintable(dir.filePath())); + qPrintable(fi.filePath())); ++c; } count = c; -- cgit v1.2.3