diff --git a/lib/filterx/CMakeLists.txt b/lib/filterx/CMakeLists.txt index 7c3c774eca..fc930ef2b2 100644 --- a/lib/filterx/CMakeLists.txt +++ b/lib/filterx/CMakeLists.txt @@ -40,6 +40,7 @@ set(FILTERX_HEADERS filterx/func-len.h filterx/func-vars.h filterx/func-unset-empties.h + filterx/func-str-transform.h filterx/expr-plus.h filterx/expr-null-coalesce.h PARENT_SCOPE @@ -87,6 +88,7 @@ set(FILTERX_SOURCES filterx/func-len.c filterx/func-vars.c filterx/func-unset-empties.c + filterx/func-str-transform.c filterx/expr-plus.c filterx/filterx-private.c filterx/expr-null-coalesce.c diff --git a/lib/filterx/Makefile.am b/lib/filterx/Makefile.am index e66ed93468..a4f1bb53ab 100644 --- a/lib/filterx/Makefile.am +++ b/lib/filterx/Makefile.am @@ -42,6 +42,7 @@ filterxinclude_HEADERS = \ lib/filterx/func-len.h \ lib/filterx/func-vars.h \ lib/filterx/func-unset-empties.h \ + lib/filterx/func-str-transform.h \ lib/filterx/filterx-private.h \ lib/filterx/expr-null-coalesce.h @@ -89,6 +90,7 @@ filterx_sources = \ lib/filterx/func-len.c \ lib/filterx/func-vars.c \ lib/filterx/func-unset-empties.c \ + lib/filterx/func-str-transform.c \ lib/filterx/filterx-private.c \ lib/filterx/expr-null-coalesce.c \ lib/filterx/filterx-grammar.y diff --git a/lib/filterx/filterx-globals.c b/lib/filterx/filterx-globals.c index 7ddbcbb06b..a7cfd76745 100644 --- a/lib/filterx/filterx-globals.c +++ b/lib/filterx/filterx-globals.c @@ -34,6 +34,7 @@ #include "filterx/func-len.h" #include "filterx/func-vars.h" #include "filterx/func-unset-empties.h" +#include "filterx/func-str-transform.h" #include "filterx/filterx-eval.h" static GHashTable *filterx_builtin_simple_functions = NULL; @@ -85,7 +86,8 @@ _simple_init(void) g_assert(filterx_builtin_simple_function_register("double", filterx_typecast_double)); g_assert(filterx_builtin_simple_function_register("len", filterx_simple_function_len)); g_assert(filterx_builtin_simple_function_register("vars", filterx_simple_function_vars)); - + g_assert(filterx_builtin_simple_function_register("lower", filterx_simple_function_lower)); + g_assert(filterx_builtin_simple_function_register("upper", filterx_simple_function_upper)); } static void diff --git a/lib/filterx/func-str-transform.c b/lib/filterx/func-str-transform.c new file mode 100644 index 0000000000..71b72da075 --- /dev/null +++ b/lib/filterx/func-str-transform.c @@ -0,0 +1,79 @@ +/* + * Copyright (c) 2024 Axoflow + * Copyright (c) 2024 Attila Szakacs + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As an additional exemption you are allowed to compile & link against the + * OpenSSL libraries as published by the OpenSSL project. See the file + * COPYING for details. + * + */ + +#include "filterx/func-str-transform.h" +#include "filterx/object-string.h" +#include "filterx/filterx-eval.h" + +static const gchar * +_extract_str_arg(FilterXExpr *s, GPtrArray *args, gssize *len) +{ + if (args == NULL || args->len != 1) + { + filterx_simple_function_argument_error(s, "Requires exactly one argument", FALSE); + return NULL; + } + + gsize inner_len; + FilterXObject *object = g_ptr_array_index(args, 0); + const gchar *str = filterx_string_get_value(object, &inner_len); + if (!str) + { + filterx_simple_function_argument_error(s, "Object must be string", FALSE); + return NULL; + } + + *len = (gssize) MIN(inner_len, G_MAXINT64); + return str; +} + +FilterXObject * +filterx_simple_function_lower(FilterXExpr *s, GPtrArray *args) +{ + gssize len; + const gchar *str = _extract_str_arg(s, args, &len); + if (!str) + return NULL; + + gchar *lower = g_utf8_strdown(str, len); + FilterXObject *result = filterx_string_new(lower, -1); + g_free(lower); + + return result; +} + +FilterXObject * +filterx_simple_function_upper(FilterXExpr *s, GPtrArray *args) +{ + gssize len; + const gchar *str = _extract_str_arg(s, args, &len); + if (!str) + return NULL; + + gchar *upper = g_utf8_strup(str, len); + FilterXObject *result = filterx_string_new(upper, -1); + g_free(upper); + + return result; +} diff --git a/lib/filterx/func-str-transform.h b/lib/filterx/func-str-transform.h new file mode 100644 index 0000000000..b3d329576c --- /dev/null +++ b/lib/filterx/func-str-transform.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2024 Axoflow + * Copyright (c) 2024 Attila Szakacs + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * + * As an additional exemption you are allowed to compile & link against the + * OpenSSL libraries as published by the OpenSSL project. See the file + * COPYING for details. + * + */ + +#ifndef FILTERX_FUNC_STR_TRANSFORM_H_INCLUDED +#define FILTERX_FUNC_STR_TRANSFORM_H_INCLUDED + +#include "filterx/expr-function.h" + +FilterXObject *filterx_simple_function_lower(FilterXExpr *s, GPtrArray *args); +FilterXObject *filterx_simple_function_upper(FilterXExpr *s, GPtrArray *args); + +#endif diff --git a/lib/ivykis b/lib/ivykis index 242a35ab4c..feb0a7f318 160000 --- a/lib/ivykis +++ b/lib/ivykis @@ -1 +1 @@ -Subproject commit 242a35ab4c7117ba0692594a16e892495b2d0844 +Subproject commit feb0a7f3185d6300fecb2a8251ba2b2444df029b