|
|
@ -33,7 +33,7 @@ |
|
|
|
-module(lager_trunc_io). |
|
|
|
-author('matthias@corelatus.se'). |
|
|
|
%% And thanks to Chris Newcombe for a bug fix |
|
|
|
-export([format/3, print/2, fprint/2, safe/2]). % interface functions |
|
|
|
-export([format/3, format/4, print/2, print/3, fprint/2, fprint/3, safe/2]). % interface functions |
|
|
|
-version("$Id: trunc_io.erl,v 1.11 2009-02-23 12:01:06 matthias Exp $"). |
|
|
|
|
|
|
|
-ifdef(TEST). |
|
|
@ -41,149 +41,39 @@ |
|
|
|
-include_lib("eunit/include/eunit.hrl"). |
|
|
|
-endif. |
|
|
|
|
|
|
|
format(String, Args, Max) -> |
|
|
|
Parts = re:split(String, |
|
|
|
"(~(?:-??\\d+\\.|\\*\\.|\\.|)(?:-??\\d+\\.|\\*\\.|\\.|)(?:-??\\d+|\\*|)(?:t|)(?:[cfegswpWPBX#bx+ni~]))", |
|
|
|
[{return, list}, trim]), |
|
|
|
Maxlen = Max - length(String), |
|
|
|
format(Parts, Args, Maxlen, [], []). |
|
|
|
|
|
|
|
format([], _Args, Max, Acc, ArgAcc) -> |
|
|
|
FmtArgs = resolve_futures(Max, ArgAcc), |
|
|
|
io_lib:format(lists:flatten(lists:reverse(Acc)), lists:reverse(FmtArgs)); |
|
|
|
format([[] | T], Args, Max, Acc, ArgAcc) -> |
|
|
|
% discard the null list generated by split |
|
|
|
format(T, Args, Max, Acc, ArgAcc); |
|
|
|
format(["~~" | T], Args, Max, Acc, ArgAcc) -> |
|
|
|
format(T, Args, Max+1, ["~~" | Acc], ArgAcc); |
|
|
|
format(["~n" | T], Args, Max, Acc, ArgAcc) -> |
|
|
|
% ignore newlines for the purposes of argument indexing |
|
|
|
format(T, Args, Max+1, ["~n" | Acc], ArgAcc); |
|
|
|
format(["~i" | T], [AH | AT], Max, Acc, ArgAcc) -> |
|
|
|
% ~i means ignore this argument, but we'll just pass it through |
|
|
|
format(T, AT, Max+2, ["~i" | Acc], [AH | ArgAcc]); |
|
|
|
format([[$~|H]| T], [AH1, AH2 | AT], Max, Acc, ArgAcc) when H == "X"; H == "x" -> |
|
|
|
%% ~X consumes 2 arguments. It only prints integers so we can leave it alone |
|
|
|
format(T, AT, Max, ["~X" | Acc], [AH2, AH1 | ArgAcc]); |
|
|
|
format([[$~|H]| T], [AH1, _AH2 | AT], Max, Acc, ArgAcc) when H == "W"; H == "P" -> |
|
|
|
%% ~P and ~W consume 2 arguments, the second one being a depth limiter. |
|
|
|
%% trunc_io isn't (yet) depth aware, so we can't honor this format string |
|
|
|
%% safely at the moment, so just treat it like a regular ~p |
|
|
|
%% TODO support for depth limiting |
|
|
|
Input = case H == "P" andalso lager_stdlib:string_p(AH1) of |
|
|
|
true -> |
|
|
|
lists:flatten(AH1); |
|
|
|
_ -> AH1 |
|
|
|
end, |
|
|
|
case print(Input, Max + 2) of |
|
|
|
{_Res, Max} -> |
|
|
|
% this isn't the last argument, but it consumed all available space |
|
|
|
% delay calculating the print size until the end |
|
|
|
format(T, AT, Max + 2, ["~s" | Acc], [{future, Input} | ArgAcc]); |
|
|
|
{String, Length} -> |
|
|
|
format(T, AT, Max + 2 - Length, ["~s" | Acc], [String | ArgAcc]) |
|
|
|
end; |
|
|
|
format([[$~|H]| T], [AH | AT], Max, Acc, ArgAcc) when length(H) == 1 -> |
|
|
|
% single character format specifier, relatively simple |
|
|
|
case H of |
|
|
|
_ when H == "p"; H == "w"; H == "s" -> |
|
|
|
Input = case (H == "s" orelse H == "p") andalso lager_stdlib:string_p(AH) of |
|
|
|
true -> |
|
|
|
lists:flatten(AH); |
|
|
|
_ -> AH |
|
|
|
end, |
|
|
|
%okay, these are prime candidates for rewriting |
|
|
|
case print(Input, Max + 2) of |
|
|
|
{_Res, Max} -> |
|
|
|
% this isn't the last argument, but it consumed all available space |
|
|
|
% delay calculating the print size until the end |
|
|
|
format(T, AT, Max + 2, ["~s" | Acc], [{future, Input} | ArgAcc]); |
|
|
|
{String, Length} -> |
|
|
|
{Value, RealLen} = case H of |
|
|
|
"s" -> |
|
|
|
% strip off the doublequotes, if applicable |
|
|
|
Trimmed = unquote_string(lists:flatten(String)), |
|
|
|
{Trimmed, length(Trimmed)}; |
|
|
|
_ -> |
|
|
|
{String, Length} |
|
|
|
end, |
|
|
|
format(T, AT, Max + 2 - RealLen, ["~s" | Acc], [Value | ArgAcc]) |
|
|
|
end; |
|
|
|
_ -> |
|
|
|
% whatever, just pass them on through |
|
|
|
format(T, AT, Max, [[$~ | H] | Acc], [AH | ArgAcc]) |
|
|
|
end; |
|
|
|
format([[$~|H]| T], [AH | AT], Max, Acc, ArgAcc) -> |
|
|
|
%% its actually simplest to just look at the last character in the string |
|
|
|
case lists:nth(length(H), H) of |
|
|
|
C when C == $p; C == $w; C == $s -> |
|
|
|
%okay, these are prime candidates for rewriting |
|
|
|
Input = case (C == $s orelse C == $p) andalso lager_stdlib:string_p(AH) of |
|
|
|
true -> |
|
|
|
lists:flatten(AH); |
|
|
|
_ -> AH |
|
|
|
end, |
|
|
|
case print(Input, Max + length(H) + 1) of |
|
|
|
{_Res, Max} -> |
|
|
|
% this isn't the last argument, but it consumed all available space |
|
|
|
% delay calculating the print size until the end |
|
|
|
format(T, AT, Max + length(H) + 1, ["~s" | Acc], [{future, Input} | ArgAcc]); |
|
|
|
{String, Length} -> |
|
|
|
{Value, RealLen} = case C of |
|
|
|
$s -> |
|
|
|
% strip off the doublequotes, if applicable |
|
|
|
Trimmed = unquote_string(lists:flatten(String)), |
|
|
|
{Trimmed, length(Trimmed)}; |
|
|
|
_ -> |
|
|
|
{String, Length} |
|
|
|
end, |
|
|
|
format(T, AT, Max + length(H) + 1 - RealLen, ["~s" | Acc], [Value | ArgAcc]) |
|
|
|
end; |
|
|
|
C when C == $P; C == $W -> |
|
|
|
%% ~P and ~W consume 2 arguments, the second one being a depth limiter. |
|
|
|
%% trunc_io isn't (yet) depth aware, so we can't honor this format string |
|
|
|
%% safely at the moment, so just treat it like a regular ~p |
|
|
|
%% TODO support for depth limiting |
|
|
|
[_ | AT2] = AT, |
|
|
|
Input = case C == $P andalso lager_stdlib:string_p(AH) of |
|
|
|
true -> |
|
|
|
lists:flatten(AH); |
|
|
|
_ -> AH |
|
|
|
end, |
|
|
|
case print(Input, Max + 2) of |
|
|
|
{_Res, Max} -> |
|
|
|
% this isn't the last argument, but it consumed all available space |
|
|
|
% delay calculating the print size until the end |
|
|
|
format(T, AT2, Max + 2, ["~s" | Acc], [{future, Input} | ArgAcc]); |
|
|
|
{String, Length} -> |
|
|
|
format(T, AT2, Max + 2 - Length, ["~s" | Acc], [String | ArgAcc]) |
|
|
|
end; |
|
|
|
C when C == $X; C == $x -> |
|
|
|
%% ~X consumes 2 arguments. It only prints integers so we can leave it alone |
|
|
|
[AH2 | AT2] = AT, |
|
|
|
format(T, AT2, Max, [[$~|H]|Acc], [AH2, AH |ArgAcc]); |
|
|
|
_ -> |
|
|
|
format(T, AT, Max, [[$~|H] | Acc], [AH|ArgAcc]) |
|
|
|
end; |
|
|
|
format([H | T], Args, Max, Acc, ArgAcc) -> |
|
|
|
format(T, Args, Max, [H | Acc], ArgAcc). |
|
|
|
|
|
|
|
%% for all the really big terms encountered in a format/3 call, try to give each of them an equal share |
|
|
|
resolve_futures(Max, Args) -> |
|
|
|
Count = length(lists:filter(fun({future, _}) -> true; (_) -> false end, Args)), |
|
|
|
case Count of |
|
|
|
0 -> |
|
|
|
Args; |
|
|
|
_ -> |
|
|
|
SingleFmt = Max div Count, |
|
|
|
lists:map(fun({future, Value}) -> element(1, print(Value, SingleFmt)); (X) -> X end, Args) |
|
|
|
-record(print_options, { |
|
|
|
%% negative depth means no depth limiting |
|
|
|
depth = -1 :: integer(), |
|
|
|
%% whether to print lists as strings, if possible |
|
|
|
lists_as_strings = true :: boolean(), |
|
|
|
%% force strings, or binaries to be printed as a string, |
|
|
|
%% even if they're not printable |
|
|
|
force_strings = false :: boolean() |
|
|
|
}). |
|
|
|
|
|
|
|
format(Fmt, Args, Max) -> |
|
|
|
format(Fmt, Args, Max, []). |
|
|
|
|
|
|
|
format(Fmt, Args, Max, Options) -> |
|
|
|
try lager_format:format(Fmt, Args, Max, Options) of |
|
|
|
Result -> Result |
|
|
|
catch |
|
|
|
_:_ -> |
|
|
|
erlang:error(badarg, [Fmt, Args]) |
|
|
|
end. |
|
|
|
|
|
|
|
%% @doc Returns an flattened list containing the ASCII representation of the given |
|
|
|
%% term. |
|
|
|
-spec fprint(term(), pos_integer()) -> string(). |
|
|
|
fprint(T, Max) -> |
|
|
|
{L, _} = print(T, Max), |
|
|
|
fprint(Term, Max) -> |
|
|
|
fprint(Term, Max, []). |
|
|
|
|
|
|
|
|
|
|
|
%% @doc Returns an flattened list containing the ASCII representation of the given |
|
|
|
%% term. |
|
|
|
-spec fprint(term(), pos_integer(), #print_options{}) -> string(). |
|
|
|
fprint(T, Max, Options) -> |
|
|
|
{L, _} = print(T, Max, prepare_options(Options, #print_options{})), |
|
|
|
lists:flatten(L). |
|
|
|
|
|
|
|
%% @doc Same as print, but never crashes. |
|
|
@ -204,9 +94,23 @@ safe(What, Len) -> |
|
|
|
|
|
|
|
%% @doc Returns {List, Length} |
|
|
|
-spec print(term(), pos_integer()) -> {iolist(), pos_integer()}. |
|
|
|
print(_, Max) when Max < 0 -> {"...", 3}; |
|
|
|
print(Tuple, Max) when is_tuple(Tuple) -> |
|
|
|
{TC, Len} = tuple_contents(Tuple, Max-2), |
|
|
|
print(Term, Max) -> |
|
|
|
print(Term, Max, []). |
|
|
|
|
|
|
|
%% @doc Returns {List, Length} |
|
|
|
-spec print(term(), pos_integer(), #print_options{}) -> {iolist(), pos_integer()}. |
|
|
|
print(Term, Max, Options) when is_list(Options) -> |
|
|
|
%% need to convert the proplist to a record |
|
|
|
print(Term, Max, prepare_options(Options, #print_options{})); |
|
|
|
|
|
|
|
print(Term, _Max, #print_options{force_strings=true}) when not is_list(Term), not is_binary(Term), not is_atom(Term) -> |
|
|
|
erlang:error(badarg); |
|
|
|
|
|
|
|
print(_, Max, _Options) when Max < 0 -> {"...", 3}; |
|
|
|
print(_, _, #print_options{depth=0}) -> {"...", 3}; |
|
|
|
|
|
|
|
print(Tuple, Max, Options) when is_tuple(Tuple) -> |
|
|
|
{TC, Len} = tuple_contents(Tuple, Max-2, Options), |
|
|
|
{[${, TC, $}], Len + 2}; |
|
|
|
|
|
|
|
%% @doc We assume atoms, floats, funs, integers, PIDs, ports and refs never need |
|
|
@ -214,38 +118,49 @@ print(Tuple, Max) when is_tuple(Tuple) -> |
|
|
|
%% arbitrarily long bignum. Let's assume that won't happen unless someone |
|
|
|
%% is being malicious. |
|
|
|
%% |
|
|
|
print(Atom, _Max) when is_atom(Atom) -> |
|
|
|
print(Atom, _Max, #print_options{force_strings=NoQuote}) when is_atom(Atom) -> |
|
|
|
L = atom_to_list(Atom), |
|
|
|
R = case atom_needs_quoting_start(L) of |
|
|
|
R = case atom_needs_quoting_start(L) andalso not NoQuote of |
|
|
|
true -> lists:flatten([$', L, $']); |
|
|
|
false -> L |
|
|
|
end, |
|
|
|
{R, length(R)}; |
|
|
|
|
|
|
|
print(<<>>, _Max) -> |
|
|
|
print(<<>>, _Max, _Options) -> |
|
|
|
{"<<>>", 4}; |
|
|
|
|
|
|
|
print(Binary, 0) when is_binary(Binary) -> |
|
|
|
print(Binary, 0, _Options) when is_binary(Binary) -> |
|
|
|
{"<<..>>", 6}; |
|
|
|
|
|
|
|
print(Binary, Max) when is_binary(Binary) -> |
|
|
|
print(Binary, Max, Options) when is_binary(Binary) -> |
|
|
|
B = binary_to_list(Binary, 1, lists:min([Max, size(Binary)])), |
|
|
|
{L, Len} = alist_start(B, Max-4), |
|
|
|
{L, Len} = case Options#print_options.lists_as_strings orelse |
|
|
|
Options#print_options.force_strings of |
|
|
|
true -> |
|
|
|
alist_start(B, Max-4, Options); |
|
|
|
_ -> |
|
|
|
list_body(B, Max-4, Options, false) |
|
|
|
end, |
|
|
|
{Res, Length} = case L of |
|
|
|
[91, X, 93] -> |
|
|
|
{X, Len - 2}; |
|
|
|
X -> |
|
|
|
{X, Len} |
|
|
|
end, |
|
|
|
{["<<", Res, ">>"], Length+4}; |
|
|
|
case Options#print_options.force_strings of |
|
|
|
true -> |
|
|
|
{Res, Length}; |
|
|
|
_ -> |
|
|
|
{["<<", Res, ">>"], Length+4} |
|
|
|
end; |
|
|
|
|
|
|
|
print(Float, _Max) when is_float(Float) -> |
|
|
|
print(Float, _Max, _Options) when is_float(Float) -> |
|
|
|
%% use the same function io_lib:format uses to print floats |
|
|
|
%% float_to_list is way too verbose. |
|
|
|
L = io_lib_format:fwrite_g(Float), |
|
|
|
{L, length(L)}; |
|
|
|
|
|
|
|
print(Fun, Max) when is_function(Fun) -> |
|
|
|
print(Fun, Max, _Options) when is_function(Fun) -> |
|
|
|
L = erlang:fun_to_list(Fun), |
|
|
|
case length(L) > Max of |
|
|
|
true -> |
|
|
@ -256,50 +171,62 @@ print(Fun, Max) when is_function(Fun) -> |
|
|
|
{L, length(L)} |
|
|
|
end; |
|
|
|
|
|
|
|
print(Integer, _Max) when is_integer(Integer) -> |
|
|
|
print(Integer, _Max, _Options) when is_integer(Integer) -> |
|
|
|
L = integer_to_list(Integer), |
|
|
|
{L, length(L)}; |
|
|
|
|
|
|
|
print(Pid, _Max) when is_pid(Pid) -> |
|
|
|
print(Pid, _Max, _Options) when is_pid(Pid) -> |
|
|
|
L = pid_to_list(Pid), |
|
|
|
{L, length(L)}; |
|
|
|
|
|
|
|
print(Ref, _Max) when is_reference(Ref) -> |
|
|
|
print(Ref, _Max, _Options) when is_reference(Ref) -> |
|
|
|
L = erlang:ref_to_list(Ref), |
|
|
|
{L, length(L)}; |
|
|
|
|
|
|
|
print(Port, _Max) when is_port(Port) -> |
|
|
|
print(Port, _Max, _Options) when is_port(Port) -> |
|
|
|
L = erlang:port_to_list(Port), |
|
|
|
{L, length(L)}; |
|
|
|
|
|
|
|
print(List, Max) when is_list(List) -> |
|
|
|
alist_start(List, Max). |
|
|
|
print(List, Max, Options) when is_list(List) -> |
|
|
|
case Options#print_options.lists_as_strings orelse |
|
|
|
Options#print_options.force_strings of |
|
|
|
true -> |
|
|
|
alist_start(List, Max, dec_depth(Options)); |
|
|
|
_ -> |
|
|
|
{R, Len} = list_body(List, Max, dec_depth(Options), false), |
|
|
|
{[$[, R, $]], Len + 2} |
|
|
|
end. |
|
|
|
|
|
|
|
%% Returns {List, Length} |
|
|
|
tuple_contents(Tuple, Max) -> |
|
|
|
tuple_contents(Tuple, Max, Options) -> |
|
|
|
L = tuple_to_list(Tuple), |
|
|
|
list_body(L, Max). |
|
|
|
list_body(L, Max, dec_depth(Options), true). |
|
|
|
|
|
|
|
%% Format the inside of a list, i.e. do not add a leading [ or trailing ]. |
|
|
|
%% Returns {List, Length} |
|
|
|
list_body([], _) -> {[], 0}; |
|
|
|
list_body(_, Max) when Max < 4 -> {"...", 3}; |
|
|
|
list_body([H|T], Max) -> |
|
|
|
{List, Len} = print(H, Max), |
|
|
|
{Final, FLen} = list_bodyc(T, Max - Len), |
|
|
|
list_body([], _Max, _Options, _Tuple) -> {[], 0}; |
|
|
|
list_body(_, Max, _Options, _Tuple) when Max < 4 -> {"...", 3}; |
|
|
|
list_body(_, _Max, #print_options{depth=0}, _Tuple) -> {"...", 3}; |
|
|
|
list_body([H|T], Max, Options, Tuple) -> |
|
|
|
{List, Len} = print(H, Max, Options), |
|
|
|
{Final, FLen} = list_bodyc(T, Max - Len, Options, Tuple), |
|
|
|
{[List|Final], FLen + Len}; |
|
|
|
list_body(X, Max) -> %% improper list |
|
|
|
{List, Len} = print(X, Max - 1), |
|
|
|
list_body(X, Max, Options, _Tuple) -> %% improper list |
|
|
|
{List, Len} = print(X, Max - 1, Options), |
|
|
|
{[$|,List], Len + 1}. |
|
|
|
|
|
|
|
list_bodyc([], _) -> {[], 0}; |
|
|
|
list_bodyc(_, Max) when Max < 4 -> {"...", 3}; |
|
|
|
list_bodyc([H|T], Max) -> |
|
|
|
{List, Len} = print(H, Max), |
|
|
|
{Final, FLen} = list_bodyc(T, Max - Len - 1), |
|
|
|
{[$,, List|Final], FLen + Len + 1}; |
|
|
|
list_bodyc(X,Max) -> %% improper list |
|
|
|
{List, Len} = print(X, Max - 1), |
|
|
|
list_bodyc([], _Max, _Options, _Tuple) -> {[], 0}; |
|
|
|
list_bodyc(_, Max, _Options, _Tuple) when Max < 4 -> {",...", 3}; |
|
|
|
list_bodyc([H|T], Max, #print_options{depth=Depth} = Options, Tuple) -> |
|
|
|
{List, Len} = print(H, Max, dec_depth(Options)), |
|
|
|
{Final, FLen} = list_bodyc(T, Max - Len - 1, Options, Tuple), |
|
|
|
Sep = case Depth == 1 andalso not Tuple of |
|
|
|
true -> $|; |
|
|
|
_ -> $, |
|
|
|
end, |
|
|
|
{[Sep, List|Final], FLen + Len + 1}; |
|
|
|
list_bodyc(X, Max, Options, _Tuple) -> %% improper list |
|
|
|
{List, Len} = print(X, Max - 1, Options), |
|
|
|
{[$|,List], Len + 1}. |
|
|
|
|
|
|
|
%% The head of a list we hope is ascii. Examples: |
|
|
@ -309,39 +236,55 @@ list_bodyc(X,Max) -> %% improper list |
|
|
|
%% [0,65,66] -> [0,65,66] |
|
|
|
%% [65,b,66] -> "A"[b,66] |
|
|
|
%% |
|
|
|
alist_start([], _) -> {"[]", 2}; |
|
|
|
alist_start(_, Max) when Max < 4 -> {"...", 3}; |
|
|
|
alist_start([H|T], Max) when is_integer(H), H >= 16#20, H =< 16#7e -> % definitely printable |
|
|
|
try alist([H|T], Max -1) of |
|
|
|
alist_start([], _Max, #print_options{force_strings=true}) -> {"", 0}; |
|
|
|
alist_start([], _Max, _Options) -> {"[]", 2}; |
|
|
|
alist_start(_, Max, _Options) when Max < 4 -> {"...", 3}; |
|
|
|
alist_start(_, _Max, #print_options{depth=0}) -> {"[...]", 3}; |
|
|
|
alist_start(L, Max, #print_options{force_strings=true} = Options) -> |
|
|
|
alist(L, Max, Options); |
|
|
|
alist_start([H|T], Max, Options) when is_integer(H), H >= 16#20, H =< 16#7e -> % definitely printable |
|
|
|
try alist([H|T], Max -1, Options) of |
|
|
|
{L, Len} -> |
|
|
|
{[$"|L], Len + 1} |
|
|
|
catch |
|
|
|
throw:unprintable -> |
|
|
|
{R, Len} = list_body([H|T], Max-2), |
|
|
|
{R, Len} = list_body([H|T], Max-2, Options, false), |
|
|
|
{[$[, R, $]], Len + 2} |
|
|
|
end; |
|
|
|
alist_start([H|T], Max) when H =:= 9; H =:= 10; H =:= 13 -> |
|
|
|
try alist([H|T], Max -1) of |
|
|
|
alist_start([H|T], Max, Options) when H =:= 9; H =:= 10; H =:= 13 -> |
|
|
|
try alist([H|T], Max -1, Options) of |
|
|
|
{L, Len} -> |
|
|
|
{[$"|L], Len + 1} |
|
|
|
catch |
|
|
|
throw:unprintable -> |
|
|
|
{R, Len} = list_body([H|T], Max-2), |
|
|
|
{R, Len} = list_body([H|T], Max-2, Options, false), |
|
|
|
{[$[, R, $]], Len + 2} |
|
|
|
end; |
|
|
|
alist_start(L, Max) -> |
|
|
|
{R, Len} = list_body(L, Max-2), |
|
|
|
alist_start(L, Max, Options) -> |
|
|
|
{R, Len} = list_body(L, Max-2, Options, false), |
|
|
|
{[$[, R, $]], Len + 2}. |
|
|
|
|
|
|
|
alist([], _) -> {"\"", 1}; |
|
|
|
alist(_, Max) when Max < 5 -> {"...\"", 4}; |
|
|
|
alist([H|T], Max) when is_integer(H), H >= 16#20, H =< 16#7e -> % definitely printable |
|
|
|
{L, Len} = alist(T, Max-1), |
|
|
|
alist([], _Max, #print_options{force_strings=true}) -> {"", 0}; |
|
|
|
alist([], _Max, _Options) -> {"\"", 1}; |
|
|
|
alist(_, Max, #print_options{force_strings=true}) when Max < 4 -> {"...", 3}; |
|
|
|
alist(_, Max, #print_options{force_strings=false}) when Max < 5 -> {"...\"", 4}; |
|
|
|
alist([H|T], Max, Options) when is_integer(H), H >= 16#20, H =< 16#7e -> % definitely printable |
|
|
|
{L, Len} = alist(T, Max-1, Options), |
|
|
|
{[H|L], Len + 1}; |
|
|
|
alist([H|T], Max) when H =:= 9; H =:= 10; H =:= 13 -> |
|
|
|
{L, Len} = alist(T, Max-1), |
|
|
|
alist([H|T], Max, Options) when H =:= 9; H =:= 10; H =:= 13 -> |
|
|
|
{L, Len} = alist(T, Max-1, Options), |
|
|
|
case Options#print_options.force_strings of |
|
|
|
true -> |
|
|
|
{[H|L], Len + 1}; |
|
|
|
_ -> |
|
|
|
{[escape(H)|L], Len + 1} |
|
|
|
end; |
|
|
|
alist([H|T], Max, #print_options{force_strings=true} = Options) when is_integer(H) -> |
|
|
|
{L, Len} = alist(T, Max-1, Options), |
|
|
|
{[H|L], Len + 1}; |
|
|
|
alist(_L, _Max) -> |
|
|
|
alist(_, _, #print_options{force_strings=true}) -> |
|
|
|
error(badarg); |
|
|
|
alist(_L, _Max, _Options) -> |
|
|
|
throw(unprintable). |
|
|
|
|
|
|
|
%% is the first character in the atom alphabetic & lowercase? |
|
|
@ -359,29 +302,23 @@ atom_needs_quoting([H|T]) when (H >= $a andalso H =< $z); |
|
|
|
atom_needs_quoting(_) -> |
|
|
|
true. |
|
|
|
|
|
|
|
unquote_string([$<, $<, $"|T] = Str) -> |
|
|
|
case string:substr(T, length(T) - 2) of |
|
|
|
"\">>" -> |
|
|
|
string:substr(T, 1, length(T) - 3); |
|
|
|
_ -> |
|
|
|
Str |
|
|
|
end; |
|
|
|
unquote_string([$"|_] = Str) -> |
|
|
|
case lists:last(Str) == $" of |
|
|
|
true -> |
|
|
|
string:strip(Str, both, $"); |
|
|
|
_ -> |
|
|
|
Str |
|
|
|
end; |
|
|
|
unquote_string([$'|_] = Str) -> |
|
|
|
case lists:last(Str) == $' of |
|
|
|
true -> |
|
|
|
string:strip(Str, both, $'); |
|
|
|
_ -> |
|
|
|
Str |
|
|
|
end; |
|
|
|
unquote_string(S) -> |
|
|
|
S. |
|
|
|
prepare_options([], Options) -> |
|
|
|
Options; |
|
|
|
prepare_options([{depth, Depth}|T], Options) when is_integer(Depth) -> |
|
|
|
prepare_options(T, Options#print_options{depth=Depth}); |
|
|
|
prepare_options([{lists_as_strings, Bool}|T], Options) when is_boolean(Bool) -> |
|
|
|
prepare_options(T, Options#print_options{lists_as_strings = Bool}); |
|
|
|
prepare_options([{force_strings, Bool}|T], Options) when is_boolean(Bool) -> |
|
|
|
prepare_options(T, Options#print_options{force_strings = Bool}). |
|
|
|
|
|
|
|
dec_depth(#print_options{depth=Depth} = Options) when Depth > 0 -> |
|
|
|
Options#print_options{depth=Depth-1}; |
|
|
|
dec_depth(Options) -> |
|
|
|
Options. |
|
|
|
|
|
|
|
escape(9) -> "\\t"; |
|
|
|
escape(10) -> "\\n"; |
|
|
|
escape(13) -> "\\r". |
|
|
|
|
|
|
|
-ifdef(TEST). |
|
|
|
%%-------------------- |
|
|
@ -444,20 +381,22 @@ perf1() -> |
|
|
|
format_test() -> |
|
|
|
%% simple format strings |
|
|
|
?assertEqual("foobar", lists:flatten(format("~s", [["foo", $b, $a, $r]], 50))), |
|
|
|
?assertEqual("\"foobar\"", lists:flatten(format("~p", [["foo", $b, $a, $r]], 50))), |
|
|
|
?assertEqual("\"foobar\"", lists:flatten(format("~P", [["foo", $b, $a, $r], 10], 50))), |
|
|
|
?assertEqual("[\"foo\",98,97,114]", lists:flatten(format("~w", [["foo", $b, $a, $r], 10], 50))), |
|
|
|
?assertEqual("[\"foo\",98,97,114]", lists:flatten(format("~p", [["foo", $b, $a, $r]], 50))), |
|
|
|
?assertEqual("[\"foo\",98,97,114]", lists:flatten(format("~P", [["foo", $b, $a, $r], 10], 50))), |
|
|
|
?assertEqual("[[102,111,111],98,97,114]", lists:flatten(format("~w", [["foo", $b, $a, $r]], 50))), |
|
|
|
|
|
|
|
%% complex ones |
|
|
|
?assertEqual("foobar", lists:flatten(format("~10s", [["foo", $b, $a, $r]], 50))), |
|
|
|
?assertEqual("\"foobar\"", lists:flatten(format("~10p", [["foo", $b, $a, $r]], 50))), |
|
|
|
?assertEqual("\"foobar\"", lists:flatten(format("~10P", [["foo", $b, $a, $r], 10], 50))), |
|
|
|
?assertEqual("[\"foo\",98,97,114]", lists:flatten(format("~10W", [["foo", $b, $a, $r], 10], 50))), |
|
|
|
?assertEqual(" foobar", lists:flatten(format("~10s", [["foo", $b, $a, $r]], 50))), |
|
|
|
?assertEqual(" [\"foo\",98,97,114]", lists:flatten(format("~22p", [["foo", $b, $a, $r]], 50))), |
|
|
|
?assertEqual(" [\"foo\",98,97,114]", lists:flatten(format("~22P", [["foo", $b, $a, $r], 10], 50))), |
|
|
|
?assertEqual("**********", lists:flatten(format("~10W", [["foo", $b, $a, $r], 10], 50))), |
|
|
|
?assertEqual("[[102,111,111],98,97,114]", lists:flatten(format("~25W", [["foo", $b, $a, $r], 10], 50))), |
|
|
|
ok. |
|
|
|
|
|
|
|
atom_quoting_test() -> |
|
|
|
?assertEqual("hello", lists:flatten(format("~p", [hello], 50))), |
|
|
|
?assertEqual("'hello world'", lists:flatten(format("~p", ['hello world'], 50))), |
|
|
|
?assertEqual("'Hello world'", lists:flatten(format("~p", ['Hello world'], 50))), |
|
|
|
?assertEqual("hello_world", lists:flatten(format("~p", ['hello_world'], 50))), |
|
|
|
?assertEqual("'node@127.0.0.1'", lists:flatten(format("~p", ['node@127.0.0.1'], 50))), |
|
|
|
?assertEqual("node@nohost", lists:flatten(format("~p", [node@nohost], 50))), |
|
|
@ -473,7 +412,7 @@ sane_float_printing_test() -> |
|
|
|
|
|
|
|
float_inside_list_test() -> |
|
|
|
?assertEqual("[97,38.233913133184835,99]", lists:flatten(format("~p", [[$a, 38.233913133184835, $c]], 50))), |
|
|
|
?assertEqual("[97,38.233913133184835,99]", lists:flatten(format("~s", [[$a, 38.233913133184835, $c]], 50))), |
|
|
|
?assertError(badarg, lists:flatten(format("~s", [[$a, 38.233913133184835, $c]], 50))), |
|
|
|
ok. |
|
|
|
|
|
|
|
quote_strip_test() -> |
|
|
@ -486,23 +425,77 @@ quote_strip_test() -> |
|
|
|
ok. |
|
|
|
|
|
|
|
binary_printing_test() -> |
|
|
|
?assertEqual("<<>>", lists:flatten(format("~p", [<<>>], 50))), |
|
|
|
?assertEqual("<<..>>", lists:flatten(format("~p", [<<"hi">>], 0))), |
|
|
|
?assertEqual("<<\"hello\">>", lists:flatten(format("~p", [<<$h, $e, $l, $l, $o>>], 50))), |
|
|
|
?assertEqual("<<\"hello\">>", lists:flatten(format("~p", [<<"hello">>], 50))), |
|
|
|
?assertEqual("<<104,101,108,108,111>>", lists:flatten(format("~w", [<<"hello">>], 50))), |
|
|
|
?assertEqual("<<1,2,3,4>>", lists:flatten(format("~p", [<<1, 2, 3, 4>>], 50))), |
|
|
|
?assertEqual("<<1,2,3,4>>", lists:flatten(format("~s", [<<1, 2, 3, 4>>], 50))), |
|
|
|
?assertEqual([1,2,3,4], lists:flatten(format("~s", [<<1, 2, 3, 4>>], 50))), |
|
|
|
?assertEqual("hello", lists:flatten(format("~s", [<<"hello">>], 50))), |
|
|
|
?assertEqual("hello", lists:flatten(format("~10s", [<<"hello">>], 50))), |
|
|
|
?assertEqual("hello\nworld", lists:flatten(format("~s", [<<"hello\nworld">>], 50))), |
|
|
|
?assertEqual("<<\"hello\\nworld\">>", lists:flatten(format("~p", [<<"hello\nworld">>], 50))), |
|
|
|
?assertEqual(" hello", lists:flatten(format("~10s", [<<"hello">>], 50))), |
|
|
|
ok. |
|
|
|
|
|
|
|
list_printing_test() -> |
|
|
|
?assertEqual("[]", lists:flatten(format("~p", [[]], 50))), |
|
|
|
?assertEqual("[]", lists:flatten(format("~w", [[]], 50))), |
|
|
|
?assertEqual("", lists:flatten(format("~s", [[]], 50))), |
|
|
|
?assertEqual("...", lists:flatten(format("~s", [[]], -1))), |
|
|
|
?assertEqual("[[]]", lists:flatten(format("~p", [[[]]], 50))), |
|
|
|
?assertEqual("[13,11,10,8,5,4]", lists:flatten(format("~p", [[13,11,10,8,5,4]], 50))), |
|
|
|
?assertEqual("\"\\rabc\"", lists:flatten(format("~p", [[13,$a, $b, $c]], 50))), |
|
|
|
?assertEqual("[1,2,3|4]", lists:flatten(format("~p", [[1, 2, 3|4]], 50))), |
|
|
|
?assertEqual("[...]", lists:flatten(format("~p", [[1, 2, 3,4]], 4))), |
|
|
|
?assertEqual("[1,...]", lists:flatten(format("~p", [[1, 2, 3,4]], 6))), |
|
|
|
?assertEqual("[1|4]", lists:flatten(format("~p", [[1|4]], 50))), |
|
|
|
?assertEqual("[1]", lists:flatten(format("~p", [[1]], 50))), |
|
|
|
?assertError(badarg, lists:flatten(format("~s", [[1|4]], 50))), |
|
|
|
?assertEqual("\"hello...\"", lists:flatten(format("~p", ["hello world"], 10))), |
|
|
|
?assertEqual("hello...", lists:flatten(format("~s", ["hello world"], 10))), |
|
|
|
?assertEqual("hello w...", lists:flatten(format("~s", ["hello world"], 10))), |
|
|
|
?assertEqual("hello world\r\n", lists:flatten(format("~s", ["hello world\r\n"], 50))), |
|
|
|
?assertEqual("\rhello world\r\n", lists:flatten(format("~s", ["\rhello world\r\n"], 50))), |
|
|
|
?assertEqual("\"\\rhello world\\r\\n\"", lists:flatten(format("~p", ["\rhello world\r\n"], 50))), |
|
|
|
?assertEqual("[13,104,101,108,108,111,32,119,111,114,108,100,13,10]", lists:flatten(format("~w", ["\rhello world\r\n"], 60))), |
|
|
|
?assertEqual("...", lists:flatten(format("~s", ["\rhello world\r\n"], 3))), |
|
|
|
?assertEqual("[]", lists:flatten(format("~s", [[]], 50))), |
|
|
|
ok. |
|
|
|
|
|
|
|
tuple_printing_test() -> |
|
|
|
?assertEqual("{}", lists:flatten(format("~p", [{}], 50))), |
|
|
|
?assertEqual("{}", lists:flatten(format("~w", [{}], 50))), |
|
|
|
?assertError(badarg, lists:flatten(format("~s", [{}], 50))), |
|
|
|
?assertEqual("{...}", lists:flatten(format("~p", [{foo}], 3))), |
|
|
|
?assertEqual("{foo,...}", lists:flatten(format("~p", [{foo,bar}], 6))), |
|
|
|
?assertEqual("{foo,bar}", lists:flatten(format("~p", [{foo,bar}], 9))), |
|
|
|
ok. |
|
|
|
|
|
|
|
unicode_test() -> |
|
|
|
?assertEqual([231,167,129], lists:flatten(format("~s", [<<231,167,129>>], 50))), |
|
|
|
?assertEqual([31169], lists:flatten(format("~ts", [<<231,167,129>>], 50))), |
|
|
|
ok. |
|
|
|
|
|
|
|
depth_limit_test() -> |
|
|
|
?assertEqual("{...}", lists:flatten(format("~P", [{a, [b, [c, [d]]]}, 1], 50))), |
|
|
|
?assertEqual("{a,...}", lists:flatten(format("~P", [{a, [b, [c, [d]]]}, 2], 50))), |
|
|
|
?assertEqual("{a,[...]}", lists:flatten(format("~P", [{a, [b, [c, [d]]]}, 3], 50))), |
|
|
|
?assertEqual("{a,[b|...]}", lists:flatten(format("~P", [{a, [b, [c, [d]]]}, 4], 50))), |
|
|
|
?assertEqual("{a,[b,[...]]}", lists:flatten(format("~P", [{a, [b, [c, [d]]]}, 5], 50))), |
|
|
|
?assertEqual("{a,[b,[c|...]]}", lists:flatten(format("~P", [{a, [b, [c, [d]]]}, 6], 50))), |
|
|
|
?assertEqual("{a,[b,[c,[...]]]}", lists:flatten(format("~P", [{a, [b, [c, [d]]]}, 7], 50))), |
|
|
|
?assertEqual("{a,[b,[c,[d]]]}", lists:flatten(format("~P", [{a, [b, [c, [d]]]}, 8], 50))), |
|
|
|
?assertEqual("{a,[b,[c,[d]]]}", lists:flatten(format("~P", [{a, [b, [c, [d]]]}, 9], 50))), |
|
|
|
|
|
|
|
?assertEqual("{a,{...}}", lists:flatten(format("~P", [{a, {b, {c, {d}}}}, 3], 50))), |
|
|
|
?assertEqual("{a,{b,...}}", lists:flatten(format("~P", [{a, {b, {c, {d}}}}, 4], 50))), |
|
|
|
?assertEqual("{a,{b,{...}}}", lists:flatten(format("~P", [{a, {b, {c, {d}}}}, 5], 50))), |
|
|
|
?assertEqual("{a,{b,{c,...}}}", lists:flatten(format("~P", [{a, {b, {c, {d}}}}, 6], 50))), |
|
|
|
?assertEqual("{a,{b,{c,{...}}}}", lists:flatten(format("~P", [{a, {b, {c, {d}}}}, 7], 50))), |
|
|
|
?assertEqual("{a,{b,{c,{d}}}}", lists:flatten(format("~P", [{a, {b, {c, {d}}}}, 8], 50))), |
|
|
|
|
|
|
|
?assertEqual("{\"a\",[...]}", lists:flatten(format("~P", [{"a", ["b", ["c", ["d"]]]}, 3], 50))), |
|
|
|
?assertEqual("{\"a\",[\"b\",[[...]|...]]}", lists:flatten(format("~P", [{"a", ["b", ["c", ["d"]]]}, 6], 50))), |
|
|
|
?assertEqual("{\"a\",[\"b\",[\"c\",[\"d\"]]]}", lists:flatten(format("~P", [{"a", ["b", ["c", ["d"]]]}, 9], 50))), |
|
|
|
ok. |
|
|
|
|
|
|
|
-endif. |