diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2018-11-13 11:44:44 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2018-11-13 11:44:44 +0000 |
commit | c854023470fca387017d3bf260e05ae849dd4abe (patch) | |
tree | 454dcc86d0622000f091c3c627de30c7570f4598 /lualib/lua_util.lua | |
parent | 896ede0b288344984d05b123ef87abda7a949f9c (diff) | |
download | rspamd-c854023470fca387017d3bf260e05ae849dd4abe.tar.gz rspamd-c854023470fca387017d3bf260e05ae849dd4abe.zip |
[Minor] Add function to parse humanized number
Diffstat (limited to 'lualib/lua_util.lua')
-rw-r--r-- | lualib/lua_util.lua | 49 |
1 files changed, 49 insertions, 0 deletions
diff --git a/lualib/lua_util.lua b/lualib/lua_util.lua index 18e37b3c5..276b8e05a 100644 --- a/lualib/lua_util.lua +++ b/lualib/lua_util.lua @@ -391,6 +391,55 @@ end exports.parse_time_interval = parse_time_interval --[[[ +-- @function lua_util.dehumanize_number(str) +-- Parses human readable number +-- Accepts 'k' for thousands, 'm' for millions, 'g' for billions, 'b' suffix for 1024 multiplier, +-- e.g. `10mb` equal to `10 * 1024 * 1024` +-- @param {string} str input string +-- @return {number|nil} parsed number +--]] +local function dehumanize_number(str) + local function parse_suffix(s) + if s == 'k' then + return 1000 + elseif s == 'm' then + return 1000000 + elseif s == 'g' then + return 1e9 + elseif s == 'kb' then + return 1024 + elseif s == 'mb' then + return 1024 * 1024 + elseif s == 'gb' then + return 1024 * 1024; + end + end + + local digit = lpeg.R("09") + local parser = {} + parser.integer = + (lpeg.S("+-") ^ -1) * + (digit ^ 1) + parser.fractional = + (lpeg.P(".") ) * + (digit ^ 1) + parser.number = + (parser.integer * + (parser.fractional ^ -1)) + + (lpeg.S("+-") * parser.fractional) + parser.humanized_number = lpeg.Cf(lpeg.Cc(1) * + (parser.number / tonumber) * + (((lpeg.S("kmg") * (lpeg.P("b") ^ -1)) / parse_suffix) ^ -1), + function (acc, val) return acc * val end) + + local t = lpeg.match(parser.humanized_number, str) + + return t +end + +exports.dehumanize_number = dehumanize_number + +--[[[ -- @function lua_util.table_cmp(t1, t2) -- Compare two tables deeply --]] |