@@ -106,135 +106,140 @@ function decode_next(start_idx, bytes::Array{UInt8, 1}, with_iana::Bool)
106
106
first_byte = bytes[start_idx]
107
107
typ = first_byte & TYPE_BITS_MASK
108
108
109
- if typ == TYPE_0
110
- data, bytes_consumed = decode_unsigned (start_idx, bytes)
109
+ if typ == TYPE_0
110
+ data, bytes_consumed = decode_unsigned (start_idx, bytes)
111
+
112
+ elseif typ == TYPE_1
113
+ data, bytes_consumed = decode_unsigned (start_idx, bytes)
114
+ if (i = Int128 (data) + 1 ) > typemax (Int64)
115
+ data = - i
116
+ else
117
+ data = - (Signed (data) + 1 )
118
+ end
111
119
112
- elseif typ == TYPE_1
113
- data, bytes_consumed = decode_unsigned (start_idx, bytes)
114
- if (i = Int128 (data) + 1 ) > typemax (Int64)
115
- data = - i
116
- else
117
- data = - (Signed (data) + 1 )
118
- end
120
+ elseif typ == TYPE_6
121
+ tag, bytes_consumed = decode_unsigned (start_idx, bytes)
119
122
120
- elseif typ == TYPE_6
121
- tag, bytes_consumed = decode_unsigned (start_idx, bytes)
123
+ function retrieve_plain_pair ()
124
+ tagged_data, data_bytes =
125
+ decode_next (start_idx + bytes_consumed, bytes,
126
+ with_iana)
127
+ bytes_consumed += data_bytes
128
+
129
+ return Pair (tag, tagged_data)
130
+ end
122
131
123
- function retrieve_plain_pair ()
124
- tagged_data, data_bytes =
132
+ if with_iana
133
+ if tag == POS_BIG_INT_TAG || tag == NEG_BIG_INT_TAG
134
+ big_int_bytes, sub_bytes_consumed =
125
135
decode_next (start_idx + bytes_consumed, bytes,
126
136
with_iana)
127
- bytes_consumed += data_bytes
128
-
129
- return Pair (tag, tagged_data)
130
- end
137
+ bytes_consumed += sub_bytes_consumed
131
138
132
- if with_iana
133
- if tag == POS_BIG_INT_TAG || tag == NEG_BIG_INT_TAG
134
- big_int_bytes, sub_bytes_consumed =
135
- decode_next (start_idx + bytes_consumed, bytes,
136
- with_iana)
137
- bytes_consumed += sub_bytes_consumed
138
-
139
- big_int = parse (BigInt, bytes2hex (big_int_bytes),
140
- HEX_BASE)
141
- if tag == NEG_BIG_INT_TAG
142
- big_int = - (big_int + 1 )
143
- end
144
-
145
- data = big_int
146
- else
147
- data = retrieve_plain_pair ()
139
+ big_int = parse (
140
+ BigInt, bytes2hex (big_int_bytes), base = HEX_BASE
141
+ )
142
+ if tag == NEG_BIG_INT_TAG
143
+ big_int = - (big_int + 1 )
148
144
end
145
+
146
+ data = big_int
149
147
else
150
148
data = retrieve_plain_pair ()
151
149
end
150
+ else
151
+ data = retrieve_plain_pair ()
152
+ end
152
153
153
- elseif typ == TYPE_7
154
- addntl_info = first_byte & ADDNTL_INFO_MASK
155
- bytes_consumed = 1
154
+ elseif typ == TYPE_7
155
+ addntl_info = first_byte & ADDNTL_INFO_MASK
156
+ bytes_consumed = 1
156
157
157
- if addntl_info < SINGLE_BYTE_SIMPLE_PLUS_ONE + 1
158
+ if addntl_info < SINGLE_BYTE_SIMPLE_PLUS_ONE + 1
159
+ bytes_consumed += 1
160
+ if addntl_info < SINGLE_BYTE_SIMPLE_PLUS_ONE
161
+ simple_val = addntl_info
162
+ else
158
163
bytes_consumed += 1
159
- if addntl_info < SINGLE_BYTE_SIMPLE_PLUS_ONE
160
- simple_val = addntl_info
161
- else
162
- bytes_consumed += 1
163
- simple_val = bytes[start_idx + 1 ]
164
- end
164
+ simple_val = bytes[start_idx + 1 ]
165
+ end
165
166
166
- if simple_val == SIMPLE_FALSE
167
- data = false
168
- elseif simple_val == SIMPLE_TRUE
169
- data = true
170
- elseif simple_val == SIMPLE_NULL
171
- data = Null ()
172
- elseif simple_val == SIMPLE_UNDEF
173
- data = Undefined ()
174
- else
175
- data = Simple (simple_val)
176
- end
167
+ if simple_val == SIMPLE_FALSE
168
+ data = false
169
+ elseif simple_val == SIMPLE_TRUE
170
+ data = true
171
+ elseif simple_val == SIMPLE_NULL
172
+ data = Null ()
173
+ elseif simple_val == SIMPLE_UNDEF
174
+ data = Undefined ()
177
175
else
178
- if addntl_info == ADDNTL_INFO_FLOAT64
179
- float_byte_len = SIZE_OF_FLOAT64
180
- elseif addntl_info == ADDNTL_INFO_FLOAT32
181
- float_byte_len = SIZE_OF_FLOAT32
182
- elseif addntl_info == ADDNTL_INFO_FLOAT16
183
- error (" Decoding 16-bit floats isn't supported." )
184
- end
185
-
186
- bytes_consumed += float_byte_len
187
- data = hex2num (bytes2hex (
188
- bytes[(start_idx + 1 ): (start_idx + float_byte_len)]
189
- ))
176
+ data = Simple (simple_val)
190
177
end
191
-
192
- elseif first_byte & ADDNTL_INFO_MASK == ADDNTL_INFO_INDEF
193
- data, bytes_consumed =
194
- decode_next_indef (start_idx, bytes, typ, with_iana)
195
-
196
- elseif typ == TYPE_2
197
- byte_string_len, bytes_consumed =
198
- decode_unsigned (start_idx, bytes)
199
- start_idx += bytes_consumed
200
-
201
- data =
202
- bytes[start_idx: (start_idx + byte_string_len - 1 )]
203
- bytes_consumed += byte_string_len
204
-
205
- elseif typ == TYPE_3
206
- string_bytes, bytes_consumed =
207
- decode_unsigned (start_idx, bytes)
208
- start_idx += bytes_consumed
209
- data = (VERSION < v " 0.5.0" ) ?
210
- UTF8String (bytes[start_idx: (start_idx + string_bytes - 1 )]) :
211
- String (bytes[start_idx: (start_idx + string_bytes - 1 )])
212
- bytes_consumed += string_bytes
213
-
214
- elseif typ == TYPE_4
215
- vec_len, bytes_consumed = decode_unsigned (start_idx, bytes)
216
- data = Vector (vec_len)
217
- for i in 1 : vec_len
218
- data[i], sub_bytes_consumed =
219
- decode_next (start_idx + bytes_consumed, bytes, with_iana)
220
- bytes_consumed += sub_bytes_consumed
178
+ else
179
+
180
+ if addntl_info == ADDNTL_INFO_FLOAT64
181
+ float_byte_len = SIZE_OF_FLOAT64
182
+ FloatT = Float64; UintT = UInt64
183
+ elseif addntl_info == ADDNTL_INFO_FLOAT32
184
+ float_byte_len = SIZE_OF_FLOAT32
185
+ FloatT = Float32; UintT = UInt32
186
+ elseif addntl_info == ADDNTL_INFO_FLOAT16
187
+ float_byte_len = SIZE_OF_FLOAT16
188
+ FloatT = Float16; UintT = UInt16
221
189
end
222
190
223
- elseif typ == TYPE_5
224
- map_len, bytes_consumed = decode_unsigned (start_idx, bytes)
225
- data = Dict ()
226
- for i in 1 : map_len
227
- key, key_bytes =
228
- decode_next (start_idx + bytes_consumed, bytes, with_iana)
229
- bytes_consumed += key_bytes
191
+ bytes_consumed += float_byte_len
192
+ hex = bytes2hex (
193
+ bytes[(start_idx + 1 ): (start_idx + float_byte_len)]
194
+ )
195
+ data = reinterpret (FloatT, parse (UintT, hex, base = 16 ))
196
+ end
230
197
231
- value, value_bytes =
232
- decode_next (start_idx + bytes_consumed, bytes, with_iana)
233
- bytes_consumed += value_bytes
198
+ elseif first_byte & ADDNTL_INFO_MASK == ADDNTL_INFO_INDEF
199
+ data, bytes_consumed =
200
+ decode_next_indef (start_idx, bytes, typ, with_iana)
234
201
235
- data[key] = value
236
- end
202
+ elseif typ == TYPE_2
203
+ byte_string_len, bytes_consumed =
204
+ decode_unsigned (start_idx, bytes)
205
+ start_idx += bytes_consumed
206
+
207
+ data =
208
+ bytes[start_idx: (start_idx + byte_string_len - 1 )]
209
+ bytes_consumed += byte_string_len
210
+
211
+ elseif typ == TYPE_3
212
+ string_bytes, bytes_consumed =
213
+ decode_unsigned (start_idx, bytes)
214
+ start_idx += bytes_consumed
215
+ data = String (bytes[start_idx: (start_idx + string_bytes - 1 )])
216
+
217
+ bytes_consumed += string_bytes
218
+
219
+ elseif typ == TYPE_4
220
+ vec_len, bytes_consumed = decode_unsigned (start_idx, bytes)
221
+ data = Vector (undef, vec_len)
222
+ for i in 1 : vec_len
223
+ data[i], sub_bytes_consumed =
224
+ decode_next (start_idx + bytes_consumed, bytes, with_iana)
225
+ bytes_consumed += sub_bytes_consumed
237
226
end
238
227
228
+ elseif typ == TYPE_5
229
+ map_len, bytes_consumed = decode_unsigned (start_idx, bytes)
230
+ data = Dict ()
231
+ for i in 1 : map_len
232
+ key, key_bytes =
233
+ decode_next (start_idx + bytes_consumed, bytes, with_iana)
234
+ bytes_consumed += key_bytes
235
+
236
+ value, value_bytes =
237
+ decode_next (start_idx + bytes_consumed, bytes, with_iana)
238
+ bytes_consumed += value_bytes
239
+
240
+ data[key] = value
241
+ end
242
+ end
243
+
239
244
return data, bytes_consumed
240
245
end
0 commit comments