fix off-by-one use of gensub() index parameter, and treat negative

number to mean 'replace first match'; the index use is now fully
aligned with GNU awk

fixes PR bin/25543 by Richard Rauch
This commit is contained in:
jdolecek 2004-06-05 12:01:28 +00:00
parent 388cab5e29
commit fbac8027bb

12
dist/nawk/run.c vendored
View File

@ -1946,8 +1946,16 @@ Cell *gensub(Node **a, int nnn) /* global selective substitute */
sptr = getsval(h);
if (sptr[0] == 'g' || sptr[0] == 'G')
whichm = -1;
else
whichm = (int) getfval(h);
else {
/*
* The specified number is index of replacement, starting
* from 1. GNU awk treats index lower than 0 same as
* 1, we do same for compatibility.
*/
whichm = (int) getfval(h) - 1;
if (whichm < 0)
whichm = 0;
}
tempfree(h);
if (pmatch(pfa, t)) {