分割字符串
local ngx_find = ngx.re.find
local function split(str, sep , count)
local t = {}
count = count or -1
local nfield, nstart = 1, 1
local nfirst,nlast = string.find(str, sep)
-- local nfirst,nlast = ngx_find(str, sep, "jo")
while nfirst and count ~= 0 do
t[nfield] = string.sub(str, nstart, nfirst - 1)
nfield = nfield+1
nstart = nlast+1
nfirst,nlast = string.find(str, sep, nstart)
-- nfirst,nlast = ngx_find(str, sep, "jo", nil, nstart)
count = count-1
end
t[nfield] = string.sub(str, nstart)
return t
end
保留小数后n位
local function round(num, n)
local p = math.pow(10, n or 3)
local m = num * p
local _, d = math.modf(m)
if d < 0.5 then return math.floor(m) / p end
return math.ceil(m) / p
end
随机字符串
https://gist.github.com/haggen/2fd643ea9a261fea2094