瀏覽代碼

Truncator for funs; special case binary truncation when max is 0

pull/4/head
Andrew Thompson 14 年之前
父節點
當前提交
71fae1b89a
共有 2 個檔案被更改,包括 13 行新增3 行删除
  1. +12
    -2
      src/trunc_io.erl
  2. +1
    -1
      test/trunc_io_eqc.erl

+ 12
- 2
src/trunc_io.erl 查看文件

@ -172,6 +172,9 @@ print(Atom, _Max) when is_atom(Atom) ->
print(<<>>, _Max) ->
{"<<>>", 4};
print(Binary, 0) when is_binary(Binary) ->
{"<<..>>", 6};
print(Binary, Max) when is_binary(Binary) ->
B = binary_to_list(Binary, 1, lists:min([Max, size(Binary)])),
{L, Len} = alist_start(B, Max-4),
@ -181,9 +184,16 @@ print(Float, _Max) when is_float(Float) ->
L = float_to_list(Float),
{L, length(L)};
print(Fun, _Max) when is_function(Fun) ->
print(Fun, Max) when is_function(Fun) ->
L = erlang:fun_to_list(Fun),
{L, length(L)};
case length(L) > Max of
true ->
S = erlang:max(5, Max),
Res = string:substr(L, 1, S) ++ "..>",
{Res, length(Res)};
_ ->
{L, length(L)}
end;
print(Integer, _Max) when is_integer(Integer) ->
L = integer_to_list(Integer),

+ 1
- 1
test/trunc_io_eqc.erl 查看文件

@ -115,7 +115,7 @@ gen_fun() ->
prop_format() ->
?FORALL({FmtArgs, MaxLen}, {gen_fmt_args(), gen_max_len()},
begin
FudgeLen = 31, %% trunc_io does not correctly calc safe size of pid/port/numbers
FudgeLen = 31, %% trunc_io does not correctly calc safe size of pid/port/numbers/funs
{FmtStr, Args} = build_fmt_args(FmtArgs),
try
Str = lists:flatten(trunc_io:format(FmtStr, Args, MaxLen)),

Loading…
取消
儲存