Annotate

util/format.lua @ 12035:dc7ab05005e8

util.format: Fix Lua 5.1 quirks thanks to ALL THE TESTS
author Kim Alvefur <zash@zash.se>
date Sat, 11 Dec 2021 20:40:23 +0100
parent 12033:161f8268c4b3
child 12036:2ce06f788093
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
8225
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
1 --
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
2 -- A string.format wrapper that gracefully handles invalid arguments
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
3 --
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
4
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
5 local tostring = tostring;
8417
e88db5668cfb util.format: Import unpack from table lib in Lua 5.2+
Kim Alvefur <zash@zash.se>
parents: 8383
diff changeset
6 local unpack = table.unpack or unpack; -- luacheck: ignore 113/unpack
9687
8c92ef4270c9 util.format: Use pack from util.table
Kim Alvefur <zash@zash.se>
parents: 9656
diff changeset
7 local pack = require "util.table".pack; -- TODO table.pack in 5.2+
12031
87bc26f23d9b util.format: Escape invalid UTF-8 by passing trough serialization
Kim Alvefur <zash@zash.se>
parents: 11648
diff changeset
8 local valid_utf8 = require "util.encodings".utf8.valid;
8225
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
9 local type = type;
9693
6ed0d6224d64 util.format: Serialize values for the %q format
Kim Alvefur <zash@zash.se>
parents: 9687
diff changeset
10 local dump = require "util.serialization".new("debug");
10035
386f085820e6 util.format: Handle integer formats the same way on Lua versions without integer support
Kim Alvefur <zash@zash.se>
parents: 10034
diff changeset
11 local num_type = math.type or function (n)
386f085820e6 util.format: Handle integer formats the same way on Lua versions without integer support
Kim Alvefur <zash@zash.se>
parents: 10034
diff changeset
12 return n % 1 == 0 and n <= 9007199254740992 and n >= -9007199254740992 and "integer" or "float";
386f085820e6 util.format: Handle integer formats the same way on Lua versions without integer support
Kim Alvefur <zash@zash.se>
parents: 10034
diff changeset
13 end
10034
4fca92d60040 util.format: Handle formats expecting an integer in Lua 5.3+ (fixes #1371)
Kim Alvefur <zash@zash.se>
parents: 9693
diff changeset
14
10035
386f085820e6 util.format: Handle integer formats the same way on Lua versions without integer support
Kim Alvefur <zash@zash.se>
parents: 10034
diff changeset
15 -- In Lua 5.3+ these formats throw an error if given a float
386f085820e6 util.format: Handle integer formats the same way on Lua versions without integer support
Kim Alvefur <zash@zash.se>
parents: 10034
diff changeset
16 local expects_integer = { c = true, d = true, i = true, o = true, u = true, X = true, x = true, };
11638
5f4a657136bc util.format: Escape ASCII control characters in output
Kim Alvefur <zash@zash.se>
parents: 10035
diff changeset
17 -- Printable Unicode replacements for control characters
5f4a657136bc util.format: Escape ASCII control characters in output
Kim Alvefur <zash@zash.se>
parents: 10035
diff changeset
18 local control_symbols = {
5f4a657136bc util.format: Escape ASCII control characters in output
Kim Alvefur <zash@zash.se>
parents: 10035
diff changeset
19 -- 0x00 .. 0x1F --> U+2400 .. U+241F, 0x7F --> U+2421
5f4a657136bc util.format: Escape ASCII control characters in output
Kim Alvefur <zash@zash.se>
parents: 10035
diff changeset
20 ["\000"] = "\226\144\128", ["\001"] = "\226\144\129", ["\002"] = "\226\144\130",
5f4a657136bc util.format: Escape ASCII control characters in output
Kim Alvefur <zash@zash.se>
parents: 10035
diff changeset
21 ["\003"] = "\226\144\131", ["\004"] = "\226\144\132", ["\005"] = "\226\144\133",
5f4a657136bc util.format: Escape ASCII control characters in output
Kim Alvefur <zash@zash.se>
parents: 10035
diff changeset
22 ["\006"] = "\226\144\134", ["\007"] = "\226\144\135", ["\008"] = "\226\144\136",
5f4a657136bc util.format: Escape ASCII control characters in output
Kim Alvefur <zash@zash.se>
parents: 10035
diff changeset
23 ["\009"] = "\226\144\137", ["\010"] = "\226\144\138", ["\011"] = "\226\144\139",
5f4a657136bc util.format: Escape ASCII control characters in output
Kim Alvefur <zash@zash.se>
parents: 10035
diff changeset
24 ["\012"] = "\226\144\140", ["\013"] = "\226\144\141", ["\014"] = "\226\144\142",
5f4a657136bc util.format: Escape ASCII control characters in output
Kim Alvefur <zash@zash.se>
parents: 10035
diff changeset
25 ["\015"] = "\226\144\143", ["\016"] = "\226\144\144", ["\017"] = "\226\144\145",
5f4a657136bc util.format: Escape ASCII control characters in output
Kim Alvefur <zash@zash.se>
parents: 10035
diff changeset
26 ["\018"] = "\226\144\146", ["\019"] = "\226\144\147", ["\020"] = "\226\144\148",
5f4a657136bc util.format: Escape ASCII control characters in output
Kim Alvefur <zash@zash.se>
parents: 10035
diff changeset
27 ["\021"] = "\226\144\149", ["\022"] = "\226\144\150", ["\023"] = "\226\144\151",
5f4a657136bc util.format: Escape ASCII control characters in output
Kim Alvefur <zash@zash.se>
parents: 10035
diff changeset
28 ["\024"] = "\226\144\152", ["\025"] = "\226\144\153", ["\026"] = "\226\144\154",
5f4a657136bc util.format: Escape ASCII control characters in output
Kim Alvefur <zash@zash.se>
parents: 10035
diff changeset
29 ["\027"] = "\226\144\155", ["\028"] = "\226\144\156", ["\029"] = "\226\144\157",
5f4a657136bc util.format: Escape ASCII control characters in output
Kim Alvefur <zash@zash.se>
parents: 10035
diff changeset
30 ["\030"] = "\226\144\158", ["\031"] = "\226\144\159", ["\127"] = "\226\144\161",
5f4a657136bc util.format: Escape ASCII control characters in output
Kim Alvefur <zash@zash.se>
parents: 10035
diff changeset
31 };
12035
dc7ab05005e8 util.format: Fix Lua 5.1 quirks thanks to ALL THE TESTS
Kim Alvefur <zash@zash.se>
parents: 12033
diff changeset
32 local supports_p = pcall(string.format, "%p", ""); -- >= Lua 5.4
dc7ab05005e8 util.format: Fix Lua 5.1 quirks thanks to ALL THE TESTS
Kim Alvefur <zash@zash.se>
parents: 12033
diff changeset
33 local supports_a = pcall(string.format, "%a", 0.0); -- > Lua 5.1
8225
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
34
8382
e5d00bf4a4d5 util: Various minor changes to please [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8225
diff changeset
35 local function format(formatstring, ...)
9687
8c92ef4270c9 util.format: Use pack from util.table
Kim Alvefur <zash@zash.se>
parents: 9656
diff changeset
36 local args = pack(...);
8c92ef4270c9 util.format: Use pack from util.table
Kim Alvefur <zash@zash.se>
parents: 9656
diff changeset
37 local args_length = args.n;
8225
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
38
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
39 -- format specifier spec:
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
40 -- 1. Start: '%%'
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
41 -- 2. Flags: '[%-%+ #0]'
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
42 -- 3. Width: '%d?%d?'
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
43 -- 4. Precision: '%.?%d?%d?'
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
44 -- 5. Option: '[cdiouxXaAeEfgGqs%%]'
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
45 --
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
46 -- The options c, d, E, e, f, g, G, i, o, u, X, and x all expect a number as argument, whereas q and s expect a string.
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
47 -- This function does not accept string values containing embedded zeros, except as arguments to the q option.
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
48 -- a and A are only in Lua 5.2+
12033
161f8268c4b3 util.format: Also handle the %p format added in Lua 5.4
Kim Alvefur <zash@zash.se>
parents: 12032
diff changeset
49 -- Lua 5.4 adds a p format that produces a pointer
8225
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
50
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
51
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
52 -- process each format specifier
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
53 local i = 0;
12033
161f8268c4b3 util.format: Also handle the %p format added in Lua 5.4
Kim Alvefur <zash@zash.se>
parents: 12032
diff changeset
54 formatstring = formatstring:gsub("%%[^cdiouxXaAeEfgGpqs%%]*[cdiouxXaAeEfgGpqs%%]", function(spec)
12032
3db09eb4c43b util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents: 12031
diff changeset
55 if spec == "%%" then return end
3db09eb4c43b util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents: 12031
diff changeset
56 i = i + 1;
3db09eb4c43b util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents: 12031
diff changeset
57 local arg = args[i];
3db09eb4c43b util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents: 12031
diff changeset
58
3db09eb4c43b util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents: 12031
diff changeset
59 if arg == nil then
3db09eb4c43b util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents: 12031
diff changeset
60 args[i] = "nil";
3db09eb4c43b util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents: 12031
diff changeset
61 return "(%s)";
3db09eb4c43b util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents: 12031
diff changeset
62 end
8225
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
63
12032
3db09eb4c43b util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents: 12031
diff changeset
64 local option = spec:sub(-1);
3db09eb4c43b util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents: 12031
diff changeset
65 local t = type(arg);
3db09eb4c43b util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents: 12031
diff changeset
66
3db09eb4c43b util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents: 12031
diff changeset
67 if option == "s" and t == "string" and not arg:find("[%z\1-\31\128-\255]") then
3db09eb4c43b util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents: 12031
diff changeset
68 -- No UTF-8 or control characters, assumed to be the common case.
3db09eb4c43b util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents: 12031
diff changeset
69 return
12035
dc7ab05005e8 util.format: Fix Lua 5.1 quirks thanks to ALL THE TESTS
Kim Alvefur <zash@zash.se>
parents: 12033
diff changeset
70 elseif option == "s" and t ~= "string" then
dc7ab05005e8 util.format: Fix Lua 5.1 quirks thanks to ALL THE TESTS
Kim Alvefur <zash@zash.se>
parents: 12033
diff changeset
71 args[i] = tostring(arg);
12032
3db09eb4c43b util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents: 12031
diff changeset
72 end
3db09eb4c43b util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents: 12031
diff changeset
73
12033
161f8268c4b3 util.format: Also handle the %p format added in Lua 5.4
Kim Alvefur <zash@zash.se>
parents: 12032
diff changeset
74 if option ~= "s" and option ~= "q" and option ~= "p" then
12032
3db09eb4c43b util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents: 12031
diff changeset
75 -- all other options expect numbers
3db09eb4c43b util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents: 12031
diff changeset
76 if t ~= "number" then
3db09eb4c43b util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents: 12031
diff changeset
77 -- arg isn't number as expected?
12031
87bc26f23d9b util.format: Escape invalid UTF-8 by passing trough serialization
Kim Alvefur <zash@zash.se>
parents: 11648
diff changeset
78 arg = tostring(arg);
87bc26f23d9b util.format: Escape invalid UTF-8 by passing trough serialization
Kim Alvefur <zash@zash.se>
parents: 11648
diff changeset
79 option = "s";
87bc26f23d9b util.format: Escape invalid UTF-8 by passing trough serialization
Kim Alvefur <zash@zash.se>
parents: 11648
diff changeset
80 spec = "[%s]";
87bc26f23d9b util.format: Escape invalid UTF-8 by passing trough serialization
Kim Alvefur <zash@zash.se>
parents: 11648
diff changeset
81 t = "string";
10034
4fca92d60040 util.format: Handle formats expecting an integer in Lua 5.3+ (fixes #1371)
Kim Alvefur <zash@zash.se>
parents: 9693
diff changeset
82 elseif expects_integer[option] and num_type(arg) ~= "integer" then
4fca92d60040 util.format: Handle formats expecting an integer in Lua 5.3+ (fixes #1371)
Kim Alvefur <zash@zash.se>
parents: 9693
diff changeset
83 args[i] = tostring(arg);
12032
3db09eb4c43b util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents: 12031
diff changeset
84 return "[%s]";
12035
dc7ab05005e8 util.format: Fix Lua 5.1 quirks thanks to ALL THE TESTS
Kim Alvefur <zash@zash.se>
parents: 12033
diff changeset
85 elseif (option == "a" or option == "A") and not supports_a then
dc7ab05005e8 util.format: Fix Lua 5.1 quirks thanks to ALL THE TESTS
Kim Alvefur <zash@zash.se>
parents: 12033
diff changeset
86 return "%x";
12032
3db09eb4c43b util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents: 12031
diff changeset
87 else
3db09eb4c43b util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents: 12031
diff changeset
88 return -- acceptable number
8225
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
89 end
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
90 end
12032
3db09eb4c43b util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents: 12031
diff changeset
91
12033
161f8268c4b3 util.format: Also handle the %p format added in Lua 5.4
Kim Alvefur <zash@zash.se>
parents: 12032
diff changeset
92
161f8268c4b3 util.format: Also handle the %p format added in Lua 5.4
Kim Alvefur <zash@zash.se>
parents: 12032
diff changeset
93 if option == "p" and not supports_p then
161f8268c4b3 util.format: Also handle the %p format added in Lua 5.4
Kim Alvefur <zash@zash.se>
parents: 12032
diff changeset
94 arg = tostring(arg);
161f8268c4b3 util.format: Also handle the %p format added in Lua 5.4
Kim Alvefur <zash@zash.se>
parents: 12032
diff changeset
95 option = "s";
161f8268c4b3 util.format: Also handle the %p format added in Lua 5.4
Kim Alvefur <zash@zash.se>
parents: 12032
diff changeset
96 spec = "[%s]";
161f8268c4b3 util.format: Also handle the %p format added in Lua 5.4
Kim Alvefur <zash@zash.se>
parents: 12032
diff changeset
97 t = "string";
161f8268c4b3 util.format: Also handle the %p format added in Lua 5.4
Kim Alvefur <zash@zash.se>
parents: 12032
diff changeset
98 end
161f8268c4b3 util.format: Also handle the %p format added in Lua 5.4
Kim Alvefur <zash@zash.se>
parents: 12032
diff changeset
99
161f8268c4b3 util.format: Also handle the %p format added in Lua 5.4
Kim Alvefur <zash@zash.se>
parents: 12032
diff changeset
100 if t == "string" and option ~= "p" then
12032
3db09eb4c43b util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents: 12031
diff changeset
101 if not valid_utf8(arg) then
3db09eb4c43b util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents: 12031
diff changeset
102 option = "q";
3db09eb4c43b util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents: 12031
diff changeset
103 else
3db09eb4c43b util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents: 12031
diff changeset
104 args[i] = arg:gsub("[%z\1-\8\11-\31\127]", control_symbols):gsub("\n\t?", "\n\t");
3db09eb4c43b util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents: 12031
diff changeset
105 return spec;
3db09eb4c43b util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents: 12031
diff changeset
106 end
3db09eb4c43b util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents: 12031
diff changeset
107 end
3db09eb4c43b util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents: 12031
diff changeset
108
3db09eb4c43b util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents: 12031
diff changeset
109 if option == "q" then
3db09eb4c43b util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents: 12031
diff changeset
110 args[i] = dump(arg);
3db09eb4c43b util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents: 12031
diff changeset
111 return "%s";
3db09eb4c43b util.format: Ensure sanitation of strings passed to wrong format
Kim Alvefur <zash@zash.se>
parents: 12031
diff changeset
112 end
12033
161f8268c4b3 util.format: Also handle the %p format added in Lua 5.4
Kim Alvefur <zash@zash.se>
parents: 12032
diff changeset
113
161f8268c4b3 util.format: Also handle the %p format added in Lua 5.4
Kim Alvefur <zash@zash.se>
parents: 12032
diff changeset
114 if option == "p" and (t == "boolean" or t == "number") then
161f8268c4b3 util.format: Also handle the %p format added in Lua 5.4
Kim Alvefur <zash@zash.se>
parents: 12032
diff changeset
115 args[i] = tostring(arg);
161f8268c4b3 util.format: Also handle the %p format added in Lua 5.4
Kim Alvefur <zash@zash.se>
parents: 12032
diff changeset
116 return "[%s]";
161f8268c4b3 util.format: Also handle the %p format added in Lua 5.4
Kim Alvefur <zash@zash.se>
parents: 12032
diff changeset
117 end
8225
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
118 end);
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
119
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
120 -- process extra args
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
121 while i < args_length do
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
122 i = i + 1;
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
123 local arg = args[i];
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
124 if arg == nil then
11644
fc1b8fe94d04 util.format: Change formatting of nil values to avoid looking like XML
Kim Alvefur <zash@zash.se>
parents: 11638
diff changeset
125 args[i] = "(nil)";
8225
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
126 else
11648
96d3cbeb9275 util.format: Escape ASCII control characters also in extra arguments
Kim Alvefur <zash@zash.se>
parents: 11647
diff changeset
127 args[i] = tostring(arg):gsub("[%z\1-\8\11-\31\127]", control_symbols):gsub("\n\t?", "\n\t");
8225
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
128 end
8382
e5d00bf4a4d5 util: Various minor changes to please [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8225
diff changeset
129 formatstring = formatstring .. " [%s]"
8225
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
130 end
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
131
8382
e5d00bf4a4d5 util: Various minor changes to please [luacheck]
Kim Alvefur <zash@zash.se>
parents: 8225
diff changeset
132 return formatstring:format(unpack(args));
8225
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
133 end
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
134
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
135 return {
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
136 format = format;
70cb72f46a3b util.format: A string.format wrapper that gracefully handles invalid arguments
Waqas Hussain <waqas20@gmail.com>
parents:
diff changeset
137 };