From 6f5b0dc8d9905a28322a95e5bab813854c16b5f4 Mon Sep 17 00:00:00 2001 From: KristofferC Date: Tue, 5 May 2020 11:48:37 +0200 Subject: [PATCH 1/2] add better implementations for generic findprev findnext for AbstractString --- base/strings/search.jl | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/base/strings/search.jl b/base/strings/search.jl index 9dec4f1db66cc..3db59b68aac35 100644 --- a/base/strings/search.jl +++ b/base/strings/search.jl @@ -127,16 +127,17 @@ findfirst(ch::AbstractChar, string::AbstractString) = findfirst(==(ch), string) function findnext(testf::Function, s::AbstractString, i::Integer) i = Int(i) z = ncodeunits(s) + 1 - 1 ≤ i ≤ z || throw(BoundsError(s, i)) + 1 ≤ i ≤ z || throw(BoundsError(s, i)) @inbounds i == z || isvalid(s, i) || string_index_err(s, i) - for (j, d) in pairs(SubString(s, i)) - if testf(d) - return i + j - 1 - end + e = lastindex(s) + @inbounds while i <= e + testf(s[i]) && return i + i = nextind(s, i) end return nothing end + in(c::AbstractChar, s::AbstractString) = (findfirst(isequal(c),s)!==nothing) function _searchindex(s::Union{AbstractString,ByteArray}, @@ -334,18 +335,16 @@ findlast(ch::AbstractChar, string::AbstractString) = findlast(==(ch), string) # AbstractString implementation of the generic findprev interface function findprev(testf::Function, s::AbstractString, i::Integer) - if i < 1 - return i == 0 ? nothing : throw(BoundsError(s, i)) - end - n = ncodeunits(s) - if i > n - return i == n+1 ? nothing : throw(BoundsError(s, i)) + i = Int(i) + z = ncodeunits(s) + 1 + 0 ≤ i ≤ z || throw(BoundsError(s, i)) + i == z && return nothing + @inbounds i == 0 || isvalid(s, i) || string_index_err(s, i) + @inbounds while i >= 1 + testf(s[i]) && return i + i = prevind(s, i) end - # r[reverseind(r,i)] == reverse(r)[i] == s[i] - # s[reverseind(s,j)] == reverse(s)[j] == r[j] - r = reverse(s) - j = findnext(testf, r, reverseind(r, i)) - j === nothing ? nothing : reverseind(s, j) + return nothing end function _rsearchindex(s::AbstractString, From 30b8419b93a30adef008bd7319c8cfbfce3aada9 Mon Sep 17 00:00:00 2001 From: KristofferC Date: Wed, 6 May 2020 15:02:30 +0200 Subject: [PATCH 2/2] tweak inbounds usage --- base/strings/search.jl | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/base/strings/search.jl b/base/strings/search.jl index 3db59b68aac35..9d2fdf73afff4 100644 --- a/base/strings/search.jl +++ b/base/strings/search.jl @@ -130,9 +130,9 @@ function findnext(testf::Function, s::AbstractString, i::Integer) 1 ≤ i ≤ z || throw(BoundsError(s, i)) @inbounds i == z || isvalid(s, i) || string_index_err(s, i) e = lastindex(s) - @inbounds while i <= e - testf(s[i]) && return i - i = nextind(s, i) + while i <= e + testf(@inbounds s[i]) && return i + i = @inbounds nextind(s, i) end return nothing end @@ -340,9 +340,9 @@ function findprev(testf::Function, s::AbstractString, i::Integer) 0 ≤ i ≤ z || throw(BoundsError(s, i)) i == z && return nothing @inbounds i == 0 || isvalid(s, i) || string_index_err(s, i) - @inbounds while i >= 1 - testf(s[i]) && return i - i = prevind(s, i) + while i >= 1 + testf(@inbounds s[i]) && return i + i = @inbounds prevind(s, i) end return nothing end