diff options
author | Louis Pilfold <louis@lpil.uk> | 2022-06-15 21:58:48 +0100 |
---|---|---|
committer | Louis Pilfold <louis@lpil.uk> | 2022-06-15 21:58:50 +0100 |
commit | efdf9ba616850cd85d0f621ca4f2e486b36bceec (patch) | |
tree | 88ea38d3ed4b2de94e45e892f219a6ead60db5c8 | |
parent | ba0a15976a908e1d4b5a13fc88b533adf2c35f85 (diff) | |
download | gleam_stdlib-efdf9ba616850cd85d0f621ca4f2e486b36bceec.tar.gz gleam_stdlib-efdf9ba616850cd85d0f621ca4f2e486b36bceec.zip |
Inspect bit strings
-rw-r--r-- | src/gleam_stdlib.erl | 39 | ||||
-rw-r--r-- | test/gleam/string_test.gleam | 3 |
2 files changed, 22 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), diff --git a/test/gleam/string_test.gleam b/test/gleam/string_test.gleam index ca986e8..0fcc0e7 100644 --- a/test/gleam/string_test.gleam +++ b/test/gleam/string_test.gleam @@ -700,6 +700,9 @@ pub fn inspect_test() { string.inspect(InspectTypeOne(InspectTypeZero)) |> should.equal("InspectTypeOne(InspectTypeZero)") + + string.inspect(<<255, 2, 0>>) + |> should.equal("<<255, 2, 0>>") } if javascript { |