Skip to content

Commit

Permalink
change sentinel value to firstindex - 1
Browse files Browse the repository at this point in the history
  • Loading branch information
Moelf committed Sep 13, 2020
1 parent e30cef9 commit cb01268
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 18 deletions.
34 changes: 17 additions & 17 deletions base/strings/search.jl
Original file line number Diff line number Diff line change
Expand Up @@ -196,23 +196,23 @@ end
function _searchindex(s::AbstractVector{<:Union{Int8,UInt8}},
t::AbstractVector{<:Union{Int8,UInt8}},
_i::Integer)
require_one_based_indexing(s)
sentinel = firstindex(s) - 1
n = length(t)
m = length(s)
i = Int(_i) - (firstindex(s) - 1)
i = Int(_i) - sentinel
(i < 1 || i > m+1) && throw(BoundsError(s, _i))

if n == 0
return 1 <= i <= m+1 ? max(1, i) : 0
return 1 <= i <= m+1 ? max(1, i) : sentinel
elseif m == 0
return 0
return sentinel
elseif n == 1
return something(findnext(isequal(_nthbyte(t,1)), s, i), 0)
return something(findnext(isequal(_nthbyte(t,1)), s, i), sentinel)
end

w = m - n
if w < 0 || i - 1 > w
return 0
return sentinel
end

bloom_mask = UInt64(0)
Expand Down Expand Up @@ -257,7 +257,7 @@ function _searchindex(s::AbstractVector{<:Union{Int8,UInt8}},
i += 1
end

0
sentinel
end

function _search(s::Union{AbstractString,AbstractVector{<:Union{Int8,UInt8}}},
Expand All @@ -266,7 +266,7 @@ function _search(s::Union{AbstractString,AbstractVector{<:Union{Int8,UInt8}}},
idx = _searchindex(s,t,i)
if isempty(t)
idx:idx-1
elseif idx > 0
elseif idx > firstindex(s) - 1
idx:(idx + lastindex(t) - 1)
else
nothing
Expand Down Expand Up @@ -454,23 +454,23 @@ function _rsearchindex(s::String, t::String, i::Integer)
end

function _rsearchindex(s::AbstractVector{<:Union{Int8,UInt8}}, t::AbstractVector{<:Union{Int8,UInt8}}, _k::Integer)
require_one_based_indexing(s)
sentinel = firstindex(s) - 1
n = length(t)
m = length(s)
k = Int(_k) - (firstindex(s) - 1)
k = Int(_k) - sentinel
k < 1 && throw(BoundsError(s, _k))

if n == 0
return 0 <= k <= m ? max(k, 1) : 0
return 0 <= k <= m ? max(k, 1) : sentinel
elseif m == 0
return 0
return sentinel
elseif n == 1
return something(findprev(isequal(_nthbyte(t,1)), s, k), 0)
return something(findprev(isequal(_nthbyte(t,1)), s, k), sentinel)
end

w = m - n
if w < 0 || k <= 0
return 0
return sentinel
end

bloom_mask = UInt64(0)
Expand All @@ -497,7 +497,7 @@ function _rsearchindex(s::AbstractVector{<:Union{Int8,UInt8}}, t::AbstractVector

# match found, restore in case `s` is an OffsetArray
if j == n
return i + (firstindex(s) - 1)
return i + sentinel
end

# no match, try to rule out the next character
Expand All @@ -514,7 +514,7 @@ function _rsearchindex(s::AbstractVector{<:Union{Int8,UInt8}}, t::AbstractVector
i -= 1
end

0
sentinel
end

function _rsearch(s::Union{AbstractString,AbstractVector{<:Union{Int8,UInt8}}},
Expand All @@ -523,7 +523,7 @@ function _rsearch(s::Union{AbstractString,AbstractVector{<:Union{Int8,UInt8}}},
idx = _rsearchindex(s,t,i)
if isempty(t)
idx:idx-1
elseif idx > 0
elseif idx > firstindex(s) - 1
idx:(idx + lastindex(t) - 1)
else
nothing
Expand Down
43 changes: 43 additions & 0 deletions test/offsetarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -630,3 +630,46 @@ end
@test last(v, 100) !== v
@test last(v, 1) == [v[end]]
end

@testset "findfirst findnext of U/Int8 Offset Array" begin
for VT in [Int8, UInt8]
OA = OffsetArray(VT[0x40,0x52,0x62,0x52,0x62], 1)
OB = OffsetArray(VT[0x40,0x52,0x62,0x52,0x62], -2)
for PT in [Int8, UInt8]
pattern = PT[0x52, 0x62]
l_OA = lastindex(OA)
l_OB = lastindex(OB)
@test findfirst(pattern, OA) === 3:4
@test findnext(pattern, OA, 2) === 3:4
@test findnext(pattern, OA, 4) === 5:6
@test findnext(pattern, OA, 6) === nothing
@test findnext(pattern, OA, 7) === nothing
@test findnext(pattern, OA, 2) === 3:4
@test findnext(pattern, OA, 4) === 5:6
# negatively Offset array
@test findfirst(pattern, OB) === 0:1
@test findnext(pattern, OB, -1) === 0:1
@test findnext(pattern, OB, 1) === 2:3
@test findnext(pattern, OB, 3) === nothing
@test findnext(pattern, OB, 4) === nothing
@test findnext(pattern, OB, -1) === 0:1
@test findnext(pattern, OB, 1) === 2:3
# 1 idx too far is allowed
@test findnext(pattern, OA, l_OA+1) === nothing
@test_throws BoundsError findnext(pattern, OA, l_OA+2)
@test_throws BoundsError findnext(pattern, OA, 1)
@test findlast(pattern, OA) === 5:6
@test findprev(pattern, OA, 2) === nothing
@test findprev(pattern, OA, 4) === 3:4
@test findprev(pattern, OA, 6) === 5:6
@test findprev(pattern, OA, l_OA+1) == findlast(pattern, OA)
@test findprev(pattern, OA, l_OA+2) == findlast(pattern, OA)
@test findlast(pattern, OB) === 2:3
@test findprev(pattern, OB, -1) === nothing
@test findprev(pattern, OB, 1) === 0:1
@test findprev(pattern, OB, 3) === 2:3
@test findprev(pattern, OB, l_OB+1) == findlast(pattern, OB)
@test findprev(pattern, OB, l_OB+2) == findlast(pattern, OB)
end
end
end
2 changes: 1 addition & 1 deletion test/strings/search.jl
Original file line number Diff line number Diff line change
Expand Up @@ -405,7 +405,7 @@ end
@test findfirst(pattern, A) === 2:3
@test findnext(pattern, A, 2) === 2:3
@test findnext(pattern, A, 3) === 4:5
# 1 idx too long is allowed
# 1 idx too far is allowed
@test findnext(pattern, A, length(A)+1) === nothing
@test_throws BoundsError findnext(pattern, A, -3)
@test_throws BoundsError findnext(pattern, A, length(A)+2)
Expand Down

0 comments on commit cb01268

Please sign in to comment.