Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 601 lines (537 sloc) 26.566 kb
5bc97a9 Andrew Thompson Forgot to add trunc_io
Vagabond authored
1 %% ``The contents of this file are subject to the Erlang Public License,
2 %% Version 1.1, (the "License"); you may not use this file except in
3 %% compliance with the License. You should have received a copy of the
4 %% Erlang Public License along with your Erlang distribution. If not, it can be
5 %% retrieved via the world wide web at http://www.erlang.org/.
6 %%
7 %% Software distributed under the License is distributed on an "AS IS"
8 %% basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
9 %% the License for the specific language governing rights and limitations
10 %% under the License.
11 %%
12 %% The Initial Developer of the Original Code is Corelatus AB.
13 %% Portions created by Corelatus are Copyright 2003, Corelatus
14 %% AB. All Rights Reserved.''
15 %%
16 %% @doc Module to print out terms for logging. Limits by length rather than depth.
17 %%
18 %% The resulting string may be slightly larger than the limit; the intention
19 %% is to provide predictable CPU and memory consumption for formatting
20 %% terms, not produce precise string lengths.
21 %%
22 %% Typical use:
23 %%
24 %% trunc_io:print(Term, 500).
25 %%
26 %% Source license: Erlang Public License.
27 %% Original author: Matthias Lang, <tt>matthias@corelatus.se</tt>
4ac0137 Andrew Thompson Rename trunc_io to lager_trunc_io to prevent clashes
Vagabond authored
28 %%
29 %% Various changes to this module, most notably the format/3 implementation
6a8403f Andrew Thompson Fix edoc generation
Vagabond authored
30 %% were added by Andrew Thompson `<andrew@basho.com>'. The module has been renamed
4ac0137 Andrew Thompson Rename trunc_io to lager_trunc_io to prevent clashes
Vagabond authored
31 %% to avoid conflicts with the vanilla module.
5bc97a9 Andrew Thompson Forgot to add trunc_io
Vagabond authored
32
4ac0137 Andrew Thompson Rename trunc_io to lager_trunc_io to prevent clashes
Vagabond authored
33 -module(lager_trunc_io).
5bc97a9 Andrew Thompson Forgot to add trunc_io
Vagabond authored
34 -author('matthias@corelatus.se').
35 %% And thanks to Chris Newcombe for a bug fix
3311702 Andrew Thompson Add builtin chomping to lager_format
Vagabond authored
36 -export([format/3, format/4, print/2, print/3, fprint/2, fprint/3, safe/2]). % interface functions
5bc97a9 Andrew Thompson Forgot to add trunc_io
Vagabond authored
37 -version("$Id: trunc_io.erl,v 1.11 2009-02-23 12:01:06 matthias Exp $").
38
03d3fa1 Andrew Thompson Fixes and tests for deep-list handling in trunc_io:format
Vagabond authored
39 -ifdef(TEST).
40 -export([perf/0, perf/3, perf1/0, test/0, test/2]). % testing functions
41 -include_lib("eunit/include/eunit.hrl").
42 -endif.
43
e9c4d77 Kostis Sagonas Fix specs so that they correspond to the code
kostis authored
44 -type option() :: {'depth', integer()}
45 | {'lists_as_strings', boolean()}
46 | {'force_strings', boolean()}.
47 -type options() :: [option()].
48
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
49 -record(print_options, {
50 %% negative depth means no depth limiting
51 depth = -1 :: integer(),
52 %% whether to print lists as strings, if possible
31d9529 Andrew Thompson Default to treating lists as strings, (and set it false for ~w)
Vagabond authored
53 lists_as_strings = true :: boolean(),
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
54 %% force strings, or binaries to be printed as a string,
55 %% even if they're not printable
56 force_strings = false :: boolean()
57 }).
58
e45d5bc Andrew Thompson Implement an alternative and much faster lager_trunc_io:format/3
Vagabond authored
59 format(Fmt, Args, Max) ->
3311702 Andrew Thompson Add builtin chomping to lager_format
Vagabond authored
60 format(Fmt, Args, Max, []).
61
62 format(Fmt, Args, Max, Options) ->
efc7b75 Andrew Thompson Changes suggested by Kostis, Dialyzer -Wunmatched_returns and Tidier
Vagabond authored
63 try lager_format:format(Fmt, Args, Max, Options)
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
64 catch
d416dc0 Andrew Thompson Fix escaped char printing in ~p mode, reported by @etrepum
Vagabond authored
65 _What:_Why ->
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
66 erlang:error(badarg, [Fmt, Args])
67 end.
37f190f Andrew Thompson Try to give more equal allocations to large terms
Vagabond authored
68
5bc97a9 Andrew Thompson Forgot to add trunc_io
Vagabond authored
69 %% @doc Returns an flattened list containing the ASCII representation of the given
70 %% term.
71 -spec fprint(term(), pos_integer()) -> string().
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
72 fprint(Term, Max) ->
73 fprint(Term, Max, []).
74
75
76 %% @doc Returns an flattened list containing the ASCII representation of the given
77 %% term.
e9c4d77 Kostis Sagonas Fix specs so that they correspond to the code
kostis authored
78 -spec fprint(term(), pos_integer(), options()) -> string().
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
79 fprint(T, Max, Options) ->
80 {L, _} = print(T, Max, prepare_options(Options, #print_options{})),
5bc97a9 Andrew Thompson Forgot to add trunc_io
Vagabond authored
81 lists:flatten(L).
82
83 %% @doc Same as print, but never crashes.
84 %%
85 %% This is a tradeoff. Print might conceivably crash if it's asked to
86 %% print something it doesn't understand, for example some new data
87 %% type in a future version of Erlang. If print crashes, we fall back
88 %% to io_lib to format the term, but then the formatting is
89 %% depth-limited instead of length limited, so you might run out
90 %% memory printing it. Out of the frying pan and into the fire.
91 %%
92 -spec safe(term(), pos_integer()) -> {string(), pos_integer()} | {string()}.
93 safe(What, Len) ->
94 case catch print(What, Len) of
95 {L, Used} when is_list(L) -> {L, Used};
96 _ -> {"unable to print" ++ io_lib:write(What, 99)}
97 end.
98
99 %% @doc Returns {List, Length}
100 -spec print(term(), pos_integer()) -> {iolist(), pos_integer()}.
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
101 print(Term, Max) ->
102 print(Term, Max, []).
103
104 %% @doc Returns {List, Length}
e9c4d77 Kostis Sagonas Fix specs so that they correspond to the code
kostis authored
105 -spec print(term(), pos_integer(), options() | #print_options{}) -> {iolist(), pos_integer()}.
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
106 print(Term, Max, Options) when is_list(Options) ->
107 %% need to convert the proplist to a record
108 print(Term, Max, prepare_options(Options, #print_options{}));
9e735a7 Andrew Thompson Improve tests, fix some minor bugs
Vagabond authored
109
110 print(Term, _Max, #print_options{force_strings=true}) when not is_list(Term), not is_binary(Term), not is_atom(Term) ->
111 erlang:error(badarg);
112
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
113 print(_, Max, _Options) when Max < 0 -> {"...", 3};
d16dfaa Andrew Thompson Implement ~P/~W style depth-limiting
Vagabond authored
114 print(_, _, #print_options{depth=0}) -> {"...", 3};
9e735a7 Andrew Thompson Improve tests, fix some minor bugs
Vagabond authored
115
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
116 print(Tuple, Max, Options) when is_tuple(Tuple) ->
117 {TC, Len} = tuple_contents(Tuple, Max-2, Options),
5bc97a9 Andrew Thompson Forgot to add trunc_io
Vagabond authored
118 {[${, TC, $}], Len + 2};
119
120 %% @doc We assume atoms, floats, funs, integers, PIDs, ports and refs never need
121 %% to be truncated. This isn't strictly true, someone could make an
122 %% arbitrarily long bignum. Let's assume that won't happen unless someone
123 %% is being malicious.
124 %%
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
125 print(Atom, _Max, #print_options{force_strings=NoQuote}) when is_atom(Atom) ->
5bc97a9 Andrew Thompson Forgot to add trunc_io
Vagabond authored
126 L = atom_to_list(Atom),
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
127 R = case atom_needs_quoting_start(L) andalso not NoQuote of
3babca1 Andrew Thompson Make sure atoms needing quoting are quoted.
Vagabond authored
128 true -> lists:flatten([$', L, $']);
129 false -> L
130 end,
131 {R, length(R)};
5bc97a9 Andrew Thompson Forgot to add trunc_io
Vagabond authored
132
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
133 print(<<>>, _Max, _Options) ->
5bc97a9 Andrew Thompson Forgot to add trunc_io
Vagabond authored
134 {"<<>>", 4};
135
6b7f1dd Andrew Thompson Support for printing bitstrings (reported by @freeaakk)
Vagabond authored
136 print(Binary, 0, _Options) when is_bitstring(Binary) ->
71fae1b Andrew Thompson Truncator for funs; special case binary truncation when max is 0
Vagabond authored
137 {"<<..>>", 6};
138
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
139 print(Binary, Max, Options) when is_binary(Binary) ->
6b7f1dd Andrew Thompson Support for printing bitstrings (reported by @freeaakk)
Vagabond authored
140 B = binary_to_list(Binary, 1, lists:min([Max, byte_size(Binary)])),
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
141 {L, Len} = case Options#print_options.lists_as_strings orelse
142 Options#print_options.force_strings of
143 true ->
144 alist_start(B, Max-4, Options);
145 _ ->
d16dfaa Andrew Thompson Implement ~P/~W style depth-limiting
Vagabond authored
146 list_body(B, Max-4, Options, false)
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
147 end,
7e9da64 Andrew Thompson Several fixes for printing binaries, improve quote stripping
Vagabond authored
148 {Res, Length} = case L of
149 [91, X, 93] ->
150 {X, Len - 2};
151 X ->
152 {X, Len}
153 end,
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
154 case Options#print_options.force_strings of
155 true ->
156 {Res, Length};
157 _ ->
158 {["<<", Res, ">>"], Length+4}
159 end;
5bc97a9 Andrew Thompson Forgot to add trunc_io
Vagabond authored
160
6b7f1dd Andrew Thompson Support for printing bitstrings (reported by @freeaakk)
Vagabond authored
161 %% bitstrings are binary's evil brother who doesn't end on an 8 bit boundary.
162 %% This makes printing them extremely annoying, so list_body/list_bodyc has
163 %% some magic for dealing with the output of bitstring_to_list, which returns
164 %% a list of integers (as expected) but with a trailing binary that represents
165 %% the remaining bits.
166 print(BitString, Max, Options) when is_bitstring(BitString) ->
167 case byte_size(BitString) > Max of
168 true ->
169 BL = binary_to_list(BitString, 1, Max);
170 _ ->
171 BL = erlang:bitstring_to_list(BitString)
172 end,
173 {X, Len0} = list_body(BL, Max - 4, Options, false),
174 {["<<", X, ">>"], Len0 + 4};
175
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
176 print(Float, _Max, _Options) when is_float(Float) ->
9a577df Andrew Thompson Use the io_lib_format:fwrite_g function to print floats
Vagabond authored
177 %% use the same function io_lib:format uses to print floats
178 %% float_to_list is way too verbose.
179 L = io_lib_format:fwrite_g(Float),
5bc97a9 Andrew Thompson Forgot to add trunc_io
Vagabond authored
180 {L, length(L)};
181
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
182 print(Fun, Max, _Options) when is_function(Fun) ->
5bc97a9 Andrew Thompson Forgot to add trunc_io
Vagabond authored
183 L = erlang:fun_to_list(Fun),
71fae1b Andrew Thompson Truncator for funs; special case binary truncation when max is 0
Vagabond authored
184 case length(L) > Max of
185 true ->
186 S = erlang:max(5, Max),
187 Res = string:substr(L, 1, S) ++ "..>",
188 {Res, length(Res)};
189 _ ->
190 {L, length(L)}
191 end;
5bc97a9 Andrew Thompson Forgot to add trunc_io
Vagabond authored
192
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
193 print(Integer, _Max, _Options) when is_integer(Integer) ->
5bc97a9 Andrew Thompson Forgot to add trunc_io
Vagabond authored
194 L = integer_to_list(Integer),
195 {L, length(L)};
196
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
197 print(Pid, _Max, _Options) when is_pid(Pid) ->
5bc97a9 Andrew Thompson Forgot to add trunc_io
Vagabond authored
198 L = pid_to_list(Pid),
199 {L, length(L)};
200
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
201 print(Ref, _Max, _Options) when is_reference(Ref) ->
5bc97a9 Andrew Thompson Forgot to add trunc_io
Vagabond authored
202 L = erlang:ref_to_list(Ref),
203 {L, length(L)};
204
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
205 print(Port, _Max, _Options) when is_port(Port) ->
5bc97a9 Andrew Thompson Forgot to add trunc_io
Vagabond authored
206 L = erlang:port_to_list(Port),
207 {L, length(L)};
208
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
209 print(List, Max, Options) when is_list(List) ->
9c6ce62 Andrew Thompson Fix some more formatting bugs
Vagabond authored
210 case Options#print_options.lists_as_strings orelse
211 Options#print_options.force_strings of
212 true ->
213 alist_start(List, Max, dec_depth(Options));
214 _ ->
5068e29 Andrew Thompson Fix a couple minor issues with length calculation, add tests
Vagabond authored
215 {R, Len} = list_body(List, Max - 2, dec_depth(Options), false),
9c6ce62 Andrew Thompson Fix some more formatting bugs
Vagabond authored
216 {[$[, R, $]], Len + 2}
217 end.
5bc97a9 Andrew Thompson Forgot to add trunc_io
Vagabond authored
218
219 %% Returns {List, Length}
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
220 tuple_contents(Tuple, Max, Options) ->
5bc97a9 Andrew Thompson Forgot to add trunc_io
Vagabond authored
221 L = tuple_to_list(Tuple),
d16dfaa Andrew Thompson Implement ~P/~W style depth-limiting
Vagabond authored
222 list_body(L, Max, dec_depth(Options), true).
5bc97a9 Andrew Thompson Forgot to add trunc_io
Vagabond authored
223
224 %% Format the inside of a list, i.e. do not add a leading [ or trailing ].
225 %% Returns {List, Length}
d16dfaa Andrew Thompson Implement ~P/~W style depth-limiting
Vagabond authored
226 list_body([], _Max, _Options, _Tuple) -> {[], 0};
227 list_body(_, Max, _Options, _Tuple) when Max < 4 -> {"...", 3};
228 list_body(_, _Max, #print_options{depth=0}, _Tuple) -> {"...", 3};
79b8fd1 Andrew Thompson Don't break printing lists of binaries
Vagabond authored
229 list_body([B], _Max, _Options, _Tuple) when is_bitstring(B), not is_binary(B) ->
6b7f1dd Andrew Thompson Support for printing bitstrings (reported by @freeaakk)
Vagabond authored
230 Size = bit_size(B),
231 <<Value:Size>> = B,
232 ValueStr = integer_to_list(Value),
233 SizeStr = integer_to_list(Size),
234 {[ValueStr, $:, SizeStr], length(ValueStr) + length(SizeStr) +1};
d16dfaa Andrew Thompson Implement ~P/~W style depth-limiting
Vagabond authored
235 list_body([H|T], Max, Options, Tuple) ->
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
236 {List, Len} = print(H, Max, Options),
d16dfaa Andrew Thompson Implement ~P/~W style depth-limiting
Vagabond authored
237 {Final, FLen} = list_bodyc(T, Max - Len, Options, Tuple),
5bc97a9 Andrew Thompson Forgot to add trunc_io
Vagabond authored
238 {[List|Final], FLen + Len};
d16dfaa Andrew Thompson Implement ~P/~W style depth-limiting
Vagabond authored
239 list_body(X, Max, Options, _Tuple) -> %% improper list
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
240 {List, Len} = print(X, Max - 1, Options),
5bc97a9 Andrew Thompson Forgot to add trunc_io
Vagabond authored
241 {[$|,List], Len + 1}.
242
d16dfaa Andrew Thompson Implement ~P/~W style depth-limiting
Vagabond authored
243 list_bodyc([], _Max, _Options, _Tuple) -> {[], 0};
5068e29 Andrew Thompson Fix a couple minor issues with length calculation, add tests
Vagabond authored
244 list_bodyc(_, Max, _Options, _Tuple) when Max < 5 -> {",...", 4};
79b8fd1 Andrew Thompson Don't break printing lists of binaries
Vagabond authored
245 list_bodyc([B], _Max, _Options, _Tuple) when is_bitstring(B), not is_binary(B) ->
6b7f1dd Andrew Thompson Support for printing bitstrings (reported by @freeaakk)
Vagabond authored
246 Size = bit_size(B),
247 <<Value:Size>> = B,
248 ValueStr = integer_to_list(Value),
249 SizeStr = integer_to_list(Size),
250 {[$, , ValueStr, $:, SizeStr], length(ValueStr) + length(SizeStr) +2};
d16dfaa Andrew Thompson Implement ~P/~W style depth-limiting
Vagabond authored
251 list_bodyc([H|T], Max, #print_options{depth=Depth} = Options, Tuple) ->
252 {List, Len} = print(H, Max, dec_depth(Options)),
253 {Final, FLen} = list_bodyc(T, Max - Len - 1, Options, Tuple),
254 Sep = case Depth == 1 andalso not Tuple of
255 true -> $|;
256 _ -> $,
257 end,
258 {[Sep, List|Final], FLen + Len + 1};
259 list_bodyc(X, Max, Options, _Tuple) -> %% improper list
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
260 {List, Len} = print(X, Max - 1, Options),
5bc97a9 Andrew Thompson Forgot to add trunc_io
Vagabond authored
261 {[$|,List], Len + 1}.
262
263 %% The head of a list we hope is ascii. Examples:
264 %%
265 %% [65,66,67] -> "ABC"
266 %% [65,0,67] -> "A"[0,67]
267 %% [0,65,66] -> [0,65,66]
268 %% [65,b,66] -> "A"[b,66]
269 %%
9e735a7 Andrew Thompson Improve tests, fix some minor bugs
Vagabond authored
270 alist_start([], _Max, #print_options{force_strings=true}) -> {"", 0};
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
271 alist_start([], _Max, _Options) -> {"[]", 2};
272 alist_start(_, Max, _Options) when Max < 4 -> {"...", 3};
5068e29 Andrew Thompson Fix a couple minor issues with length calculation, add tests
Vagabond authored
273 alist_start(_, _Max, #print_options{depth=0}) -> {"[...]", 5};
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
274 alist_start(L, Max, #print_options{force_strings=true} = Options) ->
275 alist(L, Max, Options);
276 alist_start([H|T], Max, Options) when is_integer(H), H >= 16#20, H =< 16#7e -> % definitely printable
277 try alist([H|T], Max -1, Options) of
5140cb3 Andrew Thompson Don't convert \r, etc to spaces, don't assume printablility by first cha...
Vagabond authored
278 {L, Len} ->
279 {[$"|L], Len + 1}
280 catch
281 throw:unprintable ->
d16dfaa Andrew Thompson Implement ~P/~W style depth-limiting
Vagabond authored
282 {R, Len} = list_body([H|T], Max-2, Options, false),
5140cb3 Andrew Thompson Don't convert \r, etc to spaces, don't assume printablility by first cha...
Vagabond authored
283 {[$[, R, $]], Len + 2}
284 end;
d416dc0 Andrew Thompson Fix escaped char printing in ~p mode, reported by @etrepum
Vagabond authored
285 alist_start([H|T], Max, Options) when H =:= $\t; H =:= $\n; H =:= $\r; H =:= $\v; H =:= $\e; H=:= $\f; H=:= $\b ->
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
286 try alist([H|T], Max -1, Options) of
5140cb3 Andrew Thompson Don't convert \r, etc to spaces, don't assume printablility by first cha...
Vagabond authored
287 {L, Len} ->
288 {[$"|L], Len + 1}
289 catch
290 throw:unprintable ->
d16dfaa Andrew Thompson Implement ~P/~W style depth-limiting
Vagabond authored
291 {R, Len} = list_body([H|T], Max-2, Options, false),
5140cb3 Andrew Thompson Don't convert \r, etc to spaces, don't assume printablility by first cha...
Vagabond authored
292 {[$[, R, $]], Len + 2}
293 end;
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
294 alist_start(L, Max, Options) ->
d16dfaa Andrew Thompson Implement ~P/~W style depth-limiting
Vagabond authored
295 {R, Len} = list_body(L, Max-2, Options, false),
5bc97a9 Andrew Thompson Forgot to add trunc_io
Vagabond authored
296 {[$[, R, $]], Len + 2}.
297
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
298 alist([], _Max, #print_options{force_strings=true}) -> {"", 0};
299 alist([], _Max, _Options) -> {"\"", 1};
300 alist(_, Max, #print_options{force_strings=true}) when Max < 4 -> {"...", 3};
301 alist(_, Max, #print_options{force_strings=false}) when Max < 5 -> {"...\"", 4};
d416dc0 Andrew Thompson Fix escaped char printing in ~p mode, reported by @etrepum
Vagabond authored
302 alist([H|T], Max, Options = #print_options{force_strings=false,lists_as_strings=true}) when H =:= $"; H =:= $\\ ->
303 %% preserve escaping around quotes
304 {L, Len} = alist(T, Max-1, Options),
305 {[$\\,H|L], Len + 2};
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
306 alist([H|T], Max, Options) when is_integer(H), H >= 16#20, H =< 16#7e -> % definitely printable
307 {L, Len} = alist(T, Max-1, Options),
308 {[H|L], Len + 1};
d416dc0 Andrew Thompson Fix escaped char printing in ~p mode, reported by @etrepum
Vagabond authored
309 alist([H|T], Max, Options) when H =:= $\t; H =:= $\n; H =:= $\r; H =:= $\v; H =:= $\e; H=:= $\f; H=:= $\b ->
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
310 {L, Len} = alist(T, Max-1, Options),
9c6ce62 Andrew Thompson Fix some more formatting bugs
Vagabond authored
311 case Options#print_options.force_strings of
312 true ->
313 {[H|L], Len + 1};
314 _ ->
315 {[escape(H)|L], Len + 1}
316 end;
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
317 alist([H|T], Max, #print_options{force_strings=true} = Options) when is_integer(H) ->
318 {L, Len} = alist(T, Max-1, Options),
5140cb3 Andrew Thompson Don't convert \r, etc to spaces, don't assume printablility by first cha...
Vagabond authored
319 {[H|L], Len + 1};
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
320 alist(_, _, #print_options{force_strings=true}) ->
446ca91 Brendan Hay Prefixing alist/3 error to make it compile on R13A
brendanhay authored
321 erlang:error(badarg);
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
322 alist(_L, _Max, _Options) ->
5140cb3 Andrew Thompson Don't convert \r, etc to spaces, don't assume printablility by first cha...
Vagabond authored
323 throw(unprintable).
5bc97a9 Andrew Thompson Forgot to add trunc_io
Vagabond authored
324
3babca1 Andrew Thompson Make sure atoms needing quoting are quoted.
Vagabond authored
325 %% is the first character in the atom alphabetic & lowercase?
326 atom_needs_quoting_start([H|T]) when H >= $a, H =< $z ->
327 atom_needs_quoting(T);
328 atom_needs_quoting_start(_) ->
329 true.
330
331 atom_needs_quoting([]) ->
332 false;
333 atom_needs_quoting([H|T]) when (H >= $a andalso H =< $z);
334 (H >= $A andalso H =< $Z);
c2b5f9f Bryan Fink don't quote atoms just for containing numerals
beerriot authored
335 (H >= $0 andalso H =< $9);
3babca1 Andrew Thompson Make sure atoms needing quoting are quoted.
Vagabond authored
336 H == $@; H == $_ ->
337 atom_needs_quoting(T);
338 atom_needs_quoting(_) ->
339 true.
5bc97a9 Andrew Thompson Forgot to add trunc_io
Vagabond authored
340
e9c4d77 Kostis Sagonas Fix specs so that they correspond to the code
kostis authored
341 -spec prepare_options(options(), #print_options{}) -> #print_options{}.
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
342 prepare_options([], Options) ->
343 Options;
344 prepare_options([{depth, Depth}|T], Options) when is_integer(Depth) ->
345 prepare_options(T, Options#print_options{depth=Depth});
346 prepare_options([{lists_as_strings, Bool}|T], Options) when is_boolean(Bool) ->
347 prepare_options(T, Options#print_options{lists_as_strings = Bool});
348 prepare_options([{force_strings, Bool}|T], Options) when is_boolean(Bool) ->
349 prepare_options(T, Options#print_options{force_strings = Bool}).
350
d16dfaa Andrew Thompson Implement ~P/~W style depth-limiting
Vagabond authored
351 dec_depth(#print_options{depth=Depth} = Options) when Depth > 0 ->
352 Options#print_options{depth=Depth-1};
353 dec_depth(Options) ->
354 Options.
7e9da64 Andrew Thompson Several fixes for printing binaries, improve quote stripping
Vagabond authored
355
d416dc0 Andrew Thompson Fix escaped char printing in ~p mode, reported by @etrepum
Vagabond authored
356 escape($\t) -> "\\t";
357 escape($\n) -> "\\n";
358 escape($\r) -> "\\r";
359 escape($\e) -> "\\e";
360 escape($\f) -> "\\f";
f5ca930 Andrew Thompson Vertical tab support
Vagabond authored
361 escape($\b) -> "\\b";
362 escape($\v) -> "\\v".
9c6ce62 Andrew Thompson Fix some more formatting bugs
Vagabond authored
363
03d3fa1 Andrew Thompson Fixes and tests for deep-list handling in trunc_io:format
Vagabond authored
364 -ifdef(TEST).
5bc97a9 Andrew Thompson Forgot to add trunc_io
Vagabond authored
365 %%--------------------
366 %% The start of a test suite. So far, it only checks for not crashing.
367 -spec test() -> ok.
368 test() ->
369 test(trunc_io, print).
370
371 -spec test(atom(), atom()) -> ok.
372 test(Mod, Func) ->
373 Simple_items = [atom, 1234, 1234.0, {tuple}, [], [list], "string", self(),
374 <<1,2,3>>, make_ref(), fun() -> ok end],
375 F = fun(A) ->
376 Mod:Func(A, 100),
377 Mod:Func(A, 2),
378 Mod:Func(A, 20)
379 end,
380
381 G = fun(A) ->
382 case catch F(A) of
383 {'EXIT', _} -> exit({failed, A});
384 _ -> ok
385 end
386 end,
387
388 lists:foreach(G, Simple_items),
389
390 Tuples = [ {1,2,3,a,b,c}, {"abc", def, 1234},
391 {{{{a},b,c,{d},e}},f}],
392
393 Lists = [ [1,2,3,4,5,6,7], lists:seq(1,1000),
394 [{a}, {a,b}, {a, [b,c]}, "def"], [a|b], [$a|$b] ],
395
396
397 lists:foreach(G, Tuples),
398 lists:foreach(G, Lists).
399
400 -spec perf() -> ok.
401 perf() ->
402 {New, _} = timer:tc(trunc_io, perf, [trunc_io, print, 1000]),
403 {Old, _} = timer:tc(trunc_io, perf, [io_lib, write, 1000]),
404 io:fwrite("New code took ~p us, old code ~p\n", [New, Old]).
405
406 -spec perf(atom(), atom(), integer()) -> done.
407 perf(M, F, Reps) when Reps > 0 ->
408 test(M,F),
409 perf(M,F,Reps-1);
410 perf(_,_,_) ->
411 done.
412
413 %% Performance test. Needs a particularly large term I saved as a binary...
414 -spec perf1() -> {non_neg_integer(), non_neg_integer()}.
415 perf1() ->
416 {ok, Bin} = file:read_file("bin"),
417 A = binary_to_term(Bin),
418 {N, _} = timer:tc(trunc_io, print, [A, 1500]),
419 {M, _} = timer:tc(io_lib, write, [A]),
420 {N, M}.
03d3fa1 Andrew Thompson Fixes and tests for deep-list handling in trunc_io:format
Vagabond authored
421
422 format_test() ->
423 %% simple format strings
424 ?assertEqual("foobar", lists:flatten(format("~s", [["foo", $b, $a, $r]], 50))),
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
425 ?assertEqual("[\"foo\",98,97,114]", lists:flatten(format("~p", [["foo", $b, $a, $r]], 50))),
426 ?assertEqual("[\"foo\",98,97,114]", lists:flatten(format("~P", [["foo", $b, $a, $r], 10], 50))),
9c6ce62 Andrew Thompson Fix some more formatting bugs
Vagabond authored
427 ?assertEqual("[[102,111,111],98,97,114]", lists:flatten(format("~w", [["foo", $b, $a, $r]], 50))),
03d3fa1 Andrew Thompson Fixes and tests for deep-list handling in trunc_io:format
Vagabond authored
428
429 %% complex ones
e45d5bc Andrew Thompson Implement an alternative and much faster lager_trunc_io:format/3
Vagabond authored
430 ?assertEqual(" foobar", lists:flatten(format("~10s", [["foo", $b, $a, $r]], 50))),
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
431 ?assertEqual(" [\"foo\",98,97,114]", lists:flatten(format("~22p", [["foo", $b, $a, $r]], 50))),
432 ?assertEqual(" [\"foo\",98,97,114]", lists:flatten(format("~22P", [["foo", $b, $a, $r], 10], 50))),
e45d5bc Andrew Thompson Implement an alternative and much faster lager_trunc_io:format/3
Vagabond authored
433 ?assertEqual("**********", lists:flatten(format("~10W", [["foo", $b, $a, $r], 10], 50))),
9c6ce62 Andrew Thompson Fix some more formatting bugs
Vagabond authored
434 ?assertEqual("[[102,111,111],98,97,114]", lists:flatten(format("~25W", [["foo", $b, $a, $r], 10], 50))),
03d3fa1 Andrew Thompson Fixes and tests for deep-list handling in trunc_io:format
Vagabond authored
435 ok.
436
9a577df Andrew Thompson Use the io_lib_format:fwrite_g function to print floats
Vagabond authored
437 atom_quoting_test() ->
438 ?assertEqual("hello", lists:flatten(format("~p", [hello], 50))),
439 ?assertEqual("'hello world'", lists:flatten(format("~p", ['hello world'], 50))),
9e735a7 Andrew Thompson Improve tests, fix some minor bugs
Vagabond authored
440 ?assertEqual("'Hello world'", lists:flatten(format("~p", ['Hello world'], 50))),
9a577df Andrew Thompson Use the io_lib_format:fwrite_g function to print floats
Vagabond authored
441 ?assertEqual("hello_world", lists:flatten(format("~p", ['hello_world'], 50))),
442 ?assertEqual("'node@127.0.0.1'", lists:flatten(format("~p", ['node@127.0.0.1'], 50))),
443 ?assertEqual("node@nohost", lists:flatten(format("~p", [node@nohost], 50))),
c2b5f9f Bryan Fink don't quote atoms just for containing numerals
beerriot authored
444 ?assertEqual("abc123", lists:flatten(format("~p", [abc123], 50))),
9a577df Andrew Thompson Use the io_lib_format:fwrite_g function to print floats
Vagabond authored
445 ok.
446
447 sane_float_printing_test() ->
448 ?assertEqual("1.0", lists:flatten(format("~p", [1.0], 50))),
449 ?assertEqual("1.23456789", lists:flatten(format("~p", [1.23456789], 50))),
450 ?assertEqual("1.23456789", lists:flatten(format("~p", [1.234567890], 50))),
451 ?assertEqual("0.3333333333333333", lists:flatten(format("~p", [1/3], 50))),
452 ?assertEqual("0.1234567", lists:flatten(format("~p", [0.1234567], 50))),
453 ok.
454
af4ea7f Andrew Thompson Don't treat floats in a list as printable characters Reported by @bryanh...
Vagabond authored
455 float_inside_list_test() ->
5140cb3 Andrew Thompson Don't convert \r, etc to spaces, don't assume printablility by first cha...
Vagabond authored
456 ?assertEqual("[97,38.233913133184835,99]", lists:flatten(format("~p", [[$a, 38.233913133184835, $c]], 50))),
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
457 ?assertError(badarg, lists:flatten(format("~s", [[$a, 38.233913133184835, $c]], 50))),
af4ea7f Andrew Thompson Don't treat floats in a list as printable characters Reported by @bryanh...
Vagabond authored
458 ok.
459
a0440a9 Andrew Thompson Smarter doublequote stripping when printing strings with ~s
Vagabond authored
460 quote_strip_test() ->
461 ?assertEqual("\"hello\"", lists:flatten(format("~p", ["hello"], 50))),
462 ?assertEqual("hello", lists:flatten(format("~s", ["hello"], 50))),
463 ?assertEqual("hello", lists:flatten(format("~s", [hello], 50))),
464 ?assertEqual("hello", lists:flatten(format("~p", [hello], 50))),
7e9da64 Andrew Thompson Several fixes for printing binaries, improve quote stripping
Vagabond authored
465 ?assertEqual("'hello world'", lists:flatten(format("~p", ['hello world'], 50))),
466 ?assertEqual("hello world", lists:flatten(format("~s", ['hello world'], 50))),
467 ok.
468
469 binary_printing_test() ->
9e735a7 Andrew Thompson Improve tests, fix some minor bugs
Vagabond authored
470 ?assertEqual("<<>>", lists:flatten(format("~p", [<<>>], 50))),
471 ?assertEqual("<<..>>", lists:flatten(format("~p", [<<"hi">>], 0))),
5068e29 Andrew Thompson Fix a couple minor issues with length calculation, add tests
Vagabond authored
472 ?assertEqual("<<...>>", lists:flatten(format("~p", [<<"hi">>], 1))),
7e9da64 Andrew Thompson Several fixes for printing binaries, improve quote stripping
Vagabond authored
473 ?assertEqual("<<\"hello\">>", lists:flatten(format("~p", [<<$h, $e, $l, $l, $o>>], 50))),
474 ?assertEqual("<<\"hello\">>", lists:flatten(format("~p", [<<"hello">>], 50))),
9e735a7 Andrew Thompson Improve tests, fix some minor bugs
Vagabond authored
475 ?assertEqual("<<104,101,108,108,111>>", lists:flatten(format("~w", [<<"hello">>], 50))),
7e9da64 Andrew Thompson Several fixes for printing binaries, improve quote stripping
Vagabond authored
476 ?assertEqual("<<1,2,3,4>>", lists:flatten(format("~p", [<<1, 2, 3, 4>>], 50))),
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
477 ?assertEqual([1,2,3,4], lists:flatten(format("~s", [<<1, 2, 3, 4>>], 50))),
7e9da64 Andrew Thompson Several fixes for printing binaries, improve quote stripping
Vagabond authored
478 ?assertEqual("hello", lists:flatten(format("~s", [<<"hello">>], 50))),
9c6ce62 Andrew Thompson Fix some more formatting bugs
Vagabond authored
479 ?assertEqual("hello\nworld", lists:flatten(format("~s", [<<"hello\nworld">>], 50))),
480 ?assertEqual("<<\"hello\\nworld\">>", lists:flatten(format("~p", [<<"hello\nworld">>], 50))),
d416dc0 Andrew Thompson Fix escaped char printing in ~p mode, reported by @etrepum
Vagabond authored
481 ?assertEqual("<<\"\\\"hello world\\\"\">>", lists:flatten(format("~p", [<<"\"hello world\"">>], 50))),
482 ?assertEqual("<<\"hello\\\\world\">>", lists:flatten(format("~p", [<<"hello\\world">>], 50))),
483 ?assertEqual("<<\"hello\\\\\world\">>", lists:flatten(format("~p", [<<"hello\\\world">>], 50))),
484 ?assertEqual("<<\"hello\\\\\\\\world\">>", lists:flatten(format("~p", [<<"hello\\\\world">>], 50))),
485 ?assertEqual("<<\"hello\\bworld\">>", lists:flatten(format("~p", [<<"hello\bworld">>], 50))),
486 ?assertEqual("<<\"hello\\tworld\">>", lists:flatten(format("~p", [<<"hello\tworld">>], 50))),
487 ?assertEqual("<<\"hello\\nworld\">>", lists:flatten(format("~p", [<<"hello\nworld">>], 50))),
488 ?assertEqual("<<\"hello\\rworld\">>", lists:flatten(format("~p", [<<"hello\rworld">>], 50))),
489 ?assertEqual("<<\"hello\\eworld\">>", lists:flatten(format("~p", [<<"hello\eworld">>], 50))),
490 ?assertEqual("<<\"hello\\fworld\">>", lists:flatten(format("~p", [<<"hello\fworld">>], 50))),
f5ca930 Andrew Thompson Vertical tab support
Vagabond authored
491 ?assertEqual("<<\"hello\\vworld\">>", lists:flatten(format("~p", [<<"hello\vworld">>], 50))),
e45d5bc Andrew Thompson Implement an alternative and much faster lager_trunc_io:format/3
Vagabond authored
492 ?assertEqual(" hello", lists:flatten(format("~10s", [<<"hello">>], 50))),
a0440a9 Andrew Thompson Smarter doublequote stripping when printing strings with ~s
Vagabond authored
493 ok.
5140cb3 Andrew Thompson Don't convert \r, etc to spaces, don't assume printablility by first cha...
Vagabond authored
494
6b7f1dd Andrew Thompson Support for printing bitstrings (reported by @freeaakk)
Vagabond authored
495 bitstring_printing_test() ->
496 ?assertEqual("<<1,2,3,1:7>>", lists:flatten(format("~p",
497 [<<1, 2, 3, 1:7>>], 100))),
498 ?assertEqual("<<1:7>>", lists:flatten(format("~p",
499 [<<1:7>>], 100))),
500 ?assertEqual("<<1,2,3,...>>", lists:flatten(format("~p",
501 [<<1, 2, 3, 1:7>>], 12))),
502 ?assertEqual("<<1,2,3,...>>", lists:flatten(format("~p",
503 [<<1, 2, 3, 1:7>>], 13))),
504 ?assertEqual("<<1,2,3,1:7>>", lists:flatten(format("~p",
505 [<<1, 2, 3, 1:7>>], 14))),
506 ?assertEqual("<<..>>", lists:flatten(format("~p", [<<1:7>>], 0))),
507 ?assertEqual("<<...>>", lists:flatten(format("~p", [<<1:7>>], 1))),
79b8fd1 Andrew Thompson Don't break printing lists of binaries
Vagabond authored
508 ?assertEqual("[<<1>>,<<2>>]", lists:flatten(format("~p", [[<<1>>, <<2>>]],
509 100))),
6b7f1dd Andrew Thompson Support for printing bitstrings (reported by @freeaakk)
Vagabond authored
510 ok.
511
5140cb3 Andrew Thompson Don't convert \r, etc to spaces, don't assume printablility by first cha...
Vagabond authored
512 list_printing_test() ->
9e735a7 Andrew Thompson Improve tests, fix some minor bugs
Vagabond authored
513 ?assertEqual("[]", lists:flatten(format("~p", [[]], 50))),
514 ?assertEqual("[]", lists:flatten(format("~w", [[]], 50))),
515 ?assertEqual("", lists:flatten(format("~s", [[]], 50))),
516 ?assertEqual("...", lists:flatten(format("~s", [[]], -1))),
517 ?assertEqual("[[]]", lists:flatten(format("~p", [[[]]], 50))),
5140cb3 Andrew Thompson Don't convert \r, etc to spaces, don't assume printablility by first cha...
Vagabond authored
518 ?assertEqual("[13,11,10,8,5,4]", lists:flatten(format("~p", [[13,11,10,8,5,4]], 50))),
9c6ce62 Andrew Thompson Fix some more formatting bugs
Vagabond authored
519 ?assertEqual("\"\\rabc\"", lists:flatten(format("~p", [[13,$a, $b, $c]], 50))),
5140cb3 Andrew Thompson Don't convert \r, etc to spaces, don't assume printablility by first cha...
Vagabond authored
520 ?assertEqual("[1,2,3|4]", lists:flatten(format("~p", [[1, 2, 3|4]], 50))),
9e735a7 Andrew Thompson Improve tests, fix some minor bugs
Vagabond authored
521 ?assertEqual("[...]", lists:flatten(format("~p", [[1, 2, 3,4]], 4))),
5068e29 Andrew Thompson Fix a couple minor issues with length calculation, add tests
Vagabond authored
522 ?assertEqual("[1,...]", lists:flatten(format("~p", [[1, 2, 3, 4]], 6))),
523 ?assertEqual("[1,...]", lists:flatten(format("~p", [[1, 2, 3, 4]], 7))),
524 ?assertEqual("[1,2,...]", lists:flatten(format("~p", [[1, 2, 3, 4]], 8))),
5140cb3 Andrew Thompson Don't convert \r, etc to spaces, don't assume printablility by first cha...
Vagabond authored
525 ?assertEqual("[1|4]", lists:flatten(format("~p", [[1|4]], 50))),
9e735a7 Andrew Thompson Improve tests, fix some minor bugs
Vagabond authored
526 ?assertEqual("[1]", lists:flatten(format("~p", [[1]], 50))),
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
527 ?assertError(badarg, lists:flatten(format("~s", [[1|4]], 50))),
5140cb3 Andrew Thompson Don't convert \r, etc to spaces, don't assume printablility by first cha...
Vagabond authored
528 ?assertEqual("\"hello...\"", lists:flatten(format("~p", ["hello world"], 10))),
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
529 ?assertEqual("hello w...", lists:flatten(format("~s", ["hello world"], 10))),
5140cb3 Andrew Thompson Don't convert \r, etc to spaces, don't assume printablility by first cha...
Vagabond authored
530 ?assertEqual("hello world\r\n", lists:flatten(format("~s", ["hello world\r\n"], 50))),
531 ?assertEqual("\rhello world\r\n", lists:flatten(format("~s", ["\rhello world\r\n"], 50))),
9c6ce62 Andrew Thompson Fix some more formatting bugs
Vagabond authored
532 ?assertEqual("\"\\rhello world\\r\\n\"", lists:flatten(format("~p", ["\rhello world\r\n"], 50))),
533 ?assertEqual("[13,104,101,108,108,111,32,119,111,114,108,100,13,10]", lists:flatten(format("~w", ["\rhello world\r\n"], 60))),
5140cb3 Andrew Thompson Don't convert \r, etc to spaces, don't assume printablility by first cha...
Vagabond authored
534 ?assertEqual("...", lists:flatten(format("~s", ["\rhello world\r\n"], 3))),
5068e29 Andrew Thompson Fix a couple minor issues with length calculation, add tests
Vagabond authored
535 ?assertEqual("[22835963083295358096932575511191922182123945984,...]",
536 lists:flatten(format("~p", [
537 [22835963083295358096932575511191922182123945984,
538 22835963083295358096932575511191922182123945984]], 9))),
539 ?assertEqual("[22835963083295358096932575511191922182123945984,...]",
540 lists:flatten(format("~p", [
541 [22835963083295358096932575511191922182123945984,
542 22835963083295358096932575511191922182123945984]], 53))),
9e735a7 Andrew Thompson Improve tests, fix some minor bugs
Vagabond authored
543 ok.
544
545 tuple_printing_test() ->
546 ?assertEqual("{}", lists:flatten(format("~p", [{}], 50))),
547 ?assertEqual("{}", lists:flatten(format("~w", [{}], 50))),
548 ?assertError(badarg, lists:flatten(format("~s", [{}], 50))),
5068e29 Andrew Thompson Fix a couple minor issues with length calculation, add tests
Vagabond authored
549 ?assertEqual("{...}", lists:flatten(format("~p", [{foo}], 1))),
550 ?assertEqual("{...}", lists:flatten(format("~p", [{foo}], 2))),
9e735a7 Andrew Thompson Improve tests, fix some minor bugs
Vagabond authored
551 ?assertEqual("{...}", lists:flatten(format("~p", [{foo}], 3))),
5068e29 Andrew Thompson Fix a couple minor issues with length calculation, add tests
Vagabond authored
552 ?assertEqual("{...}", lists:flatten(format("~p", [{foo}], 4))),
553 ?assertEqual("{...}", lists:flatten(format("~p", [{foo}], 5))),
9e735a7 Andrew Thompson Improve tests, fix some minor bugs
Vagabond authored
554 ?assertEqual("{foo,...}", lists:flatten(format("~p", [{foo,bar}], 6))),
5068e29 Andrew Thompson Fix a couple minor issues with length calculation, add tests
Vagabond authored
555 ?assertEqual("{foo,...}", lists:flatten(format("~p", [{foo,bar}], 7))),
556 ?assertEqual("{foo,...}", lists:flatten(format("~p", [{foo,bar}], 9))),
557 ?assertEqual("{foo,bar}", lists:flatten(format("~p", [{foo,bar}], 10))),
558 ?assertEqual("{22835963083295358096932575511191922182123945984,...}",
559 lists:flatten(format("~w", [
560 {22835963083295358096932575511191922182123945984,
561 22835963083295358096932575511191922182123945984}], 10))),
562 ?assertEqual("{22835963083295358096932575511191922182123945984,...}",
563 lists:flatten(format("~w", [
564 {22835963083295358096932575511191922182123945984,
565 bar}], 10))),
566 ?assertEqual("{22835963083295358096932575511191922182123945984,...}",
567 lists:flatten(format("~w", [
568 {22835963083295358096932575511191922182123945984,
569 22835963083295358096932575511191922182123945984}], 53))),
5140cb3 Andrew Thompson Don't convert \r, etc to spaces, don't assume printablility by first cha...
Vagabond authored
570 ok.
e5b58fc Andrew Thompson Implement print options for trunc_io so ~s/~p/~w emulation is more accur...
Vagabond authored
571
572 unicode_test() ->
573 ?assertEqual([231,167,129], lists:flatten(format("~s", [<<231,167,129>>], 50))),
574 ?assertEqual([31169], lists:flatten(format("~ts", [<<231,167,129>>], 50))),
575 ok.
576
d16dfaa Andrew Thompson Implement ~P/~W style depth-limiting
Vagabond authored
577 depth_limit_test() ->
578 ?assertEqual("{...}", lists:flatten(format("~P", [{a, [b, [c, [d]]]}, 1], 50))),
579 ?assertEqual("{a,...}", lists:flatten(format("~P", [{a, [b, [c, [d]]]}, 2], 50))),
580 ?assertEqual("{a,[...]}", lists:flatten(format("~P", [{a, [b, [c, [d]]]}, 3], 50))),
581 ?assertEqual("{a,[b|...]}", lists:flatten(format("~P", [{a, [b, [c, [d]]]}, 4], 50))),
582 ?assertEqual("{a,[b,[...]]}", lists:flatten(format("~P", [{a, [b, [c, [d]]]}, 5], 50))),
583 ?assertEqual("{a,[b,[c|...]]}", lists:flatten(format("~P", [{a, [b, [c, [d]]]}, 6], 50))),
584 ?assertEqual("{a,[b,[c,[...]]]}", lists:flatten(format("~P", [{a, [b, [c, [d]]]}, 7], 50))),
585 ?assertEqual("{a,[b,[c,[d]]]}", lists:flatten(format("~P", [{a, [b, [c, [d]]]}, 8], 50))),
586 ?assertEqual("{a,[b,[c,[d]]]}", lists:flatten(format("~P", [{a, [b, [c, [d]]]}, 9], 50))),
587
588 ?assertEqual("{a,{...}}", lists:flatten(format("~P", [{a, {b, {c, {d}}}}, 3], 50))),
589 ?assertEqual("{a,{b,...}}", lists:flatten(format("~P", [{a, {b, {c, {d}}}}, 4], 50))),
590 ?assertEqual("{a,{b,{...}}}", lists:flatten(format("~P", [{a, {b, {c, {d}}}}, 5], 50))),
591 ?assertEqual("{a,{b,{c,...}}}", lists:flatten(format("~P", [{a, {b, {c, {d}}}}, 6], 50))),
592 ?assertEqual("{a,{b,{c,{...}}}}", lists:flatten(format("~P", [{a, {b, {c, {d}}}}, 7], 50))),
593 ?assertEqual("{a,{b,{c,{d}}}}", lists:flatten(format("~P", [{a, {b, {c, {d}}}}, 8], 50))),
594
595 ?assertEqual("{\"a\",[...]}", lists:flatten(format("~P", [{"a", ["b", ["c", ["d"]]]}, 3], 50))),
596 ?assertEqual("{\"a\",[\"b\",[[...]|...]]}", lists:flatten(format("~P", [{"a", ["b", ["c", ["d"]]]}, 6], 50))),
597 ?assertEqual("{\"a\",[\"b\",[\"c\",[\"d\"]]]}", lists:flatten(format("~P", [{"a", ["b", ["c", ["d"]]]}, 9], 50))),
598 ok.
599
03d3fa1 Andrew Thompson Fixes and tests for deep-list handling in trunc_io:format
Vagabond authored
600 -endif.
Something went wrong with that request. Please try again.