From 6373d7748a90666de519b6a944f8a017efd13976 Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Tue, 7 Jan 2014 08:56:10 -0500 Subject: [PATCH 1/5] added more escaping tests --- spec/inspect_spec.lua | 20 +++++++++++++++++--- 1 file changed, 17 insertions(+), 3 deletions(-) diff --git a/spec/inspect_spec.lua b/spec/inspect_spec.lua index 8c55052..72e38d4 100644 --- a/spec/inspect_spec.lua +++ b/spec/inspect_spec.lua @@ -24,10 +24,24 @@ describe( 'inspect', function() assert.equals(inspect("I have \"quotes\" and 'apostrophes'"), '"I have \\"quotes\\" and \'apostrophes\'"') end) - it('escapes escape control characters', function() - assert.equals(inspect('I have \n new \n lines'), '"I have \\\\n new \\\\n lines"') - assert.equals(inspect('I have \b a back space'), '"I have \\\\b a back space"') + it('escapes newlines properly', function() + assert.equals(inspect('I have \n new \n lines'), '"I have \\n new \\n lines"') end) + + it('escapes tabs properly', function() + assert.equals(inspect('I have \t a tab character'), '"I have \\t a tab character"') + end) + + it('escapes backspaces properly', function() + assert.equals(inspect('I have \b a back space'), '"I have \\b a back space"') + end) + + it('backslashes its backslashes', function() + assert.equals(inspect('I have \\ a backslash'), '"I have \\\\ a backslash"') + assert.equals(inspect('I have \\\\ two backslashes'), '"I have \\\\\\\\ two backslashes"') + assert.equals(inspect('I have \\\n a backslash followed by a newline'), '"I have \\\\\\n a backslash followed by a newline"') + end) + end) it('works with nil', function() From a5291a2a2cdb1638cd85ff23759f8edf04b07c90 Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Tue, 7 Jan 2014 09:00:54 -0500 Subject: [PATCH 2/5] renamed "unescape" functions to "escape" These functions replace raw characters with escape sequences rather than the reverse. --- inspect.lua | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/inspect.lua b/inspect.lua index ed6fa52..af9ef56 100644 --- a/inspect.lua +++ b/inspect.lua @@ -42,10 +42,10 @@ local controlCharsTranslation = { ["\r"] = "\\r", ["\t"] = "\\t", ["\v"] = "\\v", ["\\"] = "\\\\" } -local function unescapeChar(c) return controlCharsTranslation[c] end +local function escapeChar(c) return controlCharsTranslation[c] end -local function unescape(str) - local result, _ = string.gsub(str, "(%c)", unescapeChar) +local function escape(str) + local result = string.gsub(str, "(%c)", escapeChar) return result end @@ -222,7 +222,7 @@ function inspect.inspect(rootObject, options) puts('{') down(function() if to_string_result then - puts(' -- ', unescape(to_string_result)) + puts(' -- ', escape(to_string_result)) if length >= 1 then tabify() end -- tabify the array values end @@ -268,7 +268,7 @@ function inspect.inspect(rootObject, options) local tv = type(v) if tv == 'string' then - puts(smartQuote(unescape(v))) + puts(smartQuote(escape(v))) elseif tv == 'number' or tv == 'boolean' or tv == 'nil' then puts(tostring(v)) elseif tv == 'table' then From 588486afdeb1ba66fd273b6585b8124b10d54d1e Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Tue, 7 Jan 2014 09:20:31 -0500 Subject: [PATCH 3/5] removed ineffectual entry in controlCharsTranslation --- inspect.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/inspect.lua b/inspect.lua index af9ef56..c620fec 100644 --- a/inspect.lua +++ b/inspect.lua @@ -39,7 +39,7 @@ end local controlCharsTranslation = { ["\a"] = "\\a", ["\b"] = "\\b", ["\f"] = "\\f", ["\n"] = "\\n", - ["\r"] = "\\r", ["\t"] = "\\t", ["\v"] = "\\v", ["\\"] = "\\\\" + ["\r"] = "\\r", ["\t"] = "\\t", ["\v"] = "\\v" } local function escapeChar(c) return controlCharsTranslation[c] end From 0303fec453c9ad3775f55b737d8e4f62d259f259 Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Tue, 7 Jan 2014 09:21:59 -0500 Subject: [PATCH 4/5] tweaked string escaping, fixing GH-9 --- inspect.lua | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/inspect.lua b/inspect.lua index c620fec..53498fc 100644 --- a/inspect.lua +++ b/inspect.lua @@ -34,7 +34,7 @@ local function smartQuote(str) if string.match( string.gsub(str,"[^'\"]",""), '^"+$' ) then return "'" .. str .. "'" end - return string.format("%q", str ) + return string.format("%q", str ):gsub("\\\n", "\n") end local controlCharsTranslation = { @@ -268,7 +268,7 @@ function inspect.inspect(rootObject, options) local tv = type(v) if tv == 'string' then - puts(smartQuote(escape(v))) + puts(escape(smartQuote(v))) elseif tv == 'number' or tv == 'boolean' or tv == 'nil' then puts(tostring(v)) elseif tv == 'table' then From fbb524a813b1bd1e22776ec82d32473faf79f11d Mon Sep 17 00:00:00 2001 From: Kodi Arfer Date: Tue, 7 Jan 2014 10:57:42 -0500 Subject: [PATCH 5/5] removed use of string.quote("%q", str) --- inspect.lua | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/inspect.lua b/inspect.lua index 53498fc..2be3a21 100644 --- a/inspect.lua +++ b/inspect.lua @@ -31,10 +31,10 @@ local inspect ={ -- Apostrophizes the string if it has quotes, but not aphostrophes -- Otherwise, it returns a regular quoted string local function smartQuote(str) - if string.match( string.gsub(str,"[^'\"]",""), '^"+$' ) then + if str:match('"') and not str:match("'") then return "'" .. str .. "'" end - return string.format("%q", str ):gsub("\\\n", "\n") + return '"' .. str:gsub('"', '\\"') .. '"' end local controlCharsTranslation = { @@ -45,7 +45,7 @@ local controlCharsTranslation = { local function escapeChar(c) return controlCharsTranslation[c] end local function escape(str) - local result = string.gsub(str, "(%c)", escapeChar) + local result = str:gsub("\\", "\\\\"):gsub("(%c)", escapeChar) return result end @@ -268,7 +268,7 @@ function inspect.inspect(rootObject, options) local tv = type(v) if tv == 'string' then - puts(escape(smartQuote(v))) + puts(smartQuote(escape(v))) elseif tv == 'number' or tv == 'boolean' or tv == 'nil' then puts(tostring(v)) elseif tv == 'table' then