Skip to content

Commit 0875466

Browse files
authored
Add missing ready_to_read! to unread (#228)
1 parent 2e7f5fc commit 0875466

File tree

4 files changed

+42
-1
lines changed

4 files changed

+42
-1
lines changed

src/buffer.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ end
203203

204204
# Insert data to the current buffer.
205205
# `data` must not alias `buf`
206-
function insertdata!(buf::Buffer, data::Union{AbstractArray{UInt8}, Memory})
206+
function insertdata!(buf::Buffer, data::Union{AbstractVector{UInt8}, Memory})
207207
nbytes = Int(length(data))
208208
makemargin!(buf, nbytes)
209209
datapos = if iszero(buf.markpos)

src/stream.jl

+1
Original file line numberDiff line numberDiff line change
@@ -462,6 +462,7 @@ inserted.
462462
`data` must not alias any internal buffers in `stream`
463463
"""
464464
function unread(stream::TranscodingStream, data::AbstractVector{UInt8})
465+
ready_to_read!(stream)
465466
insertdata!(stream.buffer1, data)
466467
return nothing
467468
end

test/codecdoubleframe.jl

+16
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,22 @@ DoubleFrameDecoderStream(stream::IO; kwargs...) = TranscodingStream(DoubleFrameD
396396
close(stream)
397397
end
398398

399+
@testset "unread" begin
400+
stream = DoubleFrameDecoderStream(IOBuffer("[ ffoooobbaarr ]"))
401+
@test position(stream) == 0
402+
@test read(stream, 3) == b"foo"
403+
@test position(stream) == 3
404+
@test read(stream, 3) == b"bar"
405+
@test position(stream) == 6
406+
@test TranscodingStreams.unread(stream, b"baz") === nothing
407+
@test position(stream) == 3
408+
@test read(stream, 3) == b"baz"
409+
@test position(stream) == 6
410+
@test eof(stream)
411+
@test position(stream) == 6
412+
close(stream)
413+
end
414+
399415
test_roundtrip_read(DoubleFrameEncoderStream, DoubleFrameDecoderStream)
400416
test_roundtrip_write(DoubleFrameEncoderStream, DoubleFrameDecoderStream)
401417
test_roundtrip_lines(DoubleFrameEncoderStream, DoubleFrameDecoderStream)

test/codecnoop.jl

+24
Original file line numberDiff line numberDiff line change
@@ -325,13 +325,20 @@ using FillArrays: Zeros
325325
@testset "unread" begin
326326
stream = NoopStream(IOBuffer(""))
327327
@test TranscodingStreams.unread(stream, b"foo") === nothing
328+
@test position(stream) == -3
328329
@test read(stream, 3) == b"foo"
330+
@test position(stream) == 0
331+
@test eof(stream)
329332
close(stream)
330333

331334
stream = NoopStream(IOBuffer("foo"))
332335
@test read(stream, 3) == b"foo"
336+
@test position(stream) == 3
333337
@test TranscodingStreams.unread(stream, b"bar") === nothing
338+
@test position(stream) == 0
334339
@test read(stream, 3) == b"bar"
340+
@test position(stream) == 3
341+
@test eof(stream)
335342
close(stream)
336343

337344
stream = NoopStream(IOBuffer("foobar"))
@@ -351,24 +358,36 @@ using FillArrays: Zeros
351358
close(stream)
352359

353360
stream = NoopStream(IOBuffer("foobar"))
361+
@test position(stream) == 0
354362
@test read(stream, 3) == b"foo"
363+
@test position(stream) == 3
355364
@test read(stream, 3) == b"bar"
365+
@test position(stream) == 6
356366
@test TranscodingStreams.unread(stream, b"baz") === nothing
367+
@test position(stream) == 3
357368
@test read(stream, 3) == b"baz"
369+
@test position(stream) == 6
358370
@test eof(stream)
371+
@test position(stream) == 6
359372
close(stream)
360373

361374
for bufsize in (1, 2, 3, 4, 100)
362375
for n in (1, 100)
363376
stream = NoopStream(IOBuffer("foo"^n*"bar"^n); bufsize)
364377
@test mark(stream) == 0
378+
@test position(stream) == 0
365379
@test read(stream, 3n) == codeunits("foo"^n)
366380
@test read(stream, 3n) == codeunits("bar"^n)
381+
@test position(stream) == 6n
367382
TranscodingStreams.unread(stream, codeunits("baz"^n))
383+
@test position(stream) == 3n
368384
@test reset(stream) == 0
385+
@test position(stream) == 0
369386
@test read(stream, 3n) == codeunits("foo"^n)
370387
@test read(stream, 3n) == codeunits("baz"^n)
388+
@test position(stream) == 6n
371389
@test eof(stream)
390+
@test position(stream) == 6n
372391
close(stream)
373392
end
374393
end
@@ -414,6 +433,11 @@ using FillArrays: Zeros
414433
end
415434
@test read(stream, 3) == b"bar"
416435
close(stream)
436+
437+
stream = NoopStream(IOBuffer())
438+
write(stream, b"foo")
439+
@test_throws ArgumentError TranscodingStreams.unread(stream, b"bar")
440+
close(stream)
417441
end
418442

419443
stream = NoopStream(IOBuffer(""))

0 commit comments

Comments
 (0)