aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--CHANGELOG.md4
-rw-r--r--src/gleam/string.gleam23
-rw-r--r--src/gleam_stdlib.erl11
-rw-r--r--test/gleam/string_test.gleam14
4 files changed, 38 insertions, 14 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 43c84e0..e4d91dd 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -5,8 +5,8 @@
- Created the `io` module with `print` function.
- The `result` module gains the `nil_error` function.
- The `string` module gains `trim`, `trim_left`, `trim_right`, `starts_with`,
- `ends_with`, `slice`, `pad_left`, `pad_right` `drop_left` and `drop_right`
- functions.
+ `ends_with`, `slice`, `pad_left`, `pad_right` `drop_left`, `drop_right` and
+ `pop_grapheme` functions.
- `uri` module created with `parse`, `parse_query`, `path_segments`,
`query_to_string` and `to_string`.
- The `dynamic` module gains the `map`, `opaque_list`, `tuple2`, and `tuple2_of` functions.
diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam
index 25122b9..cf2a585 100644
--- a/src/gleam/string.gleam
+++ b/src/gleam/string.gleam
@@ -399,6 +399,7 @@ pub fn trim_left(string: String) -> String {
pub fn trim_right(string: String) -> String {
erl_trim(string, Trailing)
}
+
// TODO
// /// Convert a string to a list of Graphemes.
// ///
@@ -407,13 +408,15 @@ pub fn trim_right(string: String) -> String {
//
// ///
// pub fn to_graphemes(string: String) -> List(String) {}
-// TODO
-// Split a non-empty string into its head and tail. This lets you
-// pattern match on strings exactly as you would with lists.
-//
-// ## Examples
-// > next_grapheme("")
-// Error(Nil)
-//
-//
-// pub fn next_grapheme(string: String) -> Option(tuple(Grapheme, String)) {}
+/// Split a non-empty string into its head and tail. This lets you
+/// pattern match on strings exactly as you would with lists.
+///
+/// ## Examples
+/// > pop_grapheme("gleam")
+/// Ok(tuple("g", "leam"))
+///
+/// > pop_grapheme("")
+/// Error(Nil)
+///
+pub external fn pop_grapheme(string: String) -> Option(tuple(String, String)) =
+ "gleam_stdlib" "string_pop_grapheme"
diff --git a/src/gleam_stdlib.erl b/src/gleam_stdlib.erl
index 0efedec..fc5499c 100644
--- a/src/gleam_stdlib.erl
+++ b/src/gleam_stdlib.erl
@@ -8,8 +8,8 @@
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_starts_with/2, string_ends_with/2, string_pad/4,
- decode_tuple2/1, decode_map/1]).
+ string_pop_grapheme/1, string_starts_with/2, string_ends_with/2,
+ string_pad/4, decode_tuple2/1, decode_map/1]).
should_equal(Actual, Expected) -> ?assertEqual(Expected, Actual).
should_not_equal(Actual, Expected) -> ?assertNotEqual(Expected, Actual).
@@ -135,3 +135,10 @@ string_ends_with(String, Suffix) ->
string_pad(String, Length, Dir, PadString) ->
unicode:characters_to_binary(string:pad(String, Length, Dir, PadString)).
+
+string_pop_grapheme(String) ->
+ case string:next_grapheme(String) of
+ [ Next | Rest ] ->
+ {ok, {unicode:characters_to_binary([Next]), unicode:characters_to_binary(Rest)}};
+ _ -> {error, nil}
+ end.
diff --git a/test/gleam/string_test.gleam b/test/gleam/string_test.gleam
index 0695749..c8eae63 100644
--- a/test/gleam/string_test.gleam
+++ b/test/gleam/string_test.gleam
@@ -253,3 +253,17 @@ pub fn pad_right_test() {
|> string.pad_right(to: 5, with: "XY")
|> should.equal("121XYXY")
}
+
+pub fn pop_grapheme_test() {
+ "gleam"
+ |> string.pop_grapheme()
+ |> should.equal(Ok(tuple("g", "leam")))
+
+ "g"
+ |> string.pop_grapheme()
+ |> should.equal(Ok(tuple("g", "")))
+
+ ""
+ |> string.pop_grapheme()
+ |> should.equal(Error(Nil))
+}