QStringConverter: add QLatin1::convert{To,From}Unicode()
With the methods that use helpers from qstring.cpp defined in the latter. Change-Id: I11d6b0bfb95efe34e56d33d2ecbfe8f4423a9e6c Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
6a18bc8090
commit
bab660d124
@ -5413,6 +5413,15 @@ QByteArray QString::toLatin1_helper_inplace(QString &s)
|
||||
return QByteArray(std::move(ba_d));
|
||||
}
|
||||
|
||||
// QLatin1 methods that use helpers from qstring.cpp
|
||||
char16_t *QLatin1::convertToUnicode(char16_t *out, QLatin1StringView in) noexcept
|
||||
{
|
||||
const qsizetype len = in.size();
|
||||
qt_from_latin1(out, in.data(), len);
|
||||
return std::next(out, len);
|
||||
}
|
||||
|
||||
|
||||
/*!
|
||||
\fn QByteArray QString::toLatin1() const
|
||||
|
||||
|
@ -1527,19 +1527,7 @@ static char *toUtf32LE(char *out, QStringView in, QStringConverter::State *state
|
||||
return QUtf32::convertFromUnicode(out, in, state, LittleEndianness);
|
||||
}
|
||||
|
||||
void qt_from_latin1(char16_t *dst, const char *str, size_t size) noexcept;
|
||||
|
||||
static QChar *fromLatin1(QChar *out, QByteArrayView in, QStringConverter::State *state)
|
||||
{
|
||||
Q_ASSERT(state);
|
||||
Q_UNUSED(state);
|
||||
|
||||
qt_from_latin1(reinterpret_cast<char16_t *>(out), in.data(), size_t(in.size()));
|
||||
return out + in.size();
|
||||
}
|
||||
|
||||
|
||||
static char *toLatin1(char *out, QStringView in, QStringConverter::State *state)
|
||||
char *QLatin1::convertFromUnicode(char *out, QStringView in, QStringConverter::State *state) noexcept
|
||||
{
|
||||
Q_ASSERT(state);
|
||||
if (state->flags & QStringConverter::Flag::Stateless) // temporary
|
||||
@ -1725,7 +1713,7 @@ const QStringConverter::Interface QStringConverter::encodingInterfaces[QStringCo
|
||||
{ "UTF-32", fromUtf32, fromUtf32Len, toUtf32, toUtf32Len },
|
||||
{ "UTF-32LE", fromUtf32LE, fromUtf32Len, toUtf32LE, toUtf32Len },
|
||||
{ "UTF-32BE", fromUtf32BE, fromUtf32Len, toUtf32BE, toUtf32Len },
|
||||
{ "ISO-8859-1", fromLatin1, fromLatin1Len, toLatin1, toLatin1Len },
|
||||
{ "ISO-8859-1", QLatin1::convertToUnicode, fromLatin1Len, QLatin1::convertFromUnicode, toLatin1Len },
|
||||
{ "Locale", fromLocal8Bit, fromUtf8Len, toLocal8Bit, toUtf8Len }
|
||||
};
|
||||
|
||||
|
@ -29,6 +29,29 @@ enum qchar8_t : uchar {};
|
||||
using qchar8_t = char8_t;
|
||||
#endif
|
||||
|
||||
struct QLatin1
|
||||
{
|
||||
// Defined in qstring.cpp
|
||||
static char16_t *convertToUnicode(char16_t *dst, QLatin1StringView in) noexcept;
|
||||
|
||||
static QChar *convertToUnicode(QChar *buffer, QLatin1StringView in) noexcept
|
||||
{
|
||||
char16_t *dst = reinterpret_cast<char16_t *>(buffer);
|
||||
dst = convertToUnicode(dst, in);
|
||||
return reinterpret_cast<QChar *>(dst);
|
||||
}
|
||||
|
||||
static QChar *convertToUnicode(QChar *dst, QByteArrayView in,
|
||||
[[maybe_unused]] QStringConverterBase::State *state) noexcept
|
||||
{
|
||||
Q_ASSERT(state);
|
||||
|
||||
return convertToUnicode(dst, QLatin1StringView(in.data(), in.size()));
|
||||
}
|
||||
|
||||
static char *convertFromUnicode(char *out, QStringView in, QStringConverter::State *state) noexcept;
|
||||
};
|
||||
|
||||
struct QUtf8BaseTraits
|
||||
{
|
||||
static const bool isTrusted = false;
|
||||
|
@ -134,6 +134,8 @@ private slots:
|
||||
|
||||
void convertL1U8();
|
||||
|
||||
void convertL1U16();
|
||||
|
||||
#if QT_CONFIG(icu)
|
||||
void roundtripIcu_data();
|
||||
void roundtripIcu();
|
||||
@ -353,6 +355,33 @@ void tst_QStringConverter::convertUtf8CharByChar()
|
||||
QCOMPARE(reencoded, ba);
|
||||
}
|
||||
|
||||
void tst_QStringConverter::convertL1U16()
|
||||
{
|
||||
const QLatin1StringView latin1("some plain latin1 text");
|
||||
const QString qstr(latin1);
|
||||
|
||||
QStringDecoder decoder(QStringConverter::Latin1);
|
||||
QVERIFY(decoder.isValid());
|
||||
QString uniString = decoder(latin1);
|
||||
QCOMPARE(uniString, qstr);
|
||||
QCOMPARE(latin1, uniString.toLatin1());
|
||||
|
||||
// do it again (using .decode())
|
||||
uniString = decoder.decode(latin1);
|
||||
QCOMPARE(uniString, qstr);
|
||||
QCOMPARE(latin1, uniString.toLatin1());
|
||||
|
||||
QStringEncoder encoder(QStringConverter::Latin1);
|
||||
QByteArray reencoded = encoder(uniString);
|
||||
QCOMPARE(reencoded, QByteArrayView(latin1));
|
||||
QCOMPARE(reencoded, uniString.toLatin1());
|
||||
|
||||
// do it again (using .encode())
|
||||
reencoded = encoder.encode(uniString);
|
||||
QCOMPARE(reencoded, QByteArrayView(latin1));
|
||||
QCOMPARE(reencoded, uniString.toLatin1());
|
||||
}
|
||||
|
||||
void tst_QStringConverter::roundtrip_data()
|
||||
{
|
||||
QTest::addColumn<QStringView>("utf16");
|
||||
|
Loading…
Reference in New Issue
Block a user