Qu

peek_char

Builtin index · File I/O

result = peek_char(f)

Input: f · Output: result

What it is

Looks at the next character of f (file handle, opened readable) without consuming it.

Overloads

One call shape — this function is not overloaded.

CodeInputOutputDescription
result = peek_char(f)
f stringnot documented yet
result string
Returns exactly what read_char(f) would return (a one-character string, or Nothing at EOF), cursor restored.

Examples

From the documentation, and run by the build -- the output and the figure below are what the engine actually produced. Simplest first.

f = fopen("greeting.txt", "w")
write_char(f, "Hello,World")
close(f)

g = fopen("greeting.txt", "r")
first       = read_char(g)         # "H"

peeked      = peek_char(g)         # "e" -- cursor not moved

next        = read_char(g)         # "e" again, same char peeked returned

chunk       = read_chars(g, 3)     # "llo"

next_byte   = peek_byte(g)         # 44, the byte for ',' -- still not consumed

before_comma = read_until(g, ",")  # "" -- the comma was right there, now consumed

w_byte      = read_byte(g)         # 87, the byte for 'W'

rest        = read_all(g)          # "orld"

close(g)
print("{first}{next}{chunk} peeked={peeked} next_byte={next_byte} before_comma=\"{before_comma}\" w_byte={w_byte} rest={rest}")
# Hello peeked=e next_byte=44 before_comma="" w_byte=87 rest=orld

output
Hello peeked=e next_byte=44 before_comma="" w_byte=87 rest=orld

from file-io

How it works

peek_line/peek_char/peek_byte (§ file I/O extensions, wrappers around read_line/read_char/read_byte; see fs_ops::call's own doc comment for the full design (why three distinct names rather than one generic peek(), and why this is implemented via a real seek round-trip rather than a buffered "un-read").

From the note above this builtin in the interpreter, where the reasoning was written down as it was built.