Skip to content

Commit d6b213d

Browse files
committed
Deprecate (r)searchindex(...) in favor of first(findnext/findprev(...))
1 parent 655280b commit d6b213d

File tree

6 files changed

+42
-158
lines changed

6 files changed

+42
-158
lines changed

base/deprecated.jl

+10
Original file line numberDiff line numberDiff line change
@@ -3292,6 +3292,16 @@ end
32923292
@deprecate rsearch(a::String, b::Union{Int8,UInt8}, i::Integer = endof(a)) findprev(equalto(Char(b)), a, i)
32933293
@deprecate rsearch(a::ByteArray, b::Char, i::Integer = endof(a)) findprev(equalto(UInt8(b)), a, i)
32943294

3295+
@deprecate searchindex(s::AbstractString, t::AbstractString) first(findfirst(t, s))
3296+
@deprecate searchindex(s::AbstractString, t::AbstractString, i::Integer) first(findnext(t, s, i))
3297+
@deprecate rsearchindex(s::AbstractString, t::AbstractString) first(findlast(t, s))
3298+
@deprecate rsearchindex(s::AbstractString, t::AbstractString, i::Integer) first(findprev(t, s, i))
3299+
3300+
@deprecate searchindex(s::AbstractString, c::Char) first(findfirst(equalto(c), s))
3301+
@deprecate searchindex(s::AbstractString, c::Char, i::Integer) first(findnext(equalto(c), s, i))
3302+
@deprecate rsearchindex(s::AbstractString, c::Char) first(findlast(equalto(c), s))
3303+
@deprecate rsearchindex(s::AbstractString, c::Char, i::Integer) first(findprev(equalto(c), s, i))
3304+
32953305
# END 0.7 deprecations
32963306

32973307
# BEGIN 1.0 deprecations

base/exports.jl

-2
Original file line numberDiff line numberDiff line change
@@ -537,8 +537,6 @@ export
537537
occursin,
538538
match,
539539
matchall,
540-
rsearchindex,
541-
searchindex,
542540
searchsorted,
543541
searchsortedfirst,
544542
searchsortedlast,

base/precompile.jl

-2
Original file line numberDiff line numberDiff line change
@@ -625,7 +625,6 @@ precompile(Tuple{typeof(Base.unique), Array{String, 1}})
625625
precompile(Tuple{typeof(Base.REPL.beforecursor), Base.GenericIOBuffer{Array{UInt8, 1}}})
626626
precompile(Tuple{typeof(Base.REPLCompletions.completions), String, Int64})
627627
precompile(Tuple{typeof(Base.incomplete_tag), Symbol})
628-
precompile(Tuple{typeof(Base.rsearchindex), String, String, Int64})
629628
precompile(Tuple{typeof(Base._rsearch), String, String, Int64})
630629
precompile(Tuple{typeof(Base.unshift!), Array{Base.SubString{String}, 1}, Base.SubString{String}})
631630
precompile(Tuple{typeof(Base.startswith), String, Base.SubString{String}})
@@ -923,7 +922,6 @@ precompile(Tuple{typeof(Base.Markdown.parseinline), Base.GenericIOBuffer{Array{U
923922
precompile(Tuple{typeof(Base.Markdown.config), Base.Markdown.MD})
924923
precompile(Tuple{typeof(Base.Markdown.parseinline), Base.GenericIOBuffer{Array{UInt8, 1}}, Base.Markdown.MD, Base.Markdown.Config})
925924
precompile(Tuple{typeof(Base.Markdown.list), Base.GenericIOBuffer{Array{UInt8, 1}}, Base.Markdown.MD})
926-
precompile(Tuple{typeof(Base.searchindex), String, String})
927925
precompile(Tuple{typeof(Base._searchindex), Base.SubString{String}, String, Int64})
928926
precompile(Tuple{getfield(Base.Markdown, Symbol("#kw##skipwhitespace")), Array{Any, 1}, typeof(Base.Markdown.skipwhitespace), Base.GenericIOBuffer{Array{UInt8, 1}}})
929927
precompile(Tuple{typeof(Base.ht_keyindex), Base.Dict{Symbol, Base.Markdown.Config}, Symbol})

base/strings/search.jl

+30-94
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function findnext(pred::EqualTo{Char}, s::String, i::Integer)
66
throw(BoundsError(s, i))
77
end
88
@inbounds isvalid(s, i) || string_index_err(s, i)
9-
c = pred.x
9+
c = pred.x
1010
c '\x7f' && return _search(s, c % UInt8, i)
1111
while true
1212
i = _search(s, first_utf8_byte(c), i)
@@ -146,8 +146,11 @@ end
146146
_nthbyte(s::String, i) = codeunit(s, i)
147147
_nthbyte(a::ByteArray, i) = a[i]
148148

149-
_searchindex(s::String, t::String, i::Integer) =
149+
function _searchindex(s::String, t::String, i::Integer)
150+
# Check for fast case of a single byte
151+
endof(t) == 1 && return findnext(equalto(t[1]), s, i)
150152
_searchindex(Vector{UInt8}(s), Vector{UInt8}(t), i)
153+
end
151154

152155
function _searchindex(s::ByteArray, t::ByteArray, i::Integer)
153156
n = sizeof(t)
@@ -210,43 +213,10 @@ function _searchindex(s::ByteArray, t::ByteArray, i::Integer)
210213
0
211214
end
212215

213-
searchindex(s::ByteArray, t::ByteArray, i::Integer) = _searchindex(s,t,i)
214-
215-
"""
216-
searchindex(s::AbstractString, substring, [start::Integer])
217-
218-
Similar to `search`, but return only the start index at which
219-
the substring is found, or `0` if it is not.
220-
221-
# Examples
222-
```jldoctest
223-
julia> searchindex("Hello to the world", "z")
224-
0
225-
226-
julia> searchindex("JuliaLang","Julia")
227-
1
228-
229-
julia> searchindex("JuliaLang","Lang")
230-
6
231-
```
232-
"""
233-
searchindex(s::AbstractString, t::AbstractString, i::Integer) = _searchindex(s,t,i)
234-
searchindex(s::AbstractString, t::AbstractString) = searchindex(s,t,start(s))
235-
searchindex(s::AbstractString, c::Char, i::Integer) = _searchindex(s,c,i)
236-
searchindex(s::AbstractString, c::Char) = searchindex(s,c,start(s))
237-
238-
function searchindex(s::String, t::String, i::Integer=1)
239-
# Check for fast case of a single byte
240-
# (for multi-byte UTF-8 sequences, use searchindex on byte arrays instead)
241-
if endof(t) == 1
242-
findnext(equalto(t[1]), s, i)
243-
else
244-
_searchindex(s, t, i)
245-
end
246-
end
247-
248-
function _search(s, t, i::Integer)
249-
idx = searchindex(s,t,i)
216+
function _search(s::Union{AbstractString,ByteArray},
217+
t::Union{AbstractString,Char,Int8,UInt8},
218+
i::Integer)
219+
idx = _searchindex(s,t,i)
250220
if isempty(t)
251221
idx:idx-1
252222
else
@@ -281,8 +251,6 @@ julia> findnext("Julia", "JuliaLang", 2)
281251
```
282252
"""
283253
findnext(t::AbstractString, s::AbstractString, i::Integer) = _search(s, t, i)
284-
# TODO: remove?
285-
findnext(t::ByteArray, s::ByteArray, i::Integer) = _search(s, t, i)
286254

287255
"""
288256
findlast(pattern::AbstractString, string::AbstractString)
@@ -353,8 +321,21 @@ function _rsearchindex(s::AbstractString,
353321
end
354322
end
355323

356-
_rsearchindex(s::String, t::String, i::Integer) =
357-
_rsearchindex(Vector{UInt8}(s), Vector{UInt8}(t), i)
324+
function _rsearchindex(s::String, t::String, i::Integer)
325+
# Check for fast case of a single byte
326+
if endof(t) == 1
327+
return findprev(equalto(t[1]), s, i)
328+
elseif endof(t) != 0
329+
j = i ncodeunits(s) ? nextind(s, i)-1 : i
330+
return _rsearchindex(Vector{UInt8}(s), Vector{UInt8}(t), j)
331+
elseif i > sizeof(s)
332+
return 0
333+
elseif i == 0
334+
return 1
335+
else
336+
return i
337+
end
338+
end
358339

359340
function _rsearchindex(s::ByteArray, t::ByteArray, k::Integer)
360341
n = sizeof(t)
@@ -417,54 +398,10 @@ function _rsearchindex(s::ByteArray, t::ByteArray, k::Integer)
417398
0
418399
end
419400

420-
rsearchindex(s::ByteArray, t::ByteArray, i::Integer) = _rsearchindex(s,t,i)
421-
422-
"""
423-
rsearchindex(s::AbstractString, substring, [start::Integer])
424-
425-
Similar to `rsearch`, but return only the start index at which the substring is found, or `0` if it is not.
426-
427-
# Examples
428-
```jldoctest
429-
julia> rsearchindex("aaabbb","b")
430-
6
431-
432-
julia> rsearchindex("aaabbb","a")
433-
3
434-
```
435-
"""
436-
rsearchindex(s::AbstractString, t::AbstractString, i::Integer) = _rsearchindex(s,t,i)
437-
rsearchindex(s::AbstractString, t::AbstractString) = (isempty(s) && isempty(t)) ? 1 : rsearchindex(s,t,endof(s))
438-
439-
function rsearchindex(s::String, t::String)
440-
# Check for fast case of a single byte
441-
# (for multi-byte UTF-8 sequences, use rsearchindex instead)
442-
if endof(t) == 1
443-
findprev(equalto(t[1]), s)
444-
else
445-
_rsearchindex(s, t, sizeof(s))
446-
end
447-
end
448-
449-
function rsearchindex(s::String, t::String, i::Integer)
450-
# Check for fast case of a single byte
451-
# (for multi-byte UTF-8 sequences, use rsearchindex instead)
452-
if endof(t) == 1
453-
findprev(equalto(t[1]), s, i)
454-
elseif endof(t) != 0
455-
j = i ncodeunits(s) ? nextind(s, i)-1 : i
456-
_rsearchindex(s, t, j)
457-
elseif i > sizeof(s)
458-
return 0
459-
elseif i == 0
460-
return 1
461-
else
462-
return i
463-
end
464-
end
465-
466-
function _rsearch(s, t, i::Integer)
467-
idx = rsearchindex(s,t,i)
401+
function _rsearch(s::Union{AbstractString,ByteArray},
402+
t::Union{AbstractString,Char,Int8,UInt8},
403+
i::Integer)
404+
idx = _rsearchindex(s,t,i)
468405
if isempty(t)
469406
idx:idx-1
470407
else
@@ -499,8 +436,6 @@ julia> findprev("Julia", "JuliaLang", 6)
499436
```
500437
"""
501438
findprev(t::AbstractString, s::AbstractString, i::Integer) = _rsearch(s, t, i)
502-
# TODO: remove?
503-
findprev(t::ByteArray, s::ByteArray, i::Integer) = _rsearch(s, t, i)
504439

505440
"""
506441
contains(haystack::AbstractString, needle::Union{AbstractString,Char})
@@ -513,6 +448,7 @@ julia> contains("JuliaLang is pretty cool!", "Julia")
513448
true
514449
```
515450
"""
516-
contains(haystack::AbstractString, needle::Union{AbstractString,Char}) = searchindex(haystack,needle)!=0
451+
contains(haystack::AbstractString, needle::Union{AbstractString,Char}) =
452+
_searchindex(haystack, needle, start(haystack)) != 0
517453

518454
in(::AbstractString, ::AbstractString) = error("use contains(x,y) for string containment")

doc/src/stdlib/strings.md

-2
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,6 @@ Base.findfirst(::AbstractString, ::AbstractString)
3636
Base.findnext(::AbstractString, ::AbstractString, ::Integer)
3737
Base.findlast(::AbstractString, ::AbstractString)
3838
Base.findprev(::AbstractString, ::AbstractString, ::Integer)
39-
Base.searchindex
40-
Base.rsearchindex
4139
Base.contains(::AbstractString, ::AbstractString)
4240
Base.reverse(::Union{String,SubString{String}})
4341
Base.replace

test/strings/search.jl

+2-58
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,18 @@ u8str = "∀ ε > 0, ∃ δ > 0: |x-y| < δ ⇒ |f(x)-f(y)| < ε"
99
for ind in (0, 5)
1010
@test_throws BoundsError findnext(SubString("",1,1), "foo", ind)
1111
@test_throws BoundsError findprev(SubString("",1,1), "foo", ind)
12-
@test_throws BoundsError searchindex("foo", SubString("",1,1), ind)
13-
@test_throws BoundsError rsearchindex("foo", SubString("",1,1), ind)
1412
end
1513

16-
# Note: the commented out tests will be enabled after fixes to make
17-
# sure that findnext/findprev/searchindex/rsearchindex are consistent
14+
# Note: the commented out test will be enabled after fixes to make
15+
# sure that findnext/findprev are consistent
1816
# no matter what type of AbstractString the second argument is
1917
@test_throws BoundsError findnext(equalto('a'), "foo", 0)
2018
@test_throws BoundsError findnext(occursin(Char[]), "foo", 5)
2119
# @test_throws BoundsError findprev(occursin(Char[]), "foo", 0)
2220
@test_throws BoundsError findprev(occursin(Char[]), "foo", 5)
2321

24-
# @test_throws BoundsError searchindex("foo", Char[], 0)
25-
# @test_throws BoundsError searchindex("foo", Char[], 5)
26-
# @test_throws BoundsError rsearchindex("foo", Char[], 0)
27-
# @test_throws BoundsError rsearchindex("foo", Char[], 5)
28-
2922
# @test_throws ErrorException in("foobar","bar")
3023
@test_throws BoundsError findnext(equalto(0x1),b"\x1\x2",0)
31-
@test rsearchindex(b"foo",b"o",0) == 0
32-
@test rsearchindex(SubString("",1,0),SubString("",1,0)) == 1
3324

3425
# ascii forward search
3526
for str in [astr, GenericString(astr)]
@@ -308,10 +299,6 @@ end
308299
@test findlast("az", "foo,bar,baz") == 10:11
309300
@test findprev("az", "foo,bar,baz", 10) == 0:-1
310301

311-
# array backward search
312-
@test findprev(UInt8[2,3],UInt8[1,2,3],3) == 2:3
313-
@test findprev(UInt8[2,3],UInt8[1,2,3],1) == 0:-1
314-
315302
# string search with a two-char regex
316303
@test findfirst(r"xx", "foo,bar,baz") == 0:-1
317304
@test findfirst(r"fo", "foo,bar,baz") == 1:2
@@ -326,53 +313,10 @@ end
326313
@test findfirst(r"az", "foo,bar,baz") == 10:11
327314
@test findnext(r"az", "foo,bar,baz", 12) == 0:-1
328315

329-
@test searchindex("foo", 'o') == 2
330-
@test searchindex("foo", 'o', 3) == 3
331-
332-
# string searchindex with a two-char UTF-8 (2 byte) string literal
333-
@test searchindex("ééé", "éé") == 1
334-
@test searchindex("ééé", "éé", 1) == 1
335-
# string searchindex with a two-char UTF-8 (3 byte) string literal
336-
@test searchindex("€€€", "€€") == 1
337-
@test searchindex("€€€", "€€", 1) == 1
338-
# string searchindex with a two-char UTF-8 (4 byte) string literal
339-
@test searchindex("\U1f596\U1f596\U1f596", "\U1f596\U1f596") == 1
340-
@test searchindex("\U1f596\U1f596\U1f596", "\U1f596\U1f596", 1) == 1
341-
342-
# string searchindex with a two-char UTF-8 (2 byte) string literal
343-
@test searchindex("éé", "éé") == 1
344-
@test searchindex("éé", "éé", 1) == 1
345-
# string searchindex with a two-char UTF-8 (3 byte) string literal
346-
@test searchindex("€€", "€€") == 1
347-
@test searchindex("€€", "€€", 1) == 1
348-
# string searchindex with a two-char UTF-8 (4 byte) string literal
349-
@test searchindex("\U1f596\U1f596", "\U1f596\U1f596") == 1
350-
@test searchindex("\U1f596\U1f596", "\U1f596\U1f596", 1) == 1
351-
352316
# contains with a String and Char needle
353317
@test contains("foo", "o")
354318
@test contains("foo", 'o')
355319

356-
# string rsearchindex with a two-char UTF-8 (2 byte) string literal
357-
@test rsearchindex("ééé", "éé") == 3
358-
@test rsearchindex("ééé", "éé", endof("ééé")) == 3
359-
# string rsearchindex with a two-char UTF-8 (3 byte) string literal
360-
@test rsearchindex("€€€", "€€") == 4
361-
@test rsearchindex("€€€", "€€", endof("€€€")) == 4
362-
# string rsearchindex with a two-char UTF-8 (4 byte) string literal
363-
@test rsearchindex("\U1f596\U1f596\U1f596", "\U1f596\U1f596") == 5
364-
@test rsearchindex("\U1f596\U1f596\U1f596", "\U1f596\U1f596", endof("\U1f596\U1f596\U1f596")) == 5
365-
366-
# string rsearchindex with a two-char UTF-8 (2 byte) string literal
367-
@test rsearchindex("éé", "éé") == 1
368-
@test rsearchindex("éé", "éé", endof("ééé")) == 1
369-
# string searchindex with a two-char UTF-8 (3 byte) string literal
370-
@test rsearchindex("€€", "€€") == 1
371-
@test rsearchindex("€€", "€€", endof("€€€")) == 1
372-
# string searchindex with a two-char UTF-8 (4 byte) string literal
373-
@test rsearchindex("\U1f596\U1f596", "\U1f596\U1f596") == 1
374-
@test rsearchindex("\U1f596\U1f596", "\U1f596\U1f596", endof("\U1f596\U1f596\U1f596")) == 1
375-
376320
@test_throws ErrorException "ab" "abc"
377321

378322
# issue #15723

0 commit comments

Comments
 (0)