[PATCH] for review Re: Sed substitution bugs
Tony Finch
dot at dotat.at
Tue Jun 3 08:19:27 PDT 2003
On Tue, Jun 03, 2003 at 03:42:25PM +0100, Tony Finch wrote:
> $ echo foo | sed 's/ */,/g'
> ,f,o,o
> $ echo foo | sed 's/ */,/3'
> ,foo
> $ echo foo | sed 's/ */,/4'
> ,foo
> $ echo foo | sed 's/ */,/5'
> ,foo
> $
$ echo foo | ./sed 's/ */,/g'
,f,o,o,
$ echo foo | ./sed 's/ */,/3'
fo,o
$ echo foo | ./sed 's/ */,/4'
foo,
$ echo foo | ./sed 's/ */,/5'
foo
$
--- process.c 20 Sep 2002 19:40:23 -0000 1.29
+++ process.c 3 Jun 2003 15:11:47 -0000
@@ -323,7 +323,7 @@
{
SPACE tspace;
regex_t *re;
- size_t re_off, slen;
+ regoff_t re_off, slen;
int lastempty, n;
char *s;
@@ -361,9 +361,6 @@
s += match[0].rm_eo;
slen -= match[0].rm_eo;
lastempty = 0;
- } else if (match[0].rm_so == slen) {
- s += match[0].rm_so;
- slen = 0;
} else {
if (match[0].rm_so == 0)
cspace(&SS, s, match[0].rm_so + 1,
@@ -375,15 +372,19 @@
slen -= match[0].rm_so + 1;
lastempty = 1;
}
- } while (slen > 0 && regexec_e(re, s, REG_NOTBOL, 0, slen));
+ } while (slen >= 0 && regexec_e(re, s, REG_NOTBOL, 0, slen));
/* Copy trailing retained string. */
if (slen > 0)
cspace(&SS, s, slen, APPEND);
break;
default: /* Nth occurrence */
while (--n) {
+ if (match[0].rm_eo == match[0].rm_so)
+ match[0].rm_eo = match[0].rm_so + 1;
s += match[0].rm_eo;
slen -= match[0].rm_eo;
+ if (slen < 0)
+ return (0);
if (!regexec_e(re, s, REG_NOTBOL, 0, slen))
return (0);
}
Tony.
--
f.a.n.finch <dot at dotat.at> http://dotat.at/
SHANNON: VARIABLE 3 BECOMING SOUTHERLY 5 OR 6. SHOWERS. GOOD.
More information about the freebsd-standards
mailing list