aboutsummaryrefslogtreecommitdiff
path: root/gen
diff options
context:
space:
mode:
authorgreggreg <greg@greggreg.org>2020-04-16 08:50:24 -0600
committerLouis Pilfold <louis@lpil.uk>2020-04-16 16:23:19 +0100
commitbfdde0ee0b60482f5d41e7e6cbae2b11f9daf08f (patch)
tree15df4af63010fb0010b821dcf6f4b5000b4f7479 /gen
parent95ad77fdd2a5f2282eee5248921973863f998427 (diff)
downloadgleam_stdlib-bfdde0ee0b60482f5d41e7e6cbae2b11f9daf08f.tar.gz
gleam_stdlib-bfdde0ee0b60482f5d41e7e6cbae2b11f9daf08f.zip
Implementing String.contains and String.repeat
Diffstat (limited to 'gen')
-rw-r--r--gen/src/gleam@string.erl20
-rw-r--r--gen/test/gleam@string_test.erl15
2 files changed, 33 insertions, 2 deletions
diff --git a/gen/src/gleam@string.erl b/gen/src/gleam@string.erl
index a09e7e4..d383316 100644
--- a/gen/src/gleam@string.erl
+++ b/gen/src/gleam@string.erl
@@ -1,7 +1,7 @@
-module(gleam@string).
-compile(no_auto_import).
--export([is_empty/1, length/1, reverse/1, replace/3, lowercase/1, uppercase/1, compare/2, split/2, append/2, concat/1, join/2]).
+-export([is_empty/1, length/1, reverse/1, replace/3, lowercase/1, uppercase/1, compare/2, contains/2, split/2, append/2, concat/1, repeat/2, join/2]).
is_empty(Str) ->
Str =:= <<"">>.
@@ -26,6 +26,12 @@ uppercase(A) ->
compare(A, B) ->
gleam_stdlib:compare_strings(A, B).
+erl_contains(A, B) ->
+ gleam_stdlib:string_contains(A, B).
+
+contains(Haystack, Needle) ->
+ erl_contains(Haystack, Needle).
+
split(X, Substring) ->
gleam@list:map(
gleam@iodata:split(gleam@iodata:new(X), Substring),
@@ -40,6 +46,18 @@ append(First, Second) ->
concat(Strings) ->
gleam@iodata:to_string(gleam@iodata:from_strings(Strings)).
+repeat_help(Chunk, Result, Repeats) ->
+ case Repeats =< 0 of
+ true ->
+ concat(Result);
+
+ false ->
+ repeat_help(Chunk, [Chunk | Result], Repeats - 1)
+ end.
+
+repeat(String, Times) ->
+ repeat_help(String, [<<"">>], Times).
+
join(Strings, Separator) ->
gleam@iodata:to_string(
gleam@iodata:from_strings(gleam@list:intersperse(Strings, Separator))
diff --git a/gen/test/gleam@string_test.erl b/gen/test/gleam@string_test.erl
index b6708d3..15d3a4f 100644
--- a/gen/test/gleam@string_test.erl
+++ b/gen/test/gleam@string_test.erl
@@ -1,7 +1,7 @@
-module(gleam@string_test).
-compile(no_auto_import).
--export([length_test/0, lowercase_test/0, uppercase_test/0, reverse_test/0, split_test/0, replace_test/0, append_test/0, compare_test/0, concat_test/0, join_test/0]).
+-export([length_test/0, lowercase_test/0, uppercase_test/0, reverse_test/0, split_test/0, replace_test/0, append_test/0, repeat_test/0, compare_test/0, contains_test/0, concat_test/0, join_test/0]).
length_test() ->
gleam@should:equal(gleam@string:length(<<"ß↑e̊">>), 3),
@@ -39,6 +39,11 @@ append_test() ->
<<"Test Me">>
).
+repeat_test() ->
+ gleam@should:equal(gleam@string:repeat(<<"hi">>, 3), <<"hihihi">>),
+ gleam@should:equal(gleam@string:repeat(<<"hi">>, 0), <<"">>),
+ gleam@should:equal(gleam@string:repeat(<<"hi">>, -1), <<"">>).
+
compare_test() ->
gleam@should:equal(gleam@string:compare(<<"">>, <<"">>), eq),
gleam@should:equal(gleam@string:compare(<<"a">>, <<"">>), gt),
@@ -46,6 +51,14 @@ compare_test() ->
gleam@should:equal(gleam@string:compare(<<"A">>, <<"B">>), lt),
gleam@should:equal(gleam@string:compare(<<"t">>, <<"ABC">>), gt).
+contains_test() ->
+ gleam@should:equal(gleam@string:contains(<<"gleam">>, <<"ea">>), true),
+ gleam@should:equal(gleam@string:contains(<<"gleam">>, <<"x">>), false),
+ gleam@should:equal(
+ gleam@string:contains(<<"bellwether">>, <<"bell">>),
+ true
+ ).
+
concat_test() ->
gleam@should:equal(
gleam@string:concat([<<"Hello">>, <<", ">>, <<"world!">>]),