diff --git a/base/show.jl b/base/show.jl index cb71376fedc68..8b1169d876214 100644 --- a/base/show.jl +++ b/base/show.jl @@ -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 @@ -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) = diff --git a/test/show.jl b/test/show.jl index 3b3de18bde9c0..ef8310f043bf2 100644 --- a/test/show.jl +++ b/test/show.jl @@ -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