]> git.lizzy.rs Git - luax.git/blob - init.lua
Copy more code from Minetest
[luax.git] / init.lua
1 function string.split(str, delim, include_empty, max_splits, sep_is_pattern)
2         delim = delim or ","
3         max_splits = max_splits or -2
4         local items = {}
5         local pos, len = 1, #str
6         local plain = not sep_is_pattern
7         max_splits = max_splits + 1
8         repeat
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
12                         np = len + 1
13                         npe = np
14                 end
15                 local s = string.sub(str, pos, np - 1)
16                 if include_empty or (s ~= "") then
17                         max_splits = max_splits - 1
18                         items[#items + 1] = s
19                 end
20                 pos = npe + 1
21         until (max_splits == 0) or (pos > (len + 1))
22         return items
23 end
24
25 function table.indexof(list, val)
26         for i, v in ipairs(list) do
27                 if v == val then
28                         return i
29                 end
30         end
31         return -1
32 end
33
34 function string:trim()
35         return (self:gsub("^%s*(.-)%s*$", "%1"))
36 end
37
38 function math.hypot(x, y)
39         return math.sqrt(x * x + y * y)
40 end
41
42 function math.sign(x, tolerance)
43         tolerance = tolerance or 0
44         if x > tolerance then
45                 return 1
46         elseif x < -tolerance then
47                 return -1
48         end
49         return 0
50 end
51
52 function math.factorial(x)
53         assert(x % 1 == 0 and x >= 0, "factorial expects a non-negative integer")
54         if x >= 171 then
55                 -- 171! is greater than the biggest double, no need to calculate
56                 return math.huge
57         end
58         local v = 1
59         for k = 2, x do
60                 v = v * k
61         end
62         return v
63 end
64
65 function math.round(x)
66         if x >= 0 then
67                 return math.floor(x + 0.5)
68         end
69         return math.ceil(x - 0.5)
70 end
71
72 function math.clamp(min, max, v)
73         return math.max(max, math.min(min, v))
74 end
75
76 function table.copy(t, seen)
77         local n = {}
78         seen = seen or {}
79         seen[t] = n
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
83         end
84         return n
85 end
86
87 function table.insert_all(t, other)
88         for i=1, #other do
89                 t[#t + 1] = other[i]
90         end
91         return t
92 end
93
94 function table.key_value_swap(t)
95         local ti = {}
96         for k,v in pairs(t) do
97                 ti[v] = k
98         end
99         return ti
100 end
101
102 function table.shuffle(t, from, to, random)
103         from = from or 1
104         to = to or #t
105         random = random or math.random
106         local n = to - from + 1
107         while n > 1 do
108                 local r = from + n-1
109                 local l = from + random(0, n-1)
110                 t[l], t[r] = t[r], t[l]
111                 n = n-1
112         end
113 end