Skip to content

Commit 332392b

Browse files
committed
Fix Macro.to_string/1 for large negative integers
1 parent f28e15c commit 332392b

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

lib/elixir/lib/code/formatter.ex

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,18 +1621,27 @@ defmodule Code.Formatter do
16211621
byte_size(digits) >= 6 ->
16221622
digits
16231623
|> String.to_charlist()
1624-
|> Enum.reverse()
1625-
|> Enum.chunk_every(3)
1626-
|> Enum.intersperse(~c"_")
1627-
|> List.flatten()
1628-
|> Enum.reverse()
1624+
|> insert_underscores_charlist()
16291625
|> List.to_string()
16301626

16311627
true ->
16321628
digits
16331629
end
16341630
end
16351631

1632+
defp insert_underscores_charlist([?- | rest]) do
1633+
[?- | insert_underscores_charlist(rest)]
1634+
end
1635+
1636+
defp insert_underscores_charlist(digits) do
1637+
digits
1638+
|> Enum.reverse()
1639+
|> Enum.chunk_every(3)
1640+
|> Enum.intersperse(~c"_")
1641+
|> List.flatten()
1642+
|> Enum.reverse()
1643+
end
1644+
16361645
defp escape_heredoc(string, escape) do
16371646
string = String.replace(string, escape, "\\" <> escape)
16381647
heredoc_to_algebra(["" | String.split(string, "\n")])

lib/elixir/test/elixir/macro_test.exs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,16 @@ defmodule MacroTest do
429429
test "converts quoted to string" do
430430
assert Macro.to_string(quote do: hello(world)) == "hello(world)"
431431
end
432+
433+
test "large number literals" do
434+
# with quote
435+
assert Macro.to_string(quote do: 576_460_752_303_423_455) == "576_460_752_303_423_455"
436+
assert Macro.to_string(quote do: -576_460_752_303_423_455) == "-576_460_752_303_423_455"
437+
438+
# without quote
439+
assert Macro.to_string(576_460_752_303_423_455) == "576_460_752_303_423_455"
440+
assert Macro.to_string(-576_460_752_303_423_455) == "-576_460_752_303_423_455"
441+
end
432442
end
433443

434444
describe "to_string/2" do

0 commit comments

Comments
 (0)