mirror of git://git.suckless.org/sbase
expr: don't evaluate matched substr as a number
POSIX specifies that if the pattern contains a subexpression then the first matched subexpression should be returned if it exists. This fixes things like the following: ./expr 00003 : '\(.*\)' Before: 3 After: 00003
This commit is contained in:
parent
e50d533d59
commit
270ca025ce
9
expr.c
9
expr.c
|
@ -59,11 +59,9 @@ match(struct val *vstr, struct val *vregx, struct val *ret)
|
||||||
{
|
{
|
||||||
regex_t re;
|
regex_t re;
|
||||||
regmatch_t matches[2];
|
regmatch_t matches[2];
|
||||||
long long d;
|
|
||||||
size_t anchlen;
|
size_t anchlen;
|
||||||
char *s, *p, *anchreg;
|
char *s, *p, *anchreg;
|
||||||
char *str = vstr->str, *regx = vregx->str;
|
char *str = vstr->str, *regx = vregx->str;
|
||||||
const char *errstr;
|
|
||||||
|
|
||||||
/* anchored regex */
|
/* anchored regex */
|
||||||
anchlen = strlen(regx) + 1 + 1;
|
anchlen = strlen(regx) + 1 + 1;
|
||||||
|
@ -83,15 +81,8 @@ match(struct val *vstr, struct val *vregx, struct val *ret)
|
||||||
s = str + matches[1].rm_so;
|
s = str + matches[1].rm_so;
|
||||||
p = str + matches[1].rm_eo;
|
p = str + matches[1].rm_eo;
|
||||||
*p = '\0';
|
*p = '\0';
|
||||||
|
|
||||||
d = strtonum(s, LLONG_MIN, LLONG_MAX, &errstr);
|
|
||||||
if (!errstr) {
|
|
||||||
ret->num = d;
|
|
||||||
return;
|
|
||||||
} else {
|
|
||||||
ret->str = enstrdup(3, s);
|
ret->str = enstrdup(3, s);
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
regfree(&re);
|
regfree(&re);
|
||||||
str += matches[0].rm_so;
|
str += matches[0].rm_so;
|
||||||
|
|
Loading…
Reference in New Issue