Browse Source

Merge pull request #16444 from glassez/path-has-ext

Improve performance of checking path extension
adaptive-webui-19844
Vladimir Golovnev 3 years ago committed by GitHub
parent
commit
70ec183fca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      src/base/bittorrent/customstorage.cpp
  2. 6
      src/base/bittorrent/torrentimpl.cpp
  3. 12
      src/base/path.cpp
  4. 1
      src/base/path.h
  5. 2
      src/gui/uithememanager.cpp

2
src/base/bittorrent/customstorage.cpp

@ -209,7 +209,7 @@ void CustomDiskIOThread::handleCompleteFiles(lt::storage_index_t storage, const
{ {
const Path incompleteFilePath = savePath / filePath; const Path incompleteFilePath = savePath / filePath;
Path completeFilePath = incompleteFilePath; Path completeFilePath = incompleteFilePath;
completeFilePath.removeExtension(); completeFilePath.removeExtension(QB_EXT);
if (completeFilePath.exists()) if (completeFilePath.exists())
{ {
Utils::Fs::removeFile(incompleteFilePath); Utils::Fs::removeFile(incompleteFilePath);

6
src/base/bittorrent/torrentimpl.cpp

@ -294,8 +294,7 @@ TorrentImpl::TorrentImpl(Session *session, lt::session *nativeSession
m_indexMap[nativeIndex] = i; m_indexMap[nativeIndex] = i;
Path filePath {fileStorage.file_path(nativeIndex)}; Path filePath {fileStorage.file_path(nativeIndex)};
if (filePath.hasExtension(QB_EXT)) filePath.removeExtension(QB_EXT);
filePath.removeExtension();
m_filePaths.append(filePath); m_filePaths.append(filePath);
const auto priority = LT::fromNative(filePriorities[LT::toUnderlyingType(nativeIndex)]); 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); Path filePath = fileNames.at(i);
p.renamed_files[nativeIndex] = filePath.toString().toStdString(); p.renamed_files[nativeIndex] = filePath.toString().toStdString();
if (filePath.hasExtension(QB_EXT)) filePath.removeExtension(QB_EXT);
filePath.removeExtension();
m_filePaths.append(filePath); m_filePaths.append(filePath);
const auto priority = LT::fromNative(filePriorities[LT::toUnderlyingType(nativeIndex)]); const auto priority = LT::fromNative(filePriorities[LT::toUnderlyingType(nativeIndex)]);

12
src/base/path.cpp

@ -149,13 +149,15 @@ QString Path::extension() const
const int slashIndex = m_pathStr.lastIndexOf(QLatin1Char('/')); const int slashIndex = m_pathStr.lastIndexOf(QLatin1Char('/'));
const auto filename = QStringView(m_pathStr).mid(slashIndex + 1); 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()); return ((dotIndex == -1) ? QString() : filename.mid(dotIndex).toString());
} }
bool Path::hasExtension(const QString &ext) const bool Path::hasExtension(const QString &ext) const
{ {
return (extension().compare(ext, Qt::CaseInsensitive) == 0); Q_ASSERT(ext.startsWith(QLatin1Char('.')) && (ext.size() >= 2));
return m_pathStr.endsWith(ext, Qt::CaseInsensitive);
} }
bool Path::hasAncestor(const Path &other) const bool Path::hasAncestor(const Path &other) const
@ -181,6 +183,12 @@ void Path::removeExtension()
m_pathStr.chop(extension().size()); m_pathStr.chop(extension().size());
} }
void Path::removeExtension(const QString &ext)
{
if (hasExtension(ext))
m_pathStr.chop(ext.size());
}
QString Path::data() const QString Path::data() const
{ {
return m_pathStr; return m_pathStr;

1
src/base/path.h

@ -58,6 +58,7 @@ public:
QString extension() const; QString extension() const;
bool hasExtension(const QString &ext) const; bool hasExtension(const QString &ext) const;
void removeExtension(); void removeExtension();
void removeExtension(const QString &ext);
bool hasAncestor(const Path &other) const; bool hasAncestor(const Path &other) const;
Path relativePathOf(const Path &childPath) const; Path relativePathOf(const Path &childPath) const;

2
src/gui/uithememanager.cpp

@ -141,7 +141,7 @@ namespace
if (themePath.filename() == CONFIG_FILE_NAME) if (themePath.filename() == CONFIG_FILE_NAME)
return std::make_unique<FolderThemeSource>(themePath); return std::make_unique<FolderThemeSource>(themePath);
if ((themePath.extension() == QLatin1String(".qbtheme")) if ((themePath.hasExtension(QLatin1String(".qbtheme")))
&& QResource::registerResource(themePath.data(), QLatin1String("/uitheme"))) && QResource::registerResource(themePath.data(), QLatin1String("/uitheme")))
{ {
return std::make_unique<QRCThemeSource>(); return std::make_unique<QRCThemeSource>();

Loading…
Cancel
Save