Skip to content

Commit adb1fa1

Browse files
fhunlethJosé Valim
authored andcommitted
Fix inspect for non-decimal negative integers (#7181)
Before: iex> inspect(-1, base: :hex) "0x-1" After: iex> inspect(-1, base: :hex) "-0x1" This change also applies to the octal and binary base options.
1 parent bd275ea commit adb1fa1

File tree

2 files changed

+7
-0
lines changed

2 files changed

+7
-0
lines changed

lib/elixir/lib/inspect.ex

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,10 @@ defimpl Inspect, for: Integer do
262262

263263
defp prepend_prefix(value, :decimal), do: value
264264

265+
defp prepend_prefix(<<?-, value::binary>>, base) do
266+
"-" <> prepend_prefix(value, base)
267+
end
268+
265269
defp prepend_prefix(value, base) do
266270
prefix =
267271
case base do

lib/elixir/test/elixir/inspect_test.exs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,14 +194,17 @@ defmodule Inspect.NumberTest do
194194

195195
test "hex" do
196196
assert inspect(100, base: :hex) == "0x64"
197+
assert inspect(-100, base: :hex) == "-0x64"
197198
end
198199

199200
test "octal" do
200201
assert inspect(100, base: :octal) == "0o144"
202+
assert inspect(-100, base: :octal) == "-0o144"
201203
end
202204

203205
test "binary" do
204206
assert inspect(86, base: :binary) == "0b1010110"
207+
assert inspect(-86, base: :binary) == "-0b1010110"
205208
end
206209

207210
test "float" do

0 commit comments

Comments
 (0)