From f5fbad669d8b9b7284d8644ae51098a9c1998f64 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Wed, 3 Nov 2021 12:26:43 +0100 Subject: [PATCH] QByteArrayList: add join(QByteArrayView) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reviewed-by: Edward Welbourne --- src/corelib/text/qbytearraylist.cpp | 8 ++++++ src/corelib/text/qbytearraylist.h | 7 ++++- .../qbytearraylist/tst_qbytearraylist.cpp | 28 +++++++++++++++++++ 3 files changed, 42 insertions(+), 1 deletion(-) 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 @@ -115,6 +115,14 @@ QT_BEGIN_NAMESPACE element separated by the given \a separator. */ +/*! + \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 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 #include #include +#include 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 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