Skip to content

Commit

Permalink
Rename nb_available to bytesleft
Browse files Browse the repository at this point in the history
  • Loading branch information
ararslan committed Jan 19, 2018
1 parent 0379a38 commit 3a4932b
Show file tree
Hide file tree
Showing 18 changed files with 71 additions and 67 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -690,6 +690,8 @@ Deprecated or removed

* `read(::IO, ::Ref)` is now a method of `read!`, since it mutates its `Ref` argument ([#21592]).

* `nb_available` is now `bytesleft` ([#TODO]).

* `Bidiagonal` constructors now use a `Symbol` (`:U` or `:L`) for the upper/lower
argument, instead of a `Bool` or a `Char` ([#22703]).

Expand Down
2 changes: 2 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1573,6 +1573,8 @@ end

@deprecate object_id objectid

@deprecate nb_available bytesleft

# issue #9053
if Sys.iswindows()
function Filesystem.tempname(uunique::UInt32)
Expand Down
2 changes: 1 addition & 1 deletion base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -856,7 +856,7 @@ export
listenany,
ltoh,
mark,
nb_available,
bytesleft,
ntoh,
open,
pipeline,
Expand Down
12 changes: 6 additions & 6 deletions base/filesystem.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export File,

import Base:
UVError, _sizeof_uv_fs, check_open, close, eof, eventloop, fd, isopen,
nb_available, position, read, read!, readavailable, seek, seekend, show,
bytesleft, position, read, read!, readavailable, seek, seekend, show,
skip, stat, unsafe_read, unsafe_write, transcode, uv_error, uvhandle,
uvtype, write
using Base: coalesce
Expand Down Expand Up @@ -179,12 +179,12 @@ function unsafe_read(f::File, p::Ptr{UInt8}, nel::UInt)
nothing
end

nb_available(f::File) = max(0, filesize(f) - position(f)) # position can be > filesize
bytesleft(f::File) = max(0, filesize(f) - position(f)) # position can be > filesize

eof(f::File) = nb_available(f) == 0
eof(f::File) = bytesleft(f) == 0

function readbytes!(f::File, b::Array{UInt8}, nb=length(b))
nr = min(nb, nb_available(f))
nr = min(nb, bytesleft(f))
if length(b) < nr
resize!(b, nr)
end
Expand All @@ -193,9 +193,9 @@ function readbytes!(f::File, b::Array{UInt8}, nb=length(b))
uv_error("read",ret)
return ret
end
read(io::File) = read!(io, Base.StringVector(nb_available(io)))
read(io::File) = read!(io, Base.StringVector(bytesleft(io)))
readavailable(io::File) = read(io)
read(io::File, nb::Integer) = read!(io, Base.StringVector(min(nb, nb_available(io))))
read(io::File, nb::Integer) = read!(io, Base.StringVector(min(nb, bytesleft(io))))

const SEEK_SET = Int32(0)
const SEEK_CUR = Int32(1)
Expand Down
8 changes: 4 additions & 4 deletions base/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ function wait_connected end
function wait_readnb end
function wait_readbyte end
function wait_close end
function nb_available end
function bytesleft end

"""
readavailable(stream)
Expand Down Expand Up @@ -243,19 +243,19 @@ wait_readbyte(io::AbstractPipe, byte::UInt8) = wait_readbyte(pipe_reader(io), by
wait_close(io::AbstractPipe) = (wait_close(pipe_writer(io)); wait_close(pipe_reader(io)))

"""
nb_available(io)
bytesleft(io)
Return the number of bytes available for reading before a read from this stream or buffer will block.
# Examples
```jldoctest
julia> io = IOBuffer("JuliaLang is a GitHub organization");
julia> nb_available(io)
julia> bytesleft(io)
34
```
"""
nb_available(io::AbstractPipe) = nb_available(pipe_reader(io))
bytesleft(io::AbstractPipe) = bytesleft(pipe_reader(io))

"""
eof(stream) -> Bool
Expand Down
30 changes: 15 additions & 15 deletions base/iobuffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ show(io::IO, b::GenericIOBuffer) = print(io, "IOBuffer(data=UInt8[...], ",

function unsafe_read(from::GenericIOBuffer, p::Ptr{UInt8}, nb::UInt)
from.readable || throw(ArgumentError("read failed, IOBuffer is not readable"))
avail = nb_available(from)
avail = bytesleft(from)
adv = min(avail, nb)
@gc_preserve from unsafe_copyto!(p, pointer(from.data, from.ptr), adv)
from.ptr += adv
Expand Down Expand Up @@ -200,8 +200,8 @@ iswritable(io::GenericIOBuffer) = io.writable

# TODO: GenericIOBuffer is not iterable, so doesn't really have a length.
# This should maybe be sizeof() instead.
#length(io::GenericIOBuffer) = (io.seekable ? io.size : nb_available(io))
nb_available(io::GenericIOBuffer) = io.size - io.ptr + 1
#length(io::GenericIOBuffer) = (io.seekable ? io.size : bytesleft(io))
bytesleft(io::GenericIOBuffer) = io.size - io.ptr + 1
position(io::GenericIOBuffer) = io.ptr-1

function skip(io::GenericIOBuffer, n::Integer)
Expand Down Expand Up @@ -251,10 +251,10 @@ function compact(io::GenericIOBuffer)
if ismarked(io) && io.mark < io.ptr
if io.mark == 0 return end
ptr = io.mark
bytes_to_move = nb_available(io) + (io.ptr-io.mark)
bytes_to_move = bytesleft(io) + (io.ptr-io.mark)
else
ptr = io.ptr
bytes_to_move = nb_available(io)
bytes_to_move = bytesleft(io)
end
copyto!(io.data, 1, io.data, ptr, bytes_to_move)
io.size -= ptr - 1
Expand Down Expand Up @@ -305,7 +305,7 @@ eof(io::GenericIOBuffer) = (io.ptr-1 == io.size)
nothing
end

isopen(io::GenericIOBuffer) = io.readable || io.writable || io.seekable || nb_available(io) > 0
isopen(io::GenericIOBuffer) = io.readable || io.writable || io.seekable || bytesleft(io) > 0

"""
take!(b::IOBuffer)
Expand All @@ -330,7 +330,7 @@ function take!(io::GenericIOBuffer)
nbytes = io.size
data = copyto!(StringVector(nbytes), 1, io.data, 1, nbytes)
else
nbytes = nb_available(io)
nbytes = bytesleft(io)
data = read!(io,StringVector(nbytes))
end
if io.writable
Expand All @@ -351,7 +351,7 @@ function take!(io::IOBuffer)
end
resize!(data,io.size)
else
nbytes = nb_available(io)
nbytes = bytesleft(io)
a = StringVector(nbytes)
data = read!(io, a)
end
Expand All @@ -367,7 +367,7 @@ function write(to::GenericIOBuffer, from::GenericIOBuffer)
from.ptr = from.size + 1
return 0
end
written::Int = write_sub(to, from.data, from.ptr, nb_available(from))
written::Int = write_sub(to, from.data, from.ptr, bytesleft(from))
from.ptr += written
return written
end
Expand Down Expand Up @@ -415,20 +415,20 @@ end

readbytes!(io::GenericIOBuffer, b::Array{UInt8}, nb=length(b)) = readbytes!(io, b, Int(nb))
function readbytes!(io::GenericIOBuffer, b::Array{UInt8}, nb::Int)
nr = min(nb, nb_available(io))
nr = min(nb, bytesleft(io))
if length(b) < nr
resize!(b, nr)
end
read_sub(io, b, 1, nr)
return nr
end
read(io::GenericIOBuffer) = read!(io,StringVector(nb_available(io)))
read(io::GenericIOBuffer) = read!(io,StringVector(bytesleft(io)))
readavailable(io::GenericIOBuffer) = read(io)
read(io::GenericIOBuffer, nb::Integer) = read!(io,StringVector(min(nb, nb_available(io))))
read(io::GenericIOBuffer, nb::Integer) = read!(io,StringVector(min(nb, bytesleft(io))))

function findfirst(delim::EqualTo{UInt8}, buf::IOBuffer)
p = pointer(buf.data, buf.ptr)
q = @gc_preserve buf ccall(:memchr,Ptr{UInt8},(Ptr{UInt8},Int32,Csize_t),p,delim.x,nb_available(buf))
q = @gc_preserve buf ccall(:memchr,Ptr{UInt8},(Ptr{UInt8},Int32,Csize_t),p,delim.x,bytesleft(buf))
q == C_NULL && return nothing
return Int(q-p+1)
end
Expand Down Expand Up @@ -472,10 +472,10 @@ end
function _crc32c(io::IOBuffer, nb::Integer, crc::UInt32=0x00000000)
nb < 0 && throw(ArgumentError("number of bytes to checksum must be ≥ 0"))
io.readable || throw(ArgumentError("read failed, IOBuffer is not readable"))
n = min(nb, nb_available(io))
n = min(nb, bytesleft(io))
n == 0 && return crc
crc = @gc_preserve io unsafe_crc32c(pointer(io.data, io.ptr), n, crc)
io.ptr += n
return crc
end
_crc32c(io::IOBuffer, crc::UInt32=0x00000000) = _crc32c(io, nb_available(io), crc)
_crc32c(io::IOBuffer, crc::UInt32=0x00000000) = _crc32c(io, bytesleft(io), crc)
4 changes: 2 additions & 2 deletions base/iostream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,9 @@ function unsafe_write(s::IOStream, p::Ptr{UInt8}, nb::UInt)
end

# num bytes available without blocking
nb_available(s::IOStream) = ccall(:jl_nb_available, Int32, (Ptr{Cvoid},), s.ios)
bytesleft(s::IOStream) = ccall(:jl_nb_available, Int32, (Ptr{Cvoid},), s.ios)

readavailable(s::IOStream) = read!(s, Vector{UInt8}(uninitialized, nb_available(s)))
readavailable(s::IOStream) = read!(s, Vector{UInt8}(uninitialized, bytesleft(s)))

function read(s::IOStream, ::Type{UInt8})
b = ccall(:ios_getc, Cint, (Ptr{Cvoid},), s.ios)
Expand Down
2 changes: 1 addition & 1 deletion base/repl/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@ function setup_interface(
sbuffer = LineEdit.buffer(s)
curspos = position(sbuffer)
seek(sbuffer, 0)
shouldeval = (nb_available(sbuffer) == curspos && findfirst(equalto(UInt8('\n')), sbuffer) === nothing)
shouldeval = (bytesleft(sbuffer) == curspos && findfirst(equalto(UInt8('\n')), sbuffer) === nothing)
seek(sbuffer, curspos)
if curspos == 0
# if pasting at the beginning, strip leading whitespace
Expand Down
2 changes: 1 addition & 1 deletion base/socket.jl
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ function TCPServer(; delay=true)
return tcp
end

isreadable(io::TCPSocket) = isopen(io) || nb_available(io) > 0
isreadable(io::TCPSocket) = isopen(io) || bytesleft(io) > 0
iswritable(io::TCPSocket) = isopen(io) && io.status != StatusClosing

## VARIOUS METHODS TO BE MOVED TO BETTER LOCATION
Expand Down
44 changes: 22 additions & 22 deletions base/stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,16 @@ function stream_wait(x, c...) # for x::LibuvObject
end
end

nb_available(s::LibuvStream) = nb_available(s.buffer)
bytesleft(s::LibuvStream) = bytesleft(s.buffer)

function eof(s::LibuvStream)
if isopen(s) # fast path
nb_available(s) > 0 && return false
bytesleft(s) > 0 && return false
else
return nb_available(s) <= 0
return bytesleft(s) <= 0
end
wait_readnb(s,1)
return !isopen(s) && nb_available(s) <= 0
return !isopen(s) && bytesleft(s) <= 0
end

# Limit our default maximum read and buffer size,
Expand Down Expand Up @@ -205,12 +205,12 @@ show(io::IO, stream::LibuvServer) = print(io, typeof(stream), "(",
show(io::IO, stream::LibuvStream) = print(io, typeof(stream), "(",
_fd(stream), " ",
uv_status_string(stream), ", ",
nb_available(stream.buffer)," bytes waiting)")
bytesleft(stream.buffer)," bytes waiting)")

# Shared LibuvStream object interface

function isreadable(io::LibuvStream)
nb_available(io) > 0 && return true
bytesleft(io) > 0 && return true
isopen(io) || return false
return ccall(:uv_is_readable, Cint, (Ptr{Cvoid},), io.handle) != 0
end
Expand Down Expand Up @@ -292,14 +292,14 @@ end

function wait_readnb(x::LibuvStream, nb::Int)
if isopen(x) # fast path
nb_available(x.buffer) >= nb && return
bytesleft(x.buffer) >= nb && return
else
return
end
oldthrottle = x.throttle
preserve_handle(x)
try
while isopen(x) && nb_available(x.buffer) < nb
while isopen(x) && bytesleft(x.buffer) < nb
x.throttle = max(nb, x.throttle)
start_reading(x) # ensure we are reading
wait(x.readnotify)
Expand Down Expand Up @@ -536,8 +536,8 @@ function uv_readcb(handle::Ptr{Cvoid}, nread::Cssize_t, buf::Ptr{Cvoid})
# 3) we have an alternate buffer that has reached its limit.
if stream.status == StatusPaused ||
(stream.status == StatusActive &&
((nb_available(stream.buffer) >= stream.throttle) ||
(nb_available(stream.buffer) >= stream.buffer.maxsize)))
((bytesleft(stream.buffer) >= stream.throttle) ||
(bytesleft(stream.buffer) >= stream.buffer.maxsize)))
# save cycles by stopping kernel notifications from arriving
ccall(:uv_read_stop, Cint, (Ptr{Cvoid},), stream)
stream.status = StatusOpen
Expand Down Expand Up @@ -606,7 +606,7 @@ show(io::IO, stream::Pipe) = print(io,
uv_status_string(stream.in), " => ",
_fd(stream.out), " ",
uv_status_string(stream.out), ", ",
nb_available(stream), " bytes waiting)")
bytesleft(stream), " bytes waiting)")


## Functions for PipeEndpoint and PipeServer ##
Expand Down Expand Up @@ -763,7 +763,7 @@ function readbytes!(s::LibuvStream, a::Vector{UInt8}, nb::Int)
@assert sbuf.seekable == false
@assert sbuf.maxsize >= nb

if nb_available(sbuf) >= nb
if bytesleft(sbuf) >= nb
return readbytes!(sbuf, a, nb)
end

Expand All @@ -779,7 +779,7 @@ function readbytes!(s::LibuvStream, a::Vector{UInt8}, nb::Int)
write(newbuf, sbuf)
wait_readnb(s, Int(nb))
compact(newbuf)
return nb_available(newbuf)
return bytesleft(newbuf)
finally
s.buffer = sbuf
if !isempty(s.readnotify.waitq)
Expand All @@ -800,7 +800,7 @@ function unsafe_read(s::LibuvStream, p::Ptr{UInt8}, nb::UInt)
@assert sbuf.seekable == false
@assert sbuf.maxsize >= nb

if nb_available(sbuf) >= nb
if bytesleft(sbuf) >= nb
return unsafe_read(sbuf, p, nb)
end

Expand All @@ -815,7 +815,7 @@ function unsafe_read(s::LibuvStream, p::Ptr{UInt8}, nb::UInt)
s.buffer = newbuf
write(newbuf, sbuf)
wait_readnb(s, Int(nb))
nb == nb_available(newbuf) || throw(EOFError())
nb == bytesleft(newbuf) || throw(EOFError())
finally
s.buffer = sbuf
if !isempty(s.readnotify.waitq)
Expand Down Expand Up @@ -910,7 +910,7 @@ function unsafe_write(s::LibuvStream, p::Ptr{UInt8}, n::UInt)
end

buf = s.sendbuf
totb = nb_available(buf) + n
totb = bytesleft(buf) + n
if totb < buf.maxsize
nb = unsafe_write(buf, p, n)
else
Expand All @@ -927,7 +927,7 @@ end
function flush(s::LibuvStream)
if s.sendbuf !== nothing
buf = s.sendbuf
if nb_available(buf) > 0
if bytesleft(buf) > 0
arr = take!(buf) # Array of UInt8s
uv_write(s, arr)
return
Expand All @@ -944,7 +944,7 @@ buffer_writes(s::LibuvStream, bufsize) = (s.sendbuf=PipeBuffer(bufsize); s)
function write(s::LibuvStream, b::UInt8)
if s.sendbuf !== nothing
buf = s.sendbuf
if nb_available(buf) + 1 < buf.maxsize
if bytesleft(buf) + 1 < buf.maxsize
return write(buf, b)
end
end
Expand Down Expand Up @@ -1239,18 +1239,18 @@ uvfinalize(s::BufferStream) = nothing

read(s::BufferStream, ::Type{UInt8}) = (wait_readnb(s, 1); read(s.buffer, UInt8))
unsafe_read(s::BufferStream, a::Ptr{UInt8}, nb::UInt) = (wait_readnb(s, Int(nb)); unsafe_read(s.buffer, a, nb))
nb_available(s::BufferStream) = nb_available(s.buffer)
bytesleft(s::BufferStream) = bytesleft(s.buffer)

isreadable(s::BufferStream) = s.buffer.readable
iswritable(s::BufferStream) = s.buffer.writable

function wait_readnb(s::BufferStream, nb::Int)
while isopen(s) && nb_available(s.buffer) < nb
while isopen(s) && bytesleft(s.buffer) < nb
wait(s.r_c)
end
end

show(io::IO, s::BufferStream) = print(io,"BufferStream() bytes waiting:",nb_available(s.buffer),", isopen:", s.is_open)
show(io::IO, s::BufferStream) = print(io,"BufferStream() bytes waiting:",bytesleft(s.buffer),", isopen:", s.is_open)

function wait_readbyte(s::BufferStream, c::UInt8)
while isopen(s) && findfirst(equalto(c), s.buffer) === nothing
Expand All @@ -1271,7 +1271,7 @@ end

function eof(s::BufferStream)
wait_readnb(s, 1)
return !isopen(s) && nb_available(s)<=0
return !isopen(s) && bytesleft(s)<=0
end

# If buffer_writes is called, it will delay notifying waiters till a flush is called.
Expand Down
Loading

0 comments on commit 3a4932b

Please sign in to comment.