Skip to content

Commit cd9f783

Browse files
committed
bit_string.{to_string, is_utf8}
1 parent 1fb3e8e commit cd9f783

File tree

4 files changed

+54
-4
lines changed

4 files changed

+54
-4
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
## Unreleased
44

55
- `bit_string` module created with `from_string`, `byte_size`, `append`,
6-
`part`, `int_to_u32` and `int_from_u32`.
6+
`part`, `to_string`, `is_utf8`, `int_to_u32` and `int_from_u32` functions.
77
- The `bit_builder` module has been introduced with `prepend`, `append`,
88
`prepend_builder`, `append_builder`, `prepend_string`, `append_string`,
99
`concat`, `from_bit_string`, `to_bit_string`, and `byte_size` functions.

src/gleam/bit_string.gleam

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
pub type BitString =
66
BitString
77

8-
/// Convert a utf8 String type into a raw BitString type.
8+
/// Convert a UTF-8 String type into a raw BitString type.
99
///
1010
pub external fn from_string(String) -> BitString =
1111
"gleam_stdlib" "identity"
@@ -52,3 +52,31 @@ pub external fn int_to_u32(Int) -> Result(BitString, Nil) =
5252
///
5353
pub external fn int_from_u32(BitString) -> Result(Int, Nil) =
5454
"gleam_stdlib" "bit_string_int_from_u32"
55+
56+
/// Test to see whether a bit string is valid UTF-8.
57+
///
58+
pub fn is_utf8(bits: BitString) -> Bool {
59+
case bits {
60+
<<>> -> True
61+
<<c:utf8, rest:binary>> -> {
62+
// TODO: https://github.com/gleam-lang/gleam/issues/704
63+
let _ = c
64+
is_utf8(rest)
65+
}
66+
_ -> False
67+
}
68+
}
69+
70+
external fn unsafe_to_string(BitString) -> String =
71+
"gleam_stdlib" "identity"
72+
73+
/// Convert a bit string to a string.
74+
///
75+
/// Returns an error if the bit string is valid UTF-8 data.
76+
///
77+
pub fn to_string(bits: BitString) -> Result(String, Nil) {
78+
case is_utf8(bits) {
79+
True -> Ok(unsafe_to_string(bits))
80+
False -> Error(Nil)
81+
}
82+
}

src/gleam_stdlib.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,6 @@ regex_scan(Regex, String) ->
202202
end.
203203

204204
base_decoded4(S) ->
205-
try {ok, base64:decode(S)} catch
206-
error:badarith -> {error, nil}
205+
try {ok, base64:decode(S)}
206+
catch error:badarith -> {error, nil}
207207
end.

test/gleam/bit_string_test.gleam

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,25 @@ pub fn u32_test() {
6969
bit_string.int_from_u32(bit_string.from_string("12345")),
7070
)
7171
}
72+
73+
pub fn to_string_test() {
74+
<<>>
75+
|> bit_string.to_string
76+
|> should.equal(Ok(""))
77+
78+
<<"":utf8>>
79+
|> bit_string.to_string
80+
|> should.equal(Ok(""))
81+
82+
<<"Hello":utf8>>
83+
|> bit_string.to_string
84+
|> should.equal(Ok("Hello"))
85+
86+
<<"ø":utf8>>
87+
|> bit_string.to_string
88+
|> should.equal(Ok("ø"))
89+
90+
<<65535:16>>
91+
|> bit_string.to_string
92+
|> should.equal(Error(Nil))
93+
}

0 commit comments

Comments
 (0)