Browse Source

ft:bug fix

master
lijie 2 years ago
parent
commit
d2ce0f6526
1 changed files with 52 additions and 22 deletions
  1. +52
    -22
      src/eFmt.erl

+ 52
- 22
src/eFmt.erl View File

@ -157,6 +157,17 @@ write(Term, Depth, IsPretty) ->
writeTerm(Term, Depth, latin1) writeTerm(Term, Depth, latin1)
end. end.
splitPart(Term)when is_map(Term) ->
<<",...}">>;
splitPart(Term)when is_tuple(Term) ->
<<",...}">>;
splitPart(Term)when is_list(Term) ->
<<",...]">>;
splitPart(Term) when is_binary(Term) ->
<<",...>>">>;
splitPart(_Term) ->
<<",...">>.
-spec write(Term :: term(), Depth :: depth(), Encoding :: encoding(), CharsLimit :: charsLimit()) -> chars(). -spec write(Term :: term(), Depth :: depth(), Encoding :: encoding(), CharsLimit :: charsLimit()) -> chars().
write(Term, Depth, Encoding, CharsLimit) -> write(Term, Depth, Encoding, CharsLimit) ->
if if
@ -165,13 +176,13 @@ write(Term, Depth, Encoding, CharsLimit) ->
CharsLimit < 0 -> CharsLimit < 0 ->
writeTerm(Term, Depth, Encoding); writeTerm(Term, Depth, Encoding);
true -> true ->
BinTerm = writeTerm(Term, Depth, ?LineCCnt, Encoding, false),
BinTerm = writeTerm(Term, Depth, Encoding),
BinTermSize = byte_size(BinTerm), BinTermSize = byte_size(BinTerm),
if if
CharsLimit < 0 -> CharsLimit < 0 ->
BinTerm; BinTerm;
BinTermSize > CharsLimit -> BinTermSize > CharsLimit ->
<<(part(BinTerm, 0, CharsLimit))/binary, "...">>;
<<(part(BinTerm, 0, CharsLimit))/binary, (splitPart(Term))/binary>>;
true -> true ->
BinTerm BinTerm
end end
@ -188,7 +199,7 @@ write(Term, Depth, Width, CharsLimit, Encoding, Strings) ->
CharsLimit < 0 -> CharsLimit < 0 ->
BinTerm; BinTerm;
BinTermSize > CharsLimit -> BinTermSize > CharsLimit ->
<<(part(BinTerm, 0, CharsLimit))/binary, "...">>;
<<(part(BinTerm, 0, CharsLimit))/binary, (splitPart(Term))/binary>>;
true -> true ->
BinTerm BinTerm
end end
@ -333,7 +344,7 @@ writeList([One], Depth, Width, Encoding, Strings, SumLC, BinAcc) ->
TermBin = writeTerm(One, Depth, Width, Encoding, Strings), TermBin = writeTerm(One, Depth, Width, Encoding, Strings),
TermBinBinSize = byte_size(TermBin), TermBinBinSize = byte_size(TermBin),
NewSumLC = SumLC + TermBinBinSize, NewSumLC = SumLC + TermBinBinSize,
case NewSumLC >= Width of
case Width /= 0 andalso NewSumLC >= Width of
true -> true ->
<<BinAcc/binary, TermBin/binary, "]\n">>; <<BinAcc/binary, TermBin/binary, "]\n">>;
_ -> _ ->
@ -347,7 +358,7 @@ writeList([One | List], Depth, Width, Encoding, Strings, SumLC, BinAcc) ->
TermBin = writeTerm(One, Depth, Width, Encoding, Strings), TermBin = writeTerm(One, Depth, Width, Encoding, Strings),
TermBinBinSize = byte_size(TermBin), TermBinBinSize = byte_size(TermBin),
NewSumLC = SumLC + TermBinBinSize, NewSumLC = SumLC + TermBinBinSize,
case NewSumLC >= Width of
case Width /= 0 andalso NewSumLC >= Width of
true -> true ->
writeList(List, Depth - 1, Width, Encoding, Strings, 0, <<BinAcc/binary, TermBin/binary, ",\n">>); writeList(List, Depth - 1, Width, Encoding, Strings, 0, <<BinAcc/binary, TermBin/binary, ",\n">>);
_ -> _ ->
@ -359,7 +370,7 @@ writeList(Other, Depth, Width, Encoding, Strings, SumLC, BinAcc) ->
TermBinBinSize = byte_size(TermBin), TermBinBinSize = byte_size(TermBin),
NewSumLC = SumLC + TermBinBinSize, NewSumLC = SumLC + TermBinBinSize,
NewBinAcc = part(BinAcc, 0, byte_size(BinAcc) - 1), NewBinAcc = part(BinAcc, 0, byte_size(BinAcc) - 1),
case NewSumLC >= Width of
case Width /= 0 andalso NewSumLC >= Width of
true -> true ->
<<NewBinAcc/binary, "|", TermBin/binary, "]\n">>; <<NewBinAcc/binary, "|", TermBin/binary, "]\n">>;
_ -> _ ->
@ -375,7 +386,7 @@ writeTuple(Tuple, Depth, Width, Encoding, Strings, Index, TupleSize, SumLC, BinA
TermBin = writeTerm(element(Index, Tuple), Depth, Width, Encoding, Strings), TermBin = writeTerm(element(Index, Tuple), Depth, Width, Encoding, Strings),
TermBinBinSize = byte_size(TermBin), TermBinBinSize = byte_size(TermBin),
NewSumLC = SumLC + TermBinBinSize, NewSumLC = SumLC + TermBinBinSize,
case NewSumLC >= Width of
case Width /= 0 andalso NewSumLC >= Width of
true -> 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, 0, <<BinAcc/binary, TermBin/binary, ",\n">>);
_ -> _ ->
@ -385,7 +396,7 @@ writeTuple(Tuple, Depth, Width, Encoding, Strings, Index, TupleSize, SumLC, BinA
TermBin = writeTerm(element(Index, Tuple), Depth, Width, Encoding, Strings), TermBin = writeTerm(element(Index, Tuple), Depth, Width, Encoding, Strings),
TermBinBinSize = byte_size(TermBin), TermBinBinSize = byte_size(TermBin),
NewSumLC = SumLC + TermBinBinSize, NewSumLC = SumLC + TermBinBinSize,
case NewSumLC >= Width of
case Width /= 0 andalso NewSumLC >= Width of
true -> true ->
<<BinAcc/binary, TermBin/binary, "}\n">>; <<BinAcc/binary, TermBin/binary, "}\n">>;
_ -> _ ->
@ -415,7 +426,7 @@ writeMapBody(I, Depth, Width, Encoding, Strings, SumLC, BinAcc) ->
ValueTermBin = writeTerm(V, -1, Width, Encoding, Strings), ValueTermBin = writeTerm(V, -1, Width, Encoding, Strings),
TermBinBinSize = byte_size(KeyTermBin) + byte_size(ValueTermBin), TermBinBinSize = byte_size(KeyTermBin) + byte_size(ValueTermBin),
NewSumLC = SumLC + TermBinBinSize, NewSumLC = SumLC + TermBinBinSize,
case NewSumLC >= Width of
case Width /= 0 andalso NewSumLC >= Width of
true -> true ->
<<BinAcc/binary, KeyTermBin/binary, " => ", ValueTermBin/binary, "}\n">>; <<BinAcc/binary, KeyTermBin/binary, " => ", ValueTermBin/binary, "}\n">>;
_ -> _ ->
@ -426,7 +437,7 @@ writeMapBody(I, Depth, Width, Encoding, Strings, SumLC, BinAcc) ->
ValueTermBin = writeTerm(V, -1, Width, Encoding, Strings), ValueTermBin = writeTerm(V, -1, Width, Encoding, Strings),
TermBinBinSize = byte_size(KeyTermBin) + byte_size(ValueTermBin), TermBinBinSize = byte_size(KeyTermBin) + byte_size(ValueTermBin),
NewSumLC = SumLC + TermBinBinSize, NewSumLC = SumLC + TermBinBinSize,
case NewSumLC >= Width of
case Width /= 0 andalso NewSumLC >= Width of
true -> true ->
writeMapBody(NextI, Depth - 1, Width, Encoding, Strings, 0, <<BinAcc/binary, KeyTermBin/binary, " => ", ValueTermBin/binary, ",\n">>); writeMapBody(NextI, Depth - 1, Width, Encoding, Strings, 0, <<BinAcc/binary, KeyTermBin/binary, " => ", ValueTermBin/binary, ",\n">>);
_ -> _ ->
@ -461,7 +472,7 @@ writeBinary(Bin, Depth, Width, Encoding, Strings, SumLC, BinAcc) ->
TermBin = integer_to_binary(Int), TermBin = integer_to_binary(Int),
TermBinBinSize = byte_size(TermBin), TermBinBinSize = byte_size(TermBin),
NewSumLC = SumLC + TermBinBinSize, NewSumLC = SumLC + TermBinBinSize,
case NewSumLC >= Width of
case Width /= 0 andalso NewSumLC >= Width of
true -> true ->
writeBinary(LeftBin, Depth - 1, Width, Encoding, Strings, 0, <<BinAcc/binary, TermBin/binary, ",\n">>); writeBinary(LeftBin, Depth - 1, Width, Encoding, Strings, 0, <<BinAcc/binary, TermBin/binary, ",\n">>);
_ -> _ ->
@ -538,7 +549,7 @@ doCollect(FmtBinStr, Args, Acc) ->
true = [] == Args, true = [] == Args,
?IIF(NotMatch == <<>>, Acc, [NotMatch | Acc]); ?IIF(NotMatch == <<>>, Acc, [NotMatch | Acc]);
[FPart, LPart] -> [FPart, LPart] ->
doCollWidth(LPart, Args, 0, right, ?IIF(FPart == <<>>, Acc, [FPart | Acc]))
doCollWidth(LPart, Args, none, right, ?IIF(FPart == <<>>, Acc, [FPart | Acc]))
end. end.
doCollWidth(<<>>, _Args, _Width, _Adjust, Acc) -> doCollWidth(<<>>, _Args, _Width, _Adjust, Acc) ->
@ -547,23 +558,41 @@ doCollWidth(LPart, Args, Width, Adjust, Acc) ->
case LPart of case LPart of
<<"-*", LeftLPart/binary>> -> <<"-*", LeftLPart/binary>> ->
[WidthArgs | LeftArgs] = Args, [WidthArgs | LeftArgs] = Args,
doCollPrecision(LeftLPart, LeftArgs, WidthArgs, left, Acc);
if
WidthArgs == 0 ->
NewWidth = WidthArgs,
NewAdjust = left;
WidthArgs < 0 ->
NewWidth = -WidthArgs,
NewAdjust = right;
true ->
NewWidth = WidthArgs,
NewAdjust = left
end,
doCollPrecision(LeftLPart, LeftArgs, NewWidth, NewAdjust, Acc);
<<"-", LeftLPart/binary>> -> <<"-", LeftLPart/binary>> ->
doCollWidth(LeftLPart, Args, Width, left, Acc); doCollWidth(LeftLPart, Args, Width, left, Acc);
<<"*", LeftLPart/binary>> -> <<"*", LeftLPart/binary>> ->
[WidthArgs | LeftArgs] = Args, [WidthArgs | LeftArgs] = Args,
doCollPrecision(LeftLPart, LeftArgs, WidthArgs, right, Acc);
if
WidthArgs == 0 ->
NewWidth = WidthArgs,
NewAdjust = left;
WidthArgs < 0 ->
NewWidth = -WidthArgs,
NewAdjust = left;
true ->
NewWidth = WidthArgs,
NewAdjust = right
end,
doCollPrecision(LeftLPart, LeftArgs, NewWidth, NewAdjust, Acc);
<<WidthInt:8/integer, LeftLPart/binary>> -> <<WidthInt:8/integer, LeftLPart/binary>> ->
case WidthInt >= $0 andalso WidthInt =< $9 of case WidthInt >= $0 andalso WidthInt =< $9 of
true -> true ->
doCollWidth(LeftLPart, Args, 10 * Width + (WidthInt - $0), Adjust, Acc);
NewWidth = ?IIF(Width == none, WidthInt - $0, 10 * Width + (WidthInt - $0)),
doCollWidth(LeftLPart, Args, NewWidth, Adjust, Acc);
_ -> _ ->
case Width == 0 of
true ->
doCollPrecision(LPart, Args, none, left, Acc);
_ ->
doCollPrecision(LPart, Args, Width, Adjust, Acc)
end
doCollPrecision(LPart, Args, Width, Adjust, Acc)
end end
end. end.
@ -579,7 +608,8 @@ doCollPrecision(LPart, Args, Width, Adjust, Precision, Acc) ->
case LPart of case LPart of
<<"*", LeftLPart/binary>> -> <<"*", LeftLPart/binary>> ->
[PrecisionArgs | LeftArgs] = Args, [PrecisionArgs | LeftArgs] = Args,
doCollPadChar(LeftLPart, LeftArgs, Width, Adjust, PrecisionArgs, Acc);
NewPrecision = ?IIF(PrecisionArgs == 0, none, PrecisionArgs),
doCollPadChar(LeftLPart, LeftArgs, Width, Adjust, NewPrecision, Acc);
<<PrecisionInt:8/integer, LeftLPart/binary>> -> <<PrecisionInt:8/integer, LeftLPart/binary>> ->
case PrecisionInt >= $0 andalso PrecisionInt =< $9 of case PrecisionInt >= $0 andalso PrecisionInt =< $9 of
true -> true ->

Loading…
Cancel
Save