Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parse floats with exponent part #30

Merged
merged 1 commit into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions src/ered_parser.erl
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,14 @@ parse_float(Data) ->
try binary_to_float(Data)
catch
error:badarg ->
%% maybe its a float without decimal
try float(binary_to_integer(Data))
%% maybe its a float without decimal fractions
try
case binary:split(Data, [<<"E">>, <<"e">>]) of
[Integer, Exponent] ->
binary_to_float(<<Integer/binary, ".0e", Exponent/binary>>);
[Integer] ->
float(binary_to_integer(Integer))
end
catch
error:badarg -> throw({parse_error, {not_float, Data}})
end
Expand Down
4 changes: 4 additions & 0 deletions test/ered_parser_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ test_data() ->
{"streamed map", <<"%?\r\n+a\r\n:1\r\n+b\r\n:2\r\n.\r\n">>, #{<<"a">> => 1, <<"b">> => 2}},
%% Additional tests
{"streamed set", <<"~?\r\n+a\r\n:1\r\n+b\r\n:2\r\n.\r\n">>, set_from_list([<<"a">>, 1, <<"b">>, 2])},
{"float exponent", <<",1.1e100\r\n">>, 1.1e100},
{"float exponent no fractions", <<",4e100\r\n">>, 4.0e100},
{"float +exponent no fractions", <<",4e+100\r\n">>, 4.0e100},
{"float -exponent no fractions", <<",4e-100\r\n">>, 4.0e-100},
{"float negative", <<",-1.23\r\n">>, -1.23}
].

Expand Down