From 4696759e9203bdd1fcca62790cd0a49be73dd822 Mon Sep 17 00:00:00 2001 From: SisMaker <1713699517@qq.com> Date: Thu, 18 Feb 2021 18:22:13 +0800 Subject: [PATCH] =?UTF-8?q?ft:=20=E4=BF=AE=E6=94=B9=E4=B8=BA=E4=BA=8C?= =?UTF-8?q?=E8=BF=9B=E5=88=B6=E5=8C=B9=E9=85=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/eFmt.hrl | 10 +++++++ src/eFmt.erl | 70 +++++++++++++++++----------------------------- src/eFmtFormat.erl | 24 +++++++++++----- 3 files changed, 53 insertions(+), 51 deletions(-) create mode 100644 include/eFmt.hrl diff --git a/include/eFmt.hrl b/include/eFmt.hrl new file mode 100644 index 0000000..490ac58 --- /dev/null +++ b/include/eFmt.hrl @@ -0,0 +1,10 @@ +-record(fmtSpec, { + ctlChar :: char() %% 控制序列的类型 $p $w + , args :: [any()] %% 是控制序列使用的参数的列表,如果控制序列不带任何参数,则为空列表。 + , width :: 'none' | integer() %% 字段宽度 + , adjust :: 'left' | 'right' %% 对齐方式 + , precision :: 'none' | integer() %% 打印参数的精度 + , padChar :: char() %% 填充字符 + , encoding :: 'unicode' | 'latin1' %% 如果存在翻译修饰符t,则编码设置为true + , strings :: boolean() %% 如果存在修饰符l,则将 string设置为false。 +}). diff --git a/src/eFmt.erl b/src/eFmt.erl index 7e0b7f1..e117a79 100644 --- a/src/eFmt.erl +++ b/src/eFmt.erl @@ -1,5 +1,7 @@ -module(eFmt). +-include("eFmt.hrl"). + -export([ format/2 , format/3 @@ -17,7 +19,6 @@ , write/1 , write/2 , write/3 - , nl/0 , format_prompt/1 , format_prompt/2 @@ -67,17 +68,6 @@ , chars_length/1 ]). --record(fmtSpec, { - ctlChar :: char() %% 控制序列的类型 $p $w - , args :: [any()] %% 是控制序列使用的参数的列表,如果控制序列不带任何参数,则为空列表。 - , width :: 'none' | integer() %% 字段宽度 - , adjust :: 'left' | 'right' %% 对齐方式 - , precision :: 'none' | integer() %% 打印参数的精度 - , padChar :: char() %% 填充字符 - , encoding :: 'unicode' | 'latin1' %% 如果存在翻译修饰符t,则编码设置为true - , strings :: boolean() %% 如果存在修饰符l,则将 string设置为false。 -}). - -export_type([ chars/0 , latin1_string/0 @@ -98,38 +88,38 @@ -type chars_limit() :: integer(). -opaque continuation() :: - {Format :: string(), Stack :: chars(), Nchars :: non_neg_integer(), Results :: [term()]}. +{Format :: string(), Stack :: chars(), Nchars :: non_neg_integer(), Results :: [term()]}. -type fread_error() :: - 'atom' - | 'based' - | 'character' - | 'float' - | 'format' - | 'input' - | 'integer' - | 'string' - | 'unsigned'. +'atom' +| 'based' +| 'character' +| 'float' +| 'format' +| 'input' +| 'integer' +| 'string' +| 'unsigned'. -type fread_item() :: - string() | - atom() | - integer() | - float(). +string() | +atom() | +integer() | +float(). -type fmtSpec() :: #fmtSpec{}. -type format_spec() :: - #{ - control_char := char(), - args := [any()], - width := 'none' | integer(), - adjust := 'left' | 'right', - precision := 'none' | integer(), - pad_char := char(), - encoding := 'unicode' | 'latin1', - strings := boolean() - }. +#{ +control_char := char(), +args := [any()], +width := 'none' | integer(), +adjust := 'left' | 'right', +precision := 'none' | integer(), +pad_char := char(), +encoding := 'unicode' | 'latin1', +strings := boolean() +}. %%---------------------------------------------------------------------- -spec format(Format :: io:format(), Data :: [term()]) -> chars(). @@ -715,14 +705,6 @@ printable_unicode_list([$\e | Cs]) -> printable_unicode_list(Cs); printable_unicode_list([]) -> true; printable_unicode_list(_) -> false. %Everything else is false -%% List = nl() -%% Return a list of characters to generate a newline. - --spec nl() -> string(). - -nl() -> - "\n". - %% %% Utilities for collecting characters in input files %% diff --git a/src/eFmtFormat.erl b/src/eFmtFormat.erl index 263cc79..d92aca1 100644 --- a/src/eFmtFormat.erl +++ b/src/eFmtFormat.erl @@ -1,5 +1,7 @@ -module(eFmtFormat). +-include("eFmt.hrl"). + %% Formatting functions of io library. -export([ fwrite/2 @@ -70,6 +72,18 @@ scan(Format, Args) -> collect(Format, Args) end. +doCollect(FmtBinStr, Args) -> + MatchList = binary:matches(FmtBinStr, <<"~">>), + doCollectList(MatchList, FmtBinStr, Args, 0, []). + +doCollectList([], _FmtBinStr, _Args, Index, Acc) -> + Acc; +doCollectList([OneMatch | MatchList], FmtBinStr, Args, Index, Acc) -> + ok. + + + + collect([$~ | Fmt0], Args0) -> {C, Fmt1, Args1} = collect_cseq(Fmt0, Args0), [C | collect(Fmt1, Args1)]; @@ -175,10 +189,8 @@ count_small([], P, S, W, Other) -> {P, S, W, Other}. %% build_small([Control]) -> eFmt:chars(). -%% Interpret the control structures, but only the small ones. -%% The big ones are saved for later. -%% build_limited([Control], NumberOfPps, NumberOfLimited, -%% CharsLimit, Indentation) +%% Interpret the control structures, but only the small ones. The big ones are saved for later. +%% build_limited([Control], NumberOfPps, NumberOfLimited, CharsLimit, Indentation) %% Interpret the control structures. Count the number of print %% remaining and only calculate indentation when necessary. Must also %% be smart when calculating indentation for characters in format. @@ -191,9 +203,7 @@ build_small([#{control_char := C, args := As, width := F, adjust := Ad, precisio build_small([C | Cs]) -> [C | build_small(Cs)]; build_small([]) -> []. -build_limited([#{control_char := C, args := As, width := F, adjust := Ad, - precision := P, pad_char := Pad, encoding := Enc, - strings := Str} | Cs], NumOfPs0, Count0, MaxLen0, I) -> +build_limited([#{control_char := C, args := As, width := F, adjust := Ad, precision := P, pad_char := Pad, encoding := Enc, strings := Str} | Cs], NumOfPs0, Count0, MaxLen0, I) -> MaxChars = if MaxLen0 < 0 -> MaxLen0; true -> MaxLen0 div Count0