-
-
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
Allow nchar to be greater than string length in first/last #24556
Changes from 1 commit
1272653
1792389
93f56db
6d6de2a
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 |
---|---|---|
|
@@ -595,7 +595,7 @@ end | |
""" | ||
first(str::AbstractString, nchar::Integer) | ||
|
||
Get a string consisting of the first `nchar` characters of `str`. | ||
Get a string consisting of at most the first `nchar` characters of `str`. | ||
|
||
```jldoctest | ||
julia> first("∀ϵ≠0: ϵ²>0", 0) | ||
|
@@ -606,19 +606,24 @@ julia> first("∀ϵ≠0: ϵ²>0", 1) | |
|
||
julia> first("∀ϵ≠0: ϵ²>0", 3) | ||
"∀ϵ≠" | ||
|
||
julia> first("1234", 10) | ||
"1234" | ||
``` | ||
""" | ||
function first(str::AbstractString, nchar::Integer) | ||
s = start(str) | ||
if 0 <= nchar <= 1 | ||
return str[1:nchar] | ||
return str[s:(s-1+nchar)] | ||
end | ||
str[1:nextind(str, 1, nchar-1)] | ||
idx = min(endof(str), nextind(str, s, nchar-1)) | ||
str[s:idx] | ||
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. I guess you can use 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. I am not sure that The optimization would be to write a specialized method for Any thoughts on this? 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. OK. I guess leave it that way for now if the performance impact isn't too large. |
||
end | ||
|
||
""" | ||
last(str::AbstractString, nchar::Integer) | ||
|
||
Get a string consisting of the last `nchar` characters of `str`. | ||
Get a string consisting of at most the last `nchar` characters of `str`. | ||
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. Similar comment as above. 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. fixed |
||
|
||
```jldoctest | ||
julia> last("∀ϵ≠0: ϵ²>0", 0) | ||
|
@@ -629,14 +634,18 @@ julia> last("∀ϵ≠0: ϵ²>0", 1) | |
|
||
julia> last("∀ϵ≠0: ϵ²>0", 3) | ||
"²>0" | ||
|
||
julia> last("1234", 10) | ||
"1234" | ||
``` | ||
""" | ||
function last(str::AbstractString, nchar::Integer) | ||
e = endof(str) | ||
if 0 <= nchar <= 1 | ||
return str[(e-nchar+1):e] | ||
end | ||
str[prevind(str, e, nchar-1):e] | ||
idx = max(start(str), prevind(str, e, nchar-1)) | ||
str[idx:e] | ||
end | ||
|
||
# reverse-order iteration for strings and indices thereof | ||
|
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 wording makes it sound like the function may just randomly decide to give you any number of characters up to the number you asked for. It would be clearer to leave this sentence as is but add:
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.
fixed