Changeset

12035:dc7ab05005e8

util.format: Fix Lua 5.1 quirks thanks to ALL THE TESTS
author Kim Alvefur <zash@zash.se>
date Sat, 11 Dec 2021 20:40:23 +0100
parents 12034:ee94ac51b2dd
children 12036:2ce06f788093
files util/format.lua
diffstat 1 files changed, 6 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/util/format.lua	Sat Dec 11 20:38:54 2021 +0100
+++ b/util/format.lua	Sat Dec 11 20:40:23 2021 +0100
@@ -29,7 +29,8 @@
 	["\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 supports_p = pcall(string.format, "%p", ""); -- >= Lua 5.4
+local supports_a = pcall(string.format, "%a", 0.0); -- > Lua 5.1
 
 local function format(formatstring, ...)
 	local args = pack(...);
@@ -66,6 +67,8 @@
 		if option == "s" and t == "string" and not arg:find("[%z\1-\31\128-\255]") then
 			-- No UTF-8 or control characters, assumed to be the common case.
 			return
+		elseif option == "s" and t ~= "string" then
+			args[i] = tostring(arg);
 		end
 
 		if option ~= "s" and option ~= "q" and option ~= "p" then
@@ -79,6 +82,8 @@
 			elseif expects_integer[option] and num_type(arg) ~= "integer" then
 				args[i] = tostring(arg);
 				return "[%s]";
+			elseif (option == "a" or option == "A") and not supports_a then
+				return "%x";
 			else
 				return -- acceptable number
 			end