Skip to content

Commit

Permalink
QString::fromUcs4: use std::char_traits or wcslen() to find the size
Browse files Browse the repository at this point in the history
... 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).

Pick-to: 6.8 6.9
Change-Id: Ia143270869a3a7cf5754fffdc17e500fc454397b
Reviewed-by: Edward Welbourne <[email protected]>
  • Loading branch information
thiagomacieira authored and ediosyncratic committed Dec 12, 2024
1 parent 973d0c4 commit b98cf4f
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/corelib/text/qstring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6115,9 +6115,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));
Expand Down

0 comments on commit b98cf4f

Please sign in to comment.