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

Indexing arrays with AbstractUnitRanges retains indices #40660

Merged
merged 1 commit into from
Apr 29, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -838,9 +838,9 @@ function getindex(A::Array, I::AbstractUnitRange{<:Integer})
@_inline_meta
@boundscheck checkbounds(A, I)
lI = length(I)
X = similar(A, lI)
X = similar(A, axes(I))
if lI > 0
unsafe_copyto!(X, 1, A, first(I), lI)
copyto!(X, firstindex(X), A, first(I), lI)
end
return X
end
Expand Down
10 changes: 10 additions & 0 deletions test/offsetarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -776,3 +776,13 @@ end
strY = String(take!(io))
@test strX == strY
end

@testset "vector indexing (issue #39896)" begin
a = collect(1:10)
r = Base.IdentityUnitRange(2:3)
b = a[r]
@test axes(b) == axes(r)
for i in r
@test b[i] == a[r[i]]
end
end