From b5b678c58f708b23300cb87a51e8c1e6ffdf0927 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Tue, 6 Aug 2019 23:07:57 +0800 Subject: [PATCH] Replace QString::split() by faster alternatives --- src/base/bittorrent/session.cpp | 6 +++-- src/base/bittorrent/torrentinfo.cpp | 7 +++--- src/base/http/connection.cpp | 10 ++++---- src/base/search/searchhandler.cpp | 21 ++++++++++------ src/base/search/searchpluginmanager.cpp | 6 +++-- src/base/utils/misc.cpp | 2 +- src/gui/downloadfromurldialog.cpp | 17 +++++++------ src/gui/mainwindow.cpp | 25 ++++++++++--------- src/gui/programupdater.cpp | 8 +++--- src/gui/properties/trackersadditiondialog.cpp | 6 +++-- src/gui/search/pluginselectdialog.cpp | 2 +- src/gui/torrentcontentmodel.cpp | 14 +++++++---- 12 files changed, 72 insertions(+), 52 deletions(-) diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index a049eb478..a17d3a167 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -1501,10 +1501,12 @@ void Session::enableBandwidthScheduler() void Session::populateAdditionalTrackers() { m_additionalTrackerList.clear(); - for (QString tracker : asConst(additionalTrackers().split('\n'))) { + + const QString trackers = additionalTrackers(); + for (QStringRef tracker : asConst(trackers.splitRef('\n'))) { tracker = tracker.trimmed(); if (!tracker.isEmpty()) - m_additionalTrackerList << tracker; + m_additionalTrackerList << tracker.toString(); } } diff --git a/src/base/bittorrent/torrentinfo.cpp b/src/base/bittorrent/torrentinfo.cpp index 8f128dce2..3ce4c9cc6 100644 --- a/src/base/bittorrent/torrentinfo.cpp +++ b/src/base/bittorrent/torrentinfo.cpp @@ -429,9 +429,10 @@ void TorrentInfo::stripRootFolder() lt::file_storage files = m_nativeInfo->files(); // Solution for case of renamed root folder - const std::string testName = filePath(0).split('/').value(0).toStdString(); - if (files.name() != testName) { - files.set_name(testName); + const QString path = filePath(0); + const std::string newName = path.left(path.indexOf('/')).toStdString(); + if (files.name() != newName) { + files.set_name(newName); for (int i = 0; i < files.num_files(); ++i) files.rename_file(LTFileIndex {i}, files.file_path(LTFileIndex {i})); } diff --git a/src/base/http/connection.cpp b/src/base/http/connection.cpp index 27cb53c5c..d570fc32b 100644 --- a/src/base/http/connection.cpp +++ b/src/base/http/connection.cpp @@ -131,9 +131,9 @@ bool Connection::acceptsGzipEncoding(QString codings) { // [rfc7231] 5.3.4. Accept-Encoding - const auto isCodingAvailable = [](const QStringList &list, const QString &encoding) -> bool + const auto isCodingAvailable = [](const QVector &list, const QString &encoding) -> bool { - for (const QString &str : list) { + for (const QStringRef &str : list) { if (!str.startsWith(encoding)) continue; @@ -142,11 +142,11 @@ bool Connection::acceptsGzipEncoding(QString codings) return true; // [rfc7231] 5.3.1. Quality Values - const QStringRef substr = str.midRef(encoding.size() + 3); // ex. skip over "gzip;q=" + const QStringRef substr = str.mid(encoding.size() + 3); // ex. skip over "gzip;q=" bool ok = false; const double qvalue = substr.toDouble(&ok); - if (!ok || (qvalue <= 0.0)) + if (!ok || (qvalue <= 0)) return false; return true; @@ -154,7 +154,7 @@ bool Connection::acceptsGzipEncoding(QString codings) return false; }; - const QStringList list = codings.remove(' ').remove('\t').split(',', QString::SkipEmptyParts); + const QVector list = codings.remove(' ').remove('\t').splitRef(',', QString::SkipEmptyParts); if (list.isEmpty()) return false; diff --git a/src/base/search/searchhandler.cpp b/src/base/search/searchhandler.cpp index 5194c5007..903fe46d0 100644 --- a/src/base/search/searchhandler.cpp +++ b/src/base/search/searchhandler.cpp @@ -32,9 +32,9 @@ #include #include -#include "../global.h" -#include "../utils/foreignapps.h" -#include "../utils/fs.h" +#include "base/global.h" +#include "base/utils/foreignapps.h" +#include "base/utils/fs.h" #include "searchpluginmanager.h" namespace @@ -161,24 +161,29 @@ void SearchHandler::processFailed() // file url | file name | file size | nb seeds | nb leechers | Search engine url bool SearchHandler::parseSearchResult(const QString &line, SearchResult &searchResult) { - const QStringList parts = line.split('|'); + const QVector parts = line.splitRef('|'); const int nbFields = parts.size(); + if (nbFields < (NB_PLUGIN_COLUMNS - 1)) return false; // -1 because desc_link is optional searchResult = SearchResult(); - searchResult.fileUrl = parts.at(PL_DL_LINK).trimmed(); // download URL - searchResult.fileName = parts.at(PL_NAME).trimmed(); // Name + searchResult.fileUrl = parts.at(PL_DL_LINK).trimmed().toString(); // download URL + searchResult.fileName = parts.at(PL_NAME).trimmed().toString(); // Name searchResult.fileSize = parts.at(PL_SIZE).trimmed().toLongLong(); // Size + bool ok = false; + searchResult.nbSeeders = parts.at(PL_SEEDS).trimmed().toLongLong(&ok); // Seeders if (!ok || (searchResult.nbSeeders < 0)) searchResult.nbSeeders = -1; + searchResult.nbLeechers = parts.at(PL_LEECHS).trimmed().toLongLong(&ok); // Leechers if (!ok || (searchResult.nbLeechers < 0)) searchResult.nbLeechers = -1; - searchResult.siteUrl = parts.at(PL_ENGINE_URL).trimmed(); // Search site URL + + searchResult.siteUrl = parts.at(PL_ENGINE_URL).trimmed().toString(); // Search site URL if (nbFields == NB_PLUGIN_COLUMNS) - searchResult.descrLink = parts.at(PL_DESC_LINK).trimmed(); // Description Link + searchResult.descrLink = parts.at(PL_DESC_LINK).trimmed().toString(); // Description Link return true; } diff --git a/src/base/search/searchpluginmanager.cpp b/src/base/search/searchpluginmanager.cpp index 28dfe948a..ce0a777a3 100644 --- a/src/base/search/searchpluginmanager.cpp +++ b/src/base/search/searchpluginmanager.cpp @@ -376,8 +376,10 @@ void SearchPluginManager::pluginDownloadFinished(const Net::DownloadResult &resu Utils::Fs::forceRemove(filePath); } else { - QString pluginName = result.url.split('/').last(); + const QString url = result.url; + QString pluginName = url.mid(url.lastIndexOf('/') + 1); pluginName.replace(".py", "", Qt::CaseInsensitive); + if (pluginInfo(pluginName)) emit pluginUpdateFailed(pluginName, tr("Failed to download the plugin file. %1").arg(result.errorString)); else @@ -462,7 +464,7 @@ void SearchPluginManager::update() plugin->fullName = engineElem.elementsByTagName("name").at(0).toElement().text(); plugin->url = engineElem.elementsByTagName("url").at(0).toElement().text(); - const auto categories = engineElem.elementsByTagName("categories").at(0).toElement().text().split(' '); + const QStringList categories = engineElem.elementsByTagName("categories").at(0).toElement().text().split(' '); for (QString cat : categories) { cat = cat.trimmed(); if (!cat.isEmpty()) diff --git a/src/base/utils/misc.cpp b/src/base/utils/misc.cpp index e9f089918..fa91e0667 100644 --- a/src/base/utils/misc.cpp +++ b/src/base/utils/misc.cpp @@ -465,7 +465,7 @@ QString Utils::Misc::libtorrentVersionString() QString Utils::Misc::opensslVersionString() { const QString version {OPENSSL_VERSION_TEXT}; - return version.split(' ', QString::SkipEmptyParts)[1]; + return version.splitRef(' ', QString::SkipEmptyParts)[1].toString(); } QString Utils::Misc::zlibVersionString() diff --git a/src/gui/downloadfromurldialog.cpp b/src/gui/downloadfromurldialog.cpp index e1d690d46..ebd6b8d43 100644 --- a/src/gui/downloadfromurldialog.cpp +++ b/src/gui/downloadfromurldialog.cpp @@ -67,13 +67,15 @@ DownloadFromURLDialog::DownloadFromURLDialog(QWidget *parent) m_ui->textUrls->setWordWrapMode(QTextOption::NoWrap); // Paste clipboard if there is an URL in it - const QStringList clipboardList = qApp->clipboard()->text().split('\n'); + const QString clipboardText = qApp->clipboard()->text(); + const QVector clipboardList = clipboardText.splitRef('\n'); QSet uniqueURLs; - for (QString str : clipboardList) { - str = str.trimmed(); - if (str.isEmpty()) continue; + for (QStringRef strRef : clipboardList) { + strRef = strRef.trimmed(); + if (strRef.isEmpty()) continue; + const QString str = strRef.toString(); if (isDownloadable(str)) uniqueURLs << str; } @@ -90,14 +92,15 @@ DownloadFromURLDialog::~DownloadFromURLDialog() void DownloadFromURLDialog::downloadButtonClicked() { - const QStringList urls = m_ui->textUrls->toPlainText().split('\n'); + const QString plainText = m_ui->textUrls->toPlainText(); + const QVector urls = plainText.splitRef('\n'); QSet uniqueURLs; - for (QString url : urls) { + for (QStringRef url : urls) { url = url.trimmed(); if (url.isEmpty()) continue; - uniqueURLs << url; + uniqueURLs << url.toString(); } if (uniqueURLs.isEmpty()) { diff --git a/src/gui/mainwindow.cpp b/src/gui/mainwindow.cpp index 44652c1c7..76dd2bb54 100644 --- a/src/gui/mainwindow.cpp +++ b/src/gui/mainwindow.cpp @@ -1354,21 +1354,22 @@ void MainWindow::on_actionOpen_triggered() QFileDialog::getOpenFileNames(this, tr("Open Torrent Files"), pref->getMainLastDir(), tr("Torrent Files") + " (*" + C_TORRENT_FILE_EXTENSION + ')'); + if (pathsList.isEmpty()) + return; + const bool useTorrentAdditionDialog = AddNewTorrentDialog::isEnabled(); - if (!pathsList.isEmpty()) { - for (const QString &file : pathsList) { - qDebug("Dropped file %s on download list", qUtf8Printable(file)); - if (useTorrentAdditionDialog) - AddNewTorrentDialog::show(file, this); - else - BitTorrent::Session::instance()->addTorrent(file); - } - // Save last dir to remember it - QStringList topDir = Utils::Fs::toUniformPath(pathsList.at(0)).split('/'); - topDir.removeLast(); - pref->setMainLastDir(Utils::Fs::toUniformPath(topDir.join('/'))); + for (const QString &file : pathsList) { + if (useTorrentAdditionDialog) + AddNewTorrentDialog::show(file, this); + else + BitTorrent::Session::instance()->addTorrent(file); } + + // Save last dir to remember it + QString topDir = Utils::Fs::toUniformPath(pathsList.at(0)); + topDir = topDir.left(topDir.lastIndexOf('/')); + pref->setMainLastDir(topDir); } void MainWindow::activate() diff --git a/src/gui/programupdater.cpp b/src/gui/programupdater.cpp index 95c8e5ee2..9ac1311c6 100644 --- a/src/gui/programupdater.cpp +++ b/src/gui/programupdater.cpp @@ -139,10 +139,10 @@ bool ProgramUpdater::isVersionMoreRecent(const QString &remoteVersion) const { const QRegularExpressionMatch regVerMatch = QRegularExpression("([0-9.]+)").match(QBT_VERSION); if (regVerMatch.hasMatch()) { - QString localVersion = regVerMatch.captured(1); - qDebug() << Q_FUNC_INFO << "local version:" << localVersion << "/" << QBT_VERSION; - QStringList remoteParts = remoteVersion.split('.'); - QStringList localParts = localVersion.split('.'); + const QString localVersion = regVerMatch.captured(1); + const QVector remoteParts = remoteVersion.splitRef('.'); + const QVector localParts = localVersion.splitRef('.'); + for (int i = 0; i < qMin(remoteParts.size(), localParts.size()); ++i) { if (remoteParts[i].toInt() > localParts[i].toInt()) return true; diff --git a/src/gui/properties/trackersadditiondialog.cpp b/src/gui/properties/trackersadditiondialog.cpp index 6d5feeee1..92e00d1fc 100644 --- a/src/gui/properties/trackersadditiondialog.cpp +++ b/src/gui/properties/trackersadditiondialog.cpp @@ -56,11 +56,13 @@ TrackersAdditionDialog::~TrackersAdditionDialog() QStringList TrackersAdditionDialog::newTrackers() const { + const QString plainText = m_ui->textEditTrackersList->toPlainText(); + QStringList cleanTrackers; - for (QString url : asConst(m_ui->textEditTrackersList->toPlainText().split('\n'))) { + for (QStringRef url : asConst(plainText.splitRef('\n'))) { url = url.trimmed(); if (!url.isEmpty()) - cleanTrackers << url; + cleanTrackers << url.toString(); } return cleanTrackers; } diff --git a/src/gui/search/pluginselectdialog.cpp b/src/gui/search/pluginselectdialog.cpp index e6395c377..7bfa2a6d3 100644 --- a/src/gui/search/pluginselectdialog.cpp +++ b/src/gui/search/pluginselectdialog.cpp @@ -119,7 +119,7 @@ void PluginSelectDialog::dropEvent(QDropEvent *event) } } else { - files = event->mimeData()->text().split(QLatin1String("\n")); + files = event->mimeData()->text().split('\n'); } if (files.isEmpty()) return; diff --git a/src/gui/torrentcontentmodel.cpp b/src/gui/torrentcontentmodel.cpp index 5c81f94e3..e2ff20a04 100644 --- a/src/gui/torrentcontentmodel.cpp +++ b/src/gui/torrentcontentmodel.cpp @@ -469,14 +469,18 @@ void TorrentContentModel::setupModelData(const BitTorrent::TorrentInfo &info) // Iterate over files for (int i = 0; i < filesCount; ++i) { currentParent = m_rootItem; - QString path = Utils::Fs::toUniformPath(info.filePath(i)); + const QString path = Utils::Fs::toUniformPath(info.filePath(i)); + // Iterate of parts of the path to create necessary folders - QStringList pathFolders = path.split('/', QString::SkipEmptyParts); + QVector pathFolders = path.splitRef('/', QString::SkipEmptyParts); pathFolders.removeLast(); - for (const QString &pathPart : asConst(pathFolders)) { - if (pathPart == ".unwanted") + + for (const QStringRef &pathPartRef : asConst(pathFolders)) { + if (pathPartRef == QLatin1String(".unwanted")) continue; - TorrentContentModelFolder* newParent = currentParent->childFolderWithName(pathPart); + + const QString pathPart = pathPartRef.toString(); + TorrentContentModelFolder *newParent = currentParent->childFolderWithName(pathPart); if (!newParent) { newParent = new TorrentContentModelFolder(pathPart, currentParent); currentParent->appendChild(newParent);