diff options
author | Tom Whatmore <tom@whatmore.me> | 2020-06-25 16:48:12 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2020-06-30 12:15:19 +0100 |
commit | 725e96b338621b8e1161679d9dc4215212df1e81 (patch) | |
tree | 3dca7ba0d81a7078ec69fdc668becd8988cc0fd1 | |
parent | c3c2284841b8dda0236d3366932a3d555e1ead55 (diff) | |
download | gleam_stdlib-725e96b338621b8e1161679d9dc4215212df1e81.tar.gz gleam_stdlib-725e96b338621b8e1161679d9dc4215212df1e81.zip |
Move UtfCodepoint functionality into string module
-rw-r--r-- | src/gleam/string.gleam | 21 | ||||
-rw-r--r-- | src/gleam/utf_codepoint.gleam | 22 | ||||
-rw-r--r-- | test/gleam/string_test.gleam | 14 | ||||
-rw-r--r-- | test/utf_codepoint_test.gleam | 16 |
4 files changed, 35 insertions, 38 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)) - } -} diff --git a/test/gleam/string_test.gleam b/test/gleam/string_test.gleam index 682dc94..2d563b4 100644 --- a/test/gleam/string_test.gleam +++ b/test/gleam/string_test.gleam @@ -295,3 +295,17 @@ pub fn to_graphemes_test() { |> string.to_graphemes() |> should.equal([]) } + +pub fn utf_codepoint_test() { + string.utf_codepoint(1114444) + |> should.be_error + + string.utf_codepoint(65534) + |> should.be_error + + string.utf_codepoint(55296) + |> should.be_error + + assert Ok(snake) = string.utf_codepoint(128013) + should.equal(<<snake:utf8_codepoint>>, <<"🐍":utf8>>) +} diff --git a/test/utf_codepoint_test.gleam b/test/utf_codepoint_test.gleam deleted file mode 100644 index a3747ba..0000000 --- a/test/utf_codepoint_test.gleam +++ /dev/null @@ -1,16 +0,0 @@ -import gleam/should -import gleam/utf_codepoint - -pub fn from_int_test() { - utf_codepoint.from_int(1114444) - |> should.be_error - - utf_codepoint.from_int(65534) - |> should.be_error - - utf_codepoint.from_int(55296) - |> should.be_error - - assert Ok(snake) = utf_codepoint.from_int(128013) - should.equal(<<snake:utf8_codepoint>>, <<"🐍":utf8>>) -} |