From 35cbb5751ed7ae578c9a6194404a516c9f9d2ef7 Mon Sep 17 00:00:00 2001 From: Kate F Date: Sun, 20 Sep 2020 22:05:12 -0700 Subject: [PATCH] Rewrite `(?:|xyz)` and `(?:xyz|)` to `(?:xyz)?`. --- src/libre/ast_rewrite.c | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/src/libre/ast_rewrite.c b/src/libre/ast_rewrite.c index 4b17b0594..88f62dcf0 100644 --- a/src/libre/ast_rewrite.c +++ b/src/libre/ast_rewrite.c @@ -317,6 +317,30 @@ 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) { + 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: