Add literal operators for QString and QByteArray

[ChangeLog][QtCore][QString] Added literal operator
u"..."_qs that converts a char16_t string literal to QString

[ChangeLog][QtCore][QByteArray] Added literal operator
"..."_qba that converts char string literal to QByteArray

Change-Id: I4aa59b28cc17bff346b378eb70008fb8185d21ac
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
Reviewed-by: Fabian Kosmale <fabian.kosmale@qt.io>
This commit is contained in:
Andrei Golubev 2021-03-04 17:19:33 +01:00
parent ccf1a1a953
commit b1377ed02d
6 changed files with 104 additions and 0 deletions

View File

@ -4766,6 +4766,27 @@ QByteArray QByteArray::toPercentEncoding(const QByteArray &exclude, const QByteA
\sa QStringLiteral
*/
/*!
\function QtLiterals::operator""_qba(const char *str, size_t size)
\relates QByteArray
\since 6.2
Literal operator that creates a QByteArray out of a char string literal \a
str. Creating a QByteArray from it is free in this case, and the generated
string data is stored in the read-only segment of the compiled object file.
Duplicate literals may share the same read-only memory. This functionality is
interchangeable with QByteArrayLiteral, but saves typing when many string
literals are present in the code.
The following code creates a QByteArray:
\code
auto str = "hello"_qba;
\endcode
\sa QByteArrayLiteral, QtLiterals::operator""_qs(const char16_t *str, size_t size)
*/
/*!
\class QByteArray::FromBase64Result
\inmodule QtCore

View File

@ -756,6 +756,13 @@ QByteArray QByteArrayView::toByteArray() const
return QByteArray(data(), size());
}
inline namespace QtLiterals {
inline QByteArray operator"" _qba(const char *str, size_t size) noexcept
{
return QByteArray(QByteArrayData(nullptr, const_cast<char *>(str), qsizetype(size)));
}
} // QtLiterals
QT_END_NAMESPACE
#endif // QBYTEARRAY_H

View File

@ -10505,6 +10505,27 @@ QString QString::toHtmlEscaped() const
\sa QByteArrayLiteral
*/
/*!
\function QtLiterals::operator""_qs(const char16_t *str, size_t size)
\relates QString
\since 6.2
Literal operator that creates a QString out of a char16_t string literal \a
str. Creating a QString from it is free in this case, and the generated string
data is stored in the read-only segment of the compiled object file. Duplicate
literals may share the same read-only memory. This functionality is
interchangeable with QStringLiteral, but saves typing when many string
literals are present in the code.
The following code creates a QString:
\code
auto str = u"hello"_qs;
\endcode
\sa QStringLiteral, QtLiterals::operator""_qba(const char *str, size_t size)
*/
/*!
\internal
*/

View File

@ -1541,6 +1541,13 @@ qsizetype erase_if(QString &s, Predicate pred)
return QtPrivate::sequential_erase_if(s, pred);
}
inline namespace QtLiterals {
inline QString operator"" _qs(const char16_t *str, size_t size) noexcept
{
return QString(QStringPrivate(nullptr, const_cast<char16_t *>(str), qsizetype(size)));
}
} // QtLiterals
QT_END_NAMESPACE
#if defined(QT_USE_FAST_OPERATOR_PLUS) || defined(QT_USE_QSTRINGBUILDER)

View File

@ -126,6 +126,7 @@ private slots:
void movablity_data();
void movablity();
void literals();
void userDefinedLiterals();
void toUpperLower_data();
void toUpperLower();
void isUpper();
@ -1991,6 +1992,29 @@ void tst_QByteArray::literals()
QVERIFY(str2.capacity() >= str2.length());
}
void tst_QByteArray::userDefinedLiterals()
{
QByteArray str = "abcd"_qba;
QVERIFY(str.length() == 4);
QCOMPARE(str.capacity(), 0);
QVERIFY(str == "abcd");
QVERIFY(!str.data_ptr()->isMutable());
const char *s = str.constData();
QByteArray str2 = str;
QVERIFY(str2.constData() == s);
QCOMPARE(str2.capacity(), 0);
// detach on non const access
QVERIFY(str.data() != s);
QVERIFY(str.capacity() >= str.length());
QVERIFY(str2.constData() == s);
QVERIFY(str2.data() != s);
QVERIFY(str2.capacity() >= str2.length());
}
void tst_QByteArray::toUpperLower_data()
{
QTest::addColumn<QByteArray>("input");

View File

@ -569,6 +569,7 @@ private slots:
#if !defined(QT_NO_UNICODE_LITERAL)
void literals();
#endif
void userDefinedLiterals();
void eightBitLiterals_data();
void eightBitLiterals();
void reserve();
@ -6445,6 +6446,29 @@ void tst_QString::literals()
}
#endif
void tst_QString::userDefinedLiterals()
{
QString str = u"abcd"_qs;
QVERIFY(str.length() == 4);
QCOMPARE(str.capacity(), 0);
QVERIFY(str == QLatin1String("abcd"));
QVERIFY(!str.data_ptr()->isMutable());
const QChar *s = str.constData();
QString str2 = str;
QVERIFY(str2.constData() == s);
QCOMPARE(str2.capacity(), 0);
// detach on non const access
QVERIFY(str.data() != s);
QVERIFY(str.capacity() >= str.length());
QVERIFY(str2.constData() == s);
QVERIFY(str2.data() != s);
QVERIFY(str2.capacity() >= str2.length());
}
void tst_QString::eightBitLiterals_data()
{
QTest::addColumn<QByteArray>("data");