aboutsummaryrefslogtreecommitdiff
path: root/src/str.gleam
diff options
context:
space:
mode:
authorMainShayne233 <shaynetremblay@hotmail.com>2019-04-05 18:51:13 -0400
committerLouis Pilfold <louis@lpil.uk>2019-04-07 08:54:47 +0000
commit673e9e907093bd448db3b108b3d654c13cdc66f7 (patch)
tree5d1b50f5ffb80b35c5d8a7c033e769f6337a1893 /src/str.gleam
parente6fc269473f9b60e12a3c4eb4d2e17d0bd0c8f4a (diff)
downloadgleam_stdlib-673e9e907093bd448db3b108b3d654c13cdc66f7.tar.gz
gleam_stdlib-673e9e907093bd448db3b108b3d654c13cdc66f7.zip
Implement parse_int
Diffstat (limited to 'src/str.gleam')
-rw-r--r--src/str.gleam31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/str.gleam b/src/str.gleam
index b09b8dc..6ee8235 100644
--- a/src/str.gleam
+++ b/src/str.gleam
@@ -7,6 +7,9 @@ import list
pub external fn length(String) -> Int = "string" "length"
+pub enum ParseError =
+ | ParseError
+
test length {
length("ß↑e̊")
|> expect:equal(_, 3)
@@ -90,6 +93,34 @@ test from_int {
|> expect:equal(_, "123")
}
+pub external fn parse_int(String) -> Result(Int, ParseError) = "gleam__stdlib" "parse_int";
+
+test parse_int {
+ "123"
+ |> parse_int
+ |> expect:equal(_, Ok(123))
+
+ "-123"
+ |> parse_int
+ |> expect:equal(_, Ok(-123))
+
+ "0123"
+ |> parse_int
+ |> expect:equal(_, Ok(123))
+
+ ""
+ |> parse_int
+ |> expect:equal(_, Error(ParseError))
+
+ "what"
+ |> parse_int
+ |> expect:equal(_, Error(ParseError))
+
+ "1.23"
+ |> parse_int
+ |> expect:equal(_, Error(ParseError))
+}
+
pub external fn base_from_int(Int, Int) -> String = "erlang" "integer_to_binary"
test base_from_int {