From 725e96b338621b8e1161679d9dc4215212df1e81 Mon Sep 17 00:00:00 2001 From: Tom Whatmore Date: Thu, 25 Jun 2020 16:48:12 +0100 Subject: Move UtfCodepoint functionality into string module --- src/gleam/string.gleam | 21 +++++++++++++++++++++ src/gleam/utf_codepoint.gleam | 22 ---------------------- 2 files changed, 21 insertions(+), 22 deletions(-) delete mode 100644 src/gleam/utf_codepoint.gleam (limited to 'src') diff --git a/src/gleam/string.gleam b/src/gleam/string.gleam index 1714dad..2ae654e 100644 --- a/src/gleam/string.gleam +++ b/src/gleam/string.gleam @@ -10,6 +10,10 @@ import gleam/result pub type String = String +/// A UtfCodepoint is the integer representation of a valid UTF codepoint +pub type UtfCodepoint = + UtfCodepoint + /// Determine if a string is empty. /// /// ## Examples @@ -451,3 +455,20 @@ pub fn to_graphemes(string: String) -> List(String) { _ -> [] } } + +external fn int_to_utf_codepoint(Int) -> UtfCodepoint = + "gleam_stdlib" "identity" + +/// Convert an integer to a UtfCodepoint +/// +/// Returns an error if the integer does not represent a valid UTF codepoint. +/// +pub fn utf_codepoint(value: Int) -> Result(UtfCodepoint, Nil) { + case value { + i if i > 1114111 -> Error(Nil) + i if i == 65534 -> Error(Nil) + i if i == 65535 -> Error(Nil) + i if i >= 55296 && i <= 57343 -> Error(Nil) + i -> Ok(int_to_utf_codepoint(i)) + } +} diff --git a/src/gleam/utf_codepoint.gleam b/src/gleam/utf_codepoint.gleam deleted file mode 100644 index f1d0fbb..0000000 --- a/src/gleam/utf_codepoint.gleam +++ /dev/null @@ -1,22 +0,0 @@ -import gleam/result - -/// A UtfCodepoint is the integer representation of a valid UTF codepoint -pub type UtfCodepoint = - UtfCodepoint - -external fn int_to_utf8_codepoint(Int) -> UtfCodepoint = - "gleam_stdlib" "identity" - -/// Convert an integer to a UtfCodepoint -/// -/// Returns an error if the integer does not represent a valid UTF codepoint. -/// -pub fn from_int(value: Int) -> Result(UtfCodepoint, Nil) { - case value { - i if i > 1114111 -> Error(Nil) - i if i == 65534 -> Error(Nil) - i if i == 65535 -> Error(Nil) - i if i >= 55296 && i <= 57343 -> Error(Nil) - i -> Ok(int_to_utf8_codepoint(i)) - } -} -- cgit v1.2.3