Skip to content

Commit 73204b7

Browse files
committed
add readfloatorNA!(io, vec::AbstractVector)
allows to fix the result of readcomplex() from ReinterpretArray to Vector on 0.7
1 parent f8a8b9b commit 73204b7

File tree

3 files changed

+16
-9
lines changed

3 files changed

+16
-9
lines changed

src/io/ASCIIIO.jl

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,21 @@ readintorNA(io::ASCIIIO, n::RVecLength) = Int32[readintorNA(io) for i in 1:n]
2424
# str == R_NA_STRING ? R_NA_FLOAT64 : parse(Float64, str)
2525
#end
2626

27-
function readfloatorNA(io::ASCIIIO, n::RVecLength)
28-
res = Vector{Float64}(n)
29-
res_uint = reinterpret(UInt64, res) # alias of res for setting NA
30-
@inbounds for i in 1:n
27+
function readfloatorNA!(io::ASCIIIO, v::AbstractVector{Float64})
28+
v_uint = reinterpret(UInt64, v) # alias of res for setting NA
29+
@inbounds for i in eachindex(v)
3130
str = chomp(readline(io.sub))
3231
if str != R_NA_STRING
33-
res[i] = parse(Float64, str)
32+
v[i] = parse(Float64, str)
3433
else
35-
res_uint[i] = R_NA_FLOAT64 # see JuliaStats/RData.jl#5
34+
v_uint[i] = R_NA_FLOAT64 # see JuliaStats/RData.jl#5
3635
end
3736
end
38-
res
37+
v
3938
end
4039

40+
readfloatorNA(io::ASCIIIO, n::RVecLength) = readfloatorNA!(io, Vector{Float64}(n))
41+
4142
readuint8(io::ASCIIIO, n::RVecLength) = UInt8[hex2bytes(chomp(readline(io.sub)))[1] for i in 1:n] # FIXME optimize for speed
4243

4344
function readnchars(io::ASCIIIO, n::Int32) # reads N bytes-sized string

src/io/XDRIO.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ function readfloatorNA(io::XDRIO, n::RVecLength)
2626
map!(ntoh, v, v)
2727
end
2828

29+
function readfloatorNA!(io::XDRIO, v::AbstractVector{Float64})
30+
readbytes!(io.sub, reinterpret(UInt8, v))
31+
map!(ntoh, v, v)
32+
end
33+
2934
readuint8(io::XDRIO, n::RVecLength) = read(io.sub, UInt8, n)
3035

3136
function readnchars(io::XDRIO, n::Int32) # a single character string

src/readers.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ end
3838
function readcomplex(ctx::RDAContext, fl::RDATag)
3939
@assert sxtype(fl) == CPLXSXP
4040
n = readlength(ctx.io)
41-
RComplexVector(reinterpret(Complex128, readfloatorNA(ctx.io, 2n)),
42-
readattrs(ctx, fl))
41+
v = Vector{Complex128}(n)
42+
readfloatorNA!(ctx.io, reinterpret(Float64, v))
43+
RComplexVector(v, readattrs(ctx, fl))
4344
end
4445

4546
function readstring(ctx::RDAContext, fl::RDATag)

0 commit comments

Comments
 (0)