summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorQt CI Bot <[email protected]>2021-03-30 13:30:26 +0000
committerQt CI Bot <[email protected]>2021-03-30 13:30:26 +0000
commit31c81e08c6b0fa878ab7a22771230635ac2e85c6 (patch)
tree3664d654cc443cc116043c09bf76ac383ad8ae62
parent7ae037bc82085c73b271d70532b858fb8d8bcef9 (diff)
parentc34f51d58c1e3333a663b09b974bbd7b145a2d1b (diff)
Merge integration refs/builds/qtci/dev/1617098611
-rw-r--r--src/corelib/text/qbytearray.cpp21
-rw-r--r--src/corelib/text/qbytearray.h7
-rw-r--r--src/corelib/text/qstring.cpp21
-rw-r--r--src/corelib/text/qstring.h7
-rw-r--r--tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp25
-rw-r--r--tests/auto/corelib/text/qstring/tst_qstring.cpp29
6 files changed, 104 insertions, 6 deletions
diff --git a/src/corelib/text/qbytearray.cpp b/src/corelib/text/qbytearray.cpp
index bead997ab84..677fc6d6ace 100644
--- a/src/corelib/text/qbytearray.cpp
+++ b/src/corelib/text/qbytearray.cpp
@@ -4767,6 +4767,27 @@ QByteArray QByteArray::toPercentEncoding(const QByteArray &exclude, const QByteA
*/
/*!
+ \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
\ingroup tools
diff --git a/src/corelib/text/qbytearray.h b/src/corelib/text/qbytearray.h
index 0aad272f942..91c34747d84 100644
--- a/src/corelib/text/qbytearray.h
+++ b/src/corelib/text/qbytearray.h
@@ -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
diff --git a/src/corelib/text/qstring.cpp b/src/corelib/text/qstring.cpp
index 49e56d26d63..3455daa3ca0 100644
--- a/src/corelib/text/qstring.cpp
+++ b/src/corelib/text/qstring.cpp
@@ -10506,6 +10506,27 @@ QString QString::toHtmlEscaped() const
*/
/*!
+ \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
*/
void QAbstractConcatenable::appendLatin1To(QLatin1String in, QChar *out) noexcept
diff --git a/src/corelib/text/qstring.h b/src/corelib/text/qstring.h
index abf8957be9b..c6d97c9d513 100644
--- a/src/corelib/text/qstring.h
+++ b/src/corelib/text/qstring.h
@@ -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)
diff --git a/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp b/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp
index 70180bbf3b8..382190567b2 100644
--- a/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp
+++ b/tests/auto/corelib/text/qbytearray/tst_qbytearray.cpp
@@ -126,6 +126,7 @@ private slots:
void movablity_data();
void movablity();
void literals();
+ void userDefinedLiterals();
void toUpperLower_data();
void toUpperLower();
void isUpper();
@@ -1967,7 +1968,6 @@ void tst_QByteArray::movablity()
QVERIFY(true);
}
-// Only tested on c++0x compliant compiler or gcc
void tst_QByteArray::literals()
{
QByteArray str(QByteArrayLiteral("abcd"));
@@ -1991,6 +1991,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");
diff --git a/tests/auto/corelib/text/qstring/tst_qstring.cpp b/tests/auto/corelib/text/qstring/tst_qstring.cpp
index 79c4fa4ab02..57885f596df 100644
--- a/tests/auto/corelib/text/qstring/tst_qstring.cpp
+++ b/tests/auto/corelib/text/qstring/tst_qstring.cpp
@@ -566,9 +566,8 @@ private slots:
#if QT_CONFIG(icu)
void toUpperLower_icu();
#endif
-#if !defined(QT_NO_UNICODE_LITERAL)
void literals();
-#endif
+ void userDefinedLiterals();
void eightBitLiterals_data();
void eightBitLiterals();
void reserve();
@@ -6419,8 +6418,6 @@ void tst_QString::toUpperLower_icu()
}
#endif // icu
-#if !defined(QT_NO_UNICODE_LITERAL)
-// Only tested on c++0x compliant compiler or gcc
void tst_QString::literals()
{
QString str(QStringLiteral("abcd"));
@@ -6443,7 +6440,29 @@ void tst_QString::literals()
QVERIFY(str2.data() != s);
QVERIFY(str2.capacity() >= str2.length());
}
-#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()
{