From a28bda36efe705caac9a14ee35a5d8cf2b9d1361 Mon Sep 17 00:00:00 2001 From: JamesWrigley Date: Thu, 27 Nov 2025 22:22:00 +0100 Subject: [PATCH 1/2] Add support for printing unsigned integers as decimals --- base/show.jl | 13 ++++++++++++- test/show.jl | 3 +++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/base/show.jl b/base/show.jl index cb71376fedc68..b2b418fc24449 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,13 @@ 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 + print(io, string(n), "u") + 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..72a6a39bb52f6 100644 --- a/test/show.jl +++ b/test/show.jl @@ -1363,6 +1363,9 @@ 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 == "42u" + end let repr = sprint(dump, Core.svec()) @test repr == "empty SimpleVector\n" end From f498e86887232aed87468e14305fa960334ae518 Mon Sep 17 00:00:00 2001 From: JamesWrigley Date: Fri, 28 Nov 2025 19:33:22 +0100 Subject: [PATCH 2/2] fixup! Add support for printing unsigned integers as decimals --- base/show.jl | 6 +++++- test/show.jl | 5 ++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/base/show.jl b/base/show.jl index b2b418fc24449..8b1169d876214 100644 --- a/base/show.jl +++ b/base/show.jl @@ -1302,7 +1302,11 @@ function show(io::IO, n::Unsigned) if get(io, :hexunsigned, true)::Bool print(io, "0x", string(n, pad = sizeof(n)<<1, base = 16)) else - print(io, string(n), "u") + 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)) diff --git a/test/show.jl b/test/show.jl index 72a6a39bb52f6..ef8310f043bf2 100644 --- a/test/show.jl +++ b/test/show.jl @@ -1364,7 +1364,10 @@ end @test repr == "Ptr{UInt8}($(Base.repr(UInt(1))))\n" end let repr = sprint(show, UInt(42); context=(:hexunsigned => false)) - @test repr == "42u" + @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"