-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcommon.lua
41 lines (38 loc) · 960 Bytes
/
common.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
-- Strips any kind of escape codes (translation, colors) from a string
-- https://github.com/minetest/minetest/blob/53dd7819277c53954d1298dfffa5287c306db8d0/src/util/string.cpp#L777
-- https://github.com/Uberi/Minetest-WorldEdit/blob/abc9efeeb8cccb3e23c055414941fed4a9871b9a/worldedit_commands/init.lua
function mtui.strip_escapes(input)
if not input then
return ""
end
local s = function(idx) return input:sub(idx, idx) end
local out = ""
local i = 1
while i <= #input do
if s(i) == "\027" then -- escape sequence
i = i + 1
if s(i) == "(" then -- enclosed
i = i + 1
while i <= #input and s(i) ~= ")" do
if s(i) == "\\" then
i = i + 2
else
i = i + 1
end
end
end
else
out = out .. s(i)
end
i = i + 1
end
return out
end
function mtui.sanitize_ip(ip)
if ip and string.sub(ip, 1, 7) == "::ffff:" then
-- trim leading garbage
return string.sub(ip, 8)
end
-- keep it as-is
return ip
end