From 737797473135745b0d9caad030470c167b7fdf85 Mon Sep 17 00:00:00 2001 From: Vladimir Golovnev Date: Thu, 14 Apr 2022 09:43:07 +0300 Subject: [PATCH] Improve path extension handling PR #16867. --- src/base/bittorrent/customstorage.cpp | 6 ++---- src/base/bittorrent/torrentimpl.cpp | 8 +++----- src/base/path.cpp | 9 +++++++-- src/base/path.h | 5 +++-- src/base/search/searchpluginmanager.cpp | 3 +-- 5 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/base/bittorrent/customstorage.cpp b/src/base/bittorrent/customstorage.cpp index 9466db666..c2934421d 100644 --- a/src/base/bittorrent/customstorage.cpp +++ b/src/base/bittorrent/customstorage.cpp @@ -208,8 +208,7 @@ void CustomDiskIOThread::handleCompleteFiles(lt::storage_index_t storage, const if (filePath.hasExtension(QB_EXT)) { const Path incompleteFilePath = savePath / filePath; - Path completeFilePath = incompleteFilePath; - completeFilePath.removeExtension(QB_EXT); + const Path completeFilePath = incompleteFilePath.removedExtension(QB_EXT); if (completeFilePath.exists()) { Utils::Fs::removeFile(incompleteFilePath); @@ -274,8 +273,7 @@ void CustomStorage::handleCompleteFiles(const Path &savePath) if (filePath.hasExtension(QB_EXT)) { const Path incompleteFilePath = savePath / filePath; - Path completeFilePath = incompleteFilePath; - completeFilePath.removeExtension(); + const Path completeFilePath = incompleteFilePath.removedExtension(QB_EXT); if (completeFilePath.exists()) { Utils::Fs::removeFile(incompleteFilePath); diff --git a/src/base/bittorrent/torrentimpl.cpp b/src/base/bittorrent/torrentimpl.cpp index 174bb91e0..06454a680 100644 --- a/src/base/bittorrent/torrentimpl.cpp +++ b/src/base/bittorrent/torrentimpl.cpp @@ -294,8 +294,7 @@ TorrentImpl::TorrentImpl(Session *session, lt::session *nativeSession const lt::file_index_t nativeIndex = m_torrentInfo.nativeIndexes().at(i); m_indexMap[nativeIndex] = i; - Path filePath {fileStorage.file_path(nativeIndex)}; - filePath.removeExtension(QB_EXT); + const auto filePath = Path(fileStorage.file_path(nativeIndex)).removedExtension(QB_EXT); m_filePaths.append(filePath); const auto priority = LT::fromNative(filePriorities[LT::toUnderlyingType(nativeIndex)]); @@ -1520,11 +1519,10 @@ void TorrentImpl::endReceivedMetadataHandling(const Path &savePath, const PathLi { const auto nativeIndex = nativeIndexes.at(i); - Path filePath = fileNames.at(i); + const Path filePath = fileNames.at(i); p.renamed_files[nativeIndex] = filePath.toString().toStdString(); - filePath.removeExtension(QB_EXT); - m_filePaths.append(filePath); + m_filePaths.append(filePath.removedExtension(QB_EXT)); const auto priority = LT::fromNative(filePriorities[LT::toUnderlyingType(nativeIndex)]); m_filePriorities.append(priority); diff --git a/src/base/path.cpp b/src/base/path.cpp index 966cdc32b..0fa65d11a 100644 --- a/src/base/path.cpp +++ b/src/base/path.cpp @@ -151,7 +151,7 @@ QString Path::extension() const return ((dotIndex == -1) ? QString() : filename.mid(dotIndex).toString()); } -bool Path::hasExtension(const QString &ext) const +bool Path::hasExtension(const QStringView ext) const { Q_ASSERT(ext.startsWith(u'.') && (ext.size() >= 2)); @@ -186,12 +186,17 @@ Path Path::removedExtension() const return createUnchecked(m_pathStr.chopped(extension().size())); } -void Path::removeExtension(const QString &ext) +void Path::removeExtension(const QStringView ext) { if (hasExtension(ext)) m_pathStr.chop(ext.size()); } +Path Path::removedExtension(const QStringView ext) const +{ + return (hasExtension(ext) ? createUnchecked(m_pathStr.chopped(ext.size())) : *this); +} + QString Path::data() const { return m_pathStr; diff --git a/src/base/path.h b/src/base/path.h index d372471a1..6fbb94604 100644 --- a/src/base/path.h +++ b/src/base/path.h @@ -58,10 +58,11 @@ public: QString filename() const; QString extension() const; - bool hasExtension(const QString &ext) const; + bool hasExtension(QStringView ext) const; void removeExtension(); Path removedExtension() const; - void removeExtension(const QString &ext); + void removeExtension(QStringView ext); + Path removedExtension(QStringView ext) const; bool hasAncestor(const Path &other) const; Path relativePathOf(const Path &childPath) const; diff --git a/src/base/search/searchpluginmanager.cpp b/src/base/search/searchpluginmanager.cpp index b2c72de02..2e4ad2348 100644 --- a/src/base/search/searchpluginmanager.cpp +++ b/src/base/search/searchpluginmanager.cpp @@ -392,8 +392,7 @@ void SearchPluginManager::pluginDownloadFinished(const Net::DownloadResult &resu { const Path filePath = result.filePath; - Path pluginPath {QUrl(result.url).path()}; - pluginPath.removeExtension(); // Remove extension + const auto pluginPath = Path(QUrl(result.url).path()).removedExtension(); installPlugin_impl(pluginPath.filename(), filePath); Utils::Fs::removeFile(filePath); }