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 /gen | |
parent | 2cbe6bd721ae2be68c29e4de71db51bafc3478f8 (diff) | |
download | gleam_stdlib-4120b46b38382395bcf4009b2c5ffd9442426547.tar.gz gleam_stdlib-4120b46b38382395bcf4009b2c5ffd9442426547.zip |
string module, iodata additions
Diffstat (limited to 'gen')
-rw-r--r-- | gen/iodata.erl | 71 | ||||
-rw-r--r-- | gen/str.erl | 59 |
2 files changed, 127 insertions, 3 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. |