-
Notifications
You must be signed in to change notification settings - Fork 1
/
leaf_core.lua
147 lines (95 loc) · 2.51 KB
/
leaf_core.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
function leaf.log(tag, ...)
local arg = {...}
if type(tag) == 'table' then
-- Check if object has tostring --
if getmetatable(tag) then
tag = tostring(tag)
else
local tmp = ''
for i, v in pairs(tag) do
if tmp ~= '' then
if type(i) ~= 'number' then
tmp = tmp .. ', ' .. (i) .. ': '
else tmp = tmp .. ', ' end
else
if type(i) ~= 'number' then
tmp = (i) .. ': '
end
end
tmp = tmp .. tostring(v)
end
tag = tmp
end
end
-- Make all arguments strings --
for i, a in ipairs(arg) do
arg[i] = tostring(a)
end
-- Concat arguments --
arg = table.concat(arg, ", ")
local out = '[' .. tostring(tag) .. ']'
if arg ~= '' then out = out .. '[' .. arg .. ']' end
print(out)
end
function leaf.table_first(lst)
for i, v in pairs(lst) do
return i, v
end
end
function leaf.table_last(lst)
local idx, val
for i, v in pairs(lst) do
idx, val = i, v
end
return idx, val
end
function leaf.table_find(lst, val)
for idx, itm in pairs(lst) do
if itm == val then return idx end
end
end
function leaf.table_eq(lst, otr)
for i in pairs(lst) do
if lst[i] ~= otr[i] or not otr[i] then
return false
end
end
return true
end
function leaf.table_copy(lst)
local other = {}
for idx, val in pairs(lst) do
other[idx] = val
end
return other
end
function string.split(self, pat)
-- Table to store substrings --
local subs = {}
-- For every word --
while true do
-- Get index of substring (div) --
local findx, lindx = self:find(pat)
-- Store last substring --
if not findx then
subs[#subs + 1] = self
break
end
-- Store the substring before (div) --
subs[#subs + 1], self = self:sub(1, findx - 1), self:sub(lindx + 1)
end
return subs
end
function string.startswith(self, sub)
return self:sub(1, #sub) == sub
end
function string.endswith(self, sub)
return self:sub(-#sub) == sub
end
function tobool(value)
if type(value) == 'string' then
if value:lower() == 'true' then return true
end
elseif type(value) == 'number' then return value ~= 0
else return value ~= nil end
end