From 92daca1fefdf9a4ea8a4e9c80f63f0f96a7668d0 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Mon, 11 Nov 2024 19:19:10 +0800 Subject: [PATCH] Avoid redundant string length scan PR #21807. --- src/base/bittorrent/sessionimpl.cpp | 9 +++++---- src/base/bittorrent/torrentimpl.cpp | 12 ++++++------ src/base/utils/string.cpp | 10 ++++++++++ src/base/utils/string.h | 2 ++ 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/src/base/bittorrent/sessionimpl.cpp b/src/base/bittorrent/sessionimpl.cpp index d54cb8c50..efcabbf0d 100644 --- a/src/base/bittorrent/sessionimpl.cpp +++ b/src/base/bittorrent/sessionimpl.cpp @@ -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); } } diff --git a/src/base/bittorrent/torrentimpl.cpp b/src/base/bittorrent/torrentimpl.cpp index 3f873890e..ad749d1c3 100644 --- a/src/base/bittorrent/torrentimpl.cpp +++ b/src/base/bittorrent/torrentimpl.cpp @@ -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()) diff --git a/src/base/utils/string.cpp b/src/base/utils/string.cpp index da42f79bb..35570660e 100644 --- a/src/base/utils/string.cpp +++ b/src/base/utils/string.cpp @@ -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); diff --git a/src/base/utils/string.h b/src/base/utils/string.h index 6a72176f2..fdbe90565 100644 --- a/src/base/utils/string.h +++ b/src/base/utils/string.h @@ -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 QString joinIntoString(const Container &container, const QString &separator)