From afd9b2a4f2187b802f1febcff9b54073c6d8943d Mon Sep 17 00:00:00 2001 From: Peter Melnichenko Date: Fri, 28 Oct 2016 14:09:20 +0300 Subject: Refactor windows argument quoting functions Use string replacements instead of functions in calls to gsub. --- src/luarocks/fs/win32.lua | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) (limited to 'src') diff --git a/src/luarocks/fs/win32.lua b/src/luarocks/fs/win32.lua index c99cc895..14804713 100644 --- a/src/luarocks/fs/win32.lua +++ b/src/luarocks/fs/win32.lua @@ -35,19 +35,6 @@ end local drive_letter = "[%.a-zA-Z]?:?[\\/]" -local win_escape_chars = { - ["%"] = "%%", - ['"'] = '\\"', -} - -local function q_escaper(bs, q) - return ("\\"):rep(2*#bs-1) .. (q or "\\") -end - -local function p_escaper(bs) - return bs .. bs .. '"%"' -end - --- Quote argument for shell processing. Fixes paths on Windows. -- Adds double quotes and escapes. -- @param arg string: Unquoted argument. @@ -61,11 +48,11 @@ function win32.Q(arg) if arg == "\\" then return '\\' -- CHDIR needs special handling for root dir end - -- URLs and anything else - arg = arg:gsub('(\\+)(")', q_escaper) - arg = arg:gsub('(\\+)$', q_escaper) - arg = arg:gsub('"', win_escape_chars) - arg = arg:gsub('(\\*)%%', p_escaper) + -- URLs and anything else + arg = arg:gsub('\\(\\*)"', '\\%1%1"') + arg = arg:gsub('\\+$', '%0%0') + arg = arg:gsub('"', '\\"') + arg = arg:gsub('(\\*)%%', '%1%1"%%"') return '"' .. arg .. '"' end @@ -83,9 +70,10 @@ function win32.Qb(arg) return '\\' -- CHDIR needs special handling for root dir end -- URLs and anything else - arg = arg:gsub('(\\+)(")', q_escaper) - arg = arg:gsub('(\\+)$', q_escaper) - arg = arg:gsub('[%%"]', win_escape_chars) + arg = arg:gsub('\\(\\*)"', '\\%1%1"') + arg = arg:gsub('\\+$', '%0%0') + arg = arg:gsub('"', '\\"') + arg = arg:gsub('%%', '%%%%') return '"' .. arg .. '"' end -- cgit v1.2.3-55-g6feb