FilePath: Improve speed of ::relativePathFrom

Change-Id: Ie7aef2aeb01251edd6825fcd56a73ab65b57cbf5
Reviewed-by: hjk <hjk@qt.io>
This commit is contained in:
Marcus Tillmanns
2024-04-02 15:51:53 +02:00
parent 1c905c6980
commit d3279f1e0b

View File

@@ -19,9 +19,11 @@
#include <QFileInfo> #include <QFileInfo>
#include <QRegularExpression> #include <QRegularExpression>
#include <QStringView> #include <QStringView>
#include <QUrl>
#include <QtGlobal>
#include <QTemporaryFile> #include <QTemporaryFile>
#include <QUrl>
#include <QtConcurrent>
#include <QtGlobal>
#ifdef Q_OS_WIN #ifdef Q_OS_WIN
#ifdef QTCREATOR_PCH_H #ifdef QTCREATOR_PCH_H
@@ -1497,21 +1499,28 @@ FilePath FilePath::relativePathFrom(const FilePath &anchor) const
FilePath absPath; FilePath absPath;
QString filename; QString filename;
if (isFile()) {
const QList<FilePathInfo> infos
= QtConcurrent::blockingMapped(QList<FilePath>{*this, anchor}, [](const FilePath &path) {
return path.filePathInfo();
});
if (infos.first().fileFlags.testFlag(FilePathInfo::FileFlag::FileType)) {
absPath = absolutePath(); absPath = absolutePath();
filename = fileName(); filename = fileName();
} else if (isDir()) { } else if (infos.first().fileFlags.testFlag(FilePathInfo::FileFlag::DirectoryType)) {
absPath = absoluteFilePath(); absPath = absoluteFilePath();
} else { } else {
return {}; return {};
} }
FilePath absoluteAnchorPath; FilePath absoluteAnchorPath;
if (anchor.isFile()) if (infos.last().fileFlags.testFlag(FilePathInfo::FileFlag::FileType))
absoluteAnchorPath = anchor.absolutePath(); absoluteAnchorPath = anchor.absolutePath();
else if (anchor.isDir()) else if (infos.last().fileFlags.testFlag(FilePathInfo::FileFlag::DirectoryType))
absoluteAnchorPath = anchor.absoluteFilePath(); absoluteAnchorPath = anchor.absoluteFilePath();
else else
return {}; return {};
QString relativeFilePath = calcRelativePath(absPath.pathView(), absoluteAnchorPath.pathView()); QString relativeFilePath = calcRelativePath(absPath.pathView(), absoluteAnchorPath.pathView());
if (!filename.isEmpty()) { if (!filename.isEmpty()) {
if (relativeFilePath == ".") if (relativeFilePath == ".")