Rich Salz <rich.salz(a)gmail.com> writes:
if there need to be negative references in array accesses (which certainly
makes sense to me, on its face), it seems reasonable to have whatever
intermediate variable be signed.
In my first C programming job I saw the source to V7 grep which had a
"foo[-2]" construct. It was a moment of enlightenment and another bit of
K&R fell into place. (
https://www.tuhs.org/cgi-bin/utree.pl?file=V7/usr/src/cmd/grep.c; search
for "[-")
Now this thread already derailed into C undefined behavior semantics,
but nobody bothered to look at the actual code, which is perfectly fine:
if ((c = *sp++) != '*')
lastep = ep;
switch (c) {
...
case '[':
...
neg = 0;
if((c = *sp++) == '^') {
neg = 1;
c = *sp++;
}
cstart = sp;
do {
...
if (c=='-' && sp>cstart && *sp!=']') {
for (c = sp[-2]; c<*sp; c++)
ep[c>>3] |= bittab[c&07];
sp++;
}
ep[c>>3] |= bittab[c&07];
} while((c = *sp++) != ']');
Since sp has been incremented twice already, accessing sp[-2] is fine
in any case, but it's also guarded by cstart, so the regexp range
"[-z]" doesn't expand to [[-z].
--
Leah Neukirchen <leah(a)vuxu.org>
https://leahneukirchen.org/