ExperimentalGear/scripts/common/util.lua

64 lines
1.1 KiB
Lua
Raw Normal View History

local function splitString(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
local function filter(tableIn, predicate)
local out = {}
for _, val in ipairs(tableIn) do
if predicate(val) then
table.insert(out, val)
end
end
return out
end
2022-03-03 03:55:46 +01:00
local function clamp(x, min, max)
if x < min then
x = min
end
if x > max then
x = max
end
return x
end
local function round(num)
return num + (2^52 + 2^51) - (2^52 + 2^51)
end
local function sign(x)
return (
(x > 0) and 1
or
(x < 0) and -1
or
0
)
end
local function roundToZero(x)
if x < 0 then
return math.ceil(x)
elseif x > 0 then
return math.floor(x)
else
return 0
end
end
return {
splitString = splitString,
2022-03-03 03:55:46 +01:00
filter = filter,
clamp = clamp,
round = round,
sign = sign,
roundToZero = roundToZero
}