Skip to content

Commit

Permalink
add readfloatorNA!(io, vec::AbstractVector)
Browse files Browse the repository at this point in the history
allows to fix the result of readcomplex() from ReinterpretArray to
Vector on 0.7
  • Loading branch information
alyst committed Nov 26, 2017
1 parent f8a8b9b commit 73204b7
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 9 deletions.
15 changes: 8 additions & 7 deletions src/io/ASCIIIO.jl
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,21 @@ readintorNA(io::ASCIIIO, n::RVecLength) = Int32[readintorNA(io) for i in 1:n]
# str == R_NA_STRING ? R_NA_FLOAT64 : parse(Float64, str)
#end

function readfloatorNA(io::ASCIIIO, n::RVecLength)
res = Vector{Float64}(n)
res_uint = reinterpret(UInt64, res) # alias of res for setting NA
@inbounds for i in 1:n
function readfloatorNA!(io::ASCIIIO, v::AbstractVector{Float64})
v_uint = reinterpret(UInt64, v) # alias of res for setting NA
@inbounds for i in eachindex(v)
str = chomp(readline(io.sub))
if str != R_NA_STRING
res[i] = parse(Float64, str)
v[i] = parse(Float64, str)
else
res_uint[i] = R_NA_FLOAT64 # see JuliaStats/RData.jl#5
v_uint[i] = R_NA_FLOAT64 # see JuliaStats/RData.jl#5
end
end
res
v
end

readfloatorNA(io::ASCIIIO, n::RVecLength) = readfloatorNA!(io, Vector{Float64}(n))

readuint8(io::ASCIIIO, n::RVecLength) = UInt8[hex2bytes(chomp(readline(io.sub)))[1] for i in 1:n] # FIXME optimize for speed

function readnchars(io::ASCIIIO, n::Int32) # reads N bytes-sized string
Expand Down
5 changes: 5 additions & 0 deletions src/io/XDRIO.jl
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ function readfloatorNA(io::XDRIO, n::RVecLength)
map!(ntoh, v, v)
end

function readfloatorNA!(io::XDRIO, v::AbstractVector{Float64})
readbytes!(io.sub, reinterpret(UInt8, v))
map!(ntoh, v, v)
end

readuint8(io::XDRIO, n::RVecLength) = read(io.sub, UInt8, n)

function readnchars(io::XDRIO, n::Int32) # a single character string
Expand Down
5 changes: 3 additions & 2 deletions src/readers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,9 @@ end
function readcomplex(ctx::RDAContext, fl::RDATag)
@assert sxtype(fl) == CPLXSXP
n = readlength(ctx.io)
RComplexVector(reinterpret(Complex128, readfloatorNA(ctx.io, 2n)),
readattrs(ctx, fl))
v = Vector{Complex128}(n)
readfloatorNA!(ctx.io, reinterpret(Float64, v))
RComplexVector(v, readattrs(ctx, fl))
end

function readstring(ctx::RDAContext, fl::RDATag)
Expand Down

0 comments on commit 73204b7

Please sign in to comment.