-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.lua
45 lines (39 loc) · 845 Bytes
/
util.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
local ffi = require "ffi"
ffi.cdef[[
double hypot(double x, double y);
double copysign(double x, double y);
]]
local hypot = ffi.C.hypot
local copysign = ffi.C.copysign
local function round(x)
local i, f = math.modf(x + copysign(0.5, x))
return i
end
local function rtrim(s, c)
local i = #s
while s:sub(i, i) == c do
i = i - 1
end
return s:sub(1, i)
end
local function startswith(s1, s2)
return s1:sub(1, #s2) == s2
end
local function func_iter(seq)
if type(seq) == "table" then
local i = 0
return function()
i = i + 1
if i <= #seq then
return seq[i]
end
end
else
return seq
end
end
return {
hypot=hypot, copysign=copysign, round=round,
rtrim=rtrim, startswith=startswith,
func_iter=func_iter
}