streams: get rid of last_code slot in the structure

It was used to store bytes for unread, but we are going to change how unread
works, and we still can simply test for newline and encode behavior directly in
unread-char for newlines.
This commit is contained in:
Daniel Kochmański 2025-07-26 15:27:38 +02:00
parent c8f41912a0
commit a726cdf879
5 changed files with 21 additions and 32 deletions

View file

@ -45,7 +45,6 @@ ecl_alloc_stream(void)
x->stream.byte_decoder = NULL;
x->stream.last_char = EOF;
x->stream.byte_stack = ECL_NIL;
x->stream.last_code[0] = x->stream.last_code[1] = EOF;
x->stream.eof_char = EOF;
return x;
}

View file

@ -78,8 +78,6 @@ ecl_eformat_read_char(cl_object strm)
return EOF;
if (c != EOF) {
strm->stream.last_char = c;
strm->stream.last_code[0] = c;
strm->stream.last_code[1] = EOF;
}
return c;
}
@ -90,28 +88,33 @@ ecl_eformat_unread_char(cl_object strm, ecl_character c)
unlikely_if (c != strm->stream.last_char) {
ecl_unread_twice(strm);
}
{
if (c == ECL_CHAR_CODE_NEWLINE) {
unsigned char *buffer = strm->stream.byte_buffer;
int ndx = 0;
cl_object l = strm->stream.byte_stack;
cl_fixnum i;
/* Byte stack lands in a reverse order. */
i = strm->stream.last_code[1];
if (i != EOF) {
ndx = strm->stream.encoder(strm, buffer, i);
while (ndx != 0) {
l = CONS(ecl_make_fixnum(buffer[--ndx]), l);
}
}
i = strm->stream.last_code[0];
if (i != EOF) {
ndx = strm->stream.encoder(strm, buffer, i);
while (ndx != 0) {
l = CONS(ecl_make_fixnum(buffer[--ndx]), l);
int flags = strm->stream.flags;
if (flags & ECL_STREAM_CR) {
if (flags & ECL_STREAM_LF) {
/* Byte stack lands in a reverse order. */
ndx = strm->stream.encoder(strm, buffer, ECL_CHAR_CODE_LINEFEED);
while (ndx != 0) l = CONS(ecl_make_fixnum(buffer[--ndx]), l);
}
ndx = strm->stream.encoder(strm, buffer, ECL_CHAR_CODE_RETURN);
while (ndx != 0) l = CONS(ecl_make_fixnum(buffer[--ndx]), l);
} else {
ndx = strm->stream.encoder(strm, buffer, ECL_CHAR_CODE_NEWLINE);
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;
cl_object l = strm->stream.byte_stack;
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;
}
}
@ -155,14 +158,10 @@ eformat_read_char_crlf(cl_object strm)
if (c == ECL_CHAR_CODE_RETURN) {
c = ecl_eformat_read_char(strm);
if (c == ECL_CHAR_CODE_LINEFEED) {
strm->stream.last_code[0] = ECL_CHAR_CODE_RETURN;
strm->stream.last_code[1] = c;
c = ECL_CHAR_CODE_NEWLINE;
} else {
ecl_eformat_unread_char(strm, c);
c = ECL_CHAR_CODE_RETURN;
strm->stream.last_code[0] = c;
strm->stream.last_code[1] = EOF;
}
strm->stream.last_char = c;
}

View file

@ -372,7 +372,6 @@ static ecl_character io_file_decode_char_from_buffer(cl_object strm, unsigned ch
/* Ugly handling of line breaks */
if (crlf) {
if (c == ECL_CHAR_CODE_LINEFEED) {
strm->stream.last_code[1] = c;
c = ECL_CHAR_CODE_NEWLINE;
}
else {
@ -381,17 +380,12 @@ static ecl_character io_file_decode_char_from_buffer(cl_object strm, unsigned ch
}
} else if (strm->stream.flags & ECL_STREAM_CR && c == ECL_CHAR_CODE_RETURN) {
if (strm->stream.flags & ECL_STREAM_LF) {
strm->stream.last_code[0] = c;
crlf = 1;
goto AGAIN;
}
else
c = ECL_CHAR_CODE_NEWLINE;
}
if (!crlf) {
strm->stream.last_code[0] = c;
strm->stream.last_code[1] = EOF;
}
strm->stream.last_char = c;
return c;
} else {

View file

@ -711,8 +711,6 @@ Buffer for unread bytes.
File column.
@item cl_fixnum last_char
Last character read.
@item cl_fixnum last_code[2]
Actual composition of the last character.
@item cl_fixnum int0 int1
Some integers (may be used for a specific implementation purposes).
@item cl_index byte_size

View file

@ -690,10 +690,9 @@ struct ecl_stream {
cl_object object0; /* some object */
cl_object object1; /* some object */
cl_object last_byte; /* last byte read */
cl_fixnum last_char; /* last character read */
cl_object byte_stack; /* buffer for unread bytes */
cl_index column; /* file column */
cl_fixnum last_char; /* last character read */
cl_fixnum last_code[2]; /* actual composition of last character */
cl_fixnum int0; /* some int */
cl_fixnum int1; /* some int */
cl_index byte_size; /* size of byte in binary streams */