diff --git a/src/convert/axisarray.jl b/src/convert/axisarray.jl index cb147c5e..bc7e9f5e 100644 --- a/src/convert/axisarray.jl +++ b/src/convert/axisarray.jl @@ -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 diff --git a/src/convert/base.jl b/src/convert/base.jl index 2463cd4f..b14375a3 100644 --- a/src/convert/base.jl +++ b/src/convert/base.jl @@ -114,7 +114,7 @@ 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 @@ -122,7 +122,7 @@ 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 @@ -135,7 +135,7 @@ 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 @@ -143,7 +143,7 @@ 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 @@ -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) @@ -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) diff --git a/src/methods.jl b/src/methods.jl index 2b5fbfab..6f3cbe17 100644 --- a/src/methods.jl +++ b/src/methods.jl @@ -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.