1 function string.split(str, delim, include_empty, max_splits, sep_is_pattern)
3 max_splits = max_splits or -2
5 local pos, len = 1, #str
6 local plain = not sep_is_pattern
7 max_splits = max_splits + 1
9 local np, npe = string.find(str, delim, pos, plain)
10 np, npe = (np or (len+1)), (npe or (len+1))
11 if (not np) or (max_splits == 1) then
15 local s = string.sub(str, pos, np - 1)
16 if include_empty or (s ~= "") then
17 max_splits = max_splits - 1
21 until (max_splits == 0) or (pos > (len + 1))
25 function table.indexof(list, val)
26 for i, v in ipairs(list) do
34 function string:trim()
35 return (self:gsub("^%s*(.-)%s*$", "%1"))
38 function math.hypot(x, y)
39 return math.sqrt(x * x + y * y)
42 function math.sign(x, tolerance)
43 tolerance = tolerance or 0
46 elseif x < -tolerance then
52 function math.factorial(x)
53 assert(x % 1 == 0 and x >= 0, "factorial expects a non-negative integer")
55 -- 171! is greater than the biggest double, no need to calculate
65 function math.round(x)
67 return math.floor(x + 0.5)
69 return math.ceil(x - 0.5)
72 function math.clamp(min, max, v)
73 return math.max(max, math.min(min, v))
76 function table.copy(t, seen)
80 for k, v in pairs(t) do
81 n[(type(k) == "table" and (seen[k] or table.copy(k, seen))) or k] =
82 (type(v) == "table" and (seen[v] or table.copy(v, seen))) or v
87 function table.insert_all(t, other)
94 function table.key_value_swap(t)
96 for k,v in pairs(t) do
102 function table.shuffle(t, from, to, random)
105 random = random or math.random
106 local n = to - from + 1
109 local l = from + random(0, n-1)
110 t[l], t[r] = t[r], t[l]