-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
checkbounds for strings #22548
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
Comments
I agree. I think the consensus is that indices should always point to the beginning of a codepoint. All other cases should be an error. See @stevengj's comments at #14158 (comment) and #14158 (comment), as well as the rest of the thread. I'm not sure why the PR was abandoned at the time, but a PR implementing the strict behavior would be welcome AFAICT. See also #5446. |
I agree with @nalimilan. Actually #22511 it would be simpler and more consistent to implement if lower and upper index would be required to point to the beginning of a codepoint. As this issue is revisited the additional question is if the following statements should throw an error or go through and return empty string: For invalid indices:
For valid indices but in empty range:
|
I'd use the same rules as for arrays. I just realized indexing an array with an empty range always returns an empty array, even if the range endpoints are completely out of bounds. That's kind of surprising to me, but better stick to that, and possibly change it later for both strings and arrays if we decide to. |
That is exactly why I have asked. I understand that the desired rules in
Then the following definition should work:
|
The semantics look correct, but this method is very performance-sensitive so we should probably stick to the current code as much as possible. AFAICT it should already throw an error when |
This is the redesigned code:
and here is the benchmark:
producing
so it seems to be roughly equivalent. |
And a small fix as
and corrected benchmark code:
giving
|
Looks good, that's certainly worth a PR. But I would keep
You can just use |
Make `getindex` for `String` check if indices are valid.
Closes JuliaLang#22548 fixes a bug with use of prevind in dates/io.jl
Currently
checkbounds
is inconsitent withgetindex
forString
.Example:
The consequence is that in particular in
SubString
we get inconsistent behavior ofgetindex
:I would recommend to make it consistent. The difference in current functionality for string
x
is:getindex
: allow indexing tosizeof(x)
;chcekbounds
allows indexing toendof(x)
.I do not have a strong preference of one over the other but I feel that this should be consistent.
The text was updated successfully, but these errors were encountered: