Skip to content

Commit

Permalink
Specialize nextind and prevind for String
Browse files Browse the repository at this point in the history
  • Loading branch information
TotalVerb committed Jun 1, 2016
1 parent 2fa728a commit 9c47bd2
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions base/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -135,17 +135,43 @@ end

## Generic indexing functions ##

prevind(s::DirectIndexString, i::Integer) = i-1
prevind(s::AbstractArray , i::Integer) = i-1
nextind(s::DirectIndexString, i::Integer) = i+1
nextind(s::AbstractArray , i::Integer) = i+1
prevind(s::DirectIndexString, i::Integer) = Int(i)-1
prevind(s::AbstractArray , i::Integer) = Int(i)-1
nextind(s::DirectIndexString, i::Integer) = Int(i)+1
nextind(s::AbstractArray , i::Integer) = Int(i)+1

function prevind(s::String, i::Integer)
j = Int(i)
e = endof(s.data)
if j > e
return endof(s)
end
j -= 1
while j > 0 && is_valid_continuation(s.data[j])
j -= 1
end
j
end

function nextind(s::String, i::Integer)
j = Int(i)
if j < 1
return 1
end
e = endof(s.data)
j += 1
while j <= e && is_valid_continuation(s.data[j])
j += 1
end
j
end

function prevind(s::AbstractString, i::Integer)
e = endof(s)
if i > e
return e
end
j = i-1
j = Int(i)-1
while j >= 1
if isvalid(s,j)
return j
Expand All @@ -161,9 +187,9 @@ function nextind(s::AbstractString, i::Integer)
return 1
end
if i > e
return i+1
return Int(i)+1
end
for j = i+1:e
for j = Int(i)+1:e
if isvalid(s,j)
return j
end
Expand Down

0 comments on commit 9c47bd2

Please sign in to comment.