summaryrefslogtreecommitdiffstats
path: root/src/lua/lua_util.c
diff options
context:
space:
mode:
authorVsevolod Stakhov <vsevolod@highsecure.ru>2016-03-07 15:14:01 +0000
committerVsevolod Stakhov <vsevolod@highsecure.ru>2016-03-07 15:14:01 +0000
commit76740965ccff2563afb08e37ce0185003bc2063b (patch)
tree83d86a26b2bb587bbeff1d1b06b219cfa04630e1 /src/lua/lua_util.c
parenteeeabacd11a7801168eaaeed2c2ba26137307d20 (diff)
downloadrspamd-76740965ccff2563afb08e37ce0185003bc2063b.tar.gz
rspamd-76740965ccff2563afb08e37ce0185003bc2063b.zip
[Feature] Add lua functions for caseless comparison
Diffstat (limited to 'src/lua/lua_util.c')
-rw-r--r--src/lua/lua_util.c114
1 files changed, 114 insertions, 0 deletions
diff --git a/src/lua/lua_util.c b/src/lua/lua_util.c
index 04bf76942..0707f098c 100644
--- a/src/lua/lua_util.c
+++ b/src/lua/lua_util.c
@@ -193,6 +193,36 @@ LUA_FUNCTION_DEF (util, parse_mail_address);
*/
LUA_FUNCTION_DEF (util, strlen_utf8);
+/***
+ * @function util.strcasecmp(str1, str2)
+ * Compares two utf8 strings regardless of their case. Return value >0, 0 and <0
+ * if `str1` is more, equal or less than `str2`
+ * @param {string} str1 utf8 encoded string
+ * @param {string} str2 utf8 encoded string
+ * @return {number} result of comparison
+ */
+LUA_FUNCTION_DEF (util, strcasecmp_utf8);
+
+/***
+ * @function util.strcasecmp(str1, str2)
+ * Compares two ascii strings regardless of their case. Return value >0, 0 and <0
+ * if `str1` is more, equal or less than `str2`
+ * @param {string} str1 plain string
+ * @param {string} str2 plain string
+ * @return {number} result of comparison
+ */
+LUA_FUNCTION_DEF (util, strcasecmp_ascii);
+
+/***
+ * @function util.strequal_caseless(str1, str2)
+ * Compares two utf8 strings regardless of their case. Return `true` if `str1` is
+ * equal to `str2`
+ * @param {string} str1 utf8 encoded string
+ * @param {string} str2 utf8 encoded string
+ * @return {bool} result of comparison
+ */
+LUA_FUNCTION_DEF (util, strequal_caseless);
+
static const struct luaL_reg utillib_f[] = {
LUA_INTERFACE_DEF (util, create_event_base),
LUA_INTERFACE_DEF (util, load_rspamd_config),
@@ -214,6 +244,9 @@ 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, strcasecmp_utf8),
+ LUA_INTERFACE_DEF (util, strcasecmp_ascii),
+ LUA_INTERFACE_DEF (util, strequal_caseless),
{NULL, NULL}
};
@@ -942,6 +975,87 @@ lua_util_strlen_utf8 (lua_State *L)
}
static gint
+lua_util_strcasecmp_utf8 (lua_State *L)
+{
+ const gchar *str1, *str2;
+ gsize len1, len2;
+ gint ret = -1;
+
+ str1 = lua_tolstring (L, 1, &len1);
+ str2 = lua_tolstring (L, 1, &len2);
+
+ if (str1 && str2) {
+
+ if (len1 == len2) {
+ ret = rspamd_lc_cmp (str1, str2, len1);
+ }
+ else {
+ ret = len1 - len2;
+ }
+ }
+ else {
+ return luaL_error (L, "invalid arguments");
+ }
+
+ lua_pushnumber (L, ret);
+ return 1;
+}
+
+static gint
+lua_util_strcasecmp_ascii (lua_State *L)
+{
+ const gchar *str1, *str2;
+ gsize len1, len2;
+ gint ret = -1;
+
+ str1 = lua_tolstring (L, 1, &len1);
+ str2 = lua_tolstring (L, 1, &len2);
+
+ if (str1 && str2) {
+
+ if (len1 == len2) {
+ ret = g_ascii_strncasecmp (str1, str2, len1);
+ }
+ else {
+ ret = len1 - len2;
+ }
+ }
+ else {
+ return luaL_error (L, "invalid arguments");
+ }
+
+ lua_pushnumber (L, ret);
+ return 1;
+}
+
+static gint
+lua_util_strequal_caseless (lua_State *L)
+{
+ const gchar *str1, *str2;
+ gsize len1, len2;
+ gint ret = -1;
+
+ str1 = lua_tolstring (L, 1, &len1);
+ str2 = lua_tolstring (L, 1, &len2);
+
+ if (str1 && str2) {
+
+ if (len1 == len2) {
+ ret = rspamd_lc_cmp (str1, str2, len1);
+ }
+ else {
+ ret = len1 - len2;
+ }
+ }
+ else {
+ return luaL_error (L, "invalid arguments");
+ }
+
+ lua_pushboolean (L, (ret == 0));
+ return 1;
+}
+
+static gint
lua_load_util (lua_State * L)
{
lua_newtable (L);