From 26c7f2e0968906d4bf77efdea9e6c0389df10bee Mon Sep 17 00:00:00 2001 From: Michael Mark Date: Fri, 24 May 2024 16:51:06 -0700 Subject: Generalized escape handling for all chars < 32 Also some minor improvements like more tests, correcting codes in format \u{xx} to \u{xxxx}, and escaping code 127. Now, all characters with an ASCII code of less than 32 are automatically converted into \u{xxxx} syntax in string.inspect. Now, the only way that string.inspect will show two strings as identical when they're not is if they use copycat unicodes, such as greek question mark or accent modifiers. --- src/gleam_stdlib.erl | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/gleam_stdlib.erl b/src/gleam_stdlib.erl index cab299f..230ca4c 100644 --- a/src/gleam_stdlib.erl +++ b/src/gleam_stdlib.erl @@ -498,9 +498,13 @@ inspect_maybe_utf8_string(Binary, Acc) -> $\n -> <<$\\, $n>>; $\t -> <<$\\, $t>>; $\f -> <<$\\, $f>>; - $\b -> <<$\\, $b>>; - $\v -> <<$\\, $v>>; - $\e -> <<$\\, $e>>; + 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, $}>>; Other -> <> end, inspect_maybe_utf8_string(Rest, <>); -- cgit v1.2.3