diff options
author | Louis Pilfold <louis@lpil.uk> | 2019-03-18 22:49:05 +0000 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2019-03-18 22:50:06 +0000 |
commit | 4120b46b38382395bcf4009b2c5ffd9442426547 (patch) | |
tree | abfb7cab57fad8a85fb210c208af0e4ec333b222 /src | |
parent | 2cbe6bd721ae2be68c29e4de71db51bafc3478f8 (diff) | |
download | gleam_stdlib-4120b46b38382395bcf4009b2c5ffd9442426547.tar.gz gleam_stdlib-4120b46b38382395bcf4009b2c5ffd9442426547.zip |
string module, iodata additions
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam__stdlib.erl | 4 | ||||
-rw-r--r-- | src/iodata.gleam | 94 | ||||
-rw-r--r-- | src/str.gleam | 79 |
3 files changed, 173 insertions, 4 deletions
diff --git a/src/gleam__stdlib.erl b/src/gleam__stdlib.erl index 3ab9fd6..8e288c0 100644 --- a/src/gleam__stdlib.erl +++ b/src/gleam__stdlib.erl @@ -8,8 +8,8 @@ decode_string/1, decode_bool/1, decode_float/1, decode_thunk/1, decode_tuple/1, decode_list/1, decode_field/2]). -expect_equal(A, Expected) -> ?assertEqual(Expected, A). -expect_not_equal(A, Expected) -> ?assertNotEqual(Expected, A). +expect_equal(Actual, Expected) -> ?assertEqual(Expected, Actual). +expect_not_equal(Actual, Expected) -> ?assertNotEqual(Expected, Actual). expect_true(A) -> ?assert(A). expect_false(A) -> ?assertNot(A). expect_is_ok(A) -> ?assertMatch({ok, _}, A). diff --git a/src/iodata.gleam b/src/iodata.gleam index 2d14ed0..cc54965 100644 --- a/src/iodata.gleam +++ b/src/iodata.gleam @@ -8,7 +8,10 @@ pub external fn prepend(Iodata, String) -> Iodata = pub external fn append(Iodata, String) -> Iodata = "gleam__stdlib" "iodata_append"; -pub external fn from(List(String)) -> Iodata = +pub external fn concat(List(String)) -> Iodata = + "gleam__stdlib" "identity"; + +pub external fn new(String) -> Iodata = "gleam__stdlib" "identity"; pub external fn to_string(Iodata) -> String = @@ -18,7 +21,7 @@ pub external fn byte_size(Iodata) -> Int = "erlang" "iolist_size"; test iodata { - let iodata = from(["ello"]) + let iodata = new("ello") |> append(_, ",") |> append(_, " world!") |> prepend(_, "H") @@ -31,3 +34,90 @@ test iodata { |> byte_size |> expect:equal(_, 13) } + +pub external fn lowercase(Iodata) -> Iodata = "string" "lowercase" + +test lowercase { + ["Gleam", "Gleam"] + |> concat + |> lowercase + |> to_string + |> expect:equal(_, "gleamgleam") +} + +pub external fn uppercase(Iodata) -> Iodata = "string" "uppercase" + +test uppercase { + ["Gleam", "Gleam"] + |> concat + |> uppercase + |> to_string + |> expect:equal(_, "GLEAMGLEAM") +} + +pub external fn reverse(Iodata) -> Iodata = "string" "reverse" + +enum Direction = + | All + +external fn erl_split(Iodata, String, Direction) -> List(Iodata) = + "string" "split" + +pub fn split(iodata, on) { + erl_split(iodata, on, All) +} + +test split { + "Gleam,Erlang,Elixir" + |> new + |> split(_, ",") + |> expect:equal(_, [new("Gleam"), new("Erlang"), new("Elixir")]) + + ["Gleam, Erl", "ang,Elixir"] + |> concat + |> split(_, ", ") + |> expect:equal(_, [new("Gleam"), concat(["Erl", "ang,Elixir"])]) +} + +external fn erl_replace(Iodata, String, String, Direction) -> Iodata = + "string" "replace" + +pub fn replace(iodata, pattern, replacement) { + erl_replace(iodata, pattern, replacement, All) +} + +pub external fn is_equal(Iodata, Iodata) -> Bool = "string" "equal" + +test is_equal { + new("12") + |> is_equal(_, concat(["1", "2"])) + |> expect:true + + new("12") + |> is_equal(_, new("12")) + |> expect:true + + new("12") + |> is_equal(_, new("2")) + |> expect:false +} + +pub external fn is_empty(Iodata) -> Bool = "string" "is_empty" + +test is_empty { + new("") + |> is_empty + |> expect:true + + new("12") + |> is_empty + |> expect:false + + concat([]) + |> is_empty + |> expect:true + + concat(["", ""]) + |> is_empty + |> expect:true +} diff --git a/src/str.gleam b/src/str.gleam new file mode 100644 index 0000000..8aa3e29 --- /dev/null +++ b/src/str.gleam @@ -0,0 +1,79 @@ +// Named str to avoid name collisions with the Erlang string module. +// Will rename later once we have namespaces for modules. + +import expect +import iodata +import list + +pub external fn length(String) -> Int = "string" "length" + +test length { + length("ß↑e̊") + |> expect:equal(_, 3) + + length("Gleam") + |> expect:equal(_, 5) + + length("") + |> expect:equal(_, 0) + + // TODO: This crashes. + // length("é") + // |> expect:equal(_, 1) +} + +pub external fn lowercase(String) -> String = "string" "lowercase" + +test lowercase { + lowercase("Gleam") + |> expect:equal(_, "gleam") +} + +pub external fn uppercase(String) -> String = "string" "uppercase" + +test uppercase { + uppercase("Gleam") + |> expect:equal(_, "GLEAM") +} + +pub fn reverse(string) { + string + |> iodata:new + |> iodata:reverse + |> iodata:to_string +} + +test reverse { + reverse("Gleam") + |> expect:equal(_, "maelG") +} + +pub fn split(string, on) { + string + |> iodata:new + |> iodata:split(_, on) + |> list:map(_, iodata:to_string) +} + +test split { + "Gleam,Erlang,Elixir" + |> split(_, ",") + |> expect:equal(_, ["Gleam", "Erlang", "Elixir"]) + + "Gleam, Erlang,Elixir" + |> split(_, ", ") + |> expect:equal(_, ["Gleam", "Erlang,Elixir"]) +} + +pub fn replace(string, pattern, with) { + string + |> iodata:new + |> iodata:replace(_, pattern, with) + |> iodata:to_string +} + +test replace { + "Gleam,Erlang,Elixir" + |> replace(_, ",", "++") + |> expect:equal(_, "Gleam++Erlang++Elixir") +} |