Patch from Shaun Jackman:

> This patch modfies expr to use portable POSIX regex rather than BSD
> regex.
...
> This updated patch implements an anchored regex by checking that the
> match starts at offset 0.

More to the point, this patch uses the same regex that sed.c is already using
(opportunity to suck in less library code), and even building a dynamically
linked busybox with just expr the result is a slightly smaller binary (by 94
bytes, I dunno what nm --size-sort has to say about it because I didn't build
with debug info, since that changes the binary size a lot by disabling
optimization...)

Your mileage may vary.  Handle with caution.  Do not taunt happy fun ball.
This commit is contained in:
Rob Landley 2005-05-09 21:42:42 +00:00
parent b9dfb8c03f
commit 540d3f60f3

View File

@ -245,10 +245,9 @@ static int arithmetic_common (VALUE *l, VALUE *r, int op)
static VALUE *docolon (VALUE *sv, VALUE *pv) static VALUE *docolon (VALUE *sv, VALUE *pv)
{ {
VALUE *v; VALUE *v;
const char *errmsg; regex_t re_buffer;
struct re_pattern_buffer re_buffer; const int NMATCH = 2;
struct re_registers re_regs; regmatch_t re_regs[NMATCH];
int len;
tostring (sv); tostring (sv);
tostring (pv); tostring (pv);
@ -260,27 +259,22 @@ of a basic regular expression is not portable; it is being ignored",
pv->u.s); pv->u.s);
} }
len = strlen (pv->u.s);
memset (&re_buffer, 0, sizeof (re_buffer)); memset (&re_buffer, 0, sizeof (re_buffer));
memset (&re_regs, 0, sizeof (re_regs)); memset (re_regs, 0, sizeof (*re_regs));
re_buffer.allocated = 2 * len; if( regcomp (&re_buffer, pv->u.s, 0) != 0 )
re_buffer.buffer = (unsigned char *) xmalloc (re_buffer.allocated); bb_error_msg_and_die("Invalid regular expression");
re_buffer.translate = 0;
re_syntax_options = RE_SYNTAX_POSIX_BASIC;
errmsg = re_compile_pattern (pv->u.s, len, &re_buffer);
if (errmsg) {
bb_error_msg_and_die("%s", errmsg);
}
len = re_match (&re_buffer, sv->u.s, strlen (sv->u.s), 0, &re_regs); /* expr uses an anchored pattern match, so check that there was a
if (len >= 0) { * match and that the match starts at offset 0. */
if (regexec (&re_buffer, sv->u.s, NMATCH, re_regs, 0) != REG_NOMATCH &&
re_regs[0].rm_so == 0) {
/* Were \(...\) used? */ /* Were \(...\) used? */
if (re_buffer.re_nsub > 0) { /* was (re_regs.start[1] >= 0) */ if (re_buffer.re_nsub > 0) {
sv->u.s[re_regs.end[1]] = '\0'; sv->u.s[re_regs[1].rm_eo] = '\0';
v = str_value (sv->u.s + re_regs.start[1]); v = str_value (sv->u.s + re_regs[1].rm_so);
} }
else else
v = int_value (len); v = int_value (re_regs[0].rm_eo);
} }
else { else {
/* Match failed -- return the right kind of null. */ /* Match failed -- return the right kind of null. */
@ -289,7 +283,6 @@ of a basic regular expression is not portable; it is being ignored",
else else
v = int_value (0); v = int_value (0);
} }
free (re_buffer.buffer);
return v; return v;
} }