Fix substitution when the regex matches the zero-length string.

There are two bugs: in the s///g case, the substitution didn't occur
at the end of the line; in the s///N case, the code didn't count
forwards along the line properly. See the sg, s3, s4, and s5 tests
in src/tools/regression/usr.bin/sed/.

Reviewed by:	tjr
This commit is contained in:
Tony Finch 2003-06-04 15:31:55 +00:00
parent 485721b25e
commit 2f17b7e279
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=115831

View File

@ -323,7 +323,7 @@ substitute(cp)
{
SPACE tspace;
regex_t *re;
size_t re_off, slen;
regoff_t re_off, slen;
int lastempty, n;
char *s;
@ -361,9 +361,6 @@ substitute(cp)
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 @@ substitute(cp)
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);
}