Skip to content

Commit

Permalink
safer indexing for AbstractArray (#540)
Browse files Browse the repository at this point in the history
* safer indexing for AbstractArray

* more eachindex

* eachindex method
  • Loading branch information
palday authored Jul 24, 2024
1 parent b6f4f19 commit 73e0a64
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 9 deletions.
2 changes: 1 addition & 1 deletion src/convert/axisarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ function rcopy(::Type{AxisArray}, r::Ptr{S}) where {S<:VectorSxp}
dnames = getattrib(r, Const.DimNamesSymbol)
isnull(dnames) && error("r has no dimnames")
dsym = rcopy(Array{Symbol}, getnames(dnames))
for i in 1:length(dsym)
for i in eachindex(dsym)
if dsym[i] == Symbol("")
dsym[i] = i == 1 ? (:row) : i == 2 ? (:col) : i == 3 ? (:page) : Symbol(:dim_, i)
end
Expand Down
20 changes: 12 additions & 8 deletions src/convert/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,15 @@ end
function rcopy(::Type{Vector{Bool}},s::Ptr{LglSxp})
a = Array{Bool}(undef, length(s))
v = unsafe_vec(s)
for i = 1:length(a)
for i in eachindex(a, v)
a[i] = v[i] != 0
end
a
end
function rcopy(::Type{BitVector},s::Ptr{LglSxp})
a = BitArray(undef, length(s))
v = unsafe_vec(s)
for i = 1:length(a)
for i in eachindex(a, v)
a[i] = v[i] != 0
end
a
Expand All @@ -135,15 +135,15 @@ end
function rcopy(::Type{Array{Bool}},s::Ptr{LglSxp})
a = Array{Bool}(undef, size(s)...)
v = unsafe_vec(s)
for i = 1:length(a)
for i in eachindex(a, v)
a[i] = v[i] != 0
end
a
end
function rcopy(::Type{BitArray},s::Ptr{LglSxp})
a = BitArray(undef, size(s)...)
v = unsafe_vec(s)
for i = 1:length(a)
for i in eachindex(a, v)
a[i] = v[i] != 0
end
a
Expand Down Expand Up @@ -264,8 +264,10 @@ sexp(::Type{RClass{:character}},st::AbstractString) = sexp(RClass{:character}, s
function sexp(::Type{RClass{:character}}, a::AbstractArray{T}) where T<:AbstractString
ra = protect(allocArray(StrSxp, size(a)...))
try
for i in 1:length(a)
ra[i] = a[i]
# we want this to work even if a doesn't use one-based indexing
# we only care about ra having the same length (which it does)
for (i, idx) in zip(eachindex(ra), eachindex(a))
ra[i] = a[idx]
end
finally
unprotect(1)
Expand Down Expand Up @@ -296,8 +298,10 @@ end
function sexp(::Type{RClass{:list}}, a::AbstractArray)
ra = protect(allocArray(VecSxp, size(a)...))
try
for i in 1:length(a)
ra[i] = a[i]
# we want this to work even if a doesn't use one-based indexing
# we only care about ra having the same length (which it does)
for (i, idx) in zip(eachindex(ra), eachindex(a))
ra[i] = a[idx]
end
finally
unprotect(1)
Expand Down
1 change: 1 addition & 0 deletions src/methods.jl
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ function iterate(s::Ptr{S}, state) where S<:VectorSxp
(s[state], state)
end

Base.eachindex(s::Ptr{<:VectorSxp}) = Base.OneTo(length(s))

"""
Set element of a VectorSxp by a label.
Expand Down

0 comments on commit 73e0a64

Please sign in to comment.