-
Notifications
You must be signed in to change notification settings - Fork 0
/
KittyLib Lua54.lua
221 lines (185 loc) · 4.42 KB
/
KittyLib Lua54.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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
local table = setmetatable({}, {__index = table})
local math = setmetatable({}, {__index = math})
local string = setmetatable({}, {__index = string})
local KittyLib = {_VERSION = "0.1.2"}
do -- string
function string.split(s, sep)
if sep == nil then
sep = "%s"
end
local t = {}
local splits = 0
for str in string.gmatch(s, "([^"..sep.."]+)") do
table.insert(t, str)
splits = splits + 1
end
return t, splits
end
end
do -- math
function math.rounddec(n, decimal)
local mult = 10 ^ (decimal)
return math.modf(n * mult) / mult
end
function math.round2(n, d)
return math.floor(n / d) * d
end
function math.tobinary(n)
assert(math.type(n) == "integer", "input value must be an integer.")
local ret=""
while n ~= 1 and n ~= 0 do
ret = tostring(n % 2) .. ret
n = math.modf(n / 2)
end
ret = tostring(n) .. ret
return ret
end
function math.frombinary(binary)
return tonumber(binary, 2)
end
function math.randomfloat(min, max, decimal)
local decimal = decimal or 1000
return math.random(min * decimal, max * decimal) / decimal
end
end
do -- table
local function tostringTable(t, mode, indent, tostringed)
local tostringed = tostringed or {}
tostringed[tostring(t)] = true
local stringTable = "{" .. (mode == 1 and "\n" or "")
for i, v in next, t do
local indexType = type(i)
local valueType = type(v)
local index = i
if indexType == "number" then
index = '[' .. i .. ']'
elseif indexType == "string" and string.find(i, " ") then
index = '["' .. i .. '"]'
elseif indexType == "table" then
index = "[" .. tostringTable(i, 0, 0) .. "]"
end
local value = ""
if valueType == "string" then
value = '"' .. v .. '"'
elseif valueType == "number" or valueType == "nil" or valueType == "boolean" then
value = tostring(v)
elseif valueType == "table" then
if tostringed[tostring(v)] == nil then
value = tostringTable(v, mode, indent + 1, tostringed)
end
else
value = tostring(v)
end
stringTable = stringTable .. string.rep("\t", (mode == 1 and indent or 0)) .. index .. " = " .. value
if table.findkey(t, i) ~= table.len(t) then
stringTable = stringTable .. ", "
end
stringTable = stringTable .. (mode == 1 and "\n" or "")
end
stringTable = stringTable .. string.rep("\t", (mode == 1 and indent - 1 or 0)) .. "}"
return stringTable
end
function table.tostring(t, mode)
local mode = mode or 0
assert(not (mode > 1 or mode < 0), "mode is out of range.")
assert(type(t) == "table", "t must be a table.")
return tostringTable(t, mode, 1)
end
function table.copy(t)
local copied = {}
for i, v in next, t do
if type(v) == "table" then
copied[i] = table.copy(v)
else
copied[i] = v
end
end
return copied
end
function table.destroy(t)
for i, v in next, t do
if type(v) == "table" then
table.destroy(v)
end
end
table.clear(t)
return t
end
function table.clear(t)
for i, v in next, t do
t[i] = nil
end
return t
end
function table.replace(t, vt)
for i, v in next, vt do
if t[i] then
t[i] = v
end
end
return t
end
function table.deepreplace(t, vt)
for i, v in next, vt do
if t[i] then
if type(t[i]) == "table" and type(v) == "table" then
table.deepreplace(t[i], v)
else
t[i] = v
end
end
end
return t
end
function table.find(t, value)
local key = 0
for i, v in next, t do
key = key + 1
if v == value then
break
end
end
if key == 0 then
key = nil
end
return key
end
function table.findkey(t, index)
if type(index) == "number" then
return index
else
if table.find(t, index) == nil then
error(tostring(index) .. " is not found in " .. tostring(t))
end
local key = 0
for i, v in next, t do
key = key + 1
if i == index then
return key
end
end
return nil
end
end
function table.len(t)
local len = 0
for i, v in next, t do
len = len + 1
end
return len
end
function table.deeplen(t)
local len = 0
for i, v in next, t do
len = len + 1
if type(v) == "table" then
len = len + table.deeplen(v)
end
end
return len
end
end
KittyLib.math = math
KittyLib.table = table
KittyLib.string = string
return KittyLib