Skip to content

Commit

Permalink
fix bug when first field contains "=>", and add tests (tkelman)
Browse files Browse the repository at this point in the history
  • Loading branch information
rfourquet committed Jul 21, 2017
1 parent 0f4af18 commit 1765862
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
19 changes: 12 additions & 7 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -273,11 +273,12 @@ print(io::IO, n::Unsigned) = print(io, dec(n))

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

show_pair_with_arrow(p::Pair) =
typeof(p.first) == typeof(p).parameters[1] &&
typeof(p.second) == typeof(p).parameters[2]

function show(io::IO, p::Pair)
if typeof(p.first) != typeof(p).parameters[1] ||
typeof(p.second) != typeof(p).parameters[2]
return show_default(io, p)
end
show_pair_with_arrow(p) || return show_default(io, p)

isa(p.first,Pair) && print(io, "(")
show(io, p.first)
Expand Down Expand Up @@ -1373,9 +1374,13 @@ function alignment(io::IO, x::Rational)
end

function alignment(io::IO, x::Pair)
m = match(r"^(.*?=)(>.*)$", sprint(0, show, x, env=io))
m === nothing ? (length(sprint(0, show, x, env=io)), 0) :
(length(m.captures[1]), length(m.captures[2]))
s = sprint(0, show, x, env=io)
if show_pair_with_arrow(x)
left = length(sprint(0, show, x.first, env=io)) + 2isa(x.first, Pair) # 2 for parens
(left+1, length(s)-left-1) # +1 for the "=" part of "=>"
else
(0, length(s)) # as for x::Any
end
end

const undef_ref_str = "#undef"
Expand Down
12 changes: 12 additions & 0 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -818,3 +818,15 @@ end
@test sprint(show, Val(:Float64)) == "Val{:Float64}()" # Val of a symbol
@test sprint(show, Val(true)) == "Val{true}()" # Val of a value
end

@testset "alignment for pairs" begin # (#22899)
@test replstr([1=>22,33=>4]) == "2-element Array{Pair{Int64,Int64},1}:\n 1=>22\n 33=>4 "
# first field may have "=>" in its representation
@test replstr(Pair[(1=>2)=>3, 4=>5]) ==
"2-element Array{Pair,1}:\n (1=>2)=>3\n 4=>5"
@test replstr(Any[Dict(1=>2)=> (3=>4), 1=>2]) ==
"2-element Array{Any,1}:\n Dict(1=>2)=>(3=>4)\n 1=>2 "
# left-alignment when not using the "=>" symbol
@test replstr(Pair{Integer,Int}[1=>2, 33=>4]) ==
"2-element Array{Pair{Integer,Int64},1}:\n Pair{Integer,Int64}(1, 2) \n Pair{Integer,Int64}(33, 4)"
end

0 comments on commit 1765862

Please sign in to comment.