From 0f8da5029dcd3e6eea4581e2395cbba2fd8d85ed Mon Sep 17 00:00:00 2001 From: SisMaker <1713699517@qq.com> Date: Fri, 26 Feb 2021 14:27:28 +0800 Subject: [PATCH] =?UTF-8?q?ft:=20=E7=BB=88=E7=89=881?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/eFmt.erl | 203 +++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 164 insertions(+), 39 deletions(-) diff --git a/src/eFmt.erl b/src/eFmt.erl index 90707ff..620ff7b 100644 --- a/src/eFmt.erl +++ b/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) -> <>; writeList([One], D, E, BinAcc) -> @@ -156,16 +157,74 @@ writeList([One | List], D, E, BinAcc) -> writeList(Other, D, E, BinAcc) -> <>. +writeTuple(Tuple, D, E, Index, TupleSize, BinAcc) -> + if + D =:= 1 -> <>; + true -> + if + Index < TupleSize -> + writeTuple(Tuple, D - 1, E, Index + 1, TupleSize, <>); + Index == TupleSize -> + <>; + true -> + <> + end + end. + +writeMap(Map, D, E, BinAcc) -> + if + D =:= 1 -> + <>; + true -> + writeMapBody(maps:iterator(Map), D, E, BinAcc) + end. +writeMapBody(I, D, E, BinAcc) -> + if + D =:= 1 -> + <>; + true -> + case maps:next(I) of + {K, V, none} -> + < ", (writeTerm(V, D, E))/binary, "}">>; + {K, V, NextI} -> + writeMapBody(NextI, D - 1, E, < ", (writeTerm(V, D, E))/binary, ",">>); + none -> + <> + end + end. + +writeBinary(Bin, D, BinAcc) -> + if + D == 1 -> + <>">>; + true -> + case Bin of + <<>> -> + <>">>; + <> -> + <>">>; + <> -> + writeBinary(LeftBin, D - 1, <>); + _ -> + L = bit_size(Bin), + <> = Bin, + <>">> + 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) -> <>; 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 -> <>; true -> - writeList(List, Depth - 1, E, <>) + 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, <>); + _ -> + writeList(List, Depth - 1, Width, Encoding, Strings, NewSumLC, <>) + end end; -writeList(Term, Depth, Width, Encoding, Strings, <<"[">>) -> - <>. +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 -> + <>; + _ -> + <> + end. -writeTuple(Tuple, Depth, Width, CharsLimit, Encoding, Strings, Index, TupleSize, BinAcc) -> +writeTuple(Tuple, Depth, Width, Encoding, Strings, Index, TupleSize, SumLC, BinAcc) -> if - + Depth =:= 1 -> <>; + 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, <>); + _ -> + writeTuple(Tuple, Depth - 1, Width, Encoding, Strings, Index + 1, TupleSize, NewSumLC, <>) + 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 -> + <>; + _ -> + <> + end; + true -> + <> + end end. -writeMap(Map, D, E, BinAcc) -> +writeMap(Map, Depth, Width, Encoding, Strings, SumLC, BinAcc) -> if - D =:= 1 -> + Depth =:= 1 -> <>; 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 -> <>; true -> case maps:next(I) of {K, V, none} -> - < ", (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 -> + < ", ValueTermBin/binary, "}\n">>; + _ -> + < ", ValueTermBin/binary, "}">> + end; {K, V, NextI} -> - writeMapBody(NextI, D - 1, E, < ", (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, < ", ValueTermBin/binary, ",\n">>); + _ -> + writeMapBody(NextI, Depth - 1, Width, Encoding, Strings, NewSumLC, < ", ValueTermBin/binary, ",">>) + end; none -> <> 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 -> <>">>; true -> case Bin of @@ -226,22 +352,24 @@ writeBinary(Bin, D, BinAcc) -> <> -> <>">>; <> -> - writeBinary(LeftBin, D - 1, <>); + 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, <>); + _ -> + writeBinary(LeftBin, Depth - 1, Width, Encoding, Strings, NewSumLC, <>) + end; _ -> L = bit_size(Bin), <> = Bin, <>">> 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 ********************************************************** \ No newline at end of file