mailing list of musl libc
 help / color / mirror / code / Atom feed
From: Julien Ramseier <j.ramseier@gmail.com>
To: musl@lists.openwall.com
Subject: [PATCH] regex: support non-greedy quantifiers
Date: Sun, 13 Mar 2016 12:06:39 +0100	[thread overview]
Message-ID: <DB44FB24-7EE0-49C9-A122-D8CA66E2D0A2@gmail.com> (raw)

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


             reply	other threads:[~2016-03-13 11:06 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-03-13 11:06 Julien Ramseier [this message]
2016-06-16 15:33 ` Julien Ramseier
2016-06-16 19:10   ` Szabolcs Nagy
2016-06-16 19:41     ` Rich Felker

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=DB44FB24-7EE0-49C9-A122-D8CA66E2D0A2@gmail.com \
    --to=j.ramseier@gmail.com \
    --cc=musl@lists.openwall.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).