Skip to content

Commit

Permalink
Update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
saleyn committed Oct 28, 2023
1 parent 27d42fb commit 368d379
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 23 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -347,8 +347,8 @@ This module implements a transform to stringify an Erlang term.
`lists:flatten(io_lib:format("~p", [Term]))` format.
* `str(Fmt, Args)` is equivalent to `lists:flatten(io_lib:format(Fmt, Args))`.
* `bin(Fmt, Args)` is equivalent to `list_to_binary(lists:flatten(io_lib:format(Fmt, Args)))`.
* `throw(Fmt,Args)` is equivalent to `throw(lists:flatten(io_lib:format(Fmt, Args)))`.
* `error(Fmt,Args)` is equivalent to `error(lists:flatten(io_lib:format(Fmt, Args)))`.
* `throw(Fmt,Args)` is equivalent to `throw(list_to_binary(io_lib:format(Fmt, Args)))`.
* `error(Fmt,Args)` is equivalent to `error(list_to_binary(io_lib:format(Fmt, Args)))`.

Two other shorthand transforms are optionally supported:

Expand Down
42 changes: 21 additions & 21 deletions src/str.erl
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
%%% Use `{parse_transform,str}' compiler's option to use this transform.
%%% ```
%%% str(Fmt, Args) -> lists:flatten(io_lib:format(Fmt, Args))
%%% bin(Fmt, Args) -> list_to_binary(lists:flatten(io_lib:format(Fmt, Args)))
%%% throw(Fmt, Args) -> erlang:throw(list_to_binary(lists:flatten(io_lib:format(Fmt, Args)))
%%% error(Fmt, Args) -> erlang:error(list_to_binary(lists:flatten(io_lib:format(Fmt, Args)))
%%% bin(Fmt, Args) -> list_to_binary(io_lib:format(Fmt, Args))
%%% throw(Fmt, Args) -> erlang:throw(list_to_binary(io_lib:format(Fmt, Args))
%%% error(Fmt, Args) -> erlang:error(list_to_binary(io_lib:format(Fmt, Args))
%%% i2l(Int) -> integer_to_list(Int) % Enabled with compiled with
%%% % the `{d,str_i2l}' option
%%% b2l(Bin) -> binary_to_list(Bin) % Enabled with compiled with
Expand Down Expand Up @@ -59,7 +59,7 @@ str(I) when is_list(I) ->
end);
str(I) when is_integer(I) -> integer_to_list(I);
str(I) when is_binary(I) -> binary_to_list(I);
str(I) when is_float(I) -> str(I, get_float_fmt());
str(I) when is_float(I) -> float_to_list(I, get_float_fmt());
str(I) when is_atom(I) -> atom_to_list(I);
str(I) ->
lists:flatten(io_lib:format("~p", [I])).
Expand All @@ -73,25 +73,27 @@ str(I) ->
%% @doc Stringify an argument with options passed to float_to_list/2 when
%% the first argument is a float
-spec str(term(), fmt_args()) -> string().
str(I, undefined) when is_float(I) ->
float_to_list(I);
str(I, Opts) when is_float(I) ->
float_to_list(I, Opts);
str(I, _Opts) ->
str(I).
str(I, undefined) when is_float(I) -> float_to_list(I);
str(I, Opts) when is_float(I) -> float_to_list(I, Opts);
str(I,_Opts) -> str(I).

%% @doc Stringify an argument and return as binary
-spec bin(term()) -> binary().
bin(I) -> list_to_binary(str(I)).

%% @doc Stringify an argument and return as binary
-spec bin(term(), fmt_args()) -> binary().
bin(I, undefined) when is_float(I) ->
float_to_binary(I);
bin(I, Opts) when is_float(I) ->
float_to_binary(I, Opts);
bin(I, _) ->
list_to_binary(str(I)).
bin(I, undefined) when is_float(I) -> float_to_binary(I);
bin(I, Opts) when is_float(I) -> float_to_binary(I, Opts);
bin(I, _) when is_binary(I) -> I;
bin(I, _) when is_list(I) ->
list_to_binary(
try io_lib:format("~s", [I])
catch _:_ -> io_lib:format("~p", [I])
end);
bin(I, _) when is_integer(I) -> integer_to_binary(I);
bin(I, _) when is_atom(I) -> atom_to_binary(I);
bin(I, _) -> list_to_binary(io_lib:format("~p", [I])).

%% @doc Erase custom float format from the process dictionary
reset_float_fmt() -> erase(float_fmt).
Expand Down Expand Up @@ -168,14 +170,12 @@ update4(Arg, Node, Line, _Opt) when Arg==str; Arg==bin ->
Node
end;
update4(I, Node, Line, _Opt) when I==throw; I==error ->
%% Replace throw(A, B) -> throw(list_to_binary(lists:flatten(io_lib:format(A, B)))).
%% Replace error(A, B) -> error(list_to_binary(lists:flatten(io_lib:format(A, B)))).
%% Replace throw(A, B) -> throw(list_to_binary(io_lib:format(A, B))).
%% Replace error(A, B) -> error(list_to_binary(io_lib:format(A, B))).
put(line, Line),
case erl_syntax:application_arguments(Node) of
[A,B] ->
syn_call(I, [syn_call(erlang, list_to_binary,
[syn_call(lists, flatten, [
syn_call(io_lib, format, [A,B])])])]);
syn_call(I, [syn_call(erlang, list_to_binary, [syn_call(io_lib, format, [A,B])])]);
_ ->
Node
end;
Expand Down

0 comments on commit 368d379

Please sign in to comment.