From 77433df581950d3eaad454a651c8f9215192f9a6 Mon Sep 17 00:00:00 2001 From: SisMaker <1713699517@qq.com> Date: Sun, 28 Mar 2021 17:23:26 +0800 Subject: [PATCH] =?UTF-8?q?ft:=20=E9=83=A8=E5=88=86=E4=BB=A3=E7=A0=81?= =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/eFmt.erl | 71 +++++++++++++++++++---------------- src/test/recon-2.5.1/test.erl | 19 ++++++++++ 2 files changed, 57 insertions(+), 33 deletions(-) diff --git a/src/eFmt.erl b/src/eFmt.erl index 1e9c495..0428b70 100644 --- a/src/eFmt.erl +++ b/src/eFmt.erl @@ -1,5 +1,7 @@ -module(eFmt). +-import(binary, [split/2, part/3]). + -compile(inline). -compile({inline_size, 128}). @@ -51,8 +53,8 @@ format(Format, Args) -> try fWrite(Format, Args) catch - _C:_R:S -> - erlang:error(badarg, [Format, Args, _C, _R, S]) + _C:_R -> + erlang:error(badarg, [Format, Args, _C, _R]) end. -spec format(Format :: io:format(), Data :: [term()], Options :: [{charsLimit, CharsLimit :: charsLimit()}]) -> chars(). @@ -115,7 +117,7 @@ write(Term, Depth) -> write(Term, Depth, IsPretty) -> case IsPretty of true -> - eFmtPretty:pPrint(Term, 1, ?LineCCnt, Depth); + writeTerm(Term, Depth, ?LineCCnt, latin1, true); _ -> writeTerm(Term, Depth, latin1) end. @@ -129,12 +131,12 @@ write(Term, Depth, Encoding, CharsLimit) -> writeTerm(Term, Depth, Encoding); true -> BinTerm = writeTerm(Term, Depth, ?LineCCnt, Encoding, false), - BinTermSize = erlang:byte_size(BinTerm), + BinTermSize = byte_size(BinTerm), if CharsLimit < 0 -> BinTerm; BinTermSize > CharsLimit -> - <<(binary:part(BinTerm, 0, CharsLimit))/binary, "...">>; + <<(part(BinTerm, 0, CharsLimit))/binary, "...">>; true -> BinTerm end @@ -146,12 +148,12 @@ write(Term, Depth, Width, CharsLimit, Encoding, Strings) -> <<"...">>; true -> BinTerm = writeTerm(Term, Depth, Width, Encoding, Strings), - BinTermSize = erlang:byte_size(BinTerm), + BinTermSize = byte_size(BinTerm), if CharsLimit < 0 -> BinTerm; BinTermSize > CharsLimit -> - <<(binary:part(BinTerm, 0, CharsLimit))/binary, "...">>; + <<(part(BinTerm, 0, CharsLimit))/binary, "...">>; true -> BinTerm end @@ -159,9 +161,9 @@ write(Term, Depth, Width, CharsLimit, Encoding, Strings) -> -define(writeInt(Int), integer_to_binary(Term)). -define(writeFloat(Float), floatG(Term)). --define(writePort(Port), list_to_binary(erlang:port_to_list(Port))). --define(writeRef(Ref), list_to_binary(erlang:ref_to_list(Ref))). --define(writePid(Ref), list_to_binary(erlang:pid_to_list(Ref))). +-define(writePort(Port), list_to_binary(port_to_list(Port))). +-define(writeRef(Ref), list_to_binary(ref_to_list(Ref))). +-define(writePid(Ref), list_to_binary(pid_to_list(Ref))). -define(writeFun(Fun), list_to_binary(erlang:fun_to_list(Fun))). writeAtom(Atom, Encoding) -> @@ -205,7 +207,7 @@ writeList([One | List], D, E, BinAcc) -> writeList(List, D - 1, E, <>) end; writeList(Other, D, E, BinAcc) -> - NewBinAcc = binary:part(BinAcc, 0, erlang:byte_size(BinAcc) - 1), + NewBinAcc = part(BinAcc, 0, byte_size(BinAcc) - 1), <>. writeTuple(Tuple, D, E, Index, TupleSize, BinAcc) -> @@ -240,7 +242,7 @@ writeMapBody(I, D, E, BinAcc) -> < ", (writeTerm(V, D, E))/binary, "}">>; {K, V, NextI} -> writeMapBody(NextI, D - 1, E, < ", (writeTerm(V, D, E))/binary, ",">>); - none -> + _ -> <> end end. @@ -281,7 +283,7 @@ writeList([], _Depth, _Width, _Encoding, _Strings, _SumLC, BinAcc) -> <>; writeList([One], Depth, Width, Encoding, Strings, SumLC, BinAcc) -> TermBin = writeTerm(One, Depth, Width, Encoding, Strings), - TermBinBinSize = erlang:byte_size(TermBin), + TermBinBinSize = byte_size(TermBin), NewSumLC = SumLC + TermBinBinSize, case NewSumLC >= Width of true -> @@ -295,7 +297,7 @@ writeList([One | List], Depth, Width, Encoding, Strings, SumLC, BinAcc) -> Depth =:= 1 -> <>; true -> TermBin = writeTerm(One, Depth, Width, Encoding, Strings), - TermBinBinSize = erlang:byte_size(TermBin), + TermBinBinSize = byte_size(TermBin), NewSumLC = SumLC + TermBinBinSize, case NewSumLC >= Width of true -> @@ -306,9 +308,9 @@ writeList([One | List], Depth, Width, Encoding, Strings, SumLC, BinAcc) -> end; writeList(Other, Depth, Width, Encoding, Strings, SumLC, BinAcc) -> TermBin = writeTerm(Other, Depth, Width, Encoding, Strings), - TermBinBinSize = erlang:byte_size(TermBin), + TermBinBinSize = byte_size(TermBin), NewSumLC = SumLC + TermBinBinSize, - NewBinAcc = binary:part(BinAcc, 0, erlang:byte_size(BinAcc) - 1), + NewBinAcc = part(BinAcc, 0, byte_size(BinAcc) - 1), case NewSumLC >= Width of true -> <>; @@ -323,7 +325,7 @@ writeTuple(Tuple, Depth, Width, Encoding, Strings, Index, TupleSize, SumLC, BinA if Index < TupleSize -> TermBin = writeTerm(element(Index, Tuple), Depth, Width, Encoding, Strings), - TermBinBinSize = erlang:byte_size(TermBin), + TermBinBinSize = byte_size(TermBin), NewSumLC = SumLC + TermBinBinSize, case NewSumLC >= Width of true -> @@ -333,7 +335,7 @@ writeTuple(Tuple, Depth, Width, Encoding, Strings, Index, TupleSize, SumLC, BinA end; Index == TupleSize -> TermBin = writeTerm(element(Index, Tuple), Depth, Width, Encoding, Strings), - TermBinBinSize = erlang:byte_size(TermBin), + TermBinBinSize = byte_size(TermBin), NewSumLC = SumLC + TermBinBinSize, case NewSumLC >= Width of true -> @@ -363,7 +365,7 @@ writeMapBody(I, Depth, Width, Encoding, Strings, SumLC, BinAcc) -> {K, V, none} -> KeyTermBin = writeTerm(K, -1, Width, Encoding, Strings), ValueTermBin = writeTerm(V, -1, Width, Encoding, Strings), - TermBinBinSize = erlang:byte_size(KeyTermBin) + erlang:byte_size(ValueTermBin), + TermBinBinSize = byte_size(KeyTermBin) + byte_size(ValueTermBin), NewSumLC = SumLC + TermBinBinSize, case NewSumLC >= Width of true -> @@ -374,7 +376,7 @@ writeMapBody(I, Depth, Width, Encoding, Strings, SumLC, BinAcc) -> {K, V, NextI} -> KeyTermBin = writeTerm(K, -1, Width, Encoding, Strings), ValueTermBin = writeTerm(V, -1, Width, Encoding, Strings), - TermBinBinSize = erlang:byte_size(KeyTermBin) + erlang:byte_size(ValueTermBin), + TermBinBinSize = byte_size(KeyTermBin) + byte_size(ValueTermBin), NewSumLC = SumLC + TermBinBinSize, case NewSumLC >= Width of true -> @@ -382,7 +384,7 @@ writeMapBody(I, Depth, Width, Encoding, Strings, SumLC, BinAcc) -> _ -> writeMapBody(NextI, Depth - 1, Width, Encoding, Strings, NewSumLC, < ", ValueTermBin/binary, ",">>) end; - none -> + _ -> <> end end. @@ -409,7 +411,7 @@ writeBinary(Bin, Depth, Width, Encoding, Strings, SumLC, BinAcc) -> <>">>; <> -> TermBin = integer_to_binary(Int), - TermBinBinSize = erlang:byte_size(TermBin), + TermBinBinSize = byte_size(TermBin), NewSumLC = SumLC + TermBinBinSize, case NewSumLC >= Width of true -> @@ -444,9 +446,12 @@ 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, 0, <<"#{">>); -writeTerm(Term, Depth, Width, Encoding, Strings) when is_tuple(Term) -> writeTuple(Term, Depth, Width, Encoding, Strings, 1, tuple_size(Term), 0, <<"{">>); -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, tuple_size(Term), 0, <<"{">>); +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); @@ -480,7 +485,7 @@ fScan(Format, Args) -> end. doCollect(FmtBinStr, Args, Acc) -> - case binary:split(FmtBinStr, <<"~">>) of + case split(FmtBinStr, <<"~">>) of [NotMatch] -> true = [] == Args, ?IIF(NotMatch == <<>>, Acc, [NotMatch | Acc]); @@ -615,7 +620,7 @@ buildSmall([], CharsLimit, P, S, W, Other, Acc) -> case buildLimited(Acc, P, NumOfLimited, RemainChars, 0, []) of [] -> []; - [_One] = Ret -> + [_One] = Ret -> Ret; [One, Two] -> [Two, One]; @@ -742,7 +747,7 @@ buildLimited([OneCA | Cs], NumOfPs, Count, MaxLen, I, Acc) -> ctlLimited($s, Args, Width, Adjust, Precision, PadChar, Encoding, _Strings, CharsLimit, _I) -> case Encoding of latin1 -> - BinStr = erlang:iolist_to_binary(Args); + BinStr = iolist_to_binary(Args); _ -> BinStr = case catch unicode:characters_to_binary(Args, unicode) of @@ -769,7 +774,7 @@ term(BinStrOrIoList, Width, Adjust, Precision, PadChar) -> BinStrOrIoList; Width == none -> StrLen = charsLen(BinStrOrIoList), - NewPrecision = erlang:min(StrLen, Precision), + NewPrecision = min(StrLen, Precision), if StrLen > NewPrecision -> adjust(Adjust, makePadChars($*, NewPrecision, <<>>), <<>>); @@ -778,7 +783,7 @@ term(BinStrOrIoList, Width, Adjust, Precision, PadChar) -> end; true -> StrLen = charsLen(BinStrOrIoList), - NewPrecision = erlang:min(StrLen, case Precision of none -> Width; _ -> min(Precision, Width) end), + NewPrecision = min(StrLen, case Precision of none -> Width; _ -> min(Precision, Width) end), if StrLen > NewPrecision -> adjust(Adjust, makePadChars($*, NewPrecision, <<>>), makePadChars(PadChar, Width - NewPrecision, <<>>)); @@ -826,12 +831,12 @@ strToChars(BinStr, Width, CharsLimit) -> true -> BinStr; _ -> - <<(binary:part(BinStr, 0, CharsLimit))/binary, "...">> + <<(part(BinStr, 0, CharsLimit))/binary, "...">> end; CharsLimit < 0 orelse CharsLimit >= Width -> BinStr; true -> - <<(binary:part(BinStr, 0, CharsLimit))/binary, "...">> + <<(part(BinStr, 0, CharsLimit))/binary, "...">> end. string(Str, Width, Adjust, Precision, PadChar, Encoding) -> @@ -870,7 +875,7 @@ strField(Str, Width, Adjust, StrLen, PadChar, Encoding) when StrLen > Width -> end. flatTrunc(List, Width, _Encoding) -> - binary:part(iolist_to_binary(List), 0, Width). + part(iolist_to_binary(List), 0, Width). makePadChars(PadChar, Cnt, BinStr) -> case Cnt > 0 of diff --git a/src/test/recon-2.5.1/test.erl b/src/test/recon-2.5.1/test.erl index a4073d6..4d84dad 100644 --- a/src/test/recon-2.5.1/test.erl +++ b/src/test/recon-2.5.1/test.erl @@ -397,4 +397,23 @@ tt_p22() -> eFmt:format("~p", [get_test2()]), ?MEM_INFO_PRINT(0). +-define(SQL_ROLE_CHAT_DATA_UPDATE, <<"update `role_chat` set `channels`='~s', `sensitive`=~p, `ban_times`=~p, `time`=~p where `role_id`=~p">>). + +for(0, _M, _F, _A) -> + ok; +for(N, M, F, A) -> + apply(M, F, A), + for(N - 1, M, F, A). + + +tt_S1(N) -> + ?MEM_INFO_INIT(), + for(N, io_lib, format, [?SQL_ROLE_CHAT_DATA_UPDATE, [<<"YYYY">>, [abcdef, 134, 423], {adfs, gfdgfg, "fdsfdsfs"}, #{aaaa => bbb, vvv => dddd}, self()]]), + ?MEM_INFO_PRINT(0). + +tt_S2(N) -> + ?MEM_INFO_INIT(), + for(N, eFmt, format, [?SQL_ROLE_CHAT_DATA_UPDATE, [<<"YYYY">>, [abcdef, 134, 423], {adfs, gfdgfg, "fdsfdsfs"}, #{aaaa => bbb, vvv => dddd}, self()]]), + ?MEM_INFO_PRINT(0). +