Skip to content

Commit

Permalink
stdlib: Make io_lib and io_lib_pretty use maps iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
garazdawi committed Nov 20, 2017
1 parent a1c796e commit f1666d9
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 4 deletions.
Binary file modified bootstrap/lib/stdlib/ebin/io_lib.beam
Binary file not shown.
Binary file modified bootstrap/lib/stdlib/ebin/io_lib_pretty.beam
Binary file not shown.
13 changes: 12 additions & 1 deletion lib/stdlib/src/io_lib.erl
Original file line number Diff line number Diff line change
Expand Up @@ -963,7 +963,18 @@ limit_tail(Other, D) ->
%% maps:from_list() creates a map with the same internal ordering of
%% the selected associations as in Map.
limit_map(Map, D) ->
maps:from_list(erts_internal:maps_to_list(Map, D)).
limit_map(maps:iterator(Map), D, []).

limit_map(_I, 0, Acc) ->
maps:from_list(Acc);
limit_map(I, D, Acc) ->
case maps:next(I) of
{K, V, NextI} ->
limit_map(NextI, D-1, [{K,V} | Acc]);
none ->
maps:from_list(Acc)
end.

%% maps:from_list(limit_map_body(erts_internal:maps_to_list(Map, D), D)).

%% limit_map_body(_, 0) -> [{'...', '...'}];
Expand Down
12 changes: 11 additions & 1 deletion lib/stdlib/src/io_lib_pretty.erl
Original file line number Diff line number Diff line change
Expand Up @@ -478,9 +478,19 @@ print_length(Term, _D, _RF, _Enc, _Str) ->
print_length_map(_Map, 1, _RF, _Enc, _Str) ->
{"#{...}", 6};
print_length_map(Map, D, RF, Enc, Str) when is_map(Map) ->
Pairs = print_length_map_pairs(erts_internal:maps_to_list(Map, D), D, RF, Enc, Str),
Pairs = print_length_map_pairs(limit_map(maps:iterator(Map), D, []), D, RF, Enc, Str),
{{map, Pairs}, list_length(Pairs, 3)}.

limit_map(_I, 0, Acc) ->
Acc;
limit_map(I, D, Acc) ->
case maps:next(I) of
{K, V, NextI} ->
limit_map(NextI, D-1, [{K,V} | Acc]);
none ->
Acc
end.

print_length_map_pairs([], _D, _RF, _Enc, _Str) ->
[];
print_length_map_pairs(_Pairs, 1, _RF, _Enc, _Str) ->
Expand Down
4 changes: 2 additions & 2 deletions lib/stdlib/test/io_SUITE.erl
Original file line number Diff line number Diff line change
Expand Up @@ -2138,8 +2138,8 @@ otp_14175(_Config) ->
"#{}" = p(#{}, 1),
"#{...}" = p(#{a => 1}, 1),
"#{#{} => a}" = p(#{#{} => a}, 2),
"#{a => 1,...}" = p(#{a => 1, b => 2}, 2),
"#{a => 1,b => 2}" = p(#{a => 1, b => 2}, -1),
mt("#{a => 1,...}", p(#{a => 1, b => 2}, 2)),
mt("#{a => 1,b => 2}", p(#{a => 1, b => 2}, -1)),

M = #{kaaaaaaaaaaaaaaaaaaa => v1,kbbbbbbbbbbbbbbbbbbb => v2,
kccccccccccccccccccc => v3,kddddddddddddddddddd => v4,
Expand Down

0 comments on commit f1666d9

Please sign in to comment.