|
Message-Id: <DB44FB24-7EE0-49C9-A122-D8CA66E2D0A2@gmail.com> Date: Sun, 13 Mar 2016 12:06:39 +0100 From: Julien Ramseier <j.ramseier@...il.com> To: musl@...ts.openwall.com Subject: [PATCH] regex: support non-greedy quantifiers Here's a tiny patch to enable non-greedy regex quantifiers. This is not specified by POSIX, but I think it's a useful extension, and all the code for supporting it is already present. I tested this against the TRE and AT&T test suites (from NetBSD) and didn't found any regressions. However I don't know all the ins and outs of the implementation and I may have missed something obvious. - Julien diff --git a/src/regex/regcomp.c b/src/regex/regcomp.c index 5fad98b..cc7d633 100644 --- a/src/regex/regcomp.c +++ b/src/regex/regcomp.c @@ -979,6 +979,7 @@ static reg_errcode_t tre_parse(tre_parse_ctx_t *ctx) parse_iter: for (;;) { int min, max; + int minimal = 0; if (*s!='\\' && *s!='*') { if (!ere) @@ -1014,11 +1015,16 @@ static reg_errcode_t tre_parse(tre_parse_ctx_t *ctx) if (*s == '?') max = 1; s++; + /* Non-greedy */ + if (ere && *s == '?') { + minimal = 1; + s++; + } } if (max == 0) ctx->n = tre_ast_new_literal(ctx->mem, EMPTY, -1, -1); else - ctx->n = tre_ast_new_iter(ctx->mem, ctx->n, min, max, 0); + ctx->n = tre_ast_new_iter(ctx->mem, ctx->n, min, max, minimal); if (!ctx->n) return REG_ESPACE; } -- 2.7.2
Powered by blists - more mailing lists
Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.