瀏覽代碼

ft:模块融合

master
SisMaker 4 年之前
父節點
當前提交
7d1b78e746
共有 1 個檔案被更改,包括 44 行新增46 行删除
  1. +44
    -46
      src/eFmt.erl

+ 44
- 46
src/eFmt.erl 查看文件

@ -49,8 +49,8 @@
format(Format, Args) ->
try fWrite(Format, Args)
catch
_C:_R ->
erlang:error(badarg, [Format, Args])
_C:_R:S ->
erlang:error(badarg, [Format, Args, _C, _R, S])
end.
-spec format(Format :: io:format(), Data :: [term()], Options :: [{charsLimit, CharsLimit :: charsLimit()}]) -> chars().
@ -192,7 +192,7 @@ writeBinary(Bin, D, BinAcc) ->
<<Int:8>> ->
<<BinAcc/binary, (integer_to_binary(Int))/binary, ">>">>;
<<Int:8, LeftBin/bitstring>> ->
writeBinary(LeftBin, D - 1, <<BinAcc/binary, (integer_to_binary(Int))/binary, ",">>);
writeBinary(LeftBin, D - 1, <<BinAcc/binary, (integer_to_binary(Int))/binary, ",">>);
_ ->
L = bit_size(Bin),
<<X:L>> = Bin,
@ -214,7 +214,6 @@ writeTerm(Term, _D, _E) when is_reference(Term) -> ?writeRef(Term);
writeTerm(Term, _D, _E) when is_function(Term) -> ?writeFun(Term).
%% ********************************************** eFmt end *************************************************************
%% ********************************************** eFmtFormat start *****************************************************
-spec fWrite(Format :: io:format(), Data :: [term()]) -> chars().
@ -363,28 +362,57 @@ fBuild(Cs) ->
-spec fBuild(FormatList :: [char() | fmtSpec()], Options :: [{'chars_limit', CharsLimit :: integer()}]) -> chars().
fBuild(Cs, Options) ->
CharsLimit = getOpt(chars_limit, Options, -1),
ResList = buildSmall(Cs, []),
{P, S, W, Other} = cntSmall(ResList, 0, 0, 0, 0),
buildSmall(Cs, CharsLimit, 0, 0, 0, 0, []).
buildSmall([], CharsLimit, P, S, W, Other, Acc) ->
NumOfLimited = P + S + W,
case NumOfLimited of
0 ->
ResList;
Acc;
_ ->
RemainChars = remainChars(CharsLimit, Other),
buildLimited(ResList, P, NumOfLimited, RemainChars, 0, [])
end.
buildSmall([], Acc) -> Acc;
buildSmall([OneCA | Cs], Acc) ->
buildLimited(Acc, P, NumOfLimited, RemainChars, 0, [])
end;
buildSmall([OneCA | Cs], CharsLimit, P, S, W, Other, Acc) ->
case OneCA of
#fmtSpec{ctlChar = CtlChar, args = Args, width = Width, adjust = Adjust, precision = Precision, padChar = PadChar, encoding = Encoding} ->
case ctlSmall(CtlChar, Args, Width, Adjust, Precision, PadChar, Encoding) of
not_small -> buildSmall(Cs, [OneCA | Acc]);
ignore -> buildSmall(Cs, Acc);
Str -> buildSmall(Cs, [Str | Acc])
not_small ->
case CtlChar of
$p ->
buildSmall(Cs, CharsLimit, P + 1, S, W, Other, [OneCA | Acc]);
$P ->
buildSmall(Cs, CharsLimit, P + 1, S, W, Other, [OneCA | Acc]);
$w ->
buildSmall(Cs, CharsLimit, P, S, W + 1, Other, [OneCA | Acc]);
$W ->
buildSmall(Cs, CharsLimit, P, S, W + 1, Other, [OneCA | Acc]);
$s ->
buildSmall(Cs, CharsLimit, P, S, W + 1, Other, [OneCA | Acc]);
_ ->
buildSmall(Cs, CharsLimit, P, S, W, Other, [OneCA | Acc])
end;
ignore ->
buildSmall(Cs, CharsLimit, P, S, W, Other, Acc);
Str ->
if
is_binary(Str) orelse is_list(Str) ->
buildSmall(Cs, CharsLimit, P, S, W, Other + charsLen(Str), [Str | Acc]);
is_integer(Str) ->
buildSmall(Cs, CharsLimit, P, S, W, Other + 1, [Str | Acc]);
true ->
buildSmall(Cs, CharsLimit, P, S, W, Other, [Str | Acc])
end
end;
_ ->
buildSmall(Cs, [OneCA | Acc])
if
is_binary(OneCA) orelse is_list(OneCA) ->
buildSmall(Cs, CharsLimit, P, S, W, Other + charsLen(OneCA), [OneCA | Acc]);
is_integer(OneCA) ->
buildSmall(Cs, CharsLimit, P, S, W, Other + 1, [OneCA | Acc]);
true ->
buildSmall(Cs, CharsLimit, P, S, W, Other, [OneCA | Acc])
end
end.
ctlSmall($s, Args, Width, Adjust, Precision, PadChar, Encoding) when is_atom(Args) ->
@ -431,36 +459,6 @@ ctlSmall($n, _Args, Width, Adjust, Precision, PadChar, _Encoding) -> newline(Wid
ctlSmall($i, _Args, _Width, _Adjust, _Precision, _PadChar, _Encoding) -> ignore;
ctlSmall(_C, _Args, _Width, _Adjust, _Precision, _PadChar, _Encoding) -> not_small.
cntSmall([], P, S, W, Other) ->
{P, S, W, Other};
cntSmall([OneRes | Cs], P, S, W, Other) ->
case OneRes of
#fmtSpec{ctlChar = CtlChar} ->
case CtlChar of
$p ->
cntSmall(Cs, P + 1, S, W, Other);
$P ->
cntSmall(Cs, P + 1, S, W, Other);
$w ->
cntSmall(Cs, P, S, W + 1, Other);
$W ->
cntSmall(Cs, P, S, W + 1, Other);
$s ->
cntSmall(Cs, P, S, W + 1, Other);
_ ->
cntSmall(Cs, P, S, W, Other)
end;
_ ->
if
is_binary(OneRes) orelse is_list(OneRes) ->
cntSmall(Cs, P, S, W, Other + charsLen(OneRes));
is_integer(OneRes) ->
cntSmall(Cs, P, S, W, Other + 1);
true ->
cntSmall(Cs, P, S, W, Other)
end
end.
buildLimited([], _, _, _, _, Acc) -> Acc;
buildLimited([OneCA | Cs], NumOfPs, Count, MaxLen, I, Acc) ->
case OneCA of

Loading…
取消
儲存