Merge pull request #10 from Kodiologist/escaping

Fix escaping bug (GH-9)
This commit is contained in:
Enrique García 2014-01-09 04:02:16 -08:00
commit b718a2e55c
2 changed files with 25 additions and 11 deletions

View File

@ -31,21 +31,21 @@ 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 )
return '"' .. str:gsub('"', '\\"') .. '"'
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 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 = str:gsub("\\", "\\\\"):gsub("(%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

View File

@ -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()