Skip to content

Commit

Permalink
indexing arrays with abstractunitranges retains indices (JuliaLang#40660
Browse files Browse the repository at this point in the history
)
  • Loading branch information
jishnub authored and jarlebring committed May 4, 2021
1 parent 186ab9c commit bd17e7c
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 2 deletions.
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

0 comments on commit bd17e7c

Please sign in to comment.