convert all space/newlines in json formatter to emit binaries

This commit is contained in:
alisdair sullivan 2013-10-27 01:28:49 +00:00
parent 5409668cf4
commit 7296f790b2

View file

@ -157,22 +157,17 @@ encode(float, Float, _Config) ->
space(Config) -> space(Config) ->
case Config#config.space of case Config#config.space of
0 -> [] 0 -> <<>>
; X when X > 0 -> binary:copy(?space, X) ; X when X > 0 -> binary:copy(?space, X)
end. end.
indent(Config) -> indent(Config) ->
case Config#config.indent of case Config#config.indent of
0 -> [] 0 -> <<>>
; X when X > 0 -> ; X when X > 0 -> <<?newline/binary, (binary:copy(?space, X * Config#config.depth))/binary>>
Indent = binary:copy(?space, X),
indent(Indent, Config#config.depth, [?newline])
end. end.
indent(_Indent, 0, Acc) -> Acc;
indent(Indent, N, Acc) -> indent(Indent, N - 1, [Acc, Indent]).
indent_or_space(Config) -> indent_or_space(Config) ->
case Config#config.indent > 0 of case Config#config.indent > 0 of
@ -258,7 +253,7 @@ config_test_() ->
space_test_() -> space_test_() ->
[ [
{"no space", ?_assertEqual([], space(#config{space=0}))}, {"no space", ?_assertEqual(<<>>, space(#config{space=0}))},
{"one space", ?_assertEqual(<<" ">>, space(#config{space=1}))}, {"one space", ?_assertEqual(<<" ">>, space(#config{space=1}))},
{"four spaces", ?_assertEqual(<<" ">>, space(#config{space=4}))} {"four spaces", ?_assertEqual(<<" ">>, space(#config{space=4}))}
]. ].
@ -266,21 +261,21 @@ space_test_() ->
indent_test_() -> indent_test_() ->
[ [
{"no indent", ?_assertEqual([], indent(#config{indent=0, depth=1}))}, {"no indent", ?_assertEqual(<<>>, indent(#config{indent=0, depth=1}))},
{"indent 1 depth 1", ?_assertEqual( {"indent 1 depth 1", ?_assertEqual(
[[?newline], ?space], <<?newline/binary, <<" ">>/binary>>,
indent(#config{indent=1, depth=1}) indent(#config{indent=1, depth=1})
)}, )},
{"indent 1 depth 2", ?_assertEqual( {"indent 1 depth 2", ?_assertEqual(
[[[?newline], ?space], ?space], <<?newline/binary, <<" ">>/binary>>,
indent(#config{indent=1, depth=2}) indent(#config{indent=1, depth=2})
)}, )},
{"indent 4 depth 1", ?_assertEqual( {"indent 4 depth 1", ?_assertEqual(
[[?newline], <<" ">>], <<?newline/binary, <<" ">>/binary>>,
indent(#config{indent=4, depth=1}) indent(#config{indent=4, depth=1})
)}, )},
{"indent 4 depth 2", ?_assertEqual( {"indent 4 depth 2", ?_assertEqual(
[[[?newline], <<" ">>], <<" ">>], <<?newline/binary, <<" ">>/binary, <<" ">>/binary>>,
indent(#config{indent=4, depth=2}) indent(#config{indent=4, depth=2})
)} )}
]. ].
@ -293,7 +288,7 @@ indent_or_space_test_() ->
indent_or_space(#config{space=1, indent=0, depth=1}) indent_or_space(#config{space=1, indent=0, depth=1})
)}, )},
{"indent so no space", ?_assertEqual( {"indent so no space", ?_assertEqual(
[[?newline], ?space], <<?newline/binary, <<" ">>/binary>>,
indent_or_space(#config{space=1, indent=1, depth=1}) indent_or_space(#config{space=1, indent=1, depth=1})
)} )}
]. ].