Skip to content
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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 14 additions & 5 deletions base/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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`.
Copy link
Member

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:

unless str is shorter than nchar characters, in which case a string equal to str is returned.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


```jldoctest
julia> first("∀ϵ≠0: ϵ²>0", 0)
Expand All @@ -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]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess you can use @inbounds here now? That would (or could, if @boundscheck is implemented where needed) offset the cost of callind endof.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure that @inbounds will help (but please correct me if I am wrong or I have misunderstood something what you have meant), because neither
getindex(s::String, r::UnitRange{Int}) nor getindex(s::AbstractString, r::UnitRange{Int})
call endof (they work differently, but perform the checks without calling endof and what they do does not seem to me to be influenced by @inbounds (but maybe I am wrong).

The optimization would be to write a specialized method for first for String only which would call unsafe_string instead of getindex. It would be a bit faster, but I do not know if we want to have such a messy code (the same could be done for last).

Any thoughts on this?

Copy link
Member

Choose a reason for hiding this comment

The 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`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Similar comment as above.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


```jldoctest
julia> last("∀ϵ≠0: ϵ²>0", 0)
Expand All @@ -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
Expand Down
35 changes: 18 additions & 17 deletions test/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -637,23 +637,24 @@ end
end

@testset "first and last" begin
s = "∀ϵ≠0: ϵ²>0"
@test_throws ArgumentError first(s, -1)
@test first(s, 0) == ""
@test first(s, 1) == "∀"
@test first(s, 2) == "∀ϵ"
@test first(s, 3) == "∀ϵ≠"
@test first(s, 4) == "∀ϵ≠0"
@test first(s, length(s)) == s
@test_throws BoundsError first(s, length(s)+1)
@test_throws ArgumentError last(s, -1)
@test last(s, 0) == ""
@test last(s, 1) == "0"
@test last(s, 2) == ">0"
@test last(s, 3) == "²>0"
@test last(s, 4) == "ϵ²>0"
@test last(s, length(s)) == s
@test_throws BoundsError last(s, length(s)+1)
let s = "∀ϵ≠0: ϵ²>0"
@test_throws ArgumentError first(s, -1)
@test first(s, 0) == ""
@test first(s, 1) == "∀"
@test first(s, 2) == "∀ϵ"
@test first(s, 3) == "∀ϵ≠"
@test first(s, 4) == "∀ϵ≠0"
@test first(s, length(s)) == s
@test first(s, length(s)+1) == s
@test_throws ArgumentError last(s, -1)
@test last(s, 0) == ""
@test last(s, 1) == "0"
@test last(s, 2) == ">0"
@test last(s, 3) == "²>0"
@test last(s, 4) == "ϵ²>0"
@test last(s, length(s)) == s
@test last(s, length(s)+1) == s
end
end

@testset "invalid code point" begin
Expand Down