QStringList::join: add an overload taking a single QChar
This overload avoids the needless heap allocation that the traditional overload incurs due to the implicit QChar -> QString conversion involved there. In order to share the implementation between the two overloads, QStringList_join now takes the separator as a (Char*,int) tuple instead of as a QString. Change-Id: I92961f13a3f19099de2a6e2df9f4789a12fc83a0 Reviewed-by: João Abecasis <joao.abecasis@nokia.com> Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
This commit is contained in:
parent
64a8c78538
commit
68e04c3ac1
@ -434,7 +434,13 @@ void QtPrivate::QStringList_replaceInStrings(QStringList *that, const QRegularEx
|
||||
|
||||
\sa QString::split()
|
||||
*/
|
||||
QString QtPrivate::QStringList_join(const QStringList *that, const QString &sep)
|
||||
|
||||
/*!
|
||||
\fn QString QStringList::join(QChar separator) const
|
||||
\since 5.0
|
||||
\overload join()
|
||||
*/
|
||||
QString QtPrivate::QStringList_join(const QStringList *that, const QChar *sep, int seplen)
|
||||
{
|
||||
int totalLength = 0;
|
||||
const int size = that->size();
|
||||
@ -443,7 +449,7 @@ QString QtPrivate::QStringList_join(const QStringList *that, const QString &sep)
|
||||
totalLength += that->at(i).size();
|
||||
|
||||
if(size > 0)
|
||||
totalLength += sep.size() * (size - 1);
|
||||
totalLength += seplen * (size - 1);
|
||||
|
||||
QString res;
|
||||
if (totalLength == 0)
|
||||
@ -451,7 +457,7 @@ QString QtPrivate::QStringList_join(const QStringList *that, const QString &sep)
|
||||
res.reserve(totalLength);
|
||||
for (int i = 0; i < that->size(); ++i) {
|
||||
if (i)
|
||||
res += sep;
|
||||
res.append(sep, seplen);
|
||||
res += that->at(i);
|
||||
}
|
||||
return res;
|
||||
|
@ -75,6 +75,7 @@ public:
|
||||
inline int removeDuplicates();
|
||||
|
||||
inline QString join(const QString &sep) const;
|
||||
inline QString join(QChar sep) const;
|
||||
|
||||
inline QStringList filter(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
|
||||
inline bool contains(const QString &str, Qt::CaseSensitivity cs = Qt::CaseSensitive) const;
|
||||
@ -122,7 +123,7 @@ Q_DECLARE_TYPEINFO(QStringList, Q_MOVABLE_TYPE);
|
||||
namespace QtPrivate {
|
||||
void Q_CORE_EXPORT QStringList_sort(QStringList *that, Qt::CaseSensitivity cs);
|
||||
int Q_CORE_EXPORT QStringList_removeDuplicates(QStringList *that);
|
||||
QString Q_CORE_EXPORT QStringList_join(const QStringList *that, const QString &sep);
|
||||
QString Q_CORE_EXPORT QStringList_join(const QStringList *that, const QChar *sep, int seplen);
|
||||
QStringList Q_CORE_EXPORT QStringList_filter(const QStringList *that, const QString &str,
|
||||
Qt::CaseSensitivity cs);
|
||||
|
||||
@ -161,7 +162,12 @@ inline int QStringList::removeDuplicates()
|
||||
|
||||
inline QString QStringList::join(const QString &sep) const
|
||||
{
|
||||
return QtPrivate::QStringList_join(this, sep);
|
||||
return QtPrivate::QStringList_join(this, sep.constData(), sep.length());
|
||||
}
|
||||
|
||||
inline QString QStringList::join(QChar sep) const
|
||||
{
|
||||
return QtPrivate::QStringList_join(this, &sep, 1);
|
||||
}
|
||||
|
||||
inline QStringList QStringList::filter(const QString &str, Qt::CaseSensitivity cs) const
|
||||
|
@ -66,6 +66,8 @@ private slots:
|
||||
void join() const;
|
||||
void join_data() const;
|
||||
void joinEmptiness() const;
|
||||
void joinChar() const;
|
||||
void joinChar_data() const;
|
||||
|
||||
void initializeList() const;
|
||||
};
|
||||
@ -362,6 +364,42 @@ void tst_QStringList::join_data() const
|
||||
<< QString("a b c");
|
||||
}
|
||||
|
||||
void tst_QStringList::joinChar() const
|
||||
{
|
||||
QFETCH(QStringList, input);
|
||||
QFETCH(QChar, separator);
|
||||
QFETCH(QString, expectedResult);
|
||||
|
||||
QCOMPARE(input.join(separator), expectedResult);
|
||||
}
|
||||
|
||||
void tst_QStringList::joinChar_data() const
|
||||
{
|
||||
QTest::addColumn<QStringList>("input");
|
||||
QTest::addColumn<QChar>("separator");
|
||||
QTest::addColumn<QString>("expectedResult");
|
||||
|
||||
QTest::newRow("data1")
|
||||
<< QStringList()
|
||||
<< QChar(QLatin1Char(' '))
|
||||
<< QString();
|
||||
|
||||
QTest::newRow("data5")
|
||||
<< (QStringList()
|
||||
<< QLatin1String("a")
|
||||
<< QLatin1String("b"))
|
||||
<< QChar(QLatin1Char(' '))
|
||||
<< QString("a b");
|
||||
|
||||
QTest::newRow("data6")
|
||||
<< (QStringList()
|
||||
<< QLatin1String("a")
|
||||
<< QLatin1String("b")
|
||||
<< QLatin1String("c"))
|
||||
<< QChar(QLatin1Char(' '))
|
||||
<< QString("a b c");
|
||||
}
|
||||
|
||||
void tst_QStringList::joinEmptiness() const
|
||||
{
|
||||
QStringList list;
|
||||
|
Loading…
Reference in New Issue
Block a user