diff options
author | inoas <mail@inoas.com> | 2022-12-04 19:39:37 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2022-12-22 20:44:33 +0000 |
commit | 663729b8a619d1076fda8953abdb06070a786269 (patch) | |
tree | 6bbfd6a6d7bae928acf89821a19757b7fbbc5360 | |
parent | 86532391dac84e5136031940db962d45a7814a02 (diff) | |
download | gleam_stdlib-663729b8a619d1076fda8953abdb06070a786269.tar.gz gleam_stdlib-663729b8a619d1076fda8953abdb06070a786269.zip |
add string.to_ints
-rw-r--r-- | src/gleam/string.gleam | 15 | ||||
-rw-r--r-- | src/gleam_stdlib.mjs | 4 | ||||
-rw-r--r-- | test/gleam/string_test.gleam | 10 |
3 files changed, 29 insertions, 0 deletions
diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam index ece64b4..5369b32 100644 --- a/src/gleam/string.gleam +++ b/src/gleam/string.gleam @@ -900,6 +900,21 @@ pub fn utf_codepoint(value: Int) -> Result(UtfCodepoint, Nil) { } } +pub fn to_ints(s: String) -> List(Int) { + to_utf_codepoints(s) + |> list.map(utf_codepoint_to_int) +} + +if erlang { + external fn utf_codepoint_to_int(cp: UtfCodepoint) -> Int = + "gleam_stdlib" "identity" +} + +if javascript { + external fn utf_codepoint_to_int(cp: UtfCodepoint) -> Int = + "../gleam_stdlib.mjs" "utf_codepoint_to_int" +} + /// Converts a `String` into `Option(String)` where an empty `String` becomes /// `None`. /// diff --git a/src/gleam_stdlib.mjs b/src/gleam_stdlib.mjs index 9d37ec3..69644dc 100644 --- a/src/gleam_stdlib.mjs +++ b/src/gleam_stdlib.mjs @@ -363,6 +363,10 @@ export function utf_codepoint_list_to_string(utf_codepoint_integer_list) { .join(""); } +export function utf_codepoint_to_int(utf_codepoint) { + return utf_codepoint.value; +} + export function regex_check(regex, string) { return regex.test(string); } diff --git a/test/gleam/string_test.gleam b/test/gleam/string_test.gleam index 2092b8b..37671d3 100644 --- a/test/gleam/string_test.gleam +++ b/test/gleam/string_test.gleam @@ -510,6 +510,16 @@ pub fn bit_string_utf_codepoint_test() { should.equal(<<snake:utf8_codepoint>>, <<"🐍":utf8>>) } +pub fn to_ints_test() { + "abc" + |> string.to_ints + |> should.equal([97, 98, 99]) + + "🐍" + |> string.to_ints + |> should.equal([128013]) +} + pub fn to_option_test() { "" |> string.to_option |