From 341eb947b4f3f33ea2bf700f629d328444b1df3f Mon Sep 17 00:00:00 2001 From: Tom Whatmore Date: Fri, 19 Jun 2020 11:21:20 +0100 Subject: Add utf_codepoint to stdlib --- src/gleam/utf_codepoint.gleam | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 src/gleam/utf_codepoint.gleam (limited to 'src') diff --git a/src/gleam/utf_codepoint.gleam b/src/gleam/utf_codepoint.gleam new file mode 100644 index 0000000..2c3cb04 --- /dev/null +++ b/src/gleam/utf_codepoint.gleam @@ -0,0 +1,20 @@ +import gleam/result + +pub type UtfCodepoint = UtfCodepoint + +pub type Error { + Invalid +} + +external fn int_to_utf8_codepoint(Int) -> UtfCodepoint = + "gleam_stdlib" "identity" + +pub fn from_int(value: Int) -> Result(UtfCodepoint, Error) { + case value { + i if i > 1114111 -> Error(Invalid) + i if i == 65534 -> Error(Invalid) + i if i == 65535 -> Error(Invalid) + i if i >= 55296 && i <= 57343 -> Error(Invalid) + i -> Ok(int_to_utf8_codepoint(i)) + } +} -- cgit v1.2.3