Skip to content

Commit

Permalink
display something useful for text/plain output of invalid String (#18296
Browse files Browse the repository at this point in the history
)
  • Loading branch information
stevengj authored Aug 31, 2016
1 parent 738c273 commit 2b2894c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
10 changes: 10 additions & 0 deletions base/replutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,16 @@ end
show(io::IO, ::MIME"text/plain", X::AbstractArray) = showarray(io, X, false)
show(io::IO, ::MIME"text/plain", r::Range) = show(io, r) # always use the compact form for printing ranges

# display something useful even for strings containing arbitrary
# (non-UTF8) binary data:
function show(io::IO, ::MIME"text/plain", s::String)
if isvalid(s)
show(io, s)
else
println(io, sizeof(s), "-byte String of invalid UTF-8 data:")
showarray(io, s.data, false; header=false)
end
end

# showing exception objects as descriptive error messages

Expand Down
3 changes: 3 additions & 0 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -603,3 +603,6 @@ end
@test repr(NTuple{7,Int64}) == "NTuple{7,Int64}"
@test repr(Tuple{Float64, Float64, Float64, Float64}) == "NTuple{4,Float64}"
@test repr(Tuple{Float32, Float32, Float32}) == "Tuple{Float32,Float32,Float32}"

# Test that REPL/mime display of invalid UTF-8 data doesn't throw an exception:
@test isa(stringmime("text/plain", String(UInt8[0x00:0xff;])), String)

0 comments on commit 2b2894c

Please sign in to comment.