Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rewrite (?:|xyz) and (?:xyz|) to (?:xyz)?. #259

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/libre/ast_rewrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,30 @@ rewrite_alt(struct ast_expr *n, enum re_flags flags)
return 1;
}

if (n->u.alt.count == 2) {
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@hvdijk points out that this would also apply for n > 2, which I hadn't thought of.

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) {
free(n->u.alt.n);

/* 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