Skip to content

Commit 5751c1f

Browse files
committed
Rewrite escape function
1 parent 2fa9745 commit 5751c1f

File tree

1 file changed

+6
-15
lines changed

1 file changed

+6
-15
lines changed

src/prometheus/util.lua

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,14 @@ local function unlookupify(tb)
2222
return tb2;
2323
end
2424

25-
26-
local InvalidStringCharsMatch = "[^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ %+%/!?%_-=,\n\r\t\a\b\v\\\"\'%.:;%&/()%%%[%]%*%^]"
2725
local function escape(str)
28-
if(string.match(str, InvalidStringCharsMatch)) then
29-
local ret = "";
30-
local bytes = {string.byte(str, 1, -1)};
31-
for i, byte in ipairs(bytes) do
32-
ret = ret .. string.format("\\%03d", byte);
33-
end
34-
return ret;
35-
end
36-
3726
return str:gsub(".", function(char)
27+
if char:match('[^ -~\n\t\a\b\v\r\"\']') then -- Check if non Printable Ascii Character
28+
return string.format("\\%03d", string.byte(char))
29+
end
30+
if(char == "\\") then
31+
return "\\\\";
32+
end
3833
if(char == "\n") then
3934
return "\\n";
4035
end
@@ -53,16 +48,12 @@ local function escape(str)
5348
if(char == "\v") then
5449
return "\\v";
5550
end
56-
if(char == "\\") then
57-
return "\\\\";
58-
end
5951
if(char == "\"") then
6052
return "\\\"";
6153
end
6254
if(char == "\'") then
6355
return "\\\'";
6456
end
65-
6657
return char;
6758
end)
6859
end

0 commit comments

Comments
 (0)