Fix negative offsets not being supported by container[]
[oweals/minetest.git] / builtin / common / misc_helpers.lua
index d7f51f07296403af3377dc979e867617467aa768..25632b4ca21754aa6ff805323c8e76d6e8b6a0ce 100644 (file)
@@ -166,7 +166,7 @@ end
 --------------------------------------------------------------------------------
 function string.split(str, delim, include_empty, max_splits, sep_is_pattern)
        delim = delim or ","
-       max_splits = max_splits or -1
+       max_splits = max_splits or -2
        local items = {}
        local pos, len = 1, #str
        local plain = not sep_is_pattern
@@ -243,6 +243,20 @@ function math.sign(x, tolerance)
        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 get_last_folder(text,count)
        local parts = text:split(DIR_DELIM)