Avoid redundant string length function calls

Also switch to `std::string_view` as it is more generic and can handle more types (including
view types).

PR #21861.
This commit is contained in:
Chocobo1
2024-11-19 02:53:16 +08:00
committed by GitHub
parent 530631322d
commit 6ddde3f4b6
9 changed files with 112 additions and 108 deletions

View File

@@ -60,7 +60,6 @@
#include <QRegularExpression>
#include <QStorageInfo>
#include "base/global.h"
#include "base/path.h"
/**

View File

@@ -49,14 +49,14 @@ 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)
QString Utils::String::fromLatin1(const std::string_view string)
{
return QString::fromLatin1(string.c_str(), string.size());
return QString::fromLatin1(string.data(), string.size());
}
QString Utils::String::fromLocal8Bit(const std::string &string)
QString Utils::String::fromLocal8Bit(const std::string_view string)
{
return QString::fromLocal8Bit(string.c_str(), string.size());
return QString::fromLocal8Bit(string.data(), string.size());
}
QString Utils::String::wildcardToRegexPattern(const QString &pattern)

View File

@@ -32,6 +32,7 @@
#include <numeric>
#include <optional>
#include <string_view>
#include <QChar>
#include <QMetaEnum>
@@ -66,8 +67,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);
QString fromLatin1(std::string_view string);
QString fromLocal8Bit(std::string_view string);
template <typename Container>
QString joinIntoString(const Container &container, const QString &separator)