Add literal operators for QString/QByteArray to StringLiterals namespace

[ChangeLog][QtCore] Added literal operators for _s and _ba for QString
and QByteArray respectively in the Qt::Literals::StringLiterals
namespace.

Task-number: QTBUG-101408
Change-Id: I5cd4e7f36f614ea805cfecc27b91c5d981cd3794
Reviewed-by: Ivan Solovev <ivan.solovev@qt.io>
Reviewed-by: Andrei Golubev <andrei.golubev@qt.io>
Reviewed-by: Marc Mutz <marc.mutz@qt.io>
This commit is contained in:
Sona Kurazyan 2022-03-21 14:32:58 +01:00
parent a0470ec261
commit 8aa3cf21da
6 changed files with 156 additions and 38 deletions

View File

@ -4778,6 +4778,31 @@ QByteArray QByteArray::toPercentEncoding(const QByteArray &exclude, const QByteA
\sa QByteArrayLiteral, QtLiterals::operator""_qs(const char16_t *str, size_t size)
*/
/*!
\fn Qt::Literals::StringLiterals::operator""_ba(const char *str, size_t size)
\relates QByteArray
\since 6.4
Literal operator that creates a QByteArray out of the first \a size characters
in the char string literal \a str.
The QByteArray is created at compile time, 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
using namespace Qt::Literals::StringLiterals;
auto str = "hello"_ba;
\endcode
\sa Qt::Literals::StringLiterals
*/
/*!
\class QByteArray::FromBase64Result
\inmodule QtCore

View File

@ -690,10 +690,23 @@ QByteArray QByteArrayView::toByteArray() const
return QByteArray(data(), size());
}
namespace Qt {
inline namespace Literals {
inline namespace StringLiterals {
inline QByteArray operator"" _ba(const char *str, size_t size) noexcept
{
return QByteArray(QByteArrayData(nullptr, const_cast<char *>(str), qsizetype(size)));
}
} // StringLiterals
} // Literals
} // Qt
inline namespace QtLiterals {
inline QByteArray operator"" _qba(const char *str, size_t size) noexcept
{
return QByteArray(QByteArrayData(nullptr, const_cast<char *>(str), qsizetype(size)));
return Qt::StringLiterals::operator""_ba(str, size);
}
} // QtLiterals

View File

@ -10983,6 +10983,31 @@ QString QString::toHtmlEscaped() const
\sa QStringLiteral, QtLiterals::operator""_qba(const char *str, size_t size)
*/
/*!
\fn Qt::Literals::StringLiterals::operator""_s(const char16_t *str, size_t size)
\relates QString
\since 6.4
Literal operator that creates a QString out of the first \a size characters in
the char16_t string literal \a str.
The QString is created at compile time, 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
using namespace Qt::Literals::StringLiterals;
auto str = u"hello"_s;
\endcode
\sa Qt::Literals::StringLiterals
*/
/*!
\fn Qt::Literals::StringLiterals::operator""_L1(const char *str, size_t size)

View File

@ -1658,13 +1658,6 @@ 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
namespace Qt {
inline namespace Literals {
inline namespace StringLiterals {
@ -1674,10 +1667,22 @@ constexpr inline QLatin1StringView operator"" _L1(const char *str, size_t size)
return {str, qsizetype(size)};
}
inline QString operator"" _s(const char16_t *str, size_t size) noexcept
{
return QString(QStringPrivate(nullptr, const_cast<char16_t *>(str), qsizetype(size)));
}
} // StringLiterals
} // Literals
} // Qt
inline namespace QtLiterals {
inline QString operator"" _qs(const char16_t *str, size_t size) noexcept
{
return Qt::StringLiterals::operator""_s(str, size);
}
} // QtLiterals
QT_END_NAMESPACE
#if defined(QT_USE_FAST_OPERATOR_PLUS) || defined(QT_USE_QSTRINGBUILDER)

View File

@ -2215,25 +2215,50 @@ void tst_QByteArray::literals()
void tst_QByteArray::userDefinedLiterals()
{
QByteArray str = "abcd"_qba;
{
using namespace Qt::StringLiterals;
QByteArray str = "abcd"_ba;
QVERIFY(str.length() == 4);
QCOMPARE(str.capacity(), 0);
QVERIFY(str == "abcd");
QVERIFY(!str.data_ptr()->isMutable());
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);
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());
// 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());
QVERIFY(str2.constData() == s);
QVERIFY(str2.data() != s);
QVERIFY(str2.capacity() >= str2.length());
}
{
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()

View File

@ -7030,25 +7030,50 @@ void tst_QString::literals()
void tst_QString::userDefinedLiterals()
{
QString str = u"abcd"_qs;
{
using namespace Qt::StringLiterals;
QString str = u"abcd"_s;
QVERIFY(str.length() == 4);
QCOMPARE(str.capacity(), 0);
QVERIFY(str == QLatin1String("abcd"));
QVERIFY(!str.data_ptr()->isMutable());
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);
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());
// 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());
QVERIFY(str2.constData() == s);
QVERIFY(str2.data() != s);
QVERIFY(str2.capacity() >= str2.length());
}
{
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()