From 7b93bedb602724b80c03a55419cfcd33d9a70cc3 Mon Sep 17 00:00:00 2001 From: Lars Knoll Date: Fri, 17 Apr 2020 19:48:33 +0200 Subject: [PATCH] Add Latin1 to the set of supported encodings in QStringConverter Latin1 is the only non Unicode encoding that is still being used to some extent. Current web site statistics show that it is being used in ~2% of all web sites. An additional 1% of web sites use Windows1251 (which is almost the same as latin1). As it's trivial to support this encoding, we keep it supported in QStringConverter. Change-Id: I0eff53a490b6c43d3e474107e7823be245d1715a Reviewed-by: Thiago Macieira --- src/corelib/text/qstringconverter.cpp | 31 +++++++++++++++++++++++++++ src/corelib/text/qstringconverter.h | 1 + 2 files changed, 32 insertions(+) diff --git a/src/corelib/text/qstringconverter.cpp b/src/corelib/text/qstringconverter.cpp index 92cb327577..5858dc3e0f 100644 --- a/src/corelib/text/qstringconverter.cpp +++ b/src/corelib/text/qstringconverter.cpp @@ -1296,6 +1296,33 @@ static char *toUtf32LE(char *out, QStringView in, QStringConverter::State *state return out + s.length(); } +void qt_from_latin1(char16_t *dst, const char *str, size_t size) noexcept; + +static QChar *fromLatin1(QChar *out, const char *chars, qsizetype len, QStringConverter::State *) +{ + qt_from_latin1(reinterpret_cast(out), chars, size_t(len)); + return out + len; +} + + +static char *toLatin1(char *out, QStringView in, QStringConverter::State *state) +{ + const char replacement = (state && state->flags & QStringConverter::ConvertInvalidToNull) ? 0 : '?'; + int invalid = 0; + for (qsizetype i = 0; i < in.length(); ++i) { + if (in[i] > QChar(0xff)) { + *out = replacement; + ++invalid; + } else { + *out = (char)in[i].cell(); + } + ++out; + } + if (state) + state->invalidChars += invalid; + return out; +} + static QChar *fromLocal8Bit(QChar *out, const char *in, qsizetype length, QStringConverter::State *state) { QString s = QLocal8Bit::convertToUnicode(in, length, state); @@ -1320,6 +1347,9 @@ static qsizetype toUtf16Len(qsizetype l) { return 2*(l + 1); } static qsizetype fromUtf32Len(qsizetype l) { return l + 1; } static qsizetype toUtf32Len(qsizetype l) { return 4*(l + 1); } +static qsizetype fromLatin1Len(qsizetype l) { return l + 1; } +static qsizetype toLatin1Len(qsizetype l) { return l + 1; } + const QStringConverter::Interface QStringConverter::encodingInterfaces[QStringConverter::LastEncoding + 1] = { { fromUtf8, fromUtf8Len, toUtf8, toUtf8Len }, @@ -1329,6 +1359,7 @@ const QStringConverter::Interface QStringConverter::encodingInterfaces[QStringCo { fromUtf32, fromUtf32Len, toUtf32, toUtf32Len }, { fromUtf32LE, fromUtf32Len, toUtf32LE, toUtf32Len }, { fromUtf32BE, fromUtf32Len, toUtf32BE, toUtf32Len }, + { fromLatin1, fromLatin1Len, toLatin1, toLatin1Len }, { fromLocal8Bit, fromUtf8Len, toLocal8Bit, toUtf8Len } }; diff --git a/src/corelib/text/qstringconverter.h b/src/corelib/text/qstringconverter.h index d3c0e9a502..87fb7fc06a 100644 --- a/src/corelib/text/qstringconverter.h +++ b/src/corelib/text/qstringconverter.h @@ -96,6 +96,7 @@ public: Utf32, Utf32LE, Utf32BE, + Latin1, Locale, LastEncoding = Locale };