Skip to content

Commit

Permalink
buffered writedlm. print substring calls write substring. fixes #3483
Browse files Browse the repository at this point in the history
faster utf8 readdlm
  • Loading branch information
tanmaykm committed Jun 22, 2013
1 parent ddd9f12 commit a7efcbb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 17 deletions.
33 changes: 16 additions & 17 deletions base/datafmt.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ function dlm_fill{T}(cells::Array{T,2}, offsets::Array{Int,2}, sbuff::String, au

if T <: Char
(length(sval) != 1) && error("file entry \"$(sval)\" is not a Char")
cells[cell_row,col] = sval
cells[cell_row,col] = next(sval,1)[1]
elseif T <: Number
if(float64_isvalid(sval, tmp64))
cells[cell_row,col] = tmp64[1]
Expand All @@ -132,6 +132,8 @@ end


function dlm_offsets(sbuff::UTF8String, dlm, eol, offsets::Array{Int,2})
isascii(dlm) && isascii(eol) && (return dlm_offsets(sbuff.data, uint8(dlm), uint8(eol), offsets))

col = 0
row = 1
maxrow,maxcol = size(offsets)
Expand All @@ -145,10 +147,9 @@ function dlm_offsets(sbuff::UTF8String, dlm, eol, offsets::Array{Int,2})
(val == eol) && (row += 1; col = 0)
end
end
function dlm_offsets(sbuff::ASCIIString, dlmc, eolc, offsets::Array{Int,2})
dbuff = sbuff.data
dlm = uint8(dlmc)
eol = uint8(eolc)

dlm_offsets(sbuff::ASCIIString, dlmc, eolc, offsets::Array{Int,2}) = dlm_offsets(sbuff.data, uint8(dlmc), uint8(eolc), offsets)
function dlm_offsets(dbuff::Vector{Uint8}, dlm::Uint8, eol::Uint8, offsets::Array{Int,2})
col = 0
row = 1
maxrow,maxcol = size(offsets)
Expand All @@ -162,8 +163,9 @@ function dlm_offsets(sbuff::ASCIIString, dlmc, eolc, offsets::Array{Int,2})
end
end

dlm_dims(s::ASCIIString, eol, dlm) = dlm_dims(s.data, uint8(eol), uint8(dlm))
function dlm_dims(dbuff, eol, dlm)
dlm_dims(s::ASCIIString, eol::Char, dlm::Char) = dlm_dims(s.data, uint8(eol), uint8(dlm))
function dlm_dims{T,D}(dbuff::T, eol::D, dlm::D)
isa(dbuff, UTF8String) && isascii(eol) && isascii(dlm) && (return dlm_dims(dbuff.data, uint8(eol), uint8(dlm)))
ncols = nrows = col = 0
try
for val in dbuff
Expand All @@ -184,22 +186,19 @@ readcsv(io; opts...) = readdlm(io, ','; opts...)
readcsv(io, T::Type; opts...) = readdlm(io, ',', T; opts...)

# todo: keyword argument for # of digits to print
writedlm_cell{T<:FloatingPoint}(io::IO, elt::T) = print_shortest(io, elt)
writedlm_cell(io::IO, elt) = print(io, elt)
function writedlm(io::IO, a::Matrix, dlm::Char)
pb = PipeBuffer()
nr, nc = size(a)
for i = 1:nr
for j = 1:nc
elt = a[i,j]
if isa(elt,FloatingPoint)
print_shortest(io, elt)
else
print(io, elt)
end
if j < nc
write(io, dlm)
end
writedlm_cell(pb, a[i,j])
write(pb, (j == nc) ? '\n' : dlm)
end
write(io, '\n')
(nb_available(pb) > (16*1024)) && write(io, takebuf_array(pb))
end
write(io, takebuf_array(pb))
nothing
end

Expand Down
1 change: 1 addition & 0 deletions base/string.jl
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ SubString(s::String, i::Integer) = SubString(s, i, endof(s))

write{T<:ByteString}(to::IOBuffer, s::SubString{T}) =
s.endof==0 ? 0 : write_sub(to, s.string.data, s.offset+1, next(s,s.endof)[2]-1)
print(io::IOBuffer, s::SubString) = write(io, s)

sizeof{T<:ByteString}(s::SubString{T}) = s.endof==0 ? 0 : next(s,s.endof)[2]-1

Expand Down

0 comments on commit a7efcbb

Please sign in to comment.