Skip to content

Commit

Permalink
safer indexing for AbstractArray
Browse files Browse the repository at this point in the history
  • Loading branch information
palday committed Jul 19, 2024
1 parent 61b6ecb commit f7aa5f3
Show file tree
Hide file tree
Showing 2 changed files with 9 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)

Check warning on line 7 in src/convert/axisarray.jl

View check run for this annotation

Codecov / codecov/patch

src/convert/axisarray.jl#L7

Added line #L7 was not covered by tests
if dsym[i] == Symbol("")
dsym[i] = i == 1 ? (:row) : i == 2 ? (:col) : i == 3 ? (:page) : Symbol(:dim_, i)
end
Expand Down
16 changes: 8 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,8 @@ 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]
for (i, idx) in enumerate(eachindex(a))
ra[i] = a[idx]
end
finally
unprotect(1)
Expand Down Expand Up @@ -296,8 +296,8 @@ 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]
for (i, idx) in enumerate(eachindex(a))
ra[i] = a[idx]
end
finally
unprotect(1)
Expand Down

0 comments on commit f7aa5f3

Please sign in to comment.