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 4 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
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ 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]).

#### LinearAlgebra

Expand Down
4 changes: 4 additions & 0 deletions base/strings/substring.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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::UnitRange{<:Integer}) = SubString(s, r)
@propagate_inbounds maybeview(s::AbstractString, r::UnitRange{<:Integer}) = view(s, r)
oxinabox marked this conversation as resolved.
Show resolved Hide resolved
@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
20 changes: 20 additions & 0 deletions test/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,26 @@ end
@test endswith(z)(z)
end

@testset "SubStrings and Views" begin
x = "abcdefg"
@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

# 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}
oxinabox marked this conversation as resolved.
Show resolved Hide resolved
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