streams: invert .last_char processing and UNREAD-CHAR

Previously we've stored in this field the last read char, while now we store
there the last unread char. This way we can't tell whether the last read char
was the same as the unread one, but on the other hand this way requires less
bookeeping and the code shape is similar to UNREAD-BYTE.
This commit is contained in:
Daniel Kochmański 2025-07-28 09:45:38 +02:00
parent a726cdf879
commit 10a1e8dddf
2 changed files with 3 additions and 9 deletions

View file

@ -66,6 +66,7 @@ ecl_eformat_read_char(cl_object strm)
unsigned char *buffer_pos = buffer;
unsigned char *buffer_end = buffer;
cl_index byte_size = (strm->stream.byte_size / 8);
strm->stream.last_char = EOF;
do {
if (ecl_read_byte8(strm, buffer_end, byte_size) < byte_size) {
c = EOF;
@ -76,18 +77,16 @@ ecl_eformat_read_char(cl_object strm)
} while(c == EOF && (buffer_end - buffer) < ENCODING_BUFFER_MAX_SIZE);
unlikely_if (c == strm->stream.eof_char)
return EOF;
if (c != EOF) {
strm->stream.last_char = c;
}
return c;
}
void
ecl_eformat_unread_char(cl_object strm, ecl_character c)
{
unlikely_if (c != strm->stream.last_char) {
unlikely_if (strm->stream.last_char != EOF) {
ecl_unread_twice(strm);
}
strm->stream.last_char = c;
if (c == ECL_CHAR_CODE_NEWLINE) {
unsigned char *buffer = strm->stream.byte_buffer;
int ndx = 0;
@ -106,7 +105,6 @@ ecl_eformat_unread_char(cl_object strm, ecl_character c)
while (ndx != 0) l = CONS(ecl_make_fixnum(buffer[--ndx]), l);
}
strm->stream.byte_stack = l;
strm->stream.last_char = EOF;
} else {
unsigned char *buffer = strm->stream.byte_buffer;
int ndx = 0;
@ -114,7 +112,6 @@ ecl_eformat_unread_char(cl_object strm, ecl_character c)
ndx = strm->stream.encoder(strm, buffer, c);
while (ndx != 0) l = CONS(ecl_make_fixnum(buffer[--ndx]), l);
strm->stream.byte_stack = l;
strm->stream.last_char = EOF;
}
}
@ -135,7 +132,6 @@ eformat_read_char_cr(cl_object strm)
ecl_character c = ecl_eformat_read_char(strm);
if (c == ECL_CHAR_CODE_RETURN) {
c = ECL_CHAR_CODE_NEWLINE;
strm->stream.last_char = c;
}
return c;
}
@ -163,7 +159,6 @@ eformat_read_char_crlf(cl_object strm)
ecl_eformat_unread_char(strm, c);
c = ECL_CHAR_CODE_RETURN;
}
strm->stream.last_char = c;
}
return c;
}

View file

@ -386,7 +386,6 @@ static ecl_character io_file_decode_char_from_buffer(cl_object strm, unsigned ch
else
c = ECL_CHAR_CODE_NEWLINE;
}
strm->stream.last_char = c;
return c;
} else {
/* We need more bytes. First copy unconsumed bytes at the