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