escape DEL character. fixes #40

This commit is contained in:
Enrique García Cota 2021-11-12 10:48:57 +01:00
parent b611db6bfa
commit df482c613a
2 changed files with 10 additions and 4 deletions

View File

@ -46,12 +46,12 @@ local function smartQuote(str)
return '"' .. str:gsub('"', '\\"') .. '"' return '"' .. str:gsub('"', '\\"') .. '"'
end end
-- \a => '\\a', \0 => '\\0', 31 => '\31' -- \a => '\\a', \0 => nil
local shortControlCharEscapes = { local shortControlCharEscapes = {
["\a"] = "\\a", ["\b"] = "\\b", ["\f"] = "\\f", ["\n"] = "\\n", ["\a"] = "\\a", ["\b"] = "\\b", ["\f"] = "\\f", ["\n"] = "\\n",
["\r"] = "\\r", ["\t"] = "\\t", ["\v"] = "\\v" ["\r"] = "\\r", ["\t"] = "\\t", ["\v"] = "\\v", ["\127"] = "\\127",
} }
local longControlCharEscapes = {} -- \a => nil, \0 => \000, 31 => \031 local longControlCharEscapes = {["\127"]="\127"} -- \a => nil, \0 => \000, 31 => \031
for i=0, 31 do for i=0, 31 do
local ch = string.char(i) local ch = string.char(i)
if not shortControlCharEscapes[ch] then if not shortControlCharEscapes[ch] then
@ -59,6 +59,7 @@ for i=0, 31 do
longControlCharEscapes[ch] = string.format("\\%03d", i) longControlCharEscapes[ch] = string.format("\\%03d", i)
end end
end end
--longControlCharEscapes["\127"]="\\127"
local function escape(str) local function escape(str)
return (str:gsub("\\", "\\\\") return (str:gsub("\\", "\\\\")

View File

@ -43,7 +43,12 @@ describe( 'inspect', function()
inspect('Here are some control characters: \0 \1 \6 \17 \27 \31')) inspect('Here are some control characters: \0 \1 \6 \17 \27 \31'))
end) end)
it('escapes unnamed control characters with 3 digits when they are followed by numbers', function() it('escapes DEL', function()
assert.equals('"DEL: \\127"',
inspect('DEL: \127'))
end)
it('escapes unnamed control characters with 4 digits when they are followed by numbers', function()
assert.equals('"Control chars followed by digits \\0001 \\0011 \\0061 \\0171 \\0271 \\0311"', assert.equals('"Control chars followed by digits \\0001 \\0011 \\0061 \\0171 \\0271 \\0311"',
inspect('Control chars followed by digits \0001 \0011 \0061 \0171 \0271 \0311')) inspect('Control chars followed by digits \0001 \0011 \0061 \0171 \0271 \0311'))
end) end)