Diff

util/format.lua @ 12036:2ce06f788093

util.format: Fix some formats expecting positive numbers in Lua 5.2 Amazing how string.format behaves differently under each Lua version
author Kim Alvefur <zash@zash.se>
date Sat, 11 Dec 2021 20:54:37 +0100
parent 12035:dc7ab05005e8
child 12039:e0a8c5b1ab4f
line wrap: on
line diff
--- a/util/format.lua	Sat Dec 11 20:40:23 2021 +0100
+++ b/util/format.lua	Sat Dec 11 20:54:37 2021 +0100
@@ -14,6 +14,8 @@
 
 -- 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 @@
 			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