QString::fromUcs4: use std::char_traits or wcslen() to find the size

... when the user passes size = -1. std::char_traits<char32_t>::length()
doesn't appear to be an any better implementation than our simple loop,
but maybe some compiler will optimize it.

wcslen() is usually optimized in the C libraries, even for Unix
platforms that hardly ever use it (it's used as a fallback in qustrlen()
for non-x86 Windows systems).

Change-Id: Ia143270869a3a7cf5754fffdc17e500fc454397b
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
(cherry picked from commit b98cf4fc4afaf55145c34ba06d61fec38fadc25d)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit bb91219b26ac5a617916ba2704ff353b7bea6513)
This commit is contained in:
Thiago Macieira 2024-12-06 08:19:20 -08:00 committed by Qt Cherry-pick Bot
parent 74d883029e
commit 4a0bd93bab

View File

@ -6098,9 +6098,10 @@ QString QString::fromUcs4(const char32_t *unicode, qsizetype size)
if (!unicode)
return QString();
if (size < 0) {
size = 0;
while (unicode[size] != 0)
++size;
if constexpr (sizeof(char32_t) == sizeof(wchar_t))
size = wcslen(reinterpret_cast<const wchar_t *>(unicode));
else
size = std::char_traits<char32_t>::length(unicode);
}
QStringDecoder toUtf16(QStringDecoder::Utf32, QStringDecoder::Flag::Stateless);
return toUtf16(QByteArrayView(reinterpret_cast<const char *>(unicode), size * 4));