Differences From
Artifact [4468d8dfeb]:
1 +local sanitable = {
2 + ['\xfe'] = '\xf0';
3 + ['\1'] = '\xf1';
4 + ['\2'] = '\xf2';
5 + ['\3'] = '\xf3';
6 +
7 + ['\xf0'] = '\xfe';
8 + ['\xf1'] = '\1';
9 + ['\xf2'] = '\2';
10 + ['\xf3'] = '\3';
11 +}
1 12 return {
2 13 capitalize = function(str)
3 14 return string.upper(string.sub(str, 1,1)) .. string.sub(str, 2)
4 15 end;
5 16
6 17 rand = function(min,max)
7 18 if not min then min = 16 end
................................................................................
32 43 str = string.sub(str, 2)
33 44 end
34 45 if string.sub(str, #str,#str) == ' ' then
35 46 str = string.sub(str, 1, #str - 1)
36 47 end
37 48 return str
38 49 end;
50 +
51 + meta_armor = function(str,mark_struct)
52 + -- binary values stored in metadata need to be sanitized so
53 + -- they don't contain values that will disrupt parsing of the
54 + -- KV store, as minetest (stupidly) uses in-band signalling
55 + local sanitized = string.gsub(str, '[\xfe\1\2\3]', function(char)
56 + return '\xfe' .. sanitable[char]
57 + end)
58 + if sanitized ~= str and mark_struct then
59 + -- use different type code to mark struct headers for
60 + -- back-compat
61 + return string.gsub(sanitized,'^\xfe\xf0\x99','\xfe\x98')
62 + else return sanitized end
63 + end;
64 + meta_dearmor = function(str,cond)
65 + local dearmor = function(s)
66 + return string.gsub(s, '\xfe([\xf0\xf1\xf2\xf3])', function(char)
67 + return sanitable[char]
68 + end)
69 + end
70 + if cond then
71 + if string.sub(str,1,2) == '\xfe\x98' then
72 + return dearmor(string.gsub(str,'^\xfe\x98','\xfe\xf0\x99'))
73 + else return str end
74 + else return dearmor(str) end
75 + end;
39 76 }