Skip to content

Commit

Permalink
filterx: add eval wrapper for generators
Browse files Browse the repository at this point in the history
This makes is more simpler to write generator evals.

Signed-off-by: Attila Szakacs <[email protected]>
  • Loading branch information
alltilla committed May 6, 2024
1 parent 4c81873 commit 4bdbe80
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 28 deletions.
17 changes: 17 additions & 0 deletions lib/filterx/expr-generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,27 @@ filterx_generator_set_fillable(FilterXExpr *s, FilterXExpr *fillable)
self->fillable = fillable;
}

static FilterXObject *
_eval(FilterXExpr *s)
{
FilterXExprGenerator *self = (FilterXExprGenerator *) s;

FilterXObject *fillable = filterx_expr_eval_typed(self->fillable);
if (!fillable)
return NULL;

if (self->eval(self, fillable))
return fillable;

filterx_object_unref(fillable);
return NULL;
}

void
filterx_generator_init_instance(FilterXExpr *s)
{
filterx_expr_init_instance(s);
s->eval = _eval;
s->ignore_falsy_result = TRUE;
}

Expand Down
1 change: 1 addition & 0 deletions lib/filterx/expr-generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct FilterXExprGenerator_
{
FilterXExpr super;
FilterXExpr *fillable;
gboolean (*eval)(FilterXExprGenerator *self, FilterXObject *fillable);
FilterXObject *(*create_container)(FilterXExprGenerator *self, FilterXExpr *fillable_parent);
};

Expand Down
15 changes: 4 additions & 11 deletions lib/filterx/expr-literal-generator.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,19 +133,12 @@ _list_generator_create_container(FilterXExprGenerator *s, FilterXExpr *fillable_
return result;
}

static FilterXObject *
_literal_generator_eval(FilterXExpr *s)
static gboolean
_literal_generator_eval(FilterXExprGenerator *s, FilterXObject *fillable)
{
FilterXExprLiteralGenerator *self = (FilterXExprLiteralGenerator *) s;

FilterXObject *fillable = filterx_expr_eval_typed(self->super.fillable);
if (!fillable)
return NULL;

if (!_eval_elements(fillable, self->elements))
return NULL;

return fillable;
return _eval_elements(fillable, self->elements);
}

void
Expand All @@ -161,7 +154,7 @@ static void
_literal_generator_init_instance(FilterXExprLiteralGenerator *self)
{
filterx_generator_init_instance(&self->super.super);
self->super.super.eval = _literal_generator_eval;
self->super.eval = _literal_generator_eval;
self->super.super.free_fn = _literal_generator_free;
}

Expand Down
28 changes: 11 additions & 17 deletions lib/filterx/expr-regexp.c
Original file line number Diff line number Diff line change
Expand Up @@ -336,36 +336,30 @@ typedef struct FilterXExprRegexpSearchGenerator_
pcre2_code_8 *pattern;
} FilterXExprRegexpSearchGenerator;

static FilterXObject *
_regexp_search_generator_eval(FilterXExpr *s)
static gboolean
_regexp_search_generator_eval(FilterXExprGenerator *s, FilterXObject *fillable)
{
FilterXExprRegexpSearchGenerator *self = (FilterXExprRegexpSearchGenerator *) s;

FilterXObject *fillable = filterx_expr_eval_typed(self->super.fillable);
if (!fillable)
return NULL;

FilterXObject *result = NULL;
gboolean result;
FilterXReMatchState state;
_state_init(&state);

gboolean matched = _match(self->lhs, self->pattern, &state);
if (!state.match_data)
if (!matched)
{
/* Error happened during matching. */
result = TRUE;
goto exit;
}

if (matched)
if (!state.match_data)
{
if (!_store_matches(self->pattern, &state, fillable))
{
filterx_object_unref(fillable);
goto exit;
}
/* Error happened during matching. */
result = FALSE;
goto exit;
}

result = fillable;
result = _store_matches(self->pattern, &state, fillable);

exit:
_state_cleanup(&state);
Expand Down Expand Up @@ -409,7 +403,7 @@ filterx_expr_regexp_search_generator_new(FilterXExpr *lhs, const gchar *pattern)
FilterXExprRegexpSearchGenerator *self = g_new0(FilterXExprRegexpSearchGenerator, 1);

filterx_generator_init_instance(&self->super.super);
self->super.super.eval = _regexp_search_generator_eval;
self->super.eval = _regexp_search_generator_eval;
self->super.super.free_fn = _regexp_search_generator_free;
self->super.create_container = _regexp_search_generator_create_container;

Expand Down

0 comments on commit 4bdbe80

Please sign in to comment.