SisMaker 4 лет назад
Родитель
Сommit
0f8da5029d
1 измененных файлов: 164 добавлений и 39 удалений
  1. +164
    -39
      src/eFmt.erl

+ 164
- 39
src/eFmt.erl Просмотреть файл

@ -143,6 +143,7 @@ write(Term, Depth, Width, CharsLimit, Encoding, Strings) ->
-define(writePid(Ref), list_to_binary(erlang:pid_to_list(Ref))).
-define(writeFun(Fun), list_to_binary(erlang:fun_to_list(Fun))).
%% **************************************************** ~w start *******************************************************
writeList([], _D, _E, BinAcc) ->
<<BinAcc/binary, "]">>;
writeList([One], D, E, BinAcc) ->
@ -156,16 +157,74 @@ writeList([One | List], D, E, BinAcc) ->
writeList(Other, D, E, BinAcc) ->
<<BinAcc/binary, "|", (writeTerm(Other, D, E))/binary, "]">>.
writeTuple(Tuple, D, E, Index, TupleSize, BinAcc) ->
if
D =:= 1 -> <<BinAcc/binary, "...}">>;
true ->
if
Index < TupleSize ->
writeTuple(Tuple, D - 1, E, Index + 1, TupleSize, <<BinAcc/binary, (writeTerm(element(Index, Tuple), D - 1, E))/binary, ",">>);
Index == TupleSize ->
<<BinAcc/binary, (writeTerm(element(Index, Tuple), D - 1, E))/binary, "}">>;
true ->
<<BinAcc/binary, "}">>
end
end.
writeMap(Map, D, E, BinAcc) ->
if
D =:= 1 ->
<<BinAcc/binary, "...}">>;
true ->
writeMapBody(maps:iterator(Map), D, E, BinAcc)
end.
writeMapBody(I, D, E, BinAcc) ->
if
D =:= 1 ->
<<BinAcc/binary, " ...}">>;
true ->
case maps:next(I) of
{K, V, none} ->
<<BinAcc/binary, (writeTerm(K, -1, E))/binary, " => ", (writeTerm(V, D, E))/binary, "}">>;
{K, V, NextI} ->
writeMapBody(NextI, D - 1, E, <<BinAcc/binary, (writeTerm(K, -1, E))/binary, " => ", (writeTerm(V, D, E))/binary, ",">>);
none ->
<<BinAcc/binary, "}">>
end
end.
writeBinary(Bin, D, BinAcc) ->
if
D == 1 ->
<<BinAcc/binary, "...>>">>;
true ->
case Bin of
<<>> ->
<<BinAcc/binary, ">>">>;
<<Int:8>> ->
<<BinAcc/binary, (integer_to_binary(Int))/binary, ">>">>;
<<Int:8, LeftBin/bitstring>> ->
writeBinary(LeftBin, D - 1, <<BinAcc/binary, (integer_to_binary(Int))/binary, ",">>);
_ ->
L = bit_size(Bin),
<<X:L>> = Bin,
<<BinAcc/binary, (integer_to_binary(X))/binary, ":", (integer_to_binary(L))/binary, ">>">>
end
end.
%% **************************************************** ~w end *******************************************************
%% **************************************************** ~p start *******************************************************
writeList(List, Depth, Width, Encoding, Strings) ->
case Strings andalso visualList(List, Encoding) of
true ->
list_to_binary(List);
_ ->
writeList([], Depth, Width, Encoding, Strings, SumLC, <<"[">>)
writeList(List, Depth, Width, Encoding, Strings, 0, <<"[">>)
end.
writeList([], Depth, Width, Encoding, Strings, SumLC, BinAcc) ->
writeList([], _Depth, _Width, _Encoding, _Strings, _SumLC, BinAcc) ->
<<BinAcc/binary, "]">>;
writeList([One], Depth, Width, Encoding, Strings, SumLC, BinAcc) ->
TermBin = writeTerm(One, Depth, Width, Encoding, Strings),
@ -182,42 +241,109 @@ writeList([One | List], Depth, Width, Encoding, Strings, SumLC, BinAcc) ->
if
Depth =:= 1 -> <<BinAcc, "|...]">>;
true ->
writeList(List, Depth - 1, E, <<BinAcc/binary, (writeTerm(One, Depth, E))/binary, ",">>)
TermBin = writeTerm(One, Depth, Width, Encoding, Strings),
TermBinBinSize = erlang:byte_size(TermBin),
NewSumLC = SumLC + TermBinBinSize,
case NewSumLC >= Width of
true ->
writeList(List, Depth - 1, Width, Encoding, Strings, 0, <<BinAcc/binary, TermBin/binary, ",\n">>);
_ ->
writeList(List, Depth - 1, Width, Encoding, Strings, NewSumLC, <<BinAcc/binary, TermBin/binary, ",">>)
end
end;
writeList(Term, Depth, Width, Encoding, Strings, <<"[">>) ->
<<BinAcc/binary, (writeTerm(One, D, E))/binary, "]">>.
writeList(Other, Depth, Width, Encoding, Strings, SumLC, BinAcc) ->
TermBin = writeTerm(Other, Depth, Width, Encoding, Strings),
TermBinBinSize = erlang:byte_size(TermBin),
NewSumLC = SumLC + TermBinBinSize,
case NewSumLC >= Width of
true ->
<<BinAcc/binary, "|", TermBin/binary, "]\n">>;
_ ->
<<BinAcc/binary, "|", TermBin/binary, "]">>
end.
writeTuple(Tuple, Depth, Width, CharsLimit, Encoding, Strings, Index, TupleSize, BinAcc) ->
writeTuple(Tuple, Depth, Width, Encoding, Strings, Index, TupleSize, SumLC, BinAcc) ->
if
Depth =:= 1 -> <<BinAcc/binary, "...}">>;
true ->
if
Index < TupleSize ->
TermBin = writeTerm(element(Index, Tuple), Depth, Width, Encoding, Strings),
TermBinBinSize = erlang:byte_size(TermBin),
NewSumLC = SumLC + TermBinBinSize,
case NewSumLC >= Width of
true ->
writeTuple(Tuple, Depth - 1, Width, Encoding, Strings, Index + 1, TupleSize, 0, <<BinAcc/binary, TermBin/binary, ",\n">>);
_ ->
writeTuple(Tuple, Depth - 1, Width, Encoding, Strings, Index + 1, TupleSize, NewSumLC, <<BinAcc/binary, TermBin/binary, ",">>)
end;
Index == TupleSize ->
TermBin = writeTerm(element(Index, Tuple), Depth, Width, Encoding, Strings),
TermBinBinSize = erlang:byte_size(TermBin),
NewSumLC = SumLC + TermBinBinSize,
case NewSumLC >= Width of
true ->
<<BinAcc/binary, TermBin/binary, "}\n">>;
_ ->
<<BinAcc/binary, TermBin/binary, "}">>
end;
true ->
<<BinAcc/binary, "}">>
end
end.
writeMap(Map, D, E, BinAcc) ->
writeMap(Map, Depth, Width, Encoding, Strings, SumLC, BinAcc) ->
if
D =:= 1 ->
Depth =:= 1 ->
<<BinAcc/binary, "...}">>;
true ->
writeMapBody(maps:iterator(Map), D, E, BinAcc)
writeMapBody(maps:iterator(Map), Depth, Width, Encoding, Strings, SumLC, BinAcc)
end.
writeMapBody(I, D, E, BinAcc) ->
writeMapBody(I, Depth, Width, Encoding, Strings, SumLC, BinAcc) ->
if
D =:= 1 ->
Depth =:= 1 ->
<<BinAcc/binary, " ...}">>;
true ->
case maps:next(I) of
{K, V, none} ->
<<BinAcc/binary, (writeTerm(K, -1, E))/binary, " => ", (writeTerm(V, D, E))/binary, "}">>;
KeyTermBin = writeTerm(K, -1, Width, Encoding, Strings),
ValueTermBin = writeTerm(V, -1, Width, Encoding, Strings),
TermBinBinSize = erlang:byte_size(KeyTermBin) + erlang:byte_size(ValueTermBin),
NewSumLC = SumLC + TermBinBinSize,
case NewSumLC >= Width of
true ->
<<BinAcc/binary, KeyTermBin/binary, " => ", ValueTermBin/binary, "}\n">>;
_ ->
<<BinAcc/binary, KeyTermBin/binary, " => ", ValueTermBin/binary, "}">>
end;
{K, V, NextI} ->
writeMapBody(NextI, D - 1, E, <<BinAcc/binary, (writeTerm(K, -1, E))/binary, " => ", (writeTerm(V, D, E))/binary, ",">>);
KeyTermBin = writeTerm(K, -1, Width, Encoding, Strings),
ValueTermBin = writeTerm(V, -1, Width, Encoding, Strings),
TermBinBinSize = erlang:byte_size(KeyTermBin) + erlang:byte_size(ValueTermBin),
NewSumLC = SumLC + TermBinBinSize,
case NewSumLC >= Width of
true ->
writeMapBody(NextI, Depth - 1, Width, Encoding, Strings, 0, <<BinAcc/binary, KeyTermBin/binary, " => ", ValueTermBin/binary, ",\n">>);
_ ->
writeMapBody(NextI, Depth - 1, Width, Encoding, Strings, NewSumLC, <<BinAcc/binary, KeyTermBin/binary, " => ", ValueTermBin/binary, ",">>)
end;
none ->
<<BinAcc/binary, "}">>
end
end.
writeBinary(Bin, D, BinAcc) ->
writeBinary(Bin, Depth, Width, Encoding, Strings) ->
case Strings andalso visualBin(Bin, Encoding) of
true ->
<<"<<", Bin/binary, ">>">>;
_ ->
writeBinary(Bin, Depth, Width, Encoding, Strings, 0, <<"<<">>)
end.
writeBinary(Bin, Depth, Width, Encoding, Strings, SumLC, BinAcc) ->
if
D == 1 ->
Depth == 1 ->
<<BinAcc/binary, "...>>">>;
true ->
case Bin of
@ -226,22 +352,24 @@ writeBinary(Bin, D, BinAcc) ->
<<Int:8>> ->
<<BinAcc/binary, (integer_to_binary(Int))/binary, ">>">>;
<<Int:8, LeftBin/bitstring>> ->
writeBinary(LeftBin, D - 1, <<BinAcc/binary, (integer_to_binary(Int))/binary, ",">>);
TermBin = integer_to_binary(Int),
TermBinBinSize = erlang:byte_size(TermBin),
NewSumLC = SumLC + TermBinBinSize,
case NewSumLC >= Width of
true ->
writeBinary(LeftBin, Depth - 1, Width, Encoding, Strings, 0, <<BinAcc/binary, TermBin/binary, ",\n">>);
_ ->
writeBinary(LeftBin, Depth - 1, Width, Encoding, Strings, NewSumLC, <<BinAcc/binary, TermBin/binary, ",">>)
end;
_ ->
L = bit_size(Bin),
<<X:L>> = Bin,
<<BinAcc/binary, (integer_to_binary(X))/binary, ":", (integer_to_binary(L))/binary, ">>">>
end
end.
%% **************************************************** ~p end *******************************************************
writeBinary(Bin, Depth, Width, Encoding, Strings) ->
case Strings andalso visableBin(Bin) of
true ->
<<"<<", Bin/binary, ">>">>;
_ ->
writeBinary([], Depth, Width, Encoding, Strings, <<"<<">>)
end.
%% ~w
writeTerm(_Term, Depth, _E) when Depth =< 0 -> <<"...">>;
writeTerm(Term, _Depth, _E) when is_integer(Term) -> ?writeInt(Term);
writeTerm(Term, _Depth, E) when is_atom(Term) -> ?writeAtom(Term, E);
@ -255,16 +383,14 @@ writeTerm(Term, _Depth, _E) when is_port(Term) -> ?writePort(Term);
writeTerm(Term, _Depth, _E) when is_reference(Term) -> ?writeRef(Term);
writeTerm(Term, _Depth, _E) when is_function(Term) -> ?writeFun(Term).
%% ~p
writeTerm(_Term, Depth, _Width, _Encoding, _Strings) when Depth =< 0 -> <<"...">>;
writeTerm(Term, _Depth, _Width, _Encoding, _Strings) when is_integer(Term) -> ?writeInt(Term);
writeTerm(Term, _Depth, _Width, Encoding, _Strings) when is_atom(Term) -> ?writeAtom(Term, Encoding);
writeTerm(Term, Depth, Width, Encoding, Strings) when is_list(Term) -> writeList(Term, Depth, Width, Encoding, Strings);
writeTerm(Term, Depth, Width, Encoding, Strings) when is_map(Term) ->
writeMap(Term, Depth, Width, Encoding, Strings, <<"#{">>);
writeTerm(Term, Depth, Width, Encoding, Strings) when is_tuple(Term) ->
writeTuple(Term, Depth, Width, Encoding, Strings, 1, tuple_size(Term), <<"{">>);
writeTerm(Term, Depth, Width, Encoding, Strings) when is_bitstring(Term) ->
writeBinary(Term, Depth, Width, Encoding, Strings, <<"<<">>);
writeTerm(Term, Depth, Width, Encoding, Strings) when is_map(Term) -> writeMap(Term, Depth, Width, Encoding, Strings, 0, <<"#{">>);
writeTerm(Term, Depth, Width, Encoding, Strings) when is_tuple(Term) -> writeTuple(Term, Depth, Width, Encoding, Strings, 1, 0, tuple_size(Term), <<"{">>);
writeTerm(Term, Depth, Width, Encoding, Strings) when is_bitstring(Term) -> writeBinary(Term, Depth, Width, Encoding, Strings);
writeTerm(Term, _Depth, _Width, _Encoding, _Strings) when is_pid(Term) -> ?writePid(Term);
writeTerm(Term, _Depth, _Width, _Encoding, _Strings) when is_float(Term) -> ?writeFloat(Term);
writeTerm(Term, _Depth, _Width, _Encoding, _Strings) when is_port(Term) -> ?writePort(Term);
@ -841,8 +967,7 @@ visualLatin1Char($\v) -> true;
visualLatin1Char($\b) -> true;
visualLatin1Char($\f) -> true;
visualLatin1Char($\e) -> true;
visualLatin1Char(C) ->
C >= $\040 andalso C =< $\176 orelse C >= $\240, C =< $\377.
visualLatin1Char(C) -> C >= $\040 andalso C =< $\176 orelse C >= $\240 andalso C =< $\377.
visualUtf8Char($\n, _) -> true;
visualUtf8Char($\r, _) -> true;
@ -853,10 +978,10 @@ visualUtf8Char($\f, _) -> true;
visualUtf8Char($\e, _) -> true;
visualUtf8Char(C, _Encoding) ->
C >= $\s andalso C =< $~ orelse C >= 16#A0 andalso C < 16#D800 orelse C > 16#DFFF andalso C < 16#FFFE orelse C > 16#FFFF andalso C =< 16#10FFFF.
%% case Encoding of
%% latin1 ->
%% C >= $\s andalso C =< $~ orelse C >= 16#A0 andalso C =< 16#FF;
%% _ ->
%% C >= $\s andalso C =< $~ orelse C >= 16#A0 andalso C < 16#D800 orelse C > 16#DFFF andalso C < 16#FFFE orelse C > 16#FFFF andalso C =< 16#10FFFF
%% end.
%% case Encoding of
%% latin1 ->
%% C >= $\s andalso C =< $~ orelse C >= 16#A0 andalso C =< 16#FF;
%% _ ->
%% C >= $\s andalso C =< $~ orelse C >= 16#A0 andalso C < 16#D800 orelse C > 16#DFFF andalso C < 16#FFFE orelse C > 16#FFFF andalso C =< 16#10FFFF
%% end.
%% ********************************************** utils end **********************************************************

Загрузка…
Отмена
Сохранить