Skip to content

Commit

Permalink
Deprecate findfirst on I/O buffers (JuliaLang#26600)
Browse files Browse the repository at this point in the history
These two methods are exceptions in the API as we generally do not support find* for non-indexable collections.
occursin and readuntil are replacements for most typical cases (but not all).
  • Loading branch information
nalimilan authored and JeffBezanson committed Mar 26, 2018
1 parent 89c4e3d commit 44500ce
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 17 deletions.
6 changes: 5 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -1041,7 +1041,10 @@ Deprecated or removed

* `search` and `rsearch` have been deprecated in favor of `findfirst`/`findnext` and
`findlast`/`findprev` respectively, in combination with curried `isequal` and `in`
predicates for some methods ([#24673]
predicates for some methods ([#24673]).

* `search(buf::IOBuffer, delim::UInt8)` has been deprecated in favor of either `occursin(delim, buf)`
(to test containment) or `readuntil(buf, delim)` (to read data up to `delim`) ([#26600]).

* `ismatch(regex, str)` has been deprecated in favor of `contains(str, regex)` ([#24673]).

Expand Down Expand Up @@ -1433,3 +1436,4 @@ Command-line option changes
[#26286]: https://github.com/JuliaLang/julia/issues/26286
[#26436]: https://github.com/JuliaLang/julia/issues/26436
[#26442]: https://github.com/JuliaLang/julia/issues/26442
[#26600]: https://github.com/JuliaLang/julia/issues/26600
11 changes: 8 additions & 3 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1227,9 +1227,6 @@ end
@deprecate search(s::AbstractString, t::AbstractString, i::Integer) coalesce(findnext(t, s, i), 0:-1)
@deprecate search(s::AbstractString, t::AbstractString) coalesce(findfirst(t, s), 0:-1)

@deprecate search(buf::IOBuffer, delim::UInt8) coalesce(findfirst(isequal(delim), buf), 0)
@deprecate search(buf::Base.GenericIOBuffer, delim::UInt8) coalesce(findfirst(isequal(delim), buf), 0)

@deprecate rsearch(s::AbstractString, c::Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}, i::Integer) coalesce(findprev(in(c), s, i), 0)
@deprecate rsearch(s::AbstractString, c::Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}) coalesce(findlast(in(c), s), 0)
@deprecate rsearch(s::AbstractString, t::AbstractString, i::Integer) coalesce(findprev(t, s, i), 0:-1)
Expand Down Expand Up @@ -1565,6 +1562,14 @@ end
@deprecate ucfirst uppercasefirst
@deprecate lcfirst lowercasefirst

function search(buf::IOBuffer, delim::UInt8)
Base.depwarn("search(buf::IOBuffer, delim::UInt8) is deprecated: use occursin(delim, buf) or readuntil(buf, delim) instead", :search)
p = pointer(buf.data, buf.ptr)
q = GC.@preserve buf ccall(:memchr,Ptr{UInt8},(Ptr{UInt8},Int32,Csize_t),p,delim,bytesavailable(buf))
q == C_NULL && return nothing
return Int(q-p+1)
end

# END 0.7 deprecations

# BEGIN 1.0 deprecations
Expand Down
15 changes: 6 additions & 9 deletions base/iobuffer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -435,22 +435,19 @@ read(io::GenericIOBuffer) = read!(io,StringVector(bytesavailable(io)))
readavailable(io::GenericIOBuffer) = read(io)
read(io::GenericIOBuffer, nb::Integer) = read!(io,StringVector(min(nb, bytesavailable(io))))

function findfirst(delim::Fix2{<:Union{typeof(isequal),typeof(==)},UInt8}, buf::IOBuffer)
function occursin(delim::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,bytesavailable(buf))
q == C_NULL && return nothing
return Int(q-p+1)
q = GC.@preserve buf ccall(:memchr,Ptr{UInt8},(Ptr{UInt8},Int32,Csize_t),p,delim,bytesavailable(buf))
return q != C_NULL
end

function findfirst(isdelim::Fix2{<:Union{typeof(isequal),typeof(==)},UInt8}, buf::GenericIOBuffer)
function occursin(delim::UInt8, buf::GenericIOBuffer)
data = buf.data
for i = buf.ptr:buf.size
@inbounds b = data[i]
if isdelim(b)
return i - buf.ptr + 1
end
b == delim && return true
end
return nothing
return false
end

function readuntil(io::GenericIOBuffer, delim::UInt8; keep::Bool=false)
Expand Down
6 changes: 3 additions & 3 deletions base/stream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -267,13 +267,13 @@ end

function wait_readbyte(x::LibuvStream, c::UInt8)
if isopen(x) # fast path
findfirst(isequal(c), x.buffer) !== nothing && return
occursin(c, x.buffer) && return
else
return
end
preserve_handle(x)
try
while isopen(x) && coalesce(findfirst(isequal(c), x.buffer), 0) <= 0
while isopen(x) && !occursin(c, x.buffer)
start_reading(x) # ensure we are reading
wait(x.readnotify)
end
Expand Down Expand Up @@ -1074,7 +1074,7 @@ end
show(io::IO, s::BufferStream) = print(io,"BufferStream() bytes waiting:",bytesavailable(s.buffer),", isopen:", s.is_open)

function wait_readbyte(s::BufferStream, c::UInt8)
while isopen(s) && findfirst(isequal(c), s.buffer) === nothing
while isopen(s) && !occursin(c, s.buffer)
wait(s.r_c)
end
end
Expand Down
2 changes: 1 addition & 1 deletion stdlib/REPL/src/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -893,7 +893,7 @@ function setup_interface(
sbuffer = LineEdit.buffer(s)
curspos = position(sbuffer)
seek(sbuffer, 0)
shouldeval = (bytesavailable(sbuffer) == curspos && findfirst(isequal(UInt8('\n')), sbuffer) === nothing)
shouldeval = (bytesavailable(sbuffer) == curspos && !occursin(UInt8('\n'), sbuffer))
seek(sbuffer, curspos)
if curspos == 0
# if pasting at the beginning, strip leading whitespace
Expand Down

0 comments on commit 44500ce

Please sign in to comment.