Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix peek Char #225

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,8 @@ end
# Read Functions
# --------------

function Base.read(stream::TranscodingStream, ::Type{UInt8})
# needed for `peek(stream, Char)` to work
function Base.peek(stream::TranscodingStream, ::Type{UInt8})::UInt8
nhz2 marked this conversation as resolved.
Show resolved Hide resolved
# eof and ready_to_read! are inlined here because ready_to_read! is very slow and eof is broken
eof = buffersize(stream.buffer1) == 0
state = stream.state
Expand All @@ -325,7 +326,14 @@ function Base.read(stream::TranscodingStream, ::Type{UInt8})
if eof && sloweof(stream)
throw(EOFError())
end
return readbyte!(stream.buffer1)
buf = stream.buffer1
return buf.data[buf.bufferpos]
end

function Base.read(stream::TranscodingStream, ::Type{UInt8})::UInt8
x = peek(stream)
consumed!(stream.buffer1, 1)
x
end

function Base.readuntil(stream::TranscodingStream, delim::UInt8; keep::Bool=false)
Expand Down
10 changes: 10 additions & 0 deletions test/codecdoubleframe.jl
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,16 @@ DoubleFrameDecoderStream(stream::IO; kwargs...) = TranscodingStream(DoubleFrameD
@test_throws ErrorException("short write") flush(stream)
end

@testset "peek" begin
stream = DoubleFrameDecoderStream(DoubleFrameEncoderStream(IOBuffer(
codeunits("こんにちは")
)))
@test peek(stream) == 0xe3
@test peek(stream, Char) == 'こ'
@test peek(stream, Int32) == -476872221
close(stream)
end

test_roundtrip_read(DoubleFrameEncoderStream, DoubleFrameDecoderStream)
test_roundtrip_write(DoubleFrameEncoderStream, DoubleFrameDecoderStream)
test_roundtrip_lines(DoubleFrameEncoderStream, DoubleFrameDecoderStream)
Expand Down
8 changes: 8 additions & 0 deletions test/codecnoop.jl
Original file line number Diff line number Diff line change
Expand Up @@ -541,4 +541,12 @@ using FillArrays: Zeros
@test take!(sink) == b"abcd"
end

@testset "peek" begin
stream = NoopStream(IOBuffer(codeunits("こんにちは")))
@test peek(stream) == 0xe3
@test peek(stream, Char) == 'こ'
@test peek(stream, Int32) == -476872221
close(stream)
end

end
Loading