From: Vsevolod Stakhov Date: Wed, 16 Dec 2015 22:08:22 +0000 (+0000) Subject: Add humanize number lua utility X-Git-Tag: 1.1.0~266 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=53114c87873eae766c7d20bea4713fe4262df618;p=thirdparty%2Frspamd.git Add humanize number lua utility --- diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c index 5568b874ae..65afa8470b 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} }; @@ -678,6 +688,19 @@ lua_util_is_uppercase (lua_State *L) return 1; } +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) {