Comparison

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
comparison
equal deleted inserted replaced
12035:dc7ab05005e8 12036:2ce06f788093
12 return n % 1 == 0 and n <= 9007199254740992 and n >= -9007199254740992 and "integer" or "float"; 12 return n % 1 == 0 and n <= 9007199254740992 and n >= -9007199254740992 and "integer" or "float";
13 end 13 end
14 14
15 -- In Lua 5.3+ these formats throw an error if given a float 15 -- In Lua 5.3+ these formats throw an error if given a float
16 local expects_integer = { c = true, d = true, i = true, o = true, u = true, X = true, x = true, }; 16 local expects_integer = { c = true, d = true, i = true, o = true, u = true, X = true, x = true, };
17 -- In Lua 5.2 these throw an error given a negative number
18 local expects_positive = { o = true; u = true; x = true; X = true };
17 -- Printable Unicode replacements for control characters 19 -- Printable Unicode replacements for control characters
18 local control_symbols = { 20 local control_symbols = {
19 -- 0x00 .. 0x1F --> U+2400 .. U+241F, 0x7F --> U+2421 21 -- 0x00 .. 0x1F --> U+2400 .. U+241F, 0x7F --> U+2421
20 ["\000"] = "\226\144\128", ["\001"] = "\226\144\129", ["\002"] = "\226\144\130", 22 ["\000"] = "\226\144\128", ["\001"] = "\226\144\129", ["\002"] = "\226\144\130",
21 ["\003"] = "\226\144\131", ["\004"] = "\226\144\132", ["\005"] = "\226\144\133", 23 ["\003"] = "\226\144\131", ["\004"] = "\226\144\132", ["\005"] = "\226\144\133",
80 spec = "[%s]"; 82 spec = "[%s]";
81 t = "string"; 83 t = "string";
82 elseif expects_integer[option] and num_type(arg) ~= "integer" then 84 elseif expects_integer[option] and num_type(arg) ~= "integer" then
83 args[i] = tostring(arg); 85 args[i] = tostring(arg);
84 return "[%s]"; 86 return "[%s]";
87 elseif expects_positive[option] and arg < 0 then
88 args[i] = tostring(arg);
89 return "[%s]";
85 elseif (option == "a" or option == "A") and not supports_a then 90 elseif (option == "a" or option == "A") and not supports_a then
86 return "%x"; 91 return "%x";
87 else 92 else
88 return -- acceptable number 93 return -- acceptable number
89 end 94 end