From df482c613a38c35b11c12c814fca0e4de6916445 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enrique=20Garc=C3=ADa=20Cota?= Date: Fri, 12 Nov 2021 10:48:57 +0100 Subject: [PATCH] escape DEL character. fixes #40 --- inspect.lua | 7 ++++--- spec/inspect_spec.lua | 7 ++++++- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/inspect.lua b/inspect.lua index e2e3806..dedd91f 100644 --- a/inspect.lua +++ b/inspect.lua @@ -46,12 +46,12 @@ local function smartQuote(str) return '"' .. str:gsub('"', '\\"') .. '"' end --- \a => '\\a', \0 => '\\0', 31 => '\31' +-- \a => '\\a', \0 => nil local shortControlCharEscapes = { ["\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 local ch = string.char(i) if not shortControlCharEscapes[ch] then @@ -59,6 +59,7 @@ for i=0, 31 do longControlCharEscapes[ch] = string.format("\\%03d", i) end end +--longControlCharEscapes["\127"]="\\127" local function escape(str) return (str:gsub("\\", "\\\\") diff --git a/spec/inspect_spec.lua b/spec/inspect_spec.lua index edea720..a6ddd00 100644 --- a/spec/inspect_spec.lua +++ b/spec/inspect_spec.lua @@ -43,7 +43,12 @@ describe( 'inspect', function() inspect('Here are some control characters: \0 \1 \6 \17 \27 \31')) 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"', inspect('Control chars followed by digits \0001 \0011 \0061 \0171 \0271 \0311')) end)