comparison util/format.lua @ 10035:386f085820e6

util.format: Handle integer formats the same way on Lua versions without integer support
author Kim Alvefur <zash@zash.se>
date Thu, 30 May 2019 13:54:11 +0200
parents 4fca92d60040
children 5f4a657136bc
comparison
equal deleted inserted replaced
10034:4fca92d60040 10035:386f085820e6
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; 10 local num_type = math.type or function (n)
11 return n % 1 == 0 and n <= 9007199254740992 and n >= -9007199254740992 and "integer" or "float";
12 end
11 13
12 local expects_integer = num_type and { c = true, d = true, i = true, o = true, u = true, X = true, x = true, } or {}; 14 -- In Lua 5.3+ these formats throw an error if given a float
15 local expects_integer = { c = true, d = true, i = true, o = true, u = true, X = true, x = true, };
13 16
14 local function format(formatstring, ...) 17 local function format(formatstring, ...)
15 local args = pack(...); 18 local args = pack(...);
16 local args_length = args.n; 19 local args_length = args.n;
17 20