aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md3
-rw-r--r--gen/src/gleam@string.erl8
-rw-r--r--gen/test/gleam@string_test.erl38
-rw-r--r--src/gleam/string.gleam40
-rw-r--r--src/gleam_stdlib.erl14
-rw-r--r--test/gleam/string_test.gleam36
6 files changed, 115 insertions, 24 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 32fd37e..a09dae5 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -2,7 +2,8 @@
## Unreleased
-- The `string` module gains `trim`, `trim_left` and `trim_right` functions.
+- The `string` module gains `trim`, `trim_left`, `trim_right`, `starts_with`
+ and `ends_with` functions.
## v0.8.0 - 2020-04-28
diff --git a/gen/src/gleam@string.erl b/gen/src/gleam@string.erl
index c0557fd..1b914c9 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, contains/2, split/2, append/2, concat/1, repeat/2, join/2, trim/1, trim_left/1, trim_right/1]).
+-export([is_empty/1, length/1, reverse/1, replace/3, lowercase/1, uppercase/1, compare/2, contains/2, starts_with/2, ends_with/2, split/2, append/2, concat/1, repeat/2, join/2, trim/1, trim_left/1, trim_right/1]).
is_empty(Str) ->
Str =:= <<""/utf8>>.
@@ -32,6 +32,12 @@ erl_contains(A, B) ->
contains(Haystack, Needle) ->
erl_contains(Haystack, Needle).
+starts_with(A, B) ->
+ gleam_stdlib:string_starts_with(A, B).
+
+ends_with(A, B) ->
+ gleam_stdlib:string_ends_with(A, B).
+
split(X, Substring) ->
gleam@list:map(
gleam@iodata:split(gleam@iodata:new(X), Substring),
diff --git a/gen/test/gleam@string_test.erl b/gen/test/gleam@string_test.erl
index 07f3f9b..c6fe92f 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, contains_test/0, concat_test/0, repeat_test/0, join_test/0, trim_test/0, trim_left_test/0, trim_right_test/0]).
+-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, contains_test/0, concat_test/0, repeat_test/0, join_test/0, trim_test/0, trim_left_test/0, trim_right_test/0, starts_with_test/0, ends_with_test/0]).
length_test() ->
gleam@should:equal(gleam@string:length(<<"ß↑e̊"/utf8>>), 3),
@@ -116,3 +116,39 @@ trim_right_test() ->
gleam@string:trim_right(<<" hats \n"/utf8>>),
<<" hats"/utf8>>
).
+
+starts_with_test() ->
+ gleam@should:equal(
+ gleam@string:starts_with(<<"theory"/utf8>>, <<""/utf8>>),
+ true
+ ),
+ gleam@should:equal(
+ gleam@string:starts_with(<<"theory"/utf8>>, <<"the"/utf8>>),
+ true
+ ),
+ gleam@should:equal(
+ gleam@string:starts_with(<<"theory"/utf8>>, <<"ory"/utf8>>),
+ false
+ ),
+ gleam@should:equal(
+ gleam@string:starts_with(<<"theory"/utf8>>, <<"theory2"/utf8>>),
+ false
+ ).
+
+ends_with_test() ->
+ gleam@should:equal(
+ gleam@string:ends_with(<<"theory"/utf8>>, <<""/utf8>>),
+ true
+ ),
+ gleam@should:equal(
+ gleam@string:ends_with(<<"theory"/utf8>>, <<"ory"/utf8>>),
+ true
+ ),
+ gleam@should:equal(
+ gleam@string:ends_with(<<"theory"/utf8>>, <<"the"/utf8>>),
+ false
+ ),
+ gleam@should:equal(
+ gleam@string:ends_with(<<"theory"/utf8>>, <<"theory2"/utf8>>),
+ false
+ ).
diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam
index 2cad6f2..93c686f 100644
--- a/src/gleam/string.gleam
+++ b/src/gleam/string.gleam
@@ -176,26 +176,26 @@ pub fn contains(does haystack: String, contain needle: String) -> Bool {
erl_contains(haystack, needle)
}
-// TODO
-// TODO: Not sure about the name and labels here
-// See if the second string starts with the first one.
-//
-// ## Examples
-// > starts_with(does: "theory", start_with: "ory")
-// False
-//
-//
-// pub fn starts_with(does string: String, start_with prefix: String) -> String {}
-// TODO
-// TODO: Not sure about the name and labels here
-// See if the second string ends with the first one.
-//
-// ## Examples
-// > ends_with(does: "theory", end_with: "ory")
-// True
-//
-//
-// pub fn ends_with(does string: String, end_with suffix: String) -> String {}
+/// See if the first string starts with the second one.
+///
+/// ## Examples
+/// > starts_with("theory", "ory")
+/// False
+///
+///
+pub external fn starts_with(String, String) -> Bool =
+ "gleam_stdlib" "string_starts_with"
+
+/// See if the first string ends with the second one.
+///
+/// ## Examples
+/// > ends_with("theory", "ory")
+/// True
+///
+///
+pub external fn ends_with(String, String) -> Bool =
+ "gleam_stdlib" "string_ends_with"
+
/// Create a list of strings by splitting a given string on a given substring.
///
/// ## Examples
diff --git a/src/gleam_stdlib.erl b/src/gleam_stdlib.erl
index 49a7e2a..fbd84da 100644
--- a/src/gleam_stdlib.erl
+++ b/src/gleam_stdlib.erl
@@ -7,7 +7,7 @@
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,
- string_contains/2]).
+ string_contains/2, string_starts_with/2, string_ends_with/2]).
should_equal(Actual, Expected) -> ?assertEqual(Expected, Actual).
should_not_equal(Actual, Expected) -> ?assertNotEqual(Expected, Actual).
@@ -127,3 +127,15 @@ string_contains(Haystack, Needle) ->
_ ->
true
end.
+
+string_starts_with(_, <<>>) -> true;
+string_starts_with(String, Prefix) when byte_size(Prefix) > byte_size(String) -> false;
+string_starts_with(String, Prefix) ->
+ PrefixSize = byte_size(Prefix),
+ Prefix == binary_part(String, 0, PrefixSize).
+
+string_ends_with(_, <<>>) -> true;
+string_ends_with(String, Suffix) when byte_size(Suffix) > byte_size(String) -> false;
+string_ends_with(String, Suffix) ->
+ SuffixSize = byte_size(Suffix),
+ Suffix == binary_part(String, byte_size(String) - SuffixSize, SuffixSize).
diff --git a/test/gleam/string_test.gleam b/test/gleam/string_test.gleam
index 01e95eb..03678ea 100644
--- a/test/gleam/string_test.gleam
+++ b/test/gleam/string_test.gleam
@@ -127,3 +127,39 @@ pub fn trim_right_test() {
|> string.trim_right()
|> should.equal(" hats")
}
+
+pub fn starts_with_test(){
+ "theory"
+ |> string.starts_with("")
+ |> should.equal(True)
+
+ "theory"
+ |> string.starts_with("the")
+ |> should.equal(True)
+
+ "theory"
+ |> string.starts_with("ory")
+ |> should.equal(False)
+
+ "theory"
+ |> string.starts_with("theory2")
+ |> should.equal(False)
+}
+
+pub fn ends_with_test() {
+ "theory"
+ |> string.ends_with("")
+ |> should.equal(True)
+
+ "theory"
+ |> string.ends_with("ory")
+ |> should.equal(True)
+
+ "theory"
+ |> string.ends_with("the")
+ |> should.equal(False)
+
+ "theory"
+ |> string.ends_with("theory2")
+ |> should.equal(False)
+}