From 2ccacfb5c9a013fb8d32dfc90e55f0890e7b7a3a Mon Sep 17 00:00:00 2001
From: Anton Kudryavtsev <a.kudryavtsev@netris.ru>
Date: Thu, 24 Dec 2015 17:00:58 +0300
Subject: [PATCH] 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>
---
 src/gui/text/qsyntaxhighlighter.cpp | 20 +++++++++++---------
 1 file changed, 11 insertions(+), 9 deletions(-)

diff --git a/src/gui/text/qsyntaxhighlighter.cpp b/src/gui/text/qsyntaxhighlighter.cpp
index f180a839b70..f300c181989 100644
--- a/src/gui/text/qsyntaxhighlighter.cpp
+++ b/src/gui/text/qsyntaxhighlighter.cpp
@@ -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();
-- 
GitLab