aboutsummaryrefslogtreecommitdiffstats
path: root/src/lua/lua_util.c
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2016-03-05 22:16:16 +0000
committerVsevolod Stakhov <vsevolod@highsecure.ru>2016-03-05 22:16:48 +0000
commitd84f6846de6ed096d035fe45f48d4c890c05cc7f (patch)
tree4a66071a7f99d34ca43c3211ef8d060a9d2ea217 /src/lua/lua_util.c
parent14d274c3db5316770f78f8b295f2eaacdad9e8d6 (diff)
downloadrspamd-d84f6846de6ed096d035fe45f48d4c890c05cc7f.tar.gz
rspamd-d84f6846de6ed096d035fe45f48d4c890c05cc7f.zip
[Feature] Add util.strlen_utf8 lua function
Diffstat (limited to 'src/lua/lua_util.c')
-rw-r--r--src/lua/lua_util.c44
1 files changed, 40 insertions, 4 deletions
diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c
index d43d7979c..04bf76942 100644
--- a/src/lua/lua_util.c
+++ b/src/lua/lua_util.c
@@ -143,7 +143,7 @@ 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)
*
@@ -152,7 +152,7 @@ LUA_FUNCTION_DEF (util, is_uppercase);
*/
LUA_FUNCTION_DEF (util, humanize_number);
-/**
+/***
* @function util.get_tld(host)
* Returns tld for the specified host
*
@@ -161,7 +161,7 @@ LUA_FUNCTION_DEF (util, humanize_number);
*/
LUA_FUNCTION_DEF (util, get_tld);
-/**
+/***
* @function util.glob(pattern)
* Returns results for the glob match for the specified pattern
*
@@ -170,7 +170,7 @@ LUA_FUNCTION_DEF (util, get_tld);
*/
LUA_FUNCTION_DEF (util, glob);
-/**
+/***
* @function util.parse_mail_address(str)
* Parses email address and returns a table of tables in the following format:
*
@@ -184,6 +184,15 @@ LUA_FUNCTION_DEF (util, glob);
*/
LUA_FUNCTION_DEF (util, parse_mail_address);
+/***
+ * @function util.strlen_utf8(str)
+ * Returns length of string encoded in utf-8 in characters.
+ * If invalid characters are found, then this function returns number of bytes.
+ * @param {string} str utf8 encoded string
+ * @return {number} number of characters in string
+ */
+LUA_FUNCTION_DEF (util, strlen_utf8);
+
static const struct luaL_reg utillib_f[] = {
LUA_INTERFACE_DEF (util, create_event_base),
LUA_INTERFACE_DEF (util, load_rspamd_config),
@@ -204,6 +213,7 @@ static const struct luaL_reg utillib_f[] = {
LUA_INTERFACE_DEF (util, get_tld),
LUA_INTERFACE_DEF (util, glob),
LUA_INTERFACE_DEF (util, parse_mail_address),
+ LUA_INTERFACE_DEF (util, strlen_utf8),
{NULL, NULL}
};
@@ -906,6 +916,32 @@ lua_util_parse_mail_address (lua_State *L)
}
static gint
+lua_util_strlen_utf8 (lua_State *L)
+{
+ const gchar *str, *end;
+ gsize len;
+
+ str = lua_tolstring (L, 1, &len);
+
+ if (str) {
+ if (g_utf8_validate (str, len, &end)) {
+ len = g_utf8_strlen (str, len);
+ }
+ else if (end != NULL && end > str) {
+ len = (g_utf8_strlen (str, end - str)) /* UTF part */
+ + (len - (end - str)) /* raw part */;
+ }
+
+ lua_pushnumber (L, len);
+ }
+ else {
+ return luaL_error (L, "invalid arguments");
+ }
+
+ return 1;
+}
+
+static gint
lua_load_util (lua_State * L)
{
lua_newtable (L);