[net.lang.c++] snextc function in class streambuf

mayer@rochester.ARPA (04/28/86)

From: mayer

The function "streambuf::snextc()" is undefined unless the stream buffer
has been filled at least once.  In particular, the sequence:

	filebuf input(0);

	int c = input.snextc();

results in a segmentation fault.  The problem could be fixed quite
easily by changing the implementation from:

	inline int snextc()
	{
		return ((++gptr)==pptr) ? underflow() : *gptr & 0377;
	}

to

	inline int snextc()
	{
		return ((++gptr)>=pptr) ? underflow() : *gptr & 0377;
	}

This wouldn't be any less efficient on most machines, and would allow
define "snextc" appropriately over the buffer's entire life.

By the way, replacing "*gptr & 0377" with "*(unsigned char *)gptr"
will save a little space and time on most machines.  For example,
the SUN (V2) compiler produces:

	movb a5@,d0
	andl #255,d0

for the first implementation, and

	moveq #0,d0
	movb a5@,d0

for the second.  This saves two 16 bit words of extention and 800ns on
each call.  Perhaps the buffers should be "unsigned char" also.  I
don't usually pick bits like this, but these routines are at the heart
of the IO system.

-- Jim

-- Jim Mayer					University of Rochester
(arpa) mayer@Rochester.ARPA			Department of Computer Science
(uucp) rochester!mayer				Ray P. Hylan Building
       (via allegra, decvax, or seismo)		Rochester, New York 14627