New conversion specifier '%p' for 'string.format'

The call 'string.format("%p", val)' gives a Lua equivalent to the
C API function 'lua_topointer'.
This commit is contained in:
Roberto Ierusalimschy
2019-03-13 14:04:01 -03:00
parent cf71a5ddc7
commit dfebe439db
3 changed files with 31 additions and 3 deletions

View File

@@ -153,6 +153,22 @@ else -- compatible coercion
assert(tostring(-1203 + 0.0) == "-1203")
end
do -- tests for '%p' format
-- not much to test, as C does not specify what '%p' does.
-- ("The value of the pointer is converted to a sequence of printing
-- characters, in an implementation-defined manner.")
local null = string.format("%p", nil)
assert(string.format("%p", {}) ~= null)
assert(string.format("%p", 4) == null)
assert(string.format("%p", print) ~= null)
assert(string.format("%p", coroutine.running()) ~= null)
assert(string.format("%p", {}) ~= string.format("%p", {}))
assert(string.format("%p", string.rep("a", 10)) ==
string.format("%p", string.rep("a", 10))) -- short strings
assert(string.format("%p", string.rep("a", 300)) ~=
string.format("%p", string.rep("a", 300))) -- long strings
assert(#string.format("%90p", {}) == 90)
end
x = '"<22>lo"\n\\'
assert(string.format('%q%s', x, x) == '"\\"<22>lo\\"\\\n\\\\""<22>lo"\n\\')