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>
This commit is contained in:
parent
122f7d7adc
commit
f5fbad669d
@ -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
|
||||
|
||||
|
@ -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); }
|
||||
};
|
||||
|
@ -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
|
||||
|
Loading…
Reference in New Issue
Block a user