Skip to content

Commit

Permalink
Rename nb_available to bytesavailable
Browse files Browse the repository at this point in the history
  • Loading branch information
ararslan committed Jan 19, 2018
1 parent 625923f commit 38ebdbb
Show file tree
Hide file tree
Showing 18 changed files with 72 additions and 67 deletions.
3 changes: 3 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 `bytesavailable` ([#25634]).

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

Expand Down Expand Up @@ -1222,3 +1224,4 @@ Command-line option changes
[#25532]: https://github.com/JuliaLang/julia/issues/25532
[#25545]: https://github.com/JuliaLang/julia/issues/25545
[#25616]: https://github.com/JuliaLang/julia/issues/25616
[#25634]: https://github.com/JuliaLang/julia/issues/25634
2 changes: 2 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1583,6 +1583,8 @@ end
@deprecate gc_enable GC.enable
@eval @deprecate $(Symbol("@gc_preserve")) GC.$(Symbol("@preserve")) false

@deprecate nb_available bytesavailable

# 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 @@ -855,7 +855,7 @@ export
listenany,
ltoh,
mark,
nb_available,
bytesavailable,
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,
bytesavailable, 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
bytesavailable(f::File) = max(0, filesize(f) - position(f)) # position can be > filesize

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

function readbytes!(f::File, b::Array{UInt8}, nb=length(b))
nr = min(nb, nb_available(f))
nr = min(nb, bytesavailable(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(bytesavailable(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, bytesavailable(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 bytesavailable 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)
bytesavailable(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> bytesavailable(io)
34
```
"""
nb_available(io::AbstractPipe) = nb_available(pipe_reader(io))
bytesavailable(io::AbstractPipe) = bytesavailable(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 = bytesavailable(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 : bytesavailable(io))
bytesavailable(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 = bytesavailable(io) + (io.ptr-io.mark)
else
ptr = io.ptr
bytes_to_move = nb_available(io)
bytes_to_move = bytesavailable(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 || bytesavailable(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 = bytesavailable(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 = bytesavailable(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, bytesavailable(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, bytesavailable(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(bytesavailable(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, bytesavailable(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,bytesavailable(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, bytesavailable(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, bytesavailable(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)
bytesavailable(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, bytesavailable(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 = (bytesavailable(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) || bytesavailable(io) > 0
iswritable(io::TCPSocket) = isopen(io) && io.status != StatusClosing

## VARIOUS METHODS TO BE MOVED TO BETTER LOCATION
Expand Down
Loading

0 comments on commit 38ebdbb

Please sign in to comment.