Skip to content

Commit efdf9ba

Browse files
committed
Inspect bit strings
1 parent ba0a159 commit efdf9ba

File tree

2 files changed

+22
-20
lines changed

2 files changed

+22
-20
lines changed

src/gleam_stdlib.erl

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -337,35 +337,34 @@ inspect(Any) when is_integer(Any) ->
337337
erlang:integer_to_list(Any);
338338
inspect(Any) when is_float(Any) ->
339339
io_lib_format:fwrite_g(Any);
340-
inspect(Any) when is_binary(Any) ->
341-
Pattern = [$"],
342-
Replacement = [$\\, $\\, $"],
343-
Escaped = re:replace(Any, Pattern, Replacement, [{return, binary}, global]),
344-
["\"", Escaped, "\""];
345-
inspect(Any) when is_list(Any) ->
346-
["[",
347-
lists:join(<<", ">>,
348-
lists:map(fun inspect/1, Any)
349-
),
350-
"]"];
340+
inspect(Binary) when is_binary(Binary) ->
341+
case gleam@bit_string:is_utf8(Binary) of
342+
true ->
343+
Pattern = [$"],
344+
Replacement = [$\\, $\\, $"],
345+
Escaped = re:replace(Binary, Pattern, Replacement, [{return, binary}, global]),
346+
["\"", Escaped, "\""];
347+
false ->
348+
Segments = [erlang:integer_to_list(X) || <<X>> <= Binary],
349+
["<<", lists:join(", ", Segments), ">>"]
350+
end;
351+
inspect(List) when is_list(List) ->
352+
Elements = lists:join(<<", ">>, lists:map(fun inspect/1, List)),
353+
["[", Elements, "]"];
351354
inspect(Any) when is_tuple(Any) % Record constructors
352355
andalso is_atom(element(1, Any))
353356
andalso element(1, Any) =/= false
354357
andalso element(1, Any) =/= true
355358
andalso element(1, Any) =/= nil
356359
->
357360
[Atom | ArgsList] = erlang:tuple_to_list(Any),
358-
Args =
359-
lists:join(<<", ">>,
360-
lists:map(fun inspect/1, ArgsList)
361+
Args = lists:join(<<", ">>,
362+
lists:map(fun inspect/1, ArgsList)
361363
),
362364
[inspect(Atom), "(", Args, ")"];
363-
inspect(Any) when is_tuple(Any) ->
364-
["#(",
365-
lists:join(<<", ">>,
366-
lists:map(fun inspect/1, erlang:tuple_to_list(Any))
367-
),
368-
")"];
365+
inspect(Tuple) when is_tuple(Tuple) ->
366+
Elements = lists:map(fun inspect/1, erlang:tuple_to_list(Tuple)),
367+
["#(", lists:join(", ", Elements), ")"];
369368
inspect(Any) when is_function(Any) ->
370369
{arity, Arity} = erlang:fun_info(Any, arity),
371370
ArgsAsciiCodes = lists:seq($a, $a + Arity - 1),

test/gleam/string_test.gleam

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -700,6 +700,9 @@ pub fn inspect_test() {
700700

701701
string.inspect(InspectTypeOne(InspectTypeZero))
702702
|> should.equal("InspectTypeOne(InspectTypeZero)")
703+
704+
string.inspect(<<255, 2, 0>>)
705+
|> should.equal("<<255, 2, 0>>")
703706
}
704707

705708
if javascript {

0 commit comments

Comments
 (0)