Skip to content

Commit 9091cb0

Browse files
authored
cleanups to copyuntil (#50485)
* remove duplicate readuntil(s::IO, delim::UInt8) method, some cleanup * more conservative ensureroom in copyuntil * whoops * tweak * test fallbacks
1 parent 9b73611 commit 9091cb0

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

base/io.jl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,7 @@ julia> rm("my_file.txt")
524524
readuntil(filename::AbstractString, delim; kw...) = open(io->readuntil(io, delim; kw...), convert(String, filename)::String)
525525
readuntil(stream::IO, delim::UInt8; kw...) = _unsafe_take!(copyuntil(IOBuffer(sizehint=70), stream, delim; kw...))
526526
readuntil(stream::IO, delim::Union{AbstractChar, AbstractString}; kw...) = String(_unsafe_take!(copyuntil(IOBuffer(sizehint=70), stream, delim; kw...)))
527+
readuntil(stream::IO, delim::T; keep::Bool=false) where T = _copyuntil(Vector{T}(), stream, delim, keep)
527528

528529

529530
"""
@@ -914,6 +915,10 @@ function copyuntil(out::IO, s::IO, delim::AbstractChar; keep::Bool=false)
914915
end
915916

916917
# note: optimized methods of copyuntil for IOStreams and delim::UInt8 in iostream.jl
918+
# and for IOBuffer with delim::UInt8 in iobuffer.jl
919+
copyuntil(out::IO, s::IO, delim; keep::Bool=false) = _copyuntil(out, s, delim, keep)
920+
921+
# supports out::Union{IO, AbstractVector} for use with both copyuntil & readuntil
917922
function _copyuntil(out, s::IO, delim::T, keep::Bool) where T
918923
output! = isa(out, IO) ? write : push!
919924
for c in readeach(s, T)
@@ -925,12 +930,6 @@ function _copyuntil(out, s::IO, delim::T, keep::Bool) where T
925930
end
926931
return out
927932
end
928-
readuntil(s::IO, delim::T; keep::Bool=false) where T =
929-
_copyuntil(Vector{T}(), s, delim, keep)
930-
readuntil(s::IO, delim::UInt8; keep::Bool=false) =
931-
_copyuntil(resize!(StringVector(70), 0), s, delim, keep)
932-
copyuntil(out::IO, s::IO, delim::T; keep::Bool=false) where T =
933-
_copyuntil(out, s, delim, keep)
934933

935934
# requires that indices for target are the integer unit range from firstindex to lastindex
936935
# returns whether the delimiter was matched

base/iostream.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ function readline(s::IOStream; keep::Bool=false)
452452
end
453453

454454
function copyuntil(out::IOBuffer, s::IOStream, delim::UInt8; keep::Bool=false)
455-
ensureroom(out, 16)
455+
ensureroom(out, 1) # make sure we can read at least 1 byte, for iszero(n) check below
456456
ptr = (out.append ? out.size+1 : out.ptr)
457457
d = out.data
458458
len = length(d)

test/read.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -677,6 +677,21 @@ end
677677
@test isempty(itr) # now it is empty
678678
end
679679

680+
@testset "readuntil/copyuntil fallbacks" begin
681+
# test fallback for generic delim::T
682+
buf = IOBuffer()
683+
fib = [1,1,2,3,5,8,13,21]
684+
write(buf, fib)
685+
@test readuntil(seekstart(buf), 21) == fib[1:end-1]
686+
@test readuntil(buf, 21) == Int[]
687+
@test readuntil(seekstart(buf), 21; keep=true) == fib
688+
out = IOBuffer()
689+
@test copyuntil(out, seekstart(buf), 21) === out
690+
@test reinterpret(Int, take!(out)) == fib[1:end-1]
691+
@test copyuntil(out, seekstart(buf), 21; keep=true) === out
692+
@test reinterpret(Int, take!(out)) == fib
693+
end
694+
680695
# more tests for reverse(eachline)
681696
@testset "reverse(eachline)" begin
682697
lines = vcat(repr.(1:4), ' '^50000 .* repr.(5:10), repr.(11:10^5))

0 commit comments

Comments
 (0)