forked from qt-creator/qt-creator
Utils: Improve FileUtils::commonPath
Make it a worthy FilePath-based counterpart of Utils::commonPath. With tests. Change-Id: I68bae129c5d9d9dd3e46dfa6093e229f8ca0aee1 Reviewed-by: Jarek Kobus <jaroslaw.kobus@qt.io> Reviewed-by: <github-actions-qt-creator@cristianadam.eu> Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
This commit is contained in:
@@ -385,6 +385,52 @@ static QByteArray fileIdWin(HANDLE fHandle)
|
||||
}
|
||||
#endif
|
||||
|
||||
FilePath FileUtils::commonPath(const FilePaths &paths)
|
||||
{
|
||||
if (paths.isEmpty())
|
||||
return {};
|
||||
|
||||
if (paths.count() == 1)
|
||||
return paths.constFirst();
|
||||
|
||||
const FilePath &first = paths.constFirst();
|
||||
const FilePaths others = paths.mid(1);
|
||||
FilePath result;
|
||||
|
||||
// Common scheme
|
||||
const QString &commonScheme = first.scheme();
|
||||
auto sameScheme = [&commonScheme] (const FilePath &fp) {
|
||||
return commonScheme == fp.scheme();
|
||||
};
|
||||
if (!allOf(others, sameScheme))
|
||||
return result;
|
||||
result.setScheme(commonScheme);
|
||||
|
||||
// Common host
|
||||
const QString &commonHost = first.host();
|
||||
auto sameHost = [&commonHost] (const FilePath &fp) {
|
||||
return commonHost == fp.host();
|
||||
};
|
||||
if (!allOf(others, sameHost))
|
||||
return result;
|
||||
result.setHost(commonHost);
|
||||
|
||||
// Common path
|
||||
QString commonPath;
|
||||
auto sameBasePath = [&commonPath] (const FilePath &fp) {
|
||||
return QString(fp.path() + '/').startsWith(commonPath);
|
||||
};
|
||||
const QStringList pathSegments = first.path().split('/');
|
||||
for (const QString &segment : pathSegments) {
|
||||
commonPath += segment + '/';
|
||||
if (!allOf(others, sameBasePath))
|
||||
return result;
|
||||
result.setPath(commonPath.chopped(1));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
QByteArray FileUtils::fileId(const FilePath &fileName)
|
||||
{
|
||||
QByteArray result;
|
||||
|
||||
Reference in New Issue
Block a user