Comparison

util/format.lua @ 9693:6ed0d6224d64

util.format: Serialize values for the %q format Improves eg debug logs
author Kim Alvefur <zash@zash.se>
date Fri, 12 Oct 2018 01:29:34 +0200
parent 9687:8c92ef4270c9
child 10034:4fca92d60040
comparison
equal deleted inserted replaced
9692:affcbccc1dff 9693:6ed0d6224d64
4 4
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 10
10 local function format(formatstring, ...) 11 local function format(formatstring, ...)
11 local args = pack(...); 12 local args = pack(...);
12 local args_length = args.n; 13 local args_length = args.n;
13 14
32 33
33 local option = spec:sub(-1); 34 local option = spec:sub(-1);
34 if arg == nil then 35 if arg == nil then
35 args[i] = "nil"; 36 args[i] = "nil";
36 spec = "<%s>"; 37 spec = "<%s>";
37 elseif option == "q" or option == "s" then -- arg should be string 38 elseif option == "q" then
39 args[i] = dump(arg);
40 spec = "%s";
41 elseif option == "s" then
38 args[i] = tostring(arg); 42 args[i] = tostring(arg);
39 elseif type(arg) ~= "number" then -- arg isn't number as expected? 43 elseif type(arg) ~= "number" then -- arg isn't number as expected?
40 args[i] = tostring(arg); 44 args[i] = tostring(arg);
41 spec = "[%s]"; 45 spec = "[%s]";
42 end 46 end