Comparison

util/format.lua @ 10034:4fca92d60040

util.format: Handle formats expecting an integer in Lua 5.3+ (fixes #1371)
author Kim Alvefur <zash@zash.se>
date Thu, 30 May 2019 13:41:05 +0200
parent 9693:6ed0d6224d64
child 10035:386f085820e6
comparison
equal deleted inserted replaced
10033:ca8333d1a7fe 10034:4fca92d60040
5 local tostring = tostring; 5 local tostring = tostring;
6 local unpack = table.unpack or unpack; -- luacheck: ignore 113/unpack 6 local unpack = table.unpack or unpack; -- luacheck: ignore 113/unpack
7 local pack = require "util.table".pack; -- TODO table.pack in 5.2+ 7 local pack = require "util.table".pack; -- TODO table.pack in 5.2+
8 local type = type; 8 local type = type;
9 local dump = require "util.serialization".new("debug"); 9 local dump = require "util.serialization".new("debug");
10 local num_type = math.type;
11
12 local expects_integer = num_type and { c = true, d = true, i = true, o = true, u = true, X = true, x = true, } or {};
10 13
11 local function format(formatstring, ...) 14 local function format(formatstring, ...)
12 local args = pack(...); 15 local args = pack(...);
13 local args_length = args.n; 16 local args_length = args.n;
14 17
41 elseif option == "s" then 44 elseif option == "s" then
42 args[i] = tostring(arg); 45 args[i] = tostring(arg);
43 elseif type(arg) ~= "number" then -- arg isn't number as expected? 46 elseif type(arg) ~= "number" then -- arg isn't number as expected?
44 args[i] = tostring(arg); 47 args[i] = tostring(arg);
45 spec = "[%s]"; 48 spec = "[%s]";
49 elseif expects_integer[option] and num_type(arg) ~= "integer" then
50 args[i] = tostring(arg);
51 spec = "[%s]";
46 end 52 end
47 end 53 end
48 return spec; 54 return spec;
49 end); 55 end);
50 56