From dfebe439db76b5c9fd9b4e3877857e2449c8ad87 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Wed, 13 Mar 2019 14:04:01 -0300 Subject: New conversion specifier '%p' for 'string.format' The call 'string.format("%p", val)' gives a Lua equivalent to the C API function 'lua_topointer'. --- testes/strings.lua | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) (limited to 'testes') diff --git a/testes/strings.lua b/testes/strings.lua index da53a87e..8bcbb391 100644 --- a/testes/strings.lua +++ b/testes/strings.lua @@ -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 = '"ílo"\n\\' assert(string.format('%q%s', x, x) == '"\\"ílo\\"\\\n\\\\""ílo"\n\\') -- cgit v1.2.3-55-g6feb