aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorErik Terpstra <39518+eterps@users.noreply.github.com>2020-05-15 10:21:25 +0200
committerLouis Pilfold <louis@lpil.uk>2020-05-17 13:26:48 +0100
commit157b535280ba3d579d4ec3aafb0ec45db7272ca3 (patch)
tree556ec949bf6078d39100cdf00d42c5fcf48c89f0 /src
parente6b3c90c83a0eb78fe896377c524d496cfd2cb8e (diff)
downloadgleam_stdlib-157b535280ba3d579d4ec3aafb0ec45db7272ca3.tar.gz
gleam_stdlib-157b535280ba3d579d4ec3aafb0ec45db7272ca3.zip
string.starts_with & string.ends_with
Diffstat (limited to 'src')
-rw-r--r--src/gleam/string.gleam40
-rw-r--r--src/gleam_stdlib.erl14
2 files changed, 33 insertions, 21 deletions
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).