QTextCodec: add QStringView overloads

[ChangeLog][QtCore][QTextCodec] Added fromUnicode()
and canEncode() overloads taking QStringView.
[ChangeLog][QtCore][QTextEncoder] Added fromUnicode()
overload taking QStringView.

Change-Id: I2599f25570480d967921ccd4414e092bfc90d821
Reviewed-by: Lars Knoll <lars.knoll@qt.io>
This commit is contained in:
Marc Mutz 2017-03-31 16:51:16 +02:00
parent edb29be8ba
commit c928d08267
3 changed files with 55 additions and 3 deletions

View File

@ -803,6 +803,7 @@ QTextEncoder* QTextCodec::makeEncoder(QTextCodec::ConversionFlags flags) const
The \a state of the convertor used is updated.
*/
#if QT_STRINGVIEW_LEVEL < 2
/*!
Converts \a str from Unicode to the encoding of this codec, and
returns the result in a QByteArray.
@ -811,6 +812,19 @@ QByteArray QTextCodec::fromUnicode(const QString& str) const
{
return convertFromUnicode(str.constData(), str.length(), 0);
}
#endif
/*!
\overload
\since 5.10
Converts \a str from Unicode to the encoding of this codec, and
returns the result in a QByteArray.
*/
QByteArray QTextCodec::fromUnicode(QStringView str) const
{
return convertFromUnicode(str.data(), str.length(), nullptr);
}
/*!
\fn QString QTextCodec::toUnicode(const char *input, int size,
@ -844,6 +858,7 @@ bool QTextCodec::canEncode(QChar ch) const
return (state.invalidChars == 0);
}
#if QT_STRINGVIEW_LEVEL < 2
/*!
\overload
@ -856,7 +871,22 @@ bool QTextCodec::canEncode(const QString& s) const
convertFromUnicode(s.constData(), s.length(), &state);
return (state.invalidChars == 0);
}
#endif
/*!
\overload
\since 5.10
Returns \c true if the Unicode string \a s can be fully encoded
with this codec; otherwise returns \c false.
*/
bool QTextCodec::canEncode(QStringView s) const
{
ConverterState state;
state.flags = ConvertInvalidToNull;
convertFromUnicode(s.data(), s.length(), &state);
return !state.invalidChars;
}
/*!
\overload
@ -921,6 +951,7 @@ bool QTextEncoder::hasFailure() const
return state.invalidChars != 0;
}
#if QT_STRINGVIEW_LEVEL < 2
/*!
Converts the Unicode string \a str into an encoded QByteArray.
*/
@ -929,6 +960,17 @@ QByteArray QTextEncoder::fromUnicode(const QString& str)
QByteArray result = c->fromUnicode(str.constData(), str.length(), &state);
return result;
}
#endif
/*!
\overload
\since 5.10
Converts the Unicode string \a str into an encoded QByteArray.
*/
QByteArray QTextEncoder::fromUnicode(QStringView str)
{
return c->fromUnicode(str.data(), str.length(), &state);
}
/*!
\overload

View File

@ -79,11 +79,17 @@ public:
static QTextCodec *codecForUtfText(const QByteArray &ba, QTextCodec *defaultCodec);
bool canEncode(QChar) const;
#if QT_STRINGVIEW_LEVEL < 2
bool canEncode(const QString&) const;
#endif
bool canEncode(QStringView) const;
QString toUnicode(const QByteArray&) const;
QString toUnicode(const char* chars) const;
#if QT_STRINGVIEW_LEVEL < 2
QByteArray fromUnicode(const QString& uc) const;
#endif
QByteArray fromUnicode(QStringView uc) const;
enum ConversionFlag {
DefaultConversion,
ConvertInvalidToNull = 0x80000000,
@ -135,7 +141,10 @@ public:
explicit QTextEncoder(const QTextCodec *codec) : c(codec), state() {}
QTextEncoder(const QTextCodec *codec, QTextCodec::ConversionFlags flags);
~QTextEncoder();
#if QT_STRINGVIEW_LEVEL < 2
QByteArray fromUnicode(const QString& str);
#endif
QByteArray fromUnicode(QStringView str);
QByteArray fromUnicode(const QChar *uc, int len);
bool hasFailure() const;
private:

View File

@ -126,6 +126,7 @@ void tst_QTextCodec::toUnicode()
}
QVERIFY(!uniString.isEmpty());
QCOMPARE( ba, c->fromUnicode( uniString ) );
QCOMPARE(ba, c->fromUnicode(QStringView(uniString)) );
char ch = '\0';
QVERIFY(c->toUnicode(&ch, 1).length() == 1);
@ -262,7 +263,7 @@ void tst_QTextCodec::fromUnicode()
If the encoding is a superset of ASCII, test that the byte
array is correct (no off by one, no trailing '\0').
*/
QByteArray result = codec->fromUnicode(QString("abc"));
QByteArray result = codec->fromUnicode(QStringViewLiteral("abc"));
if (result.startsWith('a')) {
QCOMPARE(result.size(), 3);
QCOMPARE(result, QByteArray("abc"));
@ -397,6 +398,7 @@ void tst_QTextCodec::asciiToIscii() const
QVERIFY2(textCodec->canEncode(ascii), qPrintable(QString::fromLatin1("Failed for full string with encoding %1")
.arg(QString::fromLatin1(textCodec->name().constData()))));
QVERIFY(textCodec->canEncode(QStringView(ascii)));
}
}
@ -404,12 +406,11 @@ void tst_QTextCodec::nonFlaggedCodepointFFFF() const
{
//Check that the code point 0xFFFF (=non-character code 0xEFBFBF) is not flagged
const QChar ch(0xFFFF);
QString input(ch);
QTextCodec *const codec = QTextCodec::codecForMib(106); // UTF-8
QVERIFY(codec);
const QByteArray asDecoded(codec->fromUnicode(input));
const QByteArray asDecoded = codec->fromUnicode(QStringView(&ch, 1));
QCOMPARE(asDecoded, QByteArray("\357\277\277"));
QByteArray ffff("\357\277\277");