+function table.indexof(list, val)
+ for i, v in ipairs(list) do
+ if v == val then
+ return i
+ end
+ end
+ return -1
+end
+
+function string:trim()
+ return (self:gsub("^%s*(.-)%s*$", "%1"))
+end
+
+function math.hypot(x, y)
+ return math.sqrt(x * x + y * y)
+end
+
+function math.sign(x, tolerance)
+ tolerance = tolerance or 0
+ if x > tolerance then
+ return 1
+ elseif x < -tolerance then
+ return -1
+ end
+ return 0
+end
+
+function math.factorial(x)
+ assert(x % 1 == 0 and x >= 0, "factorial expects a non-negative integer")
+ if x >= 171 then
+ -- 171! is greater than the biggest double, no need to calculate
+ return math.huge
+ end
+ local v = 1
+ for k = 2, x do
+ v = v * k
+ end
+ return v
+end
+
+function math.round(x)
+ if x >= 0 then
+ return math.floor(x + 0.5)
+ end
+ return math.ceil(x - 0.5)
+end
+
+function math.clamp(min, max, v)
+ return math.max(max, math.min(min, v))
+end
+