]> source.dussan.org Git - rspamd.git/commitdiff
[Minor] Lua_util: Add lower_utf8 utility
authorVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 13 Sep 2019 14:55:44 +0000 (15:55 +0100)
committerVsevolod Stakhov <vsevolod@highsecure.ru>
Fri, 13 Sep 2019 14:55:44 +0000 (15:55 +0100)
Issue: #3036

src/lua/lua_util.c

index c25d20471746799eaae8ab6b9e184737238e6b25..f86f975e76f4650c2c7a56d60cf334d269e8525f 100644 (file)
@@ -233,6 +233,14 @@ LUA_FUNCTION_DEF (util, parse_mail_address);
  */
 LUA_FUNCTION_DEF (util, strlen_utf8);
 
+/***
+ * @function util.lower_utf8(str)
+ * Converts utf8 string to lower case
+ * @param {string} str utf8 encoded string
+ * @return {string} lowercased utf8 string
+ */
+LUA_FUNCTION_DEF (util, lower_utf8);
+
 
 /***
  * @function util.strcasecmp(str1, str2)
@@ -636,6 +644,7 @@ static const struct luaL_reg utillib_f[] = {
        LUA_INTERFACE_DEF (util, glob),
        LUA_INTERFACE_DEF (util, parse_mail_address),
        LUA_INTERFACE_DEF (util, strlen_utf8),
+       LUA_INTERFACE_DEF (util, lower_utf8),
        LUA_INTERFACE_DEF (util, strcasecmp_ascii),
        LUA_INTERFACE_DEF (util, strequal_caseless),
        LUA_INTERFACE_DEF (util, get_ticks),
@@ -1680,21 +1689,53 @@ static gint
 lua_util_strlen_utf8 (lua_State *L)
 {
        LUA_TRACE_POINT;
-       const gchar *str, *end;
+       const gchar *str;
        gsize len;
 
        str = lua_tolstring (L, 1, &len);
 
        if (str) {
-               if (g_utf8_validate (str, len, &end)) {
-                       len = g_utf8_strlen (str, len);
+               gint32 i = 0, nchars = 0;
+               UChar32 uc;
+
+               while (i < len) {
+                       U8_NEXT ((guint8 *) str, i, len, uc);
+                       nchars ++;
                }
-               else if (end != NULL && end > str) {
-                       len = (g_utf8_strlen (str, end - str)) /* UTF part */
-                                       + (len - (end - str)) /* raw part */;
+
+               lua_pushinteger (L, nchars);
+       }
+       else {
+               return luaL_error (L, "invalid arguments");
+       }
+
+       return 1;
+}
+
+static gint
+lua_util_lower_utf8 (lua_State *L)
+{
+       LUA_TRACE_POINT;
+       const gchar *str;
+       gchar *dst;
+       gsize len;
+       UChar32 uc;
+       UBool err = 0;
+       gint32 i = 0, j = 0;
+
+       str = lua_tolstring (L, 1, &len);
+
+       if (str) {
+               dst = g_malloc (len);
+
+               while (i < len && err == 0) {
+                       U8_NEXT ((guint8 *) str, i, len, uc);
+                       uc = u_tolower (uc);
+                       U8_APPEND (dst, j, len, uc, err);
                }
 
-               lua_pushinteger (L, len);
+               lua_pushlstring (L, dst, j);
+               g_free (dst);
        }
        else {
                return luaL_error (L, "invalid arguments");