Skip to content

Commit

Permalink
correct nextind/prevind/thisind for SubString{String}
Browse files Browse the repository at this point in the history
  • Loading branch information
bkamins committed Jan 12, 2018
1 parent 4440b0f commit 3a0a01b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 24 deletions.
6 changes: 3 additions & 3 deletions base/strings/string.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand All @@ -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))
Expand Down
21 changes: 0 additions & 21 deletions base/strings/substring.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
19 changes: 19 additions & 0 deletions test/strings/types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3a0a01b

Please sign in to comment.