From 663729b8a619d1076fda8953abdb06070a786269 Mon Sep 17 00:00:00 2001 From: inoas Date: Sun, 4 Dec 2022 19:39:37 +0100 Subject: add string.to_ints --- src/gleam/string.gleam | 15 +++++++++++++++ src/gleam_stdlib.mjs | 4 ++++ 2 files changed, 19 insertions(+) (limited to 'src') 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); } -- cgit v1.2.3