Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion lib/elixir/lib/code/identifier.ex
Original file line number Diff line number Diff line change
Expand Up @@ -150,11 +150,16 @@ defmodule Code.Identifier do
<<acc::binary, char::utf8>>
end

defp escape_char(char, acc) when char < 0x100 do
defp escape_char(char, acc) when char < 0x80 do
<<a::4, b::4>> = <<char::8>>
<<acc::binary, ?\\, ?x, to_hex(a), to_hex(b)>>
end

defp escape_char(char, acc) when char < 0x100 do
<<a::4, b::4>> = <<char::8>>
<<acc::binary, ?\\, ?u, ?0, ?0, to_hex(a), to_hex(b)>>
end

defp escape_char(char, acc) when char < 0x10000 do
<<a::4, b::4, c::4, d::4>> = <<char::16>>
<<acc::binary, ?\\, ?x, ?{, to_hex(a), to_hex(b), to_hex(c), to_hex(d), ?}>>
Expand Down
8 changes: 8 additions & 0 deletions lib/elixir/test/elixir/macro_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -1081,6 +1081,14 @@ defmodule MacroTest do
end
end

test "escapes C1 control characters without changing their encoding" do
for codepoint <- 0x80..0x9F do
string = <<codepoint::utf8>>
source = Macro.to_string(string)
assert {^string, []} = Code.eval_string(source)
end
end

test "converts invalid AST with inspect" do
assert Macro.to_string(1..3) == "1..3"
end
Expand Down
Loading