From 2a5205e53cb2b615ce41e09d308324bd12e78d66 Mon Sep 17 00:00:00 2001 From: Andreas Hofer Date: Tue, 29 Mar 2016 21:58:17 +0200 Subject: [PATCH 1/3] fix problem with escaping zero and other control characters --- inspect.lua | 11 ++++++++--- spec/inspect_spec.lua | 5 +++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/inspect.lua b/inspect.lua index 39f43af..a053da6 100644 --- a/inspect.lua +++ b/inspect.lua @@ -41,12 +41,17 @@ local function smartQuote(str) end local controlCharsTranslation = { - ["\a"] = "\\a", ["\b"] = "\\b", ["\f"] = "\\f", ["\n"] = "\\n", - ["\r"] = "\\r", ["\t"] = "\\t", ["\v"] = "\\v" + ["\\7"] = "\\a", ["\\007"] = "\\a", + ["\\8"] = "\\b", ["\\008"] = "\\b", + ["\\12"] = "\\f", ["\\012"] = "\\f", + ["\\13"] = "\\r", ["\\013"] = "\\r", + ["\\9"] = "\\t", ["\\009"] = "\\t", + ["\\11"] = "\\v", ["\\011"] = "\\v", } local function escape(str) - local result = str:gsub("\\", "\\\\"):gsub("(%c)", controlCharsTranslation) + local f = string.format("%q", str) + local result = f:gsub("\\%d%d?%d?", controlCharsTranslation):gsub("\\\n", "\\n"):gsub('\\"', '"'):sub(2,-2) return result end diff --git a/spec/inspect_spec.lua b/spec/inspect_spec.lua index 9a81642..9264033 100644 --- a/spec/inspect_spec.lua +++ b/spec/inspect_spec.lua @@ -38,6 +38,11 @@ describe( 'inspect', function() assert.equals('"I have \\b a back space"', inspect('I have \b a back space')) end) + it('escapes zeroes and other control characters properly', function() + assert.equals('"I have \\0 zeroes \\0000 and other \\1 control \\0010 characters"', + inspect('I have \0 zeroes \0000 and other \1 control \0010 characters')) + end) + it('backslashes its backslashes', function() assert.equals('"I have \\\\ a backslash"', inspect('I have \\ a backslash')) assert.equals('"I have \\\\\\\\ two backslashes"', inspect('I have \\\\ two backslashes')) From ef3ae6b2d7926f9d37de2510b7635904920457d9 Mon Sep 17 00:00:00 2001 From: Andreas Hofer Date: Thu, 31 Mar 2016 21:22:23 +0200 Subject: [PATCH 2/3] alternative implementation to fix problem with Lua 5.1 --- inspect.lua | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/inspect.lua b/inspect.lua index a053da6..07c443c 100644 --- a/inspect.lua +++ b/inspect.lua @@ -41,17 +41,22 @@ local function smartQuote(str) end local controlCharsTranslation = { - ["\\7"] = "\\a", ["\\007"] = "\\a", - ["\\8"] = "\\b", ["\\008"] = "\\b", - ["\\12"] = "\\f", ["\\012"] = "\\f", - ["\\13"] = "\\r", ["\\013"] = "\\r", - ["\\9"] = "\\t", ["\\009"] = "\\t", - ["\\11"] = "\\v", ["\\011"] = "\\v", + ["\a"] = "\\a", ["\b"] = "\\b", ["\f"] = "\\f", ["\n"] = "\\n", + ["\r"] = "\\r", ["\t"] = "\\t", ["\v"] = "\\v" } +local controlCharsTranslationBeforeDigit = {} + +for i=0, 31 do + local ch = string.char(i) + if not controlCharsTranslation[ch] then + controlCharsTranslation[ch] = "\\"..i + controlCharsTranslationBeforeDigit[ch] = string.format("\\%03d",i) + end +end + local function escape(str) - local f = string.format("%q", str) - local result = f:gsub("\\%d%d?%d?", controlCharsTranslation):gsub("\\\n", "\\n"):gsub('\\"', '"'):sub(2,-2) + local result = str:gsub("\\", "\\\\"):gsub("(%c)%f[0-9]", controlCharsTranslationBeforeDigit):gsub("(%c)", controlCharsTranslation) return result end From 864066c51e510b2291b12cdcc7eb30d4e4870d4b Mon Sep 17 00:00:00 2001 From: Andreas Hofer Date: Sun, 3 Apr 2016 19:51:48 +0200 Subject: [PATCH 3/3] added more control characters to test case --- spec/inspect_spec.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/inspect_spec.lua b/spec/inspect_spec.lua index 9264033..f6456f5 100644 --- a/spec/inspect_spec.lua +++ b/spec/inspect_spec.lua @@ -39,8 +39,8 @@ describe( 'inspect', function() end) it('escapes zeroes and other control characters properly', function() - assert.equals('"I have \\0 zeroes \\0000 and other \\1 control \\0010 characters"', - inspect('I have \0 zeroes \0000 and other \1 control \0010 characters')) + assert.equals('"I have \\0 zeroes \\0000 and other \\1 control \\0010 characters \\6 \\17 \\0279 \\31"', + inspect('I have \0 zeroes \0000 and other \1 control \0010 characters \6 \17 \0279 \31')) end) it('backslashes its backslashes', function()