QSyntaxHighlighterPrivate: use erase and std::remove_if with QVector

... instead of using erase in a loop, with quadratic complexity.

Change-Id: If30c6c99a775aec07eef9ddf953e944dc916b5a2
Reviewed-by: Lars Knoll <lars.knoll@theqtcompany.com>
Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
This commit is contained in:
Anton Kudryavtsev 2015-12-24 17:00:58 +03:00
parent b4c11c7cc5
commit 2ccacfb5c9

View File

@ -44,6 +44,8 @@
#include <qdebug.h>
#include <qtimer.h>
#include <algorithm>
QT_BEGIN_NAMESPACE
class QSyntaxHighlighterPrivate : public QObjectPrivate
@ -96,15 +98,15 @@ void QSyntaxHighlighterPrivate::applyFormatChanges()
const int preeditAreaLength = layout->preeditAreaText().length();
if (preeditAreaLength != 0) {
QVector<QTextLayout::FormatRange>::Iterator it = ranges.begin();
while (it != ranges.end()) {
if (it->start >= preeditAreaStart
&& it->start + it->length <= preeditAreaStart + preeditAreaLength) {
++it;
} else {
it = ranges.erase(it);
formatsChanged = true;
}
auto isOutsidePreeditArea = [=](const QTextLayout::FormatRange &range) {
return range.start < preeditAreaStart
|| range.start + range.length > preeditAreaStart + preeditAreaLength;
};
const auto it = std::remove_if(ranges.begin(), ranges.end(),
isOutsidePreeditArea);
if (it != ranges.end()) {
ranges.erase(it, ranges.end());
formatsChanged = true;
}
} else if (!ranges.isEmpty()) {
ranges.clear();