Skip to content

Commit 8cf56da

Browse files
MainShayne233lpil
authored andcommitted
Implement parse_float
1 parent 673e9e9 commit 8cf56da

File tree

3 files changed

+52
-2
lines changed

3 files changed

+52
-2
lines changed

gen/str.erl

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
-compile(no_auto_import).
33
-include_lib("eunit/include/eunit.hrl").
44

5-
-export([length/1, lowercase/1, uppercase/1, reverse/1, split/2, replace/3, from_int/1, parse_int/1, base_from_int/2, from_float/1]).
5+
-export([length/1, lowercase/1, uppercase/1, reverse/1, split/2, replace/3, from_int/1, parse_int/1, parse_float/1, base_from_int/2, from_float/1]).
66

77
length(A) ->
88
string:length(A).
@@ -81,6 +81,19 @@ parse_int_test() ->
8181
expect:equal(parse_int(<<"1.23">>), {error, parse_error}).
8282
-endif.
8383

84+
parse_float(A) ->
85+
gleam__stdlib:parse_float(A).
86+
87+
-ifdef(TEST).
88+
parse_float_test() ->
89+
expect:equal(parse_float(<<"1.23">>), {ok, 1.23}),
90+
expect:equal(parse_float(<<"5.0">>), {ok, 5.0}),
91+
expect:equal(parse_float(<<"0.123456789">>), {ok, 0.123456789}),
92+
expect:equal(parse_float(<<"">>), {error, parse_error}),
93+
expect:equal(parse_float(<<"what">>), {error, parse_error}),
94+
expect:equal(parse_float(<<"1">>), {error, parse_error}).
95+
-endif.
96+
8497
base_from_int(A, B) ->
8598
erlang:integer_to_binary(A, B).
8699

src/gleam__stdlib.erl

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
atom_create_from_string/1, atom_to_string/1, map_fetch/2,
77
iodata_append/2, iodata_prepend/2, identity/1, decode_int/1,
88
decode_string/1, decode_bool/1, decode_float/1, decode_thunk/1,
9-
decode_tuple/1, decode_list/1, decode_field/2, parse_int/1]).
9+
decode_tuple/1, decode_list/1, decode_field/2, parse_int/1, parse_float/1]).
1010

1111
expect_equal(Actual, Expected) -> ?assertEqual(Expected, Actual).
1212
expect_not_equal(Actual, Expected) -> ?assertNotEqual(Expected, Actual).
@@ -78,3 +78,12 @@ parse_int(String) ->
7878
_ ->
7979
{error, parse_error}
8080
end.
81+
82+
parse_float(String) ->
83+
case string:to_float(binary:bin_to_list(String)) of
84+
{Float, []} ->
85+
{ok, Float};
86+
87+
_ ->
88+
{error, parse_error}
89+
end.

src/str.gleam

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,34 @@ test parse_int {
121121
|> expect:equal(_, Error(ParseError))
122122
}
123123

124+
pub external fn parse_float(String) -> Result(Float, ParseError) = "gleam__stdlib" "parse_float";
125+
126+
test parse_float {
127+
"1.23"
128+
|> parse_float
129+
|> expect:equal(_, Ok(1.23))
130+
131+
"5.0"
132+
|> parse_float
133+
|> expect:equal(_, Ok(5.0))
134+
135+
"0.123456789"
136+
|> parse_float
137+
|> expect:equal(_, Ok(0.123456789))
138+
139+
""
140+
|> parse_float
141+
|> expect:equal(_, Error(ParseError))
142+
143+
"what"
144+
|> parse_float
145+
|> expect:equal(_, Error(ParseError))
146+
147+
"1"
148+
|> parse_float
149+
|> expect:equal(_, Error(ParseError))
150+
}
151+
124152
pub external fn base_from_int(Int, Int) -> String = "erlang" "integer_to_binary"
125153

126154
test base_from_int {

0 commit comments

Comments
 (0)