From a7323614a09f341b5ef4fe9f359204658b930cc2 Mon Sep 17 00:00:00 2001 From: David Schulz <david.schulz@digia.com> Date: Thu, 3 Jan 2013 15:44:35 +0100 Subject: [PATCH] Editor: Fixed endless loop in BaseTextFind ...when trying to replace some regular expressions that could result in empty search results like ^, $ or \b. Task-number: QTCREATORBUG-8464 Change-Id: I91a304d3609c3ec20437c698d53e6a1819dfb924 Reviewed-by: Nikolai Kosjar <nikolai.kosjar@digia.com> --- src/plugins/find/basetextfind.cpp | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/src/plugins/find/basetextfind.cpp b/src/plugins/find/basetextfind.cpp index 5bcdb2835f5..df55f136c94 100644 --- a/src/plugins/find/basetextfind.cpp +++ b/src/plugins/find/basetextfind.cpp @@ -264,9 +264,23 @@ int BaseTextFind::replaceAll(const QString &before, const QString &after, regexp.setPatternSyntax(usesRegExp ? QRegExp::RegExp : QRegExp::FixedString); regexp.setCaseSensitivity((findFlags & Find::FindCaseSensitively) ? Qt::CaseSensitive : Qt::CaseInsensitive); QTextCursor found = findOne(regexp, editCursor, Find::textDocumentFlagsForFindFlags(findFlags)); - while (!found.isNull() - && (found.selectionStart() < found.selectionEnd() || after.length() > 0) - && inScope(found.selectionStart(), found.selectionEnd())) { + bool first = true; + while (!found.isNull() && inScope(found.selectionStart(), found.selectionEnd())) { + if (found == editCursor && !first) { + if (editCursor.atEnd()) + break; + // If the newly found QTextCursor is the same as recently edit one we have to move on, + // otherwise we would run into an endless loop for some regular expressions + // like ^ or \b. + QTextCursor newPosCursor = editCursor; + newPosCursor.movePosition(findFlags & Find::FindBackward ? + QTextCursor::PreviousCharacter : + QTextCursor::NextCharacter); + found = findOne(regexp, newPosCursor, Find::textDocumentFlagsForFindFlags(findFlags)); + continue; + } + if (first) + first = false; ++count; editCursor.setPosition(found.selectionStart()); editCursor.setPosition(found.selectionEnd(), QTextCursor::KeepAnchor); -- GitLab