aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorLouis Pilfold <louis@lpil.uk>2022-06-15 21:58:48 +0100
committerLouis Pilfold <louis@lpil.uk>2022-06-15 21:58:50 +0100
commitefdf9ba616850cd85d0f621ca4f2e486b36bceec (patch)
tree88ea38d3ed4b2de94e45e892f219a6ead60db5c8 /src
parentba0a15976a908e1d4b5a13fc88b533adf2c35f85 (diff)
downloadgleam_stdlib-efdf9ba616850cd85d0f621ca4f2e486b36bceec.tar.gz
gleam_stdlib-efdf9ba616850cd85d0f621ca4f2e486b36bceec.zip
Inspect bit strings
Diffstat (limited to 'src')
-rw-r--r--src/gleam_stdlib.erl39
1 files changed, 19 insertions, 20 deletions
diff --git a/src/gleam_stdlib.erl b/src/gleam_stdlib.erl
index 320d80b..e7b80d6 100644
--- a/src/gleam_stdlib.erl
+++ b/src/gleam_stdlib.erl
@@ -337,17 +337,20 @@ inspect(Any) when is_integer(Any) ->
erlang:integer_to_list(Any);
inspect(Any) when is_float(Any) ->
io_lib_format:fwrite_g(Any);
-inspect(Any) when is_binary(Any) ->
- Pattern = [$"],
- Replacement = [$\\, $\\, $"],
- Escaped = re:replace(Any, Pattern, Replacement, [{return, binary}, global]),
- ["\"", Escaped, "\""];
-inspect(Any) when is_list(Any) ->
- ["[",
- lists:join(<<", ">>,
- lists:map(fun inspect/1, Any)
- ),
- "]"];
+inspect(Binary) when is_binary(Binary) ->
+ case gleam@bit_string:is_utf8(Binary) of
+ true ->
+ Pattern = [$"],
+ Replacement = [$\\, $\\, $"],
+ Escaped = re:replace(Binary, Pattern, Replacement, [{return, binary}, global]),
+ ["\"", Escaped, "\""];
+ false ->
+ Segments = [erlang:integer_to_list(X) || <<X>> <= Binary],
+ ["<<", lists:join(", ", Segments), ">>"]
+ end;
+inspect(List) when is_list(List) ->
+ Elements = lists:join(<<", ">>, lists:map(fun inspect/1, List)),
+ ["[", Elements, "]"];
inspect(Any) when is_tuple(Any) % Record constructors
andalso is_atom(element(1, Any))
andalso element(1, Any) =/= false
@@ -355,17 +358,13 @@ inspect(Any) when is_tuple(Any) % Record constructors
andalso element(1, Any) =/= nil
->
[Atom | ArgsList] = erlang:tuple_to_list(Any),
- Args =
- lists:join(<<", ">>,
- lists:map(fun inspect/1, ArgsList)
+ Args = lists:join(<<", ">>,
+ lists:map(fun inspect/1, ArgsList)
),
[inspect(Atom), "(", Args, ")"];
-inspect(Any) when is_tuple(Any) ->
- ["#(",
- lists:join(<<", ">>,
- lists:map(fun inspect/1, erlang:tuple_to_list(Any))
- ),
- ")"];
+inspect(Tuple) when is_tuple(Tuple) ->
+ Elements = lists:map(fun inspect/1, erlang:tuple_to_list(Tuple)),
+ ["#(", lists:join(", ", Elements), ")"];
inspect(Any) when is_function(Any) ->
{arity, Arity} = erlang:fun_info(Any, arity),
ArgsAsciiCodes = lists:seq($a, $a + Arity - 1),