aboutsummaryrefslogtreecommitdiffstats
path: root/lib
diff options
context:
space:
mode:
authorHans Bolinder <[email protected]>2018-04-17 14:55:35 +0200
committerHans Bolinder <[email protected]>2018-04-25 12:30:06 +0200
commit513e6c069c31da33d435d16d811211eee7e16399 (patch)
tree569917dc0034701cd18b8dcab2795830b9a645a1 /lib
parentd3f0d1785e5847031f21484ca99c503c6ef704b8 (diff)
downloadotp-513e6c069c31da33d435d16d811211eee7e16399.tar.gz
otp-513e6c069c31da33d435d16d811211eee7e16399.tar.bz2
otp-513e6c069c31da33d435d16d811211eee7e16399.zip
stdlib: Modify ~w/~W when number of characters is limited
A bug fix: limited maps end with "...", not "...=>...". A modification: wW separate pairs with " => ", not "=>". When the output is limited on number of characters, the term is balanced by wW the same way as is done with pP (see commit bc38638).
Diffstat (limited to 'lib')
-rw-r--r--lib/stdlib/src/io_lib.erl22
-rw-r--r--lib/stdlib/src/io_lib_pretty.erl7
-rw-r--r--lib/stdlib/test/io_SUITE.erl100
3 files changed, 71 insertions, 58 deletions
diff --git a/lib/stdlib/src/io_lib.erl b/lib/stdlib/src/io_lib.erl
index 17b6e9967c..7a520f17d9 100644
--- a/lib/stdlib/src/io_lib.erl
+++ b/lib/stdlib/src/io_lib.erl
@@ -65,6 +65,7 @@
-export([print/1,print/4,indentation/2]).
-export([write/1,write/2,write/3,nl/0,format_prompt/1,format_prompt/2]).
+-export([write_binary/2]).
-export([write_atom/1,write_string/1,write_string/2,write_latin1_string/1,
write_latin1_string/2, write_char/1, write_latin1_char/1]).
@@ -259,7 +260,8 @@ add_modifier(_, C) ->
-spec write(Term) -> chars() when
Term :: term().
-write(Term) -> write(Term, -1).
+write(Term) ->
+ write1(Term, -1, latin1).
-spec write(term(), depth(), boolean()) -> chars().
@@ -281,9 +283,19 @@ write(Term, D, false) ->
write(Term, Options) when is_list(Options) ->
Depth = get_option(depth, Options, -1),
Encoding = get_option(encoding, Options, epp:default_encoding()),
- write1(Term, Depth, Encoding);
+ CharsLimit = get_option(chars_limit, Options, -1),
+ RecDefFun = no_fun,
+ case Depth of
+ 0 -> "...";
+ _ when CharsLimit < 0 ->
+ write1(Term, Depth, Encoding);
+ _ when CharsLimit >= 0 ->
+ If = io_lib_pretty:intermediate
+ (Term, Depth, CharsLimit, RecDefFun, Encoding, _Str=false),
+ io_lib_pretty:write(If)
+ end;
write(Term, Depth) ->
- write1(Term, Depth, latin1).
+ write(Term, [{depth, Depth}, {encoding, latin1}]).
write1(_Term, 0, _E) -> "...";
write1(Term, _D, _E) when is_integer(Term) -> integer_to_list(Term);
@@ -339,14 +351,14 @@ write_ref(Ref) ->
write_map(Map, D, E) when is_integer(D) ->
[$#,${,write_map_body(maps:to_list(Map), D, E),$}].
-write_map_body(_, 0, _E) -> "...";
+write_map_body(_, 1, _E) -> "...";
write_map_body([], _, _E) -> [];
write_map_body([{K,V}], D, E) -> write_map_assoc(K, V, D, E);
write_map_body([{K,V}|KVs], D, E) ->
[write_map_assoc(K, V, D, E),$, | write_map_body(KVs, D-1, E)].
write_map_assoc(K, V, D, E) ->
- [write1(K, D - 1, E),"=>",write1(V, D-1, E)].
+ [write1(K, D - 1, E)," => ",write1(V, D-1, E)].
write_binary(B, D) when is_integer(D) ->
[$<,$<,write_binary_body(B, D),$>,$>].
diff --git a/lib/stdlib/src/io_lib_pretty.erl b/lib/stdlib/src/io_lib_pretty.erl
index 785c602ac0..9b0000ded0 100644
--- a/lib/stdlib/src/io_lib_pretty.erl
+++ b/lib/stdlib/src/io_lib_pretty.erl
@@ -26,6 +26,9 @@
-export([print/1,print/2,print/3,print/4,print/5,print/6]).
+%% To be used by io_lib only.
+-export([intermediate/6, write/1]).
+
%%%
%%% Exported functions
%%%
@@ -573,10 +576,10 @@ print_length(<<_/bitstring>> = Bin, D, T, RF, Enc, Str) ->
end,
{[$<,$<,S|"/utf8...>>"], 12 + length(S), 3, More};
false when byte_size(Bin) < D ->
- S = io_lib:write(Bin, D),
+ S = io_lib:write_binary(Bin, D),
{{bin, S}, iolist_size(S), 0, no_more};
false ->
- S = io_lib:write(Bin, D),
+ S = io_lib:write_binary(Bin, D),
More = fun(T1, Dd) ->
?FUNCTION_NAME(Bin, D+Dd, T1, RF, Enc, Str)
end,
diff --git a/lib/stdlib/test/io_SUITE.erl b/lib/stdlib/test/io_SUITE.erl
index 9fdd7da4dc..e1a6f9031b 100644
--- a/lib/stdlib/test/io_SUITE.erl
+++ b/lib/stdlib/test/io_SUITE.erl
@@ -2053,19 +2053,19 @@ maps(_Config) ->
%% in a map with more than one element.
"#{}" = fmt("~w", [#{}]),
- "#{a=>b}" = fmt("~w", [#{a=>b}]),
- re_fmt(<<"#\\{(a=>b|c=>d),[.][.][.]=>[.][.][.]\\}">>,
- "~W", [#{a=>b,c=>d},2]),
- re_fmt(<<"#\\{(a=>b|c=>d|e=>f),[.][.][.]=>[.][.][.],[.][.][.]\\}">>,
- "~W", [#{a=>b,c=>d,e=>f},2]),
+ "#{a => b}" = fmt("~w", [#{a=>b}]),
+ re_fmt(<<"#\\{(a => b),[.][.][.]\\}">>,
+ "~W", [#{a => b,c => d},2]),
+ re_fmt(<<"#\\{(a => b),[.][.][.]\\}">>,
+ "~W", [#{a => b,c => d,e => f},2]),
"#{}" = fmt("~p", [#{}]),
- "#{a => b}" = fmt("~p", [#{a=>b}]),
- "#{...}" = fmt("~P", [#{a=>b},1]),
+ "#{a => b}" = fmt("~p", [#{a => b}]),
+ "#{...}" = fmt("~P", [#{a => b},1]),
re_fmt(<<"#\\{(a => b|c => d),[.][.][.]\\}">>,
- "~P", [#{a=>b,c=>d},2]),
+ "~P", [#{a => b,c => d},2]),
re_fmt(<<"#\\{(a => b|c => d|e => f),[.][.][.]\\}">>,
- "~P", [#{a=>b,c=>d,e=>f},2]),
+ "~P", [#{a => b,c => d,e => f},2]),
List = [{I,I*I} || I <- lists:seq(1, 20)],
Map = maps:from_list(List),
@@ -2493,25 +2493,27 @@ pp(Term, Depth) ->
otp_14983(_Config) ->
trunc_depth(-1),
trunc_depth(10),
+ trunc_depth_p(-1),
+ trunc_depth_p(10),
ok.
trunc_depth(D) ->
- "..." = tp("", D, 0),
- "[]" = tp("", D, 1),
+ "..." = trp("", D, 0),
+ "[]" = trp("", D, 1),
- "#{}" = tp(#{}, D, 1),
- "#{a => 1}" = tp(#{a => 1}, D, 7),
- "#{...}" = tp(#{a => 1}, D, 5),
- "#{a => 1}" = tp(#{a => 1}, D, 6),
+ "#{}" = trp(#{}, D, 1),
+ "#{a => 1}" = trp(#{a => 1}, D, 7),
+ "#{...}" = trp(#{a => 1}, D, 5),
+ "#{a => 1}" = trp(#{a => 1}, D, 6),
A = lists:seq(1, 1000),
M = #{A => A, {A,A} => {A,A}},
- "#{...}" = tp(M, D, 6),
- "#{{...} => {...},...}" = tp(M, D, 7),
- "#{{[...],...} => {[...],...},...}" = tp(M, D, 22),
- "#{{[...],...} => {[...],...},[...] => [...]}" = tp(M, D, 31),
- "#{{[...],...} => {[...],...},[...] => [...]}" = tp(M, D, 33),
+ "#{...}" = trp(M, D, 6),
+ "#{{...} => {...},...}" = trp(M, D, 7),
+ "#{{[...],...} => {[...],...},...}" = trp(M, D, 22),
+ "#{{[...],...} => {[...],...},[...] => [...]}" = trp(M, D, 31),
+ "#{{[...],...} => {[...],...},[...] => [...]}" = trp(M, D, 33),
"#{{[1|...],[...]} => {[1|...],[...]},[1|...] => [...]}" =
- tp(M, D, 50),
+ trp(M, D, 50),
"..." = trp({c, 1, 2}, D, 0),
"{...}" = trp({c, 1, 2}, D, 1),
@@ -2522,42 +2524,38 @@ trunc_depth(D) ->
"#c{f1 = [1,2|...],f2 = [1|...]}" = trp({c, A, A}, D, 31),
"#c{f1 = [1,2,3|...],f2 = [1,2|...]}" = trp({c, A, A}, D, 32),
- "..." = tp({}, D, 0),
- "{}" = tp({}, D, 1),
+ "..." = trp({}, D, 0),
+ "{}" = trp({}, D, 1),
T = {A, A, A},
- "{...}" = tp(T, D, 5),
- "{[...],...}" = tp(T, D, 6),
- "{[1|...],[...],...}" = tp(T, D, 12),
- "{[1,2|...],[1|...],...}" = tp(T, D, 20),
- "{[1,2|...],[1|...],[...]}" = tp(T, D, 21),
- "{[1,2,3|...],[1,2|...],[1|...]}" = tp(T, D, 28),
-
- "{[1],[1,2|...]}" = tp({[1],[1,2,3,4]}, D, 14),
- "[...]" = tp("abcdefg", D, 4),
- "\"abc\"..." = tp("abcdefg", D, 5),
- "\"abcdef\"..." = tp("abcdefg", D, 8),
- "\"abcdefg\"" = tp("abcdefg", D, 9),
- "\"abcdefghijkl\"" = tp("abcdefghijkl", D, -1),
-
+ "{...}" = trp(T, D, 5),
+ "{[...],...}" = trp(T, D, 6),
+ "{[1|...],[...],...}" = trp(T, D, 12),
+ "{[1,2|...],[1|...],...}" = trp(T, D, 20),
+ "{[1,2|...],[1|...],[...]}" = trp(T, D, 21),
+ "{[1,2,3|...],[1,2|...],[1|...]}" = trp(T, D, 28),
+
+ "{[1],[1,2|...]}" = trp({[1],[1,2,3,4]}, D, 14).
+
+trunc_depth_p(D) ->
+ "[...]" = trp("abcdefg", D, 4),
+ "\"abc\"..." = trp("abcdefg", D, 5),
+ "\"abcdef\"..." = trp("abcdefg", D, 8),
+ "\"abcdefg\"" = trp("abcdefg", D, 9),
+ "\"abcdefghijkl\"" = trp("abcdefghijkl", D, -1),
AZ = lists:seq($A, $Z),
AZb = list_to_binary(AZ),
AZbS = "<<\"" ++ AZ ++ "\">>",
- AZbS = tp(AZb, D, -1),
- "<<\"ABCDEFGH\"...>>" = tp(AZb, D, 17), % 4 chars even if D = -1...
- "<<\"ABCDEFGHIJKL\"...>>" = tp(AZb, D, 18),
+ AZbS = trp(AZb, D, -1),
+ "<<\"ABCDEFGH\"...>>" = trp(AZb, D, 17), % 4 chars even if D = -1...
+ "<<\"ABCDEFGHIJKL\"...>>" = trp(AZb, D, 18),
B1 = <<"abcdef",0:8>>,
- "<<\"ab\"...>>" = tp(B1, D, 8),
- "<<\"abcdef\"...>>" = tp(B1, D, 14),
- %% Tricky. Much more than 15 characters:
- "<<97,98,99,100,101,102,0>>" = tp(B1, D, 15),
- "<<97,98,99,100,101,102,0>>" = tp(B1, D, -1),
+ "<<\"ab\"...>>" = trp(B1, D, 8),
+ "<<\"abcdef\"...>>" = trp(B1, D, 14),
+ "<<97,98,99,100,101,102,0>>" = trp(B1, D, -1),
B2 = <<AZb/binary,0:8>>,
- "<<\"AB\"...>>" = tp(B2, D, 8),
- "<<\"ABCDEFGH\"...>>" = tp(B2, D, 14),
- "<<65,66,67,68,69,70,71,72,0>>" = tp(<<"ABCDEFGH",0:8>>, D, -1).
-
-tp(Term, D, T) ->
- trp(Term, D, T, fun(_, _) -> no end).
+ "<<\"AB\"...>>" = trp(B2, D, 8),
+ "<<\"ABCDEFGH\"...>>" = trp(B2, D, 14),
+ "<<65,66,67,68,69,70,71,72,0>>" = trp(<<"ABCDEFGH",0:8>>, D, -1).
trp(Term, D, T) ->
trp(Term, D, T, fun rfd/2).