From 10a1e8dddf6f278c6ab738cef2f0db1effcb7d67 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Kochma=C5=84ski?= Date: Mon, 28 Jul 2025 09:45:38 +0200 Subject: [PATCH] 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. --- src/c/streams/strm_eformat.d | 11 +++-------- src/c/streams/strm_os.d | 1 - 2 files changed, 3 insertions(+), 9 deletions(-) diff --git a/src/c/streams/strm_eformat.d b/src/c/streams/strm_eformat.d index 4f1ad3503..2c9b2995a 100644 --- a/src/c/streams/strm_eformat.d +++ b/src/c/streams/strm_eformat.d @@ -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; } diff --git a/src/c/streams/strm_os.d b/src/c/streams/strm_os.d index 095d32be7..ef1d27e7f 100644 --- a/src/c/streams/strm_os.d +++ b/src/c/streams/strm_os.d @@ -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