Skip to content

Commit

Permalink
Merge pull request JuliaLang#13989 from stevengj/writedlm_itr
Browse files Browse the repository at this point in the history
fix writedlm for vectors of iterable rows
  • Loading branch information
tanmaykm committed Nov 15, 2015
2 parents a1a7f7b + f117461 commit 48da32e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 10 deletions.
33 changes: 24 additions & 9 deletions base/datafmt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -563,7 +563,7 @@ function writedlm_cell{T}(io::IO, elt::AbstractString, dlm::T, quotes::Bool)
end
end
writedlm_cell(io::IO, elt, dlm, quotes) = print(io, elt)
function writedlm(io::IO, a::AbstractVecOrMat, dlm; opts...)
function writedlm(io::IO, a::AbstractMatrix, dlm; opts...)
optsd = val_opts(opts)
quotes = get(optsd, :quotes, true)
pb = PipeBuffer()
Expand All @@ -582,17 +582,32 @@ end

writedlm{T}(io::IO, a::AbstractArray{T,0}, dlm; opts...) = writedlm(io, reshape(a,1), dlm; opts...)

# write an iterable row as dlm-separated items
function writedlm_row(io::IO, row, dlm, quotes)
state = start(row)
while !done(row, state)
(x, state) = next(row, state)
writedlm_cell(io, x, dlm, quotes)
done(row, state) ? write(io,'\n') : print(io,dlm)
end
end

# If the row is a single string, write it as a string rather than
# iterating over characters. Also, include the common case of
# a Number (handled correctly by the generic writedlm_row above)
# purely as an optimization.
function writedlm_row(io::IO, row::Union{Number,AbstractString}, dlm, quotes)
writedlm_cell(io, row, dlm, quotes)
write(io, '\n')
end

# write an iterable collection of iterable rows
function writedlm(io::IO, itr, dlm; opts...)
optsd = val_opts(opts)
quotes = get(optsd, :quotes, true)
pb = PipeBuffer()
for row in itr
state = start(row)
while !done(row, state)
(x, state) = next(row, state)
writedlm_cell(pb, x, dlm, quotes)
done(row, state) ? write(pb,'\n') : print(pb,dlm)
end
writedlm_row(pb, row, dlm, quotes)
(nb_available(pb) > (16*1024)) && write(io, takebuf_array(pb))
end
write(io, takebuf_array(pb))
Expand All @@ -608,7 +623,7 @@ end
writedlm(io, a; opts...) = writedlm(io, a, '\t'; opts...)
writecsv(io, a; opts...) = writedlm(io, a, ','; opts...)

writemime(io::IO, ::MIME"text/csv", a::AbstractVecOrMat) = writedlm(io, a, ',')
writemime(io::IO, ::MIME"text/tab-separated-values", a::AbstractVecOrMat) = writedlm(io, a, '\t')
writemime(io::IO, ::MIME"text/csv", a) = writedlm(io, a, ',')
writemime(io::IO, ::MIME"text/tab-separated-values", a) = writedlm(io, a, '\t')

end # module DataFmt
2 changes: 1 addition & 1 deletion test/choosetests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ function choosetests(choices = [])
"bigint", "sorting", "statistics", "spawn", "backtrace",
"priorityqueue", "file", "mmap", "version", "resolve",
"pollfd", "mpfr", "broadcast", "complex", "socket",
"floatapprox", "readdlm", "reflection", "regex", "float16",
"floatapprox", "datafmt", "reflection", "regex", "float16",
"combinatorics", "sysinfo", "rounding", "ranges", "mod2pi",
"euler", "show", "lineedit", "replcompletions", "repl",
"replutil", "sets", "test", "goto", "llvmcall", "grisu",
Expand Down
16 changes: 16 additions & 0 deletions test/readdlm.jl → test/datafmt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -237,3 +237,19 @@ end
# test writemime
@test sprint(io -> writemime(io, "text/csv", [1 2; 3 4])) == "1,2\n3,4\n"

for writefunc in ((io,x) -> writemime(io, "text/csv", x),
(io,x) -> invoke(writedlm, (IO, Any, Any), io, x, ","))
# iterable collections of iterable rows:
let x = [(1,2), (3,4)], io = IOBuffer()
writefunc(io, x)
seek(io, 0)
@test readcsv(io) == [1 2; 3 4]
end
# vectors of strings:
let x = ["foo", "bar"], io = IOBuffer()
writefunc(io, x)
seek(io, 0)
@test collect(readcsv(io)) == x
end
end

0 comments on commit 48da32e

Please sign in to comment.