aboutsummaryrefslogtreecommitdiff
path: root/src
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 /src
parent95ad77fdd2a5f2282eee5248921973863f998427 (diff)
downloadgleam_stdlib-bfdde0ee0b60482f5d41e7e6cbae2b11f9daf08f.tar.gz
gleam_stdlib-bfdde0ee0b60482f5d41e7e6cbae2b11f9daf08f.zip
Implementing String.contains and String.repeat
Diffstat (limited to 'src')
-rw-r--r--src/gleam/string.gleam38
-rw-r--r--src/gleam_stdlib.erl11
2 files changed, 35 insertions, 14 deletions
diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam
index 46b1a25..b278c25 100644
--- a/src/gleam/string.gleam
+++ b/src/gleam/string.gleam
@@ -32,15 +32,6 @@ pub fn is_empty(str: String) -> Bool {
///
pub external fn length(String) -> Int = "string" "length"
-/// Repeat a string `n` times.
-///
-/// ## Examples
-/// ```gleam
-/// repeat("ha", times: 3) == "hahaha"
-/// ```
-///
-// pub fn repeat(string: String, times n: Int) -> String {}
-
/// Reverse a string.
///
/// ## Examples
@@ -141,9 +132,7 @@ pub external fn compare(String, String) -> order.Order =
/// ## Check for Substrings
-// TODO
-// TODO: Not sure about the name and labels here
-/// See if the second string contains the first one.
+/// Check if the first string contains the second.
///
/// ## Examples
/// ```gleam
@@ -152,7 +141,12 @@ pub external fn compare(String, String) -> order.Order =
/// contains(does: "theory", contain: "THE") == False
/// ```
///
-// pub fn contains(does haystack: String, contain needle: String) -> String {}
+external fn erl_contains(String, String) -> Bool =
+ "gleam_stdlib" "string_contains"
+
+pub fn contains(does haystack: String, contain needle: String) -> Bool {
+ erl_contains(haystack, needle)
+}
// TODO
// TODO: Not sure about the name and labels here
@@ -220,6 +214,24 @@ pub fn concat(strings: List(String)) -> String {
|> iodata.to_string
}
+/// Repeat a string `n` times.
+///
+/// ## Examples
+/// ```gleam
+/// repeat("ha", times: 3) == "hahaha"
+/// ```
+///
+fn repeat_help(chunk: String, result: List(String), repeats: Int) -> String {
+ case repeats <= 0 {
+ True -> concat(result)
+ False -> repeat_help(chunk, [chunk | result], repeats - 1)
+ }
+}
+
+pub fn repeat(string: String, times times: Int) -> String {
+ repeat_help(string, [""], times)
+}
+
/// Join many strings together with a given separator.
///
/// ## Examples
diff --git a/src/gleam_stdlib.erl b/src/gleam_stdlib.erl
index 1f1f652..9e6a864 100644
--- a/src/gleam_stdlib.erl
+++ b/src/gleam_stdlib.erl
@@ -6,7 +6,8 @@
atom_create_from_string/1, atom_to_string/1, map_get/2,
iodata_append/2, iodata_prepend/2, identity/1, decode_int/1,
decode_string/1, decode_bool/1, decode_float/1, decode_thunk/1, decode_atom/1,
- decode_list/1, decode_field/2, decode_element/2, parse_int/1, parse_float/1, compare_strings/2]).
+ decode_list/1, decode_field/2, decode_element/2, parse_int/1, parse_float/1, compare_strings/2,
+ string_contains/2]).
should_equal(Actual, Expected) -> ?assertEqual(Expected, Actual).
should_not_equal(Actual, Expected) -> ?assertNotEqual(Expected, Actual).
@@ -107,3 +108,11 @@ compare_strings(Lhs, Rhs) ->
true ->
gt
end.
+
+string_contains(Haystack, Needle) ->
+ case string:find(Haystack, Needle) of
+ nomatch ->
+ false;
+ _ ->
+ true
+ end.