Skip to content

Commit

Permalink
also handle floating point numbers (#26847)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lilith Hafner authored and Lilith Hafner committed Feb 28, 2023
1 parent 1c482e8 commit 6ff4fc8
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 5 deletions.
2 changes: 1 addition & 1 deletion base/ryu/Ryu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ end
function Base.show(io::IO, x::T, forceuntyped::Bool=false, fromprint::Bool=false) where {T <: Base.IEEEFloat}
compact = get(io, :compact, false)::Bool
buf = Base.StringVector(neededdigits(T))
typed = !forceuntyped && !compact && get(io, :typeinfo, Any) != typeof(x)
typed = !forceuntyped && !compact && Base.nonnothing_nonmissing_typeinfo(io) != typeof(x)
pos = writeshortest(buf, 1, x, false, false, true, -1,
(x isa Float32 && !fromprint) ? UInt8('f') : UInt8('e'), false, UInt8('.'), typed, compact)
write(io, resize!(buf, pos - 1))
Expand Down
6 changes: 2 additions & 4 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1140,14 +1140,12 @@ function show(io::IO, tn::Core.TypeName)
print(io, ")")
end

nonnothing_nonmissing_typeinfo(io::IO) = nonmissingtype(nonnothingtype(get(io, :typeinfo, Any)))
show(io::IO, b::Bool) = print(io, nonnothing_nonmissing_typeinfo(io) === Bool ? (b ? "1" : "0") : (b ? "true" : "false"))
show(io::IO, ::Nothing) = print(io, "nothing")
show(io::IO, n::Signed) = (write(io, string(n)); nothing)
show(io::IO, n::Unsigned) = print(io, "0x", string(n, pad = sizeof(n)<<1, base = 16))
print(io::IO, n::Unsigned) = print(io, string(n))
function show(io::IO, b::Bool)
typeinfo = nonmissingtype(nonnothingtype(get(io, :typeinfo, Any)))
print(io, typeinfo === Bool ? (b ? "1" : "0") : (b ? "true" : "false"))
end

show(io::IO, p::Ptr) = print(io, typeof(p), " @0x$(string(UInt(p), base = 16, pad = Sys.WORD_SIZE>>2))")

Expand Down
3 changes: 3 additions & 0 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2123,6 +2123,9 @@ end
@test_repr "Union{Bool, Nothing}[1, 0, nothing]"
end

# issue #26847
@test_repr "Union{Missing, Float32}[1.0]"

# issue #30505
@test repr(Union{Tuple{Char}, Tuple{Char, Char}}[('a','b')]) == "Union{Tuple{Char}, Tuple{Char, Char}}[('a', 'b')]"

Expand Down

0 comments on commit 6ff4fc8

Please sign in to comment.