1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-04-30 11:50:28 +02:00

Speed up port position access from Scheme

* libguile/ports-internal.h (scm_port_buffer_position):
  (scm_port_position_line, scm_port_position_set_line):
  (scm_port_position_column, scm_port_position_set_column): New
  helpers.
  (scm_t_port): Ports now hold position as a pair, so that Scheme can
  access it easily.
  (SCM_LINUM, SCM_COL, SCM_INCLINE, SCM_ZEROCOL, SCM_INCCOL)
  (SCM_DECCOL, SCM_TABCOL): Remove.
* libguile/ports.c (make_port_buffer): Rename from
  scm_c_make_port_buffer, make static, and take port as an argument so
  we can initialize the position field.
  (initialize_port_buffers): Adapt make_port_buffer change.
  (scm_c_make_port_with_encoding): Initialize position.
  (update_port_position): Rename from update_port_lf, and operate on
  port position objects.
  (scm_ungetc): Operate on port position objects.
  (scm_setvbuf, scm_expand_port_read_buffer_x): Adapt to
  make_port_buffer change.
  (scm_lfwrite): Adapt to call update_port_position.
  (scm_port_line, scm_set_port_line_x, scm_port_column)
  (scm_set_port_column_x): Adapt to use port positions.
* libguile/ports.h (scm_c_make_port_buffer): Remove internal decl.
* libguile/read.c: Adapt to use scm_port_line / scm_port_column instead
  of SCM_LINUM et al.
* module/ice-9/ports.scm (port-buffer-position, port-position-line)
  (port-position-column, set-port-position-line!)
  (set-port-position-column!): New accessors for the internals module.
* module/ice-9/sports.scm (advance-port-position!): Rename from
  port-advance-position! and use the new accessors.
  (read-char, port-fold-chars/iso-8859-1): Adapt to use
  advance-port-position!.
This commit is contained in:
Andy Wingo 2016-05-22 18:16:19 +02:00
parent a4b06357f6
commit fd17cf9f72
6 changed files with 147 additions and 79 deletions

View file

@ -94,6 +94,7 @@ enum scm_port_buffer_field {
SCM_PORT_BUFFER_FIELD_CUR, SCM_PORT_BUFFER_FIELD_CUR,
SCM_PORT_BUFFER_FIELD_END, SCM_PORT_BUFFER_FIELD_END,
SCM_PORT_BUFFER_FIELD_HAS_EOF_P, SCM_PORT_BUFFER_FIELD_HAS_EOF_P,
SCM_PORT_BUFFER_FIELD_POSITION,
SCM_PORT_BUFFER_FIELD_COUNT SCM_PORT_BUFFER_FIELD_COUNT
}; };
@ -152,6 +153,39 @@ scm_port_buffer_set_has_eof_p (SCM buf, SCM has_eof_p)
has_eof_p); has_eof_p);
} }
/* The port position object is a pair that is referenced by the port.
To make things easier for Scheme port code, it is also referenced by
port buffers. */
static inline SCM
scm_port_buffer_position (SCM buf)
{
return SCM_SIMPLE_VECTOR_REF (buf, SCM_PORT_BUFFER_FIELD_POSITION);
}
static inline SCM
scm_port_position_line (SCM position)
{
return scm_car (position);
}
static inline void
scm_port_position_set_line (SCM position, SCM line)
{
scm_set_car_x (position, line);
}
static inline SCM
scm_port_position_column (SCM position)
{
return scm_cdr (position);
}
static inline void
scm_port_position_set_column (SCM position, SCM column)
{
scm_set_cdr_x (position, column);
}
static inline size_t static inline size_t
scm_port_buffer_size (SCM buf) scm_port_buffer_size (SCM buf)
{ {
@ -290,8 +324,7 @@ struct scm_t_port
{ {
/* Source location information. */ /* Source location information. */
SCM file_name; SCM file_name;
long line_number; SCM position;
int column_number;
/* Port buffers. */ /* Port buffers. */
SCM read_buf; SCM read_buf;
@ -325,14 +358,6 @@ struct scm_t_port
#define SCM_FILENAME(x) (SCM_PORT (x)->file_name) #define SCM_FILENAME(x) (SCM_PORT (x)->file_name)
#define SCM_SET_FILENAME(x, n) (SCM_PORT (x)->file_name = (n)) #define SCM_SET_FILENAME(x, n) (SCM_PORT (x)->file_name = (n))
#define SCM_LINUM(x) (SCM_PORT (x)->line_number)
#define SCM_COL(x) (SCM_PORT (x)->column_number)
#define SCM_INCLINE(port) do {SCM_LINUM (port) += 1; SCM_COL (port) = 0;} while (0)
#define SCM_ZEROCOL(port) do {SCM_COL (port) = 0;} while (0)
#define SCM_INCCOL(port) do {SCM_COL (port) += 1;} while (0)
#define SCM_DECCOL(port) do {if (SCM_COL (port) > 0) SCM_COL (port) -= 1;} while (0)
#define SCM_TABCOL(port) do {SCM_COL (port) += 8 - SCM_COL (port) % 8;} while (0)
SCM_INTERNAL scm_t_iconv_descriptors * scm_i_port_iconv_descriptors (SCM port); SCM_INTERNAL scm_t_iconv_descriptors * scm_i_port_iconv_descriptors (SCM port);

View file

@ -494,13 +494,15 @@ scm_i_dynwind_current_load_port (SCM port)
/* Port buffers. */ /* Port buffers. */
SCM static SCM
scm_c_make_port_buffer (size_t size) make_port_buffer (SCM port, size_t size)
{ {
SCM ret = scm_c_make_vector (SCM_PORT_BUFFER_FIELD_COUNT, SCM_INUM0); SCM ret = scm_c_make_vector (SCM_PORT_BUFFER_FIELD_COUNT, SCM_INUM0);
SCM_SIMPLE_VECTOR_SET (ret, SCM_PORT_BUFFER_FIELD_BYTEVECTOR, SCM_SIMPLE_VECTOR_SET (ret, SCM_PORT_BUFFER_FIELD_BYTEVECTOR,
scm_c_make_bytevector (size)); scm_c_make_bytevector (size));
SCM_SIMPLE_VECTOR_SET (ret, SCM_PORT_BUFFER_FIELD_POSITION,
SCM_PORT (port)->position);
scm_port_buffer_set_has_eof_p (ret, SCM_BOOL_F); scm_port_buffer_set_has_eof_p (ret, SCM_BOOL_F);
return ret; return ret;
@ -649,8 +651,8 @@ initialize_port_buffers (SCM port)
write_buf_size = 1; write_buf_size = 1;
pt->read_buffering = read_buf_size; pt->read_buffering = read_buf_size;
pt->read_buf = scm_c_make_port_buffer (read_buf_size); pt->read_buf = make_port_buffer (port, read_buf_size);
pt->write_buf = scm_c_make_port_buffer (write_buf_size); pt->write_buf = make_port_buffer (port, write_buf_size);
} }
SCM SCM
@ -672,6 +674,7 @@ scm_c_make_port_with_encoding (scm_t_port_type *ptob, unsigned long mode_bits,
pt->conversion_strategy = conversion_strategy; pt->conversion_strategy = conversion_strategy;
pt->file_name = SCM_BOOL_F; pt->file_name = SCM_BOOL_F;
pt->iconv_descriptors = NULL; pt->iconv_descriptors = NULL;
pt->position = scm_cons (SCM_INUM0, SCM_INUM0);
pt->at_stream_start_for_bom_read = 1; pt->at_stream_start_for_bom_read = 1;
pt->at_stream_start_for_bom_write = 1; pt->at_stream_start_for_bom_write = 1;
@ -1598,27 +1601,34 @@ scm_c_read (SCM port, void *buffer, size_t size)
/* Update the line and column number of PORT after consumption of C. */ /* Update the line and column number of PORT after consumption of C. */
static inline void static inline void
update_port_lf (scm_t_wchar c, SCM port) update_port_position (SCM port, scm_t_wchar c)
{ {
SCM position = SCM_PORT (port)->position;
long line = scm_to_long (scm_port_position_line (position));
int column = scm_to_int (scm_port_position_column (position));
switch (c) switch (c)
{ {
case '\a': case '\a':
case EOF: case EOF:
break; break;
case '\b': case '\b':
SCM_DECCOL (port); if (column > 0)
scm_port_position_set_column (position, scm_from_int (column - 1));
break; break;
case '\n': case '\n':
SCM_INCLINE (port); scm_port_position_set_line (position, scm_from_long (line + 1));
scm_port_position_set_column (position, SCM_INUM0);
break; break;
case '\r': case '\r':
SCM_ZEROCOL (port); scm_port_position_set_column (position, SCM_INUM0);
break; break;
case '\t': case '\t':
SCM_TABCOL (port); scm_port_position_set_column (position,
scm_from_int (column + 8 - column % 8));
break; break;
default: default:
SCM_INCCOL (port); scm_port_position_set_column (position, scm_from_int (column + 1));
break; break;
} }
} }
@ -1898,7 +1908,7 @@ scm_getc (SCM port)
scm_port_buffer_did_take (SCM_PORT (port)->read_buf, len); scm_port_buffer_did_take (SCM_PORT (port)->read_buf, len);
if (codepoint == EOF) if (codepoint == EOF)
scm_i_clear_pending_eof (port); scm_i_clear_pending_eof (port);
update_port_lf (codepoint, port); update_port_position (port, codepoint);
return codepoint; return codepoint;
} }
@ -2031,9 +2041,18 @@ scm_ungetc (scm_t_wchar c, SCM port)
if (SCM_UNLIKELY (result != result_buf)) if (SCM_UNLIKELY (result != result_buf))
free (result); free (result);
{
long line;
int column;
line = scm_to_long (scm_port_position_line (pt->position));
column = scm_to_int (scm_port_position_column (pt->position));
if (c == '\n') if (c == '\n')
SCM_LINUM (port) -= 1; scm_port_position_set_line (pt->position, scm_from_long (line - 1));
SCM_DECCOL (port); if (column > 0)
scm_port_position_set_column (pt->position, scm_from_int (column - 1));
}
} }
#undef FUNC_NAME #undef FUNC_NAME
@ -2216,8 +2235,8 @@ SCM_DEFINE (scm_setvbuf, "setvbuf", 2, 1, 0,
SCM_SET_CELL_WORD_0 (port, tag_word); SCM_SET_CELL_WORD_0 (port, tag_word);
pt->read_buffering = read_buf_size; pt->read_buffering = read_buf_size;
pt->read_buf = scm_c_make_port_buffer (read_buf_size); pt->read_buf = make_port_buffer (port, read_buf_size);
pt->write_buf = scm_c_make_port_buffer (write_buf_size); pt->write_buf = make_port_buffer (port, write_buf_size);
if (saved_read_buf) if (saved_read_buf)
scm_unget_bytes (scm_port_buffer_take_pointer (saved_read_buf), scm_unget_bytes (scm_port_buffer_take_pointer (saved_read_buf),
@ -2563,7 +2582,7 @@ SCM_DEFINE (scm_expand_port_read_buffer_x, "expand-port-read-buffer!", 2, 1, 0,
if (SCM_UNBNDP (putback_p)) if (SCM_UNBNDP (putback_p))
putback_p = SCM_BOOL_F; putback_p = SCM_BOOL_F;
new_buf = scm_c_make_port_buffer (c_size); new_buf = make_port_buffer (port, c_size);
scm_port_buffer_set_has_eof_p (new_buf, scm_port_buffer_set_has_eof_p (new_buf,
scm_port_buffer_has_eof_p (pt->read_buf)); scm_port_buffer_has_eof_p (pt->read_buf));
if (scm_is_true (putback_p)) if (scm_is_true (putback_p))
@ -2780,7 +2799,7 @@ scm_c_write (SCM port, const void *ptr, size_t size)
void void
scm_lfwrite (const char *ptr, size_t size, SCM port) scm_lfwrite (const char *ptr, size_t size, SCM port)
{ {
int saved_line; SCM position, saved_line;
if (size == 0) if (size == 0)
return; return;
@ -2789,12 +2808,14 @@ scm_lfwrite (const char *ptr, size_t size, SCM port)
scm_c_write (port, ptr, size); scm_c_write (port, ptr, size);
saved_line = SCM_LINUM (port); position = SCM_PORT (port)->position;
saved_line = scm_port_position_line (position);
for (; size; ptr++, size--) for (; size; ptr++, size--)
update_port_lf ((scm_t_wchar) (unsigned char) *ptr, port); update_port_position (port, (scm_t_wchar) (unsigned char) *ptr);
/* Handle line buffering. */ /* Handle line buffering. */
if ((SCM_CELL_WORD_0 (port) & SCM_BUFLINE) && saved_line != SCM_LINUM (port)) if ((SCM_CELL_WORD_0 (port) & SCM_BUFLINE) &&
!scm_is_eq (saved_line, scm_port_position_line (position)))
scm_flush (port); scm_flush (port);
} }
@ -3046,7 +3067,7 @@ SCM_DEFINE (scm_port_line, "port-line", 1, 0, 0,
{ {
port = SCM_COERCE_OUTPORT (port); port = SCM_COERCE_OUTPORT (port);
SCM_VALIDATE_OPENPORT (1, port); SCM_VALIDATE_OPENPORT (1, port);
return scm_from_long (SCM_LINUM (port)); return scm_port_position_line (SCM_PORT (port)->position);
} }
#undef FUNC_NAME #undef FUNC_NAME
@ -3058,7 +3079,8 @@ SCM_DEFINE (scm_set_port_line_x, "set-port-line!", 2, 0, 0,
{ {
port = SCM_COERCE_OUTPORT (port); port = SCM_COERCE_OUTPORT (port);
SCM_VALIDATE_OPENPORT (1, port); SCM_VALIDATE_OPENPORT (1, port);
SCM_PORT (port)->line_number = scm_to_long (line); scm_to_long (line);
scm_port_position_set_line (SCM_PORT (port)->position, line);
return SCM_UNSPECIFIED; return SCM_UNSPECIFIED;
} }
#undef FUNC_NAME #undef FUNC_NAME
@ -3077,7 +3099,7 @@ SCM_DEFINE (scm_port_column, "port-column", 1, 0, 0,
{ {
port = SCM_COERCE_OUTPORT (port); port = SCM_COERCE_OUTPORT (port);
SCM_VALIDATE_OPENPORT (1, port); SCM_VALIDATE_OPENPORT (1, port);
return scm_from_int (SCM_COL (port)); return scm_port_position_column (SCM_PORT (port)->position);
} }
#undef FUNC_NAME #undef FUNC_NAME
@ -3089,7 +3111,8 @@ SCM_DEFINE (scm_set_port_column_x, "set-port-column!", 2, 0, 0,
{ {
port = SCM_COERCE_OUTPORT (port); port = SCM_COERCE_OUTPORT (port);
SCM_VALIDATE_OPENPORT (1, port); SCM_VALIDATE_OPENPORT (1, port);
SCM_PORT (port)->column_number = scm_to_int (column); scm_to_int (column);
scm_port_position_set_column (SCM_PORT (port)->position, column);
return SCM_UNSPECIFIED; return SCM_UNSPECIFIED;
} }
#undef FUNC_NAME #undef FUNC_NAME

View file

@ -131,9 +131,6 @@ SCM_API void scm_dynwind_current_output_port (SCM port);
SCM_API void scm_dynwind_current_error_port (SCM port); SCM_API void scm_dynwind_current_error_port (SCM port);
SCM_INTERNAL void scm_i_dynwind_current_load_port (SCM port); SCM_INTERNAL void scm_i_dynwind_current_load_port (SCM port);
/* Port buffers. */
SCM_INTERNAL SCM scm_c_make_port_buffer (size_t size);
/* Mode bits. */ /* Mode bits. */
SCM_INTERNAL long scm_i_mode_bits (SCM modes); SCM_INTERNAL long scm_i_mode_bits (SCM modes);
SCM_API long scm_mode_bits (char *modes); SCM_API long scm_mode_bits (char *modes);

View file

@ -149,8 +149,8 @@ scm_i_input_error (char const *function,
scm_simple_format (string_port, scm_simple_format (string_port,
scm_from_locale_string ("~A:~S:~S: ~A"), scm_from_locale_string ("~A:~S:~S: ~A"),
scm_list_4 (fn, scm_list_4 (fn,
scm_from_long (SCM_LINUM (port) + 1), scm_sum (scm_port_line (port), SCM_INUM1),
scm_from_int (SCM_COL (port) + 1), scm_sum (scm_port_column (port), SCM_INUM1),
scm_from_locale_string (message))); scm_from_locale_string (message)));
string = scm_get_output_string (string_port); string = scm_get_output_string (string_port);
@ -434,8 +434,8 @@ scm_read_sexp (scm_t_wchar chr, SCM port, scm_t_read_opts *opts)
: ')')); : ')'));
/* Need to capture line and column numbers here. */ /* Need to capture line and column numbers here. */
long line = SCM_LINUM (port); long line = scm_to_long (scm_port_line (port));
int column = SCM_COL (port) - 1; int column = scm_to_int (scm_port_column (port)) - 1;
c = flush_ws (port, opts, FUNC_NAME); c = flush_ws (port, opts, FUNC_NAME);
if (terminating_char == c) if (terminating_char == c)
@ -612,8 +612,8 @@ scm_read_string_like_syntax (int chr, SCM port, scm_t_read_opts *opts)
scm_t_wchar c, c_str[READER_STRING_BUFFER_SIZE]; scm_t_wchar c, c_str[READER_STRING_BUFFER_SIZE];
/* Need to capture line and column numbers here. */ /* Need to capture line and column numbers here. */
long line = SCM_LINUM (port); long line = scm_to_long (scm_port_line (port));
int column = SCM_COL (port) - 1; int column = scm_to_int (scm_port_column (port)) - 1;
while (chr != (c = scm_getc (port))) while (chr != (c = scm_getc (port)))
{ {
@ -739,8 +739,8 @@ scm_read_number (scm_t_wchar chr, SCM port, scm_t_read_opts *opts)
size_t bytes_read; size_t bytes_read;
/* Need to capture line and column numbers here. */ /* Need to capture line and column numbers here. */
long line = SCM_LINUM (port); long line = scm_to_long (scm_port_line (port));
int column = SCM_COL (port) - 1; int column = scm_to_int (scm_port_column (port)) - 1;
scm_ungetc (chr, port); scm_ungetc (chr, port);
buffer = read_complete_token (port, opts, local_buffer, sizeof local_buffer, buffer = read_complete_token (port, opts, local_buffer, sizeof local_buffer,
@ -759,7 +759,9 @@ scm_read_number (scm_t_wchar chr, SCM port, scm_t_read_opts *opts)
else if (SCM_NIMP (result)) else if (SCM_NIMP (result))
result = maybe_annotate_source (result, port, opts, line, column); result = maybe_annotate_source (result, port, opts, line, column);
SCM_COL (port) += scm_i_string_length (str); scm_set_port_column_x (port,
scm_sum (scm_port_column (port),
scm_string_length (str)));
return result; return result;
} }
@ -796,7 +798,9 @@ scm_read_mixed_case_symbol (scm_t_wchar chr, SCM port, scm_t_read_opts *opts)
result = scm_string_to_symbol (str); result = scm_string_to_symbol (str);
} }
SCM_COL (port) += scm_i_string_length (str); scm_set_port_column_x (port,
scm_sum (scm_port_column (port),
scm_string_length (str)));
return result; return result;
} }
@ -845,7 +849,9 @@ scm_read_number_and_radix (scm_t_wchar chr, SCM port, scm_t_read_opts *opts)
result = scm_string_to_number (str, scm_from_uint (radix)); result = scm_string_to_number (str, scm_from_uint (radix));
SCM_COL (port) += scm_i_string_length (str); scm_set_port_column_x (port,
scm_sum (scm_port_column (port),
scm_string_length (str)));
if (scm_is_true (result)) if (scm_is_true (result))
return result; return result;
@ -860,8 +866,8 @@ static SCM
scm_read_quote (int chr, SCM port, scm_t_read_opts *opts) scm_read_quote (int chr, SCM port, scm_t_read_opts *opts)
{ {
SCM p; SCM p;
long line = SCM_LINUM (port); long line = scm_to_long (scm_port_line (port));
int column = SCM_COL (port) - 1; int column = scm_to_int (scm_port_column (port)) - 1;
switch (chr) switch (chr)
{ {
@ -907,8 +913,8 @@ static SCM
scm_read_syntax (int chr, SCM port, scm_t_read_opts *opts) scm_read_syntax (int chr, SCM port, scm_t_read_opts *opts)
{ {
SCM p; SCM p;
long line = SCM_LINUM (port); long line = scm_to_long (scm_port_line (port));
int column = SCM_COL (port) - 1; int column = scm_to_int (scm_port_column (port)) - 1;
switch (chr) switch (chr)
{ {
@ -1068,7 +1074,7 @@ scm_read_character (scm_t_wchar chr, SCM port, scm_t_read_opts *opts)
((unsigned char) buffer[0] <= 127 ((unsigned char) buffer[0] <= 127
|| scm_is_eq (pt->encoding, sym_ISO_8859_1))) || scm_is_eq (pt->encoding, sym_ISO_8859_1)))
{ {
SCM_COL (port) += 1; scm_set_port_column_x (port, scm_sum (scm_port_column (port), SCM_INUM1));
return SCM_MAKE_CHAR (buffer[0]); return SCM_MAKE_CHAR (buffer[0]);
} }
@ -1076,7 +1082,9 @@ scm_read_character (scm_t_wchar chr, SCM port, scm_t_read_opts *opts)
processing. */ processing. */
charname = scm_from_port_stringn (buffer, bytes_read, port); charname = scm_from_port_stringn (buffer, bytes_read, port);
charname_len = scm_i_string_length (charname); charname_len = scm_i_string_length (charname);
SCM_COL (port) += charname_len; scm_set_port_column_x (port,
scm_sum (scm_port_column (port),
scm_from_size_t (charname_len)));
cp = scm_i_string_ref (charname, 0); cp = scm_i_string_ref (charname, 0);
if (charname_len == 1) if (charname_len == 1)
return SCM_MAKE_CHAR (cp); return SCM_MAKE_CHAR (cp);
@ -1629,8 +1637,8 @@ scm_read_sharp_extension (int chr, SCM port, scm_t_read_opts *opts)
proc = scm_get_hash_procedure (chr); proc = scm_get_hash_procedure (chr);
if (scm_is_true (scm_procedure_p (proc))) if (scm_is_true (scm_procedure_p (proc)))
{ {
long line = SCM_LINUM (port); long line = scm_to_long (scm_port_line (port));
int column = SCM_COL (port) - 2; int column = scm_to_int (scm_port_column (port)) - 2;
SCM got; SCM got;
got = scm_call_2 (proc, SCM_MAKE_CHAR (chr), port); got = scm_call_2 (proc, SCM_MAKE_CHAR (chr), port);
@ -1782,8 +1790,8 @@ read_inner_expression (SCM port, scm_t_read_opts *opts)
be part of an unescaped symbol. We might as well do be part of an unescaped symbol. We might as well do
something useful with it, so we adopt Kawa's convention: something useful with it, so we adopt Kawa's convention:
[...] => ($bracket-list$ ...) */ [...] => ($bracket-list$ ...) */
long line = SCM_LINUM (port); long line = scm_to_long (scm_port_line (port));
int column = SCM_COL (port) - 1; int column = scm_to_int (scm_port_column (port)) - 1;
return maybe_annotate_source return maybe_annotate_source
(scm_cons (sym_bracket_list, scm_read_sexp (chr, port, opts)), (scm_cons (sym_bracket_list, scm_read_sexp (chr, port, opts)),
port, opts, line, column); port, opts, line, column);
@ -1805,8 +1813,8 @@ read_inner_expression (SCM port, scm_t_read_opts *opts)
return (scm_read_quote (chr, port, opts)); return (scm_read_quote (chr, port, opts));
case '#': case '#':
{ {
long line = SCM_LINUM (port); long line = scm_to_long (scm_port_line (port));
int column = SCM_COL (port) - 1; int column = scm_to_int (scm_port_column (port)) - 1;
SCM result = scm_read_sharp (chr, port, opts, line, column); SCM result = scm_read_sharp (chr, port, opts, line, column);
if (scm_is_eq (result, SCM_UNSPECIFIED)) if (scm_is_eq (result, SCM_UNSPECIFIED))
/* We read a comment or some such. */ /* We read a comment or some such. */
@ -1870,8 +1878,8 @@ scm_read_expression (SCM port, scm_t_read_opts *opts)
if (c == EOF) if (c == EOF)
return SCM_EOF_VAL; return SCM_EOF_VAL;
scm_ungetc (c, port); scm_ungetc (c, port);
line = SCM_LINUM (port); line = scm_to_long (scm_port_line (port));
column = SCM_COL (port); column = scm_to_int (scm_port_column (port));
} }
expr = read_inner_expression (port, opts); expr = read_inner_expression (port, opts);

View file

@ -169,9 +169,14 @@ interpret its input and output."
port-buffer-cur port-buffer-cur
port-buffer-end port-buffer-end
port-buffer-has-eof? port-buffer-has-eof?
port-buffer-position
set-port-buffer-cur! set-port-buffer-cur!
set-port-buffer-end! set-port-buffer-end!
set-port-buffer-has-eof?! set-port-buffer-has-eof?!
port-position-line
port-position-column
set-port-position-line!
set-port-position-column!
port-read port-read
port-write port-write
port-clear-stream-start-for-bom-read port-clear-stream-start-for-bom-read
@ -188,6 +193,7 @@ interpret its input and output."
(define-syntax-rule (port-buffer-cur buf) (vector-ref buf 1)) (define-syntax-rule (port-buffer-cur buf) (vector-ref buf 1))
(define-syntax-rule (port-buffer-end buf) (vector-ref buf 2)) (define-syntax-rule (port-buffer-end buf) (vector-ref buf 2))
(define-syntax-rule (port-buffer-has-eof? buf) (vector-ref buf 3)) (define-syntax-rule (port-buffer-has-eof? buf) (vector-ref buf 3))
(define-syntax-rule (port-buffer-position buf) (vector-ref buf 4))
(define-syntax-rule (set-port-buffer-cur! buf cur) (define-syntax-rule (set-port-buffer-cur! buf cur)
(vector-set! buf 1 cur)) (vector-set! buf 1 cur))
@ -196,6 +202,15 @@ interpret its input and output."
(define-syntax-rule (set-port-buffer-has-eof?! buf has-eof?) (define-syntax-rule (set-port-buffer-has-eof?! buf has-eof?)
(vector-set! buf 3 has-eof?)) (vector-set! buf 3 has-eof?))
(define-syntax-rule (port-position-line position)
(car position))
(define-syntax-rule (port-position-column position)
(cdr position))
(define-syntax-rule (set-port-position-line! position line)
(set-car! position line))
(define-syntax-rule (set-port-position-column! position column)
(set-cdr! position column))
(eval-when (expand) (eval-when (expand)
(define-syntax-rule (private-port-bindings binding ...) (define-syntax-rule (private-port-bindings binding ...)
(begin (begin

View file

@ -384,34 +384,34 @@
(peek-bytes port 1 fast-path (peek-bytes port 1 fast-path
(lambda (buf bv cur buffered) (slow-path)))) (lambda (buf bv cur buffered) (slow-path))))
(define-inlinable (port-advance-position! port char) (define-inlinable (advance-port-position! pos char)
;; FIXME: this cond is a speed hack; really we should just compile ;; FIXME: this cond is a speed hack; really we should just compile
;; `case' better. ;; `case' better.
(cond (cond
;; FIXME: char>? et al should compile well. ;; FIXME: char>? et al should compile well.
((<= (char->integer #\space) (char->integer char)) ((<= (char->integer #\space) (char->integer char))
(set-port-column! port (1+ (port-column port)))) (set-port-position-column! pos (1+ (port-position-column pos))))
(else (else
(case char (case char
((#\alarm) #t) ; No change. ((#\alarm) #t) ; No change.
((#\backspace) ((#\backspace)
(let ((col (port-column port))) (let ((col (port-position-column pos)))
(when (> col 0) (when (> col 0)
(set-port-column! port (1- col))))) (set-port-position-column! pos (1- col)))))
((#\newline) ((#\newline)
(set-port-line! port (1+ (port-line port))) (set-port-position-line! pos (1+ (port-position-line pos)))
(set-port-column! port 0)) (set-port-position-column! pos 0))
((#\return) ((#\return)
(set-port-column! port 0)) (set-port-position-column! pos 0))
((#\tab) ((#\tab)
(let ((col (port-column port))) (let ((col (port-position-column pos)))
(set-port-column! port (- (+ col 8) (remainder col 8))))) (set-port-position-column! pos (- (+ col 8) (remainder col 8)))))
(else (else
(set-port-column! port (1+ (port-column port)))))))) (set-port-position-column! pos (1+ (port-position-column pos))))))))
(define* (read-char #:optional (port (current-input-port))) (define* (read-char #:optional (port (current-input-port)))
(define (finish char) (define (finish buf char)
(port-advance-position! port char) (advance-port-position! (port-buffer-position buf) char)
char) char)
(define (slow-path) (define (slow-path)
(call-with-values (lambda () (peek-char-and-len port)) (call-with-values (lambda () (peek-char-and-len port))
@ -422,7 +422,7 @@
(begin (begin
(set-port-buffer-has-eof?! buf #f) (set-port-buffer-has-eof?! buf #f)
char) char)
(finish char)))))) (finish buf char))))))
(define (fast-path buf bv cur buffered) (define (fast-path buf bv cur buffered)
(let ((u8 (bytevector-u8-ref bv cur)) (let ((u8 (bytevector-u8-ref bv cur))
(enc (%port-encoding port))) (enc (%port-encoding port)))
@ -431,11 +431,11 @@
(decode-utf8 bv cur buffered u8 (decode-utf8 bv cur buffered u8
(lambda (char len) (lambda (char len)
(set-port-buffer-cur! buf (+ cur len)) (set-port-buffer-cur! buf (+ cur len))
(finish char)) (finish buf char))
slow-path)) slow-path))
((ISO-8859-1) ((ISO-8859-1)
(set-port-buffer-cur! buf (+ cur 1)) (set-port-buffer-cur! buf (+ cur 1))
(finish (integer->char u8))) (finish buf (integer->char u8)))
(else (slow-path))))) (else (slow-path)))))
(peek-bytes port 1 fast-path (peek-bytes port 1 fast-path
(lambda (buf bv cur buffered) (slow-path)))) (lambda (buf bv cur buffered) (slow-path))))
@ -460,7 +460,7 @@
(let ((ch (integer->char (bytevector-u8-ref bv cur))) (let ((ch (integer->char (bytevector-u8-ref bv cur)))
(cur (1+ cur))) (cur (1+ cur)))
(set-port-buffer-cur! buf cur) (set-port-buffer-cur! buf cur)
(port-advance-position! port ch) (advance-port-position! (port-buffer-position buf) ch)
(call-with-values (lambda () (proc ch seed)) (call-with-values (lambda () (proc ch seed))
(lambda (seed done?) (lambda (seed done?)
(if done? seed (fold-chars cur seed))))))))))) (if done? seed (fold-chars cur seed)))))))))))