Skip to content

Commit efbcdb4

Browse files
committed
Revert "add better implementations for generic findprev findnext for AbstractString (JuliaLang#35742)"
This reverts commit a47cf00.
1 parent edeff81 commit efbcdb4

File tree

1 file changed

+16
-15
lines changed

1 file changed

+16
-15
lines changed

base/strings/search.jl

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -127,17 +127,16 @@ findfirst(ch::AbstractChar, string::AbstractString) = findfirst(==(ch), string)
127127
function findnext(testf::Function, s::AbstractString, i::Integer)
128128
i = Int(i)
129129
z = ncodeunits(s) + 1
130-
1 i z || throw(BoundsError(s, i))
130+
1 i  z || throw(BoundsError(s, i))
131131
@inbounds i == z || isvalid(s, i) || string_index_err(s, i)
132-
e = lastindex(s)
133-
while i <= e
134-
testf(@inbounds s[i]) && return i
135-
i = @inbounds nextind(s, i)
132+
for (j, d) in pairs(SubString(s, i))
133+
if testf(d)
134+
return i + j - 1
135+
end
136136
end
137137
return nothing
138138
end
139139

140-
141140
in(c::AbstractChar, s::AbstractString) = (findfirst(isequal(c),s)!==nothing)
142141

143142
function _searchindex(s::Union{AbstractString,ByteArray},
@@ -335,16 +334,18 @@ findlast(ch::AbstractChar, string::AbstractString) = findlast(==(ch), string)
335334

336335
# AbstractString implementation of the generic findprev interface
337336
function findprev(testf::Function, s::AbstractString, i::Integer)
338-
i = Int(i)
339-
z = ncodeunits(s) + 1
340-
0 i z || throw(BoundsError(s, i))
341-
i == z && return nothing
342-
@inbounds i == 0 || isvalid(s, i) || string_index_err(s, i)
343-
while i >= 1
344-
testf(@inbounds s[i]) && return i
345-
i = @inbounds prevind(s, i)
337+
if i < 1
338+
return i == 0 ? nothing : throw(BoundsError(s, i))
346339
end
347-
return nothing
340+
n = ncodeunits(s)
341+
if i > n
342+
return i == n+1 ? nothing : throw(BoundsError(s, i))
343+
end
344+
# r[reverseind(r,i)] == reverse(r)[i] == s[i]
345+
# s[reverseind(s,j)] == reverse(s)[j] == r[j]
346+
r = reverse(s)
347+
j = findnext(testf, r, reverseind(r, i))
348+
j === nothing ? nothing : reverseind(s, j)
348349
end
349350

350351
function _rsearchindex(s::AbstractString,

0 commit comments

Comments
 (0)