Skip to content

Commit

Permalink
handle single character string views correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
oxinabox committed May 16, 2020
1 parent eba2f11 commit b3e1837
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion base/strings/substring.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ 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) = SubString(s, r)
@propagate_inbounds view(s::AbstractString, r::UnitRange{<:Integer}) = SubString(s, r)
@propagate_inbounds maybeview(s::AbstractString, r::UnitRange{<:Integer}) = view(s, r)
@propagate_inbounds maybeview(s::AbstractString, args...) = getindex(s, args...)

Expand Down
8 changes: 6 additions & 2 deletions test/strings/basic.jl
Original file line number Diff line number Diff line change
Expand Up @@ -175,10 +175,14 @@ end
@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]]) == "ace"
@test_throws MethodError (@view x[[1,3,5]])
@test (@views (x[[1,3,5]])) isa String

@test (@views (x[1], x[1:2], x[[1,4]])) isa Tuple{Char, SubString, 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}
end


Expand Down

0 comments on commit b3e1837

Please sign in to comment.