Skip to content

Commit d4d9303

Browse files
authored
Document and export Base.peek (#28811)
1 parent 52c9e3d commit d4d9303

File tree

22 files changed

+85
-27
lines changed

22 files changed

+85
-27
lines changed

NEWS.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ New library features
137137
* The introspection macros (`@which`, `@code_typed`, etc.) now work with `do`-block syntax ([#35283]) and with dot syntax ([#35522]).
138138
* `count` now accepts the `dims` keyword.
139139
* new in-place `count!` function similar to `sum!`.
140+
* `peek` is now exported and accepts a type to peek from a stream ([#28811]).
140141

141142
Standard library changes
142143
------------------------

base/essentials.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -785,6 +785,31 @@ ismissing(::Any) = false
785785
ismissing(::Missing) = true
786786

787787
function popfirst! end
788+
789+
"""
790+
peek(stream[, T=UInt8])
791+
792+
Read and return a value of type `T` from a stream without advancing the current position
793+
in the stream.
794+
795+
# Examples
796+
797+
```jldoctest
798+
julia> b = IOBuffer("julia");
799+
800+
julia> peek(b)
801+
0x6a
802+
803+
julia> position(b)
804+
0
805+
806+
julia> peek(b, Char)
807+
'j': ASCII/Unicode U+006A (category Ll: Letter, lowercase)
808+
```
809+
810+
!!! compat "Julia 1.5"
811+
The method which accepts a type requires Julia 1.5 or later.
812+
"""
788813
function peek end
789814

790815
"""

base/exports.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -795,6 +795,7 @@ export
795795
bytesavailable,
796796
ntoh,
797797
open,
798+
peek,
798799
pipeline,
799800
Pipe,
800801
PipeBuffer,

base/io.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,14 +251,16 @@ function unsafe_read(s::IO, p::Ptr{UInt8}, n::UInt)
251251
nothing
252252
end
253253

254-
function peek(s::IO)
254+
function peek(s::IO, ::Type{T}) where T
255255
mark(s)
256-
try read(s, UInt8)
256+
try read(s, T)
257257
finally
258258
reset(s)
259259
end
260260
end
261261

262+
peek(s) = peek(s, UInt8)
263+
262264
# Generic `open` methods
263265

264266
"""
@@ -348,11 +350,12 @@ readbytes!(io::AbstractPipe, target::AbstractVector{UInt8}, n=length(target)) =
348350

349351
for f in (
350352
# peek/mark interface
351-
:peek, :mark, :unmark, :reset, :ismarked,
353+
:mark, :unmark, :reset, :ismarked,
352354
# Simple reader functions
353355
:readavailable, :isreadable)
354356
@eval $(f)(io::AbstractPipe) = $(f)(pipe_reader(io))
355357
end
358+
peek(io::AbstractPipe, ::Type{T}) where {T} = peek(pipe_reader(io), T)
356359

357360
iswritable(io::AbstractPipe) = iswritable(pipe_writer(io))
358361
isopen(io::AbstractPipe) = isopen(pipe_writer(io)) || isopen(pipe_reader(io))

base/iobuffer.jl

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ function unsafe_read(from::GenericIOBuffer, p::Ptr{UInt8}, nb::UInt)
172172
nothing
173173
end
174174

175-
function read(from::GenericIOBuffer, T::Union{Type{Int16},Type{UInt16},Type{Int32},Type{UInt32},Type{Int64},Type{UInt64},Type{Int128},Type{UInt128},Type{Float16},Type{Float32},Type{Float64}})
175+
function peek(from::GenericIOBuffer, T::Union{Type{Int16},Type{UInt16},Type{Int32},Type{UInt32},Type{Int64},Type{UInt64},Type{Int128},Type{UInt128},Type{Float16},Type{Float32},Type{Float64}})
176176
from.readable || _throw_not_readable()
177177
avail = bytesavailable(from)
178178
nb = sizeof(T)
@@ -183,7 +183,12 @@ function read(from::GenericIOBuffer, T::Union{Type{Int16},Type{UInt16},Type{Int3
183183
ptr::Ptr{T} = pointer(from.data, from.ptr)
184184
x = unsafe_load(ptr)
185185
end
186-
from.ptr += nb
186+
return x
187+
end
188+
189+
function read(from::GenericIOBuffer, T::Union{Type{Int16},Type{UInt16},Type{Int32},Type{UInt32},Type{Int64},Type{UInt64},Type{Int128},Type{UInt128},Type{Float16},Type{Float32},Type{Float64}})
190+
x = peek(from, T)
191+
from.ptr += sizeof(T)
187192
return x
188193
end
189194

@@ -216,7 +221,7 @@ end
216221
return byte
217222
end
218223

219-
function peek(from::GenericIOBuffer)
224+
function peek(from::GenericIOBuffer, ::Type{UInt8})
220225
from.readable || _throw_not_readable()
221226
if from.ptr > from.size
222227
throw(EOFError())

base/iostream.jl

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -525,6 +525,10 @@ end
525525

526526
## peek ##
527527

528-
function peek(s::IOStream)
529-
@_lock_ios s ccall(:ios_peekc, Cint, (Ptr{Cvoid},), s)
528+
function peek(s::IOStream, ::Type{UInt8})
529+
b = @_lock_ios s ccall(:ios_peekc, Cint, (Ptr{Cvoid},), s.ios)
530+
if b == -1
531+
throw(EOFError())
532+
end
533+
return b % UInt8
530534
end

base/iterators.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1205,7 +1205,7 @@ julia> a = Iterators.Stateful([1,1,1,2,3,4]);
12051205
12061206
julia> for x in a; x == 1 || break; end
12071207
1208-
julia> Base.peek(a)
1208+
julia> peek(a)
12091209
3
12101210
12111211
julia> sum(a) # Sum the remaining elements

base/secretbuffer.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ bytesavailable(io::SecretBuffer) = io.size - io.ptr + 1
154154
position(io::SecretBuffer) = io.ptr-1
155155
eof(io::SecretBuffer) = io.ptr > io.size
156156
isempty(io::SecretBuffer) = io.size == 0
157-
function peek(io::SecretBuffer)
157+
function peek(io::SecretBuffer, ::Type{UInt8})
158158
eof(io) && throw(EOFError())
159159
return io.data[io.ptr]
160160
end

base/stream.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1193,9 +1193,9 @@ unmark(x::LibuvStream) = unmark(x.buffer)
11931193
reset(x::LibuvStream) = reset(x.buffer)
11941194
ismarked(x::LibuvStream) = ismarked(x.buffer)
11951195

1196-
function peek(s::LibuvStream)
1196+
function peek(s::LibuvStream, ::Type{T}) where T
11971197
mark(s)
1198-
try read(s, UInt8)
1198+
try read(s, T)
11991199
finally
12001200
reset(s)
12011201
end

doc/src/base/io-network.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ Base.read!
1919
Base.readbytes!
2020
Base.unsafe_read
2121
Base.unsafe_write
22+
Base.peek
2223
Base.position
2324
Base.seek
2425
Base.seekstart

0 commit comments

Comments
 (0)