소스 검색

Merge pull request #17 from basho/adt-fix-binary-printing

Several fixes for printing binaries, improve quote stripping
pull/18/head
Andrew Thompson 13 년 전
부모
커밋
445a546391
1개의 변경된 파일46개의 추가작업 그리고 9개의 파일을 삭제
  1. +46
    -9
      src/lager_trunc_io.erl

+ 46
- 9
src/lager_trunc_io.erl 파일 보기

@ -102,12 +102,7 @@ format([[$~|H]| T], [AH | AT], Max, Acc, ArgAcc) when length(H) == 1 ->
{Value, RealLen} = case H of
"s" ->
% strip off the doublequotes, if applicable
Trimmed = case {hd(String), lists:last(String)} == {$", $"} of
true ->
string:strip(String, both, $");
_ ->
String
end,
Trimmed = unquote_string(lists:flatten(String)),
{Trimmed, length(Trimmed)};
_ ->
{String, Length}
@ -136,8 +131,9 @@ format([[$~|H]| T], [AH | AT], Max, Acc, ArgAcc) ->
{String, Length} ->
{Value, RealLen} = case C of
$s ->
% strip off the doublequotes
{string:substr(String, 2, length(String) -2), Length -2};
% strip off the doublequotes, if applicable
Trimmed = unquote_string(lists:flatten(String)),
{Trimmed, length(Trimmed)};
_ ->
{String, Length}
end,
@ -235,7 +231,13 @@ print(Binary, 0) when is_binary(Binary) ->
print(Binary, Max) when is_binary(Binary) ->
B = binary_to_list(Binary, 1, lists:min([Max, size(Binary)])),
{L, Len} = alist_start(B, Max-4),
{["<<", L, ">>"], Len+4};
{Res, Length} = case L of
[91, X, 93] ->
{X, Len - 2};
X ->
{X, Len}
end,
{["<<", Res, ">>"], Length+4};
print(Float, _Max) when is_float(Float) ->
%% use the same function io_lib:format uses to print floats
@ -346,6 +348,30 @@ atom_needs_quoting([H|T]) when (H >= $a andalso H =< $z);
atom_needs_quoting(_) ->
true.
unquote_string([$<, $<, $"|T] = Str) ->
case string:substr(T, length(T) - 2) of
"\">>" ->
string:substr(T, 1, length(T) - 3);
_ ->
Str
end;
unquote_string([$"|_] = Str) ->
case lists:last(Str) == $" of
true ->
string:strip(Str, both, $");
_ ->
Str
end;
unquote_string([$'|_] = Str) ->
case lists:last(Str) == $' of
true ->
string:strip(Str, both, $');
_ ->
Str
end;
unquote_string(S) ->
S.
-ifdef(TEST).
%%--------------------
%% The start of a test suite. So far, it only checks for not crashing.
@ -444,5 +470,16 @@ quote_strip_test() ->
?assertEqual("hello", lists:flatten(format("~s", ["hello"], 50))),
?assertEqual("hello", lists:flatten(format("~s", [hello], 50))),
?assertEqual("hello", lists:flatten(format("~p", [hello], 50))),
?assertEqual("'hello world'", lists:flatten(format("~p", ['hello world'], 50))),
?assertEqual("hello world", lists:flatten(format("~s", ['hello world'], 50))),
ok.
binary_printing_test() ->
?assertEqual("<<\"hello\">>", lists:flatten(format("~p", [<<$h, $e, $l, $l, $o>>], 50))),
?assertEqual("<<\"hello\">>", lists:flatten(format("~p", [<<"hello">>], 50))),
?assertEqual("<<1,2,3,4>>", lists:flatten(format("~p", [<<1, 2, 3, 4>>], 50))),
?assertEqual("<<1,2,3,4>>", lists:flatten(format("~s", [<<1, 2, 3, 4>>], 50))),
?assertEqual("hello", lists:flatten(format("~s", [<<"hello">>], 50))),
?assertEqual("hello", lists:flatten(format("~10s", [<<"hello">>], 50))),
ok.
-endif.

불러오는 중...
취소
저장