From 293479a1f24e13a8d3d5fe78fbcfba3da6566139 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Mon, 14 Feb 2022 11:47:29 +0300 Subject: [PATCH 1/2] Improve performance of checking path extension --- src/base/path.cpp | 4 +++- src/gui/uithememanager.cpp | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/base/path.cpp b/src/base/path.cpp index 8f72c97ba..248fafb45 100644 --- a/src/base/path.cpp +++ b/src/base/path.cpp @@ -155,7 +155,9 @@ QString Path::extension() const bool Path::hasExtension(const QString &ext) const { - return (extension().compare(ext, Qt::CaseInsensitive) == 0); + Q_ASSERT(ext.startsWith(QLatin1Char('.'))); + + return m_pathStr.endsWith(ext, Qt::CaseInsensitive); } bool Path::hasAncestor(const Path &other) const diff --git a/src/gui/uithememanager.cpp b/src/gui/uithememanager.cpp index 295c92d97..4f033b63f 100644 --- a/src/gui/uithememanager.cpp +++ b/src/gui/uithememanager.cpp @@ -141,7 +141,7 @@ namespace if (themePath.filename() == CONFIG_FILE_NAME) return std::make_unique(themePath); - if ((themePath.extension() == QLatin1String(".qbtheme")) + if ((themePath.hasExtension(QLatin1String(".qbtheme"))) && QResource::registerResource(themePath.data(), QLatin1String("/uitheme"))) { return std::make_unique(); From 1e45b7f50baa00675287e0e143d7e78975f130a9 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Tue, 15 Feb 2022 09:31:35 +0300 Subject: [PATCH 2/2] Add fast way of removing suggested extension --- src/base/bittorrent/customstorage.cpp | 2 +- src/base/bittorrent/torrentimpl.cpp | 6 ++---- src/base/path.cpp | 10 ++++++++-- src/base/path.h | 1 + 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/src/base/bittorrent/customstorage.cpp b/src/base/bittorrent/customstorage.cpp index e6c24785b..9466db666 100644 --- a/src/base/bittorrent/customstorage.cpp +++ b/src/base/bittorrent/customstorage.cpp @@ -209,7 +209,7 @@ void CustomDiskIOThread::handleCompleteFiles(lt::storage_index_t storage, const { const Path incompleteFilePath = savePath / filePath; Path completeFilePath = incompleteFilePath; - completeFilePath.removeExtension(); + completeFilePath.removeExtension(QB_EXT); if (completeFilePath.exists()) { Utils::Fs::removeFile(incompleteFilePath); diff --git a/src/base/bittorrent/torrentimpl.cpp b/src/base/bittorrent/torrentimpl.cpp index e90574808..c53af8777 100644 --- a/src/base/bittorrent/torrentimpl.cpp +++ b/src/base/bittorrent/torrentimpl.cpp @@ -294,8 +294,7 @@ TorrentImpl::TorrentImpl(Session *session, lt::session *nativeSession m_indexMap[nativeIndex] = i; Path filePath {fileStorage.file_path(nativeIndex)}; - if (filePath.hasExtension(QB_EXT)) - filePath.removeExtension(); + filePath.removeExtension(QB_EXT); m_filePaths.append(filePath); const auto priority = LT::fromNative(filePriorities[LT::toUnderlyingType(nativeIndex)]); @@ -1511,8 +1510,7 @@ void TorrentImpl::endReceivedMetadataHandling(const Path &savePath, const PathLi Path filePath = fileNames.at(i); p.renamed_files[nativeIndex] = filePath.toString().toStdString(); - if (filePath.hasExtension(QB_EXT)) - filePath.removeExtension(); + filePath.removeExtension(QB_EXT); m_filePaths.append(filePath); const auto priority = LT::fromNative(filePriorities[LT::toUnderlyingType(nativeIndex)]); diff --git a/src/base/path.cpp b/src/base/path.cpp index 248fafb45..292163c98 100644 --- a/src/base/path.cpp +++ b/src/base/path.cpp @@ -149,13 +149,13 @@ QString Path::extension() const const int slashIndex = m_pathStr.lastIndexOf(QLatin1Char('/')); const auto filename = QStringView(m_pathStr).mid(slashIndex + 1); - const int dotIndex = filename.lastIndexOf(QLatin1Char('.')); + const int dotIndex = filename.lastIndexOf(QLatin1Char('.'), -2); return ((dotIndex == -1) ? QString() : filename.mid(dotIndex).toString()); } bool Path::hasExtension(const QString &ext) const { - Q_ASSERT(ext.startsWith(QLatin1Char('.'))); + Q_ASSERT(ext.startsWith(QLatin1Char('.')) && (ext.size() >= 2)); return m_pathStr.endsWith(ext, Qt::CaseInsensitive); } @@ -183,6 +183,12 @@ void Path::removeExtension() m_pathStr.chop(extension().size()); } +void Path::removeExtension(const QString &ext) +{ + if (hasExtension(ext)) + m_pathStr.chop(ext.size()); +} + QString Path::data() const { return m_pathStr; diff --git a/src/base/path.h b/src/base/path.h index 0a4d7c1f8..1abef4e02 100644 --- a/src/base/path.h +++ b/src/base/path.h @@ -58,6 +58,7 @@ public: QString extension() const; bool hasExtension(const QString &ext) const; void removeExtension(); + void removeExtension(const QString &ext); bool hasAncestor(const Path &other) const; Path relativePathOf(const Path &childPath) const;