Skip to content

Commit 7fce824

Browse files
authored
add Base.position to NoopStream (#27)
1 parent c04cfa2 commit 7fce824

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/noop.jl

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,19 @@ function TranscodingStream(codec::Noop, stream::IO;
4141
return TranscodingStream(codec, stream, State(buffer, buffer))
4242
end
4343

44+
"""
45+
position(stream::NoopStream)
46+
47+
Get the current poition of `stream`.
48+
49+
Note that this method may return a wrong position when
50+
- some data have been inserted by `TranscodingStreams.unread`, or
51+
- the position of the wrapped stream has been changed outside of this package.
52+
"""
53+
function Base.position(stream::NoopStream)
54+
return position(stream.stream) - buffersize(stream.state.buffer1)
55+
end
56+
4457
function Base.seek(stream::NoopStream, pos::Integer)
4558
seek(stream.stream, pos)
4659
initbuffer!(stream.state.buffer1)

test/runtests.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,13 @@ end
8080
@test take!(sink) == data
8181
close(stream)
8282

83+
stream = TranscodingStream(Noop(), IOBuffer(b"foobarbaz"))
84+
@test position(stream) === 0
85+
read(stream, UInt8)
86+
@test position(stream) === 1
87+
read(stream)
88+
@test position(stream) === 9
89+
8390
data = collect(0x00:0x0f)
8491
stream = TranscodingStream(Noop(), IOBuffer(data))
8592
@test !ismarked(stream)

0 commit comments

Comments
 (0)