瀏覽代碼

ft: 部分代码调整

master
SisMaker 4 年之前
父節點
當前提交
77433df581
共有 2 個檔案被更改,包括 57 行新增33 行删除
  1. +38
    -33
      src/eFmt.erl
  2. +19
    -0
      src/test/recon-2.5.1/test.erl

+ 38
- 33
src/eFmt.erl 查看文件

@ -1,5 +1,7 @@
-module(eFmt). -module(eFmt).
-import(binary, [split/2, part/3]).
-compile(inline). -compile(inline).
-compile({inline_size, 128}). -compile({inline_size, 128}).
@ -51,8 +53,8 @@
format(Format, Args) -> format(Format, Args) ->
try fWrite(Format, Args) try fWrite(Format, Args)
catch catch
_C:_R:S ->
erlang:error(badarg, [Format, Args, _C, _R, S])
_C:_R ->
erlang:error(badarg, [Format, Args, _C, _R])
end. end.
-spec format(Format :: io:format(), Data :: [term()], Options :: [{charsLimit, CharsLimit :: charsLimit()}]) -> chars(). -spec format(Format :: io:format(), Data :: [term()], Options :: [{charsLimit, CharsLimit :: charsLimit()}]) -> chars().
@ -115,7 +117,7 @@ write(Term, Depth) ->
write(Term, Depth, IsPretty) -> write(Term, Depth, IsPretty) ->
case IsPretty of case IsPretty of
true -> true ->
eFmtPretty:pPrint(Term, 1, ?LineCCnt, Depth);
writeTerm(Term, Depth, ?LineCCnt, latin1, true);
_ -> _ ->
writeTerm(Term, Depth, latin1) writeTerm(Term, Depth, latin1)
end. end.
@ -129,12 +131,12 @@ write(Term, Depth, Encoding, CharsLimit) ->
writeTerm(Term, Depth, Encoding); writeTerm(Term, Depth, Encoding);
true -> true ->
BinTerm = writeTerm(Term, Depth, ?LineCCnt, Encoding, false), BinTerm = writeTerm(Term, Depth, ?LineCCnt, Encoding, false),
BinTermSize = erlang:byte_size(BinTerm),
BinTermSize = byte_size(BinTerm),
if if
CharsLimit < 0 -> CharsLimit < 0 ->
BinTerm; BinTerm;
BinTermSize > CharsLimit -> BinTermSize > CharsLimit ->
<<(binary:part(BinTerm, 0, CharsLimit))/binary, "...">>;
<<(part(BinTerm, 0, CharsLimit))/binary, "...">>;
true -> true ->
BinTerm BinTerm
end end
@ -146,12 +148,12 @@ write(Term, Depth, Width, CharsLimit, Encoding, Strings) ->
<<"...">>; <<"...">>;
true -> true ->
BinTerm = writeTerm(Term, Depth, Width, Encoding, Strings), BinTerm = writeTerm(Term, Depth, Width, Encoding, Strings),
BinTermSize = erlang:byte_size(BinTerm),
BinTermSize = byte_size(BinTerm),
if if
CharsLimit < 0 -> CharsLimit < 0 ->
BinTerm; BinTerm;
BinTermSize > CharsLimit -> BinTermSize > CharsLimit ->
<<(binary:part(BinTerm, 0, CharsLimit))/binary, "...">>;
<<(part(BinTerm, 0, CharsLimit))/binary, "...">>;
true -> true ->
BinTerm BinTerm
end end
@ -159,9 +161,9 @@ write(Term, Depth, Width, CharsLimit, Encoding, Strings) ->
-define(writeInt(Int), integer_to_binary(Term)). -define(writeInt(Int), integer_to_binary(Term)).
-define(writeFloat(Float), floatG(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))). -define(writeFun(Fun), list_to_binary(erlang:fun_to_list(Fun))).
writeAtom(Atom, Encoding) -> writeAtom(Atom, Encoding) ->
@ -205,7 +207,7 @@ writeList([One | List], D, E, BinAcc) ->
writeList(List, D - 1, E, <<BinAcc/binary, (writeTerm(One, D, E))/binary, ",">>) writeList(List, D - 1, E, <<BinAcc/binary, (writeTerm(One, D, E))/binary, ",">>)
end; end;
writeList(Other, D, E, BinAcc) -> writeList(Other, D, E, BinAcc) ->
NewBinAcc = binary:part(BinAcc, 0, erlang:byte_size(BinAcc) - 1),
NewBinAcc = part(BinAcc, 0, byte_size(BinAcc) - 1),
<<NewBinAcc/binary, "|", (writeTerm(Other, D, E))/binary, "]">>. <<NewBinAcc/binary, "|", (writeTerm(Other, D, E))/binary, "]">>.
writeTuple(Tuple, D, E, Index, TupleSize, BinAcc) -> writeTuple(Tuple, D, E, Index, TupleSize, BinAcc) ->
@ -240,7 +242,7 @@ writeMapBody(I, D, E, BinAcc) ->
<<BinAcc/binary, (writeTerm(K, -1, E))/binary, " => ", (writeTerm(V, D, E))/binary, "}">>; <<BinAcc/binary, (writeTerm(K, -1, E))/binary, " => ", (writeTerm(V, D, E))/binary, "}">>;
{K, V, NextI} -> {K, V, NextI} ->
writeMapBody(NextI, D - 1, E, <<BinAcc/binary, (writeTerm(K, -1, E))/binary, " => ", (writeTerm(V, D, E))/binary, ",">>); writeMapBody(NextI, D - 1, E, <<BinAcc/binary, (writeTerm(K, -1, E))/binary, " => ", (writeTerm(V, D, E))/binary, ",">>);
none ->
_ ->
<<BinAcc/binary, "}">> <<BinAcc/binary, "}">>
end end
end. end.
@ -281,7 +283,7 @@ writeList([], _Depth, _Width, _Encoding, _Strings, _SumLC, BinAcc) ->
<<BinAcc/binary, "]">>; <<BinAcc/binary, "]">>;
writeList([One], Depth, Width, Encoding, Strings, SumLC, BinAcc) -> writeList([One], Depth, Width, Encoding, Strings, SumLC, BinAcc) ->
TermBin = writeTerm(One, Depth, Width, Encoding, Strings), TermBin = writeTerm(One, Depth, Width, Encoding, Strings),
TermBinBinSize = erlang:byte_size(TermBin),
TermBinBinSize = byte_size(TermBin),
NewSumLC = SumLC + TermBinBinSize, NewSumLC = SumLC + TermBinBinSize,
case NewSumLC >= Width of case NewSumLC >= Width of
true -> true ->
@ -295,7 +297,7 @@ writeList([One | List], Depth, Width, Encoding, Strings, SumLC, BinAcc) ->
Depth =:= 1 -> <<BinAcc, "|...]">>; Depth =:= 1 -> <<BinAcc, "|...]">>;
true -> true ->
TermBin = writeTerm(One, Depth, Width, Encoding, Strings), TermBin = writeTerm(One, Depth, Width, Encoding, Strings),
TermBinBinSize = erlang:byte_size(TermBin),
TermBinBinSize = byte_size(TermBin),
NewSumLC = SumLC + TermBinBinSize, NewSumLC = SumLC + TermBinBinSize,
case NewSumLC >= Width of case NewSumLC >= Width of
true -> true ->
@ -306,9 +308,9 @@ writeList([One | List], Depth, Width, Encoding, Strings, SumLC, BinAcc) ->
end; end;
writeList(Other, Depth, Width, Encoding, Strings, SumLC, BinAcc) -> writeList(Other, Depth, Width, Encoding, Strings, SumLC, BinAcc) ->
TermBin = writeTerm(Other, Depth, Width, Encoding, Strings), TermBin = writeTerm(Other, Depth, Width, Encoding, Strings),
TermBinBinSize = erlang:byte_size(TermBin),
TermBinBinSize = byte_size(TermBin),
NewSumLC = SumLC + TermBinBinSize, 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 case NewSumLC >= Width of
true -> true ->
<<NewBinAcc/binary, "|", TermBin/binary, "]\n">>; <<NewBinAcc/binary, "|", TermBin/binary, "]\n">>;
@ -323,7 +325,7 @@ writeTuple(Tuple, Depth, Width, Encoding, Strings, Index, TupleSize, SumLC, BinA
if if
Index < TupleSize -> Index < TupleSize ->
TermBin = writeTerm(element(Index, Tuple), Depth, Width, Encoding, Strings), TermBin = writeTerm(element(Index, Tuple), Depth, Width, Encoding, Strings),
TermBinBinSize = erlang:byte_size(TermBin),
TermBinBinSize = byte_size(TermBin),
NewSumLC = SumLC + TermBinBinSize, NewSumLC = SumLC + TermBinBinSize,
case NewSumLC >= Width of case NewSumLC >= Width of
true -> true ->
@ -333,7 +335,7 @@ writeTuple(Tuple, Depth, Width, Encoding, Strings, Index, TupleSize, SumLC, BinA
end; end;
Index == TupleSize -> Index == TupleSize ->
TermBin = writeTerm(element(Index, Tuple), Depth, Width, Encoding, Strings), TermBin = writeTerm(element(Index, Tuple), Depth, Width, Encoding, Strings),
TermBinBinSize = erlang:byte_size(TermBin),
TermBinBinSize = byte_size(TermBin),
NewSumLC = SumLC + TermBinBinSize, NewSumLC = SumLC + TermBinBinSize,
case NewSumLC >= Width of case NewSumLC >= Width of
true -> true ->
@ -363,7 +365,7 @@ writeMapBody(I, Depth, Width, Encoding, Strings, SumLC, BinAcc) ->
{K, V, none} -> {K, V, none} ->
KeyTermBin = writeTerm(K, -1, Width, Encoding, Strings), KeyTermBin = writeTerm(K, -1, Width, Encoding, Strings),
ValueTermBin = writeTerm(V, -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, NewSumLC = SumLC + TermBinBinSize,
case NewSumLC >= Width of case NewSumLC >= Width of
true -> true ->
@ -374,7 +376,7 @@ writeMapBody(I, Depth, Width, Encoding, Strings, SumLC, BinAcc) ->
{K, V, NextI} -> {K, V, NextI} ->
KeyTermBin = writeTerm(K, -1, Width, Encoding, Strings), KeyTermBin = writeTerm(K, -1, Width, Encoding, Strings),
ValueTermBin = writeTerm(V, -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, NewSumLC = SumLC + TermBinBinSize,
case NewSumLC >= Width of case NewSumLC >= Width of
true -> true ->
@ -382,7 +384,7 @@ writeMapBody(I, Depth, Width, Encoding, Strings, SumLC, BinAcc) ->
_ -> _ ->
writeMapBody(NextI, Depth - 1, Width, Encoding, Strings, NewSumLC, <<BinAcc/binary, KeyTermBin/binary, " => ", ValueTermBin/binary, ",">>) writeMapBody(NextI, Depth - 1, Width, Encoding, Strings, NewSumLC, <<BinAcc/binary, KeyTermBin/binary, " => ", ValueTermBin/binary, ",">>)
end; end;
none ->
_ ->
<<BinAcc/binary, "}">> <<BinAcc/binary, "}">>
end end
end. end.
@ -409,7 +411,7 @@ writeBinary(Bin, Depth, Width, Encoding, Strings, SumLC, BinAcc) ->
<<BinAcc/binary, (integer_to_binary(Int))/binary, ">>">>; <<BinAcc/binary, (integer_to_binary(Int))/binary, ">>">>;
<<Int:8, LeftBin/bitstring>> -> <<Int:8, LeftBin/bitstring>> ->
TermBin = integer_to_binary(Int), TermBin = integer_to_binary(Int),
TermBinBinSize = erlang:byte_size(TermBin),
TermBinBinSize = byte_size(TermBin),
NewSumLC = SumLC + TermBinBinSize, NewSumLC = SumLC + TermBinBinSize,
case NewSumLC >= Width of case NewSumLC >= Width of
true -> 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_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_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_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_pid(Term) -> ?writePid(Term);
writeTerm(Term, _Depth, _Width, _Encoding, _Strings) when is_float(Term) -> ?writeFloat(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); writeTerm(Term, _Depth, _Width, _Encoding, _Strings) when is_port(Term) -> ?writePort(Term);
@ -480,7 +485,7 @@ fScan(Format, Args) ->
end. end.
doCollect(FmtBinStr, Args, Acc) -> doCollect(FmtBinStr, Args, Acc) ->
case binary:split(FmtBinStr, <<"~">>) of
case split(FmtBinStr, <<"~">>) of
[NotMatch] -> [NotMatch] ->
true = [] == Args, true = [] == Args,
?IIF(NotMatch == <<>>, Acc, [NotMatch | Acc]); ?IIF(NotMatch == <<>>, Acc, [NotMatch | Acc]);
@ -615,7 +620,7 @@ buildSmall([], CharsLimit, P, S, W, Other, Acc) ->
case buildLimited(Acc, P, NumOfLimited, RemainChars, 0, []) of case buildLimited(Acc, P, NumOfLimited, RemainChars, 0, []) of
[] -> [] ->
[]; [];
[_One] = Ret ->
[_One] = Ret ->
Ret; Ret;
[One, Two] -> [One, Two] ->
[Two, One]; [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) -> ctlLimited($s, Args, Width, Adjust, Precision, PadChar, Encoding, _Strings, CharsLimit, _I) ->
case Encoding of case Encoding of
latin1 -> latin1 ->
BinStr = erlang:iolist_to_binary(Args);
BinStr = iolist_to_binary(Args);
_ -> _ ->
BinStr = BinStr =
case catch unicode:characters_to_binary(Args, unicode) of case catch unicode:characters_to_binary(Args, unicode) of
@ -769,7 +774,7 @@ term(BinStrOrIoList, Width, Adjust, Precision, PadChar) ->
BinStrOrIoList; BinStrOrIoList;
Width == none -> Width == none ->
StrLen = charsLen(BinStrOrIoList), StrLen = charsLen(BinStrOrIoList),
NewPrecision = erlang:min(StrLen, Precision),
NewPrecision = min(StrLen, Precision),
if if
StrLen > NewPrecision -> StrLen > NewPrecision ->
adjust(Adjust, makePadChars($*, NewPrecision, <<>>), <<>>); adjust(Adjust, makePadChars($*, NewPrecision, <<>>), <<>>);
@ -778,7 +783,7 @@ term(BinStrOrIoList, Width, Adjust, Precision, PadChar) ->
end; end;
true -> true ->
StrLen = charsLen(BinStrOrIoList), 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 if
StrLen > NewPrecision -> StrLen > NewPrecision ->
adjust(Adjust, makePadChars($*, NewPrecision, <<>>), makePadChars(PadChar, Width - NewPrecision, <<>>)); adjust(Adjust, makePadChars($*, NewPrecision, <<>>), makePadChars(PadChar, Width - NewPrecision, <<>>));
@ -826,12 +831,12 @@ strToChars(BinStr, Width, CharsLimit) ->
true -> true ->
BinStr; BinStr;
_ -> _ ->
<<(binary:part(BinStr, 0, CharsLimit))/binary, "...">>
<<(part(BinStr, 0, CharsLimit))/binary, "...">>
end; end;
CharsLimit < 0 orelse CharsLimit >= Width -> CharsLimit < 0 orelse CharsLimit >= Width ->
BinStr; BinStr;
true -> true ->
<<(binary:part(BinStr, 0, CharsLimit))/binary, "...">>
<<(part(BinStr, 0, CharsLimit))/binary, "...">>
end. end.
string(Str, Width, Adjust, Precision, PadChar, Encoding) -> string(Str, Width, Adjust, Precision, PadChar, Encoding) ->
@ -870,7 +875,7 @@ strField(Str, Width, Adjust, StrLen, PadChar, Encoding) when StrLen > Width ->
end. end.
flatTrunc(List, Width, _Encoding) -> flatTrunc(List, Width, _Encoding) ->
binary:part(iolist_to_binary(List), 0, Width).
part(iolist_to_binary(List), 0, Width).
makePadChars(PadChar, Cnt, BinStr) -> makePadChars(PadChar, Cnt, BinStr) ->
case Cnt > 0 of case Cnt > 0 of

+ 19
- 0
src/test/recon-2.5.1/test.erl 查看文件

@ -397,4 +397,23 @@ tt_p22() ->
eFmt:format("~p", [get_test2()]), eFmt:format("~p", [get_test2()]),
?MEM_INFO_PRINT(0). ?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).

Loading…
取消
儲存