FakeVim: Fix infinite loop when replacing empty text

Change-Id: Ie4ba6420889b0a6a5712b43a11f8366aa9a30edc
Reviewed-by: Eike Ziller <eike.ziller@digia.com>
Reviewed-by: hjk <hjk121@nokiamail.com>
This commit is contained in:
hluk
2013-12-01 15:09:04 +01:00
committed by hjk
parent 4fd4f5d9fc
commit b1a714ed44
2 changed files with 24 additions and 1 deletions

View File

@@ -2206,6 +2206,17 @@ void FakeVimPlugin::test_vim_substitute()
COMMAND("'<,'>s/^/*", "abc" N "**def" N X "**ghi" N "jkl"); COMMAND("'<,'>s/^/*", "abc" N "**def" N X "**ghi" N "jkl");
KEYS("u", "abc" N X "*def" N "*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"); 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() void FakeVimPlugin::test_vim_ex_yank()

View File

@@ -623,10 +623,22 @@ static bool substituteText(QString *text, QRegExp &pattern, const QString &repla
{ {
bool substituted = false; bool substituted = false;
int pos = 0; int pos = 0;
int right = -1;
while (true) { while (true) {
pos = pattern.indexIn(*text, pos, QRegExp::CaretAtZero); pos = pattern.indexIn(*text, pos, QRegExp::CaretAtZero);
if (pos == -1) if (pos == -1)
break; 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; substituted = true;
QString matched = text->mid(pos, pattern.cap(0).size()); QString matched = text->mid(pos, pattern.cap(0).size());
QString repl; QString repl;
@@ -652,7 +664,7 @@ static bool substituteText(QString *text, QRegExp &pattern, const QString &repla
} }
} }
text->replace(pos, matched.size(), repl); text->replace(pos, matched.size(), repl);
pos += qMax(1, repl.size()); pos += (repl.isEmpty() && matched.isEmpty()) ? 1 : repl.size();
if (pos >= text->size() || !global) if (pos >= text->size() || !global)
break; break;