Skip to content

Commit ea231e8

Browse files
committed
rename fields
1 parent 6bb9606 commit ea231e8

File tree

2 files changed

+46
-39
lines changed

2 files changed

+46
-39
lines changed

src/state.jl

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,31 @@
11
# Transcoding State
22
# =================
33

4+
5+
# Data Layout
6+
# -----------
7+
#
8+
# Buffered data are stored in `data` and two position fields are used to keep
9+
# track of buffered data and margin.
10+
#
11+
# buffered data margin
12+
# |<----------->||<----------->|
13+
# |......XXXXXXXXXXXXXXX..............|
14+
# ^ ^ ^ ^
15+
# 1 bufferpos marginpos endof(data)
16+
417
mutable struct State
518
# current mode (:init, :read, :write, or :closed)
619
mode::Symbol
720

8-
# buffered data
21+
# storage for buffering
922
data::Vector{UInt8}
1023

1124
# the starting position of the buffered data
12-
position::Int
25+
bufferpos::Int
1326

14-
# the ending position + 1 of the buffered data
15-
fposition::Int
27+
# the starting position of the margin
28+
marginpos::Int
1629

1730
# the last return code of `process`
1831
proc::ProcCode
@@ -24,41 +37,36 @@ mutable struct State
2437
end
2538

2639
function bufferptr(state::State)
27-
return pointer(state.data, state.position)
40+
return pointer(state.data, state.bufferpos)
2841
end
2942

3043
function buffersize(state::State)
31-
return state.fposition - state.position
32-
end
33-
34-
function isemptybuf(state::State)
35-
return buffersize(state) == 0
44+
return state.marginpos - state.bufferpos
3645
end
3746

3847
function marginptr(state::State)
39-
return pointer(state.data, state.fposition)
48+
return pointer(state.data, state.marginpos)
4049
end
4150

4251
function marginsize(state::State)
43-
return endof(state.data) - state.fposition + 1
44-
end
45-
46-
function isfullbuf(state::State)
47-
return marginsize(state) == 0
52+
return endof(state.data) - state.marginpos + 1
4853
end
4954

5055
function makemargin!(state::State, minsize::Int)::Int
5156
if buffersize(state) == 0
52-
state.position = state.fposition = 1
57+
# reset positions
58+
state.bufferpos = state.marginpos = 1
5359
end
54-
if marginsize(state) < minsize && state.position > 0
60+
if marginsize(state) < minsize && state.bufferpos > 0
61+
# shift buffered data to left
5562
bufsize = buffersize(state)
56-
copy!(state.data, 1, state.data, state.position, bufsize)
57-
state.position = 1
58-
state.fposition = 1 + bufsize
63+
copy!(state.data, 1, state.data, state.bufferpos, bufsize)
64+
state.bufferpos = 1
65+
state.marginpos = 1 + bufsize
5966
end
6067
if marginsize(state) < minsize
61-
resize!(state.data, state.fposition + minsize - 1)
68+
# expand data buffer
69+
resize!(state.data, state.marginpos + minsize - 1)
6270
end
6371
@assert marginsize(state) minsize
6472
return marginsize(state)

src/stream.jl

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ function Base.eof(stream::TranscodingStream)
3636
if state.mode == :init
3737
return eof(stream.stream)
3838
elseif state.mode == :read
39-
return isemptybuf(state) && fillbuffer(stream) == 0 && eof(stream.stream)
39+
return buffersize(state) == 0 && fillbuffer(stream) == 0 && eof(stream.stream)
4040
elseif state.mode == :write
4141
return eof(stream.stream)
4242
elseif state.mode == :closed
@@ -82,9 +82,8 @@ function Base.read(stream::TranscodingStream, ::Type{UInt8})
8282
throw(EOFError())
8383
end
8484
state = stream.state
85-
#@assert state.position < state.fposition
86-
b = state.data[state.position]
87-
state.position += 1
85+
b = state.data[state.bufferpos]
86+
state.bufferpos += 1
8887
return b
8988
end
9089

@@ -94,10 +93,10 @@ function Base.unsafe_read(stream::TranscodingStream, output::Ptr{UInt8}, nbytes:
9493
p = output
9594
p_end = output + nbytes
9695
while p < p_end && !eof(stream)
97-
m = min(state.fposition - state.position, nbytes)
96+
m = min(buffersize(state), nbytes)
9897
unsafe_copy!(p, bufferptr(state), m)
9998
p += m
100-
state.position += m
99+
state.bufferpos += m
101100
end
102101
if p < p_end && eof(stream)
103102
throw(EOFError())
@@ -107,7 +106,7 @@ end
107106

108107
function Base.nb_available(stream::TranscodingStream)
109108
prepare(stream, :read)
110-
return stream.state.fposition - stream.state.position
109+
return buffersize(stream.state)
111110
end
112111

113112

@@ -117,11 +116,11 @@ end
117116
function Base.write(stream::TranscodingStream, b::UInt8)
118117
prepare(stream, :write)
119118
state = stream.state
120-
if isfullbuf(state) && flushbuffer(stream) == 0
119+
if marginsize(state) == 0 && flushbuffer(stream) == 0
121120
return 0
122121
end
123-
state.data[state.position] = b
124-
state.position += 1
122+
state.data[state.marginpos] = b
123+
state.marginpos += 1
125124
return 1
126125
end
127126

@@ -131,13 +130,13 @@ function Base.unsafe_write(stream::TranscodingStream, input::Ptr{UInt8}, nbytes:
131130
p = input
132131
p_end = p + nbytes
133132
while p < p_end
134-
if isfullbuf(state) && flushbuffer(stream) == 0
133+
if marginsize(state) == 0 && flushbuffer(stream) == 0
135134
break
136135
end
137-
m = min(endof(state.data) - (state.fposition - 1), p_end - p)
136+
m = min(marginsize(state), p_end - p)
138137
unsafe_copy!(marginptr(state), p, m)
139138
p += m
140-
state.fposition += m
139+
state.marginpos += m
141140
end
142141
return Int(p - input)
143142
end
@@ -153,9 +152,9 @@ function fillbuffer(stream::TranscodingStream)::Int
153152
end
154153
nfilled = 0
155154
makemargin!(state, 1)
156-
while !isfullbuf(state) && state.proc != PROC_FINISH
155+
while marginsize(state) > 0 && state.proc != PROC_FINISH
157156
n, code = process(Read, stream.codec, stream.stream, marginptr(state), marginsize(state))
158-
state.fposition += n
157+
state.marginpos += n
159158
state.proc = code
160159
nfilled += n
161160
end
@@ -168,9 +167,9 @@ function flushbuffer(stream::TranscodingStream)::Int
168167
return 0
169168
end
170169
nflushed = 0
171-
while !isemptybuf(state)
170+
while buffersize(state) > 0
172171
n, code = process(Write, stream.codec, stream.stream, bufferptr(state), buffersize(state))
173-
state.position += n
172+
state.bufferpos += n
174173
state.proc = code
175174
nflushed += n
176175
end

0 commit comments

Comments
 (0)