From 9be4e9c559238b5bdd2cf6ca89725306ab52b70b Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Thu, 20 Jul 2023 22:18:33 +0100 Subject: [PATCH] [Minor] Add Lua API for transliterate function --- src/lua/lua_util.c | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c index 9f0630557..39e08c1fb 100644 --- a/src/lua/lua_util.c +++ b/src/lua/lua_util.c @@ -252,6 +252,15 @@ LUA_FUNCTION_DEF (util, lower_utf8); */ LUA_FUNCTION_DEF (util, normalize_utf8); + +/*** + * @function util.transliterate(str) + * Converts utf8 encoded string to latin transliteration + * @param {string/text} str utf8 encoded string + * @return {text} transliterated string + */ +LUA_FUNCTION_DEF (util, transliterate); + /*** * @function util.strequal_caseless(str1, str2) * Compares two strings regardless of their case using ascii comparison. @@ -686,6 +695,7 @@ static const struct luaL_reg utillib_f[] = { LUA_INTERFACE_DEF (util, strlen_utf8), LUA_INTERFACE_DEF (util, lower_utf8), LUA_INTERFACE_DEF (util, normalize_utf8), + LUA_INTERFACE_DEF (util, transliterate), LUA_INTERFACE_DEF (util, strequal_caseless), LUA_INTERFACE_DEF (util, strequal_caseless_utf8), LUA_INTERFACE_DEF (util, get_ticks), @@ -1635,6 +1645,24 @@ lua_util_normalize_utf8 (lua_State *L) return 2; } +static gint +lua_util_transliterate (lua_State *L) +{ + LUA_TRACE_POINT; + struct rspamd_lua_text *t; + t = lua_check_text_or_string (L, 1); + + if (!t) { + return luaL_error(L, "invalid arguments"); + } + + gsize outlen; + char *transliterated = rspamd_utf8_transliterate(t->start, t->len, &outlen); + lua_new_text(L, transliterated, outlen, TRUE); + + return 1; +} + static gint lua_util_strequal_caseless (lua_State *L) { -- 2.39.5