-
-
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
Make getindex for String check if indices are valid #22572
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -235,20 +235,25 @@ isvalid(s::String, i::Integer) = | |
|
||
function getindex(s::String, r::UnitRange{Int}) | ||
isempty(r) && return "" | ||
i, j = first(r), last(r) | ||
l = sizeof(s) | ||
i = first(r) | ||
if i < 1 || i > l | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Just a styling issue, but I don't see the point of changing these from |
||
throw(BoundsError(s, i)) | ||
end | ||
@inbounds si = codeunit(s, i) | ||
if is_valid_continuation(si) | ||
throw(UnicodeError(UTF_ERR_INVALID_INDEX, i, si)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Better continue throwing a |
||
end | ||
j = last(r) | ||
if j > l | ||
throw(BoundsError()) | ||
throw(BoundsError(s, j)) | ||
end | ||
j = nextind(s,j)-1 | ||
unsafe_string(pointer(s,i), j-i+1) | ||
@inbounds sj = codeunit(s, j) | ||
if is_valid_continuation(sj) | ||
throw(UnicodeError(UTF_ERR_INVALID_INDEX, j, sj)) | ||
end | ||
j = nextind(s,j) | ||
unsafe_string(pointer(s,i), j-i) | ||
end | ||
|
||
function search(s::String, c::Char, i::Integer = 1) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should have been OK, since we know the three last characters are ASCII.
-2
has no reason to be more correct than-3
, right? Same below.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately not:
I can use
end-2
exactly because I know that last three characters are ASCII.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, carry on, I need to learn how to count up to three. :-)