Optimize QString::section(QString)

The port from split() to splitRef() speeds up typical section()
calls (where the separator is included) by ca. 1/3.

The complete truth includes that section() calls where the
separator is not found seem to have gotten twice slower.
But that's a corner-case.

Change-Id: I7e957cb65fccfd095ac522d523aef3464425e4e4
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
This commit is contained in:
Marc Mutz 2014-08-14 08:46:30 +02:00
parent f02b849e97
commit f1530d0be1

View File

@ -3997,10 +3997,9 @@ int QString::count(const QRegularExpression &re) const
QString QString::section(const QString &sep, int start, int end, SectionFlags flags) const
{
QStringList sections = split(sep, KeepEmptyParts,
(flags & SectionCaseInsensitiveSeps) ? Qt::CaseInsensitive : Qt::CaseSensitive);
const QVector<QStringRef> sections = splitRef(sep, KeepEmptyParts,
(flags & SectionCaseInsensitiveSeps) ? Qt::CaseInsensitive : Qt::CaseSensitive);
const int sectionsSize = sections.size();
if (!(flags & SectionSkipEmpty)) {
if (start < 0)
start += sectionsSize;
@ -4024,7 +4023,7 @@ QString QString::section(const QString &sep, int start, int end, SectionFlags fl
QString ret;
int first_i = start, last_i = end;
for (int i = 0; x <= end && i < sectionsSize; ++i) {
QString section = sections.at(i);
const QStringRef &section = sections.at(i);
const bool empty = section.isEmpty();
if (x >= start) {
if(x == start)