Comparison

util/format.lua @ 8382:e5d00bf4a4d5

util: Various minor changes to please [luacheck]
author Kim Alvefur <zash@zash.se>
date Fri, 10 Nov 2017 05:42:32 +0100
parent 8225:70cb72f46a3b
child 8383:d967d6f2ad00
comparison
equal deleted inserted replaced
8381:7f6184474149 8382:e5d00bf4a4d5
6 local select = select; 6 local select = select;
7 local assert = assert; 7 local assert = assert;
8 local unpack = unpack; 8 local unpack = unpack;
9 local type = type; 9 local type = type;
10 10
11 local function format(format, ...) 11 local function format(formatstring, ...)
12 local args, args_length = { ... }, select('#', ...); 12 local args, args_length = { ... }, select('#', ...);
13 13
14 -- format specifier spec: 14 -- format specifier spec:
15 -- 1. Start: '%%' 15 -- 1. Start: '%%'
16 -- 2. Flags: '[%-%+ #0]' 16 -- 2. Flags: '[%-%+ #0]'
23 -- a and A are only in Lua 5.2+ 23 -- a and A are only in Lua 5.2+
24 24
25 25
26 -- process each format specifier 26 -- process each format specifier
27 local i = 0; 27 local i = 0;
28 format = format:gsub("%%[^cdiouxXaAeEfgGqs%%]*[cdiouxXaAeEfgGqs%%]", function(spec) 28 formatstring = formatstring:gsub("%%[^cdiouxXaAeEfgGqs%%]*[cdiouxXaAeEfgGqs%%]", function(spec)
29 if spec ~= "%%" then 29 if spec ~= "%%" then
30 i = i + 1; 30 i = i + 1;
31 local arg = args[i]; 31 local arg = args[i];
32 if arg == nil then -- special handling for nil 32 if arg == nil then -- special handling for nil
33 arg = "<nil>" 33 arg = "<nil>"
52 if arg == nil then 52 if arg == nil then
53 args[i] = "<nil>"; 53 args[i] = "<nil>";
54 else 54 else
55 args[i] = tostring(arg); 55 args[i] = tostring(arg);
56 end 56 end
57 format = format .. " [%s]" 57 formatstring = formatstring .. " [%s]"
58 end 58 end
59 59
60 return format:format(unpack(args)); 60 return formatstring:format(unpack(args));
61 end 61 end
62 62
63 local function test() 63 local function test()
64 assert(format("%s", "hello") == "hello"); 64 assert(format("%s", "hello") == "hello");
65 assert(format("%s") == "<nil>"); 65 assert(format("%s") == "<nil>");