-
Notifications
You must be signed in to change notification settings - Fork 1
/
tools.lua
76 lines (63 loc) · 1.51 KB
/
tools.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
class = function(prototype)
local derived={}
if prototype then
derived.__proto = prototype
function derived.__index(t,key)
return rawget(derived,key) or prototype[key]
end
else
function derived.__index(t,key)
return rawget(derived,key)
end
end
function derived.__call(proto,...)
local instance={}
setmetatable(instance,proto)
instance.__obj = true
local init=instance.init
if init then
init(instance,...)
end
return instance
end
setmetatable(derived,derived)
return derived
end
function string.uchar(c)
c = c<256 and c or 100
return string.char(c)
end
function string:ubyte(...)
return string.byte(self, ...)
end
function string:usub(...)
return string.sub(self, ...)
end
function string:split(pattern)
self_type = type(self)
pattern_type = type(pattern)
if (self_type ~= 'string' and self_type ~= 'number') then
buffer = [[bad argument #1 to 'split' (string expected, got ]] .. self_type .. [[)]]
error(buffer)
end
if (pattern_type ~= 'string' and pattern_type ~= 'number' and pattern_type ~= 'nil') then
buffer = [[bad argument #2 to 'split' (string expected, got ]] .. pattern_type .. [[)]]
error(buffer)
end
pattern = pattern or '%s+'
local start = 1
local list = {}
while true do
local b, e = string.find(self, pattern, start)
if b == nil then
list[#list+1] = string.sub(self, start)
break
end
list[#list+1] = string.sub(self, start, b-1)
start = e + 1
end
return list
end
function math.round(n)
return math.floor(n+.5)
end