Skip to content

Commit 3a4932b

Browse files
committed
Rename nb_available to bytesleft
1 parent 0379a38 commit 3a4932b

18 files changed

+71
-67
lines changed

NEWS.md

+2
Original file line numberDiff line numberDiff line change
@@ -690,6 +690,8 @@ Deprecated or removed
690690

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

693+
* `nb_available` is now `bytesleft` ([#TODO]).
694+
693695
* `Bidiagonal` constructors now use a `Symbol` (`:U` or `:L`) for the upper/lower
694696
argument, instead of a `Bool` or a `Char` ([#22703]).
695697

base/deprecated.jl

+2
Original file line numberDiff line numberDiff line change
@@ -1573,6 +1573,8 @@ end
15731573

15741574
@deprecate object_id objectid
15751575

1576+
@deprecate nb_available bytesleft
1577+
15761578
# issue #9053
15771579
if Sys.iswindows()
15781580
function Filesystem.tempname(uunique::UInt32)

base/exports.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,7 @@ export
856856
listenany,
857857
ltoh,
858858
mark,
859-
nb_available,
859+
bytesleft,
860860
ntoh,
861861
open,
862862
pipeline,

base/filesystem.jl

+6-6
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ export File,
4040

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

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

184-
eof(f::File) = nb_available(f) == 0
184+
eof(f::File) = bytesleft(f) == 0
185185

186186
function readbytes!(f::File, b::Array{UInt8}, nb=length(b))
187-
nr = min(nb, nb_available(f))
187+
nr = min(nb, bytesleft(f))
188188
if length(b) < nr
189189
resize!(b, nr)
190190
end
@@ -193,9 +193,9 @@ function readbytes!(f::File, b::Array{UInt8}, nb=length(b))
193193
uv_error("read",ret)
194194
return ret
195195
end
196-
read(io::File) = read!(io, Base.StringVector(nb_available(io)))
196+
read(io::File) = read!(io, Base.StringVector(bytesleft(io)))
197197
readavailable(io::File) = read(io)
198-
read(io::File, nb::Integer) = read!(io, Base.StringVector(min(nb, nb_available(io))))
198+
read(io::File, nb::Integer) = read!(io, Base.StringVector(min(nb, bytesleft(io))))
199199

200200
const SEEK_SET = Int32(0)
201201
const SEEK_CUR = Int32(1)

base/io.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function wait_connected end
6565
function wait_readnb end
6666
function wait_readbyte end
6767
function wait_close end
68-
function nb_available end
68+
function bytesleft end
6969

7070
"""
7171
readavailable(stream)
@@ -243,19 +243,19 @@ wait_readbyte(io::AbstractPipe, byte::UInt8) = wait_readbyte(pipe_reader(io), by
243243
wait_close(io::AbstractPipe) = (wait_close(pipe_writer(io)); wait_close(pipe_reader(io)))
244244

245245
"""
246-
nb_available(io)
246+
bytesleft(io)
247247
248248
Return the number of bytes available for reading before a read from this stream or buffer will block.
249249
250250
# Examples
251251
```jldoctest
252252
julia> io = IOBuffer("JuliaLang is a GitHub organization");
253253
254-
julia> nb_available(io)
254+
julia> bytesleft(io)
255255
34
256256
```
257257
"""
258-
nb_available(io::AbstractPipe) = nb_available(pipe_reader(io))
258+
bytesleft(io::AbstractPipe) = bytesleft(pipe_reader(io))
259259

260260
"""
261261
eof(stream) -> Bool

base/iobuffer.jl

+15-15
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ show(io::IO, b::GenericIOBuffer) = print(io, "IOBuffer(data=UInt8[...], ",
147147

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

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

207207
function skip(io::GenericIOBuffer, n::Integer)
@@ -251,10 +251,10 @@ function compact(io::GenericIOBuffer)
251251
if ismarked(io) && io.mark < io.ptr
252252
if io.mark == 0 return end
253253
ptr = io.mark
254-
bytes_to_move = nb_available(io) + (io.ptr-io.mark)
254+
bytes_to_move = bytesleft(io) + (io.ptr-io.mark)
255255
else
256256
ptr = io.ptr
257-
bytes_to_move = nb_available(io)
257+
bytes_to_move = bytesleft(io)
258258
end
259259
copyto!(io.data, 1, io.data, ptr, bytes_to_move)
260260
io.size -= ptr - 1
@@ -305,7 +305,7 @@ eof(io::GenericIOBuffer) = (io.ptr-1 == io.size)
305305
nothing
306306
end
307307

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

310310
"""
311311
take!(b::IOBuffer)
@@ -330,7 +330,7 @@ function take!(io::GenericIOBuffer)
330330
nbytes = io.size
331331
data = copyto!(StringVector(nbytes), 1, io.data, 1, nbytes)
332332
else
333-
nbytes = nb_available(io)
333+
nbytes = bytesleft(io)
334334
data = read!(io,StringVector(nbytes))
335335
end
336336
if io.writable
@@ -351,7 +351,7 @@ function take!(io::IOBuffer)
351351
end
352352
resize!(data,io.size)
353353
else
354-
nbytes = nb_available(io)
354+
nbytes = bytesleft(io)
355355
a = StringVector(nbytes)
356356
data = read!(io, a)
357357
end
@@ -367,7 +367,7 @@ function write(to::GenericIOBuffer, from::GenericIOBuffer)
367367
from.ptr = from.size + 1
368368
return 0
369369
end
370-
written::Int = write_sub(to, from.data, from.ptr, nb_available(from))
370+
written::Int = write_sub(to, from.data, from.ptr, bytesleft(from))
371371
from.ptr += written
372372
return written
373373
end
@@ -415,20 +415,20 @@ end
415415

416416
readbytes!(io::GenericIOBuffer, b::Array{UInt8}, nb=length(b)) = readbytes!(io, b, Int(nb))
417417
function readbytes!(io::GenericIOBuffer, b::Array{UInt8}, nb::Int)
418-
nr = min(nb, nb_available(io))
418+
nr = min(nb, bytesleft(io))
419419
if length(b) < nr
420420
resize!(b, nr)
421421
end
422422
read_sub(io, b, 1, nr)
423423
return nr
424424
end
425-
read(io::GenericIOBuffer) = read!(io,StringVector(nb_available(io)))
425+
read(io::GenericIOBuffer) = read!(io,StringVector(bytesleft(io)))
426426
readavailable(io::GenericIOBuffer) = read(io)
427-
read(io::GenericIOBuffer, nb::Integer) = read!(io,StringVector(min(nb, nb_available(io))))
427+
read(io::GenericIOBuffer, nb::Integer) = read!(io,StringVector(min(nb, bytesleft(io))))
428428

429429
function findfirst(delim::EqualTo{UInt8}, buf::IOBuffer)
430430
p = pointer(buf.data, buf.ptr)
431-
q = @gc_preserve buf ccall(:memchr,Ptr{UInt8},(Ptr{UInt8},Int32,Csize_t),p,delim.x,nb_available(buf))
431+
q = @gc_preserve buf ccall(:memchr,Ptr{UInt8},(Ptr{UInt8},Int32,Csize_t),p,delim.x,bytesleft(buf))
432432
q == C_NULL && return nothing
433433
return Int(q-p+1)
434434
end
@@ -472,10 +472,10 @@ end
472472
function _crc32c(io::IOBuffer, nb::Integer, crc::UInt32=0x00000000)
473473
nb < 0 && throw(ArgumentError("number of bytes to checksum must be ≥ 0"))
474474
io.readable || throw(ArgumentError("read failed, IOBuffer is not readable"))
475-
n = min(nb, nb_available(io))
475+
n = min(nb, bytesleft(io))
476476
n == 0 && return crc
477477
crc = @gc_preserve io unsafe_crc32c(pointer(io.data, io.ptr), n, crc)
478478
io.ptr += n
479479
return crc
480480
end
481-
_crc32c(io::IOBuffer, crc::UInt32=0x00000000) = _crc32c(io, nb_available(io), crc)
481+
_crc32c(io::IOBuffer, crc::UInt32=0x00000000) = _crc32c(io, bytesleft(io), crc)

base/iostream.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,9 @@ function unsafe_write(s::IOStream, p::Ptr{UInt8}, nb::UInt)
326326
end
327327

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

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

333333
function read(s::IOStream, ::Type{UInt8})
334334
b = ccall(:ios_getc, Cint, (Ptr{Cvoid},), s.ios)

base/repl/REPL.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -888,7 +888,7 @@ function setup_interface(
888888
sbuffer = LineEdit.buffer(s)
889889
curspos = position(sbuffer)
890890
seek(sbuffer, 0)
891-
shouldeval = (nb_available(sbuffer) == curspos && findfirst(equalto(UInt8('\n')), sbuffer) === nothing)
891+
shouldeval = (bytesleft(sbuffer) == curspos && findfirst(equalto(UInt8('\n')), sbuffer) === nothing)
892892
seek(sbuffer, curspos)
893893
if curspos == 0
894894
# if pasting at the beginning, strip leading whitespace

base/socket.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ function TCPServer(; delay=true)
326326
return tcp
327327
end
328328

329-
isreadable(io::TCPSocket) = isopen(io) || nb_available(io) > 0
329+
isreadable(io::TCPSocket) = isopen(io) || bytesleft(io) > 0
330330
iswritable(io::TCPSocket) = isopen(io) && io.status != StatusClosing
331331

332332
## VARIOUS METHODS TO BE MOVED TO BETTER LOCATION

base/stream.jl

+22-22
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ function stream_wait(x, c...) # for x::LibuvObject
4343
end
4444
end
4545

46-
nb_available(s::LibuvStream) = nb_available(s.buffer)
46+
bytesleft(s::LibuvStream) = bytesleft(s.buffer)
4747

4848
function eof(s::LibuvStream)
4949
if isopen(s) # fast path
50-
nb_available(s) > 0 && return false
50+
bytesleft(s) > 0 && return false
5151
else
52-
return nb_available(s) <= 0
52+
return bytesleft(s) <= 0
5353
end
5454
wait_readnb(s,1)
55-
return !isopen(s) && nb_available(s) <= 0
55+
return !isopen(s) && bytesleft(s) <= 0
5656
end
5757

5858
# Limit our default maximum read and buffer size,
@@ -205,12 +205,12 @@ show(io::IO, stream::LibuvServer) = print(io, typeof(stream), "(",
205205
show(io::IO, stream::LibuvStream) = print(io, typeof(stream), "(",
206206
_fd(stream), " ",
207207
uv_status_string(stream), ", ",
208-
nb_available(stream.buffer)," bytes waiting)")
208+
bytesleft(stream.buffer)," bytes waiting)")
209209

210210
# Shared LibuvStream object interface
211211

212212
function isreadable(io::LibuvStream)
213-
nb_available(io) > 0 && return true
213+
bytesleft(io) > 0 && return true
214214
isopen(io) || return false
215215
return ccall(:uv_is_readable, Cint, (Ptr{Cvoid},), io.handle) != 0
216216
end
@@ -292,14 +292,14 @@ end
292292

293293
function wait_readnb(x::LibuvStream, nb::Int)
294294
if isopen(x) # fast path
295-
nb_available(x.buffer) >= nb && return
295+
bytesleft(x.buffer) >= nb && return
296296
else
297297
return
298298
end
299299
oldthrottle = x.throttle
300300
preserve_handle(x)
301301
try
302-
while isopen(x) && nb_available(x.buffer) < nb
302+
while isopen(x) && bytesleft(x.buffer) < nb
303303
x.throttle = max(nb, x.throttle)
304304
start_reading(x) # ensure we are reading
305305
wait(x.readnotify)
@@ -536,8 +536,8 @@ function uv_readcb(handle::Ptr{Cvoid}, nread::Cssize_t, buf::Ptr{Cvoid})
536536
# 3) we have an alternate buffer that has reached its limit.
537537
if stream.status == StatusPaused ||
538538
(stream.status == StatusActive &&
539-
((nb_available(stream.buffer) >= stream.throttle) ||
540-
(nb_available(stream.buffer) >= stream.buffer.maxsize)))
539+
((bytesleft(stream.buffer) >= stream.throttle) ||
540+
(bytesleft(stream.buffer) >= stream.buffer.maxsize)))
541541
# save cycles by stopping kernel notifications from arriving
542542
ccall(:uv_read_stop, Cint, (Ptr{Cvoid},), stream)
543543
stream.status = StatusOpen
@@ -606,7 +606,7 @@ show(io::IO, stream::Pipe) = print(io,
606606
uv_status_string(stream.in), " => ",
607607
_fd(stream.out), " ",
608608
uv_status_string(stream.out), ", ",
609-
nb_available(stream), " bytes waiting)")
609+
bytesleft(stream), " bytes waiting)")
610610

611611

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

766-
if nb_available(sbuf) >= nb
766+
if bytesleft(sbuf) >= nb
767767
return readbytes!(sbuf, a, nb)
768768
end
769769

@@ -779,7 +779,7 @@ function readbytes!(s::LibuvStream, a::Vector{UInt8}, nb::Int)
779779
write(newbuf, sbuf)
780780
wait_readnb(s, Int(nb))
781781
compact(newbuf)
782-
return nb_available(newbuf)
782+
return bytesleft(newbuf)
783783
finally
784784
s.buffer = sbuf
785785
if !isempty(s.readnotify.waitq)
@@ -800,7 +800,7 @@ function unsafe_read(s::LibuvStream, p::Ptr{UInt8}, nb::UInt)
800800
@assert sbuf.seekable == false
801801
@assert sbuf.maxsize >= nb
802802

803-
if nb_available(sbuf) >= nb
803+
if bytesleft(sbuf) >= nb
804804
return unsafe_read(sbuf, p, nb)
805805
end
806806

@@ -815,7 +815,7 @@ function unsafe_read(s::LibuvStream, p::Ptr{UInt8}, nb::UInt)
815815
s.buffer = newbuf
816816
write(newbuf, sbuf)
817817
wait_readnb(s, Int(nb))
818-
nb == nb_available(newbuf) || throw(EOFError())
818+
nb == bytesleft(newbuf) || throw(EOFError())
819819
finally
820820
s.buffer = sbuf
821821
if !isempty(s.readnotify.waitq)
@@ -910,7 +910,7 @@ function unsafe_write(s::LibuvStream, p::Ptr{UInt8}, n::UInt)
910910
end
911911

912912
buf = s.sendbuf
913-
totb = nb_available(buf) + n
913+
totb = bytesleft(buf) + n
914914
if totb < buf.maxsize
915915
nb = unsafe_write(buf, p, n)
916916
else
@@ -927,7 +927,7 @@ end
927927
function flush(s::LibuvStream)
928928
if s.sendbuf !== nothing
929929
buf = s.sendbuf
930-
if nb_available(buf) > 0
930+
if bytesleft(buf) > 0
931931
arr = take!(buf) # Array of UInt8s
932932
uv_write(s, arr)
933933
return
@@ -944,7 +944,7 @@ buffer_writes(s::LibuvStream, bufsize) = (s.sendbuf=PipeBuffer(bufsize); s)
944944
function write(s::LibuvStream, b::UInt8)
945945
if s.sendbuf !== nothing
946946
buf = s.sendbuf
947-
if nb_available(buf) + 1 < buf.maxsize
947+
if bytesleft(buf) + 1 < buf.maxsize
948948
return write(buf, b)
949949
end
950950
end
@@ -1239,18 +1239,18 @@ uvfinalize(s::BufferStream) = nothing
12391239

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

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

12471247
function wait_readnb(s::BufferStream, nb::Int)
1248-
while isopen(s) && nb_available(s.buffer) < nb
1248+
while isopen(s) && bytesleft(s.buffer) < nb
12491249
wait(s.r_c)
12501250
end
12511251
end
12521252

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

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

12721272
function eof(s::BufferStream)
12731273
wait_readnb(s, 1)
1274-
return !isopen(s) && nb_available(s)<=0
1274+
return !isopen(s) && bytesleft(s)<=0
12751275
end
12761276

12771277
# If buffer_writes is called, it will delay notifying waiters till a flush is called.

0 commit comments

Comments
 (0)