From 1605795a4a45b736a4556d6965344ca58bd5e003 Mon Sep 17 00:00:00 2001 From: "email@jochen-baier.de" Date: Sun, 30 Jan 2022 15:52:48 +0100 Subject: [PATCH] FakeVim substitute command: Support for backslash in pattern Fixes findUnescaped() function to consider escaped backslashes --- src/plugins/fakevim/fakevim_test.cpp | 44 ++++++++++++++++++++++++++ src/plugins/fakevim/fakevimhandler.cpp | 11 ++++++- 2 files changed, 54 insertions(+), 1 deletion(-) diff --git a/src/plugins/fakevim/fakevim_test.cpp b/src/plugins/fakevim/fakevim_test.cpp index f8b37bd814..ec7c4df68b 100644 --- a/src/plugins/fakevim/fakevim_test.cpp +++ b/src/plugins/fakevim/fakevim_test.cpp @@ -3029,6 +3029,50 @@ void FakeVimPlugin::test_vim_substitute() COMMAND("undo | s/\\(b...E\\)/\\U\\1/g", "aBC DEfGh"); COMMAND("undo | s/\\(C..E\\)/\\l\\1/g", "abc dEfGh"); COMMAND("undo | s/\\(b...E\\)/\\L\\1/g", "abc defGh"); + + // replace 1 backslash with 1 forward slash (seperator: /) + data.setText(R"(abc\def)"); + COMMAND(R"(s/\\/\/)", X "abc/def"); + + // replace 1 backslash with X normal on line (seperator: /) + data.setText(R"(abc\def\ghi)"); + COMMAND(R"(s/\\/X/g)", X "abcXdefXghi"); + + // replace 1 backslash with 1 forward slash on line (seperator: /) + data.setText(R"(abc\def\ghi)"); + COMMAND(R"(s/\\/\//g)", X "abc/def/ghi"); + + // replace 1 backslash with 1 forward slash + data.setText(R"(abc\def)"); + COMMAND(R"(s#\\#/)", X "abc/def"); + + // replace 1 backslash with 1 forward slash on line + data.setText(R"(abc\def\ghi)"); + COMMAND(R"(s#\\#/#g)", X "abc/def/ghi"); + + // replace 2 backslash with 2 forward slash + data.setText(R"(abc\\def)"); + COMMAND(R"(s#\\\\#//)", X "abc//def"); + + // replace 2 backslash with 2 forward slash on line + data.setText(R"(abc\\def\\ghi)"); + COMMAND(R"(s#\\\\#//#g)", X "abc//def//ghi"); + + // replace 1 backslash with 1 forward slash last char + data.setText(R"(abc\)"); + COMMAND(R"(s#\\#/)", X "abc/"); + + // replace 1 backslash with 1 forward slash first char + data.setText(R"(\abc)"); + COMMAND(R"(s#\\#/)", X "/abc"); + + // replace 1 # with 2 # on line + data.setText(R"(abc#def#ghi)"); + COMMAND(R"(s#\##\#\##g)", X "abc##def##ghi"); + + // replace 2 # with 4 # on line + data.setText(R"(abc##def##ghi)"); + COMMAND(R"(s#\#\##\#\#\#\##g)", X "abc####def####ghi"); } void FakeVimPlugin::test_vim_ex_commandbuffer_paste() diff --git a/src/plugins/fakevim/fakevimhandler.cpp b/src/plugins/fakevim/fakevimhandler.cpp index aa8b74a00e..2c5193cbe4 100644 --- a/src/plugins/fakevim/fakevimhandler.cpp +++ b/src/plugins/fakevim/fakevimhandler.cpp @@ -816,9 +816,18 @@ static bool substituteText(QString *text, static int findUnescaped(QChar c, const QString &line, int from) { + bool singleBackSlashBefore=false; for (int i = from; i < line.size(); ++i) { - if (line.at(i) == c && (i == 0 || line.at(i - 1) != '\\')) + const QChar& currentChar = line.at(i); + if (currentChar == '\\') { + singleBackSlashBefore=!singleBackSlashBefore; + continue; + } + + if (currentChar == c && !singleBackSlashBefore) return i; + + singleBackSlashBefore = false; } return -1; } -- 2.31.0.windows.1