Skip to content

Commit

Permalink
show(::String): elide long strings (close #40724)
Browse files Browse the repository at this point in the history
  • Loading branch information
StefanKarpinski committed May 7, 2021
1 parent fbe28e4 commit 909f4f9
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 4 deletions.
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ New library features
Standard library changes
------------------------

* Long strings are now elided using the syntax `"head" ⋯ 12345 bytes ⋯ "tail"` when displayed in the REPL ([#40736]).
* `count` and `findall` now accept an `AbstractChar` argument to search for a character in a string ([#38675]).
* `range` now supports the `range(start, stop)` and `range(start, stop, length)` methods ([#39228]).
* `range` now supports `start` as an optional keyword argument ([#38041]).
Expand Down
48 changes: 48 additions & 0 deletions base/strings/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,54 @@ print(io::IO, s::AbstractString) = for c in s; print(io, c); end
write(io::IO, s::AbstractString) = (len = 0; for c in s; len += Int(write(io, c))::Int; end; len)
show(io::IO, s::AbstractString) = print_quoted(io, s)

# show elided string if more than `limit` characters
function show(
io :: IO,
mime :: MIME"text/plain",
str :: AbstractString;
limit :: Union{Int, Nothing} = nothing,
)
# compute limit in default case
if limit === nothing
get(io, :limit, false) || return show(io, str)
limit = max(20, displaysize(io)[2])
# one line in collection, seven otherwise
get(io, :typeinfo, nothing) === nothing && (limit *= 7)
end

# early out for short strings
len = ncodeunits(str)
len  limit - 2 && # quote chars
return show(io, str)

# these don't depend on string data
units = codeunit(str) == UInt8 ? "bytes" : "code units"
skip_text(skip) = "$skip $units"
short = length(skip_text("")) + 4 # quote chars
chars = max(limit, short + 1) - short # at least 1 digit

# figure out how many characters to print in elided case
chars -= d = ndigits(len - chars) # first adjustment
chars += d - ndigits(len - chars) # second if needed
chars = max(0, chars)

# find head & tail, avoiding O(length(str)) computation
head = nextind(str, 0, 1 + (chars + 1) ÷ 2)
tail = prevind(str, len + 1, chars ÷ 2)

# threshold: min chars skipped to make elision worthwhile
t = short + ndigits(len - chars) - 1
n = tail - head # skipped code units
if 4t n || t n && t length(str, head, tail-1)
skip = skip_text(n)
show(io, SubString(str, 1:prevind(str, head)))
print(io, skip) # TODO: bold styled
show(io, SubString(str, tail))
else
show(io, str)
end
end

# optimized methods to avoid iterating over chars
write(io::IO, s::Union{String,SubString{String}}) =
GC.@preserve s Int(unsafe_write(io, pointer(s), reinterpret(UInt, sizeof(s))))::Int
Expand Down
54 changes: 50 additions & 4 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -808,13 +808,20 @@ Base.methodloc_callback[] = nothing
# test that no spurious visual lines are added when one element spans multiple lines
v = fill!(Array{Any}(undef, 9), 0)
v[1] = "look I'm wide! --- " ^ 9
@test replstr(v) == "9-element Vector{Any}:\n \"look I'm wide! --- look I'm wide! --- look I'm wide! --- look I'm wide! --- look I'm wide! --- look I'm wide! --- look I'm wide! --- look I'm wide! --- look I'm wide! --- \"\n 0\n 0\n 0\n 0\n 0\n 0\n 0\n 0"
@test replstr([fill(0, 9) v]) == "9×2 Matrix{Any}:\n 0 … \"look I'm wide! --- look I'm wide! --- look I'm wide! --- look I'm wide! --- look I'm wide! --- look I'm wide! --- look I'm wide! --- look I'm wide! --- look I'm wide! --- \"\n 0 0\n 0 0\n 0 0\n 0 0\n 0 … 0\n 0 0\n 0 0\n 0 0"
r = replstr(v)
@test startswith(r, "9-element Vector{Any}:\n \"look I'm wide! ---")
@test endswith(r, "look I'm wide! --- \"\n 0\n 0\n 0\n 0\n 0\n 0\n 0\n 0")

# test vertical/diagonal ellipsis
v = fill!(Array{Any}(undef, 50), 0)
v[1] = "look I'm wide! --- " ^ 9
@test replstr(v) == "50-element Vector{Any}:\n \"look I'm wide! --- look I'm wide! --- look I'm wide! --- look I'm wide! --- look I'm wide! --- look I'm wide! --- look I'm wide! --- look I'm wide! --- look I'm wide! --- \"\n 0\n 0\n 0\n 0\n 0\n 0\n 0\n 0\n 0\n\n 0\n 0\n 0\n 0\n 0\n 0\n 0\n 0\n 0"
@test replstr([fill(0, 50) v]) == "50×2 Matrix{Any}:\n 0 … \"look I'm wide! --- look I'm wide! --- look I'm wide! --- look I'm wide! --- look I'm wide! --- look I'm wide! --- look I'm wide! --- look I'm wide! --- look I'm wide! --- \"\n 0 0\n 0 0\n 0 0\n 0 0\n 0 … 0\n 0 0\n 0 0\n 0 0\n 0 0\n ⋮ ⋱ \n 0 0\n 0 0\n 0 0\n 0 0\n 0 … 0\n 0 0\n 0 0\n 0 0\n 0 0"
r = replstr(v)
@test startswith(r, "50-element Vector{Any}:\n \"look I'm wide! ---")
@test endswith(r, "look I'm wide! --- \"\n 0\n 0\n 0\n 0\n 0\n 0\n 0\n 0\n 0\n\n 0\n 0\n 0\n 0\n 0\n 0\n 0\n 0\n 0")

r = replstr([fill(0, 50) v])
@test startswith(r, "50×2 Matrix{Any}:\n 0 … \"look I'm wide! ---")
@test endswith(r, "look I'm wide! --- \"\n 0 0\n 0 0\n 0 0\n 0 0\n 0 … 0\n 0 0\n 0 0\n 0 0\n 0 0\n ⋮ ⋱ \n 0 0\n 0 0\n 0 0\n 0 0\n 0 … 0\n 0 0\n 0 0\n 0 0\n 0 0")

# issue #34659
@test replstr(Int32[]) == "Int32[]"
Expand All @@ -825,6 +832,45 @@ Base.methodloc_callback[] = nothing
@test replstr([zeros(3,0),zeros(2,0)]) == "2-element Vector{Matrix{Float64}}:\n 3×0 Matrix{Float64}\n 2×0 Matrix{Float64}"
end

# string show with elision
@testset "string show with elision" begin
@testset "elision logic" begin
strs = ["A", "", "∀A", "A∀", "😃"]
for limit = 0:100, len = 0:100, str in strs
str = str^len
str = str[1:nextind(str, 0, len)]
out = sprint() do io
show(io, MIME"text/plain"(), str; limit)
end
lower = length("\"\"$(ncodeunits(str)) bytes ⋯ \"\"")
limit = max(limit, lower)
if length(str) + 2  limit
@test eval(Meta.parse(out)) == str
else
@test limit-!isascii(str) <= length(out) <= limit
re = r"(\"[^\"]*\") ⋯ (\d+) bytes ⋯ (\"[^\"]*\")"
m = match(re, out)
head = eval(Meta.parse(m.captures[1]))
tail = eval(Meta.parse(m.captures[3]))
skip = parse(Int, m.captures[2])
@test startswith(str, head)
@test endswith(str, tail)
@test ncodeunits(str) ==
ncodeunits(head) + skip + ncodeunits(tail)
end
end
end

@testset "default elision limit" begin
r = replstr("x"^1000)
@test length(r) == 7*80
@test r == repr("x"^271) * " ⋯ 459 bytes ⋯ " * repr("x"^270)
r = replstr(["x"^1000])
@test length(r) < 120
@test r == "1-element Vector{String}:\n " * repr("x"^31) * " ⋯ 939 bytes ⋯ " * repr("x"^30)
end
end

# Issue 14121
@test_repr "(A'x)'"

Expand Down

0 comments on commit 909f4f9

Please sign in to comment.