aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--gen/iodata.erl71
-rw-r--r--gen/str.erl59
-rw-r--r--src/gleam__stdlib.erl4
-rw-r--r--src/iodata.gleam94
-rw-r--r--src/str.gleam79
5 files changed, 300 insertions, 7 deletions
diff --git a/gen/iodata.erl b/gen/iodata.erl
index 5503bf1..5a2ab4a 100644
--- a/gen/iodata.erl
+++ b/gen/iodata.erl
@@ -2,7 +2,7 @@
-compile(no_auto_import).
-include_lib("eunit/include/eunit.hrl").
--export([prepend/2, append/2, from/1, to_string/1, byte_size/1]).
+-export([prepend/2, append/2, concat/1, new/1, to_string/1, byte_size/1, lowercase/1, uppercase/1, reverse/1, split/2, replace/3, is_equal/2, is_empty/1]).
prepend(A, B) ->
gleam__stdlib:iodata_prepend(A, B).
@@ -10,7 +10,10 @@ prepend(A, B) ->
append(A, B) ->
gleam__stdlib:iodata_append(A, B).
-from(A) ->
+concat(A) ->
+ gleam__stdlib:identity(A).
+
+new(A) ->
gleam__stdlib:identity(A).
to_string(A) ->
@@ -21,8 +24,70 @@ byte_size(A) ->
-ifdef(TEST).
iodata_test() ->
- Iodata = prepend(append(append(from([<<"ello">>]), <<",">>), <<" world!">>),
+ Iodata = prepend(append(append(new(<<"ello">>), <<",">>), <<" world!">>),
<<"H">>),
expect:equal(to_string(Iodata), <<"Hello, world!">>),
expect:equal(byte_size(Iodata), 13).
-endif.
+
+lowercase(A) ->
+ string:lowercase(A).
+
+-ifdef(TEST).
+lowercase_test() ->
+ expect:equal(to_string(lowercase(concat([<<"Gleam">>, <<"Gleam">>]))),
+ <<"gleamgleam">>).
+-endif.
+
+uppercase(A) ->
+ string:uppercase(A).
+
+-ifdef(TEST).
+uppercase_test() ->
+ expect:equal(to_string(uppercase(concat([<<"Gleam">>, <<"Gleam">>]))),
+ <<"GLEAMGLEAM">>).
+-endif.
+
+reverse(A) ->
+ string:reverse(A).
+
+erl_split(A, B, C) ->
+ string:split(A, B, C).
+
+split(Iodata, On) ->
+ erl_split(Iodata, On, all).
+
+-ifdef(TEST).
+split_test() ->
+ expect:equal(split(new(<<"Gleam,Erlang,Elixir">>), <<",">>),
+ [new(<<"Gleam">>), new(<<"Erlang">>), new(<<"Elixir">>)]),
+ expect:equal(split(concat([<<"Gleam, Erl">>, <<"ang,Elixir">>]), <<", ">>),
+ [new(<<"Gleam">>), concat([<<"Erl">>, <<"ang,Elixir">>])]).
+-endif.
+
+erl_replace(A, B, C, D) ->
+ string:replace(A, B, C, D).
+
+replace(Iodata, Pattern, Replacement) ->
+ erl_replace(Iodata, Pattern, Replacement, all).
+
+is_equal(A, B) ->
+ string:equal(A, B).
+
+-ifdef(TEST).
+is_equal_test() ->
+ expect:true(is_equal(new(<<"12">>), concat([<<"1">>, <<"2">>]))),
+ expect:true(is_equal(new(<<"12">>), new(<<"12">>))),
+ expect:false(is_equal(new(<<"12">>), new(<<"2">>))).
+-endif.
+
+is_empty(A) ->
+ string:is_empty(A).
+
+-ifdef(TEST).
+is_empty_test() ->
+ expect:true(is_empty(new(<<"">>))),
+ expect:false(is_empty(new(<<"12">>))),
+ expect:true(is_empty(concat([]))),
+ expect:true(is_empty(concat([<<"">>, <<"">>]))).
+-endif.
diff --git a/gen/str.erl b/gen/str.erl
new file mode 100644
index 0000000..9625807
--- /dev/null
+++ b/gen/str.erl
@@ -0,0 +1,59 @@
+-module(str).
+-compile(no_auto_import).
+-include_lib("eunit/include/eunit.hrl").
+
+-export([length/1, lowercase/1, uppercase/1, reverse/1, split/2, replace/3]).
+
+length(A) ->
+ string:length(A).
+
+-ifdef(TEST).
+length_test() ->
+ expect:equal(length(<<"ß↑e̊">>), 3),
+ expect:equal(length(<<"Gleam">>), 5),
+ expect:equal(length(<<"">>), 0).
+-endif.
+
+lowercase(A) ->
+ string:lowercase(A).
+
+-ifdef(TEST).
+lowercase_test() ->
+ expect:equal(lowercase(<<"Gleam">>), <<"gleam">>).
+-endif.
+
+uppercase(A) ->
+ string:uppercase(A).
+
+-ifdef(TEST).
+uppercase_test() ->
+ expect:equal(uppercase(<<"Gleam">>), <<"GLEAM">>).
+-endif.
+
+reverse(String) ->
+ iodata:to_string(iodata:reverse(iodata:new(String))).
+
+-ifdef(TEST).
+reverse_test() ->
+ expect:equal(reverse(<<"Gleam">>), <<"maelG">>).
+-endif.
+
+split(String, On) ->
+ list:map(iodata:split(iodata:new(String), On), fun iodata:to_string/1).
+
+-ifdef(TEST).
+split_test() ->
+ expect:equal(split(<<"Gleam,Erlang,Elixir">>, <<",">>),
+ [<<"Gleam">>, <<"Erlang">>, <<"Elixir">>]),
+ expect:equal(split(<<"Gleam, Erlang,Elixir">>, <<", ">>),
+ [<<"Gleam">>, <<"Erlang,Elixir">>]).
+-endif.
+
+replace(String, Pattern, With) ->
+ iodata:to_string(iodata:replace(iodata:new(String), Pattern, With)).
+
+-ifdef(TEST).
+replace_test() ->
+ expect:equal(replace(<<"Gleam,Erlang,Elixir">>, <<",">>, <<"++">>),
+ <<"Gleam++Erlang++Elixir">>).
+-endif.
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")
+}