diff options
author | Andy Wingo <wingo@pobox.com> | 2016-04-22 17:12:58 +0200 |
---|---|---|
committer | Andy Wingo <wingo@pobox.com> | 2016-04-22 17:12:58 +0200 |
commit | fb577b59af2619edd78fea71ce6250a36376abdd (patch) | |
tree | ba5b3da01cfe0ee9adb1a467ab32b10b2c449c67 /libguile/ports.c | |
parent | 3e951f7dfc6260da597e7677120a5f012c943bff (diff) | |
download | guile-fb577b59af2619edd78fea71ce6250a36376abdd.tar.gz |
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.
Diffstat (limited to 'libguile/ports.c')
-rw-r--r-- | libguile/ports.c | 115 |
1 files changed, 87 insertions, 28 deletions
diff --git a/libguile/ports.c b/libguile/ports.c index 0a424e04f..b754e1bae 100644 --- a/libguile/ports.c +++ b/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); 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); i++; @@ -1364,32 +1364,91 @@ scm_dynwind_lock_port (SCM port) /* Input. */ -int -scm_get_byte_or_eof (SCM port) +static int +get_byte_or_eof (SCM port) { - scm_i_pthread_mutex_t *lock; - int ret; + SCM buf = SCM_PTAB_ENTRY (port)->read_buf; + SCM buf_bv, buf_cur, buf_end; + size_t cur; - scm_c_lock_port (port, &lock); - ret = scm_get_byte_or_eof_unlocked (port); - if (lock) - scm_i_pthread_mutex_unlock (lock); + 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); - return ret; + 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; } -int -scm_peek_byte_or_eof (SCM port) +/* Like `scm_get_byte_or_eof' but does not change PORT's `read_pos'. */ +static int +peek_byte_or_eof (SCM port) { - scm_i_pthread_mutex_t *lock; - int ret; + SCM buf = SCM_PTAB_ENTRY (port)->read_buf; + SCM buf_bv, buf_cur, buf_end; + size_t cur; - scm_c_lock_port (port, &lock); - ret = scm_peek_byte_or_eof_unlocked (port); - if (lock) - scm_i_pthread_mutex_unlock (lock); + 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; + } - 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 +scm_get_byte_or_eof (SCM port) +{ + return get_byte_or_eof (port); +} + +int +scm_peek_byte_or_eof (SCM port) +{ + return peek_byte_or_eof (port); } static size_t @@ -1648,7 +1707,7 @@ get_utf8_codepoint (SCM port, scm_t_wchar *codepoint, *len = 0; pt = SCM_PTAB_ENTRY (port); - byte = scm_get_byte_or_eof_unlocked (port); + byte = get_byte_or_eof (port); if (byte == EOF) { *codepoint = EOF; @@ -1664,7 +1723,7 @@ get_utf8_codepoint (SCM port, scm_t_wchar *codepoint, else if (buf[0] >= 0xc2 && buf[0] <= 0xdf) { /* 2-byte form. */ - byte = scm_peek_byte_or_eof_unlocked (port); + byte = peek_byte_or_eof (port); ASSERT_NOT_EOF (byte); 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) { /* 3-byte form. */ - byte = scm_peek_byte_or_eof_unlocked (port); + byte = peek_byte_or_eof (port); ASSERT_NOT_EOF (byte); 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; *len = 2; - byte = scm_peek_byte_or_eof_unlocked (port); + byte = peek_byte_or_eof (port); ASSERT_NOT_EOF (byte); 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) { /* 4-byte form. */ - byte = scm_peek_byte_or_eof_unlocked (port); + byte = peek_byte_or_eof (port); ASSERT_NOT_EOF (byte); 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; *len = 2; - byte = scm_peek_byte_or_eof_unlocked (port); + byte = peek_byte_or_eof (port); ASSERT_NOT_EOF (byte); 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; *len = 3; - byte = scm_peek_byte_or_eof_unlocked (port); + byte = peek_byte_or_eof (port); ASSERT_NOT_EOF (byte); if (SCM_UNLIKELY ((byte & 0xc0) != 0x80)) @@ -1771,7 +1830,7 @@ static int get_latin1_codepoint (SCM port, scm_t_wchar *codepoint, 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) *len = 0; @@ -1801,7 +1860,7 @@ get_iconv_codepoint (SCM port, scm_t_wchar *codepoint, char *input, *output; 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_LIKELY (input_size == 0)) |