aboutsummaryrefslogtreecommitdiffstats
path: root/util/format.lua
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2019-11-23 23:12:01 +0100
committerKim Alvefur <zash@zash.se>2019-11-23 23:12:01 +0100
commit72f1544f6d7301d1313c333f1147b27e242063df (patch)
tree1a5b57117c8c88f05ebb8b67a0d2848988b7620c /util/format.lua
parent7ac5e467baa7ebc557ed3a07bdca7dc9ade0c1db (diff)
parentdfa2bdffc083faa597509d7f35125c41f6ffbc9d (diff)
downloadprosody-72f1544f6d7301d1313c333f1147b27e242063df.tar.gz
prosody-72f1544f6d7301d1313c333f1147b27e242063df.zip
Merge 0.11->trunk
Diffstat (limited to 'util/format.lua')
-rw-r--r--util/format.lua27
1 files changed, 20 insertions, 7 deletions
diff --git a/util/format.lua b/util/format.lua
index c5e513fa..1ce670f3 100644
--- a/util/format.lua
+++ b/util/format.lua
@@ -3,12 +3,20 @@
--
local tostring = tostring;
-local select = select;
local unpack = table.unpack or unpack; -- luacheck: ignore 113/unpack
+local pack = require "util.table".pack; -- TODO table.pack in 5.2+
local type = type;
+local dump = require "util.serialization".new("debug");
+local num_type = math.type or function (n)
+ return n % 1 == 0 and n <= 9007199254740992 and n >= -9007199254740992 and "integer" or "float";
+end
+
+-- In Lua 5.3+ these formats throw an error if given a float
+local expects_integer = { c = true, d = true, i = true, o = true, u = true, X = true, x = true, };
local function format(formatstring, ...)
- local args, args_length = { ... }, select('#', ...);
+ local args = pack(...);
+ local args_length = args.n;
-- format specifier spec:
-- 1. Start: '%%'
@@ -28,17 +36,22 @@ local function format(formatstring, ...)
if spec ~= "%%" then
i = i + 1;
local arg = args[i];
- if arg == nil then -- special handling for nil
- arg = "<nil>"
- args[i] = "<nil>";
- end
local option = spec:sub(-1);
- if option == "q" or option == "s" then -- arg should be string
+ if arg == nil then
+ args[i] = "nil";
+ spec = "<%s>";
+ elseif option == "q" then
+ args[i] = dump(arg);
+ spec = "%s";
+ elseif option == "s" then
args[i] = tostring(arg);
elseif type(arg) ~= "number" then -- arg isn't number as expected?
args[i] = tostring(arg);
spec = "[%s]";
+ elseif expects_integer[option] and num_type(arg) ~= "integer" then
+ args[i] = tostring(arg);
+ spec = "[%s]";
end
end
return spec;