Commits

Jacob Vorreuter  committed dc9fb7a

updating bson decode functions

  • Participants
  • Parent commits 876b13f

Comments (0)

Files changed (1)

File src/emongo_bson.erl

 decode(<<>>, Acc) ->
 	lists:reverse(Acc);
 	
-decode(<<Size:32/little-signed, Rest/binary>>, Acc) ->
+decode(Bin, Acc) ->
+	{Doc, Rest} = decode_internal(Bin),
+	decode(Rest, [Doc|Acc]).
+	
+decode_internal(<<Size:32/little-signed, Rest/binary>>) ->
 	Size1 = Size-5,
 	<<Bin:Size1/binary, 0:8, Tail/binary>> = Rest,
-	decode(Tail, [decode_document(Bin, [])|Acc]).
+	{decode_document(Bin, []), Tail}.
 	
 decode_document(<<>>, Acc) ->
 	lists:reverse(Acc);
 	
 %% OBJECT
 decode_value(3, Bin) ->
-	[Val] = decode(Bin),
-	{Val, <<>>};
+	decode_internal(Bin);
 
 %% DATA ARRAY
 decode_value(4, Bin) ->
-	[Val] = decode(Bin),
-	{{array, [V || {_, V} <- Val]}, <<>>};
+	{Val, Rest} = decode_internal(Bin),
+	{{array, [V || {_, V} <- Val]}, Rest};
 
 %% BINARY
 decode_value(5, <<_Size:32/little-signed, 2:8/little, BinSize:32/little-signed, BinData:BinSize/binary-little-unit:8, Tail/binary>>) ->