Use slice method where applicable

These code segments already have its boundary checked and can thus be faster.

PR #22411.
This commit is contained in:
Chocobo1
2025-03-15 14:58:59 +08:00
committed by GitHub
parent d174bc75e4
commit 5a4b3b25d3
23 changed files with 113 additions and 63 deletions

View File

@@ -122,7 +122,7 @@ namespace
fsPathEdit->setCurrentIndex(existingIndex);
}
void updatePathHistory(const QString &settingsKey, const Path &path, const int maxLength)
void updatePathHistory(const QString &settingsKey, const Path &path, const qsizetype maxLength)
{
// Add last used save path to the front of history
@@ -134,7 +134,10 @@ namespace
else
pathList.prepend(path.toString());
settings()->storeValue(settingsKey, QStringList(pathList.mid(0, maxLength)));
if (pathList.size() > maxLength)
pathList.resize(maxLength);
settings()->storeValue(settingsKey, pathList);
}
}
@@ -375,8 +378,7 @@ AddNewTorrentDialog::AddNewTorrentDialog(const BitTorrent::TorrentDescriptor &to
connect(Preferences::instance(), &Preferences::changed, []
{
const int length = Preferences::instance()->addNewTorrentDialogSavePathHistoryLength();
settings()->storeValue(KEY_SAVEPATHHISTORY
, QStringList(settings()->loadValue<QStringList>(KEY_SAVEPATHHISTORY).mid(0, length)));
settings()->storeValue(KEY_SAVEPATHHISTORY, settings()->loadValue<QStringList>(KEY_SAVEPATHHISTORY).mid(0, length));
});
setCurrentContext(std::make_shared<Context>(Context {torrentDescr, inParams}));

View File

@@ -234,14 +234,14 @@ void FileSystemPathEdit::setFileNameFilter(const QString &val)
const int closeBracePos = val.indexOf(u')', (openBracePos + 1));
if ((openBracePos > 0) && (closeBracePos > 0) && (closeBracePos > openBracePos + 2))
{
QString filterString = val.mid(openBracePos + 1, closeBracePos - openBracePos - 1);
const QString filterString = val.sliced((openBracePos + 1), (closeBracePos - openBracePos - 1));
if (filterString == u"*")
{ // no filters
d->m_editor->setFilenameFilters({});
}
else
{
QStringList filters = filterString.split(u' ', Qt::SkipEmptyParts);
const QStringList filters = filterString.split(u' ', Qt::SkipEmptyParts);
d->m_editor->setFilenameFilters(filters);
}
}

View File

@@ -1859,10 +1859,10 @@ void OptionsDialog::setLocale(const QString &localeStr)
if (index < 0)
{
//Attempt to find a language match without a country
int pos = name.indexOf(u'_');
const int pos = name.indexOf(u'_');
if (pos > -1)
{
QString lang = name.left(pos);
const QString lang = name.first(pos);
index = m_ui->comboLanguage->findData(lang, Qt::UserRole);
}
}

View File

@@ -82,7 +82,13 @@ namespace
QString relativePath = match.captured(6);
if (relativePath.startsWith(u'/'))
relativePath = relativePath.mid(1);
{
#if QT_VERSION >= QT_VERSION_CHECK(6, 8, 0)
relativePath.slice(1);
#else
relativePath.remove(0, 1);
#endif
}
const QString absoluteUrl = !host.isEmpty()
? QString(defaultScheme + u':' + host) : (normalizedBaseUrl + relativePath);

View File

@@ -46,7 +46,7 @@ void SearchSortModel::setNameFilter(const QString &searchTerm)
{
m_searchTerm = searchTerm;
if ((searchTerm.length() > 2) && searchTerm.startsWith(u'"') && searchTerm.endsWith(u'"'))
m_searchTermWords = QStringList(m_searchTerm.mid(1, m_searchTerm.length() - 2));
m_searchTermWords = QStringList(m_searchTerm.sliced(1, (m_searchTerm.length() - 2)));
else
m_searchTermWords = searchTerm.split(u' ', Qt::SkipEmptyParts);
}

View File

@@ -675,7 +675,7 @@ void SearchWidget::loadHistory()
}
if (history.size() > m_historyLength)
history = history.mid(history.size() - m_historyLength);
history.remove(0, (history.size() - m_historyLength));
m_searchPatternCompleterModel->setStringList(history);
});

View File

@@ -174,9 +174,9 @@ namespace
{
QString shortName(const QString &fullName)
{
int pos = fullName.lastIndexOf(u'/');
const int pos = fullName.lastIndexOf(u'/');
if (pos >= 0)
return fullName.mid(pos + 1);
return fullName.sliced(pos + 1);
return fullName;
}
}

View File

@@ -555,7 +555,7 @@ void TrackersFilterWidget::handleFavicoDownloadFinished(const Net::DownloadResul
{
if (result.url.endsWith(u".ico", Qt::CaseInsensitive))
{
const QString faviconURL = result.url.left(result.url.size() - 4) + u".png";
const QString faviconURL = QStringView(result.url).chopped(4) + u".png";
for (const auto &trackerHost : trackerHosts)
{
if (m_trackers.contains(trackerHost))