-
-
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
first and last with nchar #23960
first and last with nchar #23960
Changes from 4 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 |
---|---|---|
|
@@ -610,3 +610,50 @@ function filter(f, s::AbstractString) | |
end | ||
String(take!(out)) | ||
end | ||
|
||
## string first and last ## | ||
|
||
""" | ||
first(str::AbstractString, nchar::Integer) | ||
|
||
Get a string consisting of the first `nchar` characters of `str`. | ||
|
||
```jldoctest | ||
julia> first("∀ϵ≠0: ϵ²>0", 0) | ||
"" | ||
|
||
julia> first("∀ϵ≠0: ϵ²>0", 1) | ||
"∀" | ||
|
||
julia> first("∀ϵ≠0: ϵ²>0", 3) | ||
"∀ϵ≠" | ||
``` | ||
""" | ||
function first(str::AbstractString, nchar::Integer) | ||
nchar == 0 && return "" | ||
nchar == 1 && return str[1:1] | ||
str[1:nextind(str, 1, nchar-1)] | ||
end | ||
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. Should it return a 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. My recommendation in #23765 is that not. Usually |
||
|
||
""" | ||
last(str::AbstractString, nchar::Integer) | ||
|
||
Get a string consisting of the last `nchar` characters of `str`. | ||
|
||
```jldoctest | ||
julia> last("∀ϵ≠0: ϵ²>0", 0) | ||
"" | ||
|
||
julia> last("∀ϵ≠0: ϵ²>0", 1) | ||
"0" | ||
|
||
julia> last("∀ϵ≠0: ϵ²>0", 3) | ||
"²>0" | ||
``` | ||
""" | ||
function last(str::AbstractString, nchar::Integer) | ||
nchar == 0 && return "" | ||
e = endof(str) | ||
nchar == 1 && return str[e:e] | ||
str[prevind(str, e, nchar-1):e] | ||
end |
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 seems like it will give a
BoundsError
fornchar > length(str)
. Shouldn't it bemin(endof(str), nextind(str, 1, nchar-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.
Or is
BoundsError
intended here?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.
It was intended. The idea is to have an invariant
length(first(s, nchar)) == nchar
. Similarly forlast
.This might be changed the way you propose - then also docstring should be changed as it now promises
nchar
characters in the produced string.I slightly prefer the current implementation (as it ensures the invariant) but I can see the rationale behind your proposal so I am OK change it.
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.
If we changed it to "at most" nchar elements of the string, then it would be similar to
take
.My feeling is that using "at most" nchar elements would be a big more flexible, and it wouldn't hurt the uses where you want exactly nchar elements except that it wouldn't throw an error for a string of the wrong size.
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. I will make a PR to have a clear decision point.