mirror of
https://git.savannah.gnu.org/git/guile.git
synced 2025-05-20 11:40:18 +02:00
Refactor to internal get/peek-byte functions
* libguile/ports.h (scm_get_byte_or_eof_unlocked) (scm_peek_byte_or_eof_unlocked): Remove inline functions. The important uses are in ports.c anyway and we will use a static function there. (scm_slow_get_byte_or_eof_unlocked) (scm_slow_peek_byte_or_eof_unlocked): Remove declarations without definitions. * libguile/ports.c (looking_at_bytes): Use scm_peek_byte_or_eof instead of the _unlocked variant. (get_byte_or_eof, peek_byte_or_eof): New static functions. (scm_get_byte_or_eof, scm_peek_byte_or_eof): Don't lock: the port buffer mechanism means that we won't crash. More comments to come. (get_utf8_codepoint, get_latin1_codepoint, get_iconv_codepoint): Use new static functions. * libguile/read.c (read_token, scm_read_semicolon_comment): Use scm_get_byte_or_eof, not scm_get_byte_or_eof_unlocked.
This commit is contained in:
parent
3e951f7dfc
commit
fb577b59af
3 changed files with 90 additions and 112 deletions
115
libguile/ports.c
115
libguile/ports.c
|
@ -1001,7 +1001,7 @@ looking_at_bytes (SCM port, const unsigned char *bytes, int len)
|
||||||
scm_t_port *pt = SCM_PTAB_ENTRY (port);
|
scm_t_port *pt = SCM_PTAB_ENTRY (port);
|
||||||
int i = 0;
|
int i = 0;
|
||||||
|
|
||||||
while (i < len && scm_peek_byte_or_eof_unlocked (port) == bytes[i])
|
while (i < len && scm_peek_byte_or_eof (port) == bytes[i])
|
||||||
{
|
{
|
||||||
scm_port_buffer_did_take (pt->read_buf, 1);
|
scm_port_buffer_did_take (pt->read_buf, 1);
|
||||||
i++;
|
i++;
|
||||||
|
@ -1364,32 +1364,91 @@ scm_dynwind_lock_port (SCM port)
|
||||||
|
|
||||||
/* Input. */
|
/* Input. */
|
||||||
|
|
||||||
|
static int
|
||||||
|
get_byte_or_eof (SCM port)
|
||||||
|
{
|
||||||
|
SCM buf = SCM_PTAB_ENTRY (port)->read_buf;
|
||||||
|
SCM buf_bv, buf_cur, buf_end;
|
||||||
|
size_t cur;
|
||||||
|
|
||||||
|
buf_bv = scm_port_buffer_bytevector (buf);
|
||||||
|
buf_cur = scm_port_buffer_cur (buf);
|
||||||
|
buf_end = scm_port_buffer_end (buf);
|
||||||
|
cur = SCM_I_INUM (buf_cur);
|
||||||
|
|
||||||
|
if (SCM_LIKELY (SCM_I_INUMP (buf_cur))
|
||||||
|
&& SCM_LIKELY (SCM_I_INUMP (buf_end))
|
||||||
|
&& SCM_LIKELY (cur < SCM_I_INUM (buf_end))
|
||||||
|
&& SCM_LIKELY (cur < SCM_BYTEVECTOR_LENGTH (buf_bv)))
|
||||||
|
{
|
||||||
|
scm_t_uint8 ret = SCM_BYTEVECTOR_CONTENTS (buf_bv)[cur];
|
||||||
|
scm_port_buffer_set_cur (buf, SCM_I_MAKINUM (cur + 1));
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
buf = scm_fill_input (port);
|
||||||
|
buf_bv = scm_port_buffer_bytevector (buf);
|
||||||
|
buf_cur = scm_port_buffer_cur (buf);
|
||||||
|
buf_end = scm_port_buffer_end (buf);
|
||||||
|
cur = scm_to_size_t (buf_cur);
|
||||||
|
if (cur < scm_to_size_t (buf_end))
|
||||||
|
{
|
||||||
|
scm_t_uint8 ret = SCM_BYTEVECTOR_CONTENTS (buf_bv)[cur];
|
||||||
|
scm_port_buffer_set_cur (buf, SCM_I_MAKINUM (cur + 1));
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* The next peek or get should cause the read() function to be called
|
||||||
|
to see if we still have EOF. */
|
||||||
|
scm_port_buffer_set_has_eof_p (buf, SCM_BOOL_F);
|
||||||
|
return EOF;
|
||||||
|
}
|
||||||
|
|
||||||
|
/* Like `scm_get_byte_or_eof' but does not change PORT's `read_pos'. */
|
||||||
|
static int
|
||||||
|
peek_byte_or_eof (SCM port)
|
||||||
|
{
|
||||||
|
SCM buf = SCM_PTAB_ENTRY (port)->read_buf;
|
||||||
|
SCM buf_bv, buf_cur, buf_end;
|
||||||
|
size_t cur;
|
||||||
|
|
||||||
|
buf_bv = scm_port_buffer_bytevector (buf);
|
||||||
|
buf_cur = scm_port_buffer_cur (buf);
|
||||||
|
buf_end = scm_port_buffer_end (buf);
|
||||||
|
cur = scm_to_size_t (buf_cur);
|
||||||
|
if (SCM_LIKELY (SCM_I_INUMP (buf_cur))
|
||||||
|
&& SCM_LIKELY (SCM_I_INUMP (buf_end))
|
||||||
|
&& SCM_LIKELY (cur < SCM_I_INUM (buf_end))
|
||||||
|
&& SCM_LIKELY (cur < SCM_BYTEVECTOR_LENGTH (buf_bv)))
|
||||||
|
{
|
||||||
|
scm_t_uint8 ret = SCM_BYTEVECTOR_CONTENTS (buf_bv)[cur];
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
buf = scm_fill_input (port);
|
||||||
|
buf_bv = scm_port_buffer_bytevector (buf);
|
||||||
|
buf_cur = scm_port_buffer_cur (buf);
|
||||||
|
buf_end = scm_port_buffer_end (buf);
|
||||||
|
cur = scm_to_size_t (buf_cur);
|
||||||
|
if (cur < scm_to_size_t (buf_end))
|
||||||
|
{
|
||||||
|
scm_t_uint8 ret = SCM_BYTEVECTOR_CONTENTS (buf_bv)[cur];
|
||||||
|
return ret;
|
||||||
|
}
|
||||||
|
|
||||||
|
return EOF;
|
||||||
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
scm_get_byte_or_eof (SCM port)
|
scm_get_byte_or_eof (SCM port)
|
||||||
{
|
{
|
||||||
scm_i_pthread_mutex_t *lock;
|
return get_byte_or_eof (port);
|
||||||
int ret;
|
|
||||||
|
|
||||||
scm_c_lock_port (port, &lock);
|
|
||||||
ret = scm_get_byte_or_eof_unlocked (port);
|
|
||||||
if (lock)
|
|
||||||
scm_i_pthread_mutex_unlock (lock);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
scm_peek_byte_or_eof (SCM port)
|
scm_peek_byte_or_eof (SCM port)
|
||||||
{
|
{
|
||||||
scm_i_pthread_mutex_t *lock;
|
return peek_byte_or_eof (port);
|
||||||
int ret;
|
|
||||||
|
|
||||||
scm_c_lock_port (port, &lock);
|
|
||||||
ret = scm_peek_byte_or_eof_unlocked (port);
|
|
||||||
if (lock)
|
|
||||||
scm_i_pthread_mutex_unlock (lock);
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static size_t
|
static size_t
|
||||||
|
@ -1648,7 +1707,7 @@ get_utf8_codepoint (SCM port, scm_t_wchar *codepoint,
|
||||||
*len = 0;
|
*len = 0;
|
||||||
pt = SCM_PTAB_ENTRY (port);
|
pt = SCM_PTAB_ENTRY (port);
|
||||||
|
|
||||||
byte = scm_get_byte_or_eof_unlocked (port);
|
byte = get_byte_or_eof (port);
|
||||||
if (byte == EOF)
|
if (byte == EOF)
|
||||||
{
|
{
|
||||||
*codepoint = EOF;
|
*codepoint = EOF;
|
||||||
|
@ -1664,7 +1723,7 @@ get_utf8_codepoint (SCM port, scm_t_wchar *codepoint,
|
||||||
else if (buf[0] >= 0xc2 && buf[0] <= 0xdf)
|
else if (buf[0] >= 0xc2 && buf[0] <= 0xdf)
|
||||||
{
|
{
|
||||||
/* 2-byte form. */
|
/* 2-byte form. */
|
||||||
byte = scm_peek_byte_or_eof_unlocked (port);
|
byte = peek_byte_or_eof (port);
|
||||||
ASSERT_NOT_EOF (byte);
|
ASSERT_NOT_EOF (byte);
|
||||||
|
|
||||||
if (SCM_UNLIKELY ((byte & 0xc0) != 0x80))
|
if (SCM_UNLIKELY ((byte & 0xc0) != 0x80))
|
||||||
|
@ -1680,7 +1739,7 @@ get_utf8_codepoint (SCM port, scm_t_wchar *codepoint,
|
||||||
else if ((buf[0] & 0xf0) == 0xe0)
|
else if ((buf[0] & 0xf0) == 0xe0)
|
||||||
{
|
{
|
||||||
/* 3-byte form. */
|
/* 3-byte form. */
|
||||||
byte = scm_peek_byte_or_eof_unlocked (port);
|
byte = peek_byte_or_eof (port);
|
||||||
ASSERT_NOT_EOF (byte);
|
ASSERT_NOT_EOF (byte);
|
||||||
|
|
||||||
if (SCM_UNLIKELY ((byte & 0xc0) != 0x80
|
if (SCM_UNLIKELY ((byte & 0xc0) != 0x80
|
||||||
|
@ -1692,7 +1751,7 @@ get_utf8_codepoint (SCM port, scm_t_wchar *codepoint,
|
||||||
buf[1] = (scm_t_uint8) byte;
|
buf[1] = (scm_t_uint8) byte;
|
||||||
*len = 2;
|
*len = 2;
|
||||||
|
|
||||||
byte = scm_peek_byte_or_eof_unlocked (port);
|
byte = peek_byte_or_eof (port);
|
||||||
ASSERT_NOT_EOF (byte);
|
ASSERT_NOT_EOF (byte);
|
||||||
|
|
||||||
if (SCM_UNLIKELY ((byte & 0xc0) != 0x80))
|
if (SCM_UNLIKELY ((byte & 0xc0) != 0x80))
|
||||||
|
@ -1709,7 +1768,7 @@ get_utf8_codepoint (SCM port, scm_t_wchar *codepoint,
|
||||||
else if (buf[0] >= 0xf0 && buf[0] <= 0xf4)
|
else if (buf[0] >= 0xf0 && buf[0] <= 0xf4)
|
||||||
{
|
{
|
||||||
/* 4-byte form. */
|
/* 4-byte form. */
|
||||||
byte = scm_peek_byte_or_eof_unlocked (port);
|
byte = peek_byte_or_eof (port);
|
||||||
ASSERT_NOT_EOF (byte);
|
ASSERT_NOT_EOF (byte);
|
||||||
|
|
||||||
if (SCM_UNLIKELY (((byte & 0xc0) != 0x80)
|
if (SCM_UNLIKELY (((byte & 0xc0) != 0x80)
|
||||||
|
@ -1721,7 +1780,7 @@ get_utf8_codepoint (SCM port, scm_t_wchar *codepoint,
|
||||||
buf[1] = (scm_t_uint8) byte;
|
buf[1] = (scm_t_uint8) byte;
|
||||||
*len = 2;
|
*len = 2;
|
||||||
|
|
||||||
byte = scm_peek_byte_or_eof_unlocked (port);
|
byte = peek_byte_or_eof (port);
|
||||||
ASSERT_NOT_EOF (byte);
|
ASSERT_NOT_EOF (byte);
|
||||||
|
|
||||||
if (SCM_UNLIKELY ((byte & 0xc0) != 0x80))
|
if (SCM_UNLIKELY ((byte & 0xc0) != 0x80))
|
||||||
|
@ -1731,7 +1790,7 @@ get_utf8_codepoint (SCM port, scm_t_wchar *codepoint,
|
||||||
buf[2] = (scm_t_uint8) byte;
|
buf[2] = (scm_t_uint8) byte;
|
||||||
*len = 3;
|
*len = 3;
|
||||||
|
|
||||||
byte = scm_peek_byte_or_eof_unlocked (port);
|
byte = peek_byte_or_eof (port);
|
||||||
ASSERT_NOT_EOF (byte);
|
ASSERT_NOT_EOF (byte);
|
||||||
|
|
||||||
if (SCM_UNLIKELY ((byte & 0xc0) != 0x80))
|
if (SCM_UNLIKELY ((byte & 0xc0) != 0x80))
|
||||||
|
@ -1771,7 +1830,7 @@ static int
|
||||||
get_latin1_codepoint (SCM port, scm_t_wchar *codepoint,
|
get_latin1_codepoint (SCM port, scm_t_wchar *codepoint,
|
||||||
char buf[SCM_MBCHAR_BUF_SIZE], size_t *len)
|
char buf[SCM_MBCHAR_BUF_SIZE], size_t *len)
|
||||||
{
|
{
|
||||||
*codepoint = scm_get_byte_or_eof_unlocked (port);
|
*codepoint = get_byte_or_eof (port);
|
||||||
|
|
||||||
if (*codepoint == EOF)
|
if (*codepoint == EOF)
|
||||||
*len = 0;
|
*len = 0;
|
||||||
|
@ -1801,7 +1860,7 @@ get_iconv_codepoint (SCM port, scm_t_wchar *codepoint,
|
||||||
char *input, *output;
|
char *input, *output;
|
||||||
size_t input_left, output_left, done;
|
size_t input_left, output_left, done;
|
||||||
|
|
||||||
byte_read = scm_get_byte_or_eof_unlocked (port);
|
byte_read = get_byte_or_eof (port);
|
||||||
if (SCM_UNLIKELY (byte_read == EOF))
|
if (SCM_UNLIKELY (byte_read == EOF))
|
||||||
{
|
{
|
||||||
if (SCM_LIKELY (input_size == 0))
|
if (SCM_LIKELY (input_size == 0))
|
||||||
|
|
|
@ -308,11 +308,7 @@ SCM_INLINE int scm_c_try_lock_port (SCM port, scm_i_pthread_mutex_t **lock);
|
||||||
|
|
||||||
/* Input. */
|
/* Input. */
|
||||||
SCM_API int scm_get_byte_or_eof (SCM port);
|
SCM_API int scm_get_byte_or_eof (SCM port);
|
||||||
SCM_INLINE int scm_get_byte_or_eof_unlocked (SCM port);
|
|
||||||
SCM_API int scm_slow_get_byte_or_eof_unlocked (SCM port);
|
|
||||||
SCM_API int scm_peek_byte_or_eof (SCM port);
|
SCM_API int scm_peek_byte_or_eof (SCM port);
|
||||||
SCM_INLINE int scm_peek_byte_or_eof_unlocked (SCM port);
|
|
||||||
SCM_API int scm_slow_peek_byte_or_eof_unlocked (SCM port);
|
|
||||||
SCM_API size_t scm_c_read (SCM port, void *buffer, size_t size);
|
SCM_API size_t scm_c_read (SCM port, void *buffer, size_t size);
|
||||||
SCM_API size_t scm_c_read_unlocked (SCM port, void *buffer, size_t size);
|
SCM_API size_t scm_c_read_unlocked (SCM port, void *buffer, size_t size);
|
||||||
SCM_API size_t scm_c_read_bytes (SCM port, SCM dst, size_t start, size_t count);
|
SCM_API size_t scm_c_read_bytes (SCM port, SCM dst, size_t start, size_t count);
|
||||||
|
@ -421,83 +417,6 @@ scm_c_try_lock_port (SCM port, scm_i_pthread_mutex_t **lock)
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
SCM_INLINE_IMPLEMENTATION int
|
|
||||||
scm_get_byte_or_eof_unlocked (SCM port)
|
|
||||||
{
|
|
||||||
SCM buf = SCM_PTAB_ENTRY (port)->read_buf;
|
|
||||||
SCM buf_bv, buf_cur, buf_end;
|
|
||||||
size_t cur;
|
|
||||||
|
|
||||||
buf_bv = SCM_SIMPLE_VECTOR_REF (buf, SCM_PORT_BUFFER_FIELD_BYTEVECTOR);
|
|
||||||
buf_cur = SCM_SIMPLE_VECTOR_REF (buf, SCM_PORT_BUFFER_FIELD_CUR);
|
|
||||||
buf_end = SCM_SIMPLE_VECTOR_REF (buf, SCM_PORT_BUFFER_FIELD_END);
|
|
||||||
cur = SCM_I_INUM (buf_cur);
|
|
||||||
|
|
||||||
if (SCM_LIKELY (SCM_I_INUMP (buf_cur))
|
|
||||||
&& SCM_LIKELY (SCM_I_INUMP (buf_end))
|
|
||||||
&& SCM_LIKELY (cur < SCM_I_INUM (buf_end))
|
|
||||||
&& SCM_LIKELY (cur < SCM_BYTEVECTOR_LENGTH (buf_bv)))
|
|
||||||
{
|
|
||||||
scm_t_uint8 ret = SCM_BYTEVECTOR_CONTENTS (buf_bv)[cur];
|
|
||||||
buf_cur = SCM_I_MAKINUM (cur + 1);
|
|
||||||
SCM_SIMPLE_VECTOR_SET (buf, SCM_PORT_BUFFER_FIELD_CUR, buf_cur);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
buf = scm_fill_input_unlocked (port);
|
|
||||||
buf_bv = SCM_SIMPLE_VECTOR_REF (buf, SCM_PORT_BUFFER_FIELD_BYTEVECTOR);
|
|
||||||
buf_cur = SCM_SIMPLE_VECTOR_REF (buf, SCM_PORT_BUFFER_FIELD_CUR);
|
|
||||||
buf_end = SCM_SIMPLE_VECTOR_REF (buf, SCM_PORT_BUFFER_FIELD_END);
|
|
||||||
cur = scm_to_size_t (buf_cur);
|
|
||||||
if (cur < scm_to_size_t (buf_end))
|
|
||||||
{
|
|
||||||
scm_t_uint8 ret = SCM_BYTEVECTOR_CONTENTS (buf_bv)[cur];
|
|
||||||
buf_cur = SCM_I_MAKINUM (cur + 1);
|
|
||||||
SCM_SIMPLE_VECTOR_SET (buf, SCM_PORT_BUFFER_FIELD_CUR, buf_cur);
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* The next peek or get should cause the read() function to be called
|
|
||||||
to see if we still have EOF. */
|
|
||||||
SCM_SIMPLE_VECTOR_SET (buf, SCM_PORT_BUFFER_FIELD_HAS_EOF_P, SCM_BOOL_F);
|
|
||||||
return EOF;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Like `scm_get_byte_or_eof' but does not change PORT's `read_pos'. */
|
|
||||||
SCM_INLINE_IMPLEMENTATION int
|
|
||||||
scm_peek_byte_or_eof_unlocked (SCM port)
|
|
||||||
{
|
|
||||||
SCM buf = SCM_PTAB_ENTRY (port)->read_buf;
|
|
||||||
SCM buf_bv, buf_cur, buf_end;
|
|
||||||
size_t cur;
|
|
||||||
|
|
||||||
buf_bv = SCM_SIMPLE_VECTOR_REF (buf, SCM_PORT_BUFFER_FIELD_BYTEVECTOR);
|
|
||||||
buf_cur = SCM_SIMPLE_VECTOR_REF (buf, SCM_PORT_BUFFER_FIELD_CUR);
|
|
||||||
buf_end = SCM_SIMPLE_VECTOR_REF (buf, SCM_PORT_BUFFER_FIELD_END);
|
|
||||||
cur = SCM_I_INUM (buf_cur);
|
|
||||||
if (SCM_LIKELY (SCM_I_INUMP (buf_cur))
|
|
||||||
&& SCM_LIKELY (SCM_I_INUMP (buf_end))
|
|
||||||
&& SCM_LIKELY (cur < SCM_I_INUM (buf_end))
|
|
||||||
&& SCM_LIKELY (cur < SCM_BYTEVECTOR_LENGTH (buf_bv)))
|
|
||||||
{
|
|
||||||
scm_t_uint8 ret = SCM_BYTEVECTOR_CONTENTS (buf_bv)[cur];
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
buf = scm_fill_input_unlocked (port);
|
|
||||||
buf_bv = SCM_SIMPLE_VECTOR_REF (buf, SCM_PORT_BUFFER_FIELD_BYTEVECTOR);
|
|
||||||
buf_cur = SCM_SIMPLE_VECTOR_REF (buf, SCM_PORT_BUFFER_FIELD_CUR);
|
|
||||||
buf_end = SCM_SIMPLE_VECTOR_REF (buf, SCM_PORT_BUFFER_FIELD_END);
|
|
||||||
cur = scm_to_size_t (buf_cur);
|
|
||||||
if (cur < scm_to_size_t (buf_end))
|
|
||||||
{
|
|
||||||
scm_t_uint8 ret = SCM_BYTEVECTOR_CONTENTS (buf_bv)[cur];
|
|
||||||
return ret;
|
|
||||||
}
|
|
||||||
|
|
||||||
return EOF;
|
|
||||||
}
|
|
||||||
|
|
||||||
SCM_INLINE_IMPLEMENTATION void
|
SCM_INLINE_IMPLEMENTATION void
|
||||||
scm_putc_unlocked (char c, SCM port)
|
scm_putc_unlocked (char c, SCM port)
|
||||||
{
|
{
|
||||||
|
|
|
@ -263,7 +263,7 @@ read_token (SCM port, scm_t_read_opts *opts,
|
||||||
{
|
{
|
||||||
int chr;
|
int chr;
|
||||||
|
|
||||||
chr = scm_get_byte_or_eof_unlocked (port);
|
chr = scm_get_byte_or_eof (port);
|
||||||
|
|
||||||
if (chr == EOF)
|
if (chr == EOF)
|
||||||
return 0;
|
return 0;
|
||||||
|
@ -965,9 +965,9 @@ scm_read_semicolon_comment (int chr, SCM port)
|
||||||
/* We use the get_byte here because there is no need to get the
|
/* We use the get_byte here because there is no need to get the
|
||||||
locale correct with comment input. This presumes that newline
|
locale correct with comment input. This presumes that newline
|
||||||
always represents itself no matter what the encoding is. */
|
always represents itself no matter what the encoding is. */
|
||||||
for (c = scm_get_byte_or_eof_unlocked (port);
|
for (c = scm_get_byte_or_eof (port);
|
||||||
(c != EOF) && (c != '\n');
|
(c != EOF) && (c != '\n');
|
||||||
c = scm_get_byte_or_eof_unlocked (port));
|
c = scm_get_byte_or_eof (port));
|
||||||
|
|
||||||
return SCM_UNSPECIFIED;
|
return SCM_UNSPECIFIED;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue