Skip to content

Commit 144b3cf

Browse files
authored
implement Base.position (#68)
1 parent 1dfb063 commit 144b3cf

File tree

5 files changed

+72
-9
lines changed

5 files changed

+72
-9
lines changed

docs/make.jl

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,14 @@ using Documenter
22
using TranscodingStreams
33

44
makedocs(
5-
format=:html,
65
sitename="TranscodingStreams.jl",
76
modules=[TranscodingStreams],
87
pages=["index.md", "examples.md", "reference.md", "devnotes.md"],
9-
assets=["assets/custom.css"])
8+
assets=["assets/custom.css"],
9+
)
1010

1111
deploydocs(
1212
repo="github.com/bicycle1885/TranscodingStreams.jl.git",
13-
julia="0.7",
1413
target="build",
1514
deps=nothing,
1615
make=nothing)

docs/src/reference.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ TranscodingStreams.TOKEN_END
1616
TranscodingStreams.unsafe_read
1717
TranscodingStreams.unread
1818
TranscodingStreams.unsafe_unread
19+
Base.position(stream::TranscodingStream)
1920
```
2021

2122
Statistics

src/buffer.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,11 @@
77
# Buffered data are stored in `data` and three position fields are used to keep
88
# track of marked data, buffered data and margin.
99
#
10-
# marked buffer margin
11-
# |<-------->||<-------->||<-------->|
12-
# |....xxxxxxxxxxxxXXXXXXXXXXXX...........|
13-
# ^ ^ ^ ^ ^
14-
# 1 markpos bufferpos marginpos lastindex(data)
10+
# marked buffer margin
11+
# |<-------->||<-------->||<-------->|
12+
# data ....xxxxxxxxxxxxXXXXXXXXXXXX............
13+
# ^ ^ ^ ^ ^
14+
# position 1 markpos bufferpos marginpos lastindex(data)
1515
#
1616
# `markpos` is positive iff there are marked data; otherwise it is set to zero.
1717
# `markpos` ≤ `bufferpos` ≤ `marginpos` must hold whenever possible.

src/stream.jl

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,53 @@ function Base.skip(stream::TranscodingStream, offset::Integer)
241241
return
242242
end
243243

244+
"""
245+
position(stream::TranscodingStream)
246+
247+
Return the number of bytes read from or written to `stream`.
248+
249+
Note that the returned value will be different from that of the underlying
250+
stream wrapped by `stream`. This is because `stream` buffers some data and the
251+
codec may change the length of data.
252+
253+
Examples
254+
--------
255+
256+
```
257+
julia> using CodecZlib, TranscodingStreams
258+
259+
julia> file = open(joinpath(dirname(pathof(CodecZlib)), "..", "test", "abra.gz"));
260+
261+
julia> stream = GzipDecompressorStream(file)
262+
TranscodingStream{GzipDecompressor,IOStream}(<mode=idle>)
263+
264+
julia> position(stream)
265+
0
266+
267+
julia> read(stream, 4)
268+
4-element Array{UInt8,1}:
269+
0x61
270+
0x62
271+
0x72
272+
0x61
273+
274+
julia> position(stream)
275+
4
276+
```
277+
"""
278+
function Base.position(stream::TranscodingStream)
279+
mode = stream.state.mode
280+
@checkmode (:idle, :read, :write)
281+
if mode === :idle
282+
return Int64(0)
283+
elseif mode === :read
284+
return stats(stream).out
285+
elseif mode === :write
286+
return stats(stream).in
287+
end
288+
@assert false "unreachable"
289+
end
290+
244291

245292
# Seek Operations
246293
# ---------------
@@ -517,7 +564,7 @@ function stats(stream::TranscodingStream)
517564
in = transcoded_in + buffersize(buffer1)
518565
out = transcoded_out - buffersize(buffer2)
519566
else
520-
assert(false)
567+
@assert false "unreachable"
521568
end
522569
return Stats(in, out, transcoded_in, transcoded_out)
523570
end

test/codecquadruple.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,22 @@ end
5050
@test read(stream) == b"ffffoooooooo"
5151
close(stream)
5252

53+
stream = TranscodingStream(QuadrupleCodec(), IOBuffer("foo"))
54+
@test position(stream) === 0
55+
read(stream, 3)
56+
@test position(stream) === 3
57+
read(stream, UInt8)
58+
@test position(stream) === 4
59+
close(stream)
60+
61+
stream = TranscodingStream(QuadrupleCodec(), IOBuffer())
62+
@test position(stream) === 0
63+
write(stream, 0x00)
64+
@test position(stream) === 1
65+
write(stream, "foo")
66+
@test position(stream) === 4
67+
close(stream)
68+
5369
# Buffers are shared.
5470
stream1 = TranscodingStream(QuadrupleCodec(), IOBuffer("foo"))
5571
stream2 = TranscodingStream(QuadrupleCodec(), stream1)

0 commit comments

Comments
 (0)