From ef3ae6b2d7926f9d37de2510b7635904920457d9 Mon Sep 17 00:00:00 2001 From: Andreas Hofer Date: Thu, 31 Mar 2016 21:22:23 +0200 Subject: [PATCH] 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