-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathutils.lua
98 lines (81 loc) · 1.79 KB
/
utils.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
local fmt = import("fmt")
function toBytes(str)
local result = {}
for i = 1, #str do
local b = str:byte(i)
if b < 32 then
table.insert(result, b)
end
end
return result
end
function getUriFromBuf(buf)
if buf == nil then return; end
local file = buf.AbsPath
local uri = fmt.Sprintf("file://%s", file)
return uri
end
function mysplit(inputstr, sep)
if sep == nil then
sep = "%s"
end
local t = {}
for str in string.gmatch(inputstr, "([^" .. sep .. "]+)") do
table.insert(t, str)
end
return t
end
function table.join(tbl, sep)
local result = ''
for _, value in ipairs(tbl) do
result = result .. (#result > 0 and sep or '') .. value
end
return result
end
function contains(list, x)
for _, v in pairs(list) do
if v == x then return true; end
end
return false
end
function string.starts(String, Start)
return string.sub(String, 1, #Start) == Start
end
function string.ends(String, End)
return string.sub(String, #String - (#End - 1), #String) == End
end
function string.random(CharSet, Length, prefix)
local _CharSet = CharSet or '.'
if _CharSet == '' then
return ''
else
local Result = prefix or ""
math.randomseed(os.time())
for Loop = 1, Length do
local char = math.random(1, #CharSet)
Result = Result .. CharSet:sub(char, char)
end
return Result
end
end
function string.parse(text)
if not text:find('"jsonrpc":') then return {}; end
local start, fin = text:find("\n%s*\n")
local cleanedText = text
if fin ~= nil then
cleanedText = text:sub(fin)
end
local status, res = pcall(json.parse, cleanedText)
if status then
return res
end
return false
end
table.filter = function(t, filterIter)
local out = {}
for k, v in pairs(t) do
if filterIter(v, k, t) then table.insert(out, v) end
end
return out
end
table.unpack = table.unpack or unpack