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 535de85
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 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
# terminal width
limit = max(20, displaysize(io)[2])
# one line if compact, seven lines otherwise
get(io, :limit, false) || (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
27 changes: 27 additions & 0 deletions test/strings/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -317,3 +317,30 @@ end
# test empty args
@test string() == ""
end

@testset "string show with elision" 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

0 comments on commit 535de85

Please sign in to comment.