Replace some QString::fromUtf16() with QStringView::toString()

The QStringView counterpart is somewhat faster because it doesn't go
through the UTF-16 codec in QUtf16::convertToUnicode(), which tries to
detect the BOM.

I've included QString::fromWCharArray in this because:
a) it's used extensively in Windows code
b) wide chars in memory probably don't have BOMs anyway

Change-Id: I01ec3c774d9943adb903fffd17b815be4d2ab8ba
Reviewed-by: Qt CI Bot <qt_ci_bot@qt-project.org>
Reviewed-by: Ahmad Samir <a.samirh78@gmail.com>
bb10
Thiago Macieira 2024-02-28 09:06:05 -08:00
parent a14bba6803
commit a6b4ff16b9
5 changed files with 11 additions and 8 deletions

View File

@ -2353,7 +2353,7 @@ QString qt_normalizePathSegments(const QString &name, QDirPrivate::PathNormaliza
// If path was not modified return the original value
if (used == 0)
return name;
return QString::fromUtf16(out + used, len - used);
return QStringView(out + used, len - used).toString();
}
static QString qt_cleanPath(const QString &path, bool *ok)

View File

@ -1508,7 +1508,7 @@ QStringList QRegularExpression::namedCaptureGroups() const
reinterpret_cast<const char16_t *>(namedCapturingTable) + namedCapturingTableEntrySize * i;
const int index = *currentNamedCapturingTableRow;
result[index] = QString::fromUtf16(currentNamedCapturingTableRow + 1);
result[index] = QStringView(currentNamedCapturingTableRow + 1).toString();
}
return result;

View File

@ -1307,8 +1307,11 @@ QT_WARNING_POP
QString QString::fromWCharArray(const wchar_t *string, qsizetype size)
{
return sizeof(wchar_t) == sizeof(QChar) ? fromUtf16(reinterpret_cast<const char16_t *>(string), size)
: fromUcs4(reinterpret_cast<const char32_t *>(string), size);
if constexpr (sizeof(wchar_t) == sizeof(QChar)) {
return QString(reinterpret_cast<const QChar *>(string), size);
} else {
return fromUcs4(reinterpret_cast<const char32_t *>(string), size);
}
}
constexpr QString::QString() noexcept {}

View File

@ -98,8 +98,8 @@ bool isMeteredFromNMMetered(QNetworkManagerInterface::NMMetered metered)
static QString backendName()
{
return QString::fromUtf16(QNetworkInformationBackend::PluginNames
[QNetworkInformationBackend::PluginNamesLinuxIndex]);
return QStringView(QNetworkInformationBackend::PluginNames
[QNetworkInformationBackend::PluginNamesLinuxIndex]).toString();
}
QString QNetworkManagerNetworkInformationBackend::name() const

View File

@ -155,7 +155,7 @@ QVariant QXcbMime::mimeConvertToFormat(QXcbConnection *connection, xcb_atom_t a,
const quint8 byte1 = data.at(1);
if ((byte0 == 0xff && byte1 == 0xfe) || (byte0 == 0xfe && byte1 == 0xff)
|| (byte0 != 0 && byte1 == 0) || (byte0 == 0 && byte1 != 0)) {
const QString str = QString::fromUtf16(
const QStringView str(
reinterpret_cast<const char16_t *>(data.constData()), data.size() / 2);
if (!str.isNull()) {
if (format == "text/uri-list"_L1) {
@ -174,7 +174,7 @@ QVariant QXcbMime::mimeConvertToFormat(QXcbConnection *connection, xcb_atom_t a,
return list.constFirst();
return list;
} else {
return str;
return str.toString();
}
}
}