Skip to content

Commit a7efcbb

Browse files
committed
buffered writedlm. print substring calls write substring. fixes #3483
faster utf8 readdlm
1 parent ddd9f12 commit a7efcbb

File tree

2 files changed

+17
-17
lines changed

2 files changed

+17
-17
lines changed

base/datafmt.jl

+16-17
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ function dlm_fill{T}(cells::Array{T,2}, offsets::Array{Int,2}, sbuff::String, au
109109

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

133133

134134
function dlm_offsets(sbuff::UTF8String, dlm, eol, offsets::Array{Int,2})
135+
isascii(dlm) && isascii(eol) && (return dlm_offsets(sbuff.data, uint8(dlm), uint8(eol), offsets))
136+
135137
col = 0
136138
row = 1
137139
maxrow,maxcol = size(offsets)
@@ -145,10 +147,9 @@ function dlm_offsets(sbuff::UTF8String, dlm, eol, offsets::Array{Int,2})
145147
(val == eol) && (row += 1; col = 0)
146148
end
147149
end
148-
function dlm_offsets(sbuff::ASCIIString, dlmc, eolc, offsets::Array{Int,2})
149-
dbuff = sbuff.data
150-
dlm = uint8(dlmc)
151-
eol = uint8(eolc)
150+
151+
dlm_offsets(sbuff::ASCIIString, dlmc, eolc, offsets::Array{Int,2}) = dlm_offsets(sbuff.data, uint8(dlmc), uint8(eolc), offsets)
152+
function dlm_offsets(dbuff::Vector{Uint8}, dlm::Uint8, eol::Uint8, offsets::Array{Int,2})
152153
col = 0
153154
row = 1
154155
maxrow,maxcol = size(offsets)
@@ -162,8 +163,9 @@ function dlm_offsets(sbuff::ASCIIString, dlmc, eolc, offsets::Array{Int,2})
162163
end
163164
end
164165

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

186188
# todo: keyword argument for # of digits to print
189+
writedlm_cell{T<:FloatingPoint}(io::IO, elt::T) = print_shortest(io, elt)
190+
writedlm_cell(io::IO, elt) = print(io, elt)
187191
function writedlm(io::IO, a::Matrix, dlm::Char)
192+
pb = PipeBuffer()
188193
nr, nc = size(a)
189194
for i = 1:nr
190195
for j = 1:nc
191-
elt = a[i,j]
192-
if isa(elt,FloatingPoint)
193-
print_shortest(io, elt)
194-
else
195-
print(io, elt)
196-
end
197-
if j < nc
198-
write(io, dlm)
199-
end
196+
writedlm_cell(pb, a[i,j])
197+
write(pb, (j == nc) ? '\n' : dlm)
200198
end
201-
write(io, '\n')
199+
(nb_available(pb) > (16*1024)) && write(io, takebuf_array(pb))
202200
end
201+
write(io, takebuf_array(pb))
203202
nothing
204203
end
205204

base/string.jl

+1
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,7 @@ SubString(s::String, i::Integer) = SubString(s, i, endof(s))
394394

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

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

0 commit comments

Comments
 (0)