Skip to content

Commit

Permalink
special-case Base.rest for AbstractString (#38194)
Browse files Browse the repository at this point in the history
  • Loading branch information
simeonschaub authored Oct 30, 2020
1 parent 280b9ea commit 8d55e04
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
9 changes: 9 additions & 0 deletions base/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -711,3 +711,12 @@ julia> ascii("abcdefgh")
```
"""
ascii(x::AbstractString) = ascii(String(x))

Base.rest(s::Union{String,SubString{String}}, i=1) = SubString(s, i)
function Base.rest(s::AbstractString, st...)
io = IOBuffer()
for c in Iterators.rest(s, st...)
print(io, c)
end
return String(take!(io))
end
6 changes: 4 additions & 2 deletions base/tuple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,10 @@ end
Base.rest(collection[, itr_state])
Generic function for taking the tail of `collection`, starting from a specific iteration
state `itr_state`. Return a `Tuple`, if `collection` itself is a `Tuple`, a `Vector`, if
`collection` is an `AbstractArray` and `Iterators.rest(collection[, itr_state])` otherwise.
state `itr_state`. Return a `Tuple`, if `collection` itself is a `Tuple`, a subtype of
`AbstractVector`, if `collection` is an `AbstractArray`, a subtype of `AbstractString`
if `collection` is an `AbstractString`, and an arbitrary iterator, falling back to
`Iterators.rest(collection[, itr_state])`, otherwise.
Can be overloaded for user-defined collection types to customize the behavior of slurping
in assignments, like `a, b... = collection`.
Expand Down
17 changes: 17 additions & 0 deletions test/strings/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -386,3 +386,20 @@ let testb() = b"0123"
@test_throws ErrorException b[4] = '4'
@test testb() == UInt8['0','1','2','3']
end

@testset "Base.rest" begin
s = "aβcd"
@test Base.rest(s) === SubString(s)
a, b, c... = s
@test c === SubString(s, 4)

s = SubString("aβcd", 2)
@test Base.rest(s) === SubString(s)
b, c... = s
@test c === SubString(s, 3)

s = GenericString("aβcd")
@test Base.rest(s) === "aβcd"
a, b, c... = s
@test c === "cd"
end

8 comments on commit 8d55e04

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

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

Executing the daily benchmark build, I will reply here when finished:

@nanosoldier runbenchmarks(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

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

Executing the daily package evaluation, I will reply here when finished:

@nanosoldier runtests(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

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

Your package evaluation job has completed - possible new issues were detected. A full report can be found here. cc @maleadt

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

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

Your benchmark job has completed - successfully executed benchmarks. A full report can be found here. cc @christopher-dG

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

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

Executing the daily benchmark build, I will reply here when finished:

@nanosoldier runbenchmarks(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

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

Executing the daily package evaluation, I will reply here when finished:

@nanosoldier runtests(ALL, isdaily = true)

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

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

Your package evaluation job has completed - possible issues were detected. A full report can be found here. cc @maleadt

@nanosoldier
Copy link
Collaborator

Choose a reason for hiding this comment

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

Your benchmark job has completed - successfully executed benchmarks. A full report can be found here. cc @christopher-dG

Please sign in to comment.