Skip to content

Commit 331d6a5

Browse files
committed
Merge search into findfirst/findnext
1 parent 80d8719 commit 331d6a5

24 files changed

+303
-242
lines changed

base/deprecated.jl

+19
Original file line numberDiff line numberDiff line change
@@ -2116,6 +2116,25 @@ end
21162116
finalizer(f::Ptr{Void}, o::Ptr{Void}) = invoke(finalizer, Tuple{Ptr{Void}, Any}, f, o)
21172117
finalizer(f::Ptr{Void}, o::Function) = invoke(finalizer, Tuple{Ptr{Void}, Any}, f, o)
21182118

2119+
@deprecate search(str::Union{String,SubString}, re::Regex, idx::Integer) findnext(re, str, idx)
2120+
@deprecate search(str::Union{String,SubString}, re::Regex) findfirst(re, str)
2121+
@deprecate search(s::AbstractString, r::Regex, idx::Integer) findnext(r, s, idx)
2122+
@deprecate search(s::AbstractString, r::Regex) findfirst(r, s)
2123+
@deprecate search(s::String, c::Char, i::Integer) findnext(equalto(c), s, i)
2124+
@deprecate search(s::String, c::Char) findfirst(equalto(c), s)
2125+
# FIXME: no replacement to search for a multibyte char in a ByteArray
2126+
@deprecate search(a::Union{String,ByteArray}, b::Union{Int8,UInt8}, i::Integer = 1) findnext(equalto(b), a, i)
2127+
@deprecate search(a::String, b::Union{Int8,UInt8}, i::Integer = 1) findnext(equalto(Char(b)), a, i)
2128+
@deprecate search(a::ByteArray, b::Char, i::Integer = 1) findnext(equalto(Unt8(b)), a, i)
2129+
2130+
@deprecate search(s::AbstractString, c::Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}, i::Integer) findnext(x -> x in c, s, i)
2131+
@deprecate search(s::AbstractString, c::Union{Tuple{Vararg{Char}},AbstractVector{Char},Set{Char}}) findfirst(x -> x in c, s, i)
2132+
@deprecate search(s::AbstractString, t::AbstractString, i::Integer) findnext(t, s, i)
2133+
@deprecate search(s::AbstractString, t::AbstractString) findfirst(t, s)
2134+
2135+
@deprecate search(buf::IOBuffer, delim::UInt8) findfirst(equalto(delim), buf)
2136+
@deprecate search(buf::Base.GenericIOBuffer, delim::UInt8) findfirst(equalto(delim), buf)
2137+
21192138
# END 0.7 deprecations
21202139

21212140
# BEGIN 1.0 deprecations

base/docs/utils.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -373,7 +373,7 @@ completions(name::Symbol) = completions(string(name))
373373
# Searching and apropos
374374

375375
# Docsearch simply returns true or false if an object contains the given needle
376-
docsearch(haystack::AbstractString, needle) = !isempty(search(haystack, needle))
376+
docsearch(haystack::AbstractString, needle) = !isempty(findfirst(needle, haystack))
377377
docsearch(haystack::Symbol, needle) = docsearch(string(haystack), needle)
378378
docsearch(::Void, needle) = false
379379
function docsearch(haystack::Array, needle)

base/exports.jl

-1
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,6 @@ export
775775
rsearchindex,
776776
rsplit,
777777
rstrip,
778-
search,
779778
searchindex,
780779
show,
781780
showcompact,

base/iobuffer.jl

+4-4
Original file line numberDiff line numberDiff line change
@@ -365,18 +365,18 @@ read(io::GenericIOBuffer) = read!(io,StringVector(nb_available(io)))
365365
readavailable(io::GenericIOBuffer) = read(io)
366366
read(io::GenericIOBuffer, nb::Integer) = read!(io,StringVector(min(nb, nb_available(io))))
367367

368-
function search(buf::IOBuffer, delim::UInt8)
368+
function findfirst(delim::EqualTo{UInt8}, buf::IOBuffer)
369369
p = pointer(buf.data, buf.ptr)
370-
q = @gc_preserve buf ccall(:memchr,Ptr{UInt8},(Ptr{UInt8},Int32,Csize_t),p,delim,nb_available(buf))
370+
q = @gc_preserve buf ccall(:memchr,Ptr{UInt8},(Ptr{UInt8},Int32,Csize_t),p,delim.x,nb_available(buf))
371371
nb::Int = (q == C_NULL ? 0 : q-p+1)
372372
return nb
373373
end
374374

375-
function search(buf::GenericIOBuffer, delim::UInt8)
375+
function findfirst(delim::EqualTo{UInt8}, buf::GenericIOBuffer)
376376
data = buf.data
377377
for i = buf.ptr : buf.size
378378
@inbounds b = data[i]
379-
if b == delim
379+
if b == delim.x
380380
return i - buf.ptr + 1
381381
end
382382
end

base/libgit2/types.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -893,7 +893,7 @@ function Base.split(ce::ConfigEntry)
893893
key = unsafe_string(ce.name)
894894

895895
# Determine the positions of the delimiters
896-
subsection_delim = search(key, '.')
896+
subsection_delim = findfirst(equalto('.'), key)
897897
name_delim = rsearch(key, '.')
898898

899899
section = SubString(key, 1, subsection_delim - 1)

base/methodshow.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function argtype_decl(env, n, sig::DataType, i::Int, nargs, isva::Bool) # -> (ar
1111
n = n.args[1] # handle n::T in arg list
1212
end
1313
s = string(n)
14-
i = search(s,'#')
14+
i = findfirst(equalto('#'), s)
1515
if i > 0
1616
s = s[1:i-1]
1717
end

base/precompile.jl

+1-2
Original file line numberDiff line numberDiff line change
@@ -583,7 +583,6 @@ precompile(Tuple{typeof(Base.LineEdit.complete_line), Base.REPL.REPLCompletionPr
583583
precompile(Tuple{getfield(Base, Symbol("#kw##parse")), Array{Any, 1}, typeof(Base.parse), String})
584584
precompile(Tuple{typeof(Base.isvalid), Base.RevString{String}, Int64})
585585
precompile(Tuple{typeof(Base.nextind), Base.RevString{String}, Int64})
586-
precompile(Tuple{typeof(Base.search), Base.RevString{String}, Array{Char, 1}, Int64})
587586
precompile(Tuple{typeof(Base.rsearch), String, Array{Char, 1}, Int64})
588587
precompile(Tuple{getfield(Base.REPLCompletions, Symbol("#kw##find_start_brace")), Array{Any, 1}, typeof(Base.REPLCompletions.find_start_brace), String})
589588
precompile(Tuple{typeof(Core.Inference.isbits), Tuple{Void, Void, Void}})
@@ -1217,7 +1216,7 @@ precompile(Tuple{typeof(Base.Distributed.setup_launched_worker), Base.Distribute
12171216
precompile(Tuple{typeof(Base.connect), Base.Distributed.LocalManager, Int64, Base.Distributed.WorkerConfig})
12181217
precompile(Tuple{typeof(Base.Distributed.read_worker_host_port), Base.Pipe})
12191218
precompile(Tuple{typeof(Base.isreadable), Base.PipeEndpoint})
1220-
precompile(Tuple{typeof(Base.search), Base.GenericIOBuffer{Array{UInt8, 1}}, UInt8})
1219+
precompile(Tuple{typeof(Base.findfirst), UInt8, Base.GenericIOBuffer{Array{UInt8, 1}}})
12211220
precompile(Tuple{typeof(Base.start_reading), Base.PipeEndpoint})
12221221
precompile(Tuple{typeof(Base.wait_readbyte), Base.PipeEndpoint, UInt8})
12231222
precompile(Tuple{typeof(Base.readuntil), Base.PipeEndpoint, UInt8})

base/regex.jl

+4-3
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,8 @@ end
285285
matchall(re::Regex, str::SubString, overlap::Bool=false) =
286286
matchall(re, String(str), overlap)
287287

288-
function search(str::Union{String,SubString}, re::Regex, idx::Integer)
288+
# TODO: return only start index and update deprecation
289+
function findnext(re::Regex, str::Union{String,SubString}, idx::Integer)
289290
if idx > nextind(str,endof(str))
290291
throw(BoundsError())
291292
end
@@ -294,10 +295,10 @@ function search(str::Union{String,SubString}, re::Regex, idx::Integer)
294295
PCRE.exec(re.regex, str, idx-1, opts, re.match_data) ?
295296
((Int(re.ovec[1])+1):prevind(str,Int(re.ovec[2])+1)) : (0:-1)
296297
end
297-
search(s::AbstractString, r::Regex, idx::Integer) = throw(ArgumentError(
298+
findnext(r::Regex, s::AbstractString, idx::Integer) = throw(ArgumentError(
298299
"regex search is only available for the String type; use String(s) to convert"
299300
))
300-
search(s::AbstractString, r::Regex) = search(s,r,start(s))
301+
findfirst(r::Regex, s::AbstractString) = findnext(r,s,start(s))
301302

302303
struct SubstitutionString{T<:AbstractString} <: AbstractString
303304
string::T

base/repl/LineEdit.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -512,7 +512,7 @@ function edit_move_down(buf::IOBuffer)
512512
npos = rsearch(buf.data[1:buf.size], '\n', position(buf))
513513
# We're interested in character count, not byte count
514514
offset = length(String(buf.data[(npos+1):(position(buf))]))
515-
npos2 = search(buf.data[1:buf.size], '\n', position(buf)+1)
515+
npos2 = findnext(equalto(UInt8('\n')), buf.data[1:buf.size], position(buf)+1)
516516
if npos2 == 0 #we're in the last line
517517
return false
518518
end
@@ -1615,7 +1615,7 @@ function move_line_end(s::MIState)
16151615
end
16161616
function move_line_end(buf::IOBuffer)
16171617
eof(buf) && return
1618-
pos = search(buf.data, '\n', position(buf)+1)
1618+
pos = findnext(equalto(UInt8('\n')), buf.data, position(buf)+1)
16191619
if pos == 0
16201620
move_input_end(buf)
16211621
return

base/repl/REPL.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ function history_search(hist::REPLHistoryProvider, query_buffer::IOBuffer, respo
584584
!skip_current && searchdata == response_str[a:b] && return true
585585

586586
searchfunc, searchstart, skipfunc = backwards ? (rsearch, b, prevind) :
587-
(search, a, nextind)
587+
(findfirst, a, nextind)
588588
skip_current && (searchstart = skipfunc(response_str, searchstart))
589589

590590
# Start searching
@@ -601,7 +601,7 @@ function history_search(hist::REPLHistoryProvider, query_buffer::IOBuffer, respo
601601
idxs = backwards ? ((hist.cur_idx-1):-1:1) : ((hist.cur_idx+1):length(hist.history))
602602
for idx in idxs
603603
h = hist.history[idx]
604-
match = searchfunc(h, searchdata)
604+
match = searchfunc === findfirst ? findfirst(searchdata, h) : rsearch(h, searchdata)
605605
if match != 0:-1 && h != response_str && haskey(hist.mode_mapping, hist.modes[idx])
606606
truncate(response_buffer, 0)
607607
write(response_buffer, h)
@@ -855,7 +855,7 @@ function setup_interface(
855855
sbuffer = LineEdit.buffer(s)
856856
curspos = position(sbuffer)
857857
seek(sbuffer, 0)
858-
shouldeval = (nb_available(sbuffer) == curspos && search(sbuffer, UInt8('\n')) == 0)
858+
shouldeval = (nb_available(sbuffer) == curspos && findfirst(equalto(UInt8('\n')), sbuffer) == 0)
859859
seek(sbuffer, curspos)
860860
if curspos == 0
861861
# if pasting at the beginning, strip leading whitespace

base/repl/REPLCompletions.jl

+2-2
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,7 @@ function afterusing(string::String, startpos::Int)
405405
str = string[1:prevind(string,startpos)]
406406
isempty(str) && return false
407407
rstr = reverse(str)
408-
r = search(rstr, r"\s(gnisu|tropmi)\b")
408+
r = findfirst(r"\s(gnisu|tropmi)\b", rstr)
409409
isempty(r) && return false
410410
fr = reverseind(str, last(r))
411411
return ismatch(r"^\b(using|import)\s*((\w+[.])*\w+\s*,\s*)*$", str[fr:end])
@@ -458,7 +458,7 @@ function dict_identifier_key(str,tag)
458458
# Avoid `isdefined(::Array, ::Symbol)`
459459
isa(obj, Array) && return (nothing, nothing, nothing)
460460
end
461-
begin_of_key = first(search(str, r"\S", nextind(str, end_of_identifier) + 1)) # 1 for [
461+
begin_of_key = first(findnext(r"\S", str, nextind(str, end_of_identifier) + 1)) # 1 for [
462462
begin_of_key==0 && return (true, nothing, nothing)
463463
partial_key = str[begin_of_key:end]
464464
(isa(obj, Associative) && length(obj) < 1e6) || return (true, nothing, nothing)

base/stream.jl

+3-3
Original file line numberDiff line numberDiff line change
@@ -260,13 +260,13 @@ end
260260

261261
function wait_readbyte(x::LibuvStream, c::UInt8)
262262
if isopen(x) # fast path
263-
search(x.buffer, c) > 0 && return
263+
findfirst(equalto(c), x.buffer) > 0 && return
264264
else
265265
return
266266
end
267267
preserve_handle(x)
268268
try
269-
while isopen(x) && search(x.buffer, c) <= 0
269+
while isopen(x) && findfirst(equalto(c), x.buffer) <= 0
270270
start_reading(x) # ensure we are reading
271271
wait(x.readnotify)
272272
end
@@ -1185,7 +1185,7 @@ end
11851185
show(io::IO, s::BufferStream) = print(io,"BufferStream() bytes waiting:",nb_available(s.buffer),", isopen:", s.is_open)
11861186

11871187
function wait_readbyte(s::BufferStream, c::UInt8)
1188-
while isopen(s) && search(s.buffer,c) <= 0
1188+
while isopen(s) && findfirst(equalto(c), x.buffer) <= 0
11891189
wait(s.r_c)
11901190
end
11911191
end

0 commit comments

Comments
 (0)