Skip to content

Commit

Permalink
Merge pull request #16648 from TotalVerb/fast-utf8string
Browse files Browse the repository at this point in the history
Specialize nextind and prevind for String
  • Loading branch information
JeffBezanson committed Jun 8, 2016
2 parents 615f7ca + 9c47bd2 commit 11e4031
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 11e4031

Please sign in to comment.