aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKim Alvefur <zash@zash.se>2021-12-11 20:54:37 +0100
committerKim Alvefur <zash@zash.se>2021-12-11 20:54:37 +0100
commitd482ece962f9d003cb055316c5535eb5b5c719ef (patch)
tree1d7e8ac1d075b716e44f27d2bd814ae6101ff0d1
parentdb41cd8e4df7bb1003e9dfb60671ef56113ad69c (diff)
downloadprosody-d482ece962f9d003cb055316c5535eb5b5c719ef.tar.gz
prosody-d482ece962f9d003cb055316c5535eb5b5c719ef.zip
util.format: Fix some formats expecting positive numbers in Lua 5.2
Amazing how string.format behaves differently under each Lua version
-rw-r--r--spec/util_format_spec.lua8
-rw-r--r--util/format.lua5
2 files changed, 9 insertions, 4 deletions
diff --git a/spec/util_format_spec.lua b/spec/util_format_spec.lua
index bc3a5867..ca3025c8 100644
--- a/spec/util_format_spec.lua
+++ b/spec/util_format_spec.lua
@@ -287,7 +287,7 @@ describe("util.format", function()
describe("to %o", function ()
it("works", function ()
assert.equal("141", format("%o", 97))
- assert.equal("1777777777777777747707", format("%o", -12345))
+ assert.equal("[-12345]", format("%o", -12345))
assert.equal("[1.5]", format("%o", 1.5))
assert.equal("[7.3786976294838e+19]", format("%o", 73786976294838206464))
assert.equal("[inf]", format("%o", math.huge))
@@ -298,7 +298,7 @@ describe("util.format", function()
describe("to %u", function ()
it("works", function ()
assert.equal("97", format("%u", 97))
- assert.equal("18446744073709539271", format("%u", -12345))
+ assert.equal("[-12345]", format("%u", -12345))
assert.equal("[1.5]", format("%u", 1.5))
assert.equal("[7.3786976294838e+19]", format("%u", 73786976294838206464))
assert.equal("[inf]", format("%u", math.huge))
@@ -309,7 +309,7 @@ describe("util.format", function()
describe("to %x", function ()
it("works", function ()
assert.equal("61", format("%x", 97))
- assert.equal("ffffffffffffcfc7", format("%x", -12345))
+ assert.equal("[-12345]", format("%x", -12345))
assert.equal("[1.5]", format("%x", 1.5))
assert.equal("[7.3786976294838e+19]", format("%x", 73786976294838206464))
assert.equal("[inf]", format("%x", math.huge))
@@ -320,7 +320,7 @@ describe("util.format", function()
describe("to %X", function ()
it("works", function ()
assert.equal("61", format("%X", 97))
- assert.equal("FFFFFFFFFFFFCFC7", format("%X", -12345))
+ assert.equal("[-12345]", format("%X", -12345))
assert.equal("[1.5]", format("%X", 1.5))
assert.equal("[7.3786976294838e+19]", format("%X", 73786976294838206464))
assert.equal("[inf]", format("%X", math.huge))
diff --git a/util/format.lua b/util/format.lua
index 76296fbf..efd92e3d 100644
--- a/util/format.lua
+++ b/util/format.lua
@@ -14,6 +14,8 @@ 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, };
+-- In Lua 5.2 these throw an error given a negative number
+local expects_positive = { o = true; u = true; x = true; X = true };
-- Printable Unicode replacements for control characters
local control_symbols = {
-- 0x00 .. 0x1F --> U+2400 .. U+241F, 0x7F --> U+2421
@@ -82,6 +84,9 @@ local function format(formatstring, ...)
elseif expects_integer[option] and num_type(arg) ~= "integer" then
args[i] = tostring(arg);
return "[%s]";
+ elseif expects_positive[option] and arg < 0 then
+ args[i] = tostring(arg);
+ return "[%s]";
elseif (option == "a" or option == "A") and not supports_a then
return "%x";
else