From 7d1b78e7466dbf23b661e3467286d0d3e3722aef Mon Sep 17 00:00:00 2001 From: SisMaker <1713699517@qq.com> Date: Sun, 21 Feb 2021 21:40:02 +0800 Subject: [PATCH] =?UTF-8?q?ft:=E6=A8=A1=E5=9D=97=E8=9E=8D=E5=90=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/eFmt.erl | 90 +++++++++++++++++++++++++--------------------------- 1 file changed, 44 insertions(+), 46 deletions(-) diff --git a/src/eFmt.erl b/src/eFmt.erl index 087fe0d..a957610 100644 --- a/src/eFmt.erl +++ b/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) -> <> -> <>">>; <> -> - writeBinary(LeftBin, D - 1, <>); + writeBinary(LeftBin, D - 1, <>); _ -> L = bit_size(Bin), <> = 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