@@ -170,14 +170,16 @@ unpack_stream(<<16#C9, Len:32, Type:1/signed-integer-unit:8, Data:Len/binary, Re
170
170
? OPTION {ext_unpacker = Unpack , original_list = Orig } = Opt ) ->
171
171
maybe_unpack_ext (16#C9 , Unpack , Type , Data , Rest , Orig , Opt );
172
172
173
- unpack_stream (_Bin , _Opt ) ->
173
+ % % Do not remove the binary wrapper from the function clause, it is a bin optimization for reusing the context
174
+ unpack_stream (<<_Bin /binary >>, _Opt ) ->
174
175
throw (incomplete ).
175
176
177
+ % % Do not remove the binary wrapper from the function clauses, it is a bin optimization for reusing the context
176
178
-spec unpack_array (binary (), non_neg_integer (), [msgpack :object ()], ? OPTION {}) ->
177
- {[msgpack :object ()], binary ()} | no_return ().
178
- unpack_array (Bin , 0 , Acc , _ ) ->
179
+ {[msgpack :object ()], binary ()} | no_return ().
180
+ unpack_array (<< Bin / binary >> , 0 , Acc , _ ) ->
179
181
{lists :reverse (Acc ), Bin };
180
- unpack_array (Bin , Len , Acc , Opt ) ->
182
+ unpack_array (<< Bin / binary >> , Len , Acc , Opt ) ->
181
183
{Term , Rest } = unpack_stream (Bin , Opt ),
182
184
unpack_array (Rest , Len - 1 , [Term |Acc ], Opt ).
183
185
@@ -216,18 +218,20 @@ unpack_map_jsx(Bin, Len, Opt) ->
216
218
{Map , Rest } -> {Map , Rest }
217
219
end .
218
220
221
+ % % Do not remove the binary wrapper from the function clause, it is a bin optimization for reusing the context
219
222
- spec unpack_map_as_proplist (binary (), non_neg_integer (), proplists :proplist (), ? OPTION {}) ->
220
223
{proplists :proplist (), binary ()} | no_return ().
221
- unpack_map_as_proplist (Bin , 0 , Acc , _ ) ->
224
+ unpack_map_as_proplist (<< Bin / binary >> , 0 , Acc , _ ) ->
222
225
{lists :reverse (Acc ), Bin };
223
- unpack_map_as_proplist (Bin , Len , Acc , Opt ) ->
226
+ unpack_map_as_proplist (<< Bin / binary >> , Len , Acc , Opt ) ->
224
227
{Key , Rest } = unpack_stream (Bin , Opt ),
225
228
{Value , Rest2 } = unpack_stream (Rest , Opt ),
226
229
unpack_map_as_proplist (Rest2 , Len - 1 , [{Key ,Value }|Acc ], Opt ).
227
230
228
- unpack_str_or_raw (V , ? OPTION {spec = old } = Opt , Rest ) ->
231
+ % % Do not remove the binary wrapper from the function clauses, it is a bin optimization for reusing the context
232
+ unpack_str_or_raw (<<V /binary >>, ? OPTION {spec = old } = Opt , Rest ) ->
229
233
{maybe_bin (V , Opt ), Rest };
230
- unpack_str_or_raw (V , ? OPTION {spec = new ,
234
+ unpack_str_or_raw (<< V / binary >> , ? OPTION {spec = new ,
231
235
unpack_str = UnpackStr ,
232
236
validate_string = ValidateString } = Opt , Rest ) ->
233
237
{case UnpackStr of
@@ -237,37 +241,39 @@ unpack_str_or_raw(V, ?OPTION{spec=new,
237
241
as_tagged_list -> {string , unpack_str (V )}
238
242
end , Rest }.
239
243
240
- maybe_bin (Bin , ? OPTION {known_atoms = Known }) when Known =/= [] ->
244
+ % % Do not remove the binary wrapper from the function clauses, it is a bin optimization for reusing the context
245
+ maybe_bin (<<Bin /binary >>, ? OPTION {known_atoms = Known }) when Known =/= [] ->
241
246
case lists :member (Bin ,Known ) of
242
247
true ->
243
248
erlang :binary_to_existing_atom (Bin ,utf8 );
244
249
false ->
245
250
Bin
246
251
end ;
247
252
248
- maybe_bin (Bin , _ ) ->
253
+ maybe_bin (<< Bin / binary >> , _ ) ->
249
254
Bin .
250
255
251
256
% % NOTE: msgpack DOES validate the binary as valid unicode string.
252
- unpack_str (Binary ) ->
257
+ unpack_str (<< Binary / binary >> ) ->
253
258
case unicode :characters_to_list (Binary ) of
254
259
{error , _S , _Rest } -> throw ({invalid_string , Binary });
255
260
{incomplete , _S , _Rest } -> throw ({invalid_string , Binary });
256
261
String -> String
257
262
end .
258
263
259
- maybe_unpack_ext (F , _ , _ , _ , _Rest , _ , ? OPTION {spec = old }) ->
264
+ % % Do not remove the binary wrapper from the function clauses, it is a bin optimization for reusing the context
265
+ maybe_unpack_ext (F , _ , _ , <<_ /binary >>, <<_Rest /binary >>, _ , ? OPTION {spec = old }) ->
260
266
% % trying to unpack new ext formats with old unpacker
261
267
throw ({badarg , {new_spec , F }});
262
- maybe_unpack_ext (F , undefined , _ , _ , _Rest , _ , _ ) ->
268
+ maybe_unpack_ext (F , undefined , _ , << _ / binary >>, << _Rest / binary >> , _ , _ ) ->
263
269
throw ({badarg , {bad_ext , F }});
264
- maybe_unpack_ext (_ , Unpack , Type , Data , Rest , Orig , _ )
270
+ maybe_unpack_ext (_ , Unpack , Type , << Data / binary >>, << Rest / binary >> , Orig , _ )
265
271
when is_function (Unpack , 3 ) ->
266
272
case Unpack (Type , Data , Orig ) of
267
273
{ok , Term } -> {Term , Rest };
268
274
{error , E } -> {error , E }
269
275
end ;
270
- maybe_unpack_ext (_ , Unpack , Type , Data , Rest , _ , _ )
276
+ maybe_unpack_ext (_ , Unpack , Type , << Data / binary >>, << Rest / binary >> , _ , _ )
271
277
when is_function (Unpack , 2 ) ->
272
278
case Unpack (Type , Data ) of
273
279
{ok , Term } -> {Term , Rest };
0 commit comments