diff options
author | Szabolcs Nagy <nsz@port70.net> | 2015-04-18 17:53:38 +0000 |
---|---|---|
committer | Rich Felker <dalias@aerifal.cx> | 2016-01-30 20:53:52 -0500 |
commit | 831e9d9efa61566a25c1dcdbd28f55daeea4dd32 (patch) | |
tree | fc2395ef07acb1eb4fcba5508682568c2644aeac /src/regex/regcomp.c | |
parent | 25160f1c08235cf5b6a9617c5640380618a0f6ff (diff) | |
download | musl-831e9d9efa61566a25c1dcdbd28f55daeea4dd32.tar.gz musl-831e9d9efa61566a25c1dcdbd28f55daeea4dd32.tar.bz2 musl-831e9d9efa61566a25c1dcdbd28f55daeea4dd32.tar.xz musl-831e9d9efa61566a25c1dcdbd28f55daeea4dd32.zip |
regex: simplify the {,} repetition parsing logic
Diffstat (limited to 'src/regex/regcomp.c')
-rw-r--r-- | src/regex/regcomp.c | 39 |
1 files changed, 19 insertions, 20 deletions
diff --git a/src/regex/regcomp.c b/src/regex/regcomp.c index b3dbb252..7ce29889 100644 --- a/src/regex/regcomp.c +++ b/src/regex/regcomp.c @@ -708,7 +708,7 @@ static const char *parse_dup_count(const char *s, int *n) return s; } -static reg_errcode_t parse_dup(tre_parse_ctx_t *ctx, const char *s) +static const char *parse_dup(const char *s, int ere, int *pmin, int *pmax) { int min, max; @@ -723,19 +723,13 @@ static reg_errcode_t parse_dup(tre_parse_ctx_t *ctx, const char *s) max > RE_DUP_MAX || min > RE_DUP_MAX || min < 0 || - (!(ctx->cflags & REG_EXTENDED) && *s++ != '\\') || + (!ere && *s++ != '\\') || *s++ != '}' ) - return REG_BADBR; - - if (min == 0 && 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); - if (!ctx->n) - return REG_ESPACE; - ctx->s = s; - return REG_OK; + return 0; + *pmin = min; + *pmax = max; + return s; } static int hexval(unsigned c) @@ -988,6 +982,8 @@ static reg_errcode_t tre_parse(tre_parse_ctx_t *ctx) eg. (+), |*, {2}, but assertions are not treated as empty so ^* or $? are accepted currently. */ for (;;) { + int min, max; + if (*s!='\\' && *s!='*') { if (!ere) break; @@ -1007,21 +1003,24 @@ static reg_errcode_t tre_parse(tre_parse_ctx_t *ctx) sense, note however that the RE_DUP_MAX limit can be circumvented: (a{255}){255} uses a lot of memory.. */ if (*s=='{') { - err = parse_dup(ctx, s+1); - if (err != REG_OK) - return err; - s = ctx->s; + s = parse_dup(s+1, ere, &min, &max); + if (!s) + return REG_BADBR; } else { - int min=0, max=-1; + min=0; + max=-1; if (*s == '+') min = 1; if (*s == '?') max = 1; s++; - ctx->n = tre_ast_new_iter(ctx->mem, ctx->n, min, max, 0); - if (!ctx->n) - return REG_ESPACE; } + 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); + if (!ctx->n) + return REG_ESPACE; } nbranch = tre_ast_new_catenation(ctx->mem, nbranch, ctx->n); |