diff options
author | Marc Mutz <marc.mutz@qt.io> | 2021-11-03 12:26:43 +0100 |
---|---|---|
committer | Marc Mutz <marc.mutz@qt.io> | 2021-11-04 01:16:16 +0100 |
commit | f5fbad669d8b9b7284d8644ae51098a9c1998f64 (patch) | |
tree | f7213bcb2575d0e97d0b6f6a604050ac20e1e5fd | |
parent | 122f7d7adcbc16389728855dcff9f3e19c4652bd (diff) |
QByteArrayList: add join(QByteArrayView)
Since QByteArray/QByteArrayView don't overload nicely, we need to make
the existing QByteArray overload a Q_WEAK_OVERLOAD (= a template) as a
tie breaker. This automatically prefers the QByteArrayView version
over the QByteArray overload, transparently optimizing existing users
passing char string literals to avoid the implicit creation of a
QByteArray just for passing the separator.
None of our modules exports a subclass of QByteArrayList, so turning
join(QByteArray) into a function template should be ok.
[ChangeLog][QtCore][QByteArrayList] Added join(QByteArrayView)
overload.
Change-Id: I090671d9b94c30b63a986f17e966d124c22b5c54
Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
-rw-r--r-- | src/corelib/text/qbytearraylist.cpp | 8 | ||||
-rw-r--r-- | src/corelib/text/qbytearraylist.h | 7 | ||||
-rw-r--r-- | tests/auto/corelib/text/qbytearraylist/tst_qbytearraylist.cpp | 28 |
3 files changed, 42 insertions, 1 deletions
diff --git a/src/corelib/text/qbytearraylist.cpp b/src/corelib/text/qbytearraylist.cpp index c815e766ab..5142869c72 100644 --- a/src/corelib/text/qbytearraylist.cpp +++ b/src/corelib/text/qbytearraylist.cpp @@ -116,6 +116,14 @@ QT_BEGIN_NAMESPACE */ /*! + \fn QByteArray QByteArrayList::join(QByteArrayView separator) const + \since 6.3 + + Joins all the byte arrays into a single byte array with each + element separated by the given \a separator. +*/ + +/*! \fn QByteArray QByteArrayList::join(char separator) const Joins all the byte arrays into a single byte array with each diff --git a/src/corelib/text/qbytearraylist.h b/src/corelib/text/qbytearraylist.h index e00586291f..7e8fe89bab 100644 --- a/src/corelib/text/qbytearraylist.h +++ b/src/corelib/text/qbytearraylist.h @@ -77,8 +77,13 @@ public: inline QByteArray join() const { return QtPrivate::QByteArrayList_join(self(), nullptr, 0); } + inline QByteArray join(QByteArrayView sep) const // ### Qt 7: merge with the () overload + { + return QtPrivate::QByteArrayList_join(self(), sep.data(), sep.size()); + } + Q_WEAK_OVERLOAD inline QByteArray join(const QByteArray &sep) const - { return QtPrivate::QByteArrayList_join(self(), sep.constData(), sep.size()); } + { return join(QByteArrayView{sep}); } inline QByteArray join(char sep) const { return QtPrivate::QByteArrayList_join(self(), &sep, 1); } }; diff --git a/tests/auto/corelib/text/qbytearraylist/tst_qbytearraylist.cpp b/tests/auto/corelib/text/qbytearraylist/tst_qbytearraylist.cpp index 29e5b947f6..82459f21ea 100644 --- a/tests/auto/corelib/text/qbytearraylist/tst_qbytearraylist.cpp +++ b/tests/auto/corelib/text/qbytearraylist/tst_qbytearraylist.cpp @@ -27,10 +27,13 @@ ** ****************************************************************************/ +#define QT_USE_QSTRINGBUILDER + #include <QTest> #include <qbytearraylist.h> #include <qmetatype.h> +#include <qproperty.h> Q_DECLARE_METATYPE(QByteArrayList) @@ -38,6 +41,7 @@ class tst_QByteArrayList : public QObject { Q_OBJECT private slots: + void join_overloads() const; void join() const; void join_data() const; void joinByteArray() const; @@ -55,12 +59,34 @@ private slots: void initializerList() const; }; +void tst_QByteArrayList::join_overloads() const +{ + // Checks that there are no ambiguities between the different join() overloads: + + const QByteArrayList list = {"a", "b", "c"}; + const QByteArray expected = "aXbXc"; + + QCOMPARE(list.join('X'), expected); + QCOMPARE(list.join("X"), expected); + QCOMPARE(list.join(QByteArrayLiteral("X")), expected); + QCOMPARE(list.join(QByteArray("X")), expected); + QCOMPARE(list.join(QByteArrayView("X")), expected); + const char *sep = "X"; + QCOMPARE(list.join(sep), expected); + QCOMPARE(list.join(QByteArray() % "X"), expected); // QStringBuilder expression + QProperty<QByteArray> prop("X"); // implicitly convertible to QByteArray + QCOMPARE(list.join(prop), expected); + QCOMPARE(list.join(std::as_const(prop)), expected); +} + void tst_QByteArrayList::join() const { QFETCH(QByteArrayList, input); QFETCH(QByteArray, expectedResult); QCOMPARE(input.join(), expectedResult); + QCOMPARE(input.join(QByteArrayView{}), expectedResult); + QCOMPARE(input.join(QByteArray{}), expectedResult); } void tst_QByteArrayList::join_data() const @@ -88,6 +114,7 @@ void tst_QByteArrayList::joinByteArray() const QFETCH(QByteArray, expectedResult); QCOMPARE(input.join(separator), expectedResult); + QCOMPARE(input.join(QByteArrayView{separator}), expectedResult); } void tst_QByteArrayList::joinByteArray_data() const @@ -132,6 +159,7 @@ void tst_QByteArrayList::joinChar() const QFETCH(QByteArray, expectedResult); QCOMPARE(input.join(separator), expectedResult); + QCOMPARE(input.join(QByteArrayView{&separator, 1}), expectedResult); } void tst_QByteArrayList::joinChar_data() const |