Преглед изворни кода

Merge pull request #327 from basho/bugfix/improper-io-list-support

Port improper iolist support from io_lib_format.erl & add tests

Reviewed-by: macintux
pull/325/merge
Bishop Bors пре 9 година
родитељ
комит
91e2e9e652
2 измењених фајлова са 28 додато и 6 уклоњено
  1. +22
    -6
      src/lager_format.erl
  2. +6
    -0
      src/lager_trunc_io.erl

+ 22
- 6
src/lager_format.erl Прегледај датотеку

@ -245,18 +245,34 @@ control2($P, [A,Depth], _F, _Adj, _P, _Pad, _Enc, L) when is_integer(Depth) ->
Term = lager_trunc_io:fprint(A, L, [{depth, Depth}, {lists_as_strings, true}]),
{Term, lists:flatlength(Term)};
control2($s, [L0], F, Adj, P, Pad, latin1, L) ->
List = lager_trunc_io:fprint(maybe_flatten(L0), L, [{force_strings, true}]),
List = lager_trunc_io:fprint(iolist_to_chars(L0), L, [{force_strings, true}]),
Res = string(List, F, Adj, P, Pad),
{Res, lists:flatlength(Res)};
control2($s, [L0], F, Adj, P, Pad, unicode, L) ->
List = lager_trunc_io:fprint(unicode:characters_to_list(L0), L, [{force_strings, true}]),
List = lager_trunc_io:fprint(cdata_to_chars(L0), L, [{force_strings, true}]),
Res = uniconv(string(List, F, Adj, P, Pad)),
{Res, lists:flatlength(Res)}.
maybe_flatten(X) when is_list(X) ->
lists:flatten(X);
maybe_flatten(X) ->
X.
iolist_to_chars([C|Cs]) when is_integer(C), C >= $\000, C =< $\377 ->
[C | iolist_to_chars(Cs)];
iolist_to_chars([I|Cs]) ->
[iolist_to_chars(I) | iolist_to_chars(Cs)];
iolist_to_chars([]) ->
[];
iolist_to_chars(B) when is_binary(B) ->
binary_to_list(B).
cdata_to_chars([C|Cs]) when is_integer(C), C >= $\000 ->
[C | cdata_to_chars(Cs)];
cdata_to_chars([I|Cs]) ->
[cdata_to_chars(I) | cdata_to_chars(Cs)];
cdata_to_chars([]) ->
[];
cdata_to_chars(B) when is_binary(B) ->
case catch unicode:characters_to_list(B) of
L when is_list(L) -> L;
_ -> binary_to_list(B)
end.
make_options([], Options) ->
Options;

+ 6
- 0
src/lager_trunc_io.erl Прегледај датотеку

@ -882,4 +882,10 @@ print_terms_without_format_string_test() ->
?assertError(badarg, format(65535, [], 50)),
ok.
improper_io_list_test() ->
?assertEqual(">hello", lists:flatten(format('~s', [[$>|<<"hello">>]], 50))),
?assertEqual(">hello", lists:flatten(format('~ts', [[$>|<<"hello">>]], 50))),
?assertEqual("helloworld", lists:flatten(format('~ts', [[<<"hello">>|<<"world">>]], 50))),
ok.
-endif.

Loading…
Откажи
Сачувај