Skip to content

Commit e7fe7be

Browse files
committed
cache reads from io
1 parent 436991a commit e7fe7be

File tree

3 files changed

+29
-25
lines changed

3 files changed

+29
-25
lines changed

src/CBOR.jl

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,11 @@ export decode, decode_with_iana
4545
export Simple, Null, Undefined
4646

4747
function decode(data::Array{UInt8, 1})
48-
return decode(IOBuffer(data))
48+
return decode_internal(IOBuffer(data))
49+
end
50+
51+
function decode(data::IO)
52+
return decode(read(data))
4953
end
5054

5155
function encode(data)

src/decode.jl

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -71,9 +71,9 @@ end
7171

7272

7373

74-
decode(io::IO, ::Val{TYPE_0}) = decode_unsigned(io)
74+
decode_internal(io::IO, ::Val{TYPE_0}) = decode_unsigned(io)
7575

76-
function decode(io::IO, ::Val{TYPE_1})
76+
function decode_internal(io::IO, ::Val{TYPE_1})
7777
data = signed(decode_unsigned(io))
7878
if (i = Int128(data) + one(data)) > typemax(Int64)
7979
return -i
@@ -85,12 +85,12 @@ end
8585
"""
8686
Decode Byte Array
8787
"""
88-
function decode(io::IO, ::Val{TYPE_2})
88+
function decode_internal(io::IO, ::Val{TYPE_2})
8989
if (peekbyte(io) & ADDNTL_INFO_MASK) == ADDNTL_INFO_INDEF
9090
skip(io, 1)
9191
result = IOBuffer()
9292
while peekbyte(io) !== BREAK_INDEF
93-
write(result, decode(io))
93+
write(result, decode_internal(io))
9494
end
9595
return take!(result)
9696
else
@@ -101,12 +101,12 @@ end
101101
"""
102102
Decode String
103103
"""
104-
function decode(io::IO, ::Val{TYPE_3})
104+
function decode_internal(io::IO, ::Val{TYPE_3})
105105
if (peekbyte(io) & ADDNTL_INFO_MASK) == ADDNTL_INFO_INDEF
106106
skip(io, 1)
107107
result = IOBuffer()
108108
while peekbyte(io) !== BREAK_INDEF
109-
write(result, decode(io))
109+
write(result, decode_internal(io))
110110
end
111111
return String(take!(result))
112112
else
@@ -117,25 +117,25 @@ end
117117
"""
118118
Decode Vector of arbitrary elements
119119
"""
120-
function decode(io::IO, ::Val{TYPE_4})
121-
return map(identity, decode_ntimes(decode, io))
120+
function decode_internal(io::IO, ::Val{TYPE_4})
121+
return map(identity, decode_ntimes(decode_internal, io))
122122
end
123123

124124
"""
125125
Decode Dict
126126
"""
127-
function decode(io::IO, ::Val{TYPE_5})
127+
function decode_internal(io::IO, ::Val{TYPE_5})
128128
return Dict(decode_ntimes(io) do io
129-
decode(io) => decode(io)
129+
decode_internal(io) => decode_internal(io)
130130
end)
131131
end
132132

133133
"""
134134
Decode Tagged type
135135
"""
136-
function decode(io::IO, ::Val{TYPE_6})
136+
function decode_internal(io::IO, ::Val{TYPE_6})
137137
tag = decode_unsigned(io)
138-
data = decode(io)
138+
data = decode_internal(io)
139139
if tag in (POS_BIG_INT_TAG, NEG_BIG_INT_TAG)
140140
big_int = parse(
141141
BigInt, bytes2hex(data), base = HEX_BASE
@@ -157,7 +157,7 @@ function decode(io::IO, ::Val{TYPE_6})
157157
return Tag(tag, data)
158158
end
159159

160-
function decode(io::IO, ::Val{TYPE_7})
160+
function decode_internal(io::IO, ::Val{TYPE_7})
161161
first_byte = read(io, UInt8)
162162
addntl_info = first_byte & ADDNTL_INFO_MASK
163163
if addntl_info < SINGLE_BYTE_SIMPLE_PLUS_ONE + 1
@@ -190,16 +190,16 @@ function decode(io::IO, ::Val{TYPE_7})
190190
end
191191
end
192192

193-
function decode(io::IO)
193+
function decode_internal(io::IO)
194194
# leave startbyte in io
195195
first_byte = peekbyte(io)
196196
typ = first_byte & TYPE_BITS_MASK
197-
typ == TYPE_0 && return decode(io, Val(TYPE_0))
198-
typ == TYPE_1 && return decode(io, Val(TYPE_1))
199-
typ == TYPE_2 && return decode(io, Val(TYPE_2))
200-
typ == TYPE_3 && return decode(io, Val(TYPE_3))
201-
typ == TYPE_4 && return decode(io, Val(TYPE_4))
202-
typ == TYPE_5 && return decode(io, Val(TYPE_5))
203-
typ == TYPE_6 && return decode(io, Val(TYPE_6))
204-
typ == TYPE_7 && return decode(io, Val(TYPE_7))
197+
typ == TYPE_0 && return decode_internal(io, Val(TYPE_0))
198+
typ == TYPE_1 && return decode_internal(io, Val(TYPE_1))
199+
typ == TYPE_2 && return decode_internal(io, Val(TYPE_2))
200+
typ == TYPE_3 && return decode_internal(io, Val(TYPE_3))
201+
typ == TYPE_4 && return decode_internal(io, Val(TYPE_4))
202+
typ == TYPE_5 && return decode_internal(io, Val(TYPE_5))
203+
typ == TYPE_6 && return decode_internal(io, Val(TYPE_6))
204+
typ == TYPE_7 && return decode_internal(io, Val(TYPE_7))
205205
end

test/runtests.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ function CBOR.encode(io::IO, x::OrderedDict)
111111
encode(io, value)
112112
end
113113
end
114-
function CBOR.decode(io::IO, ::Val{CBOR.TYPE_5})
114+
function CBOR.decode_internal(io::IO, ::Val{CBOR.TYPE_5})
115115
return OrderedDict(CBOR.decode_ntimes(io) do io
116-
decode(io) => decode(io)
116+
CBOR.decode_internal(io) => CBOR.decode_internal(io)
117117
end...)
118118
end
119119

0 commit comments

Comments
 (0)