Avoid redundant string length scan

PR #21807.
This commit is contained in:
Chocobo1 2024-11-11 19:19:10 +08:00 committed by GitHub
parent 889df72ab3
commit 92daca1fef
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 23 additions and 10 deletions

View file

@ -86,6 +86,7 @@
#include "base/utils/net.h"
#include "base/utils/number.h"
#include "base/utils/random.h"
#include "base/utils/string.h"
#include "base/version.h"
#include "bandwidthscheduler.h"
#include "bencoderesumedatastorage.h"
@ -222,7 +223,7 @@ namespace
{
try
{
return QString::fromLatin1(address.to_string().c_str());
return Utils::String::fromLatin1(address.to_string());
}
catch (const std::exception &)
{
@ -5831,7 +5832,7 @@ void SessionImpl::handleTorrentDeleteFailedAlert(const lt::torrent_delete_failed
#else
const auto torrentID = TorrentID::fromInfoHash(alert->info_hash);
#endif
const auto errorMessage = alert->error ? QString::fromLocal8Bit(alert->error.message().c_str()) : QString();
const auto errorMessage = alert->error ? Utils::String::fromLocal8Bit(alert->error.message()) : QString();
handleRemovedTorrent(torrentID, errorMessage);
}
@ -5989,7 +5990,7 @@ void SessionImpl::handleListenFailedAlert(const lt::listen_failed_alert *alert)
const QString proto {toString(alert->socket_type)};
LogMsg(tr("Failed to listen on IP. IP: \"%1\". Port: \"%2/%3\". Reason: \"%4\"")
.arg(toString(alert->address), proto, QString::number(alert->port)
, QString::fromLocal8Bit(alert->error.message().c_str())), Log::CRITICAL);
, Utils::String::fromLocal8Bit(alert->error.message())), Log::CRITICAL);
}
void SessionImpl::handleExternalIPAlert(const lt::external_ip_alert *alert)
@ -6215,7 +6216,7 @@ void SessionImpl::handleSocks5Alert(const lt::socks5_alert *alert) const
const QString endpoint = (addr.is_v6() ? u"[%1]:%2"_s : u"%1:%2"_s)
.arg(QString::fromStdString(addr.to_string()), QString::number(alert->ip.port()));
LogMsg(tr("SOCKS5 proxy error. Address: %1. Message: \"%2\".")
.arg(endpoint, QString::fromLocal8Bit(alert->error.message().c_str()))
.arg(endpoint, Utils::String::fromLocal8Bit(alert->error.message()))
, Log::WARNING);
}
}

View file

@ -63,6 +63,7 @@
#include "base/types.h"
#include "base/utils/fs.h"
#include "base/utils/io.h"
#include "base/utils/string.h"
#include "common.h"
#include "downloadpriority.h"
#include "extensiondata.h"
@ -100,8 +101,7 @@ namespace
if (const QString *endpointName = cache.object(ltTCPEndpoint))
return *endpointName;
const std::string tmp = (std::ostringstream() << ltTCPEndpoint).str();
const auto endpointName = QString::fromLatin1(tmp.c_str(), tmp.size());
const auto endpointName = Utils::String::fromLatin1((std::ostringstream() << ltTCPEndpoint).str());
cache.insert(ltTCPEndpoint, new QString(endpointName));
return endpointName;
}
@ -1255,12 +1255,12 @@ int TorrentImpl::queuePosition() const
QString TorrentImpl::error() const
{
if (m_nativeStatus.errc)
return QString::fromLocal8Bit(m_nativeStatus.errc.message().c_str());
return Utils::String::fromLocal8Bit(m_nativeStatus.errc.message());
if (m_nativeStatus.flags & lt::torrent_flags::upload_mode)
{
return tr("Couldn't write to file. Reason: \"%1\". Torrent is now in \"upload only\" mode.")
.arg(QString::fromLocal8Bit(m_lastFileError.error.message().c_str()));
.arg(Utils::String::fromLocal8Bit(m_lastFileError.error.message()));
}
return {};
@ -2277,7 +2277,7 @@ void TorrentImpl::handleSaveResumeDataFailedAlert(const lt::save_resume_data_fai
if (p->error != lt::errors::resume_data_not_modified)
{
LogMsg(tr("Generate resume data failed. Torrent: \"%1\". Reason: \"%2\"")
.arg(name(), QString::fromLocal8Bit(p->error.message().c_str())), Log::CRITICAL);
.arg(name(), Utils::String::fromLocal8Bit(p->error.message())), Log::CRITICAL);
}
}
@ -2365,7 +2365,7 @@ void TorrentImpl::handleFileRenameFailedAlert(const lt::file_rename_failed_alert
Q_ASSERT(fileIndex >= 0);
LogMsg(tr("File rename failed. Torrent: \"%1\", file: \"%2\", reason: \"%3\"")
.arg(name(), filePath(fileIndex).toString(), QString::fromLocal8Bit(p->error.message().c_str())), Log::WARNING);
.arg(name(), filePath(fileIndex).toString(), Utils::String::fromLocal8Bit(p->error.message())), Log::WARNING);
--m_renameCount;
while (!isMoveInProgress() && (m_renameCount == 0) && !m_moveFinishedTriggers.isEmpty())

View file

@ -49,6 +49,16 @@ QString Utils::String::fromDouble(const double n, const int precision)
return QLocale::system().toString(std::floor(n * prec) / prec, 'f', precision);
}
QString Utils::String::fromLatin1(const std::string &string)
{
return QString::fromLatin1(string.c_str(), string.size());
}
QString Utils::String::fromLocal8Bit(const std::string &string)
{
return QString::fromLocal8Bit(string.c_str(), string.size());
}
QString Utils::String::wildcardToRegexPattern(const QString &pattern)
{
return QRegularExpression::wildcardToRegularExpression(pattern, QRegularExpression::UnanchoredWildcardConversion);

View file

@ -66,6 +66,8 @@ namespace Utils::String
QStringList splitCommand(const QString &command);
QString fromDouble(double n, int precision);
QString fromLatin1(const std::string &string);
QString fromLocal8Bit(const std::string &string);
template <typename Container>
QString joinIntoString(const Container &container, const QString &separator)