diff --git a/src/eFmt.erl b/src/eFmt.erl index d04bb73..9ff9938 100644 --- a/src/eFmt.erl +++ b/src/eFmt.erl @@ -1,11 +1,7 @@ -module(eFmt). -export([ - fwrite/2 - , fwrite/3 - , fread/2 - , fread/3 - , format/2 + format/2 , format/3 , scan_format/2 @@ -87,107 +83,53 @@ -type latin1_string() :: [unicode:latin1_char()]. -type depth() :: -1 | non_neg_integer(). +-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 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() + }. %%---------------------------------------------------------------------- - -%% Interface calls to sub-modules. - --spec fwrite(Format, Data) -> chars() when - Format :: io:format(), - Data :: [term()]. - -fwrite(Format, Args) -> - format(Format, Args). - --type chars_limit() :: integer(). - --spec fwrite(Format, Data, Options) -> chars() when - Format :: io:format(), - Data :: [term()], - Options :: [Option], - Option :: {'chars_limit', CharsLimit}, - CharsLimit :: chars_limit(). - -fwrite(Format, Args, Options) -> - format(Format, Args, Options). - --spec fread(Format, String) -> Result when - Format :: string(), - String :: string(), - Result :: {'ok', InputList :: [fread_item()], LeftOverChars :: string()} - | {'more', RestFormat :: string(), - Nchars :: non_neg_integer(), - InputStack :: chars()} - | {'error', {'fread', What :: fread_error()}}. - -fread(Chars, Format) -> - io_lib_fread:fread(Chars, Format). - --spec fread(Continuation, CharSpec, Format) -> Return when - Continuation :: continuation() | [], - CharSpec :: string() | 'eof', - Format :: string(), - Return :: {'more', Continuation1 :: continuation()} - | {'done', Result, LeftOverChars :: string()}, - Result :: {'ok', InputList :: [fread_item()]} - | 'eof' - | {'error', {'fread', What :: fread_error()}}. - -fread(Cont, Chars, Format) -> - io_lib_fread:fread(Cont, Chars, Format). - --spec format(Format, Data) -> chars() when - Format :: io:format(), - Data :: [term()]. - +-spec format(Format :: io:format(), Data :: [term()]) -> chars(). format(Format, Args) -> - try io_lib_format:fwrite(Format, Args) + try eFmtFormat:fwrite(Format, Args) catch C:R:S -> test_modules_loaded(C, R, S), erlang:error(badarg, [Format, Args]) end. --spec format(Format, Data, Options) -> chars() when - Format :: io:format(), - Data :: [term()], - Options :: [Option], - Option :: {'chars_limit', CharsLimit}, - CharsLimit :: chars_limit(). - +-spec format(Format :: io:format(), Data :: [term()], Options :: [{'chars_limit', CharsLimit :: chars_limit()}]) -> chars(). format(Format, Args, Options) -> - try io_lib_format:fwrite(Format, Args, Options) + try eFmtFormat:fwrite(Format, Args, Options) catch C:R:S -> test_modules_loaded(C, R, S), @@ -200,7 +142,7 @@ format(Format, Args, Options) -> FormatList :: [char() | format_spec()]. scan_format(Format, Args) -> - try io_lib_format:scan(Format, Args) + try eFmtFormat:scan(Format, Args) catch C:R:S -> test_modules_loaded(C, R, S), @@ -213,13 +155,13 @@ scan_format(Format, Args) -> Data :: [term()]. unscan_format(FormatList) -> - io_lib_format:unscan(FormatList). + eFmtFormat:unscan(FormatList). -spec build_text(FormatList) -> chars() when FormatList :: [char() | format_spec()]. build_text(FormatList) -> - try io_lib_format:build(FormatList) + try eFmtFormat:build(FormatList) catch C:R:S -> test_modules_loaded(C, R, S), @@ -233,7 +175,7 @@ build_text(FormatList) -> CharsLimit :: chars_limit(). build_text(FormatList, Options) -> - try io_lib_format:build(FormatList, Options) + try eFmtFormat:build(FormatList, Options) catch C:R:S -> test_modules_loaded(C, R, S), @@ -242,10 +184,10 @@ build_text(FormatList, Options) -> %% Failure to load a module must not be labeled as badarg. %% C, R, and S are included so that the original error, which could be -%% a bug in io_lib_format, can be found by tracing on +%% a bug in eFmtFormat, can be found by tracing on %% test_modules_loaded/3. test_modules_loaded(_C, _R, _S) -> - Modules = [io_lib_format, io_lib_pretty, string, unicode], + Modules = [eFmtFormat, io_lib_pretty, string, unicode], case code:ensure_modules_loaded(Modules) of ok -> ok; Error -> erlang:error(Error) @@ -271,7 +213,7 @@ print(Term, Column, LineLength, Depth) -> StartIndent :: integer(). indentation(Chars, Current) -> - io_lib_format:indentation(Chars, Current). + eFmtFormat:indentation(Chars, Current). %% Format an IO-request prompt (handles formatting errors safely). @@ -355,7 +297,7 @@ write(Term, Depth) -> write1(_Term, 0, _E) -> "..."; write1(Term, _D, _E) when is_integer(Term) -> integer_to_list(Term); -write1(Term, _D, _E) when is_float(Term) -> io_lib_format:fwrite_g(Term); +write1(Term, _D, _E) when is_float(Term) -> eFmtFormat:fwrite_g(Term); write1(Atom, _D, latin1) when is_atom(Atom) -> write_atom_as_latin1(Atom); write1(Atom, _D, _E) when is_atom(Atom) -> write_atom(Atom); write1(Term, _D, _E) when is_port(Term) -> write_port(Term);