Skip to content

Commit

Permalink
Fix Printf for typemin(<:Base.BitSigned) (JuliaLang#42341)
Browse files Browse the repository at this point in the history
  • Loading branch information
petvana committed Sep 27, 2021
1 parent 8987bc2 commit 82d8a36
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
3 changes: 2 additions & 1 deletion stdlib/Printf/src/Printf.jl
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,8 @@ fmt(buf, pos, arg::AbstractFloat, spec::Spec{T}) where {T <: Ints} =
bs = base(T)
arg2 = toint(arg)
n = i = ndigits(arg2, base=bs, pad=1)
x, neg = arg2 < 0 ? (-arg2, true) : (arg2, false)
neg = arg2 < 0
x = arg2 isa Base.BitSigned ? unsigned(abs(arg2)) : abs(arg2)
arglen = n + (neg || (plus | space)) +
(T == Val{'o'} && hash ? 1 : 0) +
(T == Val{'x'} && hash ? 2 : 0) + (T == Val{'X'} && hash ? 2 : 0)
Expand Down
11 changes: 11 additions & 0 deletions stdlib/Printf/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -762,6 +762,17 @@ end
@test Printf.@sprintf("%20.0X", UInt(3989525555)) == " EDCB5433"
@test Printf.@sprintf("%20.X", UInt(0)) == " 0"

# issue #41971
@test Printf.@sprintf("%4d", typemin(Int8)) == "-128"
@test Printf.@sprintf("%4d", typemax(Int8)) == " 127"
@test Printf.@sprintf("%6d", typemin(Int16)) == "-32768"
@test Printf.@sprintf("%6d", typemax(Int16)) == " 32767"
@test Printf.@sprintf("%11d", typemin(Int32)) == "-2147483648"
@test Printf.@sprintf("%11d", typemax(Int32)) == " 2147483647"
@test Printf.@sprintf("%20d", typemin(Int64)) == "-9223372036854775808"
@test Printf.@sprintf("%20d", typemax(Int64)) == " 9223372036854775807"
@test Printf.@sprintf("%40d", typemin(Int128)) == "-170141183460469231731687303715884105728"
@test Printf.@sprintf("%40d", typemax(Int128)) == " 170141183460469231731687303715884105727"
end

@testset "%n" begin
Expand Down

0 comments on commit 82d8a36

Please sign in to comment.