From c4470122c11d3b50234556f914927822455d47df Mon Sep 17 00:00:00 2001 From: Erik Terpstra Date: Fri, 22 May 2020 14:56:05 +0200 Subject: string.to_graphemes --- src/gleam/string.gleam | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) (limited to 'src') diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam index cf2a585..a6c7944 100644 --- a/src/gleam/string.gleam +++ b/src/gleam/string.gleam @@ -400,14 +400,6 @@ pub fn trim_right(string: String) -> String { erl_trim(string, Trailing) } -// TODO -// /// Convert a string to a list of Graphemes. -// /// -// /// > to_graphemes("abc") -// ['a','b','c'] -// -// /// -// pub fn to_graphemes(string: String) -> List(String) {} /// Split a non-empty string into its head and tail. This lets you /// pattern match on strings exactly as you would with lists. /// @@ -420,3 +412,15 @@ pub fn trim_right(string: String) -> String { /// pub external fn pop_grapheme(string: String) -> Option(tuple(String, String)) = "gleam_stdlib" "string_pop_grapheme" + +/// Convert a string to a list of Graphemes. +/// +/// > to_graphemes("abc") +/// ["a", "b", "c"] +/// +pub fn to_graphemes(string: String) -> List(String) { + case pop_grapheme(string) { + Ok(tuple(grapheme, rest)) -> [grapheme, ..to_graphemes(rest)] + _ -> [] + } +} -- cgit v1.2.3