mirror of
https://github.com/kikito/inspect.lua.git
synced 2024-12-15 14:34:21 +00:00
Merge branch 'zerochars' of https://github.com/andreashofer123/inspect.lua into andreashofer123-zerochars
This commit is contained in:
commit
f8da52ca3d
12
inspect.lua
12
inspect.lua
@ -47,8 +47,18 @@ local controlCharsTranslation = {
|
|||||||
["\r"] = "\\r", ["\t"] = "\\t", ["\v"] = "\\v"
|
["\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 function escape(str)
|
||||||
local result = str:gsub("\\", "\\\\"):gsub("(%c)", controlCharsTranslation)
|
local result = str:gsub("\\", "\\\\"):gsub("(%c)%f[0-9]", controlCharsTranslationBeforeDigit):gsub("(%c)", controlCharsTranslation)
|
||||||
return result
|
return result
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -38,6 +38,11 @@ describe( 'inspect', function()
|
|||||||
assert.equals('"I have \\b a back space"', inspect('I have \b a back space'))
|
assert.equals('"I have \\b a back space"', inspect('I have \b a back space'))
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
it('escapes zeroes and other control characters properly', function()
|
||||||
|
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()
|
it('backslashes its backslashes', function()
|
||||||
assert.equals('"I have \\\\ a backslash"', inspect('I have \\ a backslash'))
|
assert.equals('"I have \\\\ a backslash"', inspect('I have \\ a backslash'))
|
||||||
assert.equals('"I have \\\\\\\\ two backslashes"', inspect('I have \\\\ two backslashes'))
|
assert.equals('"I have \\\\\\\\ two backslashes"', inspect('I have \\\\ two backslashes'))
|
||||||
|
Loading…
Reference in New Issue
Block a user