less list allocation in jsx_to_term
This commit is contained in:
parent
c39515bd60
commit
261574ee03
1 changed files with 17 additions and 17 deletions
|
@ -172,10 +172,10 @@ start_array({Stack, Config}) -> {[{array, []}] ++ Stack, Config}.
|
|||
%% return it if it is the root object
|
||||
finish({[{object, []}], Config}) -> {[{}], Config};
|
||||
finish({[{object, []}|Rest], Config}) -> insert([{}], {Rest, Config});
|
||||
finish({[{object, Pairs}], Config}) -> {lists:reverse(Pairs), Config};
|
||||
finish({[{object, Pairs}|Rest], Config}) -> insert(lists:reverse(Pairs), {Rest, Config});
|
||||
finish({[{array, Values}], Config}) -> {lists:reverse(Values), Config};
|
||||
finish({[{array, Values}|Rest], Config}) -> insert(lists:reverse(Values), {Rest, Config});
|
||||
finish({[{object, Pairs}], Config}) -> {Pairs, Config};
|
||||
finish({[{object, Pairs}|Rest], Config}) -> insert(Pairs, {Rest, Config});
|
||||
finish({[{array, Values}], Config}) -> {Values, Config};
|
||||
finish({[{array, Values}|Rest], Config}) -> insert(Values, {Rest, Config});
|
||||
finish(_) -> erlang:error(badarg).
|
||||
|
||||
|
||||
|
@ -185,9 +185,9 @@ insert(Value, {[], Config}) -> {Value, Config};
|
|||
insert(Key, {[{object, Pairs}|Rest], Config}) ->
|
||||
{[{object, Key, Pairs}] ++ Rest, Config};
|
||||
insert(Value, {[{object, Key, Pairs}|Rest], Config}) ->
|
||||
{[{object, [{Key, Value}] ++ Pairs}] ++ Rest, Config};
|
||||
{[{object, Pairs ++ [{Key, Value}]}] ++ Rest, Config};
|
||||
insert(Value, {[{array, Values}|Rest], Config}) ->
|
||||
{[{array, [Value] ++ Values}] ++ Rest, Config};
|
||||
{[{array, Values ++ [Value]}] ++ Rest, Config};
|
||||
insert(_, _) -> erlang:error(badarg).
|
||||
-endif.
|
||||
|
||||
|
@ -212,10 +212,10 @@ finish({[{object, Map}|Rest], Config=#config{return_maps=true}}) ->
|
|||
insert(Map, {Rest, Config});
|
||||
finish({[{object, []}], Config}) -> {[{}], Config};
|
||||
finish({[{object, []}|Rest], Config}) -> insert([{}], {Rest, Config});
|
||||
finish({[{object, Pairs}], Config}) -> {lists:reverse(Pairs), Config};
|
||||
finish({[{object, Pairs}|Rest], Config}) -> insert(lists:reverse(Pairs), {Rest, Config});
|
||||
finish({[{array, Values}], Config}) -> {lists:reverse(Values), Config};
|
||||
finish({[{array, Values}|Rest], Config}) -> insert(lists:reverse(Values), {Rest, Config});
|
||||
finish({[{object, Pairs}], Config}) -> {Pairs, Config};
|
||||
finish({[{object, Pairs}|Rest], Config}) -> insert(Pairs, {Rest, Config});
|
||||
finish({[{array, Values}], Config}) -> {Values, Config};
|
||||
finish({[{array, Values}|Rest], Config}) -> insert(Values, {Rest, Config});
|
||||
finish(_) -> erlang:error(badarg).
|
||||
|
||||
|
||||
|
@ -229,9 +229,9 @@ insert(Key, {[{object, Pairs}|Rest], Config}) ->
|
|||
insert(Value, {[{object, Key, Map}|Rest], Config=#config{return_maps=true}}) ->
|
||||
{[{object, maps:put(Key, Value, Map)}] ++ Rest, Config};
|
||||
insert(Value, {[{object, Key, Pairs}|Rest], Config}) ->
|
||||
{[{object, [{Key, Value}] ++ Pairs}] ++ Rest, Config};
|
||||
{[{object, Pairs ++ [{Key, Value}]}] ++ Rest, Config};
|
||||
insert(Value, {[{array, Values}|Rest], Config}) ->
|
||||
{[{array, [Value] ++ Values}] ++ Rest, Config};
|
||||
{[{array, Values ++ [Value]}] ++ Rest, Config};
|
||||
insert(_, _) -> erlang:error(badarg).
|
||||
-endif.
|
||||
|
||||
|
@ -339,7 +339,7 @@ rep_manipulation_test_() ->
|
|||
insert(value, {[{array, []}, junk], #config{}})
|
||||
)},
|
||||
{"finish an object with no ancestor", ?_assertEqual(
|
||||
{[{a, b}, {x, y}], #config{}},
|
||||
{[{x, y}, {a, b}], #config{}},
|
||||
finish({[{object, [{x, y}, {a, b}]}], #config{}})
|
||||
)},
|
||||
{"finish an empty object", ?_assertEqual(
|
||||
|
@ -347,16 +347,16 @@ rep_manipulation_test_() ->
|
|||
finish({[{object, []}], #config{}})
|
||||
)},
|
||||
{"finish an object with an ancestor", ?_assertEqual(
|
||||
{[{object, [{key, [{a, b}, {x, y}]}, {foo, bar}]}], #config{}},
|
||||
{[{object, [{foo, bar}, {key, [{x, y}, {a, b}]}]}], #config{}},
|
||||
finish({[{object, [{x, y}, {a, b}]}, {object, key, [{foo, bar}]}], #config{}})
|
||||
)},
|
||||
{"finish an array with no ancestor", ?_assertEqual(
|
||||
{[a, b, c], #config{}},
|
||||
finish({[{array, [c, b, a]}], #config{}})
|
||||
finish({[{array, [a, b, c]}], #config{}})
|
||||
)},
|
||||
{"finish an array with an ancestor", ?_assertEqual(
|
||||
{[{array, [[a, b, c], d, e, f]}], #config{}},
|
||||
finish({[{array, [c, b, a]}, {array, [d, e, f]}], #config{}})
|
||||
{[{array, [d, e, f, [a, b, c]]}], #config{}},
|
||||
finish({[{array, [a, b, c]}, {array, [d, e, f]}], #config{}})
|
||||
)}
|
||||
].
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue