Skip to content

Commit

Permalink
simplify implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
ericphanson committed Aug 20, 2024
1 parent 8a48d1c commit 2461a98
Showing 1 changed file with 4 additions and 11 deletions.
15 changes: 4 additions & 11 deletions stdlib/REPL/src/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -516,32 +516,25 @@ end

# Semantically, we only need to override `Base.write`, but we also
# override `unsafe_write` for performance.
function Base.unsafe_write(io::Base.IOContext{<:LimitIO}, p::Ptr{UInt8}, nb::UInt)
limiter = io.io
function Base.unsafe_write(limiter::LimitIO, p::Ptr{UInt8}, nb::UInt)
# already exceeded? throw
limiter.n > limiter.maxbytes && throw(LimitIOException(limiter.maxbytes))
remaining = limiter.maxbytes - limiter.n # >= 0

# Not enough bytes left; we will print up to the limit, then throw
if remaining < nb
if remaining > 0
# note we pass on the `ioproperties` with the inner `limiter.io` object
Base.unsafe_write(IOContext(limiter.io, Base.ioproperties(io)), p, remaining)
Base.unsafe_write(limiter.io, p, remaining)
end
throw(LimitIOException(limiter.maxbytes))
end

# We won't hit the limit so we'll write the full `nb` bytes, again passing along the `ioproperties`.
bytes_written = Base.unsafe_write(IOContext(limiter.io, Base.ioproperties(io)), p, nb)
# We won't hit the limit so we'll write the full `nb` bytes
bytes_written = Base.unsafe_write(limiter.io, p, nb)
limiter.n += bytes_written
return bytes_written::Union{UInt, Int}
end

# wrap to hit optimized method
function Base.unsafe_write(limiter::LimitIO, p::Ptr{UInt8}, nb::UInt)
return unsafe_write(IOContext(limiter), p, nb)
end

struct REPLDisplay{Repl<:AbstractREPL} <: AbstractDisplay
repl::Repl
end
Expand Down

0 comments on commit 2461a98

Please sign in to comment.