Skip to content

Commit

Permalink
Rewrite (?:|xyz) and (?:xyz|) to (?:xyz)?.
Browse files Browse the repository at this point in the history
  • Loading branch information
katef committed Sep 21, 2020
1 parent c36e173 commit f513fae
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/libre/ast_rewrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,28 @@ rewrite_alt(struct ast_expr *n, enum re_flags flags)
return 1;
}

if (n->u.alt.count == 2) {
struct ast_expr *child = NULL;

if (n->u.alt.n[0]->type == AST_EXPR_EMPTY) {
ast_expr_free(n->u.alt.n[0]);
child = n->u.alt.n[1];
} else if (n->u.alt.n[1]->type == AST_EXPR_EMPTY) {
ast_expr_free(n->u.alt.n[1]);
child = n->u.alt.n[0];
}

if (child != NULL) {
/* we repurpose the same node */
n->type = AST_EXPR_REPEAT;
n->u.repeat.e = child;
n->u.repeat.min = 0;
n->u.repeat.max = 1;

return rewrite(n, flags);
}
}

return 1;

empty:
Expand Down

0 comments on commit f513fae

Please sign in to comment.