From c282e286bbf1d61ae9ffef7cbd14ca0019e68c5f Mon Sep 17 00:00:00 2001 From: Starbeamrainbowlabs Date: Wed, 28 Jul 2021 17:49:18 +0100 Subject: [PATCH] split_shell: correctly handle escaping edge cases --- worldeditadditions/utils/strings/split_shell.lua | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/worldeditadditions/utils/strings/split_shell.lua b/worldeditadditions/utils/strings/split_shell.lua index 6b8b2ba..46f1e0b 100644 --- a/worldeditadditions/utils/strings/split_shell.lua +++ b/worldeditadditions/utils/strings/split_shell.lua @@ -36,7 +36,10 @@ function split_shell(text) if curchar == "\"" and prevchar ~= "\\" and is_whitespace(nextchar) then -- It's the end of a quote! mode = "NORMAL" - elseif (curchar == "\\" and nextchar ~= "\"" and not is_whitespace(nextnextchar)) or curchar ~= "\\" then + elseif (curchar == "\\" and ( + nextchar ~= "\"" + or (nextchar == "\"" and not is_whitespace(nextnextchar)) + )) or curchar ~= "\\" then -- It's a regular character table.insert(acc, curchar) end @@ -44,7 +47,10 @@ function split_shell(text) if curchar == "'" and prevchar ~= "\\" and is_whitespace(nextchar) then -- It's the end of a quote! mode = "NORMAL" - elseif (curchar == "\\" and nextchar ~= "'" and not is_whitespace(nextnextchar)) or curchar ~= "\\" then + elseif (curchar == "\\" and ( + nextchar ~= "'" + or (nextchar == "'" and not is_whitespace(nextnextchar)) + )) or curchar ~= "\\" then -- It's a regular character table.insert(acc, curchar) end @@ -70,3 +76,8 @@ test("yay \"yay yay\" yay") test("yay \"yay\\\" yay\" yay") test("yay \"yay 'inside quotes' yay\" yay") test("yay 'inside quotes' another") +test("y\"ay \"yay 'in\\\"side quotes' yay\" y\\\"ay") + +-- for i=1,10000 do +-- split_shell("y\"ay \"yay 'in\\\"side quotes' yay\" y\\\"ay") +-- end