diff --git a/src/gui/text/qsyntaxhighlighter.cpp b/src/gui/text/qsyntaxhighlighter.cpp
index f180a839b70945ada6a8c1b2009032cf167b6de2..f300c1819897c242054c5e30f15cc66e47c18afd 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();