QLocal8Bit::convertFromUnicode[win] move cast earlier

So we don't have to do this multiple times when calling the function.

Pick-to: 6.6 6.5
Task-number: QTBUG-118185
Task-number: QTBUG-105105
Change-Id: Ifa72eedd5f71365618ec6b67fa3047f90f4eb541
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
Mårten Nordheim 2023-10-16 17:05:58 +02:00
parent 94214fe100
commit ab009f2f03

View File

@ -1383,7 +1383,7 @@ QByteArray QLocal8Bit::convertFromUnicode_sys(QStringView in, QStringConverter::
QByteArray QLocal8Bit::convertFromUnicode_sys(QStringView in, quint32 codePage, QByteArray QLocal8Bit::convertFromUnicode_sys(QStringView in, quint32 codePage,
QStringConverter::State *state) QStringConverter::State *state)
{ {
const QChar *ch = in.data(); const wchar_t *ch = reinterpret_cast<const wchar_t *>(in.data());
qsizetype uclen = in.size(); qsizetype uclen = in.size();
Q_ASSERT(uclen < INT_MAX); // ### FIXME Q_ASSERT(uclen < INT_MAX); // ### FIXME
@ -1399,22 +1399,19 @@ QByteArray QLocal8Bit::convertFromUnicode_sys(QStringView in, quint32 codePage,
BOOL used_def; BOOL used_def;
QByteArray mb(4096, 0); QByteArray mb(4096, 0);
int len; int len;
while (!(len=WideCharToMultiByte(codePage, 0, (const wchar_t*)ch, uclen, while (!(len = WideCharToMultiByte(codePage, 0, ch, uclen, mb.data(), mb.size() - 1, 0,
mb.data(), mb.size()-1, 0, &used_def))) &used_def))) {
{
int r = GetLastError(); int r = GetLastError();
if (r == ERROR_INSUFFICIENT_BUFFER) { if (r == ERROR_INSUFFICIENT_BUFFER) {
mb.resize(1+WideCharToMultiByte(codePage, 0, mb.resize(1+WideCharToMultiByte(codePage, 0, ch, uclen, 0, 0, 0, &used_def));
(const wchar_t*)ch, uclen,
0, 0, 0, &used_def));
// and try again... // and try again...
} else { } else {
// Fail. Probably can't happen in fact (dwFlags is 0). // Fail. Probably can't happen in fact (dwFlags is 0).
#ifndef QT_NO_DEBUG #ifndef QT_NO_DEBUG
// Can't use qWarning(), as it'll recurse to handle %ls // Can't use qWarning(), as it'll recurse to handle %ls
fprintf(stderr, fprintf(stderr, "WideCharToMultiByte: Cannot convert multibyte text (error %d): %ls\n",
"WideCharToMultiByte: Cannot convert multibyte text (error %d): %ls\n", r,
r, reinterpret_cast<const wchar_t*>(QString(ch, uclen).utf16())); reinterpret_cast<const wchar_t *>(QStringView(ch, uclen).toString().utf16()));
#endif #endif
break; break;
} }