Skip to content

Commit

Permalink
filterx: regexp_subst auto-disable 'groups' when no match groups are …
Browse files Browse the repository at this point in the history
…present

The function now automatically checks the replacement pattern at config
parsing time. If no match group references are found in the pattern, it
will behave as if the 'groups' option is disabled, disabling match group
functionality for performance reasons.

By default, match groups are enabled in regexp_subst. However, this behavior
can now be explicitly disabled by setting the optional named argument
groups=false, as shown:
`regexp_subst(target, match_pattern, replace_pattern, groups=false);`

Signed-off-by: shifter <shifter@axoflow.com>
  • Loading branch information
bshifter committed Dec 12, 2024
1 parent 1fbb4ff commit 2ba815d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
15 changes: 12 additions & 3 deletions lib/filterx/expr-regexp-subst.c
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,6 @@ _replace_matches(const FilterXFuncRegexpSubst *self, FilterXReMatchState *state)
_build_replacement_stirng_with_match_groups(self, state, rep_str);
replacement_string = rep_str->str;
}

do
{
ovector = pcre2_get_ovector_pointer(state->match_data);
Expand Down Expand Up @@ -296,6 +295,13 @@ _extract_optional_flags(FilterXFuncRegexpSubst *self, FilterXFunctionArgs *args,
return TRUE;
}

static gboolean
_contains_match_grp_ref(gchar *str)
{
gchar *close = NULL;
return _next_matchgrp_ref(str, &close) != NULL;
}

static gboolean
_extract_subst_args(FilterXFuncRegexpSubst *self, FilterXFunctionArgs *args, GError **error)
{
Expand All @@ -320,7 +326,9 @@ _extract_subst_args(FilterXFuncRegexpSubst *self, FilterXFunctionArgs *args, GEr
self->replacement = _extract_subst_replacement_arg(args, error);
if (!self->replacement)
return FALSE;

// turn off group mode if there is no match grp ref due to it's performance impact
if (!_contains_match_grp_ref(self->replacement))
set_flag(&self->flags, FILTERX_FUNC_REGEXP_SUBST_FLAG_GROUPS, FALSE);

return TRUE;
}
Expand Down Expand Up @@ -365,7 +373,8 @@ filterx_function_regexp_subst_new(FilterXFunctionArgs *args, GError **error)
self->super.super.deinit = _subst_deinit;
self->super.super.free_fn = _subst_free;

reset_flags(&self->flags, FLAG_VAL(FILTERX_FUNC_REGEXP_SUBST_FLAG_JIT));
reset_flags(&self->flags, FLAG_VAL(FILTERX_FUNC_REGEXP_SUBST_FLAG_JIT) | FLAG_VAL(
FILTERX_FUNC_REGEXP_SUBST_FLAG_GROUPS));
if (!_extract_subst_args(self, args, error) ||
!filterx_function_args_check(args, error))
goto error;
Expand Down
4 changes: 2 additions & 2 deletions lib/filterx/tests/test_expr_regexp_subst.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ _build_subst_func(const gchar *pattern, const gchar *repr, const gchar *str, Fil
if (opts.utf8)
args = g_list_append(args, filterx_function_arg_new(FILTERX_FUNC_REGEXP_SUBST_FLAG_UTF8_NAME,
filterx_literal_new(filterx_boolean_new(TRUE))));
if (opts.groups)
if (!opts.groups)
args = g_list_append(args, filterx_function_arg_new(FILTERX_FUNC_REGEXP_SUBST_FLAG_GROUPS_NAME,
filterx_literal_new(filterx_boolean_new(TRUE))));
filterx_literal_new(filterx_boolean_new(FALSE))));

GError *err = NULL;
FilterXExpr *func = filterx_function_regexp_subst_new(filterx_function_args_new(args, NULL), &err);
Expand Down

0 comments on commit 2ba815d

Please sign in to comment.