Commit b1a714ed authored by hluk's avatar hluk Committed by hjk

FakeVim: Fix infinite loop when replacing empty text

Change-Id: Ie4ba6420889b0a6a5712b43a11f8366aa9a30edc
Reviewed-by: default avatarEike Ziller <eike.ziller@digia.com>
Reviewed-by: default avatarhjk <hjk121@nokiamail.com>
parent 4fd4f5d9
......@@ -2206,6 +2206,17 @@ void FakeVimPlugin::test_vim_substitute()
COMMAND("'<,'>s/^/*", "abc" N "**def" N X "**ghi" N "jkl");
KEYS("u", "abc" N X "*def" N "*ghi" N "jkl");
KEYS("gv:s/^/+<CR>", "abc" N "+*def" N X "+*ghi" N "jkl");
// replace empty string
data.setText("abc");
COMMAND("s//--/g", "--a--b--c");
// remove characters
data.setText("abc def");
COMMAND("s/[abde]//g", "c f");
COMMAND("undo | s/[bcef]//g", "a d");
COMMAND("undo | s/\\w//g", " ");
COMMAND("undo | s/f\\|$/-/g", "abc de-");
}
void FakeVimPlugin::test_vim_ex_yank()
......
......@@ -623,10 +623,22 @@ static bool substituteText(QString *text, QRegExp &pattern, const QString &repla
{
bool substituted = false;
int pos = 0;
int right = -1;
while (true) {
pos = pattern.indexIn(*text, pos, QRegExp::CaretAtZero);
if (pos == -1)
break;
// ensure that substitution is advancing towards end of line
if (right == text->size() - pos) {
++pos;
if (pos == text->size())
break;
continue;
}
right = text->size() - pos;
substituted = true;
QString matched = text->mid(pos, pattern.cap(0).size());
QString repl;
......@@ -652,7 +664,7 @@ static bool substituteText(QString *text, QRegExp &pattern, const QString &repla
}
}
text->replace(pos, matched.size(), repl);
pos += qMax(1, repl.size());
pos += (repl.isEmpty() && matched.isEmpty()) ? 1 : repl.size();
if (pos >= text->size() || !global)
break;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment