diff options
author | Louis Pilfold <louis@lpil.uk> | 2020-06-30 13:21:20 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2020-06-30 13:21:20 +0100 |
commit | cd9f78320acf2942b5e63cd5370f14f3cce12662 (patch) | |
tree | a59329451db43d2fefb6a3707e1a0bc6696a3497 | |
parent | 1fb3e8e6c69fbd254fdf5ede2dd098746ce35498 (diff) | |
download | gleam_stdlib-cd9f78320acf2942b5e63cd5370f14f3cce12662.tar.gz gleam_stdlib-cd9f78320acf2942b5e63cd5370f14f3cce12662.zip |
bit_string.{to_string, is_utf8}
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | src/gleam/bit_string.gleam | 30 | ||||
-rw-r--r-- | src/gleam_stdlib.erl | 4 | ||||
-rw-r--r-- | test/gleam/bit_string_test.gleam | 22 |
4 files changed, 54 insertions, 4 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 61c1375..b63e006 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ ## Unreleased - `bit_string` module created with `from_string`, `byte_size`, `append`, - `part`, `int_to_u32` and `int_from_u32`. + `part`, `to_string`, `is_utf8`, `int_to_u32` and `int_from_u32` functions. - The `bit_builder` module has been introduced with `prepend`, `append`, `prepend_builder`, `append_builder`, `prepend_string`, `append_string`, `concat`, `from_bit_string`, `to_bit_string`, and `byte_size` functions. diff --git a/src/gleam/bit_string.gleam b/src/gleam/bit_string.gleam index 0bcdc66..5236cf8 100644 --- a/src/gleam/bit_string.gleam +++ b/src/gleam/bit_string.gleam @@ -5,7 +5,7 @@ pub type BitString = BitString -/// Convert a utf8 String type into a raw BitString type. +/// Convert a UTF-8 String type into a raw BitString type. /// pub external fn from_string(String) -> BitString = "gleam_stdlib" "identity" @@ -52,3 +52,31 @@ pub external fn int_to_u32(Int) -> Result(BitString, Nil) = /// pub external fn int_from_u32(BitString) -> Result(Int, Nil) = "gleam_stdlib" "bit_string_int_from_u32" + +/// Test to see whether a bit string is valid UTF-8. +/// +pub fn is_utf8(bits: BitString) -> Bool { + case bits { + <<>> -> True + <<c:utf8, rest:binary>> -> { + // TODO: https://github.com/gleam-lang/gleam/issues/704 + let _ = c + is_utf8(rest) + } + _ -> False + } +} + +external fn unsafe_to_string(BitString) -> String = + "gleam_stdlib" "identity" + +/// Convert a bit string to a string. +/// +/// Returns an error if the bit string is valid UTF-8 data. +/// +pub fn to_string(bits: BitString) -> Result(String, Nil) { + case is_utf8(bits) { + True -> Ok(unsafe_to_string(bits)) + False -> Error(Nil) + } +} diff --git a/src/gleam_stdlib.erl b/src/gleam_stdlib.erl index db2f6fd..f84e658 100644 --- a/src/gleam_stdlib.erl +++ b/src/gleam_stdlib.erl @@ -202,6 +202,6 @@ regex_scan(Regex, String) -> end. base_decoded4(S) -> - try {ok, base64:decode(S)} catch - error:badarith -> {error, nil} + try {ok, base64:decode(S)} + catch error:badarith -> {error, nil} end. diff --git a/test/gleam/bit_string_test.gleam b/test/gleam/bit_string_test.gleam index 997f379..ea577fd 100644 --- a/test/gleam/bit_string_test.gleam +++ b/test/gleam/bit_string_test.gleam @@ -69,3 +69,25 @@ pub fn u32_test() { bit_string.int_from_u32(bit_string.from_string("12345")), ) } + +pub fn to_string_test() { + <<>> + |> bit_string.to_string + |> should.equal(Ok("")) + + <<"":utf8>> + |> bit_string.to_string + |> should.equal(Ok("")) + + <<"Hello":utf8>> + |> bit_string.to_string + |> should.equal(Ok("Hello")) + + <<"ø":utf8>> + |> bit_string.to_string + |> should.equal(Ok("ø")) + + <<65535:16>> + |> bit_string.to_string + |> should.equal(Error(Nil)) +} |