JuliaLang/julia — pull request #9249 by stevengj (2014-12-04 17:29:44+00:00)
Title: add reverseind(s, i): convert indices in reverse(s) to indices in s

The main use of `reverse(s::String)` appears to be regex searching backwards in a string (see the discussion in #6165), but it is insanely hard to correctly convert the resulting indices (of matches in the reversed string) back to indices in the original string (see e.g. #9227). `reverseind(s, i)` solves this problem: `reverse(s)[i] == s[reverseind(s, i)]`.

For example, in REPLCompletions this replaces code that is either tricky (`nextind(partial, endof(partial)) - m.offset`, which relies on the fact that the match is ASCII) or slow `chr2ind(str, length(str)-ind2chr(rstr, last(r))+1)` (which is linear time in `length(str)`).

(It might be useful to check whether reversed strings are being searched in other packages, and whether the indices are handled correctly.)

cc: @dhoegh, @jiahao