Skip to content

Commit

Permalink
delete ByteView (#1149)
Browse files Browse the repository at this point in the history
* delete ByteView

* Update Streams.jl

* Use `isempty` instead equality with an empty buffer
  • Loading branch information
vtjnash authored Feb 17, 2024
1 parent 581c6fc commit 040cf0a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions src/Connections.jl
Original file line number Diff line number Diff line change
Expand Up @@ -234,12 +234,12 @@ Read until `find_delimiter(bytes)` returns non-zero.
Return view of bytes up to the delimiter.
"""
function IOExtras.readuntil(c::Connection, f::F #=Vector{UInt8} -> Int=#,
sizehint=4096)::ByteView where {F <: Function}
sizehint=4096) where {F <: Function}
buf = c.buffer
if bytesavailable(buf) == 0
read_to_buffer(c, sizehint)
end
while (bytes = IOExtras.readuntil(buf, f)) == nobytes
while isempty(begin bytes = IOExtras.readuntil(buf, f) end)
read_to_buffer(c, sizehint)
end
return bytes
Expand Down
7 changes: 3 additions & 4 deletions src/IOExtras.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ using Sockets
using MbedTLS: SSLContext, MbedException
using OpenSSL: SSLStream

export bytes, isbytes, nbytes, ByteView, nobytes,
export bytes, isbytes, nbytes, nobytes,
startwrite, closewrite, startread, closeread, readuntil,
tcpsocket, localport, safe_getpeername

Expand Down Expand Up @@ -104,7 +104,6 @@ function safe_getpeername(io)
end


const ByteView = typeof(view(UInt8[], 1:0))
const nobytes = view(UInt8[], 1:0)

readuntil(args...) = Base.readuntil(args...)
Expand All @@ -114,8 +113,8 @@ Read from an `IO` stream until `find_delimiter(bytes)` returns non-zero.
Return view of bytes up to the delimiter.
"""
function readuntil(buf::IOBuffer,
find_delimiter::F #= Vector{UInt8} -> Int =#
)::ByteView where {F <: Function}
find_delimiter::F #= Vector{UInt8} -> Int =#
) where {F <: Function}
l = find_delimiter(view(buf.data, buf.ptr:buf.size))
if l == 0
return nobytes
Expand Down
5 changes: 3 additions & 2 deletions src/Streams.jl
Original file line number Diff line number Diff line change
Expand Up @@ -320,14 +320,15 @@ function readall!(http::Stream, buf::Base.GenericIOBuffer=PipeBuffer())
return n
end

function IOExtras.readuntil(http::Stream, f::Function)::ByteView
function IOExtras.readuntil(http::Stream, f::Function)
UInt(ntoread(http)) == 0 && return Connections.nobytes
try
bytes = IOExtras.readuntil(http.stream, f)
update_ntoread(http, length(bytes))
return bytes
catch
catch ex
# if we error, it means we didn't find what we were looking for
# TODO: this seems very sketchy
return UInt8[]
end
end
Expand Down

0 comments on commit 040cf0a

Please sign in to comment.