diff --git a/base/strings/string.jl b/base/strings/string.jl index 30a0fb6d830650..727e9b5168216d 100644 --- a/base/strings/string.jl +++ b/base/strings/string.jl @@ -96,9 +96,9 @@ function ==(a::String, b::String) al == sizeof(b) && 0 == ccall(:memcmp, Int32, (Ptr{UInt8}, Ptr{UInt8}, UInt), a, b, al) end -## thisind, prevind, nextind ## +## thisind, nextind ## -function thisind(s::String, i::Int) +function thisind(s::Union{String, SubString{String}}, i::Int) n = ncodeunits(s) i == n + 1 && return i @boundscheck between(i, 0, n) || throw(BoundsError(s, i)) @@ -115,7 +115,7 @@ function thisind(s::String, i::Int) return i end -function nextind(s::String, i::Int) +function nextind(s::Union{String, SubString{String}}, i::Int) i == 0 && return 1 n = ncodeunits(s) @boundscheck between(i, 1, n) || throw(BoundsError(s, i)) diff --git a/base/strings/substring.jl b/base/strings/substring.jl index f4cf3c93115378..ce0e6f578ed2bb 100644 --- a/base/strings/substring.jl +++ b/base/strings/substring.jl @@ -78,27 +78,6 @@ function isvalid(s::SubString, i::Integer) @inbounds return ib && isvalid(s.string, s.offset + i) end -function thisind(s::SubString, i::Int) - @boundscheck 0 ≤ i ≤ ncodeunits(s)+1 || throw(BoundsError(s, i)) - @inbounds return thisind(s.string, s.offset + i) - s.offset -end -function nextind(s::SubString, i::Int, n::Int) - @boundscheck 0 ≤ i < ncodeunits(s)+1 || throw(BoundsError(s, i)) - @inbounds return nextind(s.string, s.offset + i, n) - s.offset -end -function nextind(s::SubString, i::Int) - @boundscheck 0 ≤ i < ncodeunits(s)+1 || throw(BoundsError(s, i)) - @inbounds return nextind(s.string, s.offset + i) - s.offset -end -function prevind(s::SubString, i::Int, n::Int) - @boundscheck 0 < i ≤ ncodeunits(s)+1 || throw(BoundsError(s, i)) - @inbounds return prevind(s.string, s.offset + i, n) - s.offset -end -function prevind(s::SubString, i::Int) - @boundscheck 0 < i ≤ ncodeunits(s)+1 || throw(BoundsError(s, i)) - @inbounds return prevind(s.string, s.offset + i) - s.offset -end - function cmp(a::SubString{String}, b::SubString{String}) na = sizeof(a) nb = sizeof(b) diff --git a/test/strings/types.jl b/test/strings/types.jl index 0eefac0549c3c0..ea4127d8b4c649 100644 --- a/test/strings/types.jl +++ b/test/strings/types.jl @@ -188,6 +188,25 @@ let s = "lorem ipsum", sdict = Dict( end end +# proper nextind/prevind/thisind for SubString{String} +let s = "∀∃∀", ss = SubString(s, 4, 4), s2 = s[4:4] + for i in 0:4 + @test thisind(ss, i) == thisind(s2, i) + end + for i in 0:3 + @test nextind(ss, i) == nextind(s2, i) + for j in 0:10 + @test nextind(ss, i, j) == nextind(s2, i, j) + end + end + for i in 1:4 + @test prevind(ss, i) == prevind(s2, i) + for j in 0:10 + @test prevind(ss, i, j) == prevind(s2, i, j) + end + end +end + # for isvalid(SubString{String}) let s = "Σx + βz - 2" for i in -1:ncodeunits(s)+2