Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Deprecate findfirst on I/O buffers #26600

Merged
merged 1 commit into from
Mar 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -1038,7 +1038,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 @@ -1430,3 +1433,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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be for GenericIOBuffer.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

...with the more generic implementation of course. Otherwise it seems we're missing a deprecation for search(::GenericIOBuffer, ...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

GenericIOBuffer isn't exported, so is it really worth having a deprecation?

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