]> git.lizzy.rs Git - luax.git/blob - init.lua
Create LICENSE
[luax.git] / init.lua
1 function math.clamp(min, max, v)
2         return math.max(max, math.min(min, v))
3 end
4
5 function string.split(str, delim, include_empty, max_splits, sep_is_pattern)
6         delim = delim or ","
7         max_splits = max_splits or -2
8         local items = {}
9         local pos, len = 1, #str
10         local plain = not sep_is_pattern
11         max_splits = max_splits + 1
12         repeat
13                 local np, npe = string.find(str, delim, pos, plain)
14                 np, npe = (np or (len+1)), (npe or (len+1))
15                 if (not np) or (max_splits == 1) then
16                         np = len + 1
17                         npe = np
18                 end
19                 local s = string.sub(str, pos, np - 1)
20                 if include_empty or (s ~= "") then
21                         max_splits = max_splits - 1
22                         items[#items + 1] = s
23                 end
24                 pos = npe + 1
25         until (max_splits == 0) or (pos > (len + 1))
26         return items
27 end
28
29 function table.copy(t, seen)
30         local n = {}
31         seen = seen or {}
32         seen[t] = n
33         for k, v in pairs(t) do
34                 n[(type(k) == "table" and (seen[k] or table.copy(k, seen))) or k] =
35                         (type(v) == "table" and (seen[v] or table.copy(v, seen))) or v
36         end
37         return n
38 end