lookahead-u8(3scm) Scheme Programmer's Manual lookahead-u8(3scm)

lookahead-u8, peek-u8 - peek at the next byte from a port

(import (rnrs))                     ;R6RS
(import (rnrs io ports))            ;R6RS
(import (scheme base))              ;R7RS

(lookahead-u8 binary-input-port)    ;R6RS
(peek-u8)                           ;R7RS
(peek-u8 binary-input-port)         ;R7RS

This procedure is like get-u8(3scm), but it does not update binary-input-port to point past the byte.

If an end of file is pending then it is also not removed.

Returns a single value; an exact integer object or the end of file object.
R7RS
There may be several end of file objects.

; This is for R7RS. Use open-file-input-port for R6RS.
(import (scheme base) (scheme file))
(define p (open-binary-input-file "/etc/os-release"))
(peek-u8 p)  =>  80  ; P
(read-u8 p)  =>  80  ; P
(read-u8 p)  =>  82  ; R
(peek-u8 p)  =>  69  ; E
(peek-u8 p)  =>  69  ; E
(read-u8 p)  =>  69  ; E
(read-u8 p)  =>  84  ; T
(read-u8 p)  =>  84  ; T
(read-u8 p)  =>  89  ; Y

This procedure is used when parsing binary data and the parser is written to use lookahead. This is common with handwritten parsers and is generally used in modular parsers that call other parsers when they see a certain byte.

Both one-argument versions of this procedure are compatible with each other. However, some implementations of the R7RS procedure allow it to be used on any type of port, which is not guaranteed to work in all implementations.

This procedure can raise exceptions with the following condition types:
&assertion (R6RS)
The wrong number of arguments was passed or an argument was outside its domain.
R7RS
The assertions described above are errors. Implementations may signal an error, extend the procedure's domain of definition to include such arguments, or fail catastrophically.

get-u8(3scm), lookahead-char(3scm)

R6RS, R7RS

This procedure first appeared in R6RS as part of the reworked I/O system.

This page is part of the scheme-manpages project. It includes materials from the RnRS documents. More information can be found at https://weinholt.se/scheme/manpages/.

It is relatively easy to mistakenly have this procedure consume end of file objects.
2023-07-20