From ff25015b1ef7ee99dc845932600fc7d84e35e7c7 Mon Sep 17 00:00:00 2001 From: SisMaker <1713699517@qq.com> Date: Fri, 26 Feb 2021 00:54:32 +0800 Subject: [PATCH] =?UTF-8?q?ft:=E6=B5=8B=E8=AF=95=E6=B7=BB=E5=8A=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/eFmt.erl | 165 ++++++++++++++++++++++++++++----------------------- 1 file changed, 92 insertions(+), 73 deletions(-) diff --git a/src/eFmt.erl b/src/eFmt.erl index 7ae94be..b3dc872 100644 --- a/src/eFmt.erl +++ b/src/eFmt.erl @@ -14,13 +14,11 @@ , build/1 , build/2 - , print/1 - , print/4 - , write/1 , write/2 , write/3 , write/4 + , write/6 %% eFmtformat , fWrite/2 @@ -29,8 +27,6 @@ , fBuild/1 , fBuild/2 - %% eFmtPretty - %% utils , toLowerStr/1 , toUpperStr/1 @@ -85,14 +81,6 @@ build(FormatList, Options) -> erlang:error(badarg, [FormatList, Options]) end. --spec print(Term :: term()) -> chars(). -print(Term) -> - eFmtPretty:pPrint(Term). - --spec print(Term :: term(), Column :: non_neg_integer(), LineLength :: non_neg_integer(), Depth :: depth()) -> chars(). -print(Term, Column, LineLength, Depth) -> - eFmtPretty:pPrint(Term, Column, LineLength, Depth). - -spec write(Term :: term()) -> chars(). write(Term) -> writeTerm(Term, -1, latin1). @@ -168,8 +156,59 @@ writeList([One | List], D, E, BinAcc) -> writeList(Other, D, E, BinAcc) -> <>. +-spec printable_list(Term) -> boolean() when + Term :: term(). + +visableList(L, Encoding) -> + %% There will be more alternatives returns from io:printable range + %% in the future. To not have a catch-all clause is deliberate. + case Encoding of + latin1 -> + printable_latin1_list(L); + _ -> + printable_unicode_list(L) + end. + +-spec printable_unicode_list(Term) -> boolean() when + Term :: term(). + +-spec printable_latin1_list(Term) -> boolean() when + Term :: term(). + +printable_latin1_list([C|Cs]) when is_integer(C), C >= $\040, C =< $\176 -> + printable_latin1_list(Cs); +printable_latin1_list([C|Cs]) when is_integer(C), C >= $\240, C =< $\377 -> + printable_latin1_list(Cs); +printable_latin1_list([$\n|Cs]) -> printable_latin1_list(Cs); +printable_latin1_list([$\r|Cs]) -> printable_latin1_list(Cs); +printable_latin1_list([$\t|Cs]) -> printable_latin1_list(Cs); +printable_latin1_list([$\v|Cs]) -> printable_latin1_list(Cs); +printable_latin1_list([$\b|Cs]) -> printable_latin1_list(Cs); +printable_latin1_list([$\f|Cs]) -> printable_latin1_list(Cs); +printable_latin1_list([$\e|Cs]) -> printable_latin1_list(Cs); +printable_latin1_list([]) -> true; +printable_latin1_list(_) -> false. %Everything else is false + +printable_unicode_list([C|Cs]) when is_integer(C), C >= $\040, C =< $\176 -> + printable_unicode_list(Cs); +printable_unicode_list([C|Cs]) + when is_integer(C), C >= 16#A0, C < 16#D800; + is_integer(C), C > 16#DFFF, C < 16#FFFE; + is_integer(C), C > 16#FFFF, C =< 16#10FFFF -> + printable_unicode_list(Cs); +printable_unicode_list([$\n|Cs]) -> printable_unicode_list(Cs); +printable_unicode_list([$\r|Cs]) -> printable_unicode_list(Cs); +printable_unicode_list([$\t|Cs]) -> printable_unicode_list(Cs); +printable_unicode_list([$\v|Cs]) -> printable_unicode_list(Cs); +printable_unicode_list([$\b|Cs]) -> printable_unicode_list(Cs); +printable_unicode_list([$\f|Cs]) -> printable_unicode_list(Cs); +printable_unicode_list([$\e|Cs]) -> printable_unicode_list(Cs); +printable_unicode_list([]) -> true; +printable_unicode_list(_) -> false. %Everything else is false + + writeList(List, Depth, Width, Encoding, Strings) -> - case Strings andalso visableList(List) of + case Strings andalso visableList(List, Encoding) of true -> list_to_binary(List); _ -> @@ -279,12 +318,9 @@ 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, <<"#{">>); +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_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); @@ -413,16 +449,16 @@ doCollCA(LPart, Args, Width, Adjust, Precision, PadChar, Encoding, Strings, Acc) case CtlChar of $w -> [OneArgs | LeftArgs] = Args, As = OneArgs, NextArgs = LeftArgs; $p -> [OneArgs | LeftArgs] = Args, As = OneArgs, NextArgs = LeftArgs; - $W -> [OneArgs | LeftArgs] = Args, [Depth | LastArgs] = LeftArgs, As = [OneArgs, Depth], NextArgs = LastArgs; - $P -> [OneArgs | LeftArgs] = Args, [Depth | LastArgs] = LeftArgs, As = [OneArgs, Depth], NextArgs = LastArgs; + $W -> [OneArgs | LeftArgs] = Args, [Depth | LastArgs] = LeftArgs, As = {OneArgs, Depth}, NextArgs = LastArgs; + $P -> [OneArgs | LeftArgs] = Args, [Depth | LastArgs] = LeftArgs, As = {OneArgs, Depth}, NextArgs = LastArgs; $s -> [OneArgs | LeftArgs] = Args, As = OneArgs, NextArgs = LeftArgs; $e -> [OneArgs | LeftArgs] = Args, As = OneArgs, NextArgs = LeftArgs; $f -> [OneArgs | LeftArgs] = Args, As = OneArgs, NextArgs = LeftArgs; $g -> [OneArgs | LeftArgs] = Args, As = OneArgs, NextArgs = LeftArgs; $b -> [OneArgs | LeftArgs] = Args, As = OneArgs, NextArgs = LeftArgs; $B -> [OneArgs | LeftArgs] = Args, As = OneArgs, NextArgs = LeftArgs; - $x -> [OneArgs | LeftArgs] = Args, [Prefix | LastArgs] = LeftArgs, As = [OneArgs, Prefix], NextArgs = LastArgs; - $X -> [OneArgs | LeftArgs] = Args, [Prefix | LastArgs] = LeftArgs, As = [OneArgs, Prefix], NextArgs = LastArgs; + $x -> [OneArgs | LeftArgs] = Args, [Prefix | LastArgs] = LeftArgs, As = {OneArgs, Prefix}, NextArgs = LastArgs; + $X -> [OneArgs | LeftArgs] = Args, [Prefix | LastArgs] = LeftArgs, As = {OneArgs, Prefix}, NextArgs = LastArgs; $+ -> [OneArgs | LeftArgs] = Args, As = OneArgs, NextArgs = LeftArgs; $# -> [OneArgs | LeftArgs] = Args, As = OneArgs, NextArgs = LeftArgs; $c -> [OneArgs | LeftArgs] = Args, As = OneArgs, NextArgs = LeftArgs; @@ -494,39 +530,39 @@ buildSmall([OneCA | Cs], CharsLimit, P, S, W, Other, Acc) -> end end. -ctlSmall($s, Args, Width, Adjust, Precision, PadChar, Encoding) when is_atom(Args) -> - case Encoding of - latin1 -> - AtomBinStr = ?writeAtom(Args, latin1); - _ -> - AtomBinStr = ?writeAtom(Args, uft8) - end, - string(AtomBinStr, Width, Adjust, Precision, PadChar, Encoding); -ctlSmall($e, Args, Width, Adjust, Precision, PadChar, _Encoding) when is_float(Args) -> +ctlSmall($e, Args, Width, Adjust, Precision, PadChar, _Encoding) -> floatE(Args, Width, Adjust, Precision, PadChar); -ctlSmall($f, Args, Width, Adjust, Precision, PadChar, _Encoding) when is_float(Args) -> +ctlSmall($f, Args, Width, Adjust, Precision, PadChar, _Encoding) -> floatF(Args, Width, Adjust, Precision, PadChar); -ctlSmall($g, Args, Width, Adjust, Precision, PadChar, _Encoding) when is_float(Args) -> +ctlSmall($g, Args, Width, Adjust, Precision, PadChar, _Encoding) -> floatG(Args, Width, Adjust, Precision, PadChar); -ctlSmall($b, Args, Width, Adjust, Precision, PadChar, _Encoding) when is_integer(Args) -> +ctlSmall($b, Args, Width, Adjust, Precision, PadChar, _Encoding) -> unPrefixedInt(Args, Width, Adjust, ?base(Precision), PadChar, true); -ctlSmall($B, Args, Width, Adjust, Precision, PadChar, _Encoding) when is_integer(Args) -> +ctlSmall($B, Args, Width, Adjust, Precision, PadChar, _Encoding) -> unPrefixedInt(Args, Width, Adjust, ?base(Precision), PadChar, false); -ctlSmall($x, [Args, Prefix], Width, Adjust, Precision, PadChar, _Encoding) when is_integer(Args), is_atom(Prefix) -> - prefixedInt(Args, Width, Adjust, ?base(Precision), PadChar, atom_to_binary(Prefix, utf8), true); -ctlSmall($x, [Args, Prefix], Width, Adjust, Precision, PadChar, _Encoding) when is_integer(Args) -> - prefixedInt(Args, Width, Adjust, ?base(Precision), PadChar, Prefix, true); -ctlSmall($X, [Args, Prefix], Width, Adjust, Precision, PadChar, _Encoding) when is_integer(Args), is_atom(Prefix) -> - prefixedInt(Args, Width, Adjust, ?base(Precision), PadChar, atom_to_binary(Prefix, utf8), false); -ctlSmall($X, [Args, Prefix], Width, Adjust, Precision, PadChar, _Encoding) when is_integer(Args) -> - prefixedInt(Args, Width, Adjust, ?base(Precision), PadChar, Prefix, false); -ctlSmall($+, Args, Width, Adjust, Precision, PadChar, _Encoding) when is_integer(Args) -> +ctlSmall($x, {Args, Prefix}, Width, Adjust, Precision, PadChar, _Encoding) -> + case is_atom(Prefix) of + true -> + prefixedInt(Args, Width, Adjust, ?base(Precision), PadChar, atom_to_binary(Prefix, utf8), true); + _ -> + prefixedInt(Args, Width, Adjust, ?base(Precision), PadChar, Prefix, true) + + end; +ctlSmall($X, {Args, Prefix}, Width, Adjust, Precision, PadChar, _Encoding) -> + case is_atom(Prefix) of + true -> + prefixedInt(Args, Width, Adjust, ?base(Precision), PadChar, atom_to_binary(Prefix, utf8), false); + _ -> + Base = ?base(Precision), + prefixedInt(Args, Width, Adjust, Base, PadChar, integer_to_binary(Base), $#, true) + end; +ctlSmall($+, Args, Width, Adjust, Precision, PadChar, _Encoding) -> Base = ?base(Precision), prefixedInt(Args, Width, Adjust, Base, PadChar, integer_to_binary(Base), $#, true); -ctlSmall($#, Args, Width, Adjust, Precision, PadChar, _Encoding) when is_integer(Args) -> +ctlSmall($#, Args, Width, Adjust, Precision, PadChar, _Encoding) -> Base = ?base(Precision), prefixedInt(Args, Width, Adjust, Base, PadChar, integer_to_binary(Base), $#, false); -ctlSmall($c, Args, Width, Adjust, Precision, PadChar, Encoding) when is_integer(Args) -> +ctlSmall($c, Args, Width, Adjust, Precision, PadChar, Encoding) -> case Encoding of unicode -> char(Args, Width, Adjust, Precision, PadChar); @@ -536,6 +572,14 @@ ctlSmall($c, Args, Width, Adjust, Precision, PadChar, Encoding) when is_integer( ctlSmall($~, _Args, Width, Adjust, Precision, PadChar, _Encoding) -> char($~, Width, Adjust, Precision, PadChar); ctlSmall($n, _Args, Width, Adjust, Precision, PadChar, _Encoding) -> newline(Width, Adjust, Precision, PadChar); ctlSmall($i, _Args, _Width, _Adjust, _Precision, _PadChar, _Encoding) -> ignore; +ctlSmall($s, Args, Width, Adjust, Precision, PadChar, Encoding) when is_atom(Args) -> + case Encoding of + latin1 -> + AtomBinStr = ?writeAtom(Args, latin1); + _ -> + AtomBinStr = ?writeAtom(Args, uft8) + end, + string(AtomBinStr, Width, Adjust, Precision, PadChar, Encoding); ctlSmall(_C, _Args, _Width, _Adjust, _Precision, _PadChar, _Encoding) -> not_small. buildLimited([], _, _, _, _, Acc) -> Acc; @@ -606,24 +650,6 @@ term(BinStrOrIoList, Width, Adjust, Precision, PadChar) -> end end. -print(Term, Depth, Width, _Adjust, Precision, _PadChar, Encoding, Strings, CharsLimit, I) -> - if - Width == none -> - if - Precision == none -> - print(Term, I + 1, ?LineCCnt, Depth, -1, CharsLimit, no_fun, Encoding, Strings); - true -> - print(Term, Precision, ?LineCCnt, Depth, -1, CharsLimit, no_fun, Encoding, Strings) - end; - true -> - if - Precision == none -> - print(Term, I + 1, Width, Depth, -1, CharsLimit, no_fun, Encoding, Strings); - true -> - print(Term, Precision, Width, Depth, -1, CharsLimit, no_fun, Encoding, Strings) - end - end. - floatE(Float, Width, Adjust, Precision, PadChar) -> NewPrecision = ?IIF(Precision == none, 6, Precision), @@ -795,13 +821,6 @@ remainChars(T, E) -> 0 end. %% ********************************************** eFmtFormat end ***************************************************** -%% ********************************************** eFmtPretty start ***************************************************** -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%dfsdfsdfsdfsdff start - - -%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%dfsdfsdfsdfsdff start - -%% ********************************************** eFmtPretty end ***************************************************** %% ********************************************** utils start ********************************************************** toLowerStr(BinStr) -> <