diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/gleam/string.gleam | 21 | ||||
-rw-r--r-- | src/gleam/utf_codepoint.gleam | 22 |
2 files changed, 21 insertions, 22 deletions
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)) - } -} |