1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-05-20 11:40:18 +02:00

Add `scm_peek_byte_or_eof'.

* libguile/inline.h (scm_get_byte_or_eof): Add `SCM_UNLIKELY' for EOF.
  (scm_peek_byte_or_eof): New function.

* libguile/r6rs-ports.c (scm_lookahead_u8): Use `scm_peek_byte_or_eof'.
This commit is contained in:
Ludovic Courtès 2011-05-07 22:41:32 +02:00
parent 040dfa6f37
commit 452c5ad912
2 changed files with 34 additions and 7 deletions

View file

@ -3,7 +3,8 @@
#ifndef SCM_INLINE_H
#define SCM_INLINE_H
/* Copyright (C) 2001, 2002, 2003, 2004, 2006, 2008, 2009, 2010 Free Software Foundation, Inc.
/* Copyright (C) 2001, 2002, 2003, 2004, 2006, 2008, 2009, 2010,
* 2011 Free Software Foundation, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
@ -98,6 +99,7 @@ SCM_API int scm_is_pair (SCM x);
SCM_API int scm_is_string (SCM x);
SCM_API int scm_get_byte_or_eof (SCM port);
SCM_API int scm_peek_byte_or_eof (SCM port);
SCM_API void scm_putc (char c, SCM port);
SCM_API void scm_puts (const char *str_data, SCM port);
@ -362,7 +364,7 @@ scm_get_byte_or_eof (SCM port)
if (pt->read_pos >= pt->read_end)
{
if (scm_fill_input (port) == EOF)
if (SCM_UNLIKELY (scm_fill_input (port) == EOF))
return EOF;
}
@ -371,6 +373,34 @@ scm_get_byte_or_eof (SCM port)
return c;
}
/* Like `scm_get_byte_or_eof' but does not change PORT's `read_pos'. */
#ifndef SCM_INLINE_C_INCLUDING_INLINE_H
SCM_C_EXTERN_INLINE
#endif
int
scm_peek_byte_or_eof (SCM port)
{
int c;
scm_t_port *pt = SCM_PTAB_ENTRY (port);
if (pt->rw_active == SCM_PORT_WRITE)
/* may be marginally faster than calling scm_flush. */
scm_ptobs[SCM_PTOBNUM (port)].flush (port);
if (pt->rw_random)
pt->rw_active = SCM_PORT_READ;
if (pt->read_pos >= pt->read_end)
{
if (SCM_UNLIKELY (scm_fill_input (port) == EOF))
return EOF;
}
c = *pt->read_pos;
return c;
}
#ifndef SCM_INLINE_C_INCLUDING_INLINE_H
SCM_C_EXTERN_INLINE
#endif

View file

@ -460,14 +460,11 @@ SCM_DEFINE (scm_lookahead_u8, "lookahead-u8", 1, 0, 0,
SCM_VALIDATE_BINARY_INPUT_PORT (1, port);
u8 = scm_get_byte_or_eof (port);
u8 = scm_peek_byte_or_eof (port);
if (u8 == EOF)
result = SCM_EOF_VAL;
else
{
scm_unget_byte (u8, port);
result = SCM_I_MAKINUM ((scm_t_uint8) u8);
}
result = SCM_I_MAKINUM ((scm_t_uint8) u8);
return result;
}