-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add reverseind(s, i): convert indices in reverse(s) to indices in s #9249
Conversation
So far, the only example outside of |
Wouldn't it make sense to have a function which would reverse the string, look for matches, and return the indices in the original string directly? Not saying |
(Travis failure looks unrelated.) |
@nalimilan, that could make sense, but seems like it should be a separate PR (that would use (It could even just be an |
e35a404
to
8a157bb
Compare
Again, the Travis error (on Linux) seems unrelated:
|
Worker 9 was on bitarray, that's #9176 |
@StefanKarpinski, ok to merge (after rebase)? |
Seems good to me. |
add reverseind(s, i): convert indices in reverse(s) to indices in s
[Not sure if disallowed to comment on "Merged" as a "closed" issue..] Just looking at this it seems "return the corresponding index" means (and must mean?) "return the corresponding byte index". Maybe that needs to be explicit.. I want to get rid of any kind of indexing (as a default). Seems this is one function in my way.. Any good way to know how many functions depend on the implementation (byte-indexing)? I guess, I would have too take a look at any function using UFT8String. I understand any function can access private data - and that is "rude", but I guess that it is the rule in Base and an exception to "rude"-rule.. Hopefully this function is not speed-critical, as it seems I would want to let it return code-point indexes eventually.. |
It returns whatever index is appropriate for the given string type. This won't change if indexes are changed to opaque values. |
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 and that ASCII is encoded by a single code unit in all of our encodings) or slowchr2ind(str, length(str)-ind2chr(rstr, last(r))+1)
(which is linear time inlength(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