diff options
-rw-r--r-- | gen/iodata.erl | 39 | ||||
-rw-r--r-- | gen/str.erl | 30 | ||||
-rw-r--r-- | rebar.config | 3 | ||||
-rw-r--r-- | src/iodata.gleam | 41 | ||||
-rw-r--r-- | src/str.gleam | 48 |
5 files changed, 139 insertions, 22 deletions
diff --git a/gen/iodata.erl b/gen/iodata.erl index 5a2ab4a..3401d3f 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, 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]). +-export([prepend/2, append/2, prepend_iodata/2, append_iodata/2, from_strings/1, concat/1, new/1, to_string/1, byte_size/1, from_float/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,6 +10,15 @@ prepend(A, B) -> append(A, B) -> gleam__stdlib:iodata_append(A, B). +prepend_iodata(A, B) -> + gleam__stdlib:iodata_prepend(A, B). + +append_iodata(A, B) -> + gleam__stdlib:iodata_append(A, B). + +from_strings(A) -> + gleam__stdlib:identity(A). + concat(A) -> gleam__stdlib:identity(A). @@ -22,12 +31,22 @@ to_string(A) -> byte_size(A) -> erlang:iolist_size(A). +from_float(A) -> + io_lib_format:fwrite_g(A). + -ifdef(TEST). iodata_test() -> Iodata = prepend(append(append(new(<<"ello">>), <<",">>), <<" world!">>), <<"H">>), expect:equal(to_string(Iodata), <<"Hello, world!">>), - expect:equal(byte_size(Iodata), 13). + expect:equal(byte_size(Iodata), 13), + Iodata1 = prepend_iodata(append_iodata(append_iodata(new(<<"ello">>), + new(<<",">>)), + concat([new(<<" wo">>), + new(<<"rld!">>)])), + new(<<"H">>)), + expect:equal(to_string(Iodata1), <<"Hello, world!">>), + expect:equal(byte_size(Iodata1), 13). -endif. lowercase(A) -> @@ -35,7 +54,7 @@ lowercase(A) -> -ifdef(TEST). lowercase_test() -> - expect:equal(to_string(lowercase(concat([<<"Gleam">>, <<"Gleam">>]))), + expect:equal(to_string(lowercase(from_strings([<<"Gleam">>, <<"Gleam">>]))), <<"gleamgleam">>). -endif. @@ -44,7 +63,7 @@ uppercase(A) -> -ifdef(TEST). uppercase_test() -> - expect:equal(to_string(uppercase(concat([<<"Gleam">>, <<"Gleam">>]))), + expect:equal(to_string(uppercase(from_strings([<<"Gleam">>, <<"Gleam">>]))), <<"GLEAMGLEAM">>). -endif. @@ -61,8 +80,10 @@ split(Iodata, On) -> 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">>])]). + expect:equal(split(from_strings([<<"Gleam, Erl">>, <<"ang,Elixir">>]), + <<", ">>), + [new(<<"Gleam">>), + from_strings([<<"Erl">>, <<"ang,Elixir">>])]). -endif. erl_replace(A, B, C, D) -> @@ -76,7 +97,7 @@ is_equal(A, B) -> -ifdef(TEST). is_equal_test() -> - expect:true(is_equal(new(<<"12">>), concat([<<"1">>, <<"2">>]))), + expect:true(is_equal(new(<<"12">>), from_strings([<<"1">>, <<"2">>]))), expect:true(is_equal(new(<<"12">>), new(<<"12">>))), expect:false(is_equal(new(<<"12">>), new(<<"2">>))). -endif. @@ -88,6 +109,6 @@ is_empty(A) -> is_empty_test() -> expect:true(is_empty(new(<<"">>))), expect:false(is_empty(new(<<"12">>))), - expect:true(is_empty(concat([]))), - expect:true(is_empty(concat([<<"">>, <<"">>]))). + expect:true(is_empty(from_strings([]))), + expect:true(is_empty(from_strings([<<"">>, <<"">>]))). -endif. diff --git a/gen/str.erl b/gen/str.erl index 9625807..4ca27f5 100644 --- a/gen/str.erl +++ b/gen/str.erl @@ -2,7 +2,7 @@ -compile(no_auto_import). -include_lib("eunit/include/eunit.hrl"). --export([length/1, lowercase/1, uppercase/1, reverse/1, split/2, replace/3]). +-export([length/1, lowercase/1, uppercase/1, reverse/1, split/2, replace/3, from_int/1, base_from_int/2, from_float/1]). length(A) -> string:length(A). @@ -57,3 +57,31 @@ replace_test() -> expect:equal(replace(<<"Gleam,Erlang,Elixir">>, <<",">>, <<"++">>), <<"Gleam++Erlang++Elixir">>). -endif. + +from_int(A) -> + erlang:integer_to_binary(A). + +-ifdef(TEST). +from_int_test() -> + expect:equal(from_int(123), <<"123">>), + expect:equal(from_int(-123), <<"-123">>), + expect:equal(from_int(123), <<"123">>). +-endif. + +base_from_int(A, B) -> + erlang:integer_to_binary(A, B). + +-ifdef(TEST). +base_from_int_test() -> + expect:equal(base_from_int(100, 16), <<"64">>), + expect:equal(base_from_int(-100, 16), <<"-64">>). +-endif. + +from_float(F) -> + iodata:to_string(iodata:from_float(F)). + +-ifdef(TEST). +from_float_test() -> + expect:equal(from_float(123.0), <<"123.0">>), + expect:equal(from_float(-8.1), <<"-8.1">>). +-endif. diff --git a/rebar.config b/rebar.config index c6aa981..25588d6 100644 --- a/rebar.config +++ b/rebar.config @@ -1,3 +1,6 @@ {erl_opts, [debug_info]}. {src_dirs, ["src", "gen"]}. + +{pre_hooks, [{"(linux|darwin|solaris|win32)", compile, "gleam build ."}]}. + {deps, []}. diff --git a/src/iodata.gleam b/src/iodata.gleam index 856832f..81329d0 100644 --- a/src/iodata.gleam +++ b/src/iodata.gleam @@ -11,7 +11,16 @@ pub external fn prepend(Iodata, String) -> Iodata = pub external fn append(Iodata, String) -> Iodata = "gleam__stdlib" "iodata_append"; -pub external fn concat(List(String)) -> Iodata = +pub external fn prepend_iodata(Iodata, Iodata) -> Iodata = + "gleam__stdlib" "iodata_prepend"; + +pub external fn append_iodata(Iodata, Iodata) -> Iodata = + "gleam__stdlib" "iodata_append"; + +pub external fn from_strings(List(String)) -> Iodata = + "gleam__stdlib" "identity"; + +pub external fn concat(List(Iodata)) -> Iodata = "gleam__stdlib" "identity"; pub external fn new(String) -> Iodata = @@ -23,6 +32,9 @@ pub external fn to_string(Iodata) -> String = pub external fn byte_size(Iodata) -> Int = "erlang" "iolist_size"; +pub external fn from_float(Float) -> Iodata = + "io_lib_format" "fwrite_g"; + test iodata { let iodata = new("ello") |> append(_, ",") @@ -36,13 +48,26 @@ test iodata { iodata |> byte_size |> expect:equal(_, 13) + + let iodata = new("ello") + |> append_iodata(_, new(",")) + |> append_iodata(_, concat([new(" wo"), new("rld!")])) + |> prepend_iodata(_, new("H")) + + iodata + |> to_string + |> expect:equal(_, "Hello, world!") + + iodata + |> byte_size + |> expect:equal(_, 13) } pub external fn lowercase(Iodata) -> Iodata = "string" "lowercase" test lowercase { ["Gleam", "Gleam"] - |> concat + |> from_strings |> lowercase |> to_string |> expect:equal(_, "gleamgleam") @@ -52,7 +77,7 @@ pub external fn uppercase(Iodata) -> Iodata = "string" "uppercase" test uppercase { ["Gleam", "Gleam"] - |> concat + |> from_strings |> uppercase |> to_string |> expect:equal(_, "GLEAMGLEAM") @@ -77,9 +102,9 @@ test split { |> expect:equal(_, [new("Gleam"), new("Erlang"), new("Elixir")]) ["Gleam, Erl", "ang,Elixir"] - |> concat + |> from_strings |> split(_, ", ") - |> expect:equal(_, [new("Gleam"), concat(["Erl", "ang,Elixir"])]) + |> expect:equal(_, [new("Gleam"), from_strings(["Erl", "ang,Elixir"])]) } external fn erl_replace(Iodata, String, String, Direction) -> Iodata = @@ -93,7 +118,7 @@ pub external fn is_equal(Iodata, Iodata) -> Bool = "string" "equal" test is_equal { new("12") - |> is_equal(_, concat(["1", "2"])) + |> is_equal(_, from_strings(["1", "2"])) |> expect:true new("12") @@ -116,11 +141,11 @@ test is_empty { |> is_empty |> expect:false - concat([]) + from_strings([]) |> is_empty |> expect:true - concat(["", ""]) + from_strings(["", ""]) |> is_empty |> expect:true } diff --git a/src/str.gleam b/src/str.gleam index 8aa3e29..b09b8dc 100644 --- a/src/str.gleam +++ b/src/str.gleam @@ -16,10 +16,6 @@ test length { length("") |> expect:equal(_, 0) - - // TODO: This crashes. - // length("é") - // |> expect:equal(_, 1) } pub external fn lowercase(String) -> String = "string" "lowercase" @@ -77,3 +73,47 @@ test replace { |> replace(_, ",", "++") |> expect:equal(_, "Gleam++Erlang++Elixir") } + +pub external fn from_int(Int) -> String = "erlang" "integer_to_binary" + +test from_int { + 123 + |> from_int + |> expect:equal(_, "123") + + -123 + |> from_int + |> expect:equal(_, "-123") + + 0123 + |> from_int + |> expect:equal(_, "123") +} + +pub external fn base_from_int(Int, Int) -> String = "erlang" "integer_to_binary" + +test base_from_int { + 100 + |> base_from_int(_, 16) + |> expect:equal(_, "64") + + -100 + |> base_from_int(_, 16) + |> expect:equal(_, "-64") +} + +pub fn from_float(f) { + f + |> iodata:from_float + |> iodata:to_string +} + +test from_float { + 123.0 + |> from_float + |> expect:equal(_, "123.0") + + -8.1 + |> from_float + |> expect:equal(_, "-8.1") +} |