diff options
author | Ashwini Sharma <ak.ashwini1981@gmail.com> | 2014-11-29 18:38:05 -0600 |
---|---|---|
committer | Ashwini Sharma <ak.ashwini1981@gmail.com> | 2014-11-29 18:38:05 -0600 |
commit | c3657fd41199ca97b74e66e80734f5c4a9509da4 (patch) | |
tree | 40dea9fdcfca87c060f1657ea07b6262882de6cc /toys | |
parent | 42a36d6314e6ad7af6ab732ddc6d4e7577bb2c7b (diff) | |
download | toybox-c3657fd41199ca97b74e66e80734f5c4a9509da4.tar.gz |
expr.c: Added '==' sign into list of operations. Also added support for regex pattern match.
Diffstat (limited to 'toys')
-rw-r--r-- | toys/pending/expr.c | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/toys/pending/expr.c b/toys/pending/expr.c index dd27d58f..6742ff68 100644 --- a/toys/pending/expr.c +++ b/toys/pending/expr.c @@ -111,7 +111,23 @@ struct op { static void re(struct value *lhs, const struct value *rhs) { - error_exit("regular expression match not implemented"); + regex_t rp; + regmatch_t rm[2]; + + xregcomp(&rp, rhs->s, 0); + if (!regexec(&rp, lhs->s, 2, rm, 0) && rm[0].rm_so == 0) { + if (rp.re_nsub > 0 && rm[1].rm_so >= 0) + lhs->s = xmprintf("%.*s", rm[1].rm_eo - rm[1].rm_so, lhs->s+rm[1].rm_so); + else { + lhs->i = rm[0].rm_eo; + lhs->s = 0; + } + } else { + if (!rp.re_nsub) { + lhs->i = 0; + lhs->s = 0; + } else lhs->s = ""; + } } static void mod(struct value *lhs, const struct value *rhs) @@ -203,6 +219,7 @@ static const struct op ops[] = { {"|", or }, {"&", and }, {"=", eq }, + {"==", eq }, {">", gt }, {">=", gte }, {"<", lt }, |