Skip to content
Open
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
17 changes: 16 additions & 1 deletion base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,11 @@ The following properties are in common use:
- `:color`: Boolean specifying whether ANSI color/escape codes are supported/expected.
By default, this is determined by whether `io` is a compatible terminal and by any
`--color` command-line flag when `julia` was launched.
- `:hexunsigned`: Boolean specifying whether to print unsigned integers in
hexadecimal. Defaults to `true`, otherwise they will be printed in decimal.

!!! compat "Julia 1.14"
The `:hexunsigned` option requires Julia 1.14 or later.

# Examples

Expand Down Expand Up @@ -1293,7 +1298,17 @@ nonnothing_nonmissing_typeinfo(io::IO) = nonmissingtype(nonnothingtype(get(io, :
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))
function show(io::IO, n::Unsigned)
if get(io, :hexunsigned, true)::Bool
print(io, "0x", string(n, pad = sizeof(n)<<1, base = 16))
else
if get(io, :typeinfo, Nothing)::Type == typeof(n)
print(io, n)
else
print(io, typeof(n), "($(n))")
end
end
end
print(io::IO, n::Unsigned) = print(io, string(n))

has_tight_type(p::Pair) =
Expand Down
6 changes: 6 additions & 0 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1363,6 +1363,12 @@ end
let repr = sprint(dump, Ptr{UInt8}(UInt(1)))
@test repr == "Ptr{UInt8}($(Base.repr(UInt(1))))\n"
end
let repr = sprint(show, UInt(42); context=(:hexunsigned => false))
@test repr == "$(UInt)(42)"
end
let repr = sprint(show, UInt16[1, 2]; context=(:hexunsigned => false))
@test repr == "UInt16[1, 2]"
end
let repr = sprint(dump, Core.svec())
@test repr == "empty SimpleVector\n"
end
Expand Down