aboutsummaryrefslogtreecommitdiffstats
path: root/util/format.lua
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2021-12-11 13:39:58 +0100
committerKim Alvefur <zash@zash.se>2021-12-11 13:39:58 +0100
commit0ddd204a2715415ff1d6c9c69c7c871f17745b62 (patch)
treef3d9b1003238a1bfc02f8096eaaf7200f16b71c1 /util/format.lua
parent1eca4e8870f69699f7bc9bbd132a7bf64ca70918 (diff)
downloadprosody-0ddd204a2715415ff1d6c9c69c7c871f17745b62.tar.gz
prosody-0ddd204a2715415ff1d6c9c69c7c871f17745b62.zip
util.format: Also handle the %p format added in Lua 5.4
Diffstat (limited to 'util/format.lua')
-rw-r--r--util/format.lua21
1 files changed, 18 insertions, 3 deletions
diff --git a/util/format.lua b/util/format.lua
index 8168ccaa..da48c60a 100644
--- a/util/format.lua
+++ b/util/format.lua
@@ -29,6 +29,7 @@ local control_symbols = {
["\027"] = "\226\144\155", ["\028"] = "\226\144\156", ["\029"] = "\226\144\157",
["\030"] = "\226\144\158", ["\031"] = "\226\144\159", ["\127"] = "\226\144\161",
};
+local supports_p = pcall(string.format, "%p", "");
local function format(formatstring, ...)
local args = pack(...);
@@ -44,11 +45,12 @@ local function format(formatstring, ...)
-- The options c, d, E, e, f, g, G, i, o, u, X, and x all expect a number as argument, whereas q and s expect a string.
-- This function does not accept string values containing embedded zeros, except as arguments to the q option.
-- a and A are only in Lua 5.2+
+ -- Lua 5.4 adds a p format that produces a pointer
-- process each format specifier
local i = 0;
- formatstring = formatstring:gsub("%%[^cdiouxXaAeEfgGqs%%]*[cdiouxXaAeEfgGqs%%]", function(spec)
+ formatstring = formatstring:gsub("%%[^cdiouxXaAeEfgGpqs%%]*[cdiouxXaAeEfgGpqs%%]", function(spec)
if spec == "%%" then return end
i = i + 1;
local arg = args[i];
@@ -66,7 +68,7 @@ local function format(formatstring, ...)
return
end
- if option ~= "s" and option ~= "q" then
+ if option ~= "s" and option ~= "q" and option ~= "p" then
-- all other options expect numbers
if t ~= "number" then
-- arg isn't number as expected?
@@ -82,7 +84,15 @@ local function format(formatstring, ...)
end
end
- if t == "string" then
+
+ if option == "p" and not supports_p then
+ arg = tostring(arg);
+ option = "s";
+ spec = "[%s]";
+ t = "string";
+ end
+
+ if t == "string" and option ~= "p" then
if not valid_utf8(arg) then
option = "q";
else
@@ -95,6 +105,11 @@ local function format(formatstring, ...)
args[i] = dump(arg);
return "%s";
end
+
+ if option == "p" and (t == "boolean" or t == "number") then
+ args[i] = tostring(arg);
+ return "[%s]";
+ end
end);
-- process extra args