Skip to content

Commit 87b92ef

Browse files
committed
Inspect non-byte bit strings with Gleam syntax
1 parent 126db53 commit 87b92ef

File tree

2 files changed

+16
-7
lines changed

2 files changed

+16
-7
lines changed

src/gleam_stdlib.erl

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -290,15 +290,19 @@ inspect(Any) when is_integer(Any) ->
290290
erlang:integer_to_list(Any);
291291
inspect(Any) when is_float(Any) ->
292292
io_lib_format:fwrite_g(Any);
293-
inspect(Binary) when is_binary(Binary) ->
294-
case inspect_maybe_utf8_string(Binary, <<>>) of
295-
{ok, InspectedUtf8String} -> InspectedUtf8String;
293+
inspect(Bits) when is_bitstring(Bits) ->
294+
case inspect_maybe_utf8_string(Bits, <<>>) of
295+
{ok, InspectedUtf8String} ->
296+
InspectedUtf8String;
296297
{error, not_a_utf8_string} ->
297-
Segments = [erlang:integer_to_list(X) || <<X>> <= Binary],
298-
["<<", lists:join(", ", Segments), ">>"]
298+
case is_binary(Bits) of
299+
true ->
300+
Segments = [erlang:integer_to_list(X) || <<X>> <= Bits],
301+
["<<", lists:join(", ", Segments), ">>"];
302+
false ->
303+
inspect_bit_array(Bits)
304+
end
299305
end;
300-
inspect(Bits) when is_bitstring(Bits) ->
301-
inspect_bit_array(Bits);
302306
inspect(List) when is_list(List) ->
303307
case inspect_list(List, true) of
304308
{charlist, _} -> ["charlist.from_string(\"", list_to_binary(List), "\")"];

test/gleam/string_test.gleam

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1250,6 +1250,11 @@ pub fn inspect_fifteen_bit_int_test() {
12501250
assert string.inspect(<<2, 3:size(7)>>) == "<<2, 3:size(7)>>"
12511251
}
12521252

1253+
@target(erlang)
1254+
pub fn inspect_non_byte_aligned_bit_array_test() {
1255+
assert string.inspect(<<251, 3:size(3)>>) == "<<251, 3:size(3)>>"
1256+
}
1257+
12531258
@target(javascript)
12541259
@external(javascript, "../gleam_stdlib_test_ffi.mjs", "circular_reference")
12551260
fn circular_reference() -> Dynamic

0 commit comments

Comments
 (0)