Skip to content

Commit 91f66fa

Browse files
authored
Internal refactoring (#69)
1 parent 144b3cf commit 91f66fa

File tree

3 files changed

+28
-33
lines changed

3 files changed

+28
-33
lines changed

src/buffer.jl

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ mutable struct Buffer
2323
bufferpos::Int
2424
marginpos::Int
2525

26-
# the number of total bytes passed through this buffer
27-
total::Int64
26+
# the total number of transcoded bytes
27+
transcoded::Int64
2828

2929
function Buffer(size::Integer)
3030
return new(Vector{UInt8}(undef, size), 0, 1, 1, 0)
@@ -92,34 +92,27 @@ function reset!(buf::Buffer)
9292
end
9393

9494
# Notify that `n` bytes are consumed from `buf`.
95-
function consumed!(buf::Buffer, n::Integer)
95+
function consumed!(buf::Buffer, n::Integer; transcode::Bool = false)
9696
buf.bufferpos += n
97+
if transcode
98+
buf.transcoded += n
99+
end
97100
return buf
98101
end
99102

100103
# Notify that `n` bytes are supplied to `buf`.
101-
function supplied!(buf::Buffer, n::Integer)
104+
function supplied!(buf::Buffer, n::Integer; transcode::Bool = false)
102105
buf.marginpos += n
103-
return buf
104-
end
105-
106-
function consumed2!(buf::Buffer, n::Integer)
107-
buf.bufferpos += n
108-
buf.total += n
109-
return buf
110-
end
111-
112-
function supplied2!(buf::Buffer, n::Integer)
113-
buf.marginpos += n
114-
buf.total += n
106+
if transcode
107+
buf.transcoded += n
108+
end
115109
return buf
116110
end
117111

118112
# Discard buffered data and initialize positions.
119113
function initbuffer!(buf::Buffer)
120-
buf.markpos = 0
114+
buf.markpos = buf.transcoded = 0
121115
buf.bufferpos = buf.marginpos = 1
122-
buf.total = 0
123116
return buf
124117
end
125118

src/noop.jl

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,13 +130,13 @@ function stats(stream::NoopStream)
130130
if mode == :idle
131131
consumed = supplied = 0
132132
elseif mode == :read
133-
supplied = buffer.total
133+
supplied = buffer.transcoded
134134
consumed = supplied - buffersize(buffer)
135135
elseif mode == :write
136-
supplied = buffer.total + buffersize(buffer)
137-
consumed = buffer.total
136+
supplied = buffer.transcoded + buffersize(buffer)
137+
consumed = buffer.transcoded
138138
else
139-
assert(false)
139+
@assert false "unreachable"
140140
end
141141
return Stats(consumed, supplied, supplied, supplied)
142142
end
@@ -161,7 +161,7 @@ function fillbuffer(stream::NoopStream)
161161
makemargin!(buffer, 1)
162162
nfilled += readdata!(stream.stream, buffer)
163163
end
164-
buffer.total += nfilled
164+
buffer.transcoded += nfilled
165165
return nfilled
166166
end
167167

@@ -178,11 +178,11 @@ function flushbuffer(stream::NoopStream, all::Bool=false)
178178
nflushed += writedata!(stream.stream, buffer)
179179
makemargin!(buffer, 0)
180180
end
181-
buffer.total += nflushed
181+
buffer.transcoded += nflushed
182182
return nflushed
183183
end
184184

185185
function flushuntilend(stream::NoopStream)
186-
stream.state.buffer1.total += writedata!(stream.stream, stream.state.buffer1)
186+
stream.state.buffer1.transcoded += writedata!(stream.stream, stream.state.buffer1)
187187
return
188188
end

src/stream.jl

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,8 @@ struct TranscodingStream{C<:Codec,S<:IO} <: IO
2020
# mutable state of the stream
2121
state::State
2222

23-
function TranscodingStream{C,S}(codec::C, stream::S, state::State, initialized::Bool) where {C<:Codec,S<:IO}
23+
function TranscodingStream{C,S}(
24+
codec::C, stream::S, state::State, initialized::Bool) where {C<:Codec,S<:IO}
2425
if !isopen(stream)
2526
throw(ArgumentError("closed stream"))
2627
elseif state.mode != :idle
@@ -140,8 +141,9 @@ end
140141
# Check that mode is valid.
141142
macro checkmode(validmodes)
142143
mode = esc(:mode)
144+
validmodes = [arg.value for arg in validmodes.args]
143145
quote
144-
if !$(foldr((x, y) -> :($(mode) == $(QuoteNode(x)) || $(y)), eval(validmodes), init=false))
146+
if !$(foldr((x, y) -> :($(mode) === $(QuoteNode(x)) || $(y)), validmodes, init=false))
145147
throw(ArgumentError(string("invalid mode :", $(mode))))
146148
end
147149
end
@@ -554,13 +556,13 @@ function stats(stream::TranscodingStream)
554556
if mode == :idle
555557
transcoded_in = transcoded_out = in = out = 0
556558
elseif mode == :read
557-
transcoded_in = buffer2.total
558-
transcoded_out = buffer1.total
559+
transcoded_in = buffer2.transcoded
560+
transcoded_out = buffer1.transcoded
559561
in = transcoded_in + buffersize(buffer2)
560562
out = transcoded_out - buffersize(buffer1)
561563
elseif mode == :write
562-
transcoded_in = buffer1.total
563-
transcoded_out = buffer2.total
564+
transcoded_in = buffer1.transcoded
565+
transcoded_out = buffer2.transcoded
564566
in = transcoded_in + buffersize(buffer1)
565567
out = transcoded_out - buffersize(buffer2)
566568
else
@@ -648,8 +650,8 @@ function callprocess(stream::TranscodingStream, inbuf::Buffer, outbuf::Buffer)
648650
input = buffermem(inbuf)
649651
makemargin!(outbuf, minoutsize(stream.codec, input))
650652
Δin, Δout, state.code = process(stream.codec, input, marginmem(outbuf), state.error)
651-
consumed2!(inbuf, Δin)
652-
supplied2!(outbuf, Δout)
653+
consumed!(inbuf, Δin, transcode = true)
654+
supplied!(outbuf, Δout, transcode = true)
653655
if state.code == :error
654656
changemode!(stream, :panic)
655657
elseif state.code == :ok && Δin == Δout == 0

0 commit comments

Comments
 (0)