aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2018-11-28 20:36:53 +0100
committerKim Alvefur <zash@zash.se>2018-11-28 20:36:53 +0100
commit83ab43ca8ff3d3e93042f76b91194f2ec3c6e36f (patch)
treedea88bb48f743f7cb1c2cc86cb247349a7e6846b
parent68c511e310d3f44914542a73980c7e9b55adabec (diff)
downloadprosody-83ab43ca8ff3d3e93042f76b91194f2ec3c6e36f.tar.gz
prosody-83ab43ca8ff3d3e93042f76b91194f2ec3c6e36f.zip
util.format: Tweak how nil values are handled
Because [<nil>] seems exsessive
-rw-r--r--spec/util_format_spec.lua2
-rw-r--r--util/format.lua9
2 files changed, 6 insertions, 5 deletions
diff --git a/spec/util_format_spec.lua b/spec/util_format_spec.lua
index 7e6a0c6e..8a2e9312 100644
--- a/spec/util_format_spec.lua
+++ b/spec/util_format_spec.lua
@@ -5,6 +5,8 @@ describe("util.format", function()
it("should work", function()
assert.equal("hello", format("%s", "hello"));
assert.equal("<nil>", format("%s"));
+ assert.equal("<nil>", format("%d"));
+ assert.equal("<nil>", format("%q"));
assert.equal(" [<nil>]", format("", nil));
assert.equal("true", format("%s", true));
assert.equal("[true]", format("%d", true));
diff --git a/util/format.lua b/util/format.lua
index c5e513fa..16c57bc6 100644
--- a/util/format.lua
+++ b/util/format.lua
@@ -28,13 +28,12 @@ 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" or option == "s" then -- arg should be string
args[i] = tostring(arg);
elseif type(arg) ~= "number" then -- arg isn't number as expected?
args[i] = tostring(arg);