diff options
author | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-12-16 22:08:22 +0000 |
---|---|---|
committer | Vsevolod Stakhov <vsevolod@highsecure.ru> | 2015-12-16 22:08:22 +0000 |
commit | 53114c87873eae766c7d20bea4713fe4262df618 (patch) | |
tree | f3bfed362f3f9da02b730305227e3b0cb2e041b6 /src | |
parent | 2cc1274bf2d36d0ace6c31f08fe5c29719e24391 (diff) | |
download | rspamd-53114c87873eae766c7d20bea4713fe4262df618.tar.gz rspamd-53114c87873eae766c7d20bea4713fe4262df618.zip |
Add humanize number lua utility
Diffstat (limited to 'src')
-rw-r--r-- | src/lua/lua_util.c | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c index 5568b874a..65afa8470 100644 --- a/src/lua/lua_util.c +++ b/src/lua/lua_util.c @@ -133,6 +133,15 @@ LUA_FUNCTION_DEF (util, fold_header); */ LUA_FUNCTION_DEF (util, is_uppercase); +/** + * @function util.humanize_number(num) + * Returns humanized representation of given number (like 1k instead of 1000) + * + * @param {number} num number to humanize + * @return {string} humanized representation of a number + */ +LUA_FUNCTION_DEF (util, humanize_number); + static const struct luaL_reg utillib_f[] = { LUA_INTERFACE_DEF (util, create_event_base), LUA_INTERFACE_DEF (util, load_rspamd_config), @@ -147,6 +156,7 @@ static const struct luaL_reg utillib_f[] = { LUA_INTERFACE_DEF (util, parse_addr), LUA_INTERFACE_DEF (util, fold_header), LUA_INTERFACE_DEF (util, is_uppercase), + LUA_INTERFACE_DEF (util, humanize_number), {NULL, NULL} }; @@ -679,6 +689,19 @@ lua_util_is_uppercase (lua_State *L) } static gint +lua_util_humanize_number (lua_State *L) +{ + gdouble number = luaL_checknumber (L, 1); + gchar numbuf[32]; + + + rspamd_snprintf (numbuf, sizeof (numbuf), "%hL", (gint64)number); + lua_pushstring (L, numbuf); + + return 1; +} + +static gint lua_load_util (lua_State * L) { lua_newtable (L); |