aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMichael Mark <michael.mark@oit.edu>2024-05-24 18:42:25 -0700
committerLouis Pilfold <louis@lpil.uk>2024-05-29 12:26:22 +0100
commita5581ccf55dbbfcf71260365aa773511b170a4d0 (patch)
tree0aca0784ab0b5e5a16b227b8906967488504eb0d
parentb152317a0bacbc7e17d22ffa30d3d1dc22c813df (diff)
downloadgleam_stdlib-a5581ccf55dbbfcf71260365aa773511b170a4d0.tar.gz
gleam_stdlib-a5581ccf55dbbfcf71260365aa773511b170a4d0.zip
Control characters 128-159 are now escaped
Also added tests for this new case
-rw-r--r--src/gleam_stdlib.erl16
-rw-r--r--test/gleam/string_test.gleam6
2 files changed, 15 insertions, 7 deletions
diff --git a/src/gleam_stdlib.erl b/src/gleam_stdlib.erl
index 230ca4c..c1b6a1c 100644
--- a/src/gleam_stdlib.erl
+++ b/src/gleam_stdlib.erl
@@ -498,19 +498,21 @@ inspect_maybe_utf8_string(Binary, Acc) ->
$\n -> <<$\\, $n>>;
$\t -> <<$\\, $t>>;
$\f -> <<$\\, $f>>;
- 127 -> <<$\\, $u, ${, $0, $0, $7, $F, $}>>;
- X when X < 32 ->
- Hex = integer_to_list(X, 16),
- Leading = lists:duplicate(4 - length(Hex), "0"),
- Formatted = lists:append(Leading, Hex),
- Bin = list_to_binary(Formatted),
- <<$\\, $u, ${, Bin/binary, $}>>;
+ X when X > 126, X < 160 -> convert_to_u(X);
+ X when X < 32 -> convert_to_u(X);
Other -> <<Other/utf8>>
end,
inspect_maybe_utf8_string(Rest, <<Acc/binary, Escaped/binary>>);
_ -> {error, not_a_utf8_string}
end.
+convert_to_u(Code) ->
+ Hex = integer_to_list(Code, 16),
+ Leading = lists:duplicate(4 - length(Hex), "0"),
+ Formatted = lists:append(Leading, Hex),
+ Bin = list_to_binary(Formatted),
+ <<$\\, $u, ${, Bin/binary, $}>>.
+
float_to_string(Float) when is_float(Float) ->
erlang:iolist_to_binary(io_lib_format:fwrite_g(Float)).
diff --git a/test/gleam/string_test.gleam b/test/gleam/string_test.gleam
index 9a1a315..f3c56a8 100644
--- a/test/gleam/string_test.gleam
+++ b/test/gleam/string_test.gleam
@@ -709,6 +709,12 @@ pub fn inspect_test() {
string.inspect("\u{007F}")
|> should.equal("\"\\u{007F}\"")
+ string.inspect("\u{009F}")
+ |> should.equal("\"\\u{009F}\"")
+
+ string.inspect("\u{00A0}")
+ |> should.equal("\"\u{00A0}\"")
+
string.inspect("\r\r")
|> should.equal("\"\\r\\r\"")