Skip to content

Commit

Permalink
avoid a copy in readuntil (JuliaLang#19946)
Browse files Browse the repository at this point in the history
avoid a copy in readuntil
  • Loading branch information
stevengj committed Jan 21, 2017
1 parent 17b9724 commit b07b6dd
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
6 changes: 5 additions & 1 deletion base/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -390,9 +390,13 @@ function read(s::IO, ::Type{Char})
return Char(c)
end

# readuntil_string is useful below since it has
# an optimized method for s::IOStream
readuntil_string(s::IO, delim::UInt8) = String(readuntil(s, delim))

function readuntil(s::IO, delim::Char)
if delim < Char(0x80)
return String(readuntil(s, delim % UInt8))
return readuntil_string(s, delim % UInt8)
end
out = IOBuffer()
while !eof(s)
Expand Down
5 changes: 5 additions & 0 deletions base/iostream.jl
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,11 @@ function readuntil(s::IOStream, delim::UInt8)
ccall(:jl_readuntil, Array{UInt8,1}, (Ptr{Void}, UInt8, UInt8), s.ios, delim, 0)
end

# like readuntil, above, but returns a String without requiring a copy
function readuntil_string(s::IOStream, delim::UInt8)
ccall(:jl_readuntil, Ref{String}, (Ptr{Void}, UInt8, UInt8), s.ios, delim, 1)
end

function readline(s::IOStream)
ccall(:jl_readuntil, Ref{String}, (Ptr{Void}, UInt8, UInt8), s.ios, '\n', 1)
end
Expand Down

0 comments on commit b07b6dd

Please sign in to comment.