mirror of
https://github.com/kikito/inspect.lua.git
synced 2024-12-15 14:34:21 +00:00
Merge pull request #10 from Kodiologist/escaping
Fix escaping bug (GH-9)
This commit is contained in:
commit
b718a2e55c
16
inspect.lua
16
inspect.lua
@ -31,21 +31,21 @@ local inspect ={
|
|||||||
-- Apostrophizes the string if it has quotes, but not aphostrophes
|
-- Apostrophizes the string if it has quotes, but not aphostrophes
|
||||||
-- Otherwise, it returns a regular quoted string
|
-- Otherwise, it returns a regular quoted string
|
||||||
local function smartQuote(str)
|
local function smartQuote(str)
|
||||||
if string.match( string.gsub(str,"[^'\"]",""), '^"+$' ) then
|
if str:match('"') and not str:match("'") then
|
||||||
return "'" .. str .. "'"
|
return "'" .. str .. "'"
|
||||||
end
|
end
|
||||||
return string.format("%q", str )
|
return '"' .. str:gsub('"', '\\"') .. '"'
|
||||||
end
|
end
|
||||||
|
|
||||||
local controlCharsTranslation = {
|
local controlCharsTranslation = {
|
||||||
["\a"] = "\\a", ["\b"] = "\\b", ["\f"] = "\\f", ["\n"] = "\\n",
|
["\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 function escape(str)
|
||||||
local result, _ = string.gsub(str, "(%c)", unescapeChar)
|
local result = str:gsub("\\", "\\\\"):gsub("(%c)", escapeChar)
|
||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -222,7 +222,7 @@ function inspect.inspect(rootObject, options)
|
|||||||
puts('{')
|
puts('{')
|
||||||
down(function()
|
down(function()
|
||||||
if to_string_result then
|
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
|
if length >= 1 then tabify() end -- tabify the array values
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -268,7 +268,7 @@ function inspect.inspect(rootObject, options)
|
|||||||
local tv = type(v)
|
local tv = type(v)
|
||||||
|
|
||||||
if tv == 'string' then
|
if tv == 'string' then
|
||||||
puts(smartQuote(unescape(v)))
|
puts(smartQuote(escape(v)))
|
||||||
elseif tv == 'number' or tv == 'boolean' or tv == 'nil' then
|
elseif tv == 'number' or tv == 'boolean' or tv == 'nil' then
|
||||||
puts(tostring(v))
|
puts(tostring(v))
|
||||||
elseif tv == 'table' then
|
elseif tv == 'table' then
|
||||||
|
@ -24,10 +24,24 @@ describe( 'inspect', function()
|
|||||||
assert.equals(inspect("I have \"quotes\" and 'apostrophes'"), '"I have \\"quotes\\" and \'apostrophes\'"')
|
assert.equals(inspect("I have \"quotes\" and 'apostrophes'"), '"I have \\"quotes\\" and \'apostrophes\'"')
|
||||||
end)
|
end)
|
||||||
|
|
||||||
it('escapes escape control characters', function()
|
it('escapes newlines properly', function()
|
||||||
assert.equals(inspect('I have \n new \n lines'), '"I have \\\\n new \\\\n lines"')
|
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"')
|
|
||||||
end)
|
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)
|
end)
|
||||||
|
|
||||||
it('works with nil', function()
|
it('works with nil', function()
|
||||||
|
Loading…
Reference in New Issue
Block a user