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

Make view of a string return a substring #35879

Merged
merged 7 commits into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from 6 commits
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
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ Standard library changes
------------------------
* The `nextprod` function now accepts tuples and other array types for its first argument ([#35791]).
* The function `isapprox(x,y)` now accepts the `norm` keyword argument also for numeric (i.e., non-array) arguments `x` and `y` ([#35883]).
* `view`, `@view`, and `@views` now work on `AbstractString`s, returning a `SubString` when appropriate ([#35879]).
* All `AbstractUnitRange{<:Integer}`s now work with `SubString`, `view`,`@view` and `@views` on strings ([#35879]).
oxinabox marked this conversation as resolved.
Show resolved Hide resolved

#### LinearAlgebra

Expand Down
6 changes: 5 additions & 1 deletion base/strings/substring.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ end

@propagate_inbounds SubString(s::T, i::Int, j::Int) where {T<:AbstractString} = SubString{T}(s, i, j)
@propagate_inbounds SubString(s::AbstractString, i::Integer, j::Integer=lastindex(s)) = SubString(s, Int(i), Int(j))
@propagate_inbounds SubString(s::AbstractString, r::UnitRange{<:Integer}) = SubString(s, first(r), last(r))
@propagate_inbounds SubString(s::AbstractString, r::AbstractUnitRange{<:Integer}) = SubString(s, first(r), last(r))

@propagate_inbounds function SubString(s::SubString, i::Int, j::Int)
@boundscheck i j && checkbounds(s, i:j)
Expand All @@ -47,6 +47,10 @@ end
SubString(s::AbstractString) = SubString(s, 1, lastindex(s))
SubString{T}(s::T) where {T<:AbstractString} = SubString{T}(s, 1, lastindex(s))

@propagate_inbounds view(s::AbstractString, r::AbstractUnitRange{<:Integer}) = SubString(s, r)
@propagate_inbounds maybeview(s::AbstractString, r::AbstractUnitRange{<:Integer}) = view(s, r)
@propagate_inbounds maybeview(s::AbstractString, args...) = getindex(s, args...)
oxinabox marked this conversation as resolved.
Show resolved Hide resolved

convert(::Type{SubString{S}}, s::AbstractString) where {S<:AbstractString} =
SubString(convert(S, s))
convert(::Type{T}, s::T) where {T<:SubString} = s
Expand Down
31 changes: 31 additions & 0 deletions test/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,37 @@ end
@test endswith(z)(z)
end

@testset "SubStrings and Views" begin
x = "abcdefg"
@testset "basic unit range" begin
@test SubString(x, 2:4) == "bcd"
@test view(x, 2:4) == "bcd"
@test view(x, 2:4) isa SubString
@test (@view x[4:end]) == "defg"
@test (@view x[4:end]) isa SubString
end

@testset "other AbstractUnitRanges" begin
@test SubString(x, Base.OneTo(3)) == "abc"
@test view(x, Base.OneTo(4)) == "abcd"
@test view(x, Base.OneTo(4)) isa SubString
end

@testset "views but not view" begin
# We don't (at present) make non-contiguous SubStrings with views
@test_throws MethodError (@view x[[1,3,5]])
@test (@views (x[[1,3,5]])) isa String

# We don't (at present) make single character SubStrings with views
@test_throws MethodError (@view x[3])
@test (@views (x[3])) isa Char

@test (@views (x[3], x[1:2], x[[1,4]])) isa Tuple{Char, SubString, String}
@test (@views (x[3], x[1:2], x[[1,4]])) == ('c', "ab", "ad")
end
end


@testset "filter specialization on String issue #32460" begin
@test filter(x -> x ['', 'Ï', 'z', 'ξ'],
GenericString("J'étais n작작é pour plaiÏre à toute âξme un peu fière")) ==
Expand Down