diff options
Diffstat (limited to 'src/corelib/tools/qstringlist.cpp')
-rw-r--r-- | src/corelib/tools/qstringlist.cpp | 40 |
1 files changed, 33 insertions, 7 deletions
diff --git a/src/corelib/tools/qstringlist.cpp b/src/corelib/tools/qstringlist.cpp index eae9e0e5d9..a0b65ea554 100644 --- a/src/corelib/tools/qstringlist.cpp +++ b/src/corelib/tools/qstringlist.cpp @@ -447,6 +447,17 @@ void QtPrivate::QStringList_replaceInStrings(QStringList *that, const QRegularEx #endif // QT_NO_REGULAREXPRESSION #endif // QT_BOOTSTRAPPED +static int accumulatedSize(const QStringList &list, int seplen) +{ + int result = 0; + if (!list.isEmpty()) { + for (const auto &e : list) + result += e.size() + seplen; + result -= seplen; + } + return result; +} + /*! \fn QString QStringList::join(const QString &separator) const @@ -464,15 +475,9 @@ void QtPrivate::QStringList_replaceInStrings(QStringList *that, const QRegularEx */ QString QtPrivate::QStringList_join(const QStringList *that, const QChar *sep, int seplen) { - int totalLength = 0; + const int totalLength = accumulatedSize(*that, seplen); const int size = that->size(); - for (int i = 0; i < size; ++i) - totalLength += that->at(i).size(); - - if(size > 0) - totalLength += seplen * (size - 1); - QString res; if (totalLength == 0) return res; @@ -486,6 +491,27 @@ QString QtPrivate::QStringList_join(const QStringList *that, const QChar *sep, i } /*! + \fn QString QStringList::join(QLatin1String separator) const + \since 5.8 + \overload join() +*/ +QString QtPrivate::QStringList_join(const QStringList &list, QLatin1String sep) +{ + QString result; + if (!list.isEmpty()) { + result.reserve(accumulatedSize(list, sep.size())); + const auto end = list.end(); + auto it = list.begin(); + result += *it; + while (++it != end) { + result += sep; + result += *it; + } + } + return result; +} + +/*! \fn QStringList QStringList::operator+(const QStringList &other) const Returns a string list that is the concatenation of this string |